node-schematron

2.1.0 · active · verified Tue Apr 21

The `node-schematron` package provides a pure JavaScript implementation of the Schematron validation language, designed for use in Node.js environments, web browsers (with bundlers like Webpack), and as a command-line interface (CLI) tool. As of version 2.1.0, it offers a robust solution for validating XML documents against Schematron schemas, returning results as a JSON object. Key differentiators include its complete JavaScript implementation, avoiding native dependencies, and support for Schematron includes and various CLI options for file globbing and output reporters. The library utilizes `fontoxpath` for XPath 3.1 queries, though it notes that `fontoxpath` is not yet feature-complete. While there's no explicit release cadence, the project appears actively maintained, offering a flexible and integrated Schematron solution for JavaScript ecosystems.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a Schematron `Schema` from a string and validate an XML string, logging the results.

import { Schema } from 'node-schematron';

const schematronSchema = `
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
	<pattern>
		<rule context="thunder">
			<let name="lightning" select="@foo"/>
			<report test="$lightning = 'bar'">
				Skeet boop <value-of select="$lightning" />
			</report>
			<assert test="@type = 'storm'">
				Thunder must have a 'type' attribute with value 'storm'.
			</report>
		</rule>
		<rule context="xml">
			<assert test="count(thunder) > 0">
				XML document must contain at least one thunder element.
			</assert>
		</rule>
	</pattern>
</schema>`;

const schema = Schema.fromString(schematronSchema);

const xmlToValidate = `
<xml foo="err">
	<thunder foo="bar" type="storm" />
</xml>`;

const results = schema.validateString(xmlToValidate, { debug: true });

console.log(JSON.stringify(results, null, 2));
/* Expected Output for the report (assertion passes):
[
  {
    "isReport": true,
    "context": "<thunder foo="bar" type="storm"/>",
    "message": "\n\t\t\t\tSkeet boop bar\n\t\t\t"
  }
]
*/

view raw JSON →