/xslt_polyfill

A polyfill for XSLTProcessor

Primary LanguageXSLTBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

XSLT Polyfill

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.

Usage

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.

Demos

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 the XSLTProcessor polyfill, which offers JS-based XSLT processing. [Run]
  • demo.html: a polyfill "replacement" of an XML document, demo.xml. This example uses the loadXmlUrlWithXsltWhenReady() function to load demo.xml, find its contained XSL processing instruction pointing to demo.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 as demo.html, but with a more complex/realistic document. [Run] [Compare to native XML/XSLT]

Building

The build assumes several tools such as emscripten and make. But the entire polyfill can be built with one command:

$ ./build.sh

Improvements / Bugs

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!

Other Places

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:

https://chromewebstore.google.com/detail/xslt-polyfill/hlahhpnhgficldhfioiafojgdhcppklm?authuser=0&hl=en