Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to validate xml against xsd

I am new to learning XSD. I wrote a XSD file and and XML file and a program to validate the XML against XSD. When I run the program I am getting the error stating Invalid content was found starting with element 'id'. One of '{id}' is expected. Can someone please explain where I am going wrong.

XSD file:

<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://www.edureka.co/Student" 
                xmlns:st="http://www.edureka.co/Student"
                >
        <xs:element name="student"  type="st:student"></xs:element>
            <xs:complexType name="student">
                <xs:sequence>
                    <xs:element name="id" type="xs:int"/>
                    <xs:element name="name" type="xs:string"/>
                    <xs:element name="language" type="xs:string"/>
                    <xs:element name="expertise" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:schema>

And the XML file:

<?xml version="1.0" encoding="UTF-8"?>
    <student xmlns="http://www.edureka.co/Student"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.edureka.co/Student studentRule.xsd"
             >

       <id>1234</id>
       <name>Pradeep</name>
       <language>Sanskrit</language>
       <expertise>Beginner</expertise>
    </student>

The Java program to validate the XML file:

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

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class ValidatorDemo {

    public static void main(String[] args) {

      System.out.println("student.xml validates against studentRule.xsd? "+validateXMLSchema("studentRule.xsd", "student.xml"));

      }

    public static boolean validateXMLSchema(String xsdPath, String xmlPath){

        try {
            SchemaFactory factory = 
                    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new File(xsdPath));
            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new File(xmlPath)));
        } catch (IOException | SAXException e) {
            System.out.println("Exception: "+e.getMessage());
            return false;
        }
        return true;
    }
}
like image 1000
zilcuanu Avatar asked Dec 04 '25 16:12

zilcuanu


1 Answers

Add the attribute elementFormDefault="qualified" to the root element of your schema.

The default is "unqualified", which can't be used when specifying a default namespace.

Alternatively, you could leave your schema as is, and update your document to use the unqualified format (no default namespace, and only qualify the global elements):

<st:student xmlns:st="http://www.edureka.co/Student"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.edureka.co/Student studentRule.xsd"
        >

    <id>1234</id>
    <name>Pradeep</name>
    <language>Sanskrit</language>
    <expertise>Beginner</expertise>
</st:student>

The xml document you posted is in "qualified" format; every element is explicitly associated with a namespace, which you did by specifying a default - xmlns="http://www.edureka.co/Student".

The xml above is in "unqualified" format; the global elements (i.e. the top level elements in the schema) have to be qualified, but the local elements (i.e. the nested elements in the schema) must be unqualified. In "unqualified" format the local elements are implicitly associated with the namespace of the enclosing global element.

A default namespace can't be used in documents that use "unqualified" because it explicitly associates elements without a namespace prefix with a namespace. Think of it as associating "the empty prefix" with a namespace.

In general, the "qualified" format is the easiest one to use.

like image 112
teppic Avatar answered Dec 06 '25 04:12

teppic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!