[XPath] Simple Mapping Operator

I'm submitting this as a last-call comment because I keep hearing discontent
about it, mainly from the XSLT user community. There have been many informal
discussions on this in the XSL WG, with a fair amount of sentiment in
favour, but I think it needs to go on the formal joint XPath agenda for a
decision one way or the other. 

This is a personal rather than a Software AG comment.

I believe that there is a clear need for a simple mapping operator in XPath
2.0. I will use the symbol "!" to represent this operator. The semantics are
that:

E1 ! E2 evaluates E2 for each item in E1, and returns the concatenation of
the results, retaining order. E2 is evaluated with the context item set to
the relevant item in the result of evaluating E1.

For example:

  string-join(*!name(), ",")

returns a list of names of the child elements of the context node

  sum(item!(@price*@qty))

returns the sum of price*quantity over all items

  (1 to 10)!(.*2)

returns the numbers 2,4,6,...

  $emps!@date-of-birth

returns the dates of birth of the employees in $emps, retaining the original
sequence order.  

Why is this needed?

Basically because the "for" expression is too heavyweight for the task. The
"for" expression introduces range variables, which are only really needed
when doing joins, and joins in XPath expressions are actually extremely
unusual. The vast majority of "for" expressions actually used in XPath are
not nested. By requiring users to use "for" expressions as the only mapping
construct in the language, (a) they are forced to use a syntax that is
unlike the rest of XPath, and (b) they have to switch idioms to use
variables, instead of using the context item in the way that comes naturally
- which is a very common source of errors.

There has been some sentiment that a simple mapping operator would remove
the need for "for" expressions in XPath entirely. I'm not arguing for that
here (I think there is some merit in XPath being relationally complete). But
for a language so heavily based on sequences, I think the simple mapping
operator is really needed.

Michael Kay

Received on Wednesday, 3 December 2003 07:15:28 UTC