XML Namespaces Explained

May 28, 20265 min read

Why Namespaces Exist

XML lets you define your own element names. That freedom creates collisions when you combine documents from different sources. Two <title> elements might mean completely different things — a book title versus a job title.

Namespaces solve this by attaching a URI to element and attribute names, making them globally unique. <book:title> and <job:title> are now distinct, even though the local name is the same.

The Namespace Declaration

Namespaces are declared with the xmlns attribute:

<book xmlns="http://example.com/book">
  <title>XML Basics</title>
</book>

This applies the namespace http://example.com/book to the <book> element and all its descendants — unless overridden by another declaration.

Prefixed Namespaces

When you need multiple namespaces in one document, use prefixes:

<catalog xmlns:book="http://example.com/book"
         xmlns:job="http://example.com/job">
  <book:title>XML Basics</book:title>
  <job:title>Senior Developer</job:title>
</catalog>

The prefix is a shorthand — the XML parser replaces book:title with {http://example.com/book}title internally. The prefix name itself is irrelevant; only the URI matters.

The URI vs. the Prefix

A common misconception: the namespace URI does not need to point to a real web page. It is an identifier, not a link.

<!-- These are the SAME namespace — same URI, different prefixes -->
<doc xmlns:a="http://example.com/ns">
  <a:item>Hello</a:item>
</doc>

<doc xmlns:b="http://example.com/ns">
  <b:item>Hello</b:item>
</doc>

Both <a:item> and <b:item> expand to {http://example.com/ns}item. The parser treats them as identical.

Default Namespace

An xmlns declaration without a prefix sets the default namespace for the element and its descendants:

<table xmlns="http://www.w3.org/1999/xhtml">
  <tr>
    <td>Data cell</td>
  </tr>
</table>

Here <table>, <tr>, and <td> are in the XHTML namespace. No prefix needed.

To switch back to no namespace, declare an empty default namespace:

<document xmlns="http://example.com/doc">
  <section>Namespaced content</section>
  <raw xmlns="">Unnamespaced content</raw>
</document>

Attributes and Namespaces

Attributes behave differently from elements:

<book xmlns="http://example.com/book"
      id="123"
      xml:lang="en">
  <title>XML Namespaces</title>
</book>
  • idNot in any namespace. Unprefixed attributes are always in no namespace, even when the element has a default namespace.
  • xml:lang — In the built-in xml namespace (http://www.w3.org/XML/1998/namespace).
  • title — In the default namespace http://example.com/book.

This is a frequent source of confusion. To put an attribute in a namespace, you must use a prefix:

<book xmlns:isbn="http://example.com/isbn"
      isbn:number="978-0123456789">

Namespace Scope

Namespace declarations apply to the element where they are declared and all descendants. Child elements can override:

<root xmlns="http://example.com/default">
  <child>Default namespace</child>
  <child xmlns="http://example.com/other">Different namespace</child>
  <child>Back to default namespace</child>
</root>

Each <child> element's namespace is determined by the closest xmlns declaration going up the tree.

Common Namespace URLs

PrefixURIUsed For
xmlhttp://www.w3.org/XML/1998/namespaceBuilt-in XML attributes (lang, space, base)
xmlnshttp://www.w3.org/2000/xmlns/Namespace declarations themselves
xsihttp://www.w3.org/2001/XMLSchema-instanceSchema-related attributes (type, nil, schemaLocation)
xslhttp://www.w3.org/1999/XSL/TransformXSLT stylesheets
svghttp://www.w3.org/2000/svgSVG graphics
xhtmlhttp://www.w3.org/1999/xhtmlXHTML documents

The xml and xmlns prefixes are reserved — you cannot redeclare them.

Practical Tips

  • Use default namespace for the dominant vocabulary in your document to reduce noise.
  • Use prefixes for secondary vocabularies like metadata or inline elements from other domains.
  • Never use relative URIs as namespace identifiers — they are deprecated and cause interoperability issues.
  • Be consistent with prefix names across your organization, even though the prefix is technically arbitrary.
  • Remember: unprefixed attributes are never in a namespace, regardless of the default namespace on the element.

Key Takeaways

  • Namespaces prevent name collisions by pairing local names with URIs
  • The prefix is just a shorthand — only the URI matters for identity
  • Default namespace applies to elements, never to unprefixed attributes
  • Namespace URI does not need to be a real URL — it is an identifier
  • Child elements override parent namespace declarations
  • Prefixed attributes are the only way to put an attribute in a namespace

Try It Yourself

Format XML documents with proper namespace indentation using our XML Formatter. Paste your XML and see clean, readable output instantly.