count with xpath

Questions about XML that are not covered by the other forums should go here.
kirstin
Posts: 1
Joined: Thu Mar 02, 2006 5:42 pm

count with xpath

Post by kirstin »

Hi, I have a problem with counting some Xml Elements.
I have an element in the xml structure, which occurs at different positions and different levels. How can I count this element without always starting at a new number, when it occurs in a different Tag

My structure is something like this:
<a>
<b>
<tocount/>
</b>
<b>
<b>
<tocount/>
</b>
<c>
<tocount/>
<c>
</b>
</a>

In my Xslt stylesheet I want get the number of the element, when I match it:

<xsl:template match="tocount">
<xsl:number count='//tocount'/>
</xsl:template>

The result for each element should be an increasing number
<a><b>
1
</b>
<b>
<b>
2
</b>
<c>
3
<c>
</b>
</a>

Thanks for help,
Kirstin
jkmyoung
Posts: 89
Joined: Mon Mar 06, 2006 10:13 pm

Post by jkmyoung »

<xsl:template match="tocount">
<xsl:value-of select="1+count(preceding::tocount)"/>
</xsl:template>
Post Reply