[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] sum() applied to a product


Subject: RE: [xsl] sum() applied to a product
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Wed, 30 Jun 2004 10:03:35 +0100

> -----------------------
> Okay.. I told someone I could do this in a simple xpath expression
> 
> <?xml version="1.0"?>
> <a>
>   <b>
>     <c>2</c>
>     <d>4</d>
>   </b>
>   <b>
>     <c>3</c>
>     <d>6</d>
>   </b>
> </a>
> 
> Where the xpath was to return (2*4) + (3*6) = 26
> I made several attempts with xpath's sum() function (using 1.0) and 
> couldn't get it.

Assuming that the number of <b> elements is variable, you can't do this
directly in XPath 1.0. 

In 2.0 it's easy: sum(for $b in b return $b/c * $b/d)

In 1.0 your options are (a) write a recursive template that operates over
the list of b elements, or (b) use a two-phase transformation in which the
first phase computes the products and the second phase computes the sum. You
could also use Dimitre Novatchev's FXSL library, which simulates
higher-order functions using pure XSLT 1.0.

Michael Kay



Current Thread
Keywords