1

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.

Ben
  • 137
  • 3
  • 12
  • 1
    The issue might be the import. Try two things. First, download the SOAP schema and use it instead of test.xsd in your code, to see if it validates without error. Second, download the SOAP schema and use the factory method that takes an array of sources to set both schemas as separate files instead of one file with an import in it. If the first approach validates then you can try the second for a solution, or place the content of the SOAP schema directly inside test.xsd, without import. – Bogdan Aug 06 '21 at 08:22
  • Thanks, It worked after downloading the SOAP schema(I created `soapSchema.xsd` file from it) and importing it into my `test.xsd` as ` ` – Ben Aug 09 '21 at 18:07
  • Just be careful about that `C:/Users/...` hardcoding. It might cause problems when deploying your application some place else – Bogdan Aug 12 '21 at 09:55
  • Thanks for the reminder. I found out validating the entire SOAP request message is slow. I also have to import another xsd file(in addition to `soapSchema.xsd`) to `test.xsd` for the namespaces used in the request header. I changed my approach and extracted the SOAP body programmatically and validate it against `test.xsd` , which in this case doesn't require any imports and is much faster. – Ben Aug 16 '21 at 22:01

0 Answers0