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

Re: result = node1 * node2 and then get total of all t

Subject: Re: result = node1 * node2 and then get total of all the result from whole document at the end
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Wed, 2 Jan 2002 02:43:20 -0800 (PST)
xsl get sum of products
> I am having hard
> time adding the product of rate and quantity into a variable and then
> increment it and print it at the end of document.


Look at the link "Quantity times price".

The corresponding functional solution in Haskell is:

sumOfProducts :: Num a => [[a]] -> a
sumOfProducts = sum . (map product)


 sum :: (Num a) => [a] -> a
 sum = foldl add 0


 product :: (Num a) => [a] -> a
 product = foldl multiply 1

sumOfProducts() takes a list of lists of numbers, first "map" produces a new list
whose elements are the products of the numbers contained in every element (which is
itself a list of numbers), then finally "sum" sums the (products) elements of this
new list.

For example:

sumProducts [[1,2,3],[3,4]] evaluates to 18 (sum [1*2*3, 3*4] 
= sum [6, 12] = 6 + 12 = 18).

As can be seen this is much more powerful function, allowing to sum not only
products of pairs, but products of lists of numbers with varying length.

The XSLT implementation of this functional solution is straightforward and left as
an exercise for the interested readers.

Dimitre Novatchev.

Do You Yahoo!?
Send your FREE holiday greetings online!

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

Current Thread


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.
First Name
Last Name
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.