XML Namespaces — Avoid Element Name Conflicts in XML Documents
XML namespaces prevent element name conflicts when combining XML vocabularies. Here's how namespace declarations work, when you need them, and how to handle namespaces in code.
XML namespaces solve a naming conflict problem: when you combine XML from two different sources, both might use a <title> element with completely different meanings. Namespaces qualify element and attribute names with a URI, making them globally unique regardless of what other schemas use the same local name.
Use the XML Formatter to inspect and format XML documents with namespaces.
The problem namespaces solve
Without namespaces, combining two XML vocabularies causes ambiguity:
<!-- AMBIGUOUS: which <title> is which? -->
<document>
<title>XML Guide</title> <!-- Document title -->
<table>
<title>Summary Table</title> <!-- Table caption -->
<row><cell>Data</cell></row>
</table>
</document>
With namespaces, each vocabulary is clearly identified:
<document xmlns:doc="http://example.com/doc"
xmlns:tbl="http://example.com/table">
<doc:title>XML Guide</doc:title>
<tbl:table>
<tbl:title>Summary Table</tbl:title>
<tbl:row><tbl:cell>Data</tbl:cell></tbl:row>
</tbl:table>
</document>
Namespace syntax
Namespace declaration
xmlns:prefix="namespace-URI"
xmlns— the keyword that declares a namespaceprefix— the shorthand you use in element names (likedc:orxsi:)namespace-URI— a URI that uniquely identifies the namespace (doesn’t need to resolve to a real URL)
Default namespace (no prefix)
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Page</title> <!-- All elements are in the XHTML namespace -->
</head>
</html>
Without a prefix, the namespace applies to the declaring element and all its descendants by default.
Multiple namespaces
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:rss="http://purl.org/rss/1.0/">
<rss:channel>
<dc:title>My Feed</dc:title>
<dc:creator>Alice</dc:creator>
<rss:link>https://example.com</rss:link>
</rss:channel>
</root>
Common XML namespaces
| Prefix | URI | Used in |
|---|---|---|
xmlns | (namespace declaration itself) | All XML |
xsi | http://www.w3.org/2001/XMLSchema-instance | XSD validation |
xsd | http://www.w3.org/2001/XMLSchema | Schema definitions |
xsl | http://www.w3.org/1999/XSL/Transform | XSLT transforms |
dc | http://purl.org/dc/elements/1.1/ | Dublin Core metadata |
atom | http://www.w3.org/2005/Atom | Atom feeds |
soap | http://schemas.xmlsoap.org/soap/envelope/ | SOAP messages |
wsdl | http://schemas.xmlsoap.org/wsdl/ | WSDL service descriptions |
Namespaces in practice: real examples
SOAP envelope
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header/>
<soap:Body>
<m:GetPrice xmlns:m="http://www.example.org/stock">
<m:Item>AAPL</m:Item>
</m:GetPrice>
</soap:Body>
</soap:Envelope>
RSS/Atom feed with Dublin Core
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>My Blog</title>
<atom:link href="https://example.com/feed.xml" rel="self" type="application/rss+xml"/>
<item>
<title>Article Title</title>
<dc:creator>Alice Smith</dc:creator>
<dc:date>2024-03-04T14:20:34Z</dc:date>
</item>
</channel>
</rss>
XSD instance with schema location
<users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="users.xsd">
<user>
<name>Alice</name>
</user>
</users>
The xsi:noNamespaceSchemaLocation attribute tells validators where to find the XSD schema for validation.
Handling namespaces in code
JavaScript (fast-xml-parser)
import { XMLParser } from 'fast-xml-parser';
const xml = `<root xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>My Document</dc:title>
<dc:creator>Alice</dc:creator>
</root>`;
// With namespaces preserved:
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '@_',
removeNSPrefix: false // Keep namespace prefix in keys
});
const result = parser.parse(xml);
// { root: { "dc:title": "My Document", "dc:creator": "Alice" } }
// Strip namespace prefixes:
const parser2 = new XMLParser({ removeNSPrefix: true });
const result2 = parser2.parse(xml);
// { root: { title: "My Document", creator: "Alice" } }
Python (lxml with namespace handling)
from lxml import etree
xml_string = b'''<root xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>My Document</dc:title>
<dc:creator>Alice</dc:creator>
</root>'''
root = etree.fromstring(xml_string)
# Access namespaced elements with Clark notation {uri}localname:
dc_ns = "http://purl.org/dc/elements/1.1/"
title = root.find(f"{{{dc_ns}}}title")
print(title.text) # "My Document"
# Find all elements in a namespace:
for elem in root.iter(f"{{{dc_ns}}}*"):
print(elem.tag, elem.text)
# Use namespace map for convenience:
nsmap = {'dc': 'http://purl.org/dc/elements/1.1/'}
title = root.find('dc:title', nsmap)
print(title.text) # "My Document"
# XPath with namespaces:
titles = root.xpath('//dc:title', namespaces=nsmap)
Python (xml.etree.ElementTree)
import xml.etree.ElementTree as ET
xml_string = '''<root xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>My Document</dc:title>
</root>'''
# Register namespace prefix for output:
ET.register_namespace('dc', 'http://purl.org/dc/elements/1.1/')
root = ET.fromstring(xml_string)
# Clark notation for finding namespaced elements:
title = root.find('{http://purl.org/dc/elements/1.1/}title')
print(title.text) # "My Document"
Namespace pitfalls
Namespace URIs are case-sensitive: http://example.COM/ns and http://example.com/ns are different namespaces. Be consistent.
Prefix is irrelevant to identity: dc:title and meta:title are the same element if dc and meta both map to the same URI. Only the URI matters, not the prefix.
Default namespace doesn’t apply to attributes: In <element xmlns="http://example.com" attr="value">, the attr attribute is NOT in the http://example.com namespace — only elements without a prefix are. Attribute namespace must be declared explicitly.
Namespace scope: A namespace declaration applies to the element where it’s declared and all its descendants, unless overridden. You can re-declare a namespace with a different URI in a child element.
Related tools
- XML Formatter — format XML with namespace handling
- XML Validator Online — validate XML against schemas
- XML vs JSON — when to use each format
Related posts
- XML Still Matters in 2026 (Here's Where and Why) — JSON won the wire format war years ago, but XML is still everywhere it actually …
- XML Formatter Online — Beautify and Validate XML Instantly — An XML formatter adds proper indentation to minified XML, making it human-readab…
- XML vs JSON in API Design — When to Choose Each Format — JSON has largely replaced XML in REST APIs, but XML still dominates in SOAP, ent…
- XML Validator Online — Check XML Syntax and Structure — An XML validator checks that XML is well-formed (correct syntax) and optionally …
- XML vs JSON — Which Data Format Should You Use? — XML and JSON both represent structured data but make different tradeoffs. XML su…
Related tool
Format, validate, and beautify XML documents.
Written by Mian Ali Khalid. Part of the Data & Format pillar.