This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: counting leaf nodes
- To: xsl-list at mulberrytech dot com
- Subject: Re: counting leaf nodes
- From: Nick Browne <NickBrowne at slipstone dot co dot uk>
- Date: Thu, 31 Aug 2000 23:51:56 +0100
- Organization: Slipstone Ltd
- References: <200008301502.LAA32082@node15.unix.Virginia.EDU>
- Reply-To: xsl-list at mulberrytech dot com
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) > 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::*)]) > 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