XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> + How does one add working days ... (4)
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
Topic  
Postnext
Kam ChanSubject: What is the best way to sum value excluding a large list
Author: Kam Chan
Date: 21 Aug 2009 06:08 PM
Hello,

Given a very large XML doc, I need to sum up the values under a certain node, but excluding a large list (different element names) from the total.

To make a small example, lets say I have the following XML:

<root>
<itemA>
<price>123</price>
</itemA>
<itemB>
<price>234</price>
</itemB>
<itemC>
<price>222</price>
</itemC>
<myItem>
<price>88</price>
</myItem>
<hisHouse>
<price>300000</price>
</hisHouse>
<theZebra>
<price>35.5</price>
</theZebra>
.....
</root>


And I need to add up all the prices except for node names of "itemC", "hisHouse", "theZebra", and a hundred other names.

One solution is:
<xsl:value-of select="sum(/root/*[not(self:itemC)][not(self:hisHouse)]....)" />

That makes a long xpath, and easy to make an error.

Is there a different (better, or may be not better in terms of performance, but better in terms of maintenance or readability) way that we can accomplish this?

Thanks

Kam

Postnext
James DelaneySubject: What is the best way to sum value excluding a large list
Author: James Delaney
Date: 27 Aug 2009 03:57 PM
I'm new to the language, as well as the forums. I tried solving your question but failed. I was going down the road of applying templates to the nodes:


<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="root/itemC[1] | hisHouse[1] | theZebra[1]">
</xsl:template>


<xsl:template match="root/itemA[1] | itemB[1] | myItem[1]">

<html>
<body>

<xsl:for-each-group select="price" group-by="/">
<xsl:copy-of select="sum(current-group())"/>
</xsl:for-each-group>

</body>
</html>

</xsl:template>

</xsl:stylesheet>

needless to say, it's not fully working. I'm not sure how to get the values grouped, as for-each-group and for-each are still separating the values due to the node processing sequence I guess. Priority doesn't seem to really matter.

Posttop
(Deleted User) Subject: What is the best way to sum value excluding a large list
Author: (Deleted User)
Date: 02 Sep 2009 12:33 PM
Hi James,
you could do a two-step process; first exclude the nodes you don't want, then process the resulting list.

For instance, write an empty template for the 100+ nodes you want to eliminate

<xsl:template match="one | two | three"/>

Then collect the rest in a variable

<xsl:template match="/">
<xsl:variable name="okNodes">
<xsl:apply-templates/>
</xsl:variable>

<xsl:value-of select="$okNodes"/>
</xsl:template>

then you can work on the variable to do the processing you need

Alberto


   
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.