fn:root

Returns the root of the tree to which $arg belongs. This will usually, but not necessarily, be a document node.

Signatures

fn:root() as node()
fn:root($arg as node()?) as node()?

Properties

The zero-argument form of this function is deterministic, context-dependent, and focus-dependent.

The one-argument form of this function is deterministic, context-independent, and focus-independent.

Rules

If the function is called without an argument, the context item (.) is used as the default argument. The behavior of the function if the argument is omitted is exactly the same as if the context item had been passed as the argument.

The function returns the value of the expression ($arg/ancestor-or-self::node())[1].

Error Conditions

The following errors may be raised when $arg is omitted:

If the context item is absent, dynamic error [ERRXPDY0002]

If the context item is not a node, type error [ERRXPTY0004].

Examples

These examples use some variables which could be defined in [XQuery 3.1: An XML Query Language] as:

let $i := <tool>wrench</tool>
let $o := <order> {$i} <quantity>5</quantity> </order>
let $odoc := document {$o}
let $newi := $o/tool

Or they could be defined in [XSLT 3.0] as:

<xsl:variable name="i" as="element()">
  <tool>wrench</tool>
</xsl:variable>

<xsl:variable name="o" as="element()">
  <order>
    <xsl:copy-of select="$i"/>
    <quantity>5</quantity>
  </order>
</xsl:variable>

<xsl:variable name="odoc">
  <xsl:copy-of select="$o"/>
</xsl:variable>

<xsl:variable name="newi" select="$o/tool"/>

fn:root($i) returns the element node $i

fn:root($o/quantity) returns the element node $o

fn:root($odoc//quantity) returns the document node $odoc

fn:root($newi) returns the element node $o

The final three examples could be made type-safe by wrapping their operands with fn:exactly-one().