xslt - Height of a Table of Contents: finding maximum depth of a node -


the height of chapters , sections can computed separately. use matching templates only. input:

<?xml version='1.0' encoding='utf-8'?> <!doctype book system "book.dtd"> <book title="definitive xml schema">     <author first="priscilla" last="walmsley"/>     <chapter title="a">         <section title="d"/>         <section title="g">             <section title="s"/>             <section title="t"/>         </section>         <section title="e">             <section title="f"/>         </section>     </chapter>     <chapter title="b">         <section title="n"/>         <section title="c">             <section title="a"/>             <section title="m"/>         </section>     </chapter> </book> 

the out put is:

 3 

which maximum depth of section node

if want compute maximum depth of section node, can use following xslt:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform"     version="1.0">      <xsl:output method="text"/>      <xsl:template match="book">         <xsl:for-each select="//section">             <xsl:sort select="count( ancestor::node() )"                  data-type="number" order="descending"/>             <xsl:if test="position() = 1">                 <xsl:value-of select="count( ancestor::node() ) - 1"/>             </xsl:if>         </xsl:for-each>     </xsl:template>  </xsl:stylesheet> 

which, given example xml imput, produce following output:

3 

Comments