Search code examples
xmlxsdxsd-validationxml-validation

S4s-elt-invalid-content.1: Element 'element' Is Invalid, Misplaced, Or Occurs Too Often


I am trying to validate my XML with the given schema but keep getting this error.

S4s-elt-invalid-content.1: The Content Of 'coreTextType' Is Invalid. Element 'element' Is Invalid, Misplaced, Or Occurs Too Often.

Here is my XML

<?xml version="1.0" encoding="UTF-8"?>
<authors>
  <coretext>
    <author id="VH">
      <name>Victor Hugo</name>
      <nationality>French</nationality>
     <rating>4.00</rating>
    </author>
    <author period="classical">
      <name>Sophocles</name>
      <nationality>Greek</nationality>
     <rating>15.00</rating>
    </author>
    <author>
      <name>Nikolai Gogol</name>
      <nationality>Russian</nationality>
      <rating>11.00</rating>
    </author>
  </coretext>
  <author>
    <name>Leo Tolstoy</name>
   <nationality>Russian</nationality>
   <rating>12.00</rating>
  </author>
  <author id ="AR">
    <name>Alexander Pushkin</name>
    <nationality>Russian</nationality>
    <rating>13.88</rating>
  </author>
  <author period="classical">
    <name>Plato</name>
    <nationality>Greek</nationality>
    <rating>20.15</rating>
  </author>
</authors> 

Here is my schema

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="authors">
        <xs:complexType>
            <xs:choice maxOccurs="unbounded">
                <xs:element name="coretext" type="coreTextType" />
                <xs:element name="author" type="authorType" />
            </xs:choice>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="coreTextType">
        <xs:element name="author" type="authorType" maxOccurs="unbounded"/>
    </xs:complexType>

    <xs:complexType name="authorType">
        <xs:sequence>
            <xs:element name="name" type="xs:string" />
            <xs:element name="nationality" type="xs:string" />
            <xs:element name="rating" type="xs:integer" />
            <xs:attribute name="id" type="xsd:string" />
            <xs:attribute name="binding" type="xsd:string" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

Can somebody please point me in the right direction.?


Solution

  • Your XML is fine, but your XSD has many errors:

    • A xs:complexType cannot have xs:element directly as a child; wrap it first in xs:sequence. (This is the error related to your current error message.)
    • A xs:attribute cannot appear within xs:sequence; move them outside but still within xs:complexType.
    • xsd: is not defined; you meant xs:.
    • The type of rating should be xs:decimal, not xs:integer
    • @binding should be @period to match your XML.

    XSD

    Here's your XSD will all errors repaired:

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
      <xs:element name="authors">
        <xs:complexType>
          <xs:choice maxOccurs="unbounded">
            <xs:element name="coretext" type="coreTextType" />
            <xs:element name="author" type="authorType" />
          </xs:choice>
        </xs:complexType>
      </xs:element>
    
      <xs:complexType name="coreTextType">
        <xs:sequence>
          <xs:element name="author" type="authorType" maxOccurs="unbounded"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:complexType name="authorType">
        <xs:sequence>
          <xs:element name="name" type="xs:string" />
          <xs:element name="nationality" type="xs:string" />
          <xs:element name="rating" type="xs:decimal" />
        </xs:sequence>
        <xs:attribute name="id" type="xs:string" />
        <xs:attribute name="period" type="xs:string" />
      </xs:complexType>
    
    </xs:schema>
    

    It will now validate your XML sucessfully.