• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

namespaces in XML

 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can someone explain in layman's terms what namespaces in XML are used for? I have read the description from W3C: http://www.w3.org/TR/REC-xml-names/
, but it still does not quite make sense to me. What do we need it for? Is this similar to packages in Java?

thanks,
Barry
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Barry
This link we will go you a clear idea about namespace with good examples.
http://www.w3schools.com/xml/xml_namespaces.asp if you are not happy with that link,reply back.
Regards
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you're right on the money. They are like packages, not in terms of organization, but in terms of usage.
Think of namespaces as a way to enable use of similarly named tags in a single XML document. Let's suppose you own a retail book store and you are doing your sales analysis for this quarter. You have two XML documents that you received from two different sources - one from the sales department called sales.xml having a tag ( among many others ) named "quantity". The other XML is received from the inventory department called "inventory.xml" that also has a tag named "quantity"!
You have a program that consumes these XML documents in a combined format and churns out the quarterly sales report. The key here is that the sales program consumes the merged document. But as you can see, when you merge the two documents, the sales program has no way to recognize which "quantity" tag represents the number of books sold.
In order to solve the problem, you use namespaces. The program(XSLT!) that you use to merge the XML document can create a namespace enabled tag such as < sales:quantity > for representing the quanityt from sales.xml. Similarly it can create another tag called < inventory:quantity > for representing the quanity tag from inventory.xml.
With the name collision resolved( now there are no similarly named tags with different meanings ) in the XML document, the sales analysis program can be sure where to look for the sales quantity.
Hope you get the picture. The problem namespaces solve is just not name collision, but name collision with a semantic difference. It is okay to have similarly named tags in an XML document as long as they mean the same. When the similarly named tag can be interpreted in different ways, you should start using namespaces.
Hope this helps.
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ajith for the detailed explanation. This clears everything up very nicely. You should write a book!
So, the XSLT can create a namespace enabled tag such as <MyNameSpace:MyElement> and this would be the equilavent of a fully qualified class name in Java such as java.lang.String. Is this correct?

thanks,
Barry
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is correct. Although XSLT is normally used to create presentable documents, its main purpose is to transform one XML document to another.
If you were to programmatically merge the documents, you would use DOM parser to get the rootnode of the two documents and then doing an importNode(..) on the specific nodes in the required order. Once you have the merged document, you can use XMLSerializer to create the document on disk.
Remember, in order to declare your own namespaces, you will have to use an unique string for each namespace. If you are using XSLT, you can simply output a new node with namespace declaration so that it gets included in the merged document.
Cheers!
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith,
One last question. This unique string then should be a URL to be considered an XML standard namespace?

Thanks again,
Barry

By the way, thanks Balaji for the cool web site link. There is lots of useful info there.
[ July 23, 2002: Message edited by: Barry Andrews ]
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The unique string need not be an URL. In fact it is an URI - Universal Resource Identifier - an universal name that is universally unique. URLs are often used in place of URIs because they are considered unique. Organizations also use their own URLs becasue they have complete control over it.
It is important to remember, the parser does not validate the URI. A perfectly valid URI may not be a valid URL! URIs used as XML namespace names are just identifiers. They are not guaranteed or required to point to anyting.
In fact, the following URIs are perfectly valid identifiers.

xmlns:xmldbms="tag:rpbourret.com,2002:xmldbmsv2"
xmlns:foo="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"

Perhaps sometime in future, when the vision of "networked world" becomes a reality, the parsers will be able to verify the uniqueness of URIs using some sort of global registry.
I suggest that you read the article - Namespace Myths Exploded for very useful and often misunderstood namespace concepts.
Cheers!
 
reply
    Bookmark Topic Watch Topic
  • New Topic