8

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

致命错误:调用未定义函数 xslt_create()

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

这是我正在使用的 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); 

    ?>

提前致谢!

4

3 回答 3

16

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

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

于 2012-04-25T17:56:02.460 回答
7

您必须编译 PHP并支持它,并确保具有所有必需的依赖项。请参阅PHP Manual for XSLT中的相应章节。

来自章节要求

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

从章节安装

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

在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);

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

于 2010-12-15T14:43:30.707 回答
2

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

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

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

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

我最终找到了 php5-xsl,所以它只是 install apt-get install php5-xsl

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

插入此内容:extension=xsl.so

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

重新启动你的php fpm并完成!

于 2014-10-14T10:01:24.543 回答