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

前端之家收集整理的这篇文章主要介绍了如何在PHP 5中启用XSLT功能?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用xslt将xml文件打印为 HTML嵌套列表,据我所知,代码是正确的,但是我收到此错误

Fatal error: Call to undefined function 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); 

    ?>

提前致谢!

您需要 compile PHP支持它,并确保拥有所有必需的依赖项.请参阅 PHP Manual for XSLT中的相应章节.

从第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 » 07003. Binaries are provided as well as source. Enable by using the –with-xslt option with PHP 4.

从第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.

recommended extension for using XSL transformations with PHP5 is 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相关文章