PHP and XSLTAlthough there is nothing to stop you from creating an XML document and a corresponding XSLT stylesheet, you typically need to rely on a third-party XSLT processor (such as Sablotron) to combine the two. Web browsers today have limited XML parsing capabilities, and even less support for XSLT stylesheet processing; this implies that an intermediary layer, such as PHP, is required to perform the task of formatting XML data per XSLT-specified template rules. PHP 4.0.6 (and later) comes with an XSLT extension designed specifically to provide a consistent interface to different XSLT processors (Sablotron, Xalan, and so on). Created by Sterling Hughes, this XSLT extension works much like an abstraction layer, exposing a generic API that can be used to interact with any compatible XSLT processor. Because the functions defined in this API are common to most XSLT engines, it allows developers to switch between different processors without requiring any changes to the PHP code or application layer. If you're using a stock PHP binary, it's quite likely that you'll need to recompile PHP to add support for this extension to your PHP build (detailed instructions for accomplishing this are available in Appendix A, "Recompiling PHP to Add XML Support"). As of PHP 4.1.1, the XSLT extension supports only the Sablotron XSLT engine, although you can expect to see support for other XSLT engines in future PHP releases.
A Simple ExamplePHP's XSLT extension exposes a number of functions to easily transform XML documents into other formats. Together, these functions can be organized into a standard process that is applicable to all XSL Transformations performed with PHP. In order to demonstrate this process, consider the PHP script in Listing 4.1 and Listing 4.2 to carry out an XSL Transformation on the server and return the generated result tree to the browser. Listing 4.3 Performing an XSL Transformation with PHP
This is much simpler than it looks. Let's go through it step by step: The first order of business is to create an instance of the XSLT processor, which is accomplished via PHP's xslt_create() function.
The resulting handle is assigned to a variable, and it is used in calls to subsequent XSLT functions. Next, the stylesheet and XML source need to be processed. PHP offers the xslt_process() function for this purpose, which accepts three arguments: a handle for the XSLT processor, the name of the file containing the XML data, and the name of the file containing the XSLT template rules.
The xslt_process() function is the real workhorse hereit reads the XML source tree, matches it against the rules in the XSLT stylesheet, and outputs a new result tree. The results of the transformation are assigned to a variable ($result in the previous example). Note that for large or complex transformations, the process can take awhile.
Most often, you want to store and display the results of the transformation to the user. If, however, you prefer to write the result tree to a file, you can specify the filename as an optional fourth argument to xslt_process(). For an example, check out Listing 4.20. If the processing is successful, all that remains is to output the result. If it is not successful, however, the xslt_error() and xslt_errno() functions can be used to obtain error information (see the "Handling Errors" section for more on these functions). After the processing is complete, the final task is to clean things up by freeing all memory occupied by the handle:
And that's it. XSLT processing in three easy steps!
|