How to get horizontal node depth?

note I coined the term horizontal depth to measure the sub-size node inside a tree.

So, imagine that xpath will have something like / html / table / tbody / tr / td and "horizontal depth" 5

I am trying to figure out if there is a way to identify and select elements based on this horizontal depth.

How can I find the maximum depth?

+1
source share
2 answers

If you need all nodes with depth> = 5:

/*/*/*/*//* 

And if you need all nodes with depth == 5:

 /*/*/*/*/* 

There is actually an XPath count function that you can combine with the ancestor axis:

 //*[count(ancestor::*) >= 4] 
+4
source

I think that “vertical depth” and “horizontal depth” are ambiguous. Is there any reason not to use the axial terminology that already exists in XPath and refer to the "number of ancestors" and "the number of previous siblings"? It is a little more detailed, but not very detailed, and a) it is unambiguous and b) the members map to count(ancestor::*) and count(preceding-sibling::*) .

+1
source

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


All Articles