How xmlns=“” Affects Child XML Elements
In XML document processing, declaring xmlns="" on an
element explicitly unsets or undeclares the inherited default namespace.
This ensures that the targeted element, along with all of its unprefixed
child elements, belongs to no namespace (the empty or null namespace)
rather than inheriting the default namespace of an ancestor. This
article explains how namespace undeclaration works, its cascading
behavior down the XML tree, and its impact on prefixed elements and XML
query processors.
How Default Namespace Inheritance Works
When a default namespace is declared on a parent element using
xmlns="http://example.com/ns", it applies to that element
and automatically cascades to all nested child elements that do not have
an explicit prefix or their own namespace declaration.
Without an explicit override, every child element remains in the ancestor’s default namespace.
The Role of xmlns=“” (Namespace Undeclaration)
Setting xmlns="" sets the default namespace URI to an
empty string. According to the W3C Namespaces in XML specification, this
resets the default namespace scope.
The effects of xmlns="" on child elements are as
follows:
- Unprefixed Child Elements: Any child element inside
the scope of
xmlns=""that does not carry a namespace prefix will belong to no namespace. These elements will not be associated with the parent’s or root’s URI. - Prefixed Child Elements: Elements with explicit
namespace prefixes (such as
<custom:tag>) are completely unaffected byxmlns="". They will continue to resolve to whichever URI is bound to their prefix. - Subsequent Namespace Declarations: A deeper child
element can override
xmlns=""at any point by declaring a newxmlns="http://new-namespace.com"or re-declaring the original parent namespace.
Example Scenario
Consider the following XML structure:
<root xmlns="https://example.com/parent">
<parentChild>Inherits parent namespace</parentChild>
<isolatedContainer xmlns="">
<childOne>Belongs to no namespace</childOne>
<childTwo>
<grandChild>Also belongs to no namespace</grandChild>
</childTwo>
<ns:prefixedChild xmlns:ns="https://example.com/custom">
Belongs to the custom namespace
</ns:prefixedChild>
</isolatedContainer>
</root>In this structure: 1. <root> and
<parentChild> belong to the
https://example.com/parent namespace. 2.
<isolatedContainer>, <childOne>,
<childTwo>, and <grandChild> have
no namespace (null). 3.
<ns:prefixedChild> belongs to
https://example.com/custom.
Behavior with Attributes
Default namespaces do not apply to unprefixed attributes by standard
XML rules. An unprefixed attribute like id="123" is always
in no namespace, regardless of whether xmlns="" is present.
Therefore, xmlns="" only affects element names, not
attribute names.
Impact on Parsing and XPath Queries
When querying or parsing XML documents using DOM parsers, XPath, or XSLT:
- XPath 1.0/2.0: A query selecting
/root/isolatedContainer/childOnewithout namespace prefixes will successfully match<childOne>because it resides in the empty namespace. - Namespace-Aware APIs: In APIs such as DOM or SAX,
isolatedContainer.getNamespaceURI()andchildOne.getNamespaceURI()will returnnull(or an empty string depending on the implementation language), whereasroot.getNamespaceURI()returnshttps://example.com/parent.