Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: xsl not working

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    406
    Thanks
    0
    Thanked 0 Times in 0 Posts

    xsl not working

    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>
    Last edited by chelvis; 03-12-2003 at 09:42 PM.

  • #2
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts
    Try REL="stylesheet" (lowercase). It may make a difference.

    Bear in mind XSLT is not my strong point.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by brothercake; 03-13-2003 at 12:58 AM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Newcastle, England
    Posts
    178
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
    }
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Newcastle, England
    Posts
    178
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Brothercake, I forgot to mention that.

    patrick


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •