2 * Copyright (C) 2014 Jonathan Rajotte <jonathan.r.julien@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 * Usage: extract_xml [-v|-e] xml_path xpath_expression
10 * Evaluate XPath expression and prints result node set.
11 * args[1] path to the xml file
12 * args[2] xpath expression to extract
13 * If -e look if node exist return "true" else nothing
14 * If -v is set the name of the node will appear with his value delimited by
17 * Command:extract_xml ../file.xml /test/node/text()
33 #include <libxml/tree.h>
34 #include <libxml/parser.h>
35 #include <libxml/xpath.h>
36 #include <libxml/xpathInternals.h>
37 #include <common/defaults.h>
39 #if defined(LIBXML_XPATH_ENABLED)
41 static int opt_verbose
;
42 static int node_exist
;
43 static bool result
= false;
47 * nodes: the nodes set.
48 * output: the output file handle.
50 * Print the node content to the file
52 static int print_xpath_nodes(xmlDocPtr doc
, xmlNodeSetPtr nodes
, FILE *output
)
59 xmlChar
*node_child_value_string
= NULL
;
62 size
= (nodes
) ? nodes
->nodeNr
: 0;
64 for (i
= 0; i
< size
; ++i
) {
65 LTTNG_ASSERT(nodes
->nodeTab
[i
]);
67 if (nodes
->nodeTab
[i
]->type
== XML_NAMESPACE_DECL
) {
68 fprintf(stderr
, "ERR:%s\n",
69 "This executable does not support xml namespacing\n");
72 } else if (nodes
->nodeTab
[i
]->type
== XML_ELEMENT_NODE
) {
73 cur
= nodes
->nodeTab
[i
];
75 if (xmlChildElementCount(cur
) == 0) {
76 if (xmlNodeIsText(cur
->children
)) {
77 node_child_value_string
= xmlNodeListGetString(doc
,
81 } else if (opt_verbose
) {
82 fprintf(output
, "%s;%s;\n", cur
->name
,
83 node_child_value_string
);
85 fprintf(output
, "%s\n",
86 node_child_value_string
);
88 xmlFree(node_child_value_string
);
90 /* We don't want to print non-final element */
94 fprintf(stderr
, "ERR:%s\n",
95 "Xpath expression return non-final xml element");
104 /* We don't want to print non-final element */
105 fprintf(stderr
, "ERR:%s\n",
106 "Xpath expression return non-final xml element");
113 cur
= nodes
->nodeTab
[i
];
116 } else if (opt_verbose
) {
117 fprintf(output
, "%s;%s;\n", cur
->parent
->name
, cur
->content
);
119 fprintf(output
, "%s\n", cur
->content
);
123 /* Command Success */
130 static int register_lttng_namespace(xmlXPathContextPtr xpathCtx
)
136 prefix
= xmlCharStrdup("lttng");
142 ns
= xmlCharStrdup(DEFAULT_LTTNG_MI_NAMESPACE
);
148 ret
= xmlXPathRegisterNs(xpathCtx
, prefix
, ns
);
156 * Extract element corresponding to xpath
157 * xml_path The path to the xml file
158 * xpath: The xpath to evaluate.
160 * Evaluate an xpath expression onto an xml file.
161 * and print the result one by line.
163 * Returns 0 on success and a negative value otherwise.
165 static int extract_xpath(const char *xml_path
, const xmlChar
*xpath
)
168 xmlDocPtr doc
= NULL
;
169 xmlXPathContextPtr xpathCtx
= NULL
;
170 xmlXPathObjectPtr xpathObj
= NULL
;
172 LTTNG_ASSERT(xml_path
);
175 /* Parse the xml file */
176 doc
= xmlParseFile(xml_path
);
178 fprintf(stderr
, "ERR parsing: xml file invalid \"%s\"\n", xml_path
);
182 /* Initialize a xpath context */
183 xpathCtx
= xmlXPathNewContext(doc
);
185 fprintf(stderr
, "ERR: XPath context invalid\n");
190 /* Register the LTTng MI namespace */
191 ret
= register_lttng_namespace(xpathCtx
);
193 fprintf(stderr
, "ERR: Could not register lttng namespace\n");
194 xmlXPathFreeContext(xpathCtx
);
199 /* Evaluate xpath expression */
200 xpathObj
= xmlXPathEvalExpression(xpath
, xpathCtx
);
202 fprintf(stderr
, "ERR: invalid xpath expression \"%s\"\n", xpath
);
203 xmlXPathFreeContext(xpathCtx
);
209 if (print_xpath_nodes(doc
, xpathObj
->nodesetval
, stdout
)) {
210 xmlXPathFreeObject(xpathObj
);
211 xmlXPathFreeContext(xpathCtx
);
215 if (node_exist
&& result
) {
216 fprintf(stdout
, "true\n");
220 xmlXPathFreeObject(xpathObj
);
221 xmlXPathFreeContext(xpathCtx
);
227 int main(int argc
, char **argv
)
231 /* Parse command line and process file */
232 while ((opt
= getopt(argc
, argv
, "ve")) != -1) {
245 if (!(optind
+ 1 < argc
)) {
246 fprintf(stderr
, "ERR:%s\n", "Arguments missing");
252 xmlKeepBlanksDefault(0);
253 if (access(argv
[optind
], F_OK
)) {
254 fprintf(stderr
, "ERR:%s\n", "Xml path not valid");
257 /* Do the main job */
258 if (extract_xpath(argv
[optind
], (xmlChar
*)argv
[optind
+1])) {
262 /* Shutdown libxml */
271 fprintf(stderr
, "XPath support not compiled in\n");