Wubin Ouyang
Wubin Ouyang

Reputation: 787

Xpath. How to specify a node between two nodes?

There may be two possible structures:

1.

<c>TTTT</c>
<a>AAAA</a>
<b>BBBB</b>
<c>CCCC</c>
<d>DDDD</d>
<c>CCCC</c>

2.

<c>TTTT</c>
<a>AAAA</a>
<b>BBBB</b>
<d>DDDD</d>
<c>CCCC</c>

How can I select the node <c> after <b> in first case and if it is the second case, set it null?

Upvotes: 1

Views: 300

Answers (1)

har07
har07

Reputation: 89325

You can use following-sibling axis for this purpose :

//b/following-sibling::*[1][self::c]

Brief explanation :

  • b/following-sibling::*[1]: get direct sibling following a <b>

  • [self::c]: check if that sibling is a <c> element

Upvotes: 2

Related Questions