blob: 533ca3fcbfe20159a15e741fd624aea1b08a6887 [file] [log] [blame]
Daniel Veillard1566d3a1999-07-15 14:24:29 +00001/*
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 Veillard361d8452000-04-03 19:48:13 +000015#include <libxml/tree.h>
Daniel Veillardb24054a1999-12-18 15:32:46 +000016
Daniel Veillarde4e51311999-12-18 15:32:46 +000017#ifdef __cplusplus
Daniel Veillard5cb5ab81999-12-21 15:35:29 +000018extern "C" {
Daniel Veillarde4e51311999-12-18 15:32:46 +000019#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000020
Daniel Veillard71b656e2000-01-05 14:46:17 +000021typedef struct _xmlXPathContext xmlXPathContext;
22typedef xmlXPathContext *xmlXPathContextPtr;
23typedef struct _xmlXPathParserContext xmlXPathParserContext;
24typedef xmlXPathParserContext *xmlXPathParserContextPtr;
Daniel Veillardc08a2c61999-09-08 21:35:25 +000025
Daniel Veillard55b91f22000-10-05 16:30:11 +000026/**
27 * The set of XPath error codes
28 */
29
30typedef enum {
31 XPATH_EXPRESSION_OK = 0,
32 XPATH_NUMBER_ERROR,
33 XPATH_UNFINISHED_LITERAL_ERROR,
34 XPATH_START_LITERAL_ERROR,
35 XPATH_VARIABLE_REF_ERROR,
36 XPATH_UNDEF_VARIABLE_ERROR,
37 XPATH_INVALID_PREDICATE_ERROR,
38 XPATH_EXPR_ERROR,
39 XPATH_UNCLOSED_ERROR,
40 XPATH_UNKNOWN_FUNC_ERROR,
41 XPATH_INVALID_OPERAND,
42 XPATH_INVALID_TYPE,
43 XPATH_INVALID_ARITY,
44 XPATH_INVALID_CTXT_SIZE,
Daniel Veillardb71379b2000-10-09 12:30:39 +000045 XPATH_INVALID_CTXT_POSITION,
46 XPATH_MEMORY_ERROR,
47 XPTR_SYNTAX_ERROR,
48 XPTR_RESOURCE_ERROR,
49 XPTR_SUB_RESOURCE_ERROR
Daniel Veillard55b91f22000-10-05 16:30:11 +000050} xmlXPathError;
51
Daniel Veillard1566d3a1999-07-15 14:24:29 +000052/*
53 * A node-set (an unordered collection of nodes without duplicates)
54 */
Daniel Veillard71b656e2000-01-05 14:46:17 +000055typedef struct _xmlNodeSet xmlNodeSet;
56typedef xmlNodeSet *xmlNodeSetPtr;
57struct _xmlNodeSet {
Daniel Veillardbe803962000-06-28 23:40:59 +000058 int nodeNr; /* number of nodes in the set */
59 int nodeMax; /* size of the array as allocated */
Daniel Veillard1566d3a1999-07-15 14:24:29 +000060 xmlNodePtr *nodeTab; /* array of nodes in no particular order */
Daniel Veillard71b656e2000-01-05 14:46:17 +000061};
Daniel Veillard1566d3a1999-07-15 14:24:29 +000062
63/*
64 * An expression is evaluated to yield an object, which
65 * has one of the following four basic types:
66 * - node-set
67 * - boolean
68 * - number
69 * - string
Daniel Veillardbe803962000-06-28 23:40:59 +000070 *
71 * @@ XPointer will add more types !
Daniel Veillard1566d3a1999-07-15 14:24:29 +000072 */
73
Daniel Veillardf09e7e32000-10-01 15:53:30 +000074typedef enum {
75 XPATH_UNDEFINED = 0,
76 XPATH_NODESET = 1,
77 XPATH_BOOLEAN = 2,
78 XPATH_NUMBER = 3,
79 XPATH_STRING = 4,
Daniel Veillardac260302000-10-04 13:33:43 +000080 XPATH_POINT = 5,
81 XPATH_RANGE = 6,
82 XPATH_LOCATIONSET = 7,
83 XPATH_USERS = 8
Daniel Veillardf09e7e32000-10-01 15:53:30 +000084} xmlXPathObjectType;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000085
Daniel Veillard71b656e2000-01-05 14:46:17 +000086typedef struct _xmlXPathObject xmlXPathObject;
87typedef xmlXPathObject *xmlXPathObjectPtr;
88struct _xmlXPathObject {
Daniel Veillardf09e7e32000-10-01 15:53:30 +000089 xmlXPathObjectType type;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000090 xmlNodeSetPtr nodesetval;
91 int boolval;
Daniel Veillarde2d034d1999-07-27 19:52:06 +000092 double floatval;
Daniel Veillarddd6b3671999-09-23 22:19:22 +000093 xmlChar *stringval;
Daniel Veillardc08a2c61999-09-08 21:35:25 +000094 void *user;
Daniel Veillardac260302000-10-04 13:33:43 +000095 int index;
96 void *user2;
97 int index2;
Daniel Veillard71b656e2000-01-05 14:46:17 +000098};
Daniel Veillard1566d3a1999-07-15 14:24:29 +000099
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000100/*
101 * A conversion function is associated to a type and used to cast
102 * the new type to primitive values.
103 */
104typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
105
106/*
107 * Extra type: a name and a conversion function.
108 */
109
Daniel Veillard71b656e2000-01-05 14:46:17 +0000110typedef struct _xmlXPathType xmlXPathType;
111typedef xmlXPathType *xmlXPathTypePtr;
112struct _xmlXPathType {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000113 const xmlChar *name; /* the type name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000114 xmlXPathConvertFunc func; /* the conversion function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000115};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000116
117/*
118 * Extra variable: a name and a value.
119 */
120
Daniel Veillard71b656e2000-01-05 14:46:17 +0000121typedef struct _xmlXPathVariable xmlXPathVariable;
122typedef xmlXPathVariable *xmlXPathVariablePtr;
123struct _xmlXPathVariable {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000124 const xmlChar *name; /* the variable name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000125 xmlXPathObjectPtr value; /* the value */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000126};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000127
128/*
129 * an evaluation function, the parameters are on the context stack
130 */
131
132typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs);
133
134/*
135 * Extra function: a name and a evaluation function.
136 */
137
Daniel Veillard71b656e2000-01-05 14:46:17 +0000138typedef struct _xmlXPathFunct xmlXPathFunct;
139typedef xmlXPathFunct *xmlXPathFuncPtr;
140struct _xmlXPathFunct {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000141 const xmlChar *name; /* the function name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000142 xmlXPathEvalFunc func; /* the evaluation function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000143};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000144
145/*
146 * An axis traversal function. To traverse an axis, the engine calls
147 * the first time with cur == NULL and repeat until the function returns
148 * NULL indicating the end of the axis traversal.
149 */
150
151typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
152 xmlXPathObjectPtr cur);
153
154/*
155 * Extra axis: a name and an axis function.
156 */
157
Daniel Veillard71b656e2000-01-05 14:46:17 +0000158typedef struct _xmlXPathAxis xmlXPathAxis;
159typedef xmlXPathAxis *xmlXPathAxisPtr;
160struct _xmlXPathAxis {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000161 const xmlChar *name; /* the axis name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000162 xmlXPathAxisFunc func; /* the search function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000163};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000164
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000165/*
166 * Expression evaluation occurs with respect to a context.
167 * he context consists of:
168 * - a node (the context node)
169 * - a node list (the context node list)
170 * - a set of variable bindings
171 * - a function library
172 * - the set of namespace declarations in scope for the expression
173 */
174
Daniel Veillard71b656e2000-01-05 14:46:17 +0000175struct _xmlXPathContext {
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000176 xmlDocPtr doc; /* The current document */
177 xmlNodePtr node; /* The current node */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000178
179 int nb_variables; /* number of defined variables */
180 int max_variables; /* max number of variables */
181 xmlXPathVariablePtr *variables; /* Array of defined variables */
182
183 int nb_types; /* number of defined types */
184 int max_types; /* max number of types */
185 xmlXPathTypePtr *types; /* Array of defined types */
186
187 int nb_funcs; /* number of defined funcs */
188 int max_funcs; /* max number of funcs */
189 xmlXPathFuncPtr *funcs; /* Array of defined funcs */
190
191 int nb_axis; /* number of defined axis */
192 int max_axis; /* max number of axis */
193 xmlXPathAxisPtr *axis; /* Array of defined axis */
194
195 /* Namespace traversal should be implemented with user */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000196 xmlNsPtr *namespaces; /* The namespaces lookup */
197 int nsNr; /* the current Namespace index */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000198 void *user; /* user defined extra info */
Daniel Veillardf09e7e32000-10-01 15:53:30 +0000199
200 /* extra variables */
201 int contextSize; /* the context size */
202 int proximityPosition; /* the proximity position */
Daniel Veillardac260302000-10-04 13:33:43 +0000203
204 /* extra stuff for XPointer */
205 int xptr; /* it this an XPointer context */
206 xmlNodePtr here; /* for here() */
207 xmlNodePtr origin; /* for origin() */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000208};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000209
210/*
211 * An XPath parser context, it contains pure parsing informations,
212 * an xmlXPathContext, and the stack of objects.
213 */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000214struct _xmlXPathParserContext {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000215 const xmlChar *cur; /* the current char being parsed */
216 const xmlChar *base; /* the full expression */
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000217
218 int error; /* error code */
219
220 xmlXPathContextPtr context; /* the evaluation context */
221 xmlXPathObjectPtr value; /* the current value */
222 int valueNr; /* number of values stacked */
223 int valueMax; /* max number of values stacked */
224 xmlXPathObjectPtr *valueTab; /* stack of values */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000225};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000226
227/*
228 * An XPath function
229 * The arguments (if any) are popped out of the context stack
230 * and the result is pushed on the stack.
231 */
232
233typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
234
235/************************************************************************
236 * *
Daniel Veillard55b91f22000-10-05 16:30:11 +0000237 * Helpers *
238 * *
239 ************************************************************************/
240
241#define CHECK_ERROR \
242 if (ctxt->error != XPATH_EXPRESSION_OK) return
243
244#define CHECK_ERROR0 \
245 if (ctxt->error != XPATH_EXPRESSION_OK) return(0)
246
247#define XP_ERROR(X) \
248 { xmlXPatherror(ctxt, __FILE__, __LINE__, X); \
249 ctxt->error = (X); return; }
250
251#define XP_ERROR0(X) \
252 { xmlXPatherror(ctxt, __FILE__, __LINE__, X); \
253 ctxt->error = (X); return(0); }
254
255#define CHECK_TYPE(typeval) \
256 if ((ctxt->value == NULL) || (ctxt->value->type != typeval)) \
257 XP_ERROR(XPATH_INVALID_TYPE) \
258
259#define CHECK_ARITY(x) \
260 if (nargs != (x)) { \
261 XP_ERROR(XPATH_INVALID_ARITY); \
262 } \
263
264void xmlXPatherror (xmlXPathParserContextPtr ctxt,
265 const char *file,
266 int line,
267 int no);
268
269/**
Daniel Veillardb71379b2000-10-09 12:30:39 +0000270 * Utilities to extend XPath (XPointer)
Daniel Veillard55b91f22000-10-05 16:30:11 +0000271 */
Daniel Veillardb71379b2000-10-09 12:30:39 +0000272xmlXPathParserContextPtr
273 xmlXPathNewParserContext (const xmlChar *str,
274 xmlXPathContextPtr ctxt);
275void xmlXPathFreeParserContext (xmlXPathParserContextPtr ctxt);
276
Daniel Veillard55b91f22000-10-05 16:30:11 +0000277xmlXPathObjectPtr valuePop (xmlXPathParserContextPtr ctxt);
278int valuePush (xmlXPathParserContextPtr ctxt,
Daniel Veillard7e99c632000-10-06 12:59:53 +0000279 xmlXPathObjectPtr value);
280
281xmlXPathObjectPtr xmlXPathNewString (const xmlChar *val);
282xmlXPathObjectPtr xmlXPathNewNodeSet (xmlNodePtr val);
283void xmlXPathNodeSetAdd (xmlNodeSetPtr cur,
284 xmlNodePtr val);
285
286
287void xmlXPathIdFunction (xmlXPathParserContextPtr ctxt,
288 int nargs);
289void xmlXPathRoot (xmlXPathParserContextPtr ctxt);
290void xmlXPathEvalExpr (xmlXPathParserContextPtr ctxt);
Daniel Veillard2d38f042000-10-11 10:54:10 +0000291xmlChar * xmlXPathParseName (xmlXPathParserContextPtr ctxt);
Daniel Veillard55b91f22000-10-05 16:30:11 +0000292
293/************************************************************************
294 * *
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000295 * Public API *
296 * *
297 ************************************************************************/
298
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000299/**
Daniel Veillard2d38f042000-10-11 10:54:10 +0000300 * Extending a context
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000301 */
Daniel Veillard2d38f042000-10-11 10:54:10 +0000302int xmlXPathRegisterFunc (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000303 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000304 xmlXPathFunction f);
Daniel Veillard2d38f042000-10-11 10:54:10 +0000305int xmlXPathRegisterVariable (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000306 const xmlChar *name,
Daniel Veillard2d38f042000-10-11 10:54:10 +0000307 xmlXPathObjectPtr value);
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000308
309/**
310 * Evaluation functions.
311 */
Daniel Veillardac260302000-10-04 13:33:43 +0000312void xmlXPathInit (void);
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000313xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000314void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000315xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000316 xmlXPathContextPtr ctxt);
Daniel Veillardac260302000-10-04 13:33:43 +0000317xmlXPathObjectPtr xmlXPathEvalXPtrExpr (const xmlChar *str,
318 xmlXPathContextPtr ctxt);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000319void xmlXPathFreeObject (xmlXPathObjectPtr obj);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000320xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000321 xmlXPathContextPtr ctxt);
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000322xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val);
323void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
324void xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
Daniel Veillard2d38f042000-10-11 10:54:10 +0000325xmlXPathObjectPtr xmlXPathObjectCopy (xmlXPathObjectPtr val);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000326
Daniel Veillard55b91f22000-10-05 16:30:11 +0000327
Daniel Veillarde4e51311999-12-18 15:32:46 +0000328#ifdef __cplusplus
329}
330#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000331#endif /* ! __XML_XPATH_H__ */