【问题标题】:Element 'sequence' is invalid, misplaced, or occurs too often元素“序列”无效、放错位置或出现过于频繁
【发布时间】:2016-02-04 19:58:18
【问题描述】:

我在使用架构验证 XML 文件时出错。

s4s-elt-invalid-content.1: '#AnonType_userusersroot' 的内容是 无效的。元素“序列”无效、错位或也出现 经常。 [25]

我的 XML 文件:

 <root>  
    <users>   
        <user code="10">
            <fullName>sandesh poudel</fullName>
            <sex>male</sex>
            <age>19</age>
            <phoneNo>239239</phoneNo>
            <address>Rasinkatu 13</address> 
            <title>owner</title> 
        </user>
        <user code ="20">
            <fullName>Surendra pandey</fullName>
            <sex>male</sex>
            <age>22</age>
            <phoneNo>3432</phoneNo>
            <address>kilo 13</address>
            <title>manager</title> 
        </user>
        <user code ="40">
            <fullName>sangam poudel</fullName>
            <sex>male</sex>
            <age>22</age>
            <phoneNo>239239</phoneNo>
            <address>sydney</address> 
            <title>Programmer</title> 
        </user>
    </users>> 
</root>

我的 XML 架构文件:

<xs:schema    xmlns:xs="http://www.w3.org/2001/XMLSchema"
              targetNamespace="http://www.w3schools.com"
              xmlns="http://www.w3schools.com"
              elementFormDefault="qualified">
    <xs:element name="root">
        <xs:complexType mixed="true">
            <xs:sequence maxOccurs="unbounded">
                <xs:element name="users">
                    <xs:complexType>
                        <xs:sequence maxOccurs="unbounded">

                            <xs:element name="user">


                                <xs:complexType mixed="true">
                                    <xs:attribute name="code" type="xs:string"/>
                                    <xs:sequence>

                                    <xs:element name="fullName" type="xs:string"/> 
                                    <xs:element name="sex" type="xs:string"/> 
                                    <xs:element name="age" type="xs:string"/> 
                                    <xs:element name="phoneNo" type="xs:string"/>
                                    <xs:element name="address" type="xs:string"/> 
                                    <xs:element name ="title" type="xs:string"/>
                                    </xs:sequence>
                                </xs:complexType>

                            </xs:element> <!-- name -->

                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

【问题讨论】:

    标签: xml xsd xml-validation


    【解决方案1】:

    您需要对 XSD 进行两项更改:

    1. 将xs:attribute 声明移动到下方xs:sequence。
    2. 从 XSD 中删除 xs:targetNamspace,因为没有使用命名空间 在 XML 中。

    总共:

    XSD

    <xs:schema    xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  elementFormDefault="qualified">
      <xs:element name="root">
        <xs:complexType mixed="true">
          <xs:sequence maxOccurs="unbounded">
            <xs:element name="users">
              <xs:complexType>
                <xs:sequence maxOccurs="unbounded">
                  <xs:element name="user">
                    <xs:complexType mixed="true">
                      <xs:sequence>
                        <xs:element name="fullName" type="xs:string"/> 
                        <xs:element name="sex" type="xs:string"/> 
                        <xs:element name="age" type="xs:string"/> 
                        <xs:element name="phoneNo" type="xs:string"/>
                        <xs:element name="address" type="xs:string"/> 
                        <xs:element name ="title" type="xs:string"/>
                      </xs:sequence>
                      <xs:attribute name="code" type="xs:string"/>
                    </xs:complexType>
                  </xs:element> <!-- name -->
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    【讨论】:

    • 谢谢.. 但是如果我只是删除 xs:targetSpace 它会显示一堆错误。但只是在序列后添加属性修复了以前的错误。
    • 对,这两个修复都是必要的。删除 xs:targetNamespace 的另一种方法是将命名空间添加到您的 XML。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多