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

Looping through multiple elements

Status
Not open for further replies.

danylo13

Programmer
May 23, 2007
7
CA
Hi,

I'm new to XML and what I have an XML document with the following strucure:
<TOP>
<Header>
<Subheader>
...
<Subheader>
</Header>
...
<Header>
<Subheader>
...
<Subheader>
</Header>
</TOP>
What I'm trying to do is loop through each header and print out all the subheaders it has.
If I use <xsl:for-each select="TOP/Header"> then <xsl:value-of select="subheader"/> it only prints out the first subheader. How do I get it to print all the subheaders?
Also if I have a name associated with each Header, how do I get to print those out too?
thanks.
 
Please post an actual sample input (XML) document and your current XSL.

It would appear to me that you need to use something like:
Code:
<xsl:for-each select="TOP/Header>
    <xsl:for-each select="Subheader">
        <xsl:value-of select="."/>
    </xsl:for-each>
</xsl:for-each>

xsl:value-of evaluates a node set using the rules of the string() function, which in turn says that it returns the value of the first node in a nodeset.



Tom Morrison
 
Hi,

that works for the subheader part. But for header what happens when I use the "." is that it prints out the header's name plus all of its subheader's names.
Code:
          <xsl:for-each select="Page/TOC/Header">
            <tr>
              <td><xsl:value-of select="." /></td>
            </tr>
            <xsl:for-each select="Subheader">
              <tr>
                <td>
                  <xsl:value-of select="." />
                </td>
              </tr>
            </xsl:for-each>
          </xsl:for-each>
How do I get to print what is in the header only?
Right now my table looks like:
IntroductionPurposeOverviewScopeTarget AudienceDocument OrganizationList of Acronyms
Purpose
Overview
Scope
Target Audience
Document Organization
List of Acronyms
 
here is the sample for my xml..

<Page>
<TOC>
<Header>Introduction
<Subheader>Purpose</Subheader>
<Subheader>Overview</Subheader>
<Subheader>Scope</Subheader>
<Subheader>Target Audience</Subheader>
<Subheader>Document Organization</Subheader>
<Subheader>List of Acronyms</Subheader>
</Header>
<Header>Quick Tour
<Subheader>Tour</Subheader></Header>
</TOC>
</Page>
and there can be more headers.. and as many subheaders as you want for each header
 
Try this:
Code:
          <xsl:for-each select="Page/TOC/Header">
            <tr>
              <td><xsl:value-of select="[b][COLOR=blue]text()[/color][/b]" /></td>
            </tr>
            <xsl:for-each select="Subheader">
              <tr>
                <td>
                  <xsl:value-of select="." />
                </td>
              </tr>
            </xsl:for-each>
          </xsl:for-each>

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top