Bug #5361
closedBug in XPath optimizer with positional filter, when LHS of filter is empty sequence
100%
Description
Hello,
This is a similar issue and context to #4610.
I'm using the XPath engine of Saxon HE 10.6 (JRE) and inlining statically known variables. The optimizer fails to reduce the following expression to ()
when $foo
is replaced with false()
:
//A[$foo][child::*[2]]
with the following warning
Warning on line 1 column 5
Evaluation will always throw a dynamic error: Axis step child::element() cannot be used here:
the context item is absent
Note that if you write //A[false()][child::*[2]]
, the parser does some early optimization, prunes the branch, and the optimizer isn't run at all. Only if you use a variable and set its static value to false()
in IndependentContext::bindVariable
, you get the bug. You can find a MWE attached.
Also note that the following work fine:
//A[$foo]/*[2]
//A[$foo=true()][child::*[2]]
I reproduced the issue on Saxon HE 10.2 and 10.6.
While debugging I found that the offending expression is represented like so:
(/)/((descendant::element(Q{}A)[false()])[child::element()[2]])
------------------------------------
Then, the LHS of the FilterExpression (underlined part above) is reduced to ()
. At this point the entire FilterExpression could already be folded to ()
, but optimization of the FilterExpression continues by optimizing the filter child::element()[2]
. This is initially a FilterExpression and is replaced by a SubscriptExpression. Type checking the SubscriptExpression fails with "the context item is absent" (the warning is reported at this point) and the SubscriptExpression becomes error("Axis step child::element() cannot be used here: the context item is absent")[2]
. This shouldn't have to happen as the LHS of the filter is ()
.
A possible fix would be to end the optimization of a FilterExpression as soon as the LHS has been optimized to ()
, without considering the predicate.
Files
Related issues
Please register to edit this issue