xpath.h 16 KB

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