xml/xslt help

Discussion in 'XML' started by Bob, May 6, 2004.

  1. Bob

    Bob Guest

    I am having problems turning my xml into html for display. A sample
    xml file is given below, followed by my pathetic xslt attempt to
    transform it into html. A textual representation of what I was hoping
    to see is last. I am using saxon to do the transformation.

    <?xml version="1.0"?>
    <Whatever>
    <Company>Company One
    <DateRange>21-27Mar2004</DateRange>
    <SubOrg>Division A
    <Data>163
    <Description>A description of 163, Div A, Comp A</Description>
    </Data>
    <Data>201
    <Description>Description of 201, Div. A, Comp A</Description>
    </Data>
    </SubOrg>
    <SubOrg>Division B
    <Data>213
    <Description>More descriptive text.</Description>
    </Data>
    <Data>222
    <Description>Again, more stuff.</Description>
    </Data>
    </SubOrg>
    </Company>
    </Whatever>


    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="/">
    <html>
    <head><title>Title Information</title></head>
    <body bgcolor="white">
    <p align="center"><font size="+3">Report</font></p>
    <xsl:apply-templates/>
    </body>
    </html>
    </xsl:template>
    <xsl:template match="Company">
    <br/>
    <p><font size="+2" color="green"><xsl:value-of
    select="."/></font></p>
    <xsl:apply-templates select="DateRange"/>
    <xsl:apply-templates select="SubOrg"/>
    <xsl:apply-templates select="Data"/>
    </xsl:template>
    <xsl:template match="DateRange">
    <p><font color="blue"><xsl:value-of select="."/></font></p>
    </xsl:template>
    <xsl:template match="SubOrg">
    <br/><font size="+1" color="red"><xsl:value-of
    select="."/></font>
    </xsl:template>
    <xsl:template match="Data">
    <br/><font color="black"><xsl:value-of select="."/></font>
    </xsl:template>
    </xsl:stylesheet>


    What I would like to see is some html that would appear somewhat like
    this:
    Report
    Company One
    21-27Mar2004
    Division A
    163 - A description of 163, Div A, Comp A
    201 - Description of 201, Div. A, Comp A
    Division B
    213 -
    222 -
    Company Two
    07-14Apr2004
    Division A
    128 -
    Division B
    221 -
    234 -

    etc...

    I am using saxon to turn the xml/xslt into html. It's probably
    obvious that I'm not getting the html I wanted.

    Can any provide some help? Thanks in advance!

    Bob.
    Bob, May 6, 2004
    #1
    1. Advertising

  2. (Bob) writes:
    > I am having problems turning my xml into html for display. A sample
    > xml file is given below, followed by my pathetic xslt attempt to
    > transform it into html. A textual representation of what I was hoping
    > to see is last. I am using saxon to do the transformation.
    >
    > <?xml version="1.0"?>
    > <Whatever>
    > <Company>Company One
    > <DateRange>21-27Mar2004</DateRange>
    > <SubOrg>Division A
    > <Data>163
    > <Description>A description of 163, Div A, Comp A</Description>
    > </Data>
    > <Data>201
    > <Description>Description of 201, Div. A, Comp A</Description>
    > </Data>
    > </SubOrg>
    > <SubOrg>Division B
    > <Data>213
    > <Description>More descriptive text.</Description>
    > </Data>
    > <Data>222
    > <Description>Again, more stuff.</Description>
    > </Data>
    > </SubOrg>
    > </Company>
    > </Whatever>


    You've got the right idea, but a couple of comments:

    - the difficulty with your XML is that not every item is wrapped up
    individually as an element. So in

    <Data>163
    <Description>A description of 163, Div A, Comp A</Description>
    </Data>

    the Data element contains two nodes: a text node and a Description
    node. Your <xsl:value-of select="."/> extracts the text of both.
    Extracting just the text node is ugly: you can use text(), but you
    get all the newlines and junk, so I used normalize-space(text()).

    It would be much neater and flexible if you could rewrite this as

    <Data>
    <Title>163</Title>
    <Description>A description of 163, Div A, Comp A</Description>
    </Data>

    for example.

    - You should read up on CSS. <p align="center"><font size="+3">
    Report</font></p> is truly horrible. The modern way is to mark it
    up as <h1> or whatever and use CSS to style it.


    This transformation with your data

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

    <xsl:template match="/">
    <html>
    <head><title>Title Information</title></head>
    <body>
    <h1>Report</h1>
    <xsl:apply-templates/>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="Company">
    <h2><xsl:value-of select="normalize-space(text())"/></h2>
    <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="DateRange">
    <h3><xsl:value-of select="normalize-space(text())"/></h3>
    </xsl:template>

    <xsl:template match="SubOrg">
    <h4><xsl:value-of select="normalize-space(text())"/></h4>
    <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="Data">
    <p>
    <xsl:value-of select="normalize-space(text())"/>
    <xsl:text> - </xsl:text>
    <xsl:value-of select="Description"/>
    </p>
    </xsl:template>

    <!-- Omit text not explicitly dealt with above -->
    <xsl:template match="text()"/>

    </xsl:stylesheet>


    gives


    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Title Information</title>
    </head>
    <body>
    <h1>Report</h1>
    <h2>Company One</h2>
    <h3>21-27Mar2004</h3>
    <h4>Division A</h4>
    <p>163 - A description of 163, Div A, Comp A</p>
    <p>201 - Description of 201, Div. A, Comp A</p>
    <h4>Division B</h4>
    <p>213 - More descriptive text.</p>
    <p>222 - Again, more stuff.</p>
    </body>
    </html>


    Which is approximately what you want.

    --
    Ben Edgington
    Mail to the address above is discarded.
    Mail to ben at that address might be read.
    http://www.edginet.org/
    Ben Edgington, May 7, 2004
    #2
    1. Advertising

  3. Bob

    Bob Guest

    Ben,

    Thanks for the help - it is greatly appreciated. I'm going to give it
    a try this morning.

    Bob.
    Bob, May 7, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    658
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,681
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    623
    Laurens
    Jan 26, 2004
  4. Replies:
    4
    Views:
    672
  5. jkflens
    Replies:
    2
    Views:
    1,468
    jkflens
    May 30, 2006
Loading...

Share This Page