A .NET implementation of Schematron for validating XML with standalone .sch schemas or
Schematron rules embedded in W3C XML Schema. The library supports both the current ISO
Schematron namespace and the legacy ASCC namespace, with a compact public API centered on
Validator and Schema.
To ensure the long-term sustainability of this project, users of this package who generate revenue must pay an Open Source Maintenance Fee. While the source code is freely available under the terms of the License, this package and other aspects of the project require adherence to the Maintenance Fee.
To pay the Maintenance Fee, become a Sponsor at the proper OSMF tier. A single fee covers all of Devlooped packages.
dotnet add package SchematronThe package targets netstandard2.0 and net8.0.
The package exposes two primary entry points:
Validatorloads Schematron and XML Schema definitions and validates XML documents.Schemaloads and inspects Schematron documents programmatically.
using Schematron;
var validator = new Validator();
validator.AddSchema("order.sch");
try
{
validator.Validate("order.xml");
Console.WriteLine("Document is valid.");
}
catch (ValidationException ex)
{
Console.WriteLine(ex.Message);
}Validate returns the loaded IXPathNavigable document on success and throws
ValidationException when XML Schema or Schematron validation fails.
When the schema is a W3C XML Schema document with embedded Schematron, Validator runs
both validations in one pass:
using Schematron;
var validator = new Validator(OutputFormatting.XML);
// Use the overload with the target namespace when the XSD imports or includes other schemas.
validator.AddSchema("http://example.com/po-schematron", "po-schema.xsd");
try
{
validator.Validate("purchase-order.xml");
}
catch (ValidationException ex)
{
Console.WriteLine(ex.Message); // XML formatted output
}If you already have an IXPathNavigable, use ValidateSchematron to skip XML Schema
validation and evaluate only the loaded Schematron rules:
using System.Xml.XPath;
using Schematron;
var validator = new Validator();
validator.AddSchema("rules.sch");
var document = new XPathDocument("order.xml");
validator.ValidateSchematron(document);Validator lets you control the active phase, output format, and result type:
using Schematron;
using Schematron.Formatters;
var validator = new Validator(OutputFormatting.XML, NavigableType.XmlDocument)
{
Phase = "paymentInfo",
Formatter = new XmlFormatter(),
};
validator.AddSchema("purchase-order.sch");
var result = validator.Validate("purchase-order.xml");Use Phase.All (#ALL) to evaluate every pattern regardless of phase activation.
Use Schema directly when you want to inspect a Schematron document without validating
an instance document yet:
using Schematron;
var schema = new Schema();
schema.Load("rules.sch");
Console.WriteLine(schema.Title);
Console.WriteLine(schema.SchematronEdition);
Console.WriteLine(schema.DefaultPhase);
Console.WriteLine(schema.IsLibrary);
Console.WriteLine(schema.Patterns.Count);
Console.WriteLine(schema.Lets.Contains("maxAge"));This is useful for tooling, analyzers, test helpers, or apps that need to inspect declared
phases, patterns, diagnostics, parameters, and let bindings.
Schematron keeps the public surface intentionally small:
Validatoris the main entry point for loading schemas and validating XML.Schemaloads and inspects Schematron documents programmatically.OutputFormattingselects the built-in output style:Default/Log,Simple,Boolean, orXML.Validator.Phase,Validator.Formatter, andValidator.ReturnTypelet you choose the active phase, custom output formatter, and returned document type.BadSchemaExceptionsignals invalid schema input, whileValidationExceptionsignals XML or Schematron validation failures.
The package currently supports the main scenarios expected for a v1 release, including:
- Standalone
.schschemas and Schematron embedded in W3C XML Schema - ISO Schematron namespace (
http://purl.oclc.org/dsdl/schematron) and legacy ASCC namespace compatibility - ISO Schematron 2025 features such as
<library>, phase@when, rule@visit-each, rule flags, and schema parameters - Schema-, pattern-, and rule-level
letbindings - Diagnostics, severity metadata, abstract patterns/rules, and groups