cieron said:
Hi
I've got something like this one:
<positions>
<position>
<value>1000</value>
<count>3</count>
</position>
<position>
<value>800</value>
<count>2</count>
</position>
</positions>
I need on my page this result
1000 * 3 + 800 * 2 = 4600
but sum(position/value * position/count) is not working. How can I get what
I want?
sum has to take a node set, so you can not in pure XSLT1 use sum() over
a computed expression.
IN Xpath2 (which is still in draft form, but implemented in saxon 8) you
will be able to do
sum(position/(value * count))
However for XSLT1, possibly the simplest is to use yor processor's
xx:node-set() extension function (if it has one, most do) and first
generate a list of products
<xsl:variable name="x">
<xsl:for-each select="position"/>
<x><xsl:value-of select="value * count"/></x>
</xsl:for-each>
</xsl:variable>
then sum these new x elements.:
<xsl:value-of select="xx:node-set($x/x)
Or you need to write a recursive template that accumuates the sum
<xsl:template match="positions">
<xsl:apply-templates select="position[1]"/>
</xsl:template>
<xsl:template match="position">
<xsl
aram name="s" select="0"/>/>
<xsl:variable name="s2" select="$s + value * count"/>
<xsl:choose>
<xsl:when test="following-sibling:
osition">
<xsl:apply-templates select="following-sibling:
osition[1]">
<xsl:with-param name="s" select="$s2"/>
</xsl:apply-templates>
<xsl
therwise>
<xsl:value-of select="$s2"/>
</xsl
therwise>
</xsl:choose>
</xsl:template>
David