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, |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 85 | XPATH_USERS = 8, |
| 86 | XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */ |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 87 | } xmlXPathObjectType; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 88 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 89 | typedef struct _xmlXPathObject xmlXPathObject; |
| 90 | typedef xmlXPathObject *xmlXPathObjectPtr; |
| 91 | struct _xmlXPathObject { |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 92 | xmlXPathObjectType type; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 93 | xmlNodeSetPtr nodesetval; |
| 94 | int boolval; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 95 | double floatval; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 96 | xmlChar *stringval; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 97 | void *user; |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 98 | int index; |
| 99 | void *user2; |
| 100 | int index2; |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 101 | }; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 102 | |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 103 | /* |
| 104 | * A conversion function is associated to a type and used to cast |
| 105 | * the new type to primitive values. |
| 106 | */ |
| 107 | typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type); |
| 108 | |
| 109 | /* |
| 110 | * Extra type: a name and a conversion function. |
| 111 | */ |
| 112 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 113 | typedef struct _xmlXPathType xmlXPathType; |
| 114 | typedef xmlXPathType *xmlXPathTypePtr; |
| 115 | struct _xmlXPathType { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 116 | const xmlChar *name; /* the type name */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 117 | xmlXPathConvertFunc func; /* the conversion function */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 118 | }; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * Extra variable: a name and a value. |
| 122 | */ |
| 123 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 124 | typedef struct _xmlXPathVariable xmlXPathVariable; |
| 125 | typedef xmlXPathVariable *xmlXPathVariablePtr; |
| 126 | struct _xmlXPathVariable { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 127 | const xmlChar *name; /* the variable name */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 128 | xmlXPathObjectPtr value; /* the value */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 129 | }; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * an evaluation function, the parameters are on the context stack |
| 133 | */ |
| 134 | |
| 135 | typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs); |
| 136 | |
| 137 | /* |
| 138 | * Extra function: a name and a evaluation function. |
| 139 | */ |
| 140 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 141 | typedef struct _xmlXPathFunct xmlXPathFunct; |
| 142 | typedef xmlXPathFunct *xmlXPathFuncPtr; |
| 143 | struct _xmlXPathFunct { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 144 | const xmlChar *name; /* the function name */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 145 | xmlXPathEvalFunc func; /* the evaluation function */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 146 | }; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * An axis traversal function. To traverse an axis, the engine calls |
| 150 | * the first time with cur == NULL and repeat until the function returns |
| 151 | * NULL indicating the end of the axis traversal. |
| 152 | */ |
| 153 | |
| 154 | typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt, |
| 155 | xmlXPathObjectPtr cur); |
| 156 | |
| 157 | /* |
| 158 | * Extra axis: a name and an axis function. |
| 159 | */ |
| 160 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 161 | typedef struct _xmlXPathAxis xmlXPathAxis; |
| 162 | typedef xmlXPathAxis *xmlXPathAxisPtr; |
| 163 | struct _xmlXPathAxis { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 164 | const xmlChar *name; /* the axis name */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 165 | xmlXPathAxisFunc func; /* the search function */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 166 | }; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 167 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 168 | /* |
| 169 | * Expression evaluation occurs with respect to a context. |
| 170 | * he context consists of: |
| 171 | * - a node (the context node) |
| 172 | * - a node list (the context node list) |
| 173 | * - a set of variable bindings |
| 174 | * - a function library |
| 175 | * - the set of namespace declarations in scope for the expression |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 176 | * Following the switch to hash tables, this need to be trimmed up at |
| 177 | * the next binary incompatible release. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 178 | */ |
| 179 | |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 180 | struct _xmlXPathContext { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 181 | xmlDocPtr doc; /* The current document */ |
| 182 | xmlNodePtr node; /* The current node */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 183 | |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 184 | int nb_variables_unused; /* unused (hash table) */ |
| 185 | int max_variables_unused; /* unused (hash table) */ |
| 186 | xmlHashTablePtr varHash; /* Hash table of defined variables */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 187 | |
| 188 | int nb_types; /* number of defined types */ |
| 189 | int max_types; /* max number of types */ |
Daniel Veillard | c2df4cd | 2000-10-12 23:15:24 +0000 | [diff] [blame] | 190 | xmlXPathTypePtr types; /* Array of defined types */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 191 | |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 192 | int nb_funcs_unused; /* unused (hash table) */ |
| 193 | int max_funcs_unused; /* unused (hash table) */ |
| 194 | xmlHashTablePtr funcHash; /* Hash table of defined funcs */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 195 | |
| 196 | int nb_axis; /* number of defined axis */ |
| 197 | int max_axis; /* max number of axis */ |
Daniel Veillard | c2df4cd | 2000-10-12 23:15:24 +0000 | [diff] [blame] | 198 | xmlXPathAxisPtr axis; /* Array of defined axis */ |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 199 | |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 200 | /* the namespace nodes of the context node */ |
| 201 | xmlNsPtr *namespaces; /* Array of namespaces */ |
| 202 | int nsNr; /* number of namespace in scope */ |
| 203 | void *user; /* function to free */ |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 204 | |
| 205 | /* extra variables */ |
| 206 | int contextSize; /* the context size */ |
| 207 | int proximityPosition; /* the proximity position */ |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 208 | |
| 209 | /* extra stuff for XPointer */ |
| 210 | int xptr; /* it this an XPointer context */ |
| 211 | xmlNodePtr here; /* for here() */ |
| 212 | xmlNodePtr origin; /* for origin() */ |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 213 | |
| 214 | /* the set of namespace declarations in scope for the expression */ |
| 215 | xmlHashTablePtr nsHash; /* The namespaces hash table */ |
Daniel Veillard | 8f4d975 | 2001-01-19 05:32:34 +0000 | [diff] [blame] | 216 | void *varLookupFunc; /* variable lookup func */ |
| 217 | void *varLookupData; /* variable lookup data */ |
Daniel Veillard | 9308666 | 2001-01-25 18:13:04 +0000 | [diff] [blame] | 218 | |
| 219 | /* Possibility to link in an extra item */ |
| 220 | void *extra; /* needed for XSLT */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 221 | }; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * An XPath parser context, it contains pure parsing informations, |
| 225 | * an xmlXPathContext, and the stack of objects. |
| 226 | */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 227 | struct _xmlXPathParserContext { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 228 | const xmlChar *cur; /* the current char being parsed */ |
| 229 | const xmlChar *base; /* the full expression */ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 230 | |
| 231 | int error; /* error code */ |
| 232 | |
| 233 | xmlXPathContextPtr context; /* the evaluation context */ |
| 234 | xmlXPathObjectPtr value; /* the current value */ |
| 235 | int valueNr; /* number of values stacked */ |
| 236 | int valueMax; /* max number of values stacked */ |
| 237 | xmlXPathObjectPtr *valueTab; /* stack of values */ |
Daniel Veillard | 71b656e | 2000-01-05 14:46:17 +0000 | [diff] [blame] | 238 | }; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * An XPath function |
| 242 | * The arguments (if any) are popped out of the context stack |
| 243 | * and the result is pushed on the stack. |
| 244 | */ |
| 245 | |
| 246 | typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs); |
| 247 | |
| 248 | /************************************************************************ |
| 249 | * * |
| 250 | * Public API * |
| 251 | * * |
| 252 | ************************************************************************/ |
| 253 | |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 254 | /** |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 255 | * Evaluation functions. |
| 256 | */ |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 257 | void xmlXPathInit (void); |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 258 | xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 259 | void xmlXPathFreeContext (xmlXPathContextPtr ctxt); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 260 | xmlXPathObjectPtr xmlXPathEval (const xmlChar *str, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 261 | xmlXPathContextPtr ctxt); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 262 | xmlXPathObjectPtr xmlXPathEvalXPtrExpr (const xmlChar *str, |
| 263 | xmlXPathContextPtr ctxt); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 264 | void xmlXPathFreeObject (xmlXPathObjectPtr obj); |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 265 | xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 266 | xmlXPathContextPtr ctxt); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 267 | xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val); |
| 268 | void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj); |
| 269 | void xmlXPathFreeNodeSet (xmlNodeSetPtr obj); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 270 | xmlXPathObjectPtr xmlXPathObjectCopy (xmlXPathObjectPtr val); |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 271 | int xmlXPathCmpNodes (xmlNodePtr node1, |
| 272 | xmlNodePtr node2); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 273 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 274 | |
Daniel Veillard | e4e5131 | 1999-12-18 15:32:46 +0000 | [diff] [blame] | 275 | #ifdef __cplusplus |
| 276 | } |
| 277 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 278 | #endif /* ! __XML_XPATH_H__ */ |