Having markup like this:
<div class="foo">
<div><span class="a1"></span><a href="...">...</a></div>
<div><span class="a2"></span><a href="...">...</a></div>
<div><span class="a1"></span>some text</div>
<div><span class="a3"></span>some text</div>
</div>
I am interested in getting all <a> and some text ONLY if adjacent span is of class a1. So at the end of the whole code my result should be <a> from first div and some text from third one. It’d be easy if <a> and some text were inside span or div would have class attribute, but no luck.
What I am doing now is look for span with a1 class:
//div[contains(@class,'foo')]/div/span[contains(@class,'a1')]
then I get its parent and do another query() with that parent as context node. This simply looks far from being efficient so the question clearly is if there is any better way to accomplish my goal?
THE ANSWER ADDENDUM
As per @MarcB accepted answer, the right query to use is:
//div[contains(@class,'foo')]/div/span[contains(@class,'a1')]/..
but for <a> it may be better to use:
//div[contains(@class,'foo')]/div/span[contains(@class,'a1')]/../a
the get the <a> instead of its container.
The nice thing about xpath queries is that you can essentially treat them like a file system path, so simply having
will find all your .a1 nodes that are below a .foo node, then move up one level to the a1 nodes’ parents.