xpath.h 16 KB

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