使用 Java 验证 XML 文件,XSD 具有 include

Validate XML file using Java with an XSD having an include

提问人:Dangel 提问时间:6/27/2023 更新时间:6/27/2023 访问量:70

问:

我需要对 xsd 架构执行 xml 验证。xsd 架构存储在数据库表中。许多方案通过导入相互连接,包括。我将一个架构数组传递给验证函数。如果在 xsd 架构中只找到“导入”,则一切正常。如果“include” - 我收到错误:“org.xml.sax.SAXParseException;cvc-elt.1.a:找不到元素的声明”。

// p_schemasArr[] - xsd array. It has all the schemes that are connected via include and import. The array is sorted. The last one in array b includes all the previous ones.
// e - Total xsd schema. There can be many schemes (up to 130).
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source[] sources = new Source[e];
for (int i = 0; i < e; i++) {
  sources[i] = new StreamSource(new StringReader(p_schemasArr[i]), "xsd_" + i);
}
Schema schema = schemaFactory.newSchema(sources);
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new StringReader(xmlFileSource.getValue())));
Java XML 验证 XSD

评论


答:

0赞 Michael Kay 6/27/2023 #1

请参阅标记为“未解决”的 https://issues.apache.org/jira/browse/XERCESJ-1130

我相信这将与 Saxon 模式验证器一起按预期工作,至少如果您设置了适当的选项。