Re: counting leaf nodes

Subject: Re: counting leaf nodes
From: Nick Browne <NickBrowne@xxxxxxxxxxxxxxx>
Date: Thu, 31 Aug 2000 23:51:56 +0100
Perry, it might be a bit late now but here is a complete solution thanks to Mike
Balls suggestion and the FAQ list :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl=""; >
 <xsl:template match="/">

    <TABLE BORDER="1">

    <!-- Begin the recursive template call passing it the set
         of elements at the root element level. Pass the level
         as well for incrementing in the recursive call -->
    <xsl:call-template name="create_row">
     <xsl:with-param name="lvl" select="0"/>
     <xsl:with-param name="elems" select="//*[count(ancestor::*)=number('0')]"/>



 <!-- Using the node set passed, generate a row. For each element
      in the node set create a new cell. Stop the recursive call
      when a level has been reached where there are no elements   -->

 <xsl:template name="create_row">
  <xsl:param name="lvl"/>
  <xsl:param name="elems"/>

  <!-- if the node set passed contains elements generate a row -->

  <xsl:if test="count($elems) &gt; 0">

    <xsl:for-each select="$elems">

     <!-- add the COLSPAN attribute only if the current element
          has more than 1 leaf nodes (? put the count into a variable ?) -->
     <xsl:if test="count(descendant::*[not(child::*)]) &gt; 1 ">
      <xsl:attribute name="COLSPAN">
       <xsl:value-of select="count(descendant::*[not(child::*)])"/>

      <xsl:value-of select="./text()"/>



  <!-- ... and call the template again for the next level down -->
   <xsl:call-template name="create_row">
    <xsl:with-param name="lvl" select="$lvl + 1"/>
    <xsl:with-param name="elems" select="//*[count(ancestor::*)=number($lvl +


  <!-- if here, the passed node set was empty so return -->


Given the sprinkling of '//'s this is probably not a great performer but it gives
the answer you were after !


Nick Browne

Perry Roland wrote:

> Hi,
> I need some help with an expression that will count the leaf
> nodes on a given branch. etc.

 XSL-List info and archive:

Current Thread