使用嵌套的 XPath 谓词......精炼 [英] Working With Nested XPath Predicates ... Refined
问题描述
所有很棒的答案!但问题值得细化......
All great answers! But the question deserves refinement ...
我有以下示例 XML ...
I've got the following sample 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 查询...
... and the following XPath query ...
//object[//properties[@refObjectId=@objectId and not(@refPropertyId)]]
我认为这个查询会返回所有 object
节点,其中有一个 properties
节点,该节点具有一个 refObjectId
属性等于 object
节点的 objectId 属性并且没有 'refPropertyId' 属性......即只有对象 1123,而不是对象 2123......但它没有.嵌套谓词中的 @objectId
似乎没有引用 object
节点的 objectId
属性.
I thought this query would return all object
nodes where there is a properties
node that has a refObjectId
attribute that equals the objectId
attribute of the object
node and no 'refPropertyId' attribute ... namely object 1123 only, not object 2123 ... but it doesn't. It seems the @objectId
in the nested predicate does not refer to the objectId
attribute of the object
node.
有什么想法吗?我知道 XML 结构不像您期望的那样嵌套,但这种结构是有原因的.
Any ideas? I know the XML structure isn't nested as you would expect, but there are reasons for this structure.
推荐答案
通常,您应该尽可能避免使用 //
.我会考虑改写:
Generally you should avoid using //
where you can. I'd consider rephrasing:
//object[../properties/@refObjectId=@objectId]
在提供的表达式中,您的嵌套谓词实际上是在检查
In the expression provided, your nested predicate is actually checking for
//properties/@refObjectId=//properties/@objectId
其中没有.
我希望这会有所帮助!
由于问题已更新,这里是更新的回复:您添加了似乎嵌套谓词中的 @objectId 并未引用对象节点的 objectId 属性."你是绝对正确的!所以让我们修复它!
Since the question has been updated here is an updated response: You added "It seems the @objectId in the nested predicate does not refer to the objectId attribute of the object node." You're absolutely right! So let's fix it!!
//object[../properties[not(@refPropertyId)]/@refObjectId=@objectId]
这应该更接近你所追求的!
This should be closer to what you're after!
这篇关于使用嵌套的 XPath 谓词......精炼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!