xsd:sequence:发现问题始于:simpleType

xsd:sequence: A problem was found starting at: simpleType

所以我在根据 XSD

验证 XML 时遇到了这个错误
Error - Line 38, 44: org.xml.sax.SAXParseException; lineNumber: 38; columnNumber: 44; s4s-elt-must-match.1: The content of 'sequence' must match (annotation?, (element | group | choice | sequence | any)*). A problem was found starting at: simpleType.
Error - Line 43, 41: org.xml.sax.SAXParseException; lineNumber: 43; columnNumber: 41; s4s-elt-must-match.1: The content of 'sequence' must match (annotation?, (element | group | choice | sequence | any)*). A problem was found starting at: simpleType.

简而言之

S4s-elt-must-match.1: The Content Of 'sequence' Must Match (annotation?, (element | Group | Choice | Sequence | Any)*). A Problem Was Found Starting At: SimpleType.

我附上我的 xml 和 xsd 文件以供参考,

academics.xml

<?xml version="1.0"?>
<academic>
  <idNumber>18sdss146</idNumber>
  <firstName>Piyush</firstName>
  <lastName>Bajaj</lastName>
  <degree>Btech CSE</degree>
  <admissionYear>2019/</admissionYear>
  <cgpa>9.8</cgpa>
  <interests>
    <interest>Competitive Programming</interest>
    <interest>Artificial Intelligence</interest>
    <interest>Web Development</interest>
  </interests>
  <eventsAttended>
    <event>Hello World</event>
    <event>GDG Hackathon</event>
    <event>Riviera Agaaz</event>
  </eventsAttended>
  <address>
    <houseNumber>A743</houseNumber>
    <streetName>Lincoln Street</streetName>
    <pincode>100200</pincode>
    <city>Delhi</city>
    <state>Delhi</state>
  </address>
  <emailAddress>piyush@gmail.com</emailAddress>
  <mobileNum>1234567890</mobileNum>
</academic>

academics.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">
  <xs:element name="academic">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="idNumber" type="xs:string"/>
        <xs:element name="firstName" type="xs:string"/>
        <xs:element name="lastName" type="xs:string"/>
        <xs:element name="degree" type="xs:string"/>
        <xs:element name="admissionYear" type="xs:integer"/>
        <xs:element name="cgpa" type="xs:decimal"/>
        <xs:element name="interests">
          <xs:complexType>
            <xs:sequence minOccurs="3" maxOccurs="5">
              <xs:element name="interest" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="eventsAttended">
          <xs:complexType>
            <xs:sequence minOccurs="3" maxOccurs="5">
              <xs:element name="event" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="address">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="houseNumber" type="xs:string"/>
              <xs:element name="streetName" type="xs:string"/>
              <xs:element name="pincode" type="xs:integer"/>
              <xs:element name="city" type="xs:string"/>
              <xs:element name="state" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:simpleType name="emailAddress">
          <xs:restriction base="xsd:string">
            <xs:pattern value="[^@]+@[^\.]+\..+"/>
          </xs:restriction>
        </xs:simpleType>
        <xs:simpleType name="mobileNum">
          <xs:restriction base="xs:string">
            <xs:pattern value="([0-9]{3}) [0-9]{3}-[0-9]{4}"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

我在网上看到很多帖子都出现了类似的bug错误。我检查了我的代码,我找不到问题。我认为可能是因为 xs 而不是 xsd,但我认为这不是错误。我想我已经正确地使用了 simpleType 和 complexType。请告诉我。

我修复了 XSD。您只是错过了声明两个元素:emailAddressmobileNum.

尽管现在无法根据实际值验证输入 XML。

