Namespaces

  • Revision slug: Namespaces
  • Revision title: Namespaces
  • Revision id: 108509
  • Created:
  • Creator: Tonymec
  • Is current revision? No
  • Comment didn't work, try a different hack; 3 words added, 1 words removed

Revision Content

 

In addition to this document, see Namespaces Crash Course.

XML namespaces provide a way to distinguish duplicate element and attribute names. Duplicates element and attribute names can occur when an XML document contains elements and attributes from two or more different XML schemas (or DTDs). To quote Wikipedia: "In general, a namespace is an abstract container providing context for the items ... it holds and allows disambiguation of items having the same name."

If you are familiar with C++ namespaces, Java packages, perl packages, or Python module importing, you are already familiar with the namespace concept.

An XML namespace is identified by an unique name (called a URI, not a URL, even though it can look like a URL). An URI is any string, although most people choose a URL-based URI because URLs are an easy way to hope for uniqueness. Although there's nothing preventing someone else from using the namespace http://www.mozilla.org/keymaster/gatek...re.is.only.xul, it's fairly unlikely anyone would choose that accidentally. Even if they did accidentally choose it, they may not define the same elements as XUL anyway (e.g., <textbox/>) in their schema/DTD.

Any element type or attribute name in an XML namespace can be uniquely identified by its XML namespace and its "local name". Together, these two items define a qualified name, or QName.

For example, <xul:textbox/> uses a namespace named "xul" and a local name "textbox". This distinguishes it from, for example, <foobar:textbox/> which might occur in the same document. The xul and foobar namespaces must be defined at the top of the XML document in which they are used, like so:

 <foobar:some-element 
     xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
     xmlns:foobar="the-foobar-namespace">
   <xul:textbox id="foo" value="bar"/>
   <foobar:textbox favorite-food="pancakes"/>
 </foobar:some-element>

Notice I've mixed two <textboxes/> in the same document. The only way to distinguish that they have different meanings is with namespaces.

There's only one other thing to know: "default namespace". Every XML element has a "default namespace", and this is used with XUL elements all the time. In XUL documents, you'll usually see this:

 <window
     id="foo"
     xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
   ...
   ...
 </window>

and in XHTML documents, you'll see this:

 <html xmlns="http://www.w3.org/1999/xhtml">
   ...
   ...
 </html>

There is a very subtle difference here than before. Before I wrote xmlns:xul="http://www.mozilla.org/keymaster/gat...re.is.only.xul" but here the :xul piece is omitted. This signifies to the XML parser that http://www.mozilla.org/keymaster/gat...re.is.only.xul is the default namespace for the element and its descendant elements (unless further overridden by a default namespace on a descendant element), and that any element without a namespace (i.e., no prefix and colon) belongs to the default namespace. That's why we can write the shorthand <textbox/> instead of <xul:textbox/> in XUL (although the latter is just as correct when not using http://www.mozilla.org/keymaster/gat...re.is.only.xul as the default namespace) -- the XUL namespace is defined as the default on the topmost element. In other words, a default namespace permits a kind of short-hand to be used for all descendants of an element.

Here's a question: what namespace contains the element foo in the XML document below?

  <foo/>

The answer is that it's in no namespace, or alternately, it's in the namespace denoted by the empty string:

  <foo xmlns=""/>

This second example is semantically equivalent to the first.

Now, a second question: what namespaces are the bar, baz, and quux attributes in?

  <foo bar="value">
    <element xmlns="namespace!" baz="value">
      <element quux="value"/>
    </element>
  </foo>

bar is obviously not in a namespace. What about baz and quux? The answer is that they aren't in a namespace either. In fact no unprefixed attribute is ever in a namespace, primarily because XML originally didn't have namespaces, and all XML from that time had to stay in no namespace. This is a source of perennial confusion for XML namespaces.

Revision Source

