从 URL 创建 XML 模式可以工作,但从本地文件创建 XML 模式失败?

发布于 2024-12-01 22:56:32 字数 2010 浏览 1 评论 0原文

我需要验证以编程方式生成的 XML 架构实例 (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));

但是,当我保存 XML 架构定义文件 本地并以这种方式引用它:

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

它失败并出现以下异常:

org.xml.sax.SAXParseException:schema_reference.4:无法读取模式文档“file:/Users/foo/bar/test/xsd/XMLSchema.xsd”,因为1)找不到该文档; 2)文档无法读取; 3) 文档的根元素不是

我通过对 File 对象执行 exists()canRead() 断言确保文件存在且可读。我还使用几个不同的实用程序(Web 浏览器、wget)下载了该文件,以确保没有损坏。

知道为什么当我从 HTTP URL 生成架构时可以验证 XSD 实例文档,但在尝试从具有相同内容的本地文件生成时却出现上述异常?

[编辑]

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

完整答案

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

  1. XMLSchema.xsd - 更改 <将 XML 命名空间的“import”元素中的 code>schemaLocation 更改为“xml.xsd”。
  2. XMLSchema.dtd - 按原样。
  3. datatypes.dtd - 按原样。
  4. xml.xsd - 按原样。

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

I need to validate XML Schema Instance (XSD) documents which are programmatically generated so I'm using the following Java snippet, which works fine:

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:

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>.

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.

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?

[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.

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:

  1. XMLSchema.xsd - change schemaLocation to "xml.xsd" in the "import" element for XML Namespace.
  2. XMLSchema.dtd - as is.
  3. datatypes.dtd - as is.
  4. xml.xsd - as is.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小巷里的女流氓 2024-12-08 22:56:32

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

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.

む无字情书 2024-12-08 22:56:32

更新

XMLSchema.xsd 是否通过不在本地文件系统上的相对路径导入任何其他模式?


您的工作目录的相对路径可能不正确。尝试输入完全限定路径以消除找不到文件的可能性。

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

UPDATE

Is XMLSchema.xsd importing any other schemas by relative paths that are not on the local file 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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文