Nested foreach loop Urgent
-
Hi all My XML is like this <Compare> <columnNames> <column> <na>Name</na> </column> <column> <na>Address</na> </column> <column> <na>Phone</na> </column> </columnNames> <Hotels> <Hotel> <Name>Hotel Mount View</Name> <Address>123</Address> <Phone>1234</Phone> </Hotel> <Hotel> <Name>Hotel Mayur</Name> <Address>Adress1</Address> <Phone>12345678</Phone> </Hotel> </Hotels> </Compare> My requirement is that the na items in column specify the name and count of the child items in Hotel. I have written the xsl like this <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <table> <xsl:for-each select="Compare/columnNames/column"> <tr> <xsl:variable name="ColName" select="na"></xsl:variable> <td> <xsl:value-of select="na"/> </td> <xsl:for-each select="Compare/Hotels/Hotel"> <td> <xsl:value-of select="Name"/> </td> </xsl:for-each> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet> but its showing the result properly.Need your help?
-
Hi all My XML is like this <Compare> <columnNames> <column> <na>Name</na> </column> <column> <na>Address</na> </column> <column> <na>Phone</na> </column> </columnNames> <Hotels> <Hotel> <Name>Hotel Mount View</Name> <Address>123</Address> <Phone>1234</Phone> </Hotel> <Hotel> <Name>Hotel Mayur</Name> <Address>Adress1</Address> <Phone>12345678</Phone> </Hotel> </Hotels> </Compare> My requirement is that the na items in column specify the name and count of the child items in Hotel. I have written the xsl like this <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <table> <xsl:for-each select="Compare/columnNames/column"> <tr> <xsl:variable name="ColName" select="na"></xsl:variable> <td> <xsl:value-of select="na"/> </td> <xsl:for-each select="Compare/Hotels/Hotel"> <td> <xsl:value-of select="Name"/> </td> </xsl:for-each> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet> but its showing the result properly.Need your help?
This XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table>
<xsl:for-each select="Compare/columnNames/column">
<tr>
<xsl:variable name="ColName" select="na"></xsl:variable>
<td>
<xsl:value-of select="na"/>
</td>
<xsl:for-each select="///Compare/Hotels/Hotel">
<td>
<xsl:value-of select="./*[name()=$ColName]"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>produces this HTML fragment - is that what you were after?
<?xml version="1.0"?>
<table>
<tr>
<td>Name</td>
<td>Hotel Mount View</td>
<td>Hotel Mayur</td>
</tr>
<tr>
<td>Address</td>
<td>123</td>
<td>Adress1</td>
</tr>
<tr>
<td>Phone</td>
<td>1234</td>
<td>12345678</td>
</tr>
</table>Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p