News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

What is a "namespace"? Why use a URI?

Started by VelMurugan, Jul 31, 2008, 08:53 PM

Previous topic - Next topic

VelMurugan

What is a "namespace"? Why use a URI?

A namespace is just that, a virtual "space" reserved for that particular identifying name. In reality it is just a name made up by whomever that particular XML tag belongs to.

Normally a URI is used as a global identifier as to the source of that namespace. It doesnt matter what you call a namespace when you create one, as long as your system knows how to handle it. Every other system will just ignore it if it doesnt know what to do with it.

Since XML can be used in global systems like the internet a namespace needs to have a URI string to uniquely identify it. URIs (also knowns as URLs) are used because the chances of having two namespaces the same from the same global client are almost zero. You do not have to use a valid URI, it can be any identifying string you like and it doesn't even have to exist!

To put this in context and to give a relevant demonstration of this, consider the following example:

When you have an XML parser sitting there and parsing XML documents coming its way, it needs a way to identify its own and potentially other peoples XML tags in order to extract the information relevant to it.

The namespace will uniquely identify the tags that are relevant to its and other peoples XML data in order for it to internally process or disregard that data.

For example, two nodes can be labelled "reference-number", and may at some time be in the same document. One node "reference-number" could belong to a library system, which could mean that this node contains a reference to the shelf number and location of a book. The other node "reference-number" could belong to the same companies online billing or tracking system which lends books from the library over the internet and refers to the customers reference number. In order to identify these two nodes you need a namespace. Therefore nodes can be identified completely by the namespaces "billing:reference-number" and "library:reference-number".

Ok imagine now, that all of itacumens.com's accounts are audited through a third company call "Just Accounting". Their XML documents can also contain a node called "reference-number". They also have a namespace "billing" ("billing:reference-number").

We now have a conflict with the two identical namespaces when the accounts of itacumens.com were processed by the systems in itacumens.com's system. How would a system know whether "billing:reference-number" came from itacumens or justaccounting? How would we process two completely different reference numbers?

The problem is solved if we identify one "billing" namespace with the URI "librarysonline.com" and the other "billing" namespace with the URL "justaccounting.com". Then we will inherently know that the librariesonline tag needs to be ignored (or processed differently!) by the justaccounting system just by looking at the namespace URI.

The URI uniquely identifies this in each namespace at each use of the node "billing:reference-number" in the XML Document so that each system can read and process it correctly.

So to answer your question, the URI uniquely identifies the namespace its self, and that uniquely qualified namespace uniquely identifies the tagname within the context of multiple systems.

Try this in any parser. Enter this XML:

<overall-document>
<library xmlns:billing="www.itacumens.com">;

    <billing:reference-number>
        23
    </billing:reference-number>
</library>
<justaccounting xmlns:billing="www.forum.itacumens.com">;
    <billing:reference-number>
        23.34
    </billing:reference-number>
</justaccounting>
</overall-document>

and then use this stylesheet to transform it:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; >
<xsl:template match="/" xmlns:billing="www.itacumens.com">;
    <xsl:value-of select="//billing:reference-number"/>
</xsl:template>

</xsl:stylesheet>


Notice that only the value 23 is printed out.

alenfort

- Name Space is  a virtual space set aside a specific identifier. In reality it's just a name formed by the person-specific XML tags .

- URI as a global identifier to the source name space. What is your name no matter from space, when you create one, as long as your system knows how to handle it. All other systems will just ignore it, if it doesn't know how to do.

jeckdenis

- 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.

- URL stand for Uniform Resource Locator. This is global address of documents and other resources on the World Wide Web.

naftaderefe

Yesterday, while I was at work, my cousin stole my apple ipad and tested to see if it can survive a forty foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views. I know this is totally off topic but I had to share it with someone!

mikerock

very informative article. I have a question

is there any disadvantage using namespace instead of class?

Sudhakar

good question, will check with my buddy and reply you.