 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 24th, 2006, 09:06 AM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Adding numbers in #.###,0 format
Hello programmers,
I live in Iceland and here we use a comma (,) for a decimal point. I'm receiving an XML with some numbers in this format and need to add them together, f.ex. using the XPath function "sum".
I have been trying to use xsl:decimal-format to do this but as I understand it this function requires the input number to have a point (.) as an input and formats the number in combination with format-number only for displaying.
Can you please help
Margret Asgeirsdottir
- Remaining alive is all there is to life. -
|
|

April 24th, 2006, 09:19 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
There are functions in XSLT for converting numbers from "international" representation into localized representation (format-number), but there is nothing for the reverse direction. Just use translate() to convert symbols as required, for example translate($in, ',', '.')
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

April 24th, 2006, 09:30 AM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you very much for your quick response, I'll try this.
Margret Asgeirsdottir
- Remaining alive is all there is to life. -
|
|

April 25th, 2006, 06:44 AM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I've been trying to use the translate function to convert the numeric format and then summing it like this:
sum(translate(OverViewItem/ChargeItem/Hours, ',', ''))
But I get the following error:
msxml3.dll (0x80004005)
Expression does not return a DOM node. -->sum(translate(OverViewItem/ChargeItem/Hours, ',', ''))<--
I have also tried to use this articles solution but it does not work, not even for integers.
Can you please help, I'm completely stranded.
Margret Asgeirsdottir
- Remaining alive is all there is to life. -
|
|

April 25th, 2006, 07:23 AM
|
|
Authorized User
|
|
Join Date: Apr 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
[ http://www.zvon.org/xxl/XSLTreferenc...ranslate.html]
translate(string_input_node, string_from, string_too):
translate(., ',', '.')
Perhaps using Params/Variables of what the inputs are then using the SUM function, might work.
<xsl:variable name="input_name" select="translate(OverViewItem/ChargeItem/Hours, ',', '.')" />
...
<xsl:variable name="subtotal">
<xsl:value-of select="sum($input_name1+$input_name2)">
</xsl:variable
(Or if you more more that 1 input, use * or for-each loop?) - Just an idea, prob completely wrong though!
|
|

April 25th, 2006, 08:04 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Summing over a set of computed numbers is easy in XPath 2.0
sum(for $x in $numbers return number(translate($x,....)))
but it's harder in 1.0. The following thread gives various suggestions:
http://www.biglist.com/lists/xsl-lis.../msg01228.html
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

April 25th, 2006, 09:52 AM
|
|
Registered User
|
|
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I tried Dimitre Novatchev's solution again from the start and this time it worked completely, not sure what I did wrong the first time:
http://www.stylusstudio.com/xsllist/...post50830.html
Thank you so much for your help!! This is a great forum! :D
Margret Asgeirsdottir
- Remaining alive is all there is to life. -
|
|
 |