-->

Is it an error to import the same XSD multiple tim

2019-01-15 22:13发布

问题:

I have a schema (a) that imports another schema (z). Then I have yet another schema (b) that imports that same schema (z).

It happens so that XML document (d) conforming to schema (a) can also contain an optional element that conforms to schema (z).

In consequence, when (d) is validated in .NET, I'm having exception "The simpleType 'z:x' has already been declared."

But there is no exception in Oxygen.

I'd like to configure .net the way it does NOT throw this exception. Just to behave like Oxygen.

How?

回答1:

The problem arises due to the XSD Recommendation allowing, but not requiring, conformant processors to ignore the import of an XSD from an already encountered namespace. .NET is re-importing, as it is allowed to do. Oxygen is ignoring re-imports, as it is allowed to do.

Both are operating in conformance to the XSD Recommendation. See the last note in 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.

Fortunately, Oxygen is based on Xerces, which allows the user to control this behavior via

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

Unfortunately, the .NET XML/XSD processor is not based on Xerces, and I am unaware of any parallel configuration option in any .NET version. (I will happily update this answer if shown otherwise.)