XML-Datei gegen XSD-Schema validieren mittels Validator

blauer_hase

Grünschnabel
Hey!

Ich bekomme immer eine Fehlermeldung, wenn ich versuche meine XML-Datei zu validieren:

convert.xml is not valid because
cvc-elt.1: Cannot find the declaration of element 'input'.

input ist mein root-element in der XML-Datei und ist auch in der XSD-Datei so angegeben. Daher frage ich mich, warum er es nicht finden kann.

also meine Schema-Datei "input.xsd" sieht so aus:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  targetNamespace="http://www.example.org/input" xmlns:tns="http://www.example.org/input" elementFormDefault="qualified">

    <xsd:element name="input" type="tns:inputType"></xsd:element>

...hier stehen noch Type-Definitionen...

</xsd:schema>


Meine XML-Datei "convert.xml" sieht so aus:

Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <!-- <input xmlns="http://www.example.org/input/"> -->
<input xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="input.xsd">
	<convert todo="kml to dff">
		<inputFile>/home/sa.haas/Ausbildung_und_Studium/Seminar/Tests/Route2.kml</inputFile>
		<outputFile>/home/sa.haas/Ausbildung_und_Studium/Seminar/Tests/Route2.dff</outputFile>
	</convert>
</input>

den auskommentierten Teil in der 2. Zeile hatte ich mal als Alternative zu dem root-element getestet, hat aber auch nichts gebracht...

und meine Java-Klasse, die validieren soll (wobei file der XML-Datei convert.xml entspricht):

Code:
// 1. Lookup a factory for the W3C XML Schema language
		SchemaFactory factory = 
			SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

		// 2. Compile the schema. 
		// Here the schema is loaded from a java.io.File, but you could use 
		// a java.net.URL or a javax.xml.transform.Source instead.
		File schemaLocation = new File("input.xsd");
		Schema schema = null;
		try {
			schema = factory.newSchema(schemaLocation);
		} catch (SAXException e1) {
			System.out.println(e1.getMessage());
		}

		// 3. Get a validator from the schema.
		Validator validator = schema.newValidator();

		// 4. Parse the document you want to check.
		Source source = new StreamSource(file);

		// 5. Check the document
		try {
			validator.validate(source);
			System.out.println(file + " is valid.");
		}catch (SAXException ex) {
			System.out.println(file + " is not valid because ");
			System.out.println(ex.getMessage());
			System.exit(1);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}


Ich habe das ganze dann auch nochmal auf einem anderen Weg versucht:

Code:
// create a factory that understands namespaces and validates the XML input
		      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		      factory.setNamespaceAware(true);
		   
		      // read the XML file
		      DocumentBuilder builder = null;
			try {
				builder = factory.newDocumentBuilder();
			} catch (ParserConfigurationException e3) {
				e3.printStackTrace();
			}
		      Document doc = null;
			try {
				doc = builder.parse(new File("convert.xml"));
			} catch (SAXException e2) {
				e2.printStackTrace();
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		   
		      // create a SchemaFactory and a Schema
		      SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);   
		      Source schemaFile = new StreamSource(new File("input.xsd")); 
		      Schema schema = null;
			try {
				schema = schemaFactory.newSchema(schemaFile);
			} catch (SAXException e1) {
				e1.printStackTrace();
			}
		  
		      // create a Validator object and validate the XML file
		      Validator validator = schema.newValidator();
		      try {
				validator.validate(new DOMSource(doc));
				System.out.println(file + " is valid");
			} catch (SAXException e) {
				System.out.println(file + " is not valid because: ");
				System.out.println(e.getMessage());
			} catch (IOException e) {
				e.printStackTrace();
			}

...ging aber natürlich auch nicht -> gleicher Fehler...


Hat jemand eine Idee, wo hier der Fehler liegen könnte? Ich hab schon von vielen ähnlichen Problemen und auch Lösungen gelesen und auch schon viel mit den Namespaces herumprobiert, aber bei mir möchte einfach nichts funktionieren... :(

Wäre echt super, wenn mir jemand helfen könnte!

Danke schonmal, Sarah
 
Zuletzt bearbeitet:
wow, ich glaub ich habs gelöst... hab so lange rumprobiert bis jetzt plötzlich die XML-Datei valid war...
Also für alle anderen, die das Problem auch haben:

Ich habe den Namespace xsd: aus dem Schema herausgenommen und das root-element umbenannt, da es genauso hieß wie auch das Schema (keine Ahnung, ob das was mit dem Fehler zu tun hat, jetzt geht es jedenfalls, dann lass ich jetzt lieber die Finger von weiteren Spielereien):
Code:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/input/" xmlns:tns="http://www.example.org/input/" elementFormDefault="qualified">


    <element name="root" type="tns:rootType"></element>
    
...Type-Definitionen...

</schema>

und auch in der XML-Datei habe ich die auskommentierte Alternative für das root-element benutzt und natürlich auch hier den Namen des root-element entsprechend des Schemas geändert:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root xmlns="http://www.example.org/input/">
	<convert todo="kml to dff">
		<inputFile>/home/sa.haas/Ausbildung_und_Studium/Seminar/Tests/Route2.kml</inputFile>
		<outputFile>/home/sa.haas/Ausbildung_und_Studium/Seminar/Tests/Route2.dff</outputFile>
	</convert>
</root>

zusammen mit diesem Java-Code funktionierte es dann:
Code:
// 1. Lookup a factory for the W3C XML Schema language
		SchemaFactory factory = 
			SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

		// 2. Compile the schema. 
		// Here the schema is loaded from a java.io.File, but you could use 
		// a java.net.URL or a javax.xml.transform.Source instead.
		File schemaLocation = new File("input.xsd");
		Schema schema = null;
		try {
			schema = factory.newSchema(schemaLocation);
		} catch (SAXException e1) {
			System.out.println(e1.getMessage());
		}

		// 3. Get a validator from the schema.
		Validator validator = schema.newValidator();

		// 4. Parse the document you want to check.
		Source source = new StreamSource(file);

		// 5. Check the document
		try {
			validator.validate(source);
			System.out.println(file + " is valid.");
		}catch (SAXException ex) {
			System.out.println(file + " is not valid because ");
			System.out.println(ex.getMessage());
			System.exit(1);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}

Also ingesamt hab ich geändert: den Namen des root-elements, den Namespace xsd: rausgenommen und dieses root-element benutzt: <root xmlns="http://www.example.org/input/">
 
Wenn du die Lösung hast, dann setzt das Thema auf erledigt ;)

Aber hier noch nee Erklärung:

Erstmal ist für den Parser die XML nur eine simple text datei und wird so gelesen. Anhand von Keywords und regex werden Elemente und co erkannt und vom Parser Verarbeitet, du hast nun ein Keyword verwendet welches intern schon verwendet wird und dein Validator hat sich daran verschluckt ;)

MFG
 
Zurück