I am trying to calculate the sum of numeric nodes. The following code doesn't return anything except the HTML. I know that sum() takes a nodeset. I think I have created the variable correctly. What am I doing wrong.
Thanks.
XML Example:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<container xmlns="http://www.gtech.com/lsp/2009-09-23">
<Root>
<Incentives>
<Date>2015-03-14</Date>
<ID>507200</ID>
<ProgramName>Retailer Cash Incentive 16</ProgramName>
<Retailer>
<ID>507201</ID>
<Name>Acme #2102</Name>
<Detail>
<CashPaymentsToday>50.00</CashPaymentsToday>
<Potential>0</Potential>
</Detail>
</Retailer>
<Retailer>
<ID>507202</ID>
<Name>Acme #2103</Name>
<Detail>
<CashPaymentsToday>60.00</CashPaymentsToday>
<Potential>0</Potential>
</Detail>
</Retailer>
<Retailer>
<ID>507203</ID>
<Name>Acme #1008</Name>
<Detail>
<CashPaymentsToday>0.00</CashPaymentsToday>
<Potential>0</Potential>
</Detail>
</Retailer>
<Retailer>
<ID>507207</ID>
<Name>Acme #2228</Name>
<Detail>
<CashPaymentsToday>200.00</CashPaymentsToday>
<Potential>3</Potential>
</Detail>
</Retailer>
<Retailer>
<ID>598419</ID>
<Name>Acme NO 1071</Name>
<Detail>
<CashPaymentsToday>NONQUAL</CashPaymentsToday>
<Potential>NONQUAL</Potential>
</Detail>
</Retailer>
<Retailer>
<ID>598421</ID>
<Name>Acme NO 1072</Name>
<Detail>
<CashPaymentsToday>NONQUAL</CashPaymentsToday>
<Potential>NONQUAL</Potential>
</Detail>
</Retailer>
</Incentives>
</Root>
</container>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:variable name="cashPayments">
<xsl:for-each select="//Detail">
<xsl:if test="CashPaymentsToday"/>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="sum($cashPayments)"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
First of all, your input document contains a default namespace:
that you need to take into account, i.e. redeclare in your stylesheet and prefix elements when referring to the input document.
Then, there is absolutely no need to construct a variable when you can directly sum the nodes you are interested in - but some of the
CashPaymentsToday
elements do not contain a number:You need to exclude those elements from the sum. Finally, what you are currently doing is not possible in XSLT 1.0 - because the
sum()
function cannot take a so-called result tree fragment as an argument. Your approach should not "only return html", it should actually fail.XSLT Stylesheet
HTML Output