xpath.h

Go to the documentation of this file.
00001 /*
00002  * Summary: XML Path Language implementation
00003  * Description: API for the XML Path Language implementation
00004  *
00005  * XML Path Language implementation
00006  * XPath is a language for addressing parts of an XML document,
00007  * designed to be used by both XSLT and XPointer
00008  *     http://www.w3.org/TR/xpath
00009  *
00010  * Implements
00011  * W3C Recommendation 16 November 1999
00012  *     http://www.w3.org/TR/1999/REC-xpath-19991116
00013  *
00014  * Copy: See Copyright for the status of this software.
00015  *
00016  * Author: Daniel Veillard
00017  */
00018 
00019 #ifndef __XML_XPATH_H__
00020 #define __XML_XPATH_H__
00021 
00022 #include <libxml/xmlversion.h>
00023 
00024 #ifdef LIBXML_XPATH_ENABLED
00025 
00026 #include <libxml/xmlerror.h>
00027 #include <libxml/tree.h>
00028 #include <libxml/hash.h>
00029 #endif /* LIBXML_XPATH_ENABLED */
00030 
00031 #if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
00032 #ifdef __cplusplus
00033 extern "C" {
00034 #endif
00035 #endif /* LIBXML_XPATH_ENABLED or LIBXML_SCHEMAS_ENABLED */
00036     
00037 #ifdef LIBXML_XPATH_ENABLED
00038 
00039 typedef struct _xmlXPathContext xmlXPathContext;
00040 typedef xmlXPathContext *xmlXPathContextPtr;
00041 typedef struct _xmlXPathParserContext xmlXPathParserContext;
00042 typedef xmlXPathParserContext *xmlXPathParserContextPtr;
00043 
00048 typedef enum {
00049     XPATH_EXPRESSION_OK = 0,
00050     XPATH_NUMBER_ERROR,
00051     XPATH_UNFINISHED_LITERAL_ERROR,
00052     XPATH_START_LITERAL_ERROR,
00053     XPATH_VARIABLE_REF_ERROR,
00054     XPATH_UNDEF_VARIABLE_ERROR,
00055     XPATH_INVALID_PREDICATE_ERROR,
00056     XPATH_EXPR_ERROR,
00057     XPATH_UNCLOSED_ERROR,
00058     XPATH_UNKNOWN_FUNC_ERROR,
00059     XPATH_INVALID_OPERAND,
00060     XPATH_INVALID_TYPE,
00061     XPATH_INVALID_ARITY,
00062     XPATH_INVALID_CTXT_SIZE,
00063     XPATH_INVALID_CTXT_POSITION,
00064     XPATH_MEMORY_ERROR,
00065     XPTR_SYNTAX_ERROR,
00066     XPTR_RESOURCE_ERROR,
00067     XPTR_SUB_RESOURCE_ERROR,
00068     XPATH_UNDEF_PREFIX_ERROR,
00069     XPATH_ENCODING_ERROR,
00070     XPATH_INVALID_CHAR_ERROR,
00071     XPATH_INVALID_CTXT
00072 } xmlXPathError;
00073 
00074 /*
00075  * A node-set (an unordered collection of nodes without duplicates).
00076  */
00077 typedef struct _xmlNodeSet xmlNodeSet;
00078 typedef xmlNodeSet *xmlNodeSetPtr;
00079 struct _xmlNodeSet {
00080     int nodeNr;         /* number of nodes in the set */
00081     int nodeMax;        /* size of the array as allocated */
00082     xmlNodePtr *nodeTab;    /* array of nodes in no particular order */
00083     /* @@ with_ns to check wether namespace nodes should be looked at @@ */
00084 };
00085 
00086 /*
00087  * An expression is evaluated to yield an object, which
00088  * has one of the following four basic types:
00089  *   - node-set
00090  *   - boolean
00091  *   - number
00092  *   - string
00093  *
00094  * @@ XPointer will add more types !
00095  */
00096 
00097 typedef enum {
00098     XPATH_UNDEFINED = 0,
00099     XPATH_NODESET = 1,
00100     XPATH_BOOLEAN = 2,
00101     XPATH_NUMBER = 3,
00102     XPATH_STRING = 4,
00103     XPATH_POINT = 5,
00104     XPATH_RANGE = 6,
00105     XPATH_LOCATIONSET = 7,
00106     XPATH_USERS = 8,
00107     XPATH_XSLT_TREE = 9  /* An XSLT value tree, non modifiable */
00108 } xmlXPathObjectType;
00109 
00110 typedef struct _xmlXPathObject xmlXPathObject;
00111 typedef xmlXPathObject *xmlXPathObjectPtr;
00112 struct _xmlXPathObject {
00113     xmlXPathObjectType type;
00114     xmlNodeSetPtr nodesetval;
00115     int boolval;
00116     double floatval;
00117     xmlChar *stringval;
00118     void *user;
00119     int index;
00120     void *user2;
00121     int index2;
00122 };
00123 
00134 typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
00135 
00136 /*
00137  * Extra type: a name and a conversion function.
00138  */
00139 
00140 typedef struct _xmlXPathType xmlXPathType;
00141 typedef xmlXPathType *xmlXPathTypePtr;
00142 struct _xmlXPathType {
00143     const xmlChar         *name;        /* the type name */
00144     xmlXPathConvertFunc func;       /* the conversion function */
00145 };
00146 
00147 /*
00148  * Extra variable: a name and a value.
00149  */
00150 
00151 typedef struct _xmlXPathVariable xmlXPathVariable;
00152 typedef xmlXPathVariable *xmlXPathVariablePtr;
00153 struct _xmlXPathVariable {
00154     const xmlChar       *name;      /* the variable name */
00155     xmlXPathObjectPtr value;        /* the value */
00156 };
00157 
00166 typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt,
00167                              int nargs);
00168 
00169 /*
00170  * Extra function: a name and a evaluation function.
00171  */
00172 
00173 typedef struct _xmlXPathFunct xmlXPathFunct;
00174 typedef xmlXPathFunct *xmlXPathFuncPtr;
00175 struct _xmlXPathFunct {
00176     const xmlChar      *name;       /* the function name */
00177     xmlXPathEvalFunc func;      /* the evaluation function */
00178 };
00179 
00192 typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
00193                  xmlXPathObjectPtr cur);
00194 
00195 /*
00196  * Extra axis: a name and an axis function.
00197  */
00198 
00199 typedef struct _xmlXPathAxis xmlXPathAxis;
00200 typedef xmlXPathAxis *xmlXPathAxisPtr;
00201 struct _xmlXPathAxis {
00202     const xmlChar      *name;       /* the axis name */
00203     xmlXPathAxisFunc func;      /* the search function */
00204 };
00205 
00216 typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
00217 
00218 /*
00219  * Function and Variable Lookup.
00220  */
00221 
00233 typedef xmlXPathObjectPtr (*xmlXPathVariableLookupFunc) (void *ctxt,
00234                                          const xmlChar *name,
00235                                          const xmlChar *ns_uri);
00236 
00248 typedef xmlXPathFunction (*xmlXPathFuncLookupFunc) (void *ctxt,
00249                      const xmlChar *name,
00250                      const xmlChar *ns_uri);
00251 
00261 #define XML_XPATH_CHECKNS (1<<0)
00262 
00267 #define XML_XPATH_NOVAR   (1<<1)
00268 
00283 struct _xmlXPathContext {
00284     xmlDocPtr doc;          /* The current document */
00285     xmlNodePtr node;            /* The current node */
00286 
00287     int nb_variables_unused;        /* unused (hash table) */
00288     int max_variables_unused;       /* unused (hash table) */
00289     xmlHashTablePtr varHash;        /* Hash table of defined variables */
00290 
00291     int nb_types;           /* number of defined types */
00292     int max_types;          /* max number of types */
00293     xmlXPathTypePtr types;      /* Array of defined types */
00294 
00295     int nb_funcs_unused;        /* unused (hash table) */
00296     int max_funcs_unused;       /* unused (hash table) */
00297     xmlHashTablePtr funcHash;       /* Hash table of defined funcs */
00298 
00299     int nb_axis;            /* number of defined axis */
00300     int max_axis;           /* max number of axis */
00301     xmlXPathAxisPtr axis;       /* Array of defined axis */
00302 
00303     /* the namespace nodes of the context node */
00304     xmlNsPtr *namespaces;       /* Array of namespaces */
00305     int nsNr;               /* number of namespace in scope */
00306     void *user;             /* function to free */
00307 
00308     /* extra variables */
00309     int contextSize;            /* the context size */
00310     int proximityPosition;      /* the proximity position */
00311 
00312     /* extra stuff for XPointer */
00313     int xptr;               /* it this an XPointer context */
00314     xmlNodePtr here;            /* for here() */
00315     xmlNodePtr origin;          /* for origin() */
00316 
00317     /* the set of namespace declarations in scope for the expression */
00318     xmlHashTablePtr nsHash;     /* The namespaces hash table */
00319     xmlXPathVariableLookupFunc varLookupFunc;/* variable lookup func */
00320     void *varLookupData;        /* variable lookup data */
00321 
00322     /* Possibility to link in an extra item */
00323     void *extra;                        /* needed for XSLT */
00324 
00325     /* The function name and URI when calling a function */
00326     const xmlChar *function;
00327     const xmlChar *functionURI;
00328 
00329     /* function lookup function and data */
00330     xmlXPathFuncLookupFunc funcLookupFunc;/* function lookup func */
00331     void *funcLookupData;       /* function lookup data */
00332 
00333     /* temporary namespace lists kept for walking the namespace axis */
00334     xmlNsPtr *tmpNsList;        /* Array of namespaces */
00335     int tmpNsNr;            /* number of namespace in scope */
00336 
00337     /* error reporting mechanism */
00338     void *userData;                     /* user specific data block */
00339     xmlStructuredErrorFunc error;       /* the callback in case of errors */
00340     xmlError lastError;         /* the last error */
00341     xmlNodePtr debugNode;       /* the source node XSLT */
00342 
00343     /* dictionnary */
00344     xmlDictPtr dict;            /* dictionnary if any */
00345 
00346     int flags;              /* flags to control compilation */
00347 
00348     /* Cache for reusal of XPath objects */
00349     void *cache;
00350 };
00351 
00352 /*
00353  * The structure of a compiled expression form is not public.
00354  */
00355 
00356 typedef struct _xmlXPathCompExpr xmlXPathCompExpr;
00357 typedef xmlXPathCompExpr *xmlXPathCompExprPtr;
00358 
00365 struct _xmlXPathParserContext {
00366     const xmlChar *cur;         /* the current char being parsed */
00367     const xmlChar *base;            /* the full expression */
00368 
00369     int error;              /* error code */
00370 
00371     xmlXPathContextPtr  context;    /* the evaluation context */
00372     xmlXPathObjectPtr     value;    /* the current value */
00373     int                 valueNr;    /* number of values stacked */
00374     int                valueMax;    /* max number of values stacked */
00375     xmlXPathObjectPtr *valueTab;    /* stack of values */
00376 
00377     xmlXPathCompExprPtr comp;       /* the precompiled expression */
00378     int xptr;               /* it this an XPointer expression */
00379     xmlNodePtr         ancestor;    /* used for walking preceding axis */
00380 };
00381 
00382 /************************************************************************
00383  *                                  *
00384  *          Public API                  *
00385  *                                  *
00386  ************************************************************************/
00387 
00392 XMLPUBVAR double xmlXPathNAN;
00393 XMLPUBVAR double xmlXPathPINF;
00394 XMLPUBVAR double xmlXPathNINF;
00395 
00396 /* These macros may later turn into functions */
00405 #define xmlXPathNodeSetGetLength(ns) ((ns) ? (ns)->nodeNr : 0)
00406 
00416 #define xmlXPathNodeSetItem(ns, index)              \
00417         ((((ns) != NULL) &&                 \
00418           ((index) >= 0) && ((index) < (ns)->nodeNr)) ? \
00419          (ns)->nodeTab[(index)]             \
00420          : NULL)
00421 
00429 #define xmlXPathNodeSetIsEmpty(ns)                                      \
00430     (((ns) == NULL) || ((ns)->nodeNr == 0) || ((ns)->nodeTab == NULL))
00431 
00432 
00433 XMLPUBFUN void XMLCALL         
00434             xmlXPathFreeObject      (xmlXPathObjectPtr obj);
00435 XMLPUBFUN xmlNodeSetPtr XMLCALL    
00436             xmlXPathNodeSetCreate   (xmlNodePtr val);
00437 XMLPUBFUN void XMLCALL         
00438             xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
00439 XMLPUBFUN void XMLCALL         
00440             xmlXPathFreeNodeSet     (xmlNodeSetPtr obj);
00441 XMLPUBFUN xmlXPathObjectPtr XMLCALL  
00442             xmlXPathObjectCopy      (xmlXPathObjectPtr val);
00443 XMLPUBFUN int XMLCALL          
00444             xmlXPathCmpNodes        (xmlNodePtr node1,
00445                          xmlNodePtr node2);
00449 XMLPUBFUN int XMLCALL          
00450             xmlXPathCastNumberToBoolean (double val);
00451 XMLPUBFUN int XMLCALL          
00452             xmlXPathCastStringToBoolean (const xmlChar * val);
00453 XMLPUBFUN int XMLCALL          
00454             xmlXPathCastNodeSetToBoolean(xmlNodeSetPtr ns);
00455 XMLPUBFUN int XMLCALL          
00456             xmlXPathCastToBoolean   (xmlXPathObjectPtr val);
00457 
00458 XMLPUBFUN double XMLCALL           
00459             xmlXPathCastBooleanToNumber (int val);
00460 XMLPUBFUN double XMLCALL           
00461             xmlXPathCastStringToNumber  (const xmlChar * val);
00462 XMLPUBFUN double XMLCALL           
00463             xmlXPathCastNodeToNumber    (xmlNodePtr node);
00464 XMLPUBFUN double XMLCALL           
00465             xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns);
00466 XMLPUBFUN double XMLCALL           
00467             xmlXPathCastToNumber    (xmlXPathObjectPtr val);
00468 
00469 XMLPUBFUN xmlChar * XMLCALL    
00470             xmlXPathCastBooleanToString (int val);
00471 XMLPUBFUN xmlChar * XMLCALL    
00472             xmlXPathCastNumberToString  (double val);
00473 XMLPUBFUN xmlChar * XMLCALL    
00474             xmlXPathCastNodeToString    (xmlNodePtr node);
00475 XMLPUBFUN xmlChar * XMLCALL    
00476             xmlXPathCastNodeSetToString (xmlNodeSetPtr ns);
00477 XMLPUBFUN xmlChar * XMLCALL    
00478             xmlXPathCastToString    (xmlXPathObjectPtr val);
00479 
00480 XMLPUBFUN xmlXPathObjectPtr XMLCALL  
00481             xmlXPathConvertBoolean  (xmlXPathObjectPtr val);
00482 XMLPUBFUN xmlXPathObjectPtr XMLCALL  
00483             xmlXPathConvertNumber   (xmlXPathObjectPtr val);
00484 XMLPUBFUN xmlXPathObjectPtr XMLCALL  
00485             xmlXPathConvertString   (xmlXPathObjectPtr val);
00486 
00490 XMLPUBFUN xmlXPathContextPtr XMLCALL 
00491             xmlXPathNewContext      (xmlDocPtr doc);
00492 XMLPUBFUN void XMLCALL
00493             xmlXPathFreeContext     (xmlXPathContextPtr ctxt);
00494 XMLPUBFUN int XMLCALL
00495             xmlXPathContextSetCache(xmlXPathContextPtr ctxt,
00496                             int active,
00497                         int value,
00498                         int options);
00502 XMLPUBFUN long XMLCALL               
00503             xmlXPathOrderDocElems   (xmlDocPtr doc);
00504 XMLPUBFUN xmlXPathObjectPtr XMLCALL  
00505             xmlXPathEval        (const xmlChar *str,
00506                          xmlXPathContextPtr ctx);
00507 XMLPUBFUN xmlXPathObjectPtr XMLCALL  
00508             xmlXPathEvalExpression  (const xmlChar *str,
00509                          xmlXPathContextPtr ctxt);
00510 XMLPUBFUN int XMLCALL                
00511             xmlXPathEvalPredicate   (xmlXPathContextPtr ctxt,
00512                          xmlXPathObjectPtr res);
00516 XMLPUBFUN xmlXPathCompExprPtr XMLCALL 
00517             xmlXPathCompile     (const xmlChar *str);
00518 XMLPUBFUN xmlXPathCompExprPtr XMLCALL 
00519             xmlXPathCtxtCompile     (xmlXPathContextPtr ctxt,
00520                              const xmlChar *str);
00521 XMLPUBFUN xmlXPathObjectPtr XMLCALL   
00522             xmlXPathCompiledEval    (xmlXPathCompExprPtr comp,
00523                          xmlXPathContextPtr ctx);
00524 XMLPUBFUN int XMLCALL   
00525             xmlXPathCompiledEvalToBoolean(xmlXPathCompExprPtr comp,
00526                          xmlXPathContextPtr ctxt);
00527 XMLPUBFUN void XMLCALL                
00528             xmlXPathFreeCompExpr    (xmlXPathCompExprPtr comp);
00529 #endif /* LIBXML_XPATH_ENABLED */
00530 #if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
00531 XMLPUBFUN void XMLCALL         
00532             xmlXPathInit        (void);
00533 XMLPUBFUN int XMLCALL
00534         xmlXPathIsNaN   (double val);
00535 XMLPUBFUN int XMLCALL
00536         xmlXPathIsInf   (double val);
00537 
00538 #ifdef __cplusplus
00539 }
00540 #endif
00541 
00542 #endif /* LIBXML_XPATH_ENABLED or LIBXML_SCHEMAS_ENABLED*/
00543 #endif /* ! __XML_XPATH_H__ */
footer
 SourceForge.net Logo