Peter De Winter
Peter De Winter

Reputation: 1205

How to correctly handle inheritance in JAXB

I have a question regarding JAXB and inheritance with following requirements:

This is my base XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       targetNamespace="http://api.mijathi.be/msg/schema/vehicle"
       xmlns="http://api.mijathi.be/msg/schema/vehicle"
       elementFormDefault="qualified"
       attributeFormDefault="unqualified">

<xs:element name="vehicles">
    <xs:complexType>
        <xs:sequence minOccurs="1" maxOccurs="1">
            <xs:element ref="vehicleType" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="vehicleType" type="vehicleType"/>

<xs:complexType name="vehicleType" abstract="true">
    <xs:sequence/>
</xs:complexType>
</xs:schema>

Now an implementation:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       targetNamespace="http://api.mijathi.be/msg/schema/vehicle"
       xmlns="http://api.mijathi.be/msg/schema/vehicle"
       elementFormDefault="qualified"
       attributeFormDefault="unqualified">

<xs:include schemaLocation="http://api.mijathi.be/msg/schema/vehicle/main.xsd"/>

<xs:element name="car" type="carDescription" substitutionGroup="vehicleType"/>

<xs:complexType name="carDescription">
    <xs:complexContent>
        <xs:extension base="vehicleType">
            <xs:sequence>
                <xs:element name="make" type="xs:string" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>

</xs:complexType>
</xs:schema>

Generation of the classes succeeds nicely. I can even unmarshal nicely using this example xml:

<veh:vehicles xmlns:veh="http://api.mijathi.be/msg/schema/vehicle">
<veh:vehicleType xsi:type="veh:car" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <veh:make>string</veh:make>
</veh:vehicleType>
</veh:vehicles>

But external, using your IDE for example, validation of the XML of course fails on this block. Because "car" is not defined in the veg namespace:

<veh:vehicleType xsi:type="veh:car" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <veh:make>string</veh:make>
</veh:vehicleType>

How can I fix this?

Upvotes: 2

Views: 166

Answers (1)

lexicore
lexicore

Reputation: 43671

Few things wich may be helpful:

  • If you have separate schemas, consider using xs:import instead of xs:include. Otherwise it is becoming basically "one" schema, I don't think this is your intention.
  • Use xsi:schemaLocation in your XML, see here: what is the use of xsi:schemaLocation?
  • In Eclipse, you can add your XML Schema in Preferences > XML Catalog, for instance using a namespace URI.

To validate, Eclipse needs access to your schema. How is it supposed to know where to get it? Well, either xsi:schemaLocation or the central catalog.

Upvotes: 1

Related Questions