How to enable XSLT functions in PHP 5?

I want to print the xml file as a nested HTML list using xslt, and as far as I know, the code is correct, however I get this error

Fatal error: function call undefined xslt_create ()

I assume xslt functions were not included. How to include these functions in PHP? Is there a php file that I need to include (for example, how Javascript libraries work), or is it something more complicated? I am hosted by 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!

+8
xml php xslt mediatemple
source share
3 answers

You must compile PHP with its support and be sure to have all the necessary dependencies. See Related Chapters in the PHP Manual for XSLT .

From the chapter Requirements

This extension requires the libxml PHP extension. This means that you also need to pass inenable-libxml, although this is implicitly executed because libxml is enabled by default. This extension uses Sablotron and expat, which can be found on the page " http://freshmeat.net/projects/sablotron/ . It also provides binaries as well as the source. Enable using the -with-xslt option with PHP 4.

From the Installation chapter

On Unix, run configure with the options --enable-xslt --with-xslt-sablot. The Sablotron library must be installed somewhere your compiler can find it. Make sure you have the same libraries that are associated with the Sablotron library, like those that are associated with PHP. Configuration options: --with-expat-dir = DIR --with-iconv-dir = DIR to help you specify them. When requesting support, always indicate these directives and whether there are other versions of these libraries installed on your system somewhere. Naturally, include all version numbers.

The recommended extension for using XSL transforms with PHP5 is XSL . If all you have to do is convert two documents as shown in your example, consider this example in 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 a converted document or FALSE so that you can save if / else from your code. In any case, updating the code should be trivial.

+7
source share

Depending on your Linux distribution, you can simply install the php5-xsl module, add the line "extension = php_xsl.so" to your php.ini file and restart the apache2 server.

This worked for me on Ubuntu 11.10. You can enter "sudo apt-get install php5-xsl" at the command prompt to install php5-xml.

+16
source share

Well, very simple, but on php.net it can be explained more explicitly.

I use one docker container containing the ubuntu base and use php-fpm.

The steps to install this extension in my context:

First search for xsl extension in linux repository apt-cache xsl search

As a result, I found php5-xsl, so it was installed only apt-get install php5-xsl

that the process of setting configuration settings has already been added, if this does not happen, just do vim / etc / php5 / mods-available / xsl.ini

paste this content: extension = xsl.so

(obviously the paths match your php configuration settings, but my example is the default configuration)

Restart php fpm and finish!

+2
source share

Source: https://habr.com/ru/post/650164/


All Articles