如何在 PHP 5 中启用 XSLT 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4451042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:04:51  来源:igfitidea点击:

How do I enable XSLT functions in PHP 5?

phpxmlxsltmediatemple

提问by Chris Armstrong

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

我想使用 xslt 将 xml 文件作为 HTML 嵌套列表打印出来,据我所知代码是正确的,但是我收到了这个错误

Fatal error: Call to undefined function xslt_create()

致命错误:调用未定义的函数 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.

我认为这意味着尚未启用 xslt 功能。如何在 PHP 中启用这些功能?是否有我需要包含的 php 文件(比如 Javascript 库的工作方式)还是更复杂的东西?我由 MediaTemple 托管。

Here's the php code I'm using:

这是我正在使用的php代码:

        <?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!

提前致谢!

采纳答案by Gordon

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

您必须在支持PHP的情况下编译 PHP,并确保具有所有必需的依赖项。请参阅XSLTPHP 手册中的相应章节。

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.

此扩展需要 libxml PHP 扩展。这意味着还需要传入 --enable-libxml,尽管这是隐式完成的,因为默认情况下启用了 libxml。这个扩展使用了 Sablotron 和 expat,它们都可以在 ? http://freshmeat.net/projects/sablotron/。提供二进制文件和源代码。在 PHP 4 中使用 --with-xslt 选项启用。

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.

在 Unix 上,使用 --enable-xslt --with-xslt-sablot 选项运行配置。Sablotron 库应该安装在编译器可以找到的地方。确保链接到 Sablotron 库的库与链接到 PHP 的库相同。配置选项:--with-expat-dir=DIR --with-iconv-dir=DIR 可帮助您指定它们。在寻求支持时,请始终提及这些指令,以及您的系统上是否安装了这些库的其他版本。当然,提供所有版本号。

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:

PHP5 中使用 XSL 转换推荐扩展是 XSL。如果您需要做的只是转换两个文档,如您的示例所示,请考虑 PHP 手册中的这个示例:

$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 transformToXMLmethod 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.

transformToXML方法将返回转换后的文档 or FALSE,因此您可以从代码中保留 if/else 。无论如何,升级您的代码应该是微不足道的。

回答by jakimfett

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.

根据您的 linux 发行版,您可以只安装 php5-xsl 模块,将“extension=php_xsl.so”行添加到 php.ini 文件中,然后重新启动 apache2 服务器。

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.

这在 Ubuntu 11.10 上对我有用。可以在命令行输入“sudo apt-get install php5-xsl”来安装php5-xml。

回答by Paulo Victor

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

嗯,很简单,但在 php.net 上可以更明确地解释它。

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

我正在使用一个包含 ubuntu 基础的 docker 容器并使用 php-fpm。

The steps to install this extension in my context were:

在我的上下文中安装此扩展的步骤是:

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

首先在linux存储库上搜索xsl扩展apt-cache search xsl

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

我最终找到了 php5-xsl,所以它只是 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

安装过程中已经添加了setup配置,如果没有,就自己做vim /etc/php5/mods-available/xsl.ini

insert this content: extension=xsl.so

插入此内容:extension=xsl.so

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

(显然路径是根据你的php配置设置,但我的例子是默认配置)

Restart you php fpm and done!

重启你的 php fpm 就完成了!