2017-02-06 31 views
1

考慮下面的XML架構:xsd錯誤消息:「派生類型的內容類型和其基類的內容類型必須混合使用,或者都是僅使用元素。」

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" 
    vc:minVersion="1.0" vc:maxVersion="1.1"> 


    <xs:element name="zoo"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="name" type="xs:string"/> 
       <xs:element name="zootier" type="tier" maxOccurs="unbounded"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 



    <xs:complexType name="tier"> 
     <xs:sequence> 
      <xs:element name="name" type="xs:string"/> 
      <xs:element name="alter" type="xs:positiveInteger"/> 
     </xs:sequence> 
     <xs:attribute name="id" type="xs:ID"/> 
    </xs:complexType> 

    <xs:complexType name="säugetier_ct"> 
     <xs:complexContent> 
      <xs:extension base="tier"> 
       <xs:sequence> 
        <xs:element name="tragezeit" type="xs:positiveInteger"/> 
       </xs:sequence> 
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="pferd_ct"> 
     <xs:complexContent> 
      <xs:extension base="tier"> 
        <xs:attribute name="schimmel" type="xs:boolean"/>    
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="katze_ct"> 
     <xs:complexContent> 
      <xs:sequence> 
       <xs:element name="fellfarbe" type="xs:string"/> 
      </xs:sequence> 
     </xs:complexContent> 
    </xs:complexType> 


    <xs:complexType name="vogel_ct"> 
     <xs:complexContent> 
      <xs:extension base="tier"> 
       <xs:attribute name="flugfaehig" type="xs:boolean"/>     
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="gans_ct"> 
     <xs:complexContent> 
      <xs:extension base="tier"> 
       <xs:sequence> 
        <xs:element name="schlachtgewicht" type="xs:double"/> 
       </xs:sequence>    
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="fink_ct"> 
     <xs:complexContent> 
      <xs:sequence> 
       <xs:element name="beringt" type="xs:boolean"/> 
      </xs:sequence> 
     </xs:complexContent> 
    </xs:complexType> 



    <xs:complexType name="pfleger"> 
     <xs:sequence> 
      <xs:element name="pflegt" maxOccurs="unbounded"> 
       <xs:complexType> 
        <xs:attribute name="tier" type="xs:IDREF"/> 
       </xs:complexType> 
      </xs:element> 
     </xs:sequence> 
    </xs:complexType> 



    <xs:complexType name="behausung_ct"> 
     <xs:attribute name="name" type="xs:string"/> 
     <xs:sequence> 
      <xs:element name="zootier" type="tier" maxOccurs="unbounded"/> 
     </xs:sequence>  
    </xs:complexType> 

    <xs:complexType name="gebäude_ct"> 
     <xs:complexContent> 
      <xs:extension base="behausung_ct"> 
       <xs:sequence> 
        <xs:element name="flaeche" type="xs:double"/> 
       </xs:sequence>  
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 

    <xs:complexType name="teich_ct"> 
     <xs:complexContent> 
      <xs:extension base="behausung_ct"> 
       <xs:sequence> 
        <xs:element name="wassertiefe" type="xs:double"/> 
       </xs:sequence> 
      </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 



</xs:schema> 

這使我有以下錯誤消息:

Error1: The content of 'katze_ct' is invalid. Element 'sequence' is invalid, misplaced, or occurs too often. 

Error2: The content of 'fink_ct' is invalid. Element 'sequence' is invalid, misplaced, or occurs too often. 

Error3: The content of 'behausung_ct' is invalid. Element 'sequence' is invalid, misplaced, or occurs too often. 

Error 4: The content type of a derived type and that of its base must both be mixed or both be element-only. Type 'gebäude_ct' is element only, but its base type is not. 

Error 5: The content type of a derived type and that of its base must both be mixed or both be element-only. Type 'teich_ct' is element only, but its base type is not. 

錯誤1-3是同一類型的。錯誤4和5也是同一類型。 關於錯誤4 + 5:爲什麼派生類型必須具有與父類型相同的內容類型?這不能是xml架構規則的一部分......這將是荒謬的。如果擴展類與父類相比只有一個附加元素(例如由元素和屬性組成),該怎麼辦?我是否必須爲派生類創造一些屬性才能使這兩個類相似?

我根本不明白我的代碼有什麼問題。

回答

2

錯誤1-3有意義,4-5錯誤。也許他們會在你解決前三個問題時離開。編譯器在發現錯誤後繼續嘗試繼續時出現奇怪的路徑並不罕見。

使用撒克遜驗證模式,我得到katze_ct和fink_ct誤差,這可以是固定的通過去除雜散複雜內容元素:

<xs:complexType name="katze_ct"> 
    <xs:sequence> 
     <xs:element name="fellfarbe" type="xs:string"/> 
    </xs:sequence> 
</xs:complexType> 

和behausing_ct因爲XS一個錯誤:屬性是錯誤的,它應該是:

<xs:complexType name="behausung_ct">  
     <xs:sequence> 
      <xs:element name="zootier" type="tier" maxOccurs="unbounded"/> 
     </xs:sequence> 
     <xs:attribute name="name" type="xs:string"/> 
    </xs:complexType> 

通過這些更改,模式現在可以編譯。

僅限元素類型不能擴展混合類型或反過來的規則是完全明智的規則,但不違反規則。

+0

非常感謝您的回答!現在正在驗證。 – Tommy

相關問題