Louis W Louis W - 11 months ago 107
PHP Question

Upgrade PHP XSLT processor to XSLT 2.0

Is it possible/easy to upgrade PHP's library to use XSLT 2.0?

Current set up:

XSL enabled
libxslt Version 1.1.24
libxslt compiled against libxml Version 2.6.32
EXSLT enabled
libexslt Version 1.1.24

Answer Source

The Saxon-C project provides a PHP API for its XSLT 2.0 implementation.

Here is the basic installation process:

Please have the following packages on your machine to build the Saxon/C PHP extension: make, php-devel, (php5-dev/php55-dev/php55w-devel), apache2 or httpd, gcc-c++ or g++, gcj (or just link the jni.h file)

Run the commands:

./configure --enable-saxon
sudo make install

Update the php.ini file (if using Ubuntu it is usually in the location '/etc/php5/apache2/') to contain the php extension. Insert the following in the Dynamic Extensions section: extension=saxon.so

Run the command:

sudo service apache2 restart

Example code:

/* simple example to show transforming to string */
 function exampleSimple1($proc, $xmlfile, $xslFile){

    $result = $proc->transformToString();               
if($result != null) {               
echo '<b/>exampleSimple1:</b/><br/>';       
echo 'Output:'.$result;
} else {
    echo "Result is null";

$foo_xml = "xml/foo.xml";
$foo_xsl = "xsl/foo.xsl";

$proc = new SaxonProcessor();

//On Windows we recommend setting the cwd using the overloaded constructor 
//because there remains an issue with building Saxon/C with PHP when using the function VCWD_GETCWD. i.e. $proc = new SaxonProcessor('C://www/html//trax//');

$version = $proc->version();
echo 'Saxon Processor version: '.$version;
echo '<br/>';        
exampleSimple1($proc, $foo_xml, $foo_xsl);

The libxslt2 and libexslt libraries, which are limited to XSLT 1.0, XPath 1.0, and EXSLT support, are used to provide the default XSLT processor for PHP. The XML_XSLT2Processor project is intended to provide an upgrade path.

Here is the basic installation process:

Follow the instructions provided on the site of the processor you want to use for instructions on how to install that XSLT processor. Basically, you'll be required to extract the processor binary in some directory.

Once you have the processor set up, you can download XML_XSLT2Processor. Using the PEAR installer

If you don't already have the PEAR installer, check the installation instructions on the PEAR site (basically, on Windows, you start the go-pear.bat file in PHP's folder, and in the typical case click "Enter" all the way), and install the PEAR installer a.k.a. the "PEAR package manager".

Once you have the PEAR installer, you can install XML_XSLT2Processor from it, by simply typing pear install path/to/the/tgz/arhive but replace the path of course. For example, if version 0.5.3 was in the same folder as the PHP folder, you can install it with the command pear install XML_XSLT2Processor_v0_5_3.tgz

Manual installation

If you don't have (access to) the PEAR installer, you can still install XML_XSLT2Processor by extracting the contents of the archive in any directory. However, it is recommended that this directory is among the paths in your include_path, which you can specify in php.ini. To more closely emulate the PEAR installer, you may also rename the "XSLT2Processor-verion" directory to "XML".


Once all of the above is done, you can create a new PHP file and include XML_XSLT2Processor in it. If you've used the PEAR installer, "XSLT2Processor.php" should be available from the "XML" folder, thus:

include "XML/XSLT2Processor.php";
//The rest of the code

You'll need the include line in the PHP file that will be using the class and it should occur before you use any of the functions in that class. The rest of the documentation will show you how to construct the XML_XSLT2Processor class, as well as explain each function's prototype and give some examples.

Note that if you've worked with the PHP XSL extension before using this one, the only thing you really must know is the XML_XSLT2Processor::__construct() function. The rest is compatible with it, though there are some new features available only here. Be aware that the registerPHPFunctions() and setProfiling() functions are not available due to the architecture of the class (not being a PECL extension and all...).