2 * Summary: XML Path Language implementation
3 * Description: API for the XML Path Language implementation
5 * XML Path Language implementation
6 * XPath is a language for addressing parts of an XML document,
7 * designed to be used by both XSLT and XPointer
8 * http://www.w3.org/TR/xpath
11 * W3C Recommendation 16 November 1999
12 * http://www.w3.org/TR/1999/REC-xpath-19991116
14 * Copy: See Copyright for the status of this software.
16 * Author: Daniel Veillard
17 * Portion Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
28 #include <stdapis/libxml2/libxml2_hash.h>
29 #include <stdapis/libxml2/libxml2_dict.h>
30 #include <stdapis/libxml2/libxml2_xmlerror.h>
37 typedef struct _xmlXPathContext xmlXPathContext;
38 typedef xmlXPathContext *xmlXPathContextPtr;
39 typedef struct _xmlXPathParserContext xmlXPathParserContext;
40 typedef xmlXPathParserContext *xmlXPathParserContextPtr;
43 * The set of XPath error codes.
46 XPATH_EXPRESSION_OK = 0,
48 XPATH_UNFINISHED_LITERAL_ERROR,
49 XPATH_START_LITERAL_ERROR,
50 XPATH_VARIABLE_REF_ERROR,
51 XPATH_UNDEF_VARIABLE_ERROR,
52 XPATH_INVALID_PREDICATE_ERROR,
55 XPATH_UNKNOWN_FUNC_ERROR,
56 XPATH_INVALID_OPERAND,
59 XPATH_INVALID_CTXT_SIZE,
60 XPATH_INVALID_CTXT_POSITION,
64 XPTR_SUB_RESOURCE_ERROR,
65 XPATH_UNDEF_PREFIX_ERROR,
67 XPATH_INVALID_CHAR_ERROR,
68 XPATH_XE_EXTENSION_FUNC_ERROR
72 * A node-set (an unordered collection of nodes without duplicates).
74 typedef struct _xmlNodeSet xmlNodeSet;
75 typedef xmlNodeSet *xmlNodeSetPtr;
77 int nodeNr; /* number of nodes in the set */
78 int nodeMax; /* size of the array as allocated */
79 xmlNodePtr* nodeTab; /* array of nodes in no particular order */
80 /* @@ with_ns to check wether namespace nodes should be looked at @@ */
84 * An expression is evaluated to yield an object, which
85 * has one of the following four basic types:
91 * @@ XPointer will add more types !
102 XPATH_LOCATIONSET = 7,
104 XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */
105 } xmlXPathObjectType;
107 typedef struct _xmlXPathObject xmlXPathObject;
108 typedef xmlXPathObject *xmlXPathObjectPtr;
109 struct _xmlXPathObject {
110 xmlXPathObjectType type;
111 xmlNodeSetPtr nodesetval;
122 * xmlXPathConvertFunc:
123 * @param obj an XPath object
124 * @param type the number of the target type
126 * A conversion function is associated to a type and used to cast
127 * the new type to primitive values.
129 * Returns -1 in case of error, 0 otherwise
131 typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
134 * Extra type: a name and a conversion function.
137 typedef struct _xmlXPathType xmlXPathType;
138 typedef xmlXPathType *xmlXPathTypePtr;
139 struct _xmlXPathType {
140 const xmlChar *name; /* the type name */
141 xmlXPathConvertFunc func; /* the conversion function */
145 * Extra variable: a name and a value.
148 typedef struct _xmlXPathVariable xmlXPathVariable;
149 typedef xmlXPathVariable *xmlXPathVariablePtr;
150 struct _xmlXPathVariable {
151 const xmlChar *name; /* the variable name */
152 xmlXPathObjectPtr value; /* the value */
157 * @param ctxt an XPath parser context
158 * @param nargs the number of arguments passed to the function
160 * An XPath evaluation function, the parameters are on the XPath context stack.
163 typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs);
166 * Extra function: a name and a evaluation function.
169 typedef struct _xmlXPathFunct xmlXPathFunct;
170 typedef xmlXPathFunct *xmlXPathFuncPtr;
171 struct _xmlXPathFunct {
172 const xmlChar *name; /* the function name */
173 xmlXPathEvalFunc func; /* the evaluation function */
178 * @param ctxt the XPath interpreter context
179 * @param cur the previous node being explored on that axis
181 * An axis traversal function. To traverse an axis, the engine calls
182 * the first time with cur == NULL and repeat until the function returns
183 * NULL indicating the end of the axis traversal.
185 * Returns the next node in that axis or NULL if at the end of the axis.
188 typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr cur);
191 * Extra axis: a name and an axis function.
194 typedef struct _xmlXPathAxis xmlXPathAxis;
195 typedef xmlXPathAxis* xmlXPathAxisPtr;
196 struct _xmlXPathAxis {
197 const xmlChar* name; /* the axis name */
198 xmlXPathAxisFunc func; /* the search function */
201 // XMLENGINE: NEW CODE -- XForms extensions support
203 Callback for resolving prefix names into namespace URIs
205 @return Namespace URI for aNs prefix.
207 Resolving is made with aCtxt context.
209 This function is used internally by for implementation of
210 namespace-resovling feature in XPath API of XML Engine
211 (MXmlEngNamespaceResolver interface is called by libxml2)
213 typedef const xmlChar* (*xeXPathNsResolverFunc)(void* aCtxt, const xmlChar* aNs);
219 * Expression evaluation occurs with respect to a context.
220 * the context consists of:
221 * - a node (the context node)
222 * - a node list (the context node list)
223 * - a set of variable bindings
224 * - a function library
225 * - the set of namespace declarations in scope for the expression
226 * Following the switch to hash tables, this need to be trimmed up at
227 * the next binary incompatible release.
229 struct _xmlXPathContext {
230 xmlDocPtr doc; /* The current document */
231 xmlNodePtr node; /* The current node */
233 //int nb_variables_unused; /* unused (hash table) */
234 //int max_variables_unused; /* unused (hash table) */
235 xmlHashTablePtr varHash; /* Hash table of defined variables */
237 int nb_types; /* number of defined types */
238 int max_types; /* max number of types */
239 xmlXPathTypePtr types; /* Array of defined types */
241 //int nb_funcs_unused; /* unused (hash table) */
242 //int max_funcs_unused; /* unused (hash table) */
243 xmlHashTablePtr funcHash; /* Hash table of defined funcs */
245 int nb_axis; /* number of defined axis */
246 int max_axis; /* max number of axis */
247 xmlXPathAxisPtr axis; /* Array of defined axis */
249 /* the namespace nodes of the context node */
250 xmlNsPtr *namespaces; /* Array of namespaces */
251 int nsNr; /* number of namespace in scope */
252 void *user; /* function to free */
254 /* extra variables */
255 int contextSize; /* the context size */
256 int proximityPosition; /* the proximity position */
258 /* extra stuff for XPointer */
259 int xptr; /* it this an XPointer context */
260 xmlNodePtr here; /* for here() */
261 xmlNodePtr origin; /* for origin() */
263 /* the set of namespace declarations in scope for the expression */
264 xmlHashTablePtr nsHash; /* The namespaces hash table */
265 void* varLookupFunc; /* variable lookup func */
266 void* varLookupData; /* variable lookup data */
268 /* Possibility to link in an extra item */
269 void* extra; /* needed for XSLT */
271 /* The function name and URI when calling a function */
272 const xmlChar* function;
273 const xmlChar* functionURI;
275 /* function lookup function and data */
276 void* funcLookupFunc; /* function lookup func */
277 void* funcLookupData; /* function lookup data */
279 /* temporary namespace lists kept for walking the namespace axis */
280 xmlNsPtr* tmpNsList; /* Array of namespaces */
281 int tmpNsNr; /* number of namespace in scope */
283 /* error reporting mechanism */
284 void* userData; /* user specific data block */
285 xmlStructuredErrorFunc error; /* the callback in case of errors */
286 xmlError lastError; /* the last error */
287 xmlNodePtr debugNode; /* the source node XSLT */
290 xmlDictPtr dict; /* dictionnary if any */
292 // XMLENGINE: NEW CODE -- XForms extensions support
293 xmlHashTablePtr instanceDocs; /* hash table that stores instance XForms instance docs,
294 not owned by the structure, will be freed by the client*/
295 xmlNodeSetPtr dependencyList;/* nodes that the expression depends on */
296 xeXPathNsResolverFunc xeResolveNs; /* find namespace URI bound to prefix in the current context */
297 void* xeResolveNsCtxt; /* resolver-specific context for processing */
303 * The structure of a compiled expression form has become public in XML ENGINE
326 #ifdef LIBXML_XPTR_ENABLED
331 typedef struct _xmlXPathStepOp xmlXPathStepOp;
332 typedef xmlXPathStepOp *xmlXPathStepOpPtr;
333 struct _xmlXPathStepOp {
334 xmlXPathOp op; /* The identifier of the operation */
335 int ch1; /* First child */
336 int ch2; /* Second child */
348 The initial size of 'steps' table in the precompiled XPath exression
350 Double-it policy for growth is used in the code
352 #define XPATH_STEPS_GRANULARITY 10
354 typedef struct _xmlXPathCompExpr xmlXPathCompExpr;
355 typedef xmlXPathCompExpr* xmlXPathCompExprPtr;
356 struct _xmlXPathCompExpr {
357 int nbStep; /* Number of steps in this expression */
358 int maxStep; /* Maximum number of steps allocated */
359 xmlXPathStepOp* steps; /* ops for computation of this expression */
360 int last; /* index of last step in expression */
361 xmlChar* expr; /* the expression being computed */
362 xmlDictPtr dict; /* the dictionnary to use if any */
363 #ifdef DEBUG_EVAL_COUNTS
367 //XMLENGINE: NEW CODE
368 void* extendedContext; /* some data set by API users and available for XPath extension functions */
369 void* xeNsResolver; /* default namespace resolver supplied when compiling expression */
370 //XMLENGINE: END NEW CODE
373 * xmlXPathParserContext:
375 * An XPath parser context. It contains pure parsing informations,
376 * an xmlXPathContext, and the stack of objects.
378 struct _xmlXPathParserContext {
379 const xmlChar *cur; /* the current char being parsed */
380 const xmlChar *base; /* the full expression */
382 int error; /* error code */
384 xmlXPathContextPtr context; /* the evaluation context */
385 xmlXPathObjectPtr value; /* the current value */
386 int valueNr; /* number of values stacked */
387 int valueMax; /* max number of values stacked */
388 xmlXPathObjectPtr *valueTab; /* stack of values */
390 xmlXPathCompExprPtr comp; /* the precompiled expression */
391 int xptr; /* it this an XPointer expression */
392 xmlNodePtr ancestor; /* used for walking preceding axis */
397 * @param ctxt the XPath interprestation context
398 * @param nargs the number of arguments
401 * The arguments (if any) are popped out from the context stack
402 * and the result is pushed on the stack.
405 typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
407 /************************************************************************
411 ************************************************************************/
414 NOTE: Now defined as local inline functions in xpath.c
415 It seems that no one uses them from outside..
419 #define xmlXPathIsNaN(val) trio_isnan(val)
420 #define xmlXPathIsInf(val) trio_isinf(val)
423 /* These macros may later turn into functions */
425 * xmlXPathNodeSetGetLength:
426 * @param ns a node-set
428 * Implements a functionality similar to the DOM NodeList.length.
430 * Returns the number of nodes in the node-set.
432 #define xmlXPathNodeSetGetLength(ns) ((ns) ? (ns)->nodeNr : 0)
434 * xmlXPathNodeSetItem:
435 * @param ns a node-set
436 * @param index index of a node in the set
438 * Implements a functionality similar to the DOM NodeList.item().
440 * Returns the xmlNodePtr at the given index in ns or NULL if
441 * index is out of range (0 to length-1)
443 #define xmlXPathNodeSetItem(ns, index) \
444 (( (ns) && ((index) >= 0) && ((index) < (ns)->nodeNr) ) \
445 ? (ns)->nodeTab[(index)] \
448 * xmlXPathNodeSetIsEmpty:
449 * @param ns a node-set
451 * Checks whether ns is empty or not.
453 * Returns %TRUE if ns is an empty node-set.
455 #define xmlXPathNodeSetIsEmpty(ns) \
456 (((ns) == NULL) || ((ns)->nodeNr == 0) || ((ns)->nodeTab == NULL))
459 XMLPUBFUN void XMLCALL
460 xmlXPathFreeObject (xmlXPathObjectPtr obj);
461 XMLPUBFUN xmlNodeSetPtr XMLCALL
462 xmlXPathNodeSetCreate (xmlNodePtr val);
463 XMLPUBFUN void XMLCALL
464 xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
465 XMLPUBFUN void XMLCALL
466 xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
467 XMLPUBFUN xmlXPathObjectPtr XMLCALL
468 xmlXPathObjectCopy (xmlXPathObjectPtr val);
469 XMLPUBFUN int XMLCALL
470 xmlXPathCmpNodes (xmlNodePtr node1, xmlNodePtr node2);
472 * Conversion functions to basic types.
474 XMLPUBFUN int XMLCALL
475 xmlXPathCastNumberToBoolean (double val);
476 XMLPUBFUN int XMLCALL
477 xmlXPathCastStringToBoolean (const xmlChar * val);
478 XMLPUBFUN int XMLCALL
479 xmlXPathCastNodeSetToBoolean(xmlNodeSetPtr ns);
480 XMLPUBFUN int XMLCALL
481 xmlXPathCastToBoolean (xmlXPathObjectPtr val);
483 XMLPUBFUN double XMLCALL
484 xmlXPathCastBooleanToNumber (int val);
485 XMLPUBFUN double XMLCALL
486 xmlXPathCastStringToNumber (const xmlChar * val);
487 XMLPUBFUN double XMLCALL
488 xmlXPathCastNodeToNumber (xmlNodePtr node);
489 XMLPUBFUN double XMLCALL
490 xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns);
491 XMLPUBFUN double XMLCALL
492 xmlXPathCastToNumber (xmlXPathObjectPtr val);
494 XMLPUBFUN xmlChar* XMLCALL
495 xmlXPathCastBooleanToString (int val);
496 XMLPUBFUN xmlChar* XMLCALL
497 xmlXPathCastNumberToString (double val);
498 XMLPUBFUN xmlChar* XMLCALL
499 xmlXPathCastNodeToString (xmlNodePtr node);
500 XMLPUBFUN xmlChar* XMLCALL
501 xmlXPathCastNodeSetToString (xmlNodeSetPtr ns);
502 XMLPUBFUN xmlChar* XMLCALL
503 xmlXPathCastToString (xmlXPathObjectPtr val);
505 XMLPUBFUN xmlXPathObjectPtr XMLCALL
506 xmlXPathConvertBoolean (xmlXPathObjectPtr val);
507 XMLPUBFUN xmlXPathObjectPtr XMLCALL
508 xmlXPathConvertNumber (xmlXPathObjectPtr val);
509 XMLPUBFUN xmlXPathObjectPtr XMLCALL
510 xmlXPathConvertString (xmlXPathObjectPtr val);
515 XMLPUBFUN void XMLCALL
517 XMLPUBFUN xmlXPathContextPtr XMLCALL
518 xmlXPathNewContext (xmlDocPtr doc);
519 XMLPUBFUN void XMLCALL
520 xmlXPathFreeContext (xmlXPathContextPtr ctxt);
523 * Evaluation functions.
525 XMLPUBFUN long XMLCALL
526 xmlXPathOrderDocElems (xmlDocPtr doc);
527 XMLPUBFUN xmlXPathObjectPtr XMLCALL
528 xmlXPathEval (const xmlChar *str, xmlXPathContextPtr ctx);
529 XMLPUBFUN xmlXPathObjectPtr XMLCALL
530 xmlXPathEvalExpression (const xmlChar *str, xmlXPathContextPtr ctxt);
531 XMLPUBFUN int XMLCALL
532 xmlXPathEvalPredicate (xmlXPathContextPtr ctxt, xmlXPathObjectPtr res);
534 * Separate compilation/evaluation entry points.
536 XMLPUBFUN xmlXPathCompExprPtr XMLCALL
537 xmlXPathCompile (const xmlChar *str);
538 XMLPUBFUN xmlXPathCompExprPtr XMLCALL
539 xmlXPathCtxtCompile (xmlXPathContextPtr ctxt, const xmlChar *str);
540 XMLPUBFUN xmlXPathObjectPtr XMLCALL
541 xmlXPathCompiledEval (xmlXPathCompExprPtr comp,xmlXPathContextPtr ctx);
542 XMLPUBFUN void XMLCALL
543 xmlXPathFreeCompExpr (xmlXPathCompExprPtr comp);
549 #endif /* XML_XPATH_H */