1999-07-15 18:24:29 +04:00
/*
* xpath . c : interface for XML Path Language implementation
*
* Reference : W3C Working Draft 5 July 1999
* http : //www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html
*
* See COPYRIGHT for the status of this software
*
* Author : Daniel . Veillard @ w3 . org
*/
# ifndef __XML_XPATH_H__
# define __XML_XPATH_H__
2000-04-03 23:48:13 +04:00
# include <libxml/tree.h>
2000-10-22 20:56:02 +04:00
# include <libxml/hash.h>
1999-12-18 18:32:46 +03:00
1999-12-18 18:32:46 +03:00
# ifdef __cplusplus
1999-12-21 18:35:29 +03:00
extern " C " {
1999-12-18 18:32:46 +03:00
# endif
1999-07-15 18:24:29 +04:00
2000-01-05 17:46:17 +03:00
typedef struct _xmlXPathContext xmlXPathContext ;
typedef xmlXPathContext * xmlXPathContextPtr ;
typedef struct _xmlXPathParserContext xmlXPathParserContext ;
typedef xmlXPathParserContext * xmlXPathParserContextPtr ;
1999-09-09 01:35:25 +04:00
2000-10-05 20:30:11 +04:00
/**
* The set of XPath error codes
*/
typedef enum {
XPATH_EXPRESSION_OK = 0 ,
XPATH_NUMBER_ERROR ,
XPATH_UNFINISHED_LITERAL_ERROR ,
XPATH_START_LITERAL_ERROR ,
XPATH_VARIABLE_REF_ERROR ,
XPATH_UNDEF_VARIABLE_ERROR ,
XPATH_INVALID_PREDICATE_ERROR ,
XPATH_EXPR_ERROR ,
XPATH_UNCLOSED_ERROR ,
XPATH_UNKNOWN_FUNC_ERROR ,
XPATH_INVALID_OPERAND ,
XPATH_INVALID_TYPE ,
XPATH_INVALID_ARITY ,
XPATH_INVALID_CTXT_SIZE ,
2000-10-09 16:30:39 +04:00
XPATH_INVALID_CTXT_POSITION ,
XPATH_MEMORY_ERROR ,
XPTR_SYNTAX_ERROR ,
XPTR_RESOURCE_ERROR ,
XPTR_SUB_RESOURCE_ERROR
2000-10-05 20:30:11 +04:00
} xmlXPathError ;
1999-07-15 18:24:29 +04:00
/*
* A node - set ( an unordered collection of nodes without duplicates )
*/
2000-01-05 17:46:17 +03:00
typedef struct _xmlNodeSet xmlNodeSet ;
typedef xmlNodeSet * xmlNodeSetPtr ;
struct _xmlNodeSet {
2000-06-29 03:40:59 +04:00
int nodeNr ; /* number of nodes in the set */
int nodeMax ; /* size of the array as allocated */
1999-07-15 18:24:29 +04:00
xmlNodePtr * nodeTab ; /* array of nodes in no particular order */
2000-01-05 17:46:17 +03:00
} ;
1999-07-15 18:24:29 +04:00
/*
* An expression is evaluated to yield an object , which
* has one of the following four basic types :
* - node - set
* - boolean
* - number
* - string
2000-06-29 03:40:59 +04:00
*
* @ @ XPointer will add more types !
1999-07-15 18:24:29 +04:00
*/
2000-10-01 19:53:30 +04:00
typedef enum {
XPATH_UNDEFINED = 0 ,
XPATH_NODESET = 1 ,
XPATH_BOOLEAN = 2 ,
XPATH_NUMBER = 3 ,
XPATH_STRING = 4 ,
2000-10-04 17:33:43 +04:00
XPATH_POINT = 5 ,
XPATH_RANGE = 6 ,
XPATH_LOCATIONSET = 7 ,
XPATH_USERS = 8
2000-10-01 19:53:30 +04:00
} xmlXPathObjectType ;
1999-07-15 18:24:29 +04:00
2000-01-05 17:46:17 +03:00
typedef struct _xmlXPathObject xmlXPathObject ;
typedef xmlXPathObject * xmlXPathObjectPtr ;
struct _xmlXPathObject {
2000-10-01 19:53:30 +04:00
xmlXPathObjectType type ;
1999-07-15 18:24:29 +04:00
xmlNodeSetPtr nodesetval ;
int boolval ;
1999-07-27 23:52:06 +04:00
double floatval ;
1999-09-24 02:19:22 +04:00
xmlChar * stringval ;
1999-09-09 01:35:25 +04:00
void * user ;
2000-10-04 17:33:43 +04:00
int index ;
void * user2 ;
int index2 ;
2000-01-05 17:46:17 +03:00
} ;
1999-07-15 18:24:29 +04:00
1999-09-09 01:35:25 +04:00
/*
* A conversion function is associated to a type and used to cast
* the new type to primitive values .
*/
typedef int ( * xmlXPathConvertFunc ) ( xmlXPathObjectPtr obj , int type ) ;
/*
* Extra type : a name and a conversion function .
*/
2000-01-05 17:46:17 +03:00
typedef struct _xmlXPathType xmlXPathType ;
typedef xmlXPathType * xmlXPathTypePtr ;
struct _xmlXPathType {
1999-09-24 02:19:22 +04:00
const xmlChar * name ; /* the type name */
1999-09-09 01:35:25 +04:00
xmlXPathConvertFunc func ; /* the conversion function */
2000-01-05 17:46:17 +03:00
} ;
1999-09-09 01:35:25 +04:00
/*
* Extra variable : a name and a value .
*/
2000-01-05 17:46:17 +03:00
typedef struct _xmlXPathVariable xmlXPathVariable ;
typedef xmlXPathVariable * xmlXPathVariablePtr ;
struct _xmlXPathVariable {
1999-09-24 02:19:22 +04:00
const xmlChar * name ; /* the variable name */
1999-09-09 01:35:25 +04:00
xmlXPathObjectPtr value ; /* the value */
2000-01-05 17:46:17 +03:00
} ;
1999-09-09 01:35:25 +04:00
/*
* an evaluation function , the parameters are on the context stack
*/
typedef void ( * xmlXPathEvalFunc ) ( xmlXPathParserContextPtr ctxt , int nargs ) ;
/*
* Extra function : a name and a evaluation function .
*/
2000-01-05 17:46:17 +03:00
typedef struct _xmlXPathFunct xmlXPathFunct ;
typedef xmlXPathFunct * xmlXPathFuncPtr ;
struct _xmlXPathFunct {
1999-09-24 02:19:22 +04:00
const xmlChar * name ; /* the function name */
1999-09-09 01:35:25 +04:00
xmlXPathEvalFunc func ; /* the evaluation function */
2000-01-05 17:46:17 +03:00
} ;
1999-09-09 01:35:25 +04:00
/*
* An axis traversal function . To traverse an axis , the engine calls
* the first time with cur = = NULL and repeat until the function returns
* NULL indicating the end of the axis traversal .
*/
typedef xmlXPathObjectPtr ( * xmlXPathAxisFunc ) ( xmlXPathParserContextPtr ctxt ,
xmlXPathObjectPtr cur ) ;
/*
* Extra axis : a name and an axis function .
*/
2000-01-05 17:46:17 +03:00
typedef struct _xmlXPathAxis xmlXPathAxis ;
typedef xmlXPathAxis * xmlXPathAxisPtr ;
struct _xmlXPathAxis {
1999-09-24 02:19:22 +04:00
const xmlChar * name ; /* the axis name */
1999-09-09 01:35:25 +04:00
xmlXPathAxisFunc func ; /* the search function */
2000-01-05 17:46:17 +03:00
} ;
1999-09-09 01:35:25 +04:00
1999-07-15 18:24:29 +04:00
/*
* Expression evaluation occurs with respect to a context .
* he context consists of :
* - a node ( the context node )
* - a node list ( the context node list )
* - a set of variable bindings
* - a function library
* - the set of namespace declarations in scope for the expression
*/
2000-01-05 17:46:17 +03:00
struct _xmlXPathContext {
1999-07-15 18:24:29 +04:00
xmlDocPtr doc ; /* The current document */
xmlNodePtr node ; /* The current node */
1999-09-09 01:35:25 +04:00
2000-10-24 21:10:12 +04:00
int nb_variables_unused ; /* unused (hash table) */
int max_variables_unused ; /* unused (hash table) */
xmlHashTablePtr varHash ; /* Hash table of defined variables */
1999-09-09 01:35:25 +04:00
int nb_types ; /* number of defined types */
int max_types ; /* max number of types */
2000-10-13 03:15:24 +04:00
xmlXPathTypePtr types ; /* Array of defined types */
1999-09-09 01:35:25 +04:00
2000-10-22 20:56:02 +04:00
int nb_funcs_unused ; /* unused (hash table) */
int max_funcs_unused ; /* unused (hash table) */
xmlHashTablePtr funcHash ; /* Hash table of defined funcs */
1999-09-09 01:35:25 +04:00
int nb_axis ; /* number of defined axis */
int max_axis ; /* max number of axis */
2000-10-13 03:15:24 +04:00
xmlXPathAxisPtr axis ; /* Array of defined axis */
1999-09-09 01:35:25 +04:00
/* Namespace traversal should be implemented with user */
1999-08-30 01:02:19 +04:00
xmlNsPtr * namespaces ; /* The namespaces lookup */
int nsNr ; /* the current Namespace index */
1999-09-09 01:35:25 +04:00
void * user ; /* user defined extra info */
2000-10-01 19:53:30 +04:00
/* extra variables */
int contextSize ; /* the context size */
int proximityPosition ; /* the proximity position */
2000-10-04 17:33:43 +04:00
/* extra stuff for XPointer */
int xptr ; /* it this an XPointer context */
xmlNodePtr here ; /* for here() */
xmlNodePtr origin ; /* for origin() */
2000-01-05 17:46:17 +03:00
} ;
1999-07-15 18:24:29 +04:00
/*
* An XPath parser context , it contains pure parsing informations ,
* an xmlXPathContext , and the stack of objects .
*/
2000-01-05 17:46:17 +03:00
struct _xmlXPathParserContext {
1999-09-24 02:19:22 +04:00
const xmlChar * cur ; /* the current char being parsed */
const xmlChar * base ; /* the full expression */
1999-07-15 18:24:29 +04:00
int error ; /* error code */
xmlXPathContextPtr context ; /* the evaluation context */
xmlXPathObjectPtr value ; /* the current value */
int valueNr ; /* number of values stacked */
int valueMax ; /* max number of values stacked */
xmlXPathObjectPtr * valueTab ; /* stack of values */
2000-01-05 17:46:17 +03:00
} ;
1999-07-15 18:24:29 +04:00
/*
* An XPath function
* The arguments ( if any ) are popped out of the context stack
* and the result is pushed on the stack .
*/
typedef void ( * xmlXPathFunction ) ( xmlXPathParserContextPtr ctxt , int nargs ) ;
2000-10-05 20:30:11 +04:00
/************************************************************************
* *
* Helpers *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# define CHECK_ERROR \
if ( ctxt - > error ! = XPATH_EXPRESSION_OK ) return
# define CHECK_ERROR0 \
if ( ctxt - > error ! = XPATH_EXPRESSION_OK ) return ( 0 )
# define XP_ERROR(X) \
{ xmlXPatherror ( ctxt , __FILE__ , __LINE__ , X ) ; \
ctxt - > error = ( X ) ; return ; }
# define XP_ERROR0(X) \
{ xmlXPatherror ( ctxt , __FILE__ , __LINE__ , X ) ; \
ctxt - > error = ( X ) ; return ( 0 ) ; }
# define CHECK_TYPE(typeval) \
if ( ( ctxt - > value = = NULL ) | | ( ctxt - > value - > type ! = typeval ) ) \
XP_ERROR ( XPATH_INVALID_TYPE ) \
# define CHECK_ARITY(x) \
if ( nargs ! = ( x ) ) { \
XP_ERROR ( XPATH_INVALID_ARITY ) ; \
} \
void xmlXPatherror ( xmlXPathParserContextPtr ctxt ,
const char * file ,
int line ,
int no ) ;
2000-10-13 03:15:24 +04:00
void xmlXPathDebugDumpObject ( FILE * output ,
xmlXPathObjectPtr cur ,
int depth ) ;
2000-10-05 20:30:11 +04:00
/**
2000-10-09 16:30:39 +04:00
* Utilities to extend XPath ( XPointer )
2000-10-05 20:30:11 +04:00
*/
2000-10-09 16:30:39 +04:00
xmlXPathParserContextPtr
xmlXPathNewParserContext ( const xmlChar * str ,
xmlXPathContextPtr ctxt ) ;
void xmlXPathFreeParserContext ( xmlXPathParserContextPtr ctxt ) ;
2000-10-05 20:30:11 +04:00
xmlXPathObjectPtr valuePop ( xmlXPathParserContextPtr ctxt ) ;
int valuePush ( xmlXPathParserContextPtr ctxt ,
2000-10-06 16:59:53 +04:00
xmlXPathObjectPtr value ) ;
xmlXPathObjectPtr xmlXPathNewString ( const xmlChar * val ) ;
2000-10-22 16:04:13 +04:00
xmlXPathObjectPtr xmlXPathNewCString ( const char * val ) ;
xmlXPathObjectPtr xmlXPathNewFloat ( double val ) ;
xmlXPathObjectPtr xmlXPathNewBoolean ( int val ) ;
2000-10-06 16:59:53 +04:00
xmlXPathObjectPtr xmlXPathNewNodeSet ( xmlNodePtr val ) ;
void xmlXPathNodeSetAdd ( xmlNodeSetPtr cur ,
xmlNodePtr val ) ;
void xmlXPathIdFunction ( xmlXPathParserContextPtr ctxt ,
int nargs ) ;
void xmlXPathRoot ( xmlXPathParserContextPtr ctxt ) ;
void xmlXPathEvalExpr ( xmlXPathParserContextPtr ctxt ) ;
2000-10-11 14:54:10 +04:00
xmlChar * xmlXPathParseName ( xmlXPathParserContextPtr ctxt ) ;
2000-10-05 20:30:11 +04:00
1999-07-15 18:24:29 +04:00
/************************************************************************
* *
* Public API *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-09-09 01:35:25 +04:00
/**
2000-10-11 14:54:10 +04:00
* Extending a context
1999-09-09 01:35:25 +04:00
*/
2000-10-11 14:54:10 +04:00
int xmlXPathRegisterFunc ( xmlXPathContextPtr ctxt ,
1999-09-24 02:19:22 +04:00
const xmlChar * name ,
1999-09-09 01:35:25 +04:00
xmlXPathFunction f ) ;
2000-10-11 14:54:10 +04:00
int xmlXPathRegisterVariable ( xmlXPathContextPtr ctxt ,
1999-09-24 02:19:22 +04:00
const xmlChar * name ,
2000-10-11 14:54:10 +04:00
xmlXPathObjectPtr value ) ;
1999-09-09 01:35:25 +04:00
/**
* Evaluation functions .
*/
2000-10-04 17:33:43 +04:00
void xmlXPathInit ( void ) ;
1999-09-09 01:35:25 +04:00
xmlXPathContextPtr xmlXPathNewContext ( xmlDocPtr doc ) ;
1999-08-30 01:02:19 +04:00
void xmlXPathFreeContext ( xmlXPathContextPtr ctxt ) ;
1999-09-24 02:19:22 +04:00
xmlXPathObjectPtr xmlXPathEval ( const xmlChar * str ,
1999-08-30 01:02:19 +04:00
xmlXPathContextPtr ctxt ) ;
2000-10-04 17:33:43 +04:00
xmlXPathObjectPtr xmlXPathEvalXPtrExpr ( const xmlChar * str ,
xmlXPathContextPtr ctxt ) ;
1999-08-30 01:02:19 +04:00
void xmlXPathFreeObject ( xmlXPathObjectPtr obj ) ;
1999-09-24 02:19:22 +04:00
xmlXPathObjectPtr xmlXPathEvalExpression ( const xmlChar * str ,
1999-08-30 01:02:19 +04:00
xmlXPathContextPtr ctxt ) ;
1999-12-28 19:35:14 +03:00
xmlNodeSetPtr xmlXPathNodeSetCreate ( xmlNodePtr val ) ;
void xmlXPathFreeNodeSetList ( xmlXPathObjectPtr obj ) ;
void xmlXPathFreeNodeSet ( xmlNodeSetPtr obj ) ;
2000-10-11 14:54:10 +04:00
xmlXPathObjectPtr xmlXPathObjectCopy ( xmlXPathObjectPtr val ) ;
1999-07-15 18:24:29 +04:00
2000-10-05 20:30:11 +04:00
1999-12-18 18:32:46 +03:00
# ifdef __cplusplus
}
# endif
1999-07-15 18:24:29 +04:00
# endif /* ! __XML_XPATH_H__ */