Skip to content

LibXML2 Node.js Wrapper, the aim is to implement a maximum of xmlstarlet possibilities

Notifications You must be signed in to change notification settings

MatthD/node-libxml

Repository files navigation

Node-LibXML

For old node.js versions please use version < 4.0.0 Already for old LTS node (4,6,8) npm install [email protected];

For new node.js versions (10,12,14,16,18,20) we are using n-api so please install npm install [email protected], its compatible with arm proc too for version >= 16 (you wil notice a free memory message when using free memory function after node >16) 🙂

Node-Libxml is a libxml2 Node.js Wrapper

It can perform:

  • Well-formed check & error report
  • Validity against DTD/XSD(Schema) check,
  • Get doctype information (about DTD)
  • Get XPath Values
  • Load XMl from string or file path

Requirements:

Node-Libxml has no dependencies; it's fully bundled, so you can just use it as it comes :)

Works on: Linux, macOS & Windows (no 32bits, just 64 bits)

Why use it

Node-LibXML has been thought differently than libxmljs; whereas libxmljs can be use in common uses, you can use node-libxml if:

  • You want to validate against DTD (libxmljs can't)
  • You want a silent program if XML is not well-formed (node-libxml returns well-formed error in object; libxmljs throws)
  • You want to do XML processing in parallel forks
  • You want to load XML from file path or string
  • You want to validate against DTD / schema on multiple documents with just one DTD/schema loaded in memory (libxmljs loads it on each validation request), so it's clearly by far fastest!

Install

  npm i node-libxml

Use

  const Libxml = require('node-libxml').Libxml; // or import {Libxml} from 'node-libxml'
  let libxml = new Libxml();

  let xmlIsWellformed = libxml.loadXml('path/to/xml');
  let xmlIsWellformedStr = libxml.loadXmlFromString('<name>test</name>');

  console.log(xmlIsWellformed);
  console.log(libxml.wellformedErrors);

  console.log(libxml.getDtd());

  libxml.loadDtds(['path/to/dtd1', 'path/to/dtd2']);
  let xmlIsValid = libxml.validateAgainstDtds();
  console.log(xmlIsValid);
  console.log(libxml.validationDtdErrors);

  //Get some xpaths;
  let aRandomPathBoolean = libxml.xpathSelect('boolean(//some/path'));
  let aRandomPathNumber = libxml.xpathSelect('number(//some/path'));
  let countSomeElements = libxml.xpathSelect('count(//some/path'));
  //... & all xpath could do I think

See our tests for more examples.

API

Loading functions

loadXml(string)

A function of libxml2 to load the XML file

  • TAKE a path & RETURN true if well-formed | false if not
  • SET an array wellformedErrors in libxml2 element containing well-formed errors
loadXmlFromString(string)

A function of libxml2 to create the XML DOM from a string

  • TAKE a string containing XML & RETURN true if well-formed | false if not
  • SET an array wellformedErrors in libxml2 element containing well-formed errors
loadDtds(array)

A function of libxml2 to load the DTD(s) file(s)

  • TAKE an array of path of & RETURN nothing
  • SET an array dtdsLoadedErrors in libxml2 element IF error happened on loading DTD(s)
loadSchemas(array)

A function of libxml2 to load the XSD(s) file(s)
ex

  • TAKE an array of path of & RETURN nothing
  • SET an array schemasLoadedErrors in libxml2 element IF error happened on loading XSD(s)

Validating functions

validateAgainstDtd()

A function of libxml2 to validate against the previously loaded DTD(s)

  • TAKE nothing & RETURN a string which is the name of the first DTD which has validated
  • RETURN false if no DTD(s) have validated the XML
  • RETURN null if not any DTD have been correctly loaded
  • SET an array validationDtdErrors in libxml2 element if no DTD has validate
validateAgainstSchemas()

A function of libxml2 to validate against the previously loaded DTD(s)

  • TAKE nothing & RETURN a string which is the name of the first DTD which has validated
  • RETURN false if no DTD(s) have validated the XML
  • RETURN null if no any schema have been correctly loaded
  • SET an array validationDtdErrors in libxml2 element if no DTD has validated

Get information of the XML

getDtd()

A function of libxml2 to evaluate the xpath on the previously loaded XML

  • TAKE nothing & RETURN an object containing name, externalId & systemId

xpathSelect(string)

A function of libxml2 to evaluate the xpath on the previously loaded XML
ex

  • TAKE string & RETURN value depending on what you asked (number, boolean,...)
  • RETURN null if no match

Memory management

Use those functions when you have finished jobs of elements, or before overwrite them.
ex

freeXml()

A function of libxml2 to free XML memory file

  • TAKE nothing & RETURN nothing
  • FREE memory & clear wellformedErrors property on libxml2 instance
freeDtds()

A function of libxml2 to free all the DTD(s) in memory

  • TAKE nothing & RETURN nothing
  • FREE memory & clear dtdsLoadedErrors property on libxml2 instance
  • FREE memory & clear validationDtdErrors property on libxml2 instance
freeSchemas()

A function of libxml2 to free all the Schema in memory

  • TAKE nothing & RETURN nothing
  • FREE memory & clear 'schemasLoadedErrors' property on libxml2 instance
  • FREE memory & clear 'validationSchemaErrors' property on libxml2 instance
clearAll()

A function of libxml2 to free all the memory taken by libxml2

  • TAKE nothing & RETURN nothing
  • free all memory in all instance in all fork using by libxml2

Contribute

Install

  • npm i
  • npm test should pass 😎
  • npm run publish

Important notes

Travis & appveyor cannot be used anymore with n-api because auto-publish from node-pre-gyp-github not working anymore + we have now n release for one node version