2010-12-15 12 views
8

ja chcąc wydrukować plik XML się jako zagnieżdżonej listy HTML za pomocą XSLT, io ile wiem, że kod jest poprawny, jednak dostaję ten błądJak włączyć funkcje XSLT w PHP 5?

Fatal error: Call to undefined function xslt_create()

Które Przypuszczam oznacza Funkcje xslt nie zostały włączone. Jak włączyć te funkcje w PHP? Czy istnieje plik php, który muszę dołączyć (podobnie jak działa biblioteka JavaScript), czy jest to coś bardziej skomplikowanego? Jestem hostowany z MediaTemple.

Oto kod php Używam:

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

    ?> 

Z góry dzięki!

+0

http://www.php.net/manual/en/xslt.installation.php – DampeS8N

Odpowiedz

7

Musisz compile PHP z obsługą i upewnij się, że masz wszystkie wymagane zależności. Zobacz odpowiednie rozdziały w PHP Manual for XSLT.

z rozdziału 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.

z rozdziału 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. Jeśli wszystko, co musisz zrobić, to przekształcić dwa dokumenty, jak pokazano w przykładzie, należy rozważyć ten przykład z podręcznika 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); 

Sposób transformToXML powróci przekształcony dokument lub FALSE, dzięki czemu można zachować if/jeszcze z twojego kodu. W każdym razie uaktualnienie kodu powinno być trywialne.

+0

Więc ... PO będzie musiał ponownie skompilować PHP? – sholsinger

+0

@sholsinger Tak, chyba że PHP zostało skonfigurowane jak cytowane powyżej i rozszerzenie jest wyłączone w php.ini. Ale XSLT jest PECL od PHP5, więc jest to mało prawdopodobne. – Gordon

+0

Pamiętając o tym, że mój hosting jest z MediaTemple, czy jest to coś, co muszę zrobić? –

16

W zależności od Twojej dystrybucji Linuksa możesz po prostu zainstalować moduł php5-xsl, dodać wiersz "extension = php_xsl.so" do pliku php.ini i zrestartować serwer apache2.

To działało dla mnie na Ubuntu 11.10. Możesz wprowadzić "sudo apt-get install php5-xsl" do linii poleceń, aby zainstalować php5-xml.

+0

niesamowite, dzięki dla mnie! – neildaemond

+0

Pracowałem dla mnie na ubuntu 14. Nie trzeba dodawać niczego do php.ini. – grimurd

+0

Pracowałem dla mnie na Debianie 8.0 ((jessie) 64-bitowym i nie musiałem też niczego dodawać do php.ini. –

2

Cóż, jest bardzo prosty, ale na php.net można go wyjaśnić bardziej explicite.

Używam jednego kontenera dokera zawierającego bazę ubuntu i używającego php-fpm.

Kroki, aby zainstalować to rozszerzenie w moim kontekście były:

pierwsze rozszerzenie wyszukiwania xsl na repozytorium linux apt-cache search xsl

skończyło się znalezieniem php5-xsl, więc to było zainstalować tylko apt-get install php5-xsl

że proces instalacji konfiguracja konfiguracja jest już dodane, jeśli się nie dzieje, po prostu zrobić sobie vim /etc/php5/mods-available/xsl.ini

wstaw tę treść: rozszerzenie = xsl.tak

(oczywiście ścieżki są zgodnie z ustawieniami konfiguracji php, ale mój przykład jest domyślna konfiguracja)

ponownego uruchomienia php FPM i gotowe!

Powiązane problemy