Asterisk - The Open Source Telephony Project GIT-master-80b953f
Loading...
Searching...
No Matches
Macros | Functions
xml.c File Reference

XML abstraction layer. More...

#include "asterisk.h"
#include "asterisk/xml.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/autoconfig.h"
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xinclude.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
Include dependency graph for xml.c:

Go to the source code of this file.

Macros

#define ASTMM_LIBC   ASTMM_IGNORE
 

Functions

struct ast_xml_node * ast_xml_add_child (struct ast_xml_node *parent, struct ast_xml_node *child)
 Add a child node, to a specified parent node.
 
struct ast_xml_node * ast_xml_add_child_list (struct ast_xml_node *parent, struct ast_xml_node *child)
 Add a list of child nodes, to a specified parent node.
 
void ast_xml_close (struct ast_xml_doc *doc)
 Close an already open document and free the used structure.
 
struct ast_xml_node * ast_xml_copy_node_list (struct ast_xml_node *list)
 Create a copy of a n ode list.
 
int ast_xml_doc_dump_file (FILE *output, struct ast_xml_doc *doc)
 Dump the specified document to a file.
 
void ast_xml_doc_dump_memory (struct ast_xml_doc *doc, char **buffer, int *length)
 Dump the specified document to a buffer.
 
struct ast_xml_node * ast_xml_find_element (struct ast_xml_node *root_node, const char *name, const char *attrname, const char *attrvalue)
 Find a node element by name.
 
struct ast_xml_ns * ast_xml_find_namespace (struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name)
 
int ast_xml_finish (void)
 Cleanup library allocated global data.
 
void ast_xml_free_attr (const char *attribute)
 Free an attribute returned by ast_xml_get_attribute()
 
void ast_xml_free_node (struct ast_xml_node *node)
 Free node.
 
void ast_xml_free_text (const char *text)
 Free a content element that was returned by ast_xml_get_text()
 
const char * ast_xml_get_attribute (struct ast_xml_node *node, const char *attrname)
 Get a node attribute by name.
 
struct ast_xml_doc * ast_xml_get_doc (struct ast_xml_node *node)
 Get the document based on a node.
 
const char * ast_xml_get_ns_href (struct ast_xml_ns *ns)
 Get the href of a namespace.
 
const char * ast_xml_get_ns_prefix (struct ast_xml_ns *ns)
 Get the prefix of a namespace.
 
struct ast_xml_node * ast_xml_get_root (struct ast_xml_doc *doc)
 Get the document root node.
 
const char * ast_xml_get_text (struct ast_xml_node *node)
 Get an element content string.
 
int ast_xml_init (void)
 Initialize the XML library implementation. This function is used to setup everything needed to start working with the xml implementation.
 
struct ast_xml_doc * ast_xml_new (void)
 Create a XML document.
 
struct ast_xml_node * ast_xml_new_child (struct ast_xml_node *parent, const char *child_name)
 Add a child node inside a passed parent node.
 
struct ast_xml_node * ast_xml_new_node (const char *name)
 Create a XML node.
 
struct ast_xml_node * ast_xml_node_get_children (struct ast_xml_node *node)
 Get the node's children.
 
const char * ast_xml_node_get_name (struct ast_xml_node *node)
 Get the name of a node.
 
struct ast_xml_node * ast_xml_node_get_next (struct ast_xml_node *node)
 Get the next node in the same level.
 
struct ast_xml_node * ast_xml_node_get_parent (struct ast_xml_node *node)
 Get the parent of a specified node.
 
struct ast_xml_node * ast_xml_node_get_prev (struct ast_xml_node *node)
 Get the previous node in the same leve.
 
struct ast_xml_doc * ast_xml_open (char *filename)
 Open an XML document.
 
struct ast_xml_xpath_results * ast_xml_query (struct ast_xml_doc *doc, const char *xpath_str)
 Execute an XPath query on an XML document.
 
struct ast_xml_xpath_results * ast_xml_query_with_namespaces (struct ast_xml_doc *doc, const char *xpath_str, struct ast_xml_namespace_def_vector *namespaces)
 Execute an XPath query on an XML document with namespaces.
 
