xsl-list
[Top] [All Lists]

Re: count() gives strange result

2004-09-17 16:46:53
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(_at_)agilent(_dot_)com 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(_at_)agilent(_dot_)com
  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(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--



<Prev in Thread] Current Thread [Next in Thread>