1

I need to create an XML schema definition (XSD) that describes Java objects.

I was wondering how to do this when the objects in question inherit from a common base class with a type parameter.

public abstract class Rule<T> { ... }

public abstract class TimeRule extends Rule<XTime> { ... }

public abstract class LocationRule extends Rule<Location> { ... }

public abstract class IntRule extends Rule<Integer> { ... }

....

(where XTime and Location are custom classes defined elsewhere)

How would I go about constructing an XSD that such that I can have XML nodes that represent each of the subclasses of Rule<T> - without the XSD for each of them repeating their common contents?

Thank you!

bguiz
  • 27,371
  • 47
  • 154
  • 243

1 Answers1

7

Conisder JAXB for XML Schema -> Java compilation.

XML Schema gives some possibilities for modelling iheritance:

<xs:complexType name="baseType">
    <xs:sequence>
        <xs:element name="a" type="xs:string"/>
        <xs:element name="b" type="xs:long"/>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="extendedType">
    <xs:complexContent>
        <xs:extension base="baseType">
            <xs:sequence>
                <xs:element name="c" type="xs:dateTime"/>
                <xs:element name="d" type="xs:base64Binary"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

However, I don't think you can achieve exactly the Java code you're posting. You can, nevertheless get somewhat close with the inheritance plugin.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • @lexicore Thank you for your answer! I do not need to truly model inheritance per se - I am happy so long as it achieve the effect. Also interesting is that I am defining the schema, ultimately for the purposes of using it with JAXB. – bguiz Apr 16 '10 at 08:01
  • @bguiz Ok, then I'd got with schema-based extensions plus the inheritance plugin to extend/implement your classes/interfaces. The plugin does not support parameterized classes/interfaces at the moment. Would be a nice feature, by the way. I'll consider implementing it. – lexicore Apr 16 '10 at 08:23
  • +1 and check @lexicore : Yeah, I have reached the same conclusion as what have said - XSDs do not support inheritance with class parameters. XSDs also cannot be made to support inheritance in the full OO sense of the word, except using the plugin you mentioned. – bguiz Apr 23 '10 at 00:35