This is a polyfill for the XSLT W3C 1.0 web standard, roughly as shipped in
the Chrome browser. It is intended as a replacement for using the XSLT
processing machinery built into browsers. It does still rely on the XML
parsing machinery (e.g. parseFromString(xmlText, "application/xml")) for
some things.
If you have an XML document that contains an XSL processing instruction, like this:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="demo.xsl"?>
<page>
<message>
Hello World.
</message>
</page>You can convert it to use this polyfill by generating a new HTML file like this:
<!DOCTYPE html>
<script src="xslt-polyfill.min.js"></script>
<script>
window.loadXmlUrlWithXsltWhenReady('./demo.xml');
</script>This will load the XML file, look for an XML stylesheet, process it with this XSLT polyfill, and replace the page with the result of the transformation.
There are a few demos in the test/ directory, both the XML/XSL source files
and the corresponding .html files that use the polyfill to load and process
the XSLT. In particular:
basic_example.html: a test of theXSLTProcessorpolyfill, which offers JS-based XSLT processing. [Run]demo.html: a polyfill "replacement" of an XML document,demo.xml. This example uses theloadXmlUrlWithXsltWhenReady()function to loaddemo.xml, find its contained XSL processing instruction pointing todemo.xsl, load that file, and then process them together, replacing the document. [Run] [Compare to native XML/XSLT]demo_large.html: a much larger example, taken from a public site, which does the same asdemo.html, but with a more complex/realistic document. [Run] [Compare to native XML/XSLT]
The build assumes several tools such as emscripten and make. But the entire polyfill can be built with one command:
$ ./build.sh
If you find issues with the polyfill, feel free to file them here. Even better, if you would like to contribute to this polyfill, I'm happy to review pull requests. Thanks in advance!
This polyfill is also published on npm:
It is also incorporated into a Chrome extension, which automatically applies the polyfill to raw XML files that contain XSLT stylesheets: