1

So I've been reading about namespace on w3schools and I understand they are to uniquely identify an element. But what is the point of having namespaces if you have to have prefixes along side them. Are you always required to have prefixes with namespaces? If not are you always required to have namespaces with prefixes? If so why?

For example:

<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>

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

Doesn't f and h uniquely identify the different element types?

Thanks for your help. I'm really at a loss at understanding why namespaces exists.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Uriel Katz
  • 319
  • 1
  • 8
  • 21

1 Answers1

1

Why?

As with other languages, namespaces allow you to de-clutter the global namespace. This then allows for more than one element or attribute with the same name, provided it is in a different namespace.

Do you always need to qualify an element's namespace?

If you have a 'dominant' namespace containing most of the elements (e.g. http://www.w3.org/TR/html4/), then you can change the default the namespace to this dominant namespace, like so:

<z:root xmlns="http://www.w3.org/TR/html4/" xmlns:f="http://www.w3schools.com/furniture" xmlns:z="">
 <table>
   <tr>
   <td>Apples</td>
   <td>Bananas</td>
 </tr>

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

Note that we need to move root back into the global namespace to keep it equivalent to the original document.

StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • But doesn't the f or the h allow more than one element with the same name? What's the point of defining a name if f and h already differentiate the elements? – Uriel Katz Jun 19 '13 at 07:52
  • I'm not sure I understand you. `f` and `h` are just aliases for the namespace, otherwise you would have to qualify each element explicitly e.g. `` which would be unwieldy.
    – StuartLC Jun 19 '13 at 07:56
  • Right so what's the point of having a completely unique namespace made from a _**URI**_ to avoid conflicts with naming namespaces, when you're prefixes pointing to the namespace may not be unique making you run into trouble anyways? I'm trying to understand the w3 rationale for making namespaces have to be unique by requiring them to be URIs when the prefixes which point to the namespaces in the first place can conflict. You would run into the problem anyways. – Uriel Katz Jun 19 '13 at 08:02
  • The namespace (e.g. `http://www.w3.org/TR/html4/`) isn't just a random string - it forms part of a contract with regards to the content of elements in this NS in a document, e.g. and can be validated against a [schema](http://en.wikipedia.org/wiki/XML_Schema_(W3C)) – StuartLC Jun 19 '13 at 08:04
  • So what you are saying is that a namespace URI can declare different things like element attributes, etc? From what I've been reading is that information inside a URI is only for users and not read by any program. – Uriel Katz Jun 19 '13 at 08:13
  • @UrielKatz - That's correct. There no meaning to be found in the parts of the URI, but the string as a whole has a purpose. [For example](http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#xml-namespace) The HTML namespace is: `http://www.w3.org/1999/xhtml`, The SVG namespace is: `http://www.w3.org/2000/svg` etc. As StuartLC says, you wouldn't want to put one of those strings on the front of every element name, so prefixes allow you to abbreviate them. – Alohci Jun 19 '13 at 19:32