Name

[2.0] base-uri()

Returns the base URI of a given node.

Syntax

xs:anyURI? base-uri(node()?)
xs:anyURI? base-uri()

Input

A node. Without an argument, base-uri() returns the base URI of the context item.

Output

The base URI of the given node.

The base-uri() function works with the XML Base specification. By default the base URI is the URI of the XML document itself, but a document can change that by using the xml:base attribute on any element.

If a given node does not have a base URI property and the node has a parent, base-uri() looks at the node’s ancestors. The function attempts to find the base URI of the node’s parent, then its parent’s parent, and so forth, until it either finds a base URI property or reaches a node that does not have a parent. If no base URI property can be found, base-uri() returns the empty sequence. If the argument to base-uri() is the empty sequence, the function returns the empty sequence.

Defined in

XQuery 1.0 and XPath 2.0 Functions and Operators section 2, “Accessors.”

Example

Here’s a revised version of our list of cars. Notice that the <manufacturer> elements all use the xml:base attribute to define a new base URI:

<?xml version="1.0" encoding="utf-8"?>
<!-- xmlbase.xml -->
<cars>
  <manufacturer name="Chevrolet"
    xml:base="http://www.chevrolet.com/">
    <car>Cavalier</car>
    <car>Corvette</car>
    <car>Impala</car>
    <car>Malibu</car>
  </manufacturer>
  <manufacturer name="Ford"
    xml:base="http://www.ford.com/"> <car>Pinto</car> <car>Mustang</car> <car>Taurus</car> </manufacturer> ...

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.