I am trying to validate a SOAP request XML against XSD file using Java code. To start with, I tried to validate an empty SOAP message against the XSD.
I followed the answers here XSD validation error: Cannot Find The Declaration Of Element 'soapenv:Envelope' and Cvc-elt.1: Cannot Find The Declaration Of Element 'soap:Envelope'
As suggested on the second link, I imported http://schemas.xmlsoap.org/soap/envelope/
to the schema. But I still get the exception org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 78; cvc-elt.1: Cannot find the declaration of element 'soapenv:Envelope'
in the java code below.
The request XML is, request.xml
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Body/>
</soapenv:Envelope>
the XSD is, test.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xs:import namespace="http://schemas.xmlsoap.org/soap/envelope/"
schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"/>
</xs:schema>
The Java code i am using to validate is
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
FileInputStream fileInputStream = new FileInputStream(new File("test.xsd"));
Schema schema = factory.newSchema(new StreamSource(fileInputStream));
javax.xml.validation.Validator val = schema.newValidator();
FileInputStream fileInputStream2 = new FileInputStream(new File("request.xml"));
val.validate(new StreamSource(fileInputStream2));
}catch (Exception e) {
System.out.println("Exception: "+e.getMessage());
}
The validation in the java code fails and throws a SAXParseException.The XML validation works when i try it at https://www.freeformatter.com/xml-validator-xsd.html
I can't find what is missing, appreciate any help on this.