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()
34 #include <libxml/tree.h>
35 #include <libxml/parser.h>
36 #include <libxml/xpath.h>
37 #include <libxml/xpathInternals.h>
38 #include <common/defaults.h>
40 #if defined(LIBXML_XPATH_ENABLED)
42 static int opt_verbose
;
43 static int node_exist
;
44 static bool result
= false;
48 * nodes: the nodes set.
49 * output: the output file handle.
51 * Print the node content to the file
53 static int print_xpath_nodes(xmlDocPtr doc
, xmlNodeSetPtr nodes
, FILE *output
)
60 xmlChar
*node_child_value_string
= NULL
;
63 size
= (nodes
) ? nodes
->nodeNr
: 0;
65 for (i
= 0; i
< size
; ++i
) {
66 assert(nodes
->nodeTab
[i
]);
68 if (nodes
->nodeTab
[i
]->type
== XML_NAMESPACE_DECL
) {
69 fprintf(stderr
, "ERR:%s\n",
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
= xmlNodeListGetString(doc
,
82 } else if (opt_verbose
) {
83 fprintf(output
, "%s;%s;\n", cur
->name
,
84 node_child_value_string
);
86 fprintf(output
, "%s\n",
87 node_child_value_string
);
89 xmlFree(node_child_value_string
);
91 /* We don't want to print non-final element */
95 fprintf(stderr
, "ERR:%s\n",
96 "Xpath expression return non-final xml element");
105 /* We don't want to print non-final element */
106 fprintf(stderr
, "ERR:%s\n",
107 "Xpath expression return non-final xml element");
114 cur
= nodes
->nodeTab
[i
];
117 } else if (opt_verbose
) {
118 fprintf(output
, "%s;%s;\n", cur
->parent
->name
, cur
->content
);
120 fprintf(output
, "%s\n", cur
->content
);
124 /* Command Success */
131 static int register_lttng_namespace(xmlXPathContextPtr xpathCtx
)
137 prefix
= xmlCharStrdup("lttng");
143 ns
= xmlCharStrdup(DEFAULT_LTTNG_MI_NAMESPACE
);
149 ret
= xmlXPathRegisterNs(xpathCtx
, prefix
, ns
);
157 * Extract element corresponding to xpath
158 * xml_path The path to the xml file
159 * xpath: The xpath to evaluate.
161 * Evaluate an xpath expression onto an xml file.
162 * and print the result one by line.
164 * Returns 0 on success and a negative value otherwise.
166 static int extract_xpath(const char *xml_path
, const xmlChar
*xpath
)
169 xmlDocPtr doc
= NULL
;
170 xmlXPathContextPtr xpathCtx
= NULL
;
171 xmlXPathObjectPtr xpathObj
= NULL
;
176 /* Parse the xml file */
177 doc
= xmlParseFile(xml_path
);
179 fprintf(stderr
, "ERR parsing: xml file invalid \"%s\"\n", xml_path
);
183 /* Initialize a xpath context */
184 xpathCtx
= xmlXPathNewContext(doc
);
186 fprintf(stderr
, "ERR: XPath context invalid\n");
191 /* Register the LTTng MI namespace */
192 ret
= register_lttng_namespace(xpathCtx
);
194 fprintf(stderr
, "ERR: Could not register lttng namespace\n");
195 xmlXPathFreeContext(xpathCtx
);
200 /* Evaluate xpath expression */
201 xpathObj
= xmlXPathEvalExpression(xpath
, xpathCtx
);
203 fprintf(stderr
, "ERR: invalid xpath expression \"%s\"\n", xpath
);
204 xmlXPathFreeContext(xpathCtx
);
210 if (print_xpath_nodes(doc
, xpathObj
->nodesetval
, stdout
)) {
211 xmlXPathFreeObject(xpathObj
);
212 xmlXPathFreeContext(xpathCtx
);
216 if (node_exist
&& result
) {
217 fprintf(stdout
, "true\n");
221 xmlXPathFreeObject(xpathObj
);
222 xmlXPathFreeContext(xpathCtx
);
228 int main(int argc
, char **argv
)
232 /* Parse command line and process file */
233 while ((opt
= getopt(argc
, argv
, "ve")) != -1) {
246 if (!(optind
+ 1 < argc
)) {
247 fprintf(stderr
, "ERR:%s\n", "Arguments missing");
253 xmlKeepBlanksDefault(0);
254 if (access(argv
[optind
], F_OK
)) {
255 fprintf(stderr
, "ERR:%s\n", "Xml path not valid");
258 /* Do the main job */
259 if (extract_xpath(argv
[optind
], (xmlChar
*)argv
[optind
+1])) {
263 /* Shutdown libxml */
272 fprintf(stderr
, "XPath support not compiled in\n");