XSD

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="unqualified">
    <xs:element name="academic">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="idNumber" type="xs:string"/>
                <xs:element name="firstName" type="xs:string"/>
                <xs:element name="lastName" type="xs:string"/>
                <xs:element name="degree" type="xs:string"/>
                <xs:element name="admissionYear" type="xs:integer"/>
                <xs:element name="cgpa" type="xs:decimal"/>
                <xs:element name="interests">
                    <xs:complexType>
                        <xs:sequence minOccurs="3" maxOccurs="5">
                            <xs:element name="interest" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="eventsAttended">
                    <xs:complexType>
                        <xs:sequence minOccurs="3" maxOccurs="5">
                            <xs:element name="event" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="address">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="houseNumber" type="xs:string"/>
                            <xs:element name="streetName" type="xs:string"/>
                            <xs:element name="pincode" type="xs:integer"/>
                            <xs:element name="city" type="xs:string"/>
                            <xs:element name="state" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="emailAddress" type="emailAddressType"/>
                <xs:element name="mobileNum" type="mobileNumType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="emailAddressType">
        <xs:restriction base="xs:string">
            <xs:pattern value="[^@]+@[^\.]+\..+"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="mobileNumType">
        <xs:restriction base="xs:string">
            <xs:pattern value="([0-9]{3}) [0-9]{3}-[0-9]{4}"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

XSD 验证错误

Error The 'admissionYear' element is invalid - The value '2019/' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:integer' - The string '2019/' is not a valid Integer value.
Error The 'mobileNum' element is invalid - The value '1234567890' is invalid according to its datatype 'mobileNumType' - The Pattern constraint failed.

错误是指 emailAddressmobileNumxs:simpleType 声明实际上应该是元素声明。 (xs:simpleType 不能作为 xs:sequence 的子元素出现,但 xs:element 可以。)您可以在命名元素声明下匿名保留 xs:simpleType,就像 [=] 的其余部分一样41=] 已经在那个点以上 xs:complexType 做了。

对 XSD 进行这些修复后,您还会发现您的 XML 在两点上不符合 XSD:admissionYear偏离 / 字符,并且 mobileNum 遵循错误的格式。请注意,如果您希望 (123) 456-7890 成为有效的 mobileNum,您还必须更新 XSD 以转义 () 27=]正则表达式。

以上所有修复已应用于以下 XML 和 XSD,现在验证成功:

XML

<?xml version="1.0"?>
<academic xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="try.xsd">
  <idNumber>18sdss146</idNumber>
  <firstName>Piyush</firstName>
  <lastName>Bajaj</lastName>
  <degree>Btech CSE</degree>
  <admissionYear>2019</admissionYear>
  <cgpa>9.8</cgpa>
  <interests>
    <interest>Competitive Programming</interest>
    <interest>Artificial Intelligence</interest>
    <interest>Web Development</interest>
  </interests>
  <eventsAttended>
    <event>Hello World</event>
    <event>GDG Hackathon</event>
    <event>Riviera Agaaz</event>
  </eventsAttended>
  <address>
    <houseNumber>A743</houseNumber>
    <streetName>Lincoln Street</streetName>
    <pincode>100200</pincode>
    <city>Delhi</city>
    <state>Delhi</state>
  </address>
  <emailAddress>piyush@gmail.com</emailAddress>
  <mobileNum>(123) 456-7890</mobileNum>
</academic>

XSD

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">
  <xs:element name="academic">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="idNumber" type="xs:string"/>
        <xs:element name="firstName" type="xs:string"/>
        <xs:element name="lastName" type="xs:string"/>
        <xs:element name="degree" type="xs:string"/>
        <xs:element name="admissionYear" type="xs:integer"/>
        <xs:element name="cgpa" type="xs:decimal"/>
        <xs:element name="interests">
          <xs:complexType>
            <xs:sequence minOccurs="3" maxOccurs="5">
              <xs:element name="interest" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="eventsAttended">
          <xs:complexType>
            <xs:sequence minOccurs="3" maxOccurs="5">
              <xs:element name="event" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="address">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="houseNumber" type="xs:string"/>
              <xs:element name="streetName" type="xs:string"/>
              <xs:element name="pincode" type="xs:integer"/>
              <xs:element name="city" type="xs:string"/>
              <xs:element name="state" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="emailAddress">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:pattern value="[^@]+@[^\.]+\..+"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name="mobileNum">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:pattern value="\([0-9]{3}\) [0-9]{3}-[0-9]{4}"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>