Reputation: 55
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
Upvotes: 0
Views: 466
Reputation: 167716
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.
Upvotes: 1