Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +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 <libxml/tree.h> |
| 16 | #include <libxml/hash.h> |
| 17 | |
| 18 | #ifdef __cplusplus |
| 19 | extern "C" { |
| 20 | #endif |
| 21 | |
| 22 | typedef struct _xmlXPathContext xmlXPathContext; |
| 23 | typedef xmlXPathContext *xmlXPathContextPtr; |
| 24 | typedef struct _xmlXPathParserContext xmlXPathParserContext; |
| 25 | typedef xmlXPathParserContext *xmlXPathParserContextPtr; |
| 26 | |
| 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, |
| 46 | XPATH_INVALID_CTXT_POSITION, |
| 47 | XPATH_MEMORY_ERROR, |
| 48 | XPTR_SYNTAX_ERROR, |
| 49 | XPTR_RESOURCE_ERROR, |
| 50 | XPTR_SUB_RESOURCE_ERROR, |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame^] | 51 | XPATH_UNDEF_PREFIX_ERROR, |
| 52 | XPATH_ENCODING_ERROR, |
| 53 | XPATH_INVALID_CHAR_ERROR |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 54 | } xmlXPathError; |
| 55 | |
| 56 | /* |
| 57 | * A node-set (an unordered collection of nodes without duplicates) |
| 58 | */ |
| 59 | typedef struct _xmlNodeSet xmlNodeSet; |
| 60 | typedef xmlNodeSet *xmlNodeSetPtr; |
| 61 | struct _xmlNodeSet { |
| 62 | int nodeNr; /* number of nodes in the set */ |
| 63 | int nodeMax; /* size of the array as allocated */ |
| 64 | xmlNodePtr *nodeTab; /* array of nodes in no particular order */ |
| 65 | }; |
| 66 | |
| 67 | /* |
| 68 | * An expression is evaluated to yield an object, which |
| 69 | * has one of the following four basic types: |
| 70 | * - node-set |
| 71 | * - boolean |
| 72 | * - number |
| 73 | * - string |
| 74 | * |
| 75 | * @@ XPointer will add more types ! |
| 76 | */ |
| 77 | |
| 78 | typedef enum { |
| 79 | XPATH_UNDEFINED = 0, |
| 80 | XPATH_NODESET = 1, |
| 81 | XPATH_BOOLEAN = 2, |
| 82 | XPATH_NUMBER = 3, |
| 83 | XPATH_STRING = 4, |
| 84 | XPATH_POINT = 5, |
| 85 | XPATH_RANGE = 6, |
| 86 | XPATH_LOCATIONSET = 7, |
| 87 | XPATH_USERS = 8, |
| 88 | XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */ |
| 89 | } xmlXPathObjectType; |
| 90 | |
| 91 | typedef struct _xmlXPathObject xmlXPathObject; |
| 92 | typedef xmlXPathObject *xmlXPathObjectPtr; |
| 93 | struct _xmlXPathObject { |
| 94 | xmlXPathObjectType type; |
| 95 | xmlNodeSetPtr nodesetval; |
| 96 | int boolval; |
| 97 | double floatval; |
| 98 | xmlChar *stringval; |
| 99 | void *user; |
| 100 | int index; |
| 101 | void *user2; |
| 102 | int index2; |
| 103 | }; |
| 104 | |
| 105 | /* |
| 106 | * A conversion function is associated to a type and used to cast |
| 107 | * the new type to primitive values. |
| 108 | */ |
| 109 | typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type); |
| 110 | |
| 111 | /* |
| 112 | * Extra type: a name and a conversion function. |
| 113 | */ |
| 114 | |
| 115 | typedef struct _xmlXPathType xmlXPathType; |
| 116 | typedef xmlXPathType *xmlXPathTypePtr; |
| 117 | struct _xmlXPathType { |
| 118 | const xmlChar *name; /* the type name */ |
| 119 | xmlXPathConvertFunc func; /* the conversion function */ |
| 120 | }; |
| 121 | |
| 122 | /* |
| 123 | * Extra variable: a name and a value. |
| 124 | */ |
| 125 | |
| 126 | typedef struct _xmlXPathVariable xmlXPathVariable; |
| 127 | typedef xmlXPathVariable *xmlXPathVariablePtr; |
| 128 | struct _xmlXPathVariable { |
| 129 | const xmlChar *name; /* the variable name */ |
| 130 | xmlXPathObjectPtr value; /* the value */ |
| 131 | }; |
| 132 | |
| 133 | /* |
| 134 | * an evaluation function, the parameters are on the context stack |
| 135 | */ |
| 136 | |
| 137 | typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs); |
| 138 | |
| 139 | /* |
| 140 | * Extra function: a name and a evaluation function. |
| 141 | */ |
| 142 | |
| 143 | typedef struct _xmlXPathFunct xmlXPathFunct; |
| 144 | typedef xmlXPathFunct *xmlXPathFuncPtr; |
| 145 | struct _xmlXPathFunct { |
| 146 | const xmlChar *name; /* the function name */ |
| 147 | xmlXPathEvalFunc func; /* the evaluation function */ |
| 148 | }; |
| 149 | |
| 150 | /* |
| 151 | * An axis traversal function. To traverse an axis, the engine calls |
| 152 | * the first time with cur == NULL and repeat until the function returns |
| 153 | * NULL indicating the end of the axis traversal. |
| 154 | */ |
| 155 | |
| 156 | typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt, |
| 157 | xmlXPathObjectPtr cur); |
| 158 | |
| 159 | /* |
| 160 | * Extra axis: a name and an axis function. |
| 161 | */ |
| 162 | |
| 163 | typedef struct _xmlXPathAxis xmlXPathAxis; |
| 164 | typedef xmlXPathAxis *xmlXPathAxisPtr; |
| 165 | struct _xmlXPathAxis { |
| 166 | const xmlChar *name; /* the axis name */ |
| 167 | xmlXPathAxisFunc func; /* the search function */ |
| 168 | }; |
| 169 | |
| 170 | /* |
| 171 | * Expression evaluation occurs with respect to a context. |
| 172 | * he context consists of: |
| 173 | * - a node (the context node) |
| 174 | * - a node list (the context node list) |
| 175 | * - a set of variable bindings |
| 176 | * - a function library |
| 177 | * - the set of namespace declarations in scope for the expression |
| 178 | * Following the switch to hash tables, this need to be trimmed up at |
| 179 | * the next binary incompatible release. |
| 180 | */ |
| 181 | |
| 182 | struct _xmlXPathContext { |
| 183 | xmlDocPtr doc; /* The current document */ |
| 184 | xmlNodePtr node; /* The current node */ |
| 185 | |
| 186 | int nb_variables_unused; /* unused (hash table) */ |
| 187 | int max_variables_unused; /* unused (hash table) */ |
| 188 | xmlHashTablePtr varHash; /* Hash table of defined variables */ |
| 189 | |
| 190 | int nb_types; /* number of defined types */ |
| 191 | int max_types; /* max number of types */ |
| 192 | xmlXPathTypePtr types; /* Array of defined types */ |
| 193 | |
| 194 | int nb_funcs_unused; /* unused (hash table) */ |
| 195 | int max_funcs_unused; /* unused (hash table) */ |
| 196 | xmlHashTablePtr funcHash; /* Hash table of defined funcs */ |
| 197 | |
| 198 | int nb_axis; /* number of defined axis */ |
| 199 | int max_axis; /* max number of axis */ |
| 200 | xmlXPathAxisPtr axis; /* Array of defined axis */ |
| 201 | |
| 202 | /* the namespace nodes of the context node */ |
| 203 | xmlNsPtr *namespaces; /* Array of namespaces */ |
| 204 | int nsNr; /* number of namespace in scope */ |
| 205 | void *user; /* function to free */ |
| 206 | |
| 207 | /* extra variables */ |
| 208 | int contextSize; /* the context size */ |
| 209 | int proximityPosition; /* the proximity position */ |
| 210 | |
| 211 | /* extra stuff for XPointer */ |
| 212 | int xptr; /* it this an XPointer context */ |
| 213 | xmlNodePtr here; /* for here() */ |
| 214 | xmlNodePtr origin; /* for origin() */ |
| 215 | |
| 216 | /* the set of namespace declarations in scope for the expression */ |
| 217 | xmlHashTablePtr nsHash; /* The namespaces hash table */ |
| 218 | void *varLookupFunc; /* variable lookup func */ |
| 219 | void *varLookupData; /* variable lookup data */ |
| 220 | |
| 221 | /* Possibility to link in an extra item */ |
| 222 | void *extra; /* needed for XSLT */ |
| 223 | }; |
| 224 | |
| 225 | /* |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 226 | * The structure of a compiled expression form is not public |
| 227 | */ |
| 228 | |
| 229 | typedef struct _xmlXPathCompExpr xmlXPathCompExpr; |
| 230 | typedef xmlXPathCompExpr *xmlXPathCompExprPtr; |
| 231 | |
| 232 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 233 | * An XPath parser context, it contains pure parsing informations, |
| 234 | * an xmlXPathContext, and the stack of objects. |
| 235 | */ |
| 236 | struct _xmlXPathParserContext { |
| 237 | const xmlChar *cur; /* the current char being parsed */ |
| 238 | const xmlChar *base; /* the full expression */ |
| 239 | |
| 240 | int error; /* error code */ |
| 241 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 242 | xmlXPathContextPtr context; /* the evaluation context */ |
| 243 | xmlXPathObjectPtr value; /* the current value */ |
| 244 | int valueNr; /* number of values stacked */ |
| 245 | int valueMax; /* max number of values stacked */ |
| 246 | xmlXPathObjectPtr *valueTab; /* stack of values */ |
Daniel Veillard | d007d6c | 2001-03-19 00:01:07 +0000 | [diff] [blame] | 247 | |
| 248 | xmlXPathCompExprPtr comp; /* the precompiled expression */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 249 | int xptr; /* it this an XPointer expression */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | /* |
| 253 | * An XPath function |
| 254 | * The arguments (if any) are popped out of the context stack |
| 255 | * and the result is pushed on the stack. |
| 256 | */ |
| 257 | |
| 258 | typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs); |
| 259 | |
| 260 | /************************************************************************ |
| 261 | * * |
| 262 | * Public API * |
| 263 | * * |
| 264 | ************************************************************************/ |
| 265 | |
| 266 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 267 | * Objects and Nodesets handling |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 268 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 269 | void xmlXPathFreeObject (xmlXPathObjectPtr obj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 270 | xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val); |
| 271 | void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj); |
| 272 | void xmlXPathFreeNodeSet (xmlNodeSetPtr obj); |
| 273 | xmlXPathObjectPtr xmlXPathObjectCopy (xmlXPathObjectPtr val); |
| 274 | int xmlXPathCmpNodes (xmlNodePtr node1, |
| 275 | xmlNodePtr node2); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 276 | /** |
| 277 | * Conversion functions to basic types |
| 278 | */ |
| 279 | xmlXPathObjectPtr xmlXPathConvertBoolean (xmlXPathObjectPtr val); |
| 280 | xmlXPathObjectPtr xmlXPathConvertNumber (xmlXPathObjectPtr val); |
| 281 | xmlXPathObjectPtr xmlXPathConvertString (xmlXPathObjectPtr val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 282 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 283 | /** |
| 284 | * Context handling |
| 285 | */ |
| 286 | void xmlXPathInit (void); |
| 287 | xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc); |
| 288 | void xmlXPathFreeContext (xmlXPathContextPtr ctxt); |
| 289 | |
| 290 | /** |
| 291 | * Evaluation functions. |
| 292 | */ |
| 293 | xmlXPathObjectPtr xmlXPathEval (const xmlChar *str, |
| 294 | xmlXPathContextPtr ctxt); |
| 295 | xmlXPathObjectPtr xmlXPathEvalXPtrExpr (const xmlChar *str, |
| 296 | xmlXPathContextPtr ctxt); |
| 297 | xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str, |
| 298 | xmlXPathContextPtr ctxt); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 299 | int xmlXPathEvalPredicate (xmlXPathContextPtr ctxt, |
| 300 | xmlXPathObjectPtr res); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 301 | /** |
| 302 | * Separate compilation/evaluation entry points |
| 303 | */ |
| 304 | xmlXPathCompExprPtr xmlXPathCompile (const xmlChar *str); |
| 305 | xmlXPathObjectPtr xmlXPathCompiledEval (xmlXPathCompExprPtr comp, |
| 306 | xmlXPathContextPtr ctx); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 307 | void xmlXPathFreeCompExpr (xmlXPathCompExprPtr comp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 308 | #ifdef __cplusplus |
| 309 | } |
| 310 | #endif |
| 311 | #endif /* ! __XML_XPATH_H__ */ |