Sunday, June 17, 2007

Cooking with XSLT

At the risk of sounding like the biggest geek in the world (waaaaay to late in the game to be worried about that anyway) - XSLT is starting to reveal itself as a remarkably flexible language with a high coolness factor. Short for eXtensible Stylesheet Language Transformation. XSLT is a functional language designed to transform XML into HTML, XHTML, PDF, other XML formats, or basically anything else you want to output from it. XML is basically just style-stripped text organized into a meaningful hierarchical format. If you've every worked with SGML, XML is basically just the well-formed version of SGML (think Twins, the movie, with Schwartzneggar and DeVito - XML is the kid every parent wishes for and SGML is its job-hopping, looser, screw-up brother).

Anyhoo, back to the coolness factor surrounding this language. I first had the pleasure of working with XSLT when a former employer dubbed me the team "Stylesheet Expert" and tasked me with converting numerous XML military technical manual documents into HTML and PDF format. I refer to it as a pleasurable experience now, but the language of XSLT is very far removed from the functionality of most other mainstream languages out there (C, C++, C#, Java, etc.) and it was a serious pain in the brain trying to understand how it does what it does. For instance, XSLT does not have true variables in the way most other languages do, rather its variables are more like constants and the language itself is purely recursive, not iterative; another far cry from most other languages I'd worked with.

In spite of our initial quarrels, eventually XSLT and I learned to get along quite well and my most recent employer stumbled onto my hidden talent and tasked me with developing stylesheets for their military technical manual XML documents. This time, though, I had some slightly more severe requirements from the client and I've gotten a real taste of just how powerful XSLT can really be.

The website itself was quite straight-forward; some simple Javascript for dynamic table of contents behavior, a slew of internal/external cross-document links, but otherwise quite static and mellow. There was, however, an enthusiastic request for some search box functionality. Not a big deal until I learned the clients (military peepz) were not real excited about any third party packages or extensions being included on the distribution CD and the red tape required to get approval for such an inclusion was soooooo not worth the time or effort.


After some google searching I found a couple of simple scripts that would strategically escape a search phrase typed into a text-box and then hunt through delimited strings of text within an array for a match. Would this be a truly useful methodology for a traditional website; probably not if it were a sizable site with plentiful content, but for my situation, it was absolutely perfect. I could dynamically construct, not only the Javascript itself, but the javascript array of content; dividing the text into chunks according to the pages and URLS I was creating on the fly. It worked like a charm and took me maybe a couple of hours to write code that filled a couple thousand element sized array with every bit of content of the entire website. And best of all, I would never have to worry about updating the array when changes/additions were made to the site content; just a simple command line utility to reprocess the updated XML and viola - any new content is automatically integrated into the site.

Composing a website using XSLT is alot like writing a recipe that matches all the ingredients you already happen to have lying around your kitchen, to prepare exactly the meal you're craving. XSLT is not necessarily ideal for all situations, but if you're responsible for maintaining/developing multiple sites with virtually identical templates, or the content of your site changes heavily and frequently, but the template in which you display it does not, ooooor you're just a big geek like me and feel like tinkering around with some "cool" [in every un-cool sense of the word] technology - then take it for a test drive.

Some useful links:
There is no 'I' in style-sheet so feel free to drop me a line if you need any assistance my fellow XSLT-ers.
Good luck and happy transforming!!

1 comment:

appwiz said...

You should play with LINQ.
LINQ is to C# what XSLT is to XML.