XSLT: Sum of specific attribute value based on other attribute's name
xml, xslt, xslt-1.0
Solution
Have a look at the following XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
Sum of TaxA where type = 11:
<xsl:value-of select="sum(transactions/transaction[@TaxAType='11']/@TaxAValue)" />
Sum of all tax where type = 11:
<xsl:value-of select="sum(transactions/transaction[@TaxAType='11']/@TaxAValue)
+ sum(transactions/transaction[@TaxBType='11']/@TaxBValue)
+ sum(transactions/transaction[@TaxCType='11']/@TaxCValue)" />
</xsl:template>
</xsl:stylesheet>
It computes sum of TaxAValue for nodes with TaxAType = 11 and sum of all Tax?Value for nodes with Tax?Type = 11.
Problem
I have the following XML document (provided by a different party) ``` <transactions> <transaction TaxAType="11" TaxAValue="111" TaxBType="2" TaxBValue="222" TaxCType="3" TaxCValue="333"/> <transaction TaxAType="11" TaxAValue="111" TaxBType="2" TaxBValue="222" TaxCType="3" TaxCValue="333"/> </transactions> ``` I would like to write an XSLT document that would transform these lines and sum up the Tax*Value if the corresponding Tax*Type = '11' for example. Is this something possible without using a template? name(), substring(), etc. functions in the XQuery? What are your thoughts on this?