Re: [xml] XSchema and libxml
- From: Kasimier Buchcik <kbuchcik 4commerce de>
- To: veillard redhat com
- Cc: xml gnome org
- Subject: Re: [xml] XSchema and libxml
- Date: Wed, 09 Mar 2005 18:56:43 +0100
Hi,
Daniel Veillard wrote:
On Wed, Mar 09, 2005 at 05:16:46PM +0100, Christian HAESSIG wrote:
Hi people,
I have a question about the XSchema integration in the library.
Is it possible to get and validate a xml file having a reference to its
schema in it ?
I think it's possible, you need to create the shemas context with a
NULL URL, Kasimier should know more precisely.
Subsidiary question : does anyone have a little code/example for doing this
?
if there is such an example I would add it to the example section
Daniel
I attached an example.
Regards,
Kasimier
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlschemas.h>
#ifdef LIBXML_SCHEMAS_ENABLED
/**
* validateViaXsi:
* @filename: a filename or an URL
*
* Parse the instance document and validate it using the schemata
* specified by xsi information.
*/
void
validateViaXsi(const char *filename) {
int res;
xmlDocPtr doc; /* the instance document */
xmlSchemaValidCtxtPtr validationCtxt; /* the XSD validation context */
/* Read the instance document. */
doc = xmlReadFile(filename, NULL, 0);
if (doc == NULL) {
fprintf(stderr, "Failed to parse the document '%s'.\n", filename);
return;
}
/* A validation context created with NULL as the
* @schema argument will validate the instance against the schemata
* indicated by xsi:schemaLocation and xsi:noNamespaceSchemaLocation.
*/
validationCtxt = xmlSchemaNewValidCtxt(NULL);
if (validationCtxt == NULL) {
fprintf(stderr, "Failed to create the validation context.\n");
xmlFreeDoc(doc);
return;
}
/* Validate the instance. */
res = xmlSchemaValidateDoc(validationCtxt, doc);
if (res == -1)
fprintf(stderr, "Internal error during validation.\n");
else if (res == 0)
fprintf(stdout, "The document '%s' is valid.\n", filename);
else
fprintf(stderr, "The document '%s' is not valid.\n", filename);
xmlSchemaFreeValidCtxt(validationCtxt);
xmlFreeDoc(doc);
return;
}
int main(int argc, char **argv) {
if (argc != 2)
return(1);
/*
* this initialize the library and check potential ABI mismatches
* between the version it was compiled for and the actual shared
* library used.
*/
LIBXML_TEST_VERSION
validateViaXsi(argv[1]);
/*
* Cleanup function for the XML library.
*/
xmlCleanupParser();
/*
* this is to debug memory for regression tests
*/
xmlMemoryDump();
return(0);
}
#else
int main(void) {
fprintf(stderr, "XML Schema support not compiled in\n");
exit(1);
}
#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]