Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

xslt-processor

DesignLiquido124.3kLGPL-3.03.3.1TypeScript support: included

A JavaScript XSLT Processor

xslt, xpath, xml

readme

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 XmlParser class and use this way:

import { Xslt, XmlParser } from 'xslt-processor'

// xmlString: string of xml file contents
// xsltString: string of xslt file contents
// outXmlString: output xml string.
const xslt = new Xslt();
const xmlParser = new XmlParser();
// Either
const outXmlString = await xslt.xsltProcess(
    xmlParser.xmlParse(xmlString),
    xmlParser.xmlParse(xsltString)
);
// Or
xslt.xsltProcess(
    xmlParser.xmlParse(xmlString),
    xmlParser.xmlParse(xsltString)
).then(output => {
  // `output` is equivalent to `outXmlString` (a string with XML).
});

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' }],
  outputMethod: 'xml'
};
const xslt = new Xslt(options);
  • cData (boolean, default true): resolves CDATA elements in the output. Content under CDATA is resolved as text. This overrides escape for CDATA content.
  • escape (boolean, default true): replaces symbols like <, >, & and " by the corresponding HTML/XML entities. Can be overridden by disable-output-escaping, that also does the opposite, unescaping &gt; and &lt; by < and >, respectively.
  • selfClosingTags (boolean, default true): Self-closes tags that don't have inner elements, if true. For instance, <test></test> becomes <test />.
  • outputMethod (string, default xml): Specifies the default output method. if <xsl:output> is declared in your XSLT file, this will be overridden.
  • 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@3.0.0/umd/xslt-processor.js"></script>

All the exports will live under globalThis.XsltProcessor and window.XsltProcessor. See a usage example here.

Breaking Changes

Version 2

Until version 2.3.1, use like the example below:

import { Xslt, XmlParser } from 'xslt-processor'

// xmlString: string of xml file contents
// xsltString: string of xslt file contents
// outXmlString: output xml string.
const xslt = new Xslt();
const xmlParser = new XmlParser();
const outXmlString = xslt.xsltProcess( // Not async.
    xmlParser.xmlParse(xmlString),
    xmlParser.xmlParse(xsltString)
);

Version 3 received <xsl:include> which relies on Fetch API, which is asynchronous. Version 2 doesn't support <xsl:include>.

If using Node.js older than version v17.5.0, please use version 3.2.3, that uses node-fetch package. Versions 3.3.0 onward require at least Node.js version v17.5.0, since they use native fetch() function.

Version 1

Until version 1.2.8, use like the example below:

import { Xslt, xmlParse } from 'xslt-processor'

// xmlString: string of xml file contents
// xsltString: string of xslt file contents
// outXmlString: output xml string.
const xslt = new Xslt();
const outXmlString = xslt.xsltProcess(
    xmlParse(xmlString),
    xmlParse(xsltString)
);

Version 0

Until version 0.11.7, use like the example below:

import { xsltProcess, xmlParse } from 'xslt-processor'

// xmlString: string of xml file contents
// xsltString: string of xslt file contents
// outXmlString: output xml string.
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).

So far, we have implemented XQuery functions for versions 1.0 and 2.0, but this is not complete yet.

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.

HTML Conformance

HTML per se is not strict XML. Because of that, starting on version 2.0.0, this library handles HTML differently than XML:

  • For a document to be treated as HTML, it needs to have a <!DOCTYPE> tag defined with one of the following valid formats:
    • <!DOCTYPE html> (for HTML5);
    • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> (for HTML4);
    • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> (for XHTML 1.1).
  • Tags like <hr>, <link> and <meta> don't need to be closed. The output for these tags doesn't close them (adding a / before the tag closes, or a corresponding close tag);
    • This rule doesn't apply for XHTML, which is strict XML.

References

changelog

This document is no longer maintained in favor of GitHub's release system, and just kept here for historical reasons.

2018-03-20 Johannes Wilm johannes@fiduswriter.org

* Version 0.9.0
* Changed code to ES2015+ style
* Fixed a bug where xml parsing choked on < and > inside of quotation marks.
* Renamed to xslt-processor

2007-11-16 Google Inc. opensource@google.com

* Version 0.8
* Released as http://ajaxslt.googlecode.com/svn/tags/release-0-8/

* Fixed a bug in parsing XPaths that end with "//qname" (Issue 17)

