Skip to main content

Search 400 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, guides, blog and product.

Namespaces guide

Why the default XML namespace does not apply to attributes

Distinguish unprefixed element and attribute names, qualify attributes deliberately, and prevent namespace-aware lookups from returning nothing.

7 min read · Updated 2026-08-21

The short answer

A default namespace applies to unprefixed element names, not to unprefixed attribute names. In an element inside a default namespace, id is still an attribute with no namespace; if an attribute belongs to a namespace, it must use a prefix bound to that namespace URI.

Resolve the element and attribute separately

The source spelling can make the names look like one vocabulary, but namespace processing produces different expanded names. The element below is in urn:example:catalog, while its unprefixed id attribute has no namespace.

Default namespace with an unqualified attribute
<item xmlns="urn:example:catalog" id="42"/>
Explicitly namespaced attribute
<item xmlns="urn:example:catalog" xmlns:cat="urn:example:catalog" cat:id="42"/>

Use the contract to decide whether qualification is required

Do not add a prefix merely for visual consistency. Many vocabularies intentionally keep attributes unqualified, while others define particular attributes in a namespace. Schema form rules and the vocabulary specification decide which expanded name is valid.

  • Look up unqualified attributes without a namespace URI.
  • Look up prefixed attributes with the URI bound to that prefix.
  • Treat id and cat:id as different attributes even on the same element.
  • Preserve attribute namespaces explicitly during transformations.

Fix namespace-aware code at construction and lookup

DOM and streaming APIs commonly distinguish getAttribute from getAttributeNS, or accept namespace URI and local name as separate arguments. Pass no namespace for an unprefixed attribute and the exact URI for a qualified one.

Test both a namespaced element with an ordinary id and an explicitly namespaced attribute. This catches code that incorrectly inherits the element's default namespace into every attribute lookup.

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.