1

I am validating my XSD through https://www.freeformatter.com/xml-validator-xsd.html but it throws an error

The content of sequence must match (annotation?, (element | Group | Choice | Sequence | Any)*). A problem was found starting at: Attribute

I need to make NOT NULL for the reportType. I used attribute (use="required") and tried to move the attribute out of sequence as well but it still throws an error. Any help will be appreciated.

Below is my XSD code.

<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="missing">

  <xs:complexType>
    <xs:sequence>
      <xs:element name="missingID" type="xs:string"/>
      <xs:attribute name="reportType" use="required"/>
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:minLength value= "1"/>
          <xs:maxLength value= "255"/>
        </xs:restriction>
      </xs:simpleType>

      <xs:element name="reportDescription">
        <xs:simpleType >
          <xs:restriction base="xs:string">
            <xs:minLength value= "1"/>
            <xs:maxLength value= "255"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>

      <xs:element name="contact">
        <xs:simpleType >
          <xs:restriction base="xs:string">
            <xs:minLength value= "1"/>
            <xs:maxLength value= "255"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>

      <xs:element name="reward">
        <xs:simpleType >
          <xs:restriction base="xs:string">
            <xs:minLength value= "1"/>
            <xs:maxLength value= "255"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>

      <xs:element name="location">
        <xs:simpleType >
          <xs:restriction base="xs:string">
            <xs:minLength value= "1"/>
            <xs:maxLength value= "255"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>

      <xs:element name="photo" type="xs:base64Binary"/>
      <xs:element name="statusID" type="xs:string"/>
      <xs:element name="userID" type="xs:string"/>

      <xs:element name="created_date">
        <xs:simpleType>
          <xs:restriction base="xs:dateTime"/>
        </xs:simpleType>
      </xs:element>

    </xs:sequence>

  </xs:complexType>

</xs:element>
</xs:schema>

(Added from a subsequent comment:)

This is the xml I'm working with my xsd.

<?xml version="1.0" encoding="UTF-8"?> 
<missing> 
  <missingID> m1234 </missingID> 
  <reportType> Missing Report </reportType> 
  <reportDescription> A person was missing yesterday at Manhattan around 8 P.M. </reportDescription> 
  <contact> 1234567891 </contact> 
  <reward> Cash $10000 </reward> 
  <location> New York </location> 
  <photo> </photo>  
  <statusID> s1234 </statusID> 
  <userID> a1234 </userID> 
  <created_date> 2019-11-29T09:00:15 </created_date> 
</missing>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
Ram Shay
  • 11
  • 3
  • You can [edit] your question and add your XML if you require further help, but the changes I show in my answer resolve the original problems with your XSD. – kjhughes Nov 30 '19 at 21:29

2 Answers2

0

The immediate problem causing your immediate error is that the reportType attribute declaration has to follow the xs:sequence, not be within it. See How to add attribute declaration in XSD for element with sequence of child elements?

You'll also have to move the xs:simpleType that appears to be intended to be associated with the reportType attribute to within that attributes declaration rather than following it.

Altogether, here's is your (now valid) XSD with the above repairs:

<?xml version="1.0"?>
<xs:schema attributeFormDefault="unqualified" 
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="missing">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="missingID" type="xs:string"/>
        <xs:element name="reportDescription">
          <xs:simpleType >
            <xs:restriction base="xs:string">
              <xs:minLength value= "1"/>
              <xs:maxLength value= "255"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name="contact">
          <xs:simpleType >
            <xs:restriction base="xs:string">
              <xs:minLength value= "1"/>
              <xs:maxLength value= "255"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name="reward">
          <xs:simpleType >
            <xs:restriction base="xs:string">
              <xs:minLength value= "1"/>
              <xs:maxLength value= "255"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name="location">
          <xs:simpleType >
            <xs:restriction base="xs:string">
              <xs:minLength value= "1"/>
              <xs:maxLength value= "255"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name="photo" type="xs:base64Binary"/>
        <xs:element name="statusID" type="xs:string"/>
        <xs:element name="userID" type="xs:string"/>
        <xs:element name="created_date">
          <xs:simpleType>
            <xs:restriction base="xs:dateTime"/>
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="reportType" use="required">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:minLength value= "1"/>
            <xs:maxLength value= "255"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thank you for quick response but I am still getting the error. Cvc-complex-type.4: Attribute 'reportType' Must Appear On Element 'missing'., Line '1', Column '10'. Cvc-complex-type.2.4.d: Invalid Content Was Found Starting With Element 'reportType'. No Child Element Is Expected At This Point., Line '11', Column '15'. – Ram Shay Nov 30 '19 at 21:27
  • Your XML has `reportType` as an element, but your XSD has it as an attribute. Which way do you really want it to be? And overall, are you trying to adapt your XML to your XSD or vice-versa? – kjhughes Dec 02 '19 at 00:11
0

My first thought was that you're trying to define a complex type with simple content. If that's the case then it's easy to find examples and tutorials, see for example here:

XML Schema: Element with attributes containing only text?

But when I looked again, I'm not sure I know what you are trying to do. The problem is, you haven't shown us the XML that you want to validate. It's hard to tell you what's wrong with incorrect code when you don't know what it's supposed to do.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • This is the xml I'm working with my xsd. I tried block quote and {} to indent properly but its not working properly. m1234 Missing Report A person was missing yesterday at Manhattan around 8 P.M. 1234567891 Cash $10000 New York s1234 a1234 2019-11-29T09:00:15 – Ram Shay Nov 30 '19 at 23:54
  • Rather than adding a comment, it's best to edit the question to include this information. – Michael Kay Dec 01 '19 at 07:08