blob: 6f820d4a74c5c23f0e0b1f4866ef46e84809260a [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,
Daniel Veillarda6d8eb62000-12-27 10:46:47 +000050 XPTR_SUB_RESOURCE_ERROR,
51 XPATH_UNDEF_PREFIX_ERROR
Daniel Veillard55b91f22000-10-05 16:30:11 +000052} xmlXPathError;
53
Daniel Veillard1566d3a1999-07-15 14:24:29 +000054/*
55 * A node-set (an unordered collection of nodes without duplicates)
56 */
Daniel Veillard71b656e2000-01-05 14:46:17 +000057typedef struct _xmlNodeSet xmlNodeSet;
58typedef xmlNodeSet *xmlNodeSetPtr;
59struct _xmlNodeSet {
Daniel Veillardbe803962000-06-28 23:40:59 +000060 int nodeNr; /* number of nodes in the set */
61 int nodeMax; /* size of the array as allocated */
Daniel Veillard1566d3a1999-07-15 14:24:29 +000062 xmlNodePtr *nodeTab; /* array of nodes in no particular order */
Daniel Veillard71b656e2000-01-05 14:46:17 +000063};
Daniel Veillard1566d3a1999-07-15 14:24:29 +000064
65/*
66 * An expression is evaluated to yield an object, which
67 * has one of the following four basic types:
68 * - node-set
69 * - boolean
70 * - number
71 * - string
Daniel Veillardbe803962000-06-28 23:40:59 +000072 *
73 * @@ XPointer will add more types !
Daniel Veillard1566d3a1999-07-15 14:24:29 +000074 */
75
Daniel Veillardf09e7e32000-10-01 15:53:30 +000076typedef enum {
77 XPATH_UNDEFINED = 0,
78 XPATH_NODESET = 1,
79 XPATH_BOOLEAN = 2,
80 XPATH_NUMBER = 3,
81 XPATH_STRING = 4,
Daniel Veillardac260302000-10-04 13:33:43 +000082 XPATH_POINT = 5,
83 XPATH_RANGE = 6,
84 XPATH_LOCATIONSET = 7,
85 XPATH_USERS = 8
Daniel Veillardf09e7e32000-10-01 15:53:30 +000086} xmlXPathObjectType;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000087
Daniel Veillard71b656e2000-01-05 14:46:17 +000088typedef struct _xmlXPathObject xmlXPathObject;
89typedef xmlXPathObject *xmlXPathObjectPtr;
90struct _xmlXPathObject {
Daniel Veillardf09e7e32000-10-01 15:53:30 +000091 xmlXPathObjectType type;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000092 xmlNodeSetPtr nodesetval;
93 int boolval;
Daniel Veillarde2d034d1999-07-27 19:52:06 +000094 double floatval;
Daniel Veillarddd6b3671999-09-23 22:19:22 +000095 xmlChar *stringval;
Daniel Veillardc08a2c61999-09-08 21:35:25 +000096 void *user;
Daniel Veillardac260302000-10-04 13:33:43 +000097 int index;
98 void *user2;
99 int index2;
Daniel Veillard71b656e2000-01-05 14:46:17 +0000100};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000101
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000102/*
103 * A conversion function is associated to a type and used to cast
104 * the new type to primitive values.
105 */
106typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
107
108/*
109 * Extra type: a name and a conversion function.
110 */
111
Daniel Veillard71b656e2000-01-05 14:46:17 +0000112typedef struct _xmlXPathType xmlXPathType;
113typedef xmlXPathType *xmlXPathTypePtr;
114struct _xmlXPathType {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000115 const xmlChar *name; /* the type name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000116 xmlXPathConvertFunc func; /* the conversion function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000117};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000118
119/*
120 * Extra variable: a name and a value.
121 */
122
Daniel Veillard71b656e2000-01-05 14:46:17 +0000123typedef struct _xmlXPathVariable xmlXPathVariable;
124typedef xmlXPathVariable *xmlXPathVariablePtr;
125struct _xmlXPathVariable {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000126 const xmlChar *name; /* the variable name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000127 xmlXPathObjectPtr value; /* the value */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000128};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000129
130/*
131 * an evaluation function, the parameters are on the context stack
132 */
133
134typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs);
135
136/*
137 * Extra function: a name and a evaluation function.
138 */
139
Daniel Veillard71b656e2000-01-05 14:46:17 +0000140typedef struct _xmlXPathFunct xmlXPathFunct;
141typedef xmlXPathFunct *xmlXPathFuncPtr;
142struct _xmlXPathFunct {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000143 const xmlChar *name; /* the function name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000144 xmlXPathEvalFunc func; /* the evaluation function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000145};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000146
147/*
148 * An axis traversal function. To traverse an axis, the engine calls
149 * the first time with cur == NULL and repeat until the function returns
150 * NULL indicating the end of the axis traversal.
151 */
152
153typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
154 xmlXPathObjectPtr cur);
155
156/*
157 * Extra axis: a name and an axis function.
158 */
159
Daniel Veillard71b656e2000-01-05 14:46:17 +0000160typedef struct _xmlXPathAxis xmlXPathAxis;
161typedef xmlXPathAxis *xmlXPathAxisPtr;
162struct _xmlXPathAxis {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000163 const xmlChar *name; /* the axis name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000164 xmlXPathAxisFunc func; /* the search function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000165};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000166
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000167/*
168 * Expression evaluation occurs with respect to a context.
169 * he context consists of:
170 * - a node (the context node)
171 * - a node list (the context node list)
172 * - a set of variable bindings
173 * - a function library
174 * - the set of namespace declarations in scope for the expression
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000175 * Following the switch to hash tables, this need to be trimmed up at
176 * the next binary incompatible release.
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000177 */
178
Daniel Veillard71b656e2000-01-05 14:46:17 +0000179struct _xmlXPathContext {
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000180 xmlDocPtr doc; /* The current document */
181 xmlNodePtr node; /* The current node */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000182
Daniel Veillard126f2792000-10-24 17:10:12 +0000183 int nb_variables_unused; /* unused (hash table) */
184 int max_variables_unused; /* unused (hash table) */
185 xmlHashTablePtr varHash; /* Hash table of defined variables */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000186
187 int nb_types; /* number of defined types */
188 int max_types; /* max number of types */
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000189 xmlXPathTypePtr types; /* Array of defined types */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000190
Daniel Veillard52afe802000-10-22 16:56:02 +0000191 int nb_funcs_unused; /* unused (hash table) */
192 int max_funcs_unused; /* unused (hash table) */
193 xmlHashTablePtr funcHash; /* Hash table of defined funcs */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000194
195 int nb_axis; /* number of defined axis */
196 int max_axis; /* max number of axis */
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000197 xmlXPathAxisPtr axis; /* Array of defined axis */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000198
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000199 /* the namespace nodes of the context node */
200 xmlNsPtr *namespaces; /* Array of namespaces */
201 int nsNr; /* number of namespace in scope */
202 void *user; /* function to free */
Daniel Veillardf09e7e32000-10-01 15:53:30 +0000203
204 /* extra variables */
205 int contextSize; /* the context size */
206 int proximityPosition; /* the proximity position */
Daniel Veillardac260302000-10-04 13:33:43 +0000207
208 /* extra stuff for XPointer */
209 int xptr; /* it this an XPointer context */
210 xmlNodePtr here; /* for here() */
211 xmlNodePtr origin; /* for origin() */
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000212
213 /* the set of namespace declarations in scope for the expression */
214 xmlHashTablePtr nsHash; /* The namespaces hash table */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000215};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000216
217/*
218 * An XPath parser context, it contains pure parsing informations,
219 * an xmlXPathContext, and the stack of objects.
220 */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000221struct _xmlXPathParserContext {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000222 const xmlChar *cur; /* the current char being parsed */
223 const xmlChar *base; /* the full expression */
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000224
225 int error; /* error code */
226
227 xmlXPathContextPtr context; /* the evaluation context */
228 xmlXPathObjectPtr value; /* the current value */
229 int valueNr; /* number of values stacked */
230 int valueMax; /* max number of values stacked */
231 xmlXPathObjectPtr *valueTab; /* stack of values */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000232};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000233
234/*
235 * An XPath function
236 * The arguments (if any) are popped out of the context stack
237 * and the result is pushed on the stack.
238 */
239
240typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
241
242/************************************************************************
243 * *
244 * Public API *
245 * *
246 ************************************************************************/
247
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000248/**
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000249 * Evaluation functions.
250 */
Daniel Veillardac260302000-10-04 13:33:43 +0000251void xmlXPathInit (void);
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000252xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000253void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000254xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000255 xmlXPathContextPtr ctxt);
Daniel Veillardac260302000-10-04 13:33:43 +0000256xmlXPathObjectPtr xmlXPathEvalXPtrExpr (const xmlChar *str,
257 xmlXPathContextPtr ctxt);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000258void xmlXPathFreeObject (xmlXPathObjectPtr obj);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000259xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000260 xmlXPathContextPtr ctxt);
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000261xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val);
262void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
263void xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
Daniel Veillard2d38f042000-10-11 10:54:10 +0000264xmlXPathObjectPtr xmlXPathObjectCopy (xmlXPathObjectPtr val);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000265
Daniel Veillard55b91f22000-10-05 16:30:11 +0000266
Daniel Veillarde4e51311999-12-18 15:32:46 +0000267#ifdef __cplusplus
268}
269#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000270#endif /* ! __XML_XPATH_H__ */