Я искал вокруг, и неужели нет простого способа сделать элемент двух возможных типов?Я пробовал следующие обходные пути.
<xs:element name="food">
<xs:complexType>
<xs:choice>
<xs:element name="meat" type="meatFoods"/>
<xs:element name="veggies" type="veggieFoods"/>
</xs:choice>
</xs:complexType>
</xs:element>
food cannot have character [children], because the type's content type is element-only.
И:
<xs:choice>
<xs:sequence>
<xs:element name="food" type="meatFoods"/>
</xs:sequnce>
<xs:sequence>
<xs:element name="food" type="veggieFoods"/>
</xs:sequence>
</xs:choice>
Выдает ошибку об одних и тех же именах различных типов
И:
<xs:complexType name="MeatOrVeggies">
<xs:choice>
<xs:element name="meat" type="meatFoods"/>
<xs:element name="veggies" type="veggieFoods"/>
</xs:choice>
</xs:complexType>
<xs:element name="food" type="MeatOrVeggies"/>
food cannot have character [children], because the type's content type is element-only.
Все это приводит к некоторой ошибке.В последнем сообщается, что еда - это элемент, и дочерняя ошибка не допускается, а во втором - «Несколько элементов с именем« еда », причем в группе моделей появляются разные типы».
Этот XSD является полезной нагрузкой дляполезный объект Java, который имеет: FoodInterface food
.Meat и Veggies - это перечисления этого интерфейса, например:
public enum Meat implements FoodInterface{
Chicken,
Beef,
Pork
}
Meat / Veggie Enum в схеме:
<xs:simpleType name="meatFoods">
<xs:restriction base="xs:string">
<xs:enumeration value="chicken"/>
<xs:enumeration value="beef"/>
<xs:enumeration value="pork"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="veggieFoods">
<xs:restriction base="xs:string">
<xs:enumeration value="spinach"/>
<xs:enumeration value="broccoli"/>
<xs:enumeration value="tomato"/>
</xs:restriction>
</xs:simpleType>
Thanks in advance.