#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    179
    Rep Power
    28

    Inheritance with Restriction in XML


    Hi,

    Suppose I have this kind of XML schema fragment, used in a web serivce:

    Code:
        <xs:complexType name="Fruit">
            <xs:choice maxOccurs="1" minOccurs="1">
                <xs:element name="apple" type="fruit:Apple"/>
                <xs:element name="pear" type="fruit:Pear"/>
                <xs:element name="banana" type="fruit:Banana"/>
                <xs:element name="mango" type="fruit:Mango"/>
            </xs:choice>
        </xs:complexType>
    Now I am adding another element that would be almost the same as Fruit, but more restrictive:

    Code:
        <xs:complexType name="TropicalFruit">
            <xs:choice maxOccurs="1" minOccurs="1">
                <xs:element name="banana" type="fruit:Banana"/>
                <xs:element name="mango" type="fruit:Mango"/>
            </xs:choice>
        </xs:complexType>
    So, when my Java classes are automatically generated, the Fruit and TropicalFruit classes come out totally unrelated.
    But I have to work with legacy code where this part of the web service request is expected to be of the class Fruit. So, I want TropicalFruit to be a subclass of Fruit both in the XML schema and in Java. How do I do that in XSD?

    Thanks.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    It doesn't make sense to do that. Yes, a "tropical fruit" is certainly a type of "fruit", but that's not what you're doing in the XSD. What you have is something that says "a Fruit is one of these types of fruit" and "a TropicalFruit is one of these types of fruit". There is no inheritance in there.

    Or put another way, inheritance is about extending the functionality of a base class. What you're trying to do is restrict functionality: normally one could provide an Apple as the fruit but with the subtype that's not allowed. It breaks inheritance.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    179
    Rep Power
    28
    Originally Posted by requinix
    It doesn't make sense to do that. Yes, a "tropical fruit" is certainly a type of "fruit", but that's not what you're doing in the XSD. What you have is something that says "a Fruit is one of these types of fruit" and "a TropicalFruit is one of these types of fruit". There is no inheritance in there.

    Or put another way, inheritance is about extending the functionality of a base class. What you're trying to do is restrict functionality: normally one could provide an Apple as the fruit but with the subtype that's not allowed. It breaks inheritance.
    Thank you for your comment.
    You are right in general, but I have to deal with the existing code, where an object is explicitly cast to Fruit. That's why I have the choice of either changing the legacy code or coming up with an inheritance hierarchy. I'll keeping figuring this out.

IMN logo majestic logo threadwatch logo seochat tools logo