It's not a secret anymore!*
The moose likes XML and Related Technologies and the fly likes using xsl:for-each tag in  xsl file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "using xsl:for-each tag in  xsl file" Watch "using xsl:for-each tag in  xsl file" New topic
Author

using xsl:for-each tag in xsl file

vishme vish
Greenhorn

Joined: Jun 07, 2005
Posts: 7
Hi Everyone,
I am new to XML and XSLT and I am learing them.

If for a particular column we have sub rows then how can we get to show them in an html page

For example, consider these four columns..

name age gender children


in my xml i have tags as
<Family>
<name>Suresh</name>
<age>30</age>
<gender>Male</gender>
<children>
<name> Sruthi</name>
<age> 8</age>
<gender> Female</gender>
</children>
<children>
<name>Sraavya</name>
<age>4 </age>
<gender> Female</gender>
</children>
<children>
<name>Sravanthi</name>
<age>2 </age>
<gender>Female </gender>
</children>
</Family>
<Family>
<name>Nitin</name>
<age>38</age>
<gender>Male</gender>
<children>
<name> Shanthi</name>
<age> 15</age>
<gender> Female</gender>
</children>
<children>
<name>Sheetal</name>
<age>12 </age>
<gender> Female</gender>
</children>
<children>
<name>Vidya</name>
<age>9</age>
<gender>Female </gender>
</children>
<children>
<name>kaavya</name>
<age>6 </age>
<gender> Female</gender>
</children>
<children>
<name>Neeta</name>
<age>3</age>
<gender>Female </gender>
</children>
</Family>

The output in the html file after formatting in the xsl file Should be as

name age gender children

Suresh 30 Male Sruthi
Sraavya
Sravanthi

Nitin 38 Male shanthi
sheetal
vidya
kaavya
Neeta
Please note that the children names appear one after the other in the next line below the children column

And my xsl file is as follows:

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="details/family">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="age"/></td>
<td><xsl:value-of select="gender"/></td>
<xsl:for-each select="children">
<td><xsl:value-of select="name"/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

When i am doing it, i am getting the output as,

name age gender children

Suresh 30 Male sruthi sraavya Sravanthi
Nitin 38 Male shanthi sheetal vidya kaavya Neeta

Here i am getting the children names in the same line.

Can anyone Pl explain how to get the desired output

Thanks in Advance.
[ June 07, 2005: Message edited by: vishme vish ]
Dave Lenton
Ranch Hand

Joined: Jan 20, 2005
Posts: 1241
OK, so you want each of the children to be on a separate line? The first thing you need to do is to stop the parents from being in the same table row as the children, so you'll need to end the table row after the parent row. Next you want the children to each appear on their own row, so try wrapping their cell elements in row elements. Thirdly you wanted the children to appear in the third "children" column. One way to do that is to add some empty cells for the other two columns.

To put it all together, try replacing:

with

[ June 08, 2005: Message edited by: Dave Lenton ]

There will be glitches in my transition from being a saloon bar sage to a world statesman. - Tony Banks
vishme vish
Greenhorn

Joined: Jun 07, 2005
Posts: 7
Thanks Dev for the reply.

I could able to get the desired output.

Thanks
Vishme
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: using xsl:for-each tag in xsl file