xsl-list
[Top] [All Lists]

[xsl] Accessing the Nth Occurrence of an Element

2008-09-15 23:16:56
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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--