如何使用旧的(1999)模式版本在 JAXP 中解析/验证 XML?

发布于 2024-09-30 20:39:23 字数 1261 浏览 3 评论 0原文

我有一个使用旧版本的 XML 规范 (http://www.w3.org/1999/XMLSchema) 构建的 XML 模式。我正在尝试使用架构解析 XML 消息(如下所示)。默认情况下,Xerces 1.1 解析器在应用程序域根目录中查找架构。我正在尝试将架构移动到我们的 Web 目录中的公共位置。似乎我无法使用 Xerces 1.1 库在我的解析程序中指定 URL 作为架构位置。

所以我尝试升级我的程序以使用 JAXP DocumentBuilderFactory 及其 setSchema() 方法。这似乎找到了具有我提供的架构 URL 的架构,但它给出了解析错误,因为架构(大概)不符合规范的更高版本(例如,我收到错误,因为我的 Ticket 元素不符合将其成员包装在 xs:Sequence 中)。

我无法升级我的架构,因为目前我无法强迫外部客户重写向我们发送消息的应用程序。

谁能告诉我如何使用 JAXP 使用旧版模式​​规范成功解析/验证?或者,有没有办法告诉 Xerces 1.1 DOMParser 使用由 URL(而不是文件位置)定义的模式?

以下是架构的示例:

<?xml version="1.0" encoding="UTF-8" ?>
<schema>
  <element name="Ticket">
     <complexType>
        <element ref="ID" />
        <element ref="User" />
     </complexType>
  </element>

  <element name="ID" type='string' />
  <element name="UserID" type='string' />
</schema>

XML 文档如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Ticket xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation='TicketSubmission.xsd'>
  <ID>2010:December2:Dec:12:05:33:245</ID> 
  <UserID>user123</UserID> 
</Ticket>

谢谢!

I have an XML schema that was built with an old version of the XML spec (http://www.w3.org/1999/XMLSchema). I am trying to parse XML messages using the schema (shown below). By default, the Xerces 1.1 parser looks for the schema in the app domain root directory. I am trying to move the schema to a public location in our web directory. It seems like I cannot specify a URL as the schema location in my parsing program using the Xerces 1.1 library.

So I tried to upgrade my program to use JAXP DocumentBuilderFactory and its setSchema() method. This seems to find the schema with the schema URL I provide it, except that it gives parsing errors because the schema (presumably) does not conform to a later version of the spec (for example, I get errors because my Ticket element doesn't wrap its members inside an xs:Sequence).

I can't upgrade my schema since I can't, at this time, force external customers to rewrite their apps that send messages to us.

Can anyone tell me how to use JAXP to successfully parse/validate using an old-school version of the schema spec? Or alternatively, is there a way to tell the Xerces 1.1 DOMParser to use a schema defined by a URL (not a file location)?

Here is a sample of what the schema looks like:

<?xml version="1.0" encoding="UTF-8" ?>
<schema>
  <element name="Ticket">
     <complexType>
        <element ref="ID" />
        <element ref="User" />
     </complexType>
  </element>

  <element name="ID" type='string' />
  <element name="UserID" type='string' />
</schema>

The XML document looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Ticket xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation='TicketSubmission.xsd'>
  <ID>2010:December2:Dec:12:05:33:245</ID> 
  <UserID>user123</UserID> 
</Ticket>

Thanks!

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

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

发布评论

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

评论(2

╭⌒浅淡时光〆 2024-10-07 20:39:23

使用旧版 JAXP;可能是1.6

Use an old-school version of JAXP; maybe 1.6

旧人哭 2024-10-07 20:39:23

因此,我得出的结论是,使用 1999 模式规范是一个坏主意。支持 1999 架构规范的早期版本的 Xerces 解析器似乎在架构驻留在磁盘上的位置方面并不灵活。我可能一直坚持将架构放置在应用程序服务器根目录中,直到我们可以让客户使用更新版本的架构。

Xerces 的更高版本(甚至在 Xerces 2 之前)似乎放弃了对 1999 架构规范的支持,转而支持 2001 架构规范。尽管如此,Xerces 的更高版本似乎允许模式文件的位置更加灵活。

So, I've come to the conclusion that using the 1999 schema spec is a bad idea. Earlier versions of the Xerces parser that support the 1999 schema spec don't seem to be flexible in where the schemas can reside on disk. I'm probably stuck with placing the schemas in the app server root directory until we can get our clients to use a newer version of our schema.

Later versions of Xerces (even prior to Xerces 2) seem to have abandoned support of the 1999 schema spec in favor of the 2001 schema spec. Although, later versions of Xerces seem like they allow more flexibilty in the location of the schema files.

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