[xsl] Re: A question about the expressive power and limitations of XPath 2.0

Subject: [xsl] Re: A question about the expressive power and limitations of XPath 2.0
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sun, 13 Jan 2002 14:42:43 +0000
Hi Dimitre,

> A general statement about what "for" cannot do: Any problem of
> sequence processing, which requires that the result of processing an
> item be used as input for processing the next item(s).
>
> Is this true?
>
> Then an example with sales and having to compute the total sum, but
> also having to maintain a ***running total*** seems most close to
> earth.

I'd say this definition characterised some things that are inefficient
to do with the for expression (as well as some impossible things).

Taking your running total example, say $items was a set of item
elements, with price and quantity attributes, and you wanted for
whatever reason to get pairs of item and running total.

You could do:

  for $i in (1 to count($items))
  return ($items[$i],
          sum( for $j in (sublist($items, 1, $i))
               return (@price * @quantity) )

This isn't particularly efficient because you have to do two sublist
operations within the return expression, but it is possible.

What 'for' *cannot* do, I think, is any aggregation of a sequence of
values unless, of course, that aggregation is already catered for with
a function, for example sum(), count(), distinct-values(), min(),
max() and so on. That's why a concat() that takes a sequence is a good
example.

The 'for' expression is not very efficient at doing things where the
aggregation occurs over a set of nodes, and you want a set of nodes
in return, but the appropriate aggregation function would give you a
simple typed value instead. For example:

  - getting the nodes with a particular maximum value (especially if
    the 'value' of a node is calculated through some complex
    expression)

  - getting nodes with distinct values when the 'value' of a node is
    not simply the value of a related node.

It is also not very efficient when the position of a node is important
for its interpretation. I've give examples with x,y coordinates
before.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread