Debian Codegraph @

include/libxml/xpath.h

raw
1 /**
2 * @file
3 *
4 * @brief XML Path Language implementation
5 *
6 * API for the XML Path Language implementation
7 *
8 * XML Path Language implementation
9 * XPath is a language for addressing parts of an XML document,
10 * designed to be used by both XSLT and XPointer
11 * http://www.w3.org/TR/xpath
12 *
13 * Implements
14 * W3C Recommendation 16 November 1999
15 * http://www.w3.org/TR/1999/REC-xpath-19991116
16 *
17 * @copyright See Copyright for the status of this software.
18 *
19 * @author Daniel Veillard
20 */
21
22 #ifndef __XML_XPATH_H__
23 #define __XML_XPATH_H__
24
25 #include <libxml/xmlversion.h>
26
27 #ifdef LIBXML_XPATH_ENABLED
28
29 #include <libxml/xmlerror.h>
30 #include <libxml/tree.h>
31 #include <libxml/hash.h>
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /** XPath context */
38 typedef struct _xmlXPathContext xmlXPathContext;
39 typedef xmlXPathContext *xmlXPathContextPtr;
40 /** XPath parser and evaluation context */
41 typedef struct _xmlXPathParserContext xmlXPathParserContext;
42 typedef xmlXPathParserContext *xmlXPathParserContextPtr;
43
44 /**
45 * The set of XPath error codes.
46 */
47
48 typedef enum {
49 XPATH_EXPRESSION_OK = 0,
50 XPATH_NUMBER_ERROR,
51 XPATH_UNFINISHED_LITERAL_ERROR,
52 XPATH_START_LITERAL_ERROR,
53 XPATH_VARIABLE_REF_ERROR,
54 XPATH_UNDEF_VARIABLE_ERROR,
55 XPATH_INVALID_PREDICATE_ERROR,
56 XPATH_EXPR_ERROR,
57 XPATH_UNCLOSED_ERROR,
58 XPATH_UNKNOWN_FUNC_ERROR,
59 XPATH_INVALID_OPERAND,
60 XPATH_INVALID_TYPE,
61 XPATH_INVALID_ARITY,
62 XPATH_INVALID_CTXT_SIZE,
63 XPATH_INVALID_CTXT_POSITION,
64 XPATH_MEMORY_ERROR,
65 XPTR_SYNTAX_ERROR,
66 XPTR_RESOURCE_ERROR,
67 XPTR_SUB_RESOURCE_ERROR,
68 XPATH_UNDEF_PREFIX_ERROR,
69 XPATH_ENCODING_ERROR,
70 XPATH_INVALID_CHAR_ERROR,
71 XPATH_INVALID_CTXT,
72 XPATH_STACK_ERROR,
73 XPATH_FORBID_VARIABLE_ERROR,
74 XPATH_OP_LIMIT_EXCEEDED,
75 XPATH_RECURSION_LIMIT_EXCEEDED
76 } xmlXPathError;
77
78 /** XPath node set */
79 typedef struct _xmlNodeSet xmlNodeSet;
80 typedef xmlNodeSet *xmlNodeSetPtr;
81 /**
82 * A node-set (an unordered collection of nodes without duplicates).
83 */
84 struct _xmlNodeSet {
85 /** number of nodes in the set */
86 int nodeNr;
87 /** size of the array as allocated */
88 int nodeMax;
89 /** array of nodes in no particular order */
90 xmlNode **nodeTab;
91 };
92
93 /**
94 * An expression is evaluated to yield an object, which
95 * has one of the following four basic types:
96 *
97 * - node-set
98 * - boolean
99 * - number
100 * - string
101 */
102 typedef enum {
103 XPATH_UNDEFINED = 0,
104 XPATH_NODESET = 1,
105 XPATH_BOOLEAN = 2,
106 XPATH_NUMBER = 3,
107 XPATH_STRING = 4,
108 XPATH_USERS = 8,
109 XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */
110 } xmlXPathObjectType;
111
112 /** @cond IGNORE */
113 #define XPATH_POINT 5
114 #define XPATH_RANGE 6
115 #define XPATH_LOCATIONSET 7
116 /** @endcond */
117
118 /** XPath object */
119 typedef struct _xmlXPathObject xmlXPathObject;
120 typedef xmlXPathObject *xmlXPathObjectPtr;
121 /**
122 * An XPath object
123 */
124 struct _xmlXPathObject {
125 /** object type */
126 xmlXPathObjectType type;
127 /** node set */
128 xmlNodeSet *nodesetval;
129 /** boolean */
130 int boolval;
131 /** number */
132 double floatval;
133 /** string */
134 xmlChar *stringval;
135 void *user;
136 int index;
137 void *user2;
138 int index2;
139 };
140
141 /** @cond ignore */
142
143 /*
144 * unused
145 */
146 typedef int (*xmlXPathConvertFunc) (xmlXPathObject *obj, int type);
147 typedef struct _xmlXPathType xmlXPathType;
148 typedef xmlXPathType *xmlXPathTypePtr;
149 struct _xmlXPathType {
150 const xmlChar *name; /* the type name */
151 xmlXPathConvertFunc func; /* the conversion function */
152 };
153
154 /*
155 * unused
156 */
157 typedef struct _xmlXPathVariable xmlXPathVariable;
158 typedef xmlXPathVariable *xmlXPathVariablePtr;
159 struct _xmlXPathVariable {
160 const xmlChar *name; /* the variable name */
161 xmlXPathObject *value; /* the value */
162 };
163
164 /*
165 * unused
166 */
167 typedef void (*xmlXPathEvalFunc)(xmlXPathParserContext *ctxt,
168 int nargs);
169 typedef struct _xmlXPathFunct xmlXPathFunct;
170 typedef xmlXPathFunct *xmlXPathFuncPtr;
171 struct _xmlXPathFunct {
172 const xmlChar *name; /* the function name */
173 xmlXPathEvalFunc func; /* the evaluation function */
174 };
175
176 /*
177 * unused
178 */
179 typedef xmlXPathObject *(*xmlXPathAxisFunc) (xmlXPathParserContext *ctxt,
180 xmlXPathObject *cur);
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 */
186 };
187
188 /** @endcond */
189
190 /**
191 * An XPath function.
192 * The arguments (if any) are popped out from the context stack
193 * and the result is pushed on the stack.
194 *
195 * @param ctxt the XPath interprestation context
196 * @param nargs the number of arguments
197 */
198
199 typedef void (*xmlXPathFunction) (xmlXPathParserContext *ctxt, int nargs);
200
201 /*
202 * Function and Variable Lookup.
203 */
204
205 /**
206 * Prototype for callbacks used to plug variable lookup in the XPath
207 * engine.
208 *
209 * @param ctxt an XPath context
210 * @param name name of the variable
211 * @param ns_uri the namespace name hosting this variable
212 * @returns the XPath object value or NULL if not found.
213 */
214 typedef xmlXPathObject *(*xmlXPathVariableLookupFunc) (void *ctxt,
215 const xmlChar *name,
216 const xmlChar *ns_uri);
217
218 /**
219 * Prototype for callbacks used to plug function lookup in the XPath
220 * engine.
221 *
222 * @param ctxt an XPath context
223 * @param name name of the function
224 * @param ns_uri the namespace name hosting this function
225 * @returns the XPath function or NULL if not found.
226 */
227 typedef xmlXPathFunction (*xmlXPathFuncLookupFunc) (void *ctxt,
228 const xmlChar *name,
229 const xmlChar *ns_uri);
230
231 /**
232 * Flags for XPath engine compilation and runtime
233 */
234 /**
235 * check namespaces at compilation
236 */
237 #define XML_XPATH_CHECKNS (1<<0)
238 /**
239 * forbid variables in expression
240 */
241 #define XML_XPATH_NOVAR (1<<1)
242
243 /**
244 * Expression evaluation occurs with respect to a context.
245 * he context consists of:
246 * - a node (the context node)
247 * - a node list (the context node list)
248 * - a set of variable bindings
249 * - a function library
250 * - the set of namespace declarations in scope for the expression
251 * Following the switch to hash tables, this need to be trimmed up at
252 * the next binary incompatible release.
253 * The node may be modified when the context is passed to libxml2
254 * for an XPath evaluation so you may need to initialize it again
255 * before the next call.
256 */
257 struct _xmlXPathContext {
258 /** The current document */
259 xmlDoc *doc;
260 /** The current node */
261 xmlNode *node;
262
263 /* unused (hash table) */
264 int nb_variables_unused;
265 /* unused (hash table) */
266 int max_variables_unused;
267 /* Hash table of defined variables */
268 xmlHashTable *varHash;
269
270 /* number of defined types */
271 int nb_types;
272 /* max number of types */
273 int max_types;
274 /* Array of defined types */
275 xmlXPathType *types;
276
277 /* unused (hash table) */
278 int nb_funcs_unused;
279 /* unused (hash table) */
280 int max_funcs_unused;
281 /* Hash table of defined funcs */
282 xmlHashTable *funcHash;
283
284 /* number of defined axis */
285 int nb_axis;
286 /* max number of axis */
287 int max_axis;
288 /* Array of defined axis */
289 xmlXPathAxis *axis;
290
291 /* Array of namespaces */
292 xmlNs **namespaces;
293 /* number of namespace in scope */
294 int nsNr;
295 /* function to free */
296 void *user;
297
298 /** the context size */
299 int contextSize;
300 /** the proximity position */
301 int proximityPosition;
302
303 /* is this an XPointer context? */
304 int xptr;
305 /* for here() */
306 xmlNode *here;
307 /* for origin() */
308 xmlNode *origin;
309
310 /* The namespaces hash table */
311 xmlHashTable *nsHash;
312 /* variable lookup func */
313 xmlXPathVariableLookupFunc varLookupFunc;
314 /* variable lookup data */
315 void *varLookupData;
316
317 /* needed for XSLT */
318 void *extra;
319
320 /* The function name when calling a function */
321 const xmlChar *function;
322 /* The namespace URI when calling a function */
323 const xmlChar *functionURI;
324
325 /* function lookup func */
326 xmlXPathFuncLookupFunc funcLookupFunc;
327 /* function lookup data */
328 void *funcLookupData;
329
330 /* Array of temp namespaces */
331 xmlNs **tmpNsList;
332 /* number of namespaces in scope */
333 int tmpNsNr;
334
335 /* user specific data block */
336 void *userData;
337 /* the callback in case of errors */
338 xmlStructuredErrorFunc error;
339 /* the last error */
340 xmlError lastError;
341 /* the source node XSLT */
342 xmlNode *debugNode;
343
344 /* dictionary if any */
345 xmlDict *dict;
346
347 /** flags to control compilation */
348 int flags;
349
350 /* Cache for reusal of XPath objects */
351 void *cache;
352
353 /* Resource limits */
354 unsigned long opLimit;
355 unsigned long opCount;
356 int depth;
357 };
358
359 /** Compiled XPath expression */
360 typedef struct _xmlXPathCompExpr xmlXPathCompExpr;
361 typedef xmlXPathCompExpr *xmlXPathCompExprPtr;
362
363 /**
364 * An XPath parser context. It contains pure parsing information,
365 * an xmlXPathContext, and the stack of objects.
366 *
367 * This struct is used for evaluation as well and misnamed.
368 */
369 struct _xmlXPathParserContext {
370 /* the current char being parsed */
371 const xmlChar *cur;
372 /* the full expression */
373 const xmlChar *base;
374
375 /** error code */
376 int error;
377
378 /** the evaluation context */
379 xmlXPathContext *context;
380 /** the current value */
381 xmlXPathObject *value;
382 /* number of values stacked */
383 int valueNr;
384 /* max number of values stacked */
385 int valueMax;
386 /* stack of values */
387 xmlXPathObject **valueTab;
388
389 /* the precompiled expression */
390 xmlXPathCompExpr *comp;
391 /* it this an XPointer expression */
392 int xptr;
393 /* used for walking preceding axis */
394 xmlNode *ancestor;
395
396 /* always zero for compatibility */
397 int valueFrame;
398 };
399
400 /************************************************************************
401 * *
402 * Public API *
403 * *
404 ************************************************************************/
405
406 /**
407 * Objects and Nodesets handling
408 */
409
410 /** @cond ignore */
411
412 XML_DEPRECATED
413 XMLPUBVAR double xmlXPathNAN;
414 XML_DEPRECATED
415 XMLPUBVAR double xmlXPathPINF;
416 XML_DEPRECATED
417 XMLPUBVAR double xmlXPathNINF;
418
419 /* These macros may later turn into functions */
420 /**
421 * Implement a functionality similar to the DOM NodeList.length.
422 *
423 * @param ns a node-set
424 * @returns the number of nodes in the node-set.
425 */
426 #define xmlXPathNodeSetGetLength(ns) ((ns) ? (ns)->nodeNr : 0)
427 /**
428 * Implements a functionality similar to the DOM NodeList.item().
429 *
430 * @param ns a node-set
431 * @param index index of a node in the set
432 * @returns the xmlNode at the given `index` in `ns` or NULL if
433 * `index` is out of range (0 to length-1)
434 */
435 #define xmlXPathNodeSetItem(ns, index) \
436 ((((ns) != NULL) && \
437 ((index) >= 0) && ((index) < (ns)->nodeNr)) ? \
438 (ns)->nodeTab[(index)] \
439 : NULL)
440 /**
441 * Checks whether `ns` is empty or not.
442 *
443 * @param ns a node-set
444 * @returns %TRUE if `ns` is an empty node-set.
445 */
446 #define xmlXPathNodeSetIsEmpty(ns) \
447 (((ns) == NULL) || ((ns)->nodeNr == 0) || ((ns)->nodeTab == NULL))
448
449 /** @endcond */
450
451 XMLPUBFUN void
452 xmlXPathFreeObject (xmlXPathObject *obj);
453 XMLPUBFUN xmlNodeSet *
454 xmlXPathNodeSetCreate (xmlNode *val);
455 XMLPUBFUN void
456 xmlXPathFreeNodeSetList (xmlXPathObject *obj);
457 XMLPUBFUN void
458 xmlXPathFreeNodeSet (xmlNodeSet *obj);
459 XMLPUBFUN xmlXPathObject *
460 xmlXPathObjectCopy (xmlXPathObject *val);
461 XMLPUBFUN int
462 xmlXPathCmpNodes (xmlNode *node1,
463 xmlNode *node2);
464 /**
465 * Conversion functions to basic types.
466 */
467 XMLPUBFUN int
468 xmlXPathCastNumberToBoolean (double val);
469 XMLPUBFUN int
470 xmlXPathCastStringToBoolean (const xmlChar * val);
471 XMLPUBFUN int
472 xmlXPathCastNodeSetToBoolean(xmlNodeSet *ns);
473 XMLPUBFUN int
474 xmlXPathCastToBoolean (xmlXPathObject *val);
475
476 XMLPUBFUN double
477 xmlXPathCastBooleanToNumber (int val);
478 XMLPUBFUN double
479 xmlXPathCastStringToNumber (const xmlChar * val);
480 XMLPUBFUN double
481 xmlXPathCastNodeToNumber (xmlNode *node);
482 XMLPUBFUN double
483 xmlXPathCastNodeSetToNumber (xmlNodeSet *ns);
484 XMLPUBFUN double
485 xmlXPathCastToNumber (xmlXPathObject *val);
486
487 XMLPUBFUN xmlChar *
488 xmlXPathCastBooleanToString (int val);
489 XMLPUBFUN xmlChar *
490 xmlXPathCastNumberToString (double val);
491 XMLPUBFUN xmlChar *
492 xmlXPathCastNodeToString (xmlNode *node);
493 XMLPUBFUN xmlChar *
494 xmlXPathCastNodeSetToString (xmlNodeSet *ns);
495 XMLPUBFUN xmlChar *
496 xmlXPathCastToString (xmlXPathObject *val);
497
498 XMLPUBFUN xmlXPathObject *
499 xmlXPathConvertBoolean (xmlXPathObject *val);
500 XMLPUBFUN xmlXPathObject *
501 xmlXPathConvertNumber (xmlXPathObject *val);
502 XMLPUBFUN xmlXPathObject *
503 xmlXPathConvertString (xmlXPathObject *val);
504
505 /**
506 * Context handling.
507 */
508 XMLPUBFUN xmlXPathContext *
509 xmlXPathNewContext (xmlDoc *doc);
510 XMLPUBFUN void
511 xmlXPathFreeContext (xmlXPathContext *ctxt);
512 XMLPUBFUN void
513 xmlXPathSetErrorHandler(xmlXPathContext *ctxt,
514 xmlStructuredErrorFunc handler,
515 void *context);
516 XMLPUBFUN int
517 xmlXPathContextSetCache(xmlXPathContext *ctxt,
518 int active,
519 int value,
520 int options);
521 /**
522 * Evaluation functions.
523 */
524 XMLPUBFUN long
525 xmlXPathOrderDocElems (xmlDoc *doc);
526 XMLPUBFUN int
527 xmlXPathSetContextNode (xmlNode *node,
528 xmlXPathContext *ctx);
529 XMLPUBFUN xmlXPathObject *
530 xmlXPathNodeEval (xmlNode *node,
531 const xmlChar *str,
532 xmlXPathContext *ctx);
533 XMLPUBFUN xmlXPathObject *
534 xmlXPathEval (const xmlChar *str,
535 xmlXPathContext *ctx);
536 XMLPUBFUN xmlXPathObject *
537 xmlXPathEvalExpression (const xmlChar *str,
538 xmlXPathContext *ctxt);
539 XMLPUBFUN int
540 xmlXPathEvalPredicate (xmlXPathContext *ctxt,
541 xmlXPathObject *res);
542 /**
543 * Separate compilation/evaluation entry points.
544 */
545 XMLPUBFUN xmlXPathCompExpr *
546 xmlXPathCompile (const xmlChar *str);
547 XMLPUBFUN xmlXPathCompExpr *
548 xmlXPathCtxtCompile (xmlXPathContext *ctxt,
549 const xmlChar *str);
550 XMLPUBFUN xmlXPathObject *
551 xmlXPathCompiledEval (xmlXPathCompExpr *comp,
552 xmlXPathContext *ctx);
553 XMLPUBFUN int
554 xmlXPathCompiledEvalToBoolean(xmlXPathCompExpr *comp,
555 xmlXPathContext *ctxt);
556 XMLPUBFUN void
557 xmlXPathFreeCompExpr (xmlXPathCompExpr *comp);
558
559 XML_DEPRECATED
560 XMLPUBFUN void
561 xmlXPathInit (void);
562 XMLPUBFUN int
563 xmlXPathIsNaN (double val);
564 XMLPUBFUN int
565 xmlXPathIsInf (double val);
566
567 #ifdef __cplusplus
568 }
569 #endif
570
571 #endif /* LIBXML_XPATH_ENABLED */
572 #endif /* ! __XML_XPATH_H__ */