Quote:
Originally Posted by sde cool, thanks for posting the solution. it will also help people finding this thread when them come here searching. |
No problem. Since global variables are similar to a final in java, their values are constant.
Therefore, you must get the present node and if its less than or equal to the previous node add it to the previous node. And since the first node will not have a sibling node before it, it is possible that the value may come back null so you might have to do an xsl:if to check whether (in this case 'amount' is null)
Code:
<xsl:if test="amount = ' ' ">
<xsl:value-of select="amount" />
</xsl:if> Something like that would if it if you are getting NaN for the first returned value.
Here is another example, a little different.
This is a general implementation using exslt.
xml:
Code:
<?xml version="1.0"?>
<base>
<item>
<amount>1</amount>
<check>yes</check>
</item>
<item>
<amount>2</amount>
<check>yes</check>
</item>
<item>
<amount>3</amount>
<check>yes</check>
</item>
<item>
<amount>4</amount>
<check>yes</check>
</item>
<item>
<amount>5</amount>
<check>yes</check>
</item>
<item>
<amount>6</amount>
<check>yes</check>
</item>
<item>
<amount>7</amount>
<check>yes</check>
</item>
<item>
<amount>8</amount>
<check>yes</check>
</item>
</base> xslt:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://exslt.org/functions"
extension-element-prefixes="f">
<xsl:output method="xml"
indent="yes"/>
<xsl:template match="/">
<values>
<xsl:apply-templates select="*//amount"/>
<sum>
<xsl:value-of select="f:sum(*//amount)"/>
</sum>
</values>
</xsl:template>
<xsl:template match="amount">
<val>
<xsl:value-of select="."/>
</val>
</xsl:template>
<f:function name="f:sum">
<xsl:param name="mynodeset"/>
<xsl:variable name="first"
select="$mynodeset[1]"/>
<xsl:variable name="rest"
select="$mynodeset[position() > 1]"/>
<xsl:choose>
<xsl:when test="$rest">
<f:result select="$first + f:sum($rest)"/>
</xsl:when>
<xsl:otherwise>
<f:result select="$first"/>
</xsl:otherwise>
</xsl:choose>
</f:function>
</xsl:stylesheet> see also:
EXSLT - Functions