3

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 only shows how to validate with XSD with NMatrix.Schematron which is not what I need. I have the NMatrix.Schematron DLLs but I dont know how to use them. Does anyone know how to use it?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189

3 Answers3

5

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.

<?xml version="1.0" encoding="utf-8" ?>
<schema xmlns="http://www.ascc.net/xml/schematron" title="Schema for Blah">
<pattern name="A descriptive name">
 <rule context="DataNodeOrElement">
   <assert test="Xpath, where Name is element and @name is attribute"> Error Message </assert>
 </rule>
</pattern>

For example.

<root>
 <version>1</version>
  <data>
   <name surname="rulez">ppumkin</name>
   <age>na</age>
   <title/>
  </data>
 </root>

<rule context="data">
   <assert test="name != ''"> No name specified </assert>
   <assert test="@surname != 'rulez'"> This is not the ppumkin I know!</assert>

   <assert test="number(age) < 110"> Not a number or not specified</assert>
   <assert test="number(age) > 18"> Your under age. Get 'outa!ahere!</assert>
</rule>

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.

Piotr Kula
  • 9,597
  • 8
  • 59
  • 85
3

I recommend that you instead use Rick Jelliffe's reference implementation of ISO Schematron using pure XSLT, preferably XSLT 2.0, with an established XSLT processor such as Michael Kay's Saxon.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • thanks for your help. I am very new to this schematron thing. our clients will give only .xml and .sch files but schematron needs .xslt file too. did I misunderstand it? – Arif YILMAZ Jul 16 '14 at 07:02
  • 1
    A common way to implement Schematron is via XSLT, which is applied in a number of phases to transform the input .sch file into various intermediate forms until finally a validation report, often in HTML, is produced as output. Along the way, some of the intermediate forms themselves are actually XSLT too. See [this diagram](http://www.schematron.com/implementation.html) for a visual of the process. – kjhughes Jul 16 '14 at 11:42
1

Some programs, like oXygen, support schematron validation, but they probably use something like that RI under the hood.

Davio
  • 4,609
  • 2
  • 31
  • 58