在运行时使用加载模式验证XML,失败取决于模式顺序 [英] Validate XML with loading schemas at runtime, failure depending on schema order

查看:125
本文介绍了在运行时使用加载模式验证XML,失败取决于模式顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to do xml validation. I am being given a list of schemas at run-time (possibly wrapped in a jar). Validation passes or failes based on the order in which I provide the schemas to the 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()));

再次,如果传递的模式集不以xml的根元素引用的模式开始,则会失败。是否有修复此问题或我做错了什么?

again, this fails if the passed set of schema do not start with the schema to which the root element of the xml referrs. Is there a fix to this or am I doing something wrong?

推荐答案

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

By default Xerces will ignore a schema document if it already has a schema document for the same namespace. This behaviour can be changed using the factory option

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

这篇关于在运行时使用加载模式验证XML,失败取决于模式顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