Pourquoi cette validation XML via XSD échoue-t-elle dans libxml2 (mais réussit dans xmllint) et comment la réparer?

Si j'exécute ce de validation XML via xmllint:

xmllint --noout --schema schema.xsd test.xml

- Je obtenir ce succès message:

.../test.xml validates

Cependant, si je lance le même de la validation via libxml2 de l'API C:

int result = xmlSchemaValidateDoc(...)

- Je obtenir un valeur de retour de 1845 et ce échec message:

Element '{http://example.com/XMLSchema/1.0}foo': No matching global declaration available for the validation root.

Que je peux faire absolument aucun sens. 🙁


schéma.xsd:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE xs:schema PUBLIC "-//W3C//DTD XMLSCHEMA 200102//EN" "XMLSchema.dtd" >
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://example.com/XMLSchema/1.0" targetNamespace="http://example.com/XMLSchema/1.0" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:element name="foo">
    </xs:element>
</xs:schema>

test.xml:

<?xml version="1.0" encoding="UTF-8"?>

<foo xmlns="http://example.com/XMLSchema/1.0">
</foo>

principal.c:

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/valid.h>
#include <libxml/xmlschemas.h>
u_int32_t get_file_size(const char *file_name) {
struct stat buf;
if ( stat(file_name, &buf) != 0 ) return(0);
return (unsigned int)buf.st_size;
}
void handleValidationError(void *ctx, const char *format, ...) {
char *errMsg;
va_list args;
va_start(args, format);
vasprintf(&errMsg, format, args);
va_end(args);
fprintf(stderr, "Validation Error: %s", errMsg);
free(errMsg);
}
int main (int argc, const char * argv[]) {
const char *xsdPath = argv[1];
const char *xmlPath = argv[2];
printf("\n");
printf("XSD File: %s\n", xsdPath);
printf("XML File: %s\n", xmlPath);
int xmlLength = get_file_size(xmlPath);
char *xmlSource = (char *)malloc(sizeof(char) * xmlLength);
FILE *p = fopen(xmlPath, "r");
char c;
unsigned int i = 0;
while ((c = fgetc(p)) != EOF) {
xmlSource[i++] = c;
}
printf("\n");
printf("XML Source:\n\n%s\n", xmlSource);
fclose(p);
printf("\n");
int result = 42;
xmlSchemaParserCtxtPtr parserCtxt = NULL;
xmlSchemaPtr schema = NULL;
xmlSchemaValidCtxtPtr validCtxt = NULL;
xmlDocPtr xmlDocumentPointer = xmlParseMemory(xmlSource, xmlLength);
parserCtxt = xmlSchemaNewParserCtxt(xsdPath);
if (parserCtxt == NULL) {
fprintf(stderr, "Could not create XSD schema parsing context.\n");
goto leave;
}
schema = xmlSchemaParse(parserCtxt);
if (schema == NULL) {
fprintf(stderr, "Could not parse XSD schema.\n");
goto leave;
}
validCtxt = xmlSchemaNewValidCtxt(schema);
if (!validCtxt) {
fprintf(stderr, "Could not create XSD schema validation context.\n");
goto leave;
}
xmlSetStructuredErrorFunc(NULL, NULL);
xmlSetGenericErrorFunc(NULL, handleValidationError);
xmlThrDefSetStructuredErrorFunc(NULL, NULL);
xmlThrDefSetGenericErrorFunc(NULL, handleValidationError);
result = xmlSchemaValidateDoc(validCtxt, xmlDocumentPointer);
leave:
if (parserCtxt) {
xmlSchemaFreeParserCtxt(parserCtxt);
}
if (schema) {
xmlSchemaFree(schema);
}
if (validCtxt) {
xmlSchemaFreeValidCtxt(validCtxt);
}
printf("\n");
printf("Validation successful: %s (result: %d)\n", (result == 0) ? "YES" : "NO", result);
return 0;
}

sortie de la console:

XSD File: /Users/dephiniteloop/Desktop/xml_validate/schema.xsd
XML File: /Users/dephiniteloop/Desktop/xml_validate/test.gkml
XML Source:
<?xml version="1.0" encoding="UTF-8"?>
<foo xmlns="http://example.com/XMLSchema/1.0">
</foo>
Validation Error: Element '{http://example.com/XMLSchema/1.0}foo': No matching global declaration available for the validation root.
Validation successful: NO (result: 1845)

En cas de questions: je suis sur OS x 10.6.7 avec sa valeur par défaut libxml2.dylib
(/Developer/SDKs/MacOSX10.6.sdk/usr/lib/libxml2.2.7.3.dylib)

source d'informationauteur mtree