struct ast_xml_doc * ast_xml_read_memory (char *buffer, size_t size)
 Open an XML document that resides in memory.
 
int ast_xml_set_attribute (struct ast_xml_node *node, const char *name, const char *value)
 Set an attribute to a node.
 
void ast_xml_set_name (struct ast_xml_node *node, const char *name)
 Set or reset an element's name.
 
void ast_xml_set_root (struct ast_xml_doc *doc, struct ast_xml_node *node)
 Specify the root node of a XML document.
 
void ast_xml_set_text (struct ast_xml_node *node, const char *content)
 Set an element content string.
 
struct ast_xml_node * ast_xml_xpath_get_first_result (struct ast_xml_xpath_results *results)
 Return the first result node of an XPath query.
 
struct ast_xml_node * ast_xml_xpath_get_result (struct ast_xml_xpath_results *results, int i)
 Return a specific result node of an XPath query.
 
int ast_xml_xpath_num_results (struct ast_xml_xpath_results *results)
 Return the number of results from an XPath query.
 
void ast_xml_xpath_results_free (struct ast_xml_xpath_results *results)
 Free the XPath results.
 
static int process_xincludes (xmlDoc *doc)
 

Detailed Description

XML abstraction layer.

Author
Eliel C. Sardanons (LU1ALY) eliel.nosp@m.s@gm.nosp@m.ail.c.nosp@m.om

Definition in file xml.c.

Macro Definition Documentation

◆ ASTMM_LIBC

#define ASTMM_LIBC   ASTMM_IGNORE

Definition at line 28 of file xml.c.

Function Documentation

◆ ast_xml_add_child()

struct ast_xml_node * ast_xml_add_child ( struct ast_xml_node *  parent,
struct ast_xml_node *  child 
)

Add a child node, to a specified parent node.

Parameters
parentWhere to add the child node.
childThe child node to add.
Return values
NULLon error.
non-NULLThe add child node on success.

Definition at line 169 of file xml.c.

