在运行时使用加载模式验证XML,失败取决于模式顺序

Joh*_*n B 6 java xml xml-validation

我正在尝试进行xml验证.我在运行时获得了一个模式列表(可能包含在jar中).验证根据我向SchemaFactory提供模式的顺序传递或失败.

这是我在做的事情:

  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()));
Run Code Online (Sandbox Code Playgroud)

再次,如果传递的模式集不是以xml的根元素引用的模式开始,则会失败.有没有解决这个问题或者我做错了什么?

Mic*_*Kay 5

默认情况下,如果模式文档已具有相同名称空间的模式文档,则Xerces将忽略该模式文档.可以使用factory选项更改此行为

http://apache.org/xml/features/validation/schema/handle-multiple-imports