blob: 3755678046969ba3f8ba65c82c9e6cfc818b70e6 [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 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00009 * Author: daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000010 */
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,
Daniel Veillard61d80a22001-04-27 17:13:01 +000051 XPATH_UNDEF_PREFIX_ERROR,
52 XPATH_ENCODING_ERROR,
53 XPATH_INVALID_CHAR_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +000054} xmlXPathError;
55
56/*
57 * A node-set (an unordered collection of nodes without duplicates)
58 */
59typedef struct _xmlNodeSet xmlNodeSet;
60typedef xmlNodeSet *xmlNodeSetPtr;
61struct _xmlNodeSet {
62 int nodeNr; /* number of nodes in the set */
63 int nodeMax; /* size of the array as allocated */
64 xmlNodePtr *nodeTab; /* array of nodes in no particular order */
65};
66
67/*
68 * An expression is evaluated to yield an object, which
69 * has one of the following four basic types:
70 * - node-set
71 * - boolean
72 * - number
73 * - string
74 *
75 * @@ XPointer will add more types !
76 */
77
78typedef enum {
79 XPATH_UNDEFINED = 0,
80 XPATH_NODESET = 1,
81 XPATH_BOOLEAN = 2,
82 XPATH_NUMBER = 3,
83 XPATH_STRING = 4,
84 XPATH_POINT = 5,
85 XPATH_RANGE = 6,
86 XPATH_LOCATIONSET = 7,
87 XPATH_USERS = 8,
88 XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */
89} xmlXPathObjectType;
90
91typedef struct _xmlXPathObject xmlXPathObject;
92typedef xmlXPathObject *xmlXPathObjectPtr;
93struct _xmlXPathObject {
94 xmlXPathObjectType type;
95 xmlNodeSetPtr nodesetval;
96 int boolval;
97 double floatval;
98 xmlChar *stringval;
99 void *user;
100 int index;
101 void *user2;
102 int index2;
103};
104
105/*
106 * A conversion function is associated to a type and used to cast
107 * the new type to primitive values.
108 */
109typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
110
111/*
112 * Extra type: a name and a conversion function.
113 */
114
115typedef struct _xmlXPathType xmlXPathType;
116typedef xmlXPathType *xmlXPathTypePtr;
117struct _xmlXPathType {
118 const xmlChar *name; /* the type name */
119 xmlXPathConvertFunc func; /* the conversion function */
120};
121
122/*
123 * Extra variable: a name and a value.
124 */
125
126typedef struct _xmlXPathVariable xmlXPathVariable;
127typedef xmlXPathVariable *xmlXPathVariablePtr;
128struct _xmlXPathVariable {
129 const xmlChar *name; /* the variable name */
130 xmlXPathObjectPtr value; /* the value */
131};
132
133/*
134 * an evaluation function, the parameters are on the context stack
135 */
136
137typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs);
138
139/*
140 * Extra function: a name and a evaluation function.
141 */
142
143typedef struct _xmlXPathFunct xmlXPathFunct;
144typedef xmlXPathFunct *xmlXPathFuncPtr;
145struct _xmlXPathFunct {
146 const xmlChar *name; /* the function name */
147 xmlXPathEvalFunc func; /* the evaluation function */
148};
149
150/*
151 * An axis traversal function. To traverse an axis, the engine calls
152 * the first time with cur == NULL and repeat until the function returns
153 * NULL indicating the end of the axis traversal.
154 */
155
156typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
157 xmlXPathObjectPtr cur);
158
159/*
160 * Extra axis: a name and an axis function.
161 */
162
163typedef struct _xmlXPathAxis xmlXPathAxis;
164typedef xmlXPathAxis *xmlXPathAxisPtr;
165struct _xmlXPathAxis {
166 const xmlChar *name; /* the axis name */
167 xmlXPathAxisFunc func; /* the search function */
168};
169
Daniel Veillardbed7b052001-05-19 14:59:49 +0000170/**
171 * xmlXPathContext:
172 *
Owen Taylor3473f882001-02-23 17:55:21 +0000173 * Expression evaluation occurs with respect to a context.
174 * he context consists of:
175 * - a node (the context node)
176 * - a node list (the context node list)
177 * - a set of variable bindings
178 * - a function library
179 * - the set of namespace declarations in scope for the expression
180 * Following the switch to hash tables, this need to be trimmed up at
181 * the next binary incompatible release.
182 */
183
184struct _xmlXPathContext {
185 xmlDocPtr doc; /* The current document */
186 xmlNodePtr node; /* The current node */
187
188 int nb_variables_unused; /* unused (hash table) */
189 int max_variables_unused; /* unused (hash table) */
190 xmlHashTablePtr varHash; /* Hash table of defined variables */
191
192 int nb_types; /* number of defined types */
193 int max_types; /* max number of types */
194 xmlXPathTypePtr types; /* Array of defined types */
195
196 int nb_funcs_unused; /* unused (hash table) */
197 int max_funcs_unused; /* unused (hash table) */
198 xmlHashTablePtr funcHash; /* Hash table of defined funcs */
199
200 int nb_axis; /* number of defined axis */
201 int max_axis; /* max number of axis */
202 xmlXPathAxisPtr axis; /* Array of defined axis */
203
204 /* the namespace nodes of the context node */
205 xmlNsPtr *namespaces; /* Array of namespaces */
206 int nsNr; /* number of namespace in scope */
207 void *user; /* function to free */
208
209 /* extra variables */
210 int contextSize; /* the context size */
211 int proximityPosition; /* the proximity position */
212
213 /* extra stuff for XPointer */
214 int xptr; /* it this an XPointer context */
215 xmlNodePtr here; /* for here() */
216 xmlNodePtr origin; /* for origin() */
217
218 /* the set of namespace declarations in scope for the expression */
219 xmlHashTablePtr nsHash; /* The namespaces hash table */
220 void *varLookupFunc; /* variable lookup func */
221 void *varLookupData; /* variable lookup data */
222
223 /* Possibility to link in an extra item */
224 void *extra; /* needed for XSLT */
Daniel Veillard42596ad2001-05-22 16:57:14 +0000225
226 /* The function name and URI when calling a function */
227 const xmlChar *function;
228 const xmlChar *functionURI;
Owen Taylor3473f882001-02-23 17:55:21 +0000229};
230
231/*
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000232 * The structure of a compiled expression form is not public
233 */
234
235typedef struct _xmlXPathCompExpr xmlXPathCompExpr;
236typedef xmlXPathCompExpr *xmlXPathCompExprPtr;
237
Daniel Veillardbed7b052001-05-19 14:59:49 +0000238/**
239 * xmlXPathParserContext:
240 *
Owen Taylor3473f882001-02-23 17:55:21 +0000241 * An XPath parser context, it contains pure parsing informations,
242 * an xmlXPathContext, and the stack of objects.
243 */
244struct _xmlXPathParserContext {
245 const xmlChar *cur; /* the current char being parsed */
246 const xmlChar *base; /* the full expression */
247
248 int error; /* error code */
249
Owen Taylor3473f882001-02-23 17:55:21 +0000250 xmlXPathContextPtr context; /* the evaluation context */
251 xmlXPathObjectPtr value; /* the current value */
252 int valueNr; /* number of values stacked */
253 int valueMax; /* max number of values stacked */
254 xmlXPathObjectPtr *valueTab; /* stack of values */
Daniel Veillardd007d6c2001-03-19 00:01:07 +0000255
256 xmlXPathCompExprPtr comp; /* the precompiled expression */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000257 int xptr; /* it this an XPointer expression */
Daniel Veillardf06307e2001-07-03 10:35:50 +0000258 xmlNodePtr ancestor; /* used for walking preceding axis */
Owen Taylor3473f882001-02-23 17:55:21 +0000259};
260
Daniel Veillardbed7b052001-05-19 14:59:49 +0000261/**
262 * xmlXPathFunction:
263 *
Owen Taylor3473f882001-02-23 17:55:21 +0000264 * An XPath function
265 * The arguments (if any) are popped out of the context stack
266 * and the result is pushed on the stack.
267 */
268
269typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
270
271/************************************************************************
272 * *
273 * Public API *
274 * *
275 ************************************************************************/
276
277/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000278 * Objects and Nodesets handling
Owen Taylor3473f882001-02-23 17:55:21 +0000279 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000280
281/* These macros may later turn into functions */
282#define xmlXPathNodeSetGetLength(ns) ((ns) ? (ns)->nodeNr : 0)
283#define xmlXPathNodeSetItem(ns, index) \
284 ((((ns) != NULL) && \
285 ((index) > 0) && ((index) <= (ns)->nodeNr)) ? \
286 (ns)->nodeTab[(index)] \
287 : NULL)
288
289
Owen Taylor3473f882001-02-23 17:55:21 +0000290void xmlXPathFreeObject (xmlXPathObjectPtr obj);
Owen Taylor3473f882001-02-23 17:55:21 +0000291xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val);
292void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
293void xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
294xmlXPathObjectPtr xmlXPathObjectCopy (xmlXPathObjectPtr val);
295int xmlXPathCmpNodes (xmlNodePtr node1,
296 xmlNodePtr node2);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000297/**
298 * Conversion functions to basic types
299 */
Daniel Veillardba0b8c92001-05-15 09:43:47 +0000300int xmlXPathCastNumberToBoolean (double val);
301int xmlXPathCastStringToBoolean (const xmlChar * val);
Daniel Veillardbed7b052001-05-19 14:59:49 +0000302int xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns);
Daniel Veillardba0b8c92001-05-15 09:43:47 +0000303int xmlXPathCastToBoolean (xmlXPathObjectPtr val);
304
305double xmlXPathCastBooleanToNumber (int val);
306double xmlXPathCastStringToNumber (const xmlChar * val);
307double xmlXPathCastNodeToNumber (xmlNodePtr node);
308double xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns);
309double xmlXPathCastToNumber (xmlXPathObjectPtr val);
310
311xmlChar * xmlXPathCastBooleanToString (int val);
312xmlChar * xmlXPathCastNumberToString (double val);
313xmlChar * xmlXPathCastNodeToString (xmlNodePtr node);
314xmlChar * xmlXPathCastNodeSetToString (xmlNodeSetPtr ns);
315xmlChar * xmlXPathCastToString (xmlXPathObjectPtr val);
316
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000317xmlXPathObjectPtr xmlXPathConvertBoolean (xmlXPathObjectPtr val);
318xmlXPathObjectPtr xmlXPathConvertNumber (xmlXPathObjectPtr val);
319xmlXPathObjectPtr xmlXPathConvertString (xmlXPathObjectPtr val);
Owen Taylor3473f882001-02-23 17:55:21 +0000320
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000321/**
322 * Context handling
323 */
324void xmlXPathInit (void);
325xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
326void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
327
328/**
329 * Evaluation functions.
330 */
331xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
332 xmlXPathContextPtr ctxt);
333xmlXPathObjectPtr xmlXPathEvalXPtrExpr (const xmlChar *str,
334 xmlXPathContextPtr ctxt);
335xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
336 xmlXPathContextPtr ctxt);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000337int xmlXPathEvalPredicate (xmlXPathContextPtr ctxt,
338 xmlXPathObjectPtr res);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000339/**
340 * Separate compilation/evaluation entry points
341 */
342xmlXPathCompExprPtr xmlXPathCompile (const xmlChar *str);
343xmlXPathObjectPtr xmlXPathCompiledEval (xmlXPathCompExprPtr comp,
344 xmlXPathContextPtr ctx);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000345void xmlXPathFreeCompExpr (xmlXPathCompExprPtr comp);
Owen Taylor3473f882001-02-23 17:55:21 +0000346#ifdef __cplusplus
347}
348#endif
349#endif /* ! __XML_XPATH_H__ */