node-libxslt

Node.js bindings for libxslt compatible with libxmljs.
Installation
npm install libxslt
Basic usage
var lixslt = require('libxslt');
var stylesheet = libxslt.parse(stylesheetString);
var params = {
MyParam: 'my value'
};
stylesheet.apply(documentString, params, function(err, result){
});
Libxmljs integration
:warning: right now node-libxslt is only compatible with the temporary fork albanm/libxmljs.
This situation will end if the related pull request is accepted.
Node-libxslt depends on libxmljs 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.
It is possible to work with libxmljs documents instead of strings:
var lixslt = require('libxslt');
var libxmljs = require('libxmljs');
var stylesheetObj = libxmljs.parseXml(stylesheetString);
var stylesheet = libxslt.parse(stylesheetObj);
var document = libxmljs.parseXml(documentString);
stylesheet.apply(document, function(err, result){
});
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.
Sync or async
The same apply() function can be used in synchronous mode simply by removing the callback parameter.
In this case if a parsing error occurs it will be thrown.
var lixslt = require('libxslt');
var stylesheet = libxslt.parse(stylesheetString);
var result = stylesheet.apply(documentString);
The asynchronous function uses the libuv work queue
to provide parallelized computation in node.js worker threads. This makes it non-blocking for the main event loop of node.js.
Note that libxmljs parsing doesn't use the work queue, so only a part of the process is actually parallelized.
A small benchmark is available in the project. It has a very limited scope, it uses always the same small transformation a few thousand times.
To run it use:
node benchmark.js
This is an example of its results with an intel core i5 3.1GHz:
10000 synchronous apply from parsed doc in 331ms = 30211/s
10000 asynchronous apply in series from parsed doc in 538ms = 18587/s
10000 asynchronous apply in parallel from parsed doc in 217ms = 46083/s
Observations:
- it's pretty fast !
- asynchronous is slower when running in series.
- asynchronous can become faster when concurrency is high.
Conclusion:
- use asynchronous by default it will be kinder to your main event loop and is pretty fast anyway.
- use synchronous only if you really want the highest performance and expect low concurrency.
- of course you can also use synchronous simply to reduce code depth. If you don't expect a huge load it will be ok.
Environment compatibility
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 have to go through its installation. This can be a bit painful mostly for windows users. There is no system dependancy otherwise, libxslt is bundled in the project.