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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Namespace URI 1

Status
Not open for further replies.

RicoCali

Programmer
Dec 7, 2002
80
US
OK...I've read and read everything about XML namespace and what's eating me up is no one explains what the contents are in this name space URI. By definition an XML namespace is:

"...a collection of element type and attribute names. The namespace is identified by a unique name, which is a URI"

OK got that part. Now how does this URI look like? When I go to that URI it's either a site of whatever or there is nothing. It suppose to be a collection of element type and attribute names. I'm expecting like a structured specification. What am I missing here?
 
you're getting a little confused. a namespace is just that, a space reserved for that particular identifier - really, its just a name made up by whomever that particular XML tag belongs to. The uri is just a link to identify the source of that namespace.

Since XML can be used in global systems like the internet, its necessary that when a namespace is created, it needs to have a source to uniquely identify it. URIs (or as u and i know them , URLs) were used because the chances of having two namespaces the same from the same global client were almost zero.

Read this:


it may clarify things a bit more than I can. :)


Matt
 
Thanks Matt for the response and I had read the W3C link that you referred me to. You're right though, I am still terribly confused.

> a namespace is just that, a
> space reserved for that particular
> identifier - really, its just a
> name made up by whomever that
> particular XML tag belongs to

Now we're getting somewhere. So are you saying that the namespace URI:

<xyz xmlns:abc='OR
<xyz xmlns:abc='
...is valid? That URI can be anything? XML using XSLT is not dependent of that URI of that namespace for XSLT to function? So you are saying that the content of that URI is just documentation and XML is not dependant on it?

Regards,
Rico
 
>That URI can be anything? XML using XSLT is not dependent
>of that URI of that namespace for XSLT to function? So
>you are saying that the content of that URI is just >documentation and XML is not dependant on it?


In a word, Yes.

I found another article for you to read. Note particularly the last paragraph which explains what I was trying to say before :)


matt
 
Thanks for the link Matt and here is the quote:

> The URI associated with a namespace
> declaration doesn't have to point to
> an actual document or page, nor does
> it have to utilize HTTP as my examples
> do. Many known URIs don't return anything
> when plugged in to a browser. XML parsers
> compare namespace URIs on a character-by-character
> basis, so the name must be unique and
> consistent in its usage.

OK..I understand and my question now is so why have the URI point to a http? Why not have it like this:

<a xmlns:b='a'>
OR
<b xmlns:b='b'>
OR
<b xmlns:c='c'>

Is this OK?

 
No, a URI doesnt have to point at a real website. It can be anything at all.

Its just that in the global context like the internet, the URI (or URL) is going to be pretty unique. In your own applications, it doesnt really matter if the namespace is referenced by &quot; or &quot;wombles&quot; or even &quot;moomins&quot;.. its just a name that identifies the namespace at the end of the day. As long as its unique to you for the time being, all is well.


Plus, as a point of interest and to as much correct myself as anything, in your previous example where you asked

So are you saying that the namespace URI:

<xyz xmlns:abc='OR
<xyz xmlns:abc='...is valid?


the answer is still &quot;YES&quot; but these are NOT EQUIVALENT afaik. If indeed 64.109.24.36 resolves to it is irrelavent. The two namespaces you created are not the same namespace and will be treated uniquely.

Hope that clarifies a bit more...

matt
 
Thanks for clearing that up Matt. But now I have another question.

The namespace is a unique reference. A unique reference of what? The document itself? If so, give me an example of referencing the namespace.

Thanx,
Rico
 
Ok lets put this into context, and initially go back to basics:


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. In order to uniquely identify namespaces, you need a URI.

For example, two nodes can be labelled &quot;reference-number&quot;, and may at some time be in the same document. One node &quot;reference-number&quot; 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 &quot;reference-number&quot; 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 &quot;billing:reference-number&quot; and &quot;library:reference-number&quot;.

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

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 &quot;billing:reference-number&quot; came from librarysonline or justaccounting? How would we process two completely different reference numbers?

The problem is solved if we identify one &quot;billing&quot; namespace with the URI &quot;librarysonline.com&quot; and the other &quot;billing&quot; namespace with the URL &quot;justaccounting.com&quot;. 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 &quot;billing:reference-number&quot; in the XML Document so that each system can read and process it correctly.

Phew :)

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=&quot;
<billing:reference-number>
23
</billing:reference-number>
</library>
<justaccounting xmlns:billing=&quot; <billing:reference-number>
23.34
</billing:reference-number>
</justaccounting>
</overall-document>

