QName
QNames were introduced through XML namespaces with the aim of serving as a URI reference. QName stands for “qualified name” and defines a valid identifier for elements and attributes. QNames are generally used to refer to certain elements or attributes within XML documents.
motivation
Because URI references can be long and contain forbidden characters for element / attribute names, QNames are used to map the URI and namespace prefix. By mapping, URIs can be abbreviated and thus written more comfortably in XML documents (see example)
Formal definition
QNames are formally defined by the W3C as follows:
QName ::= PrefixedName | UnprefixedName
PrefixedName ::= Prefix ':' LocalPart
UnprefixedName ::= LocalPart
Prefix ::= NCName
LocalPart ::= NCName
NCName is defined as follows:
NCName ::= Name - (Char* ':' Char*) /* An XML Name, minus the ":" */
Name ::= NameStartChar (NameChar)*
NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6]
| [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF]
| [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF]
| [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD]
| [#x10000-#xEFFFF]
NameChar ::= NameStartChar | "-" | "." | [0-9]
| #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
Char ::= /* any Unicode char, excluding surrogate blocks FFFE and FFFF. */
#x9 | #xA | #xD | [#x20-#xD7FF]
| [#xE000-#xFFFD] | [#x10000-#x10FFFF]
The prefix is used as a placeholder for the namespace and the LocalPart as a local part for the qualified identifier. Local part can be an attribute name or an element name.
example
<?xml version='1.0'?>
<doc xmlns:x="http://example.com/ns/foo">
<x:p/>
</doc>
In line 2 the prefix is x
defined which is linked to the URI "http://example.com/ns/foo". This prefix can later be used as an abbreviation for this namespace. In the rest of the document, the tag is x:p
a valid QName because it uses it x
as a reference to the namespace and p
as a local part. The tag doc
is also a valid QName, but it only consists of the local part.
See also
- CURIE : Compact URI
- Fully qualified name