Wrox Programmer Forums
|
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 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 December 5th, 2008, 05:59 AM
Registered User
 
Join Date: Mar 2008
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?

 
Old December 5th, 2008, 06:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old December 5th, 2008, 08:20 AM
Registered User
 
Join Date: Mar 2008
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.







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





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