1

Trying to get Xerces-C to validate an XML file against a schema file but with no luck. The constructor below takes in the location of the XML file and the schema file and sets relevent member variables:

Config::Config(const std::string& schemaFile, const std::string& XMLFile)
    : m_schemaFile(schemaFile),
      m_XMLFile(XMLFile)
{

    {
        //initialize
        try
        {
            xercesc::XMLPlatformUtils::Initialize();
            xalanc::XPathEvaluator::initialize();
        }
        catch (xercesc::XMLException& e)
        {
            throw XercesInitialisationException();
        }
    }

    {
        //validate XML
        xercesc::XercesDOMParser m_domParser;
        if (NULL == m_domParser.loadGrammar(m_schemaFile.c_str(), xercesc::Grammar::SchemaGrammarType))
        {
            //schema file could not be loaded
            throw SchemaLoadException();
        }

        ParserErrorHandler errorHandler;
        m_domParser.setErrorHandler(&errorHandler);
        m_domParser.setDoNamespaces(true);
        m_domParser.setDoSchema(true);
        m_domParser.setValidationConstraintFatal(true);
        m_domParser.setValidationSchemaFullChecking(true);

        m_domParser.parse(m_XMLFile.c_str());

        if (NULL == m_domParser.getDocument() || NULL == m_domParser.getDocument()->getDocumentElement())
        {
            throw XMLLoadException();
        }

        if (0 == m_domParser.getErrorCount())
        {
            std::cout << "Number of schema validation errors: " << m_domParser.getErrorCount() << std::endl;
        }
        else
        {
            //m_validated unsuccessfully against the schema
            throw SchemaValidationException();
        }
    }

    {
        //set up XPath interpreter
        const xalanc::XalanDOMString m_xalanXMLFile(m_XMLFile.c_str());
        const xercesc::LocalFileInputSource m_xalanInputSource(m_xalanXMLFile.c_str());

        // Initialise XalanSourceTree subsystem...
        xalanc::XalanSourceTreeInit sourceTreeInit;
        m_liaison = std::auto_ptr<xalanc::XalanSourceTreeParserLiaison>(new xalanc::XalanSourceTreeParserLiaison(m_domSupport));
        m_domSupport.setParserLiaison(m_liaison.get());
        m_document = m_liaison->parseXMLStream(m_xalanInputSource);
        m_prefixResolver = std::auto_ptr<xalanc::XalanDocumentPrefixResolver>(new xalanc::XalanDocumentPrefixResolver(m_document));
        m_evaluator = std::auto_ptr<xalanc::XPathEvaluator>(new xalanc::XPathEvaluator);
    }
}

The area of the constructor where the XML parse is set up is shown below. This is where I think the problem lies:

//validate XML
        xercesc::XercesDOMParser m_domParser;
        if (NULL == m_domParser.loadGrammar(m_schemaFile.c_str(), xercesc::Grammar::SchemaGrammarType))
        {
            //schema file could not be loaded
            throw SchemaLoadException();
        }

        ParserErrorHandler errorHandler;
        m_domParser.setErrorHandler(&errorHandler);
        m_domParser.setDoNamespaces(true);
        m_domParser.setDoSchema(true);
        m_domParser.setValidationConstraintFatal(true);
        m_domParser.setValidationSchemaFullChecking(true);

        m_domParser.parse(m_XMLFile.c_str());

        if (NULL == m_domParser.getDocument() || NULL == m_domParser.getDocument()->getDocumentElement())
        {
            throw XMLLoadException();
        }

        if (0 == m_domParser.getErrorCount())
        {
            std::cout << "Number of schema validation errors: " << m_domParser.getErrorCount() << std::endl;
        }
        else
        {
            //m_validated unsuccessfully against the schema
            throw SchemaValidationException();
        }

When the code is compiled and ran everything works but no validation is carried out on the XML against the schema. Which means the XML is parsed even if it does not conform to the schema. After checking I have also been able to ascertain that the errorCount value remains 0 for all schemas.

Gazza
  • 11
  • 1
  • 2
  • possible duplicate of http://stackoverflow.com/questions/2455071/validating-document-in-xerces-c – ColWhi May 03 '11 at 14:30
  • Maybe you could compare your code with the example http://codesynthesis.com/~boris/data/load-grammar/load-grammar-dom.cxx that was discussed in this blog post http://www.codesynthesis.com/~boris/blog/2010/03/15/validating-external-schemas-xerces-cxx/ – Erik Sjölund Mar 19 '13 at 22:39

1 Answers1

0

You must to reference the schema on xml root node like this:

<rootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="Schema.xsd">