2012-11-25 39 views
0

我有一個如下所示的XML文件,並希望使用類型替換方法創建XML模式,以便它可以驗證下面的XML文件。但是我創建的模式是完全錯誤的。請告訴我如何對模式進行編碼以驗證下面的文件XML。類型替換的XML模式

詳情:

  • 只有兩種存儲一個類型的動物是鳥,一個是魚。
  • 對於類型,名稱和原點元素都是必需的
  • 對於類型:鳥類,可以選擇存儲其他顏色元素。
  • 類型:魚,另外大小元素必須是店內

    <animals> 
    <animal animalID="b-1" xsi:type="bird"> 
        <name>Humming Bird</name> 
        <origin>Asia</origin> 
        <color>Blue</color> 
    </animal> 
    <animal animalID="b-2" xsi:type="bird"> 
        <name>Horn Bill</name> 
        <origin>Asia</origin> 
    </animal> 
    <animal animalID="f-2" xsi:type="fish"> 
        <name>Whale</name> 
        <origin>Europe</origin> 
        <size>Large</size> 
    </animal> 
    <animal animalID="b-5" xsi:type="bird"> 
        <name>Parrot</name> 
        <origin>Europe</origin> 
    </animal> 
    

我已經推出了以下的模式,我認爲它完全錯誤的。

<xsd:element name="bird" substitutionGroup="animals" 
     type="birdType"/> 
<xsd:element name="fish" substitutionGroup="animals" 
     type="fishType"/> 
<xsd:element name="animals"> 
<xsd:complexType> 
    <xsd:sequence> 
     <xsd:element name="animal" maxOccurs="unbounded"/> 
    </xsd:sequence> 
</xsd:complexType> 
</xsd:element> 

<xsd:element name="animal"> 
<xsd:complexType> 
    <xsd:sequence> 
     <xsd:element ref="bird" maxOccurs="unbounded"/> 
    </xsd:sequence> 
</xsd:complexType> 
</xsd:element> 

回答

0

我不是生物學家,但如果我是你,我跟你吵架的分類...

如果您正在使用XSI:類型來區分這兩種類型,然後將模式需要包含全球複雜類型定義名爲「鳥」和「魚」。你可以從一些基本類型中擴展出這兩個,比如說「生物」(因爲我們在這裏沒有做真正的生物學......)。類型生物包含兩個常見元素名稱和來源,並且這兩個擴展名分別包含可選元素的顏色和大小。動物元素被定義爲「生物」類型。

+0

你好凱,你的意思是喜歡下面的代碼嗎? – setiasetia

+0

是的,這就是主意。 –

0
<?xml version="1.0"?> 
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"> 
<xsd:complexType name="animalType"> 
    <xsd:sequence> 
     <xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/> 
     <xsd:element name="origin" type="xsd:string" minOccurs="1" maxOccurs="1"/> 
    </xsd:sequence> 
</xsd:complexType> 
<xsd:complexType name="birdType"> 
    <xsd:complexContent> 
     <xsd:extension base="animalType"> 
      <xsd:sequence> 
       <xsd:element name="color" type="xsd:string" minOccurs="0"/> 
      </xsd:sequence> 
     </xsd:extension> 
    </xsd:complexContent> 
</xsd:complexType> 
<xsd:complexType name="fishType"> 
    <xsd:complexContent> 
     <xsd:extension base="animalType"> 
      <xsd:sequence> 
       <xsd:element name="size" type="xsd:string" minOccurs="1"/> 
      </xsd:sequence> 
     </xsd:extension> 
    </xsd:complexContent> 
</xsd:complexType> 
<xsd:element name="animals"> 
    <xsd:complexType> 
     <xsd:sequence> 
      <xsd:element name="animal" type="animalType" maxOccurs="unbounded"/> 
     </xsd:sequence> 
    </xsd:complexType> 
</xsd:element> 
</xsd:schema>