2013-09-27 38 views
1

我有以下XML:預定義的屬性/在XSD枚舉常量

<animals> 
    <animal name="Pongo" animalType="Dog" /> 
    <animal name="Marie" animalType="Cat" /> 
    <animal name="Skippy" animalType="Kangaroo" /> 
</animals> 

我知道這是可能使用這樣的枚舉限制動物類型:

<xs:simpleType name="animalType"> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Cat" /> 
     <xs:enumeration value="Dog" /> 
     <xs:enumeration value="Kangaroo" /> 
    </xs:restriction> 
</xs:simpleType> 

我對子級就像根據animalType值自動地知道動物在xml解析後需要多少鞋子。
而且,當添加新的動物類型時,還要爲該動物添加步行腿的數量。
舉例來說,這將是偉大的,是可以定義類似

<xs:simpleType name="animalType"> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Cat" nbOfShoes="4" /> 
     <xs:enumeration value="Dog" nbOfShoes="4" /> 
     <xs:enumeration value="Kangaroo" nbOfShoes="2" /> 
    </xs:restriction> 
</xs:simpleType> 

是否有可能實現這一目標採用XSD,或者我要實現的numberOfShoes邏輯的XML被解析後?
謝謝。

回答

1

這取決於。在XSD中,您可以從結構的角度定義xml。結構和內容之間的關係在XSD 1.0中更難表達。

您可以使用xsi:type屬性來替換類型。該XSD可能看起來像

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> 

    <!-- Let be Animals root element --> 
    <xs:element name="Animals" type="animals" /> 

    <!-- type for Animals elemes --> 
    <xs:complexType name="animals"> 
     <xs:sequence> 
      <xs:element name="Animal" maxOccurs="unbounded" type="animal"/> 
     </xs:sequence> 
    </xs:complexType> 

    <!-- Define an abstract type for animal (abstract = there shouldn't occure element of this type only of its childs). It has no attributes. --> 
    <xs:complexType name="animal" abstract="true"> 
     <xs:simpleContent> 
      <xs:extension base="xs:string"/> 
     </xs:simpleContent> 
    </xs:complexType> 

    <!-- Define a type for cat... --> 
    <xs:complexType name="catType"> 
     <xs:simpleContent> 
      <!-- ... it extends abstract animal type ... --> 
      <xs:extension base="animal"> 
       <!-- ... and add some attributes with fixed values --> 
       <xs:attribute name="name" fixed="cat" /> 
       <xs:attribute name="nbOfLegs" fixed="4" /> 
      </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 

    <!-- similar definition like catType --> 
    <xs:complexType name="kangarooType"> 
     <xs:simpleContent> 
      <xs:extension base="animal"> 
       <xs:attribute name="name" fixed="kangaroo" /> 
       <xs:attribute name="nbOfLegs" fixed="2" /> 
      </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 

</xs:schema> 

以下XML應該驗證

<?xml version="1.0" encoding="UTF-8"?> 
<Animals xsi:noNamespaceSchemaLocation="animals.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <!-- xsi:type is saying what type is actually used --> 
    <Animal xsi:type="catType" name="cat" nbOfLegs="4" /> 
    <Animal xsi:type="kangarooType" name="kangaroo" nbOfLegs="2" /> 
</Animals> 

繼不

<?xml version="1.0" encoding="UTF-8"?> 
<Animals xsi:noNamespaceSchemaLocation="animals.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <Animal xsi:type="catType" name="cat" nbOfLegs="2" /> 
</Animals> 

(有錯誤,如值 '2' 屬性 'nbOfLegs' 不等於固定默認值'4')。

+1

非常感謝Jirka。你的帖子打開了我的眼睛。但是,我使用了不同的方法:在動物中定義屬性,並在特定動物中使用xs:restriction而不是xs:extension來定義屬性的「fixed」屬性。這樣我可以在生成的Java類中使用多態。 – Cipi