多次导入相同的 XSD 是否错误?

Is it an error to import the same XSD multiple times?

我有一个模式 (a) 导入另一个模式 (z)。 然后我还有另一个模式 (b) 导入相同的模式 (z)。

碰巧 XML 符合模式 (a) 的文档 (d) 也可以包含符合模式 (z) 的可选元素。

因此,当 (d) 在 .NET 中得到验证时,我遇到了异常 "The simpleType 'z:x' has already been declared."

但氧气也不例外

我想以不抛出此异常的方式配置 .net。只是表现得像氧气。

怎么办?

问题的出现是由于 XSD 建议允许但不要求合规处理器忽略从已遇到的名称空间导入 XSD。 .NET 正在重新导入,这是允许的。氧气正在忽略重新进口,这是允许的。

两者均按照 XSD 建议进行操作。请参阅 4.2.3 References to schema components across namespaces 中的最后一个注释:

Note: The above is carefully worded so that multiple <import>ing of the same schema document will not constitute a violation of clause 2 of Schema Properties Correct (§3.15.6), but applications are allowed, indeed encouraged, to avoid <import>ing the same schema document more than once to forestall the necessity of establishing identity component by component. Given that the schemaLocation [attribute] is only a hint, it is open to applications to ignore all but the first <import> for a given namespace, regardless of the ·actual value· of schemaLocation, but such a strategy risks missing useful information when new schemaLocations are offered.

幸运的是,Oxygen 基于 Xerces,允许用户通过

控制此行为

http://apache.org/xml/features/honour-all-schemaLocations

不幸的是,.NET XML/XSD 处理器不是基于 Xerces,而且我不知道任何 .NET 版本中的任何并行配置选项。 (如果另有说明,我会很乐意更新此答案。)