Wrapper for `xmllint`. Easy validation of XML. XML Schemas and DTDs supported. Zero dependencies.
npm install validate-with-xmllint[![npm package][npm-badge]][npm]
Wrapper for xmllint. Easy validation of XML. XML Schemas and DTDs supported. Zero dependencies.
- Validate schemaless XML file 👉 function validateXML(input: string | Buffer): Promise
- Validate with DTDs 👉 function validateXMLWithDTD(input: string | Buffer): Promise
- Validate with XML Schemas (XSD) 👉 function validateXMLWithXSD(input: string | Buffer, xsdfile: string | Buffer): Promise
- Written in TypeScript
- Tests with good coverage included
- Zero NPM dependencies
- Availability of xmllint is verified during installation time
- No network connections used during validations
#### Importing functions
``js`
const {
validateXML,
validateXMLWithDTD,
validateXMLWithXSD
} = require("validate-with-xmllint");
#### Demos of passing validations
All the examples below return promises which will eventually _resolve_.
`js
validateXML("
validateXML("
/**
* All referenced DTD's must be available locally as xmllint--nonet
* is invoked with which prevents network connections
**/
validateXMLWithDTD(
);/**
* Path to XSD Schema file must be provided with the document
**/
validateXMLWithXSD(
,
"./test/xsds/valid-xsd.xsd"
);
`#### Demos of failing validations
All the examples below return promises which will eventually _reject_ with the output from
xmllint.`js
validateXML("");
validateXML("garbage");validateXMLWithDTD(
);validateXMLWithXSD(
,
"./test/xsds/valid-xsd.xsd"
);
`See more demos within the tests.
FAQ
$3
The
validate-with-xmllint is a wrapper for program xmllint. A _postinstall_ script fails if it is not available. See next section for further info.$3
You might already have it. Execute
xmllint --version and see what it prints out.On Debian distros look up
libxml2-utils, on RedHat libxml2. A recent version of xmllint should be bundled by default on MacOS.This package runs a _postinstall_ script to verify that you have
xmllint available.$3
The promise is rejected with the error output from
xmllint. The error contains all the available details. Expect to see something like this:`
Error: xmllint exited with code 3 when executed with xmllint --schema /Users/aautio/foobar/validate-with-xmllint/test/xsds/valid-xsd.xsd --noout --nonet -:
-:2: element root: Schemas validity error : Element 'root': Missing child element(s). Expected is ( body ).
- fails to validate
``[npm-badge]: https://img.shields.io/npm/v/validate-with-xmllint.svg
[npm]: https://www.npmjs.org/package/validate-with-xmllint