Reputation: 59
Having a piece of html document like this:
<div class="main">
<div class="first">
**<div id="my_target"></div>**
</div>
<div class="second">
<p class="child">I need to find preceding or nearest sibling's child with id=my_target based on this text</p>
<div>...</div>
<div/>
<div>....</div>
</div>
Can someone help me to find an element with id=my_target
and having parent whose sibling's child element p contains some specific text using XPath?
Upvotes: 0
Views: 860
Reputation: 474191
Following your explanation step by step:
//div[@id="my_target" and ../following-sibling::div/p="some text"]
Demo (using xmllint
):
$ cat test.html
<div class="main">
<div class="first">
<div id="my_target">This is what I want to find</div>
</div>
<div class="second">
<p class="child">some text</p>
<div>...</div>
</div>
<div>....</div>
</div>
$ xmllint test.html --xpath '//div[@id="my_target" and ../following-sibling::div/p="some text"]'
<div id="my_target">This is what I want to find</div>
Upvotes: 2