问题描述
我正在使用 Java 5 javax.xml.validation.Validator 来验证 XML 文件.我已经为一个只使用导入并且一切正常的模式完成了它.现在我正在尝试使用另一个使用导入和一个包含的模式进行验证.我遇到的问题是主模式中的元素被忽略,验证说它找不到他们的声明.
I'm using Java 5 javax.xml.validation.Validator to validate XML file. I've done it for one schema that uses only imports and everything works fine. Now I'm trying to validate with another schema that uses import and one include. The problem I have is that element in the main schema are ignored, the validation says it cannot find their declaration.
这是我构建架构的方式:
Here is how I build the Schema:
现在这里是 main.xsd 中声明的摘录
Now here is the extract of the declaration in main.xsd
如果我将包含的 XSD 的代码复制到 main.xsd 中,它可以正常工作.如果我不这样做,验证不会找到元素"的声明.
If I copy the code of my included XSD in the main.xsd, it works fine. If I don't, validation doesn't find the declaration of "Element".
推荐答案
你需要使用一个 LSResourceResolver 使其工作.请看下面的示例代码.
you need to use an LSResourceResolver for this to work. please take a look at the sample code below.
验证方法:
资源解析器实现:
资源解析器返回的输入实现:
The Input implemetation returned by the resource resolver:
这篇关于如何使用具有包含 XSD 的 Java 验证 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!