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