Annotation of XML/xpath.h, revision 1.23
1.1 daniel 1: /*
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: *
9: * Author: Daniel.Veillard@w3.org
10: */
11:
12: #ifndef __XML_XPATH_H__
13: #define __XML_XPATH_H__
14:
1.16 daniel 15: #include <libxml/tree.h>
1.12 daniel 16:
1.11 daniel 17: #ifdef __cplusplus
1.13 daniel 18: extern "C" {
1.11 daniel 19: #endif
1.1 daniel 20:
1.15 daniel 21: typedef struct _xmlXPathContext xmlXPathContext;
22: typedef xmlXPathContext *xmlXPathContextPtr;
23: typedef struct _xmlXPathParserContext xmlXPathParserContext;
24: typedef xmlXPathParserContext *xmlXPathParserContextPtr;
1.9 daniel 25:
1.23 ! veillard 26: /**
! 27: * The set of XPath error codes
! 28: */
! 29:
! 30: typedef enum {
! 31: XPATH_EXPRESSION_OK = 0,
! 32: XPATH_NUMBER_ERROR,
! 33: XPATH_UNFINISHED_LITERAL_ERROR,
! 34: XPATH_START_LITERAL_ERROR,
! 35: XPATH_VARIABLE_REF_ERROR,
! 36: XPATH_UNDEF_VARIABLE_ERROR,
! 37: XPATH_INVALID_PREDICATE_ERROR,
! 38: XPATH_EXPR_ERROR,
! 39: XPATH_UNCLOSED_ERROR,
! 40: XPATH_UNKNOWN_FUNC_ERROR,
! 41: XPATH_INVALID_OPERAND,
! 42: XPATH_INVALID_TYPE,
! 43: XPATH_INVALID_ARITY,
! 44: XPATH_INVALID_CTXT_SIZE,
! 45: XPATH_INVALID_CTXT_POSITION
! 46: } xmlXPathError;
! 47:
1.1 daniel 48: /*
49: * A node-set (an unordered collection of nodes without duplicates)
50: */
1.15 daniel 51: typedef struct _xmlNodeSet xmlNodeSet;
52: typedef xmlNodeSet *xmlNodeSetPtr;
53: struct _xmlNodeSet {
1.17 daniel 54: int nodeNr; /* number of nodes in the set */
55: int nodeMax; /* size of the array as allocated */
1.1 daniel 56: xmlNodePtr *nodeTab; /* array of nodes in no particular order */
1.15 daniel 57: };
1.1 daniel 58:
59: /*
60: * An expression is evaluated to yield an object, which
61: * has one of the following four basic types:
62: * - node-set
63: * - boolean
64: * - number
65: * - string
1.17 daniel 66: *
67: * @@ XPointer will add more types !
1.1 daniel 68: */
69:
1.18 veillard 70: typedef enum {
71: XPATH_UNDEFINED = 0,
72: XPATH_NODESET = 1,
73: XPATH_BOOLEAN = 2,
74: XPATH_NUMBER = 3,
75: XPATH_STRING = 4,
1.20 veillard 76: XPATH_POINT = 5,
77: XPATH_RANGE = 6,
1.21 veillard 78: XPATH_LOCATIONSET = 7,
1.20 veillard 79: XPATH_USERS = 8
1.18 veillard 80: } xmlXPathObjectType;
1.1 daniel 81:
1.15 daniel 82: typedef struct _xmlXPathObject xmlXPathObject;
83: typedef xmlXPathObject *xmlXPathObjectPtr;
84: struct _xmlXPathObject {
1.18 veillard 85: xmlXPathObjectType type;
1.1 daniel 86: xmlNodeSetPtr nodesetval;
87: int boolval;
1.6 daniel 88: double floatval;
1.10 daniel 89: xmlChar *stringval;
1.9 daniel 90: void *user;
1.20 veillard 91: int index;
92: void *user2;
93: int index2;
1.15 daniel 94: };
1.1 daniel 95:
1.9 daniel 96: /*
97: * A conversion function is associated to a type and used to cast
98: * the new type to primitive values.
99: */
100: typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
101:
102: /*
103: * Extra type: a name and a conversion function.
104: */
105:
1.15 daniel 106: typedef struct _xmlXPathType xmlXPathType;
107: typedef xmlXPathType *xmlXPathTypePtr;
108: struct _xmlXPathType {
1.10 daniel 109: const xmlChar *name; /* the type name */
1.9 daniel 110: xmlXPathConvertFunc func; /* the conversion function */
1.15 daniel 111: };
1.9 daniel 112:
113: /*
114: * Extra variable: a name and a value.
115: */
116:
1.15 daniel 117: typedef struct _xmlXPathVariable xmlXPathVariable;
118: typedef xmlXPathVariable *xmlXPathVariablePtr;
119: struct _xmlXPathVariable {
1.10 daniel 120: const xmlChar *name; /* the variable name */
1.9 daniel 121: xmlXPathObjectPtr value; /* the value */
1.15 daniel 122: };
1.9 daniel 123:
124: /*
125: * an evaluation function, the parameters are on the context stack
126: */
127:
128: typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs);
129:
130: /*
131: * Extra function: a name and a evaluation function.
132: */
133:
1.15 daniel 134: typedef struct _xmlXPathFunct xmlXPathFunct;
135: typedef xmlXPathFunct *xmlXPathFuncPtr;
136: struct _xmlXPathFunct {
1.10 daniel 137: const xmlChar *name; /* the function name */
1.9 daniel 138: xmlXPathEvalFunc func; /* the evaluation function */
1.15 daniel 139: };
1.9 daniel 140:
141: /*
142: * An axis traversal function. To traverse an axis, the engine calls
143: * the first time with cur == NULL and repeat until the function returns
144: * NULL indicating the end of the axis traversal.
145: */
146:
147: typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
148: xmlXPathObjectPtr cur);
149:
150: /*
151: * Extra axis: a name and an axis function.
152: */
153:
1.15 daniel 154: typedef struct _xmlXPathAxis xmlXPathAxis;
155: typedef xmlXPathAxis *xmlXPathAxisPtr;
156: struct _xmlXPathAxis {
1.10 daniel 157: const xmlChar *name; /* the axis name */
1.9 daniel 158: xmlXPathAxisFunc func; /* the search function */
1.15 daniel 159: };
1.9 daniel 160:
1.1 daniel 161: /*
162: * Expression evaluation occurs with respect to a context.
163: * he context consists of:
164: * - a node (the context node)
165: * - a node list (the context node list)
166: * - a set of variable bindings
167: * - a function library
168: * - the set of namespace declarations in scope for the expression
169: */
170:
1.15 daniel 171: struct _xmlXPathContext {
1.2 daniel 172: xmlDocPtr doc; /* The current document */
173: xmlNodePtr node; /* The current node */
1.9 daniel 174:
175: int nb_variables; /* number of defined variables */
176: int max_variables; /* max number of variables */
177: xmlXPathVariablePtr *variables; /* Array of defined variables */
178:
179: int nb_types; /* number of defined types */
180: int max_types; /* max number of types */
181: xmlXPathTypePtr *types; /* Array of defined types */
182:
183: int nb_funcs; /* number of defined funcs */
184: int max_funcs; /* max number of funcs */
185: xmlXPathFuncPtr *funcs; /* Array of defined funcs */
186:
187: int nb_axis; /* number of defined axis */
188: int max_axis; /* max number of axis */
189: xmlXPathAxisPtr *axis; /* Array of defined axis */
190:
191: /* Namespace traversal should be implemented with user */
1.8 daniel 192: xmlNsPtr *namespaces; /* The namespaces lookup */
193: int nsNr; /* the current Namespace index */
1.9 daniel 194: void *user; /* user defined extra info */
1.18 veillard 195:
196: /* extra variables */
197: int contextSize; /* the context size */
198: int proximityPosition; /* the proximity position */
1.21 veillard 199:
200: /* extra stuff for XPointer */
201: int xptr; /* it this an XPointer context */
202: xmlNodePtr here; /* for here() */
203: xmlNodePtr origin; /* for origin() */
1.15 daniel 204: };
1.1 daniel 205:
206: /*
207: * An XPath parser context, it contains pure parsing informations,
208: * an xmlXPathContext, and the stack of objects.
209: */
1.15 daniel 210: struct _xmlXPathParserContext {
1.10 daniel 211: const xmlChar *cur; /* the current char being parsed */
212: const xmlChar *base; /* the full expression */
1.1 daniel 213:
214: int error; /* error code */
215:
216: xmlXPathContextPtr context; /* the evaluation context */
217: xmlXPathObjectPtr value; /* the current value */
218: int valueNr; /* number of values stacked */
219: int valueMax; /* max number of values stacked */
220: xmlXPathObjectPtr *valueTab; /* stack of values */
1.15 daniel 221: };
1.1 daniel 222:
223: /*
224: * An XPath function
225: * The arguments (if any) are popped out of the context stack
226: * and the result is pushed on the stack.
227: */
228:
1.4 daniel 229: typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
1.1 daniel 230:
231: /************************************************************************
232: * *
1.22 veillard 233: * Helpers *
234: * *
235: ************************************************************************/
236:
237: #define CHECK_ERROR \
238: if (ctxt->error != XPATH_EXPRESSION_OK) return
239:
1.23 ! veillard 240: #define CHECK_ERROR0 \
! 241: if (ctxt->error != XPATH_EXPRESSION_OK) return(0)
! 242:
1.22 veillard 243: #define XP_ERROR(X) \
244: { xmlXPatherror(ctxt, __FILE__, __LINE__, X); \
245: ctxt->error = (X); return; }
246:
247: #define XP_ERROR0(X) \
248: { xmlXPatherror(ctxt, __FILE__, __LINE__, X); \
249: ctxt->error = (X); return(0); }
250:
251: #define CHECK_TYPE(typeval) \
252: if ((ctxt->value == NULL) || (ctxt->value->type != typeval)) \
253: XP_ERROR(XPATH_INVALID_TYPE) \
1.23 ! veillard 254:
! 255: #define CHECK_ARITY(x) \
! 256: if (nargs != (x)) { \
! 257: XP_ERROR(XPATH_INVALID_ARITY); \
! 258: } \
1.22 veillard 259:
260: void xmlXPatherror (xmlXPathParserContextPtr ctxt,
261: const char *file,
262: int line,
263: int no);
264:
265: /**
266: * Utilities for implementing more functions
267: */
268: xmlXPathObjectPtr valuePop (xmlXPathParserContextPtr ctxt);
269: int valuePush (xmlXPathParserContextPtr ctxt,
270: xmlXPathObjectPtr value);
271:
272: /************************************************************************
273: * *
1.1 daniel 274: * Public API *
275: * *
276: ************************************************************************/
277:
1.9 daniel 278: /**
279: * Registering extensions to the expression language
280: */
281: /* TODO */ int xmlXPathRegisterType (xmlXPathContextPtr ctxt,
1.10 daniel 282: const xmlChar *name,
1.9 daniel 283: xmlXPathConvertFunc f);
284: /* TODO */ int xmlXPathRegisterAxis (xmlXPathContextPtr ctxt,
1.10 daniel 285: const xmlChar *name,
1.9 daniel 286: xmlXPathAxisFunc f);
287: /* TODO */ int xmlXPathRegisterFunc (xmlXPathContextPtr ctxt,
1.10 daniel 288: const xmlChar *name,
1.9 daniel 289: xmlXPathFunction f);
290: /* TODO */ int xmlXPathRegisterVariable (xmlXPathContextPtr ctxt,
1.10 daniel 291: const xmlChar *name,
1.9 daniel 292: xmlXPathObject value);
293:
294: /**
295: * Evaluation functions.
296: */
1.21 veillard 297: void xmlXPathInit (void);
1.9 daniel 298: xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
1.8 daniel 299: void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
1.10 daniel 300: xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
1.21 veillard 301: xmlXPathContextPtr ctxt);
302: xmlXPathObjectPtr xmlXPathEvalXPtrExpr (const xmlChar *str,
1.8 daniel 303: xmlXPathContextPtr ctxt);
304: void xmlXPathFreeObject (xmlXPathObjectPtr obj);
1.10 daniel 305: xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
1.8 daniel 306: xmlXPathContextPtr ctxt);
1.14 daniel 307: xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val);
308: void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
309: void xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
1.22 veillard 310:
1.3 daniel 311:
1.11 daniel 312: #ifdef __cplusplus
313: }
314: #endif
1.1 daniel 315: #endif /* ! __XML_XPATH_H__ */
Webmaster