Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am trying to figure out why we need URIs for XML namespaces and I cannot find a purpose for that. Can anyone brighten me a little showing their use on a concrete example?

EDIT:

Ok so for instance: I have this from w3schools

<root
xmlns:h="http://www.w3.org/TR/html4/"
 xmlns:f="http://www.w3schools.com/furniture">

 <h:table>
   <h:tr>
     <h:td>Apples</h:td>
     <h:td>Bananas</h:td>
   </h:tr>
 </h:table>

 <f:table>
   <f:name>African Coffee Table</f:name>
   <f:width>80</f:width>
   <f:length>120</f:length>
 </f:table>

 </root>

So what should http://www.w3schools.com/furniture hold ?

share|improve this question
1  
6  
Obligatory w3fools comment. –  Yannis Rizos Nov 28 '11 at 4:04
    
So does XML namespaces have to be in a URI format? Or can I just use any string like 'My.Namespace.Foo'? –  Mas Jan 17 '12 at 13:07
add comment

3 Answers

up vote 9 down vote accepted

A namespace is a way of saying "This kind of Foo" is different from "That kind of Foo", even though they are spelled the same. Or, if you prefer "MY kind of Foo" is different from "Everybody elses kind of Foo".

The technical way of saying this is "The URI of my namespace for Foo" is different from everybody elses URI for their namespace for Foo. In other words, URI's are just strings that allow you to say so.

The trick is then to say, "Hey, URL's are valid URI's", and then use a URI corresponding to a URL under your control. If everybody do that, then you avoid accidental namespace collisions. You could as well have said namespace "A" and namespace "B", but you risk that somebody else would use the same namespace too, and then your kind of Foo is not different from their Foo anymore which is exactly what you want to avoid.

You can then add additional convention to the URLs used as URI's, for instance that the URL must correspond to a page containing documentation or XSD's or similar, but this is not necessary. It is just convenient.

share|improve this answer
    
Ok, but what's a difference between namespace A and namespace w3.org/1999/xhtml ? both of them can be used by users without them knowing that they have used the same namespaces. –  Patryk Nov 28 '11 at 4:14
    
@Patryk, that is why you use a URL you have control over. The xhtml namespace URL is under control of W3C who defined the XHTML specification. –  user1249 Nov 28 '11 at 4:24
1  
@Patryk, also note that it is the ones defining the namespace that gets to pick the URL. Then everybody else can use them. –  user1249 Nov 28 '11 at 5:07
add comment

In general terms, any unique identifier would serve for a namespace; but since it's supposed to be globally unique, the standard would have to either mandate some arbitration authority, or use another resource that is at the same time globally unique but easy to get hold and to prove it's yours.

Oh, look! if you have a domain, it's obviously only yours, and it's easy to prove it's yours!

For example, they wanted to avoid the situation where two different bookstores start using "books" as a namespace, with totally different internal definitions. While they use it internally, there's no problem; but as soon as one of them wants to publish their specification, all hell breaks loose.

Instead, if you're encouraged to use your domain, anybody (well, anybody who owns a domain) can define their own private namespace with the confidence that nobody else will use the exact same identifier.

As a bonus, you can optionally store the definition of your namespace at the URI, making it self-documenting.

Seems like a great idea to me.

share|improve this answer
    
Thanks for the answer but I would need something more concrete to understand this. I added some exemplar code that you can base on, please take a look. –  Patryk Nov 28 '11 at 3:51
1  
@patryk - nothing, it's not an address - it's just a unique token that you are sure no one else that you might mix xml files with has. All it is saying is that the names <table> etc won't be used wit any different meanings in any xml files that have the www.w3.org name at the top –  Martin Beckett Nov 28 '11 at 3:59
    
Ok but when wikipedia says something that users can use for instance w3.org/1999/xhtml as a namespace then a lot of people would have the same namespace. Am I right ? –  Patryk Nov 28 '11 at 4:12
    
@Patryk, only if you want to use it for the same use. If you want to define a new namespace, you're encouraged to use your own domain –  Javier Nov 28 '11 at 4:16
    
@Javier What do you mean by 'the same use' ? –  Patryk Nov 28 '11 at 4:39
show 3 more comments

DOM Level 2 was published in late 2000. It introduced the "getElementById" function as well as an event model and support for XML namespaces and CSS.

What xmlIns specifies?
As attribute nodes named "xmlns" or "xmlns:xxx", exactly as the namespaces are written in the source XML document. This is the model presented by DOM.

What URI contains?
An XML namespace is declared using the reserved XML pseudo-attribute xmlns or xmlns:prefix, the value of which must be a valid namespace name.

For example, the following declaration maps the "xhtml:" prefix to the XHTML namespace:

xmlns:xhtml="http://www.w3.org/1999/xhtml"

AS i have discovered after reading the article on Wikipedia. URI contains the XML formatted specifications of the namespace provide a method to avoid element name conflicts.

May be you know xml document uses XML Schema for the element/table/class structure of elements of the XML document.

what should http://www.w3schools.com/furniture hold ?

The answer is on your page where you have learned about the XML Namespace. Check Namespaces in Real Use section on this W3Schools article.

Check following link of stackoverflow which some what demonstrate that what this URI carries:

How to create and use XML namespace?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.