This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: Calculating a sum of rounded numbers
- From: "Nik Krimm" <Nik dot Krimm at emacdigital dot com>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Fri, 10 May 2002 10:55:02 -0500
- Subject: RE: [xsl] Calculating a sum of rounded numbers
- Reply-to: xsl-list at lists dot mulberrytech dot com
Michael:
Thanks for the many helpful suggestions. I have a quick follow up question:
Is there any way to build a temporary tree fragment and access its nodes without using processor-specific functionality or an extension library? (in testing I've build a tree fragment as the content of a variable, and used 'msxsl:node-set($variable)/XPath-expression' to access the nodes)
TIA
-----Original Message-----
From: Michael Kay [mailto:michael.h.kay@ntlworld.com]
Sent: Thursday, May 09, 2002 5:29 PM
To: xsl-list@lists.mulberrytech.com
Subject: RE: [xsl] Calculating a sum of rounded numbers
It's the old problem of getting the sum of a computed expression. Solutions
include:
- creating a temporary tree containing the computed values, then doing sum()
on the nodes in the temporary tree
- writing a recursive template to do the summation
- using the saxon:sum() extension function
- using Dimitre's FXSL library
- using XSLT 2.0 / XPath 2.0
Michael Kay
Software AG
home: Michael.H.Kay@ntlworld.com
work: Michael.Kay@softwareag.com
> -----Original Message-----
> From: owner-xsl-list@lists.mulberrytech.com
> [mailto:owner-xsl-list@lists.mulberrytech.com]On Behalf Of Nik Krimm
> Sent: 09 May 2002 21:45
> To: xsl-mulberry
> Subject: [xsl] Calculating a sum of rounded numbers
>
>
> I am trying the following output from the XML below:
>
> [output]
> .21
> .09
> .2
> sum: .5
>
> [xml]
> <values>
> <value>.2123</value>
> <value>.0923</value>
> <value>.2012</value>
> </values>
>
> [xsl]
> <xsl:for-each select="value">
> <xsl:value-of select="format-number((.),'.##')" /><br />
> </xsl:for-each>
> sum: <xsl:value-of select="format-number(sum(//value),'.##)" />
>
> Instead I'm getting the output
>
> [output]
> .21
> .09
> .2
> sum: .51
>
> i.e. a rounding error because the numbers are sumed first and
> then rounded, rather than rounded first and then summed.
>
>
> I tried
> <xsl:value-of
> select="format-number(sum(format-number(//measure),'##')" />
>
> But get an error:
> Argument 1 must return a node-set.
> format-number(-->sum(format-number(//measure),'##')<--)
>
> Can anyone suggest a better way to approach this problem that
> is native xsl (i/e/ without extensions)?
>
>
> TIA
>
> nik
>
>
>
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list