xjslt
v1.1.0
Published
xslt 2 transpiler to javascript
Maintainers
Readme
XJSLT - An XSLT to JavaScript transpiler
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 runs faster than SaxonJS in our experiments.
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.
Getting started
Installation:
npm install -g xjsltOr use
npx xjslt …or from source:
git clone https://github.com/egh/xjslt.git && cd xjsltCommand line invocation:
npm exec -- xjslt run examples/jats-html.xsl examples/bmj_sample.xmlCompilation examples
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.
In the browser
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)
For reuse in the command line
Pre-compiling a .js file will speed up transformation.
npm exec -- xjslt compile examples/jats-html.xslnpm exec -- xjslt run transform.js examples/bmj_sample.xmlExamples of use in cloud functions
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.
In a google cloud function
npm exec -- xjslt compile --standalone examples/jats-html.xsl examples/google-cloud/transform.jscd examples/google-cloudnpm installnpx @google-cloud/functions-framework --target=transform- Visit http://localhost:8080/?url=https://jats.nlm.nih.gov/publishing/tag-library/1.1/FullArticleSamples/bmj_sample.xml
In a cloudflare edge function
npm exec -- xjslt compile --standalone examples/jats-html.xsl examples/cloudflare/src/transform.jscd examples/cloudflarenpm installnpm run start- Visit http://localhost:8787/?url=https://jats.nlm.nih.gov/publishing/tag-library/1.1/FullArticleSamples/bmj_sample.xml
Programmatic API (Node.js)
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);Dynamic (not precompiled) use
xjslt can be used dynamically as well - that is, not precompiled. While precompiling the stylesheet is faster, this may be helpful in some circumstances.
In the browser
npm exec webpack && cp dist/xjslt-web.js examples/html/Open examples/html/dynamic.html in your browser.
In node
(To be documented.)
Supported features
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.
Incompletely supported features
functionbasically working, with better typing TBDoutputnot all options supportednumberbasic support, not all options supported
TODO (not yet implemented)
- [ ]
analyze-string(depends on https://github.com/bwrrp/xspattern.js/issues/9) - [ ] tunneled parameters
Running tests
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 installand then run tests:
npm testContributing
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.
