title: Java: SAX Schema validation
body: <p>I've following two schema's. Master.xsd and Child.xsd</p>
  
  <ol>
  <li>Child.xsd is imported by the Master.xsd. </li>
  <li>Master file has target namespace 'pub'.</li>
  <li>Child file no such namespace.</li>
  </ol>
  
  <p>When I try to validate xml with Master.xsd, I get error</p>
  
  <blockquote>
    <p>org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'author' to a(n) 'element declaration' component.</p>
  </blockquote>
  
  <p>I've also tried  using  in master.xsd, this time i get similar error:</p>
  
  <blockquote>
    <p>org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'pub:author' to a(n) 'element declaration' component.</p>
  </blockquote>
  
  <p>Though this get validated by XMLSpy successfully.</p>
  
  <p>Here are the schema's, calling code and the validation code:</p>
  
  <p>Master.xsd</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:pub="http://schema.abc.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schema.abc.com"&gt;
      &lt;xs:import schemaLocation="Child.xsd"/&gt;
      &lt;xs:element name="books"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:choice minOccurs="0" maxOccurs="unbounded"&gt;
                  &lt;xs:element ref="pub:book"/&gt;
              &lt;/xs:choice&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      &lt;xs:element name="book"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element ref="pub:published_date"/&gt;
                  &lt;xs:element ref="author"/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      &lt;xs:element name="published_date" type="xs:dateTime"/&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>Child.xsd</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;xsd:element name="author"&gt;
          &lt;xsd:complexType&gt;
              &lt;xsd:sequence&gt;
                  &lt;xsd:element ref="first_name"/&gt;
              &lt;/xsd:sequence&gt;
          &lt;/xsd:complexType&gt;
      &lt;/xsd:element&gt;
      &lt;xsd:element name="first_name" type="xsd:string"/&gt;
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p>Sample.xml that needs to be validated:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;pub:books xsi:schemaLocation="http://schema.abc.com" xmlns:pub="http://schema.abc.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;pub:book&gt;
          &lt;pub:published_date&gt;2001-12-17T09:30:47Z&lt;/pub:published_date&gt;
          &lt;author&gt;
              &lt;first_name&gt;Adi&lt;/first_name&gt;
          &lt;/author&gt;
      &lt;/pub:book&gt;
  &lt;/pub:books&gt;
  </code></pre>
  
  <p>Java code for validation:</p>
  
  <pre><code>private void validate(final String schema, final String xml) {
          SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
          InputStream is = getClass().getResourceAsStream(schema);
          Schema schema;
          try {
              schema = schemaFactory.newSchema(new StreamSource(is));
              Validator validator = schema.newValidator();
              Source xmlSource = new StreamSource( new StringReader(xml));
  
              validator.validate(xmlSource);
  
          } catch (IOException e) {
              e.printStackTrace();
          } catch (SAXException e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>calling code:</p>
  
  <pre><code>validate(masterXSDPath, "xmlString");
  </code></pre>
  
  <p>Please tell where am I going wrong??</p>