douchi0638
2018-10-15 00:20
浏览 87

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-15 07: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.

    点赞 打赏 评论

相关推荐 更多相似问题