XML XSLT Table display

Discussion in 'XML' started by Pradeep, Jan 7, 2008.

  1. Pradeep

    Pradeep Guest

    I have a following XML file
    <ITEMLIST>
    <ITEM>
    <NAME> Item1 </ITEM>
    <PRICE> 500 </PRICE>
    </ITEM>
    <ITEM>
    <NAME> Item2 </ITEM>
    <PRICE> 600 </PRICE>
    <LOCATION> XYZ</LOCATION>
    </ITEM>
    <ITEM>
    <NAME> Item3 </ITEM>
    <PRICE> 700 </PRICE>
    <QTY> 25 </QTY>
    </ITEM>
    <ITEM>
    <NAME> Item4 </ITEM>
    <PRICE> 900 </PRICE>
    <QTY> 90 </QTY>
    </ITEM>
    </ITEMLIST>

    and want to display in HTML like following

    -------------------------------------------
    | | PRICE | LOCATION | QTY |
    -------------------------------------------
    Item1 | 500 | | |
    -------------------------------------------
    Item1 | 600 | XYZ | |
    -------------------------------------------
    Item1 | 700 | | 25 |
    -------------------------------------------
    Item1 | 900 | | 90 |
    -------------------------------------------


    Is it possible via XSLT to do this ...If yes, kindly provide a small
    code snippet to do this
    Pradeep, Jan 7, 2008
    #1
    1. Advertising

  2. Pradeep wrote:
    > I have a following XML file
    > <ITEMLIST>
    > <ITEM>
    > <NAME> Item1 </ITEM>

    ^^^^^
    That is not even well-formed.

    > <PRICE> 500 </PRICE>
    > </ITEM>
    > <ITEM>
    > <NAME> Item2 </ITEM>



    > and want to display in HTML like following
    >
    > -------------------------------------------
    > | | PRICE | LOCATION | QTY |
    > -------------------------------------------
    > Item1 | 500 | | |
    > -------------------------------------------
    > Item1 | 600 | XYZ | |
    > -------------------------------------------
    > Item1 | 700 | | 25 |
    > -------------------------------------------
    > Item1 | 900 | | 90 |
    > -------------------------------------------


    Why always "Item1"? Assuming you want the NAME element output then the
    following XSLT 1.0 stylesheet should help:

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

    <xsl:eek:utput method="html" indent="yes"/>

    <xsl:template match="/">
    <html lang="en">
    <head>
    <title>Example</title>
    </head>
    <body>
    <xsl:apply-templates select="ITEMLIST"/>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="ITEMLIST">
    <table border="1">
    <thead>
    <tr>
    <th> </th>
    <th>PRICE</th>
    <th>LOCATION</th>
    <th>QTY</th>
    </tr>
    </thead>
    <xsl:apply-templates select="ITEM"/>
    </table>
    </xsl:template>

    <xsl:template match="ITEM">
    <tr>
    <td><xsl:value-of select="NAME"/></td>
    <td><xsl:value-of select="PRICE"/></td>
    <td>
    <xsl:choose>
    <xsl:when test="LOCATION">
    <xsl:value-of select="LOCATION"/>
    </xsl:when>
    <xsl:eek:therwise> </xsl:eek:therwise>
    </xsl:choose>
    </td>
    <td>
    <xsl:choose>
    <xsl:when test="QTY">
    <xsl:value-of select="QTY"/>
    </xsl:when>
    <xsl:eek:therwise> </xsl:eek:therwise>
    </xsl:choose>
    </td>
    </tr>
    </xsl:template>

    </xsl:stylesheet>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 7, 2008
    #2
    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:
    662
    Stylus Studio
    Aug 3, 2004
  2. Replies:
    4
    Views:
    674
  3. Pradeep

    XML XSLT Table display

    Pradeep, Jan 7, 2008, in forum: HTML
    Replies:
    1
    Views:
    339
  4. Pradeep

    XML XSLT Table display

    Pradeep, Jan 7, 2008, in forum: XML
    Replies:
    0
    Views:
    410
    Pradeep
    Jan 7, 2008
  5. Pradeep

    XML XSLT Table display

    Pradeep, Jan 7, 2008, in forum: XML
    Replies:
    0
    Views:
    364
    Pradeep
    Jan 7, 2008
Loading...

Share This Page