Bogdan Ghervan
Bogdan Ghervan

Reputation: 301

Select an element that has another element anywhere inside

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.

Upvotes: 7

Views: 7914

Answers (2)

Azat Razetdinov
Azat Razetdinov

Reputation: 1028

a[descendant::b]

is more accurate and efficient than

a[.//b]

which is equal to

a[self::node()/descendant-or-self::node()/child::b]

Upvotes: 17

Jeff Yates
Jeff Yates

Reputation: 62377

You should try:

//a[.//b]

Upvotes: 10

Related Questions