I am trying to validate an XML file with Schematron, which is more complicated than XSD. I googled a lot and the best thing that came up is this Microsoft website. But that
It would be better to use Saxon-HE from Nuget as it supports much more than Schematron now a days on .NET>
I am using Schematron.NET - Downlaod the source and examples, compile it or just use the DLL in the examples.
I then completely cut out XSLT of the picture because I did not need it. But some things like choice and similar are missing, but most can be tested using XPath anyway. Because its a bit old it does not implement ALL the features :(
This is an expanded version to validate with Schematron
using NMatrix.Schematron;
...
Schema schematronSchema = new Schema();
schematronSchema.Load(new FileStream("C:/thefile.sch", FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
vld.AddSchema(schematronSchema);
vld.Validate(new MemoryStream(Encoding.UTF8.GetBytes(XML_String)));
I load using a file stream so that the file does not get locked, an annoyance common with the build in XSLT engine for .NET
Then inside the ".sch" file its just simple, much neater in my opinion way to validate schema data.
Error Message
For example.
1
ppumkin
na
No name specified
This is not the ppumkin I know!
Not a number or not specified
Your under age. Get 'outa!ahere!
Remember, an error only occurs when the test fails. ie if you interested in blocking under 18, you need to test if they are over 18. Its a bit weird to get used to.
I have never used Schematron before and honestly, now a days I treat XML like Ebola and anything to do with it - but sometimes we have no choice.
The examples in the Schematron.NET show you how to mix XSLT1/XLST2 with Schematron too for extra control and decisions.