2 * xpath.c: interface for XML Path Language implementation
4 * Reference: W3C Working Draft 5 July 1999
5 * http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html
7 * See COPYRIGHT for the status of this software
9 * Author: daniel@veillard.com
12 #ifndef __XML_XPATH_H__
13 #define __XML_XPATH_H__
15 #include <libxml/tree.h>
16 #include <libxml/hash.h>
22 typedef struct _xmlXPathContext xmlXPathContext;
23 typedef xmlXPathContext *xmlXPathContextPtr;
24 typedef struct _xmlXPathParserContext xmlXPathParserContext;
25 typedef xmlXPathParserContext *xmlXPathParserContextPtr;
28 * The set of XPath error codes.
32 XPATH_EXPRESSION_OK = 0,
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,
41 XPATH_UNKNOWN_FUNC_ERROR,
42 XPATH_INVALID_OPERAND,
45 XPATH_INVALID_CTXT_SIZE,
46 XPATH_INVALID_CTXT_POSITION,
50 XPTR_SUB_RESOURCE_ERROR,
51 XPATH_UNDEF_PREFIX_ERROR,
53 XPATH_INVALID_CHAR_ERROR
57 * A node-set (an unordered collection of nodes without duplicates).
59 typedef struct _xmlNodeSet xmlNodeSet;
60 typedef xmlNodeSet *xmlNodeSetPtr;
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 /* @@ with_ns to check wether namespace nodes should be looked at @@ */
69 * An expression is evaluated to yield an object, which
70 * has one of the following four basic types:
76 * @@ XPointer will add more types !
87 XPATH_LOCATIONSET = 7,
89 XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */
92 typedef struct _xmlXPathObject xmlXPathObject;
93 typedef xmlXPathObject *xmlXPathObjectPtr;
94 struct _xmlXPathObject {
95 xmlXPathObjectType type;
96 xmlNodeSetPtr nodesetval;
107 * xmlXPathConvertFunc:
108 * @obj: an XPath object
109 * @type: the number of the target type
111 * A conversion function is associated to a type and used to cast
112 * the new type to primitive values.
114 * Returns -1 in case of error, 0 otherwise
116 typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
119 * Extra type: a name and a conversion function.
122 typedef struct _xmlXPathType xmlXPathType;
123 typedef xmlXPathType *xmlXPathTypePtr;
124 struct _xmlXPathType {
125 const xmlChar *name; /* the type name */
126 xmlXPathConvertFunc func; /* the conversion function */
130 * Extra variable: a name and a value.
133 typedef struct _xmlXPathVariable xmlXPathVariable;
134 typedef xmlXPathVariable *xmlXPathVariablePtr;
135 struct _xmlXPathVariable {
136 const xmlChar *name; /* the variable name */
137 xmlXPathObjectPtr value; /* the value */
142 * @ctxt: an XPath parser context
143 * @nargs: the number of arguments passed to the function
145 * An XPath evaluation function, the parameters are on the XPath context stack.
148 typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt,
152 * Extra function: a name and a evaluation function.
155 typedef struct _xmlXPathFunct xmlXPathFunct;
156 typedef xmlXPathFunct *xmlXPathFuncPtr;
157 struct _xmlXPathFunct {
158 const xmlChar *name; /* the function name */
159 xmlXPathEvalFunc func; /* the evaluation function */
164 * @ctxt: the XPath interpreter context
165 * @cur: the previous node being explored on that axis
167 * 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.
171 * Returns the next node in that axis or NULL if at the end of the axis.
174 typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
175 xmlXPathObjectPtr cur);
178 * Extra axis: a name and an axis function.
181 typedef struct _xmlXPathAxis xmlXPathAxis;
182 typedef xmlXPathAxis *xmlXPathAxisPtr;
183 struct _xmlXPathAxis {
184 const xmlChar *name; /* the axis name */
185 xmlXPathAxisFunc func; /* the search function */
191 * 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.
202 struct _xmlXPathContext {
203 xmlDocPtr doc; /* The current document */
204 xmlNodePtr node; /* The current node */
206 int nb_variables_unused; /* unused (hash table) */
207 int max_variables_unused; /* unused (hash table) */
208 xmlHashTablePtr varHash; /* Hash table of defined variables */
210 int nb_types; /* number of defined types */
211 int max_types; /* max number of types */
212 xmlXPathTypePtr types; /* Array of defined types */
214 int nb_funcs_unused; /* unused (hash table) */
215 int max_funcs_unused; /* unused (hash table) */
216 xmlHashTablePtr funcHash; /* Hash table of defined funcs */
218 int nb_axis; /* number of defined axis */
219 int max_axis; /* max number of axis */
220 xmlXPathAxisPtr axis; /* Array of defined axis */
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 */
227 /* extra variables */
228 int contextSize; /* the context size */
229 int proximityPosition; /* the proximity position */
231 /* extra stuff for XPointer */
232 int xptr; /* it this an XPointer context */
233 xmlNodePtr here; /* for here() */
234 xmlNodePtr origin; /* for origin() */
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 */
241 /* Possibility to link in an extra item */
242 void *extra; /* needed for XSLT */
244 /* The function name and URI when calling a function */
245 const xmlChar *function;
246 const xmlChar *functionURI;
248 /* function lookup function and data */
249 void *funcLookupFunc; /* function lookup func */
250 void *funcLookupData; /* function lookup data */
252 /* temporary namespace lists kept for walking the namespace axis */
253 xmlNsPtr *tmpNsList; /* Array of namespaces */
254 int tmpNsNr; /* number of namespace in scope */
258 * The structure of a compiled expression form is not public.
261 typedef struct _xmlXPathCompExpr xmlXPathCompExpr;
262 typedef xmlXPathCompExpr *xmlXPathCompExprPtr;
265 * xmlXPathParserContext:
267 * An XPath parser context. It contains pure parsing informations,
268 * an xmlXPathContext, and the stack of objects.
270 struct _xmlXPathParserContext {
271 const xmlChar *cur; /* the current char being parsed */
272 const xmlChar *base; /* the full expression */
274 int error; /* error code */
276 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 */
282 xmlXPathCompExprPtr comp; /* the precompiled expression */
283 int xptr; /* it this an XPointer expression */
284 xmlNodePtr ancestor; /* used for walking preceding axis */
289 * @ctxt: the XPath interprestation context
290 * @nargs: the number of arguments
293 * The arguments (if any) are popped out from the context stack
294 * and the result is pushed on the stack.
297 typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
299 /************************************************************************
303 ************************************************************************/
306 * Objects and Nodesets handling
309 LIBXML_DLL_IMPORT extern double xmlXPathNAN;
310 LIBXML_DLL_IMPORT extern double xmlXPathPINF;
311 LIBXML_DLL_IMPORT extern double xmlXPathNINF;
313 int xmlXPathIsNaN (double val);
314 int xmlXPathIsInf (double val);
316 /* These macros may later turn into functions */
318 * xmlXPathNodeSetGetLength:
321 * Implement a functionality similar to the DOM NodeList.length.
323 * Returns the number of nodes in the node-set.
325 #define xmlXPathNodeSetGetLength(ns) ((ns) ? (ns)->nodeNr : 0)
327 * xmlXPathNodeSetItem:
329 * @index: index of a node in the set
331 * Implements a functionality similar to the DOM NodeList.item().
333 * Returns the xmlNodePtr at the given @index in @ns or NULL if
334 * @index is out of range (0 to length-1)
336 #define xmlXPathNodeSetItem(ns, index) \
337 ((((ns) != NULL) && \
338 ((index) >= 0) && ((index) < (ns)->nodeNr)) ? \
339 (ns)->nodeTab[(index)] \
342 * xmlXPathNodeSetIsEmpty:
345 * Checks whether @ns is empty or not.
347 * Returns %TRUE if @ns is an empty node-set.
349 #define xmlXPathNodeSetIsEmpty(ns) \
350 (((ns) == NULL) || ((ns)->nodeNr == 0) || ((ns)->nodeTab == NULL))
353 void xmlXPathFreeObject (xmlXPathObjectPtr obj);
354 xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val);
355 void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
356 void xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
357 xmlXPathObjectPtr xmlXPathObjectCopy (xmlXPathObjectPtr val);
358 int xmlXPathCmpNodes (xmlNodePtr node1,
361 * Conversion functions to basic types.
363 int xmlXPathCastNumberToBoolean (double val);
364 int xmlXPathCastStringToBoolean (const xmlChar * val);
365 int xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns);
366 int xmlXPathCastToBoolean (xmlXPathObjectPtr val);
368 double xmlXPathCastBooleanToNumber (int val);
369 double xmlXPathCastStringToNumber (const xmlChar * val);
370 double xmlXPathCastNodeToNumber (xmlNodePtr node);
371 double xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns);
372 double xmlXPathCastToNumber (xmlXPathObjectPtr val);
374 xmlChar * xmlXPathCastBooleanToString (int val);
375 xmlChar * xmlXPathCastNumberToString (double val);
376 xmlChar * xmlXPathCastNodeToString (xmlNodePtr node);
377 xmlChar * xmlXPathCastNodeSetToString (xmlNodeSetPtr ns);
378 xmlChar * xmlXPathCastToString (xmlXPathObjectPtr val);
380 xmlXPathObjectPtr xmlXPathConvertBoolean (xmlXPathObjectPtr val);
381 xmlXPathObjectPtr xmlXPathConvertNumber (xmlXPathObjectPtr val);
382 xmlXPathObjectPtr xmlXPathConvertString (xmlXPathObjectPtr val);
387 void xmlXPathInit (void);
388 xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
389 void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
392 * Evaluation functions.
394 long xmlXPathOrderDocElems (xmlDocPtr doc);
395 xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
396 xmlXPathContextPtr ctx);
397 xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
398 xmlXPathContextPtr ctxt);
399 int xmlXPathEvalPredicate (xmlXPathContextPtr ctxt,
400 xmlXPathObjectPtr res);
402 * Separate compilation/evaluation entry points.
404 xmlXPathCompExprPtr xmlXPathCompile (const xmlChar *str);
405 xmlXPathObjectPtr xmlXPathCompiledEval (xmlXPathCompExprPtr comp,
406 xmlXPathContextPtr ctx);
407 void xmlXPathFreeCompExpr (xmlXPathCompExprPtr comp);
411 #endif /* ! __XML_XPATH_H__ */