- Why Namespaces Are Needed: Resolving Name Conflicts
- Qualified Names, Prefixes, Local Names, and Other Terminology
- Declaring Namespaces in XML Documents
- Default Namespace
- Handling Namespaces in a DTD or XML Schema
- Validating Documents with Namespaces
- What Does a Namespace Point To?
- Namespace Support and Use
- Special Attributes: xmlns, xml:space, xml:lang, and xml:base
- Common Namespaces
- For Further Exploration
Qualified Names, Prefixes, Local Names, and Other Terminology
Rather than use the curly braces notation, we can achieve the same uniqueness of naming with a simpler notation, given a few important conventions. We learned earlier that the colon character (:), although a legal character in an XML Name, is reserved for use in namespaces.
Before we see how to declare namespaces, it will be helpful to cover some of the terminology used by W3C in the Namespaces in XML Recommendation. First, let's see how W3C defines XML namespace.
XML namespaces provide a simple method for qualifying element and attribute names used in Extensible Markup Language documents by associating them with namespaces identified by URI references. . . . An XML namespace is a collection of names, identified by a URI reference [RFC 2396], which are used in XML documents as element types and attribute names. XML namespaces differ from the "namespaces" conventionally used in computing disciplines in that the XML version has internal structure and is not, mathematically speaking, a set.4
Element types are really nothing more than an element identified by its name, so the element type of the element whose start tag is <price> is simply price.
The Namespaces in XML specification briefly refers to universal names, the uniquely named elements (or attributes) associated with a particular namespace. However, the more frequently used term is qualified name (sometimes shown as QName), which is an XML Name consisting of a prefix, a single colon as a separator, and the local part (sometimes called the local name):
QName::= (Prefix ':')? LocalPart
Both Prefix and LocalPart consist of any characters that are valid in an XML Name except colon. Examples of qualified names follow:
xsl:apply-templates xlink:href xhtml:title xlink:title svg:title fo:title SuperDuperCatalog:price Discount:price price
So we speak of the prefix xsl and the local part apply-templates (an element), the prefix xlink and the local part href (which happens to be an attribute), four prefixes (xhtml, xlink, svg, and fo) with the same local part title, and so on.
Although the prefix may be any length, it is typically short to reduce typing, so SuperDuperCatalog is not a likely prefix. On the other hand, it's a good idea to keep them to at least three characters to reduce the chance of ever needing to refer to another prefix that happens to be the same. (The W3C has made some of its namespace prefixes only two or three characters, such as xsl, fo, xf, however.) It's also probably wise not to just use your company's acronym (e.g., abc for ABC.com) because other developers at your company might be tempted to use the same prefix for a different language.
Note that technically a QName does not have to include a prefix and colon (e.g., the last price example in the preceding list). If the qualified name does contain a prefix, however, it must map to a specific URI reference.
It is the URI, not the prefix, that is the actual namespace name. The prefix is merely a proxy for the URI reference because URIs may contain characters such as slash and curly braces that are not permitted in an XML Name. This means that the prefix is just a convenience; the particular prefix chosen for W3C languages (such as xsl for XSLT or xsd for XML Schema) is really only a convention. It is the URI that really matters. The actual namespace names (URIs) for common XML namespaces are shown in Table 5-1.
Another way to conceptualize an XML namespace is that it merely encompasses the set of all local parts of universal names that happen to have the same namespace URI.5