[xquery-talk] Paths, node sorting & node constructors

Michael Kay mike at saxonica.com
Sat Mar 15 11:13:09 PST 2008

> One thing with XQuery that I am constantly tripping over in 
> XQuery is that the result of sorting constructed elements is 
> undefined. Consider this:
> 	path/(<h3>{...}</h3>,
> 	      <p>{...}</p>)
> So what can one recommend users to do? To go away from 
> XPath-style, and use a for loop instead?

Yes, I think using element constructors on the rhs of "/" is best avoided.

For a time I took to using $var/(long expression) as a way of causing (long
expression) to be evaluated
with $var as the context node. For example:

declare function copyCustomer ($c) {

But the document ordering semantics of "/" make this too dangerous. It works
in this case, but fails in many others.

XQuery desperately needs a "simple mapping operator": something that's like
a "for", but setting the context item rather than a variable, and without
any ordering semantics. This came up again and again during WG discussions,
but got defeated every time - partly because no one could find a way of
writing the operator that everyone liked. In fact, in the early days before
some of the XQuery WG members fully understood XPath, they wanted "/" to be
that operator - but of course that's incompatible with its existing usages.

Michael Kay


More information about the talk mailing list