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