我正在尝试使用 Android 15 中的现有语法库创建语法(c 是上下文(
Grammars g = GrammarFactory.newInstance().createGrammars(c.getAssets().open("svg.xsd"));
从 svg.xsd,它导入另外两个架构:xlink.xsd 和 namespace.xsd。这两个文件是沿着svg.xsd出现的(正如你所看到的,它们位于svg.xsd的根目录中(。但是我没有创建语法,而是得到了这个异常:
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
的两行是:
<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"/>
到目前为止我尝试过:
- 我天真地尝试在 svg.xsd 中合并两个 xsd 只明白我根本不知道 xsd 文件是如何工作的。
- 跟随源到
SchemaInformedGrammars.class
但我不明白systemId
是什么。
( - 编辑(遵循此处支持的建议(第二篇文章(我使用
com.siemens.ct.exi.grammars.XSDGrammarsBuilder
来创建语法:
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);
只是得到完全相同的错误...
我的问题:问题似乎是解析器找不到另外两个文件。有没有办法以某种方式包含这些文件,以便解析器可以找到它们?
来自exifient开发团队的Danielpeintner将我推向了正确的方向(问题在这里(。
Daniel建议我改用createGrammar(String, XMLEntityResolver)
,而不是使用createGrammar(InputStream)
,并提供我自己的XMLEntityResolver
实现。我的实现是这样的:
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)
:
exiFactory.setGrammars(GrammarFactory.newInstance().createGrammars("svg.xsd", new XSDResolver(c)));