错误:从元素

时间:2016-01-15 20:15:59

标签: xml xsd xsd-validation

我无法让我的文件验证。

以下是我遇到的错误。

  

4:14 cvc-complex-type.2.4.a:找到无效的内容   元素'AirportList'。预计会有一个'{“”:Airport}'。

     

47:15 XML文档结构必须以相同的方式开始和结束   实体。

我将在下面发布我的XML文档代码和我的XSD架构。我是新手,所以我不确定我做错了什么。我已经改变了我的文件格式,但我仍然得到相同的错误。

<?xml version="1.0"?>
<AirportList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="filename.xsd"> 
<AirportList>
    <Airport>
        <name>Abbotsford International Airport</name>
        <community>Abbotsford</community>
        <province>British Columbia</province>
        <passengers>15</passengers>
    </Airport>
    <Airport>
        <name>Atlin Airport</name>
        <community>Atlin</community>
        <province>British Columbia</province>
        <passengers>15</passengers>
    </Airport>
    <Airport>
        <name>Atlin Water Aerodrome</name>
        <community>Atlin</community>
        <province>British Columbia</province>
        <passengers>15</passengers>
    </Airport>
    <Airport>
        <name>Baie-Comeau Water Aerodrome</name>
        <community>Baie-Comeau</community>
        <province>Quebec</province>
        <passengers>15</passengers>
    </Airport>
    <Airport>
        <name>Beaver Creek Airport</name>
        <community>Beaver Creek</community>
        <province>Yukon</province>
        <passengers>15</passengers>
    </Airport>
    <Airport>
        <name>Bedwell Harbour Water Aerodrome</name>
        <community>Bedwell Harbour</community>
        <province>British Columbia</province>
        <passengers>15</passengers>
    </Airport>
    <Airport>
        <name>Billy Bishop Toronto City Airport</name>
        <community>Toronto</community>
        <province>Ontario</province>
        <passengers>15</passengers>
    </Airport>
</AirportList>


<?xml version="1.0"?>
<!-- XSD Schema for simple_apoole33_IT_MUST_VALIDATE.xml -->

<xsd:schema xmlns:xsd=
    "http://www.w3.org/2001/XMLSchema">

    <xsd:element name="AirportList">
        <xsd:complexType>
        <xsd:sequence>

            <xsd:element name="Airport" 
                maxOccurs="unbounded">
                <xsd:complexType>
                <xsd:sequence>

                    <xsd:element name="name"type="xsd:string"/>

                    <xsd:element name="community"type="xsd:string"/>

                    <xsd:element name="province"type="xsd:string"/>

                    <xsd:element name="passengers"type="xsd:integer" minOccurs = "0"/>

                </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

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

1 个答案:

答案 0 :(得分:3)

你非常接近。只需进行以下两项更改:

  1. 消除XML中的额外AirportList元素。
  2. 在XSD中的@name@type属性之间添加空格。
  3. 然后是您更正的XML,

    <?xml version="1.0"?>
    <AirportList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:noNamespaceSchemaLocation="simple_apoole33.xsd"> 
      <Airport>
        <name>Abbotsford International Airport</name>
        <community>Abbotsford</community>
        <province>British Columbia</province>
        <passengers>15</passengers>
      </Airport>
      <Airport>
        <name>Atlin Airport</name>
        <community>Atlin</community>
        <province>British Columbia</province>
        <passengers>15</passengers>
      </Airport>
      <Airport>
        <name>Atlin Water Aerodrome</name>
        <community>Atlin</community>
        <province>British Columbia</province>
        <passengers>15</passengers>
      </Airport>
      <Airport>
        <name>Baie-Comeau Water Aerodrome</name>
        <community>Baie-Comeau</community>
        <province>Quebec</province>
        <passengers>15</passengers>
      </Airport>
      <Airport>
        <name>Beaver Creek Airport</name>
        <community>Beaver Creek</community>
        <province>Yukon</province>
        <passengers>15</passengers>
      </Airport>
      <Airport>
        <name>Bedwell Harbour Water Aerodrome</name>
        <community>Bedwell Harbour</community>
        <province>British Columbia</province>
        <passengers>15</passengers>
      </Airport>
      <Airport>
        <name>Billy Bishop Toronto City Airport</name>
        <community>Toronto</community>
        <province>Ontario</province>
        <passengers>15</passengers>
      </Airport>
    </AirportList>
    

    将验证您已更正的XSD

    <?xml version="1.0"?>
    <!-- XSD Schema for simple_apoole33_IT_MUST_VALIDATE.xml -->
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="AirportList">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="Airport" 
                         maxOccurs="unbounded">
              <xsd:complexType>
                <xsd:sequence>
                  <xsd:element name="name" type="xsd:string"/>
                  <xsd:element name="community" type="xsd:string"/>
                  <xsd:element name="province" type="xsd:string"/>
                  <xsd:element name="passengers" type="xsd:integer" minOccurs="0"/>
                </xsd:sequence>
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
    
    根据要求