为什么此XML不能使用此模式进行验证

时间:2014-05-01 08:17:40

标签: xml xsd xerces

我正在尝试编写一个模式文件来验证xml。我正在使用Xerces进行验证。我收到这条消息:

Line: 10 Column: 15 Message: element 'chord' is not allowed for content model '(chord)'

但我不明白它试图告诉我什么。这是我的XML和我的XSD

非常感谢提前。

这是我的XML

<song>
  <section tempo='120' producer='Vanilla'>
    <harmony key='Eb'>
      <chord duration='4' chord='Cm'/>
      <chord duration='4' chord='Fm'/>
      <chord duration='4' chord='Ab'/>
      <chord duration='4' chord='Bb'/>
    </harmony>
    <orchestration>
      <track type='Agent' instrumentID='HarderTo_Drums' pattern='HarderTo/HarderTo_Drums_Verse' micID='' gain='0.1'/>
    </orchestration>
  </section>
</song>

这是我的XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <!-- definition of attributes -->
    <xs:attribute name="type" type="xs:string"/>
    <xs:attribute name="agentID" type="xs:string"/>
    <xs:attribute name="insrumentID" type="xs:string"/>
    <xs:attribute name="pattern" type="xs:string"/>
    <xs:attribute name="role" type="xs:string"/>
    <xs:attribute name="gain" type="xs:float"/>                         <!-- This should be a positive float value.  Should have min and max values -->
    <xs:attribute name="id" type="xs:string"/>                          <!-- This should be a GUID -->
    <xs:attribute name="autotuneID" type="xs:nonNegativeInteger"/>
    <xs:attribute name="key" type="xs:string"/>
    <xs:attribute name="duration" type="xs:float"/>
    <xs:attribute name="chord" type="xs:string"/>
    <xs:attribute name="tempo" type="xs:positiveInteger"/>
    <xs:attribute name="producer" type="xs:string"/>

    <!-- definition of a agent track -->
    <xs:complexType name="agentTrack">
        <xs:attribute ref="type" use="required"/>
        <xs:attribute ref="insrumentID" use="required"/>
        <xs:attribute ref="pattern" use="required"/>
        <xs:attribute ref="role"/>
        <xs:attribute ref="gain"/>
    </xs:complexType>

    <!-- definition of a agent track -->
    <xs:complexType name="drumTrack">
        <xs:attribute ref="type" use="required"/>
        <xs:attribute ref="insrumentID" use="required"/>
        <xs:attribute ref="pattern" use="required"/>
        <xs:attribute ref="gain"/>
    </xs:complexType>

    <!-- definition of a lick track -->
    <xs:complexType name="lickTrack">
        <xs:attribute ref="type" use="required"/>
        <xs:attribute ref="agentID" use="required"/>
        <xs:attribute ref="pattern" use="required"/>
        <xs:attribute ref="role"/>
        <xs:attribute ref="gain"/>
    </xs:complexType>

    <!-- definition of a vocal track -->
    <xs:complexType name="vocalTrack">
        <xs:attribute ref="type" use="required"/>
        <xs:attribute ref="id" use="required"/>
        <xs:attribute ref="agentID" use="required"/>
        <xs:attribute ref="insrumentID" use="required"/>
        <xs:attribute ref="gain"/>
    </xs:complexType>

    <!-- defination of a chord -->
    <xs:complexType name="chordType">
        <xs:attribute ref="duration" use="required"/>
        <xs:attribute ref="chord" use="required"/>
    </xs:complexType>

    <!-- definition of a harmony -->
    <xs:complexType name="harmony">
        <xs:sequence>
            <xs:element name="chord" type="chordType" minOccurs="1"/>
        </xs:sequence>
        <xs:attribute ref="key" use="required"/>
    </xs:complexType>

    <!-- definition of an orchestration -->
    <xs:complexType name="orchestration">
        <xs:all>
            <xs:element name="agentTrack" type="agentTrack" minOccurs="0"/>
            <xs:element name="drumTrack" type="drumTrack" minOccurs="0"/>
            <xs:element name="lickTrack" type="lickTrack" minOccurs="0"/>
            <xs:element name="vocalTrack" type="vocalTrack" minOccurs="0"/>
        </xs:all>
    </xs:complexType>

    <!-- definition of a section -->
    <xs:complexType name="section">
        <xs:all>
            <xs:element name="harmony" type="harmony" minOccurs="1" maxOccurs="1"/>
            <xs:element name="orchestration" type="orchestration" minOccurs="1" maxOccurs="1"/>
        </xs:all>
        <xs:attribute ref="tempo" use="required"/>
        <xs:attribute ref="producer" use="required"/>
    </xs:complexType>

    <!-- definition of a song -->
    <xs:element name="song">
        <xs:complexType>
            <xs:all>
                <xs:element name="section" type="section" minOccurs="1"/>
            </xs:all>
        </xs:complexType>
    </xs:element>
</xs:schema>

1 个答案:

答案 0 :(得分:2)

<xs:element name="chord" type="chordType" minOccurs="1"/>

您尚未指定maxOccurs,且默认值为1,因此架构在此处需要正好1 chord元素,并且当它看到第二个时它会抱怨一。如果您希望每个和声允许多个和弦,请添加maxOccurs="unbounded"