I have an HTML page with this structure:
<big><b>Staff in:</b></big>
<br>
<a href='...'>Movie 1</a>
<br>
<a href='...'>Movie 2</a>
<br>
<a href='...'>Movie 3</a>
<br>
<br>
<big><b>Cast in:</b></big>
<br>
<a href='...'>Movie 4</a>
How do I select Movies 1, 2, and 3 using Xpath? I wrote this query
'//big/b[text()="Staff in:"]/following::a'
but it returns Movies 1, 2, 3, and 4. I guess I need to find a way to get items after <big><b>Staff in:
but before the next <big>
.
Thanks,
Assuming that
<big><b>Staff in:</b></big>
is a unique element that we can use as 'anchor', you can try this way :Basically, the xpath finds all
<a>
that is following sibling of the 'anchor'<big>
element mentioned above, and restrict the result to those having nearest preceding sibling<big>
equals the anchor element.output in xpath tester given markup in question as input (with minimal adjustment to make it well-formed XML) :