XSD错误元素元素无效,放错位置或过于频繁发生

时间:2018-04-23 21:51:25

标签: xml validation xsd

我写了一个XML并试图针对XSD验证它,但是我遇到了一些错误,我无法解决它们。

以下是错误消息:

Error - Line 38, 128: org.xml.sax.SAXParseException; lineNumber: 38; columnNumber: 128; s4s-elt-invalid-content.1: The content of '#AnonType_symptomsmedicindatapatientpatientshospital' is invalid.  Element 'element' is invalid, misplaced, or occurs too often.
    Error - Line 43, 129: org.xml.sax.SAXParseException; lineNumber: 43; columnNumber: 129; s4s-elt-invalid-content.1: The content of '#AnonType_diagnosemedicindatapatientpatientshospital' is invalid.  Element 'element' is invalid, misplaced, or occurs too often.
    Error - Line 48, 131: org.xml.sax.SAXParseException; lineNumber: 48; columnNumber: 131; s4s-elt-invalid-content.1: The content of '#AnonType_medicationmedicindatapatientpatientshospital' is invalid.  Element 'element' is invalid, misplaced, or occurs too often.
    Error - Line 66, 54: org.xml.sax.SAXParseException; lineNumber: 66; columnNumber: 54; s4s-elt-invalid-content.1: The content of '#AnonType_doctorsstaffhospital' is invalid.  Element 'element' is invalid, misplaced, or occurs too often.
    Error - Line 108, 87: org.xml.sax.SAXParseException; lineNumber: 108; columnNumber: 87; s4s-elt-invalid-content.1: The content of '#AnonType_fieldsnursesstaffhospital' is invalid.  Element 'element' is invalid, misplaced, or occurs too often.

我理解元素'元素'在特定的行。我现在检查了我的XSD超过一个小时,但我看不出它的错误。

我希望你能帮助我。

XML:

    <?xml version="1.0" encoding="UTF-8"?>
<hospital xmlns="http://www.hospitalinfo.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hospitalinfo.com hospital.xsd">
   <patients>
      <patient id="1954A">
         <basicdata>
            <address>
               <firstname title="Dr.">Nina</firstname>
               <lastname>Meyer</lastname>
               <street housenumber="22/3a">Münchnerstraße</street>
               <postcode>65908</postcode>
               <location>Grasensteinen</location>
            </address>
         </basicdata>
         <medicindata>
            <symptoms>
               <symptom>pain in chest</symptom>
               <symptom>breathlessness</symptom>
               <symptom>emesis</symptom>
            </symptoms>
            <diagnose>
               <disorder>myocardial infarktion</disorder>
               <disorder>angina pectoris</disorder>
            </diagnose>
            <medication>
               <medicament>Betablocker</medicament>
               <medicament>Azetylsalizylsäure</medicament>
            </medication>
         </medicindata>
      </patient>
      <patient id="4969Z">
         <basicdata>
            <address>
               <firstname title="">Chris</firstname>
               <lastname>Richard</lastname>
               <street housenumber="3">Wielandsraße</street>
               <postcode>98150</postcode>
               <location>Blumensee</location>
            </address>
         </basicdata>
         <medicindata>
            <symptoms>
               <symptom>abdominal pain</symptom>
            </symptoms>
            <diagnose>
               <disorder>acute appendictis</disorder>
            </diagnose>
         </medicindata>
      </patient>
   </patients>
   <staff>
      <doctors>
         <doctor>
            <name>
               <firstname title="Dr.">Leyla</firstname>
               <lastname>Richardson</lastname>
            </name>
            <fields>
               <field>cardiology</field>
            </fields>
         </doctor>
         <doctor>
            <name>
               <firstname title="">Lara</firstname>
               <lastname>Nick</lastname>
            </name>
            <fields>
               <field>general practitioner</field>
            </fields>
         </doctor>
      </doctors>
      <nurses>
         <nurse>
            <name>
               <firstname title="">Mell</firstname>
               <lastname>Grey</lastname>
            </name>
            <fields>
               <field>cardiology</field>
            </fields>
         </nurse>
      </nurses>
   </staff>
</hospital>

XSD:

    <?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.hospitalinfo.com" targetNamespace="http://www.hospitalinfo.com" elementFormDefault="qualified">
   <xs:element name="hospital">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="patients" minOccurs="1" maxOccurs="unbounded">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="patient">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element name="basicdata">
                                 <xs:complexType>
                                    <xs:sequence>
                                       <xs:element name="address">
                                          <xs:complexType>
                                             <xs:sequence>
                                                <xs:element name="firstname">
                                                   <xs:complexType>
                                                      <xs:attribute name="housenumber" type="xs:string" />
                                                   </xs:complexType>
                                                </xs:element>
                                                <xs:element name="lastname" type="xs:string" />
                                                <xs:element name="street" type="xs:string" />
                                                <xs:element name="postcode" type="xs:string" />
                                                <xs:element name="location" type="xs:string" />
                                             </xs:sequence>
                                          </xs:complexType>
                                       </xs:element>
                                    </xs:sequence>
                                 </xs:complexType>
                              </xs:element>
                              <xs:element name="medicindata">
                                 <xs:complexType>
                                    <xs:sequence>
                                       <xs:element name="symptoms">
                                          <xs:complexType>
                                             <xs:element name="symptom" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
                                          </xs:complexType>
                                       </xs:element>
                                       <xs:element name="diagnose">
                                          <xs:complexType>
                                             <xs:element name="disorder" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
                                          </xs:complexType>
                                       </xs:element>
                                       <xs:element name="medication">
                                          <xs:complexType>
                                             <xs:element name="medicament" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
                                          </xs:complexType>
                                       </xs:element>
                                    </xs:sequence>
                                 </xs:complexType>
                              </xs:element>
                           </xs:sequence>
                           <xs:attribute name="id" type="xs:string" />
                        </xs:complexType>
                     </xs:element>
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
            <xs:element name="staff">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="doctors">
                        <xs:complexType>
                           <xs:element name="doctor">
                              <xs:complexType>
                                 <xs:sequence>
                                    <xs:element name="name">
                                       <xs:complexType>
                                          <xs:sequence>
                                             <xs:element name="firstname">
                                                <xs:complexType>
                                                   <xs:attribute name="title" type="xs:string" />
                                                </xs:complexType>
                                             </xs:element>
                                             <xs:element name="lastname" type="xs:string" />
                                          </xs:sequence>
                                       </xs:complexType>
                                    </xs:element>
                                    <xs:element name="fields">
                                       <xs:complexType>
                                          <xs:element name="field" type="xs:string" />
                                       </xs:complexType>
                                    </xs:element>
                                 </xs:sequence>
                              </xs:complexType>
                           </xs:element>
                        </xs:complexType>
                     </xs:element>
                     <xs:element name="nurses">
                              <xs:complexType>
                                 <xs:sequence>
                                    <xs:element name="name">
                                       <xs:complexType>
                                          <xs:sequence>
                                             <xs:element name="firstname">
                                                <xs:complexType>
                                                   <xs:attribute name="title" type="xs:string" />
                                                </xs:complexType>
                                             </xs:element>
                                             <xs:element name="lastname" type="xs:string" />
                                          </xs:sequence>
                                       </xs:complexType>
                                    </xs:element>
                                    <xs:element name="fields">
                                       <xs:complexType>
                                          <xs:element name="field" type="xs:string" />
                                       </xs:complexType>
                                    </xs:element>
                                 </xs:sequence>
                              </xs:complexType>
                           </xs:element>
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>

1 个答案:

答案 0 :(得分:2)

它抱怨你的架构。

您有xs:element作为多个地方xs:complexType元素的直接子元素,这是不允许的。

您需要将这些元素嵌套在合成器中,例如xs:sequencexs:choicexs:all

例如,要更正症状问题,您可以使用xs:choice

<xs:element name="symptoms">
  <xs:complexType>
    <xs:choice>
      <xs:element name="symptom" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
    </xs:choice>
  </xs:complexType>
</xs:element>

阅读本快速教程,解释它们之间的区别以及为什么可以使用其中之一:http://www.developerdrive.com/2012/01/xml-schema-xsd-using-compositors/