blob: ef77e1344a385db4caf214a601f7358c5bba94e4 [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 */
Daniel Veillard044fc6b2002-03-04 17:09:44 +000065 /* @@ with_ns to check wether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +000066};
67
68/*
69 * An expression is evaluated to yield an object, which
70 * has one of the following four basic types:
71 * - node-set
72 * - boolean
73 * - number
74 * - string
75 *
76 * @@ XPointer will add more types !
77 */
78
79typedef enum {
80 XPATH_UNDEFINED = 0,
81 XPATH_NODESET = 1,
82 XPATH_BOOLEAN = 2,
83 XPATH_NUMBER = 3,
84 XPATH_STRING = 4,
85 XPATH_POINT = 5,
86 XPATH_RANGE = 6,
87 XPATH_LOCATIONSET = 7,
88 XPATH_USERS = 8,
89 XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */
90} xmlXPathObjectType;
91
92typedef struct _xmlXPathObject xmlXPathObject;
93typedef xmlXPathObject *xmlXPathObjectPtr;
94struct _xmlXPathObject {
95 xmlXPathObjectType type;
96 xmlNodeSetPtr nodesetval;
97 int boolval;
98 double floatval;
99 xmlChar *stringval;
100 void *user;
101 int index;
102 void *user2;
103 int index2;
104};
105
Daniel Veillard9d06d302002-01-22 18:15:52 +0000106/**
107 * xmlXPathConvertFunc:
108 * @obj: an XPath object
109 * @type: the number of the target type
110 *
Owen Taylor3473f882001-02-23 17:55:21 +0000111 * A conversion function is associated to a type and used to cast
112 * the new type to primitive values.
Daniel Veillard9d06d302002-01-22 18:15:52 +0000113 *
114 * Returns -1 in case of error, 0 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000115 */
116typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
117
118/*
119 * Extra type: a name and a conversion function.
120 */
121
122typedef struct _xmlXPathType xmlXPathType;
123typedef xmlXPathType *xmlXPathTypePtr;
124struct _xmlXPathType {
125 const xmlChar *name; /* the type name */
126 xmlXPathConvertFunc func; /* the conversion function */
127};
128
129/*
130 * Extra variable: a name and a value.
131 */
132
133typedef struct _xmlXPathVariable xmlXPathVariable;
134typedef xmlXPathVariable *xmlXPathVariablePtr;
135struct _xmlXPathVariable {
136 const xmlChar *name; /* the variable name */
137 xmlXPathObjectPtr value; /* the value */
138};
139
Daniel Veillard5168dbf2001-07-07 00:18:23 +0000140/**
141 * xmlXPathEvalFunc:
142 * @ctxt: an XPath parser context
143 * @nargs: the number of arguments passed to the function
144 *
Daniel Veillardb06c6142001-08-27 14:26:30 +0000145 * an XPath evaluation function, the parameters are on the XPath context stack
Owen Taylor3473f882001-02-23 17:55:21 +0000146 */
147
Daniel Veillard5168dbf2001-07-07 00:18:23 +0000148typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt,
149 int nargs);
Owen Taylor3473f882001-02-23 17:55:21 +0000150
151/*
152 * Extra function: a name and a evaluation function.
153 */
154
155typedef struct _xmlXPathFunct xmlXPathFunct;
156typedef xmlXPathFunct *xmlXPathFuncPtr;
157struct _xmlXPathFunct {
158 const xmlChar *name; /* the function name */
159 xmlXPathEvalFunc func; /* the evaluation function */
160};
161
Daniel Veillard9d06d302002-01-22 18:15:52 +0000162/**
163 * xmlXPathAxisFunc:
164 * @ctxt: the XPath interpreter context
165 * @cur: the previous node being explored on that axis
166 *
Owen Taylor3473f882001-02-23 17:55:21 +0000167 * An axis traversal function. To traverse an axis, the engine calls
168 * the first time with cur == NULL and repeat until the function returns
169 * NULL indicating the end of the axis traversal.
Daniel Veillard9d06d302002-01-22 18:15:52 +0000170 *
171 * Returns the next node in that axis or NULL if at the end of the axis
Owen Taylor3473f882001-02-23 17:55:21 +0000172 */
173
Daniel Veillard9d06d302002-01-22 18:15:52 +0000174typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
175 xmlXPathObjectPtr cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000176
177/*
178 * Extra axis: a name and an axis function.
179 */
180
181typedef struct _xmlXPathAxis xmlXPathAxis;
182typedef xmlXPathAxis *xmlXPathAxisPtr;
183struct _xmlXPathAxis {
184 const xmlChar *name; /* the axis name */
185 xmlXPathAxisFunc func; /* the search function */
186};
187
Daniel Veillardbed7b052001-05-19 14:59:49 +0000188/**
189 * xmlXPathContext:
190 *
Owen Taylor3473f882001-02-23 17:55:21 +0000191 * Expression evaluation occurs with respect to a context.
192 * he context consists of:
193 * - a node (the context node)
194 * - a node list (the context node list)
195 * - a set of variable bindings
196 * - a function library
197 * - the set of namespace declarations in scope for the expression
198 * Following the switch to hash tables, this need to be trimmed up at
199 * the next binary incompatible release.
200 */
201
202struct _xmlXPathContext {
203 xmlDocPtr doc; /* The current document */
204 xmlNodePtr node; /* The current node */
205
206 int nb_variables_unused; /* unused (hash table) */
207 int max_variables_unused; /* unused (hash table) */
208 xmlHashTablePtr varHash; /* Hash table of defined variables */
209
210 int nb_types; /* number of defined types */
211 int max_types; /* max number of types */
212 xmlXPathTypePtr types; /* Array of defined types */
213
214 int nb_funcs_unused; /* unused (hash table) */
215 int max_funcs_unused; /* unused (hash table) */
216 xmlHashTablePtr funcHash; /* Hash table of defined funcs */
217
218 int nb_axis; /* number of defined axis */
219 int max_axis; /* max number of axis */
220 xmlXPathAxisPtr axis; /* Array of defined axis */
221
222 /* the namespace nodes of the context node */
223 xmlNsPtr *namespaces; /* Array of namespaces */
224 int nsNr; /* number of namespace in scope */
225 void *user; /* function to free */
226
227 /* extra variables */
228 int contextSize; /* the context size */
229 int proximityPosition; /* the proximity position */
230
231 /* extra stuff for XPointer */
232 int xptr; /* it this an XPointer context */
233 xmlNodePtr here; /* for here() */
234 xmlNodePtr origin; /* for origin() */
235
236 /* the set of namespace declarations in scope for the expression */
237 xmlHashTablePtr nsHash; /* The namespaces hash table */
238 void *varLookupFunc; /* variable lookup func */
239 void *varLookupData; /* variable lookup data */
240
241 /* Possibility to link in an extra item */
242 void *extra; /* needed for XSLT */
Daniel Veillard42596ad2001-05-22 16:57:14 +0000243
244 /* The function name and URI when calling a function */
245 const xmlChar *function;
246 const xmlChar *functionURI;
Thomas Broyerba4ad322001-07-26 16:55:21 +0000247
248 /* function lookup function and data */
249 void *funcLookupFunc; /* function lookup func */
250 void *funcLookupData; /* function lookup data */
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000251
252 /* temporary namespace lists kept for walking the namespace axis */
253 xmlNsPtr *tmpNsList; /* Array of namespaces */
254 int tmpNsNr; /* number of namespace in scope */
Owen Taylor3473f882001-02-23 17:55:21 +0000255};
256
257/*
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000258 * The structure of a compiled expression form is not public
259 */
260
261typedef struct _xmlXPathCompExpr xmlXPathCompExpr;
262typedef xmlXPathCompExpr *xmlXPathCompExprPtr;
263
Daniel Veillardbed7b052001-05-19 14:59:49 +0000264/**
265 * xmlXPathParserContext:
266 *
Owen Taylor3473f882001-02-23 17:55:21 +0000267 * An XPath parser context, it contains pure parsing informations,
268 * an xmlXPathContext, and the stack of objects.
269 */
270struct _xmlXPathParserContext {
271 const xmlChar *cur; /* the current char being parsed */
272 const xmlChar *base; /* the full expression */
273
274 int error; /* error code */
275
Owen Taylor3473f882001-02-23 17:55:21 +0000276 xmlXPathContextPtr context; /* the evaluation context */
277 xmlXPathObjectPtr value; /* the current value */
278 int valueNr; /* number of values stacked */
279 int valueMax; /* max number of values stacked */
280 xmlXPathObjectPtr *valueTab; /* stack of values */
Daniel Veillardd007d6c2001-03-19 00:01:07 +0000281
282 xmlXPathCompExprPtr comp; /* the precompiled expression */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000283 int xptr; /* it this an XPointer expression */
Daniel Veillardf06307e2001-07-03 10:35:50 +0000284 xmlNodePtr ancestor; /* used for walking preceding axis */
Owen Taylor3473f882001-02-23 17:55:21 +0000285};
286
Daniel Veillardbed7b052001-05-19 14:59:49 +0000287/**
288 * xmlXPathFunction:
Daniel Veillard9d06d302002-01-22 18:15:52 +0000289 * @ctxt: the XPath interprestation context
290 * @nargs: the number of arguments
Daniel Veillardbed7b052001-05-19 14:59:49 +0000291 *
Owen Taylor3473f882001-02-23 17:55:21 +0000292 * An XPath function
Daniel Veillard9d06d302002-01-22 18:15:52 +0000293 * The arguments (if any) are popped out from the context stack
Owen Taylor3473f882001-02-23 17:55:21 +0000294 * and the result is pushed on the stack.
295 */
296
297typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
298
299/************************************************************************
300 * *
301 * Public API *
302 * *
303 ************************************************************************/
304
305/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000306 * Objects and Nodesets handling
Owen Taylor3473f882001-02-23 17:55:21 +0000307 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000308
Thomas Broyer496be682001-07-15 22:59:18 +0000309LIBXML_DLL_IMPORT extern double xmlXPathNAN;
310LIBXML_DLL_IMPORT extern double xmlXPathPINF;
311LIBXML_DLL_IMPORT extern double xmlXPathNINF;
312
Daniel Veillardcda96922001-08-21 10:56:31 +0000313int xmlXPathIsNaN (double val);
314int xmlXPathIsInf (double val);
315
Daniel Veillard790142b2001-05-15 10:51:53 +0000316/* These macros may later turn into functions */
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000317/**
318 * xmlXPathNodeSetGetLength:
319 * @ns: a node-set
320 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000321 * Implement a functionality similar to the DOM NodeList.length
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000322 *
323 * Returns the number of nodes in the node-set.
324 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000325#define xmlXPathNodeSetGetLength(ns) ((ns) ? (ns)->nodeNr : 0)
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000326/**
327 * xmlXPathNodeSetItem:
328 * @ns: a node-set
329 * @index: index of a node in the set
330 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000331 * Implements a functionality similar to the DOM NodeList.item()
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000332 *
333 * Returns the xmlNodePtr at the given @index in @ns or NULL if
334 * @index is out of range (0 to length-1)
335 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000336#define xmlXPathNodeSetItem(ns, index) \
337 ((((ns) != NULL) && \
Thomas Broyer496be682001-07-15 22:59:18 +0000338 ((index) >= 0) && ((index) < (ns)->nodeNr)) ? \
Daniel Veillard790142b2001-05-15 10:51:53 +0000339 (ns)->nodeTab[(index)] \
340 : NULL)
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000341/**
342 * xmlXPathNodeSetIsEmpty:
343 * @ns: a node-set
344 *
345 * Checks whether @ns is empty or not
346 *
347 * Returns %TRUE if @ns is an empty node-set
348 */
349#define xmlXPathNodeSetIsEmpty(ns) \
350 (((ns) == NULL) || ((ns)->nodeNr == 0) || ((ns)->nodeTab == NULL))
Daniel Veillard790142b2001-05-15 10:51:53 +0000351
352
Owen Taylor3473f882001-02-23 17:55:21 +0000353void xmlXPathFreeObject (xmlXPathObjectPtr obj);
Owen Taylor3473f882001-02-23 17:55:21 +0000354xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val);
355void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
356void xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
357xmlXPathObjectPtr xmlXPathObjectCopy (xmlXPathObjectPtr val);
358int xmlXPathCmpNodes (xmlNodePtr node1,
359 xmlNodePtr node2);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000360/**
361 * Conversion functions to basic types
362 */
Daniel Veillardba0b8c92001-05-15 09:43:47 +0000363int xmlXPathCastNumberToBoolean (double val);
364int xmlXPathCastStringToBoolean (const xmlChar * val);
Daniel Veillardbed7b052001-05-19 14:59:49 +0000365int xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns);
Daniel Veillardba0b8c92001-05-15 09:43:47 +0000366int xmlXPathCastToBoolean (xmlXPathObjectPtr val);
367
368double xmlXPathCastBooleanToNumber (int val);
369double xmlXPathCastStringToNumber (const xmlChar * val);
370double xmlXPathCastNodeToNumber (xmlNodePtr node);
371double xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns);
372double xmlXPathCastToNumber (xmlXPathObjectPtr val);
373
374xmlChar * xmlXPathCastBooleanToString (int val);
375xmlChar * xmlXPathCastNumberToString (double val);
376xmlChar * xmlXPathCastNodeToString (xmlNodePtr node);
377xmlChar * xmlXPathCastNodeSetToString (xmlNodeSetPtr ns);
378xmlChar * xmlXPathCastToString (xmlXPathObjectPtr val);
379
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000380xmlXPathObjectPtr xmlXPathConvertBoolean (xmlXPathObjectPtr val);
381xmlXPathObjectPtr xmlXPathConvertNumber (xmlXPathObjectPtr val);
382xmlXPathObjectPtr xmlXPathConvertString (xmlXPathObjectPtr val);
Owen Taylor3473f882001-02-23 17:55:21 +0000383
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000384/**
385 * Context handling
386 */
387void xmlXPathInit (void);
388xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
389void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
390
391/**
392 * Evaluation functions.
393 */
394xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000395 xmlXPathContextPtr ctx);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000396xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
397 xmlXPathContextPtr ctxt);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000398int xmlXPathEvalPredicate (xmlXPathContextPtr ctxt,
399 xmlXPathObjectPtr res);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000400/**
401 * Separate compilation/evaluation entry points
402 */
403xmlXPathCompExprPtr xmlXPathCompile (const xmlChar *str);
404xmlXPathObjectPtr xmlXPathCompiledEval (xmlXPathCompExprPtr comp,
405 xmlXPathContextPtr ctx);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000406void xmlXPathFreeCompExpr (xmlXPathCompExprPtr comp);
Owen Taylor3473f882001-02-23 17:55:21 +0000407#ifdef __cplusplus
408}
409#endif
410#endif /* ! __XML_XPATH_H__ */