Namespaces

(Excerpt from "The MathML Handbook" by Pavi Sandhu)

It is often necessary to combine markup from two different XML applications in a single document. If the same element name is used in both XML applications, the interpretation of that element name becomes ambiguous.

For example, both MathML and SVG define an element called set. If you include a MathML equation and an SVG graphic in an XHTML document, an XML processor reading that document has no way of knowing whether a given set element is a MathML element or an SVG element. This can lead to problems in validating the document and interpreting its meaning.

XML uses the concept of namespaces to distinguish elements with the same name belonging to different XML formats. Each namespace associates a collection of element and attribute names with a specific URL. So, for example, all MathML elements are placed in the MathML namespace, and all SVG elements in the SVG namespace. Since each URL is a unique string, two elements that have the same name but belong to different XML applications can always be distinguished.

There are two ways to specify the namespace for a particular element. The first is to specify the namespace explicitly on each element, using a namespace prefix. To do this, you use an attribute declaration of the form xmlns:prefix-name="url" to associate a prefix name with a specific namespace URL. This prefix declaration must occur either on or before the outermost element belonging to that namespace. You then replace the name of each element belonging to that namespace by a qualified name of the form, prefix-name:element-name. Here is an example:

<m:math xmlns:m="http://www.w3.org/1998/Math/MathML">
  <m:set>
    <m:ci>b</m:ci>
    <m:ci>a</m:ci>
  </m:set>
</m:math>

The URL "http://www.w3.org/1998/Math/MathML" is a unique identifier for the MathML namespace. If you associate the prefix m with this URL using the xmlns:m attribute, all element and attribute names of the form m:name are interpreted as names defined by MathML.

The second way is to specify a default namespace using an xmlns attribute. This provides an alternative to using a namespace prefix for each element. For example:

<math xmlns="http://www.w3.org/1998/Math/MathML">
  <set>
    <ci>b</ci>
    <ci>a</ci>
  </set>
</math>

Here, the math element contains an xmlns attribute whose value is set to the URL that defines the MathML namespace. By default, all element names that appear inside the math element (such as set and ci) are then assumed to lie within the namespace defined by that URL. This distinguishes them from any other set elements in the document belonging to another XML application.

   

<< back next >>

 

 

 


 

Copyright © CHARLES RIVER MEDIA, INC., Massachusetts (USA) 2003
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "The MathML Handbook" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.


CHARLES RIVER MEDIA, INC., 20 Downer Avenue, Suite 3, Hingham, Massachusetts 02043, United States of America