Hello,I have a validation routine that simply allows an xml file to be
validated against a supplied xsd. In the event that the xml file has an xsd
entry, I have an entity resolver than points back to the supplied xsd. That
portion is working fine. I am also using the loadGrammar method with the
hopes of it using that xsd data since the namespace is the same as that in
the document. If the document does not contain an xsd entry, it fails
validation on the root saying Unknown element. Can anyone suggest a way
that I can associate the xsd properly? The error handler and parser are
created outside the class and passed in. If more information is needed,
please let me know.
Environment:
Debian Linux running Xerces C++ 2.8
relevant code:
#include "validator.h"
#include <xercesc/framework/XMLSchemaDescription.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMDocumentType.hpp>
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMImplementationLS.hpp>
#include <xercesc/dom/DOMNodeIterator.hpp>
#include <xercesc/dom/DOMNodeList.hpp>
#include <xercesc/dom/DOMText.hpp>
#include <xercesc/util/XMLUni.hpp>
#include <xercesc/framework/LocalFileInputSource.hpp>
#include <xercesc/util/XMLString.hpp>
//
---------------------------------------------------------------------------
// XMLValidator Class
//
---------------------------------------------------------------------------
//
---------------------------------------------------------------------------
// XMLValidator Constructor
//
---------------------------------------------------------------------------
Internal::XMLValidator::XMLValidator(std::string activityFileName,
std::string xmlFileName,
xercesc::XercesDOMParser* parser,
UIIcon::ErrorObj* errorHandler,
bool forceNoNamespace) :
XMLProcessorActivityFile(activityFileName,
xmlFileName,
parser,
errorHandler){
}
//
---------------------------------------------------------------------------
// XMLValidator: XMLValidator
//
---------------------------------------------------------------------------
// make validate force external and use the xsd specified
bool Internal::XMLValidator::process(){
UIIcon::EntityResolver * entityResolver = NULL;
try{
// set the parser parameters
entityResolver = new UIIcon::EntityResolver(_activityFileName);
_parser->setXMLEntityResolver( entityResolver ) ;
xercesc::LocalFileInputSource
schemaFile(DualString(_activityFileName).asXMLString());
_parser->loadGrammar (schemaFile,
xercesc::Grammar::SchemaGrammarType, true);
_parser->setValidationScheme( _parser->Val_Always ) ;
_parser->setDoNamespaces( true ) ;
_parser->setDoValidation( true );
_parser->setDoSchema( false ) ;
_parser->setValidationSchemaFullChecking( true );
_parser->setValidationConstraintFatal(false);
_parser->useScanner( xercesc::XMLString::transcode("SGXMLScanner"));
// if not valid then return false.
try{
_parser->parse(xercesc::XMLString::transcode(_sourceFileName.c_str()));
// clear the memory used by the parser
_parser->resetDocumentPool();
// send the inverse of the error status since a succesfull
validation will have an error status of false
return !_errorHandler->getErrorStatus();
}
// any exception means there was a problem other than validation so
rethrow
catch(...){
throw;
}
}
// rethrow any raised errors
catch (...){
if (entityResolver != NULL){
delete entityResolver;
}
throw;
}
return true;
}
// end XMLValidator Class