XPath是否对逻辑表达式进行短路评估? [英] Does XPath do short-circuit evaluation of logical expressions?

查看:101
本文介绍了XPath是否对逻辑表达式进行短路评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与XPath中的执行顺序有关。

My question is regarding the order of execution in XPath.

我有一个表达式,例如:

I have an expression such as:

//person[@created and customFunction(.)]

我的问题是我的自定义函数计算量很大,我只希望它在具有创建的属性集的节点上运行。是否会始终在 customFunction 之前评估 @created ?我可以编写一个程序来测试这一点,但实际上,这样的实验是否成功,不能保证,至少从长远来看不能保证。

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.

XPath实现的问题,我正在使用.NET 4.0。

If this is a matter of XPath implementation i am using .NET 4.0.

推荐答案

XPath 1.0进行短路评估,在XPath 2.0和XPath 3.0中,它依赖于实现 >。

根据 XPath 1.0规范,第3.4节:布尔值


or 表达式 [ ...] 如果左操作数
计算为true,则不计算右操作数

表达式 [...] 如果左操作数
的计算结果为false,则不计算右操作数。

An and expression [...] The right operand is not evaluated if the left operand evaluates to false.

根据 XPath 2.0规范,第3.6节:逻辑表达式 XPath 3.0规范,第3.8节:逻辑表达式

如果XPath 1.0兼容模式为true [...] ,则定义为无需评估第二个操作数即可确定结果,则不会因为评估第二个操作数而导致
错误。

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.

如果XPath 1.0兼容模式为false,逻辑表达式的
操作数的求值顺序是
与实现相关的
。在这种情况下,如果第一个表达式的计算结果为true,则or表达式可以返回
true;如果对第一个表达式的计算结果为错误,则or表达式可以引发
错误。
同样,如果所求值的第一个表达式
为false,则与表达式可以返回false;如果对第一个表达式
的求值引起错误,则它会引发错误。这些规则的结果是,在出现错误的情况下,
逻辑表达式不是确定性的,如下面的示例中所示。

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时,可以通过评估以下示例表达式来确定当前实现是否进行短路评估:

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)

函数 name 返回节点参数的名称,或者如果您将其传递给数字(例如,数字),则会引发错误,因此:

The function name returns the name of the node parameter, or it raises an error if you pass it for example a number, so:


  • 如果返回true而不引发错误,则实现将进行短路评估。

  • 如果引发错误,则实现将不进行短路评估(因为它已评估了不必要的正确操作数)。

这篇关于XPath是否对逻辑表达式进行短路评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