| Previous | Next
NamespacesContents:The Need for Namespaces
Namespaces have two purposes in XML:
The first purpose is easier to explain and to grasp, but the second purpose is more important in practice. Namespaces are implemented by attaching a prefix to each element and attribute. Each prefix is mapped to a URI by an The Need for NamespacesSome documents combine markup from multiple XML applications. For example, an XHTML document may contain both SVG pictures and MathML equations. An XSLT stylesheet will contain both XSLT instructions and elements from the result-tree vocabulary. And XLinks are always symbiotic with the elements of the document in which they appear since XLink itself doesn't define any elements, only attributes. In some cases, these applications may use the same name to refer to different things. For example, in SVG a Consider Example 4-1. This is a simple list of paintings including the title of each painting, the date each was painted, the artist who painted it, and a description of the painting. Example 4-1. A list of paintings<?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> Two women look to the left. A third works in her garden. </description> </painting> <painting> <title>The Swing</title> <artist>Pierre-Auguste Renoir</artist> <date>1876</date> <description> A young girl on a swing. Two men and a toddler watch. </description> </painting> <!-- Many more paintings... --> </catalog> Now suppose that Example 4-1 is to be served as a web page and you want to make it accessible to search engines. One possibility is to use the Resource Description Framework (RDF) to embed metadata in the page. This describes the page for any search engines or other robots that might come along. Using the Dublin Core metadata vocabulary (http://purl.oclc.org/dc/), a standard vocabulary for library-catalog-style information that can be encoded in XML or other syntaxes, an RDF description of this page might look something like this: <RDF> <Description about="http://www.cafeconleche.org/examples/impressionists.xml"> <title> Impressionist Paintings </title> <creator> Elliotte Rusty Harold </creator> <description> A list of famous impressionist paiintings organized by painter and date </description> <date>2000-08-22</date> </Description> </RDF> Here we've used the Example 4-2. A list of paintings including catalog information about the list<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> <catalog> <RDF> <Description about="http://www.cafeconleche.org/examples/impressionists.xml"> <title> Impressionist Paintings </title> <creator> Elliotte Rusty Harold </creator> <description> A list of famous impressionist paintings organized by painter and date </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> Two women look to the left. A third works in her garden. </description> </painting> <painting> <title>The Swing</title> <artist>Pierre-Auguste Renoir</artist> <date>1876</date> <description> A young girl on a swing. Two men and a toddler watch. </description> </painting> <!-- Many more paintings... --> </catalog> Now we have a problem. Several elements have been overloaded with different meanings in different parts of the document. The This presents all sorts of problems. Validation is difficult because catalog and Dublin Core elements with the same name have different content specifications. Web browsers may want to hide the page description while showing the painting description, but not all stylesheet languages can tell the difference between the two. Processing software may understand the date format used in the Dublin Core We could change the names of the elements from our vocabulary, In other cases, there may not be any name conflicts, but it may still be important for software to determine quickly and decisively to which XML application a given element or attribute belongs. For instance, an XSLT processor needs to distinguish between XSLT instructions and literal result-tree elements. |