XSLT-processor
A JavaScript XSLT processor without native library dependencies.
How to
Install xslt-processor using npm or yarn:
npm install xslt-processor
yarn add xslt-processor
Within your ES2015+ code, import the Xslt
class, the xmlParse
function and use this way:
import { Xslt, xmlParse } from 'xslt-processor'
const xslt = new Xslt();
const outXmlString = xslt.xsltProcess(
xmlParse(xmlString),
xmlParse(xsltString)
);
To access the XPath parser, you can use the instance present at Xslt
class:
const xslt = new Xslt();
const xPath = xslt.xPath;
Or ou can import it like this:
import { XPath } from 'xslt-processor'
const xPath = new XPath();
If you write pre-2015 JS code, make adjustments as needed.
Xslt
class options
You can pass an options
object to Xslt
class:
const options = {
escape: false,
selfClosingTags: true,
parameters: [{ name: 'myparam', value: '123' }]
};
const xslt = new Xslt(options);
escape
(boolean
, default true
): replaces symbols like <
, >
, &
and "
by the corresponding XML entities.selfClosingTags
(boolean
, default true
): Self-closes tags that don't have inner elements, if true
. For instance, <test></test>
becomes <test />
.parameters
(array
, default []
): external parameters that you want to use.
name
: the parameter name;namespaceUri
(optional): the namespace;value
: the value.
Direct use in browsers
You can simply add a tag like this:
<script type="application/javascript" src="https://www.unpkg.com/xslt-processor@1.2.0/umd/xslt-processor.js"></script>
All the exports will live under globalThis.XsltProcessor
. See a usage example here.
Breaking Changes
Until version 0.11.7, use like the example below:
import { xsltProcess, xmlParse } from 'xslt-processor'
const outXmlString = xsltProcess(
xmlParse(xmlString),
xmlParse(xsltString)
);
and to access the XPath parser:
import { xpathParse } from 'xslt-processor'
These functions are part of Xslt
and XPath
classes, respectively, at version 1.x onward.
Introduction
XSLT-processor contains an implementation of XSLT in JavaScript. Because XSLT uses
XPath, it also contains an implementation of XPath that can be used
independently of XSLT. This implementation has the advantage that it
makes XSLT uniformly available whenever the browser's native XSLTProcessor()
is not available such as in Node.js or in web workers.
XSLT-processor builds on Google's AJAXSLT
which was written before XSLTProcessor()
became available in browsers, but the
code base has been updated to comply with ES2015+ and to make it work outside of
browsers.
This implementation of XSLT operates at the DOM level on its input
documents. It internally uses a DOM implementation to create the
output document, but usually returns the output document as text
stream. The DOM to construct the output document can be supplied by
the application, or else an internal minimal DOM implementation is
used. This DOM comes with a minimal XML parser that can be used to
generate a suitable DOM representation of the input documents if they
are present as text.
Tests and usage examples
New tests are written in Jest an can be run by calling: npm test
.
The files xslt.html
and xpath.html
in the directory interactive-tests
are interactive tests. They can be run directly from the file system; no HTTP server is needed.
Both interactive tests and automatic tests demonstrate the use of the library functions. There is not much more documentation so far.
Conformance
A few features that are required by the XSLT and XPath standards were left out (but patches to add them are welcome).
See our TODO for a list of missing features that we are aware of (please add more items by means of PRs).
Issues are also marked in the source code using throw-statements.
The DOM implementation is minimal so as to support the XSLT processing, and not intended to be complete.
The implementation is all agnostic about namespaces. It just expects
XSLT elements to have tags that carry the xsl:
prefix, but we disregard all namespace declaration for them.
There are a few nonstandard XPath functions. Grep xpath.js
for ext-
to see their definitions.
References