2 * xpathInternals.c: internal interfaces for XML Path Language implementation
3 * used to build new modules on top of XPath
5 * See COPYRIGHT for the status of this software
7 * Author: daniel@veillard.com
10 #ifndef __XML_XPATH_INTERNALS_H__
11 #define __XML_XPATH_INTERNALS_H__
13 #include <libxml/xmlversion.h>
14 #include <libxml/xpath.h>
20 /************************************************************************
24 ************************************************************************/
27 * Many of these macros may later turn into functions. They
28 * shouldn't be used in #ifdef's preprocessor instructions.
32 * @ctxt: an XPath parser context
33 * @err: an xmlXPathError code
37 #define xmlXPathSetError(ctxt, err) \
38 { xmlXPatherror((ctxt), __FILE__, __LINE__, (err)); \
39 (ctxt)->error = (err); }
42 * xmlXPathSetArityError:
43 * @ctxt: an XPath parser context
45 * Raises an XPATH_INVALID_ARITY error.
47 #define xmlXPathSetArityError(ctxt) \
48 xmlXPathSetError((ctxt), XPATH_INVALID_ARITY)
51 * xmlXPathSetTypeError:
52 * @ctxt: an XPath parser context
54 * Raises an XPATH_INVALID_TYPE error.
56 #define xmlXPathSetTypeError(ctxt) \
57 xmlXPathSetError((ctxt), XPATH_INVALID_TYPE)
61 * @ctxt: an XPath parser context
63 * Get the error code of an XPath context.
65 * Returns the context error.
67 #define xmlXPathGetError(ctxt) ((ctxt)->error)
71 * @ctxt: an XPath parser context
73 * Check if an XPath error was raised.
75 * Returns true if an error has been raised, false otherwise.
77 #define xmlXPathCheckError(ctxt) ((ctxt)->error != XPATH_EXPRESSION_OK)
80 * xmlXPathGetDocument:
81 * @ctxt: an XPath parser context
83 * Get the document of an XPath context.
85 * Returns the context document.
87 #define xmlXPathGetDocument(ctxt) ((ctxt)->context->doc)
90 * xmlXPathGetContextNode:
91 * @ctxt: an XPath parser context
93 * Get the context node of an XPath context.
95 * Returns the context node.
97 #define xmlXPathGetContextNode(ctxt) ((ctxt)->context->node)
99 int xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt);
100 double xmlXPathPopNumber (xmlXPathParserContextPtr ctxt);
101 xmlChar * xmlXPathPopString (xmlXPathParserContextPtr ctxt);
102 xmlNodeSetPtr xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt);
103 void * xmlXPathPopExternal (xmlXPathParserContextPtr ctxt);
106 * xmlXPathReturnBoolean:
107 * @ctxt: an XPath parser context
110 * Pushes the boolean @val on the context stack.
112 #define xmlXPathReturnBoolean(ctxt, val) \
113 valuePush((ctxt), xmlXPathNewBoolean(val))
116 * xmlXPathReturnTrue:
117 * @ctxt: an XPath parser context
119 * Pushes true on the context stack.
121 #define xmlXPathReturnTrue(ctxt) xmlXPathReturnBoolean((ctxt), 1)
124 * xmlXPathReturnFalse:
125 * @ctxt: an XPath parser context
127 * Pushes false on the context stack.
129 #define xmlXPathReturnFalse(ctxt) xmlXPathReturnBoolean((ctxt), 0)
132 * xmlXPathReturnNumber:
133 * @ctxt: an XPath parser context
136 * Pushes the double @val on the context stack.
138 #define xmlXPathReturnNumber(ctxt, val) \
139 valuePush((ctxt), xmlXPathNewFloat(val))
142 * xmlXPathReturnString:
143 * @ctxt: an XPath parser context
146 * Pushes the string @str on the context stack.
148 #define xmlXPathReturnString(ctxt, str) \
149 valuePush((ctxt), xmlXPathWrapString(str))
152 * xmlXPathReturnEmptyString:
153 * @ctxt: an XPath parser context
155 * Pushes an empty string on the stack.
157 #define xmlXPathReturnEmptyString(ctxt) \
158 valuePush((ctxt), xmlXPathNewCString(""))
161 * xmlXPathReturnNodeSet:
162 * @ctxt: an XPath parser context
165 * Pushes the node-set @ns on the context stack.
167 #define xmlXPathReturnNodeSet(ctxt, ns) \
168 valuePush((ctxt), xmlXPathWrapNodeSet(ns))
171 * xmlXPathReturnEmptyNodeSet:
172 * @ctxt: an XPath parser context
174 * Pushes an empty node-set on the context stack.
176 #define xmlXPathReturnEmptyNodeSet(ctxt) \
177 valuePush((ctxt), xmlXPathNewNodeSet(NULL))
180 * xmlXPathReturnExternal:
181 * @ctxt: an XPath parser context
184 * Pushes user data on the context stack.
186 #define xmlXPathReturnExternal(ctxt, val) \
187 valuePush((ctxt), xmlXPathWrapExternal(val))
190 * xmlXPathStackIsNodeSet:
191 * @ctxt: an XPath parser context
193 * Check if the current value on the XPath stack is a node set or
194 * an XSLT value tree.
196 * Returns true if the current object on the stack is a node-set.
198 #define xmlXPathStackIsNodeSet(ctxt) \
199 (((ctxt)->value != NULL) \
200 && (((ctxt)->value->type == XPATH_NODESET) \
201 || ((ctxt)->value->type == XPATH_XSLT_TREE)))
204 * xmlXPathStackIsExternal:
205 * @ctxt: an XPath parser context
207 * Checks if the current value on the XPath stack is an external
210 * Returns true if the current object on the stack is an external
213 #define xmlXPathStackIsExternal(ctxt) \
214 ((ctxt->value != NULL) && (ctxt->value->type == XPATH_USERS))
217 * xmlXPathEmptyNodeSet:
220 * Empties a node-set.
222 #define xmlXPathEmptyNodeSet(ns) \
223 { while ((ns)->nodeNr > 0) (ns)->nodeTab[(ns)->nodeNr--] = NULL; }
228 * Macro to return from the function if an XPath error was detected.
230 #define CHECK_ERROR \
231 if (ctxt->error != XPATH_EXPRESSION_OK) return
236 * Macro to return 0 from the function if an XPath error was detected.
238 #define CHECK_ERROR0 \
239 if (ctxt->error != XPATH_EXPRESSION_OK) return(0)
245 * Macro to raise an XPath error and return.
247 #define XP_ERROR(X) \
248 { xmlXPatherror(ctxt, __FILE__, __LINE__, X); \
249 ctxt->error = (X); return; }
255 * Macro to raise an XPath error and return 0.
257 #define XP_ERROR0(X) \
258 { xmlXPatherror(ctxt, __FILE__, __LINE__, X); \
259 ctxt->error = (X); return(0); }
263 * @typeval: the XPath type
265 * Macro to check that the value on top of the XPath stack is of a given
268 #define CHECK_TYPE(typeval) \
269 if ((ctxt->value == NULL) || (ctxt->value->type != typeval)) \
270 XP_ERROR(XPATH_INVALID_TYPE)
274 * @typeval: the XPath type
276 * Macro to check that the value on top of the XPath stack is of a given
277 * type. Return(0) in case of failure
279 #define CHECK_TYPE0(typeval) \
280 if ((ctxt->value == NULL) || (ctxt->value->type != typeval)) \
281 XP_ERROR0(XPATH_INVALID_TYPE)
285 * @x: the number of expected args
287 * Macro to check that the number of args passed to an XPath function matches.
289 #define CHECK_ARITY(x) \
291 XP_ERROR(XPATH_INVALID_ARITY);
296 * Macro to try to cast the value on the top of the XPath stack to a string.
298 #define CAST_TO_STRING \
299 if ((ctxt->value != NULL) && (ctxt->value->type != XPATH_STRING)) \
300 xmlXPathStringFunction(ctxt, 1);
305 * Macro to try to cast the value on the top of the XPath stack to a number.
307 #define CAST_TO_NUMBER \
308 if ((ctxt->value != NULL) && (ctxt->value->type != XPATH_NUMBER)) \
309 xmlXPathNumberFunction(ctxt, 1);
314 * Macro to try to cast the value on the top of the XPath stack to a boolean.
316 #define CAST_TO_BOOLEAN \
317 if ((ctxt->value != NULL) && (ctxt->value->type != XPATH_BOOLEAN)) \
318 xmlXPathBooleanFunction(ctxt, 1);
321 * Variable Lookup forwarding.
324 * xmlXPathVariableLookupFunc:
325 * @ctxt: an XPath context
326 * @name: name of the variable
327 * @ns_uri: the namespace name hosting this variable
329 * Prototype for callbacks used to plug variable lookup in the XPath
332 * Returns the XPath object value or NULL if not found.
334 typedef xmlXPathObjectPtr (*xmlXPathVariableLookupFunc) (void *ctxt,
336 const xmlChar *ns_uri);
338 void xmlXPathRegisterVariableLookup (xmlXPathContextPtr ctxt,
339 xmlXPathVariableLookupFunc f,
343 * Function Lookup forwarding.
346 * xmlXPathFuncLookupFunc:
347 * @ctxt: an XPath context
348 * @name: name of the function
349 * @ns_uri: the namespace name hosting this function
351 * Prototype for callbacks used to plug function lookup in the XPath
354 * Returns the XPath function or NULL if not found.
356 typedef xmlXPathFunction (*xmlXPathFuncLookupFunc) (void *ctxt,
358 const xmlChar *ns_uri);
360 void xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt,
361 xmlXPathFuncLookupFunc f,
367 void xmlXPatherror (xmlXPathParserContextPtr ctxt,
372 void xmlXPathDebugDumpObject (FILE *output,
373 xmlXPathObjectPtr cur,
375 void xmlXPathDebugDumpCompExpr(FILE *output,
376 xmlXPathCompExprPtr comp,
382 int xmlXPathNodeSetContains (xmlNodeSetPtr cur,
384 xmlNodeSetPtr xmlXPathDifference (xmlNodeSetPtr nodes1,
385 xmlNodeSetPtr nodes2);
386 xmlNodeSetPtr xmlXPathIntersection (xmlNodeSetPtr nodes1,
387 xmlNodeSetPtr nodes2);
389 xmlNodeSetPtr xmlXPathDistinctSorted (xmlNodeSetPtr nodes);
390 xmlNodeSetPtr xmlXPathDistinct (xmlNodeSetPtr nodes);
392 int xmlXPathHasSameNodes (xmlNodeSetPtr nodes1,
393 xmlNodeSetPtr nodes2);
395 xmlNodeSetPtr xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes,
397 xmlNodeSetPtr xmlXPathLeadingSorted (xmlNodeSetPtr nodes1,
398 xmlNodeSetPtr nodes2);
399 xmlNodeSetPtr xmlXPathNodeLeading (xmlNodeSetPtr nodes,
401 xmlNodeSetPtr xmlXPathLeading (xmlNodeSetPtr nodes1,
402 xmlNodeSetPtr nodes2);
404 xmlNodeSetPtr xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes,
406 xmlNodeSetPtr xmlXPathTrailingSorted (xmlNodeSetPtr nodes1,
407 xmlNodeSetPtr nodes2);
408 xmlNodeSetPtr xmlXPathNodeTrailing (xmlNodeSetPtr nodes,
410 xmlNodeSetPtr xmlXPathTrailing (xmlNodeSetPtr nodes1,
411 xmlNodeSetPtr nodes2);
415 * Extending a context.
418 int xmlXPathRegisterNs (xmlXPathContextPtr ctxt,
419 const xmlChar *prefix,
420 const xmlChar *ns_uri);
421 const xmlChar * xmlXPathNsLookup (xmlXPathContextPtr ctxt,
422 const xmlChar *prefix);
423 void xmlXPathRegisteredNsCleanup (xmlXPathContextPtr ctxt);
425 int xmlXPathRegisterFunc (xmlXPathContextPtr ctxt,
428 int xmlXPathRegisterFuncNS (xmlXPathContextPtr ctxt,
430 const xmlChar *ns_uri,
432 int xmlXPathRegisterVariable (xmlXPathContextPtr ctxt,
434 xmlXPathObjectPtr value);
435 int xmlXPathRegisterVariableNS (xmlXPathContextPtr ctxt,
437 const xmlChar *ns_uri,
438 xmlXPathObjectPtr value);
439 xmlXPathFunction xmlXPathFunctionLookup (xmlXPathContextPtr ctxt,
440 const xmlChar *name);
441 xmlXPathFunction xmlXPathFunctionLookupNS (xmlXPathContextPtr ctxt,
443 const xmlChar *ns_uri);
444 void xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt);
445 xmlXPathObjectPtr xmlXPathVariableLookup (xmlXPathContextPtr ctxt,
446 const xmlChar *name);
447 xmlXPathObjectPtr xmlXPathVariableLookupNS (xmlXPathContextPtr ctxt,
449 const xmlChar *ns_uri);
450 void xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt);
453 * Utilities to extend XPath.
455 xmlXPathParserContextPtr
456 xmlXPathNewParserContext (const xmlChar *str,
457 xmlXPathContextPtr ctxt);
458 void xmlXPathFreeParserContext (xmlXPathParserContextPtr ctxt);
460 /* TODO: remap to xmlXPathValuePop and Push. */
461 xmlXPathObjectPtr valuePop (xmlXPathParserContextPtr ctxt);
462 int valuePush (xmlXPathParserContextPtr ctxt,
463 xmlXPathObjectPtr value);
465 xmlXPathObjectPtr xmlXPathNewString (const xmlChar *val);
466 xmlXPathObjectPtr xmlXPathNewCString (const char *val);
467 xmlXPathObjectPtr xmlXPathWrapString (xmlChar *val);
468 xmlXPathObjectPtr xmlXPathWrapCString (char * val);
469 xmlXPathObjectPtr xmlXPathNewFloat (double val);
470 xmlXPathObjectPtr xmlXPathNewBoolean (int val);
471 xmlXPathObjectPtr xmlXPathNewNodeSet (xmlNodePtr val);
472 xmlXPathObjectPtr xmlXPathNewValueTree (xmlNodePtr val);
473 void xmlXPathNodeSetAdd (xmlNodeSetPtr cur,
475 void xmlXPathNodeSetAddUnique (xmlNodeSetPtr cur,
477 void xmlXPathNodeSetAddNs (xmlNodeSetPtr cur,
480 void xmlXPathNodeSetSort (xmlNodeSetPtr set);
482 void xmlXPathRoot (xmlXPathParserContextPtr ctxt);
483 void xmlXPathEvalExpr (xmlXPathParserContextPtr ctxt);
484 xmlChar * xmlXPathParseName (xmlXPathParserContextPtr ctxt);
485 xmlChar * xmlXPathParseNCName (xmlXPathParserContextPtr ctxt);
488 * Existing functions.
490 double xmlXPathStringEvalNumber(const xmlChar *str);
491 int xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt,
492 xmlXPathObjectPtr res);
493 void xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt);
494 xmlNodeSetPtr xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2);
495 void xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val);
496 void xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val);
497 xmlXPathObjectPtr xmlXPathNewNodeSetList(xmlNodeSetPtr val);
498 xmlXPathObjectPtr xmlXPathWrapNodeSet(xmlNodeSetPtr val);
499 xmlXPathObjectPtr xmlXPathWrapExternal(void *val);
501 int xmlXPathEqualValues(xmlXPathParserContextPtr ctxt);
502 int xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt);
503 int xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict);
504 void xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt);
505 void xmlXPathAddValues(xmlXPathParserContextPtr ctxt);
506 void xmlXPathSubValues(xmlXPathParserContextPtr ctxt);
507 void xmlXPathMultValues(xmlXPathParserContextPtr ctxt);
508 void xmlXPathDivValues(xmlXPathParserContextPtr ctxt);
509 void xmlXPathModValues(xmlXPathParserContextPtr ctxt);
511 int xmlXPathIsNodeType(const xmlChar *name);
514 * Some of the axis navigation routines.
516 xmlNodePtr xmlXPathNextSelf(xmlXPathParserContextPtr ctxt,
518 xmlNodePtr xmlXPathNextChild(xmlXPathParserContextPtr ctxt,
520 xmlNodePtr xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt,
522 xmlNodePtr xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt,
524 xmlNodePtr xmlXPathNextParent(xmlXPathParserContextPtr ctxt,
526 xmlNodePtr xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt,
528 xmlNodePtr xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt,
530 xmlNodePtr xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt,
532 xmlNodePtr xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt,
534 xmlNodePtr xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt,
536 xmlNodePtr xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt,
538 xmlNodePtr xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt,
540 xmlNodePtr xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt,
543 * The official core of XPath functions.
545 void xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs);
546 void xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs);
547 void xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs);
548 void xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs);
549 void xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs);
550 void xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs);
551 void xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs);
552 void xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs);
553 void xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs);
554 void xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs);
555 void xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs);
556 void xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs);
557 void xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs);
558 void xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs);
559 void xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs);
560 void xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs);
561 void xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs);
562 void xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs);
563 void xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs);
564 void xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs);
565 void xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs);
566 void xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs);
567 void xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs);
568 void xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs);
569 void xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs);
570 void xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs);
573 * Really internal functions
575 void xmlXPathNodeSetFreeNs(xmlNsPtr ns);
580 #endif /* ! __XML_XPATH_INTERNALS_H__ */