Angie
Angie

Reputation: 33

Select preceding sibling of a matched sibling using XPath?

I have the following code:

<include>
  <fallback>
    <title>Content</title>
  </fallback>
</include>
<include>
  <fallback>
    <title>Content</title>
  </fallback>
</include>
<include> <-- I want this.
  <fallback>
    <title>Content</title>
  </fallback>
</include>
<adhoc>
  <title>Content</title>
</adhoc>
<include>
  <fallback>
    <title>Content</title>
  </fallback>
</include>

I need to find the third <include> because it has <adhoc> as its direct following-sibling. Is there away to find this with XPath?

Upvotes: 3

Views: 53

Answers (1)

har07
har07

Reputation: 89285

"I need to find the third <include> because it has <adhoc> as it's direct following-sibling."

This requirement can be translated to XPath as follow :

//include[following-sibling::*[1][self::adhoc]]

Upvotes: 4

Related Questions