简体   繁体   English

从URL工作但从本地文件创建XML架构失败?

[英]Creating XML Schema from URL works but from Local File fails?

I need to validate XML Schema Instance (XSD) documents which are programmatically generated so I'm using the following Java snippet, which works fine: 我需要验证以编程方式生成的XML Schema Instance(XSD)文档,以便我使用以下Java代码段,它可以正常工作:

SchemaFactory factory = SchemaFactory.newInstance(
    XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema xsdSchema = factory.newSchema( // Reads URL every time...
    new URL("http://www.w3.org/2001/XMLSchema.xsd"));
Validator xsdValidator = xsdSchema.newValidator();
xsdValidator.validate(new StreamSource(schemaInstanceStream));

However, when I save the XML Schema definition file locally and refer to it this way: 但是,当我在本地保存XML Schema定义文件并以这种方式引用它时:

Schema schema = factory.newSchema(
    new File("test/xsd/XMLSchema.xsd"));

It fails with the following exception: 它失败,出现以下异常:

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'file:/Users/foo/bar/test/xsd/XMLSchema.xsd', because 1) could not find the document; org.xml.sax.SAXParseException:schema_reference.4:无法读取架构文档'file:/Users/foo/bar/test/xsd/XMLSchema.xsd',因为1)找不到该文档; 2) the document could not be read; 2)文件无法阅读; 3) the root element of the document is not <xsd:schema>. 3)文档的根元素不是<xsd:schema>。

I've ensured that the file exists and is readable by doing exists() and canRead() assertions on the File object. 我确保文件存在并且可以通过对File对象执行exists()canRead()断言来读取。 I've also downloaded the file with a couple different utilities (web browser, wget) to ensure that there is no corruption. 我还使用几个不同的实用程序(Web浏览器,wget)下载了该文件,以确保没有损坏。

Any idea why I can validate XSD instance documents when I generate the schema from the HTTP URL but I get the above exception when trying to generate from a local file with the same contents? 当我从HTTP URL生成模式时,我知道为什么我可以验证XSD实例文档,但是当我尝试从具有相同内容的本地文件生成时,我得到了上述异常?

[Edit] [编辑]

To elaborate, I've tried multiple forms of factory.newSchema(...) using Readers and InputStreams (instead of the File directly) and still get exactly the same error. 为了详细说明,我尝试了多种形式的factory.newSchema(...)使用Readers和InputStreams(而不是直接使用File)并且仍然得到完全相同的错误。 Moreover, I've dumped the file contents before using it or the various input streams to ensure it's the right one. 此外,我在使用它或各种输入流之前已经转储了文件内容以确保它是正确的。 Quite vexing. 相当令人烦恼。

Full Answer 完整答案

It turns out that there are three additional files referenced by XML Schema which must be also stored locally and XMLSchema.xsd contains an import statement whose schemaLocation attribute must be changed. 事实证明,XML Schema引用了三个附加文件,它们也必须存储在本地,而XMLSchema.xsd包含一个必须更改其schemaLocation属性的import语句。 Here are the files that must be saved in the same directory: 以下是必须保存在同一目录中的文件:

  1. XMLSchema.xsd - change schemaLocation to " xml.xsd " in the "import" element for XML Namespace. XMLSchema.xsd - 在XML Namespace的“import”元素xml.xsd schemaLocation更改为“ xml.xsd ”。
  2. XMLSchema.dtd - as is. XMLSchema.dtd - 按原样。
  3. datatypes.dtd - as is. datatypes.dtd - 按原样。
  4. xml.xsd - as is. xml.xsd - 按原样。

Thanks to @Blaise Doughan and @Tomasz Nurkiewicz for their hints. 感谢@Blaise Doughan和@Tomasz Nurkiewicz的提示。

I assume you are trying to load XMLSchema.xsd . 我假设您正在尝试加载XMLSchema.xsd Please also download XMLSchema.dtd and datatypes.dtd and put them in the same directory. 还请下载XMLSchema.dtddatatypes.dtd并将它们放在同一目录中。 This should push you a little bit further. 这应该会让你更进一步。

UPDATE UPDATE

Is XMLSchema.xsd importing any other schemas by relative paths that are not on the local file systen? XMLSchema.xsd是否通过不在本地文件systen上的相对路径导入任何其他模式?


Your relative path may not be correct wrt your working directory. 您的工作目录中的相对路径可能不正确。 Try entering a fully qualified path to eliminate the possibility that the file can not be found. 尝试输入完全限定的路径以消除无法找到文件的可能性。

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'file:/Users/foo/bar/test/xsd/XMLSchema.xsd', because 1) could not find the document; org.xml.sax.SAXParseException:schema_reference.4:无法读取架构文档'file:/Users/foo/bar/test/xsd/XMLSchema.xsd',因为1)找不到该文档; 2) the document could not be read; 2)文件无法阅读; 3) the root element of the document is not . 3)文档的根元素不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM