title: XML validation against xsd's in Java
body: <p>Issue:
  We have several services that generate a fair amount of XML via XSLT. We don't have any XSD's. I have taken the time to create the XSD's and want to confirm they are correct. Currently I am attempting to verify that the XSD and the XML are validate correctly.</p>
  
  <p>Problem:
  I have an xsd(common.xsd) that is imported into all the xsd's. It is not publicly hosted yet, so only recently I found putting the full path of the common.xsd in the AccountList.xsd I was able to get further. I am now receiving the following:</p>
  
  <blockquote>
    <p>org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 70; s4s-att-invalid-value: Invalid attribute value for 'type' in element 'element'. Recorded reason: UndeclaredPrefix: Cannot resolve 'common:response' as a QName: the prefix 'common' is not declared.</p>
  </blockquote>
  
  <p>I am at a loss. I cannot find an example that has been asked in forums or a source code snippet that gets a success. I'd appreciate any assistance in getting this to successfully validate my xml.</p>
  
  <p>common.xsd</p>
  
  <pre><code>&lt;xs:schema  version="1.0" elementFormDefault="qualified" attributeFormDefault="unqualified"
          xmlns="http://www.myorg.com/xsd/gen_fin"
          xmlns:xs="http://www.w3.org/2001/XMLSchema" 
          targetNamespace="http://www.myorg.com/xsd/gen_fin"&gt;
      &lt;xs:complexType name="response"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="code" type="xs:string"/&gt;
              &lt;xs:element name="description" type="xs:string"/&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>AccountList.xsd</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  
  &lt;xs:schema  version="1.0" elementFormDefault="qualified" attributeFormDefault="unqualified"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:tns="http://www.myorg.com/xsd/accList"
              targetNamespace="http://www.myorg.com/xsd/accList"
              xmlns:common="http://www.myorg.com/xsd/gen_fin"&gt;
      &lt;xs:import namespace="http://www.myorg.com/xsd/gen_fin" 
                 schemaLocation="/home/me/dev/projects/svn/myorg/xsd/src/main/resources/bg/gen_resp/common.xsd"/&gt;
  
      &lt;xs:element name="fundamo"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="response" type="common:response" minOccurs="1" maxOccurs="1"/&gt;
                  &lt;xs:element name="transaction" type="tns:transaction" minOccurs="0" maxOccurs="1"/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:complexType name="transaction"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="transactionRef" type="xs:string"/&gt;
              &lt;xs:element name="dateTime" type="xs:string"/&gt;
              &lt;xs:element name="userName" type="xs:string"/&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>Test.java</p>
  
  <pre><code>final InputStream commonXsdStream = getXsd(BG_GEN_RESP_XSD_PATH, COMMON);
  
  ClassPathResource fullXsdListing = new ClassPathResource(BG_GEN_RESP_XSD_PATH);
  
  File[] allXsds = fullXsdListing.getFile().listFiles();
  
  for (File currentXsd : allXsds) {
      final int filenameLength = currentXsd.getName().length();
      final String filenameSanExt = currentXsd.getName().substring(0, filenameLength - 4);
  
      if (!IGNORE.contains(filenameSanExt)) {
          final InputStream xsltStream = getXslt(BG_GEN_RESP_XSLT_PATH, filenameSanExt);
          final InputStream xsdStream = getXsd(BG_GEN_RESP_XSD_PATH, filenameSanExt);
  
          TransformerFactory xmlTransformer = TransformerFactory.newInstance();
          Templates xsltTemplate = xmlTransformer.newTemplates(new StreamSource(xsltStream));
          final XSLToXMLConvertor converter = new XSLToXMLConvertor();
          String generatedXml = converter.getXML(inputData, xsltTemplate);
  
          LOG.info(generatedXml);
  
          SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
          Schema schema = schemaFactory.newSchema(lnew StreamSource(xsdStream));
  
          Validator validator = schema.newValidator();
          validator.validate(new StreamSource(new StringReader(generatedXml)));
  
          /*
          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
          docBuilderFactory.setNamespaceAware(true);
          docBuilderFactory.setValidating(true);
  
          DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
          docBuilder.parse(new InputSource(new ByteArrayInputStream(generatedXml.getBytes("utf-8"))));
          */
          }
      }
  }
  </code></pre>