xpath - XPath 是否对逻辑表达式进行短路评估?

标签 xpath .net-4.0 xmldocument

我的问题是关于 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 ExpressionsXPath 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返回节点参数的名称,或者如果您传递它,例如一个数字,则会引发错误,因此:
  • 如果它返回 true 而不引发错误,则实现会进行短路评估。
  • 如果出现错误,则实现不会进行短路评估(因为它已经评估了不必要的正确操作数)。
  • 关于xpath - XPath 是否对逻辑表达式进行短路评估?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18386129/

    相关文章:

    javascript - 使用 jquery mobile 显示 XML - 性能?

    image - Selenium webdriver:我想单击https://www.msn.com/zh-cn/weather/today底部的“Like 1.3M”图像

    c# - 如何检查xml文档是否具有特定的属性列表?

    c# - 在 XML 中选择特定节点列表

    ruby - XPath 查找所有后续 sibling ,直到特定类型的下一个 sibling

    python - 使用 Xpath 轴提取前面的元素

    c# - 将通用列表传递给 ObservableCollection 构造函数

    c# - 检测 Microsoft.Office.Interop.Excel 可用性

    .net - .NET 4.0 中的 Microsoft.csharp.dll 是什么

    常规 : How to read xml tag names having full colon