blob: 505fa8454b7e228370e0967b60fe01b915040bcc [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 Veillardb24054a1999-12-18 15:32:46 +000015#include "tree.h"
16
Daniel Veillarde4e51311999-12-18 15:32:46 +000017#ifdef __cplusplus
18#define extern "C" {
19#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +000020
Daniel Veillardc08a2c61999-09-08 21:35:25 +000021typedef struct xmlXPathParserContext *xmlXPathParserContextPtr;
22
Daniel Veillard1566d3a1999-07-15 14:24:29 +000023/*
24 * A node-set (an unordered collection of nodes without duplicates)
25 */
26typedef struct xmlNodeSet {
27 int nodeNr; /* # of node in the set */
28 int nodeMax; /* allocated space */
29 xmlNodePtr *nodeTab; /* array of nodes in no particular order */
30} xmlNodeSet, *xmlNodeSetPtr;
31
32/*
33 * An expression is evaluated to yield an object, which
34 * has one of the following four basic types:
35 * - node-set
36 * - boolean
37 * - number
38 * - string
39 */
40
41#define XPATH_UNDEFINED 0
42#define XPATH_NODESET 1
43#define XPATH_BOOLEAN 2
44#define XPATH_NUMBER 3
45#define XPATH_STRING 4
Daniel Veillardc08a2c61999-09-08 21:35:25 +000046#define XPATH_USERS 5
Daniel Veillard1566d3a1999-07-15 14:24:29 +000047
48typedef struct xmlXPathObject {
49 int type;
50 xmlNodeSetPtr nodesetval;
51 int boolval;
Daniel Veillarde2d034d1999-07-27 19:52:06 +000052 double floatval;
Daniel Veillarddd6b3671999-09-23 22:19:22 +000053 xmlChar *stringval;
Daniel Veillardc08a2c61999-09-08 21:35:25 +000054 void *user;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000055} xmlXPathObject, *xmlXPathObjectPtr;
56
Daniel Veillardc08a2c61999-09-08 21:35:25 +000057/*
58 * A conversion function is associated to a type and used to cast
59 * the new type to primitive values.
60 */
61typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
62
63/*
64 * Extra type: a name and a conversion function.
65 */
66
67typedef struct xmlXPathType {
Daniel Veillarddd6b3671999-09-23 22:19:22 +000068 const xmlChar *name; /* the type name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +000069 xmlXPathConvertFunc func; /* the conversion function */
70} xmlXPathType, *xmlXPathTypePtr;
71
72/*
73 * Extra variable: a name and a value.
74 */
75
76typedef struct xmlXPathVariable {
Daniel Veillarddd6b3671999-09-23 22:19:22 +000077 const xmlChar *name; /* the variable name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +000078 xmlXPathObjectPtr value; /* the value */
79} xmlXPathVariable, *xmlXPathVariablePtr;
80
81/*
82 * an evaluation function, the parameters are on the context stack
83 */
84
85typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs);
86
87/*
88 * Extra function: a name and a evaluation function.
89 */
90
91typedef struct xmlXPathFunct {
Daniel Veillarddd6b3671999-09-23 22:19:22 +000092 const xmlChar *name; /* the function name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +000093 xmlXPathEvalFunc func; /* the evaluation function */
94} xmlXPathFunc, *xmlXPathFuncPtr;
95
96/*
97 * An axis traversal function. To traverse an axis, the engine calls
98 * the first time with cur == NULL and repeat until the function returns
99 * NULL indicating the end of the axis traversal.
100 */
101
102typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
103 xmlXPathObjectPtr cur);
104
105/*
106 * Extra axis: a name and an axis function.
107 */
108
109typedef struct xmlXPathAxis {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000110 const xmlChar *name; /* the axis name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000111 xmlXPathAxisFunc func; /* the search function */
112} xmlXPathAxis, *xmlXPathAxisPtr;
113
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000114/*
115 * Expression evaluation occurs with respect to a context.
116 * he context consists of:
117 * - a node (the context node)
118 * - a node list (the context node list)
119 * - a set of variable bindings
120 * - a function library
121 * - the set of namespace declarations in scope for the expression
122 */
123
124typedef struct xmlXPathContext {
125 xmlDocPtr doc; /* The current document */
126 xmlNodePtr node; /* The current node */
127 xmlNodeSetPtr nodelist; /* The current node list */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000128
129 int nb_variables; /* number of defined variables */
130 int max_variables; /* max number of variables */
131 xmlXPathVariablePtr *variables; /* Array of defined variables */
132
133 int nb_types; /* number of defined types */
134 int max_types; /* max number of types */
135 xmlXPathTypePtr *types; /* Array of defined types */
136
137 int nb_funcs; /* number of defined funcs */
138 int max_funcs; /* max number of funcs */
139 xmlXPathFuncPtr *funcs; /* Array of defined funcs */
140
141 int nb_axis; /* number of defined axis */
142 int max_axis; /* max number of axis */
143 xmlXPathAxisPtr *axis; /* Array of defined axis */
144
145 /* Namespace traversal should be implemented with user */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000146 xmlNsPtr *namespaces; /* The namespaces lookup */
147 int nsNr; /* the current Namespace index */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000148 void *user; /* user defined extra info */
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000149} xmlXPathContext, *xmlXPathContextPtr;
150
151/*
152 * An XPath parser context, it contains pure parsing informations,
153 * an xmlXPathContext, and the stack of objects.
154 */
155typedef struct xmlXPathParserContext {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000156 const xmlChar *cur; /* the current char being parsed */
157 const xmlChar *base; /* the full expression */
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000158
159 int error; /* error code */
160
161 xmlXPathContextPtr context; /* the evaluation context */
162 xmlXPathObjectPtr value; /* the current value */
163 int valueNr; /* number of values stacked */
164 int valueMax; /* max number of values stacked */
165 xmlXPathObjectPtr *valueTab; /* stack of values */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000166} xmlXPathParserContext;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000167
168/*
169 * An XPath function
170 * The arguments (if any) are popped out of the context stack
171 * and the result is pushed on the stack.
172 */
173
174typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
175
176/************************************************************************
177 * *
178 * Public API *
179 * *
180 ************************************************************************/
181
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000182/**
183 * Registering extensions to the expression language
184 */
185/* TODO */ int xmlXPathRegisterType (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000186 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000187 xmlXPathConvertFunc f);
188/* TODO */ int xmlXPathRegisterAxis (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000189 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000190 xmlXPathAxisFunc f);
191/* TODO */ int xmlXPathRegisterFunc (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000192 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000193 xmlXPathFunction f);
194/* TODO */ int xmlXPathRegisterVariable (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000195 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000196 xmlXPathObject value);
197
198/**
199 * Evaluation functions.
200 */
201xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000202void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000203xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000204 xmlXPathContextPtr ctxt);
205void xmlXPathFreeObject (xmlXPathObjectPtr obj);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000206xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000207 xmlXPathContextPtr ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000208
Daniel Veillarde4e51311999-12-18 15:32:46 +0000209#ifdef __cplusplus
210}
211#endif
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000212#endif /* ! __XML_XPATH_H__ */