使用模式验证重复元素组

时间:2012-03-31 19:31:07

标签: xml validation codeigniter xsd schema

我在尝试使用我的架构进行xml验证时遇到了很大困难。 发生以下错误:

   Element 'date': This element is not expected. Expected is ( race ).
Element 'date': This element is not expected. Expected is ( race ).
Element 'date': This element is not expected. Expected is ( race ).
Element 'date': This element is not expected. Expected is ( race ). 

以下是我的架构                                                                                                                                                                                        

<!-- Structure definition -->
    <xs:element name="history">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="period" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="period">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="date" minOccurs="1" maxOccurs="unbounded"/>
                <xs:element ref="track" minOccurs="1" maxOccurs="unbounded"/>
                <xs:element ref="race" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="year" type="Tyear"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="date">
        <xs:simpleType>
            <xs:restriction base="xs:string"/>
        </xs:simpleType>
    </xs:element>
    <xs:element name="track">
        <xs:simpleType>
            <xs:restriction base="xs:string"/>
        </xs:simpleType>
    </xs:element>
    <xs:element name="race">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="rider" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="nr" type="Tracenr"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="rider">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="place" maxOccurs="1"/>
                <xs:element ref="first_name" maxOccurs="1"/>
                <xs:element ref="last_name" maxOccurs="1"/>
                <xs:element ref="manufacturer" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="place">
        <xs:simpleType>
            <xs:restriction base="xs:string"/>
        </xs:simpleType>
    </xs:element>
    <xs:element name="first_name">
        <xs:simpleType>
            <xs:restriction base="xs:string"/>
        </xs:simpleType>
    </xs:element>
    <xs:element name="last_name">
        <xs:simpleType>
            <xs:restriction base="xs:string"/>
        </xs:simpleType>
    </xs:element>
    <xs:element name="manufacturer">
        <xs:simpleType>
            <xs:restriction base="xs:string"/>
        </xs:simpleType>
    </xs:element>

</xs:schema>

这是我的xml

的一个小快照
<history>
  <period year="2011">
    <date>10/16/2011</date>
    <track>Portimao</track>
    <race nr="1">
      <rider>
        <place>1</place>
        <first_name>Marco</first_name>
        <last_name>Melandri</last_name>
        <manufacturer>Yamaha</manufacturer>
      </rider>
      <rider>
        <place>2</place>
        <first_name>Eugene</first_name>
        <last_name>Laverty</last_name>
        <manufacturer>Yamaha</manufacturer>
      </rider>
      [...]
      <rider>
        <place>DNF</place>
        <first_name>Tom</first_name>
        <last_name>Sykes</last_name>
        <manufacturer>Kawasaki</manufacturer>
      </rider>
    </race>
    <date>10/16/2011</date>
    <track>Portimao</track>
    <race nr="2">
      <rider>
        <place>1</place>
        <first_name>Carlos</first_name>
        <last_name>Checa</last_name>
        <manufacturer>Ducati</manufacturer>
      </rider>
      <rider>
        <place>2</place>
        <first_name>Sylvain</first_name>
        <last_name>Guintoli</last_name>
        <manufacturer>Ducati</manufacturer>
      </rider>
      [...]
      <rider>
        <place>15</place>
        <first_name>Noriyuki</first_name>
        <last_name>Haga</last_name>
        <manufacturer>Aprilia</manufacturer>
      </rider>
    </race>
    <date>10/02/2011</date>
    <track>Magny-Cours</track>
    <race nr="1">
      <rider>
        <place>1</place>
        <first_name>Carlos</first_name>
        <last_name>Checa</last_name>
        <manufacturer>Ducati</manufacturer>
      </rider>
      [...]

前一年的“时期”再重复3次。 谢谢你

1 个答案:

答案 0 :(得分:1)

由于序列(日期,曲目,种族)重复,您需要指定:

    <xs:sequence maxOccurs="unbounded">
        <xs:element ref="date" minOccurs="1" maxOccurs="unbounded"/>
        <xs:element ref="track" minOccurs="1" maxOccurs="unbounded"/>
        <xs:element ref="race" minOccurs="1" maxOccurs="unbounded"/>
    </xs:sequence>