title: Multiple schema validation in Java
body: <p>I am trying to do multiple schema validation in Java. I don't understand where I am doing wrong. Any help will be appreciated. </p>
  
  <p><strong>abc.xsd</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xn="project-xml-r4j_another.xsd"&gt;
   &lt;xsd:import namespace="project-xml-r4j_another.xsd"/&gt;
   &lt;xsd:element name="abc" type="abc"&gt;
   &lt;/xsd:element&gt;
   &lt;xsd:complexType name="abc"&gt;
    &lt;xsd:sequence&gt;
     &lt;xsd:element name="test" type="test" minOccurs="0" maxOccurs="1"&gt;
     &lt;/xsd:element&gt;
     &lt;!--&lt;xsd:element name="proj" type="xn:proj"/&gt;--&gt;
    &lt;/xsd:sequence&gt;
    &lt;xsd:attribute name="id" type="xsd:ID" use="required"/&gt;
   &lt;/xsd:complexType&gt;
   &lt;xsd:complexType name="test"&gt;
    &lt;xsd:attribute name="id" type="xsd:ID" use="required"&gt;&lt;/xsd:attribute&gt;
    &lt;xsd:attribute name="value" use="required"&gt;
     &lt;xsd:simpleType&gt;
      &lt;xsd:restriction base="xsd:string"&gt;
       &lt;xsd:maxLength value="100" /&gt;
      &lt;/xsd:restriction&gt;
     &lt;/xsd:simpleType&gt;
    &lt;/xsd:attribute&gt;
   &lt;/xsd:complexType&gt;
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p><strong>project-xml-r4j_another.xsd</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="project-xml-r4j_another.xsd" xmlns="project-xml-r4j_another.xsd" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;
   &lt;xsd:element name="proj" type="proj"&gt;
    &lt;xsd:annotation&gt;
     &lt;xsd:documentation&gt;
      The project is the root tag of a project-xml.
     &lt;/xsd:documentation&gt;
    &lt;/xsd:annotation&gt;
   &lt;/xsd:element&gt;
   &lt;xsd:complexType name="proj"&gt;
    &lt;xsd:attribute name="id" type="xsd:ID" use="required"/&gt;
   &lt;/xsd:complexType&gt;
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p><strong>Test case</strong></p>
  
  <pre><code>package test;
  
  import java.io.File;
  import java.io.IOException;
  
  import javax.xml.XMLConstants;
  import javax.xml.transform.Source;
  import javax.xml.transform.stream.StreamSource;
  import javax.xml.validation.Schema;
  import javax.xml.validation.SchemaFactory;
  import javax.xml.validation.Validator;
  
  import org.apache.log4j.Logger;
  import org.junit.Test;
  import org.xml.sax.SAXException;
  import org.xml.sax.SAXParseException;
  import org.xml.sax.helpers.DefaultHandler;
  
  import com.ericsson.ccrtool.core.project.projectxml.InvalidProjectXmlException;
  
  public class TestSchema {
   private static final Logger logger = Logger.getLogger(TestSchema.class);
   static final String W3C_XML_SCHEMA = XMLConstants.W3C_XML_SCHEMA_NS_URI;
   @Test
   public void test() {
    System.out.println("TestSchema.test()");
    try {
  
     SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA);
     // create a grammar object.
     Source [] source = { new StreamSource(new File("C:\\jaydeep\\Ericsson\\R5B\\abc.xsd")),
       new StreamSource(new File("C:\\jaydeep\\Ericsson\\R5B\\project-xml-r4j.xsd"))};
     Schema schemaGrammar = schemaFactory.newSchema(source);
  
     Validator schemaValidator = schemaGrammar.newValidator();
     schemaValidator.setErrorHandler(new MessageHandler());
  
     // validate xml instance against the grammar.
     schemaValidator.validate(new StreamSource("C:\\jaydeep\\Ericsson\\R5B\\project_tmmk17cells_xnaveen_project-xml.xml"));
  
    } catch (SAXException e) {
     throw new InvalidProjectXmlException("Project-xml validation failed, Exception: " + e.getMessage(), e);
    } catch (IOException e) {
     throw new InvalidProjectXmlException("Project-xml validation failed, Exception: " + e.getMessage(), e);
    }
   }
   class MessageHandler extends DefaultHandler {
    private String errMessage = "";
  
    @Override
    public void warning(SAXParseException e) {
     logger.info("Warning Line " + e.getLineNumber() + ": " + e.getMessage());
    }
  
    @Override
    public void error(SAXParseException e) {
     errMessage = new String("Error Line " + e.getLineNumber() + ": " + e.getMessage());
     logger.info(errMessage);
     throw new InvalidProjectXmlException("Project-xml validation failed, Exception: " + errMessage);
    }
  
    @Override
    public void fatalError(SAXParseException e) {
     errMessage = new String("Error Line " + e.getLineNumber() + ": " + e.getMessage());
     logger.info(errMessage);
     throw new InvalidProjectXmlException("Project-xml validation failed, Exception: " + errMessage);
    }
   }
  }
  </code></pre>
  
  <h2>Edit</h2>
  
  <p>If I run above test case I am getting below error <code>com.ericsson.ccrtool.core.project.projectxml.InvalidProjectXmlException: Project-xml validation failed, Exception: Error Line 2: cvc-elt.1: Cannot find the declaration of element 'abc'.</code></p>