I need to parse a XSD with XSOM
but this XSD contains circular imports.
A.xsd
<xs:schema xmlns=”ns1” targetNamespace=”ns1”>
<xs:import namespace=”ns2” schemaLocation=”B.xsd”/>
<xs:element name=”MyElement” type=”xs:string”/>
</xs:schema>
B.xsd
<xs:schema xmlns=”ns2” targetNamespace=”ns2” xmlns:ns1=”ns1”>
<xs:import namespace=”ns1” schemaLocation=”A.xsd”/>
<xs:complexType name="MyComplex">
<xs:sequence>
<xs:element ref="ns1:MyElement" minOccurs="0"/>
<xs:sequence>
<xs:complexType>
</xs:schema>
XSOM
can’t parse the schema because it detects elements that have already been defined due to circular imports. So I tried to break the circular import by externalizing the elements that are defined by A and used in B.
C.xsd contains element from A that are used by B. Note that these elements are not used in A. Don’t ask me why these have been defined in A.
<xs:schema xmlns=”ns1” targetNamespace=”ns1”>
<xs:element name=”MyElement” type=”xs:string”/>
</xs:schema>
A.xsd becomes
<xs:schema xmlns=”ns1” targetNamespace=”ns1”>
<xs:import namespace=”ns2” schemaLocation=”B.xsd”/>
</xs:schema>
B.xsd (import C.xsd instead of A.xsd) becomes
<xs:schema xmlns=”ns2” targetNamespace=”ns2” xmlns:ns1=”ns1”>
<xs:import namespace=”ns1” schemaLocation=”C.xsd”/>
<xs:complexType name="MyComplex">
<xs:sequence>
<xs:element ref="ns1:MyElement" minOccurs="0"/>
<xs:sequence>
<xs:complexType>
</xs:schema>
XSOM
can parse the XSD. But now I can’t create the schema with the following code:
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
sf.setResourceResolver(new MyResourceResolver());
I use the standard implementation bundled with the JDK 1.7. I get the exception:
src-resolve: Cannot resolve the name 'ns1:MyElement' to a(n) 'element declaration' component.
The issue is that the resource resolver is called for the B namespace but not for the A namespace which makes sense. Since the namespace A is shared by A.xsd and C.xsd, the resource resolver can’t find the elements defined in C.xsd.
Are circular imports valid? Is it possible to break a circular import so it can be parsed by XSOM
and then loaded by the SchemaFactory
?