Re: [xsl] count() gives strange result

Subject: Re: [xsl] count() gives strange result
From: George Cristian Bina <george@xxxxxxx>
Date: Sat, 18 Sep 2004 02:46:53 +0300
Hi Simon,

Have you considered using sum ?

<xsl:template name="totalNumChanges">
    <xsl:param name="children"/>
    <xsl:value-of select="sum($children//@numChanges)"/>
  </xsl:template>

or even better

<xsl:value-of select="sum($tmp/wrapper/@numChanges)"/> in the template matching the document node.

Best Regards,
George
-----------------------------------------------
George Cristian Bina
<oXygen/> XML Editor & XSLT Editor/Debugger
http://www.oxygenxml.com

simon_handley@xxxxxxxxxxx wrote:

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