170{
171 if (!parent || !child) {
172 return NULL;
173 }
174 return (struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
175}
#define NULL
Definition resample.c:96

References NULL.

Referenced by ast_geoloc_eprofiles_to_pidf(), geoloc_eprofile_to_intermediate(), and geoloc_eprofile_to_xmldoc().

◆ ast_xml_add_child_list()

struct ast_xml_node * ast_xml_add_child_list ( struct ast_xml_node *  parent,
struct ast_xml_node *  child 
)

Add a list of child nodes, to a specified parent node.

Parameters
parentWhere to add the child node.
childThe child list to add.
Return values
NULLon error.
non-NULLThe added child list on success.

Definition at line 177 of file xml.c.

178{
179 if (!parent || !child) {
180 return NULL;
181 }
182 return (struct ast_xml_node *) xmlAddChildList((xmlNode *) parent, (xmlNode *) child);
183}

References NULL.

Referenced by ast_geoloc_eprofiles_to_pidf(), and handle_dump_docs().

◆ ast_xml_close()

void ast_xml_close ( struct ast_xml_doc *  doc)

Close an already open document and free the used structure.

Parameters
docXML Document to close

Definition at line 212 of file xml.c.

213{
214 if (!doc) {
215 return;
216 }
217
218 xmlFreeDoc((xmlDoc *) doc);
219 doc = NULL;
220}

References NULL.

Referenced by ast_geoloc_eprofile_create_from_pidf(), ast_geoloc_eprofile_to_pidf(), ast_geoloc_eprofiles_to_pidf(), geoloc_eprofile_to_xmldoc(), handle_dump_docs(), handle_incoming_request(), xmldoc_load_documentation(), and xmldoc_purge_documentation().

◆ ast_xml_copy_node_list()

struct ast_xml_node * ast_xml_copy_node_list ( struct ast_xml_node *  list)

Create a copy of a n ode list.

Parameters
listThe list to copy.
Return values
NULLon error.
non-NULLThe copied list.

Definition at line 185 of file xml.c.

186{
187 if (!list) {
188 return NULL;
189 }
190 return (struct ast_xml_node *) xmlCopyNodeList((xmlNode *) list);
191}

References NULL.

Referenced by ast_geoloc_eprofiles_to_pidf(), and handle_dump_docs().

◆ ast_xml_doc_dump_file()

int ast_xml_doc_dump_file ( FILE *  output,
struct ast_xml_doc *  doc 
)

Dump the specified document to a file.

Definition at line 381 of file xml.c.

382{
383 return xmlDocDump(output, (xmlDocPtr)doc);
384}

Referenced by handle_dump_docs().

◆ ast_xml_doc_dump_memory()

void ast_xml_doc_dump_memory ( struct ast_xml_doc *  doc,
char **  buffer,
int *  length 
)

Dump the specified document to a buffer.

Parameters
docThe XML doc to dump
bufferA pointer to a char * to receive the address of the results
lengthA pointer to an int to receive the length of the results
Note
The result buffer must be freed with ast_xml_free_text().

Definition at line 386 of file xml.c.

387{
388 xmlDocDumpFormatMemory((xmlDocPtr)doc, (xmlChar **)buffer, length, 1);
389}

Referenced by ast_geoloc_eprofile_create_from_pidf(), ast_geoloc_eprofile_to_pidf(), ast_geoloc_eprofiles_to_pidf(), geoloc_eprofile_create_from_xslt_result(), and geoloc_eprofile_to_xmldoc().

◆ ast_xml_find_element()

struct ast_xml_node * ast_xml_find_element ( struct ast_xml_node *  root_node,
const char *  name,
const char *  attrname,
const char *  attrvalue 
)

Find a node element by name.

Parameters
root_nodeThis is the node starting point.
nameNode name to find.
attrnameattribute name to match (if NULL it won't be matched).
attrvalueattribute value to match (if NULL it won't be matched).
Return values
NULLif not found.
Returns
The node on success.

Definition at line 298 of file xml.c.

299{
300 struct ast_xml_node *cur;
301 const char *attr;
302
303 if (!root_node) {
304 return NULL;
305 }
306
307 for (cur = root_node; cur; cur = ast_xml_node_get_next(cur)) {
308 /* Check if the name matches */
309 if (strcmp(ast_xml_node_get_name(cur), name)) {
310 continue;
311 }
312 /* We need to check for a specific attribute name? */
313 if (!attrname || !attrvalue) {
314 return cur;
315 }
316 /* Get the attribute, we need to compare it. */
317 if ((attr = ast_xml_get_attribute(cur, attrname))) {
318 /* does attribute name/value matches? */
319 if (!strcmp(attr, attrvalue)) {
320 ast_xml_free_attr(attr);
321 return cur;
322 }
323 ast_xml_free_attr(attr);
324 }
325 }
326
327 return NULL;
328}
static const char name[]
Definition format_mp3.c:68
const char * ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
Get a node attribute by name.
Definition xml.c:268
const char * ast_xml_node_get_name(struct ast_xml_node *node)
Get the name of a node.
Definition xml.c:391
void ast_xml_free_attr(const char *attribute)
Free an attribute returned by ast_xml_get_attribute()
Definition xml.c:254
struct ast_xml_node * ast_xml_node_get_next(struct ast_xml_node *node)
Get the next node in the same level.
Definition xml.c:401

References ast_xml_free_attr(), ast_xml_get_attribute(), ast_xml_node_get_name(), ast_xml_node_get_next(), name, and NULL.

Referenced by _xmldoc_build_field(), load_modules(), xmldoc_build_final_response(), xmldoc_build_list_responses(), xmldoc_get_node(), and xmldoc_get_syntax_config_object().

◆ ast_xml_find_namespace()

struct ast_xml_ns * ast_xml_find_namespace ( struct ast_xml_doc *  doc,
struct ast_xml_node *  node,
const char *  ns_name 
)

Definition at line 339 of file xml.c.

339 {
340 xmlNsPtr ns = xmlSearchNs((xmlDocPtr) doc, (xmlNodePtr) node, (xmlChar *) ns_name);
341 return (struct ast_xml_ns *) ns;
342}

◆ ast_xml_finish()

int ast_xml_finish ( void  )

Cleanup library allocated global data.

Return values
0On success.
1On error.

Definition at line 58 of file xml.c.

59{
60 xmlCleanupParser();
61#ifdef HAVE_LIBXSLT
62#ifdef HAVE_LIBXSLT_CLEANUP
63 xsltCleanupGlobals();
64#else
65 xsltUninit();
66#endif
67#endif
68
69 return 0;
70}

Referenced by xmldoc_unload_documentation().

◆ ast_xml_free_attr()

void ast_xml_free_attr ( const char *  attribute)

◆ ast_xml_free_node()

void ast_xml_free_node ( struct ast_xml_node *  node)

Free node.

Parameters
nodeNode to be released.

Definition at line 244 of file xml.c.

245{
246 if (!node) {
247 return;
248 }
249
250 xmlFreeNode((xmlNode *) node);
251 node = NULL;
252}

References NULL.

Referenced by ast_geoloc_eprofiles_to_pidf(), geoloc_civicaddr_list_to_xml(), geoloc_eprofile_to_intermediate(), and geoloc_gml_list_to_xml().

◆ ast_xml_free_text()

void ast_xml_free_text ( const char *  text)

◆ ast_xml_get_attribute()

const char * ast_xml_get_attribute ( struct ast_xml_node *  node,
const char *  attrname 
)

Get a node attribute by name.

Parameters
nodeNode where to search the attribute.
attrnameAttribute name.
Return values
NULLon error
Returns
The attribute value on success.
Note
The result must be freed with ast_xml_free_attr().

Definition at line 268 of file xml.c.

269{
270 xmlChar *attrvalue;
271
272 if (!node) {
273 return NULL;
274 }
275
276 if (!attrname) {
277 return NULL;
278 }
279
280 attrvalue = xmlGetProp((xmlNode *) node, (xmlChar *) attrname);
281
282 return (const char *) attrvalue;
283}

References NULL.

Referenced by _ast_xmldoc_build_provided_by(), _ast_xmldoc_build_seealso(), ast_geoloc_eprofiles_to_pidf(), ast_xml_find_element(), ast_xmldoc_build_documentation(), ast_xmldoc_regenerate_doc_item(), build_config_docs(), geoloc_eprofile_create_from_xslt_result(), geoloc_eprofile_to_xmldoc(), var_list_from_confidence(), var_list_from_loc_info(), var_list_from_node(), xmldoc_attribute_match(), xmldoc_build_final_response(), xmldoc_build_list_responses(), xmldoc_get_syntax_cmd(), xmldoc_get_syntax_config_object(), xmldoc_get_syntax_config_option(), xmldoc_get_syntax_fun(), xmldoc_get_syntax_manager(), xmldoc_parse_argument(), xmldoc_parse_enumlist(), xmldoc_parse_example(), xmldoc_parse_info(), xmldoc_parse_optionlist(), xmldoc_parse_parameter(), xmldoc_parse_variable(), and xmldoc_parse_variablelist().

◆ ast_xml_get_doc()

struct ast_xml_doc * ast_xml_get_doc ( struct ast_xml_node *  node)

Get the document based on a node.

Parameters
nodeA node that is part of the dom.
Returns
The dom pointer where this node resides.

Definition at line 330 of file xml.c.

331{
332 if (!node) {
333 return NULL;
334 }
335
336 return (struct ast_xml_doc *) ((xmlNode *)node)->doc;
337}

References NULL.

◆ ast_xml_get_ns_href()

const char * ast_xml_get_ns_href ( struct ast_xml_ns *  ns)

Get the href of a namespace.

Parameters
nsThe namespace
Returns
The href of the namespace.

Definition at line 349 of file xml.c.

350{
351 return (const char *) ((xmlNsPtr) ns)->href;
352}

◆ ast_xml_get_ns_prefix()

const char * ast_xml_get_ns_prefix ( struct ast_xml_ns *  ns)

Get the prefix of a namespace.

Parameters
nsThe namespace
Returns
The prefix of the namespace.

Definition at line 344 of file xml.c.

345{
346 return (const char *) ((xmlNsPtr) ns)->prefix;
347}

◆ ast_xml_get_root()

struct ast_xml_node * ast_xml_get_root ( struct ast_xml_doc *  doc)

Get the document root node.

Parameters
docXML Document reference
Return values
NULLon error
Returns
The root node on success.

Definition at line 231 of file xml.c.

232{
233 xmlNode *root_node;
234
235 if (!doc) {
236 return NULL;
237 }
238
239 root_node = xmlDocGetRootElement((xmlDoc *) doc);
240
241 return (struct ast_xml_node *) root_node;
242}

References NULL.

Referenced by ast_xmldoc_build_documentation(), geoloc_eprofile_create_from_xslt_result(), handle_dump_docs(), is_pidf_lo(), xmldoc_get_node(), and xmldoc_load_documentation().

◆ ast_xml_get_text()

const char * ast_xml_get_text ( struct ast_xml_node *  node)

Get an element content string.

Parameters
nodeNode from where to get the string.
Return values
NULLon error.
Returns
The text content of node.

Definition at line 354 of file xml.c.

355{
356 if (!node) {
357 return NULL;
358 }
359
360 return (const char *) xmlNodeGetContent((xmlNode *) node);
361}

References NULL.

Referenced by _ast_xmldoc_build_seealso(), _ast_xmldoc_build_since(), geoloc_eprofile_create_from_xslt_result(), load_modules(), var_list_from_confidence(), var_list_from_node(), xmldoc_get_formatted(), xmldoc_get_syntax_config_object(), xmldoc_parse_example(), xmldoc_parse_para(), and xmldoc_parse_variable().

◆ ast_xml_init()

int ast_xml_init ( void  )

Initialize the XML library implementation. This function is used to setup everything needed to start working with the xml implementation.

Return values
0On success.
1On error.

Definition at line 49 of file xml.c.

50{
51 LIBXML_TEST_VERSION
52#ifdef HAVE_LIBXSLT
53 xsltInit();
54#endif
55 return 0;
56}

Referenced by xmldoc_load_documentation().

◆ ast_xml_new()

struct ast_xml_doc * ast_xml_new ( void  )

Create a XML document.

Return values
NULLon error.
non-NULLThe allocated document structure.

Definition at line 137 of file xml.c.

138{
139 xmlDoc *doc;
140
141 doc = xmlNewDoc((const xmlChar *) "1.0");
142 return (struct ast_xml_doc *) doc;
143}

Referenced by ast_geoloc_eprofiles_to_pidf(), geoloc_eprofile_to_xmldoc(), and handle_dump_docs().

◆ ast_xml_new_child()

struct ast_xml_node * ast_xml_new_child ( struct ast_xml_node *  parent,
const char *  child_name 
)

Add a child node inside a passed parent node.

Parameters
parentThe pointer of the parent node.
child_nameThe name of the child node to add.
Return values
NULLon error.
non-NULLThe created child node pointer.

Definition at line 157 of file xml.c.

158{
159 xmlNode *child;
160
161 if (!parent || !child_name) {
162 return NULL;
163 }
164
165 child = xmlNewChild((xmlNode *) parent, NULL, (const xmlChar *) child_name, NULL);
166 return (struct ast_xml_node *) child;
167}

References NULL.

Referenced by geoloc_civicaddr_list_to_xml(), geoloc_eprofile_to_intermediate(), geoloc_gml_list_to_xml(), and xmldoc_update_config_type().

◆ ast_xml_new_node()

struct ast_xml_node * ast_xml_new_node ( const char *  name)

Create a XML node.

Parameters
nameThe name of the node to be created.
Return values
NULLon error.
non-NULLThe allocated node structe.

Definition at line 145 of file xml.c.

146{
147 xmlNode *node;
148 if (!name) {
149 return NULL;
150 }
151
152 node = xmlNewNode(NULL, (const xmlChar *) name);
153
154 return (struct ast_xml_node *) node;
155}

References name, and NULL.

Referenced by ast_geoloc_eprofiles_to_pidf(), geoloc_civicaddr_list_to_xml(), geoloc_eprofile_to_intermediate(), geoloc_eprofile_to_xmldoc(), geoloc_gml_list_to_xml(), and handle_dump_docs().

◆ ast_xml_node_get_children()

struct ast_xml_node * ast_xml_node_get_children ( struct ast_xml_node *  node)

◆ ast_xml_node_get_name()

const char * ast_xml_node_get_name ( struct ast_xml_node *  node)

◆ ast_xml_node_get_next()

struct ast_xml_node * ast_xml_node_get_next ( struct ast_xml_node *  node)

◆ ast_xml_node_get_parent()

struct ast_xml_node * ast_xml_node_get_parent ( struct ast_xml_node *  node)

Get the parent of a specified node.

Definition at line 411 of file xml.c.

412{
413 return (struct ast_xml_node *) ((xmlNode *) node)->parent;
414}

◆ ast_xml_node_get_prev()

struct ast_xml_node * ast_xml_node_get_prev ( struct ast_xml_node *  node)

Get the previous node in the same leve.

Definition at line 406 of file xml.c.

407{
408 return (struct ast_xml_node *) ((xmlNode *) node)->prev;
409}

◆ ast_xml_open()

struct ast_xml_doc * ast_xml_open ( char *  filename)

Open an XML document.

Parameters
filenameDocument path.
Return values
NULLon error.
Returns
The ast_xml_doc reference to the open document.

Definition at line 95 of file xml.c.

96{
97 xmlDoc *doc;
98
99 if (!filename) {
100 return NULL;
101 }
102
103 doc = xmlReadFile(filename, NULL, XML_PARSE_RECOVER | XML_PARSE_NONET);
104 if (!doc) {
105 return NULL;
106 }
107
108 /* process xinclude elements. */
109 if (process_xincludes(doc) < 0) {
110 xmlFreeDoc(doc);
111 return NULL;
112 }
113
114#ifdef HAVE_LIBXSLT
115 {
116 xsltStylesheetPtr xslt = xsltLoadStylesheetPI(doc);
117 if (xslt) {
118 xmlDocPtr tmpdoc = xsltApplyStylesheet(xslt, doc, NULL);
119 xsltFreeStylesheet(xslt);
120 xmlFreeDoc(doc);
121 if (!tmpdoc) {
122 return NULL;
123 }
124 doc = tmpdoc;
125 }
126 }
127#else /* no HAVE_LIBXSLT */
128 ast_log(LOG_NOTICE, "XSLT support not found. XML documentation may be incomplete.\n");
129#endif /* HAVE_LIBXSLT */
130
131 /* Optimize for XPath */
132 xmlXPathOrderDocElems(doc);
133
134 return (struct ast_xml_doc *) doc;
135}
#define ast_log
Definition astobj2.c:42
#define LOG_NOTICE
static int process_xincludes(xmlDoc *doc)
Definition xml.c:84

References ast_log, LOG_NOTICE, NULL, and process_xincludes().

Referenced by xmldoc_load_documentation().

◆ ast_xml_query()

struct ast_xml_xpath_results * ast_xml_query ( struct ast_xml_doc *  doc,
const char *  xpath_str 
)

Execute an XPath query on an XML document.

Parameters
docXML document to query
xpath_strThe XPath query string to execute on the document
Returns
An object containing the results of the XPath query on success
Return values
NULLon failure
Since
12

Definition at line 442 of file xml.c.

443{
444 xmlXPathContextPtr context;
445 xmlXPathObjectPtr result;
446 if (!(context = xmlXPathNewContext((xmlDoc *) doc))) {
447 ast_log(LOG_ERROR, "Could not create XPath context!\n");
448 return NULL;
449 }
450 result = xmlXPathEvalExpression((xmlChar *) xpath_str, context);
451 xmlXPathFreeContext(context);
452 if (!result) {
453 ast_log(LOG_WARNING, "Error for query: %s\n", xpath_str);
454 return NULL;
455 }
456 if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
457 xmlXPathFreeObject(result);
458 ast_debug(5, "No results for query: %s\n", xpath_str);
459 return NULL;
460 }
461 return (struct ast_xml_xpath_results *) result;
462}
static PGresult * result
Definition cel_pgsql.c:84
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_WARNING

References ast_debug, ast_log, LOG_ERROR, LOG_WARNING, NULL, and result.

Referenced by ast_xmldoc_query().

◆ ast_xml_query_with_namespaces()

struct ast_xml_xpath_results * ast_xml_query_with_namespaces ( struct ast_xml_doc *  doc,
const char *  xpath_str,
struct ast_xml_namespace_def_vector namespaces 
)

Execute an XPath query on an XML document with namespaces.

Parameters
docXML document to query
xpath_strThe XPath query string to execute on the document
namespacesA vector of ast_xml_namespace structures (not pointers)
Returns
An object containing the results of the XPath query on success
Return values
NULLon failure

Definition at line 464 of file xml.c.

466{
467 xmlXPathContextPtr context;
468 xmlXPathObjectPtr result;
469 int i;
470
471 if (!(context = xmlXPathNewContext((xmlDoc *) doc))) {
472 ast_log(LOG_ERROR, "Could not create XPath context!\n");
473 return NULL;
474 }
475
476 for (i = 0; i < AST_VECTOR_SIZE(namespaces); i++) {
477 struct ast_xml_namespace_def ns = AST_VECTOR_GET(namespaces, i);
478 if (xmlXPathRegisterNs(context, (xmlChar *)ns.prefix,
479 (xmlChar *)ns.href) != 0) {
480 xmlXPathFreeContext(context);
481 ast_log(LOG_ERROR, "Could not register namespace %s:%s\n",
482 ns.prefix, ns.href);
483 return NULL;
484 }
485 }
486
487 result = xmlXPathEvalExpression((xmlChar *) xpath_str, context);
488 xmlXPathFreeContext(context);
489 if (!result) {
490 ast_log(LOG_WARNING, "Error for query: %s\n", xpath_str);
491 return NULL;
492 }
493 if (xmlXPathNodeSetIsEmpty(result->nodesetval)) {
494 xmlXPathFreeObject(result);
495 ast_debug(5, "No results for query: %s\n", xpath_str);
496 return NULL;
497 }
498 return (struct ast_xml_xpath_results *) result;
499}
Namespace definition.
Definition xml.h:325
const char * prefix
Definition xml.h:326
const char * href
Definition xml.h:327
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition vector.h:620
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition vector.h:691

References ast_debug, ast_log, AST_VECTOR_GET, AST_VECTOR_SIZE, ast_xml_namespace_def::href, LOG_ERROR, LOG_WARNING, NULL, ast_xml_namespace_def::prefix, and result.

◆ ast_xml_read_memory()

struct ast_xml_doc * ast_xml_read_memory ( char *  buffer,
size_t  size 
)

Open an XML document that resides in memory.

Parameters
bufferThe address where the document is stored
sizeThe number of bytes in the document
Return values
NULLon error.
Returns
The ast_xml_doc reference to the open document.

Definition at line 193 of file xml.c.

194{
195 xmlDoc *doc;
196
197 if (!buffer) {
198 return NULL;
199 }
200
201 if (!(doc = xmlParseMemory(buffer, (int) size))) {
202 /* process xinclude elements. */
203 if (process_xincludes(doc) < 0) {
204 xmlFreeDoc(doc);
205 return NULL;
206 }
207 }
208
209 return (struct ast_xml_doc *) doc;
210}

References NULL, and process_xincludes().

Referenced by handle_incoming_request().

◆ ast_xml_set_attribute()

int ast_xml_set_attribute ( struct ast_xml_node *  node,
const char *  name,
const char *  value 
)

Set an attribute to a node.

Parameters
nodeIn which node we want to insert the attribute.
nameThe attribute name.
valueThe attribute value.
Return values
0on success.
-1on error.

Definition at line 285 of file xml.c.

286{
287 if (!name || !value) {
288 return -1;
289 }
290
291 if (!xmlSetProp((xmlNode *) node, (xmlChar *) name, (xmlChar *) value)) {
292 return -1;
293 }
294
295 return 0;
296}
int value
Definition syslog.c:37

References name, and value.

Referenced by ast_geoloc_eprofiles_to_pidf(), geoloc_civicaddr_list_to_xml(), geoloc_eprofile_to_intermediate(), geoloc_eprofile_to_xmldoc(), geoloc_gml_list_to_xml(), xmldoc_update_config_option(), and xmldoc_update_config_type().

◆ ast_xml_set_name()

void ast_xml_set_name ( struct ast_xml_node *  node,
const char *  name 
)

Set or reset an element's name.

Parameters
nodeNode whose name is to be set.
nameNew name.

Definition at line 372 of file xml.c.

373{
374 if (!node || !name) {
375 return;
376 }
377
378 xmlNodeSetName((xmlNode *) node, (const xmlChar *) name);
379}

References name.

◆ ast_xml_set_root()

void ast_xml_set_root ( struct ast_xml_doc *  doc,
struct ast_xml_node *  node 
)

Specify the root node of a XML document.

Parameters
docXML Document reference
nodeA pointer to the node we want to set as root node.

Definition at line 222 of file xml.c.

223{
224 if (!doc || !node) {
225 return;
226 }
227
228 xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *) node);
229}

