Xerces-C XML 模式验证不起作用

发布于 2024-11-04 23:21:48 字数 3762 浏览 1 评论 0原文

尝试让 Xerces-C 根据架构文件验证 XML 文件,但没有成功。下面的构造函数接受 XML 文件和架构文件的位置,并设置相关成员变量:

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

设置 XML 解析的构造函数区域如下所示。我认为这就是问题所在:

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

当代码编译并运行时,一切正常,但没有针对架构对 XML 进行验证。这意味着即使 XML 不符合架构,也会对其进行解析。经过检查,我还能够确定所有模式的 errorCount 值均保持为 0。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2024-11-11 23:21:48

您必须像这样引用 xml 根节点上的架构:

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

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">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文