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

时间:2021-09-23 17:16:36

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; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

org.xml.sax.SAXParseException:schema_reference.4:无法读取架构文档'file:/Users/foo/bar/test/xsd/XMLSchema.xsd',因为1)找不到文档; 2)文件无法阅读; 3)文档的根元素不是 :schema>

I've ensured that the file exists and is readable by doing exists() and canRead() assertions on the File object. I've also downloaded the file with a couple different utilities (web browser, wget) to ensure that there is no corruption.

我确保文件存在并且可以通过对File对象执行exists()和canRead()断言来读取。我还使用几个不同的实用程序(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. Moreover, I've dumped the file contents before using it or the various input streams to ensure it's the right one. Quite vexing.

为了详细说明,我尝试了多种形式的factory.newSchema(...)使用Readers和InputStreams(而不是直接使用File)并且仍然得到完全相同的错误。此外,我在使用它或各种输入流之前已经转储了文件内容,以确保它是正确的。相当令人烦恼。

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. Here are the files that must be saved in the same directory:

事实证明,XML Schema引用了三个附加文件,它们也必须存储在本地,而XMLSchema.xsd包含一个必须更改其schemaLocation属性的import语句。以下是必须保存在同一目录中的文件:

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

Thanks to @Blaise Doughan and @Tomasz Nurkiewicz for their hints.

感谢@Blaise Doughan和@Tomasz Nurkiewicz的提示。

2 个解决方案

#1


2  

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; 2) the document could not be read; 3) the root element of the document is not .

org.xml.sax.SAXParseException:schema_reference.4:无法读取架构文档'file:/Users/foo/bar/test/xsd/XMLSchema.xsd',因为1)找不到文档; 2)文件无法阅读; 3)文档的根元素不是。

#2


3  

I assume you are trying to load XMLSchema.xsd. Please also download XMLSchema.dtd and datatypes.dtd and put them in the same directory. This should push you a little bit further.

我假设您正在尝试加载XMLSchema.xsd。还请下载XMLSchema.dtd和datatypes.dtd并将它们放在同一目录中。这应该会让你更进一步。

#1


2  

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; 2) the document could not be read; 3) the root element of the document is not .

org.xml.sax.SAXParseException:schema_reference.4:无法读取架构文档'file:/Users/foo/bar/test/xsd/XMLSchema.xsd',因为1)找不到文档; 2)文件无法阅读; 3)文档的根元素不是。

#2


3  

I assume you are trying to load XMLSchema.xsd. Please also download XMLSchema.dtd and datatypes.dtd and put them in the same directory. This should push you a little bit further.

我假设您正在尝试加载XMLSchema.xsd。还请下载XMLSchema.dtd和datatypes.dtd并将它们放在同一目录中。这应该会让你更进一步。