47 int lpc, max = numXpathResults(xpathObj);
49 if (xpathObj == NULL) {
53 for (lpc = 0; lpc < max; lpc++) {
54 if (xpathObj->nodesetval->nodeTab[lpc] && xpathObj->nodesetval->nodeTab[lpc]->type != XML_NAMESPACE_DECL) {
55 xpathObj->nodesetval->nodeTab[lpc] = NULL;
60 xmlXPathFreeObject(xpathObj);
66 xmlNode *match = NULL;
67 int max = numXpathResults(xpathObj);
73 crm_err(
"Requested index %d of only %d items", index, max);
76 }
else if(xpathObj->nodesetval->nodeTab[index] == NULL) {
81 match = xpathObj->nodesetval->nodeTab[index];
84 if (xpathObj->nodesetval->nodeTab[index]->type != XML_NAMESPACE_DECL) {
86 xpathObj->nodesetval->nodeTab[index] = NULL;
89 if (match->type == XML_DOCUMENT_NODE) {
91 match = match->children;
93 }
else if (match->type != XML_ELEMENT_NODE
94 && match->parent && match->parent->type == XML_ELEMENT_NODE) {
96 match = match->parent;
98 }
else if (match->type != XML_ELEMENT_NODE) {
100 crm_err(
"We only support %d not %d", XML_ELEMENT_NODE, match->type);
109 int lpc, max = numXpathResults(xpathObj);
111 if (xpathObj == NULL) {
115 for (lpc = 0; lpc < max; lpc++) {
117 gboolean dedup = FALSE;
119 if (xpathObj->nodesetval->nodeTab[lpc] == NULL) {
123 xml = xpathObj->nodesetval->nodeTab[lpc]->parent;
125 for (; xml; xml = xml->parent) {
128 for (lpc2 = 0; lpc2 < max; lpc2++) {
129 if (xpathObj->nodesetval->nodeTab[lpc2] == xml) {
130 xpathObj->nodesetval->nodeTab[lpc] = NULL;
147 xmlDocPtr doc = NULL;
148 xmlXPathObjectPtr xpathObj = NULL;
149 xmlXPathContextPtr xpathCtx = NULL;
150 const xmlChar *xpathExpr = (
const xmlChar *)path;
154 CRM_CHECK(strlen(path) > 0,
return NULL);
158 xpathCtx = xmlXPathNewContext(doc);
161 xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
162 xmlXPathFreeContext(xpathCtx);
180 void (*helper)(xmlNode*,
void*),
void *user_data)
183 int nresults = numXpathResults(xpathObj);
186 for (i = 0; i < nresults; i++) {
191 (*helper)(result, user_data);
201 xmlNode *result = NULL;
202 char *xpath_full = NULL;
203 char *xpath_prefix = NULL;
205 if (xml_obj == NULL || xpath == NULL) {
209 xpath_prefix = (
char *)xmlGetNodePath(xml_obj);
211 len = strlen(xpath_prefix) + strlen(xpath) + 1;
212 xpath_full = malloc(len);
213 strcpy(xpath_full, xpath_prefix);
214 strcat(xpath_full, xpath);
227 xmlNode *result = NULL;
228 xmlXPathObjectPtr xpathObj = NULL;
229 char *nodePath = NULL;
230 char *matchNodePath = NULL;
237 nodePath = (
char *)xmlGetNodePath(xml_obj);
238 max = numXpathResults(xpathObj);
244 }
else if (max > 1) {
247 do_crm_log(error_level,
"Too many matches for %s in %s", xpath,
crm_str(nodePath));
249 for (lpc = 0; lpc < max; lpc++) {
254 matchNodePath = (
char *)xmlGetNodePath(match);
#define CRM_CHECK(expr, failure_action)
xmlNode * get_xpath_object_relative(const char *xpath, xmlNode *xml_obj, int error_level)
#define CRM_LOG_ASSERT(expr)
void dedupXpathResults(xmlXPathObjectPtr xpathObj)
xmlNode * get_xpath_object(const char *xpath, xmlNode *xml_obj, int error_level)
xmlDoc * getDocPtr(xmlNode *node)
#define do_crm_log(level, fmt, args...)
Log a message.
#define crm_log_xml_explicit(xml, text)
#define crm_err(fmt, args...)
xmlXPathObjectPtr xpath_search(xmlNode *xml_top, const char *path)
xmlNode * getXpathResult(xmlXPathObjectPtr xpathObj, int index)
void crm_foreach_xpath_result(xmlNode *xml, const char *xpath, void(*helper)(xmlNode *, void *), void *user_data)
Run a supplied function for each result of an xpath search.
void freeXpathObject(xmlXPathObjectPtr xpathObj)