d_mun
d_mun

Reputation: 5

getting error trying to validate XML to schema

I get the error below when validating the XML file below to the schema below.

Error: Element '{http://www.w3.org/2001/XMLSchema}sequence': The content is not valid. Expected is (annotation?, (element | group | choice | sequence | any)*). on line 7

XML file:

<?xml version="1.0"?>
<!DOCTYPE Employees>
<Employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="lab4_obj1.xsd">
  <Employee>
    <First>John</First>
    <Last>Smith</Last>
    <Phone>1-800-123-4567</Phone>
  </Employee>
</Employees>

schema:

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

<xs:element name="Employees">
  <xs:complexType>
     <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="Employee"/>
           <xs:complexType>
              <xs:sequence>
                 <xs:element name="First"/>
                 <xs:element name="Last"/>
                 <xs:element name="Phone"/>
              </xs:sequence>
           </xs:complexType>
        </xs:sequence>      
     </xs:complexType>
  </xs:element>

</xs:schema>

Upvotes: 0

Views: 2814

Answers (1)

Dijkgraaf
Dijkgraaf

Reputation: 11527

Well for starters you are self ending the Employee element before you define the complex type (because you have a / after "Employee")

You should be ending it after the ComplexType (see below).

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://Scratch.Employees" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://Scratch.Employees" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Employees">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="Employee">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="First" type="xs:string" />
              <xs:element name="Last" type="xs:string" />
              <xs:element name="Phone" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Secondly, the XML sample seems to be referring to a DTD, but your schema is a XSD. So you will want to remove that, and probably have the correct name space referenced instead.

<?xml version="1.0"?>
<ns0:Employees xmlns:ns0="http://Scratch.Employees">
  <Employee>
    <First>John</First>
    <Last>Smith</Last>
    <Phone>1-800-123-4567</Phone>
  </Employee>
</ns0:Employees>

Upvotes: 1

Related Questions