A polyfill for browser-based XSLTProcessor and friends
npm install xslt-polyfillThis is a polyfill for the XSLT W3C 1.0
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,) for some things.
"application/xml")
Note that the XSLT 1.0 standard is very old, and XSLT has evolved well
beyond the version shipped in web browsers. There are also more up-to-date
JS-based implementations of XSLT that support the most current standards.
One such example is SaxonJS.
You should check that out if you're looking for "real" XSLT support; this
polyfill is intended merely as a stopgap that can be used to maintain
functionality.
If you have an XML document that contains an XSL processing instruction, like
this:
``xml`
Hello World.
You can convert it to use this polyfill by simply adding the polyfill directly
to the XML, like this (note the new
Hello World.
`
This will automatically load the XML and XSLT, process it with this
XSLT polyfill, and replace the page with the result of the transformation.
The example above is available in the test/ folder of this repo:demo.xml.
The polyfill also provides a full implementation of the XSLTProcessor class,
so that code like this will also work:
`html`
The example above is available in the test/ folder of this repo:XSLTProcessor_example.html.
Note that as of now, there are a few things that don't work perfectly:
- The output of the transformation is assumed to be HTML in a few places.
If the output is something else, like text or XML, things will likely break.
- The parseAndReplaceCurrentXMLDoc() function will replace the contents ofXHTML
the current document (an document) with the transformed content.
Because XHTML always renders in no-quirks mode, if the transformed (HTML)
output content doesn't include a , then it ordinarily would
have rendered in quirks mode, which is different.
- Scripts in the transformed output must be "copied" to fresh