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

count() gives strange result

Subject: count() gives strange result
From: <simon_handley@xxxxxxxxxxx>
Date: Fri, 17 Sep 2004 16:22:54 -0700
xsl count
Thanks to everyone who answered my previous, not real smart, question.

I have another question that I have a funny feeling is equally dumb, but I've
been banging my head against it with no luck so far....

The following template is very simple: it takes as input a node set, each
node is an element with a numeric attribute @numChanges.  The template
returns
the sum of these attributes:

    <xsl:template name="totalNumChanges">
        <xsl:param name="children"/>
        <xsl:message>tnc <xsl:copy-of select="$children"/> count=<xsl:value-of
select="count($children)"/>
        </xsl:message>
        <xsl:choose>
            <xsl:when test="not($children)">
                <xsl:message>returning <xsl:value-of select="0"/>
                </xsl:message>
                <xsl:value-of select="0"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="tail">
                    <xsl:call-template name="totalNumChanges">
                        <xsl:with-param name="children"
select="$children[position() != 1]"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:message>returning <xsl:value-of select="$tail +
$children[1]/@numChanges"/>
                </xsl:message>
                <xsl:value-of select="$tail + $children[1]/@numChanges"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

I then run it (Saxon 8.0) from this:

    <xsl:template match="/">
        <xsl:variable name="tmp">
            <wrapper numChanges="1"/>
            <wrapper numChanges="2"/>
            <wrapper numChanges="3"/>
        </xsl:variable>
        <xsl:call-template name="totalNumChanges">
            <xsl:with-param name="children" select="$tmp/wrapper"/>
        </xsl:call-template>
    </xsl:template>

and get the following messages:

    tnc <wrapper numChanges="1"/><wrapper numChanges="2"/><wrapper
numChanges="3"/>
    count=3
    tnc <wrapper numChanges="2"/><wrapper numChanges="3"/> count=3
    tnc <wrapper numChanges="3"/> count=2
    tnc  count=0
    returning 0
    returning 3
    returning 5
    returning 6

So, the template works as expected, but the count()s are wrong (3, 3, 2, 0
rather
than 3, 2, 1, 0).  It seems like I must be missing something really
obvious....

Thanks!

Simon

------------------------------------------------------------------------
  Simon Handley
  Agilent Technologies
  5301 Stevens Creek Boulevard, MS WH
  Santa Clara, California 95051-7295
  simon_handley@xxxxxxxxxxx
  408-553-7122 (w) 408-553-7269 (fax)

--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx>
--+--

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.