How to get the sum() of node values in XSLT

2019-07-24 14:44发布

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>

标签: xml xslt sum
1条回答
啃猪蹄的小仙女
2楼-- · 2019-07-24 15:14

First of all, your input document contains a default namespace:

<container xmlns="http://www.gtech.com/lsp/2009-09-23">

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:

<CashPaymentsToday>NONQUAL</CashPaymentsToday>

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

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:gt="http://www.gtech.com/lsp/2009-09-23"
exclude-result-prefixes="gt">

    <xsl:template match="/">
    <html>
        <body>
            <xsl:value-of select="sum(//gt:CashPaymentsToday[. != 'NONQUAL'])"/>
        </body>     
    </html>
    </xsl:template>
</xsl:stylesheet>

HTML Output

<html>
   <body>310</body>
</html>
查看更多
登录 后发表回答