HomeXML Page 7 - XSL Transformation With PHP And Sablotron
Publish Or Die! - XML
So you've got your XML, and you've also got an XSLT stylesheet to format it. But how do you put the two of them together? Fear not - you can use PHP's Sablotron extension to perform XSLT transformation of XML data on the server. This article tells you how.
Now that you've seen the different techniques available to process XSLT stylesheets, here's a simple example which demonstrates a real-life use of this technology - a simple XML to HTML publishing system.
Let's assume that you have a collection of data, all neatly tagged and marked up in XML. Now, you need to use this information in different places. Some of it may need to be converted into HTML, for use on a Web site; other bits of it may need to be converted into WML, for wireless transfer, or imported into a database, or transformed into PDF documents, or...
With PHP's XSLT engine, accomplishing all this becomes a snap. All you need are separate stylesheets, each one taking care of a particular type of conversion. Feed these stylesheets to a PHP script which knows how to handle them, and Bob's your uncle.
As an example, consider the following script, which accepts XML and XSLT input and saves the results of this transformation as an HTML document:
// the fodder for the Sablotron XSLT processor
$xmlfile = "person.xml";
$htmlfile = "/www/person.html";
// create the XSLT processor
$xslthandler = xslt_create() or die("Can't create
// process the two files to get the desired output
// get result buffer
$result = xslt_fetch_result($xslthandler);
// publish the result to a static HTML file
$htmlfp = fopen($htmlfile,"w+");