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 ...

Get Real World XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.