I have requirement to group based on "TaxRateCode" tag and filter based on "LineType/Code='AWT'" and do the summation of the "LineGrossAmount/Value" for each of OutboundPayment tag.
XML below
<?xml version="1.0" encoding="UTF-8"?>
<OutboundPaymentInstruction>
<OutboundPayment>
<DocumentPayable>
<DocumentPayableLine>
<LineType>
<Code>TAX</Code>
<Meaning />
</LineType>
<LineDescription>CHANDLER BING</LineDescription>
<LineGrossAmount>
<Value>3098.76</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH VAT</TaxCode>
<TaxRate>7</TaxRate>
<TaxRateCode>TH AP STD SVC 7</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>AWT</Code>
<Meaning />
</LineType>
<LineDescription>CHANDLER BING</LineDescription>
<LineGrossAmount>
<Value>-1328.04</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH WHT</TaxCode>
<TaxRate>3</TaxRate>
<TaxRateCode>TH WHT SVCS 03</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>ITEM</Code>
<Meaning />
</LineType>
<LineDescription>CHANDLER - INV2103-0185</LineDescription>
<LineGrossAmount>
<Value>44268</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode />
<TaxRate>0</TaxRate>
<TaxRateCode />
</Tax>
</DocumentPayableLine>
</DocumentPayable>
</OutboundPayment>
<OutboundPayment>
<DocumentPayable>
<DocumentPayableLine>
<LineType>
<Code>TAX</Code>
<Meaning />
</LineType>
<LineDescription>TOT-760589905 checkin</LineDescription>
<LineGrossAmount>
<Value>2241.33</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH VAT</TaxCode>
<TaxRate>7</TaxRate>
<TaxRateCode>TH AP STD SVC 7</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>AWT</Code>
<Meaning />
</LineType>
<LineDescription>TOT-760589905 checkin</LineDescription>
<LineGrossAmount>
<Value>-1525</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH WHT</TaxCode>
<TaxRate>5</TaxRate>
<TaxRateCode>TH WHT RENT 05</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>AWT</Code>
<Meaning />
</LineType>
<LineDescription>TOT-760589905 (21/12/2563 - 20/01/2564 ) Voice for outbound and inbound (Local Call) Link 2</LineDescription>
<LineGrossAmount>
<Value>-45.57</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH WHT</TaxCode>
<TaxRate>3</TaxRate>
<TaxRateCode>TH WHT SVCS 03</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>ITEM</Code>
<Meaning />
</LineType>
<LineDescription>TOT-760589905 (21/12/2563 - 20/01/2564 ) Voice for outbound and inbound (Local Call) Link 2</LineDescription>
<LineGrossAmount>
<Value>30500</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode />
<TaxRate>0</TaxRate>
<TaxRateCode />
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>ITEM</Code>
<Meaning />
</LineType>
<LineDescription>TOT-760589905 (21/12/2563 - 20/01/2564 ) Voice for outbound and inbound (Local Call) Link 2</LineDescription>
<LineGrossAmount>
<Value>1519</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode />
<TaxRate>0</TaxRate>
<TaxRateCode />
</Tax>
</DocumentPayableLine>
</DocumentPayable>
<DocumentPayable>
<DocumentPayableLine>
<LineType>
<Code>TAX</Code>
<Meaning />
</LineType>
<LineDescription>TOT-763687229. ( 21/01/2564 - 20/02/2564 ) Voice for outbound and inbound (Local Call) Link 2</LineDescription>
<LineGrossAmount>
<Value>2241.4</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH VAT</TaxCode>
<TaxRate>7</TaxRate>
<TaxRateCode>TH AP STD SVC 7</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>AWT</Code>
<Meaning />
</LineType>
<LineDescription>TOT-763687229. ( 21/01/2564 - 20/02/2564 ) Voice for outbound and inbound (Local Call) Link 2</LineDescription>
<LineGrossAmount>
<Value>-1525</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH WHT</TaxCode>
<TaxRate>5</TaxRate>
<TaxRateCode>TH WHT RENT 05</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>AWT</Code>
<Meaning />
</LineType>
<LineDescription>TOT-763687229. ( 21/01/2564 - 20/02/2564 ) Voice for outbound and inbound (Local Call) Link 2</LineDescription>
<LineGrossAmount>
<Value>-45.6</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH WHT</TaxCode>
<TaxRate>3</TaxRate>
<TaxRateCode>TH WHT SVCS 03</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>ITEM</Code>
<Meaning />
</LineType>
<LineDescription>TOT-763687229. ( 21/01/2564 - 20/02/2564 )Voice for outbound and inbound (Local Call) Link 2</LineDescription>
<LineGrossAmount>
<Value>30500</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode />
<TaxRate>0</TaxRate>
<TaxRateCode />
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>ITEM</Code>
<Meaning />
</LineType>
<LineDescription>TOT-763687229. ( 21/01/2564 - 20/02/2564 )Voice for outbound and inbound (Local Call) Link 2</LineDescription>
<LineGrossAmount>
<Value>1520</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode />
<TaxRate>0</TaxRate>
<TaxRateCode />
</Tax>
</DocumentPayableLine>
</DocumentPayable>
<DocumentPayable>
<DocumentPayableLine>
<LineType>
<Code>TAX</Code>
<Meaning />
</LineType>
<LineDescription>TOT- 763687230 Utilities</LineDescription>
<LineGrossAmount>
<Value>3193.33</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH VAT</TaxCode>
<TaxRate>7</TaxRate>
<TaxRateCode>TH AP STD SVC 7</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>AWT</Code>
<Meaning />
</LineType>
<LineDescription>TOT- 763687230 Utilities</LineDescription>
<LineGrossAmount>
<Value>-2110</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH WHT</TaxCode>
<TaxRate>5</TaxRate>
<TaxRateCode>TH WHT RENT 05</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>AWT</Code>
<Meaning />
</LineType>
<LineDescription>TOT- 763687230 Utilities</LineDescription>
<LineGrossAmount>
<Value>-102.57</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH WHT</TaxCode>
<TaxRate>3</TaxRate>
<TaxRateCode>TH WHT SVCS 03</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>ITEM</Code>
<Meaning />
</LineType>
<LineDescription>TOT- 763687230 Utilities</LineDescription>
<LineGrossAmount>
<Value>42200</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode />
<TaxRate>0</TaxRate>
<TaxRateCode />
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>ITEM</Code>
<Meaning />
</LineType>
<LineDescription>TOT- 763687230 Utilities</LineDescription>
<LineGrossAmount>
<Value>3419</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode />
<TaxRate>0</TaxRate>
<TaxRateCode />
</Tax>
</DocumentPayableLine>
</DocumentPayable>
<DocumentPayable>
<DocumentPayableLine>
<LineType>
<Code>TAX</Code>
<Meaning />
</LineType>
<LineDescription>TOT 763955166 Utilities</LineDescription>
<LineGrossAmount>
<Value>1.81</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH VAT</TaxCode>
<TaxRate>7</TaxRate>
<TaxRateCode>TH AP STD SVC 7</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>AWT</Code>
<Meaning />
</LineType>
<LineDescription>TOT 763955166 Utilities</LineDescription>
<LineGrossAmount>
<Value>-1.29</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH WHT</TaxCode>
<TaxRate>4.998063</TaxRate>
<TaxRateCode>TH WHT RENT 05</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>ITEM</Code>
<Meaning />
</LineType>
<LineDescription>TOT 763955166 Utilities</LineDescription>
<LineGrossAmount>
<Value>25.81</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode />
<TaxRate>0</TaxRate>
<TaxRateCode />
</Tax>
</DocumentPayableLine>
</DocumentPayable>
<DocumentPayable>
<DocumentPayableLine>
<LineType>
<Code>TAX</Code>
<Meaning />
</LineType>
<LineDescription>TOT 763955167 Utilities</LineDescription>
<LineGrossAmount>
<Value>1.81</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH VAT</TaxCode>
<TaxRate>7</TaxRate>
<TaxRateCode>TH AP STD SVC 7</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>AWT</Code>
<Meaning />
</LineType>
<LineDescription>TOT 763955167 Utilities</LineDescription>
<LineGrossAmount>
<Value>-1.29</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode>TH WHT</TaxCode>
<TaxRate>4.998063</TaxRate>
<TaxRateCode>TH WHT RENT 05</TaxRateCode>
</Tax>
</DocumentPayableLine>
<DocumentPayableLine>
<LineType>
<Code>ITEM</Code>
<Meaning />
</LineType>
<LineDescription>TOT 763955167 Utilities</LineDescription>
<LineGrossAmount>
<Value>25.81</Value>
<Currency>
<Code>THB</Code>
<NumericCode>764</NumericCode>
</Currency>
</LineGrossAmount>
<Tax>
<TaxCode />
<TaxRate>0</TaxRate>
<TaxRateCode />
</Tax>
</DocumentPayableLine>
</DocumentPayable>
</OutboundPayment>
</OutboundPaymentInstruction>
Expected Output
<TAX>
<Rcrd>
<TaxAmt>
<Rate>3</Rate>
<calc>1328.04</calc>
</TaxAmt>
</Rcrd>
</TAX>
<TAX>
<Rcrd>
<TaxAmt>
<Rate>5</Rate>
<calc>5162.58</calc>
</TaxAmt>
</Rcrd>
<Rcrd>
<TaxAmt>
<Rate>3</Rate>
<calc>193.74</calc>
</TaxAmt>
</Rcrd>
</TAX>
I have tried with the below code, but it didn't give the expected output. It is having issue while using the "DocumentPayable" Tag within its own "OutboundPayment" tag. Please help with the solution. Please also note that our requirement supports only XSLT 1.0 version, Hence I have used the Muenchain grouping method.
Code tried
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="group1" match="DocumentPayable/DocumentPayableLine" use="concat(generate-id(..), Tax/TaxRateCode)"/>
<xsl:template match="/">
<xsl:for-each select="/OutboundPaymentInstruction/OutboundPayment">
<TAX>
<xsl:for-each select="DocumentPayable/DocumentPayableLine[count(. | key('group1', concat(generate-id(..), Tax/TaxRateCode))[1]) = 1]">
<xsl:if test="(LineType/Code='AWT')">
<xsl:variable name="LocationCode" select="Tax/TaxRateCode"/>
<Rcrd>
<TaxAmt>
<Rate>
<xsl:if test="not(Tax/TaxRate='')">
<xsl:value-of select="round(Tax/TaxRate)"/>
</xsl:if>
</Rate>
<xsl:variable name="calcualtedVal1">
<xsl:value-of select="translate(sum(//DocumentPayableLine[./Tax/TaxRateCode= $LocationCode]/LineGrossAmount/Value),'-','')"/>
</xsl:variable>
<calc>
<xsl:value-of select="$calcualtedVal1"/>
</calc>
</TaxAmt>
</Rcrd>
</xsl:if>
</xsl:for-each>
</TAX>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>