xsl-list
[Top] [All Lists]

Re: calculate depth of an xml-tree

2003-04-22 04:54:02
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



<Prev in Thread] Current Thread [Next in Thread>