Pankaj Pant
Pankaj Pant

Reputation: 13

Validate XML against schematron using SAXON EE edition

I am evaluating SAXON EE edition to validate XML against xsd and schematron.

Can someone help me in resolving the following queries:

  1. While validating xml document against xsd, can we also get xpath of that error node along with errors in plain text. Currently I am getting error only.

  2. Can we validate xml against schematron using Saxon EE version? Any code sample would be a great help.

Thanks.

Upvotes: 1

Views: 1059

Answers (1)

Michael Kay
Michael Kay

Reputation: 163587

1. While validating xml document against xsd, can we also get xpath of that error node.

Yes, the error information includes an XPath reference to the invalid node (in most cases: there are some cases such as duplicate IDs where there isn't one specific node in error).

If you generate an XML validity report using SchemaValidator.SetValidityReporting() then the resulting report will include the path information. Here's an example:

<?xml version="1.0" encoding="UTF-8"?>
<validation-report xmlns="http://saxon.sf.net/ns/validation"
                   system-id="file:/Users/mike/repo2/samples/data/books-invalid.xml">
   <error line="3"
          column="17"
          path="/Q{}BOOKLIST[1]/Q{}BOOKS[1]/@x"
          xsd-part="1"
          constraint="cvc-complex-type.3">Attribute @x is not allowed on element &lt;BOOKS&gt;</error>
   <error line="10"
          column="17"
          path="/Q{}BOOKLIST[1]/Q{}BOOKS[1]/Q{}ITEM[1]/Q{}PRICE[1]"
          xsd-part="2"
          constraint="cvc-datatype-valid.1">The content "$0.2" of element &lt;PRICE&gt; does not match the required simple type. Cannot convert string to decimal: $0.2</error>
   <error line="21"
          column="20"
          path="/Q{}BOOKLIST[1]/Q{}BOOKS[1]/Q{}ITEM[2]/Q{}PUB-DATE[1]"
          xsd-part="2"
          constraint="cvc-datatype-valid.1">The content "2002-02-31" of element &lt;PUB-DATE&gt; does not match the required simple type. Invalid date "2002-02-31" (Non-existent date)</error>
   <error line="42"
          column="22"
          path="/Q{}BOOKLIST[1]/Q{}BOOKS[1]/Q{}ITEM[3]/Q{}REPUTATION[1]"
          xsd-part="1"
          constraint="cvc-complex-type.2.4">In content of element &lt;ITEM&gt;: The content model does not allow element &lt;REPUTATION&gt; to appear immediately after element &lt;WEIGHT&gt;. No further elements are allowed at this point. </error>
   <meta-data>
      <validator name="SAXON-EE" version="9.8.0.9"/>
      <results errors="4" warnings="0"/>
      <schema file="books.xsd" xsd-version="1.1"/>
      <run at="2018-03-07T15:22:04.847Z"/>
   </meta-data>
</validation-report>

You can also get the information if you supply an IInvalidityHandler as a callback to the SchemaValidator, though this requires a bit more digging. Saxon calls your IInvalidityHandler supplying a StaticError object (which is a bit of a misnomer). The StaticError object doesn't have the path information directly available, but it contains a reference to an XPathException object which can be cast to a ValidationException, and ValidationException has a method getPath() which returns this information if available.

2. Can we validate xml against schematron.

Saxon doesn't include a schematron validator per se, though many of the third-party tools that do schematron validation make use of Saxon "under the hood". I'm not up-to-date with the situation on .NET - but essentially there are two kinds of Schematron processor: those that generate XSLT code from the schematron schema (which typically use Saxon both to generate the XSLT and to execute it), and "native" processors. Searching for "schematron on .NET" gives you quite a number of projects, but I have no idea of their current status or quality.

Upvotes: 2

Related Questions