CliXoN
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
Macros | Typedefs | Enumerations | Functions | Variables
clixon_xsl.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <fnmatch.h>
#include <stdint.h>
#include <assert.h>
#include <cligen/cligen.h>
#include "clixon_err.h"
#include "clixon_log.h"
#include "clixon_xml.h"
#include "clixon_xsl.h"

Macros

#define XPATH_VEC_START   128
 

Typedefs

typedef struct searchvec searchvec
 

Enumerations

enum  axis_type {
  A_SELF, A_CHILD, A_PARENT, A_ROOT,
  A_ANCESTOR, A_DESCENDANT_OR_SELF
}
 

Functions

static int xpath_split (char *xpathstr, char **pathexpr)
 
static char * axis_type2str (enum axis_type type) __attribute__((unused))
 
static int xpath_print (FILE *f, struct xpath_element *xplist)
 
static int xpath_parse_predicate (struct xpath_element *xe, char *pred)
 
static int xpath_element_new (enum axis_type atype, char *str, struct xpath_element ***xpnext)
 
static int xpath_element_free (struct xpath_element *xe)
 
static int xpath_free (struct xpath_element *xplist)
 
static int xpath_parse (char *xpath, struct xpath_element **xplist0)
 
static int recursive_find (cxobj *xn, char *pattern, int node_type, uint16_t flags, cxobj ***vec0, size_t *vec0len)
 
static int xpath_expr (char *predicate_expression, uint16_t flags, cxobj ***vec0, size_t *vec0len)
 
static int xpath_find (struct xpath_element *xe, int descendants0, cxobj **vec0, size_t vec0len, uint16_t flags, cxobj ***vec2, size_t *vec2len)
 
static int xpath_exec (char *xpath, cxobj **vec0, size_t vec0len, uint16_t flags, cxobj ***vec2, size_t *vec2len)
 
static int xpath_choice (cxobj *xtop, char *xpath0, uint16_t flags, cxobj ***vec1, size_t *vec1len)
 
static cxobj * xpath_first0 (cxobj *cxtop, char *xpath)
 
cxobj * xpath_first (cxobj *cxtop, char *format,...)
 
cxobj * xpath_each (cxobj *cxtop, char *xpath, cxobj *xprev)
 
int xpath_vec (cxobj *cxtop, char *format, cxobj ***vec, size_t *veclen,...)
 
int xpath_vec_flag (cxobj *cxtop, char *format, uint16_t flags, cxobj ***vec, size_t *veclen,...)
 

Variables

struct searchvec clixon_xml_parseerror
 
static const struct map_str2int atmap []
 

Macro Definition Documentation

#define XPATH_VEC_START   128

Typedef Documentation

typedef struct searchvec searchvec

Enumeration Type Documentation

enum axis_type
Enumerator
A_SELF 
A_CHILD 
A_PARENT 
A_ROOT 
A_ANCESTOR 
A_DESCENDANT_OR_SELF 

Function Documentation

static char * axis_type2str ( enum axis_type  type)
static
static int recursive_find ( cxobj *  xn,
char *  pattern,
int  node_type,
uint16_t  flags,
cxobj ***  vec0,
size_t *  vec0len 
)
static

Find a node 'deep' in an XML tree

The xv_* arguments are filled in nodes found earlier. args:

Parameters
[in]xn_parentBase XML object
[in]nameshell wildcard pattern to match with node name
[in]node_typeCX_ELMNT, CX_ATTR or CX_BODY
[in,out]vec1internal buffers with results
[in,out]vec0internal buffers with results
[in,out]vec_leninternal buffers with length of vec0,vec1
[in,out]vec_maxinternal buffers with max of vec0,vec1 returns: 0 on OK, -1 on error
static int xpath_choice ( cxobj *  xtop,
char *  xpath0,
uint16_t  flags,
cxobj ***  vec1,
size_t *  vec1len 
)
static

Intermediate xpath function to handle 'conditional' cases. For example: xpath = //a | //b. xpath_first+ splits xpath up in several subcalls (eg xpath=//a and xpath=//b) and collects the results. Note: if a match is found in both, two (or more) same results will be returned. Note, this could be 'folded' into xpath1 but I judged it too complex.

cxobj* xpath_each ( cxobj *  cxtop,
char *  xpath,
cxobj *  xprev 
)

A restricted xpath iterator that loops over all matching entries. Dont use.

See xpath1() on details for subset.

Parameters
[in]cxtopxml-tree where to search
[in]xpathstring with XPATH syntax
[in]xpreviterator/result should be initiated to NULL
Return values
xml-treeof n:th match, or NULL on error.
cxobj *x = NULL;
while ((x = xpath_each(cxtop, "//symbol/foo", x)) != NULL) {
...
}

Note that the returned pointer points into the original tree so should not be freed after use.

