sixfeet sixfeet - 1 month ago 8
Java Question

XSD validation error: Cannot find the declaration of element 'xs:schema'

I saw that this question was asked many times but I didn't find a solution to my problem. So the error is: Error on line 2 of document file...doc.xsd: cvc-elt.1: Cannot find the declaration of element 'xs:schema'

The code in the XSD file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Collection" >
<xs:complexType>
<xs:sequence>
<xs:element name="Description" type="xs:string"/>
<xs:element name="Recipe" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Ingredients">
<xs:complexType>
<xs:element name="Ingredient" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="amount" type="xs:integer" use="required"/>
<xs:attribute name="unit" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
<xs:element name="Preparation">
<xs:complexType>
<xs:element name="Step" type="xs:string minOccurs="0" maxOccurs="unbounded"/>"
</xs:complexType>
</xs:element>
<xs:element name="Comment" type="xs:string" minOccurs="0"/>
<xs:element name="Nutrients">
<xs:complexType>
<xs:attribute name="proteins" type="xs:integer" use="required"/>
<xs:attribute name="carbohidrati" type="xs:NMTOKEN" use="required"/>
<xs:attribute name="fat" type="xs:integer" use="choice"/>
<xs:attribute name="vitamins" type="xs:NMTOKEN" use="required"/>
<xs:attribute name="calories" type="xs:float" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


The code in the XML file:

<?xml version="1.0" encoding="UTF-8"?>

<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="src/doc.xsd">
<Description>Nu prea merge</Description>
<Recipe>
<Title>Paste</Title>
<Ingredients>
<Ingredient name="paste" amount="1" unit="pachet"></Ingredient>
</Ingredients>
<Preparation>
<Step>Fierbere</Step>
<Step>Servire</Step>
</Preparation>
<Nutrients proteins="2" carbohidrati="2" fat="da" vitamins="4" calories="1000.9"></Nutrients>
</Recipe>
</Collection>


The code in my Java class:

import java.io.File;
import java.io.IOException;

import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaders;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;


public class Main {
public static void main(String[] args) {
File newFile = new File("src/doc.xsd");
Document doc = null;

SAXBuilder sbd = new SAXBuilder(XMLReaders.XSDVALIDATING);

try {

doc = sbd.build(newFile);
} catch (JDOMException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

XMLOutputter outputDoc = new XMLOutputter();
outputDoc.setFormat(Format.getPrettyFormat());
try{
outputDoc.output(doc,System.out);
//outputDoc.output(doc, new FileWriter("src/myXmlDoc2.xml"));
}
catch(Exception e){
System.out.println("Eroare la parsarea documentului XML!");
}
}

}


If anyone has any idea about the possible problem please help me!

Answer

Assuming that your XML is fixed, then you should change your XSD as follows:

  1. Fix the extra " as mentioned by @Andreas.
  2. Add xs:sequence under xs:complexType in two places.
  3. Change the type of @fat to xs:string.
  4. Change use="choice" to use="optional".

Altogether, this XSD will successfully validate your XML:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Collection" >
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Description" type="xs:string"/>
        <xs:element name="Recipe" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Title" type="xs:string"/>
              <xs:element name="Ingredients">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Ingredient" minOccurs="0" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:attribute name="name" type="xs:string" use="required"/>
                        <xs:attribute name="amount" type="xs:integer" use="required"/>
                        <xs:attribute name="unit" type="xs:string" use="required"/>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="Preparation">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Step" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="Comment" type="xs:string" minOccurs="0"/>
              <xs:element name="Nutrients">
                <xs:complexType>
                  <xs:attribute name="proteins" type="xs:integer" use="required"/>
                  <xs:attribute name="carbohidrati" type="xs:NMTOKEN" use="required"/>
                  <xs:attribute name="fat" type="xs:string" use="optional"/>
                  <xs:attribute name="vitamins" type="xs:NMTOKEN" use="required"/>
                  <xs:attribute name="calories" type="xs:float" use="required"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Of course, you will also have to check your Java code. (Hint: make sure you're not validating your XSD as XML when you really want to validating your XML against your XSD.)