gpt4 book ai didi

xml - Xpath:比较十进制类型的节点属性

转载 作者:行者123 更新时间:2023-12-01 18:33:36 27 4
gpt4 key购买 nike

我正在尝试为以下 xml 文件创建 xpath 表达式(显然是缩写的)。

<estimate>
<heights id="4.1">
<year>1985</year>
</heights>
<heights id="4.2">
<year>1986</year>
</heights>
<heights id="4.3">
<year>1987</year>
</heights>
.
.
.
<heights id="x.x">
<year>XXXX</year>
</heights>
</estimate>

我使用的 xpath 表达式是这样的:

/estimate/heights[@id>4.2]

所以在这种情况下我基本上是在寻找 id 大于 4.2 的节点。虽然这在我使用它时似乎有效,但我担心我实际上并没有将 float 与 float 或 double-double 进行比较。如果我正确阅读了 XPATH 2.0 的维基百科条目,它似乎会将属性转换为适当的类型:

If no schema is in use, the node will be untyped, and the type of the resulting atomic value will be untypedAtomic. Typed atomic values are checked to ensure that they have an appropriate type for the context where they are used: for example, it is not possible to multiply a date by a number. Untyped atomic values, by contrast, follow a weak typing discipline: they are automatically converted to a type appropriate to the operation where they are used: for example with an arithmetic operation an untyped atomic value is converted to the type double.

我做了很多搜索,但没有找到任何明确的东西(我不清楚上面引用的陈述是否适用于我的情况。)

最佳答案

首先,您确实需要明确这是 XPath 1.0 还是 XPath 2.0(或以向后兼容模式运行的 XPath 2.0)。对于这个特定的示例,规则可能具有大致相同的效果,但它们的表达方式却截然不同。

在 XPath 1.0 中,您将节点集与数字 (=double) 进行比较,如果节点集中的任何节点在转换为数字 (=double) 后大于 4.2,则结果为 true。如果节点集为空(即如果 @id 不存在),或者如果转换产生 NaN(即如果 @id 不是数字),则结果为 false。

在 XPath 2.0 中,假设数据是无类型的(即,它不是模式感知处理器),您将节点序列与 xs:decimal 进行比较。节点被原子化,产生一系列 xs:untypedAtomic 值,并且这些值被强制转换为 xs:double 以便进行比较(因为另一个操作数是数字)。同样,如果任何值大于 4.2,则答案为 true。与 XPath 1.0 规则的唯一有效区别是,如果 @id 不是数字,则转换失败并且您会收到动态错误,而 XPath 1.0 给出“false”。

在启用向后兼容性的 XPath 2.0 中,转换为 double 使用 number() 函数而不是强制转换,因此非数字 @id 值将转换为 NaN,并且结果为 false,与 XPath 1.0 中一样。

关于xml - Xpath:比较十进制类型的节点属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25676045/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com