gpt4 book ai didi

.net - 这两种样式的 xpath 逻辑 'and' 是否等效?

转载 作者:行者123 更新时间:2023-12-03 15:42:31 25 4
gpt4 key购买 nike

假设我有这样的 xml:

<Products>
<Product name="Liquid Oxygen">
<Manufacturer name="Universal Exports" country="UK" />
</Product>
<Product name="Hydrazine">
<Manufacturer name="ACME Inc." country="USA" />
</Product>
<Product name="Gaseous Oxygen" obsolete="true">
<Manufacturer name="Universal Exports" country="UK" />
</Product>
<Product name="Liquid Nitrogen">
<Manufacturer name="Penguins R Us" country="Antarctica" />
</Product>
</Products>

我想挑出具有 Manufacturer 的 Product 节点带有 @country 的子节点的 UK ,但没有 @obsoletetrue .我可以说
/Products/Product[Manufacturer/@country = 'UK' and not(@obsolete = 'true)]

或者
/Products/Product[Manufacturer/@country = 'UK'][not(@obsolete = 'true')]

两者都为我提供了我想要的节点。

我的问题是,这两种处理 and-ed 条件的方法在功能上有区别吗?是否存在不同方法可能产生不同结果的情况? (我意识到 and 在更复杂的条件下有作用)在风格上,一个比另一个更受欢迎吗?

(我使用的是 C# 和 .NET 2.0,但我认为这不会对答案产生任何影响)

最佳答案

对于 and在你的情况下,他们给你同样的结果。至于风格,选择更能表达你的意图的。

但是,一旦您开始使用位置函数,例如 position() , count() , first()last() , 它的确有所作为。

你自己看:

/Products/Product[Manufacturer/@country = 'UK' and (position() = 1)]

如果列表中的第一个产品不是来自英国,则不返回任何节点,而:
/Products/Product[Manufacturer/@country = 'UK'][position() = 1]

将返回匹配 UK 作为国家的第一个节点,也可以写成简短的形式:
/Products/Product[Manufacturer/@country = 'UK'][1]

关于.net - 这两种样式的 xpath 逻辑 'and' 是否等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1901582/

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