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

    Join Date
    Jun 2003
    Posts
    180
    Rep Power
    30

    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. Dark Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    15,553
    Rep Power
    9565
    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
    180
    Rep Power
    30
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2016
    Posts
    35
    Rep Power
    1
    I would encourage you to check out this page:

    https://www.w3.org/TR/xmlschema11-1/#override-schema

    basically, it is the xsverride option, you are looking for. I think. If not, could you elaborate a bit on your precise scenario?
  8. #5
  9. Dark Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    15,553
    Rep Power
    9565
    Originally Posted by MortenBork
    If not, could you elaborate a bit on your precise scenario?
    It's been more than two years.
  10. #6
  11. Administrator
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2015
    Posts
    369
    Rep Power
    10
    Crap, I missed that when I saw this post this morning

    Closing this thread! MortenBork, please don't post in threads that are this old.

IMN logo majestic logo threadwatch logo seochat tools logo