几天前,我开始学习在Linux上解析xml文档的libxml2 (Ubuntu14.04)。但不幸的是我有很多问题。
首先,当我使用函数xmlParseDoc()时,我有一条错误消息
doc.xml:1:名称空间错误:未定义schemaLocation的命名空间前缀xsi。/ /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有效性错误:元素'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>发布于 2014-08-22 10:57:33
需要在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>https://stackoverflow.com/questions/25444643
复制相似问题