Team LiB
Previous Section Next Section

PHP4 and XSLT Using the XSLT Module

The XSLT extension is perhaps the most straightforward way to perform XML to HTML transformations using PHP4. It uses the Sablotron XML toolkit to perform these tasks and is considered a stable (that is, nonexperimental) extension.

Although a few vendors ship PHP4 with XSLT module support compiled in, most users will need to add support themselves if they want to use the XSLT extension. As was the case with DOM XML, code for the XSLT extension was removed from the standard PHP distribution for PHP5.

Sample Transformation Using PHP4 and XSLT

The PHP file shown in Listing 9.5 demonstrates the simplest way to perform an XSLT transformation on the sample files shown in Listing 9.1 and Listing 9.2 using the PHP4 XSLT extension.

Listing 9.5. Sample Transformation File test-xslt.php
1   <?php
3       $path_xml = "freedomland.xml";
4       $path_style = "forest.xsl";
6       $xslt_parse = xslt_create();
7       if (!$output_html = xslt_process($xslt_parse, $path_xml, $path_style)) {
8           echo "Error using " . $path_style . " on " . $path_xml . "!\n";
9           exit;
10       }
12       xslt_free($xslt_parse);
14       echo $output_html;
16   ?>

This document is simple enough that many PHP users will understand it with little or no trouble; however, a brief walk-through will clarify its flow for those less familiar with PHP scripts.

  • Lines 1 and 16 begin and end the PHP script and should at this point need little further explanation.

  • Lines 3 and 4 create and define variables to hold the names of the input XML file and the XSLT stylesheet, respectively.

  • Line 6 creates a new XSLT processor resource that will use the Sablotron library to apply the XSLT stylesheet templates to the input XML file.

  • Line 7 calls the xslt_process function, the central function in the XSLT extension, to apply the XSLT stylesheet template to the input XML file using the processor resource created in line 6. The HTML output is returned to string variable $output_html.

  • Lines 8 and 9 display an error message and end execution if for some reason the processor resource $xslt_parse can't be accessed or the files given by $path_xml or $path_style can't be opened or accessed.

  • Line 12 frees the XSLT processor resource because the script has now finished using it.

  • Line 14 outputs the HTML contents of string variable $output_html for the Web browser.

Using other PHP skills you have already acquired, you should be able to incorporate these tools easily into more complex scripts.

XSLT Functions and Properties of Note

In addition to the tools discussed in Listing 9.5, several functions or properties may be useful to PHP users needing to access XSLT transformations with the Sablotron-based XSLT extension module. These are shown in Table 9.7.

Table 9.7. XSLT Extension Functions of Note




Creates and returns a resource associated with an XSLT processor; this processor can then be used to apply XSLT transformations.


Returns a string describing in plain text the last error that occurred on the passed XSLT processor resource.


Applies an XSLT transformation using the XSLT processor resource passed in the first argument, the XML input file passed by name in the second, and the XSLT stylesheet passed by name in the third. Returns a string containing the HTML output of the transformation.


When passed a processor resource and a Boolean value, enables or disables XSLT processor logging for the passed resource. When passed a processor resource and a filename, directs all messages about the XSLT processor resource in question (if logging is enabled) to the file in question.


Directs XSLT to call the error handling function passed in the second argument whenever an error has occurred with the XSLT processor resource passed in the first argument.


Sets the base URI for all XSLT files passed to the xslt_process() function along with the processor resource provided in the first argument to the URI provided in the second argument.

Additional details on these and other functions and properties related to the PHP4 XSLT module can be found by visiting the documentation at

Including XSLT Support in PHP4 via XSLT

If you are using the standard PHP4 distribution from on a Windows system, you may be able to enable the XSLT extension by making a change to your php.ini configuration file. To do this, open up the file with a text editor such as Notepad and find the following line:


Remove the leading semicolon on this line so that the line reads as shown:


After you have made this change, save the file and try to use the XSLT extension functions as described. If you can successfully perform XML to HTML transformations this way, you don't need to do anything further to enable the XSLT extension on your system.

If you are not a Windows user, or the technique described fails to produce working XSLT extension support in your PHP4 binary, you will need to recompile PHP4 to include support for the XSLT extension and Sablotron library. To do this, follow these steps:

Obtain the latest PHP4 source code from its home at

Ensure that you have the Sablotron XML toolkit from

Compile PHP with the following additional arguments: --with-xslt and --with-xslt-sablot.

For additional details on compiling and installing PHP4 with XSLT support, visit

    Team LiB
    Previous Section Next Section