• +49-(0)721-402485-12
Your experts for XML, XQuery and XML Databases


As already explained, with XML a wide variety of vocabularies can be defined. Of course, these vocabularies should be combinable, for example, in order to be able to also use XHTML formattings in a vocabulary for the description of contracts. If the same names for elements or attributes of different meanings are used in the vocabularies to be combined, conflicts will arise.

In order to solve this problem, soon after the publication of the XML Recommendation, the W3C has released a further Recommendation under the name "Namespaces in XML". It describes how definitions of different vocabularies can be separated safely from one another by assigning them to different namespaces.

First of all, a namespace consists of a URI, which means a worldwide unique key. In particular, this may also be a URL. However, it is not defined that such a URL must point to any particular target.

For a namespace, an abbreviation can be defined in a document. All element and attribute names having this abbreviation as a prefix are part of this namespace. In this context, the term qualified name is defined: A qualified name consists of an (optional) prefix and a local name. In case a prefix exists, it is separated from the local name by a colon. Neither the prefix nor the local name are allowed to contain a colon. The definition is made with an attribute whose name starts with xmlns: and ends with the prefix to be defined. This linking of the prefix to a namespace applies in the element in which it occurs and in all descendants of this element, provided that it is not overwritten:

<hospital:Doctor xmlns:hospital="http://www.xquery-book.de/hospital.xsd">

If in an inner element a new definition for the same prefix is made, this definition applies in this element and its descendants. Not the selected abbreviation, but the corresponding namespace is relevant. Therefore, the following XML fragment is completely equivalent:

<h:Doctor xmlns:h="http://www.xquery-book.de/hospital.xsd">

Unfortunately, DTDs are not equipped for using namespaces, so that a prefix must be selected in a DTD by which the names shall be provided when defining them. A document using another prefix as defined in the DTD, does not validate against this DTD. With XML Schema, we will get to know a schema description possibility treating namespaces in a competent way.

A namespace can also be defined as default (default namespace) and then applies in its scope for all element names having no prefix, but not for attribute names. This is achieved by a xmlns attribute. Therefore, the following fragment is equivalent to the one above: In this example, too, all element names belong to the same namespace.

<Doctor xmlns="http://www.xquery-book.de/hospital.xsd">

The uniqueness of attributes within an element must be maintained by taking into account the namespaces standing behind the abbreviations. As a consequence, all elements with the name "wrong" in the following example document break the conditions of uniqueness for attributes and lead to the fact that the document is not well-formed.

<Example xmlns="http://www.xquery-book.de"
<wrong a="1" a="2"/>
<right a="1" b="2"/>
<right a="1" b1:a="1"/><!-- default namespace does not apply for attributes -->
<wrong b1:a="1" b2:a="2"/>
<right a="1" b3:a="1" b1:a="1"/>


Source: "XQuery – Grundlagen und fortgeschrittene Methoden", dpunkt-Verlag, Heidelberg (2004)

<< backnext >>