考虑以下带有简单断言的简单模式:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<xs:element name="root">
<xs:complexType>
<xs:assert test="(8 = 8) or name('error')"></xs:assert>
</xs:complexType>
</xs:element>
</xs:schema>
让我们看一下断言:
(8 = 8) or name('error')
左边的操作数为真,而第二个操作数在计算时会产生错误(因为名称函数需要节点参数而不是字符串)。 Saxon 验证器说断言得到满足,Xerces 验证器说不满足。
The right operand is not evaluated if the left operand evaluates to true
因此根据 XPath 1.0,应该满足这个断言而不引发错误,因为不应该计算正确的操作数。但是,XSD 1.1 使用 XPath 2.0,它允许依赖于实现的评估顺序,states :
If XPath 1.0 compatibility mode is false [...] 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 [...]. A logical expression is not deterministic in the presence of errors
在XSD 1.1 specs我们可以清楚地看到:
For an XPath Expression property record X to be valid, all of the following must be true:
[...]
2.2.1 XPath 1.0 compatibility mode is false.
[...]
据我所知,在 XSD 1.1 中,XPath 兼容模式为假,因此断言结果依赖于实现,因此 XML 文档应该根据验证器实现对相同的 XSD 有效。在这种情况下,Saxon 声明断言得到满足是正确的,而 Xerces 声明断言未得到满足也是正确的。是这样还是我遗漏了什么?
最佳答案
是的,你是对的。如果您希望 (A 或 B) 保证 A 在 B 之前求值,那么您可以编写 test="if (A) then true() else B"
。
关于xpath - XSD 1.1 断言依赖于实现的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31000890/