Maria Ibragimova
Maria Ibragimova

Reputation: 61

How to override type of element inherited from parent complex type?

I do code generation from XSD (using JAXB). I have one superclass and several subclasses. One of the superclass' properties (let's say, prop) has type A. I also have types B extends A and C extends A (defined in XSD). The superclass' schema defines an element of type A, which is inherited by the subclasses. I want to use types B and C instead of A in the subclasses.

<xsd:complexType name="Superclass">
    <xsd:sequence>
        <xsd:element name="prop" type="A" maxOccurs="unbounded"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="Subclass1">
    <xsd:complexContent>
        <xsd:extension base="Superclass"/>
        <xsd:attribute name="someAttribute" type="xsd:string"/>
    </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="Subclass2">
    <xsd:complexContent>
        <xsd:extension base="Superclass"/>
        <xsd:attribute name="someOtherAttribute" type="xsd:int"/>
    </xsd:complexContent>
</xsd:complexType>

The generated Superclass.java will have List<A> prop. For example, I want the prop to be overriden with type List<B> in the Subclass1.java and with List<C> in Subclass2.java. The primary goal is to have the prop in the superclass, but to use other (compatible) types in the subclasses.

Upvotes: 6

Views: 1073

Answers (1)

xerx593
xerx593

Reputation: 13261

I find no samples, where xsd elements(/complex types) are extended & overridden like that, but you can do it, when Subclass1 and Subclass2 (and Superclass) reside in different "definitions". You would xs:override Superclass in both of the "sub-xsds" (one with B and one with C type), and then extend them accordingly:

parent.xsd:

<xsd:complexType name="Superclass">
  <xsd:sequence>
    <xsd:element name="prop" type="A" maxOccurs="unbounded"/>
  </xsd:sequence>
</xsd:complexType>

sub1.xsd:

<xs:override schemaLocation="parent.xsd">
  <xsd:complexType name="Superclass">
    <xsd:sequence>
      <xsd:element name="prop" type="B" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
</xs:override>
<!-- and just : -->
<xsd:complexType name="Subclass1">
  <xsd:complexContent>
    <xsd:extension base="Superclass"/>
    <xsd:attribute name="someAttribute" type="xsd:string"/>
  </xsd:complexContent>
</xsd:complexType>

and sub2.xsd:

<xs:override schemaLocation="parent.xsd">
  <xsd:complexType name="Superclass">
    <xsd:sequence>
      <xsd:element name="prop" type="C" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
</xs:override>
<xsd:complexType name="Subclass2">
 <xsd:complexContent>
   <xsd:extension base="Superclass"/>
   <xsd:attribute name="someOtherAttribute" type="xsd:int"/>
 </xsd:complexContent>
</xsd:complexType>

It must be in different definitions, because I don't know how to overcome the "name clash" (Superclass =? Superclass), but maybe someone has an idea? ...shows a better/n alternative way.

This solution is as easy/complex as "defining SuperclassB and SuperclassC". (questioning the necessity of Superclass!)

Upvotes: 1

Related Questions