Robin Berjon

XML Bad Practices

Processing Namespaces Differently

Once in a while someone decides that they dislike namespaces enough that their program should implement them differently. Technically, that's not entirely wrong: since the Namespaces in XML specification was built separately from the XML specification, some exegesis could declare alternative approaches correct. But being pedantically right never prevented anyone from being pedantically daft at the same time, and this is one case in which the two coincide.

The more frequent in such inventive processing is to declare that all attributes of an element that have no prefix shall be considered to be in that element's namespace. There is no good reason to make it so, and people who opt for that approach often short-sightedly believe that it should have been that way from day one. An example is enough to show where this starts being problematic:


  
    ...
  
]]>
        

The XML parser will report no error here, but we now have two attributes with the same fully qualified name, and different values. Which one takes precedence? XML parsers aren't required to return attributes in their original order, so it's hard to specify. Of course, the Namespaces specification has the same issue with two attributes with different prefixes resolving to the same namespace URI, but it defines that as an error and processors implement it. This means that with this new rule, one needs to re-implement lower-level processing that is usually taken care of inside the XML parser. If one is not reusing the XML infrastructure, one might just as well not use XML at all.

There are even more creative deviations from namespaces. I will not go into the details of this since the same conclusions apply, but the rules for namespace processing in the Ant build tool at some point reached a rare degree of confusion. Not only was a default namespace applied without being declared, but elements from the default namespace sometimes had to be put in another namespace to be recognised when they were the child of an element from outside of the default namespace. If anything, this probably shows that one should be very careful before "simplifying" namespaces — many such simplifications have unpleasant side-effects.

This article is part of a series on XML Bad Practices.