/** * section: XPath * synopsis: Evaluate XPath expression and prints result node set. * purpose: Shows how to evaluate XPath expression and register * known namespaces in XPath context. * usage: xpath1 [] * test: ./xpath1 test3.xml '//child2' > xpath1.tmp ; diff xpath1.tmp xpath1.res ; rm xpath1.tmp * author: Aleksey Sanin * copy: see Copyright for the status of this software. */ #include #include #include #include #include #include #include #include #ifdef LIBXML_XPATH_ENABLED void usage(const char *name); int execute_xpath_expression(const char* filename, const xmlChar* xpathExpr, const xmlChar* nsList); void LibXML_serror_handler(void *userData, xmlErrorPtr err) { printf("ERROR occured\n"); } int main(int argc, char **argv) { /* Parse command line and process file */ if((argc < 2) || (argc > 3)) { fprintf(stderr, "Error: wrong number of arguments.\n"); usage(argv[0]); return(-1); } /* Init libxml */ xmlInitParser(); LIBXML_TEST_VERSION xmlSetStructuredErrorFunc(NULL, (xmlStructuredErrorFunc)LibXML_serror_handler); /* Do the main job */ execute_xpath_expression(argv[1], BAD_CAST argv[2], (argc > 3) ? BAD_CAST argv[3] : NULL); /* Shutdown libxml */ xmlCleanupParser(); /* * this is to debug memory for regression tests */ xmlMemoryDump(); return 0; } /** * usage: * @name: the program name. * * Prints usage information. */ void usage(const char *name) { assert(name); fprintf(stderr, "Usage: %s []\n", name); fprintf(stderr, "where is a list of known namespaces\n"); fprintf(stderr, "in \"= =href2> ...\" format\n"); } /** * execute_xpath_expression: * @filename: the input XML filename. * @xpathExpr: the xpath expression for evaluation. * @nsList: the optional list of known namespaces in * "= =href2> ..." format. * * Parses input XML file, evaluates XPath expression and prints results. * * Returns 0 on success and a negative value otherwise. */ int execute_xpath_expression(const char* filename, const xmlChar* xpathExpr, const xmlChar* nsList) { xmlDocPtr doc; xmlXPathContextPtr xpathCtx; xmlXPathObjectPtr xpathObj; xmlXPathCompExprPtr comp; assert(filename); assert(xpathExpr); /* Load XML document */ doc = xmlParseFile(filename); if (doc == NULL) { fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename); return(-1); } /* Create xpath evaluation context */ xpathCtx = xmlXPathNewContext(doc); if(xpathCtx == NULL) { fprintf(stderr,"Error: unable to create new XPath context\n"); xmlFreeDoc(doc); return(-1); } comp = xmlXPathCompile( xpathExpr ); if ( comp == NULL ) { fprintf(stderr,"Error: unable to compile XPath expression\n"); xmlFreeDoc(doc); return (-1); } /* Evaluate xpath expression */ xpathObj = xmlXPathCompiledEval(comp, xpathCtx); if(xpathObj == NULL) { fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr); xmlXPathFreeCompExpr(comp); xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); return(-1); } /* Cleanup */ xmlXPathFreeCompExpr(comp); xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); return(0); } #else int main(void) { fprintf(stderr, "XPath support not compiled in\n"); exit(1); } #endif