xml:base

xml:base and Relative URI Resolution

xml:base sets the base URI for relative references inside an element, such as XLink and XInclude hrefs; without it, the base is the document's own location. Older explanations say a nested xml:base is "appended" to its ancestor's. It is not: each is resolved by RFC 3986, like a relative link in a web page, so a value starting with / replaces the path and an absolute URI replaces everything:

Nested xml:base values resolved by RFC 3986, not by concatenationShell
cat > bases.xml <<'EOF'
<catalog xml:base="https://booknest.example.com/catalog/">
  <shelf xml:base="cooking/"><img href="salt.jpg"/></shelf>
  <shelf xml:base="/archive/2023/"><img href="salt.jpg"/></shelf>
  <shelf xml:base="https://cdn.example.com/"><img href="salt.jpg"/></shelf>
</catalog>
EOF
xquery -s:bases.xml -qs:'//img ! resolve-uri(@href, base-uri(.))'
Output
"https://booknest.example.com/catalog/cooking/salt.jpg"
"https://booknest.example.com/archive/2023/salt.jpg"
"https://cdn.example.com/salt.jpg"

Mind the trailing slash: cooking without it is a file name that gets replaced. The tour file's root base, https://booknest.example.com/catalog/, also redirects its XInclude of more-books.xml to the web address rather than the local folder.


xml base

This specifies the base URL for any relative URLs. When nested, the declaration in a descendant element can be appended to that in the ancestor element. xml:base is also recognized in XLink, XPointer, and XInclude.
ch02-xml-base-example.xmlXML
<?xml version="1.0" encoding="UTF-8"?>
<bookslist xml:base="http://GoldenBookstore.com">
   <gb:book xmlns:gb="http://GoldenBookstore.com" xml:base="/branch1">
      <title>Harry Potter</title>
   </gb:book>
</bookslist>