Reputation: 163
I would like to convert below XML through XSLT by groping based on Name tag and get the total sum of Amount tag with the condition (Status = 'Draft') . Please advise
Input XML:
<?xml version='1.0' encoding='UTF-8'?>
<wd:Data xmlns:wd="urn:com.test/bsvc">
<wd:Entry>
<wd:Name>ABC</wd:Name>
<wd:ID>123</wd:ID>
<wd:Amount>10</wd:Amount>
<wd:Status>Pending</wd:Status>
</wd:Entry>
<wd:Entry>
<wd:Name>ABC</wd:Name>
<wd:ID>123</wd:ID>
<wd:Amount>20</wd:Amount>
<wd:Status>Draft</wd:Status>
</wd:Entry>
<wd:Entry>
<wd:Name>ABC</wd:Name>
<wd:ID>123</wd:ID>
<wd:Amount>30</wd:Amount>
<wd:Status>Draft</wd:Status>
</wd:Entry>
</wd:Data>
XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wd="urn:com.test/bsvc" exclude-result-prefixes="wd">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:for-each-group select="wd:Data/wd:Entry" group-by="wd:Name">
<Details>
<ID>
<xsl:value-of select="wd:ID"/>
</ID>
<Name>
<xsl:value-of select="wd:Name"/>
</Name>
<Total_Unverified_Amount>
<xsl:value-of select='format-number(sum(current-group()/wd:Amount),"##.00")'/>
</Total_Unverified_Amount>
<Charges_Draft>
<xsl:value-of select="sum(current-group()/wd:Amount[current-group()/wd:Status = 'Draft'])"/>
</Charges_Draft>
</Details>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
Output generated:
<Details>
<ID>123</ID>
<Name>ABC</Name>
<Total_Unverified_Amount>60.00</Total_Unverified_Amount>
<Charges_Draft>60</Charges_Draft>
</Details>
Desired Output:
<Details>
<ID>123</ID>
<Name>ABC</Name>
<Total_Unverified_Amount>60.00</Total_Unverified_Amount>
<Charges_Draft>50</Charges_Draft>
</Details>
I would like to convert below XML through XSLT by groping based on Name tag and get the total sum of Amount tag with the condition (Status = 'Draft') . Please advise
Upvotes: 0
Views: 1139
Reputation: 117140
Instead of:
<Charges_Draft>
<xsl:value-of select="sum(current-group()/wd:Amount[current-group()/wd:Status = 'Draft'])"/>
</Charges_Draft>
use:
<Charges_Draft>
<xsl:value-of select="sum(current-group()[wd:Status = 'Draft']/wd:Amount)"/>
</Charges_Draft>
To clarify: your predicate:
[current-group()/wd:Status = 'Draft']
returns true whenever there is at least one wd:Entry
in the current group whose wd:Status
is "Draft"
. This result is then applied to all members of the current group being tested alike.
Upvotes: 2