XML Namespaces
Estimated study time: 10 minutes.
As XML documents combine data from multiple sources, it's common for two different vocabularies to use the same element name for different things. Namespaces solve this by qualifying element names so they don't collide.
The Problem Namespaces Solve
<!-- Which "title" is which? -->
<catalog>
<book>
<title>Learning XML</title>
</book>
<course>
<title>Data Analytics</title>
</course>
</catalog>
In this simple case it's fine, but when merging documents from different systems, identical tag names from different vocabularies can clash and cause ambiguity.
Declaring a Namespace
<catalog xmlns:book="http://example.com/book"
xmlns:course="http://example.com/course">
<book:title>Learning XML</book:title>
<course:title>Data Analytics</course:title>
</catalog>
The xmlns:prefix="URI" attribute declares a namespace with a short prefix. The URI doesn't need to point to a real, live webpage — it just needs to be a unique identifier for that vocabulary.
Default Namespace
<catalog xmlns="http://example.com/default">
<title>Applies to this namespace by default</title>
</catalog>
How Namespace Scope Works
A namespace declaration applies to the element where it is written and to everything inside it, unless a child declares something different. Two rules surprise many beginners:
- The prefix is only a nickname. What really identifies a namespace is its URI.
<a:title xmlns:a="urn:books">and<b:title xmlns:b="urn:books">are the same element to a namespace-aware parser. - A default namespace does not apply to attributes. Unprefixed attributes are in no namespace, even when the element they sit on has a default namespace.
You can also switch the default namespace off for a section by declaring an empty one:
<catalog xmlns="urn:example:catalog">
<item>In the catalog namespace</item>
<note xmlns="">
<text>In no namespace at all</text>
</note>
</catalog>
Namespaces You Will Meet in Real Projects
| Vocabulary | Namespace URI | Seen in |
|---|---|---|
| XML Schema | http://www.w3.org/2001/XMLSchema | XSD files, usually with the xs or xsd prefix |
| XSLT | http://www.w3.org/1999/XSL/Transform | Stylesheets that transform XML |
| SVG | http://www.w3.org/2000/svg | Vector graphics files |
| SOAP envelope | http://schemas.xmlsoap.org/soap/envelope/ | SOAP 1.1 web service messages |
In schemas, the targetNamespace attribute says which namespace the declared elements belong to, and elementFormDefault="qualified" requires local elements in instance documents to be namespace-qualified too:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/course"
xmlns="http://example.com/course"
elementFormDefault="qualified">
<xs:element name="title" type="xs:string"/>
</xs:schema>
Querying Namespaced XML with XPath
The most common namespace bug appears when querying. An XPath expression like /catalog/title returns nothing if catalog is in a default namespace, because in XPath an unprefixed name means "no namespace". You must map a prefix to the URI in your code and use it in the expression. In Python with lxml:
from lxml import etree
doc = etree.parse("catalog.xml")
ns = {"c": "http://example.com/default"}
titles = doc.xpath("/c:catalog/c:title/text()", namespaces=ns)
print(titles)
The prefix c in your code does not have to match anything in the file; only the URI has to match. In Java, the equivalent is to make the parser namespace aware and supply a NamespaceContext to the XPath object.
Habits That Prevent Namespace Trouble
- Choose URIs you control, such as your company domain, so they stay unique.
- Declare namespaces once on the root element instead of repeating them on many children.
- Never change a namespace URI casually; to software, it creates a completely different vocabulary.
- Turn on namespace awareness in every parser you use.
When debugging, print the fully qualified name of an element, which combines the namespace URI and the local name, often written as {uri}localName. Seeing the URI next to each name makes it obvious whether an element ended up in the namespace you expected, or silently fell into no namespace because a declaration was missing. Most libraries expose this directly, for example the tag property of an element in Python's ElementTree or the getNamespaceURI() method on a DOM node in Java.