[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: counting leaf nodes

Subject: Re: counting leaf nodes
From: Nick Browne <NickBrowne@xxxxxxxxxxxxxxx>
Date: Thu, 31 Aug 2000 23:51:56 +0100
count leaf nodes
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="http://www.w3.org/1999/XSL/Transform" >
 <xsl:template match="/">

  <HTML>
   <BODY>
    <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')]"/>
    </xsl:call-template>

    </TABLE>
   </BODY>
  </HTML>

 </xsl:template>

 <!-- 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">

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

     <!-- 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:attribute>
     </xsl:if>

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

     </TD>
    </xsl:for-each>

   </TR>

  <!-- ... 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 +
1)]"/>
   </xsl:call-template>

  </xsl:if>

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

 </xsl:template>
</xsl:stylesheet>

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

Regards

Nick Browne
Slipstone.com

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:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.