XML Namespaces – Why Are Namespaces Necessary for Prefixes?

xhtmlxml

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.

Best Answer

An XML namespace is supposed to have a reasonable chance of being universally unique. A prefix, especially a single-letter one, is almost certain not to be. A namespace declaration creates a mapping from an otherwise meaningless prefix to a stable identifier that software can rely on to identify the schemas of XML elements.

You're right: within a single document, an author can make sure that different prefixes are used for different groups of elements. But without tying those prefixes to unique namespaces, how do you accomplish any of the following:

  • Sending that XML file to an organisation that uses different prefixes for the same groups of elements, and expecting it to be interpreted correctly by (hypothetical) software that only uses prefixes?
  • Any automated processing of XML documents at all? Even something as fundamental as schema validation: how does your HTML parser know to associate the prefix h with the collection of HTML elements, and therefore knows how to render h:table?

There is no central database of prefixes where h is forever and all time associated with HTML elements - that's the purpose of the namespace declaration mapping the prefix to the namespace within the scope of the document.

Update - clarification of terminology

  • Namespace URI, also just called namespace: A stable name uniquely identifying a collection of XML elements and attributes.
    • This is just an identifier, chosen to be unique.
    • Although it is often a URL, whatever document might be available at this URL is irrelevant and is not consulted during XML processing. Browsing to the URL does not have to produce any valid content, although sometimes the company which produced the XML schema, and chose its namespace URI to have a domain which they control, chooses to publish some information about the schema at that address.
    • Note that this means you cannot expect to download a schema given a namespace URI. You will need to get hold of schemas using some other mechanism, probably documented by whoever produced the schema.
  • Prefix: a document-local representation of a namespace URI within an XML document. Any arbitrary prefix can be mapped to a namespace using xmlns. If you really wanted to, you could have both xmlns:f="http://www.w3schools.com/furniture" and xmlns:argos="http://www.w3schools.com/furniture", and then use the prefixes interchangeably: <f:table> or <argos:table>. The parser doesn't care whether the prefix is f or argos - it only cares that both of these table elements belong to the namespace http://www.w3schools.com/furniture. Further, the URL http://www.w3schools.com/furniture does not have to be a valid document. When used as an XML namespace URI, its only function is to be a unique identifier. If the parser had available a schema whose targetNamespace was http://www.w3schools.com/furniture, it could validate the table elements and their contents using that schema.