【问题标题】:libxml2 namespace prefix for schemaLocation is not defined未定义 schemaLocation 的 libxml2 命名空间前缀
【发布时间】:2014-10-16 04:01:27
【问题描述】:

几天前,我开始学习在 Linux (Ubuntu 14.04) 上解析 xml 文档的 libxml2。但不幸的是,我有很多问题。

首先,我在使用函数xmlParseDoc()时出现错误消息:

doc.xml:1:命名空间错误:未定义 OAI-PMH 上 schemaLocation 的命名空间前缀 xsi。 /www.openarchives.org/OAI/2.0/http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"

当我尝试通过 xsd 验证 xml 文档时,第二个错误。但我认为这个错误是第一个问题的结果。这是一条错误消息:

doc.xml:1:元素 OAI-PMH:架构有效性错误:元素“OAI-PMH”:没有可用于验证根的匹配全局声明。

请大家帮忙解释一下我的错吗?

parseFile()函数:

bool parseFile(xmlDocPtr& doc, const char* filename) {
    doc = xmlParseFile(filename);
    if(doc == NULL) {
        std::cout << "Document is not parsing successfully: " << filename << std::endl;
        return false;
    }
    xmlNodePtr root = xmlDocGetRootElement(doc);
    if(root == NULL) {
        std::cout << "Empty document: " << filename << std::endl;
        return false;
    }
    if(xmlStrcmp(root->name, (const xmlChar *)"OAI-PMH")) {
        std::cout << "Document of the wrong type, root node != \"OAI-PMH\"" << std::endl;
        return false;
    }
    return true;
}

validateDoc()函数:

void validateDoc(xmlDocPtr doc) {
    std::cout << "Start to validate doc func\n";
    xmlSchemaParserCtxtPtr schemaParser = xmlSchemaNewParserCtxt("http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd");
    xmlSchemaPtr schema = xmlSchemaParse(schemaParser);
    xmlSchemaValidCtxtPtr schemaValid = xmlSchemaNewValidCtxt(schema);
    int result = xmlSchemaValidateDoc(schemaValid, doc);
    std::cout << "Result: " << result << std::endl; // result is equal to 1845!
    std::cout << "End validate doc func\n";
}

这是一个 xml 文档:

<OAI-PMH xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/  http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd>
    <responseDate>2014-08-21T22:00:24.551Z</responseDate>
    <request verb="Identify"/>
    <Identify>
        <repositoryName>Musketti KDK Data</repositoryName>
        <baseURL>http://www.museot.fi/baseUrl</baseURL>
        <protocolVersion>2.0</protocolVersion>
        <adminEmail>noreply@museo.fi</adminEmail>
        <earliestDatestamp>1900-01-01T00:00:00.000Z</earliestDatestamp>
        <deletedRecord>no</deletedRecord>
        <granularity>YYYY-MM-DDThh:mm:ssZ</granularity>
        <compression>NoCompression</compression>
    </Identify>
</OAI-PMH>

【问题讨论】:

    标签: c++ xml xml-parsing xsd libxml2


    【解决方案1】:

    您需要在 XML 中声明 xsi 前缀以使其有效:

    <OAI-PMH xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/  http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        .....
        .....
    </OAI-PMH>
    

    相关讨论:is the xsi: prefix assumed to be known in XML?

    【讨论】:

    • 非常感谢,它有帮助。但是第二个错误没有解决:元素 'OAI-PMH': No matching global declaration available for the validation root.
    • 您的新(或剩余)问题是您的 XML 输入不在 OAI-PMH 命名空间中。 (如果你不知道这意味着什么,你需要找到一个关于 XML 命名空间的教程并阅读它。)
    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2011-06-09
    相关资源
    最近更新 更多