I am using Java code to validate an xml against Schemas(XSDs.).But there are multiple Schemas included in one other using .
I have to load the schemas from the classpath. I load the main Schema as the code illustrates. If the other schemas included are included with their absolute path, the code works fine. But if the statements are as is, the xml is not validated.
public void validateXml(File xmlFileToBeValidated) throws SAXException, IOException, SchemaValidationException{
InputStream in = this.getClass().getClassLoader().getResourceAsStream("Person.xsd");
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new StreamSource(in));
Validator validator = schema.newValidator();
try {
validator.validate(new StreamSource(xmlFileToBeValidated));
System.out.println("XML is valid");
} catch (SAXException e) {
throw new SchemaValidationException("XML doesnot conform to the Schema");
}
}
The Schemas are as follows : Main Schema :
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:include schemaLocation="D:\EarlyData\IBEarlyDataFeed_Map\modules\func\edf\common\src\main\resources\Common.xsd"/>
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Name"/>
<xs:element name="Address" type="AddressType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The other included schema ::
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:complexType name="AddressType">
<xs:sequence>
<xs:element name="House" type="xs:string"/>
<xs:element name="Street" type="xs:string"/>
<xs:element name="City" type="xs:string"/>
<xs:element name="County" type="xs:string"/>
<xs:element name="PostCode" type="xs:string"/>
<xs:element name="Country" type="CountryCodes"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="CountryCodes">
<xs:restriction base="xs:string">
<xs:enumeration value="UK"/>
<xs:enumeration value="US"/>
<xs:enumeration value="France"/>
<xs:enumeration value="Germany"/>
</xs:restriction>
</xs:simpleType>
I wanted the other schema to be included as this xs:include schemaLocation="Common.xsd"/> And the xml to be validated.. Thanks in Advance for anyone's help...