PHP5 and XSLT


With 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 PHP5

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

  • Lines 1 and 25 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.

  • Lines 6 and 7 create new DomDocument objects, capable of holding and manipulating well-formed XML documents (including XHTML and XSLT documents, which are also generally well-formed XML documents). These will be used to hold the XML input file and the XSLT stylesheet, respectively.

  • Line 9 calls the load() property of the $xml_obj DomDocument object to load the XML file given by the $path_xml variable defined in line 3.

  • Lines 10 and 11 display an error message and end execution if for some reason the XML input file can't be loaded or processed.

  • Line 14 calls the load() property of the $xsl_obj DomDocument object to load the XSLT file given by the $path_style variable defined in line 4.

  • Lines 15 and 16 display an error message and end execution if for some reason the XSLT input file can't be loaded or processed.

  • Line 19 creates a new XSLT processor resource at $xslt_parse, which can then be used for XSLT transformations.

  • Line 21 calls the importStyleSheet() property of the $xslt_parse resource to parse the XML document stored in $xsl_obj as an XSLT stylesheet in particular.

  • Line 23 calls the transformToXML() property of the $xslt_parse resource to apply the parsed XSLT stylesheet to the XML tree in $xml_obj; because the transformToXML() property returns a string in this case (the transformed HTML document), the echo command has been called to output this string to the Web browser.

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 Users

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

Table 9.8. DOM Extension Properties of Note

Function

Description

load()

Loads a well-formed XML tree from the passed XML input file into a DomDocument object.

loadXML()

Loads a well-formed XML tree from the passed string (containing XML-formatted data) into a DomDocument object.

save()

Saves the XML tree stored in a DomDocument object back into a text file under the passed path name.

validate()

Validates the document tree stored in a DomDocument object based on the document's declared XML Document Type Definition (DTD).


Table 9.9. XSL Extension Properties of Note

Function

Description

importStyleSheet()

Parses the passed XML object tree as an XSL stylesheet to be used in transforming the XML document.

transformToXML()

Uses an already imported XSL stylesheet to transform the passed XML document. Returns a string containing the transformed XML (for this book's purposes, THML) output, suitable for echo to and subsequent rendering in a Web browser.


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:

  • http://www.php.net/xsl

  • http://www.php.net/dom

  • http://www.php.net/xml

Including XSL Support in PHP5

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

1.

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

2.

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

3.

Compile the PHP5 binary using the additional configuration option --with-xsl=xslt_library_path, where xslt_library_path is the location of the XSLT library.

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



PHP 5 Unleashed
PHP 5 Unleashed
ISBN: 067232511X
EAN: 2147483647
Year: 2004
Pages: 257

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net