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
Tony LavinioSubject: Need Help --- A Special Sum Question
Author: Tony Lavinio
Date: 08 May 2006 11:20 AM
Originally Posted: 08 May 2006 11:19 AM
This is a help forum for users of Stylus Studio.
General XSLT questions are best handled on the list at
http://www.mulberrytech.com/xsl/xsl-list/subscribe-unsubscribe.html

That being said, the page at
http://www.stylusstudio.com/xslt_mapper.html
may provide some help for you.

Postnext
(Deleted User) Subject: Need Help --- A Special Sum Question
Author: (Deleted User)
Date: 08 May 2006 11:28 AM
Hongwei,

Try this...

<xsl:key name="EDI" match="Detail" use="concat(Entity, ':', Quantity/HourlyBeginTime, Quantity/HourlyEndTime)"/>

<xsl:template match="/EDI/Detail">

<xsl:apply-templates select="key('EDI', concat(Entity,':',Quantity/HourlyBeginTime, Quantity/HourlyEndTime))" mode="Sum"/>
</xsl:template>

<xsl:template match="Detail" mode="Sum">
<xsl:variable name="thekey" select="concat(Entity, ':', Quantity/HourlyBeginTime, Quantity/HourlyEndTime)"/>
<xsl:value-of select="sum(/EDI/Detail/Quantity/HourlyQuantity[concat(../../Entity, ':', ../HourlyBeginTime, ../HourlyEndTime) = $thekey])"/>
<xsl:text>
</xsl:text>
</xsl:template>

Regards
-Srini

Postnext
Hongwei JiaSubject: Need Help --- A Special Sum Question
Author: Hongwei Jia
Date: 13 May 2006 12:23 PM
Originally Posted: 13 May 2006 12:18 PM
Srinivas,

Thank you very much for your suggestion! I just got a chance to transform the attached xml:
<EDI>
- <Detail>
<UnitOfMeasure>KW1</UnitOfMeasure>
<DirectionOfFlow>U</DirectionOfFlow>
<Entity>Cargill</Entity>
<EntityQualifier>ZEW</EntityQualifier>
<QuantityQualifier>Z05</QuantityQualifier>
- <Quantity>
<HourlyBeginTime>2006-07-01T04:00:00</HourlyBeginTime>
<HourlyEndTime>2006-07-01T22:00:00</HourlyEndTime>
<HourlyQuantity>-19000000</HourlyQuantity>
</Quantity>
- <Quantity>
<HourlyBeginTime>2006-07-01T22:00:00</HourlyBeginTime>
<HourlyEndTime>2006-07-02T00:00:00</HourlyEndTime>
<HourlyQuantity>-1000000</HourlyQuantity>
</Quantity>
- <Quantity>
<HourlyBeginTime>2007-07-01T04:00:00</HourlyBeginTime>
<HourlyEndTime>2007-07-01T22:00:00</HourlyEndTime>
<HourlyQuantity>15000000</HourlyQuantity>
</Quantity>
</Detail>
- <Detail>
<UnitOfMeasure>KW1</UnitOfMeasure>
<DirectionOfFlow>D</DirectionOfFlow>
<Entity>Cargill</Entity>
<EntityQualifier>ZEW</EntityQualifier>
<QuantityQualifier>Z05</QuantityQualifier>
- <Quantity>
<HourlyBeginTime>2006-07-01T22:00:00</HourlyBeginTime>
<HourlyEndTime>2006-07-02T00:00:00</HourlyEndTime>
<HourlyQuantity>5990000</HourlyQuantity>
</Quantity>
- <Quantity>
<HourlyBeginTime>2006-07-02T00:00:00</HourlyBeginTime>
<HourlyEndTime>2006-07-02T04:00:00</HourlyEndTime>
<HourlyQuantity>0</HourlyQuantity>
</Quantity>
</Detail>
- <Detail>
<UnitOfMeasure>KW1</UnitOfMeasure>
<DirectionOfFlow>D</DirectionOfFlow>
<Entity>Accord</Entity>
<EntityQualifier>ZEW</EntityQualifier>
<QuantityQualifier>Z05</QuantityQualifier>
- <Quantity>
<HourlyBeginTime>2006-07-01T04:00:00</HourlyBeginTime>
<HourlyEndTime>2006-07-02T04:00:00</HourlyEndTime>
<HourlyQuantity>84000000</HourlyQuantity>
</Quantity>
</Detail>
</EDI>

I am using the xslt:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href=".xslt"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:vbs="urn:schemas-sqlxml-org:vbs">

<xsl:key name="EDI" match="Detail" use="concat(Entity, ':', Quantity/HourlyBeginTime, Quantity/HourlyEndTime)"/>

<xsl:template match="/EDI">
<xsl:apply-templates select="Detail[count(.|key('EDI',concat(Entity,':',Quantity/HourlyBeginTime,Quantity/HourlyEndTime))[1])=1]" />
</xsl:template>

<xsl:template match="Detail">
<xsl:variable name="thekey" select="concat(Entity, ':', Quantity/HourlyBeginTime, Quantity/HourlyEndTime)"/>
<xsl:value-of select="sum(/EDI/Detail/Quantity/HourlyQuantity[concat(../../Entity, ':', ../HourlyBeginTime, ../HourlyEndTime) = $thekey])"/>
</xsl:template>
</xsl:stylesheet>

I got the gouped result by "Entity"," HourlyBeginTime" and "HourlyEndTime":

