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

914

I have a FrontController made in PHP with XSLT 1.0, i have installed the Saxon/C PHP-API to work with the XSLT 2.0. But now is the Problem, i must write the FrontController around the Saxon syntax.

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

Some sample code, what i do with the XSLT 1.0.

The 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
740

Answer

Solution:

So i changed the code and now it works!

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

?>
141

Answer

Solution:

Currently, you are using PHP's XSL class to supposedly run an XSLT 2.0 script. If you really do have the Saxon/C API installed, implement this in place of PHP's XSL class.

Consider the below, untested code as example using 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);

People are also looking for solutions to the problem: php - How to find array next value based on current value

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.