40 int lpc, max = numXpathResults(xpathObj);
42 if (xpathObj == NULL) {
46 for (lpc = 0; lpc < max; lpc++) {
47 if (xpathObj->nodesetval->nodeTab[lpc] && xpathObj->nodesetval->nodeTab[lpc]->type != XML_NAMESPACE_DECL) {
48 xpathObj->nodesetval->nodeTab[lpc] = NULL;
53 xmlXPathFreeObject(xpathObj);
59 xmlNode *match = NULL;
60 int max = numXpathResults(xpathObj);
66 crm_err(
"Requested index %d of only %d items", index, max);
69 }
else if(xpathObj->nodesetval->nodeTab[index] == NULL) {
74 match = xpathObj->nodesetval->nodeTab[index];
77 if (xpathObj->nodesetval->nodeTab[index]->type != XML_NAMESPACE_DECL) {
79 xpathObj->nodesetval->nodeTab[index] = NULL;
82 if (match->type == XML_DOCUMENT_NODE) {
84 match = match->children;
86 }
else if (match->type != XML_ELEMENT_NODE
87 && match->parent && match->parent->type == XML_ELEMENT_NODE) {
89 match = match->parent;
91 }
else if (match->type != XML_ELEMENT_NODE) {
93 crm_err(
"We only support %d not %d", XML_ELEMENT_NODE, match->type);
102 int lpc, max = numXpathResults(xpathObj);
104 if (xpathObj == NULL) {
108 for (lpc = 0; lpc < max; lpc++) {
110 gboolean dedup = FALSE;
112 if (xpathObj->nodesetval->nodeTab[lpc] == NULL) {
116 xml = xpathObj->nodesetval->nodeTab[lpc]->parent;
118 for (; xml; xml = xml->parent) {
121 for (lpc2 = 0; lpc2 < max; lpc2++) {
122 if (xpathObj->nodesetval->nodeTab[lpc2] == xml) {
123 xpathObj->nodesetval->nodeTab[lpc] = NULL;
140 xmlDocPtr doc = NULL;
141 xmlXPathObjectPtr xpathObj = NULL;
142 xmlXPathContextPtr xpathCtx = NULL;
147 CRM_CHECK(strlen(path) > 0,
return NULL);
151 xpathCtx = xmlXPathNewContext(doc);
154 xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
155 xmlXPathFreeContext(xpathCtx);
173 void (*helper)(xmlNode*,
void*),
void *user_data)
176 int nresults = numXpathResults(xpathObj);
179 for (i = 0; i < nresults; i++) {
184 (*helper)(result, user_data);
193 xmlNode *result = NULL;
194 char *xpath_full = NULL;
195 char *xpath_prefix = NULL;
197 if (xml_obj == NULL || xpath == NULL) {
201 xpath_prefix = (
char *)xmlGetNodePath(xml_obj);
216 xmlNode *result = NULL;
217 xmlXPathObjectPtr xpathObj = NULL;
218 char *nodePath = NULL;
219 char *matchNodePath = NULL;
226 nodePath = (
char *)xmlGetNodePath(xml_obj);
227 max = numXpathResults(xpathObj);
231 do_crm_log(error_level,
"No match for %s in %s",
236 }
else if (max > 1) {
240 do_crm_log(error_level,
"Too many matches for %s in %s",
243 for (lpc = 0; lpc < max; lpc++) {
248 matchNodePath = (
char *) xmlGetNodePath(match);
250 xpath, lpc,
crm_str(matchNodePath));
269 int offset,
size_t buffer_size)
271 const char *
id =
ID(xml);
273 if(offset == 0 && prefix == NULL && xml->parent) {
279 offset += snprintf(buffer + offset, buffer_size - offset,
280 "/%s[@id='%s']", (
const char *) xml->name,
id);
281 }
else if(xml->name) {
282 offset += snprintf(buffer + offset, buffer_size - offset,
283 "/%s", (
const char *) xml->name);
296 return strdup(buffer);
#define CRM_CHECK(expr, failure_action)
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)
xmlNode * get_xpath_object_relative(const char *xpath, xmlNode *xml_obj, int error_level)
#define CRM_LOG_ASSERT(expr)
xmlDoc * getDocPtr(xmlNode *node)
xmlXPathObjectPtr xpath_search(xmlNode *xml_top, const char *path)
#define do_crm_log(level, fmt, args...)
Log a message.
#define crm_log_xml_explicit(xml, text)
xmlNode * getXpathResult(xmlXPathObjectPtr xpathObj, int index)
int pcmk__element_xpath(const char *prefix, xmlNode *xml, char *buffer, int offset, size_t buffer_size)
const xmlChar * pcmkXmlStr
xmlNode * get_xpath_object(const char *xpath, xmlNode *xml_obj, int error_level)
void dedupXpathResults(xmlXPathObjectPtr xpathObj)
#define PCMK__BUFFER_SIZE
char * xml_get_path(xmlNode *xml)
#define crm_err(fmt, args...)
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__