QTY+:Test -19000000
QTY+:Test 4990000
QTY+:Test 84000000

The result is missing QTY: 15000000.

This is a Muenchian Method group. Can anyone help me out on it?

Thanks in Advance!

HJ

Postnext
Ivan PedruzziSubject: Need Help --- A Special Sum Question
Author: Ivan Pedruzzi
Date: 13 May 2006 01:25 PM

Please see the attached version
It is in a more verbose form to better demostrate how grouping works.


Hope this helps
Ivan Pedruzzi
Stylus Studio Team

Postnext
Ivan PedruzziSubject: Need Help --- A Special Sum Question
Author: Ivan Pedruzzi
Date: 13 May 2006 01:26 PM
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="EDI" match="Detail/Quantity" use="concat(../Entity, ':', HourlyBeginTime, HourlyEndTime)"/>

<xsl:template match="/EDI">
<xsl:for-each select="Detail/Quantity">

<xsl:variable name="key" select="concat(../Entity,':',HourlyBeginTime,HourlyEndTime)"/>

<xsl:variable name="group" select="key('EDI', $key)"/>

<xsl:if test="generate-id(.) = generate-id( $group[1] ) ">

<xsl:variable name="quantity" select="sum($group/HourlyQuantity)"/>
<xsl:value-of select="concat(../Entity, ':', string($quantity), '&#10;')"/>

</xsl:if>

</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Ivan Pedruzzi
Stylus Studio Team

Postnext
Hongwei JiaSubject: Need Help --- A Special Sum Question
Author: Hongwei Jia
Date: 13 May 2006 11:52 PM
It works!Awesome!

Thanks a lot!

Postnext
Hongwei JiaSubject: Need Help --- A Special Sum Question
Author: Hongwei Jia
Date: 17 May 2006 08:53 PM
Is there anyway to do two level grouping? First group by Entity, then Group by Entity + HourlyBeginTime + HourlyEndTime?

Output:
Cargill:
QTY+: -19000000
QTY+: 4990000
QTY: 15000000
Accord:
QTY+: 84000000

Thank you very much for further assistance!

Postnext
Ivan PedruzziSubject: Need Help --- A Special Sum Question
Author: Ivan Pedruzzi
Date: 17 May 2006 10:53 PM

You can easily do 2 level grouping using two keys and two nested loop.

Here a solution in XSLT 2.0 that takes advantage of for-each-group


Ivan Pedruzzi
Stylus Studio Team


Unknownedi2.xsl

Postnext
Hongwei JiaSubject: Need Help --- A Special Sum Question
Author: Hongwei Jia
Date: 17 May 2006 11:52 PM
Thank you so much for your solution! Unfortunately I can't use stylesheet version="2.0" so my xslt can not contain xsl:for-each-group. I am trying to do it with version 1.0 based on your previous suggestion:

<xml version="1.0" encoding="UTF-8">
<?xml-stylesheet type="text/xsl" ><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:key name="EDI" match="Detail" use="concat(Entity, ':', Quantity/HourlyBeginTime, Quantity/HourlyEndTime)"/>
<xsl:key name="Group-Entity" match="Detail" use="Entity"/>
<xsl:template match="/EDI">
<xsl:for-each select="Detail">
<xsl:variable name="key2" select="Entity"/>
<xsl:variable name="group2" select="key('Group-Entity', $key2)"/>
<xsl:if test="generate-id(.) = generate-id( $group2[1] ) ">
<xsl:variable name="groupedEntity" select="Entity" />
<xsl:for-each select="../Detail">
<xsl:variable name="key" select="concat($groupedEntity,':',Quantity/HourlyBeginTime,Quantity/HourlyEndTime)"/>
<xsl:variable name="group" select="key('EDI', $key)"/>
<xsl:if test="generate-id(.) = generate-id( $group[1] ) ">
<xsl:variable name="quantity" select="sum($group/HourlyQuantity)"/>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each>

But the sum is zero. Could you help me out?

Thanks!

HJ

Postnext
Ivan PedruzziSubject: Need Help --- A Special Sum Question
Author: Ivan Pedruzzi
Date: 18 May 2006 12:32 AM
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="EDI" match="Detail/Quantity" use="concat(../Entity, ':', HourlyBeginTime, HourlyEndTime)"/>
<xsl:key name="Group-Entity" match="Detail" use="Entity"/>
<xsl:template match="/EDI">
<xsl:for-each select="Detail">
<xsl:variable name="key2" select="Entity"/>
<xsl:variable name="group2" select="key('Group-Entity', $key2)"/>
<xsl:if test="generate-id(.) = generate-id( $group2[1] ) ">
<xsl:value-of select="concat(Entity, ':&#10;')"/>
<xsl:variable name="groupedEntity" select="Entity"/>
<xsl:for-each select="$group2/Quantity">
<xsl:variable name="key" select="concat($groupedEntity,':',HourlyBeginTime,HourlyEndTime)"/>
<xsl:variable name="group" select="key('EDI', $key)"/>
<xsl:if test="generate-id(.) = generate-id( $group[1] ) ">
<xsl:variable name="quantity" select="sum($group/HourlyQuantity)"/>
<xsl:value-of select="concat(' ', $quantity, '&#10;')"/>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>



Ivan Pedruzzi
Stylus Studio Team

Posttop
Hongwei JiaSubject: Need Help --- A Special Sum Question
Author: Hongwei Jia
Date: 18 May 2006 08:42 AM
You are GURU! Thank you! I really appreciate!

 
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.