Search code examples
c#xmlschematron

How do I get Schematron.net to trigger diagnostic?


I am using the Schematron.net nuget package to validate some XML but I can't figure out how to use the diagnostic tag correctly. I have the following XML:

<bk:books xmlns:bk="http://www.example.com/books">
  <bk:book publisher="QUE">
    <bk:title>XML By Example</bk:title>
    <!--<bk:author>Benoit Marchal</bk:author>-->
    <bk:publication-date>1999-12-31</bk:publication-date>
    <bk:retailPrice>9.95</bk:retailPrice>
  </bk:book>
  <bk:book publisher="Addison Wesley">
    <bk:title>Essential C++</bk:title>
    <bk:author>Stanley Lippman</bk:author>
    <bk:publication-date>2000-10-31</bk:publication-date>
    <bk:retailPrice>29.95</bk:retailPrice>
  </bk:book>
</bk:books>

And my simplified Schematron schema looks like this:

<schema xmlns="http://www.ascc.net/xml/schematron"
        schemaVersion="1.01" >
  <title>A Schema for Books</title>
  <ns prefix="bk" uri="http://www.example.com/books" />
  <pattern id="authorTests">
    <rule context="bk:book">
      <assert test="count(bk:author)!= 0" diagnostics="bookTest">
        A book must have at least one author
      </assert>
    </rule>
  </pattern>
  <diagnostics>
    <diagnostic id="bookTest">
      The book that has no author is <value-of select="bk:title"></value-of>
    </diagnostic>
  </diagnostics>
</schema>

The code I'm running to exercise this schema looks like this:

   try
    {
        var bookValidator = new Validator();
        bookValidator.AddSchema("book.xsd");
        bookValidator.Validate("book.xml");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

When this runs I get the following console output:

Results from Schematron validation: A Schema for Books
    From pattern ""
        Assert fails: A book must have at least one author
        At: /bk:bk:books[1]/bk:bk:book[1]
            <bk:book publisher="QUE">...</bk:book>
            (Line: 3, Column: 4)
            xmlns:bk="http://www.example.com/books"

Why don't I see the output from the diagnostic tag in my console?


Solution

  • It seems as if the NMatrix.Schematron implementation doesn't support the full set of Schematron features. After looking at the source code I can't find any reference to the diagnostics elements at all. In addition the code seems to only support XPath 1.0 and the earlier implementations of Schematron (before the namespace was changed with ISO standardisation). Best advice is to implement a Schematron pipeline using the Schematron "Skeleton" implementation as a guide, which can be found here: http://schematron.com/front-page/the-schematron-skeleton-implementation/