Faster XSL processor for PHP

We are currently using the php build of the XSL processor as a template for our web application. Which in turn uses the libxslt library. Its speed seems unsatisfactory. Is there a faster / better XSL processing engine that can be used with PHP? Or is there a way to speed up the installation of libxslt?

+7
source share
3 answers

I had the same performance issue. When I work under Windows, I wrote a class that wraps around msxml, which I get through COM. It was much faster than the PHP XSL inline transform. (Naturally, this will not help you at all if you are not using Windows.)

I will include some of my code here, with the usual caveats that I am not a PHP guru, and I do not make promises that this is something like perfect:

class xsltransform { private $xmlfilename; private $xslfilename; private $xslt; private $xslDoc; private $xmlDoc; private $xslProc; private $parameters = array(); public function __construct() { $this->xslt = new COM("Msxml2.XSLTemplate.6.0"); $this->xslDoc = new COM("Msxml2.FreeThreadedDOMDocument.6.0"); $this->xslDoc->async = false; //to allow xsl:import in xsl since security changes in MSXML 6.0 //http://msdn.microsoft.com/en-us/library/windows/desktop/ms763800(v=vs.85).aspx $this->xslDoc->resolveExternals = true; //to allow xsl:document in xsl since security changes in MSXML 6.0 $this->xslDoc->setProperty("AllowDocumentFunction", true); $this->xmlDoc = new COM("Msxml2.DOMDocument.6.0"); $this->xmlDoc->async = false; } private function loadxml() { $this->xmlDoc->load($this->xmlfilename); $this->checkParseError($this->xmlDoc, "xmlDoc, filename={$this->xmlfilename}"); } private function loadxsl() { $this->xslDoc->load($this->xslfilename); $this->checkParseError($this->xslDoc, "xslDoc, filename={$this->xslfilename}"); } private function addParameters() { foreach ($this->parameters as $name => $value) { $this->xslProc->addParameter($name, $value, ''); } } public function setxmlfilename($filename) { $this->xmlfilename = $filename; } public function setxslfilename($filename) { $this->xslfilename = $filename; } public function addProperty($name, $value) { $this->parameters[$name] = $value; } private function checkParseError($doc, $message = '') { if ($doc->parseError->errorCode) { print("XML Parse Error (" . $message . "): " . $doc->parseError->errorCode . $doc->parseError->reason); exit; } } private function loadAndTransform() { $this->loadxsl(); $this->xslt->stylesheet = $this->xslDoc; $this->xslProc = $this->xslt->createProcessor(); $this->xslProc->input = $this->xmlDoc; $this->addParameters(); $this->xslProc->transform(); } public function output() { $this->loadxml(); $this->loadAndTransform(); return $this->xslProc->output; } public function transform($xmlText) { $this->xmlDoc->loadXML($xmlText); $this->checkParseError($this->xmlDoc, "xmlDoc"); $this->loadAndTransform(); return $this->xslProc->output; } }; function xslTransform($xmlfilename, $xslfilename, $params) { $scriptPath = (dirname(__FILE__)); $xslfilenameabsolute = "{$scriptPath}'\\..\\xsl\\{$xslfilename}"; if (!file_exists($xmlfilename)) { die("{$xmlfilename} does not exists."); } if (!file_exists($xslfilenameabsolute)) { die("{$xslfilenameabsolute} does not exists."); } $xsltransform = new xsltransform(); $xsltransform->setxmlfilename($xmlfilename); $xsltransform->setxslfilename($xslfilenameabsolute); foreach($params as $key=>$param) { $xsltransform->addProperty($key, $param, ''); } return $xsltransform->output(); } 
+1
source

Using native PHP support for XSLT has low memory usage. This creates a dependency for the DOMDocument and XSLTProcessor classes, which can be found in XSL.dll for PHP 5.

0
source

Yes, PHP uses Libxslt of libxml2 , which is so popular and possibly the safest and faster (!) XSLT processor , for 15 years the XSLT1 standandard . So this is the best solution ... In the context where the XSLT community is switching to XSLT2 (very complex and slower than v1), because with the 2014 server machines, performance problems disappear.


IMPROVE the performance of your XSLT1

There are many ways to improve processing, from XML-get to XSLT-processed cache.

  • Do you use "large XML files"? What for? Today you can split into records in a SQL database with PostgreSQL and extract (with SQL and / or XPath) only the fragments that you should use,

  • Reuse (XSLT and XML) of objects converted by the DOM: see ex. method with DOMDocument .

  • Check and simplify your XSLT: as in any MVC context, MVC-View should not be used as the โ€œTuring Complete algorithmโ€, but only a resolver placeholder and loop (see T. Parr 2004 and 2008 ). So, compare performance with simpler ones.
    IMPORTANT: well, if you need "helper methods" and other fast (for example, crop !) Or complex functions in your template, do not try to use XSLT1 as XSLT2 (!), XSLT1 is not for that ... Use registerPHPFunctions instead , as shown in this tutorial .

  • Cache : The last opportunity to increase productivity. You can cache your XSLT if you use it more than once, see this good answer about XSLT caching in PHP and this other option .


Libxslt alternative to processing Libxml2 XSLT1? I do not recommend wasting time with this. But if you really need complex templates, don't waste your time with XSLT1, use XSLT2!

Solution with PHP: noise ... this is a problem, see the discussion here for a possible solution, Saxon / C , this is the only free and good XSLT2 processor that is not Java, and has a PHP port ... but is beta version (v0.3.1).

0
source

All Articles