smrtdvlpr
smrtdvlpr

Reputation: 38

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>[email protected]</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.

Upvotes: 1

Views: 987

Answers (2)

kjhughes
kjhughes

Reputation: 111660

The errors are referring to the fact that the xs:simpleType declarations for emailAddress and mobileNum ought really be element declarations. (xs:simpleType cannot appear as a child of xs:sequence, but xs:element can.) You can retain the xs:simpleType anonymously beneath named element declarations, much like the rest of your XSD already does with xs:complexType above that point.

After making those fixes to the XSD, you'll also find that your XML fails to comply with the XSD at two points: admissionYear has a stray / char, and mobileNum follows the wrong format. Note that if you wish (123) 456-7890 to be a valid mobileNum, you'll have to update the XSD also to escape ( and ) in the xs:pattern regex.

All of the above fixes have been applied to the following XML and XSD, which now validate successfully:

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>[email protected]</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>

Upvotes: 2

Yitzhak Khabinsky
Yitzhak Khabinsky

Reputation: 22212

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.

Upvotes: 2

Related Questions