Created
August 21, 2012 21:40
-
-
Save adamretter/3419684 to your computer and use it in GitHub Desktop.
Calculate the depth of tree in XQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare function local:_recurse_depth($element, $count) as xs:integer* | |
{ | |
if($element[child::element()])then | |
for $child in $element/child::element() return | |
local:_recurse_depth($child, $count + 1) | |
else | |
$count | |
}; | |
declare function local:depth($x as node()?) as xs:integer | |
{ | |
max(local:_recurse_depth($x, ceiling(count($x/child::element()) div (count($x/child::element()) + 0.9)))) | |
}; | |
let $input := <a><b><c><d><e></e></d></c></b><b><a><b /></a></b><b></b></a> | |
return | |
local:depth($input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @adamretter.