[XSL-LIST Mailing List Archive Home]
[By Thread]
[By Date]
Re: [xsl] adding 2 formatted numbers?
Subject: Re: [xsl] adding 2 formatted numbers? From: "Joerg Heinicke" <joerg.heinicke@xxxxxx> Date: Thu, 14 Mar 2002 23:14:15 +0100 |
With this stylesheet you can add all different types of numbers: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="root"> <xsl:apply-templates select="node[1]"/> </xsl:template> <xsl:template match="node"> <xsl:param name="sum" select="0"/> <!-- grouping separator of this number --> <xsl:variable name="gs"> <!-- the separators --> <xsl:variable name="separators" select="translate(., '1234567890', '')"/> <!-- take the second last character --> <xsl:value-of select="substring($separators, string-length($separators) - 1, 1)"/> </xsl:variable> <!-- the number of the current node --> <!-- first remove the grouping separators, then replace ',' with '.' --> <xsl:variable name="number" select="translate(translate(., $gs, ''), ',', '.')"/> <xsl:apply-templates select="following-sibling::node[1]"> <xsl:with-param name="sum" select="$sum + $number"/> </xsl:apply-templates> <xsl:if test="not(following-sibling::node)"> <xsl:value-of select="$sum + $number"/> </xsl:if> </xsl:template> </xsl:stylesheet> With this XML <root> <node>100,000,000.01</node> <node>100000000,02</node> <node>100000000.03</node> <node>100.000.000,04</node> <node>100,000,000.05</node> <node>100.000.000,06</node> <node>100.000.000</node> </root> you get 700000000.21 as result. A problem are numbers like 1.000 - is it 1000 or 1? I wrote the code so, that if only one dot or comma appears, this character is used as decimal separator ($gs is empty string then). If you want to add more logic for this, add it to the end of the declaration for $gs. Regards, Joerg > Hi, > I have the following nodes in my xml: > <node1>123,456,789.01</node1> > <node2>222,222,222.54</node2> > > In my xsl I have to add the value of the 2 of them. Since my numbers are > formatted I cant do it. > How can I "unformat" my numbers and add them together? > > Note: depending on the users currency formatting preferences I might get the > number in the following format: > <node1>123.456.789,01</node1> > <node2>222.222.222,54</node2> > I also have now way of knowing how many commas there are per node (the > length varies) > > Thanks > Lou XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] adding 2 formatted numbers?, L Rutker | Thread | RE: [xsl] adding 2 formatted number, Jeff Beadle |
[xsl] Shorter, better, nicer XSLT, Antonio Bueno | Date | Re: [xsl] general purpose filter st, Robert Sösemann |
Month |