XML promises the ability to describe data from all sorts of domains.
Once you have an XML vocabulary and a DTD, you can say anything about a
domain that the vocabulary and DTD allow. What do you do when your
vocabularies overlap?
Let's design a document format for royalty. We'd like it to look
pretty, much like HTML, but we also want the ability to change the
information's style about various heads of state. So, we have the
standard HTML vocabulary:
HEAD TITLE BODY TABLE B P A (etc)
...and another vocabulary for royalty:
MONARCH NATION TITLE (etc)
Now imagine you have a document that contains some information about
royalty:
<ROYALTY>
<HEAD>
<TITLE>20th Century Royalty</TITLE>
</HEAD>
<BODY>
Hail Caesar! Here is the canonical list of all monarchs reigning
some time during the 20th century:
<MONARCH RANK="KING">
<NATION>Elbonia</NATION>
<TITLE>His Royal Elboniness</TITLE>
<NAME>Elbo</NAME>
<COUNT>I</COUNT>
</MONARCH>
<!-- And so on... -->
</BODY>
</ROYALTY>
How could a program (for example, a stylesheet processor) distinguish
between the document's TITLE and King Elbo I's TITLE? The XML namespace
mechanism allows a document's author specify *which* definition that
author means when using a particular element (i.e., distinguishing
between a document's TITLE and a monarch's TITLE).
An XML namespace is a collection of XML elements grouped by an
unambiguous name. The name then prepends to the element or attribute
name to produce a unique name. A colon separates the unambiguous name
and the element, or attribute, name (called the "namespace prefix" and
the "local part", respectively) to produce the new, unique name. For
example:
<royalty:TITLE>...</royalty:TITLE>
In this example, "royalty" is the namespace prefix, and "TITLE" is the
local part. The namespace indicates *which* TITLE is being used in the
document, and allows vocabularies to mix. But simple namespace prefixes
aren't enough. Namespace prefixes could potentially collide, and you'd
have ambiguous tags again. So, each namespace prefix is associated with
a URI (Uniform Resource Identifier) -- URLs (Uniform Resource Locators)
are one type of URI, which are unique on the Internet. To define a
namespace, zero or more "xmlns" attributes may be added to any XML
element to indicate the namespaces used in that element:
<royalty:TITLE
xmlns:royalty="http://itworld.com/ROYAL">
...</royalty:TITLE>
It doesn't matter if the URI accesses any real information resource.
The URI's purpose is simply to provide a globally unique name, much as
some Web sites use your email address as your account name (to uniquely
identify your account). Of course, XML files are fat enough without
repeating the same URIs over and over again. In fact, namespaces are
seldom used as shown above; instead, the W3C XML Namespace
recommendation provides several shortcuts to make namespaces easier to
use.
For example, all of the elements and attribute names inside of an
element that defines a namespace can simply use the namespace prefix,
instead of having to declare the URI again. So, the example above could
be extended to look like this:
<ROYALTY xmlns:h="http://www.w3.org/TR/REC-html40"
xmlns:r="http://itworld.com/ROYAL"
<h:HEAD>
<h:TITLE>20th Century Royalty</h:TITLE>
</h:HEAD>
<h:BODY>
Hail Caesar! Here is the canonical list of all monarchs reigning
some time during the 20th century:
<r:MONARCH RANK="KING">
<r:NATION>Elbonia</r:NATION>
<r:TITLE>His Royal Elboniness</r:TITLE>
<r:NAME>Elbo</r:NAME>
<r:COUNT>I</r:COUNT>
</r:MONARCH>
<!-- And so on... -->
</h:BODY>
</ROYALTY>
Notice that I defined the "h" namespace prefix to indicate HTML 4.0,
and the "r" namespace to indicate my "royal" extensions. (I used "r"
instead of "royalty" to improve readability.)
Namespaces make XML vocabularies more reusable, advantageous when
communities come to consensus on particular tags' meanings in reused
vocabularies. They also promote modularity, since existing vocabularies
(like SVG, MathML, and so on) can be combined in new documents.
Finally, namespaces promote extensibility of document types, allowing
vocabularies to "mix into" one another.
Now that you've got the basics, you can read more about XML namespaces
in various tutorials on the Web. Check out the resources below to learn
more about XML namespaces.