Home - Programming - XSLT Testing.

In implementing software systems, I sometimes come across requirements to take XML data and convert it to something else. Sometimes it's HTML. For this task I have historically used Treebeard for development and testing, but for some reason, today, just when I needed it most (and isn't that how it always is?), Treebeard completely forgot that it even has an XSL editing and testing feature. The button for that feature stopped working at all. It didn't even have the common decency to report an error. It just didn't work anymore.

And ain't that just dandy?

So I scrambled to find anything at all that would do the job. Well, okay, not anything. I looked for something free, maybe even something I already had and didn't know would do the job. And after scanning multiple posts to various nerd boards, I found a little comment at the end of a post that basically amounted to, "Y'know... your browser will do that for you..."

And as soon as I read it, and practically did a forehead slap. I already knew this, in a way. Here's what you do. In your XML data file, the first line is something like:

  <?xml version="1.0" encoding="UTF-8"?>

In the next line, you can specify a stylesheet for your XML. Normal processing of the XML will ignore this line, but a browser will see the spec and pull the XSL to format the XML for display.

  <?xml-stylesheet type="text/xsl" href="my-xslt-template.xsl"?>

Now, you can't just pop open a browser and navigate to your local drive because, in most browsers, the "same origin" policy is applied to individual files on the local drive. Not so when pulled from a website, though. Now all you need is to copy it out to a website and hit the XML file on the website with your browser. In my case, I have an Apache install on one of my virtual machines, so I modified the XML to include the stylesheet reference, copied the XML and XSL files to the htdocs directory in Apache, and started up the Apache server. Then I brought up Chrome and put in the server address and the XML file name.


And BOOM, there was the formatted data. In my case, I was converting data for a transaction into an HTML receipt. Since Apache was running on my local system, I would edit the XSL, save, flip over to the browser, reload, check the results, flip back over to Notepad++, repeat. HTML didn't show up? Press F12 and check the console for errors.

This arrangement was actually far faster and more compact, for me, than using Treebeard. I often wondered why Treebeard stopped being supported about 10 years ago. Now I know.

Todd Grigsby