Names and namespaces
QName
A qualified name: an optional namespace prefix, a colon, and a local name — the form every element and attribute name takes in a namespaced document.
A QName is what you write. What the processor compares is the expanded name it resolves to: the namespace URI the prefix is bound to, plus the local name. Two documents using different prefixes for the same URI contain the same elements, and a document reusing one prefix for two URIs in different scopes contains different ones.
As a datatype, xs:QName is unusual and worth treating with suspicion. Validating a value against it requires the prefix to be in scope at that point in the document, so a schema-valid value in one document is invalid in another that spells the prefix differently.
- Defined in
- Namespaces in XML 1.0 §4
- Category
- Names and namespaces
- Also written
- qualified name, prefix:local
Shown, not asserted
All 2 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.
xs:QName rejects a prefixed value whose prefix is not declared in scope.
schema.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="v" type="xs:QName"/> </xs:schema>value a:bWhat the engine returns
Invalid asxs:QName.Which prefix a document chose does not matter: what is compared is the namespace URI.
document.xml <r xmlns:z="urn:a"><z:c/></r>expression count(//*[namespace-uri()='urn:a'])What the engine returns
1
What catches people out
- A QName with no prefix is still a QName. In a document with a default namespace it is in that namespace; otherwise it is in none.
- xs:QName values in element content — as in XSD's own type attribute — depend on prefixes declared in the instance document, which is why they survive copy-paste so badly.
- A colon is legal in an xs:Name but not in an xs:NCName. That difference is exactly what separates a QName's two halves.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- Datatypesxs:QNameThe XSD datatype, with the literals that pass and fail put through the validator.
- Datatypesxs:NCNameThe half of a QName either side of the colon must be.
- XPathnamespace-uri()How to ask which namespace a node is actually in, rather than which prefix it wears.
Related terms
- NCNameA non-colonised name: an XML name with no colon in it, which is what a namespace prefix and a local name each have to be.
- NamespaceA URI that qualifies element and attribute names, so identically-named elements from different vocabularies can coexist in one document.
- Namespace prefixThe short NCName bound to a namespace URI by an xmlns:prefix declaration, used before a colon to qualify names.
- Expanded nameThe pair of namespace URI and local name that a QName resolves to — what processors actually compare when they match a name.
- Default namespaceThe namespace declared by a bare xmlns="…", which applies to unprefixed element names on that element and its descendants.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.