Friday 27 June 2008

XSLT

After spending so much time generating my XML data I decided that I wanted to be able to see it presented in a user friendly fashion. Enter XML Stylesheets.

Boy what a frustrating time I had getting these to work. I found two big problems that took some time to solve.

The first problem that I had was originally the browser complained about my stylesheet because the web server reported its mimetype as text/xml and suggested it should be text/css. I reconfigured the webserver thinking this was the problem (of no output) but found that the browser then tried to interpret my XML stylesheet as CSS and produces a multitude of errors as a result.

So after changing it back (and still not working) I finally hit upon the solution. The web server has to report the file as text/xml but the source XML file has to tell the browser that it is text/xsl so the browser knows how to interpret it.

Secondly, I found that although I wrote a valid stylesheet for reasons that I couldn't fathom it produced either no output or wrong output. The browser will only complain at you when your stylesheet is not valid or well formed but if it passes these two tests there is no way to ask it what is wrong with the stylesheet.

So then I had to start with the most basic stylesheet and work my way up from there. One of the things that caught me out was the matching system. If for some reason your templates didn't match everything in the XML file the the browser would just dump any XML that was left over. This was a huge source of frustration as I had the impression that anything not matched would be ignored.

I did finally get a reasonablly working stylesheet:

https://netdb.alphawest.com.au/xml/netdb-style.xsl

Now it is a simply task as I add a new node to the XML output to create a new template into the stylesheet and get it parsed as the appropriate place. Simple now that it works!

No comments: