[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Accessing the Nth Occurrence of an Element


Subject: [xsl] Accessing the Nth Occurrence of an Element
From: "mike leonard" <mikeleonard@xxxxxxxxx>
Date: Mon, 15 Sep 2008 22:16:28 -0500

Hello,

I've been searching for a solution to what I thought was a simple
problem. Given the input XML:

----------------------------------
<forest>
<monkey name="Joe" />
<tree><monkey name="Sam" /></tree>
<tree><monkey name="George" /></tree>
<tree><monkey name="Frank" /></tree>
<tree><treehouse><monkey name="Phil" /></treehouse></tree>
<tree><monkey name="Hans" /></tree>
</forest>
----------------------------------

I want to get the name of the fifth monkey (Phil). I thought this would do it:

----------------------------------
<xsl:template match="forest">
<xsl:text>The fifth monkey's name is: </xsl:text><xsl:value-of
select="//monkey[5]/@name"/>
</xsl:template>
----------------------------------

But this doesn't seem to work. It does work fine if all the monkeys
are children of the root element, but I can't count on that always
being the case. Can anyone help me with my monkey trouble?

Thank you in advance,

Mike Leonard


Current Thread