/*1* Summary: XML Path Language implementation2* Description: API for the XML Path Language implementation3*4* XML Path Language implementation5* XPath is a language for addressing parts of an XML document,6* designed to be used by both XSLT and XPointer7* http://www.w3.org/TR/xpath8*9* Implements10* W3C Recommendation 16 November 199911* http://www.w3.org/TR/1999/REC-xpath-1999111612*13* Copy: See Copyright for the status of this software.14*15* Author: Daniel Veillard16*/1718#ifndef __XML_XPATH_H__19#define __XML_XPATH_H__2021#include <libxml/xmlversion.h>2223#ifdef LIBXML_XPATH_ENABLED2425#include <libxml/xmlerror.h>26#include <libxml/tree.h>27#include <libxml/hash.h>28#endif /* LIBXML_XPATH_ENABLED */2930#if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)31#ifdef __cplusplus32extern "C" {33#endif34#endif /* LIBXML_XPATH_ENABLED or LIBXML_SCHEMAS_ENABLED */3536#ifdef LIBXML_XPATH_ENABLED3738typedef struct _xmlXPathContext xmlXPathContext;39typedef xmlXPathContext *xmlXPathContextPtr;40typedef struct _xmlXPathParserContext xmlXPathParserContext;41typedef xmlXPathParserContext *xmlXPathParserContextPtr;4243/**44* The set of XPath error codes.45*/4647typedef enum {48XPATH_EXPRESSION_OK = 0,49XPATH_NUMBER_ERROR,50XPATH_UNFINISHED_LITERAL_ERROR,51XPATH_START_LITERAL_ERROR,52XPATH_VARIABLE_REF_ERROR,53XPATH_UNDEF_VARIABLE_ERROR,54XPATH_INVALID_PREDICATE_ERROR,55XPATH_EXPR_ERROR,56XPATH_UNCLOSED_ERROR,57XPATH_UNKNOWN_FUNC_ERROR,58XPATH_INVALID_OPERAND,59XPATH_INVALID_TYPE,60XPATH_INVALID_ARITY,61XPATH_INVALID_CTXT_SIZE,62XPATH_INVALID_CTXT_POSITION,63XPATH_MEMORY_ERROR,64XPTR_SYNTAX_ERROR,65XPTR_RESOURCE_ERROR,66XPTR_SUB_RESOURCE_ERROR,67XPATH_UNDEF_PREFIX_ERROR,68XPATH_ENCODING_ERROR,69XPATH_INVALID_CHAR_ERROR,70XPATH_INVALID_CTXT,71XPATH_STACK_ERROR,72XPATH_FORBID_VARIABLE_ERROR,73XPATH_OP_LIMIT_EXCEEDED,74XPATH_RECURSION_LIMIT_EXCEEDED75} xmlXPathError;7677/*78* A node-set (an unordered collection of nodes without duplicates).79*/80typedef struct _xmlNodeSet xmlNodeSet;81typedef xmlNodeSet *xmlNodeSetPtr;82struct _xmlNodeSet {83int nodeNr; /* number of nodes in the set */84int nodeMax; /* size of the array as allocated */85xmlNodePtr *nodeTab; /* array of nodes in no particular order */86/* @@ with_ns to check whether namespace nodes should be looked at @@ */87};8889/*90* An expression is evaluated to yield an object, which91* has one of the following four basic types:92* - node-set93* - boolean94* - number95* - string96*97* @@ XPointer will add more types !98*/99100typedef enum {101XPATH_UNDEFINED = 0,102XPATH_NODESET = 1,103XPATH_BOOLEAN = 2,104XPATH_NUMBER = 3,105XPATH_STRING = 4,106#ifdef LIBXML_XPTR_LOCS_ENABLED107XPATH_POINT = 5,108XPATH_RANGE = 6,109XPATH_LOCATIONSET = 7,110#endif111XPATH_USERS = 8,112XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */113} xmlXPathObjectType;114115#ifndef LIBXML_XPTR_LOCS_ENABLED116/** DOC_DISABLE */117#define XPATH_POINT 5118#define XPATH_RANGE 6119#define XPATH_LOCATIONSET 7120/** DOC_ENABLE */121#endif122123typedef struct _xmlXPathObject xmlXPathObject;124typedef xmlXPathObject *xmlXPathObjectPtr;125struct _xmlXPathObject {126xmlXPathObjectType type;127xmlNodeSetPtr nodesetval;128int boolval;129double floatval;130xmlChar *stringval;131void *user;132int index;133void *user2;134int index2;135};136137/**138* xmlXPathConvertFunc:139* @obj: an XPath object140* @type: the number of the target type141*142* A conversion function is associated to a type and used to cast143* the new type to primitive values.144*145* Returns -1 in case of error, 0 otherwise146*/147typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);148149/*150* Extra type: a name and a conversion function.151*/152153typedef struct _xmlXPathType xmlXPathType;154typedef xmlXPathType *xmlXPathTypePtr;155struct _xmlXPathType {156const xmlChar *name; /* the type name */157xmlXPathConvertFunc func; /* the conversion function */158};159160/*161* Extra variable: a name and a value.162*/163164typedef struct _xmlXPathVariable xmlXPathVariable;165typedef xmlXPathVariable *xmlXPathVariablePtr;166struct _xmlXPathVariable {167const xmlChar *name; /* the variable name */168xmlXPathObjectPtr value; /* the value */169};170171/**172* xmlXPathEvalFunc:173* @ctxt: an XPath parser context174* @nargs: the number of arguments passed to the function175*176* An XPath evaluation function, the parameters are on the XPath context stack.177*/178179typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt,180int nargs);181182/*183* Extra function: a name and a evaluation function.184*/185186typedef struct _xmlXPathFunct xmlXPathFunct;187typedef xmlXPathFunct *xmlXPathFuncPtr;188struct _xmlXPathFunct {189const xmlChar *name; /* the function name */190xmlXPathEvalFunc func; /* the evaluation function */191};192193/**194* xmlXPathAxisFunc:195* @ctxt: the XPath interpreter context196* @cur: the previous node being explored on that axis197*198* An axis traversal function. To traverse an axis, the engine calls199* the first time with cur == NULL and repeat until the function returns200* NULL indicating the end of the axis traversal.201*202* Returns the next node in that axis or NULL if at the end of the axis.203*/204205typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,206xmlXPathObjectPtr cur);207208/*209* Extra axis: a name and an axis function.210*/211212typedef struct _xmlXPathAxis xmlXPathAxis;213typedef xmlXPathAxis *xmlXPathAxisPtr;214struct _xmlXPathAxis {215const xmlChar *name; /* the axis name */216xmlXPathAxisFunc func; /* the search function */217};218219/**220* xmlXPathFunction:221* @ctxt: the XPath interprestation context222* @nargs: the number of arguments223*224* An XPath function.225* The arguments (if any) are popped out from the context stack226* and the result is pushed on the stack.227*/228229typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);230231/*232* Function and Variable Lookup.233*/234235/**236* xmlXPathVariableLookupFunc:237* @ctxt: an XPath context238* @name: name of the variable239* @ns_uri: the namespace name hosting this variable240*241* Prototype for callbacks used to plug variable lookup in the XPath242* engine.243*244* Returns the XPath object value or NULL if not found.245*/246typedef xmlXPathObjectPtr (*xmlXPathVariableLookupFunc) (void *ctxt,247const xmlChar *name,248const xmlChar *ns_uri);249250/**251* xmlXPathFuncLookupFunc:252* @ctxt: an XPath context253* @name: name of the function254* @ns_uri: the namespace name hosting this function255*256* Prototype for callbacks used to plug function lookup in the XPath257* engine.258*259* Returns the XPath function or NULL if not found.260*/261typedef xmlXPathFunction (*xmlXPathFuncLookupFunc) (void *ctxt,262const xmlChar *name,263const xmlChar *ns_uri);264265/**266* xmlXPathFlags:267* Flags for XPath engine compilation and runtime268*/269/**270* XML_XPATH_CHECKNS:271*272* check namespaces at compilation273*/274#define XML_XPATH_CHECKNS (1<<0)275/**276* XML_XPATH_NOVAR:277*278* forbid variables in expression279*/280#define XML_XPATH_NOVAR (1<<1)281282/**283* xmlXPathContext:284*285* Expression evaluation occurs with respect to a context.286* he context consists of:287* - a node (the context node)288* - a node list (the context node list)289* - a set of variable bindings290* - a function library291* - the set of namespace declarations in scope for the expression292* Following the switch to hash tables, this need to be trimmed up at293* the next binary incompatible release.294* The node may be modified when the context is passed to libxml2295* for an XPath evaluation so you may need to initialize it again296* before the next call.297*/298299struct _xmlXPathContext {300xmlDocPtr doc; /* The current document */301xmlNodePtr node; /* The current node */302303int nb_variables_unused; /* unused (hash table) */304int max_variables_unused; /* unused (hash table) */305xmlHashTablePtr varHash; /* Hash table of defined variables */306307int nb_types; /* number of defined types */308int max_types; /* max number of types */309xmlXPathTypePtr types; /* Array of defined types */310311int nb_funcs_unused; /* unused (hash table) */312int max_funcs_unused; /* unused (hash table) */313xmlHashTablePtr funcHash; /* Hash table of defined funcs */314315int nb_axis; /* number of defined axis */316int max_axis; /* max number of axis */317xmlXPathAxisPtr axis; /* Array of defined axis */318319/* the namespace nodes of the context node */320xmlNsPtr *namespaces; /* Array of namespaces */321int nsNr; /* number of namespace in scope */322void *user; /* function to free */323324/* extra variables */325int contextSize; /* the context size */326int proximityPosition; /* the proximity position */327328/* extra stuff for XPointer */329int xptr; /* is this an XPointer context? */330xmlNodePtr here; /* for here() */331xmlNodePtr origin; /* for origin() */332333/* the set of namespace declarations in scope for the expression */334xmlHashTablePtr nsHash; /* The namespaces hash table */335xmlXPathVariableLookupFunc varLookupFunc;/* variable lookup func */336void *varLookupData; /* variable lookup data */337338/* Possibility to link in an extra item */339void *extra; /* needed for XSLT */340341/* The function name and URI when calling a function */342const xmlChar *function;343const xmlChar *functionURI;344345/* function lookup function and data */346xmlXPathFuncLookupFunc funcLookupFunc;/* function lookup func */347void *funcLookupData; /* function lookup data */348349/* temporary namespace lists kept for walking the namespace axis */350xmlNsPtr *tmpNsList; /* Array of namespaces */351int tmpNsNr; /* number of namespaces in scope */352353/* error reporting mechanism */354void *userData; /* user specific data block */355xmlStructuredErrorFunc error; /* the callback in case of errors */356xmlError lastError; /* the last error */357xmlNodePtr debugNode; /* the source node XSLT */358359/* dictionary */360xmlDictPtr dict; /* dictionary if any */361362int flags; /* flags to control compilation */363364/* Cache for reusal of XPath objects */365void *cache;366367/* Resource limits */368unsigned long opLimit;369unsigned long opCount;370int depth;371};372373/*374* The structure of a compiled expression form is not public.375*/376377typedef struct _xmlXPathCompExpr xmlXPathCompExpr;378typedef xmlXPathCompExpr *xmlXPathCompExprPtr;379380/**381* xmlXPathParserContext:382*383* An XPath parser context. It contains pure parsing information,384* an xmlXPathContext, and the stack of objects.385*/386struct _xmlXPathParserContext {387const xmlChar *cur; /* the current char being parsed */388const xmlChar *base; /* the full expression */389390int error; /* error code */391392xmlXPathContextPtr context; /* the evaluation context */393xmlXPathObjectPtr value; /* the current value */394int valueNr; /* number of values stacked */395int valueMax; /* max number of values stacked */396xmlXPathObjectPtr *valueTab; /* stack of values */397398xmlXPathCompExprPtr comp; /* the precompiled expression */399int xptr; /* it this an XPointer expression */400xmlNodePtr ancestor; /* used for walking preceding axis */401402int valueFrame; /* always zero for compatibility */403};404405/************************************************************************406* *407* Public API *408* *409************************************************************************/410411/**412* Objects and Nodesets handling413*/414415XMLPUBVAR double xmlXPathNAN;416XMLPUBVAR double xmlXPathPINF;417XMLPUBVAR double xmlXPathNINF;418419/* These macros may later turn into functions */420/**421* xmlXPathNodeSetGetLength:422* @ns: a node-set423*424* Implement a functionality similar to the DOM NodeList.length.425*426* Returns the number of nodes in the node-set.427*/428#define xmlXPathNodeSetGetLength(ns) ((ns) ? (ns)->nodeNr : 0)429/**430* xmlXPathNodeSetItem:431* @ns: a node-set432* @index: index of a node in the set433*434* Implements a functionality similar to the DOM NodeList.item().435*436* Returns the xmlNodePtr at the given @index in @ns or NULL if437* @index is out of range (0 to length-1)438*/439#define xmlXPathNodeSetItem(ns, index) \440((((ns) != NULL) && \441((index) >= 0) && ((index) < (ns)->nodeNr)) ? \442(ns)->nodeTab[(index)] \443: NULL)444/**445* xmlXPathNodeSetIsEmpty:446* @ns: a node-set447*448* Checks whether @ns is empty or not.449*450* Returns %TRUE if @ns is an empty node-set.451*/452#define xmlXPathNodeSetIsEmpty(ns) \453(((ns) == NULL) || ((ns)->nodeNr == 0) || ((ns)->nodeTab == NULL))454455456XMLPUBFUN void457xmlXPathFreeObject (xmlXPathObjectPtr obj);458XMLPUBFUN xmlNodeSetPtr459xmlXPathNodeSetCreate (xmlNodePtr val);460XMLPUBFUN void461xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);462XMLPUBFUN void463xmlXPathFreeNodeSet (xmlNodeSetPtr obj);464XMLPUBFUN xmlXPathObjectPtr465xmlXPathObjectCopy (xmlXPathObjectPtr val);466XMLPUBFUN int467xmlXPathCmpNodes (xmlNodePtr node1,468xmlNodePtr node2);469/**470* Conversion functions to basic types.471*/472XMLPUBFUN int473xmlXPathCastNumberToBoolean (double val);474XMLPUBFUN int475xmlXPathCastStringToBoolean (const xmlChar * val);476XMLPUBFUN int477xmlXPathCastNodeSetToBoolean(xmlNodeSetPtr ns);478XMLPUBFUN int479xmlXPathCastToBoolean (xmlXPathObjectPtr val);480481XMLPUBFUN double482xmlXPathCastBooleanToNumber (int val);483XMLPUBFUN double484xmlXPathCastStringToNumber (const xmlChar * val);485XMLPUBFUN double486xmlXPathCastNodeToNumber (xmlNodePtr node);487XMLPUBFUN double488xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns);489XMLPUBFUN double490xmlXPathCastToNumber (xmlXPathObjectPtr val);491492XMLPUBFUN xmlChar *493xmlXPathCastBooleanToString (int val);494XMLPUBFUN xmlChar *495xmlXPathCastNumberToString (double val);496XMLPUBFUN xmlChar *497xmlXPathCastNodeToString (xmlNodePtr node);498XMLPUBFUN xmlChar *499xmlXPathCastNodeSetToString (xmlNodeSetPtr ns);500XMLPUBFUN xmlChar *501xmlXPathCastToString (xmlXPathObjectPtr val);502503XMLPUBFUN xmlXPathObjectPtr504xmlXPathConvertBoolean (xmlXPathObjectPtr val);505XMLPUBFUN xmlXPathObjectPtr506xmlXPathConvertNumber (xmlXPathObjectPtr val);507XMLPUBFUN xmlXPathObjectPtr508xmlXPathConvertString (xmlXPathObjectPtr val);509510/**511* Context handling.512*/513XMLPUBFUN xmlXPathContextPtr514xmlXPathNewContext (xmlDocPtr doc);515XMLPUBFUN void516xmlXPathFreeContext (xmlXPathContextPtr ctxt);517XMLPUBFUN int518xmlXPathContextSetCache(xmlXPathContextPtr ctxt,519int active,520int value,521int options);522/**523* Evaluation functions.524*/525XMLPUBFUN long526xmlXPathOrderDocElems (xmlDocPtr doc);527XMLPUBFUN int528xmlXPathSetContextNode (xmlNodePtr node,529xmlXPathContextPtr ctx);530XMLPUBFUN xmlXPathObjectPtr531xmlXPathNodeEval (xmlNodePtr node,532const xmlChar *str,533xmlXPathContextPtr ctx);534XMLPUBFUN xmlXPathObjectPtr535xmlXPathEval (const xmlChar *str,536xmlXPathContextPtr ctx);537XMLPUBFUN xmlXPathObjectPtr538xmlXPathEvalExpression (const xmlChar *str,539xmlXPathContextPtr ctxt);540XMLPUBFUN int541xmlXPathEvalPredicate (xmlXPathContextPtr ctxt,542xmlXPathObjectPtr res);543/**544* Separate compilation/evaluation entry points.545*/546XMLPUBFUN xmlXPathCompExprPtr547xmlXPathCompile (const xmlChar *str);548XMLPUBFUN xmlXPathCompExprPtr549xmlXPathCtxtCompile (xmlXPathContextPtr ctxt,550const xmlChar *str);551XMLPUBFUN xmlXPathObjectPtr552xmlXPathCompiledEval (xmlXPathCompExprPtr comp,553xmlXPathContextPtr ctx);554XMLPUBFUN int555xmlXPathCompiledEvalToBoolean(xmlXPathCompExprPtr comp,556xmlXPathContextPtr ctxt);557XMLPUBFUN void558xmlXPathFreeCompExpr (xmlXPathCompExprPtr comp);559#endif /* LIBXML_XPATH_ENABLED */560#if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)561XML_DEPRECATED562XMLPUBFUN void563xmlXPathInit (void);564XMLPUBFUN int565xmlXPathIsNaN (double val);566XMLPUBFUN int567xmlXPathIsInf (double val);568569#ifdef __cplusplus570}571#endif572573#endif /* LIBXML_XPATH_ENABLED or LIBXML_SCHEMAS_ENABLED*/574#endif /* ! __XML_XPATH_H__ */575576577