Namespaces in XML

Declaring Namespaces  |  Applying Namespaces 


In its effort to enhance the functionality and interoperability of the Web, the World Wide Web Consortium (W3C) published the Namespaces in XML 1.0 recommendation in January of 1999.  The recommendation defines namespaces as a collection of names with internal structure, identified by a Uniform Resource Identifier (URI) reference, used in XML documents as element type and attribute names.  Essentially, the purpose is to create universally unique names, allowing formation of new valid documents consisting of pieces from other documents which duplicate names.  The URIs do not need to represent actual resources; while any unique text string can serve as a namespace identifier, URIs, based on proprietary domain names, have the advantage of assuring that the identifier will be unique on the WWW.  Future versions of the Namespaces recommendation may support another URI identifier syntax, Uniform Resource Name (URN).  This reading discusses the 1.0 recommendation.

Namespaces are used to uniquely identify elements with the same name type when they are combined in a single document.   The W3C recommendations envision applications of XML where a single XML document may contain elements and attributes that are defined for and used by multiple software modules.  Documents combining multiple markup vocabularies pose processor validation problems of recognition and name collision (markups using the same element type or attribute name).  The "name collisions" are a problem when validating documents.  This problem is overcome when document constructs have universally unique names, beyond the scope of the containing document.  The XML namespaces specification describes a mechanism to accomplish this.

Separator line

Declaring Namespaces in XML

A namespace in XML is a declaration of a defined collection of unique elements and attributes.  Names in the same namespace must be unique.  XML namespaces only apply to element types and attribute names.  Any element type or attribute name in an XML namespace is uniquely identified by a two-part name: the name of its XML namespace (typically a URI) and its local name.  The URI must be under the namespace author's control so it will not be used to identify a different XML namespace.

Designing a namespace URI requires adding a unique descriptor path to your domain name.  The requirements are that it is unique, in the form of a Uniform Resource locator (URL), and persistent.  It is recommended that the URI not point to a specific file, although it may point to a DTD or schema.  The following example uses the code "ns" for a fictictious directory (ns=namespace), followed by a ficticious descriptor directory.


Namespaces, as used in element and attribute naming, must be XML qualified names.  URI references contain characters not allowed in XML names.  Therefore they cannot be directly used as part of an element or attribute name.  Namespace prefixes are used as URI proxy references, with a single colon separating the namespace prefix and the local element or attribute name.   When an element type name or attribute name contains a colon, the portion of the name preceding the colon, the prefix, is treated as a namespace alias. 

Prefixes beginning with the three-letter sequence x, m, l, in any case combination, are reserved for use by XML and XML-related specifications.  By definition, the prefix xml is bound to the http://www.w3.org/XML/1998/namespace URI.  The prefix xmlns is not bound to a namespace URI.  The namespace prefix must be declared in the document prolog or in either the element start-tag where the prefix is used or in an ancestor element tag.  A prefix can be used in the element a namespace declaration associates it with, and in any child elements.

The prolog namespace declaration syntax and an example follow.

<?xml:namespace ns="URI" prefix="prefix"?>

<?xml:namespace ns="http://www.jqjacobs.net/ns/photos/" prefix="photos"?>

A common method of declaring a namespace is the addition of the declaration to an element, using the same syntax as attribute declarations.  The element namespace declaration syntax and an example using the prefix "photos" and the element "place" follow.

<prefix:element xmlns:prefix="URI">

<photos:place xmlns:photos="http://www.jqjacobs.net/ns/photos/">

Omitting the prefix in the namespace declaration creates a default namespace, thereby eliminating the need to use namespace prefixes.  A default namespace applies to all child elements without a prefix.  Declaring a default namespace for a document's root element places all the document elements in a namespace.  The following syntax and example declare a default namespace.

<element xmlns="URI">

<place xmlns="http://www.jqjacobs.net/ns/photos/">

The determination of which elements and attributes belong to a namespace is termed the scope of the namespace.  Document hierarchy impacts scope.  A namespace declared for an element cannot be applied to the element's parent or sibling elements.  Declaring a default namespace applies the namespace to all child elements.  When a default namespace is declared for a child element of a parent element with a default namespace, the child's default namespace takes precedence over the parent's.  In a prefixed element, child elements without the prefix are not in the namespace.  However, such a child element may be in a default namespace declared for the parent, the root, or another element in the ascending hierarchy.  When an empty URI reference is declared in a default namespace, the unprefixed elements in the scope of the declaration are not considered to be in any namespace, having the same effect as no default namespace.  Also, default namespaces do not apply directly to attributes. 

In the following code sample, the declaration places all elements in the HTML namespace by default.

<html xmlns="http://www.w3.org/TR/REC-html40">

Separator line

Applying Namespaces to Elements and Attributes

As many prefixed namespaces as necessary may be declared.  Multiple namespace prefixes can be declared for a single element.  Once a namespace and its prefix are declared, adding the prefix label to elements and attributes qualifies the entity for the namespace.  Prefixed names are termed qualified names, while an entity name without a namespace prefix is an unqualified name.  

A prefixed element name must contain the prefix in the end tag.  The syntax to qualify an element follows.

<prefix:element> </prefix:element>

Adding a namespace prefix to an attribute name qualifies the attribute.  Attributes rarely need to be qualified with a namespace.  Identically named attributes, due to their asssociation with different elements, do not overlap like identically named elements.  Namespaces are added to attributes when an element uses two attributes with the same name.  Elements cannot contain two attributes with the same name or two qualified attribute names with the same local part pointing to identical namespaces.

Separator line

Namespaces and Document Validation

XML parsers consider the namespace prefix, the colon, and the element or attribute name as a single entity.  Therefore, namespace prefixes must be declared in the document type definition (DTD).  The syntax follows.

<!ELEMENT prefix:element content-model>

<!ATTLIST' prefix:element prefix:attribute type default>

Separator line

References and Advanced Readings


Web Design

2004 by James Q. Jacobs. All rights reserved.