未定义schemaLocation的libxml2名称空间前缀

时间:2014-08-22 10:14:39

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

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

首先,当我使用函数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-document时,第二个是错误。但我认为这个错误是第一个问题的结果。这是一条错误消息:

  

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>

1 个答案:

答案 0 :(得分:2)

您需要在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?