Referenced by ast_geoloc_eprofiles_to_pidf(), geoloc_eprofile_to_xmldoc(), and handle_dump_docs().

◆ ast_xml_set_text()

void ast_xml_set_text ( struct ast_xml_node *  node,
const char *  content 
)

Set an element content string.

Parameters
nodeNode from where to set the content string.
contentThe text to insert in the node.

Definition at line 363 of file xml.c.

364{
365 if (!node || !content) {
366 return;
367 }
368
369 xmlNodeSetContent((xmlNode *) node, (const xmlChar *) content);
370}

Referenced by geoloc_civicaddr_list_to_xml(), geoloc_eprofile_to_intermediate(), geoloc_gml_list_to_xml(), and xmldoc_update_config_type().

◆ ast_xml_xpath_get_first_result()

struct ast_xml_node * ast_xml_xpath_get_first_result ( struct ast_xml_xpath_results *  results)

Return the first result node of an XPath query.

Parameters
resultsThe XPath results object to get the first result from
Returns
The first result in the XPath object on success
Return values
NULLon error
Since
12

Definition at line 416 of file xml.c.

417{
418 return (struct ast_xml_node *) ((xmlXPathObjectPtr) results)->nodesetval->nodeTab[0];
419}

Referenced by load_modules(), xmldoc_update_config_option(), and xmldoc_update_config_type().

