SCDJWS Study Guide: XML Namespace


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

Introduction

What is a Namespace?

A namespace is a set of names in which all names are unique. Any logically related set of names where each name should be unique is a namespace. Namespaces make it easier to come up with unique names. Before a new name is added to a namespace, a namespace authority ensures that the new name doesn't already exist in the namespace.

Namespaces themselves must also be given names in order to be useful. Once a namespace has a name, it is possible to refer to its members. For example, Micosoft.RadioService and AOL.RadioService. The names in the namespace should be unique. If this cannot be guaranteed, then the actual namespace names themselves could also be placed into a name space of their own. For example,US.Microsoft.RadioService and CHINA.Microsoft.RadioService. In order to guarantee the uniqueness of namespace names, this pattern can be iterated as many times as necessary.

More than one namespace may appear in a single XML document, to allow a name to be used more than once. Each reference can declare a prefix to be used by each name.

Why use namespace in XML?

Many developers feel that the XML 1.0 specification was incomplete because it didn't offer namespace support. As a result, all names used in XML documents belonged to one global namespace, making it very difficult to come up with unique names.

Most developers, including the XML 1.0 authors themselves, knew that this would eventually cause too much ambiguity in large XML-based distributed systems.

Since element names in XML are not fixed, very often a name conflict will occur when two different documents use the same names describing two different types of elements.

This XML document contains information in a HTML table:

<table>
  <tr>
  <td>Apples</td>
  <td>Bananas</td>
  </tr>
</table>

This XML document contains information about a table (a piece of furniture):

<table>
  <name>African Coffee Table</name>
  <width>80</width>
  <length>120</length>
</table>

If these two XML documents were added together, there would be an element name conflict because both documents contain a <table> element with different content and definition.

The Namespaces in XML Recommendation is the W3C's solution to the XML 1.0 naming woes. This specification defines how to extend the XML 1.0 concrete syntax to support namespaces. Because most developers consider this addition fundamental and absolutely necessary, it is often given the respect of an official XML 1.0 addendum, even though it isn't one. In fact, today many developers refuse to refer to XML 1.0 alone but rather as "XML 1.0 + Namespaces" for the same reason.

The Namespaces in XML Recommendation defines the syntax for naming XML namespaces as well as the syntax for referring to something in an XML namespace. It doesn't address, however, the syntax for defining what's in the XML namespace. This was left to another specification, namely XML Schema. There are more details in the XML Schema section.

XML schema provides a "namespace" mechanism, which uses additional names to distinguish elements with the same name but different meaning in different contexts.

The solution is to prefix a unique string to elements or attributes that need to be distinguished.

This XML document contains information in a HTML table:

<h:table>
  <h:tr>
  <h:td>Apples</h:td>
  <h:td>Bananas</h:td>
  </h:tr>
</h:table>

This XML document contains information about a piece of furniture:

<f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

Now the element name conflict is gone because the two documents use a different name for their <table> element (<h:table> and <f:table>). By using a prefix, we have created two different types of <table> elements.

The XML namespaces provides a way to distinguish between duplicate element types and attribute names. Duplicates can occur when an XML document contains element types and attributes from more than one different XML schemas (or DTD).



Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |