blob: 0d2fa168e513fadefa62906c26a29fb6ac2e97c4 [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 Veillard52afe802000-10-22 16:56:02 +000016#include <libxml/hash.h>
Daniel Veillardb24054a1999-12-18 15:32:46 +000017
Daniel Veillarde4e51311999-12-18 15:32:46 +000018#ifdef __cplusplus
Daniel Veillard5cb5ab81999-12-21 15:35:29 +000019extern "C" {
Daniel Veillarde4e51311999-12-18 15:32:46 +000020#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000021
Daniel Veillard71b656e2000-01-05 14:46:17 +000022typedef struct _xmlXPathContext xmlXPathContext;
23typedef xmlXPathContext *xmlXPathContextPtr;
24typedef struct _xmlXPathParserContext xmlXPathParserContext;
25typedef xmlXPathParserContext *xmlXPathParserContextPtr;
Daniel Veillardc08a2c61999-09-08 21:35:25 +000026
Daniel Veillard55b91f22000-10-05 16:30:11 +000027/**
28 * The set of XPath error codes
29 */
30
31typedef 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 Veillardb71379b2000-10-09 12:30:39 +000046 XPATH_INVALID_CTXT_POSITION,
47 XPATH_MEMORY_ERROR,
48 XPTR_SYNTAX_ERROR,
49 XPTR_RESOURCE_ERROR,
50 XPTR_SUB_RESOURCE_ERROR
Daniel Veillard55b91f22000-10-05 16:30:11 +000051} xmlXPathError;
52
Daniel Veillard1566d3a1999-07-15 14:24:29 +000053/*
54 * A node-set (an unordered collection of nodes without duplicates)
55 */
Daniel Veillard71b656e2000-01-05 14:46:17 +000056typedef struct _xmlNodeSet xmlNodeSet;
57typedef xmlNodeSet *xmlNodeSetPtr;
58struct _xmlNodeSet {
Daniel Veillardbe803962000-06-28 23:40:59 +000059 int nodeNr; /* number of nodes in the set */
60 int nodeMax; /* size of the array as allocated */
Daniel Veillard1566d3a1999-07-15 14:24:29 +000061 xmlNodePtr *nodeTab; /* array of nodes in no particular order */
Daniel Veillard71b656e2000-01-05 14:46:17 +000062};
Daniel Veillard1566d3a1999-07-15 14:24:29 +000063
64/*
65 * An expression is evaluated to yield an object, which
66 * has one of the following four basic types:
67 * - node-set
68 * - boolean
69 * - number
70 * - string
Daniel Veillardbe803962000-06-28 23:40:59 +000071 *
72 * @@ XPointer will add more types !
Daniel Veillard1566d3a1999-07-15 14:24:29 +000073 */
74
Daniel Veillardf09e7e32000-10-01 15:53:30 +000075typedef enum {
76 XPATH_UNDEFINED = 0,
77 XPATH_NODESET = 1,
78 XPATH_BOOLEAN = 2,
79 XPATH_NUMBER = 3,
80 XPATH_STRING = 4,
Daniel Veillardac260302000-10-04 13:33:43 +000081 XPATH_POINT = 5,
82 XPATH_RANGE = 6,
83 XPATH_LOCATIONSET = 7,
84 XPATH_USERS = 8
Daniel Veillardf09e7e32000-10-01 15:53:30 +000085} xmlXPathObjectType;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000086
Daniel Veillard71b656e2000-01-05 14:46:17 +000087typedef struct _xmlXPathObject xmlXPathObject;
88typedef xmlXPathObject *xmlXPathObjectPtr;
89struct _xmlXPathObject {
Daniel Veillardf09e7e32000-10-01 15:53:30 +000090 xmlXPathObjectType type;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000091 xmlNodeSetPtr nodesetval;
92 int boolval;
Daniel Veillarde2d034d1999-07-27 19:52:06 +000093 double floatval;
Daniel Veillarddd6b3671999-09-23 22:19:22 +000094 xmlChar *stringval;
Daniel Veillardc08a2c61999-09-08 21:35:25 +000095 void *user;
Daniel Veillardac260302000-10-04 13:33:43 +000096 int index;
97 void *user2;
98 int index2;
Daniel Veillard71b656e2000-01-05 14:46:17 +000099};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000100
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000101/*
102 * A conversion function is associated to a type and used to cast
103 * the new type to primitive values.
104 */
105typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
106
107/*
108 * Extra type: a name and a conversion function.
109 */
110
Daniel Veillard71b656e2000-01-05 14:46:17 +0000111typedef struct _xmlXPathType xmlXPathType;
112typedef xmlXPathType *xmlXPathTypePtr;
113struct _xmlXPathType {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000114 const xmlChar *name; /* the type name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000115 xmlXPathConvertFunc func; /* the conversion function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000116};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000117
118/*
119 * Extra variable: a name and a value.
120 */
121
Daniel Veillard71b656e2000-01-05 14:46:17 +0000122typedef struct _xmlXPathVariable xmlXPathVariable;
123typedef xmlXPathVariable *xmlXPathVariablePtr;
124struct _xmlXPathVariable {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000125 const xmlChar *name; /* the variable name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000126 xmlXPathObjectPtr value; /* the value */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000127};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000128
129/*
130 * an evaluation function, the parameters are on the context stack
131 */
132
133typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs);
134
135/*
136 * Extra function: a name and a evaluation function.
137 */
138
Daniel Veillard71b656e2000-01-05 14:46:17 +0000139typedef struct _xmlXPathFunct xmlXPathFunct;
140typedef xmlXPathFunct *xmlXPathFuncPtr;
141struct _xmlXPathFunct {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000142 const xmlChar *name; /* the function name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000143 xmlXPathEvalFunc func; /* the evaluation function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000144};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000145
146/*
147 * An axis traversal function. To traverse an axis, the engine calls
148 * the first time with cur == NULL and repeat until the function returns
149 * NULL indicating the end of the axis traversal.
150 */
151
152typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
153 xmlXPathObjectPtr cur);
154
155/*
156 * Extra axis: a name and an axis function.
157 */
158
Daniel Veillard71b656e2000-01-05 14:46:17 +0000159typedef struct _xmlXPathAxis xmlXPathAxis;
160typedef xmlXPathAxis *xmlXPathAxisPtr;
161struct _xmlXPathAxis {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000162 const xmlChar *name; /* the axis name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000163 xmlXPathAxisFunc func; /* the search function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000164};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000165
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000166/*
167 * Expression evaluation occurs with respect to a context.
168 * he context consists of:
169 * - a node (the context node)
170 * - a node list (the context node list)
171 * - a set of variable bindings
172 * - a function library
173 * - the set of namespace declarations in scope for the expression
174 */
175
Daniel Veillard71b656e2000-01-05 14:46:17 +0000176struct _xmlXPathContext {
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000177 xmlDocPtr doc; /* The current document */
178 xmlNodePtr node; /* The current node */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000179
Daniel Veillard126f2792000-10-24 17:10:12 +0000180 int nb_variables_unused; /* unused (hash table) */
181 int max_variables_unused; /* unused (hash table) */
182 xmlHashTablePtr varHash; /* Hash table of defined variables */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000183
184 int nb_types; /* number of defined types */
185 int max_types; /* max number of types */
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000186 xmlXPathTypePtr types; /* Array of defined types */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000187
Daniel Veillard52afe802000-10-22 16:56:02 +0000188 int nb_funcs_unused; /* unused (hash table) */
189 int max_funcs_unused; /* unused (hash table) */
190 xmlHashTablePtr funcHash; /* Hash table of defined funcs */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000191
192 int nb_axis; /* number of defined axis */
193 int max_axis; /* max number of axis */
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000194 xmlXPathAxisPtr axis; /* Array of defined axis */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000195
196 /* Namespace traversal should be implemented with user */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000197 xmlNsPtr *namespaces; /* The namespaces lookup */
198 int nsNr; /* the current Namespace index */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000199 void *user; /* user defined extra info */
Daniel Veillardf09e7e32000-10-01 15:53:30 +0000200
201 /* extra variables */
202 int contextSize; /* the context size */
203 int proximityPosition; /* the proximity position */
Daniel Veillardac260302000-10-04 13:33:43 +0000204
205 /* extra stuff for XPointer */
206 int xptr; /* it this an XPointer context */
207 xmlNodePtr here; /* for here() */
208 xmlNodePtr origin; /* for origin() */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000209};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000210
211/*
212 * An XPath parser context, it contains pure parsing informations,
213 * an xmlXPathContext, and the stack of objects.
214 */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000215struct _xmlXPathParserContext {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000216 const xmlChar *cur; /* the current char being parsed */
217 const xmlChar *base; /* the full expression */
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000218
219 int error; /* error code */
220
221 xmlXPathContextPtr context; /* the evaluation context */
222 xmlXPathObjectPtr value; /* the current value */
223 int valueNr; /* number of values stacked */
224 int valueMax; /* max number of values stacked */
225 xmlXPathObjectPtr *valueTab; /* stack of values */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000226};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000227
228/*
229 * An XPath function
230 * The arguments (if any) are popped out of the context stack
231 * and the result is pushed on the stack.
232 */
233
234typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
235
236/************************************************************************
237 * *
Daniel Veillard55b91f22000-10-05 16:30:11 +0000238 * Helpers *
239 * *
240 ************************************************************************/
241
242#define CHECK_ERROR \
243 if (ctxt->error != XPATH_EXPRESSION_OK) return
244
245#define CHECK_ERROR0 \
246 if (ctxt->error != XPATH_EXPRESSION_OK) return(0)
247
248#define XP_ERROR(X) \
249 { xmlXPatherror(ctxt, __FILE__, __LINE__, X); \
250 ctxt->error = (X); return; }
251
252#define XP_ERROR0(X) \
253 { xmlXPatherror(ctxt, __FILE__, __LINE__, X); \
254 ctxt->error = (X); return(0); }
255
256#define CHECK_TYPE(typeval) \
257 if ((ctxt->value == NULL) || (ctxt->value->type != typeval)) \
258 XP_ERROR(XPATH_INVALID_TYPE) \
259
260#define CHECK_ARITY(x) \
261 if (nargs != (x)) { \
262 XP_ERROR(XPATH_INVALID_ARITY); \
263 } \
264
265void xmlXPatherror (xmlXPathParserContextPtr ctxt,
266 const char *file,
267 int line,
268 int no);
269
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000270void xmlXPathDebugDumpObject (FILE *output,
271 xmlXPathObjectPtr cur,
272 int depth);
273
Daniel Veillard55b91f22000-10-05 16:30:11 +0000274/**
Daniel Veillardb71379b2000-10-09 12:30:39 +0000275 * Utilities to extend XPath (XPointer)
Daniel Veillard55b91f22000-10-05 16:30:11 +0000276 */
Daniel Veillardb71379b2000-10-09 12:30:39 +0000277xmlXPathParserContextPtr
278 xmlXPathNewParserContext (const xmlChar *str,
279 xmlXPathContextPtr ctxt);
280void xmlXPathFreeParserContext (xmlXPathParserContextPtr ctxt);
281
Daniel Veillard55b91f22000-10-05 16:30:11 +0000282xmlXPathObjectPtr valuePop (xmlXPathParserContextPtr ctxt);
283int valuePush (xmlXPathParserContextPtr ctxt,
Daniel Veillard7e99c632000-10-06 12:59:53 +0000284 xmlXPathObjectPtr value);
285
286xmlXPathObjectPtr xmlXPathNewString (const xmlChar *val);
Daniel Veillard683cb022000-10-22 12:04:13 +0000287xmlXPathObjectPtr xmlXPathNewCString (const char *val);
288xmlXPathObjectPtr xmlXPathNewFloat (double val);
289xmlXPathObjectPtr xmlXPathNewBoolean (int val);
Daniel Veillard7e99c632000-10-06 12:59:53 +0000290xmlXPathObjectPtr xmlXPathNewNodeSet (xmlNodePtr val);
291void xmlXPathNodeSetAdd (xmlNodeSetPtr cur,
292 xmlNodePtr val);
293
294
295void xmlXPathIdFunction (xmlXPathParserContextPtr ctxt,
296 int nargs);
297void xmlXPathRoot (xmlXPathParserContextPtr ctxt);
298void xmlXPathEvalExpr (xmlXPathParserContextPtr ctxt);
Daniel Veillard2d38f042000-10-11 10:54:10 +0000299xmlChar * xmlXPathParseName (xmlXPathParserContextPtr ctxt);
Daniel Veillard55b91f22000-10-05 16:30:11 +0000300
301/************************************************************************
302 * *
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000303 * Public API *
304 * *
305 ************************************************************************/
306
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000307/**
Daniel Veillard2d38f042000-10-11 10:54:10 +0000308 * Extending a context
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000309 */
Daniel Veillard2d38f042000-10-11 10:54:10 +0000310int xmlXPathRegisterFunc (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000311 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000312 xmlXPathFunction f);
Daniel Veillard2d38f042000-10-11 10:54:10 +0000313int xmlXPathRegisterVariable (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000314 const xmlChar *name,
Daniel Veillard2d38f042000-10-11 10:54:10 +0000315 xmlXPathObjectPtr value);
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000316
317/**
318 * Evaluation functions.
319 */
Daniel Veillardac260302000-10-04 13:33:43 +0000320void xmlXPathInit (void);
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000321xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000322void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000323xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000324 xmlXPathContextPtr ctxt);
Daniel Veillardac260302000-10-04 13:33:43 +0000325xmlXPathObjectPtr xmlXPathEvalXPtrExpr (const xmlChar *str,
326 xmlXPathContextPtr ctxt);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000327void xmlXPathFreeObject (xmlXPathObjectPtr obj);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000328xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000329 xmlXPathContextPtr ctxt);
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000330xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val);
331void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
332void xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
Daniel Veillard2d38f042000-10-11 10:54:10 +0000333xmlXPathObjectPtr xmlXPathObjectCopy (xmlXPathObjectPtr val);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000334
Daniel Veillard55b91f22000-10-05 16:30:11 +0000335
Daniel Veillarde4e51311999-12-18 15:32:46 +0000336#ifdef __cplusplus
337}
338#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000339#endif /* ! __XML_XPATH_H__ */