我在尝试进行XML验证。运行时我得到一个模式列表(可能包装在JAR中)。验证的结果取决于我提供模式的顺序。
这是我的做法:
private void validateXml(String xml, List<URI> schemas){
Source[] source = new StreamSource[schemas.size()];
int i=0;
for (URI f : schemas){
source[i++] = new StreamSource(f.openStream());
}
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NA_URI);
sf.setResourceResolver(new MyClassPathResourceResolver());
Schema schema = schemaFactory.newSchema(source);
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new ByteArrayInputStream(xml.getBytes()));
如果传递的模式集不以与XML根元素相对应的模式开头,则此方法将失败。这个问题有解决办法吗?还是我做错了什么?