Skip to content

devlooped/Schematron

Schematron.NET

Version Downloads License GitHub

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.

Open Source Maintenance Fee

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.

Installation

dotnet add package Schematron

The package targets netstandard2.0 and net8.0.

Usage

The package exposes two primary entry points:

  • Validator loads Schematron and XML Schema definitions and validates XML documents.
  • Schema loads and inspects Schematron documents programmatically.

One-shot validation with a standalone Schematron schema

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.

Validating XSD plus embedded Schematron

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
}

Schematron-only validation for in-memory XML

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);

Selecting the phase, formatter, and return type

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.

Loading and inspecting a schema

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.

Core API

Schematron keeps the public surface intentionally small:

  • Validator is the main entry point for loading schemas and validating XML.
  • Schema loads and inspects Schematron documents programmatically.
  • OutputFormatting selects the built-in output style: Default/Log, Simple, Boolean, or XML.
  • Validator.Phase, Validator.Formatter, and Validator.ReturnType let you choose the active phase, custom output formatter, and returned document type.
  • BadSchemaException signals invalid schema input, while ValidationException signals XML or Schematron validation failures.

Supported features

The package currently supports the main scenarios expected for a v1 release, including:

  • Standalone .sch schemas 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 let bindings
  • Diagnostics, severity metadata, abstract patterns/rules, and groups

Sponsors

Clarius Org MFB Technologies, Inc. Khamza Davletov SandRock DRIVE.NET, Inc. Keith Pickford Thomas Bolon Kori Francis Reuben Swartz Jacob Foshee Eric Johnson Jonathan Ken Bonny Simon Cropp agileworks-eu Zheyu Shen Vezel ChilliCream 4OTC domischell Adrian Alonso torutek mccaffers Seika Logiciel Andrew Grant eska-gmbh

Sponsor this project

Learn more about GitHub Sponsors

About

A Schematron ISO/IEC standard processor for .NET, written in C#

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

 
 
 

Contributors