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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top