Select an item that has another item inside

How to select any node a that has node b somewhere inside?

Given the following three XML documents:

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

or

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

or

   <a/>

I want to make an element a in the first two documents was chosen.

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

+3
source share
2 answers

You must try:

//a[.//b]

+2
source
a[descendant::b]

is more accurate and efficient than

a[.//b]

which is equal

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

Source: https://habr.com/ru/post/1703364/


All Articles