Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

XML 101

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

by  flumpy  Posted    (Edited  )
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.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top