[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: how calculate sum(x*y)

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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.