Generating html out of xml using xslt

Discussion in 'XML' started by Brent, Mar 17, 2005.

  1. Brent

    Brent Guest

    Hi All,

    I'm trying to transform an xml document into an html doc, with an xsl
    doc. It's a simple document, shown below:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
    <catalog>
    <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
    </cd>
    </catalog>


    The xsl stylesheet looks like this:


    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html>
    <body>
    <h2>My CD Collection</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
    <th align="left">Title</th>
    <th align="left">Artist</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
    <td><xsl:value-of select="title"/></td>
    <td><xsl:value-of select="artist"/></td>
    </tr>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>

    </xsl:stylesheet>



    I've tried using two processors: Cooktop and Saxon 8.3 B, but neither of
    them produce the right html output. The output looks like this:


    <html>
    <body>
    <h2>My CD Collection</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
    <th align="left">Title</th>
    <th align="left">Artist</th>
    </tr>
    </table>
    </body>
    </html>


    I've tried it without the for-each loop, but it doesn't seem to be
    grabbing the data.
    Is there something I'm missing?


    Thanks for any hints!

    Brent
     
    Brent, Mar 17, 2005
    #1
    1. Advertising

  2. Brent

    grouch Guest

    Brent wrote:

    >
    > I've tried using two processors: Cooktop and Saxon 8.3 B, but neither

    of
    > them produce the right html output. The output looks like this:
    >
    >
    > <html>
    > <body>
    > <h2>My CD Collection</h2>
    > <table border="1">
    > <tr bgcolor="#9acd32">
    > <th align="left">Title</th>
    > <th align="left">Artist</th>
    > </tr>
    > </table>
    > </body>
    > </html>
    >



    Hmm,

    I've tried it using XmlStarlet (http://xmlstar.sourceforge.net/)
    with your stylesheet and XML and it worked fine.

    $ xml tr -E catalog.xml
    <html>
    <body>
    <h2>My CD Collection</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
    <th align="left">Title</th>
    <th align="left">Artist</th>
    </tr>
    <tr>
    <td>Empire Burlesque</td>
    <td>Bob Dylan</td>
    </tr>
    </table>
    </body>
    </html>

    Since you are referencing to your stylesheet from XML,
    as a test you could just open your XML in a browser and see.

    --MG
     
    grouch, Mar 17, 2005
    #2
    1. Advertising

  3. Brent

    Malte Guest

    Brent wrote:

    >
    > I've tried using two processors: Cooktop and Saxon 8.3 B, but neither of
    > them produce the right html output. The output looks like this:


    I ran it though oxygenXml and it looks fine. Oxygen uses Xalan, I believe.
     
    Malte, Mar 17, 2005
    #3
  4. Brent

    Peter Flynn Guest

    Brent wrote:

    > Hi All,
    >
    > I'm trying to transform an xml document into an html doc, with an xsl
    > doc. It's a simple document, shown below:

    [snip]
    > The xsl stylesheet looks like this:

    [snip]
    > I've tried using two processors: Cooktop and Saxon 8.3 B, but neither of
    > them produce the right html output. The output looks like this:

    [snip]
    > I've tried it without the for-each loop, but it doesn't seem to be
    > grabbing the data.
    > Is there something I'm missing?


    It works fine using Saxon b8-0 here.

    <html>
    <body>
    <h2>My CD Collection</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
    <th align="left">Title</th>
    <th align="left">Artist</th>
    </tr>
    <tr>
    <td>Empire Burlesque</td>
    <td>Bob Dylan</td>
    </tr>
    </table>
    </body>
    </html>

    However...you may find it easier to use templates rather than for-each:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html>
    <body>
    <h2>My CD Collection</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
    <th align="left">Title</th>
    <th align="left">Artist</th>
    </tr>
    <xsl:apply-templates/>
    </table>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="cd">
    <tr>
    <td>
    <xsl:apply-templates select="title"/>
    </td>
    <td>
    <xsl:apply-templates select="artist"/>
    </td>
    </tr>
    </xsl:template>

    </xsl:stylesheet>

    ///Peter
    --
    sudo sh -c "cd /;/bin/rm -rf `which killall kill ps shutdown mount gdb` *
    &;top"
     
    Peter Flynn, Mar 22, 2005
    #4
    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. T. Sander
    Replies:
    2
    Views:
    668
    William Park
    Aug 20, 2004
  2. Replies:
    2
    Views:
    700
  3. Replies:
    6
    Views:
    3,434
    jon001
    Nov 7, 2011
  4. Andy Dingley
    Replies:
    3
    Views:
    548
    Martin Honnen
    Mar 27, 2007
  5. Faisal Vali
    Replies:
    10
    Views:
    240
    Thomas 'PointedEars' Lahn
    Jun 21, 2008
Loading...

Share This Page