My question is regarding the order of execution in XPath.
I have an expression such as:
//person[@created and customFunction(.)]
My problem is that my custom function is quite compute heavy and I only wish for it to run on nodes that have the created attribute set. Will @created
always be evaluated before customFunction
? I could cook up a program to test this, but in reality the success of such an experiment is no guarantee, at least not in the long term.
If this is a matter of XPath implementation i am using .NET 4.0.
The logical AND operator performs short-circuit evaluation: if the left-hand operand is false, the right-hand expression is not evaluated. The logical OR operator also performs short-circuit evaluation: if the left-hand operand is true, the right-hand expression is not evaluated.
Short-Circuit Evaluation: Short-circuiting is a programming concept in which the compiler skips the execution or evaluation of some sub-expressions in a logical expression. The compiler stops evaluating the further sub-expressions as soon as the value of the expression is determined.
Short-circuit evaluation means that when evaluating boolean expressions (logical AND and OR ) you can stop as soon as you find the first condition which satisfies or negates the expression.
Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first ...
XPath 1.0 does short-circuit evaluation, in XPath 2.0 and XPath 3.0 it is implementation-dependant.
According to XPath 1.0 specs, section 3.4: booleans:
An or expression [...] The right operand is not evaluated if the left operand evaluates to true.
An and expression [...] The right operand is not evaluated if the left operand evaluates to false.
According to XPath 2.0 specs, section 3.6: Logical Expressions and XPath 3.0 specs, section 3.8: Logical Expressions:
If XPath 1.0 compatibility mode is true [...] it is defined that when there is no need to evaluate the second operand in order to determine the result, then no error can occur as a result of evaluating the second operand.
If XPath 1.0 compatibility mode is false, the order in which the operands of a logical expression are evaluated is implementation-dependent. In this case, an or-expression can return true if the first expression evaluated is true, and it can raise an error if evaluation of the first expression raises an error. Similarly, an and-expression can return false if the first expression evaluated is false, and it can raise an error if evaluation of the first expression raises an error. As a result of these rules, a logical expression is not deterministic in the presence of errors, as illustrated in the examples below.
When using XPath 2.0 or XPath 3.0 you can find out whether the current implementation does short-circuit evaluation by evaluating the following example expression:
true() or name(1234)
The function name
returns the name of the node parameter, or it raises an error if you pass it for example a number, so:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With