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
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext 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
Alberto MassariSubject: What is the best way to sum value excluding a large list
Author: Alberto Massari
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


 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
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.