Sponsored

XSLT Reference

base-uri()

XSLT 2.0 node function

Returns the base URI of a node as an xs:anyURI, combining the document's URI with any xml:base attributes in scope.

Syntax
base-uri(node?)

Description

base-uri() returns the base URI of a node. The base URI is determined by combining the document URI (from where the document was loaded) with any xml:base attributes present on ancestor elements. It follows the XML Base specification (RFC 3986 resolution).

When called without an argument, the context node is used. If the argument is the empty sequence, the empty sequence is returned. If no base URI can be determined, the empty sequence is returned.

Parameters

ParameterTypeRequiredDescription
nodenode()?NoThe node whose base URI is requested. Defaults to the context node.

Return value

xs:anyURI? — the base URI of the node, or the empty sequence if no base URI is available.

Examples

Report base URIs of elements with xml:base

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<root xml:base="http://example.com/docs/">
  <chapter xml:base="chapter1/">
    <section>Introduction</section>
  </chapter>
</root>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/root">
    <base-uris>
      <root-base><xsl:value-of select="base-uri(.)"/></root-base>
      <chapter-base><xsl:value-of select="base-uri(chapter)"/></chapter-base>
      <section-base><xsl:value-of select="base-uri(chapter/section)"/></section-base>
    </base-uris>
  </xsl:template>
</xsl:stylesheet>

Output:

<base-uris>
  <root-base>http://example.com/docs/</root-base>
  <chapter-base>http://example.com/docs/chapter1/</chapter-base>
  <section-base>http://example.com/docs/chapter1/</section-base>
</base-uris>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/doc">
    <links>
      <xsl:for-each select="link">
        <resolved>
          <xsl:value-of select="resolve-uri(@href, base-uri(.))"/>
        </resolved>
      </xsl:for-each>
    </links>
  </xsl:template>
</xsl:stylesheet>

Notes

  • If the document was parsed from a string (without a known URI), base-uri() may return the empty sequence.
  • base-uri() is affected by xml:base attributes anywhere in the ancestor chain. The effective base URI is the result of resolving each xml:base relative to the one above.
  • To get the base URI of the stylesheet module itself, use static-base-uri().
  • To get the URI of the root document node (ignoring xml:base), use document-uri().

See also