RE: Sequences of numeric values in XPath predicate expressions

> It would be very useful if the value of an XPath predicate 
> expression could be *a sequence of values* of a numeric type.
> 
> Use: Since <xsl:copy-of> can now be used to copy a sequence 
> of items, it would be excellent if one could write
> 
> <xsl:copy-of select="foo[1 to 4]"/>
> 
> instead of the more long-winded
> 
> <xsl:for-each select="1 to 4">
>      <xsl:copy-of select="foo[.]"/>
> </xsl:for-each>
> 
>
This was proposed at one stage, but when you look at the actual semantics,
they become very complicated. Remember that a numeric predicate [$n] is
defined in terms of the boolean predicate [position()=$n]. $n does not have
to be constant for all items in the sequence; you can write, for example,

  $seq[string-length(.)]

and get all the items whose position in the sequence is equal to their
string length.

The other problem is that because static typing is optional, the semantics
have to be defined in terms of the dynamic type of the predicate. One of the
nice things about the "effective boolean value" rule is that when you supply
a sequence, you don't have to read the whole sequence in order to decide
what its EBV is. But if we handle a sequence of integers differently from
any other sequence, then we do (in the absence of static type analysis) have
to read the whole sequence to find out if it consists entirely of integers.

We decided that this was overloading the [] operator one step too far, and
that it wasn't a great hardship to write:

  for $i in 1 to 20 return $seq[$i]

Michael Kay

Received on Thursday, 5 June 2003 05:09:38 UTC