How can I validate documents against Schematron schemas in Java?

别等时光非礼了梦想. 提交于 2019-12-03 08:13:37

Jing supports pre-ISO Schematron validation (note that Jing's implementation is based also on XSLT).

There are also XSLT implementations that can be very easily invoked from Java. You need to decide what version of Schematron you are interested in and then get the corresponding stylesheet - all of them should be available from schematron.com. The process is very simple simple, involving basically 2 steps:

  • apply the skeleton XSLT on your Schematron schema to get a new XSLT stylesheet that represents your Schematron schema in XSLT
  • apply the obtained XSLT on your instance document or documents to validate them

JAXP is just an API and it does not require support for Relax NG from an implementation. You need to check the specific implementation that you use to see if that supports Relax NG or not.

A pure Java Schematron implementation is located at https://github.com/phax/ph-schematron/ It brings support for both the XSLT approach and the pure Java approach.

You can check out SchematronAssert (disclosure: my code). It is meant primarily for unit testing, but you may use it for normal code too. It is implemented using XSLT.

Unit testing example:

ValidationOutput result = in(booksDocument)
    .forEvery("book")
    .check("author")
    .validate();
assertThat(result).hasNoErrors();

Standalone validation example:

StreamSource schemaSource = new StreamSource(... your schematron schema ...);
StreamSource xmlSource = new StreamSource(... your xml document ... );
StreamResult output = ... here your SVRL will be saved ... 
// validation 
validator.validate(xmlSource, schemaSource, output);

Work with an object representation of SVRL:

ValidationOutput output = validator.validate(xmlSource, schemaSource);
// look at the output
output.getFailures() ... 
output.getReports() ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!