Xerces-C XML schema validation not working

笑着哭i 提交于 2019-12-11 07:44:35

问题


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


来源:https://stackoverflow.com/questions/5870797/xerces-c-xml-schema-validation-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!