PHP5 and XSLTWith the improved XML handling in PHP5, PHP4 modules like DOM XML and XSLT have been rendered obsolete, or at least deprecated. In PHP5, XSLT transformations are incorporated more cleanly into the features of its XML, DOM, and XSL extensions. Because of this, the amount of flexibility available to PHP developers in working with XML, XSL, and HTML files in PHP5 is greatly increased; all of these separate file types are manipulated with the same, more generalized toolkit. Sample Transformation Using PHP5The PHP file shown in Listing 9.6 demonstrates the simplest way to perform an XSLT transformation on the sample files shown in Listing 9.1 and Listing 9.2 using the PHP5 DOM, XML, and XSLT extensions. Listing 9.6. Sample Transformation File test-php5.php
1 <?php
2
3 $path_xml = "freedomland.xml";
4 $path_style = "forest.xsl";
5
6 $xml_obj = new DomDocument;
7 $xsl_obj = new DomDocument;
8
9 if (!$xml_obj->load($path_xml)) {
10 echo "Error! Unable to open " . $path_xml . "!\n";
11 exit;
12 }
13
14 if (!$xsl_obj->load($path_style)) {
15 echo "Error! Unable to open " . $path_style . "!\n";
16 exit;
17 }
18
19 $xslt_parse = new xsltprocessor;
20
21 $xslt_parse->importStyleSheet($xsl_obj);
22
23 echo $xslt_parse->transformToXML($xml_obj);
24
25 ?>
Although this document is simple enough that many PHP users will understand it with little or no trouble, a brief walk-through will clarify its flow for those less familiar with PHP scripts.
Again, using other PHP skills you have already acquired, you should be able to incorporate these tools easily into more complex scripts. PHP5 Functions and Properties of Note for XSLT UsersIn addition to the tools discussed in Listing 9.6, several additional functions or properties may be useful to PHP users needing to access XML documents and XSLT transformations in PHP5. These are shown in Tables 9.8 and 9.9. Additional details on these and other functions and properties related to the PHP5 DOM, XML, and XSL modules can be found by visiting the documentation for each extension at these links: Including XSL Support in PHP5The DOM and XML extension modules are built and included by default with PHP5. If you find that you are unable to use the XSL functions or properties in conjunction with these extensions, you will need to recompile your PHP5 installation to add support for XSL processing and transformations. To do so, follow these steps:
For more information on PHP5, DOM, XML, and XSL, refer to individual extension documentation and the PHP5 migration documentation and notes, all available at the official PHP website via the link at http://www.php.net/manual/en/. |