◆ ast_xml_xpath_get_result()

struct ast_xml_node * ast_xml_xpath_get_result ( struct ast_xml_xpath_results *  results,
int  n 
)

Return a specific result node of an XPath query.

Parameters
resultsThe XPath results object to get the result from
nThe index of the result to get
Returns
The nth result in the XPath object on success
Return values
NULLon error

Definition at line 421 of file xml.c.

422{
423 return (struct ast_xml_node *) ((xmlXPathObjectPtr) results)->nodesetval->nodeTab[i];
424}

◆ ast_xml_xpath_num_results()

int ast_xml_xpath_num_results ( struct ast_xml_xpath_results *  results)

Return the number of results from an XPath query.

Parameters
resultsThe XPath results object to count
Returns
The number of results in the XPath object
Since
12

Definition at line 434 of file xml.c.

435{
436 if (!results) {
437 return 0;
438 }
439 return ((xmlXPathObjectPtr) results)->nodesetval->nodeNr;
440}

◆ ast_xml_xpath_results_free()

void ast_xml_xpath_results_free ( struct ast_xml_xpath_results *  results)

Free the XPath results.

Parameters
resultsThe XPath results object to dispose of
Since
12

Definition at line 426 of file xml.c.

427{
428 if (!results) {
429 return;
430 }
431 xmlXPathFreeObject((xmlXPathObjectPtr) results);
432}

Referenced by load_modules(), xmldoc_update_config_option(), and xmldoc_update_config_type().

◆ process_xincludes()

static int process_xincludes ( xmlDoc *  doc)
static

Definition at line 84 of file xml.c.

85{
86 int res;
87
88 do {
89 res = xmlXIncludeProcess(doc);
90 } while (res > 0);
91
92 return res;
93}

Referenced by ast_xml_open(), and ast_xml_read_memory().