ISO Schematron标准已经出现两年了,但我仍然无法使用ISO Schematron XSLT文件找到Java实现(而不是来自旧版Schematron的文件,例如:http://uploading.com /files/c9c9cb87/SchematronXpath.jar/).
有没有人知道可以从Java方法轻松调用的生产就绪的ISO模式验证器?
另外,您可以使用ph-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 ();
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
你需要做一些小修改才能让Probatron知道它不是从JAR文件中运行的,但它不需要很长时间.