INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Nested for-each

Nested for-each

(OP)
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

RE: Nested for-each

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

RE: Nested for-each

(OP)
Thanks, that's done it. Figured it'd be something like that but couldn't get the synatax right for getting the parent attribute.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close