Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

PHP4 and XSLT Using the XSLT Module
 

The XSLT extension is perhaps the most straightforward way to perform XML to HTML transformations using PHP4. It uses the Sablotron XML toolkit to perform these tasks and is considered a stable (that is, nonexperimental) extension.

Although a few vendors ship PHP4 with XSLT module support compiled in, most users will need to add support themselves if they want to use the XSLT extension. As was the case with DOM XML, code for the XSLT extension was removed from the standard PHP distribution for PHP5.

Sample Transformation Using PHP4 and XSLT


 
Listing 9.5. Sample Transformation File test-xslt.php
1   <?php
2
3 $path_xml = "freedomland.xml";
4 $path_style = "forest.xsl";
5
6 $xslt_parse = xslt_create();
7 if (!$output_html = xslt_process($xslt_parse, $path_xml, $path_style)) {
8 echo "Error using " . $path_style . " on " . $path_xml . "!\n";
9 exit;
10 }
11
12 xslt_free($xslt_parse);
13
14 echo $output_html;
15
16 ?>

This document is simple enough that many PHP users will understand it with little or no trouble; however, a brief walk-through will clarify its flow for those less familiar with PHP scripts.

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

  • Line 6 creates a new XSLT processor resource that will use the Sablotron library to apply the XSLT stylesheet templates to the input XML file.

  • Line 7 calls the xslt_process function, the central function in the XSLT extension, to apply the XSLT stylesheet template to the input XML file using the processor resource created in line 6. The HTML output is returned to string variable $output_html.

  • Lines 8 and 9 display an error message and end execution if for some reason the processor resource $xslt_parse can't be accessed or the files given by $path_xml or $path_style can't be opened or accessed.

  • Line 12 frees the XSLT processor resource because the script has now finished using it.

  • Line 14 outputs the HTML contents of string variable $output_html for the Web browser.

Using other PHP skills you have already acquired, you should be able to incorporate these tools easily into more complex scripts.

XSLT Functions and Properties of Note

In addition to the tools discussed in , several functions or properties may be useful to PHP users needing to access XSLT transformations with the Sablotron-based XSLT extension module. These are shown in .

Table 9.7. XSLT Extension Functions of Note

Function

Description

xslt_create()

Creates and returns a resource associated with an XSLT processor; this processor can then be used to apply XSLT transformations.

xslt_error()

Returns a string describing in plain text the last error that occurred on the passed XSLT processor resource.

xslt_process()

Applies an XSLT transformation using the XSLT processor resource passed in the first argument, the XML input file passed by name in the second, and the XSLT stylesheet passed by name in the third. Returns a string containing the HTML output of the transformation.

xslt_set_log()

When passed a processor resource and a Boolean value, enables or disables XSLT processor logging for the passed resource. When passed a processor resource and a filename, directs all messages about the XSLT processor resource in question (if logging is enabled) to the file in question.

xslt_set_error_handler()

Directs XSLT to call the error handling function passed in the second argument whenever an error has occurred with the XSLT processor resource passed in the first argument.

xslt_set_base()

Sets the base URI for all XSLT files passed to the xslt_process() function along with the processor resource provided in the first argument to the URI provided in the second argument.


Additional details on these and other functions and properties related to the PHP4 XSLT module can be found by visiting the documentation at .

Including XSLT Support in PHP4 via XSLT

If you are using the standard PHP4 distribution from on a Windows system, you may be able to enable the XSLT extension by making a change to your php.ini configuration file. To do this, open up the file with a text editor such as Notepad and find the following line:

;extension=php_sablot.dll

Remove the leading semicolon on this line so that the line reads as shown:

extension=php_sablot.dll

Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Natural 120
Statistic


Last Post

 
Top! Top!