I mentioned Kevin Davis’ experiments with Movable Type and XSLT earlier. He had asked about how to integrate his XMLized weblog with PHP’s DOMXML extension so he could transform it on the server side instead of depending on IE or Mozilla to do it on the client.
Using DOMXML to transform XML in PHP is straightforward, you read the source into a DOM object, the transform into an XSLT DOM, and apply the latter to the former. However, since you’ve already read the source into a DOM object, it’s really easy to get processing instructions of the form:
<?xml-stylesheet type="application/xsl" href="programlist.xsl"?>
out of the source document. That’s how the original version of Cocoon worked, and it’s one of the ways AxKit mapped from XML to XSLT.
Tonight I wrote a quickie PHP script to demonstrate this.
The script reads an XML file into a DOM, and then uses XPath to get a list of transforms to apply. It then applies each transform, product-wise, to the document.
The script has primitive error handling, and no caching.
Caching should be easy to add to this. Look for a file with the name of a hash of the request URI, if it’s not there then run the transform, and write out the result to the cache folder under a file name created by hashing the request URI.
The script also passes any arguments from $_GET to the transform, so you can set up parameters in your stylesheet, and get them from the URI.
Related posts: