XPath 获取父元素 [英] XPath get parent element
问题描述
我有一个与 XPath 相关的问题.我有一个像这样的 XML 条目列表:
I have an XPath related question. I have a list of XML entries like this:
<NavigationEntry>
<BaseData>
<Name>Entry Name</Name>
</BaseData>
<Relation>
<RelationEntry isDefault="0">
<ParentNavigationEntry>456</ParentNavigationEntry>
<Order>200</Order>
</RelationEntry>
</Relation>
<NavigationEntry>
<BaseData>
<Name>Entry Name</Name>
</BaseData>
<Relation>
<RelationEntry isDefault="0">
<ParentNavigationEntry>123</ParentNavigationEntry>
<Order>200</Order>
</RelationEntry>
</Relation>
问题是:如何获取所有具有值为 456 的 Field ParentNavigationEntry 的导航条目.
The question is: how do I get all navigation entries that has a Field ParentNavigationEntry with the value 456.
我试过了//NavigationEntry//RelationEntry[ParentNavigationEntry="456"]
但这只给了我 RelationEntry 字段,但我需要 NavigationEntry 字段.
But this only gives me the RelationEntry Field, but I need the NavigationEntry Field.
是否可以通过单个 XPath 查询来解决此问题?
Is it possible to solve this with a single XPath query?
推荐答案
问题是:我如何获得所有具有字段的导航条目具有值的 ParentNavigationEntry456.
The question is: how do I get all navigation entries that has a Field ParentNavigationEntry with the value 456.
//NavigationEntry[
/Relation
/RelationEntry
/ParentNavigationEntry = 456
]
注意:选择那些 NavigationEntry
元素,然后过滤谓词.此外,当模式众所周知时,使用 //
不是一个好习惯,因为即使在匹配之后它也会遍历整个后代树.
Note: Select those NavigationEntry
elements and then filter predicate. Also, when the schema is well known it's not good practice to use //
because it traverse the whole descendant tree even after a match.
这篇关于XPath 获取父元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!