2017-09-18 49 views
2

我在嘗試使用我的XSD文件驗證我的xml文件時出現錯誤,說明The Content Of 'all' Must Match (annotation?, Element*). A Problem Was Found Starting At: Sequence.。我已經使用<xs:all>,因爲size的數量可能會在子根節點中發生變化。而且我不確定是否應在<xs:all>標記之前或之後添加屬性標記。錯誤:「全部」的內容必須匹配

這是我的xml文件

<?xml version="1.0" encoding="UTF-8"?> 
 
<catalog> 
 
    <product description="Cardigan Sweater" product_image="cardigan.jpg"> 
 
     <catalog_item gender="Men's"> 
 
     <item_number>QWZ5671</item_number> 
 
     <price>39.95</price> 
 
     <size description="Medium"> 
 
      <color_swatch image="red_cardigan.jpg">Red</color_swatch> 
 
      <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch> 
 
     </size> 
 
     <size description="Large"> 
 
      <color_swatch image="red_cardigan.jpg">Red</color_swatch> 
 
      <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch> 
 
     </size> 
 
     </catalog_item> 
 
     <catalog_item gender="Women's"> 
 
     <item_number>RRX9856</item_number> 
 
     <price>42.50</price> 
 
     <size description="Small"> 
 
      <color_swatch image="red_cardigan.jpg">Red</color_swatch> 
 
      <color_swatch image="navy_cardigan.jpg">Navy</color_swatch> 
 
      <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch> 
 
     </size> 
 
     <size description="Medium"> 
 
      <color_swatch image="red_cardigan.jpg">Red</color_swatch> 
 
      <color_swatch image="navy_cardigan.jpg">Navy</color_swatch> 
 
      <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch> 
 
      <color_swatch image="black_cardigan.jpg">Black</color_swatch> 
 
     </size> 
 
     <size description="Large"> 
 
      <color_swatch image="navy_cardigan.jpg">Navy</color_swatch> 
 
      <color_swatch image="black_cardigan.jpg">Black</color_swatch> 
 
     </size> 
 
     <size description="Extra Large"> 
 
      <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch> 
 
      <color_swatch image="black_cardigan.jpg">Black</color_swatch> 
 
     </size> 
 
     </catalog_item> 
 
    </product> 
 
</catalog>

這是我的XSD文件。不知道我的a是否被放置在正確的位置。

<?xml version="1.0"?> 
 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
 
    <xs:element name="catalog"> 
 
    <xs:complexType> 
 
    <xs:sequence> 
 
    <xs:element name="product"> 
 
     <xs:complexType> 
 
     <xs:sequence> 
 
     <xs:element name="catalog_item"> 
 
     <xs:complexType> 
 
      <xs:sequence> 
 
      <xs:element name="item_number"> 
 
      <xs:simpleType> 
 
      <xs:restriction base="xs:string"> 
 
       <xs:pattern value="[A-Z][A-Z][A-Z][0-9][0-9][0-9][0-9]"/> 
 
      </xs:restriction> 
 
      </xs:simpleType> 
 
      </xs:element> 
 
      <xs:element name="price" type="xs:decimal"/> 
 
      <xs:element name="size"> 
 
      <xs:complexType> 
 
      <xs:all minOccurs="1"> 
 
       <xs:sequence> 
 
       <xs:element name="color_swatch" type="xs:string"> 
 
       <xs:complexType> 
 
       <xs:sequence minOccurs="1" maxOccurs="unbounded"> 
 
       </xs:sequence> 
 
\t \t \t \t <xs:attribute name="image" type="xs:string"/> 
 
       </xs:complexType> 
 
       </xs:element> 
 
       </xs:sequence> 
 
\t \t \t <xs:attribute name="description" type="xs:string"/> 
 
      </xs:all> 
 
      </xs:complexType> 
 
      </xs:element> 
 
\t \t </xs:sequence> 
 
\t \t <xs:attribute name="gender" type="xs:string"/> 
 
\t \t </xs:complexType> 
 
     </xs:element> 
 
\t </xs:sequence> 
 
\t <attribute name="description" type="xs:string"/> 
 
    <attribute name="product_image" type="xs:string"/> 
 
    </xs:complexType> 
 
    </xs:element> 
 
    </xs:sequence> 
 
    </xs:complexType> 
 
</xs:element> 
 
</xs:schema>

回答

3

假設你要允許一些在<catalog_item><size>元素,你需要設置maxOccurs="unbounded"size元素。

<xs:element name="size" maxOccurs="unbounded"> 

之後,你需要糾正的<size>內容定義 - 有使用<xs:all>在這裏沒有一點(記住<xs:all>意味着「論文內容需要在這方面恰好發生一次,無論爲了」 )。這應該滿足你的要求:

<xs:element name="size" maxOccurs="unbounded"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="color_swatch" maxOccurs="unbounded"> 
       <xs:complexType mixed="true"> 
        <xs:attribute name="image" type="xs:string"/> 
       </xs:complexType> 
       </xs:element> 
      </xs:sequence> 
      <xs:attribute name="description" type="xs:string"/> 
     </xs:complexType> 
    </xs:element> 

PS:你忘了在最後兩個屬性聲明的「xs:」前綴:

<attribute name="description" type="xs:string"/> 
<attribute name="product_image" type="xs:string"/> 
+1

這解決了當前的問題。但現在我得到一個錯誤「說無效的內容被發現從元素'catalog_item'開始。沒有子元素預計在這一點上,線'15',列'38'」 我認爲這個錯誤出現在XML文件。是否因爲我的xsd文件中定義的'color_swatch'元素不允許xml具有多個元素? –

+0

很可能你需要在''上設置'maxOccurs =「unbounded」'。 – potame

相關問題