1 /**
2 * section: XPath
3 * synopsis: Evaluate XPath expression and prints result node set.
4 * purpose: Shows how to evaluate XPath expression and register
5 * known namespaces in XPath context.
6 * usage: xpath1 <xml-file> <xpath-expr> [<known-ns-list>]
7 * test: xpath1 test3.xml '//child2' > xpath1.tmp && diff xpath1.tmp $(srcdir)/xpath1.res
8 * author: Aleksey Sanin
9 * copy: see Copyright for the status of this software.
10 */
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <assert.h>
15
16 #include <libxml/tree.h>
17 #include <libxml/parser.h>
18 #include <libxml/xpath.h>
19 #include <libxml/xpathInternals.h>
20
21 #if defined(LIBXML_XPATH_ENABLED) && defined(LIBXML_SAX1_ENABLED)
22
23
24 static void usage(const char *name);
25 int execute_xpath_expression(const char* filename, const xmlChar* xpathExpr, const xmlChar* nsList);
26 int register_namespaces(xmlXPathContextPtr xpathCtx, const xmlChar* nsList);
27 void print_xpath_nodes(xmlNodeSetPtr nodes, FILE* output);
28
29 int
main(int argc,char ** argv)30 main(int argc, char **argv) {
31 /* Parse command line and process file */
32 if((argc < 3) || (argc > 4)) {
33 fprintf(stderr, "Error: wrong number of arguments.\n");
34 usage(argv[0]);
35 return(-1);
36 }
37
38 /* Init libxml */
39 xmlInitParser();
40 LIBXML_TEST_VERSION
41
42 /* Do the main job */
43 if(execute_xpath_expression(argv[1], BAD_CAST argv[2], (argc > 3) ? BAD_CAST argv[3] : NULL) < 0) {
44 usage(argv[0]);
45 return(-1);
46 }
47
48 return 0;
49 }
50
51 /**
52 * usage:
53 * @name: the program name.
54 *
55 * Prints usage information.
56 */
57 static void
usage(const char * name)58 usage(const char *name) {
59 assert(name);
60
61 fprintf(stderr, "Usage: %s <xml-file> <xpath-expr> [<known-ns-list>]\n", name);
62 fprintf(stderr, "where <known-ns-list> is a list of known namespaces\n");
63 fprintf(stderr, "in \"<prefix1>=<href1> <prefix2>=href2> ...\" format\n");
64 }
65
66 /**
67 * execute_xpath_expression:
68 * @filename: the input XML filename.
69 * @xpathExpr: the xpath expression for evaluation.
70 * @nsList: the optional list of known namespaces in
71 * "<prefix1>=<href1> <prefix2>=href2> ..." format.
72 *
73 * Parses input XML file, evaluates XPath expression and prints results.
74 *
75 * Returns 0 on success and a negative value otherwise.
76 */
77 int
execute_xpath_expression(const char * filename,const xmlChar * xpathExpr,const xmlChar * nsList)78 execute_xpath_expression(const char* filename, const xmlChar* xpathExpr, const xmlChar* nsList) {
79 xmlDocPtr doc;
80 xmlXPathContextPtr xpathCtx;
81 xmlXPathObjectPtr xpathObj;
82
83 assert(filename);
84 assert(xpathExpr);
85
86 /* Load XML document */
87 doc = xmlParseFile(filename);
88 if (doc == NULL) {
89 fprintf(stderr, "Error: unable to parse file \"%s\"\n", filename);
90 return(-1);
91 }
92
93 /* Create xpath evaluation context */
94 xpathCtx = xmlXPathNewContext(doc);
95 if(xpathCtx == NULL) {
96 fprintf(stderr,"Error: unable to create new XPath context\n");
97 xmlFreeDoc(doc);
98 return(-1);
99 }
100
101 /* Register namespaces from list (if any) */
102 if((nsList != NULL) && (register_namespaces(xpathCtx, nsList) < 0)) {
103 fprintf(stderr,"Error: failed to register namespaces list \"%s\"\n", nsList);
104 xmlXPathFreeContext(xpathCtx);
105 xmlFreeDoc(doc);
106 return(-1);
107 }
108
109 /* Evaluate xpath expression */
110 xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
111 if(xpathObj == NULL) {
112 fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr);
113 xmlXPathFreeContext(xpathCtx);
114 xmlFreeDoc(doc);
115 return(-1);
116 }
117
118 /* Print results */
119 print_xpath_nodes(xpathObj->nodesetval, stdout);
120
121 /* Cleanup */
122 xmlXPathFreeObject(xpathObj);
123 xmlXPathFreeContext(xpathCtx);
124 xmlFreeDoc(doc);
125
126 return(0);
127 }
128
129 /**
130 * register_namespaces:
131 * @xpathCtx: the pointer to an XPath context.
132 * @nsList: the list of known namespaces in
133 * "<prefix1>=<href1> <prefix2>=href2> ..." format.
134 *
135 * Registers namespaces from @nsList in @xpathCtx.
136 *
137 * Returns 0 on success and a negative value otherwise.
138 */
139 int
register_namespaces(xmlXPathContextPtr xpathCtx,const xmlChar * nsList)140 register_namespaces(xmlXPathContextPtr xpathCtx, const xmlChar* nsList) {
141 xmlChar* nsListDup;
142 xmlChar* prefix;
143 xmlChar* href;
144 xmlChar* next;
145
146 assert(xpathCtx);
147 assert(nsList);
148
149 nsListDup = xmlStrdup(nsList);
150 if(nsListDup == NULL) {
151 fprintf(stderr, "Error: unable to strdup namespaces list\n");
152 return(-1);
153 }
154
155 next = nsListDup;
156 while(next != NULL) {
157 /* skip spaces */
158 while((*next) == ' ') next++;
159 if((*next) == '\0') break;
160
161 /* find prefix */
162 prefix = next;
163 next = (xmlChar*)xmlStrchr(next, '=');
164 if(next == NULL) {
165 fprintf(stderr,"Error: invalid namespaces list format\n");
166 xmlFree(nsListDup);
167 return(-1);
168 }
169 *(next++) = '\0';
170
171 /* find href */
172 href = next;
173 next = (xmlChar*)xmlStrchr(next, ' ');
174 if(next != NULL) {
175 *(next++) = '\0';
176 }
177
178 /* do register namespace */
179 if(xmlXPathRegisterNs(xpathCtx, prefix, href) != 0) {
180 fprintf(stderr,"Error: unable to register NS with prefix=\"%s\" and href=\"%s\"\n", prefix, href);
181 xmlFree(nsListDup);
182 return(-1);
183 }
184 }
185
186 xmlFree(nsListDup);
187 return(0);
188 }
189
190 /**
191 * print_xpath_nodes:
192 * @nodes: the nodes set.
193 * @output: the output file handle.
194 *
195 * Prints the @nodes content to @output.
196 */
197 void
print_xpath_nodes(xmlNodeSetPtr nodes,FILE * output)198 print_xpath_nodes(xmlNodeSetPtr nodes, FILE* output) {
199 xmlNodePtr cur;
200 int size;
201 int i;
202
203 assert(output);
204 size = (nodes) ? nodes->nodeNr : 0;
205
206 fprintf(output, "Result (%d nodes):\n", size);
207 for(i = 0; i < size; ++i) {
208 assert(nodes->nodeTab[i]);
209
210 if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
211 xmlNsPtr ns;
212
213 ns = (xmlNsPtr)nodes->nodeTab[i];
214 cur = (xmlNodePtr)ns->next;
215 if(cur->ns) {
216 fprintf(output, "= namespace \"%s\"=\"%s\" for node %s:%s\n",
217 ns->prefix, ns->href, cur->ns->href, cur->name);
218 } else {
219 fprintf(output, "= namespace \"%s\"=\"%s\" for node %s\n",
220 ns->prefix, ns->href, cur->name);
221 }
222 } else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
223 cur = nodes->nodeTab[i];
224 if(cur->ns) {
225 fprintf(output, "= element node \"%s:%s\"\n",
226 cur->ns->href, cur->name);
227 } else {
228 fprintf(output, "= element node \"%s\"\n",
229 cur->name);
230 }
231 } else {
232 cur = nodes->nodeTab[i];
233 fprintf(output, "= node \"%s\": type %d\n", cur->name, cur->type);
234 }
235 }
236 }
237
238 #else
main(void)239 int main(void) {
240 fprintf(stderr, "XPath support not compiled in\n");
241 return 0;
242 }
243 #endif
244