title: Error "different target namespace", but schema validates and namespaces match?
body: <p>I have an XSD and several different XML messages that are being sent from a client to a server, which is attempting to validate the messages using the schema.  I'm using Xerces for parsing.  The XSD and XML are both valid, and I ran it through a couple tools (<a href="http://www.utilities-online.info/xsdvalidation/" rel="nofollow">for example</a>) to verify that the XML is validated using that schema.</p>
  
  <p>However, when I run the program, it prints out:</p>
  
  <p><code>Parsing Error: Schema in /path/to/xsd/MySchema.xsd has a different target namespace from the one specified in the instance document .</code></p>
  
  <p>(That path is the correct location of the schema, so it is reading the file as expected.) I don't know much about namespaces, so I tried to simplify as much as possible. Here's the XSD file:</p>
  
  <pre><code>&lt;xs:schema
          xmlns="http://www.mywebsite.com/schema/myschema"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          elementFormDefault="qualified"
          attributeFormDefault="unqualified"
          targetNamespace="http://www.mywebsite.com/schema/myschema"&gt;
      &lt;xs:element name="My_Data"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="PartOne" type="SubType" minOccurs="0" maxOccurs="1"/&gt;
                  &lt;xs:element name="PartTwo" type="xs:integer" minOccurs="0" maxOccurs="unbounded"/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      &lt;xs:complexType name="SubType"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="ElemOne" type="xs:string" /&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>And the XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no" ?&gt;
  &lt;My_Data
      xmlns="http://www.mywebsite.com/schema/myschema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;PartOne&gt;
          &lt;ElemOne&gt;thing&lt;/ElemOne&gt;
      &lt;/PartOne&gt;
      &lt;PartTwo&gt;123&lt;/PartTwo&gt;
  &lt;/My_Data&gt;
  </code></pre>
  
  <p>I've tried many variations on this including adding a schemaLocation attribute, but it keeps coming back to the target namespace. If I take out <code>ElemOne</code> from the above example and do not use a targetNamespace, it parses without problems.  Why would I get this error?  Is it actually an issue with namespaces (in which case, why would the online tool not catch it?), or something else?</p>
  
  <p>EDIT: here is the C++ code I am using to run this:</p>
  
  <pre class="lang-c prettyprint-override"><code>// g++ -g -Wall -pedantic -L /usr/lib -o schemasend schemasend.cc CustomParserErrorHandler.cc -lxerces-c
  
  #include &lt;xercesc/framework/MemBufInputSource.hpp&gt;
  #include "CustomParserErrorHandler.hh"
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  int main(int argc, char* argv[])
  {
    xercesc::XMLPlatformUtils::Initialize();
    std::cout &lt;&lt; "Intialized" &lt;&lt; std::endl;
    try {
      // Parse document
      const std::string aXML = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?&gt;&lt;My_Data xmlns=\"http://www.mywebsite.com/schema/myschema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema\"&gt;&lt;PartOne&gt;&lt;ElemOne&gt;thing&lt;/ElemOne&gt;&lt;/PartOne&gt;&lt;PartTwo&gt;123&lt;/PartTwo&gt;&lt;/My_Data&gt;";
      static std::string aXSDFilePath("MySchema.xsd");
  
      CustomParserErrorHandler aErrorHandler; // just a very basic implementation of error handling
  
      const xercesc::MemBufInputSource aInputSource(
        (const XMLByte*)aXML.c_str(), 
        static_cast&lt;uint&gt;(aXML.size()), 
        "stringBuffer");
  
      xercesc::XercesDOMParser* aParser = new xercesc::XercesDOMParser();
      aParser-&gt;cacheGrammarFromParse(true);
  
      aParser-&gt;setErrorHandler(&amp;aErrorHandler);
      aParser-&gt;setDoNamespaces(true);
      aParser-&gt;setDoSchema(true);
      aParser-&gt;setValidationSchemaFullChecking(true);
      aParser-&gt;setValidationScheme(xercesc::XercesDOMParser::Val_Always);
      aParser-&gt;setExternalNoNamespaceSchemaLocation((char*)NULL);
      aParser-&gt;setExternalNoNamespaceSchemaLocation(aXSDFilePath.c_str());
  
      aParser-&gt;setDoNamespaces(true);
      aParser-&gt;setDoSchema(true);
      aParser-&gt;setValidationScheme(xercesc::XercesDOMParser::Val_Always);
  
      aParser-&gt;parse(aInputSource);
      xercesc::DOMDocument* aDocument = aParser-&gt;getDocument();
      if (!aErrorHandler.HasErrorOccurred())
      {
        std::cout &lt;&lt; "--- PARSE WORKED ---" &lt;&lt; std::endl;
        // do more things
      }
      else
      {
        std::cout &lt;&lt;"--- PARSE ERROR ---" &lt;&lt; std::endl
          &lt;&lt; aErrorHandler.GetErrorString().c_str() &lt;&lt; std::endl
          &lt;&lt; "--- IN XML ---" &lt;&lt; std::endl
          &lt;&lt; aXML.c_str() &lt;&lt; std::endl;
  
          /* relevant error handler code:
            void CustomParserErrorHandler::HandleError(
                const xercesc::SAXParseException &amp;aException,
                std::stringstream                &amp;aStream)
            {
              aStream
                &lt;&lt; xercesc::XMLString::transcode(aException.getMessage())
                &lt;&lt; " at line " &lt;&lt; aException.getLineNumber()
                &lt;&lt; ", char " &lt;&lt; aException.getColumnNumber()
                &lt;&lt; " in file " 
                &lt;&lt; xercesc::XMLString::transcode(aException.getSystemId())
                &lt;&lt; std::endl;
            }
          */
      }
    }
    catch (const xercesc::XMLException&amp; toCatch) {
      std::cout &lt;&lt; "ERROR" &lt;&lt; std::endl;
      return 1;
    }
  
    xercesc::XMLPlatformUtils::Terminate();
    std::cout &lt;&lt; "bye" &lt;&lt; std::endl;
    return 0;
  }
  </code></pre>