View Single Post
  #2 (permalink)  
Old December 5th, 2008, 06:23 AM
mhkay's Avatar
mhkay mhkay is offline
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 Posts
Default

This is easy in XPath 2.0:

sum(//value/number(translate(.,",",""))

It can't be done in pure XPath 1.0 because the argument to sum() has to be a set of nodes; your nodes don't contain numbers, and XPath alone can't create new nodes. You can do it in XSLT 1.0 - one way is to write a recursive template that processes the nodes one by one, adding to a running total as it goes; another way is a two-pass approach where you construct a new tree containing the translated nodes. But 2.0 is much easier.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference