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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath of nested for-each

Status
Not open for further replies.

minasharif

Programmer
Oct 14, 2006
5
Hi;
I am going to transform a relational xml by xslt using nested for-each. The xml document is such as below (I may not change this structure!):

<root>
<parent>
<pid>1</pid>
<name>John</name>
<family>Smith</family>
.
.
.
</parent>
<parent>
<id>2</id>
<name>David</name>
<family>Green</family>
.
.
.
</parent>
.
.
.
<child>
<id>1</id>
<lesson>Mathematics</lesson>
<mark>18</mark>
.
.
.
</child>
<child>
<id>1</id>
<lesson>Physics</lesson>
<mark>15</mark>
.
.
.
</child>
<child>
<id>1</id>
<lesson>Chemistry</lesson>
<mark>17</mark>
.
.
.
</child>
<child>
<id>2</id>
<lesson>Mathematics</lesson>
<mark>9</mark>
.
.
.
</child>
<child>
<id>2</id>
<lesson>Physics</lesson>
<mark>11</mark>
.
.
.
</child>
<child>
<id>2</id>
<lesson>Chemistry</lesson>
<mark>12</mark>
.
.
.
</child>
.
.
.
</root>


And my xslt:

<xsl:for-each select="/root/parent">

ID: <xsl:value-of select="pid" /><br>
Name: <xsl:value-of select="name" /><br>
Family: <xsl:value-of select="family" /><br>

<xsl:for-each select="/root/child[id=pid]">
<xsl:value-of select="lesson" />:
<xsl:value-of select="mark" /><br>
</xsl:for-each>

</xsl:for-each>

But unfortunately, this does not work properly. The main problem is from the xpath command of interior for-each.
I am much obliged to you for your great attention.

Regards
 
This is how, with double for-each approach.
[tt]
<xsl:for-each select="/root/parent">
ID: <xsl:value-of select="pid" /><br />
Name: <xsl:value-of select="name" /><br />
Family: <xsl:value-of select="family" /><br />
<xsl:for-each select="./../child[id=current()/pid]">
<xsl:value-of select="lesson" />:
<xsl:value-of select="mark" /><br />
</xsl:for-each>
</xsl:for-each>
[/tt]
Notes:
[1] You've to close <br /> in xslt even it does not need html.
[2] You have a typo for (p)id=2 parent.
 
Thanks for your reply; it was great and useful...

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top