users@jaxb.java.net

JAXB, XML Schema, and Inheritance

From: William M. Saxton <saxton_at_gmail.com>
Date: Thu, 27 Sep 2007 09:21:27 -0400

I read Kohsuke's "missive" on why inheritance is bad in XML schema's,
but this was written in 2001 so I wonder if, given the advancement of
JAXB, it should be reconsidered.

Give then inheritance is such a big part of Java, inheritance in XML
schema (and its created JAXB classes) would be tremendously useful.
For example, in my project I'm storing disk data:

<xsd:complexType name="diskType">
  <xsd:sequence>
    <xsd:element name="serialnum" type="xsd:string" minOccurs="0"/>
    <xsd:element name="speed" type="xsd:string" minOccurs="0"/>
  </xsd:sequence>
</xsd:complexType>

But a new disk has come along that has the old disk information, plus much more:

<xsd:complexType name="specialDiskType">
  <xsd:sequence>
    <xsd:element name="serialnum" type="xsd:string" minOccurs="0"/>
    <xsd:element name="speed" type="xsd:string" minOccurs="0"/>
    <xsd:element name="hotSpare" type="xsd:string" minOccurs="0"/>
  </xsd:sequence>
</xsd:complexType>

Given the Java classes created, why wouldn't I want some code to
behave the same, whether it was using the "serialnum" or "speed" of
either type of Disk? Without inheritance, I'd have to have code stuff
like this:

getSerialNumber(DiskType disk) {
  return(disk.getSerialNum());
}

getSerialNumber(specialDiskType sdisk) {
  return(sdisk.getSerialNum());
}