Js30
Js30

Reputation: 39

XSD Validation of XML: Cannot find the declaration of element

I've been messing around with XML validation via xsd but I'm still newbie. I try validate this xml and it pops me this error: cvc-elt.1: Cannot find the declaration of element 'customers'. [5]

<?xml version="1.0"?>

<customers xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://w3schools.com requested_customer.xsd">

 <customer name="Vladimir Putin" address="St. Petersburg, wadim street 23, Russia"/>
</customers>

and the XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xs:element name="customers">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="customer">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="name"/>
                <xs:attribute type="xs:string" name="address"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Thanks for your help!

Upvotes: 2

Views: 761

Answers (2)

DavidRR
DavidRR

Reputation: 19457

Also, if your intent is to allow more than one customer within customers, you'll want to change:

  <xs:element name="customer">

To:

  <xs:element name="customer" maxOccurs="unbounded">

Of course, maxOccurs can also be a finite value such as 100. maxOccurs (and minOccurs) both default to 1 (See). minOccurs can also be 0.

Upvotes: 1

Petru Gardea
Petru Gardea

Reputation: 21658

First thing first, your XML doesn't uses XML namespaces for its content, so to reference an XSD's file location without a target namespace you should use the xsi:noNamespaceSchemaLocation attribute instead.

Secondly, you have to make sure that the location of the XSD file is known to, and accessible by, the validating program.

Your XML/XSD combo is perfectly valid.

Upvotes: 4

Related Questions