How does one expand a XML QName in the presence of default namespaces?
The XML Namespace specification defines the concept of a qualified name as a name consisting of a local part and namespace URI.
A qualified name, or QName, is specified in terms of a prefix and a local part (e.g. <foo:bar xmlns:foo=’urn:foo’>), where the prefix (foo) is resolved to a namespace URI (‘urn:foo‘).
This process of resolving the prefix results in what is commonly known as the expanded name.
The rules for resolving a prefix are mostly uniform in the context of different XML processors, with the exception of how default namespaces (e.g. xmlns=’urn:foo’) are dealt with.
Specifically:
- XML elements: if the QName does not have a prefix, then the in-scope default namespace must be used.
XML |
Resolved |
<foo xmlns=’urn:foo’/> |
‘urn:foo’ |
- XML attributes: if the QName does not have a prefix, then the namespace URI is NULL, that is, the default namespace is not used.
XML |
Resolved |
<foo xmlns=’urn:foo’a=’attr1’/> |
NULL |
<foo xmlns:ns1=’urn:foo’ ns1:a=‘attr1’ /> |
‘urn:foo’ |
- Attribute value of W3C Schema primitive type QName: if the QName does not have a prefix, then the in-scope default namespace must be used.
XML |
Resolved |
<foo xmlns=’urn:foo’ a=’myname‘/> where <xsd:attribute name=’a’ type=’xsd:QName’/> |
‘urn:foo’ |
- XSLT (1.0) variable or parameter: if the QName does not have a prefix, then the namespace URI is NULL, that is, the default namespace is not used.
XSLT |
Resolved |
<xsl:variable name=’varA’ xmlns=’urn:foo’ /> |
NULL |
<xsl:variable name=’ns1:varA’ xmlns=’urn:foo’ |
‘urn:bar’ |
- XPath 1.0 node tests: if the QName does not have a prefix, then the namespace URI is NULL, that is, the default namespace is not used.
XPATH |
Resolved |
<a xmlns:ns1=’urn:foo’ xmlns=’urn:foo’> /ns1:a/b |
NULL |
/ns1:a/ns1:b |
‘urn:foo’ |
- XPath 2.0 node (name) tests: if the QName does not have a prefix, then the in-scope default namespace must be used.
XPATH |
Resolved |
<a xmlns:ns1=’urn:foo’ xmlns=’urn:foo’> /ns1:a/b |
‘urn:foo’ |