JAVA:如何使用具有相同目标命名空间的多个模式的 xerces SAXParser

发布于 2024-08-25 02:08:51 字数 1315 浏览 5 评论 0原文

我需要使用一组模式对传入的 XML 片段执行一些验证。

所有这些模式共享相同的 targetNamespace,但分为不同的 .xsd 文件。

我的java程序将每个xsd文件加载到InputSource []数组中并将它们传递到SAX解析器(SCHEMA_SOURCE属性)。 但是,当调用 XMLReader.parse 方法时,我得到以下堆栈跟踪:

** java.lang.IllegalArgumentException:当使用对象数组作为 SCHEMA_SOURCE 属性的值时,两个模式不应共享相同的 targetNamespace。 在 org.apache.xerces.impl.xs.XMLSchemaLoader.processJAXPSchemaSource(来源未知) 在 org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(来源未知) 在 org.apache.xerces.impl.xs.XMLSchemaValidator.findSchemaGrammar(来源未知) 在 org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(来源未知) 在 org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(来源未知) 在 org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(来源未知) 在 org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(来源未知) 在 org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(来源未知) 在 org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(来源未知) 在 org.apache.xerces.parsers.XML11Configuration.parse(来源未知) 在 org.apache.xerces.parsers.XML11Configuration.parse(来源未知) 在 org.apache.xerces.parsers.XMLParser.parse(来源未知) 在 org.apache.xerces.parsers.AbstractSAXParser.parse(来源未知) **

我需要使用共享命名空间的架构来解析 XML。是否可以这样做并避免上述错误?

任何形式的帮助或建议将不胜感激

谢谢

I have a requirement to perform some validation on an incoming piece of XML using a group of schemas.

All these schemas share the same targetNamespace but are separated into different .xsd files.

My java program is loading each xsd file into an InputSource[] array and passing them into the SAX Parser (SCHEMA_SOURCE property).
However, when calling the XMLReader.parse method, I get the following stack trace:

**
java.lang.IllegalArgumentException: When using array of Objects as the value of SCHEMA_SOURCE property , no two Schemas should share the same targetNamespace.
at org.apache.xerces.impl.xs.XMLSchemaLoader.processJAXPSchemaSource(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.findSchemaGrammar(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
**

I need to parse the XML using schemas that share a namespace. Is it possible to do this and avoid the above error?

Any form of help or advice will be appreciated

Thanks

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

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

发布评论

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

评论(1

‖放下 2024-09-01 02:08:51

不,这是不可能的。它由 XML 和架构规范强制执行。

实体或属性只能与单个名称空间关联,可以是隐式的默认名称空间,也可以是作为前缀的显式名称空间。

建议:如果您的 XML 输入格式正确,但由于没有声明命名空间而无法验证,但您知道如何实现一个逻辑,该逻辑可以根据每个节点的名称空间猜测其名称空间是什么上下文就像它的前一个节点或其父节点。您可以创建一个预处理器:

  • 将 XML 输入节点解析为 DOM(甚至使用 SAXParser),无需模式验证,
  • 应用此类逻辑并使用猜测的命名空间输出来预先添加实体和/或属性
  • 再次 作为 XML;在此步骤中,您可以选择使用漂亮的格式化程序进行调试,
  • 现在通过模式验证将该输出注入到现有的解析器中

No it is not possible. It is enforced by XML and schema specification.

An entity or an attribute can only be associated with a single namespace, either the default one which is implicit, either an explicit namespace as prefix.

An advice: if your XML input is well-formed but cannot be validated because no namespaces are declared but you have an idea how to implement a logic that can guess what the namespace is for each node based on its context like its previous node or its parent node. You may create a pre-processor:

  • parse the XML input nodes as DOM (or even with a SAXParser) without schema validation
  • apply such a logic and pre-pend entities and/or attributes with guessed namespaces
  • output again as XML; in that step, you can optionally use a pretty formatter for debug purpose
  • now inject that output into your existing parser with schema validation
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文