XML technologies / XML / XML in a Nutshell / Namespaces / Why namespaces are needed
(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)
Some documents combine markup from several XML applications. For instance, a document XHTML may contain images in SVG and equations in MathML. A stylesheet XSLT contains both XSLT instructions and result-tree elements. XLinks always coexist with the surrounding document’s elements because XLink defines only attributes, not elements.
Applications sometimes use the same name for different things. In SVG, the element set sets an attribute’s value for a time interval, whereas in MathML the element set represents a mathematical set, such as all positive even numbers. Distinguishing the MathML element set from the SVG element set is crucial; otherwise validation, rendering, indexing and many other tasks become confused and fail.
This example lists paintings with titles, dates, artists and descriptions:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<catalog>
<painting>
<title>Memory of the Garden at Etten</title>
<artist>Vincent Van Gogh</artist>
<date>November 1888</date>
<description>Zwei Frauen schauen nach links. Eine dritte arbeitet in ihrem Garten.</description>
</painting>
<painting>
<title>The Swing</title>
<artist>Pierre-Auguste Renoir</artist>
<date>1876</date>
<description>Ein junges Mädchen auf einer Schaukel. Zwei Männer und ein kleines Kind schauen zu.</description>
</painting>
<!-- Viele weitere Gemälde... -->
</catalog>Code example: A list of paintings
Suppose this becomes a web page you want search engines to find. You could use the Resource Description Framework (RDF) to embed metadata describing it for search engines and other agents. With the metadata vocabulary Dublin Core, a standard for library-catalog-style information expressible in XML and other syntaxes, its RDF description looks like this:
<RDF>
<Description about="http://www.cafeconleche.org/examples/impressionists.xml">
<title>Impressionistische Gemälde</title>
<creator>Elliotte Rusty Harold</creator>
<description>Eine Liste berühmter impressionistischer Gemälde, geordnet nach Maler und Erstellungsdatum</description>
<date>2000-08-22</date>
</Description>
</RDF>Here the elements Description and RDF come from RDF, while title, creator, description and date come from Dublin Core. Their specifications fix those names; we have no choice if RDF and Dublin-aware software is to understand our documents. The next example combines this description with the paintings list.
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<catalog>
<RDF>
<Description about="http://cafeconleche.org/examples/impressionists.xml">
<title>Impressionistische Gemälde</title>
<creator>Elliotte Rusty Harold</creator>
<description>Eine Liste berühmter impressionistischer Gemälde, geordnet nach Maler und Erstellungsdatum</description>
<date>2000-08-22</date>
</Description>
</RDF>
<painting>
<title>Memory of the Garden at Etten</title>
<artist>Vincent Van Gogh</artist>
<date>November 1888</date>
<description>Zwei Frauen schauen nach links. Eine dritte arbeitet in ihrem Garten.</description>
</painting>
<painting>
<title>The Swing</title>
<artist>Pierre-Auguste Renoir</artist>
<date>1876</date>
<description>Ein junges Mädchen auf einer Schaukel. Zwei Männer und ein kleines Kind schauen zu.</description>
</painting>
<!-- Viele weitere Gemälde... -->
</catalog>Code example: Paintings list with catalog information about the list
Now we have a problem. Some elements have different meanings in different sections. The element title names both the page and a painting. The element date gives both page creation and painting dates. One element description describes pages, the other paintings.
Problems abound: validation is difficult because same-named catalog and Dublin Core elements have different content specifications. A browser may want to hide page descriptions but show painting descriptions, yet not every stylesheet language distinguishes them. A processor may understand the date format in Dublin Core’s element date but not the freer format in the painting element date.
We could rename our elements, using painting_title instead of title, date_painted instead of date, and so on. This is inconvenient with many existing documents. Sometimes it is impossible, particularly with conflicts between standard vocabularies rather than your own and a standard one. For instance, RDF and Dublin Core inevitably conflict over the elements Description and description.
Even without name conflicts, software may need to identify an element or attribute’s application quickly and unambiguously. For instance, an XSLT processor must distinguish XSLT instructions from literal result elements.
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML in a Nutshell” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top