[xsl] Re: how calculate sum(x*y)

Subject: [xsl] Re: how calculate sum(x*y)
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 7 Oct 2002 02:54:36 -0700 (PDT)
--- Àíäðåé Ñîëîí÷óê <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@xxxxxxxxxxxxxx

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:

This transformation:
<xsl:stylesheet version="1.0"
exclude-result-prefixes="xsl foldl-func"

   <xsl:import href="foldl.xsl"/>

   <xsl:variable name="vFoldlFun" 
    <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:template match="foldl-func:*">
         <xsl:param name="arg1" select="0"/>
         <xsl:param name="arg2" select="/.."/>
         <xsl:value-of select="$arg1 + $arg2/x * $arg2/y"/>


when applied on the above source xml document produces"


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

Hope this helped.


Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos & More

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread