blob: 6b66e6055650115003260339dde697974a6e2a26 [file] [log] [blame]
MDT 2003 John Fleckbc6734a2003-08-28 15:01:40 +00001<![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);
MDT 2004 John Fleck4c3bb7d2004-08-25 02:51:27 +000025 if (context == NULL) {
26 printf("Error in xmlXPathNewContext\n");
27 return NULL;
28 }
MDT 2003 John Fleckbc6734a2003-08-28 15:01:40 +000029 result = xmlXPathEvalExpression(xpath, context);
MDT 2004 John Fleck4c3bb7d2004-08-25 02:51:27 +000030 xmlXPathFreeContext(context);
31 if (result == NULL) {
32 printf("Error in xmlXPathEvalExpression\n");
33 return NULL;
34 }
MDT 2003 John Fleckbc6734a2003-08-28 15:01:40 +000035 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
MDT 2004 John Fleck4c3bb7d2004-08-25 02:51:27 +000036 xmlXPathFreeObject(result);
MDT 2003 John Fleckbc6734a2003-08-28 15:01:40 +000037 printf("No result\n");
38 return NULL;
39 }
MDT 2003 John Fleckbc6734a2003-08-28 15:01:40 +000040 return result;
41}
42int
43main(int argc, char **argv) {
44
45 char *docname;
46 xmlDocPtr doc;
MDT 2004 John Fleck218ed392004-05-28 01:49:38 +000047 xmlChar *xpath = (xmlChar*) "//keyword";
MDT 2003 John Fleckbc6734a2003-08-28 15:01:40 +000048 xmlNodeSetPtr nodeset;
49 xmlXPathObjectPtr result;
50 int i;
51 xmlChar *keyword;
52
53 if (argc <= 1) {
54 printf("Usage: %s docname\n", argv[0]);
55 return(0);
56 }
57
58 docname = argv[1];
59 doc = getdoc(docname);
60 result = getnodeset (doc, xpath);
61 if (result) {
62 nodeset = result->nodesetval;
63 for (i=0; i < nodeset->nodeNr; i++) {
64 keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
65 printf("keyword: %s\n", keyword);
MST 2004 John Fleckd14bccc2004-02-15 01:57:42 +000066 xmlFree(keyword);
MDT 2003 John Fleckbc6734a2003-08-28 15:01:40 +000067 }
MST 2004 John Fleckd14bccc2004-02-15 01:57:42 +000068 xmlXPathFreeObject (result);
MDT 2003 John Fleckbc6734a2003-08-28 15:01:40 +000069 }
MDT 2003 John Fleckbc6734a2003-08-28 15:01:40 +000070 xmlFreeDoc(doc);
71 xmlCleanupParser();
72 return (1);
73}
MDT 2004 John Fleck4c3bb7d2004-08-25 02:51:27 +000074]]>