[XSL-LIST Mailing List Archive Home]
[xsl] Re: result = node1 * node2 and then get total of all the result from whole document at the end
Subject: [xsl] 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)
> 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
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.
Do You Yahoo!?
Send your FREE holiday greetings online!
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list