Parameterized depth request in xpath

I am trying to select <BBB> elements that have at least two more elements of the same type in their descendants at different depths. I found this possible with:

 //BBB//BBB//BBB 

but if I want to change the depth of my request, I have to write:

 //BBB//BBB//BBB//BBB or //BBB//BBB 

Is it possible to specify the depth of my request with a parameter?

+4
source share
2 answers

Using

 //BBB[ancestor::BBB[$pN]] 

Where $pN should be replaced with the desired number of BBB descendants at different depths.

0
source

You can search for elements that have at least two / three / ... ancestors of the type, for example:

 //*[count(ancestor::BBB) >= 2] 
+5
source

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


All Articles