Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old April 24th, 2006, 09:06 AM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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. -
 
Old April 24th, 2006, 09:19 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old April 24th, 2006, 09:30 AM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much for your quick response, I'll try this.

Margret Asgeirsdottir
- Remaining alive is all there is to life. -
 
Old April 25th, 2006, 06:44 AM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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. -
 
Old April 25th, 2006, 07:23 AM
Authorized User
 
Join Date: Apr 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

[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!

 
Old April 25th, 2006, 08:04 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old April 25th, 2006, 09:52 AM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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. -





Similar Threads
Thread Thread Starter Forum Replies Last Post
adding column numbers decren Javascript How-To 2 May 5th, 2007 07:48 AM
Adding numbers Hudson40 Excel VBA 3 November 23rd, 2005 03:02 AM
adding numbers gmoney060 Access 1 August 15th, 2004 08:14 PM
Adding numbers kend XSLT 4 June 27th, 2003 12:33 AM
Adding numbers kend XSLT 1 June 26th, 2003 10:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.