What Is a Namespace, and Why Do You Need Them?

XML is useful for exchange of documents. A finite number of element type names is available to use to contain the document content. In a global Web, how do we handle the possibility of two element type names being the same? The solution that the W3C chose is namespaces.

Note

A namespace is a collection of names. In XML, a namespace refers to a collection of element type names and attribute names.



So, exactly why do we need namespaces? You might have document fragments such as the following:

<html> 
<head> 
<title>My XHTML document</title> 
</head> 
<body>Some content.</body> 
</html> 

and

<persons> 
<person> 
<title>President</title> 
<firstName>George</firstName> 
<lastName>Bush</lastName> 
</person> 
</persons> 

Even with simple documents that mix these two XML structures, there is a problem. In this example, how do you distinguish unambiguously the title element that belongs to XHTML from the title element in the persons data store? When you mix longer documents, possibly using more than two vocabularies, the potential for problems is greater.

Three important problems almost inevitably might arise:

  • It is difficult to recognize the application to be used for processing particular elements.

  • Elements with the same element type name are used for different real-world meanings.

  • Different element type names are used by different users or groups of users to represent the same real-world notion.

XML namespaces were designed to overcome the first two problems. A solution to the third problem using XSLT is described in Chapter 11, “XSLT—Transforming XML Structure.”

Recognizing Which Application to Use

In the preceding code, how do you signal that a Web browser should process the title element from the XHTML code snippet and that another application should process the title element from the persons data store?

The first code snippet could be nested within the second, or vice versa. So, you need a mechanism that solves the problem, regardless of which essentially infinite number of structures is used. You need to be able to identify the individual elements as belonging to a particular namespace.

Element Type Name Clashes

Element type names clash when two or more XML document authors use the same element type name to represent different real-world ideas or values.

Because the community using XML is already large and will grow further, the possibility that element type names will clash in shared documents becomes very real. The likelihood of element name clashes also increases as more XML documents are shared outside defined groups.

Consider the possibility of sharing information from several sources, each of which uses an order element:

<order> 
 <orderNumber>AB123</orderNumber> 
 <Items> 
  <Item number="20">Ink cartridges</Item> 
  <Item number="2">3.5" Floppy Disks (Box of 10)</Item> 
 </Items> 
</order> 

The order element has a different meaning in the following code:

<order> 
 <givenBy>General B. Smart</givenBy> 
 <Content>Relocate 6 jet fighters to MacDill Air Force 
  Base</Content> 
 </order> 

It is different again in the following code:

<order> 
 <Location>Seattle</Location> 
 <Description>A riot lasted several hours following clashes 
  with anti-globalization protesters. 
</order> 

Note

These examples illustrate the problem, but more subtle issues also arise. Suppose that several companies each use an order element to refer to the placing of an order for goods and services. Those companies are not obligated to use an order element with the same structure of child and descendant elements nested within it.



If these XML documents remained within commercial, military, or police organizations, possibly no confusion would arise. However, suppose that the police or the Air Force ordered a number of items from the supplier that uses the order element in the first of the three ways shown. How is the reader—and, more importantly, an XML processor—to know which order element is intended in any particular context?

Clearly, a mechanism is needed to distinguish a commercial order from a military order or an order recorded by a police department. You need to be able to express in XML similar distinctions.

In XML, elements are distinguished by using namespaces.

Note

A namespace is simply a collection of names. In XML 1.0, a namespace doesn’t imply any particular document structure. It simply expresses the notion that these elements belong together.



A concept similar to XML namespaces exists in some programming languages. For example, in Java, a particular class must have a unique name, to avoid ambiguity. In Java, a package provides a broad equivalent to the concept of XML namespaces. A class must have a name that is unique within a package. Classes in other packages might have the same class name, but because they are in a different package, there is no risk of confusion as the code is processed.

As in Java, each XML element in an XML namespace must have a unique name; otherwise, confusion can arise.

Let’s move on to examine exactly how XML distinguishes namespaces.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset