INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

XML 101

What is a "namespace"? Why use a URI? by flumpy
Posted: 24 Jan 05 (Edited 24 Jan 05)

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 librarysonline.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 librarysonline.com were processed by the systems in justaccounting.com's system. How would a system know whether "billing:reference-number" came from librarysonline 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:

CODE

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

    <billing:reference-number>
        23
    </billing:reference-number>
</library>
<justaccounting xmlns:billing="www.justaccounting.com">;
    <billing:reference-number>
        23.34
    </billing:reference-number>
</justaccounting>
</overall-document>
and then use this stylesheet to transform it:

CODE

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

</xsl:stylesheet>

Notice that only the value 23 is printed out. Change the xmlns:billing parameter in the template to "www.justaccounting.com"; to print out the other value.

Back to XML FAQ Index
Back to XML Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close