[xsl] Count() elements in range

Subject: [xsl] Count() elements in range
From: Chris.Pearson@xxxxxxxxxxxx
Date: Wed, 15 Oct 2003 20:33:58 -0400
I am wrestling with a problem related to counting a specific range of
elements in a source document. I've tried searching the archives, but I was
unable to locate any past discussions that appeared relevant.

Problem: count the number of <element> nodes in the tree starting from the
last <element> or the last <element> whose following-sibling::element does
not have a child <flag>. I think my issue is simply a matter of XPath
expressions, but my brain keeps trying differing versions of recursion with
no luck.

Source xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!--start A series of 1 -->
<element number="A1">
<!-- start B series of 4 -->
<element number="B1">
<element  number="B2"/>
<element  number="B3"/>
<element  number="B4"/>
<!-- start C series of 2 -->
<element  number="C1">
<element  number="C2"/>
<!-- start D series of 3 -->
<element  number="D1">
<element  number="D2" />
<element  number="D3" />

Stylesheet so far:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
<xsl:output method="text" media-type="text/plain"/>
<xsl:strip-space elements="*" />

<xsl:template match="/root/element">
    <xsl:when test="(following-sibling::element[1][child::flag]) or
(count(following-sibling::element) = 0)">
      <xsl:call-template name="countFlags">
        <xsl:with-param name="nodeList" select="preceding-sibling::element"
    <xsl:otherwise />

<xsl:template name="countFlags">
  <xsl:param name="nodeList"/>
<!-- do something here to count the preceding-sibling::element nodes up to
the first <element> whose following-sibling::element does not have a <flag>
child. -->

Thanks in advance,


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread