Namespace declaration attributes

Namespace declaration attributes are specified in the start tag of a direct element constructor.

Namespace declaration attributes are used for two purposes:
  • To bind a namespace prefix to a URI
  • To set the default element namespace for the constructed element node and for its attributes and descendants

Syntactically, a namespace declaration attribute has the same form as an attribute in a direct element constructor: the attribute is specified by a name and a value. The attribute name is a constant qualified name (QName). The attribute value is a string literal that represents a valid URI.

A namespace declaration attribute does not cause an attribute node to be created.

Important: The name of each namespace declaration attribute in a direct element constructor must be unique. Otherwise, the expression results in an error.

How a namespace prefix is bound to a URI

If the QName begins with the prefix xmlns followed by a local name, the declaration is used to bind the namespace prefix (specified as the local name) to a URI (specified as the attribute value). For example, the namespace declaration attribute xmlns:metric = "http://example.org/metric/units" binds the prefix metric to the namespace http://example.org/metric/units.

When the namespace declaration attribute is processed, the prefix and URI are added to the statically known namespaces of the constructor expression, and the new binding overrides any existing binding of the given prefix. The prefix and URI are also added as a namespace binding to the in-scope namespaces of the constructed element.

For example, in the following element constructor, namespace declaration attributes are used to bind the namespace prefixes metric and english:
<box xmlns:metric = "http://example.org/metric/units"
xmlns:english = "http://example.org/english/units">
  <height> <metric:meters>3</metric:meters> </height>
  <width> <english:feet>6</english:feet> </width>
  <depth> <english:inches>18</english:inches> </depth>
</box>

How the default element namespace is set

If the QName is xmlns with no prefix, the declaration is used to set the default element namespace. For example, the namespace declaration attribute xmlns = "http://example.org/animals" sets the default element namespace to http://example.org/animals.

When the namespace declaration attribute is processed, the value of the attribute is interpreted as a namespace URI. This URI specifies the default element namespace of the constructor expression, and the new specification overrides any existing default. The URI is also added, with no prefix, to the in-scope namespaces of the constructed element, and the new specification overrides any existing namespace binding that has no prefix. If the namespace URI is a zero-length string, the default element namespace of the constructor expression is set to "none".

For example, in the following direct element constructor, a namespace declaration attribute sets the default element namespace to http://example.org/animals:
<cat xmlns = "http://example.org/animals">
  <breed>Persian</breed>
</cat>

Namespace bindings after elements are copied to constructors

When an existing element node is copied by an element constructor, the namespace bindings are resolved in the following way:

  • The copied element retains all in-scope-namespaces of the original element. The default namespace is treated like any other namespace binding. The copied node has the same default namespace as the original node. If the original node has no default namespace, the copy has no default namespace.
  • The copied node inherits in-scope namespaces from the constructed node. If there is a naming conflict, the namespace bindings that were copied from the original node take precedence.