XML XSLT Table display

P

Pradeep

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
 
M

Martin Honnen

Pradeep said:
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

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top