views:

29

answers:

2

I am working on a schema file that I intend to use with JAXB for a project of mine. In Java, I have the following sort of code structure

public interface Condition {
   ...
}

public class AndCondition implements Condition {
   public AndCondition(List<Condition> conditions){
      ...
   }
}

public class StateCondition implements Condition {
   public StateCondition(Item item, String attributeName, String attributeValue){
      ...
   }
}

Naturally I have other objects in my code which rely on having a Condition object

public class Action{
   List<Item> targets;
   List<Item> tools;
   Condition condition;
   ...
}

My question is, if I define in my XML Schema a complexType and extend them:

<xs:element name="Condition">
</xs:element>

<xs:element name="StateCondition">
   <xs:complexContent>
      <xs:extension base="Condition">
         <xs:sequence>
            <xs:element ref="Item" />
            <xs:element name="ATTRIB" type="AttributeType" />
         </xs:sequence>
      </xs:extension>
   </xs:complexContent>
</xs:element>

<xs:element name="AndCondition">
   <xs:complexContent>
      <xs:extension base="Condition">
         <xs:sequence>
            <xs:element ref="Condition" minOccurs="1" maxOccurs="unbounded" />
         </xs:sequence>
      </xs:extension>
   </xs:complexContent>
</xs:element>

<xs:element name="Item">
   <xs:sequence>
      <xs:element name="TARGETS">
         <xs:element ref="Item" minOccurs="0" maxOccurs="unbounded />
      </xs:element>
      <xs:element name="TOOLS">
         <xs:element ref="Item" minOccurs="0" maxOccurs="unbounded />
      </xs:element>
      <xs:element ref="Condition" />
   </xs:sequence>
</xs:element>

Will I achieve the same sort of idea? In which I mean, if I create an XML file that holds one AndCondition, will it satisfy the validator?

+1  A: 

I found the answer myself after a bit of reading. Hopefully this question will come to use for someone else. The resource to read is here, which defines how JAXB handles extending base types in XML Schema definitions.

Quick and short, yes, JAXB views extension in schema as inheritance in Java.

duckworthd
A: 

xs:extension's base attribute must reference a type. This is a built-in type, simple type you define with a name, or a complex type you define with a name. So you could define types that correspond to your elements in the example (ConditionType, StateConditionType, etc.) and apply them to your named attributes. Then in your base schema, you can define a xs:choice to insure that you choose at least one of the elements you define.

Dave