douchi0638 2018-10-14 16:20
浏览 98
已采纳

xslt sum连接而不是添加

so I am trying to write some xslt 1.0 and trying to sum some values but the sum() function concatenates the numbers (or strings) instead of summing up. I'll paste my xslt here.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:exsl="http://exslt.org/common">

    <xsl:template match="/">
        <table id="carttable" align="center">
            <xsl:for-each select="cart/item">
                <tr>
                    <td id="itemnum"><xsl:value-of select="itemnumber" /></td>
                    <td id="itemprice"><xsl:value-of select="itemprice" /></td>
                    <td><xsl:value-of select="quantity" /></td>
                    <td id="itemadd"><input type="button" id="removeBtn" value="Remove One From Cart">
                        <xsl:attribute name="onclick">
                            <xsl:text>removeFromCart(</xsl:text>
                            <xsl:value-of select="itemnumber" />
                            <xsl:text>)</xsl:text>
                        </xsl:attribute>
                    </input></td>
                </tr>
            </xsl:for-each>
            <xsl:variable name="itemTotals">
                <xsl:for-each select="cart/item">
                    <total>
                        <xsl:value-of select="itemprice * quantity" />
                    </total>
                </xsl:for-each>
            </xsl:variable>

            <tr>
                <td align="right">Total</td>
                <td>
                    <xsl:value-of select="sum(exsl:node-set($itemTotals))" />
                </td>
            </tr>
        </table>
    </xsl:template>

</xsl:stylesheet>

have looked at other questions on stackoverflow but couldn't find one that caters to my exact scenario. hoping to find some answers here :) TIA P.S. i'm using xslt 1.0 and using it in PHP

</div>

展开全部

  • 写回答

1条回答 默认 最新

  • doulin2555 2018-10-14 23:39
    关注

    Use <xsl:value-of select="sum(exsl:node-set($itemTotals)/total)"/> to compute the sum of the total elements in your result tree fragment converted to a node-set with a root node containing total elements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部