XPath Data Model: Namespace Nodes Explained
This article explores how namespace nodes are represented in the XML Path Language (XPath) data model. It examines the fundamental structure of namespace nodes, their core properties, their relationship to parent elements, and how their representation transitioned from XPath 1.0 to the modern XQuery and XPath Data Model (XDM).
Definition of Namespace Nodes
In the XPath data model, a namespace node represents an in-scope XML
namespace declaration associated with an element node. Every element
possesses an individual namespace node for every namespace prefix
currently in scope for that element, including the default namespace (if
declared), namespaces inherited from ancestor elements, and the implicit
xml prefix bound to
http://www.w3.org/XML/1998/namespace.
Core Properties of Namespace Nodes
In XPath 1.0, namespace nodes are distinct tree nodes characterized by specific accessors:
- Node Name: The local-name corresponds to the namespace prefix. If the node represents the default namespace (unprefixed), the local-name is empty. The namespace URI of the node’s name itself is always null.
- String-Value: The string-value of a namespace node is the actual namespace URI assigned to the prefix.
- Parent: The element node to which the namespace is attached acts as the parent of the namespace node.
- Node Kind: Evaluates specifically to
"namespace".
Tree Relationships and the Namespace Axis
Although an element serves as the parent of its namespace nodes,
namespace nodes are not considered child nodes of that element.
Consequently, they do not appear along the child:: axis.
Instead, XPath 1.0 provides a dedicated namespace:: axis to
traverse all namespace nodes belonging to an element context.
Because namespaces are inherited hierarchically down the XML tree, child elements receive copies of their ancestors’ namespace nodes unless explicitly overridden. This means distinct namespace nodes exist for each element in the hierarchy, even if they bind the same prefix to the same URI.
Evolution in XPath 2.0 and XDM
In XPath 2.0, XPath 3.0, and the formal XQuery and XPath Data Model (XDM), namespace nodes were deprecated due to memory overhead and implementation complexity. In XDM:
- Elements retain an abstract set of namespace
bindings (prefix-URI pairs) via the
dm:namespace-bindingsaccessor rather than requiring physical node objects in memory. - The
namespace::axis is marked as optional and deprecated. - Namespace nodes are retained strictly for backward compatibility
with XPath 1.0 expressions that explicitly navigate the
namespace::axis. Modern processing generally relies on functions such asfn:in-scope-prefixes()andfn:namespace-uri-for-prefix()to inspect element namespaces without constructing separate node trees.