gpt4 book ai didi

xpath - 使用嵌套的 XPath 谓词......精炼

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

所有很棒的答案!但这个问题值得细化......

我有以下示例 XML ...

<objects>
<object objectId="1123" ... />
<properties refObjectId="1123" ... />
<properties refObjectId="1123" refPropertyId="2311" ... />
<properties refObjectId="1123" refPropertyId="4611" ... />
<object objectId="2123" ... />
<properties refObjectId="2123" refPropertyId="4311" ... />
<properties refObjectId="2123" refPropertyId="8611" ... />
....
</objects>

...以及以下 XPath 查询 ...
//object[//properties[@refObjectId=@objectId and not(@refPropertyId)]]

我以为这个查询会返回所有 object存在 properties 的节点具有 refObjectId 的节点属性等于 objectId object 的属性节点 并且没有“refPropertyId”属性......即只有对象 1123,而不是对象 2123 ......但它没有。看来 @objectId在嵌套谓词中不引用 objectId object 的属性节点。

有任何想法吗?我知道 XML 结构不像您期望的那样嵌套,但是这种结构是有原因的。

最佳答案

通常你应该避免使用 //在那里你可以。我会考虑改写:

//object[../properties/@refObjectId=@objectId]

在提供的表达式中,您的嵌套谓词实际上是在检查
//properties/@refObjectId=//properties/@objectId 

其中没有。

我希望这有帮助!

编辑:由于问题已更新,这里是更新的回复:
您添加了“似乎嵌套谓词中的 @objectId 并未引用对象节点的 objectId 属性。”你是绝对正确的!所以让我们修复它!
//object[../properties[not(@refPropertyId)]/@refObjectId=@objectId]

这应该更接近你所追求的!

关于xpath - 使用嵌套的 XPath 谓词......精炼,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/963451/

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