简体   繁体   中英

XPath get parent element

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>

The question is: how do I get all navigation entries that has a Field ParentNavigationEntry with the value 456.

I tried //NavigationEntry//RelationEntry[ParentNavigationEntry="456"]

But this only gives me the RelationEntry Field, but I need the NavigationEntry Field.

Is it possible to solve this with a single XPath query?

The question is: how do I get all navigation entries that has a Field ParentNavigationEntry with the value 456.

//NavigationEntry[
   /Relation
      /RelationEntry
         /ParentNavigationEntry = 456
]

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.

Add /ParentNavigationEntry to your XPath..

Example...

//NavigationEntry//RelationEntry[ParentNavigationEntry="456"]//ParentNavigationEntry

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM