How to import multiple schemas from the same namespace? (import vs. include)
lukas
When developing XML schemas we may encounter a situation where we have several XSDs that belong to the same namespace - e.g. a common namespace that contains common types:
- AddressType.xsd - in namespace=”http://company.com/common”
- UserType.xsd - also in namespace=”http://company.com/common”
To use these schemas in a WSDL or another schema programmers often try to incorrectly import them like this:
<xsd:import namespace="http://company.com/common" schemaLocation="common/AddressType.xsd"/> <xsd:import namespace="http://company.com/common" schemaLocation="common/UserType.xsd"/>
This will NOT work. We are confusing the schema processor by telling it that components from namespace “http://company.com/common” are to be found at two different locations! In most instances the second import location won’t even be processed.
The above syntax is illegal and in Eclipse this will result in failed schema validation with the following error:
src-resolve: Cannot resolve the name 'common:UserType' to a(n) 'type definition' component.
Now, if our target namespace is the SAME as that of the above schemas we can just use “include” to combine them into the current schema with the same namespace. But if the target namespace is different another approach is required.
One way around this (if you want to keep the objects in individual XSDs) is to collect together - using “include” - the various subschemas from the above “http://company.com/common” using “include” and then import the result:
First, include into intermediate.xsd:
<schema targetNamespace="http://company.com/common" ...> <include schemaLocation="AddressType.xsd"/> <include schemaLocation="UserType.xsd"/> ... </schema>
Then, import the above into someservice.xsd:
<schema targetNamespace="http://company.com/someservice" ...> <import namespace="http://company.com/common" schemaLocation="intermediate.xsd"/> ... </schema>
Another approach (which I prefer) is to define related objects from the same namespace in a single schema and then import this entire schema when required. This way we would have AddressType and UserType defined in common.xsd directly under the “http://company.com/common” namespace and we would only need to import a single namespace.
Posted in xml, programming |