Jeroen N. Witmond <jnw <at> xs4all.nl> writes:

> 
> On 2015-08-14 21:25, em wrote:
> > Hi,
> > 
> > I've been struggling to resolve this issue for a week now and haven't 
> > gotten
> > any responses on StackOverflow
> >
(http://stackoverflow.com/questions/31971324/error-different-target-namespace-but-schema-validates-and-namespaces-match),
> > so I found this list.
> 
> I've tried to recreate your problem but failed for lack of information; 
> see below.
> 
> > (snip)
>
> Please provide the exact commands that demonstrate the above file and 
> schema are valid.
>
> 
> > (snip)
> 
> Please provide the exact commands that demonstrate the above file and 
> schema are NOT valid; that is, the commands that result in the error 
> message you quoted above,.
> 
> 

Thanks for the reply, and apologies for lack of information. I just used an
online tool to verify my schema and XML:
http://www.utilities-online.info/xsdvalidation/ since I am new to XSD /
Xerces. The process for validating is inside of a rather complicated program
so I had to pare it down quite a bit.
Here is my C++ code:

// schemasend.cc, compile with:
// g++ -g -Wall -pedantic -L /usr/lib -o schemasend schemasend.cc
CustomParserErrorHandler.cc -lxerces-c

#include <xercesc/framework/MemBufInputSource.hpp>
#include "CustomParserErrorHandler.hh"
#include <string>
#include <iostream>

int main(int argc, char* argv[])
{
  xercesc::XMLPlatformUtils::Initialize();
  std::cout << "Intialized" << std::endl;
  try {
    // Parse document

    // This works:
    //const std::string aXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"
standalone=\"no\"
?><My_Data><PartOne>testing</PartOne><PartTwo>123</PartTwo></My_Data>";
    //static std::string aXSDFilePath("MySchemaSimple.xsd");
   
    const std::string aXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"
standalone=\"no\" ?><My_Data
xmlns=\"http://www.mywebsite.com/schema/myschema\";
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema\";><PartOne><ElemOne>thing</ElemOne></PartOne><PartTwo>123</PartTwo></My_Data>";
    static std::string aXSDFilePath("MySchema.xsd");
    
    CustomParserErrorHandler aErrorHandler;

    const xercesc::MemBufInputSource aInputSource(
      (const XMLByte*)aXML.c_str(), 
      static_cast<uint>(aXML.size()), 
      "stringBuffer");

    xercesc::XercesDOMParser* aParser = new xercesc::XercesDOMParser();
    aParser->cacheGrammarFromParse(true);

    aParser->setErrorHandler(&aErrorHandler);
    aParser->setDoNamespaces(true);
    aParser->setDoSchema(true);
    aParser->setValidationSchemaFullChecking(true);
    aParser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
    aParser->setExternalNoNamespaceSchemaLocation((char*)NULL);
    aParser->setExternalNoNamespaceSchemaLocation(aXSDFilePath.c_str());
    
    aParser->setDoNamespaces(true);
    aParser->setDoSchema(true);
    aParser->setValidationScheme(xercesc::XercesDOMParser::Val_Always);
    
    aParser->parse(aInputSource);
    xercesc::DOMDocument* aDocument = aParser->getDocument();
    if (!aErrorHandler.HasErrorOccurred())
    {
      std::cout << "--- PARSE WORKED ---" << std::endl;
      // do more things
    }
    else
    {
      std::cout <<"--- PARSE ERROR ---" << std::endl
        << aErrorHandler.GetErrorString().c_str() << std::endl
        << "--- IN XML ---" << std::endl
        << aXML.c_str() << std::endl;

        /* relevant error handler code:
          void CustomParserErrorHandler::HandleError(
              const xercesc::SAXParseException &aException,
              std::stringstream                &aStream)
          {
            aStream
              << xercesc::XMLString::transcode(aException.getMessage())
              << " at line " << aException.getLineNumber()
              << ", char " << aException.getColumnNumber()
              << " in file " 
              << xercesc::XMLString::transcode(aException.getSystemId())
              << std::endl;
          }
        */
    }
  }
  catch (const xercesc::XMLException& toCatch) {
    std::cout << "ERROR" << std::endl;
    return 1;
  }

  xercesc::XMLPlatformUtils::Terminate();
  std::cout << "bye" << std::endl;
  return 0;
}
// end schemasend.cc

where “MySchemaSimple.xsd” is the first schema in my original email, and
“MySchema.xsd” is the second (the XML strings are also the same).  I can
send CustomParserErrorHandler.hh/cc if necessary but all that does is print
out error messages, so I don’t imagine it is the issue.

Reply via email to