RE: [XQuery] static typing of node comparisons

> Why operate on the first item? Because this will work in 
> every case -- even in the case when the sequence consists of 
> just one element.
> 
> This semantics may seem "strange". However, it has been 
> proven to be useful and effective in practice when it is well 
> documented and known. If many thousands of programmers are 
> using XPath 1.0 without problems then why should this 
> semantics be changed?

On the whole, I have been one of those whose has been in favour of ditching
the "first-item" rule and making it an error to supply multiple items where
a single item is expected. It is something that often causes confusion to
users, particularly with xsl:value-of.

However, the XSLT 1.0 / XPath 1.0 philosophy of "no run-time errors" does
have something going for it. We have a lot of complexity in our specs
because of the difficulty of achieving interoperable handling of run-time
errors, and the difficulty of rewriting expressions so that the rewritten
expression has equivalent error-handling behavior as well as delivering the
same results.

The first-item rule also has a major performance benefit, which is that when
the user writes a path expresion such as 1+.//cost, the system can stop
after finding the first item in the result sequence, and isn't obliged to
carry on looking for further items. In cases where the user knows that there
is only one <cost> element (and probably knows that it appears very early in
the subtree) searching the rest of the subtree to find a non-existent second
<cost> element is a lot of wasted effort. In fact this illustrates the
previous point about interoperable error-handling behavior, because we have
a general rule that the system isn't required to do extra work that's needed
merely in order to detect errors, and this rule could be used to claim that
a system providing "first-item" semantics in this case is fully conformant.
This suggests that we have actually reinvented the much-criticised XSLT 1.0
approach of "it's an error, but the system isn't required to report it".

So I have some sympathy with Dimitre on this. I don't like the "first-item"
rule, but it may turn out to be the lesser of two evils: and if
implementations are going to use the let-out clause to provide first-item
behavior anyway, then we might just as well mandate it so that everyone does
the same thing and the results are predictable.

Michael Kay

Received on Tuesday, 2 December 2003 06:47:41 UTC