XPath/XQuery root function

Summary

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, focus-dependent, and special-streaming-rules.
The one-argument form of this function is deterministic, context-independent, focus-independent, and special-streaming-rules.

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].

Examples

These examples use some variables which could be defined in 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 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().

Error Conditions

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

If the context item is absent, dynamic error

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