How to Construct an XML QName with Prefix and Local Part
In XML, a Qualified Name (QName) resolves naming conflicts by binding an element or attribute name to a specific Uniform Resource Identifier (URI) namespace. This article explains the structural syntax of a QName, detailing how the namespace prefix and the local part are combined using a colon delimiter and resolved by an XML parser.
Anatomy of a QName
A QName consists of two main lexical components defined by the W3C XML Namespaces specification:
- Prefix: An identifier that acts as a compact proxy for a full namespace URI.
- Local Part: The specific name of the XML element or
attribute (technically defined as an
NCName, or Non-Colon Name).
When combined, the syntax follows this strict format:
Prefix:LocalPart
If no namespace prefix is used, the QName consists solely of the
LocalPart (an unprefixed QName), which belongs either to
the default namespace (if declared) or to no namespace.
Step-by-Step Construction Process
1. Declare the Namespace URI
Before a prefix can be used in a QName, it must be mapped to a URI
using an xmlns attribute within the XML document:
<library xmlns:bk="https://example.org/bookstore">In this declaration, bk is established as the prefix
associated with the URI https://example.org/bookstore.
2. Define the Local Part
The local part represents the standard element or attribute name. It
must be a valid XML identifier (must not contain whitespace or colons,
and must begin with a letter or underscore). For example:
title.
3. Join with a Colon Delimiter
To construct the final QName, place the prefix and the local part
together, separated directly by a single colon (:) with no
spaces:
<bk:title>XML Fundamentals</bk:title>Here, bk:title is the complete QName.
How Parsers Resolve a QName into an Expanded Name
While the lexical representation in the document is
Prefix:LocalPart, an XML processor resolves the QName into
an Expanded Name (an internal two-part identifier):
- Namespace Name:
https://example.org/bookstore - Local Name:
title
Often represented conceptually in James Clark notation as:
{https://example.org/bookstore}title
The prefix itself serves only as a temporary syntactic placeholder within the document markup to keep files readable, while the combination of the resolved URI and the local part ensures global uniqueness.