blob: d7e23d16d71cdce966f281832776d49580a2fb90 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +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
15#include <libxml/tree.h>
16#include <libxml/hash.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22typedef struct _xmlXPathContext xmlXPathContext;
23typedef xmlXPathContext *xmlXPathContextPtr;
24typedef struct _xmlXPathParserContext xmlXPathParserContext;
25typedef xmlXPathParserContext *xmlXPathParserContextPtr;
26
27/**
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,
46 XPATH_INVALID_CTXT_POSITION,
47 XPATH_MEMORY_ERROR,
48 XPTR_SYNTAX_ERROR,
49 XPTR_RESOURCE_ERROR,
50 XPTR_SUB_RESOURCE_ERROR,
51 XPATH_UNDEF_PREFIX_ERROR
52} xmlXPathError;
53
54/*
55 * A node-set (an unordered collection of nodes without duplicates)
56 */
57typedef struct _xmlNodeSet xmlNodeSet;
58typedef xmlNodeSet *xmlNodeSetPtr;
59struct _xmlNodeSet {
60 int nodeNr; /* number of nodes in the set */
61 int nodeMax; /* size of the array as allocated */
62 xmlNodePtr *nodeTab; /* array of nodes in no particular order */
63};
64
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
72 *
73 * @@ XPointer will add more types !
74 */
75
76typedef enum {
77 XPATH_UNDEFINED = 0,
78 XPATH_NODESET = 1,
79 XPATH_BOOLEAN = 2,
80 XPATH_NUMBER = 3,
81 XPATH_STRING = 4,
82 XPATH_POINT = 5,
83 XPATH_RANGE = 6,
84 XPATH_LOCATIONSET = 7,
85 XPATH_USERS = 8,
86 XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */
87} xmlXPathObjectType;
88
89typedef struct _xmlXPathObject xmlXPathObject;
90typedef xmlXPathObject *xmlXPathObjectPtr;
91struct _xmlXPathObject {
92 xmlXPathObjectType type;
93 xmlNodeSetPtr nodesetval;
94 int boolval;
95 double floatval;
96 xmlChar *stringval;
97 void *user;
98 int index;
99 void *user2;
100 int index2;
101};
102
103/*
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
113typedef struct _xmlXPathType xmlXPathType;
114typedef xmlXPathType *xmlXPathTypePtr;
115struct _xmlXPathType {
116 const xmlChar *name; /* the type name */
117 xmlXPathConvertFunc func; /* the conversion function */
118};
119
120/*
121 * Extra variable: a name and a value.
122 */
123
124typedef struct _xmlXPathVariable xmlXPathVariable;
125typedef xmlXPathVariable *xmlXPathVariablePtr;
126struct _xmlXPathVariable {
127 const xmlChar *name; /* the variable name */
128 xmlXPathObjectPtr value; /* the value */
129};
130
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
141typedef struct _xmlXPathFunct xmlXPathFunct;
142typedef xmlXPathFunct *xmlXPathFuncPtr;
143struct _xmlXPathFunct {
144 const xmlChar *name; /* the function name */
145 xmlXPathEvalFunc func; /* the evaluation function */
146};
147
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
161typedef struct _xmlXPathAxis xmlXPathAxis;
162typedef xmlXPathAxis *xmlXPathAxisPtr;
163struct _xmlXPathAxis {
164 const xmlChar *name; /* the axis name */
165 xmlXPathAxisFunc func; /* the search function */
166};
167
168/*
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
176 * Following the switch to hash tables, this need to be trimmed up at
177 * the next binary incompatible release.
178 */
179
180struct _xmlXPathContext {
181 xmlDocPtr doc; /* The current document */
182 xmlNodePtr node; /* The current node */
183
184 int nb_variables_unused; /* unused (hash table) */
185 int max_variables_unused; /* unused (hash table) */
186 xmlHashTablePtr varHash; /* Hash table of defined variables */
187
188 int nb_types; /* number of defined types */
189 int max_types; /* max number of types */
190 xmlXPathTypePtr types; /* Array of defined types */
191
192 int nb_funcs_unused; /* unused (hash table) */
193 int max_funcs_unused; /* unused (hash table) */
194 xmlHashTablePtr funcHash; /* Hash table of defined funcs */
195
196 int nb_axis; /* number of defined axis */
197 int max_axis; /* max number of axis */
198 xmlXPathAxisPtr axis; /* Array of defined axis */
199
200 /* 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 */
204
205 /* extra variables */
206 int contextSize; /* the context size */
207 int proximityPosition; /* the proximity position */
208
209 /* extra stuff for XPointer */
210 int xptr; /* it this an XPointer context */
211 xmlNodePtr here; /* for here() */
212 xmlNodePtr origin; /* for origin() */
213
214 /* the set of namespace declarations in scope for the expression */
215 xmlHashTablePtr nsHash; /* The namespaces hash table */
216 void *varLookupFunc; /* variable lookup func */
217 void *varLookupData; /* variable lookup data */
218
219 /* Possibility to link in an extra item */
220 void *extra; /* needed for XSLT */
221};
222
223/*
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000224 * The structure of a compiled expression form is not public
225 */
226
227typedef struct _xmlXPathCompExpr xmlXPathCompExpr;
228typedef xmlXPathCompExpr *xmlXPathCompExprPtr;
229
230/*
Owen Taylor3473f882001-02-23 17:55:21 +0000231 * An XPath parser context, it contains pure parsing informations,
232 * an xmlXPathContext, and the stack of objects.
233 */
234struct _xmlXPathParserContext {
235 const xmlChar *cur; /* the current char being parsed */
236 const xmlChar *base; /* the full expression */
237
238 int error; /* error code */
239
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000240 xmlXPathCompExprPtr comp; /* the precompiled expression */
241
Owen Taylor3473f882001-02-23 17:55:21 +0000242 xmlXPathContextPtr context; /* the evaluation context */
243 xmlXPathObjectPtr value; /* the current value */
244 int valueNr; /* number of values stacked */
245 int valueMax; /* max number of values stacked */
246 xmlXPathObjectPtr *valueTab; /* stack of values */
247};
248
249/*
250 * An XPath function
251 * The arguments (if any) are popped out of the context stack
252 * and the result is pushed on the stack.
253 */
254
255typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
256
257/************************************************************************
258 * *
259 * Public API *
260 * *
261 ************************************************************************/
262
263/**
264 * Evaluation functions.
265 */
266void xmlXPathInit (void);
267xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
268void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
269xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
270 xmlXPathContextPtr ctxt);
271xmlXPathObjectPtr xmlXPathEvalXPtrExpr (const xmlChar *str,
272 xmlXPathContextPtr ctxt);
273void xmlXPathFreeObject (xmlXPathObjectPtr obj);
274xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
275 xmlXPathContextPtr ctxt);
276xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val);
277void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
278void xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
279xmlXPathObjectPtr xmlXPathObjectCopy (xmlXPathObjectPtr val);
280int xmlXPathCmpNodes (xmlNodePtr node1,
281 xmlNodePtr node2);
282
283
284#ifdef __cplusplus
285}
286#endif
287#endif /* ! __XML_XPATH_H__ */