xmllint.js (libxml2 Emscripten Port)

0.1.1 · abandoned · verified Wed Apr 22

This package, `xmllint`, is an Emscripten port of `libxml2`'s `xmllint` command-line tool, enabling XML schema validation directly within Node.js environments and web browsers. It provides a JavaScript API, primarily `xmllint.validateXML`, to check XML against XSD or RelaxNG schemas. The current stable version is 0.1.1, which was last published to npm approximately 10 years ago. The package is largely unmaintained, with its GitHub repository showing minimal activity for several years. A key differentiator at the time of its release was bringing robust XML validation (from the native `libxml2`) to JavaScript without native bindings, relying instead on Emscripten compilation. Due to its abandonment, modern projects are advised to consider the actively maintained `xmllint-wasm` fork, which offers WebAssembly support, updated `libxml2` versions, and an expanded API.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `xmllint.validateXML` to validate XML strings against single or multiple XSD schemas, showcasing both successful and erroneous validation scenarios.

const xmllint = require('xmllint');

const xmlString = `<root><item id="1">Value 1</item><item id="2">Value 2</item></root>`;
const validSchema = `<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="item" maxOccurs="unbounded">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="id" type="xs:integer" use="required"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>`;
const invalidSchema = `<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="bad_item" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>`;

// Example 1: Valid XML against a valid schema
let result1 = xmllint.validateXML({
  xml: xmlString,
  schema: validSchema
});

if (result1.errors) {
  console.log('Validation 1 (Expected Pass) Errors:', result1.errors);
} else {
  console.log('Validation 1 (Expected Pass): No errors found.');
}

// Example 2: Invalid XML against a valid schema
const invalidXmlString = `<root><bad_item>This is wrong</bad_item></root>`;
let result2 = xmllint.validateXML({
  xml: invalidXmlString,
  schema: validSchema
});

if (result2.errors) {
  console.log('Validation 2 (Expected Fail) Errors:', result2.errors[0]);
} else {
  console.log('Validation 2 (Expected Fail): No errors found (unexpected).');
}

// Example 3: Multiple schemas
const schemas = [
  `<?xml version="1.0" encoding="UTF-8"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/ns" elementFormDefault="qualified">
    <xs:element name="document" type="tns:DocumentType"/>
    <xs:complexType name="DocumentType">
      <xs:sequence>
        <xs:element name="header" type="tns:HeaderType"/>
      </xs:sequence>
    </xs:complexType>
  </xs:schema>`,
  `<?xml version="1.0" encoding="UTF-8"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.com/ns" targetNamespace="http://example.com/ns" elementFormDefault="qualified">
    <xs:complexType name="HeaderType">
      <xs:sequence>
        <xs:element name="title" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:schema>`
];
const xmlWithMultipleSchemas = `<document xmlns="http://example.com/ns"><header><title>My Document</title></header></document>`;
let result3 = xmllint.validateXML({
  xml: xmlWithMultipleSchemas,
  schema: schemas
});

if (result3.errors) {
  console.log('Validation 3 (Multiple Schemas - Expected Pass) Errors:', result3.errors);
} else {
  console.log('Validation 3 (Multiple Schemas - Expected Pass): No errors found.');
}

view raw JSON →