Default XML Namespace Syntax Using xmlns

This article explains the syntax and implementation of default XML namespaces using the xmlns attribute. It covers the fundamental declaration structure, how default namespaces affect child elements, the scope of the declaration, and the method for overriding or resetting the namespace within nested elements.

The Syntax for a Default XML Namespace

To declare a default XML namespace, assign a namespace Uniform Resource Identifier (URI) to the xmlns attribute within the opening tag of an XML element.

The standard syntax is:

xmlns="namespaceURI"

Unlike prefixed namespaces (e.g., xmlns:prefix="URI"), a default namespace does not use a colon or a prefix name.

Basic Example

When declared on the root element, all elements within the document without an explicit prefix belong to that default namespace:

<bookstore xmlns="https://www.example.com/bookstore">
    <book>
        <title>XML Guide</title>
        <author>John Doe</author>
    </book>
</bookstore>

In this example, <bookstore>, <book>, <title>, and <author> all belong to the https://www.example.com/bookstore namespace.


Scope and Inheritance

A default namespace applies to: 1. The element in which it is declared. 2. All nested descendant elements that do not have an explicit prefix or their own namespace declaration.

Overriding a Default Namespace

You can override a default namespace in any child element by declaring a new xmlns attribute inside that child element:

<document xmlns="https://www.example.com/primary">
    <chapter>Chapter 1</chapter>
    <metadata xmlns="https://www.example.com/secondary">
        <created>2026-01-01</created>
    </metadata>
</document>

Here, <document> and <chapter> use the primary namespace, while <metadata> and <created> use the secondary namespace.

Unsetting a Default Namespace

To remove a default namespace for a subset of child elements, set the xmlns attribute to an empty string:

<root xmlns="https://www.example.com/main">
    <scopedElement>Inherits namespace</scopedElement>
    <unscopedElement xmlns="">Does not belong to any namespace</unscopedElement>
</root>

Important Rules for Default Namespaces