错误:“所有”的内容必须匹配

时间:2017-09-18 11:56:24

标签: xml xsd

我在尝试使用我的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>
				 <xs:attribute name="image" type="xs:string"/>
                </xs:complexType>
               </xs:element>
              </xs:sequence>
			  <xs:attribute name="description" type="xs:string"/>
             </xs:all>
            </xs:complexType>
           </xs:element>
		 </xs:sequence>
		 <xs:attribute name="gender" type="xs:string"/>
		</xs:complexType>
       </xs:element>
	</xs:sequence>
	<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>

1 个答案:

答案 0 :(得分:3)

假设您要在<size>中允许多个<catalog_item>元素,则需要在maxOccurs="unbounded"元素上设置size

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

之后,您需要更正<size>的内容定义 - 此处使用<xs:all>没有意义(请记住<xs:all>表示&#34;} 这些元素在此上下文中只需要发生一次,无论顺序如何&#34;)。这应该符合您的要求:

    <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:你忘记了&#34; xs:&#34;最后两个属性声明的前缀:

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