You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by ashlesha1 <as...@gmail.com> on 2010/04/26 15:26:59 UTC

Re: validating xml with xsd schema

Hi ,

I am trying to use Xerces-c to validate xml with xsd schema. I have checked
that I have done all settings mentioned in this thread. Still I am facing a
specific problem - I have an xml with its 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). Can anybody please give me
some pointers on this?

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

    const char *xmlFile = 0;
    char *xsdFile = 0;

    SAX2XMLReader::ValSchemes valScheme = SAX2XMLReader::Val_Auto;
    bool doNamespaces = true;
    bool doSchema = true;
    bool schemaFullChecking = false;
    bool identityConstraintChecking = true;
    bool errorOccurred = false;
    bool namespacePrefixes = true;

    // 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;
    }

   SAX2XMLReader *parser = XMLReaderFactory::createXMLReader();

    parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, doNamespaces);
    parser->setFeature(XMLUni::fgXercesSchema, doSchema);
   
parser->setFeature(XMLUni::fgXercesSchemaFullChecking,schemaFullChecking);
   
parser->setFeature(XMLUni::fgXercesIdentityConstraintChecking,identityConstraintChecking);
   
parser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes,namespacePrefixes);

    parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
    parser->setFeature(XMLUni::fgXercesDynamic, false);
    parser->setFeature(XMLUni::fgXercesContinueAfterFatalError, false);
    parser->setFeature(XMLUni::fgXercesValidationErrorAsFatal, false);
    parser->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, false);

    parser->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,xsdFile
);
    schemaValidateHandlers handler;

    parser->setErrorHandler(&handler);
    parser->setEntityResolver(&handler);

    xmlFile = argV[1];
    xsdFile = argV[2];

    handler.resetErrors();
    try
    {
        parser->loadGrammar(xsdFile, Grammar::SchemaGrammarType, true);
        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 parser;
    XMLPlatformUtils::Terminate();


Regards,
-Ashlesha
  




                                                                                                                                          
91,5          28%




-- 
View this message in context: http://old.nabble.com/validating-xml-with-xsd-schema-tp23315036p28364402.html
Sent from the Xerces - C - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org