PHP4 and XSLT Using the XSLT ModuleThe 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 XSLTThe 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.php1 <?php 2 3 $path_xml = "freedomland.xml"; 4 $path_style = "forest.xsl"; 5 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 } 11 12 xslt_free($xslt_parse); 13 14 echo $output_html; 15 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.
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 NoteIn 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. Additional details on these and other functions and properties related to the PHP4 XSLT module can be found by visiting the documentation at http://www.php.net/xslt. Including XSLT Support in PHP4 via XSLTIf you are using the standard PHP4 distribution from http://www.php.net 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: ;extension=php_sablot.dll Remove the leading semicolon on this line so that the line reads as shown: extension=php_sablot.dll 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:
For additional details on compiling and installing PHP4 with XSLT support, visit http://www.php.net/manual/en/ref.xslt.php. ![]() |