blob: dbd1b777988259409478fc4889b1f33bf72fed01 [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>
Daniel Veillardd96f6d32003-10-07 21:25:12 +000016#include <libxml/xmlerror.h>
Owen Taylor3473f882001-02-23 17:55:21 +000017#include <libxml/tree.h>
18#include <libxml/hash.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24typedef struct _xmlXPathContext xmlXPathContext;
25typedef xmlXPathContext *xmlXPathContextPtr;
26typedef struct _xmlXPathParserContext xmlXPathParserContext;
27typedef xmlXPathParserContext *xmlXPathParserContextPtr;
28
29/**
Daniel Veillard61f26172002-03-12 18:46:39 +000030 * The set of XPath error codes.
Owen Taylor3473f882001-02-23 17:55:21 +000031 */
32
33typedef enum {
34 XPATH_EXPRESSION_OK = 0,
35 XPATH_NUMBER_ERROR,
36 XPATH_UNFINISHED_LITERAL_ERROR,
37 XPATH_START_LITERAL_ERROR,
38 XPATH_VARIABLE_REF_ERROR,
39 XPATH_UNDEF_VARIABLE_ERROR,
40 XPATH_INVALID_PREDICATE_ERROR,
41 XPATH_EXPR_ERROR,
42 XPATH_UNCLOSED_ERROR,
43 XPATH_UNKNOWN_FUNC_ERROR,
44 XPATH_INVALID_OPERAND,
45 XPATH_INVALID_TYPE,
46 XPATH_INVALID_ARITY,
47 XPATH_INVALID_CTXT_SIZE,
48 XPATH_INVALID_CTXT_POSITION,
49 XPATH_MEMORY_ERROR,
50 XPTR_SYNTAX_ERROR,
51 XPTR_RESOURCE_ERROR,
52 XPTR_SUB_RESOURCE_ERROR,
Daniel Veillard61d80a22001-04-27 17:13:01 +000053 XPATH_UNDEF_PREFIX_ERROR,
54 XPATH_ENCODING_ERROR,
55 XPATH_INVALID_CHAR_ERROR
Owen Taylor3473f882001-02-23 17:55:21 +000056} xmlXPathError;
57
58/*
Daniel Veillard61f26172002-03-12 18:46:39 +000059 * A node-set (an unordered collection of nodes without duplicates).
Owen Taylor3473f882001-02-23 17:55:21 +000060 */
61typedef struct _xmlNodeSet xmlNodeSet;
62typedef xmlNodeSet *xmlNodeSetPtr;
63struct _xmlNodeSet {
64 int nodeNr; /* number of nodes in the set */
65 int nodeMax; /* size of the array as allocated */
66 xmlNodePtr *nodeTab; /* array of nodes in no particular order */
Daniel Veillard044fc6b2002-03-04 17:09:44 +000067 /* @@ with_ns to check wether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +000068};
69
70/*
71 * An expression is evaluated to yield an object, which
72 * has one of the following four basic types:
73 * - node-set
74 * - boolean
75 * - number
76 * - string
77 *
78 * @@ XPointer will add more types !
79 */
80
81typedef enum {
82 XPATH_UNDEFINED = 0,
83 XPATH_NODESET = 1,
84 XPATH_BOOLEAN = 2,
85 XPATH_NUMBER = 3,
86 XPATH_STRING = 4,
87 XPATH_POINT = 5,
88 XPATH_RANGE = 6,
89 XPATH_LOCATIONSET = 7,
90 XPATH_USERS = 8,
91 XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */
92} xmlXPathObjectType;
93
94typedef struct _xmlXPathObject xmlXPathObject;
95typedef xmlXPathObject *xmlXPathObjectPtr;
96struct _xmlXPathObject {
97 xmlXPathObjectType type;
98 xmlNodeSetPtr nodesetval;
99 int boolval;
100 double floatval;
101 xmlChar *stringval;
102 void *user;
103 int index;
104 void *user2;
105 int index2;
106};
107
Daniel Veillard9d06d302002-01-22 18:15:52 +0000108/**
109 * xmlXPathConvertFunc:
110 * @obj: an XPath object
111 * @type: the number of the target type
112 *
Owen Taylor3473f882001-02-23 17:55:21 +0000113 * A conversion function is associated to a type and used to cast
114 * the new type to primitive values.
Daniel Veillard9d06d302002-01-22 18:15:52 +0000115 *
116 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000117 */
118typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
119
120/*
121 * Extra type: a name and a conversion function.
122 */
123
124typedef struct _xmlXPathType xmlXPathType;
125typedef xmlXPathType *xmlXPathTypePtr;
126struct _xmlXPathType {
127 const xmlChar *name; /* the type name */
128 xmlXPathConvertFunc func; /* the conversion function */
129};
130
131/*
132 * Extra variable: a name and a value.
133 */
134
135typedef struct _xmlXPathVariable xmlXPathVariable;
136typedef xmlXPathVariable *xmlXPathVariablePtr;
137struct _xmlXPathVariable {
138 const xmlChar *name; /* the variable name */
139 xmlXPathObjectPtr value; /* the value */
140};
141
Daniel Veillard5168dbf2001-07-07 00:18:23 +0000142/**
143 * xmlXPathEvalFunc:
144 * @ctxt: an XPath parser context
145 * @nargs: the number of arguments passed to the function
146 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000147 * An XPath evaluation function, the parameters are on the XPath context stack.
Owen Taylor3473f882001-02-23 17:55:21 +0000148 */
149
Daniel Veillard5168dbf2001-07-07 00:18:23 +0000150typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt,
151 int nargs);
Owen Taylor3473f882001-02-23 17:55:21 +0000152
153/*
154 * Extra function: a name and a evaluation function.
155 */
156
157typedef struct _xmlXPathFunct xmlXPathFunct;
158typedef xmlXPathFunct *xmlXPathFuncPtr;
159struct _xmlXPathFunct {
160 const xmlChar *name; /* the function name */
161 xmlXPathEvalFunc func; /* the evaluation function */
162};
163
Daniel Veillard9d06d302002-01-22 18:15:52 +0000164/**
165 * xmlXPathAxisFunc:
166 * @ctxt: the XPath interpreter context
167 * @cur: the previous node being explored on that axis
168 *
Owen Taylor3473f882001-02-23 17:55:21 +0000169 * An axis traversal function. To traverse an axis, the engine calls
170 * the first time with cur == NULL and repeat until the function returns
171 * NULL indicating the end of the axis traversal.
Daniel Veillard9d06d302002-01-22 18:15:52 +0000172 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000173 * Returns the next node in that axis or NULL if at the end of the axis.
Owen Taylor3473f882001-02-23 17:55:21 +0000174 */
175
Daniel Veillard9d06d302002-01-22 18:15:52 +0000176typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
177 xmlXPathObjectPtr cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000178
179/*
180 * Extra axis: a name and an axis function.
181 */
182
183typedef struct _xmlXPathAxis xmlXPathAxis;
184typedef xmlXPathAxis *xmlXPathAxisPtr;
185struct _xmlXPathAxis {
186 const xmlChar *name; /* the axis name */
187 xmlXPathAxisFunc func; /* the search function */
188};
189
Daniel Veillardbed7b052001-05-19 14:59:49 +0000190/**
191 * xmlXPathContext:
192 *
Owen Taylor3473f882001-02-23 17:55:21 +0000193 * Expression evaluation occurs with respect to a context.
194 * he context consists of:
195 * - a node (the context node)
196 * - a node list (the context node list)
197 * - a set of variable bindings
198 * - a function library
199 * - the set of namespace declarations in scope for the expression
200 * Following the switch to hash tables, this need to be trimmed up at
201 * the next binary incompatible release.
202 */
203
204struct _xmlXPathContext {
205 xmlDocPtr doc; /* The current document */
206 xmlNodePtr node; /* The current node */
207
208 int nb_variables_unused; /* unused (hash table) */
209 int max_variables_unused; /* unused (hash table) */
210 xmlHashTablePtr varHash; /* Hash table of defined variables */
211
212 int nb_types; /* number of defined types */
213 int max_types; /* max number of types */
214 xmlXPathTypePtr types; /* Array of defined types */
215
216 int nb_funcs_unused; /* unused (hash table) */
217 int max_funcs_unused; /* unused (hash table) */
218 xmlHashTablePtr funcHash; /* Hash table of defined funcs */
219
220 int nb_axis; /* number of defined axis */
221 int max_axis; /* max number of axis */
222 xmlXPathAxisPtr axis; /* Array of defined axis */
223
224 /* the namespace nodes of the context node */
225 xmlNsPtr *namespaces; /* Array of namespaces */
226 int nsNr; /* number of namespace in scope */
227 void *user; /* function to free */
228
229 /* extra variables */
230 int contextSize; /* the context size */
231 int proximityPosition; /* the proximity position */
232
233 /* extra stuff for XPointer */
234 int xptr; /* it this an XPointer context */
235 xmlNodePtr here; /* for here() */
236 xmlNodePtr origin; /* for origin() */
237
238 /* the set of namespace declarations in scope for the expression */
239 xmlHashTablePtr nsHash; /* The namespaces hash table */
240 void *varLookupFunc; /* variable lookup func */
241 void *varLookupData; /* variable lookup data */
242
243 /* Possibility to link in an extra item */
244 void *extra; /* needed for XSLT */
Daniel Veillard42596ad2001-05-22 16:57:14 +0000245
246 /* The function name and URI when calling a function */
247 const xmlChar *function;
248 const xmlChar *functionURI;
Thomas Broyerba4ad322001-07-26 16:55:21 +0000249
250 /* function lookup function and data */
251 void *funcLookupFunc; /* function lookup func */
252 void *funcLookupData; /* function lookup data */
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000253
254 /* temporary namespace lists kept for walking the namespace axis */
255 xmlNsPtr *tmpNsList; /* Array of namespaces */
256 int tmpNsNr; /* number of namespace in scope */
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000257
258 /* error reporting mechanism */
259 void *userData; /* user specific data block */
260 xmlStructuredErrorFunc error; /* the callback in case of errors */
261 xmlError lastError; /* the last error */
262 xmlNodePtr debugNode; /* the source node XSLT */
Owen Taylor3473f882001-02-23 17:55:21 +0000263};
264
265/*
Daniel Veillard61f26172002-03-12 18:46:39 +0000266 * The structure of a compiled expression form is not public.
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000267 */
268
269typedef struct _xmlXPathCompExpr xmlXPathCompExpr;
270typedef xmlXPathCompExpr *xmlXPathCompExprPtr;
271
Daniel Veillardbed7b052001-05-19 14:59:49 +0000272/**
273 * xmlXPathParserContext:
274 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000275 * An XPath parser context. It contains pure parsing informations,
Owen Taylor3473f882001-02-23 17:55:21 +0000276 * an xmlXPathContext, and the stack of objects.
277 */
278struct _xmlXPathParserContext {
279 const xmlChar *cur; /* the current char being parsed */
280 const xmlChar *base; /* the full expression */
281
282 int error; /* error code */
283
Owen Taylor3473f882001-02-23 17:55:21 +0000284 xmlXPathContextPtr context; /* the evaluation context */
285 xmlXPathObjectPtr value; /* the current value */
286 int valueNr; /* number of values stacked */
287 int valueMax; /* max number of values stacked */
288 xmlXPathObjectPtr *valueTab; /* stack of values */
Daniel Veillardd007d6c2001-03-19 00:01:07 +0000289
290 xmlXPathCompExprPtr comp; /* the precompiled expression */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000291 int xptr; /* it this an XPointer expression */
Daniel Veillardf06307e2001-07-03 10:35:50 +0000292 xmlNodePtr ancestor; /* used for walking preceding axis */
Owen Taylor3473f882001-02-23 17:55:21 +0000293};
294
Daniel Veillardbed7b052001-05-19 14:59:49 +0000295/**
296 * xmlXPathFunction:
Daniel Veillard9d06d302002-01-22 18:15:52 +0000297 * @ctxt: the XPath interprestation context
298 * @nargs: the number of arguments
Daniel Veillardbed7b052001-05-19 14:59:49 +0000299 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000300 * An XPath function.
Daniel Veillard9d06d302002-01-22 18:15:52 +0000301 * The arguments (if any) are popped out from the context stack
Owen Taylor3473f882001-02-23 17:55:21 +0000302 * and the result is pushed on the stack.
303 */
304
305typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
306
307/************************************************************************
308 * *
309 * Public API *
310 * *
311 ************************************************************************/
312
313/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000314 * Objects and Nodesets handling
Owen Taylor3473f882001-02-23 17:55:21 +0000315 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000316
Igor Zlatkovic76874e42003-08-25 09:05:12 +0000317XMLPUBVAR double xmlXPathNAN;
318XMLPUBVAR double xmlXPathPINF;
319XMLPUBVAR double xmlXPathNINF;
Thomas Broyer496be682001-07-15 22:59:18 +0000320
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000321XMLPUBFUN int XMLCALL
322 xmlXPathIsNaN (double val);
323XMLPUBFUN int XMLCALL
324 xmlXPathIsInf (double val);
Daniel Veillardcda96922001-08-21 10:56:31 +0000325
Daniel Veillard790142b2001-05-15 10:51:53 +0000326/* These macros may later turn into functions */
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000327/**
328 * xmlXPathNodeSetGetLength:
329 * @ns: a node-set
330 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000331 * Implement a functionality similar to the DOM NodeList.length.
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000332 *
333 * Returns the number of nodes in the node-set.
334 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000335#define xmlXPathNodeSetGetLength(ns) ((ns) ? (ns)->nodeNr : 0)
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000336/**
337 * xmlXPathNodeSetItem:
338 * @ns: a node-set
339 * @index: index of a node in the set
340 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000341 * Implements a functionality similar to the DOM NodeList.item().
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000342 *
343 * Returns the xmlNodePtr at the given @index in @ns or NULL if
344 * @index is out of range (0 to length-1)
345 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000346#define xmlXPathNodeSetItem(ns, index) \
347 ((((ns) != NULL) && \
Thomas Broyer496be682001-07-15 22:59:18 +0000348 ((index) >= 0) && ((index) < (ns)->nodeNr)) ? \
Daniel Veillard790142b2001-05-15 10:51:53 +0000349 (ns)->nodeTab[(index)] \
350 : NULL)
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000351/**
352 * xmlXPathNodeSetIsEmpty:
353 * @ns: a node-set
354 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000355 * Checks whether @ns is empty or not.
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000356 *
Daniel Veillard61f26172002-03-12 18:46:39 +0000357 * Returns %TRUE if @ns is an empty node-set.
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000358 */
359#define xmlXPathNodeSetIsEmpty(ns) \
360 (((ns) == NULL) || ((ns)->nodeNr == 0) || ((ns)->nodeTab == NULL))
Daniel Veillard790142b2001-05-15 10:51:53 +0000361
362
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000363XMLPUBFUN void XMLCALL
364 xmlXPathFreeObject (xmlXPathObjectPtr obj);
365XMLPUBFUN xmlNodeSetPtr XMLCALL
366 xmlXPathNodeSetCreate (xmlNodePtr val);
367XMLPUBFUN void XMLCALL
368 xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
369XMLPUBFUN void XMLCALL
370 xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
371XMLPUBFUN xmlXPathObjectPtr XMLCALL
372 xmlXPathObjectCopy (xmlXPathObjectPtr val);
373XMLPUBFUN int XMLCALL
374 xmlXPathCmpNodes (xmlNodePtr node1,
Owen Taylor3473f882001-02-23 17:55:21 +0000375 xmlNodePtr node2);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000376/**
Daniel Veillard61f26172002-03-12 18:46:39 +0000377 * Conversion functions to basic types.
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000378 */
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000379XMLPUBFUN int XMLCALL
380 xmlXPathCastNumberToBoolean (double val);
381XMLPUBFUN int XMLCALL
382 xmlXPathCastStringToBoolean (const xmlChar * val);
383XMLPUBFUN int XMLCALL
384 xmlXPathCastNodeSetToBoolean(xmlNodeSetPtr ns);
385XMLPUBFUN int XMLCALL
386 xmlXPathCastToBoolean (xmlXPathObjectPtr val);
Daniel Veillardba0b8c92001-05-15 09:43:47 +0000387
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000388XMLPUBFUN double XMLCALL
389 xmlXPathCastBooleanToNumber (int val);
390XMLPUBFUN double XMLCALL
391 xmlXPathCastStringToNumber (const xmlChar * val);
392XMLPUBFUN double XMLCALL
393 xmlXPathCastNodeToNumber (xmlNodePtr node);
394XMLPUBFUN double XMLCALL
395 xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns);
396XMLPUBFUN double XMLCALL
397 xmlXPathCastToNumber (xmlXPathObjectPtr val);
Daniel Veillardba0b8c92001-05-15 09:43:47 +0000398
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000399XMLPUBFUN xmlChar * XMLCALL
400 xmlXPathCastBooleanToString (int val);
401XMLPUBFUN xmlChar * XMLCALL
402 xmlXPathCastNumberToString (double val);
403XMLPUBFUN xmlChar * XMLCALL
404 xmlXPathCastNodeToString (xmlNodePtr node);
405XMLPUBFUN xmlChar * XMLCALL
406 xmlXPathCastNodeSetToString (xmlNodeSetPtr ns);
407XMLPUBFUN xmlChar * XMLCALL
408 xmlXPathCastToString (xmlXPathObjectPtr val);
Daniel Veillardba0b8c92001-05-15 09:43:47 +0000409
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000410XMLPUBFUN xmlXPathObjectPtr XMLCALL
411 xmlXPathConvertBoolean (xmlXPathObjectPtr val);
412XMLPUBFUN xmlXPathObjectPtr XMLCALL
413 xmlXPathConvertNumber (xmlXPathObjectPtr val);
414XMLPUBFUN xmlXPathObjectPtr XMLCALL
415 xmlXPathConvertString (xmlXPathObjectPtr val);
Owen Taylor3473f882001-02-23 17:55:21 +0000416
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000417/**
Daniel Veillard61f26172002-03-12 18:46:39 +0000418 * Context handling.
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000419 */
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000420XMLPUBFUN void XMLCALL
421 xmlXPathInit (void);
422XMLPUBFUN xmlXPathContextPtr XMLCALL
423 xmlXPathNewContext (xmlDocPtr doc);
424XMLPUBFUN void XMLCALL
425 xmlXPathFreeContext (xmlXPathContextPtr ctxt);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000426
427/**
428 * Evaluation functions.
429 */
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000430XMLPUBFUN long XMLCALL
431 xmlXPathOrderDocElems (xmlDocPtr doc);
432XMLPUBFUN xmlXPathObjectPtr XMLCALL
433 xmlXPathEval (const xmlChar *str,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000434 xmlXPathContextPtr ctx);
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000435XMLPUBFUN xmlXPathObjectPtr XMLCALL
436 xmlXPathEvalExpression (const xmlChar *str,
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000437 xmlXPathContextPtr ctxt);
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000438XMLPUBFUN int XMLCALL
439 xmlXPathEvalPredicate (xmlXPathContextPtr ctxt,
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000440 xmlXPathObjectPtr res);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000441/**
Daniel Veillard61f26172002-03-12 18:46:39 +0000442 * Separate compilation/evaluation entry points.
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000443 */
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000444XMLPUBFUN xmlXPathCompExprPtr XMLCALL
445 xmlXPathCompile (const xmlChar *str);
446XMLPUBFUN xmlXPathObjectPtr XMLCALL
447 xmlXPathCompiledEval (xmlXPathCompExprPtr comp,
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000448 xmlXPathContextPtr ctx);
Igor Zlatkovicaa3cfbd2003-08-27 08:59:58 +0000449XMLPUBFUN void XMLCALL
450 xmlXPathFreeCompExpr (xmlXPathCompExprPtr comp);
Owen Taylor3473f882001-02-23 17:55:21 +0000451#ifdef __cplusplus
452}
453#endif
454#endif /* ! __XML_XPATH_H__ */