XML as a configuration medium

This morning I read an interesting post by Charles Miller titled Dear XML Programmers. Although written in May of 2003, rather than outline my response to Zanchey (who sent me the link), I figured I would put a detailed response here.

In his post, Charles talks about using XML as a programming language, and why he personally does not like the practice.

To everyone involved in writing XML-based programming languages, and by this I mean those languages where XML is the primary syntax, not useful things like ECMAScript that just happen to be applied to XML...

Please stop.

I agree that the thought of using XML as a programming language is quite absurd - not least of all the folks who think that the idea of a whole language _written_in_ XML would be good.

No, no it would not - XML is not flexible. This is both it's greatest benefit and worst restriction, depending on circumstances.

Storing data in a database is powerful because it is not flexible - data is stored in a specified manner, in a specified arrangement. It's not negotiable. XML has the same strength when used as a data storage format. Marking up documentation, storing a blog or answers to a questionnaire all require the information to be stored in a standard manner so as that they can be parsed quickly and efficiently by the handling application. With tools such as XPath and the such, data is easily accessible for reading and manipulation.

For storing data, being extremely structured is a good thing.

However, for programming languages, such as Ruby, their power comes from their flexibility - programming languages require this so as to be able to solve a vast array of problems.

As an example of this, have you ever tried writing an If-Then-Else in XSL? You actually have :

    <xsl:when test="@foo = 'bar'">
       <!-- Do stuff here -->
    <xsl:when test="@foo = 'bletch'">
       <!-- This would be an elseif -->
       <!-- This is the else -->

It is incredibly verbose, difficult to read, and not to mention a pain to write. Compare that to other languages where you are just looking at :

if ($foo == 'bar')
    //Do stuff here
elseif ($foo == 'bletch')
    //This would be an elseif
    //This is the else

Regardless, Charles' issue seems to me (based on his example) to be not so much an argument against using XML for configuration, but using XML as a scripting language.

XML isn't a scripting language. Full stop.

Although I don't particularly like Charles' solution because it is LISP and for reasons similar to some of the comments left in response, it seems to me to be a reasonable way to handle a configuration script. So long as it is usable...

Anyhu - when it comes to configuration, the solution often has to have some aspects of a programming language (flexibility), but also has to be structured (for validation and error detection) and the ability for humans to manipulate it (by the UNIX philosophy, anyway). It's hard to find something that will fit the bill perfectly, but for storing simple non-scripting configuration, XML seems like a suitable (manageable) solution. Of course, in the right situations only... In PHP, configuration may just be a series of define() statements with your constants. It depends on the situation.

All this aside, it will be interesting to see when these XML-based languages start to pop up, and just how productive they are. There are also some CASE tools popping up to allow you to convert code into XML for analysis and the such. This may be something interesting to keep your eye on for analysis and optimization and the such.