Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using xsl:for-each tag in xsl file 1

Status
Not open for further replies.

munnanext

Programmer
Aug 20, 2004
49
US
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="<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.
 
Code:
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"
xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<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>
</tr>
<xsl:for-each select="children">
<tr>
<td><xsl:value-of select="name"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Jon thanks for your reply.

I tried to execute the way you have written. What is happening is the children names are coming in the next row completely.But I don't want the children's name to be coming in the next row. what i wanted to have is to display children's name in the same row as you get the name of the parent but instead of appearing all the names in the same line, after the first child name, the second one should appear in the next line. right across the children column

Thanks
 
Slight Mod...
Try:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"
xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<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>
[B]<xsl:for-each select="children">
<td><xsl:value-of select="name"/></td>
</xsl:for-each>[/B]
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Or...

<td>
<xsl:for-each select="children">
<xsl:value-of select="name"/>
</xsl:for-each>
</td>



Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Thank you CubeE101 for your reply. I modified my code as you said, but it does not solve my problem either. All the children names are coming in the same line.


Thanks
 
Hi Everyone,

Is there any other way that i can get the output as
Code:
name   age gender  children
----------------------------
Suresh 30  Male   Sruthi
                  Sraavya
                  Sravanthi 

Nitin  38  Male   shanthi
                  sheetal 
                  vidya 
                  kaavya
                  Neeta
without using xsl:for-each tag
 
Sure...

Try this:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:template match="/">
 <html>
  <body>
   <table>
    <tr>
     <th>name</th>
     <th>age</th>
     <th>gender</th>
     <th>children</th>
    </tr>
    <xsl:for-each select="//Family">
     <xsl:variable name="Family" select="." />
     <xsl:for-each select="children">
      <tr>
       <td><xsl:if test="position() = 1"><xsl:value-of select="$Family/name"/></xsl:if></td>
       <td><xsl:if test="position() = 1"><xsl:value-of select="$Family/age"/></xsl:if></td>
       <td><xsl:if test="position() = 1"><xsl:value-of select="$Family/gender"/></xsl:if></td>
       <td><xsl:value-of select="name"/></td>
      </tr>
     </xsl:for-each>
    </xsl:for-each>
   </table>
  </body>
 </html>
</xsl:template>
</xsl:stylesheet>

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
*note: the case of the tags have to match...

I copied your XML above and pasted it into a root in a file...

the family tags were <Family> instead of <family>...
So I changed the <xsl:for-each> from details/family to //Family

You can change the case to match, and fully qualify the element as needed...

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top