Annotation of XML/xpath.h, revision 1.28

1.1       daniel      1: /*
                      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: 
1.16      daniel     15: #include <libxml/tree.h>
1.12      daniel     16: 
1.11      daniel     17: #ifdef __cplusplus
1.13      daniel     18: extern "C" {
1.11      daniel     19: #endif
1.1       daniel     20: 
1.15      daniel     21: typedef struct _xmlXPathContext xmlXPathContext;
                     22: typedef xmlXPathContext *xmlXPathContextPtr;
                     23: typedef struct _xmlXPathParserContext xmlXPathParserContext;
                     24: typedef xmlXPathParserContext *xmlXPathParserContextPtr;
1.9       daniel     25: 
1.23      veillard   26: /**
                     27:  * The set of XPath error codes
                     28:  */
                     29: 
                     30: typedef enum {
                     31:     XPATH_EXPRESSION_OK = 0,
                     32:     XPATH_NUMBER_ERROR,
                     33:     XPATH_UNFINISHED_LITERAL_ERROR,
                     34:     XPATH_START_LITERAL_ERROR,
                     35:     XPATH_VARIABLE_REF_ERROR,
                     36:     XPATH_UNDEF_VARIABLE_ERROR,
                     37:     XPATH_INVALID_PREDICATE_ERROR,
                     38:     XPATH_EXPR_ERROR,
                     39:     XPATH_UNCLOSED_ERROR,
                     40:     XPATH_UNKNOWN_FUNC_ERROR,
                     41:     XPATH_INVALID_OPERAND,
                     42:     XPATH_INVALID_TYPE,
                     43:     XPATH_INVALID_ARITY,
                     44:     XPATH_INVALID_CTXT_SIZE,
1.25      veillard   45:     XPATH_INVALID_CTXT_POSITION,
                     46:     XPATH_MEMORY_ERROR,
                     47:     XPTR_SYNTAX_ERROR,
                     48:     XPTR_RESOURCE_ERROR,
                     49:     XPTR_SUB_RESOURCE_ERROR
1.23      veillard   50: } xmlXPathError;
                     51: 
1.1       daniel     52: /*
                     53:  * A node-set (an unordered collection of nodes without duplicates) 
                     54:  */
1.15      daniel     55: typedef struct _xmlNodeSet xmlNodeSet;
                     56: typedef xmlNodeSet *xmlNodeSetPtr;
                     57: struct _xmlNodeSet {
1.17      daniel     58:     int nodeNr;                        /* number of nodes in the set */
                     59:     int nodeMax;               /* size of the array as allocated */
1.1       daniel     60:     xmlNodePtr *nodeTab;       /* array of nodes in no particular order */
1.15      daniel     61: };
1.1       daniel     62: 
                     63: /*
                     64:  * An expression is evaluated to yield an object, which
                     65:  * has one of the following four basic types:
                     66:  *   - node-set
                     67:  *   - boolean
                     68:  *   - number
                     69:  *   - string
1.17      daniel     70:  *
                     71:  * @@ XPointer will add more types !
1.1       daniel     72:  */
                     73: 
1.18      veillard   74: typedef enum {
                     75:     XPATH_UNDEFINED = 0,
                     76:     XPATH_NODESET = 1,
                     77:     XPATH_BOOLEAN = 2,
                     78:     XPATH_NUMBER = 3,
                     79:     XPATH_STRING = 4,
1.20      veillard   80:     XPATH_POINT = 5,
                     81:     XPATH_RANGE = 6,
1.21      veillard   82:     XPATH_LOCATIONSET = 7,
1.20      veillard   83:     XPATH_USERS = 8
1.18      veillard   84: } xmlXPathObjectType;
1.1       daniel     85: 
1.15      daniel     86: typedef struct _xmlXPathObject xmlXPathObject;
                     87: typedef xmlXPathObject *xmlXPathObjectPtr;
                     88: struct _xmlXPathObject {
1.18      veillard   89:     xmlXPathObjectType type;
1.1       daniel     90:     xmlNodeSetPtr nodesetval;
                     91:     int boolval;
1.6       daniel     92:     double floatval;
1.10      daniel     93:     xmlChar *stringval;
1.9       daniel     94:     void *user;
1.20      veillard   95:     int index;
                     96:     void *user2;
                     97:     int index2;
1.15      daniel     98: };
1.1       daniel     99: 
1.9       daniel    100: /*
                    101:  * A conversion function is associated to a type and used to cast
                    102:  * the new type to primitive values.
                    103:  */
                    104: typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
                    105: 
                    106: /*
                    107:  * Extra type: a name and a conversion function.
                    108:  */
                    109: 
