blob: 9a082cedccf5462ac1c851e196c9616789545e6f [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
15#include "tree.h"
16
Daniel Veillardc08a2c61999-09-08 21:35:25 +000017typedef struct xmlXPathParserContext *xmlXPathParserContextPtr;
18
Daniel Veillard1566d3a1999-07-15 14:24:29 +000019/*
20 * A node-set (an unordered collection of nodes without duplicates)
21 */
22typedef struct xmlNodeSet {
23 int nodeNr; /* # of node in the set */
24 int nodeMax; /* allocated space */
25 xmlNodePtr *nodeTab; /* array of nodes in no particular order */
26} xmlNodeSet, *xmlNodeSetPtr;
27
28/*
29 * An expression is evaluated to yield an object, which
30 * has one of the following four basic types:
31 * - node-set
32 * - boolean
33 * - number
34 * - string
35 */
36
37#define XPATH_UNDEFINED 0
38#define XPATH_NODESET 1
39#define XPATH_BOOLEAN 2
40#define XPATH_NUMBER 3
41#define XPATH_STRING 4
Daniel Veillardc08a2c61999-09-08 21:35:25 +000042#define XPATH_USERS 5
Daniel Veillard1566d3a1999-07-15 14:24:29 +000043
44typedef struct xmlXPathObject {
45 int type;
46 xmlNodeSetPtr nodesetval;
47 int boolval;
Daniel Veillarde2d034d1999-07-27 19:52:06 +000048 double floatval;
Daniel Veillarddd6b3671999-09-23 22:19:22 +000049 xmlChar *stringval;
Daniel Veillardc08a2c61999-09-08 21:35:25 +000050 void *user;
Daniel Veillard1566d3a1999-07-15 14:24:29 +000051} xmlXPathObject, *xmlXPathObjectPtr;
52
Daniel Veillardc08a2c61999-09-08 21:35:25 +000053/*
54 * A conversion function is associated to a type and used to cast
55 * the new type to primitive values.
56 */
57typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
58
59/*
60 * Extra type: a name and a conversion function.
61 */
62
63typedef struct xmlXPathType {
Daniel Veillarddd6b3671999-09-23 22:19:22 +000064 const xmlChar *name; /* the type name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +000065 xmlXPathConvertFunc func; /* the conversion function */
66} xmlXPathType, *xmlXPathTypePtr;
67
68/*
69 * Extra variable: a name and a value.
70 */
71
72typedef struct xmlXPathVariable {
Daniel Veillarddd6b3671999-09-23 22:19:22 +000073 const xmlChar *name; /* the variable name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +000074 xmlXPathObjectPtr value; /* the value */
75} xmlXPathVariable, *xmlXPathVariablePtr;
76
77/*
78 * an evaluation function, the parameters are on the context stack
79 */
80
81typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs);
82
83/*
84 * Extra function: a name and a evaluation function.
85 */
86
87typedef struct xmlXPathFunct {
Daniel Veillarddd6b3671999-09-23 22:19:22 +000088 const xmlChar *name; /* the function name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +000089 xmlXPathEvalFunc func; /* the evaluation function */
90} xmlXPathFunc, *xmlXPathFuncPtr;
91
92/*
93 * An axis traversal function. To traverse an axis, the engine calls
94 * the first time with cur == NULL and repeat until the function returns
95 * NULL indicating the end of the axis traversal.
96 */
97
98typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
99 xmlXPathObjectPtr cur);
100
101/*
102 * Extra axis: a name and an axis function.
103 */
104
105typedef struct xmlXPathAxis {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000106 const xmlChar *name; /* the axis name */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000107 xmlXPathAxisFunc func; /* the search function */
108} xmlXPathAxis, *xmlXPathAxisPtr;
109
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000110/*
111 * Expression evaluation occurs with respect to a context.
112 * he context consists of:
113 * - a node (the context node)
114 * - a node list (the context node list)
115 * - a set of variable bindings
116 * - a function library
117 * - the set of namespace declarations in scope for the expression
118 */
119
120typedef struct xmlXPathContext {
121 xmlDocPtr doc; /* The current document */
122 xmlNodePtr node; /* The current node */
123 xmlNodeSetPtr nodelist; /* The current node list */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000124
125 int nb_variables; /* number of defined variables */
126 int max_variables; /* max number of variables */
127 xmlXPathVariablePtr *variables; /* Array of defined variables */
128
129 int nb_types; /* number of defined types */
130 int max_types; /* max number of types */
131 xmlXPathTypePtr *types; /* Array of defined types */
132
133 int nb_funcs; /* number of defined funcs */
134 int max_funcs; /* max number of funcs */
135 xmlXPathFuncPtr *funcs; /* Array of defined funcs */
136
137 int nb_axis; /* number of defined axis */
138 int max_axis; /* max number of axis */
139 xmlXPathAxisPtr *axis; /* Array of defined axis */
140
141 /* Namespace traversal should be implemented with user */
Daniel Veillardb96e6431999-08-29 21:02:19 +0000142 xmlNsPtr *namespaces; /* The namespaces lookup */
143 int nsNr; /* the current Namespace index */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000144 void *user; /* user defined extra info */
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000145} xmlXPathContext, *xmlXPathContextPtr;
146
147/*
148 * An XPath parser context, it contains pure parsing informations,
149 * an xmlXPathContext, and the stack of objects.
150 */
151typedef struct xmlXPathParserContext {
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000152 const xmlChar *cur; /* the current char being parsed */
153 const xmlChar *base; /* the full expression */
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000154
155 int error; /* error code */
156
157 xmlXPathContextPtr context; /* the evaluation context */
158 xmlXPathObjectPtr value; /* the current value */
159 int valueNr; /* number of values stacked */
160 int valueMax; /* max number of values stacked */
161 xmlXPathObjectPtr *valueTab; /* stack of values */
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000162} xmlXPathParserContext;
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000163
164/*
165 * An XPath function
166 * The arguments (if any) are popped out of the context stack
167 * and the result is pushed on the stack.
168 */
169
170typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
171
172/************************************************************************
173 * *
174 * Public API *
175 * *
176 ************************************************************************/
177
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000178/**
179 * Registering extensions to the expression language
180 */
181/* TODO */ int xmlXPathRegisterType (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000182 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000183 xmlXPathConvertFunc f);
184/* TODO */ int xmlXPathRegisterAxis (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000185 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000186 xmlXPathAxisFunc f);
187/* TODO */ int xmlXPathRegisterFunc (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000188 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000189 xmlXPathFunction f);
190/* TODO */ int xmlXPathRegisterVariable (xmlXPathContextPtr ctxt,
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000191 const xmlChar *name,
Daniel Veillardc08a2c61999-09-08 21:35:25 +0000192 xmlXPathObject value);
193
194/**
195 * Evaluation functions.
196 */
197xmlXPathContextPtr xmlXPathNewContext (xmlDocPtr doc);
Daniel Veillardb96e6431999-08-29 21:02:19 +0000198void xmlXPathFreeContext (xmlXPathContextPtr ctxt);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000199xmlXPathObjectPtr xmlXPathEval (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000200 xmlXPathContextPtr ctxt);
201void xmlXPathFreeObject (xmlXPathObjectPtr obj);
Daniel Veillarddd6b3671999-09-23 22:19:22 +0000202xmlXPathObjectPtr xmlXPathEvalExpression (const xmlChar *str,
Daniel Veillardb96e6431999-08-29 21:02:19 +0000203 xmlXPathContextPtr ctxt);
Daniel Veillard1566d3a1999-07-15 14:24:29 +0000204
205#endif /* ! __XML_XPATH_H__ */