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

Re: count Nodes with attributes until... (XSLT 1.0)

Subject: Re: count Nodes with attributes until... (XSLT 1.0)
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Wed, 28 Jun 2006 14:54:36 +0530
count xsl
Hi Jan,
  Please try the following stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" />

   <xsl:template match="/theExampleXMLTree">
       <xsl:text>NodeNr.|Value &#xa;</xsl:text>
       <xsl:for-each select="Node">
           <xsl:variable name="count">
              <xsl:call-template name="XYZ">
                 <xsl:with-param name="nodeset"
select="following-sibling::*" />
                 <xsl:with-param name="n" select="0" />
             </xsl:call-template>
           </xsl:variable>
           <xsl:value-of select="position()" />|<xsl:value-of
select="$count" /><xsl:text>&#xa;</xsl:text>
       </xsl:for-each>
   </xsl:template>

   <xsl:template name="XYZ">
       <xsl:param name="nodeset" />
       <xsl:param name="n" />

       <xsl:choose>
           <xsl:when test="$nodeset[1]/@count-me = 'yes'">
               <xsl:call-template name="XYZ">
                 <xsl:with-param name="nodeset"
select="$nodeset[position() &gt; 1]" />
                 <xsl:with-param name="n" select="$n + 1" />
               </xsl:call-template>
           </xsl:when>
           <xsl:otherwise>
               <xsl:value-of select="$n" />
           </xsl:otherwise>
       </xsl:choose>

</xsl:template>

</xsl:stylesheet>

Regards,
Mukul

On 6/28/06, news@xxxxxxxxxxx <news@xxxxxxxxxxx> wrote:
Hello List,

I have to count a number of nodes. Each node that is to be counted has an
attribute 'count-me="yes"' (this is a example :)). But as soon there is a
node which does'nt have such an attribute, or has a attribute
'count-me="false"' this and all following nodes are not to be counted. The
result value is dependent on the number of the node that is in the focus.

Example:
<theExampleXMLTree>
       <Node count-me="yes"/>
       <Node count-me="yes"/>
       <Node count-me="yes"/>
       <Node/>
       <Node count-me="no"/>
       <Node count-me="yes"/>
</theExampleXMLTree>

The value wanted is e.g. 2 when processing the first node.

...

This is how I've tried it so far. The problem here is, that when processing
the first Node I always get the value of 4 (what is quite logical to me).

<xsl:template natch="Node" mode="counting_example">
 <xsl:variable name="number" select="following-sibling::*[@count-me =
'yes')]"/>
</xsl:template>

I also tried:
 <xsl:variable name="number" select="following-sibling::*[position()
+1][@count-me = 'yes')]"/>

But that just counts the next node if it has the matching attribute.

Let me show what I actually want at the following tabel (using the XML shown
above):
NodeNr.|Value
1|2
2|1
3|0
4|0
5|1
6|0

Is this possible? and how?

With best regards,
Jan

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.