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)
51 static int opt_verbose
;
52 static int node_exist
;
53 static bool result
= false;
57 * nodes: the nodes set.
58 * output: the output file handle.
60 * Print the node content to the file
62 static int print_xpath_nodes(xmlDocPtr doc
, xmlNodeSetPtr nodes
, FILE *output
)
69 xmlChar
*node_child_value_string
= NULL
;
72 size
= (nodes
) ? nodes
->nodeNr
: 0;
74 for (i
= 0; i
< size
; ++i
) {
75 assert(nodes
->nodeTab
[i
]);
77 if (nodes
->nodeTab
[i
]->type
== XML_NAMESPACE_DECL
) {
78 fprintf(stderr
, "ERR:%s\n",
79 "This executable does not support xml namespacing\n");
82 } else if (nodes
->nodeTab
[i
]->type
== XML_ELEMENT_NODE
) {
83 cur
= nodes
->nodeTab
[i
];
85 if (xmlChildElementCount(cur
) == 0) {
86 if (xmlNodeIsText(cur
->children
)) {
87 node_child_value_string
= xmlNodeListGetString(doc
,
91 } else if (opt_verbose
) {
92 fprintf(output
, "%s;%s;\n", cur
->name
,
93 node_child_value_string
);
95 fprintf(output
, "%s\n",
96 node_child_value_string
);
98 xmlFree(node_child_value_string
);
100 /* We don't want to print non-final element */
104 fprintf(stderr
, "ERR:%s\n",
105 "Xpath expression return non-final xml element");
114 /* We don't want to print non-final element */
115 fprintf(stderr
, "ERR:%s\n",
116 "Xpath expression return non-final xml element");
123 cur
= nodes
->nodeTab
[i
];
126 } else if (opt_verbose
) {
127 fprintf(output
, "%s;%s;\n", cur
->parent
->name
, cur
->content
);
129 fprintf(output
, "%s\n", cur
->content
);
133 /* Command Success */
140 static int register_lttng_namespace(xmlXPathContextPtr xpathCtx
)
146 prefix
= xmlCharStrdup("lttng");
152 ns
= xmlCharStrdup(DEFAULT_LTTNG_MI_NAMESPACE
);
158 ret
= xmlXPathRegisterNs(xpathCtx
, prefix
, ns
);
166 * Extract element corresponding to xpath
167 * xml_path The path to the xml file
168 * xpath: The xpath to evaluate.
170 * Evaluate an xpath expression onto an xml file.
171 * and print the result one by line.
173 * Returns 0 on success and a negative value otherwise.
175 static int extract_xpath(const char *xml_path
, const xmlChar
*xpath
)
178 xmlDocPtr doc
= NULL
;
179 xmlXPathContextPtr xpathCtx
= NULL
;
180 xmlXPathObjectPtr xpathObj
= NULL
;
185 /* Parse the xml file */
186 doc
= xmlParseFile(xml_path
);
188 fprintf(stderr
, "ERR parsing: xml file invalid \"%s\"\n", xml_path
);
192 /* Initialize a xpath context */
193 xpathCtx
= xmlXPathNewContext(doc
);
195 fprintf(stderr
, "ERR: XPath context invalid\n");
200 /* Register the LTTng MI namespace */
201 ret
= register_lttng_namespace(xpathCtx
);
203 fprintf(stderr
, "ERR: Could not register lttng namespace\n");
204 xmlXPathFreeContext(xpathCtx
);
209 /* Evaluate xpath expression */
210 xpathObj
= xmlXPathEvalExpression(xpath
, xpathCtx
);
212 fprintf(stderr
, "ERR: invalid xpath expression \"%s\"\n", xpath
);
213 xmlXPathFreeContext(xpathCtx
);
219 if (print_xpath_nodes(doc
, xpathObj
->nodesetval
, stdout
)) {
220 xmlXPathFreeObject(xpathObj
);
221 xmlXPathFreeContext(xpathCtx
);
225 if (node_exist
&& result
) {
226 fprintf(stdout
, "true\n");
230 xmlXPathFreeObject(xpathObj
);
231 xmlXPathFreeContext(xpathCtx
);
237 int main(int argc
, char **argv
)
241 /* Parse command line and process file */
242 while ((opt
= getopt(argc
, argv
, "ve")) != -1) {
255 if (!(optind
+ 1 < argc
)) {
256 fprintf(stderr
, "ERR:%s\n", "Arguments missing");
262 xmlKeepBlanksDefault(0);
263 if (access(argv
[optind
], F_OK
)) {
264 fprintf(stderr
, "ERR:%s\n", "Xml path not valid");
267 /* Do the main job */
268 if (extract_xpath(argv
[optind
], (xmlChar
*)argv
[optind
+1])) {
272 /* Shutdown libxml */
281 fprintf(stderr
, "XPath support not compiled in\n");