views:

32

answers:

2

Hello everybody

I am learning xml and xml processing.I couldn't understand existence of namespace well.

I learn that Namespace help us seperate same named elements in xml.Can't we discriminate elements by attributes which has same name ? Why namespace is important or required ?

I want to give an example:

<persons>
  <person></person>
  <s:person xmlns:s="student"></s:person>
  <person type="student"></person>
</persons>

First person is normal element.Second use namespace and third use type attribute. can't we use third one instead of second one ?

A: 

The difference between using an attribute and a namespace is to do with the semantics. In your example, a student is a sub-class of person. What if you used the tag table? A database table and a wooden table aren't sub-classes of the same generic table - they're completely different concepts, so it makes sense to use different namespaces.

Of course, if it's your own application, then no, namespaces aren't required, but if you're using another application's XML, and they're namespacing their XML, then you need to namespace it too

Have a look at XML Namespaces on W3Schools for more information.

Skilldrick
Already i learn from W3Schools
Freshblood
Student is sub-class of a person or persons ?
Freshblood
A student is a type of person, but a database table isn't a type of wooden table - they're different things.
Skilldrick
A: 

Sure you can differentiate your objects and their attributes with different names - as long as you completely and totally control all your XML data!

But consider this: you need to integrate with third-party web services, or a data interface that sends you data in XML. What if that web service or that data interface also implements a <Person> ? How do you keep your own person type apart from the potentially quite different person type implemented by a third party?

This is the idea behind XML namespaces - and that's why it's utterly important to make them globally unique which leads to the practice of using URI's (domain names) for them - since the "google.com" domain will definitely only ever exist once - globally.

marc_s