...

xsl not working

chelvis
03-12-2003, 09:40 PM
I have 3 files (xml, xsl and a .css). I have pasted the codes below for these. When I go to the browser and view the hamlet.xml file, I see every thing as one paragraph. It seems like the .css file is not being used by the xsl or xml. How can I make this look like the way I want (specified in the .css)?

Hamlet.xml
<?xml version="1.0"?>

<?xml-stylesheet type="text/xsl" href="Hamlet.xsl"?>
<EXTRACT>
<ACT><TITLE>ACT I</TITLE>

<SCENE><TITLE>SCENE I. Elsinore. A platform before the castle.</TITLE>
<STAGEDIR>FRANSICO at his post. Enter to him BERNARDO</STAGEDIR>

<SPEECH>
<SPEAKER>BERNARDO</SPEAKER>
<LINE>Who's there?</LINE>
</SPEECH>

<SPEECH>
<SPEAKER>FRANSICO</SPEAKER>
<LINE>Nay, answer me: stand, and unfold yourself.</LINE>
</SPEECH>

<SPEECH>
<SPEAKER>BERNARDO</SPEAKER>
<LINE>Long live the king!</LINE>
</SPEECH>
</SCENE>
</ACT>
</EXTRACT>


Hamlet.css
ACT TITLE {
display:block;
font-size:10pt;
color:red;
font-weight:bold;
margin-bottom:12pt;
}

SCENE TITLE {
display:block;
font-size:12pt;
color:green;
margin-bottom:6pt;
}

STAGEDIR {
display:block;
font-style:italic;
margin-top:6pt;
margin-bottom:6pt;
}

SPEAKER {
display:block;
}

LINE {
display:block;
margin-left:2em;
}

Hamlet.xsl
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="*|/"><xsl:apply-templates/>
</xsl:template>

<xsl:template match="EXTRACT">
<HTML>
<HEAD>
<TITLE>Hamlet</TITLE>
<LINK REL="StyleSheet" type="text/css" HREF="Hamlet.css" />
</HEAD>
<BODY>
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="ACT/TITLE">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="SCENE/TITLE">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="STAGEDIR">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="SPEAKER">
<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="LINE">
<xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>

Alex Vincent
03-13-2003, 12:32 AM
Try REL="stylesheet" (lowercase). It may make a difference.

Bear in mind XSLT is not my strong point.

brothercake
03-13-2003, 12:54 AM
What you're doing there is parsing the XML into an HTML framework, but you're not actually turning it into HTML - you're parsing it literally into nodes of the same name.

That's why it doesn't work - even though it is still an XML document (because there's no specified <xsl:output/>, and "xml" is the default), the <html> tag in the output document invokes the browser's HTML parser, which doesn't understand any of the tags.

I think that's the right explanation!

There are two solutions I can think of:


- apply your CSS stylesheet directly to the XML document, like this:

<?xml-stylesheet type="text/css" href="hamlet.css"?>


- use your XSL stylesheet to parse the XML nodes into HTML nodes , and apply the CSS stylesheet to that.

mpjbrennan
03-13-2003, 08:52 AM
The reason you are not getting any formatting is because you are outputting text nodes which contain the content of the matching nodes in the xml document, but they do not have class attributes. Hence they cannot pick up style information from the css stylesheet. To overcome this you need to use an xsl template like the following (using STAGEDIR as an example)

<xsl:template match="STAGEDIR">
<p class="STAGEDIR">
<xsl:value-of select="."/>
</p>
</xsl:template>

Alternatively you can do as Brothercake suggests and apply the css styling directly to the xml document via a processing instruction. In this case your xsl stylesheet would be superfluous.

patrick

brothercake
03-13-2003, 09:32 AM
Originally posted by mpjbrennan
To overcome this you need to use an xsl template like the following (using STAGEDIR as an example)

<xsl:template match="STAGEDIR">
<p class="STAGEDIR">
<xsl:value-of select="."/>
</p>
</xsl:template>

In this case you'd also need small modifications to your CSS, to apply the rules to classnames instead of element names, eg:

.STAGEDIR {
display:block;
font-style:italic;
margin-top:6pt;
margin-bottom:6pt;
}

mpjbrennan
03-13-2003, 09:52 AM
Thanks Brothercake, I forgot to mention that.

patrick



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum