douchi0638 2018-10-15 00: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-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.

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

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