发现以 element.'catalog_item' 开头的无效内容
Invalid content was found starting with element.'catalog_item'
我收到一条错误消息 错误 - 第 16、38 行:org.xml.sax.SAXParseException;行号:16;列数:38; cvc-complex-type.2.4.d:发现以元素 'catalog_item' 开头的无效内容。此时不需要子元素。 第 16 行。我应该写一个 xsd 来验证给定的 xml 文件。
这是给我的 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>
这是我对上述 xml 文件的 xsd 文件。
<?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" 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>
</xs:sequence>
<xs:attribute name="gender" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="description" type="xs:string"/>
<xs:attribute name="product_image" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The maxOccurs
occurrence constraint defaults to 1,所以你的 XSD 只允许一个 catalog_item
,而你的 XML 有两个。
要允许多个 catalog_item
元素,请添加 maxOccurs="unbounded"
...
改变
<xs:element name="catalog_item">
到
<xs:element name="catalog_item" maxOccurs="unbounded">
我收到一条错误消息 错误 - 第 16、38 行:org.xml.sax.SAXParseException;行号:16;列数:38; cvc-complex-type.2.4.d:发现以元素 'catalog_item' 开头的无效内容。此时不需要子元素。 第 16 行。我应该写一个 xsd 来验证给定的 xml 文件。
这是给我的 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>
这是我对上述 xml 文件的 xsd 文件。
<?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" 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>
</xs:sequence>
<xs:attribute name="gender" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="description" type="xs:string"/>
<xs:attribute name="product_image" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The maxOccurs
occurrence constraint defaults to 1,所以你的 XSD 只允许一个 catalog_item
,而你的 XML 有两个。
要允许多个 catalog_item
元素,请添加 maxOccurs="unbounded"
...
改变
<xs:element name="catalog_item">
到
<xs:element name="catalog_item" maxOccurs="unbounded">