home contribute faq download

FunctX XQuery Functions

functx:max-depth

The maximum depth of elements in an XML tree

Google
Webxqueryfunctions.com

Description

The functx:max-depth function returns the maximum level of elements within a document, as an integer. 1 represents the root, 2 is a child of the root, etc. If $root is a document node, it is counted as a level. Text and other kinds of nodes are not counted as a level.

Arguments and Return Type

NameTypeDescription
$root node()? the root to start from
return value xs:integer?

XQuery Function Declaration

See XSLT definition.
XQuery Syntax for April 2005 - January 2007 (1.0):
declare namespace functx = "http://www.functx.com"; 
declare function functx:max-depth 
  ( $root as node()? )  as xs:integer? {
       
   if ($root/*)
   then max($root/*/functx:max-depth(.)) + 1
   else 1
 } ;
XQuery Syntax for July 2004:
declare namespace functx = "http://www.functx.com"; 
declare function functx:max-depth 
  ( $root as node()? )  as xs:integer? {
       
   if ($root/*)
   then max(for $node in $root/*
               return functx:max-depth($node)) + 1
   else 1
 } ;
XQuery Syntax for May 2003:
declare namespace functx = "http://www.functx.com" 
define function max-depth 
  ( $root as node()? )  as xs:integer? {
       
   if ($root/*)
   then fn:max(for $node in $root/*
               return functx:max-depth($node)) + 1
   else 1
 }

Examples

let $in-xml :=
<authors>
   <author>
      <fName>Kate</fName>
      <lName>Jones</lName>
   </author>
   <author>
      <fName>John</fName>
      <lName>Doe</lName>
   </author>
</authors>
return
XQuery ExampleResults
functx:max-depth($in-xml)
3
functx:max-depth($in-xml/author[1])
2

See Also

functx:depth-of-nodeThe depth (level) of a node in an XML tree

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Need XQuery Help?
D A T Y P I C
Training | Consulting | Development

XQuery by Priscilla WalmsleyGet the book!
XQuery by Priscilla Walmsley