1.15      daniel    110: typedef struct _xmlXPathType xmlXPathType;
                    111: typedef xmlXPathType *xmlXPathTypePtr;
                    112: struct _xmlXPathType {
1.10      daniel    113:     const xmlChar         *name;               /* the type name */
1.9       daniel    114:     xmlXPathConvertFunc func;          /* the conversion function */
1.15      daniel    115: };
1.9       daniel    116: 
                    117: /*
                    118:  * Extra variable: a name and a value.
                    119:  */
                    120: 
1.15      daniel    121: typedef struct _xmlXPathVariable xmlXPathVariable;
                    122: typedef xmlXPathVariable *xmlXPathVariablePtr;
                    123: struct _xmlXPathVariable {
1.10      daniel    124:     const xmlChar       *name;         /* the variable name */
1.9       daniel    125:     xmlXPathObjectPtr value;           /* the value */
1.15      daniel    126: };
1.9       daniel    127: 
                    128: /*
                    129:  * an evaluation function, the parameters are on the context stack
                    130:  */
                    131: 
                    132: typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt, int nargs);
                    133: 
                    134: /*
                    135:  * Extra function: a name and a evaluation function.
                    136:  */
                    137: 
1.15      daniel    138: typedef struct _xmlXPathFunct xmlXPathFunct;
                    139: typedef xmlXPathFunct *xmlXPathFuncPtr;
                    140: struct _xmlXPathFunct {
1.10      daniel    141:     const xmlChar      *name;          /* the function name */
1.9       daniel    142:     xmlXPathEvalFunc func;             /* the evaluation function */
1.15      daniel    143: };
1.9       daniel    144: 
                    145: /*
                    146:  * An axis traversal function. To traverse an axis, the engine calls
                    147:  * the first time with cur == NULL and repeat until the function returns
                    148:  * NULL indicating the end of the axis traversal.
                    149:  */
                    150: 
                    151: typedef xmlXPathObjectPtr (*xmlXPathAxisFunc)  (xmlXPathParserContextPtr ctxt,
                    152:                                                 xmlXPathObjectPtr cur);
                    153: 
                    154: /*
                    155:  * Extra axis: a name and an axis function.
                    156:  */
                    157: 
1.15      daniel    158: typedef struct _xmlXPathAxis xmlXPathAxis;
                    159: typedef xmlXPathAxis *xmlXPathAxisPtr;
                    160: struct _xmlXPathAxis {
1.10      daniel    161:     const xmlChar      *name;          /* the axis name */
1.9       daniel    162:     xmlXPathAxisFunc func;             /* the search function */
1.15      daniel    163: };
1.9       daniel    164: 
1.1       daniel    165: /* 
                    166:  * Expression evaluation occurs with respect to a context.
                    167:  * he context consists of:
                    168:  *    - a node (the context node) 
                    169:  *    - a node list (the context node list) 
                    170:  *    - a set of variable bindings 
                    171:  *    - a function library 
                    172:  *    - the set of namespace declarations in scope for the expression 
                    173:  */
                    174: 
