Skip to content

Instantly share code, notes, and snippets.

@adamretter
Created August 21, 2012 21:40
Show Gist options
  • Save adamretter/3419684 to your computer and use it in GitHub Desktop.
Save adamretter/3419684 to your computer and use it in GitHub Desktop.
Calculate the depth of tree in XQuery
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)
@sonymoparthi
Copy link

Thanks @adamretter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment