xpath.inc 16 KB

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