XML Namespaces

Design decisions reached at the XML WG meeting in Montreal, Canada, August 22, 1987.

Editor:

Andrew Layman, Microsoft Corporation, andrewl@microsoft.com

Currently, XML documents use names (e.g. element names, attribute names, etc.) whose meaning is associated with the document's document type. The Document Type Definition (DTD) serves both to define a class of document and also a namespace (all the names that can be used within the document type). There are no simple, standardized means to express a document type which uses names from several namespaces. This paper proposes a conceptual model and concrete syntax for an implementation of namespaces. Document instances may use names from several spaces, while being self-describing regarding the namespaces they employ (that is, recognition of a namespace is not dependent on having read the resource which defines it).

Briefly, a namespace has a defining schema, such as a DTD. The schema defines a number of names (e.g. elements, attributes, notations, etc.). The schema can be reached via a URI; this URI is the identifier of the namespace. All particular names are qualified, that is, they consist of two parts: the namespace's identifier and the specific name within the namespace (e.g. the URI to the schema and the element name within the schema). Every qualified names is globally unique, and every specific name has only one fully-qualified form. Various syntactic devices are used to implement this conceptual model within XML.

What a namespace is and is not

A namespace is a controlled set of names. It is simply a mechanism to make sure that names are globally unique, and to allow any reader of a document to find the globally-unique, fully-qualified form of each name. It has no special provisions for associating semantics with names. A namespace per se does not imply any specific meaning to the names defined in it. It has nothing special to do with validation, multiple inheritance, formatting, versioning, brevity, or anything besides making sure that names are globally-unique and globally-defined.

In the Instance

We reserve and pre-define the special processing instruction called "xml:namespace" which will be recognized by the XML parser. In use, it has the form

<?xml:namespace href="uri" as="ns"?>

The href attribute contains a URI. This instruction associates ns with uri, such that ns can be used as a stand-in for uri as the namespace's identifier (the first part of a qualified name). This makes it valid to use names of the form ns:specific-name anywhere that XML allows a name, with the meaning that the name is to be interpreted as fully-qualified with the two parts, uri and specific-name. For example:

This introduces the namespace into the scope of its containing document, such that ns can be used as to qualify any element in the document.

<?xml:namespace href="http://zoo.org/schema.dtd/" as="zoo"?>
<zoo:animalFriends>
  <zoo:Bear>Christopher</zoo:Bear>
  <zoo:Rabbit>Sabrina</zoo:Rabbit>
</zoo:animalFriends>

(We will discuss later a mechanism that greatly reduces the need to qualify every name explicitly.)

Namespaces not only serve to tie a name to a particular definition, they also disambiguate similar-seeming names within a single document.

<?xml:namespace href="http://zoo.org/schema.dtd/" as="zoo"?>
<?xml:namespace href="http://stockmarket.org/schema.dtd/" as="market"?>
<?xml:namespace href="http://navigation.org/schema.dtd/" as="nav"?>
<someElement>
  <zoo:Bear>Christopher</zoo:Bear>
  <market:Bear>down 127 points</market:Bear>
  <nav:Bear>North by Northwest</nav:Bear>
</someElement>

Unqualified Names

All names in a document have a fully-qualified form. Unless explicitly qualified, the outermost element in a document is always assumed to come from the document's DTD namespace. For contained elements, the explicit namespace and ":" can also be omitted. If so, the enclosing element's namespace is inferred (for sub-elements, attributes, etc.). These rules ensure that all names in all existing XML documents have fully-qualified names matching their current meaning, and all validations remain unchanged.

Here is an example of explicit and inferred namespaces:

<?xml:namespace href="http://zoo.org/schema.dtd/" as="zoo"?>
<?xml:namespace href="http://foodstore.com/schema.dtd/" as="food"?>
<zoo:animalFriends>
  <Bear>Christopher</Bear>
  <Rabbit size="small" food:eats="dandilions">Sabrina</Rabbit>
</zoo:animalFriends>

Here, Bear, Rabbit, and size are qualified to the http://zoo.org/schema.dtd/ namespace, while eats is qualified to http://foodstore.com/schema.dtd/.

The pre-defined attribute xml:child-namespace sets a default namespace for contained elements differing from that of the parent.

<?xml:namespace href="http://zoo.org/schema.dtd/" as="zoo"?>
<?xml:namespace href="http://w3.org/HTML/schema.dtd/" as="html"?>
<html:ul xml:child-namespace="zoo">
  <Bear>Christopher</Bear>
  <Rabbit>Sabrina</Rabbit>
  <Pig>Claude</Pig>
  <Hedgehog>Alfred</Hedgehog>
</html:ul>

References