XJSLT is a substantially complete and performant XSLT 2.0 to JavaScript transpiler. XJSLT implements most features of XSLT 2 (see below for a few exceptions) and passes much of the test suite. XJSLT is written in TypeScript and depends on fontoxpath for an XPath implementation.
XJSLT works by compiling stylesheets to runnable JavaScript. These compiled stylesheets can be used immediately in the command line, or they can be save for later use in the command line. They can also be used programmatically either in the browser or in another JavaScript runtime.
XJSLT runs in javascript runtimes and on the browser. It has been tested with node and in Chrome and Firefox.
npm install -g xjslt
Or use
npx xjslt …
or from source:
git clone https://github.com/egh/xjslt.git && cd xjslt
npm exec -- xjslt run examples/jats-html.xsl examples/bmj_sample.xml
XJSLT can compile XSLT stylesheets into executable JavaScript code, which can then be deployed to various platforms that support JavaScript, including the browser, NodeJS, and potentially other JavaScript runtimes. The following are some examples of how to do this for the browser, google cloud functions, and cloudflare edge functions. Note that this compiled .js may need to be recompiled if the xjslt version changes.
For the following commands you will want to have the source checked out.
npm exec -- xjslt compile --web examples/jats-html.xsl examples/html/transform.js
- Open
examples/html/example.html(will load the generatedtransform.jsfile)
Pre-compiling a .js file will speed up transformation.
npm exec -- xjslt compile examples/jats-html.xsl
npm exec -- xjslt run transform.js examples/bmj_sample.xml
XJSLT can be used to compile XSLT into JavaScript that can be used in, for example, cloud functions. Here are two examples of how it could be used in cloud functions to dynamically transform XML data into HTML.
npm exec -- xjslt compile --standalone examples/jats-html.xsl examples/google-cloud/transform.js
cd examples/google-cloud
npm install
npx @google-cloud/functions-framework --target=transform
npm exec -- xjslt compile --standalone examples/jats-html.xsl examples/cloudflare/src/transform.js
cd examples/cloudflare
npm install
npm run start
Use compile from xjslt/compile to build a transform function directly in
Node.js without writing any files to disk. The stylesheet is passed as a parsed
document, and the returned function can be called immediately.
import * as slimdom from "slimdom";
import { readFileSync } from "fs";
import { compile } from "xjslt/compile";
const stylesheetPath = "jats-html.xsl";
const xslt = slimdom.parseXmlDocument(readFileSync(stylesheetPath, "utf-8"));
const transform = compile(xslt);
// Transform an XML document
const input = slimdom.parseXmlDocument(readFileSync("article.xml", "utf-8"));
const results = transform(input);
const resultDocument = results.get("#default");
const xml = slimdom.serializeToWellFormedString(resultDocument);
console.log(xml);npm exec webpack && cp dist/xjslt-web.js examples/html/
Open examples/html/dynamic.html in your browser.
(To be documented.)
All core features of XSLT 2.0. Roughly 50% of tests in the XSLT test suite (https://github.com/w3c/xslt30-test) pass - but many of these tests are for edge cases.
functionbasically working, with better typing TBDoutputnot all options supportednumberbasic support, not all options supported
-
analyze-string(depends on bwrrp/xspattern.js#9) - tunneled parameters
The test suite includes both unit tests and a subset of the W3C XSLT 3.0 test suite. To run tests, ensure dependencies are installed:
npm install
and then run tests:
npm test
Some functionality, including import and include, is implemented in terms of preprocessors: xslt stylesheets that are applied to the xslt stylesheet itself before it is compiled. If you make changes that impact these preprocessors, you will need to run npm run build-preprocessors to recompile them.