Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Nested foreach loop Urgent

Nested foreach loop Urgent

Scheduled Pinned Locked Moved XML / XSL
xmlregexhelpquestionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bbm2k3
    wrote on last edited by
    #1

    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?

    S 1 Reply Last reply
    0
    • B bbm2k3

      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?

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups