Stephan Schulze
Stephan Schulze

Reputation: 139

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?

Upvotes: 2

Views: 4458

Answers (2)

user357812
user357812

Reputation:

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.

Upvotes: 6

Travyguy9
Travyguy9

Reputation: 4344

Add /ParentNavigationEntry to your XPath..

Example...

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

Upvotes: 0

Related Questions