Re: [xsl] calculate depth of an xml-tree

Subject: Re: [xsl] calculate depth of an xml-tree
From: Oleg Tkachenko <olegt@xxxxxxxxxxxxx>
Date: Tue, 22 Apr 2003 14:54:02 +0300
Florian Bauer wrote:

and now my 2nd (and hopefully last) question for today ...

I have to write an xsl file, which calculates the maximal depth of an input xml file.

<abc>
    <cde>
        <efg/>
        <asd/>
    </cde>
    <aaa/>
</abc>

=> maxdepth = 3 ....

I think I have to use a "for each" with sorting and increment a variable, but I don't know how to do this (you see, I'm a big noob @ xml/xsl :( ... )
Forget about incrementing a variable, there is no such thing in XSLT.
Sort all elements by number of ancestors and take the first one.
<xsl:template match="/">
<xsl:for-each select="//*">
<xsl:sort select="count(ancestor::*)" data-type="number" order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="count(ancestor::*)+1"/>
</xsl:if>
</xsl:for-each>
</xsl:template>


--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread