XML Namespaces and Prefixes

Within XML documents, it sometimes becomes necessary to differentiate one element from another based on the context in which these elements are used.

For example, the <table> element is used differently between XML and HTML. Within an XML document in which both XML and HTML <table> elements are present, namespaces and prefixes can be used to differentiate XML table elements from HTML table elements.

Namespaces identify the context in which a given element is used, thereby differentiating that element from otherwise-identical elements.

Theoretically, any label can be used as a namespace, but XML namespaces should be URIs. Ideally, an XML namespace is an HTTP-capable URI that dereferences to a web-accessible description of the context identified by the namespace. Consequently, most XML namespaces in common use look like standard web addresses.

Going back to the above example, a namespace identifying HTML5 as the context for a given <table> element might appear as follows:

http://www.w3.org/TR/html5/

When entered into a web browser, this namespace dereferences to a description of HTML5.

Namespace Declaration

Before a namespace can be used to differentiate a given element, the namespace must first be declared. This is accomplished by means of the XML namespace attribute (xmlns). Continuing the example above:

<table xmlns="http://www.w3.org/TR/html5/" />

This declaration indicates that the table element is being used within the context of HTML5.

Because it can be tedious to make declarations on a per-element basis throughout long documents, prefixes are often used instead.

XML Prefixes

A prefix is a short text string that invokes a namespace, acting as a proxy for the full namespace. Prefixes can be used to differentiate tags, attributes, or even element content, like so:

<prefix:tag prefix:attribute="value">prefix:content</prefix:tag>

In order to be invoked by a prefix, a declared namespace must be bound to a prefix. This binding can take place either in the element in which the namespace is invoked; or within an element that acts as a parent to the element in which the namespace is invoked.

For example, assume that a table element has been nested within a body element (which acts as a parent element to the table element), like so:

<body>
<table>content</table>
</body>

If we wish to use a prefix to indicate that the table element is being used in the context of the html5 prefix, the html5 prefix must first be declared and bound to the namespace using the xmlns attribute, like so:

<tag xmlns:html5="http://www.w3.org/TR/html5/" />

This must be done either in the table element or any element that acts as a parent to the table element, such as the body element. Because binding the html5 prefix to the table element within the table element would be redundant under most circumstances, it is more efficient to bind the html5 prefix to the declared namespace within a parent element, such as the body element, like so:

<body xmlns:html5="http://www.w3.org/TR/html5/">
<html5:table>content</html5:table>
</body>

Having been bound to a declared namespace within the body element, the prefix can thereafter be used on any element within the body element; this includes the table element.

Typically, namespaces are declared and bound to prefixes within the root element of a given document, and invoked via prefix where necessary in nested elements.

Practical Example

To give a more concrete example of the usage of XML prefixes:

<wfs:WFS_Capabilities xmlns:wfs="http://www.opengis.net/wfs" xmlns:ows="http://www.opengis.net/ows">

This is WFS_Capabilities element (<WFS_Capabilities>) is the root element  of the capabilities document that describes a National Geothermal Data System web feature service. Here, two namespaces are declared and bound to two different prefixes via the xmlns attribute:

  • The wfs prefix is bound to the following namespace: http://www.opengis.net/wfs
  • The ows prefix is bound to the following namespace: http://www.opengis.net/ows

Later on in the document, the following elements occur:

<ows:Abstract>Well header information for oil and gas wells in Arizona.</ows:Abstract>

<wfs:Name>aasg:Wellheader</wfs:Name>

These prefixes indicate that the Abstract element is used in the context of an Open Geospatial Consortium OWS Common Implemetation Specification XML schema; likewise, the Name element is used in the context of an Open Geospatial Consortium web feature service. Information about the context of these elements can be found at the web location indicated by the namespaces to which the prefixes have been bound.

To continue the tutorial, click the links below.