JAVA:如何使用具有相同targetNamespaces的多个模式来使用xerces SAXParser

时间:2010-03-12 12:57:11

标签: java xsd xerces saxparser

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

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

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

** java.lang.IllegalArgumentException:当使用Objects数组作为SCHEMA_SOURCE属性的值时,没有两个Schema应该共享相同的targetNamespace。  在org.apache.xerces.impl.xs.XMLSchemaLoader.processJAXPSchemaSource(未知来源)  在org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(未知来源)  在org.apache.xerces.impl.xs.XMLSchemaValidator.findSchemaGrammar(未知来源)  at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(未知来源)  at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(未知来源)  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)  在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。是否可以这样做并避免上述错误?

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

由于

1 个答案:

答案 0 :(得分:0)

不,不可能。它由XML and schema specification强制执行。

实体或属性只能与单个命名空间相关联,默认值是隐式的,或者是显式命名空间作为前缀。

建议:如果您的XML输入格式正确但无法验证,因为没有声明名称空间,但您知道如何实现可以猜测每个节点名称空间的逻辑基于其上下文,如其上一个节点或其父节点。您可以创建预处理器:

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