What is the purpose of XML namespaces?
XML namespaces are designed to provide universally
unique names for elements and attributes. This allows
people to do a number of things, such as:
* Combine fragments from different documents without any
naming conflicts. (See example below.)
* Write reusable code modules that can be invoked for
specific elements and attributes. Universally unique
names guarantee that such modules are invoked only for
the correct elements and attributes.
* Define elements and attributes that can be reused in
other schemas or instance documents without fear of name
collisions. For example, you might use XHTML elements in
a parts catalog to provide part descriptions. Or you
might use the nil attribute defined in XML Schemas to
indicate a missing value.
As an example of how XML namespaces are used to resolve
naming conflicts in XML documents that contain element
types and attributes from multiple XML languages,
consider the following two XML documents:
<?xml version="1.0" ?>
<?xml version="1.0" ?>
Each document uses a different XML language and each
language defines an Address element type. Each of these
Address element types is different -- that is, each has
a different content model, a different meaning, and is
interpreted by an application in a different way. This
is not a problem as long as these element types exist
only in separate documents. But what if they are
combined in the same document, such as a list of
departments, their addresses, and their Web servers? How
does an application know which Address element type it
One solution is to simply rename one of the Address
element types -- for example, we could rename the second
element type IPAddress. However, this is not a useful
long term solution. One of the hopes of XML is that
people will standardize XML languages for various
subject areas and write modular code to process those
languages. By reusing existing languages and code,
people can quickly define new languages and write
applications that process them. If we rename the second
Address element type to IPAddress, we will break any
code that expects the old name.
A better answer is to assign each language (including
its Address element type) to a different namespace. This
allows us to continue using the Address name in each
language, but to distinguish between the two different
element types. The mechanism by which we do this is XML
(Note that by assigning each Address name to an XML
namespace, we actually change the name to a two-part
name consisting of the name of the XML namespace plus
the name Address. This means that any code that
recognizes just the name Address will need to be changed
to recognize the new two-part name. However, this only
needs to be done once, as the two-part name is
What is an XML namespace?
An XML namespace is a collection of element type and
attribute names. The collection itself is unimportant --
in fact, a reasonable argument can be made that XML
namespaces don't actually exist as physical or
conceptual entities . What is important is the name of
the XML namespace, which is a URI. This allows XML
namespaces to provide a two-part naming system for
element types and attributes. The first part of the name
is the URI used to identify the XML namespace -- the
namespace name. The second part is the element type or
attribute name itself -- the local part, also known as
the local name. Together, they form the universal name.
This two-part naming system is the only thing defined by
the XML namespaces recommendation.
Does the XML namespaces recommendation define anything
except a two-part naming system for element types and
This is a very important point and a source of much
confusion, so we will repeat it:
THE XML NAMESPACES RECOMMENDATION DOES NOT DEFINE
ANYTHING EXCEPT A TWO-PART NAMING SYSTEM FOR ELEMENT
TYPES AND ATTRIBUTES.
In particular, they do not provide or define any of the
* A way to merge two documents that use different DTDs.
* A way to associate XML namespaces and schema
* A way to validate documents that use XML namespaces.
* A way to associate element type or attribute
declarations in a DTD with an XML namespace.
What do XML namespaces actually contain?
XML namespaces are collections of names, nothing more.
That is, they contain the names of element types and
attributes, not the elements or attributes themselves.
For example, consider the following document.
<B google:C="google" D="bar"/>
The element type name A and the attribute name C are in
the http://www.google.org/ namespace because they are
mapped there by the google prefix. The element type name
B and the attribute name D are not in any XML namespace
because no prefix maps them there. On the other hand,
the elements A and B and the attributes C and D are not
in any XML namespace, even though they are physically
within the scope of the http://www.google.org/ namespace
declaration. This is because XML namespaces contain
names, not elements or attributes.
XML namespaces also do not contain the definitions of
the element types or attributes. This is an important
difference, as many people are tempted to think of an
XML namespace as a schema, which it is not.