如何在 PHP 5 中启用 XSLT 功能?
我想使用 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);
?>
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的 Linux 发行版,您可能只需安装 php5-xsl 模块,将“extension=php_xsl.so”行添加到 php.ini 文件中,然后重新启动 apache2 服务器即可。
这在 Ubuntu 11.10 上对我有用。您可以在命令行中输入“sudo apt-get install php5-xsl”来安装php5-xml。
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.
您必须编译 PHP 并支持它,并确保拥有所有所需的依赖项。请参阅XSLT PHP 手册中的相应章节。
来自章节要求
来自章节 安装
在 PHP5 中使用 XSL 转换的推荐扩展是 XSL。如果您需要做的只是转换两个文档(如示例所示),请考虑 PHP 手册中的示例:
transformToXML
方法将返回转换后的文档或FALSE
,所以你可以在代码中保留 if/else 。无论如何,升级代码应该很简单。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
From chapter Installation
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:
The
transformToXML
method will return the transformed document orFALSE
, so you can keep the if/else from your code. In any case, it should be trivial to upgrade your code.嗯,非常简单,但是在 php.net 上可以更明确地解释它。
我正在使用一个包含 ubuntu 基础的 docker 容器并使用 php-fpm。
在我的上下文中安装此扩展的步骤是:
首先在 Linux 存储库上搜索 xsl 扩展
apt-cache search xsl
我最终找到了 php5-xsl,所以它只是安装
apt-get install php5-xsl
说明安装过程中setup配置已经添加了,如果没有出现,就自己搞一下
vim /etc/php5/mods-available/xsl.ini
插入以下内容:
Extension=xsl.so
(显然路径是根据您的 php 配置设置,但我的示例是默认配置)
重新启动 php fpm 并完成!
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!