XML Namespaces
As XML became widely used across different industries and systems, a problem emerged: different organizations began using the same tag names for completely different things. When two XML vocabularies are combined into a single document, identical element names can cause serious confusion. XML namespaces solve this problem by allowing the same element name to be used in the same document without any conflict.
The Problem That Namespaces Solve
Imagine two different XML formats are combined in one file. One format is for a retail product, and another is for an HTML table. Both use the element name <table>.
<document>
<table>
<name>Oak Dining Table</name>
<price>299.00</price>
</table>
<table>
<tr>
<td>Item A</td>
<td>Item B</td>
</tr>
</table>
</document>
Both <table> elements exist in the same document, but one is a piece of furniture and the other is an HTML table. Without namespaces, there is no way to tell which vocabulary each element belongs to.
What is a Namespace?
A namespace is a unique name that identifies a group of XML elements and attributes. It is used to distinguish elements that have the same name but belong to different vocabularies or applications.
A namespace is represented by a URI (Uniform Resource Identifier), which looks like a web address. The URI does not have to point to an actual webpage — it simply acts as a unique identifier.
Declaring a Namespace
Namespaces are declared using the xmlns attribute. The declaration takes this form:
xmlns:prefix="URI"
xmlnsis the reserved keyword for namespace declarations.prefixis a short alias used in the document to refer to the namespace.URIis the unique identifier for the namespace.
Example Using Namespaces to Solve the Conflict
<?xml version="1.0" encoding="UTF-8"?>
<document
xmlns:furniture="http://example.com/furniture"
xmlns:html="http://www.w3.org/1999/xhtml">
<furniture:table>
<furniture:name>Oak Dining Table</furniture:name>
<furniture:price>299.00</furniture:price>
</furniture:table>
<html:table>
<html:tr>
<html:td>Item A</html:td>
<html:td>Item B</html:td>
</html:tr>
</html:table>
</document>
Now there is no ambiguity. furniture:table clearly refers to a piece of furniture, and html:table clearly refers to an HTML table layout.
Namespace Prefix Rules
- The prefix can be any valid XML name except
xml(reserved) orxmlns(reserved for namespace declarations). - The prefix is just an alias for the full URI — only the URI matters for uniqueness.
- Two different prefixes can point to the same URI and are treated as the same namespace.
- The prefix is used throughout the document wherever that namespace's elements appear.
Default Namespaces
If most of a document belongs to one namespace, repeating the prefix on every element becomes verbose. A default namespace can be declared without a prefix. All elements without any prefix automatically belong to the default namespace.
<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="http://example.com/catalog">
<product>
<name>Bluetooth Speaker</name>
<price>49.99</price>
</product>
</catalog>
Here, xmlns="http://example.com/catalog" declares a default namespace. All elements — <catalog>, <product>, <name>, <price> — automatically belong to that namespace without needing a prefix.
Combining Default and Prefixed Namespaces
<?xml version="1.0" encoding="UTF-8"?>
<order xmlns="http://example.com/orders"
xmlns:pay="http://example.com/payments">
<item>Laptop</item>
<quantity>2</quantity>
<pay:method>Credit Card</pay:method>
<pay:amount currency="USD">1800.00</pay:amount>
</order>
<item> and <quantity> belong to the default namespace, while <pay:method> and <pay:amount> belong to the payment namespace.
Namespace Scope
A namespace declared on an element applies to that element and all of its descendants — unless overridden by a new namespace declaration on a child element.
<root xmlns="http://example.com/main">
<child>Belongs to main namespace</child>
<section xmlns="http://example.com/other">
<item>Belongs to other namespace</item>
</section>
<child>Back to main namespace</child>
</root>
The inner <section> overrides the namespace for itself and its children. Once the </section> tag closes, the parent namespace becomes active again.
Namespaces and Attributes
Attributes without a namespace prefix do not belong to any namespace — even if the element they appear on has a default namespace. Namespaced attributes must explicitly use a prefix.
<product xmlns:meta="http://example.com/metadata"
meta:id="P-001"
name="Wireless Keyboard">
<price>39.99</price>
</product>
Here, meta:id belongs to the metadata namespace, while name does not belong to any namespace.
Key Points
- Namespaces prevent naming conflicts when combining XML from different sources.
- A namespace is identified by a URI, which is a unique string (not necessarily a real web page).
- Namespaces are declared with
xmlns:prefix="URI"on an element. - A default namespace (
xmlns="URI") applies to all unprefixed elements within its scope. - Namespace declarations apply to the element where they are declared and all its descendants.
- Attributes do not inherit the default namespace — they must be explicitly prefixed to belong to a namespace.
