I'm experiencing different behavior in XML validation if the XSD is loaded as a File or resource.
If i load the XSD as File everything is fine and validation works well:
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new ClassPathResource("my/perfect/path/myFile.xsd").getFile());
Validator validator = schema.newValidator();
validator.validate(sourceToValidate);
Instead since i decided to include xsd file into jar i load it as resource, but the behavior is different and i get into a SAXParseException when i construct the Schema and error is complaining about some issue in resolving a name in xsd file as a type definition. The xsd file is absolutely correct
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("my/perfect/path/myFile.xsd");
Source schemaSource = new StreamSource(inputStream);
Schema schema = sf.newSchema(schemaSource); // here i get the SAXParseException
Validator validator = schema.newValidator();
validator.validate(sourceToValidate);
I really can't understand why load the xsd as ResourceAsStream causes a different behavior
I'm using JAXB for xml validation
I'm guessing that your xsd references other xsd's and your validator does not know how to resolve the references.
You may need to provide a LSResourceResolver to the
ValidatorYou will also want to set the
systemIdon theInputSourceby calling the two args constructor ofStreamSource.See StreamSource(InputStream, String)