XML Validation with xmllint (libxml2)

1.0.0 · abandoned · verified Sun Apr 19

node-xmllint is an Emscripten port of libxml2's `xmllint` utility, enabling XML and XSD schema validation directly within Node.js environments and web browsers. It provides a synchronous `validateXML` function that returns an object containing an `errors` array (or null if no errors). As of version 1.0.0, the package has not seen updates in approximately 8 years, indicating it is an abandoned project. Its key differentiator was bringing the battle-tested libxml2 C validation engine to JavaScript runtimes, supporting complex XML Schema Definitions directly, unlike simpler regex-based or DOM-parser-based validation methods available at the time. It was designed to integrate with older frontend bundling tools like Browserify via `browserify-shim` for browser usage. Newer, actively maintained alternatives exist that leverage WebAssembly for better performance and modern API design.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `node-xmllint` to validate XML strings, both for well-formedness and against an XML Schema Definition (XSD). It shows cases for valid and invalid XML with and without schema validation.

const xmllint = require('node-xmllint');

const xmlValid = `<root><item id="1"/></root>`;
const xmlInvalid = `<root><item></wrong></item></root>`;
const schemaValid = `<?xml version="1.0" encoding="UTF-8"?>\n<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">\n  <xs:element name="root">\n    <xs:complexType>\n      <xs:sequence>\n        <xs:element name="item" minOccurs="0" maxOccurs="unbounded">\n          <xs:complexType>\n            <xs:attribute name="id" type="xs:integer" use="optional"/>\n          </xs:complexType>\n        </xs:element>\n      </xs:sequence>\n    </xs:complexType>\n  </xs:element>\n</xs:schema>`;

// Validate valid XML without schema
let result = xmllint.validateXML({ xml: xmlValid });
if (result.errors) {
  console.error('Valid XML (no schema) validation errors:', result.errors);
} else {
  console.log('Valid XML (no schema) is well-formed.');
}

// Validate invalid XML without schema
result = xmllint.validateXML({ xml: xmlInvalid });
if (result.errors) {
  console.error('Invalid XML (no schema) validation errors:', result.errors);
} else {
  console.log('Invalid XML (no schema) is well-formed.');
}

// Validate valid XML against a schema
result = xmllint.validateXML({ xml: xmlValid, schema: schemaValid });
if (result.errors) {
  console.error('Valid XML (with schema) validation errors:', result.errors);
} else {
  console.log('Valid XML (with schema) is valid.');
}

// Validate invalid XML against a schema (e.g., missing 'id' attribute if required)
const xmlSchemaInvalid = `<root><item/></root>`;
result = xmllint.validateXML({ xml: xmlSchemaInvalid, schema: schemaValid });
if (result.errors) {
  console.error('Invalid XML (with schema) validation errors:', result.errors);
} else {
  console.log('Invalid XML (with schema) is valid.');
}

view raw JSON →