如何让Schematron.net触发诊断?

时间:2017-04-19 04:11:17

标签: c# xml schematron

我正在使用Schematron.net nuget包来验证某些XML,但我无法弄清楚如何正确使用诊断标记。我有以下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>

我简化的Schematron架构如下所示:

<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>

我运行此模式的代码如下所示:

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

当它运行时,我得到以下控制台输出:

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"

为什么我在控制台中看不到诊断标签的输出?

1 个答案:

答案 0 :(得分:0)

似乎NMatrix.Schematron实现并不支持全套Schematron功能。查看源代码后,我根本无法找到对诊断元素的任何引用。此外,代码似乎只支持XPath 1.0和Schematron的早期实现(在使用ISO标准化更改名称空间之前)。最好的建议是使用Schematron&#34; Skeleton&#34;来实现Schematron管道。实施指南,可在此处找到:http://schematron.com/front-page/the-schematron-skeleton-implementation/