Namespaces represent a critical concept in terms of understanding XML and working with it as a data source. As you have already seen, the data (content) of an XML document is retrieved by examining individual nodes within the document. This works because the hierarchical structure of XML documents and the rules of validity and well-formedness that govern XML document creation ensure that each node in a document is unique. This, in turn, guarantees that only one reference exists for any one node. The potential for problems arises, however, when you use XML documents in a collaborative environment. For example, two (or more) documents might contain elements with the same names but different semantics. (And the documents might even be structured in the same way.) If it were necessary to use both documents in a single environment, there would be confusion about the overlapping elements. Consider the following XML code:
<AUTOMOBILE> <ID>232-HDF</ID> </AUTOMOBILE> <DOG> <ID>Rover</ID> </DOG> |
Here the Automobile element and the Dog element each contain an Id element, but the Id element has a different meaning in each case. If these elements came from different sources but were combined into a single document, as shown in the following code, the Id elements would lose their meaning.
<FAMILY> <MOM> |
This is a very real problem and will become pervasive as XML is used more widely on the Web and in organizations. The solution is XML namespaces, which offer a way to create names that remain unique no matter where the elements are used.
The term namespace in traditional programming parlance means a set of names in which no duplicates exist. Because the nature of XML allows authors to define their own tag sets, which could likely result in duplicate names among XML documents, the term namespaces in XML has some additional connotations. Namespaces in XML is a methodology for creating universally unique names in an XML document by identifying element names with a unique external resource. A namespace in XML is therefore a collection of names that is identified by a Uniform Resource Identifier (URI). A namespace can be qualified or unqualified.
A qualified name in XML is composed of two parts: the namespace name and the local part. The namespace name, which is a URI, selects the namespace. The local part is the local document element or attribute name. Because a URI is always unique, the namespace name combined with the local part creates a universally unique element name. To be able to use a namespace in an XML document, you include a namespace declaration in the prolog of the document. A namespace prefix can also be included in the declaration. You can then attach the prefix with a colon to the local part to associate the local part with the namespace name. Let's look at how this works. In the following document, we'll declare two namespaces with prefixes and then use those namespaces in the document.
<?xml version="1.0"?> <?xml:namespace ns="http://inventory/schema/ns" prefix="inv"?> <?xml:namespace ns="http://wildflowers/schema/ns" prefix="wf"?> <PRODUCT> <PNAME>Test1</PNAME> <inv:quantity>1</inv:quantity> <wf:price>323</wf:price> <DATE>6/1</DATE> </PRODUCT> |
In the above code, the prefixes are used to identify elements that are part of the selected namespace. The result is not only unique names, but the preservation of the semantic value of the names as well. The inv:quantity and wf:price elements now contain fully qualified names that will be unique no matter where they are used. The prefix is part of the element name and must always be included to indicate that the element belongs to the namespace.
NOTE
All namespaces in this chapter, with the exception of the datatypes namespace covered earlier, are examples only. The datatypes namespace really exists and can be used as described in the section "Specifying Data Types"
An unqualified name does not have an associated namespace name. Typical XML element names are unqualified since they do not specify a namespace. For example, all element names in the following XML code are unqualified and would not be universally unique.
<PRODUCT> <NAME>Bloodroot</TITLE> <QUANTITY>10</QUANTITY> </PRODUCT> |
The prolog is not the only option for the location of the namespace declaration. Instead, you can include the namespace declaration directly within an element that is part of the namespace. Simply include the declaration the first time the element is used, as shown here.
<PRODUCT> <PNAME>Test1</PNAME> <inv:quantity>1</inv:quantity> <wf:price xmlns:wf="urn:schemas-wildflowers-com:xml-prices"> 323 </wf:price> <DATE>6/1</DATE> </PRODUCT> |
The namespace is then available within the context of that element. In other words, that element and all its children can use the namespace. If the namespace is declared in the document element, that namespace can be used throughout the entire document.
You can make a namespace the default by declaring it without assigning a prefix. In this case, the namespace is assumed within the context of the element in which it was declared, as shown in the following code:
<CATALOG> <INDEX> <ITEM>Trees</ITEM> <ITEM>Wildflowers</ITEM> |
The Product element in the above code contains a namespace declaration without an associated prefix. As such, the namespace is assumed for the Product element and all of its child elements but not for any elements outside the Product element. The only exception occurs when a child element contains another namespace declaration, which overrides the default namespace declaration. The following code shows an example:
<CATALOG> <INDEX> <ITEM>Trees</ITEM> <ITEM>Wildflowers</ITEM> |
Here the default namespace is overridden for the inv:quantity element only.
Because all URLs are unique, they can be used to provide uniqueness to namespace names. If a namespace is mapped to a URL, that namespace is unique in the entire context of where the namespace is used.
Another scenario might be where a namespace schema exists that identifies all the names in the namespace and how they should be structured. While it is not the goal of the XML namespace name to provide a mechanism for retrieving that schema, such a method could exist using Uniform Resource Names (URNs).
NOTE
A schema is a document definition, similar to a DTD but using a special XML vocabulary named XML-Data. Schemas and XML-Data are covered in Chapter 10 . With regard to namespaces, a schema can be used to define all the names contained in the namespace. For example, a schema might define dt as the prefix that is mapped to the namespace name and string as a name within the namespace. In this case, the element name dt:string could be used in the XML document.
A URN can provide a mechanism for locating and retrieving a schema file that defines a particular namespace. While similar functionality could be provided by an ordinary URL, a URN is more robust and easier to manage for this purpose because a URN can refer to more than one URL.
NOTE
For more information about URNs, visit http://www.ncsa.uiuc.edu/InformationServers/Horizon/URN/urn.html.
The following code shows how a URN might be used in the context of an XML namespace:
<CATALOG> <INDEX> <ITEM>Trees</ITEM> <ITEM>Wildflowers</ITEM> |
Here a schema for the namespace might be found at the location identified by the URN, and the processing application would know how to retrieve that schema. The schema would detail the elements of the namespace that could be used in the document.
Namespaces can apply to attributes as well as elements, and to make it easy, the naming mechanism is the same for both, as shown in the following example.
<wf:product TYPE="plant" class:kingdom="plantae" xmlns:wf="urn:wildflowers:schemas:product" xmlns:class="urn:bio:botany:classification"> <PNAME>Test1</PNAME> <QUANTITY>1</QUANTITY> <PRICE>323</PRICE> <DATE>6/1</DATE> </wf:product> |
In this example, both the wf:product element and the class:kingdom attribute have associated namespace declarations. The attribute namespace is used in a similar fashion to the element namespace.
Namespaces will become increasingly important as new vocabularies and XML-based technologies develop. There are already technologies that rely on namespaces, such as XML-Data, XML data types, and SMIL (Synchronized Multimedia Integration Language). Chapter 10 includes practical examples of how XML namespaces are used by XML-Data.