What's a namespace?

Namespaces were designed to solve a problem that arises when using multiple markup languages in a single document. For example, a chemistry book might use a chemistry markup language when talking about the <formula> for sulfuric acid, but would use a math markup language to talk about the mathematical <formula> for calculating the acidity of a solution.

By associating a prefix with each markup language, it's possible to combine them in one document without having the names collide with one another, since each <formula> is in its own “space.” Thus, the book starts out like this:

    <book xmlns="http://www.book-o-matic.org/bookXML"
          xmlns:chem="http://chemistry.org/chemspec"
          xmlns:math="http://www.mathstuff.org/mathdocs">

The three namespace attributes mean that:

  1. Any tag without a prefix is presumed to be a tag used for a book-writing XML specification, whose unique idenitifer is http://www.book-o-matic.org/bookXML.
  2. Any tag with the prefix chem: should be validated against an XML specification whose unique identifier is http://chemistry.org/chemspec.
  3. Any tag with the prefix math: should be validated against an XML specification whose unique identifier is http://www.math.org/mathdocs.

Although the identifiers look like web addresses, they don't have to point to any valid web page. Since every XML specification has to have a unique identifier, it was decided to use Uniform Resource Identifiers (web addresses), since they can uniquely identify a developer or organization.

Once namespaces are established, an author can write this portion of the book without fear of conflicting names:

   <paragraph>
       We combine sulfuric acid,
       <chem:formula>H2SO4</chem:formula>,
       with water, <chem:formula>H2O</chem:formula> and
       determine its acidity by calculating
       <math:formula>acid / (water+acid)</math:formula>.
   </paragraph>