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

Nested for-each 1

Status
Not open for further replies.

pjc791

Programmer
Aug 13, 2011
2
Hello,

I've got a nested for-each function in an XSL file, designed to run through a XML containing R's and C's (rows and Columns) and making a table out of the XML info. The code looks something like this with a nested for-each

<xsl:for-each select="r">
<tr>
<xsl:for-each select="c">
<td> - stuff - </td>

I need to pull attribute information from the partent r inside the <td> generated during the c element but I can't see to get the code correct.

Wondering if I should make a single element for each cell and somehow filter them out by attributes, or perhaps just including all the information in the c element that I need.

Sorry if this isn't clear but I don't have the code to post in full at the moment
 
Sometimes the switching of the context within a for-each can confuse, or at least obscure how to do something. Inside the <xsl:for-each select="c"> the context is the <c> element, so how do you refer to something on the containign <r> element?. From what you have posted, the <c> element(s) are the children of the <r> element. So let's say you have:
Code:
<r someattr="123">
<c/> <c/> <c/>
</r>
...

You can get to the attribute on the parent <r> as follows:
Code:
<xsl:for-each select="r">
 <tr>
 <xsl:for-each select="c">
 <td><xsl:value-of select="../@someattr"/></td>

The '..' is a quick way in XPath to get to the parent node of the context node. So ../@someattr means, "the attribute someattr of my parent."

Tom Morrison
Micro Focus
 
Thanks, that's done it. Figured it'd be something like that but couldn't get the synatax right for getting the parent attribute.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top