When looking at one of my personal projects, where I was using an XML version of some MySQL stored data, I realised there is a very basic issue with using stylesheets.
Which is the more likely scenario? A) You have an XSLT stylesheet, which you wish to apply to several files of XML data, or B) You have one file of XML data which you wish to display in different ways using different stylesheets. In all honesty I think they are equally important. But the standards seem to have set a preference for the former, much to my dismay.
When applying an XSLT stylesheet to some XML data (through XML means alone) you would specify the stylesheet inside the XML file.
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="mytransform.xsl"?>
<data>
...
</data>
Unfortunately this literally binds the XML data to one way of displaying it, not a particularly useful thing to do. For instance, if our XML data represented a shops stock, you might want to show a summary of particular product types, with each type as a link to a page which shows a detailed list of products of that type. To do this with .XML files we would have to duplicate the XML data in another XML file and link that to a separate stylesheet. Of course this way you are free to use this stylesheet for as many different XML files as you like.
Luckily there are solutions to this problem and it doesn’t necessarily revolve around JavaScript. Not that I have anything against JavaScript or client-side scripting in general, but in these cases I would just prefer something more static on the client-side and only send the browser the resulting document. Many server-side scripting languages have support modules for processing XML documents with XSLT stylesheets/transforms including PHP, PERL, Python and I suspect ASP does as well, although I haven’t investigated this yet. For example, using PHP:
<?php
$xm = xslt_create();
$html = xslt_process($xm, 'data.xml', 'transform.xsl');
xslt_free($xm);
echo $html;
?>
Here we create an XSLT parser object ($xm) and use it to transform an XML document using a specified XSLT transform/stylesheet, finally printing the resulting output to the client browser.
If, like me, you are using a Redhat 9 Linux box to serve the pages for this then be warned that you may have to do a little more work, since the standard PHP install with Redhat 9 does not include XSLT support. In the end I had to remove my versions of PHP, apache and mysql and reinstall them along with installing expat, sablot, libxml2, zlib and curl, although not all of these are necessary. The main ones you will need are expat, sablot and libxml.
