Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select an element that has another element anywhere inside

Tags:

xpath

How do I select any node a that has node b anywhere inside it?

Given the following three XML documents:

<a>
    <b></b>
</a>

or

 <a>
    <c>
        <b></b>
    </c>
</a>

or

   <a/>

I want the a element in the first two documents to be selected.

Apparently, a[//b] is not a solution.

like image 610
Bogdan Ghervan Avatar asked Dec 04 '22 16:12

Bogdan Ghervan


2 Answers

a[descendant::b]

is more accurate and efficient than

a[.//b]

which is equal to

a[self::node()/descendant-or-self::node()/child::b]
like image 200
Azat Razetdinov Avatar answered Jan 01 '23 11:01

Azat Razetdinov


You should try:

//a[.//b]

like image 32
Jeff Yates Avatar answered Jan 01 '23 10:01

Jeff Yates