Name

[2.0] root()

Given a node, returns the root of the tree to which the node belongs.

Syntax

node() root()
node() root(node()?)

Inputs

An optional node. If no node is provided, the context node is used.

Outputs

The root of the tree to which the node (or the context node, if the call was root()) belongs. This will usually be a document node, but that isn’t always the case.

If an argument is provided, and that argument is the empty sequence, the empty sequence is returned. Also, if the argument itself is a document node, the argument is simply returned.

Defined in

XQuery 1.0 and XPath 2.0 Functions and Operators section 14, “Functions and Operators on Nodes.”

Example

To test the root() function, we’ll take another look at our list of favorite albums:

<?xml version="1.0"?>
<!-- favorites.xml -->
<list>
  <title>A few of my favorite albums</title>
  <listitem>A Love Supreme</listitem>
  <listitem>Beat Crazy</listitem>
  <listitem>Here Come the Warm Jets</listitem>
  <listitem>Kind of Blue</listitem>
  <listitem>London Calling</listitem>
  <listitem>Remain in Light</listitem>
  <listitem>The Joshua Tree</listitem>
  <listitem>The Indestructible Beat of Soweto</listitem>
</list>

We’ll use this stylesheet to test the root() function:

<?xml version="1.0"?>
<!-- root.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ora="http://www.oreilly.com"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:text>&#xA;Tests of the root() ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.