7. XML Namespaces
What you will learn
- Why name clashes happen
- How to declare a namespace with
xmlns - Prefixes and default namespaces
- How namespaces are used in real files
The problem: the same name, two meanings
Because you invent your own tag names, two different documents may use the same name for different things. When you combine them, a program cannot tell them apart.
XML code: a clash
<table>
<tr><td>Apples</td><td>Bananas</td></tr>
</table>
<table>
<name>Coffee table</name>
<width>80</width>
<length>120</length>
</table>
The first table is an HTML-style table of data. The second is a piece of furniture. If both appear in one file, software has no way to know which is which.
The solution: namespaces
A namespace is a label that says which vocabulary a name belongs to. It is a web address (a URI) that acts purely as a unique identifier. You declare it with the xmlns attribute.
Using a prefix
Write xmlns:prefix="address", then put the prefix and a colon in front of each name from that vocabulary.
XML code
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:f="https://example.com/furniture">
<h:table>
<h:tr><h:td>Apples</h:td><h:td>Bananas</h:td></h:tr>
</h:table>
<f:table>
<f:name>Coffee table</f:name>
<f:width>80</f:width>
</f:table>
</root>
What it means
There are now two different elements both called table:
h:table, which belongs to the XHTML namespacef:table, which belongs to the furniture namespace
A program sees them as different because their namespace address differs. The prefixes (h and f) are only shortcuts you chose.
The address is just an identifier
The namespace address does not need to be a real web page, and the parser never visits it. It only has to be unique and used consistently. Organisations normally use an address on a domain they own. Use an address on your own domain, or a clearly fictional one like https://example.com/furniture while learning.
Two addresses are only the same if they match exactly, letter for letter. http://example.com/a and https://example.com/a are different namespaces.
A default namespace
Write xmlns="address" with no prefix to give a namespace to every unprefixed element inside. That saves typing prefixes.
XML code
<table xmlns="https://example.com/furniture">
<name>Coffee table</name>
<width>80</width>
</table>
Here table, name, and width all belong to the furniture namespace, with no prefixes needed.
A default namespace does not apply to attributes. An unprefixed attribute has no namespace at all. Only a prefixed attribute belongs to a namespace.
Scope
A namespace declaration applies to the element that carries it and everything inside it. It is common to declare all namespaces once on the root element. A declaration deeper in the tree can add or override one for that part only.
XML code
<page xmlns="http://www.w3.org/1999/xhtml">
<p>An XHTML paragraph.</p>
<figure>
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40">
<circle cx="20" cy="20" r="18"/>
</svg>
</figure>
</page>
What it means
page, p, and figure are XHTML elements. The svg element changes the default namespace for itself and its children, so svg and circle belong to the SVG namespace. This is how different XML languages are mixed in one file.
Real namespaces you will meet
| Language | Namespace address |
|---|---|
| XHTML | http://www.w3.org/1999/xhtml |
| SVG | http://www.w3.org/2000/svg |
| XML Schema (XSD) | http://www.w3.org/2001/XMLSchema |
| XSLT | http://www.w3.org/1999/XSL/Transform |
Check each against the official standard before relying on it in a project. You will see the XSD and XSLT ones in later lessons.
Why namespaces matter in practice
- RSS and Atom feeds add extra fields with namespaced elements, such as podcast details.
- Sitemaps declare their namespace on the root element.
- Android layout files use
xmlns:android. - Office documents and many web service formats rely on them.
When a tool does not find your elements, a missing or mistyped namespace is one of the most common causes.
Common mistakes
- Using a prefix that was never declared.
- Mistyping the namespace address by one character, which makes it a different namespace.
- Believing the address must be a working web page.
- Expecting a default namespace to apply to attributes.
- Forgetting that when a default namespace exists, tools such as XPath need it taken into account.
Exercise
Write a root element <catalog> that declares a prefix b for the namespace https://example.com/books. Inside it, write a <b:book> element containing <b:title>Learning XML</b:title>.
Show solution
<catalog xmlns:b="https://example.com/books">
<b:book>
<b:title>Learning XML</b:title>
</b:book>
</catalog>
Knowledge check
1. Which attribute declares a namespace?
xmlns, written as xmlns:prefix="address" or just xmlns="address".
2. Does the namespace address have to be a real web page?
No. It is only a unique identifier.
3. What problem do namespaces solve?
They stop names from different vocabularies clashing when documents are combined.
Summary
- A namespace is a unique address that says which vocabulary an element belongs to.
- Use a prefix (
xmlns:f) or a default namespace (xmlns), and declare it on the root when you can. - The address is only an identifier, and it must match exactly.