RE: [XPath] About fn:last()

> As an implementor of XPath 2.0,I found that it's
> difficult to determind the size of context sequence in
> an efficient way.Fox example, we want to get the last
> node of a sequence:
> '/e1[predicate1]/e2[predicate2]/e3[predicate3]/e4[fn:last()]',if
> the resulting sequence is very long(e.g. 1,000,000
> nodes) and the data is stored in a file,in the worst
> case,we'll have to use 1,000,000 IO operations to get
> the value of fn:last() and another 1,000,000 IO
> operations to iterate to the last node.Note that it is
>  difficult to optimize the query if the last predicate
> combine fn:last() with other functions(e.g.
> '[fn:last() - fn:position() > 10 ]'). 

Yes, last() is tricky to implement. But this language is designed for users,
not for implementors. For you as an implementor the challenge is to find
ways of implementing it efficiently.

last() is present in XPath 1.0, and largely unchanged in 2.0. Note:

(a) there have been many successful (and fast) XPath 1.0 implementations

(b) we can't change something that is so widely used.

> 
> In fact,fn:last() is frequently used to locate the
> last item of a sequence,and other case is rare.

So the strategy is clear: optimize the case that is frequently used. In the
path expression above, given an appropriate data structure, you should be
able to find the last e4 element without touching any of the others. (For
example, you could have in internal "reverse child" axis and turn E[last()]
into reverse-child::E[1].)

In publishing applications, of course, last() is needed for numbering: "page
1 of N".

Michael Kay

Received on Thursday, 3 June 2004 03:47:49 UTC