/** * Sample namespace XSD validation program. * Released into public domain. * Piotr Sipika */ #include #include #include #include #include int main(int argc, char * argv[]) { if (argc != 3) { fprintf(stderr, "Need file name and schema file name\n"); return EXIT_FAILURE; } const char *pczFile = argv[1]; const char *pczSchema = argv[2]; xmlInitParser(); xmlDocPtr pDoc = xmlReadFile(pczFile, NULL, XML_PARSE_NONET); if (!pDoc) { fprintf(stderr, "Unable to read %s\n", pczFile); xmlCleanupParser(); return EXIT_FAILURE; } xmlDocPtr pSchemaDoc = xmlReadFile(pczSchema, NULL, XML_PARSE_NONET); if (!pSchemaDoc) { fprintf(stderr, "Unable to read %s\n", pczSchema); xmlFreeDoc(pDoc); xmlCleanupParser(); return EXIT_FAILURE; } // create actual schema xmlSchemaParserCtxtPtr pSchemaCtxt = xmlSchemaNewDocParserCtxt(pSchemaDoc); if (!pSchemaCtxt) { fprintf(stderr, "Unable to create schema context\n"); xmlFreeDoc(pDoc); xmlFreeDoc(pSchemaDoc); xmlCleanupParser(); return EXIT_FAILURE; } xmlSchemaPtr pSchema = xmlSchemaParse(pSchemaCtxt); if (!pSchema) { fprintf(stderr, "Unable to create schema\n"); xmlSchemaFreeParserCtxt(pSchemaCtxt); xmlFreeDoc(pDoc); xmlFreeDoc(pSchemaDoc); xmlCleanupParser(); return EXIT_FAILURE; } xmlSchemaValidCtxtPtr pValidCtxt = xmlSchemaNewValidCtxt(pSchema); if (!pValidCtxt) { fprintf(stderr, "Unable to create validity context for schema\n"); xmlSchemaFree(pSchema); xmlSchemaFreeParserCtxt(pSchemaCtxt); xmlFreeDoc(pDoc); xmlFreeDoc(pSchemaDoc); xmlCleanupParser(); return EXIT_FAILURE; } fprintf(stdout, "Attempting to validate %s with %s\n", pczFile, pczSchema); xmlSchemaFreeParserCtxt(pSchemaCtxt); xmlFreeDoc(pSchemaDoc); int iError = xmlSchemaValidateDoc(pValidCtxt, pDoc); if (iError == 0) { fprintf(stdout, "Document in %s is valid\n", pczFile); } else { fprintf(stdout, "Document in %s is NOT valid\n", pczFile); } xmlSchemaFree(pSchema); // xmlSchemaFreeParserCtxt(pSchemaCtxt); xmlFreeDoc(pDoc); // xmlFreeDoc(pSchemaDoc); xmlCleanupParser(); return EXIT_SUCCESS; }