blob: f8fd861eee42d19142d6af320afa8174440c75e6 [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,
Daniel Veillarde4566462001-01-22 09:58:39 +000085 XPATH_USERS = 8,
86 XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */
Daniel Veillardf09e7e32000-10-01 15:53:30 +000087} xmlXPathObjectType;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000088
Daniel Veillard71b656e2000-01-05 14:46:17 +000089typedef struct _xmlXPathObject xmlXPathObject;
90typedef xmlXPathObject *xmlXPathObjectPtr;
91struct _xmlXPathObject {
Daniel Veillardf09e7e32000-10-01 15:53:30 +000092 xmlXPathObjectType type;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000093 xmlNodeSetPtr nodesetval;
94 int boolval;
Daniel Veillarde2d034d1999-07-27 19:52:06 +000095 double floatval;
Daniel Veillarddd6b3671999-09-23 22:19:22 +000096 xmlChar *stringval;
Daniel Veillardc08a2c61999-09-08 21:35:25 +000097 void *user;
Daniel Veillardac260302000-10-04 13:33:43 +000098 int index;
99 void *user2;
100 int index2;
Daniel Veillard71b656e2000-01-05 14:46:17 +0000101};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000102
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000103/*
104 * A conversion function is associated to a type and used to cast
105 * the new type to primitive values.
106 */
107typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
108
109/*
110 * Extra type: a name and a conversion function.
111 */
112
Daniel Veillard71b656e2000-01-05 14:46:17 +0000113typedef struct _xmlXPathType xmlXPathType;
114typedef xmlXPathType *xmlXPathTypePtr;
115struct _xmlXPathType {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000116 const xmlChar *name; /* the type name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000117 xmlXPathConvertFunc func; /* the conversion function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000118};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000119
120/*
121 * Extra variable: a name and a value.
122 */
123
Daniel Veillard71b656e2000-01-05 14:46:17 +0000124typedef struct _xmlXPathVariable xmlXPathVariable;
125typedef xmlXPathVariable *xmlXPathVariablePtr;
126struct _xmlXPathVariable {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000127 const xmlChar *name; /* the variable name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000128 xmlXPathObjectPtr value; /* the value */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000129};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000130
131/*
132 * an evaluation function, the parameters are on the context stack
133 */
134
135typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs);
136
137/*
138 * Extra function: a name and a evaluation function.
139 */
140
Daniel Veillard71b656e2000-01-05 14:46:17 +0000141typedef struct _xmlXPathFunct xmlXPathFunct;
142typedef xmlXPathFunct *xmlXPathFuncPtr;
143struct _xmlXPathFunct {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000144 const xmlChar *name; /* the function name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000145 xmlXPathEvalFunc func; /* the evaluation function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000146};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000147
148/*
149 * An axis traversal function. To traverse an axis, the engine calls
150 * the first time with cur == NULL and repeat until the function returns
151 * NULL indicating the end of the axis traversal.
152 */
153
154typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
155 xmlXPathObjectPtr cur);
156
157/*
158 * Extra axis: a name and an axis function.
159 */
160
Daniel Veillard71b656e2000-01-05 14:46:17 +0000161typedef struct _xmlXPathAxis xmlXPathAxis;
162typedef xmlXPathAxis *xmlXPathAxisPtr;
163struct _xmlXPathAxis {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000164 const xmlChar *name; /* the axis name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000165 xmlXPathAxisFunc func; /* the search function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000166};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000167
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000168/*
169 * Expression evaluation occurs with respect to a context.
170 * he context consists of:
171 * - a node (the context node)
172 * - a node list (the context node list)
173 * - a set of variable bindings
174 * - a function library
175 * - the set of namespace declarations in scope for the expression
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000176 * Following the switch to hash tables, this need to be trimmed up at
177 * the next binary incompatible release.
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000178 */
179
Daniel Veillard71b656e2000-01-05 14:46:17 +0000180struct _xmlXPathContext {
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000181 xmlDocPtr doc; /* The current document */
182 xmlNodePtr node; /* The current node */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000183
Daniel Veillard126f2792000-10-24 17:10:12 +0000184 int nb_variables_unused; /* unused (hash table) */
185 int max_variables_unused; /* unused (hash table) */
186 xmlHashTablePtr varHash; /* Hash table of defined variables */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000187
188 int nb_types; /* number of defined types */
189 int max_types; /* max number of types */
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000190 xmlXPathTypePtr types; /* Array of defined types */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000191
Daniel Veillard52afe802000-10-22 16:56:02 +0000192 int nb_funcs_unused; /* unused (hash table) */
193 int max_funcs_unused; /* unused (hash table) */
194 xmlHashTablePtr funcHash; /* Hash table of defined funcs */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000195
196 int nb_axis; /* number of defined axis */
197 int max_axis; /* max number of axis */
Daniel Veillardc2df4cd2000-10-12 23:15:24 +0000198 xmlXPathAxisPtr axis; /* Array of defined axis */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000199
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000200 /* the namespace nodes of the context node */
201 xmlNsPtr *namespaces; /* Array of namespaces */
202 int nsNr; /* number of namespace in scope */
203 void *user; /* function to free */
Daniel Veillardf09e7e32000-10-01 15:53:30 +0000204
205 /* extra variables */
206 int contextSize; /* the context size */
207 int proximityPosition; /* the proximity position */
Daniel Veillardac260302000-10-04 13:33:43 +0000208
209 /* extra stuff for XPointer */
210 int xptr; /* it this an XPointer context */
211 xmlNodePtr here; /* for here() */
212 xmlNodePtr origin; /* for origin() */
Daniel Veillarda6d8eb62000-12-27 10:46:47 +0000213
214 /* the set of namespace declarations in scope for the expression */
215 xmlHashTablePtr nsHash; /* The namespaces hash table */
Daniel Veillard8f4d9752001-01-19 05:32:34 +0000216 void *varLookupFunc; /* variable lookup func */
217 void *varLookupData; /* variable lookup data */
Daniel Veillard93086662001-01-25 18:13:04 +0000218
219 /* Possibility to link in an extra item */
220 void *extra; /* needed for XSLT */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000221};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000222
223/*
224 * An XPath parser context, it contains pure parsing informations,
225 * an xmlXPathContext, and the stack of objects.
226 */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000227struct _xmlXPathParserContext {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000228 const xmlChar *cur; /* the current char being parsed */
229 const xmlChar *base; /* the full expression */
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000230
231 int error; /* error code */
232
233 xmlXPathContextPtr context; /* the evaluation context */
234 xmlXPathObjectPtr value; /* the current value */
235 int valueNr; /* number of values stacked */
236 int valueMax; /* max number of values stacked */
237 xmlXPathObjectPtr *valueTab; /* stack of values */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000238};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000239
240/*
241 * An XPath function
242 * The arguments (if any) are popped out of the context stack
243 * and the result is pushed on the stack.
244 */
245
246typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
247
248/************************************************************************
249 * *
250 * Public API *
251 * *
252 ************************************************************************/
253
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000254/**
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000255 * Evaluation functions.
256 */
Daniel Veillardac260302000-10-04 13:33:43 +0000257void xmlXPathInit (void);
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000258xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000259void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000260xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000261 xmlXPathContextPtr ctxt);
Daniel Veillardac260302000-10-04 13:33:43 +0000262xmlXPathObjectPtr xmlXPathEvalXPtrExpr (const xmlChar *str,
263 xmlXPathContextPtr ctxt);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000264void xmlXPathFreeObject (xmlXPathObjectPtr obj);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000265xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000266 xmlXPathContextPtr ctxt);
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000267xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val);
268void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
269void xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
Daniel Veillard2d38f042000-10-11 10:54:10 +0000270xmlXPathObjectPtr xmlXPathObjectCopy (xmlXPathObjectPtr val);
Daniel Veillardf17e09b2001-01-25 13:55:35 +0000271int xmlXPathCmpNodes (xmlNodePtr node1,
272 xmlNodePtr node2);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000273
Daniel Veillard55b91f22000-10-05 16:30:11 +0000274
Daniel Veillarde4e51311999-12-18 15:32:46 +0000275#ifdef __cplusplus
276}
277#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000278#endif /* ! __XML_XPATH_H__ */