SyntaxStudy
Sign Up
XML Default and Prefixed Namespace Declarations
XML Beginner 1 min read

Default and Prefixed Namespace Declarations

A default namespace declaration changes the namespace that unprefixed element names belong to, but it does not affect attribute names. Unprefixed attributes always belong to no namespace — they are in the null namespace, regardless of any default namespace declaration in scope. This asymmetry is intentional: attributes are considered to be properties of their parent element and are implicitly scoped to it, so they rarely need a separate namespace. Namespace declarations can be placed on any element and apply to that element and all its descendants, unless overridden by a closer declaration. A declaration of xmlns="" (empty string) undeclares the default namespace — from that element inward, unprefixed elements belong to no namespace. This is useful when mixing a namespaced vocabulary with a non-namespaced fragment. Prefixed declarations are usually placed on the root element or on the first element that uses the namespace, keeping declarations visible and centralized. When documents from different namespaces are merged programmatically — as in SOAP messages that combine an envelope namespace with a body namespace — the processing software must track which namespace URI each element and attribute belongs to using namespace-aware APIs rather than relying on prefix strings.
Example
<?xml version="1.0" encoding="UTF-8"?>

<!-- Default namespace declared on root -->
<root xmlns="http://example.com/default"
      xmlns:meta="http://example.com/meta"
      xmlns:html="http://www.w3.org/1999/xhtml">

    <!-- 'child' is in default namespace (example.com/default) -->
    <child>

        <!-- Unprefixed attributes are in NO namespace -->
        <item id="1" meta:version="2.0">text</item>
        <!--
            id        → null namespace (NOT example.com/default)
            meta:version → http://example.com/meta
        -->

    </child>

    <!-- Override default namespace locally -->
    <section xmlns="http://example.com/other">
        <!-- 'para' is in example.com/other -->
        <para>Different namespace here.</para>
    </section>

    <!-- Undeclare default namespace: back to no namespace -->
    <raw xmlns="">
        <data>No namespace at all.</data>
    </raw>

    <!-- Mixed: XHTML fragment embedded -->
    <html:div>
        <html:p>This paragraph is in the XHTML namespace.</html:p>
    </html:div>

</root>