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()
29 #include <common/defaults.hpp>
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
33 #include <libxml/xpath.h>
34 #include <libxml/xpathInternals.h>
41 #if defined(LIBXML_XPATH_ENABLED)
43 static int opt_verbose
;
44 static int node_exist
;
45 static bool result
= false;
49 * nodes: the nodes set.
50 * output: the output file handle.
52 * Print the node content to the file
54 static int print_xpath_nodes(xmlDocPtr doc
, xmlNodeSetPtr nodes
, FILE *output
)
61 xmlChar
*node_child_value_string
= nullptr;
64 size
= (nodes
) ? nodes
->nodeNr
: 0;
66 for (i
= 0; i
< size
; ++i
) {
67 LTTNG_ASSERT(nodes
->nodeTab
[i
]);
69 if (nodes
->nodeTab
[i
]->type
== XML_NAMESPACE_DECL
) {
72 "This executable does not support xml namespacing\n");
75 } else if (nodes
->nodeTab
[i
]->type
== XML_ELEMENT_NODE
) {
76 cur
= nodes
->nodeTab
[i
];
78 if (xmlChildElementCount(cur
) == 0) {
79 if (xmlNodeIsText(cur
->children
)) {
80 node_child_value_string
=
81 xmlNodeListGetString(doc
, cur
->children
, 1);
84 } else if (opt_verbose
) {
88 node_child_value_string
);
90 fprintf(output
, "%s\n", node_child_value_string
);
92 xmlFree(node_child_value_string
);
94 /* We don't want to print non-final element */
100 "Xpath expression return non-final xml element");
109 /* We don't want to print non-final element */
112 "Xpath expression return non-final xml element");
119 cur
= nodes
->nodeTab
[i
];
122 } else if (opt_verbose
) {
123 fprintf(output
, "%s;%s;\n", cur
->parent
->name
, cur
->content
);
125 fprintf(output
, "%s\n", cur
->content
);
129 /* Command Success */
136 static int register_lttng_namespace(xmlXPathContextPtr xpathCtx
)
140 xmlChar
*ns
= nullptr;
142 prefix
= xmlCharStrdup("lttng");
148 ns
= xmlCharStrdup(DEFAULT_LTTNG_MI_NAMESPACE
);
154 ret
= xmlXPathRegisterNs(xpathCtx
, prefix
, ns
);
162 * Extract element corresponding to xpath
163 * xml_path The path to the xml file
164 * xpath: The xpath to evaluate.
166 * Evaluate an xpath expression onto an xml file.
167 * and print the result one by line.
169 * Returns 0 on success and a negative value otherwise.
171 static int extract_xpath(const char *xml_path
, const xmlChar
*xpath
)
174 xmlDocPtr doc
= nullptr;
175 xmlXPathContextPtr xpathCtx
= nullptr;
176 xmlXPathObjectPtr xpathObj
= nullptr;
178 LTTNG_ASSERT(xml_path
);
181 xml_parser_ctx_uptr parserCtx
{ xmlNewParserCtxt() };
184 fprintf(stderr
, "ERR: could not allocate an XML parser context\n");
188 /* Parse the xml file */
189 doc
= xmlCtxtReadFile(parserCtx
.get(), xml_path
, nullptr, XML_PARSE_NOBLANKS
);
191 fprintf(stderr
, "ERR parsing: xml file invalid \"%s\"\n", xml_path
);
195 /* Initialize a xpath context */
196 xpathCtx
= xmlXPathNewContext(doc
);
198 fprintf(stderr
, "ERR: XPath context invalid\n");
203 /* Register the LTTng MI namespace */
204 ret
= register_lttng_namespace(xpathCtx
);
206 fprintf(stderr
, "ERR: Could not register lttng namespace\n");
207 xmlXPathFreeContext(xpathCtx
);
212 /* Evaluate xpath expression */
213 xpathObj
= xmlXPathEvalExpression(xpath
, xpathCtx
);
215 fprintf(stderr
, "ERR: invalid xpath expression \"%s\"\n", xpath
);
216 xmlXPathFreeContext(xpathCtx
);
222 if (print_xpath_nodes(doc
, xpathObj
->nodesetval
, stdout
)) {
223 xmlXPathFreeObject(xpathObj
);
224 xmlXPathFreeContext(xpathCtx
);
228 if (node_exist
&& result
) {
229 fprintf(stdout
, "true\n");
233 xmlXPathFreeObject(xpathObj
);
234 xmlXPathFreeContext(xpathCtx
);
240 int main(int argc
, char **argv
)
244 /* Parse command line and process file */
245 while ((opt
= getopt(argc
, argv
, "ve")) != -1) {
258 if (!(optind
+ 1 < argc
)) {
259 fprintf(stderr
, "ERR:%s\n", "Arguments missing");
265 if (access(argv
[optind
], F_OK
)) {
266 fprintf(stderr
, "ERR:%s\n", "Xml path not valid");
269 /* Do the main job */
270 if (extract_xpath(argv
[optind
], (xmlChar
*) argv
[optind
+ 1])) {
274 /* Shutdown libxml */
283 fprintf(stderr
, "XPath support not compiled in\n");