See Also
also xpath, xpath_vec. NOTE: uses a static variable: consider replacing with xpath_vec() instead
static int xpath_element_free ( struct xpath_element *  xe)
static
static int xpath_element_new ( enum axis_type  atype,
char *  str,
struct xpath_element ***  xpnext 
)
static
static int xpath_exec ( char *  xpath,
cxobj **  vec0,
size_t  vec0len,
uint16_t  flags,
cxobj ***  vec2,
size_t *  vec2len 
)
static

Process single xpath expression on xml tree

Parameters
[in]xpathstring with XPATH syntax
[in]vec0vector of XML trees
[in]vec0lenlength of XML trees
[in]flagsif != 0, only match xml nodes matching flags
[out]vec2Result XML node vector
[out]vec2lenLength of result vector.
static int xpath_expr ( char *  predicate_expression,
uint16_t  flags,
cxobj ***  vec0,
size_t *  vec0len 
)
static

XPath predicate expression check

Parameters
[in]predicate_expressionxpath expression as a string
[in]flagsExtra xml flag checks that must match (apart from predicate)
[in,out]vec0Vector or xml nodes that are checked. Not matched are filtered
[in,out]vec0lenLength of vector or matches On input, vec0 contains a list of xml nodes to match. On output, vec0 contains only the subset that matched the expression. The predicate expression is a subset of the standard, namely:
  • <attr>=
  • <number>
  • <name>=

# RelationalExpr '=' RelationalExpr

See Also
https://www.w3.org/TR/xpath/#predicates
static int xpath_find ( struct xpath_element *  xe,
int  descendants0,
cxobj **  vec0,
size_t  vec0len,
uint16_t  flags,
cxobj ***  vec2,
size_t *  vec2len 
)
static

Given vec0, add matches to vec1

Parameters
[in]xeXPATH in structured (parsed) form
[in]descendants0
[in]vec0vector of XML trees
[in]vec0lenlength of XML trees
[in]flagsif != 0, only match xml nodes matching flags
[out]vec2Result XML node vector
[out]vec2lenLength of result vector.
cxobj* xpath_first ( cxobj *  cxtop,
char *  format,
  ... 
)

A restricted xpath function where the first matching entry is returned See xpath1() on details for subset. args:

Parameters
[in]cxtopxml-tree where to search
[in]xpathstring with XPATH syntax
Return values
xml-treeof first match, or NULL on error.
cxobj *x;
if ((x = xpath_first(xtop, "//symbol/foo")) != NULL) {
...
}

Note that the returned pointer points into the original tree so should not be freed after use.

See Also
also xpath_vec.
static cxobj* xpath_first0 ( cxobj *  cxtop,
char *  xpath 
)
static
static int xpath_free ( struct xpath_element *  xplist)
static
static int xpath_parse ( char *  xpath,
struct xpath_element **  xplist0 
)
static
static int xpath_parse_predicate ( struct xpath_element *  xe,
char *  pred 
)
static
static int xpath_print ( FILE *  f,
struct xpath_element *  xplist 
)
static
static int xpath_split ( char *  xpathstr,
char **  pathexpr 
)
static

Transform eg "a/b[kalle]" -> "a/b" e="kalle"

Parameters
[in,out]xpathstrEg "a/b[kalle]" -> "a/b"
[out]pathexprEg "kalle" Which also means: "a/b[foo][bar]" -> pathexpr: "foo][bar"
Note
destructively modify xpathstr, no new strings allocated
int xpath_vec ( cxobj *  cxtop,
char *  format,
cxobj ***  vec,
size_t *  veclen,
  ... 
)

A restricted xpath that returns a vector of matches

See xpath1() on details for subset . *

Parameters
[in]cxtopxml-tree where to search
[in]xpathstring with XPATH syntax
[out]vecvector of xml-trees. Vector must be free():d after use
[out]veclenreturns length of vector in return value
Return values
0OK
-1error.
cxobj **vec;
size_t veclen;
if (xpath_vec(cxtop, "//symbol/foo", &vec, &veclen) < 0)
goto err;
for (i=0; i<veclen; i++){
xn = vec[i];
...
}
free(vec);

that although the returned vector must be freed after use, the returned xml trees need not be.

See Also
also xpath_first, xpath_each.
int xpath_vec_flag ( cxobj *  cxtop,
char *  format,
uint16_t  flags,
cxobj ***  vec,
size_t *  veclen,
  ... 
)

Variable Documentation

const struct map_str2int atmap[]
static
Initial value:
= {
{"self", A_SELF},
{"child", A_CHILD},
{"parent", A_PARENT},
{"root", A_ROOT},
{"ancestor", A_ANCESTOR},
{"descendant-or-self", A_DESCENDANT_OR_SELF},
{NULL, -1}
}
Definition: clixon_xsl.c:128
Definition: clixon_xsl.c:126
input_stmt NULL
Definition: clixon_yang_parse.y:899
Definition: clixon_xsl.c:130
Definition: clixon_xsl.c:125
Definition: clixon_xsl.c:127
Definition: clixon_xsl.c:129
struct searchvec clixon_xml_parseerror