1.15      daniel    175: struct _xmlXPathContext {
1.2       daniel    176:     xmlDocPtr doc;                     /* The current document */
                    177:     xmlNodePtr node;                   /* The current node */
1.9       daniel    178: 
                    179:     int nb_variables;                  /* number of defined variables */
                    180:     int max_variables;                 /* max number of variables */
1.27      veillard  181:     xmlXPathVariablePtr variables;     /* Array of defined variables */
1.9       daniel    182: 
                    183:     int nb_types;                      /* number of defined types */
                    184:     int max_types;                     /* max number of types */
1.27      veillard  185:     xmlXPathTypePtr types;             /* Array of defined types */
1.9       daniel    186: 
                    187:     int nb_funcs;                      /* number of defined funcs */
                    188:     int max_funcs;                     /* max number of funcs */
1.27      veillard  189:     xmlXPathFuncPtr funcs;             /* Array of defined funcs */
1.9       daniel    190: 
                    191:     int nb_axis;                       /* number of defined axis */
                    192:     int max_axis;                      /* max number of axis */
1.27      veillard  193:     xmlXPathAxisPtr axis;              /* Array of defined axis */
1.9       daniel    194: 
                    195:     /* Namespace traversal should be implemented with user */
1.8       daniel    196:     xmlNsPtr *namespaces;              /* The namespaces lookup */
                    197:     int nsNr;                          /* the current Namespace index */
1.9       daniel    198:     void *user;                                /* user defined extra info */
1.18      veillard  199: 
                    200:     /* extra variables */
                    201:     int contextSize;                   /* the context size */
                    202:     int proximityPosition;             /* the proximity position */
1.21      veillard  203: 
                    204:     /* extra stuff for XPointer */
                    205:     int xptr;                          /* it this an XPointer context */
                    206:     xmlNodePtr here;                   /* for here() */
                    207:     xmlNodePtr origin;                 /* for origin() */
1.15      daniel    208: };
1.1       daniel    209: 
                    210: /*
                    211:  * An XPath parser context, it contains pure parsing informations,
                    212:  * an xmlXPathContext, and the stack of objects.
                    213:  */
1.15      daniel    214: struct _xmlXPathParserContext {
1.10      daniel    215:     const xmlChar *cur;                        /* the current char being parsed */
                    216:     const xmlChar *base;                       /* the full expression */
1.1       daniel    217: 
                    218:     int error;                         /* error code */
                    219: 
                    220:     xmlXPathContextPtr  context;       /* the evaluation context */
                    221:     xmlXPathObjectPtr     value;       /* the current value */
                    222:     int                 valueNr;       /* number of values stacked */
                    223:     int                valueMax;       /* max number of values stacked */
                    224:     xmlXPathObjectPtr *valueTab;       /* stack of values */
1.15      daniel    225: };
1.1       daniel    226: 
                    227: /*
                    228:  * An XPath function
                    229:  * The arguments (if any) are popped out of the context stack
                    230:  * and the result is pushed on the stack.
                    231:  */
                    232: 
1.4       daniel    233: typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
1.1       daniel    234: 
                    235: /************************************************************************
                    236:  *                                                                     *
1.22      veillard  237:  *                     Helpers                                         *
                    238:  *                                                                     *
                    239:  ************************************************************************/
                    240: 
                    241: #define CHECK_ERROR                                                    \
                    242:     if (ctxt->error != XPATH_EXPRESSION_OK) return
                    243: 
1.23      veillard  244: #define CHECK_ERROR0                                                   \
                    245:     if (ctxt->error != XPATH_EXPRESSION_OK) return(0)
                    246: 
1.22      veillard  247: #define XP_ERROR(X)                                                    \
                    248:     { xmlXPatherror(ctxt, __FILE__, __LINE__, X);                      \
                    249:       ctxt->error = (X); return; }
                    250: 
                    251: #define XP_ERROR0(X)                                                   \
                    252:     { xmlXPatherror(ctxt, __FILE__, __LINE__, X);                      \
                    253:       ctxt->error = (X); return(0); }
                    254: 
                    255: #define CHECK_TYPE(typeval)                                            \
                    256:     if ((ctxt->value == NULL) || (ctxt->value->type != typeval))       \
                    257:         XP_ERROR(XPATH_INVALID_TYPE)                                   \
1.23      veillard  258: 
                    259: #define CHECK_ARITY(x)                                                 \
                    260:     if (nargs != (x)) {                                                        \
                    261:         XP_ERROR(XPATH_INVALID_ARITY);                                 \
                    262:     }                                                                  \
1.22      veillard  263: 
                    264: void           xmlXPatherror   (xmlXPathParserContextPtr ctxt,
                    265:                                 const char *file,
                    266:                                 int line,
                    267:                                 int no);
                    268: 
1.28    ! veillard  269: void           xmlXPathDebugDumpObject (FILE *output,
        !           270:                                         xmlXPathObjectPtr cur,
        !           271:                                         int depth);
        !           272: 
