验证XSD文件 [英] Validate a XSD file
问题描述
我想验证XSD文件(不是XML)。我使用的方法是将XSD视为任何其他XML文件,并使用此 www.w3.org /2001/XMLSchema.xsd 作为架构。
I want to validate an XSD file (not XML). The approach i am using is to treat the XSD as any other XML file and use this www.w3.org/2001/XMLSchema.xsd as the schema.
我使用以下代码:
String schemaLang = "http://www.w3.org/2001/XMLSchema";
SchemaFactory factory = SchemaFactory.newInstance(schemaLang);
Schema schema = factory.newSchema(new StreamSource("C:\\Users\\aprasad\\Desktop\\XMLSchema.xsd"));
Validator validator = schema.newValidator();
validator.validate(new StreamSource("shiporder.xsd"));
但是我收到以下错误:
无法读取模式文档'XMLSchema.xsd',因为1)找不到文档; 2)文件无法阅读; 3)文档的根元素不是< xsd:schema>
。
Failed to read schema document 'XMLSchema.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>
.
不确定错误是什么,因为文件路径是正确的。
Not sure what the error is as the file path is correct.
请告诉我验证XSD文件的正确方法。
Please tell me the correct approach to validate an XSD file.
推荐答案
您需要在 XMLSchema.xsd
旁边另外添加两个文件。这些是:
You need to have two additional files right beside XMLSchema.xsd
. These are:
XMLSchema.dtd
datatypes.dtd
XMLSchema.xsd
引用这两个文件。
在旁边,所以如果 XMLSchema.xsd
位于 C:/XMLSchema.xsd
那么你必须有 C:/XMLSchema.dtd
和 C:/datatypes.dtd
。
Right beside, so if XMLSchema.xsd
is located at C:/XMLSchema.xsd
then you have to have C:/XMLSchema.dtd
and C:/datatypes.dtd
.
SchemaFactory
实例默认使用(参见 SchemaFactory.setResourceResolver(LSResourceResolver)
)一个名为的内部类XMLCatalogResolver
实现 LSResourceResolver的
。前者(我假设)在 referer 旁边查找引用文件。
SchemaFactory
instances use (see SchemaFactory.setResourceResolver(LSResourceResolver)
) by default an internal class called XMLCatalogResolver
which implements LSResourceResolver
. The former (I assume) looks for referenced files beside the referer.
如果你看起来非常那么您的 SAXParseException
的原因是 FileNotFoundException
,表示系统找不到 XMLSchema .dtd
file。
If you look really hard then the cause of your SAXParseException
is a FileNotFoundException
that says the the system couldn't find the XMLSchema.dtd
file.
除此之外,您的代码还可以(以及您的架构)。
Other than this, your code is OK (and your schema too).
这篇关于验证XSD文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!