Schematron4Net
Introduction
Schematron4Net is a Schematron library conforming to .NET Standard 2.0 or .NET 5.0.
Schematron4Net supports ISO Schematron 2016 with query languages XSLT 1.0 and XSLT 2.0 as well as XPath 1.0 and XPath 2.0.
Validating a XML document against Schematron rules is a two step process:
- create a SchematronProcessor instance by loading the Schematron rules
- then use this SchematronProcessor instance to validate the XML document.
So, Schematron validation can be as easy as:
using Schematron4Net;`
. . .
var document = XDocument.Load("xmldocument.xml", LoadOptions.SetBaseUri);
using (var schematronProcessor = SchematronProcessor.LoadFile("schematron.sch"))
{
var validationResult = schematronProcessor.Validate(document);
}
Creating the SchematronProcessor instance
There are a few options to create the SchematronProcessor by loading the Schematron file(s):
- using LoadFile(string schematronFilepath) to load a Schematron file - external references are resolved relative to the specified file path
- loading a Zip file containing the Schematron and all referenced external parts by using LoadZipFile(string zipFilepath, string schematronPath)
- loading a Zip stream containing the Schematron and all referenced external parts by using LoadZipStream(Stream, String) - use this if a Schematron Zip package is embedded as a program resource
Using either a zip file or a zip stream are the preferred ways to handle non-self-contained Schematrons (i.e. that are using includes or doc() references).
Note:
The two methods handling Zip containers will fail when running under older Mono versions due to a bug in Mono's implementation of System.IO.Compression, in this case you have to use the two methods especially provided for older Mono versions:
Once the SchematronProcessor instance is created, it can be used multiple times - until it is disposed, of course.
Using the SchematronProcessor instance for validation
For XmlDocument validation there are two options, giving either only the ValidationResult or a ValidationResult and a full validation report (as SVRL XDocument):
- Validate(XDocument xmlDocument, ValidationOptions validationOptions = null)
- Validate(XValidate(XDocument xmlDocument, out XDocument validationReport, ValidationOptions validationOptions = null)
By specifying the optional ValidationOptions, you can control the executing phases and roles for classifying informational and warning messages.
The result of a validation is one of Valid, Information, Warning or Error of ValidationResult.
If you have requested the validation report, you can find the nodes containing informational, warning and error validation messages by selecting all svrl:failed-assert
and svrl:successful-report
elements:
SchematronProcessor.Validate(document, out var svrl);
XNamespace nsSvrl = "http://purl.oclc.org/dsdl/svrl";
foreach (var element in svrl.Root.Elements(nsSvrl + "failed-assert").Union(svrl.Root.Elements(nsSvrl + "successful-report")))
{
// report information, warning or error
]
The usage of a SchematronProcessor instance for validation is thread safe.