Search code examples
xmlxsdxsd-validationxml-validation

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


So I am facing this error while I was validating XML against XSD

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.

in short

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

I am attaching my xml and xsd files for reference,

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>

I saw online at many posts where similar bug error appeared. I checked my code, I couldn't find the problem. I thought maybe it's because of xs and not xsd, but I don't think that is the bug. I think I have put simpleType and complexType the right way. Please let me know.


Solution

  • I fixed the XSD. You just missed to declare both elements: emailAddress and mobileNum.

    Though it is failing now to validate the input XML based on the actual values.

    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 validation errors

    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.