6

ISO Schematron 标准已经推出两年了,但我仍然无法找到使用 ISO Schematron XSLT 文件的 Java 实现(与旧版本 Schematron 的文件相反,例如:http: //uploading.com /files/c9c9cb87/SchematronXpath.jar/)。

有谁知道可以从 Java 方法轻松调用的生产就绪 ISO 模式验证器?

4

4 回答 4

9

Additionally you may use ph-schematron which provides both support for conversion to XSLT as well as a native plain Java validation, which is quicker than the XSLT version in nearly all cases. See https://github.com/phax/ph-schematron/ for the details as well as a quick intro. Example code to check if an XML file matches a Schematron file:

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 ();
}
于 2013-11-07T17:08:29.170 回答
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 文件中运行的,但这不会花很长时间。

于 2012-12-04T15:45:56.113 回答
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() ...
于 2014-03-09T19:28:47.587 回答
-1

jing图书馆为我工作。

于 2012-07-11T13:25:20.897 回答