【问题标题】:XML Validation issue: error s4s-elt-must-matchXML 验证问题:错误 s4s-elt-must-match
【发布时间】:2012-11-20 03:29:48
【问题描述】:

我目前正在使用一些基本的 XS Schema 东西,但是在尝试验证我的架构时遇到了一个恼人的错误。我正在使用 XMLValidation.com,我得到的错误是:

s4s-elt-must-match.1: The content of 'lecturers' must match (annotation?, (simpleType | complexType)?, (unique | key | keyref)*)). A problem was found starting at: element.

那么,我的XSD如下:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="lecturers">
    <xs:element name="lecturer">
        <xs:complexType>
            <xs:sequence>
            <xs:element name="name" type="xs:string" />
            <xs:attribute name="title" type="xs:string" />
            <xs:attribute name="first" type="xs:string" />
            <xs:attribute name="last" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
    <xs:element name="teaching">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="course" type="xs:string" />
            <xs:attribute name="code" type="xs:string" />
            <xs:element name="course" type="xs:string" />
            <xs:attribute name="code" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
    </xs:element>
        <xs:element name="research" type="xs:string"/>
    </xs:element>
</xs:element>
</xs:schema>

相应的 XML 文件如下所示:

<?xml version="1.0"?>
<lecturers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="lecturers.xsd"> 
   <lecturer>   
  <name title="Professor" first="Peter" last="Quirk" /> 
  <teaching> 
    <course code="CO3070">XML and the Web</course> 
    <course code="CO3300">Web Server Architectures</course> 
  </teaching> 
<research>The application of Web protocols to Biology</research> 
   </lecturer> 
</lecturers>

任何人都知道为什么我的代码无法验证以及我可以做些什么来解决它。提前致谢

【问题讨论】:

    标签: xml validation xsd schema


    【解决方案1】:

    我得到了以下工作......

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="lecturers">
      <xs:complexType>
        <xs:sequence maxOccurs="unbounded">
          <xs:element name="lecturer">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="name">
                  <xs:complexType>
                    <xs:attribute name="title" type="xs:string" />
                    <xs:attribute name="first" type="xs:string" />
                    <xs:attribute name="last" type="xs:string" />
                  </xs:complexType>
                </xs:element> <!-- name -->
                <xs:element name="teaching">
                  <xs:complexType>
                    <xs:sequence maxOccurs="unbounded">
                      <xs:element name="course">
                        <xs:complexType mixed="true">
                          <xs:attribute name="code" type="xs:string" />
                        </xs:complexType>
                      </xs:element> <!-- course -->
                    </xs:sequence>
                  </xs:complexType>
                </xs:element> <!-- teaching -->
                <xs:element name="research" type="xs:string"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element> <!-- lecturer -->
        </xs:sequence>
      </xs:complexType>
    </xs:element> <!-- lecturers -->
    </xs:schema>
    

    【讨论】:

      【解决方案2】:

      讲师必须像其他元素一样包含 complexType :-

      <xs:element name="lecturers">
          <xs:complexType>
              <xs:sequence>
                  <xs:element name="lecturer">
                      <xs:complexType>
                          <xs:sequence>
      

      ...

      这个错误虽然很神秘,但确实如此。

      【讨论】:

        【解决方案3】:

        为了让 xml 得到验证,这个模式会做到这一点。

        <xs:element name="teaching">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="course" maxOccurs="unbounded">
                <xs:complexType>
                <xs:simpleContent>
                <xs:extension base="xs:string">
                <xs:attribute name="code" type="xs:string" />
                </xs:extension>
                </xs:simpleContent> 
                </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
        </xs:element>
        <xs:element name="research" type="xs:string"/>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-09-10
          • 1970-01-01
          • 2019-08-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多