Reputation: 77
I'm trying to transform an xml. The old xsl was written by someone else and it is quite static, looks for every node and writes them. But I need to change it to dynamic way. Here is the xml file (simplified version):
<?xml version="1.0" encoding="UTF-8"?>
<csprint>
<csrequest>
<p:Body xmlns:p="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="wsdl.http://isbank.com/OpSvcs/PaymentMgmtProcessing/TaxCollection/Service/V1" xmlns:v1_1="http://isbank.com/OpSvcs/PaymentMgmtProcessing/TaxCollection/Service/V1" xmlns:v1_2="http://isbank.com/OpSvcs/PaymentMgmtProcessing/Tax/Schema/V1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<v1_1:collect>
<v1_1:collectionData>
<v1_2:taxOfficeNo>34256</v1_2:taxOfficeNo>
<v1_2:mainTaxCode>9077</v1_2:mainTaxCode>
<v1_2:debtLineItem>
<v1_2:taxCode>1086</v1_2:taxCode>
<v1_2:installmentNumber>1</v1_2:installmentNumber>
<v1_2:dueDate>2011-02-28</v1_2:dueDate>
<v1_2:amount>
<amount>174134.40</amount>
</v1_2:amount>
<v1_2:taxShortName>Y.DIŞI ÇKŞ.H</v1_2:taxShortName>
</v1_2:debtLineItem>
<v1_2:debtLineItem>
<v1_2:taxCode>1086</v1_2:taxCode>
<v1_2:installmentNumber>1</v1_2:installmentNumber>
<v1_2:dueDate>2011-02-28</v1_2:dueDate>
<v1_2:amount>
<amount>174134.40</amount>
</v1_2:amount>
<v1_2:taxShortName>Y.DIŞI ÇKŞ.H</v1_2:taxShortName>
</v1_2:debtLineItem>
<v1_2:debtLineItem>
<v1_2:taxCode>9014</v1_2:taxCode>
<v1_2:installmentNumber>1</v1_2:installmentNumber>
<v1_2:dueDate>2011-02-28</v1_2:dueDate>
<v1_2:amount>
<amount>174134.40</amount>
</v1_2:amount>
<v1_2:taxShortName>Y.DIŞI ÇKŞ.H</v1_2:taxShortName>
</v1_2:debtLineItem>
<v1_2:debtLineItem>
<v1_2:taxCode>9014</v1_2:taxCode>
<v1_2:installmentNumber>1</v1_2:installmentNumber>
<v1_2:dueDate>2011-02-28</v1_2:dueDate>
<v1_2:amount>
<amount>174134.40</amount>
</v1_2:amount>
<v1_2:taxShortName>Y.DIŞI ÇKŞ.H</v1_2:taxShortName>
</v1_2:debtLineItem>
</v1_1:collectionData>
</v1_1:collect>
</p:Body>
</csrequest>
</csprint>
and here is my condition:
if (mainTaxCode == 9077 && taxCode == 1086)
sum /debtLineItem/amount/amount of all 1086 nodes and write just once
else
write all
and for this instance the result should look like
TaxCode DueDate Amount
1086 2011-02-28 348268.80
9014 2011-02-28 174134.40
9014 2011-02-28 174134.40
I cannot move on any further, keeps summing all 3 values. Any help appreciated. Thanks..
Upvotes: 2
Views: 889
Reputation: 24826
This transformation outputs the wanted data exactly according to the described condition (I think :P). Moreover conditional values are parametric (default to 9007 and 1086).
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v1_1="http://isbank.com/OpSvcs/PaymentMgmtProcessing/TaxCollection/Service/V1"
xmlns:v1_2="http://isbank.com/OpSvcs/PaymentMgmtProcessing/Tax/Schema/V1">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:param name="mainTaxCode" select="9077"/>
<xsl:param name="taxCode" select="1086"/>
<xsl:key name="k" match="v1_2:debtLineItem"
use="v1_2:taxCode"/>
<xsl:template match="v1_1:collect">
<xsl:apply-templates select="
v1_1:collectionData
[v1_2:mainTaxCode=$mainTaxCode]/
v1_2:debtLineItem
[generate-id()
= generate-id(key('k', v1_2:taxCode)[1])]
[v1_2:taxCode=$taxCode]
|
v1_1:collectionData/
v1_2:debtLineItem
[not(v1_2:taxCode=$taxCode)]"/>
</xsl:template>
<xsl:template match="v1_2:debtLineItem">
<xsl:value-of select="concat(
v1_2:taxCode, ' ',
v1_2:dueDate, ' ')"/>
<xsl:apply-templates select="v1_2:taxCode[.=$taxCode]" mode="sum"/>
<xsl:apply-templates select="v1_2:taxCode[.!=$taxCode]"/>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="v1_2:taxCode" mode="sum">
<xsl:value-of select="
format-number(sum(key('k',.)/v1_2:amount/*), '#.00')"/>
</xsl:template>
<xsl:template match="v1_2:taxCode">
<xsl:value-of select="format-number(../v1_2:amount, '#.00')"/>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 56212
If I understand right: this template groups by v1_2:taxCode
and sums v1_2:amount/amount
:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v1_2="http://isbank.com/OpSvcs/PaymentMgmtProcessing/Tax/Schema/V1">
<xsl:output method="text" indent="yes" />
<xsl:key name="k" match="v1_2:debtLineItem" use="v1_2:taxCode"/>
<xsl:template match="/">
<xsl:apply-templates select="//v1_2:debtLineItem[generate-id(.) = generate-id(key('k', v1_2:taxCode))]"/>
</xsl:template>
<xsl:template match="v1_2:debtLineItem">
<xsl:value-of select="concat(
v1_2:taxCode, ' ',
v1_2:dueDate, ' ',
format-number(sum(//v1_2:debtLineItem[v1_2:taxCode = current()/v1_2:taxCode]/v1_2:amount/*), '#.00'))"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
Output:
1086 2011-02-28 348268.80
9014 2011-02-28 174134.40
Upvotes: 0