xpath.h 16 KB

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