34#if defined(HAVE_LIBXML2)
35#include <libxml/parser.h>
36#include <libxml/tree.h>
37#include <libxml/xinclude.h>
38#include <libxml/xpath.h>
39#include <libxml/xpathInternals.h>
42 #include <libxslt/xsltInternals.h>
43 #include <libxslt/transform.h>
44 #include <libxslt/xsltutils.h>
61#ifdef HAVE_LIBXSLT_CLEANUP
88 res = xmlXIncludeProcess(doc);
102 xmlSubstituteEntitiesDefault(1);
104 doc = xmlReadFile(filename,
NULL, XML_PARSE_RECOVER);
117 xsltStylesheetPtr xslt = xsltLoadStylesheetPI(doc);
119 xmlDocPtr tmpdoc = xsltApplyStylesheet(xslt, doc,
NULL);
120 xsltFreeStylesheet(xslt);
129 ast_log(
LOG_NOTICE,
"XSLT support not found. XML documentation may be incomplete.\n");
133 xmlXPathOrderDocElems(doc);
135 return (
struct ast_xml_doc *) doc;
142 doc = xmlNewDoc((
const xmlChar *)
"1.0");
143 return (
struct ast_xml_doc *) doc;
155 return (
struct ast_xml_node *)
node;
162 if (!parent || !child_name) {
166 child = xmlNewChild((xmlNode *) parent,
NULL, (
const xmlChar *) child_name,
NULL);
167 return (
struct ast_xml_node *) child;
172 if (!parent || !child) {
175 return (
struct ast_xml_node *) xmlAddChild((xmlNode *) parent, (xmlNode *) child);
180 if (!parent || !child) {
183 return (
struct ast_xml_node *) xmlAddChildList((xmlNode *) parent, (xmlNode *) child);
191 return (
struct ast_xml_node *) xmlCopyNodeList((xmlNode *) list);
202 if (!(doc = xmlParseMemory(buffer, (
int) size))) {
210 return (
struct ast_xml_doc *) doc;
219 xmlFreeDoc((xmlDoc *) doc);
229 xmlDocSetRootElement((xmlDoc *) doc, (xmlNode *)
node);
240 root_node = xmlDocGetRootElement((xmlDoc *) doc);
242 return (
struct ast_xml_node *) root_node;
251 xmlFreeNode((xmlNode *)
node);
258 xmlFree((
char *) attribute);
265 xmlFree((
char *)
text);
281 attrvalue = xmlGetProp((xmlNode *)
node, (xmlChar *) attrname);
283 return (
const char *) attrvalue;
292 if (!xmlSetProp((xmlNode *)
node, (xmlChar *)
name, (xmlChar *)
value)) {
299struct ast_xml_node *
ast_xml_find_element(
struct ast_xml_node *root_node,
const char *
name,
const char *attrname,
const char *attrvalue)
301 struct ast_xml_node *cur;
314 if (!attrname || !attrvalue) {
320 if (!strcmp(attr, attrvalue)) {
337 return (
struct ast_xml_doc *) ((xmlNode *)
node)->doc;
341 xmlNsPtr ns = xmlSearchNs((xmlDocPtr) doc, (xmlNodePtr)
node, (xmlChar *) ns_name);
342 return (
struct ast_xml_ns *) ns;
347 return (
const char *) ((xmlNsPtr) ns)->prefix;
352 return (
const char *) ((xmlNsPtr) ns)->href;
361 return (
const char *) xmlNodeGetContent((xmlNode *)
node);
366 if (!
node || !content) {
370 xmlNodeSetContent((xmlNode *)
node, (
const xmlChar *) content);
379 xmlNodeSetName((xmlNode *)
node, (
const xmlChar *)
name);
384 return xmlDocDump(output, (xmlDocPtr)doc);
389 xmlDocDumpFormatMemory((xmlDocPtr)doc, (xmlChar **)buffer, length, 1);
394 return (
const char *) ((xmlNode *)
node)->name;
399 return (
struct ast_xml_node *) ((xmlNode *)
node)->children;
404 return (
struct ast_xml_node *) ((xmlNode *)
node)->next;
409 return (
struct ast_xml_node *) ((xmlNode *)
node)->prev;
414 return (
struct ast_xml_node *) ((xmlNode *)
node)->parent;
419 return (
struct ast_xml_node *) ((xmlXPathObjectPtr) results)->nodesetval->nodeTab[0];
424 return (
struct ast_xml_node *) ((xmlXPathObjectPtr) results)->nodesetval->nodeTab[i];
432 xmlXPathFreeObject((xmlXPathObjectPtr) results);
440 return ((xmlXPathObjectPtr) results)->nodesetval->nodeNr;
443struct ast_xml_xpath_results *
ast_xml_query(
struct ast_xml_doc *doc,
const char *xpath_str)
447 if (!(
context = xmlXPathNewContext((xmlDoc *) doc))) {
451 result = xmlXPathEvalExpression((xmlChar *) xpath_str,
context);
457 if (xmlXPathNodeSetIsEmpty(
result->nodesetval)) {
458 xmlXPathFreeObject(
result);
459 ast_debug(5,
"No results for query: %s\n", xpath_str);
462 return (
struct ast_xml_xpath_results *)
result;
472 if (!(
context = xmlXPathNewContext((xmlDoc *) doc))) {
480 (xmlChar *)ns.
href) != 0) {
488 result = xmlXPathEvalExpression((xmlChar *) xpath_str,
context);
494 if (xmlXPathNodeSetIsEmpty(
result->nodesetval)) {
495 xmlXPathFreeObject(
result);
496 ast_debug(5,
"No results for query: %s\n", xpath_str);
499 return (
struct ast_xml_xpath_results *)
result;
503struct ast_xslt_doc *ast_xslt_open(
char *filename)
505 xsltStylesheet *xslt;
508 xmlSubstituteEntitiesDefault(1);
510 xml = xmlReadFile(filename,
NULL, XML_PARSE_RECOVER);
519 xmlXPathOrderDocElems(xml);
521 if (!(xslt = xsltParseStylesheetDoc(xml))) {
526 return (
struct ast_xslt_doc *) xslt;
529struct ast_xslt_doc *ast_xslt_read_memory(
char *buffer,
size_t size)
531 xsltStylesheet *xslt;
538 xmlSubstituteEntitiesDefault(1);
540 if (!(doc = xmlParseMemory(buffer, (
int) size))) {
549 if (!(xslt = xsltParseStylesheetDoc(doc))) {
554 return (
struct ast_xslt_doc *) xslt;
557void ast_xslt_close(
struct ast_xslt_doc *axslt)
563 xsltFreeStylesheet((xsltStylesheet *) axslt);
566struct ast_xml_doc *ast_xslt_apply(
struct ast_xslt_doc *axslt,
struct ast_xml_doc *axml,
const char **params)
568 xsltStylesheet *xslt = (xsltStylesheet *)axslt;
569 xmlDoc *xml = (xmlDoc *)axml;
571 xsltTransformContextPtr ctxt;
573 int options = XSLT_PARSE_OPTIONS;
591 res = xsltApplyStylesheet(xslt, xml, params);
592 return (
struct ast_xml_doc *)res;
595 ctxt = xsltNewTransformContext(xslt, xml);
596 xsltSetCtxtParseOptions(ctxt,
options);
598 for (ns = xslt->doc->children->nsDef; ns; ns = ns->next) {
599 if (xmlXPathRegisterNs(ctxt->xpathCtxt, ns->prefix, ns->href) != 0) {
600 xsltFreeTransformContext(ctxt);
605 res = xsltApplyStylesheetUser(xslt, xml, params,
NULL,
NULL, ctxt);
606 xmlXPathFreeContext(ctxt->xpathCtxt);
607 ctxt->xpathCtxt =
NULL;
608 xsltFreeTransformContext(ctxt);
610 return (
struct ast_xml_doc *)res;
613int ast_xslt_save_result_to_string(
char **buffer,
int *length,
struct ast_xml_doc *
result,
614 struct ast_xslt_doc *axslt)
616 return xsltSaveResultToString((xmlChar **)buffer, length, (xmlDoc *)
result, (xsltStylesheet *)axslt);
Asterisk main include file. File version handling, generic pbx functions.
Support for logging to various files, console and syslog Configuration in file logger....
#define ast_debug(level,...)
Log a DEBUG message.
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
struct ast_xml_node * ast_xml_copy_node_list(struct ast_xml_node *list)
Create a copy of a n ode list.
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_node_get_children(struct ast_xml_node *node)
Get the node's children.
const char * ast_xml_get_attribute(struct ast_xml_node *node, const char *attrname)
Get a node attribute by name.
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.
const char * ast_xml_get_text(struct ast_xml_node *node)
Get an element content string.
struct ast_xml_ns * ast_xml_find_namespace(struct ast_xml_doc *doc, struct ast_xml_node *node, const char *ns_name)
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.
const char * ast_xml_get_ns_href(struct ast_xml_ns *ns)
Get the href of a namespace.
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.
int ast_xml_xpath_num_results(struct ast_xml_xpath_results *results)
Return the number of results from an XPath query.
struct ast_xml_node * ast_xml_new_node(const char *name)
Create a XML node.
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.
static int process_xincludes(xmlDoc *doc)
void ast_xml_close(struct ast_xml_doc *doc)
Close an already open document and free the used structure.
void ast_xml_doc_dump_memory(struct ast_xml_doc *doc, char **buffer, int *length)
Dump the specified document to a buffer.
const char * ast_xml_node_get_name(struct ast_xml_node *node)
Get the name of a node.
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()
int ast_xml_doc_dump_file(FILE *output, struct ast_xml_doc *doc)
Dump the specified document to a file.
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_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_node * ast_xml_node_get_prev(struct ast_xml_node *node)
Get the previous node in the same leve.
void ast_xml_xpath_results_free(struct ast_xml_xpath_results *results)
Free the XPath results.
struct ast_xml_doc * ast_xml_new(void)
Create a XML document.
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_text(struct ast_xml_node *node, const char *content)
Set an element content string.
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_free_text(const char *text)
Free a content element that was returned by ast_xml_get_text()
int ast_xml_init(void)
Initialize the XML library implementation. This function is used to setup everything needed to start ...
void ast_xml_set_root(struct ast_xml_doc *doc, struct ast_xml_node *node)
Specify the root node of a XML document.
struct ast_xml_node * ast_xml_node_get_parent(struct ast_xml_node *node)
Get the parent of a specified node.
struct ast_xml_doc * ast_xml_read_memory(char *buffer, size_t size)
Open an XML document that resides in memory.
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_doc * ast_xml_open(char *filename)
Open an XML document.
void ast_xml_set_name(struct ast_xml_node *node, const char *name)
Set or reset an element's name.
struct ast_xml_doc * ast_xml_get_doc(struct ast_xml_node *node)
Get the document based on a node.
void ast_xml_free_node(struct ast_xml_node *node)
Free node.
struct ast_xml_node * ast_xml_get_root(struct ast_xml_doc *doc)
Get the document root node.
const char * ast_xml_get_ns_prefix(struct ast_xml_ns *ns)
Get the prefix of a namespace.
Asterisk XML abstraction layer.