0

I have XML file having structure is as follows-

<products>
 <product>
   <ptype>fruits</ptype>
   <varieties>
     <variety>
       <id>a</id>
       <cost>100</cost>
     </variety>
     <subvarieties>
       <variety>
         <id>b</id>
         <cost>100</cost>
       </variety>
       <subvarieties>
         <variety>
           <id>c</id>
           <cost>100</cost>
         </variety> 
       </subvarieties>
       </variety>
     </subvarieties>
     <variety>
       <id>d</id>
       <cost>75</cost>
     </variety>
   </varieties>
 </product>
 <product>
   <type>vegetables</type>
   <varieties>
     <variety>
       <id>e</id>
       <cost>50</cost>
     </variety>
   </varieties>
 </product>

I need to restructure the above xml into HTML tabular format based on node <variety>. It means irrespective of the position of the node <variety> in XML, I need to select the elements under that node. Sometime the node might be empty i.e no elements under the node. Desired XML is as follows -

<html>
<body>
<table border="1">
     <tr>
        <td>a</td>
        <td>100</td>
     </tr>
     <tr>
        <td>b</td>
        <td>100</td>
     </tr>
     <tr>
        <td>c</td>
        <td>100</td>
     </tr>
     <tr>
        <td>d</td>
        <td>75</td>
     </tr>
     <tr>
        <td>e</td>
        <td>50</td>
     </tr>
</table>
</body>
</html>

Attempted XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
    <xsl:for-each select="/products">
        <xsl:for-each select="//variety">
            <tr>
                <td><xsl:value-of select="." /></td>
            </tr>
        </xsl:for-each>
    </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

I am getting an empty response for the above XSLT. Any help would be a great plus.

1 Answer 1

1

Works for me:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
    <xsl:for-each select="/products">
        <xsl:for-each select=".//variety">
            <tr>
                <td><xsl:value-of select="id" /></td>
                <td><xsl:value-of select="cost" /></td>
            </tr>
        </xsl:for-each>
    </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

I have an empty node for node <variety>. In the response it should not return an empty tag. Also in my original XML, under <variety> i have a few elements having same name. So i choose the format <td><xsl:value-of select="." /></td>
I still don't understand your question. Please post a valid XML example that has all these special cases you mentioned and the expected output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.