How do I enable XSLT functions in PHP 5?

29,582

Solution 1

You have to compile PHP with the support for it and make sure to have all the required dependencies. Please see the corresponding chapters in the PHP Manual for XSLT.

From chapter Requirements

This extension requires the libxml PHP extension. This means that passing in --enable-libxml is also required, although this is implicitly accomplished because libxml is enabled by default. This extension uses Sablotron and expat, which can both be found at » http://freshmeat.net/projects/sablotron/. Binaries are provided as well as source. Enable by using the --with-xslt option with PHP 4.

From chapter Installation

On Unix, run configure with the --enable-xslt --with-xslt-sablot options. The Sablotron library should be installed somewhere your compiler can find it. Make sure you have the same libraries linked to the Sablotron library as those, which are linked with PHP. The configuration options: --with-expat-dir=DIR --with-iconv-dir=DIR are there to help you specify them. When asking for support, always mention these directives, and whether there are other versions of those libraries installed on your system somewhere. Naturally, provide all the version numbers.

The recommended extension for using XSL transformations with PHP5 is XSL. If all you need to do is transform two documents, as show in your example, consider this example from the PHP Manual:

$xml = new DOMDocument;
$xml->load('collection.xml');

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules

echo $proc->transformToXML($xml);

The transformToXML method will return the transformed document or FALSE, so you can keep the if/else from your code. In any case, it should be trivial to upgrade your code.

Solution 2

Depending on your distro of linux, you may be able to just install the php5-xsl module, add the line "extension=php_xsl.so" to your php.ini file, and restart your apache2 server.

This worked for me on Ubuntu 11.10. You can input "sudo apt-get install php5-xsl" into the command line to install php5-xml.

Solution 3

Well, is very simple, but on php.net its could be explained more explicitly.

I'm using one docker container contain ubuntu base and using php-fpm.

The steps to install this extension in my context were:

First search xsl extension on linux repository apt-cache search xsl

I ended up finding the php5-xsl, so it was only install apt-get install php5-xsl

that the installation process the setup configuration is already added, if does not happen, just make yourself vim /etc/php5/mods-available/xsl.ini

insert this content: extension=xsl.so

(obviously the paths are according to your php configuration settings, but my example is the default configuration)

Restart you php fpm and done!

Share:
29,582
Chris Armstrong
Author by

Chris Armstrong

Updated on October 15, 2020

Comments

  • Chris Armstrong
    Chris Armstrong over 3 years

    I'm wanting to print an xml file out as an HTML nested list using xslt, and as far as I know the code is correct, however I'm getting this error

    Fatal error: Call to undefined function xslt_create()

    Which I presume means the xslt functions havn't been enabled. How do I enable these functions within PHP? Is there a php file I need to include (like the way Javascript libraries work) or is it something more complicated? I'm hosted with MediaTemple.

    Here's the php code I'm using:

            <?php 
    
        // Allocate a new XSLT processor 
        $xh = xslt_create(); 
    
        // Process the document, returning the result into the $result variable 
        $result = xslt_process($xh, 'armstrong.xml', 'familyToNestedList.xsl'); 
        if ($result) { 
            print "SUCCESS, sample.xml was transformed by sample.xsl into the \$result"; 
            print " variable, the \$result variable has the following contents\n<br>\n"; 
            print "<pre>\n"; 
            print $result; 
            print "</pre>\n"; 
        } 
        else { 
            print "Sorry, sample.xml could not be transformed by sample.xsl into"; 
            print "  the \$result variable the reason is that " . xslt_error($xh) .  
            print " and the error code is " . xslt_errno($xh); 
        } 
    
        xslt_free($xh); 
    
        ?>
    

    Thanks in advance!

  • sholsinger
    sholsinger over 13 years
    So... the OP will need to re-compile PHP?
  • Gordon
    Gordon over 13 years
    @sholsinger Yes, unless PHP was configured like quoted above already and the extension just being disabled in php.ini. But XSLT is PECL as of PHP5, so it's unlikely this is the case.
  • Chris Armstrong
    Chris Armstrong over 13 years
    So bearing in mind that my hosting is with MediaTemple, is this something I'll need to get them to do?
  • Gordon
    Gordon over 13 years
    @Chris I assume this is shared hosting then with no root access to the machine. You can ask them, but I doubt they'll do it for shared hosting setups. If the code you show is not just one simplified example of many more lines of code, I strongly encourage you to change the code to use the new extension.
  • Chris Armstrong
    Chris Armstrong over 13 years
    I tried the sample of the new extension that you gave me, and it's throwing up several errors (you can see at chris-armstrong.com/gortin ). I'm not sure whether they're errors in the php or in the xml/xslt (so not sure which I should be trying to fix)
  • Gordon
    Gordon over 13 years
    @Chris these are errors reported by libxml about invalid markup. You can disable them by using libxml_use_internal_errors(TRUE) and libxml_clear_errors. However, it is better to correct the XML. In case of armstrong.xml, there is whitespace before the xml prolog. In the case of the XSL, the error messages are quite obvious I think. They seem to stem from the orphaned closing li element.
  • grimurd
    grimurd over 9 years
    Worked for me on ubuntu 14. Didn't need to add anything to php.ini.
  • Aldo Paradiso
    Aldo Paradiso almost 9 years
    Worked for me on Debian 8.0 ( (jessie) 64-bit, and I did not need to add anything to php.ini as well.