and then use this stylesheet to transform it:

<?xml version=&quot;1.0&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot; >
<xsl:template match=&quot;/&quot; xmlns:billing=&quot; <xsl:value-of select=&quot;//billing:reference-number&quot;/>
</xsl:template>

</xsl:stylesheet>

Notice that only the value 23 is printed out. Change the xmlns:billing parameter in the template to &quot; to print out the other value.

hope that helps a bit more :)



Matt
 
Matt...Thanks for clearing that up. It's coming around now. I'm gonna have to either buy you dinner or send some business your way for going out of your way to educate me. Very much appreciated.

Basically the namespace is a unique identifier to distinguish two of the same element name that is intended to be different. From your XML document example it only looks like this only holds true if it is in the **same** document. Is that correct? Or does this also apply with same elements in separate documents. Also, if you know that you're elements are going to be all unique in that document, do you need a namespace?

Rico
 
Basically the namespace is a unique identifier to distinguish two of the same element name that is intended to be different. From your XML document example it only looks like this only holds true if it is in the **same** document. Is that correct?


Nope. The namespace will uniquely identify the element globally. Because the namespace is &quot;globally&quot; unique, therefore the element becomes &quot;globally&quot; unique by implication.

Anyone reading one document (as in a printout of the document), and then looking at another document with the same element in it (the library's billing:reference-number or the accountant's billing:reference-number, for example), will be able to tell that these two elements are unique by looking at the namespace's URI. Its the same for parsers.

Also, any systems should transfer elements between documents or other systems preserve the namespace as it was in the originator, afaik. I think its in the XML spec on w3orgs site.


Also, if you know that you're elements are going to be all unique in that document, do you need a namespace?

Well for small apps with limited interaction with other systems, no you do not need a namespace. If you can forsee any XML being transfered to other systems in the future, or for your own clarity and others, the use of a namespace is a useful addition to any XML document.


As for dinner, unless you're coming to England (i'm guessing you're American) any time soon I'll have to pass :) Just mark my post as helpful and we'll call it quits ;)


Its been fun, and its certainly made me think more about namespaces too. Its all too easy sometimes just to assume you know about something. Its not until someone asks direct questions about it and you do some research that things become clear in your own mind :)

Matt
 
I'm almost 100% confident of namespace and will probably be able to write a 2000 page book on namespace alone and will defineately share the royalties with you ;-)

This last question and your answer will probably end this whole thread:

> Anyone reading one document,....
> ... and then looking at another
> document with the same element
> in it,...will be able to tell that
> these two elements are unique by
> looking at the namespace's URI.
>Its the same for parsers.

It's the same for parsers? Well if you look at the URI you used for parsing...

> &quot;
I haven't seen anyone that ***doesn't*** use this URI for XSLT parsing. Everyone uses this namespace even in the classes that teach XML, all published book on XML and all the examples you see through out the entire web. Which leads me to believe that this particular namespace URI is mandatory for XSLT. But from everything you said, it doesn't matter what that namespace is...even for XSLT. Is that right?
 
Ok..


Lets delve into the way that parsers work at this moment in time.

All XSLT parsers have inherantly inbuilt functionality. They will know, for example, that when they come across elements identified within the &quot; namespace, this is something they know how to process with this internal functionality. Its inbuilt, because this is the &quot;standard&quot;, something that every XSLT parser should have. They internally follow all those rules laid out by the w3 org.

This is why in XSLT every namespace is identified with that uri, and every transformation tag is identified with &quot;xsl:&quot;. Theres nothing to stop you defining your own namespace called xsl, as long as the URI is different to the w3 org one; even one letter difference will do. The parse will then determine that these two namespaces are different, and disregard yours or follow your instructions for parsing them (for example when you extend the XSLT parser implementation with your own class).


What I really meant was, and you'll have to forgive me, is that &quot;It doesnt matter what you call a namespace&quot;, 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.

So yes, it doesnt matter. Except when someone else has declared that &quot;this is how you handle my data&quot;. But there are no magic scripts by which the parser follows in order to work out how to process the data contained in any particular tag, its all inbuilt into the parser.

:)

Matt
 
Very good Matt. Thank you for your time and effort. I feel real confident about namespace. It's very clear to me now. I'm sure you explained it better than anyone out there since I could never get it from everywhere I've read.

Have a good weekend :)
Rico
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top