1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
6 * Copyright (C) 2006,2007 Nedko Arnaudov <nedko@arnaudov.name>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *****************************************************************************/
23 #include <libxml/parser.h>
24 #include <libxml/xpath.h>
27 #define DISABLE_DEBUG_OUTPUT
36 xmlBufferPtr content_buffer_ptr
)
39 xmlXPathContextPtr xpath_ctx
;
40 xmlXPathObjectPtr xpath_obj
;
42 /* Create xpath evaluation context */
43 xpath_ctx
= xmlXPathNewContext(doc_ptr
);
44 if (xpath_ctx
== NULL
)
46 ERROR_OUT("Unable to create new XPath context");
51 /* Evaluate xpath expression */
52 xpath_obj
= xmlXPathEvalExpression((const xmlChar
*)xpath
, xpath_ctx
);
53 if (xpath_obj
== NULL
)
55 ERROR_OUT("Unable to evaluate XPath expression \"%s\"", xpath
);
60 if (xpath_obj
->nodesetval
== NULL
|| xpath_obj
->nodesetval
->nodeNr
== 0)
62 ERROR_OUT("XPath \"%s\" evaluation returned no data", xpath
);
67 if (xpath_obj
->nodesetval
->nodeNr
!= 1)
69 ERROR_OUT("XPath \"%s\" evaluation returned multiple entries (%d)", xpath
, xpath_obj
->nodesetval
->nodeNr
);
74 ret
= xmlNodeBufGetContent(content_buffer_ptr
, xpath_obj
->nodesetval
->nodeTab
[0]);
77 ERROR_OUT("xmlNodeBufGetContent() failed. (%d)", ret
);
85 xmlXPathFreeObject(xpath_obj
);
88 xmlXPathFreeContext(xpath_ctx
);
100 xmlXPathContextPtr xpath_ctx
;
101 xmlXPathObjectPtr xpath_obj
;
103 /* Create xpath evaluation context */
104 xpath_ctx
= xmlXPathNewContext(doc_ptr
);
105 if (xpath_ctx
== NULL
)
107 ERROR_OUT("Unable to create new XPath context");
112 /* Evaluate xpath expression */
113 xpath_obj
= xmlXPathEvalExpression((const xmlChar
*)xpath
, xpath_ctx
);
114 if (xpath_obj
== NULL
)
116 ERROR_OUT("Unable to evaluate XPath expression \"%s\"", xpath
);
121 if (xpath_obj
->nodesetval
== NULL
|| xpath_obj
->nodesetval
->nodeNr
== 0)
123 DEBUG_OUT("XPath \"%s\" evaluation returned no data", xpath
);
128 if (xpath_obj
->nodesetval
->nodeNr
!= 1)
130 DEBUG_OUT("XPath \"%s\" evaluation returned multiple entries (%d)", xpath
, xpath_obj
->nodesetval
->nodeNr
);
135 DEBUG_OUT("XPath \"%s\" evaluation returned one entry", xpath
);
139 xmlXPathFreeObject(xpath_obj
);
142 xmlXPathFreeContext(xpath_ctx
);
156 xmlBufferPtr content_buffer_ptr
;
158 content_buffer_ptr
= xmlBufferCreate();
159 if (content_buffer_ptr
== NULL
)
161 ERROR_OUT("xmlBufferCreate() failed.");
166 ret
= xml_get_content(doc_ptr
, xpath
, content_buffer_ptr
);
173 "XPath \"%s\" execution returned content \"%s\" (%u bytes)",
175 xmlBufferContent(content_buffer_ptr
),
176 xmlBufferLength(content_buffer_ptr
));
178 if (buffer_size
< xmlBufferLength(content_buffer_ptr
) + 1)
181 "%u chars are requires to store \"%s\" but only %u are available",
182 xmlBufferLength(content_buffer_ptr
) + 1,
183 xmlBufferContent(content_buffer_ptr
),
189 memcpy(buffer_ptr
, xmlBufferContent(content_buffer_ptr
), xmlBufferLength(content_buffer_ptr
) + 1);
193 xmlBufferFree(content_buffer_ptr
);
203 char ** buffer_ptr_ptr
)
206 xmlBufferPtr content_buffer_ptr
;
209 content_buffer_ptr
= xmlBufferCreate();
210 if (content_buffer_ptr
== NULL
)
212 ERROR_OUT("xmlBufferCreate() failed.");
217 ret
= xml_get_content(doc_ptr
, xpath
, content_buffer_ptr
);
224 "XPath \"%s\" execution returned content \"%s\" (%u bytes)",
226 xmlBufferContent(content_buffer_ptr
),
227 xmlBufferLength(content_buffer_ptr
));
229 buffer_ptr
= strdup((const char *)xmlBufferContent(content_buffer_ptr
));
230 if (buffer_ptr
== NULL
)
232 ERROR_OUT("strdup failed.");
237 *buffer_ptr_ptr
= buffer_ptr
;
241 xmlBufferFree(content_buffer_ptr
);
254 xmlBufferPtr content_buffer_ptr
;
256 content_buffer_ptr
= xmlBufferCreate();
257 if (content_buffer_ptr
== NULL
)
259 ERROR_OUT("xmlBufferCreate() failed.");
264 ret
= xml_get_content(doc_ptr
, xpath
, content_buffer_ptr
);
271 "XPath \"%s\" execution returned content \"%s\" (%u bytes)",
273 xmlBufferContent(content_buffer_ptr
),
274 xmlBufferLength(content_buffer_ptr
));
276 *value_ptr
= atoi((const char *)xmlBufferContent(content_buffer_ptr
));
280 xmlBufferFree(content_buffer_ptr
);
290 unsigned int * value_ptr
)
293 xmlBufferPtr content_buffer_ptr
;
296 content_buffer_ptr
= xmlBufferCreate();
297 if (content_buffer_ptr
== NULL
)
299 ERROR_OUT("xmlBufferCreate() failed.");
304 ret
= xml_get_content(doc_ptr
, xpath
, content_buffer_ptr
);
311 "XPath \"%s\" execution returned content \"%s\" (%u bytes)",
313 xmlBufferContent(content_buffer_ptr
),
314 xmlBufferLength(content_buffer_ptr
));
316 value
= atoi((const char *)xmlBufferContent(content_buffer_ptr
));
319 ERROR_OUT("\"%s\" cannot be converted to unsigned int", xmlBufferContent(content_buffer_ptr
));
323 *value_ptr
= (unsigned int)value
;
327 xmlBufferFree(content_buffer_ptr
);