Node.js bindings for libxslt compatible with libxmljs2.
This fork restores the dependency of node-libxslt on libxmljs2 rather than the Multi Threaded fork (libxmljs-mt). This was for a couple of reasons:
- libxmljs-mt appears to no longer be manintaned, wereas libxmljs is - as such it compiles with Node.js 14 -> 20.
- In our usage of the library - we don't need any of the async operations
- We don't have very complex XSLT stylesheets - and none that include other XSLT's
This fork keeps the callback implementation of parse
and apply
but both have been refactored to call the
synchronous libxmljs methods.
The fork should compile successfully on Node 14 -> 20.
NOTE: Node.js 14-18 require npm@latest-6; Node.js 20+ required npm 8 or above. This is due to python versions required by node-gyp
npm install libxslt
From source:
git clone https://github.com/albanm/node-libxslt.git
git submodule update --init
npm install
npm test
var libxslt = require('libxslt2');
libxslt.parse(stylesheetString, function(err, stylesheet){
var params = {
MyParam: 'my value'
};
// 'params' parameter is optional
stylesheet.apply(documentString, params, function(err, result){
// err contains any error from parsing the document or applying the stylesheet
// result is a string containing the result of the transformation
});
});
Node-libxslt depends on libxmljs2 in the same way that libxslt depends on libxml. This dependancy makes possible to bundle and to load in memory libxml only once for users of both libraries.
The libxmljs module required by node-libxslt is exposed as require('libxslt').libxmljs
. This prevents depending on libxmljs2 twice which is not optimal and source of weird bugs.
It is possible to work with libxmljs documents instead of strings:
var libxslt = require('libxslt');
var libxmljs = libxslt.libxmljs;
var stylesheetObj = libxmljs.parseXml(stylesheetString, { nocdata: true });
var stylesheet = libxslt.parse(stylesheetObj);
var document = libxmljs.parseXml(documentString);
stylesheet.apply(document, function(err, result){
// result is now a libxmljs document containing the result of the transformation
});
This is only useful if you already needed to parse a document before applying the stylesheet for previous manipulations. Or if you wish to be returned a document instead of a string for ulterior manipulations. In these cases you will prevent extraneous parsings and serializations.
XSL includes are supported but relative paths must be given from the execution directory, usually the root of the project.
Includes are resolved when parsing the stylesheet by libxml. Therefore the parsing task becomes IO bound, which is why you should not use synchronous parsing when you expect some includes.
A small benchmark is available in the project. It uses always the same small transformation a few thousand times.
To run it use:
node benchmark.js
For now 64bits linux and 32bits windows are confirmed. Other environments are probably ok, but not checked. Please report an issue if you encounter some difficulties.
Node-libxslt depends on node-gyp, you will need to meet its requirements. This can be a bit painful mostly for windows users. The node-gyp version bundled in your npm will have to be greater than 0.13.0, so you might have to follow these instructions to upgrade. There is no system dependancy otherwise, libxslt is bundled in the project.
Node.js bindings for libxslt compatible with libxmljs
The libxmljs module. Prevents the need for a user's code to require it a second time. Also prevent weird bugs.
Kind: static property of libxslt
Parse a XSL stylesheet
If no callback is given the function will run synchronously and return the result or throw an error.
Kind: static method of libxslt
Returns: Stylesheet
- Only if no callback is given.
Param | Type | Description |
---|---|---|
source | string | Document |
The content of the stylesheet as a string or a libxmljs document |
[callback] | parseCallback |
The callback that handles the response. Expects err and Stylesheet object. |
Parse a XSL stylesheet
Kind: static method of libxslt
Param | Type | Description |
---|---|---|
sourcePath | stringPath |
The path of the file |
callback | parseFileCallback |
The callback that handles the response. Expects err and Stylesheet object. |
Kind: inner class of libxslt
A compiled stylesheet. Do not call this constructor, instead use parse or parseFile.
store both the source document and the parsed stylesheet if we don't store the stylesheet doc it will be deleted by garbage collector and it will result in segfaults.
Param | Type | Description |
---|---|---|
stylesheetDoc | Document |
XML document source of the stylesheet |
stylesheetObj | Document |
Simple wrapper of a libxslt stylesheet |
Apply a stylesheet to a XML document
If no callback is given the function will run synchronously and return the result or throw an error.
Kind: instance method of Stylesheet
Returns: string
| Document
- Only if no callback is given. Type is the same as the source param.
Param | Type | Description |
---|---|---|
source | string | Document |
The XML content to apply the stylesheet to given as a string or a libxmljs document |
[params] | object |
Parameters passed to the stylesheet (http://www.w3schools.com/xsl/el_with-param.asp) |
[options] | applyOptions |
Options |
[callback] | applyCallback |
The callback that handles the response. Expects err and result of the same type as the source param passed to apply. |
Apply a stylesheet to a XML file
Kind: instance method of Stylesheet
Param | Type | Description |
---|---|---|
sourcePath | string |
The path of the file to read |
[params] | object |
Parameters passed to the stylesheet (http://www.w3schools.com/xsl/el_with-param.asp) |
[options] | applyOptions |
Options |
callback | applyToFileCallback |
The callback that handles the response. Expects err and result as string. |
Callback to the parse function
Kind: inner typedef of libxslt
Param | Type |
---|---|
[err] | error |
[stylesheet] | Stylesheet |
Callback to the parseFile function
Kind: inner typedef of libxslt
Param | Type |
---|---|
[err] | error |
[stylesheet] | Stylesheet |
Options for applying a stylesheet
Kind: inner typedef of libxslt
Properties
Name | Type | Description |
---|---|---|
outputFormat | String |
Force the type of the output, either 'document' or 'string'. Default is to use the type of the input. |
noWrapParams | boolean |
If true then the parameters are XPath expressions, otherwise they are treated as strings. Default is false. |
Callback to the Stylesheet.apply function
Kind: inner typedef of libxslt
Param | Type | Description |
---|---|---|
[err] | error |
Error either from parsing the XML document if given as a string or from applying the styleshet |
[result] | string | Document |
Result of the same type as the source param passed to apply |
documented by jsdoc-to-markdown.