Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * xpath.c: interface for XML Path Language implementation |
| 3 | * |
| 4 | * Reference: W3C Working Draft 5 July 1999 |
| 5 | * http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html |
| 6 | * |
| 7 | * See COPYRIGHT for the status of this software |
| 8 | * |
| 9 | * Author: Daniel.Veillard@w3.org |
| 10 | */ |
| 11 | |
| 12 | #ifndef __XML_XPATH_H__ |
| 13 | #define __XML_XPATH_H__ |
| 14 | |
| 15 | #include "tree.h" |
| 16 | |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 17 | typedef struct xmlXPathParserContext *xmlXPathParserContextPtr; |
| 18 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 19 | /* |
| 20 | * A node-set (an unordered collection of nodes without duplicates) |
| 21 | */ |
| 22 | typedef struct xmlNodeSet { |
| 23 | int nodeNr; /* # of node in the set */ |
| 24 | int nodeMax; /* allocated space */ |
| 25 | xmlNodePtr *nodeTab; /* array of nodes in no particular order */ |
| 26 | } xmlNodeSet, *xmlNodeSetPtr; |
| 27 | |
| 28 | /* |
| 29 | * An expression is evaluated to yield an object, which |
| 30 | * has one of the following four basic types: |
| 31 | * - node-set |
| 32 | * - boolean |
| 33 | * - number |
| 34 | * - string |
| 35 | */ |
| 36 | |
| 37 | #define XPATH_UNDEFINED 0 |
| 38 | #define XPATH_NODESET 1 |
| 39 | #define XPATH_BOOLEAN 2 |
| 40 | #define XPATH_NUMBER 3 |
| 41 | #define XPATH_STRING 4 |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 42 | #define XPATH_USERS 5 |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 43 | |
| 44 | typedef struct xmlXPathObject { |
| 45 | int type; |
| 46 | xmlNodeSetPtr nodesetval; |
| 47 | int boolval; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 48 | double floatval; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 49 | CHAR *stringval; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 50 | void *user; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 51 | } xmlXPathObject, *xmlXPathObjectPtr; |
| 52 | |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 53 | /* |
| 54 | * A conversion function is associated to a type and used to cast |
| 55 | * the new type to primitive values. |
| 56 | */ |
| 57 | typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type); |
| 58 | |
| 59 | /* |
| 60 | * Extra type: a name and a conversion function. |
| 61 | */ |
| 62 | |
| 63 | typedef struct xmlXPathType { |
| 64 | const CHAR *name; /* the type name */ |
| 65 | xmlXPathConvertFunc func; /* the conversion function */ |
| 66 | } xmlXPathType, *xmlXPathTypePtr; |
| 67 | |
| 68 | /* |
| 69 | * Extra variable: a name and a value. |
| 70 | */ |
| 71 | |
| 72 | typedef struct xmlXPathVariable { |
| 73 | const CHAR *name; /* the variable name */ |
| 74 | xmlXPathObjectPtr value; /* the value */ |
| 75 | } xmlXPathVariable, *xmlXPathVariablePtr; |
| 76 | |
| 77 | /* |
| 78 | * an evaluation function, the parameters are on the context stack |
| 79 | */ |
| 80 | |
| 81 | typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs); |
| 82 | |
| 83 | /* |
| 84 | * Extra function: a name and a evaluation function. |
| 85 | */ |
| 86 | |
| 87 | typedef struct xmlXPathFunct { |
| 88 | const CHAR *name; /* the function name */ |
| 89 | xmlXPathEvalFunc func; /* the evaluation function */ |
| 90 | } xmlXPathFunc, *xmlXPathFuncPtr; |
| 91 | |
| 92 | /* |
| 93 | * An axis traversal function. To traverse an axis, the engine calls |
| 94 | * the first time with cur == NULL and repeat until the function returns |
| 95 | * NULL indicating the end of the axis traversal. |
| 96 | */ |
| 97 | |
| 98 | typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt, |
| 99 | xmlXPathObjectPtr cur); |
| 100 | |
| 101 | /* |
| 102 | * Extra axis: a name and an axis function. |
| 103 | */ |
| 104 | |
| 105 | typedef struct xmlXPathAxis { |
| 106 | const CHAR *name; /* the axis name */ |
| 107 | xmlXPathAxisFunc func; /* the search function */ |
| 108 | } xmlXPathAxis, *xmlXPathAxisPtr; |
| 109 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 110 | /* |
| 111 | * Expression evaluation occurs with respect to a context. |
| 112 | * he context consists of: |
| 113 | * - a node (the context node) |
| 114 | * - a node list (the context node list) |
| 115 | * - a set of variable bindings |
| 116 | * - a function library |
| 117 | * - the set of namespace declarations in scope for the expression |
| 118 | */ |
| 119 | |
| 120 | typedef struct xmlXPathContext { |
| 121 | xmlDocPtr doc; /* The current document */ |
| 122 | xmlNodePtr node; /* The current node */ |
| 123 | xmlNodeSetPtr nodelist; /* The current node list */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 124 | |
| 125 | int nb_variables; /* number of defined variables */ |
| 126 | int max_variables; /* max number of variables */ |
| 127 | xmlXPathVariablePtr *variables; /* Array of defined variables */ |
| 128 | |
| 129 | int nb_types; /* number of defined types */ |
| 130 | int max_types; /* max number of types */ |
| 131 | xmlXPathTypePtr *types; /* Array of defined types */ |
| 132 | |
| 133 | int nb_funcs; /* number of defined funcs */ |
| 134 | int max_funcs; /* max number of funcs */ |
| 135 | xmlXPathFuncPtr *funcs; /* Array of defined funcs */ |
| 136 | |
| 137 | int nb_axis; /* number of defined axis */ |
| 138 | int max_axis; /* max number of axis */ |
| 139 | xmlXPathAxisPtr *axis; /* Array of defined axis */ |
| 140 | |
| 141 | /* Namespace traversal should be implemented with user */ |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 142 | xmlNsPtr *namespaces; /* The namespaces lookup */ |
| 143 | int nsNr; /* the current Namespace index */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 144 | void *user; /* user defined extra info */ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 145 | } xmlXPathContext, *xmlXPathContextPtr; |
| 146 | |
| 147 | /* |
| 148 | * An XPath parser context, it contains pure parsing informations, |
| 149 | * an xmlXPathContext, and the stack of objects. |
| 150 | */ |
| 151 | typedef struct xmlXPathParserContext { |
| 152 | const CHAR *cur; /* the current char being parsed */ |
| 153 | const CHAR *base; /* the full expression */ |
| 154 | |
| 155 | int error; /* error code */ |
| 156 | |
| 157 | xmlXPathContextPtr context; /* the evaluation context */ |
| 158 | xmlXPathObjectPtr value; /* the current value */ |
| 159 | int valueNr; /* number of values stacked */ |
| 160 | int valueMax; /* max number of values stacked */ |
| 161 | xmlXPathObjectPtr *valueTab; /* stack of values */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 162 | } xmlXPathParserContext; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 163 | |
| 164 | /* |
| 165 | * An XPath function |
| 166 | * The arguments (if any) are popped out of the context stack |
| 167 | * and the result is pushed on the stack. |
| 168 | */ |
| 169 | |
| 170 | typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs); |
| 171 | |
| 172 | /************************************************************************ |
| 173 | * * |
| 174 | * Public API * |
| 175 | * * |
| 176 | ************************************************************************/ |
| 177 | |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 178 | /** |
| 179 | * Registering extensions to the expression language |
| 180 | */ |
| 181 | /* TODO */ int xmlXPathRegisterType (xmlXPathContextPtr ctxt, |
| 182 | const CHAR *name, |
| 183 | xmlXPathConvertFunc f); |
| 184 | /* TODO */ int xmlXPathRegisterAxis (xmlXPathContextPtr ctxt, |
| 185 | const CHAR *name, |
| 186 | xmlXPathAxisFunc f); |
| 187 | /* TODO */ int xmlXPathRegisterFunc (xmlXPathContextPtr ctxt, |
| 188 | const CHAR *name, |
| 189 | xmlXPathFunction f); |
| 190 | /* TODO */ int xmlXPathRegisterVariable (xmlXPathContextPtr ctxt, |
| 191 | const CHAR *name, |
| 192 | xmlXPathObject value); |
| 193 | |
| 194 | /** |
| 195 | * Evaluation functions. |
| 196 | */ |
| 197 | xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 198 | void xmlXPathFreeContext (xmlXPathContextPtr ctxt); |
| 199 | xmlXPathObjectPtr xmlXPathEval (const CHAR *str, |
| 200 | xmlXPathContextPtr ctxt); |
| 201 | void xmlXPathFreeObject (xmlXPathObjectPtr obj); |
| 202 | xmlXPathObjectPtr xmlXPathEvalExpression (const CHAR *str, |
| 203 | xmlXPathContextPtr ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 204 | |
| 205 | #endif /* ! __XML_XPATH_H__ */ |