1.22      veillard  273: /**
1.25      veillard  274:  * Utilities to extend XPath (XPointer)
1.22      veillard  275:  */
1.25      veillard  276: xmlXPathParserContextPtr
                    277:                  xmlXPathNewParserContext      (const xmlChar *str,
                    278:                                                 xmlXPathContextPtr ctxt);
                    279: void             xmlXPathFreeParserContext     (xmlXPathParserContextPtr ctxt);
                    280: 
1.22      veillard  281: xmlXPathObjectPtr valuePop                     (xmlXPathParserContextPtr ctxt);
                    282: int              valuePush                     (xmlXPathParserContextPtr ctxt,
1.24      veillard  283:                                                xmlXPathObjectPtr value);
                    284: 
                    285: xmlXPathObjectPtr xmlXPathNewString            (const xmlChar *val);
                    286: xmlXPathObjectPtr xmlXPathNewNodeSet           (xmlNodePtr val);
                    287: void             xmlXPathNodeSetAdd            (xmlNodeSetPtr cur,
                    288:                                                 xmlNodePtr val);
                    289: 
                    290: 
                    291: void             xmlXPathIdFunction            (xmlXPathParserContextPtr ctxt,
                    292:                                                int nargs);
                    293: void             xmlXPathRoot                  (xmlXPathParserContextPtr ctxt);
                    294: void             xmlXPathEvalExpr              (xmlXPathParserContextPtr ctxt);
1.26      veillard  295: xmlChar *        xmlXPathParseName             (xmlXPathParserContextPtr ctxt);
1.22      veillard  296: 
                    297: /************************************************************************
                    298:  *                                                                     *
1.1       daniel    299:  *                     Public API                                      *
                    300:  *                                                                     *
                    301:  ************************************************************************/
                    302: 
1.9       daniel    303: /**
1.26      veillard  304:  * Extending a context
1.9       daniel    305:  */
1.26      veillard  306: int               xmlXPathRegisterFunc         (xmlXPathContextPtr ctxt,
1.10      daniel    307:                                                 const xmlChar *name,
1.9       daniel    308:                                                 xmlXPathFunction f);
1.26      veillard  309: int               xmlXPathRegisterVariable     (xmlXPathContextPtr ctxt,
1.10      daniel    310:                                                 const xmlChar *name,
1.26      veillard  311:                                                 xmlXPathObjectPtr value);
1.9       daniel    312: 
                    313: /**
                    314:  * Evaluation functions.
                    315:  */
1.21      veillard  316: void              xmlXPathInit                 (void);
1.9       daniel    317: xmlXPathContextPtr xmlXPathNewContext          (xmlDocPtr doc);
1.8       daniel    318: void              xmlXPathFreeContext          (xmlXPathContextPtr ctxt);
1.10      daniel    319: xmlXPathObjectPtr  xmlXPathEval                        (const xmlChar *str,
1.21      veillard  320:                                                 xmlXPathContextPtr ctxt);
                    321: xmlXPathObjectPtr  xmlXPathEvalXPtrExpr                (const xmlChar *str,
1.8       daniel    322:                                                 xmlXPathContextPtr ctxt);
                    323: void              xmlXPathFreeObject           (xmlXPathObjectPtr obj);
1.10      daniel    324: xmlXPathObjectPtr  xmlXPathEvalExpression      (const xmlChar *str,
1.8       daniel    325:                                                 xmlXPathContextPtr ctxt);
1.14      daniel    326: xmlNodeSetPtr     xmlXPathNodeSetCreate        (xmlNodePtr val);
                    327: void              xmlXPathFreeNodeSetList      (xmlXPathObjectPtr obj);
                    328: void              xmlXPathFreeNodeSet          (xmlNodeSetPtr obj);
1.26      veillard  329: xmlXPathObjectPtr  xmlXPathObjectCopy          (xmlXPathObjectPtr val);
1.22      veillard  330: 
1.3       daniel    331: 
1.11      daniel    332: #ifdef __cplusplus
                    333: }
                    334: #endif
1.1       daniel    335: #endif /* ! __XML_XPATH_H__ */

Webmaster