Xslt XSL sum未显示正确的值

Xslt XSL sum未显示正确的值,xslt,xslt-1.0,xsl-fo,Xslt,Xslt 1.0,Xsl Fo,我需要对下面XML字段的值求和,其中最终值应该是335 <charges> <fees>25</fees> <service> <servtypes>Service 1</servtypes> <servamt>150</servamt> </service> <service> <servt

我需要对下面XML字段的值求和,其中最终值应该是335

<charges>
    <fees>25</fees>
    <service>
        <servtypes>Service 1</servtypes>
        <servamt>150</servamt>
    </service>
    <service>
        <servtypes>Service 2</sertypes>
        <servamt>10</servamt>
    </service>
    <service>
        <servtypes>Service 3</servtypes>
        <servamt>150</servamt>
    </service>
<charges>

25
服务1
150
服务2
10
服务3
150
这是我用来显示数据的一段XSL代码。问题是,如果我包括sum函数,它应该对所有servamt求和,它会给我NaN值。 你怎么能修好它?另外,我如何在所有服务金额的总和上添加费用

<fo:table-row>
<fo:table-cell display-align="center">
<fo:block>Fees</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block><xsl:value-of select="fees"/></fo:block>
</fo:table-cell>
<fo:table-cell display-align="center">
<fo:block><fo:leader/></fo:block>
</fo:table-cell>
</fo:table-row>

<xsl:for-each select="service">
<fo:table-row>
<fo:table-cell display-align="center">
<fo:block><xsl:value-of select="servtypes"/></fo:block>
</fo:table-cell>
<fo:table-cell >
**<fo:block><xsl:value-of select="sum((charges/fees | service/servamt))"/>**</fo:block>
</fo:table-cell>
<fo:table-cell display-align="center">
<fo:block><fo:leader/></fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>

费用
****

在与
费用
服务
元素的父元素匹配的模板规则中,调用

sum((fees | service/servamt))

如果您的XML如下所示(在您的XML中,似乎缺少根元素):


25
服务1
150
服务2
10
服务3
150
然后,您的代码将在XSL中进行以下计算:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:template match="/">
            <fo:table-row>
                <fo:table-cell display-align="center">
                    <fo:block>Fees</fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block>
                        <xsl:value-of select="/root/fees"/>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell display-align="center">
                    <fo:block>
                        <fo:leader/>
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
            <xsl:for-each select="/root/service">
                <fo:table-row>
                    <fo:table-cell display-align="center">
                        <fo:block>
                            <xsl:value-of select="sertypes"/>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell >
                        <fo:block>
                            <xsl:value-of select="sum(//servamt) + /root/fees"/>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell display-align="center">
                        <fo:block>
                            <fo:leader/>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

费用
结果是:

<?xml version="1.0" encoding="UTF-8"?>
<fo:table-row xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:table-cell display-align="center">
        <fo:block>Fees</fo:block>
    </fo:table-cell>
    <fo:table-cell>
        <fo:block>25</fo:block>
    </fo:table-cell>
    <fo:table-cell display-align="center">
        <fo:block>
            <fo:leader/>
        </fo:block>
    </fo:table-cell>
</fo:table-row>
<fo:table-row xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:table-cell display-align="center">
        <fo:block>Service 1</fo:block>
    </fo:table-cell>
    <fo:table-cell>
        <fo:block>335</fo:block>
    </fo:table-cell>
    <fo:table-cell display-align="center">
        <fo:block>
            <fo:leader/>
        </fo:block>
    </fo:table-cell>
</fo:table-row>
<fo:table-row xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:table-cell display-align="center">
        <fo:block>Service 2</fo:block>
    </fo:table-cell>
    <fo:table-cell>
        <fo:block>335</fo:block>
    </fo:table-cell>
    <fo:table-cell display-align="center">
        <fo:block>
            <fo:leader/>
        </fo:block>
    </fo:table-cell>
</fo:table-row>
<fo:table-row xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:table-cell display-align="center">
        <fo:block>Service 3</fo:block>
    </fo:table-cell>
    <fo:table-cell>
        <fo:block>335</fo:block>
    </fo:table-cell>
    <fo:table-cell display-align="center">
        <fo:block>
            <fo:leader/>
        </fo:block>
    </fo:table-cell>
</fo:table-row>

费用
25
服务1
335
服务2
335
服务3
335

希望它能有所帮助。

您正在对
seramt
节点进行求和,但在XML中它是
servamt
(带一个“v”),请注意,当您进行
sum
时,您位于
服务
节点上,因此执行
sum(servamt)
只会对
服务
节点下的
servamt节点求和。如果你只有一个这样的节点,你就不需要使用sum。感谢Michael的回答,我尝试了你的建议(请参见我的编辑),但结果为零。然后你做错了什么,如果你想帮助我们找出什么,你必须向我们展示你到底做了什么。也许这是另一个拼写错误,就像在你原来的帖子中一样。sum()是一个应用于一组数字的函数,你不想为每个数字调用它一次。您需要将呼叫移出该循环。此外,你必须考虑上下文。如果上下文项是
charges
元素,则表达式
fees | service/servamt
给出了要求和()的一组数字。在您的(修订)代码中,上下文项是
服务
元素。
<?xml version="1.0" encoding="UTF-8"?>
<fo:table-row xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:table-cell display-align="center">
        <fo:block>Fees</fo:block>
    </fo:table-cell>
    <fo:table-cell>
        <fo:block>25</fo:block>
    </fo:table-cell>
    <fo:table-cell display-align="center">
        <fo:block>
            <fo:leader/>
        </fo:block>
    </fo:table-cell>
</fo:table-row>
<fo:table-row xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:table-cell display-align="center">
        <fo:block>Service 1</fo:block>
    </fo:table-cell>
    <fo:table-cell>
        <fo:block>335</fo:block>
    </fo:table-cell>
    <fo:table-cell display-align="center">
        <fo:block>
            <fo:leader/>
        </fo:block>
    </fo:table-cell>
</fo:table-row>
<fo:table-row xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:table-cell display-align="center">
        <fo:block>Service 2</fo:block>
    </fo:table-cell>
    <fo:table-cell>
        <fo:block>335</fo:block>
    </fo:table-cell>
    <fo:table-cell display-align="center">
        <fo:block>
            <fo:leader/>
        </fo:block>
    </fo:table-cell>
</fo:table-row>
<fo:table-row xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:table-cell display-align="center">
        <fo:block>Service 3</fo:block>
    </fo:table-cell>
    <fo:table-cell>
        <fo:block>335</fo:block>
    </fo:table-cell>
    <fo:table-cell display-align="center">
        <fo:block>
            <fo:leader/>
        </fo:block>
    </fo:table-cell>
</fo:table-row>