如何使用 Saxon/C PHP-API 进行 xslt 转换

How to make a xslt-tranformation with Saxon/C PHP-API

我有一个使用 XSLT 1.0 在 PHP 中制作的 FrontController,我已经安装了 Saxon/C PHP-API 以使用 XSLT 2.0。但现在是问题所在,我必须围绕 Saxon 语法编写 FrontController。

$xmlDoc = new DOMDocument();
$xslDoc = new DOMDocument();
$proc = new XSLTProcessor();
$xslDoc->load("../ressources/xslt/transform_article_partials_to_html.xslt");

$dir = __DIR__ . "/articles/" . $lang;
$filename = "{$rubrik}_{$seite}.html";
$filelist = scandir($dir, 1);

$proc->setParameter ("" , "articlePfad" , $filename);
$proc->setParameter("", "lang", $lang);

if($seite == null) {
  $filename = "{$rubrik}.html";
  if(in_array($filename, $filelist)) {
    $xmlDoc->load($dir . "/" . $filename);
  } else {
    echo "404";
  }
} else if(isset($seite) && isset($rubrik)){
  if(in_array($filename, $filelist)) {
    $xmlDoc->load($dir . "/" . $filename);
  } else {
    echo "404";
  }
}

$proc->importStylesheet($xslDoc);
echo $proc->transformToXML($xmlDoc);

一些示例代码,我用 XSLT 1.0 做了什么。

Apache2 Error.log:

        PHP Warning:  PHP Startup: Unable to load dynamic library 'xsl' (tried: /usr/lib/php/20170718/xsl (/usr/lib/php/20170718/xsl: cannot open shared object file: No such file or directory), /usr/lib/php/20170718/xsl.so (/usr/lib/php/20170718/xsl.so: undefined symbol: dom_node_class_entry)) in Unknown on line 0
PHP Warning:  Module 'Saxon/C' already loaded in Unknown on line 0
[Thu Jun 20 00:40:38.128672 2019] [mpm_prefork:notice] [pid 892] AH00163: Apache/2.4.29 (Ubuntu) configured -- resuming normal operations
[Thu Jun 20 00:40:38.130224 2019] [core:notice] [pid 892] AH00094: Command line: '/usr/sbin/apache2'
[Thu Jun 20 00:40:54.815341 2019] [php7:notice] [pid 2692] [client 127.0.0.1:54472] PHP Notice:  Undefined variable: saxonProc in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:40:54.815512 2019] [php7:error] [pid 2692] [client 127.0.0.1:54472] PHP Fatal error:  Uncaught Error: Call to a member function newXsltProcessor() on null in /var/www/html/Comelio Website/comelio_com/FrontController.php:5\nStack trace:\n#0 /var/www/html/Comelio Website/comelio_com/index.php(3): require()\n#1 {main}\n  thrown in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:40:55.935485 2019] [php7:notice] [pid 2691] [client 127.0.0.1:54474] PHP Notice:  Undefined variable: saxonProc in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:40:55.935580 2019] [php7:error] [pid 2691] [client 127.0.0.1:54474] PHP Fatal error:  Uncaught Error: Call to a member function newXsltProcessor() on null in /var/www/html/Comelio Website/comelio_com/FrontController.php:5\nStack trace:\n#0 /var/www/html/Comelio Website/comelio_com/index.php(3): require()\n#1 {main}\n  thrown in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:44:39.915201 2019] [php7:notice] [pid 2719] [client 127.0.0.1:54508] PHP Notice:  Undefined variable: saxonProc in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5, referer: http://localhost/Comelio%20Website/
[Thu Jun 20 00:44:39.915301 2019] [php7:error] [pid 2719] [client 127.0.0.1:54508] PHP Fatal error:  Uncaught Error: Call to a member function newXsltProcessor() on null in /var/www/html/Comelio Website/comelio_com/FrontController.php:5\nStack trace:\n#0 /var/www/html/Comelio Website/comelio_com/index.php(3): require()\n#1 {main}\n  thrown in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5, referer: http://localhost/Comelio%20Website/
[Thu Jun 20 00:44:47.966250 2019] [php7:notice] [pid 2692] [client 127.0.0.1:54510] PHP Notice:  Undefined variable: saxonProc in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:44:47.966297 2019] [php7:error] [pid 2692] [client 127.0.0.1:54510] PHP Fatal error:  Uncaught Error: Call to a member function newXsltProcessor() on null in /var/www/html/Comelio Website/comelio_com/FrontController.php:5\nStack trace:\n#0 /var/www/html/Comelio Website/comelio_com/index.php(3): require()\n#1 {main}\n  thrown in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:50:13.520374 2019] [php7:notice] [pid 2691] [client 127.0.0.1:54528] PHP Notice:  Undefined variable: saxonProc in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:50:13.520447 2019] [php7:error] [pid 2691] [client 127.0.0.1:54528] PHP Fatal error:  Uncaught Error: Call to a member function newXsltProcessor() on null in /var/www/html/Comelio Website/comelio_com/FrontController.php:5\nStack trace:\n#0 /var/www/html/Comelio Website/comelio_com/index.php(3): require()\n#1 {main}\n  thrown in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:50:14.887942 2019] [php7:notice] [pid 2806] [client 127.0.0.1:54530] PHP Notice:  Undefined variable: saxonProc in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:50:14.888235 2019] [php7:error] [pid 2806] [client 127.0.0.1:54530] PHP Fatal error:  Uncaught Error: Call to a member function newXsltProcessor() on null in /var/www/html/Comelio Website/comelio_com/FrontController.php:5\nStack trace:\n#0 /var/www/html/Comelio Website/comelio_com/index.php(3): require()\n#1 {main}\n  thrown in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:50:15.847351 2019] [php7:notice] [pid 2693] [client 127.0.0.1:54532] PHP Notice:  Undefined variable: saxonProc in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:50:15.874397 2019] [php7:error] [pid 2693] [client 127.0.0.1:54532] PHP Fatal error:  Uncaught Error: Call to a member function newXsltProcessor() on null in /var/www/html/Comelio Website/comelio_com/FrontController.php:5\nStack trace:\n#0 /var/www/html/Comelio Website/comelio_com/index.php(3): require()\n#1 {main}\n  thrown in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:50:16.475331 2019] [php7:notice] [pid 2690] [client 127.0.0.1:54534] PHP Notice:  Undefined variable: saxonProc in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 00:50:16.475456 2019] [php7:error] [pid 2690] [client 127.0.0.1:54534] PHP Fatal error:  Uncaught Error: Call to a member function newXsltProcessor() on null in /var/www/html/Comelio Website/comelio_com/FrontController.php:5\nStack trace:\n#0 /var/www/html/Comelio Website/comelio_com/index.php(3): require()\n#1 {main}\n  thrown in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 02:09:30.155871 2019] [php7:notice] [pid 2719] [client 127.0.0.1:54596] PHP Notice:  Undefined variable: saxonProc in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5
[Thu Jun 20 02:09:30.156196 2019] [php7:error] [pid 2719] [client 127.0.0.1:54596] PHP Fatal error:  Uncaught Error: Call to a member function newXsltProcessor() on null in /var/www/html/Comelio Website/comelio_com/FrontController.php:5\nStack trace:\n#0 /var/www/html/Comelio Website/comelio_com/index.php(3): require()\n#1 {main}\n  thrown in /var/www/html/Comelio Website/comelio_com/FrontController.php on line 5

