I'm new to XSLT, I need to add all the price values. But the price values in XML have '-' and also no values and while doing sum I'm getting NaN. So i tried the below XSL, but still I'm unable to achieve my result.
Below is a sample XML
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Edited by XMLSpy® -->
<root>
<qwe>
<value>+1</value>
</qwe>
<qwe>
<value>20</value>
</qwe>
<qwe>
<value>-</value>
</qwe>
<qwe>
<value>30</value>
</qwe>
<qwe>
<value>-0</value>
</qwe>
<qwe>
<value/>
</qwe>
<qwe>
<value>8</value>
</qwe>
</root>
This is the XSL I'm trying to use:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" >
<xsl:template match="/">
<html>
<body>
<h2>My qwe Collection</h2>
<xsl:for-each select="root/qwe">
<xsl:variable name="len" select="replace(value,'-','0')" />
<xsl:variable name ="asd" select="sum(//value[number(.)=number(.)])"/>
sum: <xsl:value-of select="$asd"/><br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Expected Result:
sum:59
I am slightly confused, because your existing sum expression should work (the only issue being you have it inside the xsl:for-each loop rather than after it).
Although having said that, I tried two different XSLT processors, and one returned 58 (presumably because "+1" fails the number check).
But you are using XSLT 2.0 (hopefully), then this expression should be work, and be more robust
The "translate" here will replace
-
with0
and remove+
entirely.Note you will have to declare the namespace for "xs" as "http://www.w3.org/2001/XMLSchema"
Try this XSLT