blob: 8479056e8f79177e9af7f07f7f46f1f7305c0b27 [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
Daniel Veillard5168dbf2001-07-07 00:18:23 +0000133/**
134 * xmlXPathEvalFunc:
135 * @ctxt: an XPath parser context
136 * @nargs: the number of arguments passed to the function
137 *
138 * an XPath evaluation function, the parameters are on thei XPath context stack
Owen Taylor3473f882001-02-23 17:55:21 +0000139 */
140
Daniel Veillard5168dbf2001-07-07 00:18:23 +0000141typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt,
142 int nargs);
Owen Taylor3473f882001-02-23 17:55:21 +0000143
144/*
145 * Extra function: a name and a evaluation function.
146 */
147
148typedef struct _xmlXPathFunct xmlXPathFunct;
149typedef xmlXPathFunct *xmlXPathFuncPtr;
150struct _xmlXPathFunct {
151 const xmlChar *name; /* the function name */
152 xmlXPathEvalFunc func; /* the evaluation function */
153};
154
155/*
156 * An axis traversal function. To traverse an axis, the engine calls
157 * the first time with cur == NULL and repeat until the function returns
158 * NULL indicating the end of the axis traversal.
159 */
160
161typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
162 xmlXPathObjectPtr cur);
163
164/*
165 * Extra axis: a name and an axis function.
166 */
167
168typedef struct _xmlXPathAxis xmlXPathAxis;
169typedef xmlXPathAxis *xmlXPathAxisPtr;
170struct _xmlXPathAxis {
171 const xmlChar *name; /* the axis name */
172 xmlXPathAxisFunc func; /* the search function */
173};
174
Daniel Veillardbed7b052001-05-19 14:59:49 +0000175/**
176 * xmlXPathContext:
177 *
Owen Taylor3473f882001-02-23 17:55:21 +0000178 * Expression evaluation occurs with respect to a context.
179 * he context consists of:
180 * - a node (the context node)
181 * - a node list (the context node list)
182 * - a set of variable bindings
183 * - a function library
184 * - the set of namespace declarations in scope for the expression
185 * Following the switch to hash tables, this need to be trimmed up at
186 * the next binary incompatible release.
187 */
188
189struct _xmlXPathContext {
190 xmlDocPtr doc; /* The current document */
191 xmlNodePtr node; /* The current node */
192
193 int nb_variables_unused; /* unused (hash table) */
194 int max_variables_unused; /* unused (hash table) */
195 xmlHashTablePtr varHash; /* Hash table of defined variables */
196
197 int nb_types; /* number of defined types */
198 int max_types; /* max number of types */
199 xmlXPathTypePtr types; /* Array of defined types */
200
201 int nb_funcs_unused; /* unused (hash table) */
202 int max_funcs_unused; /* unused (hash table) */
203 xmlHashTablePtr funcHash; /* Hash table of defined funcs */
204
205 int nb_axis; /* number of defined axis */
206 int max_axis; /* max number of axis */
207 xmlXPathAxisPtr axis; /* Array of defined axis */
208
209 /* the namespace nodes of the context node */
210 xmlNsPtr *namespaces; /* Array of namespaces */
211 int nsNr; /* number of namespace in scope */
212 void *user; /* function to free */
213
214 /* extra variables */
215 int contextSize; /* the context size */
216 int proximityPosition; /* the proximity position */
217
218 /* extra stuff for XPointer */
219 int xptr; /* it this an XPointer context */
220 xmlNodePtr here; /* for here() */
221 xmlNodePtr origin; /* for origin() */
222
223 /* the set of namespace declarations in scope for the expression */
224 xmlHashTablePtr nsHash; /* The namespaces hash table */
225 void *varLookupFunc; /* variable lookup func */
226 void *varLookupData; /* variable lookup data */
227
228 /* Possibility to link in an extra item */
229 void *extra; /* needed for XSLT */
Daniel Veillard42596ad2001-05-22 16:57:14 +0000230
231 /* The function name and URI when calling a function */
232 const xmlChar *function;
233 const xmlChar *functionURI;
Thomas Broyerba4ad322001-07-26 16:55:21 +0000234
235 /* function lookup function and data */
236 void *funcLookupFunc; /* function lookup func */
237 void *funcLookupData; /* function lookup data */
Daniel Veillard7d7e3792001-07-30 13:42:13 +0000238
239 /* temporary namespace lists kept for walking the namespace axis */
240 xmlNsPtr *tmpNsList; /* Array of namespaces */
241 int tmpNsNr; /* number of namespace in scope */
Owen Taylor3473f882001-02-23 17:55:21 +0000242};
243
244/*
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000245 * The structure of a compiled expression form is not public
246 */
247
248typedef struct _xmlXPathCompExpr xmlXPathCompExpr;
249typedef xmlXPathCompExpr *xmlXPathCompExprPtr;
250
Daniel Veillardbed7b052001-05-19 14:59:49 +0000251/**
252 * xmlXPathParserContext:
253 *
Owen Taylor3473f882001-02-23 17:55:21 +0000254 * An XPath parser context, it contains pure parsing informations,
255 * an xmlXPathContext, and the stack of objects.
256 */
257struct _xmlXPathParserContext {
258 const xmlChar *cur; /* the current char being parsed */
259 const xmlChar *base; /* the full expression */
260
261 int error; /* error code */
262
Owen Taylor3473f882001-02-23 17:55:21 +0000263 xmlXPathContextPtr context; /* the evaluation context */
264 xmlXPathObjectPtr value; /* the current value */
265 int valueNr; /* number of values stacked */
266 int valueMax; /* max number of values stacked */
267 xmlXPathObjectPtr *valueTab; /* stack of values */
Daniel Veillardd007d6c2001-03-19 00:01:07 +0000268
269 xmlXPathCompExprPtr comp; /* the precompiled expression */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000270 int xptr; /* it this an XPointer expression */
Daniel Veillardf06307e2001-07-03 10:35:50 +0000271 xmlNodePtr ancestor; /* used for walking preceding axis */
Owen Taylor3473f882001-02-23 17:55:21 +0000272};
273
Daniel Veillardbed7b052001-05-19 14:59:49 +0000274/**
275 * xmlXPathFunction:
276 *
Owen Taylor3473f882001-02-23 17:55:21 +0000277 * An XPath function
278 * The arguments (if any) are popped out of the context stack
279 * and the result is pushed on the stack.
280 */
281
282typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
283
284/************************************************************************
285 * *
286 * Public API *
287 * *
288 ************************************************************************/
289
290/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000291 * Objects and Nodesets handling
Owen Taylor3473f882001-02-23 17:55:21 +0000292 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000293
Thomas Broyer496be682001-07-15 22:59:18 +0000294LIBXML_DLL_IMPORT extern double xmlXPathNAN;
295LIBXML_DLL_IMPORT extern double xmlXPathPINF;
296LIBXML_DLL_IMPORT extern double xmlXPathNINF;
297
Daniel Veillardcda96922001-08-21 10:56:31 +0000298int xmlXPathIsNaN (double val);
299int xmlXPathIsInf (double val);
300
Daniel Veillard790142b2001-05-15 10:51:53 +0000301/* These macros may later turn into functions */
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000302/**
303 * xmlXPathNodeSetGetLength:
304 * @ns: a node-set
305 *
306 * Implement a functionnality similar to the DOM NodeList.length
307 *
308 * Returns the number of nodes in the node-set.
309 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000310#define xmlXPathNodeSetGetLength(ns) ((ns) ? (ns)->nodeNr : 0)
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000311/**
312 * xmlXPathNodeSetItem:
313 * @ns: a node-set
314 * @index: index of a node in the set
315 *
316 * Implements a functionnality similar to the DOM NodeList.item()
317 *
318 * Returns the xmlNodePtr at the given @index in @ns or NULL if
319 * @index is out of range (0 to length-1)
320 */
Daniel Veillard790142b2001-05-15 10:51:53 +0000321#define xmlXPathNodeSetItem(ns, index) \
322 ((((ns) != NULL) && \
Thomas Broyer496be682001-07-15 22:59:18 +0000323 ((index) >= 0) && ((index) < (ns)->nodeNr)) ? \
Daniel Veillard790142b2001-05-15 10:51:53 +0000324 (ns)->nodeTab[(index)] \
325 : NULL)
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000326/**
327 * xmlXPathNodeSetIsEmpty:
328 * @ns: a node-set
329 *
330 * Checks whether @ns is empty or not
331 *
332 * Returns %TRUE if @ns is an empty node-set
333 */
334#define xmlXPathNodeSetIsEmpty(ns) \
335 (((ns) == NULL) || ((ns)->nodeNr == 0) || ((ns)->nodeTab == NULL))
Daniel Veillard790142b2001-05-15 10:51:53 +0000336
337
Owen Taylor3473f882001-02-23 17:55:21 +0000338void xmlXPathFreeObject (xmlXPathObjectPtr obj);
Owen Taylor3473f882001-02-23 17:55:21 +0000339xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val);
340void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
341void xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
342xmlXPathObjectPtr xmlXPathObjectCopy (xmlXPathObjectPtr val);
343int xmlXPathCmpNodes (xmlNodePtr node1,
344 xmlNodePtr node2);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000345/**
346 * Conversion functions to basic types
347 */
Daniel Veillardba0b8c92001-05-15 09:43:47 +0000348int xmlXPathCastNumberToBoolean (double val);
349int xmlXPathCastStringToBoolean (const xmlChar * val);
Daniel Veillardbed7b052001-05-19 14:59:49 +0000350int xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns);
Daniel Veillardba0b8c92001-05-15 09:43:47 +0000351int xmlXPathCastToBoolean (xmlXPathObjectPtr val);
352
353double xmlXPathCastBooleanToNumber (int val);
354double xmlXPathCastStringToNumber (const xmlChar * val);
355double xmlXPathCastNodeToNumber (xmlNodePtr node);
356double xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns);
357double xmlXPathCastToNumber (xmlXPathObjectPtr val);
358
359xmlChar * xmlXPathCastBooleanToString (int val);
360xmlChar * xmlXPathCastNumberToString (double val);
361xmlChar * xmlXPathCastNodeToString (xmlNodePtr node);
362xmlChar * xmlXPathCastNodeSetToString (xmlNodeSetPtr ns);
363xmlChar * xmlXPathCastToString (xmlXPathObjectPtr val);
364
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000365xmlXPathObjectPtr xmlXPathConvertBoolean (xmlXPathObjectPtr val);
366xmlXPathObjectPtr xmlXPathConvertNumber (xmlXPathObjectPtr val);
367xmlXPathObjectPtr xmlXPathConvertString (xmlXPathObjectPtr val);
Owen Taylor3473f882001-02-23 17:55:21 +0000368
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000369/**
370 * Context handling
371 */
372void xmlXPathInit (void);
373xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
374void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
375
376/**
377 * Evaluation functions.
378 */
379xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
380 xmlXPathContextPtr ctxt);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000381xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
382 xmlXPathContextPtr ctxt);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000383int xmlXPathEvalPredicate (xmlXPathContextPtr ctxt,
384 xmlXPathObjectPtr res);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000385/**
386 * Separate compilation/evaluation entry points
387 */
388xmlXPathCompExprPtr xmlXPathCompile (const xmlChar *str);
389xmlXPathObjectPtr xmlXPathCompiledEval (xmlXPathCompExprPtr comp,
390 xmlXPathContextPtr ctx);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +0000391void xmlXPathFreeCompExpr (xmlXPathCompExprPtr comp);
Owen Taylor3473f882001-02-23 17:55:21 +0000392#ifdef __cplusplus
393}
394#endif
395#endif /* ! __XML_XPATH_H__ */