目前,您正在使用 PHP 的 XSL class 据推测 运行 XSLT 2.0 脚本。如果您确实安装了 Saxon/C API,请实施它以代替 PHP 的 XSL class。

考虑以下未经测试的代码作为示例,使用 online docs:

// INITIALIZE PROCESSOR
$saxonproc = new Saxon\SaxonProcessor();
$xsltProc = $saxonProc->newXsltProcessor();

// LOAD XSLT SCRIPT
$xslFile = "../ressources/xslt/transform_article_partials_to_html.xslt";
$xsltProc->compileFromFile($xslFile); 

// SET PARAMS
$dir = __DIR__ . "/articles/" . $lang;
$filename = "{$rubrik}_{$seite}.html";
$filelist = scandir($dir, 1);

$xdmvalue = $xsltProc->createAtomicValue($filename);
$xsltProc->setParameter("articlePfad", $xdmvalue);

$xdmvalue = $xsltProc->createAtomicValue($lang);
$xsltProc->setParameter("lang", $xdmvalue);

// LOAD SOURCE XML
if($seite == null) {
  $filename = "{$rubrik}.html";
  if(in_array($filename, $filelist)) {
    $xmlFile = $dir . "/" . $filename;    
  } else {
    echo "404";
    exit;
  }
} else if(isset($seite) && isset($rubrik)){
  if(in_array($filename, $filelist)) {
    $xmlFile = $dir . "/" . $filename;
  } else {
    echo "404";
  }
}

$xsltProc->setSourceFromFile($xmlFile);

// RUN TRANSFORMATION    
$result = $xsltProc->transformToString();

echo $result;

// RELEASE RESOURCES
$xsltProc->clearParameters();
$xsltProc->clearProperties();

unset($xsltProc);
unset($saxonproc);

所以我更改了代码,现在可以使用了!

<?php

// INITIALIZE PROCESSOR
$saxonProc = new Saxon\SaxonProcessor();
$xsltProc = $saxonProc->newXsltProcessor();

$lang = $_GET['lang'];
$rubrik = $_GET['rubrik'];
$seite = $_GET['seite'];

// LOAD XSLT SCRIPT
$xslFile = "../ressources/xslt/transform_article_partials_to_html.xslt";
$xsltProc->compileFromFile($xslFile); 

// SET PARAMS
$dir = __DIR__ . "/articles/" . $lang;
$filename = "{$rubrik}_{$seite}.html";
$filelist = scandir($dir, 1);

$xdmvalue = $saxonProc->createAtomicValue($filename);
$xsltProc->setParameter("articlePfad", $xdmvalue);

$xdmvalue = $saxonProc->createAtomicValue($lang);
$xsltProc->setParameter("lang", $xdmvalue);

// LOAD SOURCE XML
if($seite == null) {
  $filename = "{$rubrik}.html";
  if(in_array($filename, $filelist)) {
    $xmlFile = $dir . "/" . $filename;
    $xsltProc->setSourceFromFile($xmlFile);
  } else {
    echo "404";
    exit;
  }
} else if(isset($seite) && isset($rubrik)){
  if(in_array($filename, $filelist)) {
    $xmlFile = $dir . "/" . $filename;
    $xsltProc->setSourceFromFile($xmlFile);
  } else {
    echo "404";
  }
}

// RUN TRANSFORMATION    
$result = $xsltProc->transformToString();

echo $result;

// RELEASE RESOURCES
$xsltProc->clearParameters();
$xsltProc->clearProperties();

unset($xsltProc);
unset($saxonProc);

?>