2 * Copyright (C) 2014 EfficiOS Inc.
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()
27 #include <common/defaults.hpp>
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
31 #include <libxml/xpath.h>
32 #include <libxml/xpathInternals.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
= nullptr;
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
) {
70 "This executable does not support xml namespacing\n");
73 } else if (nodes
->nodeTab
[i
]->type
== XML_ELEMENT_NODE
) {
74 cur
= nodes
->nodeTab
[i
];
76 if (xmlChildElementCount(cur
) == 0) {
77 if (xmlNodeIsText(cur
->children
)) {
78 node_child_value_string
=
79 xmlNodeListGetString(doc
, cur
->children
, 1);
82 } else if (opt_verbose
) {
86 node_child_value_string
);
88 fprintf(output
, "%s\n", node_child_value_string
);
90 xmlFree(node_child_value_string
);
92 /* We don't want to print non-final element */
98 "Xpath expression return non-final xml element");
107 /* We don't want to print non-final element */
110 "Xpath expression return non-final xml element");
117 cur
= nodes
->nodeTab
[i
];
120 } else if (opt_verbose
) {
121 fprintf(output
, "%s;%s;\n", cur
->parent
->name
, cur
->content
);
123 fprintf(output
, "%s\n", cur
->content
);
127 /* Command Success */
134 static int register_lttng_namespace(xmlXPathContextPtr xpathCtx
)
138 xmlChar
*ns
= nullptr;
140 prefix
= xmlCharStrdup("lttng");
146 ns
= xmlCharStrdup(DEFAULT_LTTNG_MI_NAMESPACE
);
152 ret
= xmlXPathRegisterNs(xpathCtx
, prefix
, ns
);
160 * Extract element corresponding to xpath
161 * xml_path The path to the xml file
162 * xpath: The xpath to evaluate.
164 * Evaluate an xpath expression onto an xml file.
165 * and print the result one by line.
167 * Returns 0 on success and a negative value otherwise.
169 static int extract_xpath(const char *xml_path
, const xmlChar
*xpath
)
172 xmlDocPtr doc
= nullptr;
173 xmlXPathContextPtr xpathCtx
= nullptr;
174 xmlXPathObjectPtr xpathObj
= nullptr;
176 LTTNG_ASSERT(xml_path
);
179 /* Parse the xml file */
180 doc
= xmlParseFile(xml_path
);
182 fprintf(stderr
, "ERR parsing: xml file invalid \"%s\"\n", xml_path
);
186 /* Initialize a xpath context */
187 xpathCtx
= xmlXPathNewContext(doc
);
189 fprintf(stderr
, "ERR: XPath context invalid\n");
194 /* Register the LTTng MI namespace */
195 ret
= register_lttng_namespace(xpathCtx
);
197 fprintf(stderr
, "ERR: Could not register lttng namespace\n");
198 xmlXPathFreeContext(xpathCtx
);
203 /* Evaluate xpath expression */
204 xpathObj
= xmlXPathEvalExpression(xpath
, xpathCtx
);
206 fprintf(stderr
, "ERR: invalid xpath expression \"%s\"\n", xpath
);
207 xmlXPathFreeContext(xpathCtx
);
213 if (print_xpath_nodes(doc
, xpathObj
->nodesetval
, stdout
)) {
214 xmlXPathFreeObject(xpathObj
);
215 xmlXPathFreeContext(xpathCtx
);
219 if (node_exist
&& result
) {
220 fprintf(stdout
, "true\n");
224 xmlXPathFreeObject(xpathObj
);
225 xmlXPathFreeContext(xpathCtx
);
231 int main(int argc
, char **argv
)
235 /* Parse command line and process file */
236 while ((opt
= getopt(argc
, argv
, "ve")) != -1) {
249 if (!(optind
+ 1 < argc
)) {
250 fprintf(stderr
, "ERR:%s\n", "Arguments missing");
256 xmlKeepBlanksDefault(0);
257 if (access(argv
[optind
], F_OK
)) {
258 fprintf(stderr
, "ERR:%s\n", "Xml path not valid");
261 /* Do the main job */
262 if (extract_xpath(argv
[optind
], (xmlChar
*) argv
[optind
+ 1])) {
266 /* Shutdown libxml */
275 fprintf(stderr
, "XPath support not compiled in\n");