[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

xslt sum / group

Posted on 2005-04-14
8
Medium Priority
?
400 Views
Last Modified: 2013-11-18
<orderform id="1">
    <Items quantity="1" productid="AAA" cy_lineitem_total="72" >
       <saved_orderlevel_discounts_applied cy_discount_amount="30" discount_name="MVP Discount" discount_id="243"/>
       <saved_orderlevel_discounts_applied cy_discount_amount="10" discount_name="VIP Discount" discount_id="2"/>
    </Items>
    <Items quantity="1" productid="AAB" cy_lineitem_total="100.2" >
       <saved_orderlevel_discounts_applied cy_discount_amount="20" discount_name="MVP Discount" discount_id="243"/>
    </Items>
</orderform>

how can i transform the above xml into  the fragment below?
Notice that the saved_orderlevel_discounts_applied nodes have been grouped by discount_id and the cy_discount_amount has been summed.  
I need to do it in xslt, I am open to using the msxml extensions (? node-set() ?) but I can't use inline functions.

<orderform id="1">
    <Items quantity="1" productid="AAA" cy_lineitem_total="72" />
    <Items quantity="1" productid="AAB" cy_lineitem_total="100.2" />
       <saved_orderlevel_discounts_applied cy_discount_amount="50" discount_name="MVP Discount" discount_id="243" />
       <saved_orderlevel_discounts_applied cy_discount_amount="10" discount_name="VIP Discount" discount_id="2"/>
</orderform>
0
Comment
Question by:lunky
  • 2
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
Thogek earned 672 total points
ID: 13786619
0
 
LVL 15

Expert Comment

by:Thogek
ID: 13786663
In your XMLs above, it appears that the saved_orderlevel_discounts_applied elements are grouped regardless of which Items element each originally belonged to.  Is this intentional?  (Or should they be grouped within each Items element?)
0
 
LVL 14

Assisted Solution

by:puranik_p
puranik_p earned 664 total points
ID: 13789125
0
 
LVL 23

Assisted Solution

by:b1xml2
b1xml2 earned 664 total points
ID: 13790275
XML
===
<?xml version="1.0" encoding="utf-8" ?>
<root>
<orderform id="1">
    <Items quantity="1" productid="AAA" cy_lineitem_total="72" >
       <saved_orderlevel_discounts_applied cy_discount_amount="30" discount_name="MVP Discount" discount_id="243"/>
       <saved_orderlevel_discounts_applied cy_discount_amount="10" discount_name="VIP Discount" discount_id="2"/>
    </Items>
    <Items quantity="1" productid="AAB" cy_lineitem_total="100.2" >
       <saved_orderlevel_discounts_applied cy_discount_amount="20" discount_name="MVP Discount" discount_id="243"/>
    </Items>
</orderform>
<orderform id="2">
    <Items quantity="3" productid="AAA" cy_lineitem_total="72" >
       <saved_orderlevel_discounts_applied cy_discount_amount="50" discount_name="MVP Discount 2" discount_id="242"/>
       <saved_orderlevel_discounts_applied cy_discount_amount="10" discount_name="VIP Discount 2" discount_id="1"/>
    </Items>
    <Items quantity="1" productid="AAB" cy_lineitem_total="100.2" >
       <saved_orderlevel_discounts_applied cy_discount_amount="40" discount_name="MVP Discount 2" discount_id="242"/>
    </Items>
</orderform>
</root>


XSLT
====
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no" />
<xsl:key name="saved" match="saved_orderlevel_discounts_applied" use="concat(@discount_id,':',ancestor::orderform/@id)" />
<xsl:template match="@* | node()">
<xsl:copy><xsl:apply-templates select="@* | node()" /></xsl:copy>
</xsl:template>

<xsl:template match="orderform">
<xsl:variable name="id" select="string(@id)" />
<orderform>
<xsl:copy-of select="@*" />
<xsl:apply-templates select="Items" />
<xsl:apply-templates select="descendant::saved_orderlevel_discounts_applied[count(.|key('saved',concat(@discount_id,':',$id))[1]) = 1]" mode="group"/>
</orderform>
</xsl:template>

<!-- dont process elements inside Items -->
<xsl:template match="Items/*" />

<!-- processing the descendant accordingly -->
<xsl:template match="saved_orderlevel_discounts_applied" mode="group">
<saved_orderlevel_discounts_applied><xsl:apply-templates select="@*" /></saved_orderlevel_discounts_applied>
</xsl:template>

<!-- process the sum attribute -->
<xsl:template match="saved_orderlevel_discounts_applied/@cy_discount_amount">
<xsl:attribute name="cy_discount_amount"><xsl:value-of select="sum(key('saved',concat(../@discount_id,':',ancestor::orderform/@id))/@cy_discount_amount)" /></xsl:attribute>
</xsl:template>

</xsl:stylesheet>

 
Output
=====
<?xml version="1.0" encoding="utf-8"?>
<root>
      <orderform id="1">\
            <Items quantity="1" productid="AAA" cy_lineitem_total="72"></Items>
            <Items quantity="1" productid="AAB" cy_lineitem_total="100.2"></Items>
            <saved_orderlevel_discounts_applied cy_discount_amount="50" discount_name="MVP Discount" discount_id="243"></saved_orderlevel_discounts_applied>
            <saved_orderlevel_discounts_applied cy_discount_amount="10" discount_name="VIP Discount" discount_id="2"></saved_orderlevel_discounts_applied>
      </orderform>
      <orderform id="2">
            <Items quantity="3" productid="AAA" cy_lineitem_total="72"></Items>
            <Items quantity="1" productid="AAB" cy_lineitem_total="100.2"></Items>
            <saved_orderlevel_discounts_applied cy_discount_amount="90" discount_name="MVP Discount 2" discount_id="242"></saved_orderlevel_discounts_applied>
            <saved_orderlevel_discounts_applied cy_discount_amount="10" discount_name="VIP Discount 2" discount_id="1"></saved_orderlevel_discounts_applied>
      </orderform>
</root>
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13935193
*knock* *knock* anybody home?
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question