Search code examples
c++xmlxml-parsingxsdlibxml2

libxml2 namespace prefix for schemaLocation is not defined


Few days ago I started to learn libxml2 for parsing xml documents on Linux (Ubuntu 14.04). But unfortunately I have a lot of problems with it.

First, I have an error message when I use function 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"

Second is an error when I try to validate xml-document via xsd. But I think this error is a result of first problem. This is an error message:

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

Please can everyone help and explain my fault?

There is parseFile() function:

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

There is validateDoc() function:

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

And this is a xml-document:

<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>

Solution

  • You need to have xsi prefix declared in the XML to make it valid :

    <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>
    

    Related discussion : is the xsi: prefix assumed to be known in XML?