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 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 15 | #include <libxml/tree.h> |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 16 | #include <libxml/hash.h> |
Daniel Veillard | b24054a | 1999-12-18 15:32:46 +0000 | [diff] [blame] | 17 | |
Daniel Veillard | e4e5131 | 1999-12-18 15:32:46 +0000 | [diff] [blame] | 18 | #ifdef __cplusplus |
Daniel Veillard | 5cb5ab8 | 1999-12-21 15:35:29 +0000 | [diff] [blame] | 19 | extern "C" { |
Daniel Veillard | e4e5131 | 1999-12-18 15:32:46 +0000 | [diff] [blame] | 20 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 21 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 22 | typedef struct _xmlXPathContext xmlXPathContext; |
| 23 | typedef xmlXPathContext *xmlXPathContextPtr; |
| 24 | typedef struct _xmlXPathParserContext xmlXPathParserContext; |
| 25 | typedef xmlXPathParserContext *xmlXPathParserContextPtr; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 26 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 27 | /** |
| 28 | * The set of XPath error codes |
| 29 | */ |
| 30 | |
| 31 | typedef enum { |
| 32 | XPATH_EXPRESSION_OK = 0, |
| 33 | XPATH_NUMBER_ERROR, |
| 34 | XPATH_UNFINISHED_LITERAL_ERROR, |
| 35 | XPATH_START_LITERAL_ERROR, |
| 36 | XPATH_VARIABLE_REF_ERROR, |
| 37 | XPATH_UNDEF_VARIABLE_ERROR, |
| 38 | XPATH_INVALID_PREDICATE_ERROR, |
| 39 | XPATH_EXPR_ERROR, |
| 40 | XPATH_UNCLOSED_ERROR, |
| 41 | XPATH_UNKNOWN_FUNC_ERROR, |
| 42 | XPATH_INVALID_OPERAND, |
| 43 | XPATH_INVALID_TYPE, |
| 44 | XPATH_INVALID_ARITY, |
| 45 | XPATH_INVALID_CTXT_SIZE, |
Daniel Veillard | b71379b | 2000-10-09 12:30:39 +0000 | [diff] [blame] | 46 | XPATH_INVALID_CTXT_POSITION, |
| 47 | XPATH_MEMORY_ERROR, |
| 48 | XPTR_SYNTAX_ERROR, |
| 49 | XPTR_RESOURCE_ERROR, |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 50 | XPTR_SUB_RESOURCE_ERROR, |
| 51 | XPATH_UNDEF_PREFIX_ERROR |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 52 | } xmlXPathError; |
| 53 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 54 | /* |
| 55 | * A node-set (an unordered collection of nodes without duplicates) |
| 56 | */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 57 | typedef struct _xmlNodeSet xmlNodeSet; |
| 58 | typedef xmlNodeSet *xmlNodeSetPtr; |
| 59 | struct _xmlNodeSet { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 60 | int nodeNr; /* number of nodes in the set */ |
| 61 | int nodeMax; /* size of the array as allocated */ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 62 | xmlNodePtr *nodeTab; /* array of nodes in no particular order */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 63 | }; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * An expression is evaluated to yield an object, which |
| 67 | * has one of the following four basic types: |
| 68 | * - node-set |
| 69 | * - boolean |
| 70 | * - number |
| 71 | * - string |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 72 | * |
| 73 | * @@ XPointer will add more types ! |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 74 | */ |
| 75 | |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 76 | typedef enum { |
| 77 | XPATH_UNDEFINED = 0, |
| 78 | XPATH_NODESET = 1, |
| 79 | XPATH_BOOLEAN = 2, |
| 80 | XPATH_NUMBER = 3, |
| 81 | XPATH_STRING = 4, |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 82 | XPATH_POINT = 5, |
| 83 | XPATH_RANGE = 6, |
| 84 | XPATH_LOCATIONSET = 7, |
| 85 | XPATH_USERS = 8 |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 86 | } xmlXPathObjectType; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 87 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 88 | typedef struct _xmlXPathObject xmlXPathObject; |
| 89 | typedef xmlXPathObject *xmlXPathObjectPtr; |
| 90 | struct _xmlXPathObject { |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 91 | xmlXPathObjectType type; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 92 | xmlNodeSetPtr nodesetval; |
| 93 | int boolval; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 94 | double floatval; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 95 | xmlChar *stringval; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 96 | void *user; |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 97 | int index; |
| 98 | void *user2; |
| 99 | int index2; |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 100 | }; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 101 | |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 102 | /* |
| 103 | * A conversion function is associated to a type and used to cast |
| 104 | * the new type to primitive values. |
| 105 | */ |
| 106 | typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type); |
| 107 | |
| 108 | /* |
| 109 | * Extra type: a name and a conversion function. |
| 110 | */ |
| 111 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 112 | typedef struct _xmlXPathType xmlXPathType; |
| 113 | typedef xmlXPathType *xmlXPathTypePtr; |
| 114 | struct _xmlXPathType { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 115 | const xmlChar *name; /* the type name */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 116 | xmlXPathConvertFunc func; /* the conversion function */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 117 | }; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * Extra variable: a name and a value. |
| 121 | */ |
| 122 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 123 | typedef struct _xmlXPathVariable xmlXPathVariable; |
| 124 | typedef xmlXPathVariable *xmlXPathVariablePtr; |
| 125 | struct _xmlXPathVariable { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 126 | const xmlChar *name; /* the variable name */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 127 | xmlXPathObjectPtr value; /* the value */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 128 | }; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * an evaluation function, the parameters are on the context stack |
| 132 | */ |
| 133 | |
| 134 | typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs); |
| 135 | |
| 136 | /* |
| 137 | * Extra function: a name and a evaluation function. |
| 138 | */ |
| 139 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 140 | typedef struct _xmlXPathFunct xmlXPathFunct; |
| 141 | typedef xmlXPathFunct *xmlXPathFuncPtr; |
| 142 | struct _xmlXPathFunct { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 143 | const xmlChar *name; /* the function name */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 144 | xmlXPathEvalFunc func; /* the evaluation function */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 145 | }; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * An axis traversal function. To traverse an axis, the engine calls |
| 149 | * the first time with cur == NULL and repeat until the function returns |
| 150 | * NULL indicating the end of the axis traversal. |
| 151 | */ |
| 152 | |
| 153 | typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt, |
| 154 | xmlXPathObjectPtr cur); |
| 155 | |
| 156 | /* |
| 157 | * Extra axis: a name and an axis function. |
| 158 | */ |
| 159 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 160 | typedef struct _xmlXPathAxis xmlXPathAxis; |
| 161 | typedef xmlXPathAxis *xmlXPathAxisPtr; |
| 162 | struct _xmlXPathAxis { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 163 | const xmlChar *name; /* the axis name */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 164 | xmlXPathAxisFunc func; /* the search function */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 165 | }; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 166 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 167 | /* |
| 168 | * Expression evaluation occurs with respect to a context. |
| 169 | * he context consists of: |
| 170 | * - a node (the context node) |
| 171 | * - a node list (the context node list) |
| 172 | * - a set of variable bindings |
| 173 | * - a function library |
| 174 | * - the set of namespace declarations in scope for the expression |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 175 | * Following the switch to hash tables, this need to be trimmed up at |
| 176 | * the next binary incompatible release. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 177 | */ |
| 178 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 179 | struct _xmlXPathContext { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 180 | xmlDocPtr doc; /* The current document */ |
| 181 | xmlNodePtr node; /* The current node */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 182 | |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 183 | int nb_variables_unused; /* unused (hash table) */ |
| 184 | int max_variables_unused; /* unused (hash table) */ |
| 185 | xmlHashTablePtr varHash; /* Hash table of defined variables */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 186 | |
| 187 | int nb_types; /* number of defined types */ |
| 188 | int max_types; /* max number of types */ |
Daniel Veillard | c2df4cd | 2000-10-12 23:15:24 +0000 | [diff] [blame] | 189 | xmlXPathTypePtr types; /* Array of defined types */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 190 | |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 191 | int nb_funcs_unused; /* unused (hash table) */ |
| 192 | int max_funcs_unused; /* unused (hash table) */ |
| 193 | xmlHashTablePtr funcHash; /* Hash table of defined funcs */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 194 | |
| 195 | int nb_axis; /* number of defined axis */ |
| 196 | int max_axis; /* max number of axis */ |
Daniel Veillard | c2df4cd | 2000-10-12 23:15:24 +0000 | [diff] [blame] | 197 | xmlXPathAxisPtr axis; /* Array of defined axis */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 198 | |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 199 | /* the namespace nodes of the context node */ |
| 200 | xmlNsPtr *namespaces; /* Array of namespaces */ |
| 201 | int nsNr; /* number of namespace in scope */ |
| 202 | void *user; /* function to free */ |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 203 | |
| 204 | /* extra variables */ |
| 205 | int contextSize; /* the context size */ |
| 206 | int proximityPosition; /* the proximity position */ |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 207 | |
| 208 | /* extra stuff for XPointer */ |
| 209 | int xptr; /* it this an XPointer context */ |
| 210 | xmlNodePtr here; /* for here() */ |
| 211 | xmlNodePtr origin; /* for origin() */ |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 212 | |
| 213 | /* the set of namespace declarations in scope for the expression */ |
| 214 | xmlHashTablePtr nsHash; /* The namespaces hash table */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 215 | }; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * An XPath parser context, it contains pure parsing informations, |
| 219 | * an xmlXPathContext, and the stack of objects. |
| 220 | */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 221 | struct _xmlXPathParserContext { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 222 | const xmlChar *cur; /* the current char being parsed */ |
| 223 | const xmlChar *base; /* the full expression */ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 224 | |
| 225 | int error; /* error code */ |
| 226 | |
| 227 | xmlXPathContextPtr context; /* the evaluation context */ |
| 228 | xmlXPathObjectPtr value; /* the current value */ |
| 229 | int valueNr; /* number of values stacked */ |
| 230 | int valueMax; /* max number of values stacked */ |
| 231 | xmlXPathObjectPtr *valueTab; /* stack of values */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 232 | }; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 233 | |
| 234 | /* |
| 235 | * An XPath function |
| 236 | * The arguments (if any) are popped out of the context stack |
| 237 | * and the result is pushed on the stack. |
| 238 | */ |
| 239 | |
| 240 | typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs); |
| 241 | |
| 242 | /************************************************************************ |
| 243 | * * |
| 244 | * Public API * |
| 245 | * * |
| 246 | ************************************************************************/ |
| 247 | |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 248 | /** |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 249 | * Evaluation functions. |
| 250 | */ |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 251 | void xmlXPathInit (void); |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 252 | xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 253 | void xmlXPathFreeContext (xmlXPathContextPtr ctxt); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 254 | xmlXPathObjectPtr xmlXPathEval (const xmlChar *str, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 255 | xmlXPathContextPtr ctxt); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 256 | xmlXPathObjectPtr xmlXPathEvalXPtrExpr (const xmlChar *str, |
| 257 | xmlXPathContextPtr ctxt); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 258 | void xmlXPathFreeObject (xmlXPathObjectPtr obj); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 259 | xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 260 | xmlXPathContextPtr ctxt); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 261 | xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val); |
| 262 | void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj); |
| 263 | void xmlXPathFreeNodeSet (xmlNodeSetPtr obj); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 264 | xmlXPathObjectPtr xmlXPathObjectCopy (xmlXPathObjectPtr val); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 265 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 266 | |
Daniel Veillard | e4e5131 | 1999-12-18 15:32:46 +0000 | [diff] [blame] | 267 | #ifdef __cplusplus |
| 268 | } |
| 269 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 270 | #endif /* ! __XML_XPATH_H__ */ |