blob: d7471dfb49d581ee64a4a50cc2360c6905d61345 [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
Igor Zlatkovic76874e42003-08-25 09:05:12 +000015#include <libxml/xmlversion.h>
Owen Taylor3473f882001-02-23 17:55:21 +000016#include <libxml/tree.h>
17#include <libxml/hash.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23typedef struct _xmlXPathContext xmlXPathContext;
24typedef xmlXPathContext *xmlXPathContextPtr;
25typedef struct _xmlXPathParserContext xmlXPathParserContext;
26typedef xmlXPathParserContext *xmlXPathParserContextPtr;
27
28/**
Daniel Veillard61f26172002-03-12 18:46:39 +000029 * The set of XPath error codes.
Owen Taylor3473f882001-02-23 17:55:21 +000030 */
31
32typedef enum {
33 XPATH_EXPRESSION_OK = 0,
34 XPATH_NUMBER_ERROR,
35 XPATH_UNFINISHED_LITERAL_ERROR,
36 XPATH_START_LITERAL_ERROR,
37 XPATH_VARIABLE_REF_ERROR,
38 XPATH_UNDEF_VARIABLE_ERROR,
39 XPATH_INVALID_PREDICATE_ERROR,
40 XPATH_EXPR_ERROR,
41 XPATH_UNCLOSED_ERROR,
42 XPATH_UNKNOWN_FUNC_ERROR,
43 XPATH_INVALID_OPERAND,
44 XPATH_INVALID_TYPE,
45 XPATH_INVALID_ARITY,
46 XPATH_INVALID_CTXT_SIZE,
47 XPATH_INVALID_CTXT_POSITION,
48 XPATH_MEMORY_ERROR,
49 XPTR_SYNTAX_ERROR,
50 XPTR_RESOURCE_ERROR,
51 XPTR_SUB_RESOURCE_ERROR,
Daniel Veillard61d80a22001-04-27 17:13:01 +000052 XPATH_UNDEF_PREFIX_ERROR,
53 XPATH_ENCODING_ERROR,
54 XPATH_INVALID_CHAR_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +000055} xmlXPathError;
56
57/*
Daniel Veillard61f26172002-03-12 18:46:39 +000058 * A node-set (an unordered collection of nodes without duplicates).
Owen Taylor3473f882001-02-23 17:55:21 +000059 */
60typedef struct _xmlNodeSet xmlNodeSet;
61typedef xmlNodeSet *xmlNodeSetPtr;
62struct _xmlNodeSet {
63 int nodeNr; /* number of nodes in the set */
64 int nodeMax; /* size of the array as allocated */
65 xmlNodePtr *nodeTab; /* array of nodes in no particular order */
Daniel Veillard044fc6b2002-03-04 17:09:44 +000066 /* @@ with_ns to check wether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +000067};
68
69/*
70 * An expression is evaluated to yield an object, which
71 * has one of the following four basic types:
72 * - node-set
73 * - boolean
74 * - number
75 * - string
76 *
77 * @@ XPointer will add more types !
78 */
79
80typedef enum {
81 XPATH_UNDEFINED = 0,
82 XPATH_NODESET = 1,
83 XPATH_BOOLEAN = 2,
84 XPATH_NUMBER = 3,
85 XPATH_STRING = 4,
86 XPATH_POINT = 5,
87 XPATH_RANGE = 6,
88 XPATH_LOCATIONSET = 7,
89 XPATH_USERS = 8,
90 XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */
91} xmlXPathObjectType;
92
93typedef struct _xmlXPathObject xmlXPathObject;
94typedef xmlXPathObject *xmlXPathObjectPtr;
95struct _xmlXPathObject {
96 xmlXPathObjectType type;
97 xmlNodeSetPtr nodesetval;
98 int boolval;
99 double floatval;
100 xmlChar *stringval;
101 void *user;
102 int index;
103 void *user2;
104 int index2;
105};
106
Daniel Veillard9d06d302002-01-22 18:15:52 +0000107/**
108 * xmlXPathConvertFunc:
109 * @obj: an XPath object
110 * @type: the number of the target type
111 *
Owen Taylor3473f882001-02-23 17:55:21 +0000112 * A conversion function is associated to a type and used to cast
113 * the new type to primitive values.
Daniel Veillard9d06d302002-01-22 18:15:52 +0000114 *
115 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000116 */
117typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
118
119/*
120 * Extra type: a name and a conversion function.
121 */
122
123typedef struct _xmlXPathType xmlXPathType;
124typedef xmlXPathType *xmlXPathTypePtr;
125struct _xmlXPathType {
126 const xmlChar *name; /* the type name */
127 xmlXPathConvertFunc func; /* the conversion function */
128};
129
130/*
131 * Extra variable: a name and a value.
132 */
133
134typedef struct _xmlXPathVariable xmlXPathVariable;
135typedef xmlXPathVariable *xmlXPathVariablePtr;
136struct _xmlXPathVariable {
137 const xmlChar *name; /* the variable name */
138 xmlXPathObjectPtr value; /* the value */
139};
140
Daniel Veillard5168dbf2001-07-07 00:18:23 +0000141/**
142 * xmlXPathEvalFunc:
143 * @ctxt: an XPath parser context
144 * @nargs: the number of arguments passed to the function
145 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000146 * An XPath evaluation function, the parameters are on the XPath context stack.
Owen Taylor3473f882001-02-23 17:55:21 +0000147 */
148
Daniel Veillard5168dbf2001-07-07 00:18:23 +0000149typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt,
150 int nargs);
Owen Taylor3473f882001-02-23 17:55:21 +0000151
152/*
153 * Extra function: a name and a evaluation function.
154 */
155
156typedef struct _xmlXPathFunct xmlXPathFunct;
157typedef xmlXPathFunct *xmlXPathFuncPtr;
158struct _xmlXPathFunct {
159 const xmlChar *name; /* the function name */
160 xmlXPathEvalFunc func; /* the evaluation function */
161};
162
Daniel Veillard9d06d302002-01-22 18:15:52 +0000163/**
164 * xmlXPathAxisFunc:
165 * @ctxt: the XPath interpreter context
166 * @cur: the previous node being explored on that axis
167 *
Owen Taylor3473f882001-02-23 17:55:21 +0000168 * An axis traversal function. To traverse an axis, the engine calls
169 * the first time with cur == NULL and repeat until the function returns
170 * NULL indicating the end of the axis traversal.
Daniel Veillard9d06d302002-01-22 18:15:52 +0000171 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000172 * Returns the next node in that axis or NULL if at the end of the axis.
Owen Taylor3473f882001-02-23 17:55:21 +0000173 */
174
Daniel Veillard9d06d302002-01-22 18:15:52 +0000175typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
176 xmlXPathObjectPtr cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000177
178/*
179 * Extra axis: a name and an axis function.
180 */
181
182typedef struct _xmlXPathAxis xmlXPathAxis;
183typedef xmlXPathAxis *xmlXPathAxisPtr;
184struct _xmlXPathAxis {
185 const xmlChar *name; /* the axis name */
186 xmlXPathAxisFunc func; /* the search function */
187};
188
Daniel Veillardbed7b052001-05-19 14:59:49 +0000189/**
190 * xmlXPathContext:
191 *
Owen Taylor3473f882001-02-23 17:55:21 +0000192 * Expression evaluation occurs with respect to a context.
193 * he context consists of:
194 * - a node (the context node)
195 * - a node list (the context node list)
196 * - a set of variable bindings
197 * - a function library
198 * - the set of namespace declarations in scope for the expression
199 * Following the switch to hash tables, this need to be trimmed up at
200 * the next binary incompatible release.
201 */
202
203struct _xmlXPathContext {
204 xmlDocPtr doc; /* The current document */
205 xmlNodePtr node; /* The current node */
206
207 int nb_variables_unused; /* unused (hash table) */
208 int max_variables_unused; /* unused (hash table) */
209 xmlHashTablePtr varHash; /* Hash table of defined variables */
210
211 int nb_types; /* number of defined types */
212 int max_types; /* max number of types */
213 xmlXPathTypePtr types; /* Array of defined types */
214
215 int nb_funcs_unused; /* unused (hash table) */
216 int max_funcs_unused; /* unused (hash table) */
217 xmlHashTablePtr funcHash; /* Hash table of defined funcs */
218
219 int nb_axis; /* number of defined axis */
220 int max_axis; /* max number of axis */
221 xmlXPathAxisPtr axis; /* Array of defined axis */
222
223 /* the namespace nodes of the context node */
224 xmlNsPtr *namespaces; /* Array of namespaces */
225 int nsNr; /* number of namespace in scope */
226 void *user; /* function to free */
227
228 /* extra variables */
229 int contextSize; /* the context size */
230 int proximityPosition; /* the proximity position */
231
232 /* extra stuff for XPointer */
233 int xptr; /* it this an XPointer context */
234 xmlNodePtr here; /* for here() */
235 xmlNodePtr origin; /* for origin() */
236
237 /* the set of namespace declarations in scope for the expression */
238 xmlHashTablePtr nsHash; /* The namespaces hash table */
239 void *varLookupFunc; /* variable lookup func */
240 void *varLookupData; /* variable lookup data */
241
242 /* Possibility to link in an extra item */
243 void *extra; /* needed for XSLT */
Daniel Veillard42596ad2001-05-22 16:57:14 +0000244
245 /* The function name and URI when calling a function */
246 const xmlChar *function;
247 const xmlChar *functionURI;
Thomas Broyerba4ad322001-07-26 16:55:21 +0000248
249 /* function lookup function and data */
250 void *funcLookupFunc; /* function lookup func */
251 void *funcLookupData; /* function lookup data */
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000252
253 /* temporary namespace lists kept for walking the namespace axis */
254 xmlNsPtr *tmpNsList; /* Array of namespaces */
255 int tmpNsNr; /* number of namespace in scope */
Owen Taylor3473f882001-02-23 17:55:21 +0000256};
257
258/*
Daniel Veillard61f26172002-03-12 18:46:39 +0000259 * The structure of a compiled expression form is not public.
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000260 */
261
262typedef struct _xmlXPathCompExpr xmlXPathCompExpr;
263typedef xmlXPathCompExpr *xmlXPathCompExprPtr;
264
Daniel Veillardbed7b052001-05-19 14:59:49 +0000265/**
266 * xmlXPathParserContext:
267 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000268 * An XPath parser context. It contains pure parsing informations,
Owen Taylor3473f882001-02-23 17:55:21 +0000269 * an xmlXPathContext, and the stack of objects.
270 */
271struct _xmlXPathParserContext {
272 const xmlChar *cur; /* the current char being parsed */
273 const xmlChar *base; /* the full expression */
274
275 int error; /* error code */
276
Owen Taylor3473f882001-02-23 17:55:21 +0000277 xmlXPathContextPtr context; /* the evaluation context */
278 xmlXPathObjectPtr value; /* the current value */
279 int valueNr; /* number of values stacked */
280 int valueMax; /* max number of values stacked */
281 xmlXPathObjectPtr *valueTab; /* stack of values */
Daniel Veillardd007d6c2001-03-19 00:01:07 +0000282
283 xmlXPathCompExprPtr comp; /* the precompiled expression */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000284 int xptr; /* it this an XPointer expression */
Daniel Veillardf06307e2001-07-03 10:35:50 +0000285 xmlNodePtr ancestor; /* used for walking preceding axis */
Owen Taylor3473f882001-02-23 17:55:21 +0000286};
287
Daniel Veillardbed7b052001-05-19 14:59:49 +0000288/**
289 * xmlXPathFunction:
Daniel Veillard9d06d302002-01-22 18:15:52 +0000290 * @ctxt: the XPath interprestation context
291 * @nargs: the number of arguments
Daniel Veillardbed7b052001-05-19 14:59:49 +0000292 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000293 * An XPath function.
Daniel Veillard9d06d302002-01-22 18:15:52 +0000294 * The arguments (if any) are popped out from the context stack
Owen Taylor3473f882001-02-23 17:55:21 +0000295 * and the result is pushed on the stack.
296 */
297
298typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
299
300/************************************************************************
301 * *
302 * Public API *
303 * *
304 ************************************************************************/
305
306/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000307 * Objects and Nodesets handling
Owen Taylor3473f882001-02-23 17:55:21 +0000308 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000309
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000310XMLPUBVAR double xmlXPathNAN;
311XMLPUBVAR double xmlXPathPINF;
312XMLPUBVAR double xmlXPathNINF;
Thomas Broyer496be682001-07-15 22:59:18 +0000313
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000314XMLPUBFUN int XMLCALL
315 xmlXPathIsNaN (double val);
316XMLPUBFUN int XMLCALL
317 xmlXPathIsInf (double val);
Daniel Veillardcda96922001-08-21 10:56:31 +0000318
Daniel Veillard790142b2001-05-15 10:51:53 +0000319/* These macros may later turn into functions */
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000320/**
321 * xmlXPathNodeSetGetLength:
322 * @ns: a node-set
323 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000324 * Implement a functionality similar to the DOM NodeList.length.
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000325 *
326 * Returns the number of nodes in the node-set.
327 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000328#define xmlXPathNodeSetGetLength(ns) ((ns) ? (ns)->nodeNr : 0)
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000329/**
330 * xmlXPathNodeSetItem:
331 * @ns: a node-set
332 * @index: index of a node in the set
333 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000334 * Implements a functionality similar to the DOM NodeList.item().
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000335 *
336 * Returns the xmlNodePtr at the given @index in @ns or NULL if
337 * @index is out of range (0 to length-1)
338 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000339#define xmlXPathNodeSetItem(ns, index) \
340 ((((ns) != NULL) && \
Thomas Broyer496be682001-07-15 22:59:18 +0000341 ((index) >= 0) && ((index) < (ns)->nodeNr)) ? \
Daniel Veillard790142b2001-05-15 10:51:53 +0000342 (ns)->nodeTab[(index)] \
343 : NULL)
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000344/**
345 * xmlXPathNodeSetIsEmpty:
346 * @ns: a node-set
347 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000348 * Checks whether @ns is empty or not.
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000349 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000350 * Returns %TRUE if @ns is an empty node-set.
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000351 */
352#define xmlXPathNodeSetIsEmpty(ns) \
353 (((ns) == NULL) || ((ns)->nodeNr == 0) || ((ns)->nodeTab == NULL))
Daniel Veillard790142b2001-05-15 10:51:53 +0000354
355
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000356XMLPUBFUN void XMLCALL
357 xmlXPathFreeObject (xmlXPathObjectPtr obj);
358XMLPUBFUN xmlNodeSetPtr XMLCALL
359 xmlXPathNodeSetCreate (xmlNodePtr val);
360XMLPUBFUN void XMLCALL
361 xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
362XMLPUBFUN void XMLCALL
363 xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
364XMLPUBFUN xmlXPathObjectPtr XMLCALL
365 xmlXPathObjectCopy (xmlXPathObjectPtr val);
366XMLPUBFUN int XMLCALL
367 xmlXPathCmpNodes (xmlNodePtr node1,
Owen Taylor3473f882001-02-23 17:55:21 +0000368 xmlNodePtr node2);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000369/**
Daniel Veillard61f26172002-03-12 18:46:39 +0000370 * Conversion functions to basic types.
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000371 */
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000372XMLPUBFUN int XMLCALL
373 xmlXPathCastNumberToBoolean (double val);
374XMLPUBFUN int XMLCALL
375 xmlXPathCastStringToBoolean (const xmlChar * val);
376XMLPUBFUN int XMLCALL
377 xmlXPathCastNodeSetToBoolean(xmlNodeSetPtr ns);
378XMLPUBFUN int XMLCALL
379 xmlXPathCastToBoolean (xmlXPathObjectPtr val);
Daniel Veillardba0b8c92001-05-15 09:43:47 +0000380
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000381XMLPUBFUN double XMLCALL
382 xmlXPathCastBooleanToNumber (int val);
383XMLPUBFUN double XMLCALL
384 xmlXPathCastStringToNumber (const xmlChar * val);
385XMLPUBFUN double XMLCALL
386 xmlXPathCastNodeToNumber (xmlNodePtr node);
387XMLPUBFUN double XMLCALL
388 xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns);
389XMLPUBFUN double XMLCALL
390 xmlXPathCastToNumber (xmlXPathObjectPtr val);
Daniel Veillardba0b8c92001-05-15 09:43:47 +0000391
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000392XMLPUBFUN xmlChar * XMLCALL
393 xmlXPathCastBooleanToString (int val);
394XMLPUBFUN xmlChar * XMLCALL
395 xmlXPathCastNumberToString (double val);
396XMLPUBFUN xmlChar * XMLCALL
397 xmlXPathCastNodeToString (xmlNodePtr node);
398XMLPUBFUN xmlChar * XMLCALL
399 xmlXPathCastNodeSetToString (xmlNodeSetPtr ns);
400XMLPUBFUN xmlChar * XMLCALL
401 xmlXPathCastToString (xmlXPathObjectPtr val);
Daniel Veillardba0b8c92001-05-15 09:43:47 +0000402
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000403XMLPUBFUN xmlXPathObjectPtr XMLCALL
404 xmlXPathConvertBoolean (xmlXPathObjectPtr val);
405XMLPUBFUN xmlXPathObjectPtr XMLCALL
406 xmlXPathConvertNumber (xmlXPathObjectPtr val);
407XMLPUBFUN xmlXPathObjectPtr XMLCALL
408 xmlXPathConvertString (xmlXPathObjectPtr val);
Owen Taylor3473f882001-02-23 17:55:21 +0000409
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000410/**
Daniel Veillard61f26172002-03-12 18:46:39 +0000411 * Context handling.
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000412 */
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000413XMLPUBFUN void XMLCALL
414 xmlXPathInit (void);
415XMLPUBFUN xmlXPathContextPtr XMLCALL
416 xmlXPathNewContext (xmlDocPtr doc);
417XMLPUBFUN void XMLCALL
418 xmlXPathFreeContext (xmlXPathContextPtr ctxt);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000419
420/**
421 * Evaluation functions.
422 */
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000423XMLPUBFUN long XMLCALL
424 xmlXPathOrderDocElems (xmlDocPtr doc);
425XMLPUBFUN xmlXPathObjectPtr XMLCALL
426 xmlXPathEval (const xmlChar *str,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000427 xmlXPathContextPtr ctx);
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000428XMLPUBFUN xmlXPathObjectPtr XMLCALL
429 xmlXPathEvalExpression (const xmlChar *str,
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000430 xmlXPathContextPtr ctxt);
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000431XMLPUBFUN int XMLCALL
432 xmlXPathEvalPredicate (xmlXPathContextPtr ctxt,
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000433 xmlXPathObjectPtr res);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000434/**
Daniel Veillard61f26172002-03-12 18:46:39 +0000435 * Separate compilation/evaluation entry points.
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000436 */
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000437XMLPUBFUN xmlXPathCompExprPtr XMLCALL
438 xmlXPathCompile (const xmlChar *str);
439XMLPUBFUN xmlXPathObjectPtr XMLCALL
440 xmlXPathCompiledEval (xmlXPathCompExprPtr comp,
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000441 xmlXPathContextPtr ctx);
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000442XMLPUBFUN void XMLCALL
443 xmlXPathFreeCompExpr (xmlXPathCompExprPtr comp);
Owen Taylor3473f882001-02-23 17:55:21 +0000444#ifdef __cplusplus
445}
446#endif
447#endif /* ! __XML_XPATH_H__ */