home contribute faq download

FunctX XQuery Functions

functx:depth-of-node

The depth (level) of a node in an XML tree

Google
Webxqueryfunctions.com

Description

The functx:depth-of-node function returns the level of a node within a document, as an integer. 1 represents the root node, 2 is a child of the root, etc. If $node is a document node, the document node itself counts as the first level.

Arguments and Return Type

NameTypeDescription
$node node()? the node to check
return value xs:integer

XQuery Function Declaration

See XSLT definition.
XQuery Syntax for July 2004 - January 2007 (1.0):
declare namespace functx = "http://www.functx.com"; 
declare function functx:depth-of-node 
  ( $node as node()? )  as xs:integer {
       
   count($node/ancestor-or-self::node())
 } ;
XQuery Syntax for May 2003:
declare namespace functx = "http://www.functx.com" 
define function depth-of-node 
  ( $node as node()? )  as xs:integer {
       
   fn:count($node/ancestor-or-self::node())
 }

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:depth-of-node($in-xml)
1
functx:depth-of-node($in-xml/author[1])
2
functx:depth-of-node(
     $in-xml/author[1]/fName/text())
4

See Also

functx:max-depthThe maximum depth of elements 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