我正尝试使用多个不同的模式对XML文件进行验证(抱歉,这是一个牵强附会的例子):
- a.xsd
- b.xsd
- c.xsd
特别地,c.xsd 导入了 b.xsd ,而 b.xsd 又导入了 a.xsd,使用如下语句:
<xs:include schemaLocation="b.xsd"/>
我正在尝试通过 Xerces 以以下方式实现:
XMLSchemaFactory xmlSchemaFactory = new XMLSchemaFactory();
Schema schema = xmlSchemaFactory.newSchema(new StreamSource[] { new StreamSource(this.getClass().getResourceAsStream("a.xsd"), "a.xsd"),
new StreamSource(this.getClass().getResourceAsStream("b.xsd"), "b.xsd"),
new StreamSource(this.getClass().getResourceAsStream("c.xsd"), "c.xsd")});
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new StringReader(xmlContent)));
但是这个方法无法正确导入所有三个模式,导致无法将名称“blah”解析为“group”组件。
我已经使用Python成功验证了此问题,但在使用Java 6.0和Xerces 2.8.1时遇到了实际问题。 有人可以建议出现了什么错误,或者提供一种更简单的方法来验证我的XML文档吗?
<xsd:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="/xmldsig-core-schema.xsd" />
到使用new StreamSource(this.getClass().getResourceAsStream("parent.xsd")
加载的父XSD中。 - Jaime Hablutzel