I have created several Schematron assertations and they are all running fine against my XML instances. However, I would like to structure the output of my error messages in such a way, that I later can post-process them, with e.g., Java.
The output of my assertion error message should be
<error>
<errorMessage>My error message</errorMessage>
<violatingElement>The violating element</violatingElement>
</error>
However, a construct such as:
<pattern>
<rule context="/p1:Document/p1:Delivery/p1:DeliveryDetails/p1:ShippingDate">
<report test=".">
<error>
<errorMessage>My error message</errorMessage>
<violatingElement>The violating element</violatingElement>
</error>
</report>
</rule>
</pattern>
does not work. What is the correct way of returning Schematron results in a structured (XML-ified) manner?
Actually to get a Schematron report as XML you should use SVRL. You can learn more about it here: Schematron Validation Report Language (SVRL)
The big advantage of using SVRL is that you can also get detailed information about where the assertion/report was triggered as an XPath.
You can also leverage a native schematron engine, rather than an XSL stylesheet, to deteremine whether the assertions are satisfied. I'm working with one for .NET / C# here.
Schematron by design wants to keep the content of assertions and reports as natural language only. The Standard (available here) encourages the use of the diagnostics element for richer information. It states:
diagnostic messages are contained in <diagonstic> elements (wrapped in a <diagnostics> wrapper) and associated with reports and assertions using an ID/IDREF mechanism.
There is an example showing the use of diagnostics in Annex G of the Standard.