Namespace

From Svg wiki

A simple way to think of namespaces is as the family name of an element. You might know 3 different people named Megan, but when it's important to make a distinction between them, you can say, Megan Smith versus Megan Klein or Megan Cho.

In general, a namespace uniquely identifies a set of names so that there is no ambiguity when objects having different origins but the same names are mixed together. Using the Extensible Markup Language (XML), an XML namespace is a collection of element type and attribute names. These element types and attribute names are uniquely identified by the name of the unique XML namespace of which they are a part. In an XML document, any element type or attribute name can thus have a two-part name consisting of the name of its namespace and then its local (functional) name.

For example, suppose the same XML document included the element type of OWNER for owners of motorcycles as well as for owners of automobiles. It might be necessary or desirable to know that an owner name was one of those who owned a motorcyle rather than an automobile. Having different motorcycle and automobile namespaces would make this possible. Effectively, it would make it possible to label motorcycle owners differently than automobile owners without having to create a different element type for each.

The classic example of ambiguity is that of a document with mixed namespaces, one XML dialect being XHTML and the other being Furniture Markup Language. Both have a 'table' element, but they mean very different things. If you included both in the same document, the host language (the language which is the root document, which we will say is SVG) would be unable to tell the difference between them without an indicator from the author which is which. Namepace prefixes allow you to do this with the scope resolution operator ':' (the colon).

html:table is an HTML element
fml:table is a furniture element

SVG might render the HTML table as a tabular chart, and might render the furniture table as a pre-chosen graphical representation, a drawing of a table.

In XML, a namespace is commonly given the name of a Uniform Resource Identifier (URI) - such as a Web site's address - both because the namespace may be associated with the site or page of that URI (for example, a company name) and because a URI is conveniently likely to be a unique name. Note that the URI is not necessarily intended to be used other than as a name nor is there any namespace document or XML schema that must be accessed; the URI is simply used as a name (and part of the two-part name of any element type or attribute name so that the names are unique within the document).

It is important to declare the namespace declaration for all languages used in a document. This is done in the root of the document, like this:

<svg 
  xmlns='http://www.w3.org/2000/svg'
  xmlns:xlink='http://www.w3.org/1999/xlink'
  xmlns:html='http://www.w3.org/TR/xhtml1/strict'>

  <use xlink:href='myFile.svg#mySymbol' />

  <html:table>
    ...
  </html:table>

  <custom:barchart>
    <bar>30</bar>
    <bar>10</bar>
    <bar>15</bar>
  </custom::barchart>
</svg>

In the above example, the first 2 namespaces are for SVG and XLink, respectively. They are required for SVG; if you do not include them, the UA may not render your content. The third declaration is for the HTML NS, and the fourth is for an imaginary custom namespace; these are only necessary if you include content from that language in your SVG file.

Namespaces become even more important if you are considering using sXBL.

Implementation Details

  • ASV incorrectly does not require the namespace to be declared in the root element.
  • Firefox correctly requires requires the namespace to be declared in the root element; it will not render documents without the proper namespaces. However, there is currently a debate about whether this is a good idea.

More Information

For more information, see the W3C Namespace Spec.

For way too much information, see the Namespaces FAQ.