XSLT vs Python/LXML

I recently had to adapt some old XLST which read in an XML document and did some transformations to turn it into CSV data. For those who don’t know what XSLT is (consider yourself lucky!), it is a declarative, XML-based transformation language usually used for transforming a source XML into destination XML.

Now the XSLT I was dealing with also had some Javascript and VBScript functions inside it, and after struggling with it for a while I eventually realised – to my horror, as I needed it to run cross-platform on both Windows and Linux – that it also incorporated some Microsoft-specific extensions. So I ditched the XSLT and switched to writing it from scratch in Python with the LXML library instead. Less than 2 hours later, to do the exact same task – including thorough error checking – the Python turned out to be 205 lines, while the XSLT was 714 lines.

For a long time I wondered if it was just me, that I was too much of a procedural, C++ thinker, and just didn’t “get” XLST. XSLT is supposed to be a purpose-built tool for the job, right? Well, I’ll say now what I’ve always secretly thought, namely that XSLT is obtuse and horrible and I’ll steer clear of it forever. And evidently I’m not alone.