Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > BOOK: XSLT Programmer's Reference, 2nd Edition
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: XSLT Programmer's Reference, 2nd Edition
This is the forum to discuss the Wrox book XSLT: Programmer's Reference, 2nd Edition by Michael Kay; ISBN: 9780764543814
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: XSLT Programmer's Reference, 2nd Edition section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 5th, 2008, 05:59 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default sum and translate in XPath

My XML has commas in the numbers so I'm using translate in order to treat as numerical values.
I now need to get the sum of a nodeset and have an issue when attempting to use sum and translate together.

I'm using XPath 1.0

Example XML
<?xml version="1.0" encoding="UTF-8"?>
<items>
    <item>thing one</item>
    <item>thing two</item>
    <value>1,500</value>
    <value>3,700</value>
</items>

The XPath I'm using will never work as sum returns the nodeset and translate only a single item so even if it worked it would only get one item.

Xpath
sum(translate(//value,",",""))

Is there any way to do this or should I run a transform to remove commas?

  #2 (permalink)  
Old December 5th, 2008, 06:23 AM
mhkay's Avatar
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
  #3 (permalink)  
Old December 5th, 2008, 08:20 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your assistance, unfortunatly I am restricted to using 1.0 for the time being.


 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TRANSLATE FUNCTION INSIDE XPATH pallone XSLT 8 February 8th, 2008 05:30 PM
translate gantait XSLT 1 February 27th, 2007 04:15 AM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
Sum function and XPath problems tslag XSLT 2 June 26th, 2006 10:23 AM
Xpath: sum function gracehanh XSLT 13 September 27th, 2005 09:30 AM



All times are GMT -4. The time now is 01:56 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.