The Problem Namespaces Solves
XML is extensible. So it says in the name: eXtensible Markup Language.
The problem is that extensibility does not come free. In a distributed envi-
ronment, extensibility must be managed to avoid conflicts. Namespaces is a
solution to help manage XML extensibility.
Namespace can be defined as a mechanism to identify of XML elements. It
places the name of the elements in a more global context: the namespace.
The namespace recommendation, published by the W3C, is available at
www.w3.org/TR/REC-xml-names.
The namespace recommendation is relatively thin. The concepts are not dif-
ficult, either. Unfortunately, this means that namespaces are often over-
looked! Don’t make that mistake; namespaces are essential for many XML
applications.
Let’s suppose you decide to publish your bookmarks. (There is heavy compe-
tition from Yahoo! but let’s ignore this for a moment.) Listing 4.1 shows
what it might look like in XML. As a standalone document, Listing 4.1
works perfectly.
Listing 4.1: A List of Resources in XML
<?xml version=”1.0”?>
<references>
<name>Macmillan</name>
<link href=”http://www.mcp.com”/>
<name>Pineapplesoft Link</name>
<link href=”http://www.pineapplesoft.com/newsletter”/>
<name>XML.com</name>
<link href=”http://www.xml.com”/>
<name>Comics.com</name>
<link href=”http://www.comics.com”/>
<name>Fatbrain.com</name>
<link href=”http://www.fatbrain.com”/>
<name>ABC News</name>
<link href=”http://www.abcnews.com”/>
</references>
Chapter 5, “XSL Transformation,” page 125 and Chapter 6, “XSL Formatting Objects and
Cascading Style Sheet,” page 161 will show you how to publish this document on a Web site.
108
Chapter 4: Namespaces
EXAMPLE
In practice, however, documents are seldom standalone. In a collaborative
environment like the Web, people build on one another’s work. Somebody
might take your list and rate it. The result would be Listing 4.2 (admit-
tedly, I’m biased).
Listing 4.2: The References with Quality Ratings
<?xml version=”1.0”?>
<references>
<name>Macmillan</name>
<link href=”http://www.mcp.com”/>
<rating>5 stars</rating>
<name>Pineapplesoft Link</name>
<link href=”http://www.pineapplesoft.com/newsletter”/>
<rating>5 stars</rating>
<name>XML.com</name>
<link href=”http://www.xml.com”/>
<rating>4 stars</rating>
<name>Comics.com</name>
<link href=”http://www.comics.com”/>
<rating>5 stars</rating>
<name>Fatbrain.com</name>
<link href=”http://www.fatbrain.com”/>
<rating>4 stars</rating>
<name>ABC News</name>
<link href=”http://www.abcnews.com”/>
<rating>3 stars</rating>
</references>
Listing 4.2 is the same document with one new element: rating. As we saw
in the last chapter, it is often desirable to extend an existing document to
convey new information instead of designing new schemas from scratch.
Problems occur, however, if the extension is not managed. Suppose some-
body else decides to rate the list with parental advisory. Listing 4.3 shows
the result (ABC News might report on violence, hence its PG rating).
Listing 4.3: Another Meaning for Rating
<?xml version=”1.0”?>
<references>
<name>Macmillan</name>
<link href=”http://www.mcp.com”/>
<rating>G</rating>
109
The Problem Namespaces Solves
EXAMPLE
EXAMPLE
continues
Listing 4.3: continued
<name>Pineapplesoft Link</name>
<link href=”http://www.pineapplesoft.com/newsletter”/>
<rating>G</rating>
<name>XML.com</name>
<link href=”http://www.xml.com”/>
<rating>G</rating>
<name>Comics.com</name>
<link href=”http://www.comics.com”/>
<rating>G</rating>
<name>Fatbrain.com</name>
<link href=”http://www.fatbrain.com”/>
<rating>G</rating>
<name>ABC News</name>
<link href=”http://www.abcnews.com”/>
<rating>PG</rating>
</references>
This is problematic. Listing 4.3 also is an extension to Listing 4.1 but the
extension creates incompatibilities between Listing 4.2 and Listing 4.3.
This is a very common problem: Two people extend the same document in
incompatible ways.
Things get really out of hand when trying to combine both ratings in a list-
ing. The result would look like Listing 4.4 where the two ratings conflict
with each other.
Listing 4.4: The Combined Listing
<?xml version=”1.0”?>
<references>
<name>Macmillan</name>
<link href=”http://www.mcp.com”/>
<rating>5 stars</rating>
<rating>G</rating>
<name>Pineapplesoft Link</name>
<link href=”http://www.pineapplesoft.com/newsletter”/>
<rating>5 stars</rating>
<rating>G</rating>
<name>XML.com</name>
<link href=”http://www.xml.com”/>
<rating>4 stars</rating>
110
Chapter 4: Namespaces
EXAMPLE
<rating>G</rating>
<name>Comics.com</name>
<link href=”http://www.comics.com”/>
<rating>5 stars</rating>
<rating>G</rating>
<name>Fatbrain.com</name>
<link href=”http://www.fatbrain.com”/>
<rating>4 stars</rating>
<rating>G</rating>
<name>ABC News</name>
<link href=”http://www.abcnews.com”/>
<rating>3 stars</rating>
<rating>PG</rating>
</references>
The problem with Listing 4.4 is that software designed to operate with
Listing 4.3 and filter offensive links would be completely lost. It wouldn’t
know what to do with the “4 stars” rating. The software should simply
ignore quality rating tags but how can it ignore quality rating tags if it can-
not differentiate between the two
rating tags?
The solution is obvious: Use different names for the two ratings. Listing 4.5
renames the “quality” element as qa-rating and the “parental” element as
pa-rating.
Listing 4.5: Using Different Names
<?xml version=”1.0”?>
<references>
<name>Macmillan</name>
<link href=”http://www.mcp.com”/>
<qa-rating>5 stars</qa-rating>
<pa-rating>G</pa-rating>
<name>Pineapplesoft Link</name>
<link href=”http://www.pineapplesoft.com/newsletter”/>
<qa-rating>5 stars</qa-rating>
<pa-rating>G</pa-rating>
<name>XML.com</name>
<link href=”http://www.xml.com”/>
<qa-rating>4 stars</qa-rating>
<pa-rating>G</pa-rating>
<name>Comics.com</name>
<link href=”http://www.comics.com”/>
111
The Problem Namespaces Solves
EXAMPLE
continues

Get XML by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.