O'Reilly logo

Real World XML by Steven Holzner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling Multiple Selections with xsl:for-each

The select attribute selects only the first node that matches its selection criterion. However, what if you have multiple nodes that could match? For example, say you can have multiple <NAME> elements for each planet:

<PLANET> 
    <NAME>Mercury</NAME>
					<NAME>Closest planet to the sun</NAME>
    <MASS UNITS="(Earth = 1)">.0553</MASS>
    <DAY UNITS="days">58.65</DAY>
    <RADIUS UNITS="miles">1516</RADIUS>
    <DENSITY UNITS="(Earth = 1)">.983</DENSITY>
    <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->
</PLANET>

The <xsl:value-of> element's select attribute by itself selects only the first <NAME> element. To loop over all possible matches, you can use the <xsl:for-each> element, like this:

Listing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required