XML technologies / XML / XML in a Nutshell / XLinks / Base URIs

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

Relative references such as sark.jpg, ../pi1/sark.jpg and turing/pi1/sark.jpg require an absolute base URI before retrieval. In XLinks, xml-stylesheet processing instructions, system identifiers and elsewhere, they normally resolve against the containing document or entity’s absolute URL. In a document at "http://www.turing.org.uk/turing/index.html", finding the element <image xlink:type="simple" xlink:href="pi1/sark.jpg"/> suggests the file sark.jpg at "http://www.turing.org.uk/turing/p1/sark.jpg". No surprise: HTML links have worked this way for over a decade.

XML adds complications. A document may comprise several entities from different URLs or servers. Relative URLs then use the specific entity’s base, not the whole document’s.

Second, the document can reset the base with attributes xml:base. These may be on the element XLink itself or any ancestor in the same entity. This XLink points to ftp://ftp.knowtion.net/pub/mirrors/gutenberg/etext93/wizoz10.txt:

<novel xmlns:xlink="http://www.w3.org/1999/xlink" xml:base="ftp://ftp.knowtion.net/pub/mirrors/gutenberg/etext93/" xlink:type="simple" xlink:href="wizoz10.txt">
  <title>The Wonderful Wizard of Oz</title>
  <author>L. Frank Baum</author>
  <year>1900</year>
</novel>

So does this one:

<novel xmlns:xlink="http://www.w3.org/1999/xlink" xml:base="ftp://ftp.knowtion.net/" xlink:type="simple" xlink:href="/pub/mirrors/gutenberg/etext93/wizoz10.txt">
  <title>The Wonderful Wizard of Oz</title>
  <author>L. Frank Baum</author>
  <year>1900</year>
</novel>

And this one:

<series xml:base="ftp://ftp.knowtion.net/">
  <title>Oz Books</title>
  <author>L. Frank Baum</author>
  <novel xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="/pub/mirrors/gutenberg/etext93/">
    <title>The Wonderful Wizard of Oz</title>
    <year>1900</year>
  </novel>
  ...
</series>

All point to ftp://ftp.knowtion.net/pub/mirrors/gutenberg/etext93/wizoz10.txt regardless of the containing document’s origin. The nearest attribute xml:base in the same entity overrides its containing entity’s base URL.

xml:base attributes may themselves be relative. Resolve them against a higher ancestor’s base and/or the containing entity’s URL. In this example, resolving the attributes xlink:href of the element authors requires three separate ancestors’ URLs:

<authors xml:base="http://www.literature.org/authors/" xmlns:xlink="http://www.w3.org/1999/xlink">
  <author xml:base="baum-l-frank/">
    <name>L. Frank Baum</name>
    <novel  xml:base="the-wonderful-wizard-of-oz/">
      <title>The Wonderful Wizard of Oz</title>
      <year>1900</year>
      <chapter xlink:type="simple" xlink:href="introduction.html">Introduction</chapter>
      <chapter xlink:type="simple" xlink:href="chapter-01.html">The Cyclone</chapter>
      <chapter xlink:type="simple" xlink:href="chapter-02.html">The Council with the Munchkins</chapter>
      ...
    </novel>
  </author>
</authors>

What if the top element has a relative base or no attribute xml:base? Use its containing entity’s absolute URL. In theory, an entity must come from somewhere, providing a final base. But some APIs lose URLs or create in-memory documents without them, preventing full resolution. Specifications are unclear; one interpretation uses the empty string, meaning the current document’s URI. In normal file or URL inputs, every element should have a resolvable absolute base.

This subtle point bears repeating: resolution stays within one entity, not the whole document. In a multi-entity document, each entity’s base counts. Attributes xml:base apply only within their own entity. If A includes B, no attributes xml:base in A resolve B’s relative URLs. If B’s attributes xml:base cannot finish resolution, B’s loading URL is the final absolute base. xml:base attributes in ancestors from other entities are ignored.

Although we focused on attributes xml:base acting on attributes xlink:href, they work elsewhere too, including XInclude and XHTML 2.0. xml:base is a late XML arrival and not universal. XHTML 1.0 and 1.1 ignore attributes xml:base when resolving relative URLs in elements a and img. They instead use traditional elements base in the document’s element head.

  

 

 

 

back to top