This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: sum function
- From: "Michael Kay" <michael dot h dot kay at ntlworld dot com>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Fri, 16 Nov 2001 16:28:17 -0000
- Subject: RE: [xsl] sum function
- Reply-to: xsl-list at lists dot mulberrytech dot com
> How do I use the sum function on xml such as:
>
> <Amount>12,345.12</Amount>
> <Amount>132,345.12</Amount>
> <Amount>2,345.12</Amount>
>
> If I use Total Price = <xsl:value-of select="sum(//Amount)"/>
> I get the
> result NaN. However, if the data does not contain ',' the sum works
> correctly.
As a general principle, it's best to hold data in XML documents in a form
that's optimised for processing rather than for display to humans. Using
schemas will encourage that further. So I would say, first do a
transformation to a document that holds these numbers in their canonical
form (12345.12 etc), which you can achieve using translate(., ',', ''), and
then apply the summation to the result.
Mike Kay
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list