Artemiy
Artemiy

Reputation: 43

XPath: select children nodes of particular parent

there are a few cases

Case A:

<Root>
  <Defaults Id="a"></Defaults>
</Root>

Case B:

<Root>
  <Repeat>
    <Defaults Id="b"></Defaults>
  </Repeat>
</Root>

Case C (number of nested "Repeat" might be unlimited):

<Root>
  <Repeat>
    <Repeat>
      <Repeat>
        <Defaults Id="c"></Defaults>
      </Repeat>
    </Repeat>
  </Repeat>
</Root>

Case D:

<Root>
  <Repeat>
    <Page Id="p1">
      <Defaults Id="d"></Defaults>
    </Page>
  </Repeat>
</Root>

I need XPath query that returns Defaults nodes that belong to Root element or located inside of Repeat nodes only. If at least one parent isn't Repeat node or Root, they shouldn't be included to the result. So result of query should return nodes from test cases A, B, C.

Thanks!

Upvotes: 1

Views: 332

Answers (1)

kjhughes
kjhughes

Reputation: 111491

This XPath,

//Defaults[parent::Root or parent::Repeat]

will select all Default elements with a parent of Root or Repeat,

<Defaults Id="a"></Defaults>
<Defaults Id="b"></Defaults>
<Defaults Id="c"></Defaults>

as requested.

Upvotes: 1

Related Questions