Skip to content

Repository files navigation

Schematron.NET

VersionDownloadsLicenseGitHub

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

usingSchematron;varvalidator=newValidator();validator.AddSchema("order.sch");try{validator.Validate("order.xml");Console.WriteLine("Document is valid.");}catch(ValidationExceptionex){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:

usingSchematron;varvalidator=newValidator(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(ValidationExceptionex){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:

usingSystem.Xml.XPath;usingSchematron;varvalidator=newValidator();validator.AddSchema("rules.sch");vardocument=newXPathDocument("order.xml");validator.ValidateSchematron(document);

Selecting the phase, formatter, and return type

Validator lets you control the active phase, output format, and result type:

usingSchematron;usingSchematron.Formatters;varvalidator=newValidator(OutputFormatting.XML,NavigableType.XmlDocument){Phase="paymentInfo",Formatter=newXmlFormatter(),};validator.AddSchema("purchase-order.sch");varresult=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:

usingSchematron;varschema=newSchema();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 OrgMFB Technologies, Inc.SandRockDRIVE.NET, Inc.Keith PickfordThomas BolonKori FrancisReuben SwartzJacob FosheeEric JohnsonJonathan Ken BonnySimon Croppagileworks-euZheyu ShenVezelChilliCream4OTCdomischellAdrian AlonsotorutekRyan McCafferySeika LogicielAndrew Granteska-gmbhGeodata AS

Sponsor this project

Learn more about GitHub Sponsors

About

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

Resources

Code of conduct

Contributing

Security policy

Stars

23 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages