This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: how calculate sum(x*y)
- From: Dimitre Novatchev <dnovatchev at yahoo dot com>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Mon, 7 Oct 2002 02:54:36 -0700 (PDT)
- Subject: [xsl] Re: how calculate sum(x*y)
- Reply-to: xsl-list at lists dot mulberrytech dot com
--- Андрей Солончук <solo at ibis dot odessa dot ua> wrote:
> Hello All,
>
> how calculate sum(x*y) ??
>
> IF i try use sum() it say that need only node-list as argument....
>
> I have xml
> <root>
> <data>
> <x>2</x>
> <y>3</y>
> </data>
> <data>
> <x>5</x>
> <y>6</y>
> </data>
> .....
> <root>
>
>
> --
> Best regards,
> Andrey Solo mailto:solo@ibis.odessa.ua
Hi Andrey,
This is most easily done in a standard way by using the "foldl"
template from the FXSL library.
Given the following source xml:
------------------------------
<root>
<data>
<x>2</x>
<y>3</y>
</data>
<data>
<x>5</x>
<y>6</y>
</data>
<data>
<x>4</x>
<y>3</y>
</data>
</root>
This transformation:
-------------------
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foldl-func="foldl-func"
exclude-result-prefixes="xsl foldl-func"
>
<xsl:import href="foldl.xsl"/>
<foldl-func:foldl-func/>
<xsl:variable name="vFoldlFun"
select="document('')/*/foldl-func:*[1]"/>
<xsl:output encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:call-template name="foldl">
<xsl:with-param name="pFunc" select="$vFoldlFun"/>
<xsl:with-param name="pList" select="/*/*"/>
<xsl:with-param name="pA0" select="0"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="foldl-func:*">
<xsl:param name="arg1" select="0"/>
<xsl:param name="arg2" select="/.."/>
<xsl:value-of select="$arg1 + $arg2/x * $arg2/y"/>
</xsl:template>
</xsl:stylesheet>
when applied on the above source xml document produces"
48
This is a more efficient solution than first copying the products into
a temporary node-set, then summing them (also possible with FXSL as:
sum (zipWith (*) list1 list2)
), because it does not have to produce a big temporary node-set with
all the products and then to have a second pass at this intermediate
list.
Hope this helped.
=====
Cheers,
Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
__________________________________________________
Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos & More
http://faith.yahoo.com
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list