gpt4 book ai didi

c++ - 未定义 schemaLocation 的 libxml2 命名空间前缀

转载 作者:行者123 更新时间:2023-11-30 04:02:02 25 4
gpt4 key购买 nike

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

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

doc.xml:1: namespace error : Namespace prefix xsi for schemaLocation on OAI-PMH is not defined. /www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd"

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

doc.xml:1: element OAI-PMH: Schemas validity error : Element 'OAI-PMH': No matching global declaration available for the validation root.

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

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>

最佳答案

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

关于c++ - 未定义 schemaLocation 的 libxml2 命名空间前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25444643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com