XSL 2.0 Transpiler in PHP.
Requires supported PHP version. It is installable and autoloadable via Composer as genkgo/xsl.
To run the unit tests at the command line, issue ./vendor/bin/phpunit -c phpunit.xml. This library attempts to comply with
PSR-1, PSR-2, and PSR-4. If you notice compliance oversights, please send a patch via pull request.
Replace XSLTProcessor with Genkgo\Xsl\XsltProcessor, change version="1.0" in version="2.0" and you are done!
<?phpuseGenkgo\Xsl\XsltProcessor;
useGenkgo\Xsl\Cache\NullCache;
$xslDoc = newDOMDocument();
$xslDoc->load('Stubs/collection.xsl');
$xmlDoc = newDOMDocument();
$xmlDoc->load('Stubs/collection.xml');
$transpiler = newXsltProcessor(newNullCache());
$transpiler->importStylesheet($xslDoc);
echo$transpiler->transformToXML($xmlDoc);You can also register your own extensions. Just implement the XmlNamespaceInterface and you
are ready to use your own element transformations and xpath functions. See the example below and the integration
test to understand how it works.
<?php// use omitted for readabilityclass MyExtension implements XmlNamespaceInterface {
constURI = 'https://github.com/genkgo/xsl/tree/master/tests/Stubs/Extension/MyExtension';
publicfunctionregister(TransformerCollection$transformers, FunctionCollection$functions) {
$functions->set(
self::URI, newclassextends AbstractLazyFunctionMap {
publicfunctionnewFunctionList(): array
{
return [
'hello-world' => ['newStringFunction', MyExtension::class],
];
}
}
);
}
publicstaticfunctionhelloWorld(Arguments$arguments) {
return'Hello World was called and received ' . count($arguments->unpack()) . ' arguments!';
}
}
$factory = newProcessorFactory(newNullCache(), [newMyExtension()]);
$processor = $factory->newProcessor();and then call the function in your style sheet.
<xsl:stylesheetversion="2.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:my="https://github.com/genkgo/xsl/tree/master/tests/Stubs/Extension/MyExtension">
<xsl:outputomit-xml-declaration="yes" />
<xsl:templatematch="/">
<xsl:value-ofselect="my:hello-world(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)" />
</xsl:template>
</xsl:stylesheet>will yield: Hello World was called and received 20 arguments!.
Depending on the complexity of your stylesheet, the transpiling process could slow down the processing of your
document. Therefore, you probably want to cache the result stylesheet. By adding
psr/simple-cache to your composer.json, you will add the possibility to enable caching.
See the example below, or the integration test
to see how it works.
<?phpuseGenkgo\Xsl\Cache\ArrayCache;
useGenkgo\Xsl\ProcessorFactory;
$factory = newProcessorFactory(newArrayCache());
$processor = $factory->newProcessor();- Found a bug? Please try to solve it yourself first and issue a pull request. If you are not able to fix it, at least give a clear description what goes wrong. We will have a look when there is time.
- Want to see a feature added, issue a pull request and see what happens. You could also file a bug of the missing feature and we can discuss how to implement it.