<p> </p>
<p>In addition to this document, see <a href="/en/SVG/Namespaces_Crash_Course" title="en/SVG/Namespaces_Crash_Course">Namespaces Crash Course</a>.</p>
<p><strong>XML namespaces</strong> provide a way to distinguish duplicate element and attribute names. Duplicates element and attribute names can occur when an XML document contains elements and attributes from two or more different XML schemas (or DTDs). To quote <a class="external" href="http://en.wikipedia.org/wiki/Namespace">Wikipedia</a>: "In general, a namespace is an abstract container providing context for the items ... it holds and allows disambiguation of items having the same name."</p>
<p>If you are familiar with C++ namespaces, Java packages, perl packages, or Python module importing, you are already familiar with the namespace concept.</p>
<p>An XML namespace is identified by an unique name (called a URI, not a URL, even though it can look like a URL). An URI is any string, although most people choose a URL-based URI because URLs are an easy way to <em>hope</em> for uniqueness. Although there's nothing preventing someone else from using the namespace <code>ht<a class=" external" href="tp://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" rel="freelink">tp://www.mozilla.org/keymaster/gatek...re.is.only.xul</a></code>, it's fairly unlikely anyone would choose that accidentally. Even if they did accidentally choose it, they may not define the same elements as XUL anyway (e.g., <code>&lt;textbox/&gt;</code>) in their schema/DTD.</p>
<p>Any element type or attribute name in an XML namespace can be uniquely identified by its XML namespace and its "local name". Together, these two items define a <em>qualified name</em>, or <a class="external" href="http://www.w3.org/TR/REC-xml-names/#dt-qualname">QName</a>.</p>
<p>For example, <code>&lt;xul:textbox/&gt;</code> uses a namespace named "xul" and a local name "textbox". This distinguishes it from, for example, <code>&lt;foobar:textbox/&gt;</code> which might occur in the same document. The <strong>xul</strong> and <strong>foobar</strong> namespaces must be defined at the top of the XML document in which they are used, like so:</p>
<pre> &lt;foobar:some-element 
     xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
     xmlns:foobar="the-foobar-namespace"&gt;
   &lt;xul:textbox id="foo" value="bar"/&gt;
   &lt;foobar:textbox favorite-food="pancakes"/&gt;
 &lt;/foobar:some-element&gt;
</pre>
<p>Notice I've mixed two <code>&lt;textboxes/&gt;</code> in the same document. The only way to distinguish that they have different meanings is with namespaces.</p>
<p>There's only one other thing to know: "default namespace". Every XML element has a "default namespace", and this is used with XUL elements all the time. In XUL documents, you'll usually see this:</p>
<pre> &lt;window
     id="foo"
     xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"&gt;
   ...
   ...
 &lt;/window&gt;
</pre>
<p>and in XHTML documents, you'll see this:</p>
<pre> &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
   ...
   ...
 &lt;/html&gt;
</pre>
<p>There is a very subtle difference here than before. Before I wrote <code>xmlns<strong>:xul</strong>="<a class=" external" href="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" rel="freelink">http://www.mozilla.org/keymaster/gat...re.is.only.xul</a>"</code> but here the <strong>:xul</strong> piece is omitted. This signifies to the XML parser that <code><a class=" external" href="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" rel="freelink">http://www.mozilla.org/keymaster/gat...re.is.only.xul</a></code> is the <strong>default namespace</strong> for the element and its descendant elements (unless further overridden by a default namespace on a descendant element), and that any element without a namespace (i.e., no prefix and colon) belongs to the default namespace. That's why we can write the shorthand <code>&lt;textbox/&gt;</code> instead of <code>&lt;xul:textbox/&gt;</code> in XUL (although the latter is just as correct when not using <code><a class=" external" href="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" rel="freelink">http://www.mozilla.org/keymaster/gat...re.is.only.xul</a></code> as the default namespace) -- the XUL namespace is defined as the default on the topmost element. In other words, a default namespace permits a kind of short-hand to be used for all descendants of an element.</p>
<p>Here's a question: what namespace contains the element <code>foo</code> in the XML document below?</p>
<pre>  &lt;foo/&gt;
</pre>
<p>The answer is that it's in <strong>no</strong> namespace, or alternately, it's in the namespace denoted by the empty string:</p>
<pre>  &lt;foo xmlns=""/&gt;
</pre>
<p>This second example is semantically equivalent to the first.</p>
<p>Now, a second question: what namespaces are the <code>bar</code>, <code>baz</code>, and <code>quux</code> attributes in?</p>
<pre>  &lt;foo bar="value"&gt;
    &lt;element xmlns="namespace!" baz="value"&gt;
      &lt;element quux="value"/&gt;
    &lt;/element&gt;
  &lt;/foo&gt;
</pre>
<p><code>bar</code> is obviously not in a namespace. What about <code>baz</code> and <code>quux</code>? The answer is that they aren't in a namespace either. In fact no unprefixed attribute is ever in a namespace, primarily because XML originally didn't have namespaces, and all XML from that time had to stay in no namespace. This is a source of perennial confusion for XML namespaces.</p>
Revert to this revision