2 * Copyright (C) 2014 - Jonathan Rajotte <jonathan.r.julien@gmail.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Usage: extract_xml [-v|-e] xml_path xpath_expression
19 * Evaluate XPath expression and prints result node set.
20 * args[1] path to the xml file
21 * args[2] xpath expression to extract
22 * If -e look if node exist return "true" else nothing
23 * If -v is set the name of the node will appear with his value delimited by
26 * Command:extract_xml ../file.xml /test/node/text()
43 #include <libxml/tree.h>
44 #include <libxml/parser.h>
45 #include <libxml/xpath.h>
46 #include <libxml/xpathInternals.h>
47 #include <common/defaults.h>
49 #if defined(LIBXML_XPATH_ENABLED)
52 static int opt_verbose
;
53 static int node_exist
;
54 static bool result
= false;
58 * nodes: the nodes set.
59 * output: the output file handle.
61 * Print the node content to the file
63 static int print_xpath_nodes(xmlDocPtr doc
, xmlNodeSetPtr nodes
, FILE *output
)
70 xmlChar
*node_child_value_string
= NULL
;
73 size
= (nodes
) ? nodes
->nodeNr
: 0;
75 for (i
= 0; i
< size
; ++i
) {
76 assert(nodes
->nodeTab
[i
]);
78 if (nodes
->nodeTab
[i
]->type
== XML_NAMESPACE_DECL
) {
79 fprintf(stderr
, "ERR:%s\n",
80 "This executable does not support xml namespacing\n");
83 } else if (nodes
->nodeTab
[i
]->type
== XML_ELEMENT_NODE
) {
84 cur
= nodes
->nodeTab
[i
];
86 if (xmlChildElementCount(cur
) == 0) {
87 if (xmlNodeIsText(cur
->children
)) {
88 node_child_value_string
= xmlNodeListGetString(doc
,
92 } else if (opt_verbose
) {
93 fprintf(output
, "%s;%s;\n", cur
->name
,
94 node_child_value_string
);
96 fprintf(output
, "%s\n",
97 node_child_value_string
);
99 xmlFree(node_child_value_string
);
101 /* We don't want to print non-final element */
105 fprintf(stderr
, "ERR:%s\n",
106 "Xpath expression return non-final xml element");
115 /* We don't want to print non-final element */
116 fprintf(stderr
, "ERR:%s\n",
117 "Xpath expression return non-final xml element");
124 cur
= nodes
->nodeTab
[i
];
127 } else if (opt_verbose
) {
128 fprintf(output
, "%s;%s;\n", cur
->parent
->name
, cur
->content
);
130 fprintf(output
, "%s\n", cur
->content
);
134 /* Command Success */
141 static int register_lttng_namespace(xmlXPathContextPtr xpathCtx
)
147 prefix
= xmlCharStrdup("lttng");
153 ns
= xmlCharStrdup(DEFAULT_LTTNG_MI_NAMESPACE
);
159 ret
= xmlXPathRegisterNs(xpathCtx
, prefix
, ns
);
167 * Extract element corresponding to xpath
168 * xml_path The path to the xml file
169 * xpath: The xpath to evaluate.
171 * Evaluate an xpath expression onto an xml file.
172 * and print the result one by line.
174 * Returns 0 on success and a negative value otherwise.
176 static int extract_xpath(const char *xml_path
, const xmlChar
*xpath
)
179 xmlDocPtr doc
= NULL
;
180 xmlXPathContextPtr xpathCtx
= NULL
;
181 xmlXPathObjectPtr xpathObj
= NULL
;
186 /* Parse the xml file */
187 doc
= xmlParseFile(xml_path
);
189 fprintf(stderr
, "ERR parsing: xml file invalid \"%s\"\n", xml_path
);
193 /* Initialize a xpath context */
194 xpathCtx
= xmlXPathNewContext(doc
);
196 fprintf(stderr
, "ERR: XPath context invalid\n");
201 /* Register the LTTng MI namespace */
202 ret
= register_lttng_namespace(xpathCtx
);
204 fprintf(stderr
, "ERR: Could not register lttng namespace\n");
205 xmlXPathFreeContext(xpathCtx
);
210 /* Evaluate xpath expression */
211 xpathObj
= xmlXPathEvalExpression(xpath
, xpathCtx
);
213 fprintf(stderr
, "ERR: invalid xpath expression \"%s\"\n", xpath
);
214 xmlXPathFreeContext(xpathCtx
);
220 if (print_xpath_nodes(doc
, xpathObj
->nodesetval
, stdout
)) {
221 xmlXPathFreeObject(xpathObj
);
222 xmlXPathFreeContext(xpathCtx
);
226 if (node_exist
&& result
) {
227 fprintf(stdout
, "true\n");
231 xmlXPathFreeObject(xpathObj
);
232 xmlXPathFreeContext(xpathCtx
);
238 int main(int argc
, char **argv
)
242 /* Parse command line and process file */
243 while ((opt
= getopt(argc
, argv
, "ve")) != -1) {
256 if (!(optind
+ 1 < argc
)) {
257 fprintf(stderr
, "ERR:%s\n", "Arguments missing");
263 xmlKeepBlanksDefault(0);
264 if (access(argv
[optind
], F_OK
)) {
265 fprintf(stderr
, "ERR:%s\n", "Xml path not valid");
268 /* Do the main job */
269 if (extract_xpath(argv
[optind
], (xmlChar
*)argv
[optind
+1])) {
273 /* Shutdown libxml */
282 fprintf(stderr
, "XPath support not compiled in\n");