PHP4 and XSLT Using the DOM XML ModuleThe DOM XML module remained an experimental (and somewhat interface-unstable) extension throughout the lifespan of PHP4 and has been deprecated as of PHP5. In spite of this, it remains a relatively popular module and is shipped by several operating system maintainers, including, for example, Red Hat, Inc., in its Enterprise Linux and Fedora Core operating systems. The DOM XML module is used to parse and manipulate XML input files in their entirety, as a single large data tree. For this reason, DOM XML may at times be slower or more resource intensive when transforming large XML files than the other PHP modules that can be used for XSLT transformations. Note also that the DOM XML interface described here is the post-PHP4.3.0 interfacenot the earlier interface. Sample Transformation Using PHP4 and DOM XMLThe PHP file shown in Listing 9.4 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 DOM XML extension. Listing 9.4. Sample Transformation File test-domxml.php
1 <?php
2
3 $path_xml = "freedomland.xml";
4 $path_style = "forest.xsl";
5
6 if(!$xml_doc = domxml_open_file($path_xml)) {
7 echo "Error! Unable to open " . $path_xml . "!\n";
8 exit;
9 }
10
11 if(!$stylesheet = domxml_xslt_stylesheet_file($path_style)) {
12 echo "Error! Unable to open " . $path_style . "!\n";
13 exit;
14 }
15
16 $transformed = $stylesheet->process($xml_doc);
17
18 echo $stylesheet->result_dump_mem($transformed);
19
20 ?>
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.
Using other PHP skills you have already acquired, you should be able to incorporate these tools easily into more complex scripts. DOM XML Functions and Properties of Note for XSLT UsersIn addition to the tools discussed in Listing 9.4, several additional functions or properties may be useful to PHP users needing to access XSLT transformations with the DOM XML module. These are shown in Tables 9.5 and 9.6. Additional details on these and other functions and properties related to the PHP4 DOM XML module can be found by visiting the documentation at http://www.php.net/domxml. Including XSLT Support in PHP4 via DOM XMLDepending on the operating system and PHP version you select or use, the DOM XML module and related functions and objects may or may not be present in your PHP binary. To build PHP with support for the DOM XML module and related functions and objects, you will need to do all of the following:
For additional details on compiling and installing PHP4 with DOM XML support, visit http://www.php.net/manual/en/ref.domxml.php. |