Reputation: 1233
I am working on combining XML elements using for-each-group
and the problem I am having is that these XML elements have children elements that have children of their own.
I would also like to combine the children elements as well.
My input XML looks like this:
<Root>
<Header>
</Header>
<Summary ID="1">
<ID>1</ID>
<Batch_Date>0228</Batch_Date>
<Net_Sales_Amount>340.00</Net_Sales_Amount>
<DATA_From_2>
<Sales_Amount>340.00</_Sales_Amount>
</DATA_From_2>
</Summary>
<Summary ID="1">
<ID>1</ID>
<Batch_Date>0228</Batch_Date>
<Batch_Number>04002</Batch_Number>
<Net_Sales_Amount>130.00</Net_Sales_Amount>
<DATA_From_2>
<Sales_Amount>130.00</Sales_Amount>
</DATA_From_2>
<DATA_From_8>
<Invoice_Amount>20</Invoice_Amount>
<Invoice_Number>123</Invoice_Number>
</DATA_From_8>
<DATA_From_8>
<Invoice_Amount>15</Invoice_Amount>
<Invoice_Number>456</Invoice_Number>
</DATA_From_8>
</Summary>
<Summary ID="2">
<ID>2</ID>
<Batch_Date>0228</Batch_Date>
<Net_Sales_Amount>40.00</Net_Sales_Amount>
<DATA_From_2>
<Sales_Amount>40.00</Sales_Amount>
</DATA_From_2>
</Summary>
<Summary ID="2">
<ID>2</ID>
<Batch_Date>0228</Batch_Date>
<Net_Sales_Amount>819.91</Net_Sales_Amount>
<DATA_From_2>
<Sales_Amount>734.91</Sales_Amount>
</DATA_From_2>
<DATA_From_5>
<Sales_Amount>85.00</Sales_Amount>
</DATA_From_5>
</Summary>
</Root>
Here is my XSLT file. I get close to my desired output, but instead of combining the same children elements, it puts all of them inside the Summary
element.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="/*">
<xsl:copy>
<Root>
<xsl:for-each-group select="/Root/Summary" group-by="@ID">
<Summary ID="{@ID}">
<!-- Copy values that are the same -->
<xsl:sequence select="ID, Batch_Date"/>
<!-- Sum the amounts -->
<Net_Sales_Amount>
<xsl:value-of select="format-number(sum(current-group()/Net_Sales_Amount), '0.00')"/>
</Net_Sales_Amount>
<!-- Combine the record types in this Summary element -->
<!-- <xsl:sequence select="DATA_From_2, DATA_From_3, DATA_From_5, DATA_From_8"/> -->
<xsl:for-each-group select="current-group()" group-by="DATA_From_2">
<Data_From_2>
<Sales_Amount>
<xsl:value-of select="format-number(sum(current-group()/Data_From_2/Combined_Sales_Amount), '0.00')"/>
</Sales_Amount>
</Data_From_2>
</xsl:for-each-group>
<xsl:for-each-group select="current-group()" group-by="DATA_From_8">
<Data_From_8>
<Invoice_Amount>
<xsl:value-of select="format-number(sum(current-group()/Data_From_8/Invoice_Amount), '0.00')"/>
</Invoice_Amount>
<Invoice_Number>
<xsl:value-of select="current-group()/Data_From_8/Invoice_Number" separator=" | "/>
</Invoice_Number>
</Data_From_8>
</xsl:for-each-group>
</Summary>
</xsl:for-each-group>
<Header>
</Header>
<Footer>
</Footer>
</Root>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Here is my current output:
<Root>
<Summary ID="1">
<ID>1</ID>
<Batch_Date>0228</Batch_Date>
<Net_Sales_Amount>470.00</Net_Sales_Amount>
<Data_From_2>
<Sales_Amount>0.00</Sales_Amount>
</Data_From_2>
<Data_From_2>
<Sales_Amount>0.00</Sales_Amount>
</Data_From_2>
<Data_From_8>
<Invoice_Amount>0.00</Invoice_Amount>
<Invoice_Number/>
</Data_From_8>
<Data_From_8>
<Invoice_Amount>0.00</Invoice_Amount>
<Invoice_Number/>
</Data_From_8>
</Summary>
<Summary ID="2">
<ID>2</ID>
<Batch_Date>0228</Batch_Date>
<Net_Sales_Amount>859.91</Net_Sales_Amount>
<Data_From_2>
<Sales_Amount>0.00</Sales_Amount>
</Data_From_2>
<Data_From_2>
<Sales_Amount>0.00</Sales_Amount>
</Data_From_2>
<Data_From_2>
<Sales_Amount>0.00</Sales_Amount>
</Data_From_2>
</Summary>
</Root>
The XSLT is not combining the Data_From elements that are the same, and it's also not populating the values correctly.
This is what it should look like:
<Root>
<Summary ID="1">
<ID>1</ID>
<Batch_Date>0228</Batch_Date>
<Net_Sales_Amount>470.00</Net_Sales_Amount>
<Data_From_2>
<Sales_Amount>470.00</Sales_Amount>
</Data_From_2>
<Data_From_8>
<Invoice_Amount>35.00</Invoice_Amount>
<Invoice_Number/>
</Data_From_8>
</Summary>
<Summary ID="2">
<ID>2</ID>
<Batch_Date>0228</Batch_Date>
<Net_Sales_Amount>859.91</Net_Sales_Amount>
<Data_From_2>
<Sales_Amount>771.91</Sales_Amount>
</Data_From_2>
<DATA_From_5>
<Sales_Amount>85.00</Sales_Amount>
</DATA_From_5>
</Summary>
</Root>
The XSLT should combine the similar elements (Data_from_2, Data_from_5, etc) and add up the values. I apologize for any typos or incorrect XML, I did a lot of copy and paste. Thanks
Upvotes: 2
Views: 1619
Reputation: 167561
I think the following is what you want:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="/*">
<xsl:copy>
<Root>
<xsl:for-each-group select="/Root/Summary" group-by="@ID">
<Summary Merchant_ID="{@ID}">
<!-- Copy values that are the same -->
<xsl:sequence select="ID, Batch_Date"/>
<!-- Sum the amounts -->
<Net_Sales_Amount>
<xsl:value-of select="format-number(sum(current-group()/Net_Sales_Amount), '0.00')"/>
</Net_Sales_Amount>
<!-- Combine the record types in this Summary element -->
<!-- <xsl:sequence select="DATA_From_2, DATA_From_3, DATA_From_5, DATA_From_8"/> -->
<xsl:for-each-group select="current-group()/*[starts-with(local-name(), 'DATA_')]" group-by="node-name(.)">
<xsl:apply-templates select="."/>
</xsl:for-each-group>
</Summary>
</xsl:for-each-group>
<Header>
</Header>
<Footer>
</Footer>
</Root>
</xsl:copy>
</xsl:template>
<xsl:template match="DATA_From_2 | DATA_From_5">
<xsl:copy>
<Sales_Amount>
<xsl:value-of select="format-number(sum(current-group()/Sales_Amount), '0.00')"/>
</Sales_Amount>
</xsl:copy>
</xsl:template>
<xsl:template match="DATA_From_8">
<xsl:copy>
<Invoice_Amount>
<xsl:value-of select="format-number(sum(current-group()/Invoice_Amount), '0.00')"/>
</Invoice_Amount>
<Invoice_Number>
<xsl:value-of select="current-group()/Invoice_Number" separator=" | "/>
</Invoice_Number>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2