XML 架构未验证

XML Schema is not validating

我正在研究 XML 和架构。我需要帮助来验证我的架构,因为当我将其放入在线验证器时,我收到以下错误:S4s-elt-must-match.1: 'sequence' 的内容必须匹配(注释?,(元素 |组 | 选择 | 顺序 | 任何)*)。发现问题始于:ComplexType。

我用的是俄罗斯套娃的设计,这个好像是我最喜欢的。

catalog.xml

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance" xsi:noNamespaceSchemaLocation="catalog.xsd">
   <photo cid="c1749" donatedBy="John Borelli">
      <name metadata="tunis cooper property museum">Tunis R. Cooper property</name>
      <description>
      <![CDATA[ 
         A more recent picture of the property taken by the Borelli family.  The property is listed in the 
         National and New Jersey Registers of Historic Places.
      ]]>
      </description>
      <date>circa 1950</date>
      <images>
         <img src="1749a.jpg" />
      </images>
   </photo>

   <photo cid="c1411" donatedBy="Saint Johns Catholic Church">
      <name metadata="saint johns catholic church">Saint Johns Church</name>
      <description>
      <![CDATA[ 
         A more recent picture of the property taken by the Borelli family.  The property is listed in the 
         National and New Jersey Registers of Historic Places.
      ]]>
      </description>      
      <date>1921</date>
   </photo>
</catalog>

catalog.xsd

<?xml version="1.0" encoding="UTF-8" ?>     
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="catalog">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="photo"/>
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="name" type="xs:string"/>
                        <xs:element name="description" type="xs:string"/>
                        <xs:element name="data" type="xs:string"/>
                        <xs:element name="images">
                            <xs:complexType>
                                <xs:simpleContent>
                                    <xs:element name="img"/>
                                </xs:simpleContent>
                            </xs:complexType>
                        </xs:element>
                        <xs:attribue name="cid" type="xs:string"/>
                        <xs:attribue name="donatedBy" type="xs:string"/>
                        <xs:attribue name="metadata" type="xs:string"/>
                        <xs:attribue name="src" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:sequence>
        </xs:complexType>
    </xs:element>    
</xs:schema>

您的 XSD 中有几个错误。还有一个在你的 XML 中。

要消除 XML 中的这一错误,请将 <catalog...> 元素中的命名空间从 xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance" 更改为 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"。一个简单的错字。

你的 XSD 应该看起来像这样来验证你的 XML:

<?xml version="1.0" encoding="UTF-8" ?>     
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="catalog">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="photo" maxOccurs="unbounded">
                    <xs:complexType>                        
                        <xs:sequence>
                            <xs:element name="name">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="xs:string">
                                            <xs:attribute name="metadata" type="xs:string"/>
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>                                
                            </xs:element>                            
                            <xs:element name="description" type="xs:string"/>
                            <xs:element name="date" type="xs:string"/>
                            <xs:element name="images" minOccurs="0">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="img"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>                            
                        </xs:sequence>
                        <xs:attribute name="cid" type="xs:string"/>
                        <xs:attribute name="donatedBy" type="xs:string"/>
                        <xs:attribute name="metadata" type="xs:string"/>
                        <xs:attribute name="src" type="xs:string"/>                        
                    </xs:complexType>
                </xs:element>                
            </xs:sequence>
        </xs:complexType>
    </xs:element>    
</xs:schema>

我改的是:

  1. 元素photo出现了不止一次,所以我加了maxOccurs="unbounded"
  2. 元素 name 可以有一个属性,所以我将其定义更改为带有 simpleContentcomplexType
  3. 元素 images 不必出现,因此我添加了 minOccurs="0"
  4. 我将属性从 xs:sequence 移到了 xs:complexType

现在 XSD 应该验证您的 XML。