Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   XML - problem with sum function (http://www.velocityreviews.com/forums/t169321-xml-problem-with-sum-function.html)

cieron 05-16-2005 12:27 PM

XML - problem with sum function
 
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?


--

Thanks,
cieron



David Carlisle 05-16-2005 01:08 PM

Re: XML - problem with sum function
 
"cieron" <cieron@wp.pl> writes:

> 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?
>
>
> --
>
> Thanks,
> cieron



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:param name="s" select="0"/>/>
<xsl:variable name="s2" select="$s + value * count"/>
<xsl:choose>
<xsl:when test="following-sibling::position">
<xsl:apply-templates select="following-sibling::position[1]">
<xsl:with-param name="s" select="$s2"/>
</xsl:apply-templates>
<xsl:otherwise>
<xsl:value-of select="$s2"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

David


Dimitre Novatchev 05-16-2005 09:21 PM

Re: XML - problem with sum function
 

"cieron" <cieron@wp.pl> wrote in message
news:d6a3tk$cc1$1@nemesis.news.tpi.pl...
> 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?



FXSL is your friend -- look for the foldl or runningTotals template.

or directly sumProducts.xsl in:
http://www.mulberrytech.com/Extreme/...ovatchev01.pdf


Cheers,
Dimitre Novatchev




All times are GMT. The time now is 08:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.