Print Values in XSLT
-
I have an XML File: 10 11 9 8 8 7 6 9 2 4 6 3 Now I want to print the sales of each car Year wise in a tabular format. Something like a pivot table. If the car does not have sales for a particular year, it should display blank space. The output should be in the format like: Maruti Fort Mercedes 2000 10 8 2 2001 11 - 4 2002 9 7 - 2003 - 6 6 2004 8 9 3 I am not able to print the values particularly. Please help.
-
I have an XML File: 10 11 9 8 8 7 6 9 2 4 6 3 Now I want to print the sales of each car Year wise in a tabular format. Something like a pivot table. If the car does not have sales for a particular year, it should display blank space. The output should be in the format like: Maruti Fort Mercedes 2000 10 8 2 2001 11 - 4 2002 9 7 - 2003 - 6 6 2004 8 9 3 I am not able to print the values particularly. Please help.
Hi, This doesn't seem like a very elegant solution. I'm sure there must be a better way, but I couldn't get there :( Hope this helps.
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="unique-years" select="/ROOT/CAR/SALES[not(@Year=preceding::*/@Year)]/@Year" />
<xsl:variable name="unique-carnames" select="/ROOT/CAR[not(@Name=preceding::*/Name)]/@Name" />
<html>
<body>
<table>
<tr>
<td> </td>
<xsl:for-each select="$unique-carnames">
<xsl:sort select="." />
<td><xsl:value-of select="." /></td>
</xsl:for-each>
</tr>
<xsl:for-each select="$unique-years">
<xsl:sort select="." />
<tr>
<xsl:variable name="this-year" select="."/>
<td><xsl:value-of select="$this-year" /></td>
<xsl:for-each select="$unique-carnames">
<xsl:sort select="." />
<xsl:variable name="this-car" select="."/>
<td><xsl:value-of select="/ROOT/CAR[@Name=$this-car]/SALES[@Year=$this-year]" /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>Cheers Phil Hobgen barbari.co.uk Southampton, UK
-
Hi, This doesn't seem like a very elegant solution. I'm sure there must be a better way, but I couldn't get there :( Hope this helps.
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="unique-years" select="/ROOT/CAR/SALES[not(@Year=preceding::*/@Year)]/@Year" />
<xsl:variable name="unique-carnames" select="/ROOT/CAR[not(@Name=preceding::*/Name)]/@Name" />
<html>
<body>
<table>
<tr>
<td> </td>
<xsl:for-each select="$unique-carnames">
<xsl:sort select="." />
<td><xsl:value-of select="." /></td>
</xsl:for-each>
</tr>
<xsl:for-each select="$unique-years">
<xsl:sort select="." />
<tr>
<xsl:variable name="this-year" select="."/>
<td><xsl:value-of select="$this-year" /></td>
<xsl:for-each select="$unique-carnames">
<xsl:sort select="." />
<xsl:variable name="this-car" select="."/>
<td><xsl:value-of select="/ROOT/CAR[@Name=$this-car]/SALES[@Year=$this-year]" /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>Cheers Phil Hobgen barbari.co.uk Southampton, UK
Thanks a lot Phil. It works.