根据多个架构定义验证 XML 文件
我正在尝试根据许多不同的模式验证 XML 文件(对人为的示例表示歉意):
- a.xsd
- b.xsd
- c.xsd
c.xsd 特别是导入 b.xsd 和 b.xsd 导入 a.xsd,使用:
我尝试通过 Xerces 按以下方式执行此操作:
XMLSchemaFactory xmlSchemaFactory = new XMLSchemaFactory();
Schema schema = xmlSchemaFactory.newSchema(new StreamSource[] { new StreamSource(this.getClass().getResourceAsStream("a.xsd"), "a.xsd"),
new StreamSource(this.getClass().getResourceAsStream("b.xsd"), "b.xsd"),
new StreamSource(this.getClass().getResourceAsStream("c.xsd"), "c.xsd")});
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new StringReader(xmlContent)));
但这无法正确导入所有三个架构导致无法将名称“blah”解析为“group”组件。
我已经使用 Python 成功验证了这一点,但在使用 Java 6.0 和 Xerces 2.8.1 时遇到了实际问题。 任何人都可以建议这里出了什么问题,或者验证我的 XML 文档的更简单的方法吗?
I'm trying to validate an XML file against a number of different schemas (apologies for the contrived example):
- a.xsd
- b.xsd
- c.xsd
c.xsd in particular imports b.xsd and b.xsd imports a.xsd, using:
<xs:include schemaLocation="b.xsd"/>
I'm trying to do this via Xerces in the following manner:
XMLSchemaFactory xmlSchemaFactory = new XMLSchemaFactory();
Schema schema = xmlSchemaFactory.newSchema(new StreamSource[] { new StreamSource(this.getClass().getResourceAsStream("a.xsd"), "a.xsd"),
new StreamSource(this.getClass().getResourceAsStream("b.xsd"), "b.xsd"),
new StreamSource(this.getClass().getResourceAsStream("c.xsd"), "c.xsd")});
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new StringReader(xmlContent)));
but this is failing to import all three of the schemas correctly resulting in cannot resolve the name 'blah' to a(n) 'group' component.
I've validated this successfully using Python, but having real problems with Java 6.0 and Xerces 2.8.1. Can anybody suggest what's going wrong here, or an easier approach to validate my XML documents?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
因此,为了防止其他人在这里遇到同样的问题,我需要从单元测试中加载父架构(和隐式子架构)(作为资源)来验证 XML 字符串。 我使用 Xerces XMLSchemFactory 和 Java 6 验证器来完成此操作。
为了通过包含正确加载子架构,我必须编写一个自定义资源解析器。 代码可以在此处找到:
https://code.google.com/p/xmlsanity/source/browse/src/com/arc90/xmlsanity/validation/ResourceResolver.java
要使用解析器,请在架构工厂上指定它:
并且它将使用它通过类路径解析您的资源(在我的例子中是从 src/main/resources )。 欢迎对此提出任何意见...
So just in case anybody else runs into the same issue here, I needed to load a parent schema (and implicit child schemas) from a unit test - as a resource - to validate an XML String. I used the Xerces XMLSchemFactory to do this along with the Java 6 validator.
In order to load the child schema's correctly via an include I had to write a custom resource resolver. Code can be found here:
https://code.google.com/p/xmlsanity/source/browse/src/com/arc90/xmlsanity/validation/ResourceResolver.java
To use the resolver specify it on the schema factory:
and it will use it to resolve your resources via the classpath (in my case from src/main/resources). Any comments are welcome on this...
http://www.kdgregory.com/index.php?page=xml.parsing
“单个文档的多个架构”部分
基于该文档的解决方案:
http://www.kdgregory.com/index.php?page=xml.parsing
section 'Multiple schemas for a single document'
My solution based on that document:
Xerces 中的模式内容 (a) 非常非常迂腐,(b) 当它不喜欢它所发现的内容时,会给出完全无用的错误消息。 这是一个令人沮丧的组合。
python 中的模式可能更加宽容,并且允许模式中的小错误不被报告。
现在,如果如您所说,c.xsd 包含 b.xsd,并且 b.xsd 包含 a.xsd,则无需将所有三个加载到模式工厂中。 它不仅没有必要,而且可能会混淆 Xerces 并导致错误,所以这可能是您的问题。 只需将 c.xsd 传递给工厂,并让它自己解析 b.xsd 和 a.xsd,它应该相对于 c.xsd 执行此操作。
The schema stuff in Xerces is (a) very, very pedantic, and (b) gives utterly useless error messages when it doesn't like what it finds. It's a frustrating combination.
The schema stuff in python may be a lot more forgiving, and was letting small errors in the schema go past unreported.
Now if, as you say, c.xsd includes b.xsd, and b.xsd includes a.xsd, then there's no need to load all three into the schema factory. Not only is it unnecessary, it will likely confuse Xerces and result in errors, so this may be your problem. Just pass c.xsd to the factory, and let it resolve b.xsd and a.xsd itself, which it should do relative to c.xsd.
来自 xerces 文档:
http://xerces.apache.org/xerces2-j/faq-xs.html
From the xerces documentation :
http://xerces.apache.org/xerces2-j/faq-xs.html
我遇到了同样的问题,经过调查发现了这个解决方案。 这个对我有用。
Enum
设置不同的XSD
:验证方法:
I faced the same problem and after investigating found this solution. It works for me.
Enum
to setup the differentXSDs
:Method to validate:
我最终使用了这个:
记住更改:
1)参数“http://your_url_schema_location”代表你的xsd文件位置。
2) 字符串 "/home/user/myfile.xml" 指向您的 xml 文件。
我不必设置变量:
-Djavax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema=org.apache.xerces.jaxp.validation.XMLSchemaFactory
I ended up using this:
Remember to change:
1) The parameter "http://your_url_schema_location" for you xsd file location.
2) The string "/home/user/myfile.xml" for the one pointing to your xml file.
I didn't have to set the variable:
-Djavax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema=org.apache.xerces.jaxp.validation.XMLSchemaFactory
以防万一,有人仍然来这里寻找针对多个 XSD 验证 xml 或对象的解决方案,我在这里提到它
Just in case, anybody still come here to find the solution for validating xml or object against multiple XSDs, I am mentioning it here
如果所有 XSD 属于同一命名空间,则创建一个新的 XSD 并将其他 XSD 导入其中。 然后在 java 中使用新的 XSD 创建模式。
all_in_one.xsd :
If all XSDs belong to the same namespace then create a new XSD and import other XSDs into it. Then in java create schema with the new XSD.
all_in_one.xsd :