xml 如何在 XSLT 中获取节点值的 sum()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29832511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:07:54  来源:igfitidea点击:

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

xmlxsltsum

提问by lslep

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.

我正在尝试计算数字节点的总和。以下代码不返回除 HTML 之外的任何内容。我知道 sum() 需要一个节点集。我想我已经正确地创建了变量。我究竟做错了什么。

Thanks.

谢谢。

XML Example:

XML 示例:

<?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:

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>

回答by Mathias Müller

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 CashPaymentsTodayelements do not contain a number:

然后,当您可以直接对感兴趣的节点求和时,绝对不需要构造变量 - 但某些CashPaymentsToday元素不包含数字:

<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 fragmentas an argument. Your approach should not "only return html", it should actually fail.

您需要从总和中排除这些元素。最后,您当前所做的在 XSLT 1.0 中是不可能的 - 因为该sum()函数不能将所谓的结果树片段作为参数。你的方法不应该“只返回 html”,它实际上应该失败。

XSLT Stylesheet

XSLT 样式表

<?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 输出

<html>
   <body>310</body>
</html>