我的问题是关于 XPath 中的执行顺序。
我有一个表达式,例如:
//person[@created and customFunction(.)]
我的问题是我的自定义函数计算量很大,我只希望它在具有已创建属性集的节点上运行。威尔
@created
总是在 customFunction
之前评估?我可以编写一个程序来测试这一点,但实际上并不能保证这样一个实验的成功,至少从长远来看不能。如果这是 XPath 实现的问题,我使用的是 .NET 4.0。
最佳答案
XPath 1.0 进行短路评估,在 XPath 2.0 和 XPath 3.0 中它依赖于实现 .
根据 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.
根据 XPath 2.0 specs, section 3.6: Logical Expressions和 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.
使用 XPath 2.0 或 XPath 3.0 时,您可以通过评估以下示例表达式来确定当前实现是否进行短路评估:
true() or name(1234)
函数
name
返回节点参数的名称,或者如果您传递它,例如一个数字,则会引发错误,因此:关于xpath - XPath 是否对逻辑表达式进行短路评估?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18386129/