Team LiB
Previous Section Next Section

PHP4 and XSLT Using the DOM XML Module

The 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 XML

The 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.

  • Lines 1 and 20 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. Note that on Windows platforms, these must be absolute pathnames, although on other platforms they can be relative (as shown).

  • Line 6 attempts to create a new DomDocument object, $xml_doc, that will contain and operate on the XML tree in the sample XML file.

  • Lines 7 and 8 display an error message and end execution if the XML file can't be opened or parsed or the object $xml_doc can't be created.

  • Line 11 attempts to create a new DomXsltStylesheet object, $stylesheet, that will contain and operate on the XML tree in the sample XSLT stylesheet file.

  • Lines 12 and 13 display an error message and end execution if the XSLT file can't be opened or parsed or if the object $stylesheet can't be created.

  • Line 16 applies the XSLT transformation on the $xml_doc object tree using the stylesheet contained in the $stylesheet object tree; the results of the transformation are held in a new object, $transformed.

  • Line 18 uses the $stylesheet object tree to dump the results of the transformation held in $transformed back into a string containing HTML data; this string is then output by echo.

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 Users

In 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.

Table 9.5. DomDocument Functions and Properties of Note

Property

Description

domxml_open_file()

Returns a new object holding the entire object tree of the supplied XML file.

domxml_open_mem()

Returns a new object holding the entire object tree of the XML data contained in the supplied string.


Table 9.6. DomXsltStylesheet Functions and Properties of Note

Property

Description

domxml_xslt_stylesheet_file()

Returns a new object containing the XSLT transformation instructions in the supplied file.

domxml_xslt_stylesheet_doc()

Returns a new object containing the XSLT transformation instructions in the supplied DomDocument XML object tree.

domxml_xslt_stylesheet()

Returns a new object containing the XSLT transformation instructions from the XSLT object tree contained in the supplied string.

process()

Returns a new object containing the transformed data from the supplied DomDocument XML object tree.

result_dump_mem()

Returns a new string containing the HTML data resulting from the supplied transformation.

result_dump_file()

Returns a new string containing the HTML data resulting from the supplied transformation; also creates a new file containing this string, using the filename given as its second argument.


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 XML

Depending 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:

  • Obtain the latest PHP4 source code from its home at http://www.php.net.

  • Ensure that you have the GNOME XML library (libxml) from http://www.xmlsoft.org.

  • Ensure that you have the XSLT library (libxslt) from http://www.xmlsoft.org/XSLT.

  • Ensure that you have the EXSLT extensions from http://www.exslt.org.

  • Compile PHP with the following additional arguments: --with-dom=path-to-dom (usually found in the ext directory of the source tree), --with-xslt=path-to-xslt, and --with-exslt=path-to-exslt.

For additional details on compiling and installing PHP4 with DOM XML support, visit http://www.php.net/manual/en/ref.domxml.php.

    Team LiB
    Previous Section Next Section