Hi,
Please take a look at the attached letter and response from W3C about
is-last optimization. It looks like this could be advantageous for many
queries (especially for sql extension in streaming mode). XPath expressions
(sub-expressions) of view:
position() ? last(), where "?" is =, !=, <, >, etc,
can be easily optimized (org.apache.xpath.compiler.Compiler class) into the
call to the internal "is-last()" function.
Simpliest (or caching) iterators can implement this call just as "position()
? last()". And more complicated implementations would require (in the worst
case) caching of only one (next) node. So this can really save resources for
many queries.
I could post changes that need to be done in Compiler class (they'd be
pretty simple). But this would make sense only if DTMManager (and all other
iterator interfaces) would have isLast() method, which already requires
changes to the interfaces.
>From the iteration standpoint java.util.Iterator and Enumeration interfaces
all have hasNext() method (just as examples).
Please, send your comments if this looks interesting.
In addition, similar optimization could be done for queries "node[last()]"
as call to internal "get-last()" method, and probably some others.
Thanks,
Dimitry
_____________________________________________________
Sector Data, LLC, is not affiliated with Sector, Inc., or SIAC
<<RE: Iteration functionality request for XPath 2.0>>
--- Begin Message ---
> ....
> Sometimes it's very important to know if current node is the
> last one in the node-set. There're two basic ways to do it in
> XPath 1.0:
> **** FIRST APPROACH: using "position" and "last":
> <xsl:if test="position() = last()">
> ....
> </xsl:if>
>
> Drawbacks:
> If node-set implementation is iterative, i.e. new node
> is fetched only when for-each instruction goes to a new cycle
> and whole list of nodes is not cached in node-set object,
> then the first call to last() function will go through all
> nodes to find out their quantity. This will repeat the
> node-set fetch procedure twice or will lead to the caching of
> the nodes list.....
> In my point of view, this could be easily solved by extending
> list of node-set/context functions with one more function,
> something like "is-last" (another function "is-first" could
> also be added for symmetry, eventhough it'd be redundant).
> This function would return boolean true if current node is
> the last node in the node-set.
Saxon has an internal function like your is-last(), and its optimizer
rewrites the predicate [position()=last()] as [is-last()], for all the
reasons you mention.
I don't see a good reason why is-last() should be made available as a
user-visible function, when this construct is very easy for optimizers to
handle internally.
Michael Kay
--- End Message ---