July 09, 2003

Wherefore XML Namespaces?

The obvious answer to this question is that XML Namespaces protect against name collisions. This is the most commonly cited reason for using namespaces. Yet there is a more subtle reason for using namespaces: to convey semantic meaning where the name of an element or attribute is a homonym. To understand this, consider the following example:

It is difficult to discern the exact semantic meaning the author of the document was trying to convey. Namespaces help not only to resolve naming conflicts, but also to imbue markup with additional meaning. Consider the revised example:


There is a significant difference between both tags with the local-name 'fire'. The XML Namespaces recommendation itself alludes to this usage:

We envision applications of Extensible Markup Language (XML) where a single XML document may contain elements and attributes (here referred to as a "markup vocabulary") that are defined for and used by multiple software modules. One motivation for this is modularity; if such a markup vocabulary exists which is well-understood and for which there is useful software available, it is better to re-use this markup rather than re-invent it.

Such documents, containing multiple markup vocabularies, pose problems of recognition and collision. Software modules need to be able to recognize the tags and attributes which they are designed to process, even in the face of "collisions" occurring when markup intended for some other software package uses the same element type or attribute name.

[Emphasis added]

Posted by Christian at July 9, 2003 03:31 PM |
Comments
Post a comment