Martin Dimitrov
Martin Dimitrov

Reputation: 4956

XML validation against XSD: cvc-elt.1: Cannot find the declaration of element 'xxx'

I have the following XML file:

<?xml version="1.0"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library
xmlns="http://example.com/a"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com library.xsd"
name=".NET Developer's Library">
<book>
<category>computerss</category>
<title>Programming Microsoft .NET</title>
<author>Jeff Prosise</author>
<isbn>0-7356-1376-1</isbn>
</book>
<book>
<category>computer</category>
<title>Microsoft .NET for Programmers</title>
<author>Fergal Grimes</author>
<isbn>1-930110-19-7</isbn>
</book>
</library>

And the following Java code:

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
docBuilderFactory.setSchema(sf.newSchema(new File("library.xsd")));
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.parse(new FileInputStream("data.xml"));

It produces the following error:

[Error] :7:33: cvc-elt.1: Cannot find the declaration of element 'library'.

If I remove the XSD declaration in the XML file everything works fine...

Any inside highly appreciated. Thanks.

And here is the schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="library">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="book" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use="optional"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="book">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="category"/>
                <xs:element ref="title"/>
                <xs:element ref="author"/>
                <xs:element ref="isbn"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element  name="category">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="computer" />
            <xs:enumeration value="poetry" />
        </xs:restriction>
    </xs:simpleType>
    </xs:element>
    <xs:element name="title" type="xs:string"/>
    <xs:element name="author" type="xs:string"/>
    <xs:element name="isbn" type="xs:string"/>
</xs:schema>

Upvotes: 0

Views: 1681

Answers (2)

maximdim
maximdim

Reputation: 8169

Your XML has a reference to namespace (xmlns="http://example.com/a") which is not the same as in your schema. Have you tried to validate your XML against schema in any XML editor (e.g. Altova or Eclipse etc).

So far it looks like your parsing error is legit, XML is not valid according to the schema.

Upvotes: 2

tom redfern
tom redfern

Reputation: 31760

Your schema definition is incorrect. For a start, as maximdim says, your schema has no targetNamespace="http://mysite.com/a" attribute in the schema tag.

Secondly your schema looks as if it should only have a single root element, yours has 6.

A correct schema for your XML instance would be:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://mysite.com/a" targetNamespace="http://mysite.com/a">

  <xs:element name="library">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" type="book" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="book">
    <xs:sequence>
      <xs:element name="category">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="computer" />
            <xs:enumeration value="poetry" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="author" type="xs:string"/>
      <xs:element name="isbn" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

Upvotes: 0

Related Questions