Name: node-libxslt-old
Owner: BBC
Description: Node.js bindings for libxslt compatible with libxmljs
Created: 2015-04-24 08:54:07.0
Updated: 2016-09-26 10:49:52.0
Pushed: 2015-06-30 10:59:41.0
Homepage: null
Size: 478
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Node.js bindings for libxslt compatible with libxmljs.
npm install libxslt
libxslt = require('libxslt');
slt.parse(stylesheetString, function(err, stylesheet){
r params = {
MyParam: 'my value'
'params' parameter is optional
ylesheet.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 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.
The libxmljs module required by node-libxslt is exposed as `require('libxslt').libxmljs
`. This prevents depending on libxmljs twice which is not optimal and source of weird bugs.
It is possible to work with libxmljs documents instead of strings:
lixslt = require('libxslt');
libxmljs = libxslt.libxmljs;
stylesheetObj = libxmljs.parseXml(stylesheetString, { nocdata: true });
stylesheet = libxslt.parse(stylesheetObj);
document = libxmljs.parseXml(documentString);
esheet.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.
The same parse() and apply() functions can be used in synchronous mode simply by removing the callback parameter. In this case if a parsing error occurs it will be thrown.
lixslt = require('libxslt');
stylesheet = libxslt.parse(stylesheetString);
result = stylesheet.apply(documentString);
The asynchronous functions use 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:
0 synchronous parse from parsed doc in 52ms = 192308/s
0 asynchronous parse in series from parsed doc in 229ms = 43668/s
0 asynchronous parse in parallel from parsed doc in 56ms = 178571/s
0 synchronous apply from parsed doc in 329ms = 30395/s
0 asynchronous apply in series from parsed doc in 569ms = 17575/s
0 asynchronous apply in parallel from parsed doc in 288ms = 34722/s
Observations:
Conclusion:
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
Members
Parse a XSL stylesheet
If no callback is given the function will run synchronously and return the result or throw an error.
Params
string
| Document
- The content of the stylesheet as a string or a libxmljs documentparseCallback
- The callback that handles the response. Expects err and Stylesheet object.Returns: Stylesheet
- Only if no callback is given.
Parse a XSL stylesheet
Params
stringPath
- The path of the fileparseFileCallback
- The callback that handles the response. Expects err and Stylesheet object.Members
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.
Params
Document
- XML document source of the stylesheetDocument
- Simple wrapper of a libxslt stylesheetScope: inner class of libxslt
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.
Params
string
| Document
- The XML content to apply the stylesheet to given as a string or a libxmljs documentobject
- Parameters passed to the stylesheet (http://www.w3schools.com/xsl/el_with-param.asp)applyCallback
- The callback that handles the response. Expects err and result of the same type as the source param passed to apply.Returns: string
| Document
- Only if no callback is given. Type is the same as the source param.
Apply a stylesheet to a XML file
Params
string
- The path of the file to readobject
- Parameters passed to the stylesheet (http://www.w3schools.com/xsl/el_with-param.asp)applyToFileCallback
- The callback that handles the response. Expects err and result as string.documented by jsdoc-to-markdown.