xpath.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Summary: XML Path Language implementation
  3. * Description: API for the XML Path Language implementation
  4. *
  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
  9. *
  10. * Implements
  11. * W3C Recommendation 16 November 1999
  12. * http://www.w3.org/TR/1999/REC-xpath-19991116
  13. *
  14. * Copy: See Copyright for the status of this software.
  15. *
  16. * Author: Daniel Veillard
  17. */
  18. #ifndef __XML_XPATH_H__
  19. #define __XML_XPATH_H__
  20. #include <libxml/xmlversion.h>
  21. #ifdef LIBXML_XPATH_ENABLED
  22. #include <libxml/xmlerror.h>
  23. #include <libxml/tree.h>
  24. #include <libxml/hash.h>
  25. #endif /* LIBXML_XPATH_ENABLED */
  26. #if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #endif /* LIBXML_XPATH_ENABLED or LIBXML_SCHEMAS_ENABLED */
  31. #ifdef LIBXML_XPATH_ENABLED
  32. typedef struct _xmlXPathContext xmlXPathContext;
  33. typedef xmlXPathContext *xmlXPathContextPtr;
  34. typedef struct _xmlXPathParserContext xmlXPathParserContext;
  35. typedef xmlXPathParserContext *xmlXPathParserContextPtr;
  36. /**
  37. * The set of XPath error codes.
  38. */
  39. typedef enum {
  40. XPATH_EXPRESSION_OK = 0,
  41. XPATH_NUMBER_ERROR,
  42. XPATH_UNFINISHED_LITERAL_ERROR,
  43. XPATH_START_LITERAL_ERROR,
  44. XPATH_VARIABLE_REF_ERROR,
  45. XPATH_UNDEF_VARIABLE_ERROR,
  46. XPATH_INVALID_PREDICATE_ERROR,
  47. XPATH_EXPR_ERROR,
  48. XPATH_UNCLOSED_ERROR,
  49. XPATH_UNKNOWN_FUNC_ERROR,
  50. XPATH_INVALID_OPERAND,
  51. XPATH_INVALID_TYPE,
  52. XPATH_INVALID_ARITY,
  53. XPATH_INVALID_CTXT_SIZE,
  54. XPATH_INVALID_CTXT_POSITION,
  55. XPATH_MEMORY_ERROR,
  56. XPTR_SYNTAX_ERROR,
  57. XPTR_RESOURCE_ERROR,
  58. XPTR_SUB_RESOURCE_ERROR,
  59. XPATH_UNDEF_PREFIX_ERROR,
  60. XPATH_ENCODING_ERROR,
  61. XPATH_INVALID_CHAR_ERROR,
  62. XPATH_INVALID_CTXT
  63. } xmlXPathError;
  64. /*
  65. * A node-set (an unordered collection of nodes without duplicates).
  66. */
  67. typedef struct _xmlNodeSet xmlNodeSet;
  68. typedef xmlNodeSet *xmlNodeSetPtr;
  69. struct _xmlNodeSet {
  70. int nodeNr; /* number of nodes in the set */
  71. int nodeMax; /* size of the array as allocated */
  72. xmlNodePtr *nodeTab; /* array of nodes in no particular order */
  73. /* @@ with_ns to check wether namespace nodes should be looked at @@ */
  74. };
  75. /*
  76. * An expression is evaluated to yield an object, which
  77. * has one of the following four basic types:
  78. * - node-set
  79. * - boolean
  80. * - number
  81. * - string
  82. *
  83. * @@ XPointer will add more types !
  84. */
  85. typedef enum {
  86. XPATH_UNDEFINED = 0,
  87. XPATH_NODESET = 1,
  88. XPATH_BOOLEAN = 2,
  89. XPATH_NUMBER = 3,
  90. XPATH_STRING = 4,
  91. XPATH_POINT = 5,
  92. XPATH_RANGE = 6,
  93. XPATH_LOCATIONSET = 7,
  94. XPATH_USERS = 8,
  95. XPATH_XSLT_TREE = 9 /* An XSLT value tree, non modifiable */
  96. } xmlXPathObjectType;
  97. typedef struct _xmlXPathObject xmlXPathObject;
  98. typedef xmlXPathObject *xmlXPathObjectPtr;
  99. struct _xmlXPathObject {
  100. xmlXPathObjectType type;
  101. xmlNodeSetPtr nodesetval;
  102. int boolval;
  103. double floatval;
  104. xmlChar *stringval;
  105. void *user;
  106. int index;
  107. void *user2;
  108. int index2;
  109. };
  110. /**
  111. * xmlXPathConvertFunc:
  112. * @obj: an XPath object
  113. * @type: the number of the target type
  114. *
  115. * A conversion function is associated to a type and used to cast
  116. * the new type to primitive values.
  117. *
  118. * Returns -1 in case of error, 0 otherwise
  119. */
  120. typedef int (*xmlXPathConvertFunc) (xmlXPathObjectPtr obj, int type);
  121. /*
  122. * Extra type: a name and a conversion function.
  123. */
  124. typedef struct _xmlXPathType xmlXPathType;
  125. typedef xmlXPathType *xmlXPathTypePtr;
  126. struct _xmlXPathType {
  127. const xmlChar *name; /* the type name */
  128. xmlXPathConvertFunc func; /* the conversion function */
  129. };
  130. /*
  131. * Extra variable: a name and a value.
  132. */
  133. typedef struct _xmlXPathVariable xmlXPathVariable;
  134. typedef xmlXPathVariable *xmlXPathVariablePtr;
  135. struct _xmlXPathVariable {
  136. const xmlChar *name; /* the variable name */
  137. xmlXPathObjectPtr value; /* the value */
  138. };
  139. /**
  140. * xmlXPathEvalFunc:
  141. * @ctxt: an XPath parser context
  142. * @nargs: the number of arguments passed to the function
  143. *
  144. * An XPath evaluation function, the parameters are on the XPath context stack.
  145. */
  146. typedef void (*xmlXPathEvalFunc)(xmlXPathParserContextPtr ctxt,
  147. int nargs);
  148. /*
  149. * Extra function: a name and a evaluation function.
  150. */
  151. typedef struct _xmlXPathFunct xmlXPathFunct;
  152. typedef xmlXPathFunct *xmlXPathFuncPtr;
  153. struct _xmlXPathFunct {
  154. const xmlChar *name; /* the function name */
  155. xmlXPathEvalFunc func; /* the evaluation function */
  156. };
  157. /**
  158. * xmlXPathAxisFunc:
  159. * @ctxt: the XPath interpreter context
  160. * @cur: the previous node being explored on that axis
  161. *
  162. * An axis traversal function. To traverse an axis, the engine calls
  163. * the first time with cur == NULL and repeat until the function returns
  164. * NULL indicating the end of the axis traversal.
  165. *
  166. * Returns the next node in that axis or NULL if at the end of the axis.
  167. */
  168. typedef xmlXPathObjectPtr (*xmlXPathAxisFunc) (xmlXPathParserContextPtr ctxt,
  169. xmlXPathObjectPtr cur);
  170. /*
  171. * Extra axis: a name and an axis function.
  172. */
  173. typedef struct _xmlXPathAxis xmlXPathAxis;
  174. typedef xmlXPathAxis *xmlXPathAxisPtr;
  175. struct _xmlXPathAxis {
  176. const xmlChar *name; /* the axis name */
  177. xmlXPathAxisFunc func; /* the search function */
  178. };
  179. /**
  180. * xmlXPathFunction:
  181. * @ctxt: the XPath interprestation context
  182. * @nargs: the number of arguments
  183. *
  184. * An XPath function.
  185. * The arguments (if any) are popped out from the context stack
  186. * and the result is pushed on the stack.
  187. */
  188. typedef void (*xmlXPathFunction) (xmlXPathParserContextPtr ctxt, int nargs);
  189. /*
  190. * Function and Variable Lookup.
  191. */
  192. /**
  193. * xmlXPathVariableLookupFunc:
  194. * @ctxt: an XPath context
  195. * @name: name of the variable
  196. * @ns_uri: the namespace name hosting this variable
  197. *
  198. * Prototype for callbacks used to plug variable lookup in the XPath
  199. * engine.
  200. *
  201. * Returns the XPath object value or NULL if not found.
  202. */
  203. typedef xmlXPathObjectPtr (*xmlXPathVariableLookupFunc) (void *ctxt,
  204. const xmlChar *name,
  205. const xmlChar *ns_uri);
  206. /**
  207. * xmlXPathFuncLookupFunc:
  208. * @ctxt: an XPath context
  209. * @name: name of the function
  210. * @ns_uri: the namespace name hosting this function
  211. *
  212. * Prototype for callbacks used to plug function lookup in the XPath
  213. * engine.
  214. *
  215. * Returns the XPath function or NULL if not found.
  216. */
  217. typedef xmlXPathFunction (*xmlXPathFuncLookupFunc) (void *ctxt,
  218. const xmlChar *name,
  219. const xmlChar *ns_uri);
  220. /**
  221. * xmlXPathFlags:
  222. * Flags for XPath engine compilation and runtime
  223. */
  224. /**
  225. * XML_XPATH_CHECKNS:
  226. *
  227. * check namespaces at compilation
  228. */
  229. #define XML_XPATH_CHECKNS (1<<0)
  230. /**
  231. * XML_XPATH_NOVAR:
  232. *
  233. * forbid variables in expression
  234. */
  235. #define XML_XPATH_NOVAR (1<<1)
  236. /**
  237. * xmlXPathContext:
  238. *
  239. * Expression evaluation occurs with respect to a context.
  240. * he context consists of:
  241. * - a node (the context node)
  242. * - a node list (the context node list)
  243. * - a set of variable bindings
  244. * - a function library
  245. * - the set of namespace declarations in scope for the expression
  246. * Following the switch to hash tables, this need to be trimmed up at
  247. * the next binary incompatible release.
  248. * The node may be modified when the context is passed to libxml2
  249. * for an XPath evaluation so you may need to initialize it again
  250. * before the next call.
  251. */
  252. struct _xmlXPathContext {
  253. xmlDocPtr doc; /* The current document */
  254. xmlNodePtr node; /* The current node */
  255. int nb_variables_unused; /* unused (hash table) */
  256. int max_variables_unused; /* unused (hash table) */
  257. xmlHashTablePtr varHash; /* Hash table of defined variables */
  258. int nb_types; /* number of defined types */
  259. int max_types; /* max number of types */
  260. xmlXPathTypePtr types; /* Array of defined types */
  261. int nb_funcs_unused; /* unused (hash table) */
  262. int max_funcs_unused; /* unused (hash table) */
  263. xmlHashTablePtr funcHash; /* Hash table of defined funcs */
  264. int nb_axis; /* number of defined axis */
  265. int max_axis; /* max number of axis */
  266. xmlXPathAxisPtr axis; /* Array of defined axis */
  267. /* the namespace nodes of the context node */
  268. xmlNsPtr *namespaces; /* Array of namespaces */
  269. int nsNr; /* number of namespace in scope */
  270. void *user; /* function to free */
  271. /* extra variables */
  272. int contextSize; /* the context size */
  273. int proximityPosition; /* the proximity position */
  274. /* extra stuff for XPointer */
  275. int xptr; /* is this an XPointer context? */
  276. xmlNodePtr here; /* for here() */
  277. xmlNodePtr origin; /* for origin() */
  278. /* the set of namespace declarations in scope for the expression */
  279. xmlHashTablePtr nsHash; /* The namespaces hash table */
  280. xmlXPathVariableLookupFunc varLookupFunc;/* variable lookup func */
  281. void *varLookupData; /* variable lookup data */
  282. /* Possibility to link in an extra item */
  283. void *extra; /* needed for XSLT */
  284. /* The function name and URI when calling a function */
  285. const xmlChar *function;
  286. const xmlChar *functionURI;
  287. /* function lookup function and data */
  288. xmlXPathFuncLookupFunc funcLookupFunc;/* function lookup func */
  289. void *funcLookupData; /* function lookup data */
  290. /* temporary namespace lists kept for walking the namespace axis */
  291. xmlNsPtr *tmpNsList; /* Array of namespaces */
  292. int tmpNsNr; /* number of namespaces in scope */
  293. /* error reporting mechanism */
  294. void *userData; /* user specific data block */
  295. xmlStructuredErrorFunc error; /* the callback in case of errors */
  296. xmlError lastError; /* the last error */
  297. xmlNodePtr debugNode; /* the source node XSLT */
  298. /* dictionary */
  299. xmlDictPtr dict; /* dictionary if any */
  300. int flags; /* flags to control compilation */
  301. /* Cache for reusal of XPath objects */
  302. void *cache;
  303. };
  304. /*
  305. * The structure of a compiled expression form is not public.
  306. */
  307. typedef struct _xmlXPathCompExpr xmlXPathCompExpr;
  308. typedef xmlXPathCompExpr *xmlXPathCompExprPtr;
  309. /**
  310. * xmlXPathParserContext:
  311. *
  312. * An XPath parser context. It contains pure parsing informations,
  313. * an xmlXPathContext, and the stack of objects.
  314. */
  315. struct _xmlXPathParserContext {
  316. const xmlChar *cur; /* the current char being parsed */
  317. const xmlChar *base; /* the full expression */
  318. int error; /* error code */
  319. xmlXPathContextPtr context; /* the evaluation context */
  320. xmlXPathObjectPtr value; /* the current value */
  321. int valueNr; /* number of values stacked */
  322. int valueMax; /* max number of values stacked */
  323. xmlXPathObjectPtr *valueTab; /* stack of values */
  324. xmlXPathCompExprPtr comp; /* the precompiled expression */
  325. int xptr; /* it this an XPointer expression */
  326. xmlNodePtr ancestor; /* used for walking preceding axis */
  327. };
  328. /************************************************************************
  329. * *
  330. * Public API *
  331. * *
  332. ************************************************************************/
  333. /**
  334. * Objects and Nodesets handling
  335. */
  336. XMLPUBVAR double xmlXPathNAN;
  337. XMLPUBVAR double xmlXPathPINF;
  338. XMLPUBVAR double xmlXPathNINF;
  339. /* These macros may later turn into functions */
  340. /**
  341. * xmlXPathNodeSetGetLength:
  342. * @ns: a node-set
  343. *
  344. * Implement a functionality similar to the DOM NodeList.length.
  345. *
  346. * Returns the number of nodes in the node-set.
  347. */
  348. #define xmlXPathNodeSetGetLength(ns) ((ns) ? (ns)->nodeNr : 0)
  349. /**
  350. * xmlXPathNodeSetItem:
  351. * @ns: a node-set
  352. * @index: index of a node in the set
  353. *
  354. * Implements a functionality similar to the DOM NodeList.item().
  355. *
  356. * Returns the xmlNodePtr at the given @index in @ns or NULL if
  357. * @index is out of range (0 to length-1)
  358. */
  359. #define xmlXPathNodeSetItem(ns, index) \
  360. ((((ns) != NULL) && \
  361. ((index) >= 0) && ((index) < (ns)->nodeNr)) ? \
  362. (ns)->nodeTab[(index)] \
  363. : NULL)
  364. /**
  365. * xmlXPathNodeSetIsEmpty:
  366. * @ns: a node-set
  367. *
  368. * Checks whether @ns is empty or not.
  369. *
  370. * Returns %TRUE if @ns is an empty node-set.
  371. */
  372. #define xmlXPathNodeSetIsEmpty(ns) \
  373. (((ns) == NULL) || ((ns)->nodeNr == 0) || ((ns)->nodeTab == NULL))
  374. XMLPUBFUN void XMLCALL
  375. xmlXPathFreeObject (xmlXPathObjectPtr obj);
  376. XMLPUBFUN xmlNodeSetPtr XMLCALL
  377. xmlXPathNodeSetCreate (xmlNodePtr val);
  378. XMLPUBFUN void XMLCALL
  379. xmlXPathFreeNodeSetList (xmlXPathObjectPtr obj);
  380. XMLPUBFUN void XMLCALL
  381. xmlXPathFreeNodeSet (xmlNodeSetPtr obj);
  382. XMLPUBFUN xmlXPathObjectPtr XMLCALL
  383. xmlXPathObjectCopy (xmlXPathObjectPtr val);
  384. XMLPUBFUN int XMLCALL
  385. xmlXPathCmpNodes (xmlNodePtr node1,
  386. xmlNodePtr node2);
  387. /**
  388. * Conversion functions to basic types.
  389. */
  390. XMLPUBFUN int XMLCALL
  391. xmlXPathCastNumberToBoolean (double val);
  392. XMLPUBFUN int XMLCALL
  393. xmlXPathCastStringToBoolean (const xmlChar * val);
  394. XMLPUBFUN int XMLCALL
  395. xmlXPathCastNodeSetToBoolean(xmlNodeSetPtr ns);
  396. XMLPUBFUN int XMLCALL
  397. xmlXPathCastToBoolean (xmlXPathObjectPtr val);
  398. XMLPUBFUN double XMLCALL
  399. xmlXPathCastBooleanToNumber (int val);
  400. XMLPUBFUN double XMLCALL
  401. xmlXPathCastStringToNumber (const xmlChar * val);
  402. XMLPUBFUN double XMLCALL
  403. xmlXPathCastNodeToNumber (xmlNodePtr node);
  404. XMLPUBFUN double XMLCALL
  405. xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns);
  406. XMLPUBFUN double XMLCALL
  407. xmlXPathCastToNumber (xmlXPathObjectPtr val);
  408. XMLPUBFUN xmlChar * XMLCALL
  409. xmlXPathCastBooleanToString (int val);
  410. XMLPUBFUN xmlChar * XMLCALL
  411. xmlXPathCastNumberToString (double val);
  412. XMLPUBFUN xmlChar * XMLCALL
  413. xmlXPathCastNodeToString (xmlNodePtr node);
  414. XMLPUBFUN xmlChar * XMLCALL
  415. xmlXPathCastNodeSetToString (xmlNodeSetPtr ns);
  416. XMLPUBFUN xmlChar * XMLCALL
  417. xmlXPathCastToString (xmlXPathObjectPtr val);
  418. XMLPUBFUN xmlXPathObjectPtr XMLCALL
  419. xmlXPathConvertBoolean (xmlXPathObjectPtr val);
  420. XMLPUBFUN xmlXPathObjectPtr XMLCALL
  421. xmlXPathConvertNumber (xmlXPathObjectPtr val);
  422. XMLPUBFUN xmlXPathObjectPtr XMLCALL
  423. xmlXPathConvertString (xmlXPathObjectPtr val);
  424. /**
  425. * Context handling.
  426. */
  427. XMLPUBFUN xmlXPathContextPtr XMLCALL
  428. xmlXPathNewContext (xmlDocPtr doc);
  429. XMLPUBFUN void XMLCALL
  430. xmlXPathFreeContext (xmlXPathContextPtr ctxt);
  431. XMLPUBFUN int XMLCALL
  432. xmlXPathContextSetCache(xmlXPathContextPtr ctxt,
  433. int active,
  434. int value,
  435. int options);
  436. /**
  437. * Evaluation functions.
  438. */
  439. XMLPUBFUN long XMLCALL
  440. xmlXPathOrderDocElems (xmlDocPtr doc);
  441. XMLPUBFUN xmlXPathObjectPtr XMLCALL
  442. xmlXPathEval (const xmlChar *str,
  443. xmlXPathContextPtr ctx);
  444. XMLPUBFUN xmlXPathObjectPtr XMLCALL
  445. xmlXPathEvalExpression (const xmlChar *str,
  446. xmlXPathContextPtr ctxt);
  447. XMLPUBFUN int XMLCALL
  448. xmlXPathEvalPredicate (xmlXPathContextPtr ctxt,
  449. xmlXPathObjectPtr res);
  450. /**
  451. * Separate compilation/evaluation entry points.
  452. */
  453. XMLPUBFUN xmlXPathCompExprPtr XMLCALL
  454. xmlXPathCompile (const xmlChar *str);
  455. XMLPUBFUN xmlXPathCompExprPtr XMLCALL
  456. xmlXPathCtxtCompile (xmlXPathContextPtr ctxt,
  457. const xmlChar *str);
  458. XMLPUBFUN xmlXPathObjectPtr XMLCALL
  459. xmlXPathCompiledEval (xmlXPathCompExprPtr comp,
  460. xmlXPathContextPtr ctx);
  461. XMLPUBFUN int XMLCALL
  462. xmlXPathCompiledEvalToBoolean(xmlXPathCompExprPtr comp,
  463. xmlXPathContextPtr ctxt);
  464. XMLPUBFUN void XMLCALL
  465. xmlXPathFreeCompExpr (xmlXPathCompExprPtr comp);
  466. #endif /* LIBXML_XPATH_ENABLED */
  467. #if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
  468. XMLPUBFUN void XMLCALL
  469. xmlXPathInit (void);
  470. XMLPUBFUN int XMLCALL
  471. xmlXPathIsNaN (double val);
  472. XMLPUBFUN int XMLCALL
  473. xmlXPathIsInf (double val);
  474. #ifdef __cplusplus
  475. }
  476. #endif
  477. #endif /* LIBXML_XPATH_ENABLED or LIBXML_SCHEMAS_ENABLED*/
  478. #endif /* ! __XML_XPATH_H__ */