Terminology
The terms element (which has namespace, namespace prefix, local name, and attribute list), attribute (which has namespace, namespace prefix, local name, and value), XML namespace,
and XMLNS namespace are defined by the DOM Standard [DOM].
Namespace mappings
The namespace mapping is a data
structure with the following fields:
- Default namespace
- Either a namespace URL, null, or missing. By
default, its value is missing.
- Prefix to URL
- An ordered list of key/value pairs, where keys are namespace
prefixes and values are namespace URLs or none. By default,
the set contains two pairs: (
xml
, the XML
namespace) and (xmlns
, the XMLNS
namespace).
There will be at most one pair with same key in the prefix to URL list. However,
there might be more than one pairs with same value in the list.
A default
namespace declaration with namespace URL url is
an attribute whose namespace is the XMLNS
namespace, local
name is xmlns
, and value is url.
A namespace prefix
declaration with prefix prefix and namespace
URL url is an attribute whose namespace is the XMLNS
namespace, namespace prefix is
xmlns
, local
name is prefix, and value is url.
Namespace fixup of an XML element for serialization
The steps for namespace fixup of an XML element for
serialization, with an element element and
optional namespace mapping nsmap, are as follows:
- If nsmap is not specified, let nsmap be a new namespace
mapping.
- Otherwise, let nsmap be a deep clone of nsmap.
- Let attributes be the list containing the attributes in the attribute list of element in same order.
- For each attribute attr in attributes, in order:
- If the namespace
of attr is not the XMLNS namespace,
skip the rest of these substeps and continue with any next attribute.
- Let local name be the local name of attr.
- Let value be the value of attr.
- If local name is
xml
, or if value is the XML namespace or the
XMLNS namespace, do nothing.
- Otherwise, if local name is
xmlns
:
- If value is the empty string, set the default namespace of nsmap to null.
- Otherwise, set the default
namespace of nsmap to value.
- Otherwise:
- Delete the key/value pair whose key is equal to local name, if any, from the prefix to URL list of
nsmap.
- If value is the empty string, append
key/value pair (local name,
none) to the prefix
to URL list of nsmap.
- Otherwise, append key/value pair (local name,
value) to the prefix to URL list of
nsmap.
- Let tag name and new attr be the
values returned by the steps to obtain a QName with the
namespace, namespace prefix, and
local name of element, attributes, and nsmap and with the is element flag.
- If new attr is not null, prepend it to attributes.
- Let attribute specifications be the empty list.
- For each attribute attr in attributes, in order:
- Let attr name and new attr be
the values returned by the steps to obtain a QName with
the namespace, namespace prefix,
and local name of
attr, attributes, and nsmap (without the is element flag).
- If new attr is not null, insert it into attributes just after attr.
- Append attribute name/value pair (attr name,
the value of attr) to attribute specifications.
- Return tag name, attribute
specifications, and nsmap.
These steps return three values. The first value, tag
name, is used as the tag name in the start and end tag of the
serialization of element. The second value, attribute specifications, contains zero or more pairs of
attribute names and values, which are used in the start tag of the
serialization of element. The third value, nsmap, is used as the input to the steps for serialization
of the child elements of element.
The steps to obtain a QName, with parameters namespace URL, prefix, local
name, attributes, and nsmap,
where attributes and nsmap are
shared with the callee such that any modification to them is also
reflected to those variables in the steps which invoked these steps,
and the is element flag, are as follows:
- Let qname be local name.
- Let new attr be null.
- If namespace URL is null:
- If the is element flag is not set, do nothing.
- Otherwise, if the default
namespace of nsmap is not null:
- Set the default
namespace of nsmap to null.
- If attributes contains an attribute attr
whose namespace is
the XMLNS namespace and local name is
xmlns
:
- If the value of
attr is not the empty string, replace attr in attributes by a new defualt namespace
declaration with namespace URL the empty string,
preserving the order.
- Otherwise, let new attr be a new defualt namespace
declaration with namespace URL the empty string.
- Otherwise, if namespace URL is not null:
- If namespace URL is the XML
namespace, let prefix be
xml
.
- Otherwise, if namespace URL is the XMLNS
namespace, let prefix be
xmlns
.
- If namespace URL is the XMLNS
namespace and qname is
xmlns
,
do nothing.
- Otherwise, if the is element flag is set, prefix is null, and the default namespace of nsmap is missing:
- Set the default
namespace of nsmap to namespace
URL.
- Let new attr be a new default namespace
declaration with namespace URL namespace
URL.
- Otherwise, if the is element flag is set, prefix is null, and the default namespace of nsmap is equal to namespace URL, do
nothing.
- Otherwise, if prefix is not null and there is
the key/value pair whose key is prefix and value is
namespace URL in the prefix to URL list of nsmap, prepend prefix followed by a
U+003A
COLON
character
(:
) to qname.
- Otherwise, if prefix is not null and there is
no key/value pair whose key is prefix in the prefix to URL list of nsmap:
- Append key/value pair (prefix, namespace URL) to the prefix to URL list of
nsmap.
- Let new attr be a new namespace prefix
declaration with prefix prefix and
namespace URL namespace URL.
- Prepend prefix followed by a
U+003A
COLON
character
(:
) to qname.
- Otherwise, if there is the key/value pair whose value is equal
to namespace URL in the prefix to URL list of nsmap:
- Let prefix be the key of the last key/value
pair whose value is equal to namespace URL in the
prefix to URL list
of nsmap.
- Prepend prefix followed by a
U+003A
COLON
character
(:
) to qname.
- Otherwise, if the is element flag is set and
the default namespace of
nsmap is equal to namespace URL,
do nothing.
- Otherwise, if the is element flag is set, prefix is null, and attribute does not
contain an attribute whose
namespace is the
XMLNS namespace and local name is
xmlns
:
- Set the default
namespace of nsmap to namespace
URL.
- Let new attr be a new default namespace
declaration with namespace URL namespace
URL.
- Otherwise:
- Let prefix be
an
where n is chosen such that
n is the minimum non-negative integer an
is not found as a key of the
prefix to URL list
of nsmap.
- Append key/value pair (prefix, namespace URL) to the prefix to URL list of
nsmap.
- Let new attr be a new namespace prefix
declaration with prefix prefix and
namespace URL namespace URL.
- Prepend prefix followed by a
U+003A
COLON
character
(:
) to qname.
- Return qname and new attr.
These steps do not prevent an unserializable set of tag
name and attributes from being returned when,
for example, there is an xmlns
attribute in null namespace, there is
an xml
attribute in
the XMLNS namespace whose value is not equal to the
XML namespace, or the given nsmap
contains a key which is not a valid XML NCName
.