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.
<item xmlns="urn:example:catalog" id="42"/><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.
Prove the fix
- Validate both spellingsConfirm both documents parse before comparing their expanded attribute names.
- Review namespace identitySeparate the visible prefix from the URI that defines the expanded name.
- Inspect attribute terminologyConnect source spelling to the namespace-aware attribute model.
Related guides
- XML namespace prefix vs URIUnderstand why prefixes are replaceable aliases, why namespace URIs are compared literally, and how expanded names keep documents interoperable.
- How to fix an unbound XML namespace prefixFind the missing namespace declaration, restore it at the narrowest useful scope, and avoid repairs that silently change the vocabulary URI.
- XSD targetNamespace and elementFormDefaultPredict which instance elements must be namespace-qualified, separate schema vocabulary from target vocabulary, and fix no-declaration validation failures.
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.