* Added feature to optionally allow case-insensitive node name
comparisons; this is useful when using XPaths on HTML, where
node names are not consistent across browsers.

* Improved performance by relying on getElementsByTagName where
possible

* Workaround IE bug where "javascript:" href attribute is URL
encoded.  (Issue 19)

2006-12-28 Google Inc. opensource@google.com

* Version 0.7
* Released as http://ajaxslt.googlecode.com/svn/tags/release-0-7/

* Fixed a bug that semicolons are dropped by the XML parser when a
text nodes also contains an entity.

* Fixed a bug that xsl:variable definitions with a node set value
encountered at the root level of the input document would throw an
exception.

* Fixed a bug that XPath expression @* always evaluated to the
empty node set.

* Fixed a bug that xsl:copy would copy only attribute and element
nodes.

* Fixed a bug that if xsl:apply-templates matches multiple
templates, the output is sorted according to the order of the
matching templates, and not according to the sort order defined
for the selected node set to which templates are applied.

* Added unittests for all fixed bugs.

* Added wrapper function xmlOwnerDocument() to uniformly access
the document on both document nodes and other nodes and use it
throughout the xslt processor.

2006-12-14 Google Inc. opensource@google.com

* Version 0.6
* Released as http://ajaxslt.googlecode.com/svn/tags/release-0-6/
* Fixes infinite loops in evaluation of XPath axes "ancestor",
  "ancestor-or-self", "preceding-sibling", "following-sibling".
* Fixes evaluation of XPath axes "preceding", "following".
* Added unittests for both.
* Fixed xmlEscape*() functions to escape *all* markup characters
  not just the first.
* Fixed xsl:copy-of to also copy CDATA and COMMENT nodes.

2006-09-10 Google Inc. opensource@google.com

* Version 0.5
* Released on http://code.google.com/hosting/
* General changes:
  - remove all uses of for-in iteration
  - rename misc.js to util.js
  - log window is now in simplelog.js
* XPath changes:
  - fixed id() function
  - fixed UnionExpr::evaluate()
  - added support for Unicode chracters
* XSLT changes:
  - fixed xsl:sort in xsl:for-each (again)
* DOM changes:
  - added a few methods
* XML parser changes:
  - parses CDATA sections
  - parses comments
  - parses XML declaration
  - parses Unicode XML markup
* Test changes:
  - added several jsunit tests

2005-10-19 Google Inc. opensource@google.com

* Version 0.4
* XPath changes:
  - Optimize parsing of very common and simple expressions.
  - Fix use of XPath operator names -- div, mod, and, or --
    as node names in abbreviated step expressions.
  - Fix root node -- it is now set to ownerDocument.
* XSLT changes:
  - Fix xsl:sort in xsl:for-each.
* DOM changes:
  - Add replaceChild(), insertBefore(), removeChild().
    These are still not needed in XSLT processing, but
    in another client of the DOM implementation.
  - DOM nodes are recycled instead of garbage collected,
    in order to improve performance in some browsers.
* Test changes:
  - Add many more test cases to the XPath tests.
  - Add a note mentioning jsunit in the README.
  - Add a DOM unittest file.

2005-08-27 Google Inc. opensource@google.com

* Version 0.3 (not released on sourceforge)
* XPath changes:
  - Fix implementation of the * node test.
  - Fix implementation of the substring() function.
  - Fix non-abbreviated axis names.
  - Fix filter expressions.
* XSLT changes:
  - Fix xsl:sort.
* DOM changes:
  - Avoid using String.split() that breaks in IE.
  - Coerce nodeType to number and nodeName and nodeValue to string.
  - Fix SGML entity replacement of single quotes in attribute values.
* Test changes:
  - Fix end tags of script elements in test HTML files.
  - Add jsunit tests for xpath.js.

2005-06-29 Google Inc. opensource@google.com

* Version 0.2
* Add more missing code
  - XML parser and simple DOM implementation in dom.js
  - miscellaneous functions in misc.js.
* Add simple test pages that serve as examples.
  - test-xpath.html tests and demonstrates the XPath parser.
  - test-xslt.html tests and demonstrates the XSLT processor.
  - output methods for debugging of XPath expressions added
    in xpathdebug.js.
* Some additions and corrections in README and TODO
  - renamed XSL-T to XSLT, because that's more common.
  - miscellaneous updates.