繁体   English   中英

如何在PHP 5中启用XSLT功能?

[英]How do I enable XSLT functions in PHP 5?

我想使用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); 

    ?>

提前致谢!

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

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

您必须使用对它的支持来编译PHP ,并确保具有所有必需的依赖项。 请参阅PHP手册中的XSLT相应章节。

从章节要求

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

从章节安装

在Unix上,使用--enable-xslt --with-xslt-sablot选项运行configure。 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方法将返回已转换的文档或FALSE ,因此您可以保留代码中的if / else。 无论如何,升级代码应该是微不足道的。

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

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

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

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

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

安装过程中已经添加了安装配置,如果没有发生,只需自己创建vim /etc/php5/mods-available/xsl.ini

插入此内容:extension = xsl.so

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

重启你的php fpm并完成!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM