TExcelTAD
Home › XML › XML Namespaces

XML Namespaces

Because anyone can invent element names, two vocabularies will sooner or later use the same name for different things. A furniture shop's <table> has legs; an HTML <table> has rows. If both end up in one document, a program can't tell them apart. Namespaces solve this by attaching a unique identifier to names.

The problem

xml · try it yourself
<order>
  <table>
    <tr><td>Oak dining table</td><td>1</td></tr>
  </table>
  <table>
    <material>Oak</material>
    <seats>6</seats>
  </table>
</order>

The document is well-formed, but which <table> is the HTML one and which is the furniture?

Prefixes and xmlns

A namespace is declared with the special attribute xmlns:prefix="URI". Elements whose names start with prefix: then belong to that namespace:

xml · try it yourself
<order xmlns:h="http://www.w3.org/1999/xhtml"
       xmlns:f="https://example.com/furniture">
  <h:table>
    <h:tr><h:td>Oak dining table</h:td><h:td>1</h:td></h:tr>
  </h:table>
  <f:table>
    <f:material>Oak</f:material>
    <f:seats>6</f:seats>
  </f:table>
</order>

Now the two tables are distinct: one is {http://www.w3.org/1999/xhtml}table, the other {https://example.com/furniture}table.

The URI is just a name

The namespace URI is only a unique string. Parsers never download anything from it, and it doesn't have to point to a real page (although many organisations put documentation there). What matters is that it is unique — using a domain you own is an easy way to guarantee that.

The prefix is merely a local shortcut. These two elements are identical to a namespace-aware program, because both prefixes map to the same URI:

xml
<a:item xmlns:a="https://example.com/shop"/>
<zz:item xmlns:zz="https://example.com/shop"/>

Using an undeclared prefix is an error

xml · try it yourself
<catalog>
  <shop:item>Lamp</shop:item>
</catalog>

Add xmlns:shop="https://example.com/shop" to <catalog> and run again.

Default namespace

Writing a prefix on every element is tedious. xmlns="URI" (with no prefix) sets a default namespace for the element and all its unprefixed descendants:

xml · try it yourself
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
  <circle cx="50" cy="50" r="40" fill="teal"/>
</svg>

Here svg and circle are both in the SVG namespace even though they have no prefix.

Note: A default namespace applies to elements only. Unprefixed attributes (like cx above) are in no namespace — they simply belong to their element.

Scope

A declaration applies to the element it is written on and everything inside it, unless an inner element redeclares it. You can declare all namespaces on the root (most common) or close to where they are used.

Namespaces in JavaScript

The DOM exposes namespace information, and offers …NS methods that search by URI instead of prefix:

html · try it yourself
<!DOCTYPE html>
<html>
<body>
  <script>
    const xml = `<order xmlns:h="http://www.w3.org/1999/xhtml" xmlns:f="https://example.com/furniture">
      <h:table/><f:table/><f:table/>
    </order>`;
    const doc = new DOMParser().parseFromString(xml, 'application/xml');

    for (const t of doc.documentElement.children) {
      console.log(t.tagName, '| local:', t.localName, '| prefix:', t.prefix, '| ns:', t.namespaceURI);
    }
    const furniture = doc.getElementsByTagNameNS('https://example.com/furniture', 'table');
    console.log('Furniture tables:', furniture.length);
  </script>
</body>
</html>

Well-known namespaces

Vocabulary Namespace URI
XHTML http://www.w3.org/1999/xhtml
SVG http://www.w3.org/2000/svg
MathML http://www.w3.org/1998/Math/MathML
XSLT http://www.w3.org/1999/XSL/Transform
XML Schema http://www.w3.org/2001/XMLSchema
XLink http://www.w3.org/1999/xlink
Atom feeds http://www.w3.org/2005/Atom

The xml: prefix (as in xml:lang) is built in and never needs declaring.

Test yourself

What does a parser do with the URI in xmlns:f="https://example.com/furniture"?

  1. Downloads a schema from it
  2. Uses it only as a unique identifier
  3. Checks the website exists
  4. Converts it to a prefix

Which elements does xmlns="https://example.com/x" on the root apply to?

  1. Only the root
  2. All unprefixed elements inside it (unless redeclared)
  3. All attributes
  4. Only elements with the prefix x:

Are <a:item xmlns:a="urn:shop"/> and <b:item xmlns:b="urn:shop"/> the same element type?

  1. Yes — the namespace URI and local name match
  2. No — the prefixes differ
  3. Only if a DTD says so
  4. No — prefixes must be one letter