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
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.

   
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.