- Why Namespaces Are Needed: Resolving Name Conflicts
- Qualified Names, Prefixes, Local Names, and Other Terminology
- Declaring Namespaces in XML Documents
- Default Namespace
- Handling Namespaces in a DTD or XML Schema
- Validating Documents with Namespaces
- What Does a Namespace Point To?
- Namespace Support and Use
- Special Attributes: xmlns, xml:space, xml:lang, and xml:base
- Common Namespaces
- Summary
- For Further Exploration
Default Namespace
What if you have existing documents that haven't used namespaces and then you find you need to reference another vocabulary, but perhaps only within a limited scope of elements? In others words, suppose 90 percent of your elements are from one language and 10 percent are from another. It certainly would be tedious to add prefixes for every element. It would be much more convenient to specify only namespace prefixes for the elements from the additional vocabulary. This is one way in which a default namespace can be helpful. You declare a default namespace with the xmlns attribute as before, but you omit the colon and the prefix:
xmlns="someURI"
For example, this fragment declares XHTML to be the default namespace from an element named html and all its descendants:
<html xmlns="http://www.w3.org/1999/xhtml">
When a default namespace is used, all elements in its scope are considered to be in that namespace without adding a prefix, except for those elements that include a qualified name containing a prefix associated with a different namespace. Consider Listing 5-1, which combines XHTML, XLink, MathML, and SVG (example file amaya-xhtml-svg-mml-xlink.html). The example was initially created using W3C's Amaya 5.0, an editor/browser that supports these four XML vocabularies (to various degrees).6 One way to combine these languages is to use a default namespace for each vocabulary "island." There are four namespace declarations, three of which establish the current default namespace:
<html xmlns="http://www.w3.org/1999/xhtml"> .... <math xmlns="http://www.w3.org/1998/Math/MathML"> .... <span xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://www.w3.org/Graphics/SVG/">SVG</span> <svg xmlns="http://www.w3.org/2000/svg">
The scope of the XHTML default namespace is the entire document, except where that default is overriden by subsequent default namespace (re)declarations. That is, within the <math> element's start tag and within its descendants, the default namespace becomes http://www.w3.org/1998/Math/MathML. After the end tag </math>, XHTML is again the default namespace. The <span> element declares the XLink namespace, which goes out of scope as soon as the end tag </span> is encountered. The <svg> element redefines the default namespace to be SVG, again until it comes to its end tag, and then XHTML regains the default namespace distinction.
Amaya 5.0 handles each XML vocabulary, as you can see in Figure 5-1. (The rectangle appears around the summation symbol [sigma] because I selected the symbol in Amaya before taking the screenshot to illustrate that the math portion wasn't merely a GIF image; the highlighted portion corresponds to the entity reference ∑ defined in MathML.)
FIGURE 5-1 Display of mixed vocabularies in Amaya 5.0
Alternatively, we could attach all of the namespace declarations to the root element, html in this case, so that they are all within scope for the entire document. Of
Listing 5-1 Mixed XHTML, XLink, MathML and SVG with 3 Default Namespaces (amaya-xhtml-svg-mml-xlink.html)
<?xml version="1.0" encoding="iso-8859-1"?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Mixed XHTML, XLink, MathML and SVG</title> <meta name="GENERATOR" content="amaya V5.0" /> </head> <body style="background-color:white"> <h1 style="text-align:center">Mixed XHTML, XLink, MathML and SVG</h1> <h4>List</h4> <ul> <li>one</li> <li>two</li> <li>three</li> </ul> <h4>Switching to MathML</h4> <p>Expression with under- and overscript, <code>munderover</code>: <math xmlns="http://www.w3.org/1998/Math/MathML"> <munderover> <mo>∑</mo> <mrow> <mi>i</mi> <mo>=</mo> <mn>1</mn> </mrow> <mi>n</mi> </munderover> </math> </p> <h4>Back to XHTML</h4> <h4>Switching to <span xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://www.w3.org/Graphics/SVG/">SVG</span></h4> <svg xmlns="http://www.w3.org/2000/svg"> <rect stroke="black" fill="red" x="40px" y="15px" width="300px" height="45px" stroke-width="4"/> <circle stroke="black" fill="blue" cx="180px" cy="40px" r="40px" stroke-width="2" /> </svg> <h4>Back to XHTML</h4> </body> </html>
course, then there can be only one default namespace declaration. Since the XHTML elements are the most prevalent and the document consists of embedded MathML and SVG islands, let's give XHTML that distinction. This means that MathML and SVG now require a prefix both in their declaration and in their use (m:math, m:mo, svg:svg, svg:rect, etc.). See Listing 5-2 (file ns-amaya-xhtml-svg-mml-xlink.html). This change in the way that namespaces are specified has no impact whatsoever on the way Amaya renders the example, nor the link behavior (of the word "SVG" in the phrase "Switching to SVG"). Personally, I prefer this approach to the one in Listing 5-1 because it's less prone to mixing up which default namespace is in effect, and because it is considerably more clear which elements come from which vocabulary. This reasoning becomes much more compelling in a longer, more complex document.
Listing 5-2 Mixed XHTML, XLink, MathML, and SVG with 1 Default Namespace (ns-amaya-xhtml-svg-mml-xlink.html)
<?xml version="1.0" encoding="iso-8859-1"?> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" > <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Mixed XHTML, XLink, MathML and SVG</title> <meta name="GENERATOR" content="amaya V5.0" /> </head> <body style="background-color:white"> <h1 style="text-align:center">Mixed XHTML, XLink, MathML and SVG</h1> <h4>List</h4> <ul> <li>one</li> <li>two</li> <li>three</li> </ul> <h4>Switching to MathML</h4> <p>Expression with under- and overscript, <code>munderover</code>: <m:math> <m:munderover> <m:mo>∑</m:mo> <m:mrow> <m:mi>i</m:mi> <m:mo>=</m:mo> <m:mn>1</m:mn> </m:mrow> <m:mi>n</m:mi> </m:munderover> </m:math> </p> <h4>Back to XHTML</h4> <h4>Switching to <span xlink:type="simple" xlink:href="http://www.w3.org/Graphics/SVG/">SVG</span></h4> <svg:svg> <svg:rect stroke="black" fill="red" x="40px" y="15px" width="300px" height="45px" stroke-width="4"/> <svg:circle stroke="black" fill="blue" cx="180px" cy="40px" r="40px" stroke-width="2" /> </svg:svg> <h4>Back to XHTML</h4> </body> </html>