blob: fc567fd8dd589bfececa96265b6ca904294212a4 [file] [log] [blame]
Daniel Veillard1566d3a1999-07-15 14:24:29 +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 *
9 * Author: Daniel.Veillard@w3.org
10 */
11
12#ifndef __XML_XPATH_H__
13#define __XML_XPATH_H__
14
Daniel Veillard361d8452000-04-03 19:48:13 +000015#include <libxml/tree.h>
Daniel Veillardb24054a1999-12-18 15:32:46 +000016
Daniel Veillarde4e51311999-12-18 15:32:46 +000017#ifdef __cplusplus
Daniel Veillard5cb5ab81999-12-21 15:35:29 +000018extern "C" {
Daniel Veillarde4e51311999-12-18 15:32:46 +000019#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000020
Daniel Veillard71b656e2000-01-05 14:46:17 +000021typedef struct _xmlXPathContext xmlXPathContext;
22typedef xmlXPathContext *xmlXPathContextPtr;
23typedef struct _xmlXPathParserContext xmlXPathParserContext;
24typedef xmlXPathParserContext *xmlXPathParserContextPtr;
Daniel Veillardc08a2c61999-09-08 21:35:25 +000025
Daniel Veillard1566d3a1999-07-15 14:24:29 +000026/*
27 * A node-set (an unordered collection of nodes without duplicates)
28 */
Daniel Veillard71b656e2000-01-05 14:46:17 +000029typedef struct _xmlNodeSet xmlNodeSet;
30typedef xmlNodeSet *xmlNodeSetPtr;
31struct _xmlNodeSet {
Daniel Veillardbe803962000-06-28 23:40:59 +000032 int nodeNr; /* number of nodes in the set */
33 int nodeMax; /* size of the array as allocated */
Daniel Veillard1566d3a1999-07-15 14:24:29 +000034 xmlNodePtr *nodeTab; /* array of nodes in no particular order */
Daniel Veillard71b656e2000-01-05 14:46:17 +000035};
Daniel Veillard1566d3a1999-07-15 14:24:29 +000036
37/*
38 * An expression is evaluated to yield an object, which
39 * has one of the following four basic types:
40 * - node-set
41 * - boolean
42 * - number
43 * - string
Daniel Veillardbe803962000-06-28 23:40:59 +000044 *
45 * @@ XPointer will add more types !
Daniel Veillard1566d3a1999-07-15 14:24:29 +000046 */
47
48#define XPATH_UNDEFINED 0
49#define XPATH_NODESET 1
50#define XPATH_BOOLEAN 2
51#define XPATH_NUMBER 3
52#define XPATH_STRING 4
Daniel Veillardc08a2c61999-09-08 21:35:25 +000053#define XPATH_USERS 5
Daniel Veillard1566d3a1999-07-15 14:24:29 +000054
Daniel Veillard71b656e2000-01-05 14:46:17 +000055typedef struct _xmlXPathObject xmlXPathObject;
56typedef xmlXPathObject *xmlXPathObjectPtr;
57struct _xmlXPathObject {
Daniel Veillard1566d3a1999-07-15 14:24:29 +000058 int type;
59 xmlNodeSetPtr nodesetval;
60 int boolval;
Daniel Veillarde2d034d1999-07-27 19:52:06 +000061 double floatval;
Daniel Veillarddd6b3671999-09-23 22:19:22 +000062 xmlChar *stringval;
Daniel Veillardc08a2c61999-09-08 21:35:25 +000063 void *user;
Daniel Veillard71b656e2000-01-05 14:46:17 +000064};
Daniel Veillard1566d3a1999-07-15 14:24:29 +000065
Daniel Veillardc08a2c61999-09-08 21:35:25 +000066/*
67 * A conversion function is associated to a type and used to cast
68 * the new type to primitive values.
69 */
70typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
71
72/*
73 * Extra type: a name and a conversion function.
74 */
75
Daniel Veillard71b656e2000-01-05 14:46:17 +000076typedef struct _xmlXPathType xmlXPathType;
77typedef xmlXPathType *xmlXPathTypePtr;
78struct _xmlXPathType {
Daniel Veillarddd6b3671999-09-23 22:19:22 +000079 const xmlChar *name; /* the type name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +000080 xmlXPathConvertFunc func; /* the conversion function */
Daniel Veillard71b656e2000-01-05 14:46:17 +000081};
Daniel Veillardc08a2c61999-09-08 21:35:25 +000082
83/*
84 * Extra variable: a name and a value.
85 */
86
Daniel Veillard71b656e2000-01-05 14:46:17 +000087typedef struct _xmlXPathVariable xmlXPathVariable;
88typedef xmlXPathVariable *xmlXPathVariablePtr;
89struct _xmlXPathVariable {
Daniel Veillarddd6b3671999-09-23 22:19:22 +000090 const xmlChar *name; /* the variable name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +000091 xmlXPathObjectPtr value; /* the value */
Daniel Veillard71b656e2000-01-05 14:46:17 +000092};
Daniel Veillardc08a2c61999-09-08 21:35:25 +000093
94/*
95 * an evaluation function, the parameters are on the context stack
96 */
97
98typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs);
99
100/*
101 * Extra function: a name and a evaluation function.
102 */
103
Daniel Veillard71b656e2000-01-05 14:46:17 +0000104typedef struct _xmlXPathFunct xmlXPathFunct;
105typedef xmlXPathFunct *xmlXPathFuncPtr;
106struct _xmlXPathFunct {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000107 const xmlChar *name; /* the function name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000108 xmlXPathEvalFunc func; /* the evaluation function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000109};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000110
111/*
112 * An axis traversal function. To traverse an axis, the engine calls
113 * the first time with cur == NULL and repeat until the function returns
114 * NULL indicating the end of the axis traversal.
115 */
116
117typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
118 xmlXPathObjectPtr cur);
119
120/*
121 * Extra axis: a name and an axis function.
122 */
123
Daniel Veillard71b656e2000-01-05 14:46:17 +0000124typedef struct _xmlXPathAxis xmlXPathAxis;
125typedef xmlXPathAxis *xmlXPathAxisPtr;
126struct _xmlXPathAxis {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000127 const xmlChar *name; /* the axis name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000128 xmlXPathAxisFunc func; /* the search function */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000129};
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000130
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000131/*
132 * Expression evaluation occurs with respect to a context.
133 * he context consists of:
134 * - a node (the context node)
135 * - a node list (the context node list)
136 * - a set of variable bindings
137 * - a function library
138 * - the set of namespace declarations in scope for the expression
139 */
140
Daniel Veillard71b656e2000-01-05 14:46:17 +0000141struct _xmlXPathContext {
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000142 xmlDocPtr doc; /* The current document */
143 xmlNodePtr node; /* The current node */
144 xmlNodeSetPtr nodelist; /* The current node list */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000145
146 int nb_variables; /* number of defined variables */
147 int max_variables; /* max number of variables */
148 xmlXPathVariablePtr *variables; /* Array of defined variables */
149
150 int nb_types; /* number of defined types */
151 int max_types; /* max number of types */
152 xmlXPathTypePtr *types; /* Array of defined types */
153
154 int nb_funcs; /* number of defined funcs */
155 int max_funcs; /* max number of funcs */
156 xmlXPathFuncPtr *funcs; /* Array of defined funcs */
157
158 int nb_axis; /* number of defined axis */
159 int max_axis; /* max number of axis */
160 xmlXPathAxisPtr *axis; /* Array of defined axis */
161
162 /* Namespace traversal should be implemented with user */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000163 xmlNsPtr *namespaces; /* The namespaces lookup */
164 int nsNr; /* the current Namespace index */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000165 void *user; /* user defined extra info */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000166};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000167
168/*
169 * An XPath parser context, it contains pure parsing informations,
170 * an xmlXPathContext, and the stack of objects.
171 */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000172struct _xmlXPathParserContext {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000173 const xmlChar *cur; /* the current char being parsed */
174 const xmlChar *base; /* the full expression */
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000175
176 int error; /* error code */
177
178 xmlXPathContextPtr context; /* the evaluation context */
179 xmlXPathObjectPtr value; /* the current value */
180 int valueNr; /* number of values stacked */
181 int valueMax; /* max number of values stacked */
182 xmlXPathObjectPtr *valueTab; /* stack of values */
Daniel Veillard71b656e2000-01-05 14:46:17 +0000183};
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000184
185/*
186 * An XPath function
187 * The arguments (if any) are popped out of the context stack
188 * and the result is pushed on the stack.
189 */
190
191typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
192
193/************************************************************************
194 * *
195 * Public API *
196 * *
197 ************************************************************************/
198
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000199/**
200 * Registering extensions to the expression language
201 */
202/* TODO */ int xmlXPathRegisterType (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000203 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000204 xmlXPathConvertFunc f);
205/* TODO */ int xmlXPathRegisterAxis (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000206 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000207 xmlXPathAxisFunc f);
208/* TODO */ int xmlXPathRegisterFunc (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000209 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000210 xmlXPathFunction f);
211/* TODO */ int xmlXPathRegisterVariable (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000212 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000213 xmlXPathObject value);
214
215/**
216 * Evaluation functions.
217 */
218xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000219void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000220xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000221 xmlXPathContextPtr ctxt);
222void xmlXPathFreeObject (xmlXPathObjectPtr obj);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000223xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000224 xmlXPathContextPtr ctxt);
Daniel Veillarddbfd6411999-12-28 16:35:14 +0000225xmlNodeSetPtr xmlXPathNodeSetCreate (xmlNodePtr val);
226void xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
227void xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000228
Daniel Veillarde4e51311999-12-18 15:32:46 +0000229#ifdef __cplusplus
230}
231#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000232#endif /* ! __XML_XPATH_H__ */