XML Namespaces


In an earlier discussion, a conflict between a name tag in a user document and the name attribute in an XSL stylesheet was noted.  The work-around there, was to make the user tag unique by using name_, instead.

This problem is an example of a namespace conflict:  the same name (in this case,  name) being used in two different ways within the same context. For proper processing of XML documents, tags must be unique.  To insure this, tags can be associated with a unique namespace identifier, usually just called a namespace.  Then, it is sufficient to just insure that individual names are unique relative to the set of names associated with that namespace.  

To insure that a namespace identifier is unique, they are very frequently associated with a URL.  One can simply construct a string that conforms to the syntax of a URL and use that string, or, if one really wants to insure uniqueness, that URL (or the host portion of it) can be registered with a domain name service, just as any other domain name can.  However, a critical and often confusing point is that what really matters here is that the namespace identifier (URL) is a unique sting of characters; it is not associated with any address or information accessible on the Web.

In practice, when a namespace is defined, it is usually associated with a prefix.  Thereafter, one can indicate that a given tag element is associated with a particular namespace by preceding it with the associated prefix.  Consider the following XML sample:

<?xml version="1.0"?>
   <entry xmlns:jbs="http://jbs.cs.unc.edu/2002/Java_XML">
       <jbs:name>
         . . . . 
       </jbs:name>
    </entry>

Within the scope of the entry element, a namespace, http://jbs.cs.unc.edu/2002/Java_XML, is defined.  It is a valid URL, as indicated by the fact that browsers represent it as a link, but it does not refer to any valid Web page.  It is associated with the prefix, jbs.  Thereafter, the name tag is defined to be part of this namespace by appending the jbs prefix, producing jbs:name.  This tag is no longer ambiguous and, thus, will not be confused with the name tag that is part of the XSL namespace.

A good way to think about namespaces is as analogous to the names of Java packages.  The name of an individual class is often not globally unique, but when the fully qualified package name is appended to the front, it does become globally unique.  For example, Document is both an Interface in the javax.swing.text package and a class in the org.jdom package.  But there is no ambiguity with respect to javax.swing.text.Document and org.jdom.Document.

Below, two brief, but complete, examples are shown: one without a namespace, the other with.

An excellent discussion of XML namespaces is Aaron Skonnard's Understanding XML Namespaces.  For the definitive description, the the W3C's Namespaces in XML.


Example XML Documents:  without Namespace

<?xml version="1.0"?>

	<entry>

		<name_>
			<name_first>John </name_first>
			<name_middle>B. </name_middle>
			<name_last>Smith </name_last>
		</name_>

		<address_>
			<address_1>100 Evans Ct.</address_1>
			<city>Carrboro</city>
			<state>NC</state>
			<zip>27510</zip>
		</address_>

	</entry>

Example XML Documents:  with Namespace

<?xml version="1.0"?>

	<entry  xmlns:jbs="http://jbs.cs.unc.edu/2002/Java_XML">

		<jbs:name>
			<jbs:name_first>John </jbs:name_first>
			<jbs:name_middle>B. </jbs:name_middle>
			<jbs:name_last>Smith </jbs:name_last>
		</jbs:name>

		<jbs:address>
			<jbs:address_1>100 Evans Ct.</jbs:address_1>
			<jbs:city>Carrboro</jbs:city>
			<jbs:state>NC</jbs:state>
			<jbs:zip>27510</jbs:zip>
		</jbs:address>

	</entry>