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 July 20th, 2011, 11:14 AM
Registered User
 
Join Date: Jul 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Totaling/Summing string values

I have an xml document where I'm trying to sum up a node set but the node set contains a currency code. When I use sum(substring-before()) to retrieve numeric portion of the element/node and try to sum it up, I get the not a node-set error which I understand why. I haven't been alble to figure out how to sum() the values after removing the currency code.

This is my input XML.

<Release>
<ReleaseRefnum>
<ReleaseRefnumQualifierGid>
<Gid>
<DomainName>TEKNION</DomainName>
<Xid>NET PRICE</Xid>
</Gid>
</ReleaseRefnumQualifierGid>
<ReleaseRefnumValue>15889.61 CAD</ReleaseRefnumValue>
</ReleaseRefnum>
</Release>

<Release>
<ReleaseRefnum>
<ReleaseRefnumQualifierGid>
<Gid>
<DomainName>TEKNION</DomainName>
<Xid>NET PRICE</Xid>
</Gid>
</ReleaseRefnumQualifierGid>
<ReleaseRefnumValue>30412.71 CAD</ReleaseRefnumValue>
</ReleaseRefnum>
</Release>

This is the invalid xpath statement.

<DeclaredValue>
<xsl:value-of select="sum(substring-before(Release/ReleaseRefnum[ReleaseRefnumQualifierGid/Gid/Xid='NET PRICE']/ReleaseRefnumValue,' '))"/>
</DeclaredValue>

I'm using xslt 1.0 and the below values are included in my stylesheet.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="translator" version="1.0" xmlns:otm="http://xmlns.oracle.com/apps/otm" xmlns:translator="http://www.jclark.com/xt/java/glog.webserver.i18n.Translator">

Does anyone know how I can resolve and total up values after applying a function to remove the currency code..?

thanks...Brian
 
Old July 20th, 2011, 05:17 PM
Registered User
 
Join Date: Jul 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Totaling/Summing string values

After further research on VelocityReviews I found the following solution.

I used a for-each loop to set the value of a xslt variable (ie: DeclaredValues) to a temporary tree whose elements contain the values to sum after stripping off the currency code with a substring-before function.

I initialized the temporary tree element => TempDeclaredValue = 0 so I never have an empty node.

<xsl:variable name="DeclaredValues">
<TempDeclaredValue>0</TempDeclaredValue>
<xsl:for-each select="Release/ReleaseRefnum[ReleaseRefnumQualifierGid/Gid/Xid='NET PRICE']/ReleaseRefnumValue">
<TempDeclaredValue>
<xsl:value-of select="substring-before(../ReleaseRefnumValue,' ')"/>
</TempDeclaredValue>
</xsl:for-each>
</xsl:variable>

After iterating through all of the NET PRICE reference num elements, I then just execute a sum() function on the variable => DeclaredValues. The only caveat was that I had to include the namespace/extension library => xmlns:exslt="http://exslt.org/common" and use exslt:node-set() so I could sum the nodes correctly because I was getting the error

{"To use a result tree fragment in a path expression, first convert it to a node-set using the msxsl:node-set() function."}

<TotalDeclaredValue>
<xsl:value-of select="sum(exslt:node-set($DeclaredValues)/*)"/>
</TotalDeclaredValue>

Hope this helps others that are having a similiar issue.

Last edited by bsparker; July 20th, 2011 at 05:44 PM.. Reason: hit enter key by accident before completing





Similar Threads
Thread Thread Starter Forum Replies Last Post
Totaling columns in a footer row of a GridView psimonson ASP.NET 2.0 Basics 1 May 5th, 2008 01:34 PM
need help totaling a table michael193nj Access VBA 0 March 19th, 2008 10:08 AM
summing column values hydriswall PHP Databases 1 December 8th, 2006 04:54 AM
Summing values swwallace XSLT 4 March 4th, 2006 12:13 PM
grouping / totaling happyslug BOOK: Professional Crystal Reports for VS.NET 1 July 12th, 2005 05:07 AM





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