WARNING: This project is in alpha state, and as such, does not have any stable release and has partial support. Use at your own risk, but feel free to contribute via pull requests, bug issues or feature requests.
XSLT2Processor is a pure PHP library for processing XSLT stylesheets up to version 2.0 and XPath expressions. It offers support for:
- XPath syntax parsing, evaluation and query
- Standard XPath function library and EXSLT function library
- XSLT v2.0 stylesheets execution
This library requires:
- PHP 5.6 or greater
- PHP Extension mbstring
- PHP Extension xml
- PHP Extension xsl
To install the library using composer, execute the following line in your favourite terminal, at the root of your project:
$ php composer.phar require jdomenechb/xslt2processor
To use XSLT2Processor in your code:
$processor = new Jdomenechb\XSLT2Processor\XSLT\Processor($xslt, $xml);
$result = $processor->transformXML();
$xslt
and $xml
are DOMDocument
objects, each one containing the XSLT stylesheet and the XML to be transformed, respectivelly.
To speed up the transformation, a caching class following the PSR-6 recommendation CacheItemPoolInterface
can be provided to the processor to avoid parsing again expressions that have already been parsed. Note that it should be provided before processing.
$processor->setCache($cacheItemPool);
If no cache is provided anyway, the XPath factory has an internal memory-based cache to reuse XPaths already parsed in the current template. Note that although this can be faster, this option can use more memory.
The full list of available XPath classes can be found under the XPath src folder. But generally, a developer might be more interested in parsing a string that represents an XPath. For this goal, the Factory
class may be more useful:
$factory = new Jdomenechb\XSLT2Processor\XPath\Factory;
$xPath = $factory->create('/*/some/x-path[representation = 1]');
Once you have parsed or build your xPath, you can evaluate or query the expression by executing:
// Evaluates the expression
$evaluationResult = $xPath->evaluate($context);
// Queries the nodes represented by the expression
$queryResult = $xPath->query($context);
$content
can be any element extending the PHP class DOMNode
(http://php.net/manual/en/class.domnode.php).