Excessive Microparsing

XML Bad Practices

Microparsing is a term covering the use of an extra non-XML syntax inside of XML, usually in attribute values. It has been the subject of heated debate in the earlier days of the XML community. The idea here is not to flag microparsing as always bad since in fact there are numerous cases in which it is a good idea. Rather, there should be a rule of thumb separating the good uses of it from those in which it is simply hiding away structure that should be in the tree as in the previous CDATA section example.

Microparsing is generally good when it is designed with the author in mind. For instance, XPath is much better as it is than if one had to turn book[/bookstore/@specialty=@style]|//author[alias[2]] into an XML tree. This is essentially the same argument that goes in favour of supporting a regular expression language within a larger language rather than having to express the same concept with a long series of method calls.

There are however cases in which microparsing does not help the author much. For instance, here is an extract from an SVG path:

Some people, including yours truly, can read and even write the above. But they should be discarded as bad guinea pigs. The reason for using such a syntax for paths in SVG was two-fold (and is the same reason used in other similar situations): file size, and DOM size (whereby if an element had been used for each path command, the DOM would have been supposedly much larger). Where file size is concerned, the structure of such path data is so repetitive that a good compression algorithm (such as gzip, or of course EXI) will produce similar compressed sizes whether the microsyntax or elements are used — and since SVG path data is usually big, one wants to use compression anyway (support for which is mandated). And where the DOM size is concerned, one has to keep in mind that it is merely an API. A generic DOM will be larger, but the DOM inside an SVG implementation should be able to have a very similar footprint to the one based on the microsyntax since whether path data is in an attribute or in elements should have little effect on internal storage.

So the rule of thumb in this situation is that microparsing is for authors, not for implementations.

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