2017-09-18 48 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

相关问题