includexpath.c revision d14bccc241b9e1cdf7718d7c86e4acfb768f2261
1<![CDATA[ 2#include <libxml/parser.h> 3#include <libxml/xpath.h> 4 5xmlDocPtr 6getdoc (char *docname) { 7 xmlDocPtr doc; 8 doc = xmlParseFile(docname); 9 10 if (doc == NULL ) { 11 fprintf(stderr,"Document not parsed successfully. \n"); 12 return NULL; 13 } 14 15 return doc; 16} 17 18xmlXPathObjectPtr 19getnodeset (xmlDocPtr doc, xmlChar *xpath){ 20 21 xmlXPathContextPtr context; 22 xmlXPathObjectPtr result; 23 24 context = xmlXPathNewContext(doc); 25 result = xmlXPathEvalExpression(xpath, context); 26 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ 27 printf("No result\n"); 28 return NULL; 29 } 30 xmlXPathFreeContext(context); 31 return result; 32} 33int 34main(int argc, char **argv) { 35 36 char *docname; 37 xmlDocPtr doc; 38 xmlChar *xpath = ("//keyword"); 39 xmlNodeSetPtr nodeset; 40 xmlXPathObjectPtr result; 41 int i; 42 xmlChar *keyword; 43 44 if (argc <= 1) { 45 printf("Usage: %s docname\n", argv[0]); 46 return(0); 47 } 48 49 docname = argv[1]; 50 doc = getdoc(docname); 51 result = getnodeset (doc, xpath); 52 if (result) { 53 nodeset = result->nodesetval; 54 for (i=0; i < nodeset->nodeNr; i++) { 55 keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1); 56 printf("keyword: %s\n", keyword); 57 xmlFree(keyword); 58 } 59 xmlXPathFreeObject (result); 60 } 61 xmlFreeDoc(doc); 62 xmlCleanupParser(); 63 return (1); 64} 65]]> 66