2010-12-15 22 views
8

我想打印一個XML使用XSLT文件出來作爲一個HTML嵌套列表,而據我所知道的代碼是正確的,但我得到這個錯誤如何在PHP 5中啓用XSLT功能?

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

    ?> 

提前感謝!

+0

http://www.php.net/manual/en/xslt.installation.php – DampeS8N 2010-12-15 14:43:55

回答

7

你必須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 » http://freshmeat.net/projects/sablotron/ . 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,這樣可以在保持/否則從你的代碼。無論如何,升級你的代碼應該是微不足道的。

+0

那麼...... OP將需要重新編譯PHP? – sholsinger 2010-12-15 14:51:08

+0

@sholsinger是的,除非PHP被配置爲像上面引用的那樣,並且該擴展只是在php.ini中被禁用。但XSLT在PHP5中是PECL,所以情況不太可能如此。 – Gordon 2010-12-15 14:54:44

+0

因此請記住,我的託管與MediaTemple,這是我需要讓他們做的事情? – 2010-12-15 17:09:32

16

根據您的linux發行版,您可能只需安裝php5-xsl模塊,將「extension = php_xsl.so」行添加到您的php.ini文件中,然後重新啓動您的apache2服務器。

這在Ubuntu 11.10上適用於我。你可以在命令行輸入「sudo apt-get install php5-xsl」來安裝php5-xml。

+0

真棒,感謝爲我工作! – neildaemond 2012-11-12 15:05:13

+0

在Ubuntu 14上爲我工作。不需要添加任何內容到php.ini。 – grimurd 2015-01-21 13:50:01

+0

在Debian 8.0((jessie)64位上爲我工作,而且我也不需要在php.ini中添加任何東西。 – 2015-08-17 13:29:53

2

好吧,很簡單,但在php.net上可以更明確地解釋它。

我正在使用一個docker容器包含ubuntu base和使用php-fpm。

安裝這個擴展在我的上下文的步驟是:

在Linux庫首先搜索XSL擴展 容易緩存搜索XSL

我最終找到了PHP5,XSL,所以有人只安裝 的apt-get安裝PHP5,XSL

安裝過程已經添加安裝配置,如果不這樣做,只是讓自己 VIM /etc/php5/mods-available/xsl.ini

插入此內容: extension = xsl。所以

(明顯的路徑是根據你的PHP配置設置,但我的例子是默認配置)

重新啓動你PHP的FPM和完成!