I recently found myself inclined to learn XSLT. I find the best way to learn a language is to apply it to something practical, so I used it to update my cv/resume.
The interesting thing about XSLT is that some browsers support client side transformations, whilst other browsers don’t. I wanted to take advantage of the capability of the clients which support XSLT whilst making it compatible with the others.
Using Apache HTTPd as the web server, the solution I came up with was to serve XML to those clients which support XSLT, and to do the transformation with mod_xslt otherwise.
I had to do a fair bit of research to get a list of user agents of browsers that support client side XSLT. I’m sure the list I came up with isn’t comprehensive, but it should cover 99% of the web browsers in use. The worse that can happen is a transformation is unnecessarily done server side when it could have been done client side. I came up with the following Apache configuration which I dropped in my .htaccess file:
## Serve index.xml by default
DirectoryIndex index.xml
## Set server side transformation for xml responses
AddOutputFilterByType mod-xslt application/xml
## Set client side transformation for:
BrowserMatch \bMSIE\s no-xslt
BrowserMatch \bFirefox/\d no-xslt
BrowserMatch \bSeaMonkey\d no-xslt
BrowserMatch \bSafari/[3-9]\d\d no-xslt
BrowserMatch \bOpera[/\s]\d no-xslt
## Turn it back to server side transformation for:
BrowserMatch \bMSIE\s[1-5]\. !no-xslt
BrowserMatch \bOpera[\s/][1-8]\.\d !no-xslt
BrowserMatchNoCase (bot|spider) !no-xslt
## Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent
You might wonder why you would use XSLT for something like a CV. Well, all I have to do now is write an MS Word stylesheet and a PDF stylesheet and I’ll be able to update my CV in all three formats xhtml/doc/pdf by updating a single XML file. Neat huh?
UPDATE:
I’ve given up on client side XSLT for now. There are too many quirks and problems with it and it is just safer to do the translation on the server side. A few problems:
Firefox doesn’t support disable-output-escaping
The NoScript Firefox addon disables XSLT as well as JavaScript
Old versions of Netscape don’t support importing external xslt
Want to leave a tip?You can follow this Blog using RSS or Mastodon. To read more, visit my blog index.