首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Exificient异常"schema_reference.4:未能读取带导入关键字的模式文档xxx.xsd“

Exificient异常"schema_reference.4:未能读取带导入关键字的模式文档xxx.xsd“
EN

Stack Overflow用户
提问于 2019-10-06 08:53:17
回答 1查看 1.4K关注 0票数 0

我正在尝试使用Android 15中的exificient-grammars库创建语法(c是上下文)。

代码语言:javascript
复制
Grammars g = GrammarFactory.newInstance().createGrammars(c.getAssets().open("svg.xsd"));

从svg.xsd中导入两个模式: xlink.xsd和namespace.xsd。这两个文件伴随着svg.xsd (如您所见,它们位于svg.xsd 这里的根目录中)。但是,我没有创建语法,而是得到了这个例外:

代码语言:javascript
复制
com.siemens.ct.exi.exceptions.EXIException: Problem occured while building XML Schema Model (XSModel)!
    . [xs-warning] schema_reference.4: Failed to read schema document 'xlink.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
    . [xs-warning] schema_reference.4: Failed to read schema document 'namespace.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

使用导入的svg.xsd的两行如下:

代码语言:javascript
复制
<xs:import namespace="http://www.w3.org/1999/xlink" schemaLocation="xlink.xsd"/>
<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="namespace.xsd"/>

到目前为止我尝试过的:

  1. 我天真地尝试将svg.xsd中的两个xsd合并,只是为了理解我根本不知道xsd文件是如何工作的。
  2. 跟随源直到SchemaInformedGrammars.class,但我不明白systemId是什么。
  3. (编辑)按照支持这里 (第二篇文章)的建议,我使用com.siemens.ct.exi.grammars.XSDGrammarsBuilder创建语法:
代码语言:javascript
复制
XSDGrammarsBuilder xsd = XSDGrammarsBuilder.newInstance();
xsd.loadGrammars(c.getAssets().open("namespace.xsd"));
xsd.loadGrammars(c.getAssets().open("xlink.xsd"));
xsd.loadGrammars(c.getAssets().open("svg.xsd"));
SchemaInformedGrammars sig = xsd.toGrammars();
exiFactory.setGrammars(sig);

却得到了同样的错误..。

我的问题是:问题似乎是解析器无法找到另外两个文件。有没有一种方法可以以某种方式包含这些文件,以便解析器能够找到它们?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-07 13:42:57

来自高级开发团队的danielpeintner把我推到了正确的方向(发布这里)。

丹尼尔建议我不要使用createGrammar(InputStream),而是使用createGrammar(String, XMLEntityResolver),并提供自己的XMLEntityResolver实现。我的实施是:

代码语言:javascript
复制
public class XSDResolver implements XMLEntityResolver {

    Context context;

    public XSDResolver(Context context){
        this.context = context;
    }

    @Override
    public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws XNIException, IOException {
        String literalSystemId = resourceIdentifier.getLiteralSystemId();

        if("xlink.xsd".equals(literalSystemId)){
            InputStream is = context.getAssets().open("xlink.xsd");
            return new XMLInputSource(null, null, null, is, null);
        } else if("namespace.xsd".equals(literalSystemId)){
            InputStream is = context.getAssets().open("namespace.xsd");
            return new XMLInputSource(null, null, null, is, null);
        } else if("svg.xsd".equals(literalSystemId)){
            InputStream is = context.getAssets().open("svg.xsd");
            return new XMLInputSource(null, null, null, is, null);
        }
        return null;
    }
}

像这样调用createGrammar(String, XMLEntityResolver)

代码语言:javascript
复制
exiFactory.setGrammars(GrammarFactory.newInstance().createGrammars("svg.xsd", new XSDResolver(c)));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58255691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档