Monday, July 4, 2011

XML NAMESPACE

XML Namespace

In XML, a namespace is used to prevent any conflicts with element names.

Because XML allows you to create your own element names, there's always the possibility of naming an element exactly the same as one in another XML document. This might be OK if you never use both documents together. But what if you need to combine the content of both documents? You would have a name conflict. You would have two different elements, with different purposes, both with the same name.

Example Name Conflict

Imagine we have an XML document containing a list of books. Something like this:


<*books>
<*book>
<*title>The Dream Saga<*/title>
<*author>Matthew Mason<*/author>
<*/book>
...
<*/books>
PLEASE Remove Firstly All Asterics *


And imagine we want to combine it with the following HTML page:


<*html>
<*head>
<*title>Cool Books<*/title>
<*/head>
<*body>
<*p>Here's a list of cool books...<*/p>

(XML content goes here)

<*/body>
<*/html>
PLEASE Remove Firstly All Asterics *


We will encounter a problem if we try to combine the above documents. This is because they both have an element called title. One is the title of the book, the other is the title of the HTML page. We have a name conflict.

What we can do to prevent this name conflict is, create a namespace for the XML document.

Example Namespace

Using the above example, we could change the XML document to look something like this:


<*bk:books xmlns:bk="http://somebooksite.com/book_spec">
<*bk:book>
<*bk:title>The Dream Saga
<*bk:author>Matthew Mason
<*/bk:book>
...
<*/bk:books>
PLEASE Remove Firstly All Asterics *


We have added the xmlns:{prefix} attribute to the root element. We have assigned this attribute a unique value. This unique value is usually in the form of a Uniform Resource Identifier (URI). This defines the namespace.

And, now that the namespace has been defined, we have added a bk prefix to our element names.

Now, when we combine the two documents, the XML processor will see two different element names: bk:title (from the XML document) and title (from the HTML document).

Note: If you have defined your tags and attributes in a DTD, you will need to update your DTD in order to make the new element names legal.

Solving the Name Conflict Using a Prefix

Name conflicts in XML can easily be avoided using a name prefix.

This XML carries information about an HTML table, and a piece of furniture:


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

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

PLEASE Remove Firstly All Asterics *


In the example above, there will be no conflict because the two <*table> elements have different names.

Uniform Resource Identifier (URI)

A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource.

The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN).

In our examples we will only use URLs.

THANK YOU