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