XSL总和未显示正确的值

时间:2017-12-11 13:56:25

标签: xslt xslt-1.0 xsl-fo

我需要对下面的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>

这是我用来显示数据的一段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>

2 个答案:

答案 0 :(得分:2)

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

sum((fees | service/servamt))

答案 1 :(得分:1)

如果您的XML将如下所示(因为您的XML看起来像缺少根元素):

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

然后您的代码将在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>

希望它会有所帮助。