Xerces-C XML schema validation not working

2019-08-14 06:13发布

问题:

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.

回答1:

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">