我该在哪里找到Java实现的ISO Schematron验证器?

6
两年前,ISO Schematron标准已经发布,但我仍然找不到使用ISO Schematron XSLT文件的Java实现(而不是来自较旧版本Schematron的文件,例如这里:http://uploading.com/files/c9c9cb87/SchematronXpath.jar/)。
有没有人知道一个能够轻松从Java方法调用的生产就绪的ISO模式验证器?

1
不完全是重复,但请参阅 如何在Java中对文档进行Schematron模式验证? - Pops
4个回答

9
此外,您还可以使用ph-schematron,它不仅提供将Schematron转换为XSLT的支持,还提供本地Java验证,几乎在所有情况下都比XSLT版本快。 有关详细信息以及快速介绍,请参见https://github.com/phax/ph-schematron/。 用于检查XML文件是否与Schematron文件匹配的示例代码:
public static boolean validateXMLViaPureSchematron (File aSchematronFile, File aXMLFile) throws Exception { 
  final ISchematronResource aResPure = SchematronResourcePure.fromFile (aSchematronFile);
  if (!aResPure.isValidSchematron ()) 
    throw new IllegalArgumentException ("Invalid Schematron!"); 
  return aResPure.getSchematronValidity(new StreamSource(aXMLFile)).isValid ();
}

你好Philip,使用ph-schematron是个好主意,但需要有相应的支持教程。我找不到一个全面的关于它的教程。 - GokcenG
谢谢提供信息。如果您能在GitHub项目上提交问题,那就太好了 :) - Philip Helger
发现 ph-schematron 非常有用且易于使用。我可能会写一篇关于如何使用它的教程。感谢 Philip 提供这个库。 - MohamedSanaulla
在http://phax.github.io/ph-schematron/上有一个基本的教程 - 欢迎任何扩展。 - Philip Helger
@PhilipHelger .. 谢谢您的实现和教程。但我仍然不清楚如何运行?您能否发布一份关于这个的逐步简单教程? - Sunil Garg
@SunilGarg 好的,我会做的。如果您能在GitHub上发布一个问题,那就太好了。 - Philip Helger

6

Probatron4j 可以校验 ISO Schematron。该网站提供了一个单独的、自包含的 JAR 文件,可在命令行中运行,但如果您有其源代码,那么很容易从 Java 方法中调用 Probatron。以下是我简化后的做法:

public boolean validateSchematron(InputStream xmlDoc, File schematronSchema) {
    // Session = org.probatron.Session; think of it as the Main class
    Session theSession = new Session();
    theSession.setSchemaSysId(schematronSchema.getName());
    theSession.setFsContextDir(schematronSchema.getAbsolutePath());

    // ValidationReport = org.probatron.ValidationReport; the output class
    ValidationReport validationReport = null;
    try
    {
        validationReport = theSession.doValidation(xmlDoc);
    }
    catch(Exception e) { /* ignoring to keep this answer short */ }

    if (validationReport == null ||
        !validationReport.documentPassedValidation()) {
        return false;
    }
    return true;
}

您需要进行一些小修改来告诉Probatron它不是在JAR文件中运行,但这并不需要很长时间。


theSession.doValidation(xmlDoc); 不接受 InputStream,只接受 String。 - btiernay
2
请注意,Probatron是根据Affero GPL许可的。我不是律师,但在商业环境中使用Probatron之前,您可能需要咨询一位律师。 - Mattias Jiderhamn

0
你可以查看SchematronAssert(声明:这是我的代码)。它主要用于单元测试,但你也可以将其用于普通代码。它是使用XSLT实现的。
单元测试示例:
ValidationOutput result = in(booksDocument)
    .forEvery("book")
    .check("author")
    .validate();
assertThat(result).hasNoErrors();

独立验证示例:

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

使用SVRL的对象表示进行工作:

ValidationOutput output = validator.validate(xmlSource, schemaSource);
// look at the output
output.getFailures() ... 
output.getReports() ...

-1

Jing 库对我来说很有效。


3
虽然 Jing 能够与 Schematron 1.5 配合使用,但它尚不支持 ISO 版本的 Schematron。请参见 issue 23 - Pops
更新:Jing已经移动到这个Github仓库 - wearego

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接