Hi,
Using <xsl:for-each> and sorting method of finding a max, something like...
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="max-A">
<xsl:for-each select="//A">
<xsl:sort select="count(.| ancestor::A)" data-type="number"
order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="count(.| ancestor::A)"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:text>Max nesting of <A> is: </xsl:text>
<xsl:value-of select="$max-A"/>
</xsl:template>
</xsl:stylesheet>
Using recursion, something like...
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="max-A">
<xsl:apply-templates select="(//A)[1]"/>
</xsl:variable>
<xsl:text>Max nesting of <A> is: </xsl:text>
<xsl:value-of select="$max-A"/>
</xsl:template>
<xsl:template match="A">
<xsl:param name="max" select="0"/>
<xsl:variable name="this-max" select="count(. | ancestor::A)"/>
<xsl:variable name="new-max" select="($max * number($this-max <= $max))
+ ($this-max * number($this-max > $max))"/>
<xsl:choose>
<xsl:when test="following::A | descendant::A">
<xsl:apply-templates select="(following::A | descendant::A)[1]">
<xsl:with-param name="max" select="$new-max"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$new-max"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
HTH
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
"KemperR" <Ke*****@t-online.de> wrote in message
news:90**************************@posting.google.c om...
Dear All,
may be some of you can help me with an XSLT example how to solve the
following challange.
For the XML below I want to find out the maximum hierarchy level for a
specific element in my XSLT. The result for the example (searching for
<A/>) should be 4 as the element A is nested 4 times maximum.
I guess I have to use somehow the count function with 'following::A'
axes. But I could not get that to work yet.
Thanks a lot for your help
Rolf
<ROOT>
<A>
<A>
<B>
<C>
<A/> nested up to 3
</C>
</B>
</A>
<A>
<B>
<C>
<A>
<A/> nested up to 4
</A>
<C>
</B>
</A>
</A>
</ROOT>