Registry / serialization / node-schematron

node-schematron

JSON →
library2.1.0jsnpmunverified

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-schematron
INSTALL
IMPORT
SIG · NODE-SCHEMATRON
N
node-schematron
serializationjavascriptv2.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Schema
import { Schema } from 'node-schematron';
const { Schema } = require('node-schematron');
ESM is preferred in modern Node.js and browser projects, but CommonJS `require` is also supported for older Node.js environments as shown in the README example. TypeScript types are included.
registerCustomXPathFunction
import { registerCustomXPathFunction } from 'node-schematron';
const { registerCustomXPathFunction } = require('node-schematron');
Used to extend XPath capabilities with custom JavaScript functions. This utility is an alias for the same function in `fontoxpath`.
validateString
schema.validateString('<xml/>', options);
Schema.validateString('<xml/>', options);
This is a method on an *instance* of `Schema`, not a static method on the `Schema` class itself. Always instantiate `Schema` first.

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" } ] */
schematron --version
Debug
Known issues
gotchaThe underlying XPath 3.1 implementation, `fontoxpath`, is noted as not yet feature-complete. Users relying on advanced or less common XPath 3.1 functions might encounter unsupported features or unexpected behavior.
fix
Consult the `fontoxpath` documentation or `node-schematron` unit tests for specific supported XPath features. Consider implementing custom XPath functions using `registerCustomXPathFunction` for unsupported scenarios.
affects: >=2.0.0
gotchaSchematron's rule matching behavior can be counter-intuitive: within a single pattern, a node will only match the *first* rule whose context matches it. Subsequent rules in the same pattern for that node will not be evaluated, even if their contexts also match.
fix
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.
affects: >=1.0.0
gotchaThe library explicitly states that XSLT functions (`<xsl:function>`) are not supported. While there was a feature branch, it remains unimplemented.
fix
Avoid using `<xsl:function>` elements in your Schematron schemas. If custom logic is required, define JavaScript-based custom XPath functions using `registerCustomXPathFunction`.
affects: >=1.0.0
gotchaSome ISO/IEC 19757-3 2016 (Schematron) attributes are not supported, including `@abstract`, `@diagnostics`, `@icon`, `@see`, `@fpi`, `@flag`, `@role`, and `@subject` in certain contexts.
fix
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.
affects: >=1.0.0
gotchaWhen using `resourceDir` for schema includes, ensure the path is correct and accessible. Incorrect paths will lead to `file not found` errors, and the CLI tool may default to `*.xml` if `globPattern` is not used correctly or `schematronLocation` is relative and ambiguous.
fix
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.
affects: >=1.0.0
Errors
Common errors & fixes
ENOENT: no such file or directory, stat '/path/to/include/files/file.xml'
A Schematron schema uses `<include href="file.xml" />` but the `resourceDir` option was not provided or points to an incorrect location, preventing the included file from being found.
fix
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.
Error: XPath error: "Cannot convert XdmValue to single value: multiple values encountered"
An XPath expression in an `assert` or `report` test, or a `value-of` select, evaluates to a sequence of multiple items where a single item is expected (e.g., trying to compare `//element` with a string, when `//element` matches multiple elements).
fix
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.
Error: (XPath error) Cannot parse XQuery/XPath expression: Unexpected token "|" at position X
A syntax error in an XPath expression within a Schematron rule, often due to a typo or incorrect XPath syntax for the XPath 3.1 query language.
fix
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.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
node-schematron — npm install node-schematron · libregistry