You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Ashlesha Gore <as...@gslab.com> on 2010/04/27 16:07:47 UTC

Validation of XML file with imported schema in it.

Hi ,

I am trying to use Xerces-c (version 2.7) to validate xml against xsd 
schema. I am facing a specific problem - I have an xml with one XSD 
schema (say XSD1) imported inside it. I call the application with this 
XML and a totally different schema (say XSD2) as an argument . Now the 
given XML should get validated against XSD2 and it should return error. 
But it returns success. If I remove the reference of XSD1 from the XML, 
then it returns failure. This shows that somewhere it is trying to 
validate against that imported schema XSD1 and not the schema provided 
during validation (XSD2). On further investigation I found that it 
decides priority between XSD1 and XSD2 depending on whether the 
namespace in XML matches with the namespace in XSD1 or XSD2. Is there 
any way to override this default setting and make it ignore the imported 
schema XSD1? Any help in this regard will be much appreciated.

Here is a snippet of my application code for your reference:

/   SAX2XMLReader::ValSchemes valScheme = SAX2XMLReader::Val_Auto;
     bool errorOccurred = false;

     // Initialize the XML4C2 system
     try
     {
         XMLPlatformUtils::Initialize();
     }
     catch(const XMLException & toCatch)
     {
         XERCES_STD_QUALIFIER cerr << "Error during initialization! 
Message:\n" << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
         return 1;
     }

     xmlFile = argV[1];
     xsdFile = argV[2];
     //
     //  Create a SAX parser object. Then, according to what we were told on
     //  the command line, set it to validate or not.
     //
     SAX2XMLReader *parser = XMLReaderFactory::createXMLReader();

     parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
     parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
     parser->setFeature(XMLUni::fgXercesDynamic, false);
     parser->setFeature(XMLUni::fgXercesSchema, true);
     parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);

     //
     //  Create our SAX handler object and install it on the parser, as the
     //  document and error handler.
     //
     schemaValidateHandlers handler;

     parser->setErrorHandler(&handler);
     parser->setEntityResolver(&handler);
     //reset error count first
     handler.resetErrors();

     parser->loadGrammar(xsdFile, Grammar::SchemaGrammarType, true);
     printf("\nload grammar done");
     parser->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true);


     try
     {
         parser->parse(xmlFile);
     }
     catch(const OutOfMemoryException &)
     {
         XERCES_STD_QUALIFIER cerr << "\nOutOfMemoryException" << 
XERCES_STD_QUALIFIER endl;
         errorOccurred = true;
     }
     catch(const XMLException & e)
     {
         XERCES_STD_QUALIFIER cerr << "\nError during parsing: '" << 
xmlFile << "'\n" << "Exception message is:  \n" << StrX(e.getMessage()) 
<< "\n" << XERCES_STD_QUALIFIER endl;
         errorOccurred = true;
     }
     catch (const SAXParseException& toCatch)
     {
         XERCES_STD_QUALIFIER cerr << "Error during initialization! 
Message:\n" << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
         return -1;
     }
     catch(...)
     {
         XERCES_STD_QUALIFIER cerr << "FAILURE" << XERCES_STD_QUALIFIER 
endl;
         XERCES_STD_QUALIFIER cerr << "\nUnexpected exception during 
parsing: '" << xmlFile << "'\n";
         errorOccurred = true;
     }

     if (!handler.getSawErrors())
     {
         printf("Validation SUCCESS\n");
     } else {
         errorOccurred = true;
     }

     //  Delete the parser itself.  Must be done prior to calling 
Terminate, below.
     delete parser;

     // And call the termination method
     XMLPlatformUtils::Terminate();
/
Regards,
-Ashlesha