You can call
Nokogiri::XML::Node#ancestors.size to
see how deeply a node is nested. But
is there a way to determine how deeply
nested the most deeply nested child of
a node is?
Use:
count(ancestor::node())
This expression expresses the number of ancesstors the context (current) node has in the document hierarchy.
To find the nesting level of the "most deeply nested child" one must first determine all "leaf" nodes:
descendant-or-self::node()[not(node())]
and for each of them get their nesting level using the above XPath expression.
Then the maximum nesting level has to be calculated (the maximum of all numbers produced ), and this last calculation is not possible with pure XPath 1.0.
This is possible to express in a single XPath 2.0 expression:
max(for $leaf in /descendant-or-self::node()[not(node())],
$depth in count($leaf/ancestor::node())
return
$depth
)
Update:
It is possible to shorten this XPath 2.0 expression even more:
max(/descendant-or-self::node()[not(node())]/count(ancestor::node()))