/* * This is called by ??? when reading an annotation is encountered * while reading the instance data. */ xmlParserErrors instance_annotation_callback(void *handle, xmlNodePtr node) { int i; char key[20]; xmlChar* path; xmlDocPtr doc = node->doc; xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc); xmlXPathObjectPtr xpathObj; sprintf(key, "%p", handle); path = (xmlChar*)xmlHashLookup(Handle2Path, (xmlChar*)key); assert(path != NULL); printf("\noooooooooooo %s: %p\n", __FUNCTION__, handle); dump_doc(node->doc, node); /* walk_doc_tree(node, 0); */ printf("\n"); xpathCtx->node = node; xpathObj = xmlXPathEvalExpression(path, xpathCtx); if (xpathObj->nodesetval != NULL) for (i = 0; i < xpathObj->nodesetval->nodeNr; ++i) printf("xpath(\"%s\") => %s\n", path, xmlNodeGetContent(xpathObj->nodesetval->nodeTab[i])); return XML_ERR_OK; }
xmlNodePtr generation_callback(void * handle, xmlNodePtr node, void* ctxt) { printf("\noooooooooooo %s: %p\n", __FUNCTION__, handle); xmlSetTreeDoc(node, (xmlDocPtr)ctxt); if (xmlStrEqual(node->name, BAD_CAST "people")) xmlDocSetRootElement(node->doc, node); dump_doc(node->doc, node); if (xmlStrEqual(node->name, BAD_CAST "fname")) { printf("here!\n"); return (xmlNodePtr) xmlNewNsProp(node->parent, node->ns, node->name, BAD_CAST "bob"); } return node; }
int test (const char* base) { xmlDocPtr docPtr = NULL; char filename[100]; xmlSchemaPtr wxschemas = NULL; Handle2Path = xmlHashCreate(0); /* Read the schema. */ { /* There is no visitibility into parserCtxt. */ xmlSchemaParserCtxtPtr parserCtxt; /* parserCtxt->ctxtType is xmlSchemaTypePtr */ snprintf(filename, 100, "%s.xsd", base); printf("\n\n\n----------------------------------------------------------------\n\n\n"); printf("\n----> Reading schema %s...\n", filename); parserCtxt = xmlSchemaNewParserCtxt(filename); xmlSchemaSetParserErrors(parserCtxt, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stdout); xmlSchemaSetParserAnnotation(parserCtxt, schema_annotation_callback, NULL); wxschemas = xmlSchemaParse(parserCtxt); if (wxschemas == NULL) { printf("***** schema parsing failed!\n"); } xmlSchemaFreeParserCtxt(parserCtxt); printf("\n<---- Schema read!\n\n"); } /* Read the XML. */ { snprintf(filename, 100, "%s.xml", base); if ((docPtr = xmlReadFile(filename, NULL, 0)) == NULL) { printf("failed to parse \"%s\".\n", filename); return -1; } } if (!SKIP) { /* There is no visibility into schemaCtxt. */ xmlSchemaValidCtxtPtr schemaCtxt; int ret; printf("\n----------------------------------------------------------------\n"); printf("\n----> Validating document %s...\n", filename); /* This sets up the schemaCtxt, including a pointer to wxschemas. */ schemaCtxt = xmlSchemaNewValidCtxt(wxschemas); xmlSchemaSetValidErrors(schemaCtxt, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stdout); ret = xmlSchemaValidateDoc(schemaCtxt, docPtr); /* read me! */ if (ret == 0) { /* printf("%s validates\n", filename); */ } else if (ret > 0) { printf("%s fails to validate\n", filename); } else { printf("%s validation generated an internal error\n", filename); } xmlSchemaFreeValidCtxt(schemaCtxt); printf("\n<---- Document validated!\n"); } /* Generate a doc and validate it. */ { xmlDocPtr newDoc = xmlNewDoc(BAD_CAST "1.0"); { xmlSchemaValidCtxtPtr schemaCtxt; int ret; schemaCtxt = xmlSchemaNewValidCtxt(wxschemas); xmlSchemaSetValidErrors(schemaCtxt, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stdout); xmlSchemaSetGeneratorCallback(schemaCtxt, BAD_CAST "people", NULL, &generation_callback, newDoc); ret = xmlSchemaValidateDoc(schemaCtxt, newDoc); if (ret == 0) { /* xmlDocSetRootElement(newDoc, vctxt->node); */ dump_doc(newDoc, NULL); } else if (ret > 0) printf("%s fails to validate\n", filename); else printf("%s validation generated an internal error\n", filename); xmlSchemaFreeValidCtxt(schemaCtxt); printf("\n<---- Schema read!\n\n"); } { xmlSchemaValidCtxtPtr schemaCtxt; int ret; schemaCtxt = xmlSchemaNewValidCtxt(wxschemas); xmlSchemaSetValidErrors(schemaCtxt, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stdout); ret = xmlSchemaValidateDoc(schemaCtxt, newDoc); if (ret == 0) ; else if (ret > 0) printf("%s fails to validate\n", filename); else printf("%s validation generated an internal error\n", filename); xmlSchemaFreeValidCtxt(schemaCtxt); printf("\n<---- Schema read!\n\n"); } xmlFreeDoc(newDoc); } #if 0 /* why can't I just start with doc->children? */ tree_trunk = xmlDocGetRootElement(docPtr); #endif #if 0 tree_trunk = docPtr->children; printf("\n\n\n----------------------------------------------------------------\n\n\n"); printf("\nWalking doc tree...\n"); walk_doc_tree(tree_trunk, 0); printf("\n"); #endif printf("\n\n\n----------------------------------------------------------------\n\n\n"); printf("\nWalking schema tree...\n"); walk_schema_tree(wxschemas); printf("\n"); /*****************************************************************/ /*****************************************************************/ /*****************************************************************/ /*****************************************************************/ /* This will tell me, for example, how to decode sequences. */ printf("\n\n\n----------------------------------------------------------------\n\n\n"); xmlSchemaDump(stdout, wxschemas); /*****************************************************************/ /*****************************************************************/ /*****************************************************************/ /*****************************************************************/ xmlFreeDoc(docPtr); xmlCleanupParser(); xmlHashFree(Handle2Path, NULL); return 0; }
/// Dump DOM tree using XercesC handles void dump_tree(Document doc, ostream& os) { dump_doc((DOMDocument*)doc.ptr(),os); }