1

I have an xml and an xsd. Both in the same directory. I am trying to validate it using xmlcopyeditor but it returns the following error.

Error at line 2, column 63: No declaration found for element "x:books"

Here is the xml.

file:books.xml

<?xml version="1.0"?>
<x:books xmlns:x="urn:books" x:noNamespaceSchemaLocation="books.xsd">
   <book id="bk001">
      <author>Writer</author>
      <title>The First Book</title>
      <genre>Fiction</genre>
      <price>44.95</price>
      <pub_date>2000-10-01</pub_date>
      <review>An amazing story of nothing.</review>
   </book>

   <book id="bk002">
      <author>Poet</author>
      <title>The Poet's First Poem</title>
      <genre>Poem</genre>
      <price>24.95</price>
      <review>Least poetic poems.</review>
   </book>
</x:books>

here is the xsd

file:books.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:books"
            xmlns:bks="urn:books">

  <xsd:element name="books" type="bks:BooksForm"/>

  <xsd:complexType name="BooksForm">
    <xsd:sequence>
      <xsd:element name="book" 
                  type="bks:BookForm" 
                  minOccurs="0" 
                  maxOccurs="unbounded"/>
      </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="BookForm">
    <xsd:sequence>
      <xsd:element name="author"   type="xsd:string"/>
      <xsd:element name="title"    type="xsd:string"/>
      <xsd:element name="genre"    type="xsd:string"/>
      <xsd:element name="price"    type="xsd:float" />
      <xsd:element name="pub_date" type="xsd:date" />
      <xsd:element name="review"   type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id"   type="xsd:string"/>
  </xsd:complexType>
</xsd:schema>

I found some similar question like No declaration found for element and No declaration found for element ' ' but I have not been able to fix it. I don't understand what's wrong I should be referencing the xsd correctly like that.

kjhughes
  • 106,133
  • 27
  • 181
  • 240

1 Answers1

0

There are multiple issues in your XML and XSD, including:

  1. Use schemaLocation, not noNamespaceSchemaLocation, for namespaced XML.
    See How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?

  2. Use xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance".
    See xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?

  3. Use elementFormDefault in your XSD.
    See What does elementFormDefault do in XSD?

The XML and XSD below have all issues resolved. The fixed XML is now valid against the fixed XSD.

XML

<?xml version="1.0"?>
<books xmlns="urn:books"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="urn:books books.xsd">
   <book id="bk001">
      <author>Writer</author>
      <title>The First Book</title>
      <genre>Fiction</genre>
      <price>44.95</price>
      <pub_date>2000-10-01</pub_date>
      <review>An amazing story of nothing.</review>
   </book>
   <book id="bk002">
      <author>Poet</author>
      <title>The Poet's First Poem</title>
      <genre>Poem</genre>
      <price>24.95</price>
      <review>Least poetic poems.</review>
   </book>
</books>

XSD

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="urn:books"
            xmlns:bks="urn:books"
            elementFormDefault="qualified">
  <xsd:element name="books" type="bks:BooksForm"/>
  <xsd:complexType name="BooksForm">
    <xsd:sequence>
      <xsd:element name="book" 
                   type="bks:BookForm" 
                   minOccurs="0" 
                   maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="BookForm">
    <xsd:sequence>
      <xsd:element name="author"   type="xsd:string"/>
      <xsd:element name="title"    type="xsd:string"/>
      <xsd:element name="genre"    type="xsd:string"/>
      <xsd:element name="price"    type="xsd:float" />
      <xsd:element name="pub_date" type="xsd:date" />
      <xsd:element name="review"   type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id"   type="xsd:string"/>
  </xsd:complexType>
</xsd:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240