如何根据 Java 中的 Schematron 模式验证文档?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/910476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I validate documents against Schematron schemas in Java?
提问by brabster
As far as I can tell, JAXP by default supports W3C XML Schema and RelaxNG from Java 6.
据我所知,JAXP 默认支持来自 Java 6 的 W3C XML Schema 和 RelaxNG。
I can see a few APIs, mostly experimental or incomplete, on the schematron.com links page.
我可以在schematron.com 链接页面上看到一些 API,大部分是实验性的或不完整的。
Is there an approach on validating schematron in Java that's complete, efficient and can be used with the JAXP API?
是否有一种完整、高效且可与 JAXP API 一起使用的在 Java 中验证 schematron 的方法?
采纳答案by George Bina
Jingsupports pre-ISO Schematron validation (note that Jing's implementation is based also on XSLT).
Jing支持预 ISO Schematron 验证(注意 Jing 的实现也是基于 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:
还有一些 XSLT 实现可以很容易地从 Java 中调用。您需要决定您对哪个版本的 Schematron 感兴趣,然后获取相应的样式表——所有样式表都应该可以从 schematron.com 获得。过程很简单很简单,基本上包括2个步骤:
- 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
- 在您的 Schematron 模式上应用骨架 XSLT 以获得一个新的 XSLT 样式表,该样式表在 XSLT 中表示您的 Schematron 模式
- 将获得的 XSLT 应用于您的一个或多个实例文档以验证它们
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.
JAXP 只是一个 API,它不需要从实现中支持 Relax NG。您需要检查您使用的特定实现以查看是否支持 Relax NG。
回答by Philip Helger
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.
一个纯 Java Schematron 实现位于https://github.com/phax/ph-schematron/它同时支持 XSLT 方法和纯 Java 方法。
回答by j_maly
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.
您可以查看SchematronAssert(披露:我的代码)。它主要用于单元测试,但您也可以将它用于普通代码。它是使用 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:
使用 SVRL 的对象表示:
ValidationOutput output = validator.validate(xmlSource, schemaSource);
// look at the output
output.getFailures() ...
output.getReports() ...

