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.
npm install node-schematronVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize a Schematron `Schema` from a string and validate an XML string, logging the results.
Consult the `fontoxpath` documentation or `node-schematron` unit tests for specific supported XPath features. Consider implementing custom XPath functions using `registerCustomXPathFunction` for unsupported scenarios.
Structure your Schematron patterns carefully, ordering rules from most specific to least specific. Use multiple patterns or refactor rules if multiple independent checks are required for the same node.
Avoid using `<xsl:function>` elements in your Schematron schemas. If custom logic is required, define JavaScript-based custom XPath functions using `registerCustomXPathFunction`.
Be aware of these unsupported attributes when authoring Schematron schemas, as they will be ignored by `node-schematron`. Adjust schemas to use supported constructs or external reporting mechanisms if these features are critical.
For programmatic use, always provide an absolute or correctly resolved path to `resourceDir`. For CLI use, double-check `schematronLocation` and `globPattern` arguments, using absolute paths for clarity where possible. Use `--files` for explicit file lists if globbing is problematic.
Ensure the `resourceDir` option in `Schema.fromString()` or `Schema.fromFileSync()` points to the correct directory containing the included Schematron files. For CLI, verify `schematronLocation` and any paths referenced within the schema.
Refine the XPath expression to ensure it selects a single node or atomic value, or explicitly handle sequences using XPath functions like `string-join()`, `count()`, `head()`, `tail()`, or predicates like `[1]` to select a specific item from the sequence.
Carefully review the XPath expression for syntax errors. Validate the XPath against an XPath 3.1 validator. Pay attention to common pitfalls like incorrect axis specifiers, mismatched parentheses, or improper use of operators.
No dependency data recorded yet.