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
Kenny CudworthSubject: Grouping And Summing Up Based On Numerical Attribute
Author: Kenny Cudworth
Date: 17 Aug 2005 10:58 PM
I'm starting with this:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<A objectKey="1" quantity="2">
<type> 5 </type>
</A>
<A objectKey="2" quantity="4">
<type> 5 </type>
</A>
<A objectKey="3" quantity="7">
<type> 6 </type>
</A>
<A objectKey="4" quantity="3">
<type> 5 </type>
</A>
<B objectKey="5"/>
<B objectKey="6"/>
</root>

Transforming with this:

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

<xsl:key name="group" match="A" use="type"/>

<xsl:template match="/">
<xsl:for-each select="//A[generate-id() = generate-id(key('group', type)[1])]">
<a>

<quantity>
<xsl:value-of select="current()/@quantity + sum(following-sibling::A[type = current()/type]/@quantity)"/>
</quantity>
<type>
<xsl:value-of select="current()/type"/>
</type>
</a>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Getting this:

<?xml version='1.0' ?>
<a><quantity>16</quantity><type> 5 </type></a><a><quantity>10</quantity><type> 6 </type></a>

But want this:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<a objectKey="1" quantity="7">
<type> 5 </type>
</a>
<a objectKey="3" quantity="9">
<type> 6 </type>
</a>
<b objectKey="5" />
<b objectKey="6"/>
</root>



The filter I am setting on the following siblings isn't working and instead I'm getting all of the following siblings. I want just the siblings for the current group. Element order does not matter in the result. I need something like the group sum feature in SQL. I'd appreciate any help.

Postnext
Kenny CudworthSubject: Grouping And Summing Up Based On Numerical Attribute
Author: Kenny Cudworth
Date: 17 Aug 2005 11:00 PM
Correction!

I want this:

<?xml version='1.0' ?>
<a><quantity>9</quantity><type> 5 </type></a><a><quantity>7</quantity><type> 6 </type></a>

Postnext
Ivan PedruzziSubject: Grouping And Summing Up Based On Numerical Attribute
Author: Ivan Pedruzzi
Date: 18 Aug 2005 09:56 AM
You have to use the current group generated by key function call

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="group" match="A" use="type"/>
<xsl:template match="/">
<root>
<xsl:for-each select="//A[generate-id() = generate-id(key('group', type)[1])]">
<a>
<quantity>
<xsl:value-of select="sum(key('group', type)/@quantity)"/>
</quantity>
<xsl:copy-of select="type"/>
</a>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>

Hope this helps
Ivan Pedruzzi
Stylus Studio Team

Posttop
Kenny CudworthSubject: Grouping And Summing Up Based On Numerical Attribute
Author: Kenny Cudworth
Date: 18 Aug 2005 05:14 PM
Works well and straight-forward. Thanks Ivan.

 
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.