Developer

Use namespaces to differentiate your XML

XML namespaces help avoid the problem of conflicting XML declarations from different organizations. Their usage is simple, but there are a few details to consider.


One of the selling points of XML is the fact that it facilitates the creation of custom vocabularies; that is, an organization may create an XML vocabulary to describe its products. An XML schema or DTD defines the structure, which allows other organizations to use the vocabulary. However, a problem may arise when an organization defines an XML entity that conflicts with another organization. This is where XML namespaces enter the picture.

The XML namespaces recommendation defines a way to distinguish between duplicate element types and attribute names. Such duplication might occur, for example, in a document that contains element types and attributes from two different DTDs.

What is a namespace?
An XML namespace is a collection of the XML vocabulary unique to an organization. It contains element types and attribute names. A unique name, which is its Uniform Resource Indicator (URI), is created for the namespace. This prevents the collision of similar elements from different organizations. Any element type or attribute name in an XML namespace is uniquely identified by a two-part name: the name of its XML namespace and its local name. This two-part naming system is the only thing defined by the XML namespaces recommendation.

Create your own space
Namespaces allow you to uniquely identify and utilize an XML vocabulary. With namespaces, you can:
  • Combine fragments from different documents without any naming conflicts.
  • Write reusable code modules that can be invoked for specific elements and attributes.
  • Define elements and attributes that can be reused in other schemas or instance documents without fear of name collisions.

Using different namespaces
Namespaces are usually set up in the beginning of an XML document, but this isn't a requirement. It does improve readability, but the namespace declaration may be placed anywhere within an XML document. When declaring an XML namespace, use the following syntax:
 
xmlns:prefix=”URI”
 

The prefix points to a URI that contains the namespace definition. This URI is not used during XML document processing; it is referenced only if the XML document is validated for adherence to a schema or DTD. Here is the XML namespace declaration that takes advantage of the XHTML 1.0 standard:
 
xmlns:html=”http://www.w3.org/1999/xhtml”
 

The following is a basic XML document that takes advantage of this namespace:
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<html xmlns:html=”http://www.w3.org/1999/xhtml”>
<html:head>
<html:title>
XML Namespace example
</html:title>
</html:head>
<html:body>
<books>
<book type=”hardback”>
<title>Atlas Shrugged</title>
<author>Ayn Rand</author>
<isbn>0525934189</isbn>
</book>
<book type=”paperback”>
<title>A Burnt-Out Case</title>
<author>Graham Greene</author>
<isbn>0140185399</isbn>
</book>
</books>
</html:body>
</html>

 

The listing takes advantage of the industry standard HTML namespace. It assigns the namespace to individual elements by prepending entity names with the assigned namespace name. The entities without the associated namespace utilize the default namespace, which is nothing (null). The default namespace may be overridden in a declaration. The default namespace is defined with no associated namespace name. The following listing overrides the default namespace with a fictitious company XML namespace:
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<html xmlns:html=http://www.w3.org/1999/xhtml
xmlns:sample=”http://www.baselineinc.com/bookdefinition”>
<html:head>
<html:title>
XML Namespace example
</html:title>
</html:head>
<html:body>
<sample:books>
<sample:book type=”hardback”>
<sample:title>Atlas Shrugged</sample:title>
<sample:author>Ayn Rand</sample:author>
<sample:isbn>0525934189</sample:isbn>
</sample:book>
<sample:book type=”paperback”>
<sample:title>A Burnt-Out Case</sample:title>
<sample:author>Graham Greene</sample:author>
<sample:isbn>0140185399</sample:isbn>
</sample:book>
</sample:books>
</html:body>
</html>

 

This example allows you to utilize two different namespaces within the same XML document. This allows the same entity name to be used in different namespaces without a problem. The default namespace may be undeclared within the XML document by using an empty declaration, like this:
 
<entity_name xmlns=””>
 

Unfortunately, namespaces other than the default cannot be undeclared. The namespace declaration stays with the associated entity until it ends (closing tag).

Namespace URI
The URI associated with a namespace declaration doesn't have to point to an actual document or page, nor does it have to utilize HTTP as my examples do. Many known URIs don't return anything when plugged in to a browser. XML parsers compare namespace URIs on a character-by-character basis, so the name must be unique and consistent in its usage.

Editor's Picks

Free Newsletters, In your Inbox