I have XML file having structure like this-
<products>
<product>
<id>1</id>
<type>fruits</type>
<varieties>
<variety>
<id>a</id>
<cost>100</cost>
<availability>Y</availability>
</variety>
<variety>
<id>b</id>
<cost>75</cost>
<availability>N</availability>
</variety>
</varieties>
</product>
<product>
<id>2</id>
<type>vegetables</type>
<varieties>
<variety>
<id>c</id>
<cost>50</cost>
<availability>Y</availability>
</variety>
<variety>
<id>d</id>
<cost>55</cost>
<availability>Y</availability>
</variety>
</varieties>
</product>
</products>
I want to restructure the given XML based on products/product/id. The expected output would be as follows-
<html>
<body>
<table border="1">
<tr>
<td>1</td>
<td>fruits</td>
<td>a</td>
<td>100</td>
<td>Y</td>
</tr>
<tr>
<td>1</td>
<td>fruits</td>
<td>b</td>
<td>75</td>
<td>N</td>
</tr>
<tr>
<td>2</td>
<td>vegetables</td>
<td>c</td>
<td>50</td>
<td>Y</td>
</tr>
<tr>
<td>2</td>
<td>vegetables</td>
<td>d</td>
<td>55</td>
<td>Y</td>
</tr>
</table>
</body>
</html>
I have tried this XSLT but it is not returning the expected xml.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<products>
<product>
<varieties>
<xsl:apply-templates select="products/product/varieties/variety" />
</varieties>
</product>
</products>
</xsl:template>
<xsl:template match="products" >
<html>
<body>
<table border="1">
<xsl:for-each select="products/product">
<tr>
<td><xsl:value-of select="id" /></td>
<td><xsl:value-of select="type" /></td>
<xsl:for-each select="varieties/variety" >
<td><xsl:value-of select="id" /></td>
<td><xsl:value-of select="cost" /></td>
<td><xsl:value-of select="availability" /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
variety. You have a template matchingroot- but there is no element namedroot. And the contents of the template matching/(which is the only template that gets executed) does not match your intended output. - michael.hor257kvarietydoes not matchVariety. - michael.hor257kvariety, and use<xsl:value-of select="../../id" />and<xsl:value-of select="../../type"/>to get the ancestor product's id and type respectively. - michael.hor257k