Can we force JAXB2 to respect order file in a XSD with import?

38 Views Asked by At

I have an XSD file that import all my other XSD.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:include  schemaLocation="z-common.xsd"/>    
    <xs:include  schemaLocation="a-first.xsd"/>
    <xs:include  schemaLocation="b-second.xsd"/>    
    <xs:include  schemaLocation="c-third.xsd"/> 
</xs:schema>

common.xsd file define multiple complex Type

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="MyType">
        <xs:all>
            <xs:element minOccurs="1" type="xs:string" name="attr1"/>
            <xs:element minOccurs="1" type="xs:string" name="attr2"/>
        </xs:all>
    </xs:complexType>
    </xs:schema>

All other XSD use this complex type. Example :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="MyObject">
    <xs:all>
      <xs:element minOccurs="0" type="MyType" name="myType"/>
    </xs:all>
  </xs:complexType>
</xs:schema>

But when I build my project with jaxb2-maven-plugin, it's fail because MyType is not found. In fact, this plugin seems to don't respect import order and take them alphabetically.

I have found multiple solution with bindings for example, but these solutions must modify XSD content (and I can't because it's not mind)

How can we do that ?

1

There are 1 best solutions below

0
Karsten On
  • The first Schema is superfluous as it doesn't contain any element or type definitions.
  • Then you have to include the common Schema in every other that uses definitions from that one.
  • When your schemas grow I recommend using namespaces for better structuring. Also your classes will be generated into different packages. If using different namespaces you will have to use xs:import rather than xs:include.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://acme.org/common"
           elementFormDefault="qualified">

    <xs:complexType name="MyType">
        <xs:all>
            <xs:element minOccurs="1" type="xs:string" name="attr1"/>
            <xs:element minOccurs="1" type="xs:string" name="attr2"/>
        </xs:all>
    </xs:complexType>

</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:common="http://acme.org/common"
           targetNamespace="http://acme.org/a-first"
           elementFormDefault="qualified">

    <xs:import namespace="http://acme.org/common" schemaLocation="common.xsd"/>
    
    <xs:complexType name="MyObject">
        <xs:all>
            <xs:element name="myType" type="common:MyType" minOccurs="0"/>
        </xs:all>
    </xs:complexType>
    
</xs:schema>