|
1 | 1 | # Schematron.NET |
2 | 2 |
|
3 | | -This is a [Schematron ISO/IEC 2025](http://www.schematron.com/) standard processor for .NET, written in C#. |
| 3 | +[](https://www.nuget.org/packages/Schematron) |
| 4 | +[](https://www.nuget.org/packages/Schematron) |
| 5 | +[](https://github.com/devlooped/Schematron/blob/main/license.txt) |
| 6 | +[](https://github.com/devlooped/Schematron) |
| 7 | + |
| 8 | +<!-- #description --> |
| 9 | +A .NET implementation of Schematron for validating XML with standalone `.sch` schemas or |
| 10 | +Schematron rules embedded in W3C XML Schema. The library supports both the current ISO |
| 11 | +Schematron namespace and the legacy ASCC namespace, with a compact public API centered on |
| 12 | +`Validator` and `Schema`. |
| 13 | +<!-- #description --> |
| 14 | + |
| 15 | +<!-- include https://github.com/devlooped/.github/raw/main/osmf.md --> |
| 16 | + |
| 17 | +<!-- #content --> |
| 18 | +## Installation |
| 19 | + |
| 20 | +```shell |
| 21 | +dotnet add package Schematron |
| 22 | +``` |
| 23 | + |
| 24 | +The package targets `netstandard2.0` and `net8.0`. |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +The package exposes two primary entry points: |
| 29 | + |
| 30 | +- `Validator` loads Schematron and XML Schema definitions and validates XML documents. |
| 31 | +- `Schema` loads and inspects Schematron documents programmatically. |
| 32 | + |
| 33 | +### One-shot validation with a standalone Schematron schema |
| 34 | + |
| 35 | +```csharp |
| 36 | +using Schematron; |
| 37 | + |
| 38 | +var validator = new Validator(); |
| 39 | +validator.AddSchema("order.sch"); |
| 40 | + |
| 41 | +try |
| 42 | +{ |
| 43 | + validator.Validate("order.xml"); |
| 44 | + Console.WriteLine("Document is valid."); |
| 45 | +} |
| 46 | +catch (ValidationException ex) |
| 47 | +{ |
| 48 | + Console.WriteLine(ex.Message); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +`Validate` returns the loaded `IXPathNavigable` document on success and throws |
| 53 | +`ValidationException` when XML Schema or Schematron validation fails. |
| 54 | + |
| 55 | +### Validating XSD plus embedded Schematron |
| 56 | + |
| 57 | +When the schema is a W3C XML Schema document with embedded Schematron, `Validator` runs |
| 58 | +both validations in one pass: |
| 59 | + |
| 60 | +```csharp |
| 61 | +using Schematron; |
| 62 | + |
| 63 | +var validator = new Validator(OutputFormatting.XML); |
| 64 | + |
| 65 | +// Use the overload with the target namespace when the XSD imports or includes other schemas. |
| 66 | +validator.AddSchema("http://example.com/po-schematron", "po-schema.xsd"); |
| 67 | + |
| 68 | +try |
| 69 | +{ |
| 70 | + validator.Validate("purchase-order.xml"); |
| 71 | +} |
| 72 | +catch (ValidationException ex) |
| 73 | +{ |
| 74 | + Console.WriteLine(ex.Message); // XML formatted output |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### Schematron-only validation for in-memory XML |
| 79 | + |
| 80 | +If you already have an `IXPathNavigable`, use `ValidateSchematron` to skip XML Schema |
| 81 | +validation and evaluate only the loaded Schematron rules: |
| 82 | + |
| 83 | +```csharp |
| 84 | +using System.Xml.XPath; |
| 85 | +using Schematron; |
| 86 | + |
| 87 | +var validator = new Validator(); |
| 88 | +validator.AddSchema("rules.sch"); |
| 89 | + |
| 90 | +var document = new XPathDocument("order.xml"); |
| 91 | +validator.ValidateSchematron(document); |
| 92 | +``` |
| 93 | + |
| 94 | +### Selecting the phase, formatter, and return type |
| 95 | + |
| 96 | +`Validator` lets you control the active phase, output format, and result type: |
| 97 | + |
| 98 | +```csharp |
| 99 | +using Schematron; |
| 100 | +using Schematron.Formatters; |
| 101 | + |
| 102 | +var validator = new Validator(OutputFormatting.XML, NavigableType.XmlDocument) |
| 103 | +{ |
| 104 | + Phase = "paymentInfo", |
| 105 | + Formatter = new XmlFormatter(), |
| 106 | +}; |
| 107 | + |
| 108 | +validator.AddSchema("purchase-order.sch"); |
| 109 | +var result = validator.Validate("purchase-order.xml"); |
| 110 | +``` |
| 111 | + |
| 112 | +Use `Phase.All` (`#ALL`) to evaluate every pattern regardless of phase activation. |
| 113 | + |
| 114 | +### Loading and inspecting a schema |
| 115 | + |
| 116 | +Use `Schema` directly when you want to inspect a Schematron document without validating |
| 117 | +an instance document yet: |
| 118 | + |
| 119 | +```csharp |
| 120 | +using Schematron; |
| 121 | + |
| 122 | +var schema = new Schema(); |
| 123 | +schema.Load("rules.sch"); |
| 124 | + |
| 125 | +Console.WriteLine(schema.Title); |
| 126 | +Console.WriteLine(schema.SchematronEdition); |
| 127 | +Console.WriteLine(schema.DefaultPhase); |
| 128 | +Console.WriteLine(schema.IsLibrary); |
| 129 | +Console.WriteLine(schema.Patterns.Count); |
| 130 | +Console.WriteLine(schema.Lets.Contains("maxAge")); |
| 131 | +``` |
| 132 | + |
| 133 | +This is useful for tooling, analyzers, test helpers, or apps that need to inspect declared |
| 134 | +phases, patterns, diagnostics, parameters, and `let` bindings. |
| 135 | + |
| 136 | +## Core API |
| 137 | + |
| 138 | +Schematron keeps the public surface intentionally small: |
| 139 | + |
| 140 | +- `Validator` is the main entry point for loading schemas and validating XML. |
| 141 | +- `Schema` loads and inspects Schematron documents programmatically. |
| 142 | +- `OutputFormatting` selects the built-in output style: `Default`/`Log`, `Simple`, |
| 143 | + `Boolean`, or `XML`. |
| 144 | +- `Validator.Phase`, `Validator.Formatter`, and `Validator.ReturnType` let you choose |
| 145 | + the active phase, custom output formatter, and returned document type. |
| 146 | +- `BadSchemaException` signals invalid schema input, while `ValidationException` |
| 147 | + signals XML or Schematron validation failures. |
| 148 | + |
| 149 | +## Supported features |
| 150 | + |
| 151 | +The package currently supports the main scenarios expected for a v1 release, including: |
| 152 | + |
| 153 | +- Standalone `.sch` schemas and Schematron embedded in W3C XML Schema |
| 154 | +- ISO Schematron namespace (`http://purl.oclc.org/dsdl/schematron`) and legacy ASCC namespace compatibility |
| 155 | +- ISO Schematron 2025 features such as `<library>`, phase `@when`, rule `@visit-each`, rule flags, and schema parameters |
| 156 | +- Schema-, pattern-, and rule-level `let` bindings |
| 157 | +- Diagnostics, severity metadata, abstract patterns/rules, and groups |
| 158 | +<!-- #content --> |
| 159 | + |
| 160 | +<!-- include https://github.com/devlooped/sponsors/raw/main/footer.md --> |
0 commit comments