Appendix G: PHP and Extensible Stylesheet Language for Transformation


 Download CD Content

Hypertext Preprocessor (PHP) is a server-side scripting language that creates Web applications and processes eXtensible Markup Language (XML) documents using eXtensible Stylesheet Language for Transformation (XSLT). XSLT transforms XML documents into other formats, such as Hypertext Markup Language (HTML), ASCII, and Wireless Markup Language (WML). PHP uses the Sablotron XSLT processor to perform server-side transformation of XML documents.

This appendix explains how to use PHP with XSLT to transform XML documents.

Implementing XSLT with PHP

The XSLT processor combines PHP and XSLT to perform server-side XSLT transformations that convert the XML files into HTML files. PHP includes an XSLT extension that supports various XSLT processors, such as Sablotron and Xalan. The XSLT extension contains an Application Programming Interface (API) that lets you work with other processors. An API contains functions that are compatible with other XSLT engines. You can use these functions to change the XSLT processor without modifying the PHP code.

The PHP XSLT extension uses the Sablotron XSLT processor as the default processor. You need to install the Sablotron XSLT processor explicitly because it is not automatically installed with the Linux operating system. You can enable the Sablotron XSLT processor by reconfiguring and installing PHP.

To perform an XSL transformation with PHP:

  1. Include the xml and xsl files in the PHP script.

  2. Create an object of the XSLT processor, using the xslt_create() function of PHP, as shown in the following code:

     $xp=xslt_create(); 

    The above code shows that the XSLT processor is referenced by the $xp variable, and used by other XSLT functions, such as xslt_process().

  1. Process the XML source document and stylesheet using the xslt_process() function of PHP. The xslt_process() function accepts three mandatory parameters: the reference of the XSLT processor, the XML document, and the stylesheet.

    Listing G-1 shows how to use the xslt_process() function:

    Listing G-1: Using the xslt_process() Function

    start example
     if($res=xslt_process($xp, $xfile, $xsltfile)) {    echo $result; } else {    echo "Error"; } 
    end example
     

    In the above code:

    • The $xp parameter specifies the reference of the XSLT processor.

    • The $xfile parameter specifies the name of the XML document.

    • The $xsltfile parameter specifies the name of the stylesheet.

    • The xslt_process() function reads and validates the XML document against the rules specified in the stylesheet, and then generates a result tree.

    • The $res variable contains the result of the transformation.

    • The statement, echo $result, displays the result tree if the transformation is successful; otherwise , displays an error message.

  1. Release the memory occupied by the XSLT processor using the xslt_free() function. The xslt_free() function accepts a parameter that specifies the reference of the XSLT processor. The code to use the xslt_free() function is:

     xslt_free($xp); 
Note  

The xslt_process() function accepts six parameters, out of which, three parameters are optional and three are mandatory. The three optional parameters are:

  • Name of the file that stores the result tree.

  • Name of an associative array that contains buffers.

  • Name of an associative array that contains XSLT parameters.

Handling Errors

PHP XSLT extension provides functions for handling and displaying errors generated at the time of the transformation of XML documents. The xslt_error() function displays the error message, and the xslt_errno() function returns the error code. You can define a user -defined function for handling errors, using the xslt_set_error_handler() function. The syntax of the xslt_error() function is:

 xslt_error($xslt_processor); 

In the above syntax, the $xslt_processor parameter indicates the reference of the XSLT processor. The xslt_error() function accepts only a single parameter, and generates a system-defined error message. If you do not provide a parameter to the function, it displays the last error message generated in the transformation process.

The syntax of the xslt_errno() function is:

 xslt_errno($xslt_processor); 

In the above syntax, the $xslt_processor parameter indicates the reference of the XSLT processor. The xslt_errno() function accepts only a single parameter, and returns the error code corresponding to the generated error. If you do not provide a parameter to the function, the function returns the error code of the last error generated in the transformation process.

The xslt_set_error_handler() function lets you define a user-defined function for handling errors. This function accepts two parameters, the reference of the XSLT processor and the name of the user-defined function to be invoked.

The syntax of the xslt_set_error_handler() function is:

 xslt_set_error_handler($xslt_processor, "user_defined_function"); 

In the above syntax, the $xslt_processor parameter specifies the reference of the XSLT processor. The second parameter specifies the name of the function to be invoked when an error occurs in the transformation process.

The user-defined function for handling error accepts four parameters, which are:

  • The reference of the XSLT processor.

  • The error level.

  • The error code.

  • The associative array containing error information.

Logging Processor Messages

Error messages are stored in a log file, which specifies the description of errors generated in the transformation process. The xslt_set_log() file lets you initiate the logging process. You need to specify the name of the log file in the xslt_set_log() function, and invoke the xslt_set_log() function twice. The first invocation of the function specifies the initiation of the logging process, and the second invocation specifies the name of the file that stores the logging information. When you invoke the xslt_set_log() function for the first time, it accepts two parameters, which are:

  • The reference of the XSLT processor.

  • The Boolean value, true.

Enter the following code to invoke the xslt_set_log() function for the first time:

 xsl_set_log($xp, true); 

When you invoke the xslt_set_log() function for the second time, it accepts two parameters, which are:

  • The reference of the XSLT processor.

  • The name of a file that stores the log messages.

Enter the following code to invoke the xslt_set_log() function for the second time:

 xsl_set_log($xp, "/xfile.log"); 

In the above code:

  • The $xp parameter indicates the reference of the XSLT processor.

  • The xfile.log file stores the logging information.

  • The Web browser displays the logging information if you do not provide the second parameter.

  • The logging information is appended at the end of the log file if the log file exists.

Using Named Buffers

Named buffers are the references that help store XML and XSLT documents in memory. You can provide the named buffers in the fifth parameter of the xslt_process() function. You can use the named buffers of the XML and XSLT strings in place of the XML and XSLT files specified in the second and third parameters of the xslt_process() function. Enter the following code to create and use the named buffers of the XML and XSLT strings:

 $xml_str=join( 


Integrating PHP and XML 2004
Integrating PHP and XML 2004
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 51

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