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
<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:
<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:
<a:item xmlns:a="https://example.com/shop"/>
<zz:item xmlns:zz="https://example.com/shop"/>Using an undeclared prefix is an error
<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:
<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
cxabove) 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:
<!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"?
- Downloads a schema from it
- Uses it only as a unique identifier
- Checks the website exists
- Converts it to a prefix
Which elements does xmlns="https://example.com/x" on the root apply to?
- Only the root
- All unprefixed elements inside it (unless redeclared)
- All attributes
- Only elements with the prefix
x:
Are <a:item xmlns:a="urn:shop"/> and <b:item xmlns:b="urn:shop"/> the same element type?
- Yes — the namespace URI and local name match
- No — the prefixes differ
- Only if a DTD says so
- No — prefixes must be one letter