title: Java: string xml validation against xsd that include another xsd
body: <p>Update 1: New Validation method throws new error, see below.</p>
  
  <p>Update 2: i have checked the xml files with xml spy. No errors there.</p>
  
  <p>Hope somebody see where iam wrong.</p>
  
  <p>i'm currently trying to validate xml file against an xsd which includes another xsd. I don' know if my xsds or the java code is faulty. I dont have the xml/xsd files stored, i get them as a base64 string from the server. Hope somebody can help.</p>
  
  <p>I get the fllowing error:</p>
  
  <pre><code>org.xml.sax.SAXParseException; lineNumber: 81; columnNumber: 104; src-resolve: Cannot resolve the name 'ServiceSpecificationSchema:ServiceIdentifier' to a(n) 'type definition' component.
  </code></pre>
  
  <p>-ServiceSpecificationSchema.xsd</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ServiceSpecificationSchema="http://example.org/ServiceSpecificationSchema.xsd" targetNamespace="http://example.org/ServiceSpecificationSchema.xsd" version="1.0" xml:lang="EN"&gt;
  
      &lt;include schemaLocation="ServiceBaseTypesSchema.xsd"/&gt;
      &lt;element name="serviceSpecification" type="ServiceSpecificationSchema:ServiceSpecification"&gt;
          &lt;unique name="serviceDataModelTypeKey"&gt;
              &lt;selector xpath=".//xs:*"/&gt;
              &lt;field xpath="@name"/&gt;
          &lt;/unique&gt;
          &lt;keyref name="serviceDataModelReturnValueTypeKeyRef" refer="ServiceSpecificationSchema:serviceDataModelTypeKey"&gt;
              &lt;selector xpath=".//ServiceSpecificationSchema:returnValueType"/&gt;
              &lt;field xpath="ServiceSpecificationSchema:typeReference"/&gt;
          &lt;/keyref&gt;
          &lt;keyref name="serviceDataModelParameterTypeTypeKeyRef" refer="ServiceSpecificationSchema:serviceDataModelTypeKey"&gt;
              &lt;selector xpath=".//ServiceSpecificationSchema:parameterType"/&gt;
              &lt;field xpath="ServiceSpecificationSchema:typeReference"/&gt;
          &lt;/keyref&gt;
      &lt;/element&gt;
      &lt;complexType name="ServiceSpecification"&gt;
          &lt;all&gt;
              &lt;element name="id" type="ServiceSpecificationSchema:ServiceIdentifier" minOccurs="1" maxOccurs="1"/&gt;
  .....
  </code></pre>
  
  <p>ServiceBaseTypeSchema:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ServiceSpecificationSchema="http://example.org/ServiceSpecificationSchema.xsd" targetNamespace="http://example/ServiceSpecificationSchema.xsd" version="1.0" xml:lang="EN"&gt;
  ...
      &lt;simpleType name="ServiceIdentifier"&gt;
          &lt;restriction base="string"/&gt;
      &lt;/simpleType&gt;
  ...
  &lt;/schema&gt;
  </code></pre>
  
  <p>Java Validator</p>
  
  <pre><code>public void validate(String inputXml, String XSD, String XSD2)
          throws SAXException, IOException {
          try {
              byte[] decoded = Base64.decodeBase64(XSD);
              XSD = new String(decoded, "UTF-8");
              byte[] decoded2 = Base64.decodeBase64(XSD2);
              XSD2 = new String(decoded2, "UTF-8");
  
          SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
  
          Schema schema = factory.newSchema(new SAXSource[]
          {
          (new SAXSource(new InputSource(new StringReader(XSD)))),
          (new SAXSource(new InputSource(new StringReader(XSD2))))
          });
  
          Validator validator = schema.newValidator();
          validator.validate(new StreamSource(new StringReader(inputXml)));
          } catch (SAXException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
          } catch (IOException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
          }
  
  }
  </code></pre>
  
  <p>I found another possible solution, but it still don't work. With the following method, the exeption is thrown, wenn validating the xsds against the xml. The line of the excetion depends on the used xml. In the Example on the bottom, the error is in line 2. -> Error: <strong>Cannot find the declaration of element 'ServiceSpecificationSchema:serviceSpecification'</strong></p>
  
  <p>I think: 1. My java code is faulty or 2. all the xml files are. </p>
  
  <p>New Java Validator:</p>
  
  <pre><code>byte[] decoded = Base64.decodeBase64(XSD);
                  byte[] decoded2 = Base64.decodeBase64(XSD2);
  
                  SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
  
                  InputStream impFis = new ByteArrayInputStream(decoded2);
                  InputStream mainFis = new ByteArrayInputStream(decoded);
  
                  Source main = new StreamSource(mainFis);
                  Source imp = new StreamSource(impFis);
                  Source[] schemaFiles = new Source[] {imp, main};
                  Schema schema = factory.newSchema(schemaFiles);
                  Validator validator = schema.newValidator();
                  validator.validate(new StreamSource(new StringReader(inputXml))); 
  </code></pre>
  
  <p>Example XML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ServiceSpecificationSchema:serviceSpecification
          xmlns:ServiceSpecificationSchema="http://example.org/ServiceSpecificationSchema.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://example.org/ServiceSpecificationSchema.xsd"&gt;
  .....
  </code></pre>