1 <html><head><meta http-equiv=
"Content-Type" content=
"text/html; charset=ISO-8859-1"><title>D. Code for XPath Example
</title><meta name=
"generator" content=
"DocBook XSL Stylesheets V1.61.2"><link rel=
"home" href=
"index.html" title=
"Libxml Tutorial"><link rel=
"up" href=
"index.html" title=
"Libxml Tutorial"><link rel=
"previous" href=
"apc.html" title=
"C. Code for Keyword Example"><link rel=
"next" href=
"ape.html" title=
"E. Code for Add Keyword Example"></head><body bgcolor=
"white" text=
"black" link=
"#0000FF" vlink=
"#840084" alink=
"#0000FF"><div class=
"navheader"><table width=
"100%" summary=
"Navigation header"><tr><th colspan=
"3" align=
"center">D. Code for XPath Example
</th></tr><tr><td width=
"20%" align=
"left"><a accesskey=
"p" href=
"apc.html">Prev
</a> </td><th width=
"60%" align=
"center"> </th><td width=
"20%" align=
"right"> <a accesskey=
"n" href=
"ape.html">Next
</a></td></tr></table><hr></div><div class=
"appendix" lang=
"en"><div class=
"titlepage"><div><div><h2 class=
"title"><a name=
"xpathappendix"></a>D. Code for XPath Example
</h2></div></div><div></div></div><p>
2 </p><pre class=
"programlisting">
3 #include
<libxml/parser.h
>
4 #include
<libxml/xpath.h
>
7 getdoc (char *docname) {
9 doc = xmlParseFile(docname);
12 fprintf(stderr,
"Document not parsed successfully. \n");
20 getnodeset (xmlDocPtr doc, xmlChar *xpath){
22 xmlXPathContextPtr context;
23 xmlXPathObjectPtr result;
25 context = xmlXPathNewContext(doc);
26 if (context == NULL) {
27 printf(
"Error in xmlXPathNewContext\n");
30 result = xmlXPathEvalExpression(xpath, context);
31 xmlXPathFreeContext(context);
33 printf(
"Error in xmlXPathEvalExpression\n");
36 if(xmlXPathNodeSetIsEmpty(result-
>nodesetval)){
37 xmlXPathFreeObject(result);
38 printf(
"No result\n");
44 main(int argc, char **argv) {
48 xmlChar *xpath = (xmlChar*)
"//keyword";
49 xmlNodeSetPtr nodeset;
50 xmlXPathObjectPtr result;
55 printf(
"Usage: %s docname\n", argv[
0]);
60 doc = getdoc(docname);
61 result = getnodeset (doc, xpath);
63 nodeset = result-
>nodesetval;
64 for (i=
0; i
< nodeset-
>nodeNr; i++) {
65 keyword = xmlNodeListGetString(doc, nodeset-
>nodeTab[i]-
>xmlChildrenNode,
1);
66 printf(
"keyword: %s\n", keyword);
69 xmlXPathFreeObject (result);
76 </p></div><div class=
"navfooter"><hr><table width=
"100%" summary=
"Navigation footer"><tr><td width=
"40%" align=
"left"><a accesskey=
"p" href=
"apc.html">Prev
</a> </td><td width=
"20%" align=
"center"><a accesskey=
"u" href=
"index.html">Up
</a></td><td width=
"40%" align=
"right"> <a accesskey=
"n" href=
"ape.html">Next
</a></td></tr><tr><td width=
"40%" align=
"left" valign=
"top">C. Code for Keyword Example
</td><td width=
"20%" align=
"center"><a accesskey=
"h" href=
"index.html">Home
</a></td><td width=
"40%" align=
"right" valign=
"top"> E. Code for Add Keyword Example
</td></tr></table></div></body></html>