XSLT Considered Harmful

Warning XSLT can seriously twist your brain.

XLST was hailed at the end of the 90’s as being the uttimate tool to transform XML.
Theoretically you can use it to transform any XML into any other other format.

It can be very powerful (look at Code Generation).
However it needs to be treated especially carefully and be properly supported by unit tests of the transforms. Just because some clever code can be written does not mean that it should. Don’t take too many shortcuts.

Here is a useful python script that will make life a little easier:

import os

def XPathToXML(XPath):
   s = XPath.split(“/”)
   for x in s:
      print (“<“+x+”>”
   s.reverse()
   for x in s:
      print (“</”+x+”>”

if __name__ == “__main__”:
XPathToXML(os.argv[1])

This little beauty will take an XPath fragment such as
a/b/c and return:
<a>
<b>
<c>
</c>
</b>
</a>
Which is just what the doctor ordered when creating XSLT test data.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s