This bug affects both eXist-1.4.1 and eXist-trunk (rev. 15272). Apparently, last(), when used in a shorthand positional predicate test is not always interpreted correctly. The last node on any XPath axis evaluates to an empty result set, when the context nodes are selected from more than one document in the db. For example, take an expression like:
//LINE/ancestor::*[last()]
When evaluated on the eXist Shakespeare samples, this returns an empty result set, while it should return the three elements of the three plays stored in the db.
This happens to be the case for all XPath axes, when the context nodes appear in more than one single document in the db.
Attached is an XQuery Unit test file illustrating the problem (lastTests.xml). Since there is a noticeable difference between shorthand and full positional predicates, both are distinguished, as well as a test for the calculated count of last():
[1] shorthand position test: [last()]
[2] full position test: [position() eq last()]
[3] last() in location step: /last()
In order to isolate the problem, these three tests are repeated for a number of scenarios:
[1] 6 axes: ancestor, descendant, preceding, following, preceding-sibling, following-sibling (ancestor-or-self and descendant-or-self behaved identically to their ancestor and descendant counterparts and have been left out)
[2] persistent vs. in-memory nodes
[3] context nodes appearing in single root element vs. multiple root elements
This amounts to 72 tests.
This bug is illustrated by failing tests #2, #14, #26, #38, #50, #62, which select the last nodes on all axes for context nodes appearing in more than one document stored in the db. While they should return some nodes, they all fail by returning an empty result set. On the other hand, formulating this test as a full position test ([position() eq last()], they mostly return the correct results (succeeding tests #3-#4, #15-#16, #21-#23, #39-#40, #45-#46, #57-#58, #63-#64, #69-#70).
Additionally, there are some other remarkable errors, though I'm not sure whether they touch on other bugs:
[1] Ancestor and preceding axes selecting wrong nodes. For in-memory nodes, full positional predicates select incorrect nodes for both ancestor and preceding axes (failing tests #9-#10, and #33-#34). Additionally, the preceding axis selects wrong nodes on persistent nodes as well, both with shorthand positional predicates (failing test #25) and full positional predicates (failing tests #27-#28).
[2] Preceding-sibling produces correct nodes but in wrong order, with full positional predicate on persistent nodes (failing tests #51-#52). Also note, that only eXist-trunk produces correct nodes in wrong order for test #4.
[3] Preceding-sibling produces an empty result on persistent nodes with a shorthand positional predicate (failing test #49).
rvdb
2011-08-30