blob: 4d061cbe3038b25f8452aa5d4d300d58b53ce672 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xpath.c: XML Path Language implementation
3 * XPath is a language for addressing parts of an XML document,
4 * designed to be used by both XSLT and XPointer
5 *
6 * Reference: W3C Recommendation 16 November 1999
7 * http://www.w3.org/TR/1999/REC-xpath-19991116
8 * Public reference:
9 * http://www.w3.org/TR/xpath
10 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011 * See Copyright for the status of this software
Owen Taylor3473f882001-02-23 17:55:21 +000012 *
Daniel Veillardc5d64342001-06-24 12:13:24 +000013 * Author: daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000014 *
Owen Taylor3473f882001-02-23 17:55:21 +000015 */
16
Daniel Veillard34ce8be2002-03-18 19:37:11 +000017#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000018#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000019
Owen Taylor3473f882001-02-23 17:55:21 +000020#include <string.h>
21
22#ifdef HAVE_SYS_TYPES_H
23#include <sys/types.h>
24#endif
25#ifdef HAVE_MATH_H
26#include <math.h>
27#endif
28#ifdef HAVE_FLOAT_H
29#include <float.h>
30#endif
Owen Taylor3473f882001-02-23 17:55:21 +000031#ifdef HAVE_CTYPE_H
32#include <ctype.h>
33#endif
Daniel Veillard5792e162001-04-30 17:44:45 +000034#ifdef HAVE_SIGNAL_H
Daniel Veillardb45c43b2001-04-28 17:02:11 +000035#include <signal.h>
Daniel Veillardb45c43b2001-04-28 17:02:11 +000036#endif
Owen Taylor3473f882001-02-23 17:55:21 +000037
38#include <libxml/xmlmemory.h>
39#include <libxml/tree.h>
40#include <libxml/valid.h>
41#include <libxml/xpath.h>
42#include <libxml/xpathInternals.h>
43#include <libxml/parserInternals.h>
44#include <libxml/hash.h>
45#ifdef LIBXML_XPTR_ENABLED
46#include <libxml/xpointer.h>
47#endif
48#ifdef LIBXML_DEBUG_ENABLED
49#include <libxml/debugXML.h>
50#endif
51#include <libxml/xmlerror.h>
Daniel Veillard81463942001-10-16 12:34:39 +000052#include <libxml/threads.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000053#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000054
Daniel Veillardd96f6d32003-10-07 21:25:12 +000055#define TODO \
56 xmlGenericError(xmlGenericErrorContext, \
57 "Unimplemented block at %s:%d\n", \
58 __FILE__, __LINE__);
59
Daniel Veillard4432df22003-09-28 18:58:27 +000060#if defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XPATH_ENABLED)
Daniel Veillard9e7160d2001-03-18 23:17:47 +000061/************************************************************************
62 * *
63 * Floating point stuff *
64 * *
65 ************************************************************************/
66
Daniel Veillardc0631a62001-09-20 13:56:06 +000067#ifndef TRIO_REPLACE_STDIO
Daniel Veillardcda96922001-08-21 10:56:31 +000068#define TRIO_PUBLIC static
Daniel Veillardc0631a62001-09-20 13:56:06 +000069#endif
Daniel Veillardcda96922001-08-21 10:56:31 +000070#include "trionan.c"
71
Owen Taylor3473f882001-02-23 17:55:21 +000072/*
Owen Taylor3473f882001-02-23 17:55:21 +000073 * The lack of portability of this section of the libc is annoying !
74 */
75double xmlXPathNAN = 0;
76double xmlXPathPINF = 1;
77double xmlXPathNINF = -1;
Daniel Veillard5fc1f082002-03-27 09:05:40 +000078double xmlXPathNZERO = 0;
Daniel Veillard20ee8c02001-10-05 09:18:14 +000079static int xmlXPathInitialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +000080
Owen Taylor3473f882001-02-23 17:55:21 +000081/**
82 * xmlXPathInit:
83 *
84 * Initialize the XPath environment
85 */
86void
87xmlXPathInit(void) {
Daniel Veillard20ee8c02001-10-05 09:18:14 +000088 if (xmlXPathInitialized) return;
Owen Taylor3473f882001-02-23 17:55:21 +000089
Bjorn Reese45029602001-08-21 09:23:53 +000090 xmlXPathPINF = trio_pinf();
91 xmlXPathNINF = trio_ninf();
92 xmlXPathNAN = trio_nan();
Daniel Veillard5fc1f082002-03-27 09:05:40 +000093 xmlXPathNZERO = trio_nzero();
Owen Taylor3473f882001-02-23 17:55:21 +000094
Daniel Veillard20ee8c02001-10-05 09:18:14 +000095 xmlXPathInitialized = 1;
Owen Taylor3473f882001-02-23 17:55:21 +000096}
97
Daniel Veillardcda96922001-08-21 10:56:31 +000098/**
99 * xmlXPathIsNaN:
100 * @val: a double value
101 *
102 * Provides a portable isnan() function to detect whether a double
103 * is a NotaNumber. Based on trio code
104 * http://sourceforge.net/projects/ctrio/
105 *
106 * Returns 1 if the value is a NaN, 0 otherwise
107 */
108int
109xmlXPathIsNaN(double val) {
110 return(trio_isnan(val));
111}
112
113/**
114 * xmlXPathIsInf:
115 * @val: a double value
116 *
117 * Provides a portable isinf() function to detect whether a double
118 * is a +Infinite or -Infinite. Based on trio code
119 * http://sourceforge.net/projects/ctrio/
120 *
121 * Returns 1 vi the value is +Infinite, -1 if -Infinite, 0 otherwise
122 */
123int
124xmlXPathIsInf(double val) {
125 return(trio_isinf(val));
126}
127
Daniel Veillard4432df22003-09-28 18:58:27 +0000128#endif /* SCHEMAS or XPATH */
129#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000130/**
131 * xmlXPathGetSign:
132 * @val: a double value
133 *
134 * Provides a portable function to detect the sign of a double
135 * Modified from trio code
136 * http://sourceforge.net/projects/ctrio/
137 *
138 * Returns 1 if the value is Negative, 0 if positive
139 */
Daniel Veillard21458c82002-03-27 16:12:22 +0000140static int
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000141xmlXPathGetSign(double val) {
Daniel Veillard21458c82002-03-27 16:12:22 +0000142 return(trio_signbit(val));
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000143}
144
145
Daniel Veillardd9d32ae2003-07-05 20:32:43 +0000146/*
147 * TODO: when compatibility allows remove all "fake node libxslt" strings
148 * the test should just be name[0] = ' '
149 */
150/* #define DEBUG */
151/* #define DEBUG_STEP */
152/* #define DEBUG_STEP_NTH */
153/* #define DEBUG_EXPR */
154/* #define DEBUG_EVAL_COUNTS */
155
156static xmlNs xmlXPathXMLNamespaceStruct = {
157 NULL,
158 XML_NAMESPACE_DECL,
159 XML_XML_NAMESPACE,
160 BAD_CAST "xml",
161 NULL
162};
163static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct;
164#ifndef LIBXML_THREAD_ENABLED
165/*
166 * Optimizer is disabled only when threaded apps are detected while
167 * the library ain't compiled for thread safety.
168 */
169static int xmlXPathDisableOptimizer = 0;
170#endif
171
Owen Taylor3473f882001-02-23 17:55:21 +0000172/************************************************************************
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000173 * *
174 * Error handling routines *
175 * *
176 ************************************************************************/
177
William M. Brack08171912003-12-29 02:52:11 +0000178/*
179 * The array xmlXPathErrorMessages corresponds to the enum xmlXPathError
180 */
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000181static const char *xmlXPathErrorMessages[] = {
182 "Ok\n",
183 "Number encoding\n",
184 "Unfinished literal\n",
185 "Start of literal\n",
186 "Expected $ for variable reference\n",
187 "Undefined variable\n",
188 "Invalid predicate\n",
189 "Invalid expression\n",
190 "Missing closing curly brace\n",
191 "Unregistered function\n",
192 "Invalid operand\n",
193 "Invalid type\n",
194 "Invalid number of arguments\n",
195 "Invalid context size\n",
196 "Invalid context position\n",
197 "Memory allocation error\n",
198 "Syntax error\n",
199 "Resource error\n",
200 "Sub resource error\n",
201 "Undefined namespace prefix\n",
202 "Encoding error\n",
203 "Char out of XML range\n"
204};
205
206
207/**
208 * xmlXPathErrMemory:
209 * @ctxt: an XPath context
210 * @extra: extra informations
211 *
212 * Handle a redefinition of attribute error
213 */
214static void
215xmlXPathErrMemory(xmlXPathContextPtr ctxt, const char *extra)
216{
217 if (ctxt != NULL) {
218 if (extra) {
219 xmlChar buf[200];
220
221 xmlStrPrintf(buf, 200,
222 BAD_CAST "Memory allocation failed : %s\n",
223 extra);
224 ctxt->lastError.message = (char *) xmlStrdup(buf);
225 } else {
226 ctxt->lastError.message = (char *)
227 xmlStrdup(BAD_CAST "Memory allocation failed\n");
228 }
229 ctxt->lastError.domain = XML_FROM_XPATH;
230 ctxt->lastError.code = XML_ERR_NO_MEMORY;
231 if (ctxt->error != NULL)
232 ctxt->error(ctxt->userData, &ctxt->lastError);
233 } else {
234 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000235 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000236 NULL, NULL, XML_FROM_XPATH,
237 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0,
238 extra, NULL, NULL, 0, 0,
239 "Memory allocation failed : %s\n", extra);
240 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000241 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000242 NULL, NULL, XML_FROM_XPATH,
243 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0,
244 NULL, NULL, NULL, 0, 0,
245 "Memory allocation failed\n");
246 }
247}
248
249/**
250 * xmlXPathErrMemory:
251 * @ctxt: an XPath parser context
252 * @extra: extra informations
253 *
254 * Handle a redefinition of attribute error
255 */
256static void
257xmlXPathPErrMemory(xmlXPathParserContextPtr ctxt, const char *extra)
258{
259 ctxt->error = XPATH_MEMORY_ERROR;
260 if (ctxt == NULL)
261 xmlXPathErrMemory(NULL, extra);
262 else
263 xmlXPathErrMemory(ctxt->context, extra);
264}
265
266/**
267 * xmlXPathErr:
268 * @ctxt: a XPath parser context
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000269 * @error: the error code
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000270 *
271 * Handle a Relax NG Parsing error
272 */
273void
274xmlXPathErr(xmlXPathParserContextPtr ctxt, int error)
275{
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000276 if (ctxt == NULL) {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000277 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000278 NULL, NULL, XML_FROM_XPATH,
279 error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
280 XML_ERR_ERROR, NULL, 0,
281 NULL, NULL, NULL, 0, 0,
282 xmlXPathErrorMessages[error]);
283 return;
284 }
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000285 ctxt->error = error;
286 if (ctxt->context == NULL) {
287 __xmlRaiseError(NULL, NULL, NULL,
288 NULL, NULL, XML_FROM_XPATH,
289 error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
290 XML_ERR_ERROR, NULL, 0,
291 (const char *) ctxt->base, NULL, NULL,
292 ctxt->cur - ctxt->base, 0,
293 xmlXPathErrorMessages[error]);
294 return;
295 }
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000296 ctxt->context->lastError.domain = XML_FROM_XPATH;
297 ctxt->context->lastError.code = error + XML_XPATH_EXPRESSION_OK -
298 XPATH_EXPRESSION_OK;
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000299 ctxt->context->lastError.level = XML_ERR_ERROR;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000300 ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base);
301 ctxt->context->lastError.int1 = ctxt->cur - ctxt->base;
302 ctxt->context->lastError.node = ctxt->context->debugNode;
303 if (ctxt->context->error != NULL) {
304 ctxt->context->error(ctxt->context->userData,
305 &ctxt->context->lastError);
306 } else {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000307 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000308 NULL, ctxt->context->debugNode, XML_FROM_XPATH,
309 error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
310 XML_ERR_ERROR, NULL, 0,
311 (const char *) ctxt->base, NULL, NULL,
312 ctxt->cur - ctxt->base, 0,
313 xmlXPathErrorMessages[error]);
314 }
315
316}
317
318/**
319 * xmlXPatherror:
320 * @ctxt: the XPath Parser context
321 * @file: the file name
322 * @line: the line number
323 * @no: the error number
324 *
325 * Formats an error message.
326 */
327void
328xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file ATTRIBUTE_UNUSED,
329 int line ATTRIBUTE_UNUSED, int no) {
330 xmlXPathErr(ctxt, no);
331}
332
333
334/************************************************************************
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000335 * *
336 * Parser Types *
337 * *
338 ************************************************************************/
339
340/*
341 * Types are private:
342 */
343
344typedef enum {
345 XPATH_OP_END=0,
346 XPATH_OP_AND,
347 XPATH_OP_OR,
348 XPATH_OP_EQUAL,
349 XPATH_OP_CMP,
350 XPATH_OP_PLUS,
351 XPATH_OP_MULT,
352 XPATH_OP_UNION,
353 XPATH_OP_ROOT,
354 XPATH_OP_NODE,
355 XPATH_OP_RESET,
356 XPATH_OP_COLLECT,
357 XPATH_OP_VALUE,
358 XPATH_OP_VARIABLE,
359 XPATH_OP_FUNCTION,
360 XPATH_OP_ARG,
361 XPATH_OP_PREDICATE,
Daniel Veillardd8df6c02001-04-05 16:54:14 +0000362 XPATH_OP_FILTER,
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000363 XPATH_OP_SORT
364#ifdef LIBXML_XPTR_ENABLED
365 ,XPATH_OP_RANGETO
366#endif
367} xmlXPathOp;
368
369typedef enum {
370 AXIS_ANCESTOR = 1,
371 AXIS_ANCESTOR_OR_SELF,
372 AXIS_ATTRIBUTE,
373 AXIS_CHILD,
374 AXIS_DESCENDANT,
375 AXIS_DESCENDANT_OR_SELF,
376 AXIS_FOLLOWING,
377 AXIS_FOLLOWING_SIBLING,
378 AXIS_NAMESPACE,
379 AXIS_PARENT,
380 AXIS_PRECEDING,
381 AXIS_PRECEDING_SIBLING,
382 AXIS_SELF
383} xmlXPathAxisVal;
384
385typedef enum {
386 NODE_TEST_NONE = 0,
387 NODE_TEST_TYPE = 1,
388 NODE_TEST_PI = 2,
389 NODE_TEST_ALL = 3,
390 NODE_TEST_NS = 4,
391 NODE_TEST_NAME = 5
392} xmlXPathTestVal;
393
394typedef enum {
395 NODE_TYPE_NODE = 0,
396 NODE_TYPE_COMMENT = XML_COMMENT_NODE,
397 NODE_TYPE_TEXT = XML_TEXT_NODE,
398 NODE_TYPE_PI = XML_PI_NODE
399} xmlXPathTypeVal;
400
401
402typedef struct _xmlXPathStepOp xmlXPathStepOp;
403typedef xmlXPathStepOp *xmlXPathStepOpPtr;
404struct _xmlXPathStepOp {
William M. Brack08171912003-12-29 02:52:11 +0000405 xmlXPathOp op; /* The identifier of the operation */
406 int ch1; /* First child */
407 int ch2; /* Second child */
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000408 int value;
409 int value2;
410 int value3;
411 void *value4;
412 void *value5;
Daniel Veillarde39a93d2001-04-28 14:35:02 +0000413 void *cache;
Daniel Veillard42596ad2001-05-22 16:57:14 +0000414 void *cacheURI;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000415};
416
417struct _xmlXPathCompExpr {
William M. Brack08171912003-12-29 02:52:11 +0000418 int nbStep; /* Number of steps in this expression */
419 int maxStep; /* Maximum number of steps allocated */
420 xmlXPathStepOp *steps; /* ops for computation of this expression */
421 int last; /* index of last step in expression */
422 xmlChar *expr; /* the expression being computed */
Daniel Veillard4773df22004-01-23 13:15:13 +0000423 xmlDictPtr dict; /* the dictionnary to use if any */
Daniel Veillardf06307e2001-07-03 10:35:50 +0000424#ifdef DEBUG_EVAL_COUNTS
425 int nb;
426 xmlChar *string;
427#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000428};
429
430/************************************************************************
431 * *
432 * Parser Type functions *
433 * *
434 ************************************************************************/
435
436/**
437 * xmlXPathNewCompExpr:
438 *
439 * Create a new Xpath component
440 *
441 * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error
442 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000443static xmlXPathCompExprPtr
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000444xmlXPathNewCompExpr(void) {
445 xmlXPathCompExprPtr cur;
446
447 cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr));
448 if (cur == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000449 xmlXPathErrMemory(NULL, "allocating component\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000450 return(NULL);
451 }
452 memset(cur, 0, sizeof(xmlXPathCompExpr));
453 cur->maxStep = 10;
454 cur->nbStep = 0;
455 cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep *
456 sizeof(xmlXPathStepOp));
457 if (cur->steps == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000458 xmlXPathErrMemory(NULL, "allocating steps\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000459 xmlFree(cur);
460 return(NULL);
461 }
462 memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp));
463 cur->last = -1;
Daniel Veillardf06307e2001-07-03 10:35:50 +0000464#ifdef DEBUG_EVAL_COUNTS
465 cur->nb = 0;
466#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000467 return(cur);
468}
469
470/**
471 * xmlXPathFreeCompExpr:
472 * @comp: an XPATH comp
473 *
474 * Free up the memory allocated by @comp
475 */
476void
Daniel Veillardf06307e2001-07-03 10:35:50 +0000477xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp)
478{
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000479 xmlXPathStepOpPtr op;
480 int i;
481
482 if (comp == NULL)
Daniel Veillardf06307e2001-07-03 10:35:50 +0000483 return;
Daniel Veillard4773df22004-01-23 13:15:13 +0000484 if (comp->dict == NULL) {
485 for (i = 0; i < comp->nbStep; i++) {
486 op = &comp->steps[i];
487 if (op->value4 != NULL) {
488 if (op->op == XPATH_OP_VALUE)
489 xmlXPathFreeObject(op->value4);
490 else
491 xmlFree(op->value4);
492 }
493 if (op->value5 != NULL)
494 xmlFree(op->value5);
495 }
496 } else {
497 for (i = 0; i < comp->nbStep; i++) {
498 op = &comp->steps[i];
499 if (op->value4 != NULL) {
500 if (op->op == XPATH_OP_VALUE)
501 xmlXPathFreeObject(op->value4);
502 }
503 }
504 xmlDictFree(comp->dict);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000505 }
506 if (comp->steps != NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +0000507 xmlFree(comp->steps);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000508 }
Daniel Veillardf06307e2001-07-03 10:35:50 +0000509#ifdef DEBUG_EVAL_COUNTS
510 if (comp->string != NULL) {
511 xmlFree(comp->string);
512 }
513#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000514 if (comp->expr != NULL) {
515 xmlFree(comp->expr);
516 }
Daniel Veillardf06307e2001-07-03 10:35:50 +0000517
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000518 xmlFree(comp);
519}
520
521/**
522 * xmlXPathCompExprAdd:
523 * @comp: the compiled expression
524 * @ch1: first child index
525 * @ch2: second child index
526 * @op: an op
527 * @value: the first int value
528 * @value2: the second int value
529 * @value3: the third int value
530 * @value4: the first string value
531 * @value5: the second string value
532 *
William M. Brack08171912003-12-29 02:52:11 +0000533 * Add a step to an XPath Compiled Expression
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000534 *
535 * Returns -1 in case of failure, the index otherwise
536 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000537static int
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000538xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2,
539 xmlXPathOp op, int value,
540 int value2, int value3, void *value4, void *value5) {
541 if (comp->nbStep >= comp->maxStep) {
542 xmlXPathStepOp *real;
543
544 comp->maxStep *= 2;
545 real = (xmlXPathStepOp *) xmlRealloc(comp->steps,
546 comp->maxStep * sizeof(xmlXPathStepOp));
547 if (real == NULL) {
548 comp->maxStep /= 2;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000549 xmlXPathErrMemory(NULL, "adding step\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000550 return(-1);
551 }
552 comp->steps = real;
553 }
554 comp->last = comp->nbStep;
555 comp->steps[comp->nbStep].ch1 = ch1;
556 comp->steps[comp->nbStep].ch2 = ch2;
557 comp->steps[comp->nbStep].op = op;
558 comp->steps[comp->nbStep].value = value;
559 comp->steps[comp->nbStep].value2 = value2;
560 comp->steps[comp->nbStep].value3 = value3;
Daniel Veillard4773df22004-01-23 13:15:13 +0000561 if ((comp->dict != NULL) &&
562 ((op == XPATH_OP_FUNCTION) || (op == XPATH_OP_VARIABLE) ||
563 (op == XPATH_OP_COLLECT))) {
564 if (value4 != NULL) {
Daniel Veillardb3377952004-02-09 12:48:55 +0000565 comp->steps[comp->nbStep].value4 = (xmlChar *)
William M. Brackc07ed5e2004-01-30 07:52:48 +0000566 (void *)xmlDictLookup(comp->dict, value4, -1);
Daniel Veillard4773df22004-01-23 13:15:13 +0000567 xmlFree(value4);
568 } else
569 comp->steps[comp->nbStep].value4 = NULL;
570 if (value5 != NULL) {
Daniel Veillardb3377952004-02-09 12:48:55 +0000571 comp->steps[comp->nbStep].value5 = (xmlChar *)
William M. Brackc07ed5e2004-01-30 07:52:48 +0000572 (void *)xmlDictLookup(comp->dict, value5, -1);
Daniel Veillard4773df22004-01-23 13:15:13 +0000573 xmlFree(value5);
574 } else
575 comp->steps[comp->nbStep].value5 = NULL;
576 } else {
577 comp->steps[comp->nbStep].value4 = value4;
578 comp->steps[comp->nbStep].value5 = value5;
579 }
Daniel Veillarde39a93d2001-04-28 14:35:02 +0000580 comp->steps[comp->nbStep].cache = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000581 return(comp->nbStep++);
582}
583
Daniel Veillardf06307e2001-07-03 10:35:50 +0000584/**
585 * xmlXPathCompSwap:
586 * @comp: the compiled expression
587 * @op: operation index
588 *
589 * Swaps 2 operations in the compiled expression
Daniel Veillardf06307e2001-07-03 10:35:50 +0000590 */
591static void
592xmlXPathCompSwap(xmlXPathStepOpPtr op) {
593 int tmp;
594
Daniel Veillardbc6f7592002-04-16 07:49:59 +0000595#ifndef LIBXML_THREAD_ENABLED
Daniel Veillard81463942001-10-16 12:34:39 +0000596 /*
597 * Since this manipulates possibly shared variables, this is
William M. Brack08171912003-12-29 02:52:11 +0000598 * disabled if one detects that the library is used in a multithreaded
Daniel Veillard81463942001-10-16 12:34:39 +0000599 * application
600 */
601 if (xmlXPathDisableOptimizer)
602 return;
603#endif
604
Daniel Veillardf06307e2001-07-03 10:35:50 +0000605 tmp = op->ch1;
606 op->ch1 = op->ch2;
607 op->ch2 = tmp;
608}
609
Daniel Veillardd8df6c02001-04-05 16:54:14 +0000610#define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \
611 xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \
612 (op), (val), (val2), (val3), (val4), (val5))
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000613#define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \
614 xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \
615 (op), (val), (val2), (val3), (val4), (val5))
616
617#define PUSH_LEAVE_EXPR(op, val, val2) \
618xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL)
619
620#define PUSH_UNARY_EXPR(op, ch, val, val2) \
621xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL)
622
623#define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \
William M. Brack08171912003-12-29 02:52:11 +0000624xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \
625 (val), (val2), 0 ,NULL ,NULL)
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000626
627/************************************************************************
Owen Taylor3473f882001-02-23 17:55:21 +0000628 * *
629 * Debugging related functions *
630 * *
631 ************************************************************************/
632
Owen Taylor3473f882001-02-23 17:55:21 +0000633#define STRANGE \
634 xmlGenericError(xmlGenericErrorContext, \
635 "Internal error at %s:%d\n", \
636 __FILE__, __LINE__);
637
638#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000639static void
640xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000641 int i;
642 char shift[100];
643
644 for (i = 0;((i < depth) && (i < 25));i++)
645 shift[2 * i] = shift[2 * i + 1] = ' ';
646 shift[2 * i] = shift[2 * i + 1] = 0;
647 if (cur == NULL) {
648 fprintf(output, shift);
649 fprintf(output, "Node is NULL !\n");
650 return;
651
652 }
653
654 if ((cur->type == XML_DOCUMENT_NODE) ||
655 (cur->type == XML_HTML_DOCUMENT_NODE)) {
656 fprintf(output, shift);
657 fprintf(output, " /\n");
658 } else if (cur->type == XML_ATTRIBUTE_NODE)
659 xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth);
660 else
661 xmlDebugDumpOneNode(output, cur, depth);
662}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000663static void
664xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000665 xmlNodePtr tmp;
666 int i;
667 char shift[100];
668
669 for (i = 0;((i < depth) && (i < 25));i++)
670 shift[2 * i] = shift[2 * i + 1] = ' ';
671 shift[2 * i] = shift[2 * i + 1] = 0;
672 if (cur == NULL) {
673 fprintf(output, shift);
674 fprintf(output, "Node is NULL !\n");
675 return;
676
677 }
678
679 while (cur != NULL) {
680 tmp = cur;
681 cur = cur->next;
682 xmlDebugDumpOneNode(output, tmp, depth);
683 }
684}
Owen Taylor3473f882001-02-23 17:55:21 +0000685
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000686static void
687xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000688 int i;
689 char shift[100];
690
691 for (i = 0;((i < depth) && (i < 25));i++)
692 shift[2 * i] = shift[2 * i + 1] = ' ';
693 shift[2 * i] = shift[2 * i + 1] = 0;
694
695 if (cur == NULL) {
696 fprintf(output, shift);
697 fprintf(output, "NodeSet is NULL !\n");
698 return;
699
700 }
701
Daniel Veillard911f49a2001-04-07 15:39:35 +0000702 if (cur != NULL) {
703 fprintf(output, "Set contains %d nodes:\n", cur->nodeNr);
704 for (i = 0;i < cur->nodeNr;i++) {
705 fprintf(output, shift);
706 fprintf(output, "%d", i + 1);
707 xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1);
708 }
Owen Taylor3473f882001-02-23 17:55:21 +0000709 }
710}
711
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000712static void
713xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000714 int i;
715 char shift[100];
716
717 for (i = 0;((i < depth) && (i < 25));i++)
718 shift[2 * i] = shift[2 * i + 1] = ' ';
719 shift[2 * i] = shift[2 * i + 1] = 0;
720
721 if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) {
722 fprintf(output, shift);
723 fprintf(output, "Value Tree is NULL !\n");
724 return;
725
726 }
727
728 fprintf(output, shift);
729 fprintf(output, "%d", i + 1);
730 xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1);
731}
Owen Taylor3473f882001-02-23 17:55:21 +0000732#if defined(LIBXML_XPTR_ENABLED)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000733static void
734xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000735 int i;
736 char shift[100];
737
738 for (i = 0;((i < depth) && (i < 25));i++)
739 shift[2 * i] = shift[2 * i + 1] = ' ';
740 shift[2 * i] = shift[2 * i + 1] = 0;
741
742 if (cur == NULL) {
743 fprintf(output, shift);
744 fprintf(output, "LocationSet is NULL !\n");
745 return;
746
747 }
748
749 for (i = 0;i < cur->locNr;i++) {
750 fprintf(output, shift);
751 fprintf(output, "%d : ", i + 1);
752 xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1);
753 }
754}
Daniel Veillard017b1082001-06-21 11:20:21 +0000755#endif /* LIBXML_XPTR_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000756
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000757/**
758 * xmlXPathDebugDumpObject:
759 * @output: the FILE * to dump the output
760 * @cur: the object to inspect
761 * @depth: indentation level
762 *
763 * Dump the content of the object for debugging purposes
764 */
765void
766xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000767 int i;
768 char shift[100];
769
770 for (i = 0;((i < depth) && (i < 25));i++)
771 shift[2 * i] = shift[2 * i + 1] = ' ';
772 shift[2 * i] = shift[2 * i + 1] = 0;
773
774 fprintf(output, shift);
775
776 if (cur == NULL) {
777 fprintf(output, "Object is empty (NULL)\n");
778 return;
779 }
780 switch(cur->type) {
781 case XPATH_UNDEFINED:
782 fprintf(output, "Object is uninitialized\n");
783 break;
784 case XPATH_NODESET:
785 fprintf(output, "Object is a Node Set :\n");
786 xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth);
787 break;
788 case XPATH_XSLT_TREE:
789 fprintf(output, "Object is an XSLT value tree :\n");
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000790 xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth);
Owen Taylor3473f882001-02-23 17:55:21 +0000791 break;
792 case XPATH_BOOLEAN:
793 fprintf(output, "Object is a Boolean : ");
794 if (cur->boolval) fprintf(output, "true\n");
795 else fprintf(output, "false\n");
796 break;
797 case XPATH_NUMBER:
Daniel Veillardcda96922001-08-21 10:56:31 +0000798 switch (xmlXPathIsInf(cur->floatval)) {
Daniel Veillard357c9602001-05-03 10:49:20 +0000799 case 1:
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000800 fprintf(output, "Object is a number : Infinity\n");
Daniel Veillard357c9602001-05-03 10:49:20 +0000801 break;
802 case -1:
803 fprintf(output, "Object is a number : -Infinity\n");
804 break;
805 default:
Daniel Veillardcda96922001-08-21 10:56:31 +0000806 if (xmlXPathIsNaN(cur->floatval)) {
Daniel Veillard357c9602001-05-03 10:49:20 +0000807 fprintf(output, "Object is a number : NaN\n");
Daniel Veillardd30be4a2002-03-28 18:25:31 +0000808 } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) {
809 fprintf(output, "Object is a number : 0\n");
Daniel Veillard357c9602001-05-03 10:49:20 +0000810 } else {
811 fprintf(output, "Object is a number : %0g\n", cur->floatval);
812 }
813 }
Owen Taylor3473f882001-02-23 17:55:21 +0000814 break;
815 case XPATH_STRING:
816 fprintf(output, "Object is a string : ");
817 xmlDebugDumpString(output, cur->stringval);
818 fprintf(output, "\n");
819 break;
820 case XPATH_POINT:
821 fprintf(output, "Object is a point : index %d in node", cur->index);
822 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1);
823 fprintf(output, "\n");
824 break;
825 case XPATH_RANGE:
826 if ((cur->user2 == NULL) ||
827 ((cur->user2 == cur->user) && (cur->index == cur->index2))) {
828 fprintf(output, "Object is a collapsed range :\n");
829 fprintf(output, shift);
830 if (cur->index >= 0)
831 fprintf(output, "index %d in ", cur->index);
832 fprintf(output, "node\n");
833 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user,
834 depth + 1);
835 } else {
836 fprintf(output, "Object is a range :\n");
837 fprintf(output, shift);
838 fprintf(output, "From ");
839 if (cur->index >= 0)
840 fprintf(output, "index %d in ", cur->index);
841 fprintf(output, "node\n");
842 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user,
843 depth + 1);
844 fprintf(output, shift);
845 fprintf(output, "To ");
846 if (cur->index2 >= 0)
847 fprintf(output, "index %d in ", cur->index2);
848 fprintf(output, "node\n");
849 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2,
850 depth + 1);
851 fprintf(output, "\n");
852 }
853 break;
854 case XPATH_LOCATIONSET:
855#if defined(LIBXML_XPTR_ENABLED)
856 fprintf(output, "Object is a Location Set:\n");
857 xmlXPathDebugDumpLocationSet(output,
858 (xmlLocationSetPtr) cur->user, depth);
859#endif
860 break;
861 case XPATH_USERS:
862 fprintf(output, "Object is user defined\n");
863 break;
864 }
865}
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000866
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000867static void
868xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp,
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000869 xmlXPathStepOpPtr op, int depth) {
870 int i;
871 char shift[100];
872
873 for (i = 0;((i < depth) && (i < 25));i++)
874 shift[2 * i] = shift[2 * i + 1] = ' ';
875 shift[2 * i] = shift[2 * i + 1] = 0;
876
877 fprintf(output, shift);
878 if (op == NULL) {
879 fprintf(output, "Step is NULL\n");
880 return;
881 }
882 switch (op->op) {
883 case XPATH_OP_END:
884 fprintf(output, "END"); break;
885 case XPATH_OP_AND:
886 fprintf(output, "AND"); break;
887 case XPATH_OP_OR:
888 fprintf(output, "OR"); break;
889 case XPATH_OP_EQUAL:
890 if (op->value)
891 fprintf(output, "EQUAL =");
892 else
893 fprintf(output, "EQUAL !=");
894 break;
895 case XPATH_OP_CMP:
896 if (op->value)
897 fprintf(output, "CMP <");
898 else
899 fprintf(output, "CMP >");
900 if (!op->value2)
901 fprintf(output, "=");
902 break;
903 case XPATH_OP_PLUS:
904 if (op->value == 0)
905 fprintf(output, "PLUS -");
906 else if (op->value == 1)
907 fprintf(output, "PLUS +");
908 else if (op->value == 2)
909 fprintf(output, "PLUS unary -");
910 else if (op->value == 3)
911 fprintf(output, "PLUS unary - -");
912 break;
913 case XPATH_OP_MULT:
914 if (op->value == 0)
915 fprintf(output, "MULT *");
916 else if (op->value == 1)
917 fprintf(output, "MULT div");
918 else
919 fprintf(output, "MULT mod");
920 break;
921 case XPATH_OP_UNION:
922 fprintf(output, "UNION"); break;
923 case XPATH_OP_ROOT:
924 fprintf(output, "ROOT"); break;
925 case XPATH_OP_NODE:
926 fprintf(output, "NODE"); break;
927 case XPATH_OP_RESET:
928 fprintf(output, "RESET"); break;
929 case XPATH_OP_SORT:
930 fprintf(output, "SORT"); break;
931 case XPATH_OP_COLLECT: {
William M. Brack78637da2003-07-31 14:47:38 +0000932 xmlXPathAxisVal axis = (xmlXPathAxisVal)op->value;
933 xmlXPathTestVal test = (xmlXPathTestVal)op->value2;
934 xmlXPathTypeVal type = (xmlXPathTypeVal)op->value3;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000935 const xmlChar *prefix = op->value4;
936 const xmlChar *name = op->value5;
937
938 fprintf(output, "COLLECT ");
939 switch (axis) {
940 case AXIS_ANCESTOR:
941 fprintf(output, " 'ancestors' "); break;
942 case AXIS_ANCESTOR_OR_SELF:
943 fprintf(output, " 'ancestors-or-self' "); break;
944 case AXIS_ATTRIBUTE:
945 fprintf(output, " 'attributes' "); break;
946 case AXIS_CHILD:
947 fprintf(output, " 'child' "); break;
948 case AXIS_DESCENDANT:
949 fprintf(output, " 'descendant' "); break;
950 case AXIS_DESCENDANT_OR_SELF:
951 fprintf(output, " 'descendant-or-self' "); break;
952 case AXIS_FOLLOWING:
953 fprintf(output, " 'following' "); break;
954 case AXIS_FOLLOWING_SIBLING:
955 fprintf(output, " 'following-siblings' "); break;
956 case AXIS_NAMESPACE:
957 fprintf(output, " 'namespace' "); break;
958 case AXIS_PARENT:
959 fprintf(output, " 'parent' "); break;
960 case AXIS_PRECEDING:
961 fprintf(output, " 'preceding' "); break;
962 case AXIS_PRECEDING_SIBLING:
963 fprintf(output, " 'preceding-sibling' "); break;
964 case AXIS_SELF:
965 fprintf(output, " 'self' "); break;
966 }
967 switch (test) {
968 case NODE_TEST_NONE:
969 fprintf(output, "'none' "); break;
970 case NODE_TEST_TYPE:
971 fprintf(output, "'type' "); break;
972 case NODE_TEST_PI:
973 fprintf(output, "'PI' "); break;
974 case NODE_TEST_ALL:
975 fprintf(output, "'all' "); break;
976 case NODE_TEST_NS:
977 fprintf(output, "'namespace' "); break;
978 case NODE_TEST_NAME:
979 fprintf(output, "'name' "); break;
980 }
981 switch (type) {
982 case NODE_TYPE_NODE:
983 fprintf(output, "'node' "); break;
984 case NODE_TYPE_COMMENT:
985 fprintf(output, "'comment' "); break;
986 case NODE_TYPE_TEXT:
987 fprintf(output, "'text' "); break;
988 case NODE_TYPE_PI:
989 fprintf(output, "'PI' "); break;
990 }
991 if (prefix != NULL)
992 fprintf(output, "%s:", prefix);
993 if (name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +0000994 fprintf(output, "%s", (const char *) name);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000995 break;
996
997 }
998 case XPATH_OP_VALUE: {
999 xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4;
1000
1001 fprintf(output, "ELEM ");
1002 xmlXPathDebugDumpObject(output, object, 0);
1003 goto finish;
1004 }
1005 case XPATH_OP_VARIABLE: {
1006 const xmlChar *prefix = op->value5;
1007 const xmlChar *name = op->value4;
1008
1009 if (prefix != NULL)
1010 fprintf(output, "VARIABLE %s:%s", prefix, name);
1011 else
1012 fprintf(output, "VARIABLE %s", name);
1013 break;
1014 }
1015 case XPATH_OP_FUNCTION: {
1016 int nbargs = op->value;
1017 const xmlChar *prefix = op->value5;
1018 const xmlChar *name = op->value4;
1019
1020 if (prefix != NULL)
1021 fprintf(output, "FUNCTION %s:%s(%d args)",
1022 prefix, name, nbargs);
1023 else
1024 fprintf(output, "FUNCTION %s(%d args)", name, nbargs);
1025 break;
1026 }
1027 case XPATH_OP_ARG: fprintf(output, "ARG"); break;
1028 case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00001029 case XPATH_OP_FILTER: fprintf(output, "FILTER"); break;
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00001030#ifdef LIBXML_XPTR_ENABLED
1031 case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break;
1032#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001033 default:
1034 fprintf(output, "UNKNOWN %d\n", op->op); return;
1035 }
1036 fprintf(output, "\n");
1037finish:
1038 if (op->ch1 >= 0)
1039 xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1);
1040 if (op->ch2 >= 0)
1041 xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1);
1042}
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001043
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001044/**
1045 * xmlXPathDebugDumpCompExpr:
1046 * @output: the FILE * for the output
1047 * @comp: the precompiled XPath expression
1048 * @depth: the indentation level.
1049 *
1050 * Dumps the tree of the compiled XPath expression.
1051 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001052void
1053xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp,
1054 int depth) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001055 int i;
1056 char shift[100];
1057
1058 for (i = 0;((i < depth) && (i < 25));i++)
1059 shift[2 * i] = shift[2 * i + 1] = ' ';
1060 shift[2 * i] = shift[2 * i + 1] = 0;
1061
1062 fprintf(output, shift);
1063
1064 if (comp == NULL) {
1065 fprintf(output, "Compiled Expression is NULL\n");
1066 return;
1067 }
1068 fprintf(output, "Compiled Expression : %d elements\n",
1069 comp->nbStep);
1070 i = comp->last;
1071 xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1);
1072}
Daniel Veillard017b1082001-06-21 11:20:21 +00001073#endif /* LIBXML_DEBUG_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001074
1075/************************************************************************
1076 * *
1077 * Parser stacks related functions and macros *
1078 * *
1079 ************************************************************************/
1080
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001081/**
1082 * valuePop:
1083 * @ctxt: an XPath evaluation context
1084 *
1085 * Pops the top XPath object from the value stack
1086 *
1087 * Returns the XPath object just removed
1088 */
Daniel Veillard1c732d22002-11-30 11:22:59 +00001089extern xmlXPathObjectPtr
1090valuePop(xmlXPathParserContextPtr ctxt)
1091{
1092 xmlXPathObjectPtr ret;
1093
1094 if (ctxt->valueNr <= 0)
1095 return (0);
1096 ctxt->valueNr--;
1097 if (ctxt->valueNr > 0)
1098 ctxt->value = ctxt->valueTab[ctxt->valueNr - 1];
1099 else
1100 ctxt->value = NULL;
1101 ret = ctxt->valueTab[ctxt->valueNr];
1102 ctxt->valueTab[ctxt->valueNr] = 0;
1103 return (ret);
1104}
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001105/**
1106 * valuePush:
1107 * @ctxt: an XPath evaluation context
1108 * @value: the XPath object
1109 *
1110 * Pushes a new XPath object on top of the value stack
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001111 *
1112 * returns the number of items on the value stack
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001113 */
Daniel Veillard1c732d22002-11-30 11:22:59 +00001114extern int
1115valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value)
1116{
1117 if (ctxt->valueNr >= ctxt->valueMax) {
1118 ctxt->valueMax *= 2;
1119 ctxt->valueTab =
1120 (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab,
1121 ctxt->valueMax *
1122 sizeof(ctxt->valueTab[0]));
1123 if (ctxt->valueTab == NULL) {
1124 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1125 return (0);
1126 }
1127 }
1128 ctxt->valueTab[ctxt->valueNr] = value;
1129 ctxt->value = value;
1130 return (ctxt->valueNr++);
1131}
Owen Taylor3473f882001-02-23 17:55:21 +00001132
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001133/**
1134 * xmlXPathPopBoolean:
1135 * @ctxt: an XPath parser context
1136 *
1137 * Pops a boolean from the stack, handling conversion if needed.
1138 * Check error with #xmlXPathCheckError.
1139 *
1140 * Returns the boolean
1141 */
1142int
1143xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) {
1144 xmlXPathObjectPtr obj;
1145 int ret;
1146
1147 obj = valuePop(ctxt);
1148 if (obj == NULL) {
1149 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1150 return(0);
1151 }
William M. Brack08171912003-12-29 02:52:11 +00001152 if (obj->type != XPATH_BOOLEAN)
1153 ret = xmlXPathCastToBoolean(obj);
1154 else
1155 ret = obj->boolval;
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001156 xmlXPathFreeObject(obj);
1157 return(ret);
1158}
1159
1160/**
1161 * xmlXPathPopNumber:
1162 * @ctxt: an XPath parser context
1163 *
1164 * Pops a number from the stack, handling conversion if needed.
1165 * Check error with #xmlXPathCheckError.
1166 *
1167 * Returns the number
1168 */
1169double
1170xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) {
1171 xmlXPathObjectPtr obj;
1172 double ret;
1173
1174 obj = valuePop(ctxt);
1175 if (obj == NULL) {
1176 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1177 return(0);
1178 }
William M. Brack08171912003-12-29 02:52:11 +00001179 if (obj->type != XPATH_NUMBER)
1180 ret = xmlXPathCastToNumber(obj);
1181 else
1182 ret = obj->floatval;
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001183 xmlXPathFreeObject(obj);
1184 return(ret);
1185}
1186
1187/**
1188 * xmlXPathPopString:
1189 * @ctxt: an XPath parser context
1190 *
1191 * Pops a string from the stack, handling conversion if needed.
1192 * Check error with #xmlXPathCheckError.
1193 *
1194 * Returns the string
1195 */
1196xmlChar *
1197xmlXPathPopString (xmlXPathParserContextPtr ctxt) {
1198 xmlXPathObjectPtr obj;
1199 xmlChar * ret;
1200
1201 obj = valuePop(ctxt);
1202 if (obj == NULL) {
1203 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1204 return(NULL);
1205 }
William M. Brack08171912003-12-29 02:52:11 +00001206 ret = xmlXPathCastToString(obj); /* this does required strdup */
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001207 /* TODO: needs refactoring somewhere else */
1208 if (obj->stringval == ret)
1209 obj->stringval = NULL;
1210 xmlXPathFreeObject(obj);
1211 return(ret);
1212}
1213
1214/**
1215 * xmlXPathPopNodeSet:
1216 * @ctxt: an XPath parser context
1217 *
1218 * Pops a node-set from the stack, handling conversion if needed.
1219 * Check error with #xmlXPathCheckError.
1220 *
1221 * Returns the node-set
1222 */
1223xmlNodeSetPtr
1224xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) {
1225 xmlXPathObjectPtr obj;
1226 xmlNodeSetPtr ret;
1227
1228 if (ctxt->value == NULL) {
1229 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1230 return(NULL);
1231 }
1232 if (!xmlXPathStackIsNodeSet(ctxt)) {
1233 xmlXPathSetTypeError(ctxt);
1234 return(NULL);
1235 }
1236 obj = valuePop(ctxt);
1237 ret = obj->nodesetval;
Daniel Veillard9deb2422003-07-28 20:40:59 +00001238 /* to fix memory leak of not clearing obj->user */
1239 if (obj->boolval && obj->user != NULL)
1240 xmlFreeNodeList((xmlNodePtr) obj->user);
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001241 xmlXPathFreeNodeSetList(obj);
1242 return(ret);
1243}
1244
1245/**
1246 * xmlXPathPopExternal:
1247 * @ctxt: an XPath parser context
1248 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001249 * Pops an external object from the stack, handling conversion if needed.
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001250 * Check error with #xmlXPathCheckError.
1251 *
1252 * Returns the object
1253 */
1254void *
1255xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) {
1256 xmlXPathObjectPtr obj;
1257 void * ret;
1258
1259 if (ctxt->value == NULL) {
1260 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1261 return(NULL);
1262 }
1263 if (ctxt->value->type != XPATH_USERS) {
1264 xmlXPathSetTypeError(ctxt);
1265 return(NULL);
1266 }
1267 obj = valuePop(ctxt);
1268 ret = obj->user;
1269 xmlXPathFreeObject(obj);
1270 return(ret);
1271}
1272
Owen Taylor3473f882001-02-23 17:55:21 +00001273/*
1274 * Macros for accessing the content. Those should be used only by the parser,
1275 * and not exported.
1276 *
1277 * Dirty macros, i.e. one need to make assumption on the context to use them
1278 *
1279 * CUR_PTR return the current pointer to the xmlChar to be parsed.
1280 * CUR returns the current xmlChar value, i.e. a 8 bit value
1281 * in ISO-Latin or UTF-8.
1282 * This should be used internally by the parser
1283 * only to compare to ASCII values otherwise it would break when
1284 * running with UTF-8 encoding.
1285 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
1286 * to compare on ASCII based substring.
1287 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
1288 * strings within the parser.
1289 * CURRENT Returns the current char value, with the full decoding of
1290 * UTF-8 if we are using this mode. It returns an int.
1291 * NEXT Skip to the next character, this does the proper decoding
1292 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
1293 * It returns the pointer to the current xmlChar.
1294 */
1295
1296#define CUR (*ctxt->cur)
1297#define SKIP(val) ctxt->cur += (val)
1298#define NXT(val) ctxt->cur[(val)]
1299#define CUR_PTR ctxt->cur
Daniel Veillard61d80a22001-04-27 17:13:01 +00001300#define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l)
1301
1302#define COPY_BUF(l,b,i,v) \
1303 if (l == 1) b[i++] = (xmlChar) v; \
1304 else i += xmlCopyChar(l,&b[i],v)
1305
1306#define NEXTL(l) ctxt->cur += l
Owen Taylor3473f882001-02-23 17:55:21 +00001307
1308#define SKIP_BLANKS \
William M. Brack76e95df2003-10-18 16:20:14 +00001309 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
Owen Taylor3473f882001-02-23 17:55:21 +00001310
1311#define CURRENT (*ctxt->cur)
1312#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1313
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001314
1315#ifndef DBL_DIG
1316#define DBL_DIG 16
1317#endif
1318#ifndef DBL_EPSILON
1319#define DBL_EPSILON 1E-9
1320#endif
1321
1322#define UPPER_DOUBLE 1E9
1323#define LOWER_DOUBLE 1E-5
1324
1325#define INTEGER_DIGITS DBL_DIG
1326#define FRACTION_DIGITS (DBL_DIG + 1)
1327#define EXPONENT_DIGITS (3 + 2)
1328
1329/**
1330 * xmlXPathFormatNumber:
1331 * @number: number to format
1332 * @buffer: output buffer
1333 * @buffersize: size of output buffer
1334 *
1335 * Convert the number into a string representation.
1336 */
1337static void
1338xmlXPathFormatNumber(double number, char buffer[], int buffersize)
1339{
Daniel Veillardcda96922001-08-21 10:56:31 +00001340 switch (xmlXPathIsInf(number)) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001341 case 1:
Daniel Veillard5fc1f082002-03-27 09:05:40 +00001342 if (buffersize > (int)sizeof("Infinity"))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001343 snprintf(buffer, buffersize, "Infinity");
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001344 break;
1345 case -1:
1346 if (buffersize > (int)sizeof("-Infinity"))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001347 snprintf(buffer, buffersize, "-Infinity");
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001348 break;
1349 default:
Daniel Veillardcda96922001-08-21 10:56:31 +00001350 if (xmlXPathIsNaN(number)) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001351 if (buffersize > (int)sizeof("NaN"))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001352 snprintf(buffer, buffersize, "NaN");
Daniel Veillardd30be4a2002-03-28 18:25:31 +00001353 } else if (number == 0 && xmlXPathGetSign(number) != 0) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001354 snprintf(buffer, buffersize, "0");
Daniel Veillard28cac6b2002-03-19 11:25:30 +00001355 } else if (number == ((int) number)) {
1356 char work[30];
1357 char *ptr, *cur;
1358 int res, value = (int) number;
1359
1360 ptr = &buffer[0];
1361 if (value < 0) {
1362 *ptr++ = '-';
1363 value = -value;
1364 }
1365 if (value == 0) {
1366 *ptr++ = '0';
1367 } else {
1368 cur = &work[0];
1369 while (value != 0) {
1370 res = value % 10;
1371 value = value / 10;
1372 *cur++ = '0' + res;
1373 }
1374 cur--;
1375 while ((cur >= &work[0]) && (ptr - buffer < buffersize)) {
1376 *ptr++ = *cur--;
1377 }
1378 }
1379 if (ptr - buffer < buffersize) {
1380 *ptr = 0;
1381 } else if (buffersize > 0) {
1382 ptr--;
1383 *ptr = 0;
1384 }
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001385 } else {
Bjorn Reese70a9da52001-04-21 16:57:29 +00001386 /* 3 is sign, decimal point, and terminating zero */
1387 char work[DBL_DIG + EXPONENT_DIGITS + 3];
1388 int integer_place, fraction_place;
1389 char *ptr;
1390 char *after_fraction;
1391 double absolute_value;
1392 int size;
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001393
Bjorn Reese70a9da52001-04-21 16:57:29 +00001394 absolute_value = fabs(number);
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001395
Bjorn Reese70a9da52001-04-21 16:57:29 +00001396 /*
1397 * First choose format - scientific or regular floating point.
1398 * In either case, result is in work, and after_fraction points
1399 * just past the fractional part.
1400 */
1401 if ( ((absolute_value > UPPER_DOUBLE) ||
1402 (absolute_value < LOWER_DOUBLE)) &&
1403 (absolute_value != 0.0) ) {
1404 /* Use scientific notation */
1405 integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
1406 fraction_place = DBL_DIG - 1;
1407 snprintf(work, sizeof(work),"%*.*e",
1408 integer_place, fraction_place, number);
1409 after_fraction = strchr(work + DBL_DIG, 'e');
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001410 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00001411 else {
1412 /* Use regular notation */
Daniel Veillard56f06462001-06-24 21:34:03 +00001413 if (absolute_value > 0.0)
1414 integer_place = 1 + (int)log10(absolute_value);
1415 else
Daniel Veillarda3067d12001-06-24 21:39:39 +00001416 integer_place = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00001417 fraction_place = (integer_place > 0)
1418 ? DBL_DIG - integer_place
1419 : DBL_DIG;
1420 size = snprintf(work, sizeof(work), "%0.*f",
1421 fraction_place, number);
1422 after_fraction = work + size;
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001423 }
1424
Bjorn Reese70a9da52001-04-21 16:57:29 +00001425 /* Remove fractional trailing zeroes */
1426 ptr = after_fraction;
1427 while (*(--ptr) == '0')
1428 ;
1429 if (*ptr != '.')
1430 ptr++;
Daniel Veillard5dd3c962003-09-12 15:32:16 +00001431 while ((*ptr++ = *after_fraction++) != 0);
Bjorn Reese70a9da52001-04-21 16:57:29 +00001432
1433 /* Finally copy result back to caller */
1434 size = strlen(work) + 1;
1435 if (size > buffersize) {
1436 work[buffersize - 1] = 0;
1437 size = buffersize;
1438 }
Daniel Veillard5dd3c962003-09-12 15:32:16 +00001439 memmove(buffer, work, size);
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001440 }
1441 break;
1442 }
1443}
1444
Owen Taylor3473f882001-02-23 17:55:21 +00001445
1446/************************************************************************
1447 * *
1448 * Routines to handle NodeSets *
1449 * *
1450 ************************************************************************/
1451
1452/**
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001453 * xmlXPathOrderDocElems:
1454 * @doc: an input document
1455 *
1456 * Call this routine to speed up XPath computation on static documents.
1457 * This stamps all the element nodes with the document order
1458 * Like for line information, the order is kept in the element->content
William M. Brack08171912003-12-29 02:52:11 +00001459 * field, the value stored is actually - the node number (starting at -1)
1460 * to be able to differentiate from line numbers.
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001461 *
William M. Brack08171912003-12-29 02:52:11 +00001462 * Returns the number of elements found in the document or -1 in case
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001463 * of error.
1464 */
1465long
1466xmlXPathOrderDocElems(xmlDocPtr doc) {
1467 long count = 0;
1468 xmlNodePtr cur;
1469
1470 if (doc == NULL)
1471 return(-1);
1472 cur = doc->children;
1473 while (cur != NULL) {
1474 if (cur->type == XML_ELEMENT_NODE) {
1475 cur->content = (void *) (-(++count));
1476 if (cur->children != NULL) {
1477 cur = cur->children;
1478 continue;
1479 }
1480 }
1481 if (cur->next != NULL) {
1482 cur = cur->next;
1483 continue;
1484 }
1485 do {
1486 cur = cur->parent;
1487 if (cur == NULL)
1488 break;
1489 if (cur == (xmlNodePtr) doc) {
1490 cur = NULL;
1491 break;
1492 }
1493 if (cur->next != NULL) {
1494 cur = cur->next;
1495 break;
1496 }
1497 } while (cur != NULL);
1498 }
1499 return(count);
1500}
1501
1502/**
Owen Taylor3473f882001-02-23 17:55:21 +00001503 * xmlXPathCmpNodes:
1504 * @node1: the first node
1505 * @node2: the second node
1506 *
1507 * Compare two nodes w.r.t document order
1508 *
1509 * Returns -2 in case of error 1 if first point < second point, 0 if
William M. Brack08171912003-12-29 02:52:11 +00001510 * it's the same node, -1 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00001511 */
1512int
1513xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) {
1514 int depth1, depth2;
Daniel Veillardedfd5882003-03-07 14:20:40 +00001515 int attr1 = 0, attr2 = 0;
William M. Bracke8d1bd92003-12-23 01:28:58 +00001516 xmlNodePtr attrNode1 = NULL, attrNode2 = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001517 xmlNodePtr cur, root;
1518
1519 if ((node1 == NULL) || (node2 == NULL))
1520 return(-2);
1521 /*
1522 * a couple of optimizations which will avoid computations in most cases
1523 */
Daniel Veillardedfd5882003-03-07 14:20:40 +00001524 if (node1->type == XML_ATTRIBUTE_NODE) {
1525 attr1 = 1;
William M. Bracke8d1bd92003-12-23 01:28:58 +00001526 attrNode1 = node1;
Daniel Veillardedfd5882003-03-07 14:20:40 +00001527 node1 = node1->parent;
1528 }
1529 if (node2->type == XML_ATTRIBUTE_NODE) {
1530 attr2 = 1;
William M. Bracke8d1bd92003-12-23 01:28:58 +00001531 attrNode2 = node2;
Daniel Veillardedfd5882003-03-07 14:20:40 +00001532 node2 = node2->parent;
1533 }
1534 if (node1 == node2) {
William M. Bracke8d1bd92003-12-23 01:28:58 +00001535 if (attr1 == attr2) {
1536 /* not required, but we keep attributes in order */
1537 if (attr1 != 0) {
1538 cur = attrNode2->prev;
1539 while (cur != NULL) {
1540 if (cur == attrNode1)
1541 return (1);
1542 cur = cur->prev;
1543 }
1544 return (-1);
1545 }
Daniel Veillardedfd5882003-03-07 14:20:40 +00001546 return(0);
William M. Bracke8d1bd92003-12-23 01:28:58 +00001547 }
Daniel Veillardedfd5882003-03-07 14:20:40 +00001548 if (attr2 == 1)
1549 return(1);
1550 return(-1);
1551 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00001552 if ((node1->type == XML_NAMESPACE_DECL) ||
1553 (node2->type == XML_NAMESPACE_DECL))
1554 return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00001555 if (node1 == node2->prev)
1556 return(1);
1557 if (node1 == node2->next)
1558 return(-1);
1559
1560 /*
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001561 * Speedup using document order if availble.
Daniel Veillard7216cfd2002-11-08 15:10:00 +00001562 */
1563 if ((node1->type == XML_ELEMENT_NODE) &&
1564 (node2->type == XML_ELEMENT_NODE) &&
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001565 (0 > (long) node1->content) &&
1566 (0 > (long) node2->content) &&
1567 (node1->doc == node2->doc)) {
1568 long l1, l2;
1569
1570 l1 = -((long) node1->content);
1571 l2 = -((long) node2->content);
Daniel Veillard7216cfd2002-11-08 15:10:00 +00001572 if (l1 < l2)
1573 return(1);
1574 if (l1 > l2)
1575 return(-1);
1576 }
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001577
Daniel Veillard7216cfd2002-11-08 15:10:00 +00001578 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001579 * compute depth to root
1580 */
1581 for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) {
1582 if (cur == node1)
1583 return(1);
1584 depth2++;
1585 }
1586 root = cur;
1587 for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) {
1588 if (cur == node2)
1589 return(-1);
1590 depth1++;
1591 }
1592 /*
1593 * Distinct document (or distinct entities :-( ) case.
1594 */
1595 if (root != cur) {
1596 return(-2);
1597 }
1598 /*
1599 * get the nearest common ancestor.
1600 */
1601 while (depth1 > depth2) {
1602 depth1--;
1603 node1 = node1->parent;
1604 }
1605 while (depth2 > depth1) {
1606 depth2--;
1607 node2 = node2->parent;
1608 }
1609 while (node1->parent != node2->parent) {
1610 node1 = node1->parent;
1611 node2 = node2->parent;
1612 /* should not happen but just in case ... */
1613 if ((node1 == NULL) || (node2 == NULL))
1614 return(-2);
1615 }
1616 /*
1617 * Find who's first.
1618 */
Daniel Veillardf49be472004-02-17 11:48:18 +00001619 if (node1 == node2->prev)
1620 return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00001621 if (node1 == node2->next)
1622 return(-1);
Daniel Veillardf49be472004-02-17 11:48:18 +00001623 /*
1624 * Speedup using document order if availble.
1625 */
1626 if ((node1->type == XML_ELEMENT_NODE) &&
1627 (node2->type == XML_ELEMENT_NODE) &&
1628 (0 > (long) node1->content) &&
1629 (0 > (long) node2->content) &&
1630 (node1->doc == node2->doc)) {
1631 long l1, l2;
1632
1633 l1 = -((long) node1->content);
1634 l2 = -((long) node2->content);
1635 if (l1 < l2)
1636 return(1);
1637 if (l1 > l2)
1638 return(-1);
1639 }
1640
Owen Taylor3473f882001-02-23 17:55:21 +00001641 for (cur = node1->next;cur != NULL;cur = cur->next)
1642 if (cur == node2)
1643 return(1);
1644 return(-1); /* assume there is no sibling list corruption */
1645}
1646
1647/**
1648 * xmlXPathNodeSetSort:
1649 * @set: the node set
1650 *
1651 * Sort the node set in document order
1652 */
1653void
1654xmlXPathNodeSetSort(xmlNodeSetPtr set) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001655 int i, j, incr, len;
Owen Taylor3473f882001-02-23 17:55:21 +00001656 xmlNodePtr tmp;
1657
1658 if (set == NULL)
1659 return;
1660
1661 /* Use Shell's sort to sort the node-set */
1662 len = set->nodeNr;
1663 for (incr = len / 2; incr > 0; incr /= 2) {
1664 for (i = incr; i < len; i++) {
1665 j = i - incr;
1666 while (j >= 0) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001667 if (xmlXPathCmpNodes(set->nodeTab[j],
1668 set->nodeTab[j + incr]) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +00001669 tmp = set->nodeTab[j];
1670 set->nodeTab[j] = set->nodeTab[j + incr];
1671 set->nodeTab[j + incr] = tmp;
1672 j -= incr;
1673 } else
1674 break;
1675 }
1676 }
1677 }
1678}
1679
1680#define XML_NODESET_DEFAULT 10
1681/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001682 * xmlXPathNodeSetDupNs:
1683 * @node: the parent node of the namespace XPath node
1684 * @ns: the libxml namespace declaration node.
1685 *
1686 * Namespace node in libxml don't match the XPath semantic. In a node set
1687 * the namespace nodes are duplicated and the next pointer is set to the
1688 * parent node in the XPath semantic.
1689 *
1690 * Returns the newly created object.
1691 */
1692static xmlNodePtr
1693xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) {
1694 xmlNsPtr cur;
1695
1696 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
1697 return(NULL);
1698 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
1699 return((xmlNodePtr) ns);
1700
1701 /*
1702 * Allocate a new Namespace and fill the fields.
1703 */
1704 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
1705 if (cur == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001706 xmlXPathErrMemory(NULL, "duplicating namespace\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001707 return(NULL);
1708 }
1709 memset(cur, 0, sizeof(xmlNs));
1710 cur->type = XML_NAMESPACE_DECL;
1711 if (ns->href != NULL)
1712 cur->href = xmlStrdup(ns->href);
1713 if (ns->prefix != NULL)
1714 cur->prefix = xmlStrdup(ns->prefix);
1715 cur->next = (xmlNsPtr) node;
1716 return((xmlNodePtr) cur);
1717}
1718
1719/**
1720 * xmlXPathNodeSetFreeNs:
1721 * @ns: the XPath namespace node found in a nodeset.
1722 *
William M. Brack08171912003-12-29 02:52:11 +00001723 * Namespace nodes in libxml don't match the XPath semantic. In a node set
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001724 * the namespace nodes are duplicated and the next pointer is set to the
William M. Brack08171912003-12-29 02:52:11 +00001725 * parent node in the XPath semantic. Check if such a node needs to be freed
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001726 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001727void
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001728xmlXPathNodeSetFreeNs(xmlNsPtr ns) {
1729 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
1730 return;
1731
1732 if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) {
1733 if (ns->href != NULL)
1734 xmlFree((xmlChar *)ns->href);
1735 if (ns->prefix != NULL)
1736 xmlFree((xmlChar *)ns->prefix);
1737 xmlFree(ns);
1738 }
1739}
1740
1741/**
Owen Taylor3473f882001-02-23 17:55:21 +00001742 * xmlXPathNodeSetCreate:
1743 * @val: an initial xmlNodePtr, or NULL
1744 *
1745 * Create a new xmlNodeSetPtr of type double and of value @val
1746 *
1747 * Returns the newly created object.
1748 */
1749xmlNodeSetPtr
1750xmlXPathNodeSetCreate(xmlNodePtr val) {
1751 xmlNodeSetPtr ret;
1752
1753 ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
1754 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001755 xmlXPathErrMemory(NULL, "creating nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001756 return(NULL);
1757 }
1758 memset(ret, 0 , (size_t) sizeof(xmlNodeSet));
1759 if (val != NULL) {
1760 ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1761 sizeof(xmlNodePtr));
1762 if (ret->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001763 xmlXPathErrMemory(NULL, "creating nodeset\n");
1764 xmlFree(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001765 return(NULL);
1766 }
1767 memset(ret->nodeTab, 0 ,
1768 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1769 ret->nodeMax = XML_NODESET_DEFAULT;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001770 if (val->type == XML_NAMESPACE_DECL) {
1771 xmlNsPtr ns = (xmlNsPtr) val;
1772
1773 ret->nodeTab[ret->nodeNr++] =
1774 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1775 } else
1776 ret->nodeTab[ret->nodeNr++] = val;
Owen Taylor3473f882001-02-23 17:55:21 +00001777 }
1778 return(ret);
1779}
1780
1781/**
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001782 * xmlXPathNodeSetContains:
1783 * @cur: the node-set
1784 * @val: the node
1785 *
1786 * checks whether @cur contains @val
1787 *
1788 * Returns true (1) if @cur contains @val, false (0) otherwise
1789 */
1790int
1791xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) {
1792 int i;
1793
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001794 if (val->type == XML_NAMESPACE_DECL) {
1795 for (i = 0; i < cur->nodeNr; i++) {
1796 if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) {
1797 xmlNsPtr ns1, ns2;
1798
1799 ns1 = (xmlNsPtr) val;
1800 ns2 = (xmlNsPtr) cur->nodeTab[i];
1801 if (ns1 == ns2)
1802 return(1);
1803 if ((ns1->next != NULL) && (ns2->next == ns1->next) &&
1804 (xmlStrEqual(ns1->prefix, ns2->prefix)))
1805 return(1);
1806 }
1807 }
1808 } else {
1809 for (i = 0; i < cur->nodeNr; i++) {
1810 if (cur->nodeTab[i] == val)
1811 return(1);
1812 }
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001813 }
1814 return(0);
1815}
1816
1817/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001818 * xmlXPathNodeSetAddNs:
1819 * @cur: the initial node set
1820 * @node: the hosting node
1821 * @ns: a the namespace node
1822 *
1823 * add a new namespace node to an existing NodeSet
1824 */
Aleksey Sanin79376ba2002-05-14 06:41:32 +00001825void
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001826xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) {
1827 int i;
1828
1829 if ((ns == NULL) || (node == NULL) || (ns->type != XML_NAMESPACE_DECL) ||
1830 (node->type != XML_ELEMENT_NODE))
1831 return;
1832
William M. Brack08171912003-12-29 02:52:11 +00001833 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001834 /*
William M. Brack08171912003-12-29 02:52:11 +00001835 * prevent duplicates
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001836 */
1837 for (i = 0;i < cur->nodeNr;i++) {
1838 if ((cur->nodeTab[i] != NULL) &&
1839 (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) &&
Daniel Veillardc62a1472002-03-19 18:35:12 +00001840 (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) &&
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001841 (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix)))
1842 return;
1843 }
1844
1845 /*
1846 * grow the nodeTab if needed
1847 */
1848 if (cur->nodeMax == 0) {
1849 cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1850 sizeof(xmlNodePtr));
1851 if (cur->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001852 xmlXPathErrMemory(NULL, "growing nodeset\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001853 return;
1854 }
1855 memset(cur->nodeTab, 0 ,
1856 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1857 cur->nodeMax = XML_NODESET_DEFAULT;
1858 } else if (cur->nodeNr == cur->nodeMax) {
1859 xmlNodePtr *temp;
1860
1861 cur->nodeMax *= 2;
1862 temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
1863 sizeof(xmlNodePtr));
1864 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001865 xmlXPathErrMemory(NULL, "growing nodeset\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001866 return;
1867 }
1868 cur->nodeTab = temp;
1869 }
1870 cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns);
1871}
1872
1873/**
Owen Taylor3473f882001-02-23 17:55:21 +00001874 * xmlXPathNodeSetAdd:
1875 * @cur: the initial node set
1876 * @val: a new xmlNodePtr
1877 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001878 * add a new xmlNodePtr to an existing NodeSet
Owen Taylor3473f882001-02-23 17:55:21 +00001879 */
1880void
1881xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) {
1882 int i;
1883
1884 if (val == NULL) return;
1885
Daniel Veillardef0b4502003-03-24 13:57:34 +00001886#if 0
Daniel Veillard652d8a92003-02-04 19:28:49 +00001887 if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' '))
1888 return; /* an XSLT fake node */
Daniel Veillardef0b4502003-03-24 13:57:34 +00001889#endif
Daniel Veillard652d8a92003-02-04 19:28:49 +00001890
William M. Brack08171912003-12-29 02:52:11 +00001891 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00001892 /*
William M. Brack08171912003-12-29 02:52:11 +00001893 * prevent duplcates
Owen Taylor3473f882001-02-23 17:55:21 +00001894 */
1895 for (i = 0;i < cur->nodeNr;i++)
1896 if (cur->nodeTab[i] == val) return;
1897
1898 /*
1899 * grow the nodeTab if needed
1900 */
1901 if (cur->nodeMax == 0) {
1902 cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1903 sizeof(xmlNodePtr));
1904 if (cur->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001905 xmlXPathErrMemory(NULL, "growing nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001906 return;
1907 }
1908 memset(cur->nodeTab, 0 ,
1909 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1910 cur->nodeMax = XML_NODESET_DEFAULT;
1911 } else if (cur->nodeNr == cur->nodeMax) {
1912 xmlNodePtr *temp;
1913
1914 cur->nodeMax *= 2;
1915 temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
1916 sizeof(xmlNodePtr));
1917 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001918 xmlXPathErrMemory(NULL, "growing nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001919 return;
1920 }
1921 cur->nodeTab = temp;
1922 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001923 if (val->type == XML_NAMESPACE_DECL) {
1924 xmlNsPtr ns = (xmlNsPtr) val;
1925
1926 cur->nodeTab[cur->nodeNr++] =
1927 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1928 } else
1929 cur->nodeTab[cur->nodeNr++] = val;
Owen Taylor3473f882001-02-23 17:55:21 +00001930}
1931
1932/**
1933 * xmlXPathNodeSetAddUnique:
1934 * @cur: the initial node set
1935 * @val: a new xmlNodePtr
1936 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001937 * add a new xmlNodePtr to an existing NodeSet, optimized version
Owen Taylor3473f882001-02-23 17:55:21 +00001938 * when we are sure the node is not already in the set.
1939 */
1940void
1941xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) {
1942 if (val == NULL) return;
1943
Daniel Veillardef0b4502003-03-24 13:57:34 +00001944#if 0
Daniel Veillard652d8a92003-02-04 19:28:49 +00001945 if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' '))
1946 return; /* an XSLT fake node */
Daniel Veillardef0b4502003-03-24 13:57:34 +00001947#endif
Daniel Veillard652d8a92003-02-04 19:28:49 +00001948
William M. Brack08171912003-12-29 02:52:11 +00001949 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00001950 /*
1951 * grow the nodeTab if needed
1952 */
1953 if (cur->nodeMax == 0) {
1954 cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1955 sizeof(xmlNodePtr));
1956 if (cur->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001957 xmlXPathErrMemory(NULL, "growing nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001958 return;
1959 }
1960 memset(cur->nodeTab, 0 ,
1961 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1962 cur->nodeMax = XML_NODESET_DEFAULT;
1963 } else if (cur->nodeNr == cur->nodeMax) {
1964 xmlNodePtr *temp;
1965
1966 cur->nodeMax *= 2;
1967 temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
1968 sizeof(xmlNodePtr));
1969 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001970 xmlXPathErrMemory(NULL, "growing nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001971 return;
1972 }
1973 cur->nodeTab = temp;
1974 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001975 if (val->type == XML_NAMESPACE_DECL) {
1976 xmlNsPtr ns = (xmlNsPtr) val;
1977
1978 cur->nodeTab[cur->nodeNr++] =
1979 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1980 } else
1981 cur->nodeTab[cur->nodeNr++] = val;
Owen Taylor3473f882001-02-23 17:55:21 +00001982}
1983
1984/**
1985 * xmlXPathNodeSetMerge:
1986 * @val1: the first NodeSet or NULL
1987 * @val2: the second NodeSet
1988 *
1989 * Merges two nodesets, all nodes from @val2 are added to @val1
1990 * if @val1 is NULL, a new set is created and copied from @val2
1991 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001992 * Returns @val1 once extended or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00001993 */
1994xmlNodeSetPtr
1995xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00001996 int i, j, initNr, skip;
Owen Taylor3473f882001-02-23 17:55:21 +00001997
1998 if (val2 == NULL) return(val1);
1999 if (val1 == NULL) {
2000 val1 = xmlXPathNodeSetCreate(NULL);
2001 }
2002
William M. Brack08171912003-12-29 02:52:11 +00002003 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00002004 initNr = val1->nodeNr;
2005
2006 for (i = 0;i < val2->nodeNr;i++) {
2007 /*
William M. Brack08171912003-12-29 02:52:11 +00002008 * check against duplicates
Owen Taylor3473f882001-02-23 17:55:21 +00002009 */
Daniel Veillardd8df6c02001-04-05 16:54:14 +00002010 skip = 0;
2011 for (j = 0; j < initNr; j++) {
2012 if (val1->nodeTab[j] == val2->nodeTab[i]) {
2013 skip = 1;
2014 break;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002015 } else if ((val1->nodeTab[j]->type == XML_NAMESPACE_DECL) &&
2016 (val2->nodeTab[i]->type == XML_NAMESPACE_DECL)) {
2017 xmlNsPtr ns1, ns2;
2018 ns1 = (xmlNsPtr) val1->nodeTab[j];
2019 ns2 = (xmlNsPtr) val2->nodeTab[i];
2020 if ((ns1->next == ns2->next) &&
2021 (xmlStrEqual(ns1->prefix, ns2->prefix))) {
2022 skip = 1;
2023 break;
2024 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00002025 }
2026 }
2027 if (skip)
2028 continue;
Owen Taylor3473f882001-02-23 17:55:21 +00002029
2030 /*
2031 * grow the nodeTab if needed
2032 */
2033 if (val1->nodeMax == 0) {
2034 val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
2035 sizeof(xmlNodePtr));
2036 if (val1->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002037 xmlXPathErrMemory(NULL, "merging nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002038 return(NULL);
2039 }
2040 memset(val1->nodeTab, 0 ,
2041 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
2042 val1->nodeMax = XML_NODESET_DEFAULT;
2043 } else if (val1->nodeNr == val1->nodeMax) {
2044 xmlNodePtr *temp;
2045
2046 val1->nodeMax *= 2;
2047 temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax *
2048 sizeof(xmlNodePtr));
2049 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002050 xmlXPathErrMemory(NULL, "merging nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002051 return(NULL);
2052 }
2053 val1->nodeTab = temp;
2054 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002055 if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) {
2056 xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i];
2057
2058 val1->nodeTab[val1->nodeNr++] =
2059 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
2060 } else
2061 val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i];
Owen Taylor3473f882001-02-23 17:55:21 +00002062 }
2063
2064 return(val1);
2065}
2066
2067/**
Daniel Veillard75be0132002-03-13 10:03:35 +00002068 * xmlXPathNodeSetMergeUnique:
2069 * @val1: the first NodeSet or NULL
2070 * @val2: the second NodeSet
2071 *
2072 * Merges two nodesets, all nodes from @val2 are added to @val1
2073 * if @val1 is NULL, a new set is created and copied from @val2
2074 *
2075 * Returns @val1 once extended or NULL in case of error.
2076 */
2077static xmlNodeSetPtr
2078xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
William M. Brack78637da2003-07-31 14:47:38 +00002079 int i;
Daniel Veillard75be0132002-03-13 10:03:35 +00002080
2081 if (val2 == NULL) return(val1);
2082 if (val1 == NULL) {
2083 val1 = xmlXPathNodeSetCreate(NULL);
2084 }
2085
William M. Brack08171912003-12-29 02:52:11 +00002086 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Daniel Veillard75be0132002-03-13 10:03:35 +00002087
2088 for (i = 0;i < val2->nodeNr;i++) {
2089 /*
2090 * grow the nodeTab if needed
2091 */
2092 if (val1->nodeMax == 0) {
2093 val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
2094 sizeof(xmlNodePtr));
2095 if (val1->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002096 xmlXPathErrMemory(NULL, "merging nodeset\n");
Daniel Veillard75be0132002-03-13 10:03:35 +00002097 return(NULL);
2098 }
2099 memset(val1->nodeTab, 0 ,
2100 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
2101 val1->nodeMax = XML_NODESET_DEFAULT;
2102 } else if (val1->nodeNr == val1->nodeMax) {
2103 xmlNodePtr *temp;
2104
2105 val1->nodeMax *= 2;
2106 temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax *
2107 sizeof(xmlNodePtr));
2108 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002109 xmlXPathErrMemory(NULL, "merging nodeset\n");
Daniel Veillard75be0132002-03-13 10:03:35 +00002110 return(NULL);
2111 }
2112 val1->nodeTab = temp;
2113 }
2114 if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) {
2115 xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i];
2116
2117 val1->nodeTab[val1->nodeNr++] =
2118 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
2119 } else
2120 val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i];
2121 }
2122
2123 return(val1);
2124}
2125
2126/**
Owen Taylor3473f882001-02-23 17:55:21 +00002127 * xmlXPathNodeSetDel:
2128 * @cur: the initial node set
2129 * @val: an xmlNodePtr
2130 *
2131 * Removes an xmlNodePtr from an existing NodeSet
2132 */
2133void
2134xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) {
2135 int i;
2136
2137 if (cur == NULL) return;
2138 if (val == NULL) return;
2139
2140 /*
William M. Brack08171912003-12-29 02:52:11 +00002141 * find node in nodeTab
Owen Taylor3473f882001-02-23 17:55:21 +00002142 */
2143 for (i = 0;i < cur->nodeNr;i++)
2144 if (cur->nodeTab[i] == val) break;
2145
William M. Brack08171912003-12-29 02:52:11 +00002146 if (i >= cur->nodeNr) { /* not found */
Owen Taylor3473f882001-02-23 17:55:21 +00002147#ifdef DEBUG
2148 xmlGenericError(xmlGenericErrorContext,
2149 "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n",
2150 val->name);
2151#endif
2152 return;
2153 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002154 if ((cur->nodeTab[i] != NULL) &&
2155 (cur->nodeTab[i]->type == XML_NAMESPACE_DECL))
2156 xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00002157 cur->nodeNr--;
2158 for (;i < cur->nodeNr;i++)
2159 cur->nodeTab[i] = cur->nodeTab[i + 1];
2160 cur->nodeTab[cur->nodeNr] = NULL;
2161}
2162
2163/**
2164 * xmlXPathNodeSetRemove:
2165 * @cur: the initial node set
2166 * @val: the index to remove
2167 *
2168 * Removes an entry from an existing NodeSet list.
2169 */
2170void
2171xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) {
2172 if (cur == NULL) return;
2173 if (val >= cur->nodeNr) return;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002174 if ((cur->nodeTab[val] != NULL) &&
2175 (cur->nodeTab[val]->type == XML_NAMESPACE_DECL))
2176 xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]);
Owen Taylor3473f882001-02-23 17:55:21 +00002177 cur->nodeNr--;
2178 for (;val < cur->nodeNr;val++)
2179 cur->nodeTab[val] = cur->nodeTab[val + 1];
2180 cur->nodeTab[cur->nodeNr] = NULL;
2181}
2182
2183/**
2184 * xmlXPathFreeNodeSet:
2185 * @obj: the xmlNodeSetPtr to free
2186 *
2187 * Free the NodeSet compound (not the actual nodes !).
2188 */
2189void
2190xmlXPathFreeNodeSet(xmlNodeSetPtr obj) {
2191 if (obj == NULL) return;
2192 if (obj->nodeTab != NULL) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002193 int i;
2194
William M. Brack08171912003-12-29 02:52:11 +00002195 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002196 for (i = 0;i < obj->nodeNr;i++)
2197 if ((obj->nodeTab[i] != NULL) &&
2198 (obj->nodeTab[i]->type == XML_NAMESPACE_DECL))
2199 xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00002200 xmlFree(obj->nodeTab);
2201 }
Owen Taylor3473f882001-02-23 17:55:21 +00002202 xmlFree(obj);
2203}
2204
2205/**
2206 * xmlXPathFreeValueTree:
2207 * @obj: the xmlNodeSetPtr to free
2208 *
2209 * Free the NodeSet compound and the actual tree, this is different
2210 * from xmlXPathFreeNodeSet()
2211 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002212static void
Owen Taylor3473f882001-02-23 17:55:21 +00002213xmlXPathFreeValueTree(xmlNodeSetPtr obj) {
2214 int i;
2215
2216 if (obj == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002217
2218 if (obj->nodeTab != NULL) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002219 for (i = 0;i < obj->nodeNr;i++) {
2220 if (obj->nodeTab[i] != NULL) {
2221 if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) {
2222 xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]);
2223 } else {
2224 xmlFreeNodeList(obj->nodeTab[i]);
2225 }
2226 }
2227 }
Owen Taylor3473f882001-02-23 17:55:21 +00002228 xmlFree(obj->nodeTab);
2229 }
Owen Taylor3473f882001-02-23 17:55:21 +00002230 xmlFree(obj);
2231}
2232
2233#if defined(DEBUG) || defined(DEBUG_STEP)
2234/**
2235 * xmlGenericErrorContextNodeSet:
2236 * @output: a FILE * for the output
William M. Brack08171912003-12-29 02:52:11 +00002237 * @obj: the xmlNodeSetPtr to display
Owen Taylor3473f882001-02-23 17:55:21 +00002238 *
2239 * Quick display of a NodeSet
2240 */
2241void
2242xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) {
2243 int i;
2244
2245 if (output == NULL) output = xmlGenericErrorContext;
2246 if (obj == NULL) {
2247 fprintf(output, "NodeSet == NULL !\n");
2248 return;
2249 }
2250 if (obj->nodeNr == 0) {
2251 fprintf(output, "NodeSet is empty\n");
2252 return;
2253 }
2254 if (obj->nodeTab == NULL) {
2255 fprintf(output, " nodeTab == NULL !\n");
2256 return;
2257 }
2258 for (i = 0; i < obj->nodeNr; i++) {
2259 if (obj->nodeTab[i] == NULL) {
2260 fprintf(output, " NULL !\n");
2261 return;
2262 }
2263 if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) ||
2264 (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE))
2265 fprintf(output, " /");
2266 else if (obj->nodeTab[i]->name == NULL)
2267 fprintf(output, " noname!");
2268 else fprintf(output, " %s", obj->nodeTab[i]->name);
2269 }
2270 fprintf(output, "\n");
2271}
2272#endif
2273
2274/**
2275 * xmlXPathNewNodeSet:
2276 * @val: the NodePtr value
2277 *
2278 * Create a new xmlXPathObjectPtr of type NodeSet and initialize
2279 * it with the single Node @val
2280 *
2281 * Returns the newly created object.
2282 */
2283xmlXPathObjectPtr
2284xmlXPathNewNodeSet(xmlNodePtr val) {
2285 xmlXPathObjectPtr ret;
2286
2287 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2288 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002289 xmlXPathErrMemory(NULL, "creating nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002290 return(NULL);
2291 }
2292 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2293 ret->type = XPATH_NODESET;
Daniel Veillard77851712001-02-27 21:54:07 +00002294 ret->boolval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002295 ret->nodesetval = xmlXPathNodeSetCreate(val);
William M. Brack08171912003-12-29 02:52:11 +00002296 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00002297 return(ret);
2298}
2299
2300/**
2301 * xmlXPathNewValueTree:
2302 * @val: the NodePtr value
2303 *
2304 * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize
2305 * it with the tree root @val
2306 *
2307 * Returns the newly created object.
2308 */
2309xmlXPathObjectPtr
2310xmlXPathNewValueTree(xmlNodePtr val) {
2311 xmlXPathObjectPtr ret;
2312
2313 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2314 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002315 xmlXPathErrMemory(NULL, "creating result value tree\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002316 return(NULL);
2317 }
2318 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2319 ret->type = XPATH_XSLT_TREE;
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00002320 ret->boolval = 1;
2321 ret->user = (void *) val;
Owen Taylor3473f882001-02-23 17:55:21 +00002322 ret->nodesetval = xmlXPathNodeSetCreate(val);
2323 return(ret);
2324}
2325
2326/**
2327 * xmlXPathNewNodeSetList:
2328 * @val: an existing NodeSet
2329 *
2330 * Create a new xmlXPathObjectPtr of type NodeSet and initialize
2331 * it with the Nodeset @val
2332 *
2333 * Returns the newly created object.
2334 */
2335xmlXPathObjectPtr
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002336xmlXPathNewNodeSetList(xmlNodeSetPtr val)
2337{
Owen Taylor3473f882001-02-23 17:55:21 +00002338 xmlXPathObjectPtr ret;
2339 int i;
2340
2341 if (val == NULL)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002342 ret = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002343 else if (val->nodeTab == NULL)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002344 ret = xmlXPathNewNodeSet(NULL);
2345 else {
2346 ret = xmlXPathNewNodeSet(val->nodeTab[0]);
2347 for (i = 1; i < val->nodeNr; ++i)
2348 xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]);
2349 }
Owen Taylor3473f882001-02-23 17:55:21 +00002350
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002351 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002352}
2353
2354/**
2355 * xmlXPathWrapNodeSet:
2356 * @val: the NodePtr value
2357 *
2358 * Wrap the Nodeset @val in a new xmlXPathObjectPtr
2359 *
2360 * Returns the newly created object.
2361 */
2362xmlXPathObjectPtr
2363xmlXPathWrapNodeSet(xmlNodeSetPtr val) {
2364 xmlXPathObjectPtr ret;
2365
2366 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2367 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002368 xmlXPathErrMemory(NULL, "creating node set object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002369 return(NULL);
2370 }
2371 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2372 ret->type = XPATH_NODESET;
2373 ret->nodesetval = val;
2374 return(ret);
2375}
2376
2377/**
2378 * xmlXPathFreeNodeSetList:
2379 * @obj: an existing NodeSetList object
2380 *
2381 * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in
2382 * the list contrary to xmlXPathFreeObject().
2383 */
2384void
2385xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) {
2386 if (obj == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002387 xmlFree(obj);
2388}
2389
Thomas Broyerf06a3d82001-07-16 04:52:57 +00002390/**
2391 * xmlXPathDifference:
2392 * @nodes1: a node-set
2393 * @nodes2: a node-set
2394 *
2395 * Implements the EXSLT - Sets difference() function:
2396 * node-set set:difference (node-set, node-set)
2397 *
2398 * Returns the difference between the two node sets, or nodes1 if
2399 * nodes2 is empty
2400 */
2401xmlNodeSetPtr
2402xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2403 xmlNodeSetPtr ret;
2404 int i, l1;
2405 xmlNodePtr cur;
2406
2407 if (xmlXPathNodeSetIsEmpty(nodes2))
2408 return(nodes1);
2409
2410 ret = xmlXPathNodeSetCreate(NULL);
2411 if (xmlXPathNodeSetIsEmpty(nodes1))
2412 return(ret);
2413
2414 l1 = xmlXPathNodeSetGetLength(nodes1);
2415
2416 for (i = 0; i < l1; i++) {
2417 cur = xmlXPathNodeSetItem(nodes1, i);
2418 if (!xmlXPathNodeSetContains(nodes2, cur))
2419 xmlXPathNodeSetAddUnique(ret, cur);
2420 }
2421 return(ret);
2422}
2423
2424/**
2425 * xmlXPathIntersection:
2426 * @nodes1: a node-set
2427 * @nodes2: a node-set
2428 *
2429 * Implements the EXSLT - Sets intersection() function:
2430 * node-set set:intersection (node-set, node-set)
2431 *
2432 * Returns a node set comprising the nodes that are within both the
2433 * node sets passed as arguments
2434 */
2435xmlNodeSetPtr
2436xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2437 xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL);
2438 int i, l1;
2439 xmlNodePtr cur;
2440
2441 if (xmlXPathNodeSetIsEmpty(nodes1))
2442 return(ret);
2443 if (xmlXPathNodeSetIsEmpty(nodes2))
2444 return(ret);
2445
2446 l1 = xmlXPathNodeSetGetLength(nodes1);
2447
2448 for (i = 0; i < l1; i++) {
2449 cur = xmlXPathNodeSetItem(nodes1, i);
2450 if (xmlXPathNodeSetContains(nodes2, cur))
2451 xmlXPathNodeSetAddUnique(ret, cur);
2452 }
2453 return(ret);
2454}
2455
2456/**
2457 * xmlXPathDistinctSorted:
2458 * @nodes: a node-set, sorted by document order
2459 *
2460 * Implements the EXSLT - Sets distinct() function:
2461 * node-set set:distinct (node-set)
2462 *
2463 * Returns a subset of the nodes contained in @nodes, or @nodes if
2464 * it is empty
2465 */
2466xmlNodeSetPtr
2467xmlXPathDistinctSorted (xmlNodeSetPtr nodes) {
2468 xmlNodeSetPtr ret;
2469 xmlHashTablePtr hash;
2470 int i, l;
2471 xmlChar * strval;
2472 xmlNodePtr cur;
2473
2474 if (xmlXPathNodeSetIsEmpty(nodes))
2475 return(nodes);
2476
2477 ret = xmlXPathNodeSetCreate(NULL);
2478 l = xmlXPathNodeSetGetLength(nodes);
2479 hash = xmlHashCreate (l);
2480 for (i = 0; i < l; i++) {
2481 cur = xmlXPathNodeSetItem(nodes, i);
2482 strval = xmlXPathCastNodeToString(cur);
2483 if (xmlHashLookup(hash, strval) == NULL) {
2484 xmlHashAddEntry(hash, strval, strval);
2485 xmlXPathNodeSetAddUnique(ret, cur);
2486 } else {
2487 xmlFree(strval);
2488 }
2489 }
2490 xmlHashFree(hash, (xmlHashDeallocator) xmlFree);
2491 return(ret);
2492}
2493
2494/**
2495 * xmlXPathDistinct:
2496 * @nodes: a node-set
2497 *
2498 * Implements the EXSLT - Sets distinct() function:
2499 * node-set set:distinct (node-set)
2500 * @nodes is sorted by document order, then #exslSetsDistinctSorted
2501 * is called with the sorted node-set
2502 *
2503 * Returns a subset of the nodes contained in @nodes, or @nodes if
2504 * it is empty
2505 */
2506xmlNodeSetPtr
2507xmlXPathDistinct (xmlNodeSetPtr nodes) {
2508 if (xmlXPathNodeSetIsEmpty(nodes))
2509 return(nodes);
2510
2511 xmlXPathNodeSetSort(nodes);
2512 return(xmlXPathDistinctSorted(nodes));
2513}
2514
2515/**
2516 * xmlXPathHasSameNodes:
2517 * @nodes1: a node-set
2518 * @nodes2: a node-set
2519 *
2520 * Implements the EXSLT - Sets has-same-nodes function:
2521 * boolean set:has-same-node(node-set, node-set)
2522 *
2523 * Returns true (1) if @nodes1 shares any node with @nodes2, false (0)
2524 * otherwise
2525 */
2526int
2527xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2528 int i, l;
2529 xmlNodePtr cur;
2530
2531 if (xmlXPathNodeSetIsEmpty(nodes1) ||
2532 xmlXPathNodeSetIsEmpty(nodes2))
2533 return(0);
2534
2535 l = xmlXPathNodeSetGetLength(nodes1);
2536 for (i = 0; i < l; i++) {
2537 cur = xmlXPathNodeSetItem(nodes1, i);
2538 if (xmlXPathNodeSetContains(nodes2, cur))
2539 return(1);
2540 }
2541 return(0);
2542}
2543
2544/**
2545 * xmlXPathNodeLeadingSorted:
2546 * @nodes: a node-set, sorted by document order
2547 * @node: a node
2548 *
2549 * Implements the EXSLT - Sets leading() function:
2550 * node-set set:leading (node-set, node-set)
2551 *
2552 * Returns the nodes in @nodes that precede @node in document order,
2553 * @nodes if @node is NULL or an empty node-set if @nodes
2554 * doesn't contain @node
2555 */
2556xmlNodeSetPtr
2557xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) {
2558 int i, l;
2559 xmlNodePtr cur;
2560 xmlNodeSetPtr ret;
2561
2562 if (node == NULL)
2563 return(nodes);
2564
2565 ret = xmlXPathNodeSetCreate(NULL);
2566 if (xmlXPathNodeSetIsEmpty(nodes) ||
2567 (!xmlXPathNodeSetContains(nodes, node)))
2568 return(ret);
2569
2570 l = xmlXPathNodeSetGetLength(nodes);
2571 for (i = 0; i < l; i++) {
2572 cur = xmlXPathNodeSetItem(nodes, i);
2573 if (cur == node)
2574 break;
2575 xmlXPathNodeSetAddUnique(ret, cur);
2576 }
2577 return(ret);
2578}
2579
2580/**
2581 * xmlXPathNodeLeading:
2582 * @nodes: a node-set
2583 * @node: a node
2584 *
2585 * Implements the EXSLT - Sets leading() function:
2586 * node-set set:leading (node-set, node-set)
2587 * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted
2588 * is called.
2589 *
2590 * Returns the nodes in @nodes that precede @node in document order,
2591 * @nodes if @node is NULL or an empty node-set if @nodes
2592 * doesn't contain @node
2593 */
2594xmlNodeSetPtr
2595xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) {
2596 xmlXPathNodeSetSort(nodes);
2597 return(xmlXPathNodeLeadingSorted(nodes, node));
2598}
2599
2600/**
2601 * xmlXPathLeadingSorted:
2602 * @nodes1: a node-set, sorted by document order
2603 * @nodes2: a node-set, sorted by document order
2604 *
2605 * Implements the EXSLT - Sets leading() function:
2606 * node-set set:leading (node-set, node-set)
2607 *
2608 * Returns the nodes in @nodes1 that precede the first node in @nodes2
2609 * in document order, @nodes1 if @nodes2 is NULL or empty or
2610 * an empty node-set if @nodes1 doesn't contain @nodes2
2611 */
2612xmlNodeSetPtr
2613xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2614 if (xmlXPathNodeSetIsEmpty(nodes2))
2615 return(nodes1);
2616 return(xmlXPathNodeLeadingSorted(nodes1,
2617 xmlXPathNodeSetItem(nodes2, 1)));
2618}
2619
2620/**
2621 * xmlXPathLeading:
2622 * @nodes1: a node-set
2623 * @nodes2: a node-set
2624 *
2625 * Implements the EXSLT - Sets leading() function:
2626 * node-set set:leading (node-set, node-set)
2627 * @nodes1 and @nodes2 are sorted by document order, then
2628 * #exslSetsLeadingSorted is called.
2629 *
2630 * Returns the nodes in @nodes1 that precede the first node in @nodes2
2631 * in document order, @nodes1 if @nodes2 is NULL or empty or
2632 * an empty node-set if @nodes1 doesn't contain @nodes2
2633 */
2634xmlNodeSetPtr
2635xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2636 if (xmlXPathNodeSetIsEmpty(nodes2))
2637 return(nodes1);
2638 if (xmlXPathNodeSetIsEmpty(nodes1))
2639 return(xmlXPathNodeSetCreate(NULL));
2640 xmlXPathNodeSetSort(nodes1);
2641 xmlXPathNodeSetSort(nodes2);
2642 return(xmlXPathNodeLeadingSorted(nodes1,
2643 xmlXPathNodeSetItem(nodes2, 1)));
2644}
2645
2646/**
2647 * xmlXPathNodeTrailingSorted:
2648 * @nodes: a node-set, sorted by document order
2649 * @node: a node
2650 *
2651 * Implements the EXSLT - Sets trailing() function:
2652 * node-set set:trailing (node-set, node-set)
2653 *
2654 * Returns the nodes in @nodes that follow @node in document order,
2655 * @nodes if @node is NULL or an empty node-set if @nodes
2656 * doesn't contain @node
2657 */
2658xmlNodeSetPtr
2659xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) {
2660 int i, l;
2661 xmlNodePtr cur;
2662 xmlNodeSetPtr ret;
2663
2664 if (node == NULL)
2665 return(nodes);
2666
2667 ret = xmlXPathNodeSetCreate(NULL);
2668 if (xmlXPathNodeSetIsEmpty(nodes) ||
2669 (!xmlXPathNodeSetContains(nodes, node)))
2670 return(ret);
2671
2672 l = xmlXPathNodeSetGetLength(nodes);
Thomas Broyerf186c822001-07-31 23:30:37 +00002673 for (i = l; i > 0; i--) {
Thomas Broyerf06a3d82001-07-16 04:52:57 +00002674 cur = xmlXPathNodeSetItem(nodes, i);
2675 if (cur == node)
2676 break;
2677 xmlXPathNodeSetAddUnique(ret, cur);
2678 }
2679 return(ret);
2680}
2681
2682/**
2683 * xmlXPathNodeTrailing:
2684 * @nodes: a node-set
2685 * @node: a node
2686 *
2687 * Implements the EXSLT - Sets trailing() function:
2688 * node-set set:trailing (node-set, node-set)
2689 * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted
2690 * is called.
2691 *
2692 * Returns the nodes in @nodes that follow @node in document order,
2693 * @nodes if @node is NULL or an empty node-set if @nodes
2694 * doesn't contain @node
2695 */
2696xmlNodeSetPtr
2697xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) {
2698 xmlXPathNodeSetSort(nodes);
2699 return(xmlXPathNodeTrailingSorted(nodes, node));
2700}
2701
2702/**
2703 * xmlXPathTrailingSorted:
2704 * @nodes1: a node-set, sorted by document order
2705 * @nodes2: a node-set, sorted by document order
2706 *
2707 * Implements the EXSLT - Sets trailing() function:
2708 * node-set set:trailing (node-set, node-set)
2709 *
2710 * Returns the nodes in @nodes1 that follow the first node in @nodes2
2711 * in document order, @nodes1 if @nodes2 is NULL or empty or
2712 * an empty node-set if @nodes1 doesn't contain @nodes2
2713 */
2714xmlNodeSetPtr
2715xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2716 if (xmlXPathNodeSetIsEmpty(nodes2))
2717 return(nodes1);
2718 return(xmlXPathNodeTrailingSorted(nodes1,
2719 xmlXPathNodeSetItem(nodes2, 0)));
2720}
2721
2722/**
2723 * xmlXPathTrailing:
2724 * @nodes1: a node-set
2725 * @nodes2: a node-set
2726 *
2727 * Implements the EXSLT - Sets trailing() function:
2728 * node-set set:trailing (node-set, node-set)
2729 * @nodes1 and @nodes2 are sorted by document order, then
2730 * #xmlXPathTrailingSorted is called.
2731 *
2732 * Returns the nodes in @nodes1 that follow the first node in @nodes2
2733 * in document order, @nodes1 if @nodes2 is NULL or empty or
2734 * an empty node-set if @nodes1 doesn't contain @nodes2
2735 */
2736xmlNodeSetPtr
2737xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2738 if (xmlXPathNodeSetIsEmpty(nodes2))
2739 return(nodes1);
2740 if (xmlXPathNodeSetIsEmpty(nodes1))
2741 return(xmlXPathNodeSetCreate(NULL));
2742 xmlXPathNodeSetSort(nodes1);
2743 xmlXPathNodeSetSort(nodes2);
2744 return(xmlXPathNodeTrailingSorted(nodes1,
2745 xmlXPathNodeSetItem(nodes2, 0)));
2746}
2747
Owen Taylor3473f882001-02-23 17:55:21 +00002748/************************************************************************
2749 * *
2750 * Routines to handle extra functions *
2751 * *
2752 ************************************************************************/
2753
2754/**
2755 * xmlXPathRegisterFunc:
2756 * @ctxt: the XPath context
2757 * @name: the function name
2758 * @f: the function implementation or NULL
2759 *
2760 * Register a new function. If @f is NULL it unregisters the function
2761 *
2762 * Returns 0 in case of success, -1 in case of error
2763 */
2764int
2765xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name,
2766 xmlXPathFunction f) {
2767 return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f));
2768}
2769
2770/**
2771 * xmlXPathRegisterFuncNS:
2772 * @ctxt: the XPath context
2773 * @name: the function name
2774 * @ns_uri: the function namespace URI
2775 * @f: the function implementation or NULL
2776 *
2777 * Register a new function. If @f is NULL it unregisters the function
2778 *
2779 * Returns 0 in case of success, -1 in case of error
2780 */
2781int
2782xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2783 const xmlChar *ns_uri, xmlXPathFunction f) {
2784 if (ctxt == NULL)
2785 return(-1);
2786 if (name == NULL)
2787 return(-1);
2788
2789 if (ctxt->funcHash == NULL)
2790 ctxt->funcHash = xmlHashCreate(0);
2791 if (ctxt->funcHash == NULL)
2792 return(-1);
Daniel Veillard94394cd2003-10-29 17:07:51 +00002793 if (f == NULL)
2794 return(xmlHashRemoveEntry2(ctxt->funcHash, name, ns_uri, NULL));
Owen Taylor3473f882001-02-23 17:55:21 +00002795 return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, (void *) f));
2796}
2797
2798/**
Thomas Broyerba4ad322001-07-26 16:55:21 +00002799 * xmlXPathRegisterFuncLookup:
2800 * @ctxt: the XPath context
2801 * @f: the lookup function
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002802 * @funcCtxt: the lookup data
Thomas Broyerba4ad322001-07-26 16:55:21 +00002803 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002804 * Registers an external mechanism to do function lookup.
Thomas Broyerba4ad322001-07-26 16:55:21 +00002805 */
2806void
2807xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt,
2808 xmlXPathFuncLookupFunc f,
2809 void *funcCtxt) {
2810 if (ctxt == NULL)
2811 return;
2812 ctxt->funcLookupFunc = (void *) f;
2813 ctxt->funcLookupData = funcCtxt;
2814}
2815
2816/**
Owen Taylor3473f882001-02-23 17:55:21 +00002817 * xmlXPathFunctionLookup:
2818 * @ctxt: the XPath context
2819 * @name: the function name
2820 *
2821 * Search in the Function array of the context for the given
2822 * function.
2823 *
2824 * Returns the xmlXPathFunction or NULL if not found
2825 */
2826xmlXPathFunction
2827xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) {
Thomas Broyerba4ad322001-07-26 16:55:21 +00002828 if (ctxt == NULL)
2829 return (NULL);
2830
2831 if (ctxt->funcLookupFunc != NULL) {
2832 xmlXPathFunction ret;
Daniel Veillard99e55eb2002-01-21 08:56:29 +00002833 xmlXPathFuncLookupFunc f;
Thomas Broyerba4ad322001-07-26 16:55:21 +00002834
Daniel Veillard99e55eb2002-01-21 08:56:29 +00002835 f = (xmlXPathFuncLookupFunc) ctxt->funcLookupFunc;
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002836 ret = f(ctxt->funcLookupData, name, NULL);
Thomas Broyerba4ad322001-07-26 16:55:21 +00002837 if (ret != NULL)
2838 return(ret);
2839 }
Owen Taylor3473f882001-02-23 17:55:21 +00002840 return(xmlXPathFunctionLookupNS(ctxt, name, NULL));
2841}
2842
2843/**
2844 * xmlXPathFunctionLookupNS:
2845 * @ctxt: the XPath context
2846 * @name: the function name
2847 * @ns_uri: the function namespace URI
2848 *
2849 * Search in the Function array of the context for the given
2850 * function.
2851 *
2852 * Returns the xmlXPathFunction or NULL if not found
2853 */
2854xmlXPathFunction
2855xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2856 const xmlChar *ns_uri) {
2857 if (ctxt == NULL)
2858 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002859 if (name == NULL)
2860 return(NULL);
2861
Thomas Broyerba4ad322001-07-26 16:55:21 +00002862 if (ctxt->funcLookupFunc != NULL) {
2863 xmlXPathFunction ret;
Daniel Veillard99e55eb2002-01-21 08:56:29 +00002864 xmlXPathFuncLookupFunc f;
Thomas Broyerba4ad322001-07-26 16:55:21 +00002865
Daniel Veillard99e55eb2002-01-21 08:56:29 +00002866 f = (xmlXPathFuncLookupFunc) ctxt->funcLookupFunc;
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002867 ret = f(ctxt->funcLookupData, name, ns_uri);
Thomas Broyerba4ad322001-07-26 16:55:21 +00002868 if (ret != NULL)
2869 return(ret);
2870 }
2871
2872 if (ctxt->funcHash == NULL)
2873 return(NULL);
2874
Owen Taylor3473f882001-02-23 17:55:21 +00002875 return((xmlXPathFunction) xmlHashLookup2(ctxt->funcHash, name, ns_uri));
2876}
2877
2878/**
2879 * xmlXPathRegisteredFuncsCleanup:
2880 * @ctxt: the XPath context
2881 *
2882 * Cleanup the XPath context data associated to registered functions
2883 */
2884void
2885xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) {
2886 if (ctxt == NULL)
2887 return;
2888
2889 xmlHashFree(ctxt->funcHash, NULL);
2890 ctxt->funcHash = NULL;
2891}
2892
2893/************************************************************************
2894 * *
William M. Brack08171912003-12-29 02:52:11 +00002895 * Routines to handle Variables *
Owen Taylor3473f882001-02-23 17:55:21 +00002896 * *
2897 ************************************************************************/
2898
2899/**
2900 * xmlXPathRegisterVariable:
2901 * @ctxt: the XPath context
2902 * @name: the variable name
2903 * @value: the variable value or NULL
2904 *
2905 * Register a new variable value. If @value is NULL it unregisters
2906 * the variable
2907 *
2908 * Returns 0 in case of success, -1 in case of error
2909 */
2910int
2911xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name,
2912 xmlXPathObjectPtr value) {
2913 return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value));
2914}
2915
2916/**
2917 * xmlXPathRegisterVariableNS:
2918 * @ctxt: the XPath context
2919 * @name: the variable name
2920 * @ns_uri: the variable namespace URI
2921 * @value: the variable value or NULL
2922 *
2923 * Register a new variable value. If @value is NULL it unregisters
2924 * the variable
2925 *
2926 * Returns 0 in case of success, -1 in case of error
2927 */
2928int
2929xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2930 const xmlChar *ns_uri,
2931 xmlXPathObjectPtr value) {
2932 if (ctxt == NULL)
2933 return(-1);
2934 if (name == NULL)
2935 return(-1);
2936
2937 if (ctxt->varHash == NULL)
2938 ctxt->varHash = xmlHashCreate(0);
2939 if (ctxt->varHash == NULL)
2940 return(-1);
Daniel Veillard94394cd2003-10-29 17:07:51 +00002941 if (value == NULL)
2942 return(xmlHashRemoveEntry2(ctxt->varHash, name, ns_uri,
2943 (xmlHashDeallocator)xmlXPathFreeObject));
Owen Taylor3473f882001-02-23 17:55:21 +00002944 return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri,
2945 (void *) value,
2946 (xmlHashDeallocator)xmlXPathFreeObject));
2947}
2948
2949/**
2950 * xmlXPathRegisterVariableLookup:
2951 * @ctxt: the XPath context
2952 * @f: the lookup function
2953 * @data: the lookup data
2954 *
2955 * register an external mechanism to do variable lookup
2956 */
2957void
2958xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt,
2959 xmlXPathVariableLookupFunc f, void *data) {
2960 if (ctxt == NULL)
2961 return;
2962 ctxt->varLookupFunc = (void *) f;
2963 ctxt->varLookupData = data;
2964}
2965
2966/**
2967 * xmlXPathVariableLookup:
2968 * @ctxt: the XPath context
2969 * @name: the variable name
2970 *
2971 * Search in the Variable array of the context for the given
2972 * variable value.
2973 *
Daniel Veillard73c9c042001-07-05 20:02:54 +00002974 * Returns a copy of the value or NULL if not found
Owen Taylor3473f882001-02-23 17:55:21 +00002975 */
2976xmlXPathObjectPtr
2977xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) {
2978 if (ctxt == NULL)
2979 return(NULL);
2980
2981 if (ctxt->varLookupFunc != NULL) {
2982 xmlXPathObjectPtr ret;
2983
2984 ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc)
2985 (ctxt->varLookupData, name, NULL);
Daniel Veillard556c6682001-10-06 09:59:51 +00002986 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002987 }
2988 return(xmlXPathVariableLookupNS(ctxt, name, NULL));
2989}
2990
2991/**
2992 * xmlXPathVariableLookupNS:
2993 * @ctxt: the XPath context
2994 * @name: the variable name
2995 * @ns_uri: the variable namespace URI
2996 *
2997 * Search in the Variable array of the context for the given
Daniel Veillard73c9c042001-07-05 20:02:54 +00002998 * variable value.
Owen Taylor3473f882001-02-23 17:55:21 +00002999 *
Daniel Veillard73c9c042001-07-05 20:02:54 +00003000 * Returns the a copy of the value or NULL if not found
Owen Taylor3473f882001-02-23 17:55:21 +00003001 */
3002xmlXPathObjectPtr
3003xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name,
3004 const xmlChar *ns_uri) {
3005 if (ctxt == NULL)
3006 return(NULL);
3007
3008 if (ctxt->varLookupFunc != NULL) {
3009 xmlXPathObjectPtr ret;
3010
3011 ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc)
3012 (ctxt->varLookupData, name, ns_uri);
3013 if (ret != NULL) return(ret);
3014 }
3015
3016 if (ctxt->varHash == NULL)
3017 return(NULL);
3018 if (name == NULL)
3019 return(NULL);
3020
Daniel Veillard8c357d52001-07-03 23:43:33 +00003021 return(xmlXPathObjectCopy((xmlXPathObjectPtr)
3022 xmlHashLookup2(ctxt->varHash, name, ns_uri)));
Owen Taylor3473f882001-02-23 17:55:21 +00003023}
3024
3025/**
3026 * xmlXPathRegisteredVariablesCleanup:
3027 * @ctxt: the XPath context
3028 *
3029 * Cleanup the XPath context data associated to registered variables
3030 */
3031void
3032xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) {
3033 if (ctxt == NULL)
3034 return;
3035
Daniel Veillard76d66f42001-05-16 21:05:17 +00003036 xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject);
Owen Taylor3473f882001-02-23 17:55:21 +00003037 ctxt->varHash = NULL;
3038}
3039
3040/**
3041 * xmlXPathRegisterNs:
3042 * @ctxt: the XPath context
3043 * @prefix: the namespace prefix
3044 * @ns_uri: the namespace name
3045 *
3046 * Register a new namespace. If @ns_uri is NULL it unregisters
3047 * the namespace
3048 *
3049 * Returns 0 in case of success, -1 in case of error
3050 */
3051int
3052xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix,
3053 const xmlChar *ns_uri) {
3054 if (ctxt == NULL)
3055 return(-1);
3056 if (prefix == NULL)
3057 return(-1);
3058
3059 if (ctxt->nsHash == NULL)
3060 ctxt->nsHash = xmlHashCreate(10);
3061 if (ctxt->nsHash == NULL)
3062 return(-1);
Daniel Veillarde991fe92003-10-29 11:18:37 +00003063 if (ns_uri == NULL)
Daniel Veillard94394cd2003-10-29 17:07:51 +00003064 return(xmlHashRemoveEntry(ctxt->nsHash, prefix,
Daniel Veillarde991fe92003-10-29 11:18:37 +00003065 (xmlHashDeallocator)xmlFree));
Daniel Veillard42766c02002-08-22 20:52:17 +00003066 return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri),
Owen Taylor3473f882001-02-23 17:55:21 +00003067 (xmlHashDeallocator)xmlFree));
3068}
3069
3070/**
3071 * xmlXPathNsLookup:
3072 * @ctxt: the XPath context
3073 * @prefix: the namespace prefix value
3074 *
3075 * Search in the namespace declaration array of the context for the given
3076 * namespace name associated to the given prefix
3077 *
3078 * Returns the value or NULL if not found
3079 */
3080const xmlChar *
3081xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) {
3082 if (ctxt == NULL)
3083 return(NULL);
3084 if (prefix == NULL)
3085 return(NULL);
3086
3087#ifdef XML_XML_NAMESPACE
3088 if (xmlStrEqual(prefix, (const xmlChar *) "xml"))
3089 return(XML_XML_NAMESPACE);
3090#endif
3091
Daniel Veillardc8f620b2001-04-30 20:31:33 +00003092 if (ctxt->namespaces != NULL) {
3093 int i;
3094
3095 for (i = 0;i < ctxt->nsNr;i++) {
3096 if ((ctxt->namespaces[i] != NULL) &&
3097 (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix)))
3098 return(ctxt->namespaces[i]->href);
3099 }
3100 }
Owen Taylor3473f882001-02-23 17:55:21 +00003101
3102 return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix));
3103}
3104
3105/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003106 * xmlXPathRegisteredNsCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +00003107 * @ctxt: the XPath context
3108 *
3109 * Cleanup the XPath context data associated to registered variables
3110 */
3111void
3112xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) {
3113 if (ctxt == NULL)
3114 return;
3115
Daniel Veillard42766c02002-08-22 20:52:17 +00003116 xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree);
Owen Taylor3473f882001-02-23 17:55:21 +00003117 ctxt->nsHash = NULL;
3118}
3119
3120/************************************************************************
3121 * *
3122 * Routines to handle Values *
3123 * *
3124 ************************************************************************/
3125
William M. Brack08171912003-12-29 02:52:11 +00003126/* Allocations are terrible, one needs to optimize all this !!! */
Owen Taylor3473f882001-02-23 17:55:21 +00003127
3128/**
3129 * xmlXPathNewFloat:
3130 * @val: the double value
3131 *
3132 * Create a new xmlXPathObjectPtr of type double and of value @val
3133 *
3134 * Returns the newly created object.
3135 */
3136xmlXPathObjectPtr
3137xmlXPathNewFloat(double val) {
3138 xmlXPathObjectPtr ret;
3139
3140 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3141 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003142 xmlXPathErrMemory(NULL, "creating float object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003143 return(NULL);
3144 }
3145 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3146 ret->type = XPATH_NUMBER;
3147 ret->floatval = val;
3148 return(ret);
3149}
3150
3151/**
3152 * xmlXPathNewBoolean:
3153 * @val: the boolean value
3154 *
3155 * Create a new xmlXPathObjectPtr of type boolean and of value @val
3156 *
3157 * Returns the newly created object.
3158 */
3159xmlXPathObjectPtr
3160xmlXPathNewBoolean(int val) {
3161 xmlXPathObjectPtr ret;
3162
3163 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3164 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003165 xmlXPathErrMemory(NULL, "creating boolean object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003166 return(NULL);
3167 }
3168 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3169 ret->type = XPATH_BOOLEAN;
3170 ret->boolval = (val != 0);
3171 return(ret);
3172}
3173
3174/**
3175 * xmlXPathNewString:
3176 * @val: the xmlChar * value
3177 *
3178 * Create a new xmlXPathObjectPtr of type string and of value @val
3179 *
3180 * Returns the newly created object.
3181 */
3182xmlXPathObjectPtr
3183xmlXPathNewString(const xmlChar *val) {
3184 xmlXPathObjectPtr ret;
3185
3186 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3187 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003188 xmlXPathErrMemory(NULL, "creating string object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003189 return(NULL);
3190 }
3191 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3192 ret->type = XPATH_STRING;
3193 if (val != NULL)
3194 ret->stringval = xmlStrdup(val);
3195 else
3196 ret->stringval = xmlStrdup((const xmlChar *)"");
3197 return(ret);
3198}
3199
3200/**
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003201 * xmlXPathWrapString:
3202 * @val: the xmlChar * value
3203 *
3204 * Wraps the @val string into an XPath object.
3205 *
3206 * Returns the newly created object.
3207 */
3208xmlXPathObjectPtr
3209xmlXPathWrapString (xmlChar *val) {
3210 xmlXPathObjectPtr ret;
3211
3212 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3213 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003214 xmlXPathErrMemory(NULL, "creating string object\n");
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003215 return(NULL);
3216 }
3217 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3218 ret->type = XPATH_STRING;
3219 ret->stringval = val;
3220 return(ret);
3221}
3222
3223/**
Owen Taylor3473f882001-02-23 17:55:21 +00003224 * xmlXPathNewCString:
3225 * @val: the char * value
3226 *
3227 * Create a new xmlXPathObjectPtr of type string and of value @val
3228 *
3229 * Returns the newly created object.
3230 */
3231xmlXPathObjectPtr
3232xmlXPathNewCString(const char *val) {
3233 xmlXPathObjectPtr ret;
3234
3235 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3236 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003237 xmlXPathErrMemory(NULL, "creating string object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003238 return(NULL);
3239 }
3240 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3241 ret->type = XPATH_STRING;
3242 ret->stringval = xmlStrdup(BAD_CAST val);
3243 return(ret);
3244}
3245
3246/**
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003247 * xmlXPathWrapCString:
3248 * @val: the char * value
3249 *
3250 * Wraps a string into an XPath object.
3251 *
3252 * Returns the newly created object.
3253 */
3254xmlXPathObjectPtr
3255xmlXPathWrapCString (char * val) {
3256 return(xmlXPathWrapString((xmlChar *)(val)));
3257}
3258
3259/**
Thomas Broyerf06a3d82001-07-16 04:52:57 +00003260 * xmlXPathWrapExternal:
3261 * @val: the user data
3262 *
3263 * Wraps the @val data into an XPath object.
3264 *
3265 * Returns the newly created object.
3266 */
3267xmlXPathObjectPtr
3268xmlXPathWrapExternal (void *val) {
3269 xmlXPathObjectPtr ret;
3270
3271 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3272 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003273 xmlXPathErrMemory(NULL, "creating user object\n");
Thomas Broyerf06a3d82001-07-16 04:52:57 +00003274 return(NULL);
3275 }
3276 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3277 ret->type = XPATH_USERS;
3278 ret->user = val;
3279 return(ret);
3280}
3281
3282/**
Owen Taylor3473f882001-02-23 17:55:21 +00003283 * xmlXPathObjectCopy:
3284 * @val: the original object
3285 *
3286 * allocate a new copy of a given object
3287 *
3288 * Returns the newly created object.
3289 */
3290xmlXPathObjectPtr
3291xmlXPathObjectCopy(xmlXPathObjectPtr val) {
3292 xmlXPathObjectPtr ret;
3293
3294 if (val == NULL)
3295 return(NULL);
3296
3297 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3298 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003299 xmlXPathErrMemory(NULL, "copying object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003300 return(NULL);
3301 }
3302 memcpy(ret, val , (size_t) sizeof(xmlXPathObject));
3303 switch (val->type) {
3304 case XPATH_BOOLEAN:
3305 case XPATH_NUMBER:
3306 case XPATH_POINT:
3307 case XPATH_RANGE:
3308 break;
3309 case XPATH_STRING:
3310 ret->stringval = xmlStrdup(val->stringval);
3311 break;
3312 case XPATH_XSLT_TREE:
3313 if ((val->nodesetval != NULL) &&
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003314 (val->nodesetval->nodeTab != NULL)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003315 xmlNodePtr cur, tmp;
3316 xmlDocPtr top;
Daniel Veillardef0b4502003-03-24 13:57:34 +00003317
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003318 ret->boolval = 1;
Daniel Veillard9adc0462003-03-24 18:39:54 +00003319 top = xmlNewDoc(NULL);
3320 top->name = (char *)
3321 xmlStrdup(val->nodesetval->nodeTab[0]->name);
Daniel Veillardef0b4502003-03-24 13:57:34 +00003322 ret->user = top;
3323 if (top != NULL) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003324 top->doc = top;
Daniel Veillardef0b4502003-03-24 13:57:34 +00003325 cur = val->nodesetval->nodeTab[0]->children;
3326 while (cur != NULL) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003327 tmp = xmlDocCopyNode(cur, top, 1);
3328 xmlAddChild((xmlNodePtr) top, tmp);
Daniel Veillardef0b4502003-03-24 13:57:34 +00003329 cur = cur->next;
3330 }
3331 }
Daniel Veillard9adc0462003-03-24 18:39:54 +00003332 ret->nodesetval = xmlXPathNodeSetCreate((xmlNodePtr) top);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003333 } else
Owen Taylor3473f882001-02-23 17:55:21 +00003334 ret->nodesetval = xmlXPathNodeSetCreate(NULL);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003335 /* Deallocate the copied tree value */
Owen Taylor3473f882001-02-23 17:55:21 +00003336 break;
3337 case XPATH_NODESET:
3338 ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003339 /* Do not deallocate the copied tree value */
3340 ret->boolval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003341 break;
3342 case XPATH_LOCATIONSET:
3343#ifdef LIBXML_XPTR_ENABLED
3344 {
3345 xmlLocationSetPtr loc = val->user;
3346 ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc);
3347 break;
3348 }
3349#endif
Thomas Broyer47334c02001-10-07 16:41:52 +00003350 case XPATH_USERS:
3351 ret->user = val->user;
3352 break;
3353 case XPATH_UNDEFINED:
Owen Taylor3473f882001-02-23 17:55:21 +00003354 xmlGenericError(xmlGenericErrorContext,
3355 "xmlXPathObjectCopy: unsupported type %d\n",
3356 val->type);
3357 break;
3358 }
3359 return(ret);
3360}
3361
3362/**
3363 * xmlXPathFreeObject:
3364 * @obj: the object to free
3365 *
3366 * Free up an xmlXPathObjectPtr object.
3367 */
3368void
3369xmlXPathFreeObject(xmlXPathObjectPtr obj) {
3370 if (obj == NULL) return;
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003371 if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) {
Daniel Veillard77851712001-02-27 21:54:07 +00003372 if (obj->boolval) {
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003373 if (obj->user != NULL) {
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003374 xmlXPathFreeNodeSet(obj->nodesetval);
Daniel Veillard38bf6f02002-03-16 22:03:31 +00003375 xmlFreeNodeList((xmlNodePtr) obj->user);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003376 } else if (obj->nodesetval != NULL)
Daniel Veillard77851712001-02-27 21:54:07 +00003377 xmlXPathFreeValueTree(obj->nodesetval);
3378 } else {
3379 if (obj->nodesetval != NULL)
3380 xmlXPathFreeNodeSet(obj->nodesetval);
3381 }
Owen Taylor3473f882001-02-23 17:55:21 +00003382#ifdef LIBXML_XPTR_ENABLED
3383 } else if (obj->type == XPATH_LOCATIONSET) {
3384 if (obj->user != NULL)
3385 xmlXPtrFreeLocationSet(obj->user);
3386#endif
3387 } else if (obj->type == XPATH_STRING) {
3388 if (obj->stringval != NULL)
3389 xmlFree(obj->stringval);
Owen Taylor3473f882001-02-23 17:55:21 +00003390 }
3391
Owen Taylor3473f882001-02-23 17:55:21 +00003392 xmlFree(obj);
3393}
3394
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003395
3396/************************************************************************
3397 * *
3398 * Type Casting Routines *
3399 * *
3400 ************************************************************************/
3401
3402/**
3403 * xmlXPathCastBooleanToString:
3404 * @val: a boolean
3405 *
3406 * Converts a boolean to its string value.
3407 *
3408 * Returns a newly allocated string.
3409 */
3410xmlChar *
3411xmlXPathCastBooleanToString (int val) {
3412 xmlChar *ret;
3413 if (val)
3414 ret = xmlStrdup((const xmlChar *) "true");
3415 else
3416 ret = xmlStrdup((const xmlChar *) "false");
3417 return(ret);
3418}
3419
3420/**
3421 * xmlXPathCastNumberToString:
3422 * @val: a number
3423 *
3424 * Converts a number to its string value.
3425 *
3426 * Returns a newly allocated string.
3427 */
3428xmlChar *
3429xmlXPathCastNumberToString (double val) {
3430 xmlChar *ret;
Daniel Veillardcda96922001-08-21 10:56:31 +00003431 switch (xmlXPathIsInf(val)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003432 case 1:
Daniel Veillard5fc1f082002-03-27 09:05:40 +00003433 ret = xmlStrdup((const xmlChar *) "Infinity");
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003434 break;
3435 case -1:
3436 ret = xmlStrdup((const xmlChar *) "-Infinity");
3437 break;
3438 default:
Daniel Veillardcda96922001-08-21 10:56:31 +00003439 if (xmlXPathIsNaN(val)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003440 ret = xmlStrdup((const xmlChar *) "NaN");
Daniel Veillardd30be4a2002-03-28 18:25:31 +00003441 } else if (val == 0 && xmlXPathGetSign(val) != 0) {
3442 ret = xmlStrdup((const xmlChar *) "0");
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003443 } else {
3444 /* could be improved */
3445 char buf[100];
3446 xmlXPathFormatNumber(val, buf, 100);
3447 ret = xmlStrdup((const xmlChar *) buf);
3448 }
3449 }
3450 return(ret);
3451}
3452
3453/**
3454 * xmlXPathCastNodeToString:
3455 * @node: a node
3456 *
3457 * Converts a node to its string value.
3458 *
3459 * Returns a newly allocated string.
3460 */
3461xmlChar *
3462xmlXPathCastNodeToString (xmlNodePtr node) {
3463 return(xmlNodeGetContent(node));
3464}
3465
3466/**
3467 * xmlXPathCastNodeSetToString:
3468 * @ns: a node-set
3469 *
3470 * Converts a node-set to its string value.
3471 *
3472 * Returns a newly allocated string.
3473 */
3474xmlChar *
3475xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) {
3476 if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL))
3477 return(xmlStrdup((const xmlChar *) ""));
3478
3479 xmlXPathNodeSetSort(ns);
3480 return(xmlXPathCastNodeToString(ns->nodeTab[0]));
3481}
3482
3483/**
3484 * xmlXPathCastToString:
3485 * @val: an XPath object
3486 *
3487 * Converts an existing object to its string() equivalent
3488 *
3489 * Returns the string value of the object, NULL in case of error.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003490 * A new string is allocated only if needed (@val isn't a
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003491 * string object).
3492 */
3493xmlChar *
3494xmlXPathCastToString(xmlXPathObjectPtr val) {
3495 xmlChar *ret = NULL;
3496
3497 if (val == NULL)
3498 return(xmlStrdup((const xmlChar *) ""));
3499 switch (val->type) {
3500 case XPATH_UNDEFINED:
3501#ifdef DEBUG_EXPR
3502 xmlGenericError(xmlGenericErrorContext, "String: undefined\n");
3503#endif
3504 ret = xmlStrdup((const xmlChar *) "");
3505 break;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003506 case XPATH_NODESET:
William M. Brack0c022ad2002-07-12 00:56:01 +00003507 case XPATH_XSLT_TREE:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003508 ret = xmlXPathCastNodeSetToString(val->nodesetval);
3509 break;
3510 case XPATH_STRING:
Daniel Veillard4e2df542002-03-22 12:23:14 +00003511 return(xmlStrdup(val->stringval));
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003512 case XPATH_BOOLEAN:
3513 ret = xmlXPathCastBooleanToString(val->boolval);
3514 break;
3515 case XPATH_NUMBER: {
3516 ret = xmlXPathCastNumberToString(val->floatval);
3517 break;
3518 }
3519 case XPATH_USERS:
3520 case XPATH_POINT:
3521 case XPATH_RANGE:
3522 case XPATH_LOCATIONSET:
3523 TODO
3524 ret = xmlStrdup((const xmlChar *) "");
3525 break;
3526 }
3527 return(ret);
3528}
3529
3530/**
3531 * xmlXPathConvertString:
3532 * @val: an XPath object
3533 *
3534 * Converts an existing object to its string() equivalent
3535 *
3536 * Returns the new object, the old one is freed (or the operation
3537 * is done directly on @val)
3538 */
3539xmlXPathObjectPtr
3540xmlXPathConvertString(xmlXPathObjectPtr val) {
3541 xmlChar *res = NULL;
3542
3543 if (val == NULL)
3544 return(xmlXPathNewCString(""));
3545
3546 switch (val->type) {
3547 case XPATH_UNDEFINED:
3548#ifdef DEBUG_EXPR
3549 xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n");
3550#endif
3551 break;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003552 case XPATH_NODESET:
William M. Brack0c022ad2002-07-12 00:56:01 +00003553 case XPATH_XSLT_TREE:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003554 res = xmlXPathCastNodeSetToString(val->nodesetval);
3555 break;
3556 case XPATH_STRING:
3557 return(val);
3558 case XPATH_BOOLEAN:
3559 res = xmlXPathCastBooleanToString(val->boolval);
3560 break;
3561 case XPATH_NUMBER:
3562 res = xmlXPathCastNumberToString(val->floatval);
3563 break;
3564 case XPATH_USERS:
3565 case XPATH_POINT:
3566 case XPATH_RANGE:
3567 case XPATH_LOCATIONSET:
3568 TODO;
3569 break;
3570 }
3571 xmlXPathFreeObject(val);
3572 if (res == NULL)
3573 return(xmlXPathNewCString(""));
3574 return(xmlXPathWrapString(res));
3575}
3576
3577/**
3578 * xmlXPathCastBooleanToNumber:
3579 * @val: a boolean
3580 *
3581 * Converts a boolean to its number value
3582 *
3583 * Returns the number value
3584 */
3585double
3586xmlXPathCastBooleanToNumber(int val) {
3587 if (val)
3588 return(1.0);
3589 return(0.0);
3590}
3591
3592/**
3593 * xmlXPathCastStringToNumber:
3594 * @val: a string
3595 *
3596 * Converts a string to its number value
3597 *
3598 * Returns the number value
3599 */
3600double
3601xmlXPathCastStringToNumber(const xmlChar * val) {
3602 return(xmlXPathStringEvalNumber(val));
3603}
3604
3605/**
3606 * xmlXPathCastNodeToNumber:
3607 * @node: a node
3608 *
3609 * Converts a node to its number value
3610 *
3611 * Returns the number value
3612 */
3613double
3614xmlXPathCastNodeToNumber (xmlNodePtr node) {
3615 xmlChar *strval;
3616 double ret;
3617
3618 if (node == NULL)
3619 return(xmlXPathNAN);
3620 strval = xmlXPathCastNodeToString(node);
3621 if (strval == NULL)
3622 return(xmlXPathNAN);
3623 ret = xmlXPathCastStringToNumber(strval);
3624 xmlFree(strval);
3625
3626 return(ret);
3627}
3628
3629/**
3630 * xmlXPathCastNodeSetToNumber:
3631 * @ns: a node-set
3632 *
3633 * Converts a node-set to its number value
3634 *
3635 * Returns the number value
3636 */
3637double
3638xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) {
3639 xmlChar *str;
3640 double ret;
3641
3642 if (ns == NULL)
3643 return(xmlXPathNAN);
3644 str = xmlXPathCastNodeSetToString(ns);
3645 ret = xmlXPathCastStringToNumber(str);
3646 xmlFree(str);
3647 return(ret);
3648}
3649
3650/**
3651 * xmlXPathCastToNumber:
3652 * @val: an XPath object
3653 *
3654 * Converts an XPath object to its number value
3655 *
3656 * Returns the number value
3657 */
3658double
3659xmlXPathCastToNumber(xmlXPathObjectPtr val) {
3660 double ret = 0.0;
3661
3662 if (val == NULL)
3663 return(xmlXPathNAN);
3664 switch (val->type) {
3665 case XPATH_UNDEFINED:
3666#ifdef DEGUB_EXPR
3667 xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n");
3668#endif
3669 ret = xmlXPathNAN;
3670 break;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003671 case XPATH_NODESET:
William M. Brack0c022ad2002-07-12 00:56:01 +00003672 case XPATH_XSLT_TREE:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003673 ret = xmlXPathCastNodeSetToNumber(val->nodesetval);
3674 break;
3675 case XPATH_STRING:
3676 ret = xmlXPathCastStringToNumber(val->stringval);
3677 break;
3678 case XPATH_NUMBER:
3679 ret = val->floatval;
3680 break;
3681 case XPATH_BOOLEAN:
3682 ret = xmlXPathCastBooleanToNumber(val->boolval);
3683 break;
3684 case XPATH_USERS:
3685 case XPATH_POINT:
3686 case XPATH_RANGE:
3687 case XPATH_LOCATIONSET:
3688 TODO;
3689 ret = xmlXPathNAN;
3690 break;
3691 }
3692 return(ret);
3693}
3694
3695/**
3696 * xmlXPathConvertNumber:
3697 * @val: an XPath object
3698 *
3699 * Converts an existing object to its number() equivalent
3700 *
3701 * Returns the new object, the old one is freed (or the operation
3702 * is done directly on @val)
3703 */
3704xmlXPathObjectPtr
3705xmlXPathConvertNumber(xmlXPathObjectPtr val) {
3706 xmlXPathObjectPtr ret;
3707
3708 if (val == NULL)
3709 return(xmlXPathNewFloat(0.0));
3710 if (val->type == XPATH_NUMBER)
3711 return(val);
3712 ret = xmlXPathNewFloat(xmlXPathCastToNumber(val));
3713 xmlXPathFreeObject(val);
3714 return(ret);
3715}
3716
3717/**
3718 * xmlXPathCastNumberToBoolean:
3719 * @val: a number
3720 *
3721 * Converts a number to its boolean value
3722 *
3723 * Returns the boolean value
3724 */
3725int
3726xmlXPathCastNumberToBoolean (double val) {
Daniel Veillardcda96922001-08-21 10:56:31 +00003727 if (xmlXPathIsNaN(val) || (val == 0.0))
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003728 return(0);
3729 return(1);
3730}
3731
3732/**
3733 * xmlXPathCastStringToBoolean:
3734 * @val: a string
3735 *
3736 * Converts a string to its boolean value
3737 *
3738 * Returns the boolean value
3739 */
3740int
3741xmlXPathCastStringToBoolean (const xmlChar *val) {
3742 if ((val == NULL) || (xmlStrlen(val) == 0))
3743 return(0);
3744 return(1);
3745}
3746
3747/**
3748 * xmlXPathCastNodeSetToBoolean:
3749 * @ns: a node-set
3750 *
3751 * Converts a node-set to its boolean value
3752 *
3753 * Returns the boolean value
3754 */
3755int
3756xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) {
3757 if ((ns == NULL) || (ns->nodeNr == 0))
3758 return(0);
3759 return(1);
3760}
3761
3762/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003763 * xmlXPathCastToBoolean:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003764 * @val: an XPath object
3765 *
3766 * Converts an XPath object to its boolean value
3767 *
3768 * Returns the boolean value
3769 */
3770int
3771xmlXPathCastToBoolean (xmlXPathObjectPtr val) {
3772 int ret = 0;
3773
3774 if (val == NULL)
3775 return(0);
3776 switch (val->type) {
3777 case XPATH_UNDEFINED:
3778#ifdef DEBUG_EXPR
3779 xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n");
3780#endif
3781 ret = 0;
3782 break;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003783 case XPATH_NODESET:
William M. Brack0c022ad2002-07-12 00:56:01 +00003784 case XPATH_XSLT_TREE:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003785 ret = xmlXPathCastNodeSetToBoolean(val->nodesetval);
3786 break;
3787 case XPATH_STRING:
3788 ret = xmlXPathCastStringToBoolean(val->stringval);
3789 break;
3790 case XPATH_NUMBER:
3791 ret = xmlXPathCastNumberToBoolean(val->floatval);
3792 break;
3793 case XPATH_BOOLEAN:
3794 ret = val->boolval;
3795 break;
3796 case XPATH_USERS:
3797 case XPATH_POINT:
3798 case XPATH_RANGE:
3799 case XPATH_LOCATIONSET:
3800 TODO;
3801 ret = 0;
3802 break;
3803 }
3804 return(ret);
3805}
3806
3807
3808/**
3809 * xmlXPathConvertBoolean:
3810 * @val: an XPath object
3811 *
3812 * Converts an existing object to its boolean() equivalent
3813 *
3814 * Returns the new object, the old one is freed (or the operation
3815 * is done directly on @val)
3816 */
3817xmlXPathObjectPtr
3818xmlXPathConvertBoolean(xmlXPathObjectPtr val) {
3819 xmlXPathObjectPtr ret;
3820
3821 if (val == NULL)
3822 return(xmlXPathNewBoolean(0));
3823 if (val->type == XPATH_BOOLEAN)
3824 return(val);
3825 ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val));
3826 xmlXPathFreeObject(val);
3827 return(ret);
3828}
3829
Owen Taylor3473f882001-02-23 17:55:21 +00003830/************************************************************************
3831 * *
3832 * Routines to handle XPath contexts *
3833 * *
3834 ************************************************************************/
3835
3836/**
3837 * xmlXPathNewContext:
3838 * @doc: the XML document
3839 *
3840 * Create a new xmlXPathContext
3841 *
Daniel Veillardaf43f632002-03-08 15:05:20 +00003842 * Returns the xmlXPathContext just allocated. The caller will need to free it.
Owen Taylor3473f882001-02-23 17:55:21 +00003843 */
3844xmlXPathContextPtr
3845xmlXPathNewContext(xmlDocPtr doc) {
3846 xmlXPathContextPtr ret;
3847
3848 ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext));
3849 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003850 xmlXPathErrMemory(NULL, "creating context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003851 return(NULL);
3852 }
3853 memset(ret, 0 , (size_t) sizeof(xmlXPathContext));
3854 ret->doc = doc;
3855 ret->node = NULL;
3856
3857 ret->varHash = NULL;
3858
3859 ret->nb_types = 0;
3860 ret->max_types = 0;
3861 ret->types = NULL;
3862
3863 ret->funcHash = xmlHashCreate(0);
3864
3865 ret->nb_axis = 0;
3866 ret->max_axis = 0;
3867 ret->axis = NULL;
3868
3869 ret->nsHash = NULL;
3870 ret->user = NULL;
3871
3872 ret->contextSize = -1;
3873 ret->proximityPosition = -1;
3874
3875 xmlXPathRegisterAllFunctions(ret);
3876
3877 return(ret);
3878}
3879
3880/**
3881 * xmlXPathFreeContext:
3882 * @ctxt: the context to free
3883 *
3884 * Free up an xmlXPathContext
3885 */
3886void
3887xmlXPathFreeContext(xmlXPathContextPtr ctxt) {
3888 xmlXPathRegisteredNsCleanup(ctxt);
3889 xmlXPathRegisteredFuncsCleanup(ctxt);
3890 xmlXPathRegisteredVariablesCleanup(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003891 xmlFree(ctxt);
3892}
3893
3894/************************************************************************
3895 * *
3896 * Routines to handle XPath parser contexts *
3897 * *
3898 ************************************************************************/
3899
3900#define CHECK_CTXT(ctxt) \
3901 if (ctxt == NULL) { \
3902 xmlGenericError(xmlGenericErrorContext, \
3903 "%s:%d Internal error: ctxt == NULL\n", \
3904 __FILE__, __LINE__); \
3905 } \
3906
3907
3908#define CHECK_CONTEXT(ctxt) \
3909 if (ctxt == NULL) { \
3910 xmlGenericError(xmlGenericErrorContext, \
3911 "%s:%d Internal error: no context\n", \
3912 __FILE__, __LINE__); \
3913 } \
3914 else if (ctxt->doc == NULL) { \
3915 xmlGenericError(xmlGenericErrorContext, \
3916 "%s:%d Internal error: no document\n", \
3917 __FILE__, __LINE__); \
3918 } \
3919 else if (ctxt->doc->children == NULL) { \
3920 xmlGenericError(xmlGenericErrorContext, \
3921 "%s:%d Internal error: document without root\n", \
3922 __FILE__, __LINE__); \
3923 } \
3924
3925
3926/**
3927 * xmlXPathNewParserContext:
3928 * @str: the XPath expression
3929 * @ctxt: the XPath context
3930 *
3931 * Create a new xmlXPathParserContext
3932 *
3933 * Returns the xmlXPathParserContext just allocated.
3934 */
3935xmlXPathParserContextPtr
3936xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) {
3937 xmlXPathParserContextPtr ret;
3938
3939 ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext));
3940 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003941 xmlXPathErrMemory(ctxt, "creating parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003942 return(NULL);
3943 }
3944 memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext));
3945 ret->cur = ret->base = str;
3946 ret->context = ctxt;
3947
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003948 ret->comp = xmlXPathNewCompExpr();
3949 if (ret->comp == NULL) {
3950 xmlFree(ret->valueTab);
3951 xmlFree(ret);
3952 return(NULL);
3953 }
Daniel Veillard4773df22004-01-23 13:15:13 +00003954 if ((ctxt != NULL) && (ctxt->dict != NULL)) {
3955 ret->comp->dict = ctxt->dict;
3956 xmlDictReference(ret->comp->dict);
3957 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003958
3959 return(ret);
3960}
3961
3962/**
3963 * xmlXPathCompParserContext:
3964 * @comp: the XPath compiled expression
3965 * @ctxt: the XPath context
3966 *
3967 * Create a new xmlXPathParserContext when processing a compiled expression
3968 *
3969 * Returns the xmlXPathParserContext just allocated.
3970 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003971static xmlXPathParserContextPtr
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003972xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) {
3973 xmlXPathParserContextPtr ret;
3974
3975 ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext));
3976 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003977 xmlXPathErrMemory(ctxt, "creating evaluation context\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003978 return(NULL);
3979 }
3980 memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext));
3981
Owen Taylor3473f882001-02-23 17:55:21 +00003982 /* Allocate the value stack */
3983 ret->valueTab = (xmlXPathObjectPtr *)
3984 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003985 if (ret->valueTab == NULL) {
3986 xmlFree(ret);
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003987 xmlXPathErrMemory(ctxt, "creating evaluation context\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003988 return(NULL);
3989 }
Owen Taylor3473f882001-02-23 17:55:21 +00003990 ret->valueNr = 0;
3991 ret->valueMax = 10;
3992 ret->value = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003993
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00003994 ret->context = ctxt;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003995 ret->comp = comp;
3996
Owen Taylor3473f882001-02-23 17:55:21 +00003997 return(ret);
3998}
3999
4000/**
4001 * xmlXPathFreeParserContext:
4002 * @ctxt: the context to free
4003 *
4004 * Free up an xmlXPathParserContext
4005 */
4006void
4007xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) {
4008 if (ctxt->valueTab != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004009 xmlFree(ctxt->valueTab);
4010 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004011 if (ctxt->comp)
4012 xmlXPathFreeCompExpr(ctxt->comp);
Owen Taylor3473f882001-02-23 17:55:21 +00004013 xmlFree(ctxt);
4014}
4015
4016/************************************************************************
4017 * *
4018 * The implicit core function library *
4019 * *
4020 ************************************************************************/
4021
Owen Taylor3473f882001-02-23 17:55:21 +00004022/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004023 * xmlXPathNodeValHash:
Daniel Veillardf06307e2001-07-03 10:35:50 +00004024 * @node: a node pointer
4025 *
4026 * Function computing the beginning of the string value of the node,
4027 * used to speed up comparisons
4028 *
4029 * Returns an int usable as a hash
4030 */
4031static unsigned int
4032xmlXPathNodeValHash(xmlNodePtr node) {
4033 int len = 2;
4034 const xmlChar * string = NULL;
4035 xmlNodePtr tmp = NULL;
4036 unsigned int ret = 0;
4037
4038 if (node == NULL)
4039 return(0);
4040
Daniel Veillard9adc0462003-03-24 18:39:54 +00004041 if (node->type == XML_DOCUMENT_NODE) {
4042 tmp = xmlDocGetRootElement((xmlDocPtr) node);
4043 if (tmp == NULL)
4044 node = node->children;
4045 else
4046 node = tmp;
4047
4048 if (node == NULL)
4049 return(0);
4050 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004051
4052 switch (node->type) {
4053 case XML_COMMENT_NODE:
4054 case XML_PI_NODE:
4055 case XML_CDATA_SECTION_NODE:
4056 case XML_TEXT_NODE:
4057 string = node->content;
4058 if (string == NULL)
4059 return(0);
4060 if (string[0] == 0)
4061 return(0);
4062 return(((unsigned int) string[0]) +
4063 (((unsigned int) string[1]) << 8));
4064 case XML_NAMESPACE_DECL:
4065 string = ((xmlNsPtr)node)->href;
4066 if (string == NULL)
4067 return(0);
4068 if (string[0] == 0)
4069 return(0);
4070 return(((unsigned int) string[0]) +
4071 (((unsigned int) string[1]) << 8));
4072 case XML_ATTRIBUTE_NODE:
4073 tmp = ((xmlAttrPtr) node)->children;
4074 break;
4075 case XML_ELEMENT_NODE:
4076 tmp = node->children;
4077 break;
4078 default:
4079 return(0);
4080 }
4081 while (tmp != NULL) {
4082 switch (tmp->type) {
4083 case XML_COMMENT_NODE:
4084 case XML_PI_NODE:
4085 case XML_CDATA_SECTION_NODE:
4086 case XML_TEXT_NODE:
4087 string = tmp->content;
4088 break;
4089 case XML_NAMESPACE_DECL:
4090 string = ((xmlNsPtr)tmp)->href;
4091 break;
4092 default:
4093 break;
4094 }
4095 if ((string != NULL) && (string[0] != 0)) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00004096 if (len == 1) {
4097 return(ret + (((unsigned int) string[0]) << 8));
4098 }
4099 if (string[1] == 0) {
4100 len = 1;
4101 ret = (unsigned int) string[0];
4102 } else {
4103 return(((unsigned int) string[0]) +
4104 (((unsigned int) string[1]) << 8));
4105 }
4106 }
4107 /*
4108 * Skip to next node
4109 */
4110 if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) {
4111 if (tmp->children->type != XML_ENTITY_DECL) {
4112 tmp = tmp->children;
4113 continue;
4114 }
4115 }
4116 if (tmp == node)
4117 break;
4118
4119 if (tmp->next != NULL) {
4120 tmp = tmp->next;
4121 continue;
4122 }
4123
4124 do {
4125 tmp = tmp->parent;
4126 if (tmp == NULL)
4127 break;
4128 if (tmp == node) {
4129 tmp = NULL;
4130 break;
4131 }
4132 if (tmp->next != NULL) {
4133 tmp = tmp->next;
4134 break;
4135 }
4136 } while (tmp != NULL);
4137 }
4138 return(ret);
4139}
4140
4141/**
4142 * xmlXPathStringHash:
4143 * @string: a string
4144 *
4145 * Function computing the beginning of the string value of the node,
4146 * used to speed up comparisons
4147 *
4148 * Returns an int usable as a hash
4149 */
4150static unsigned int
4151xmlXPathStringHash(const xmlChar * string) {
4152 if (string == NULL)
4153 return((unsigned int) 0);
4154 if (string[0] == 0)
4155 return(0);
4156 return(((unsigned int) string[0]) +
4157 (((unsigned int) string[1]) << 8));
4158}
4159
4160/**
Owen Taylor3473f882001-02-23 17:55:21 +00004161 * xmlXPathCompareNodeSetFloat:
4162 * @ctxt: the XPath Parser context
4163 * @inf: less than (1) or greater than (0)
4164 * @strict: is the comparison strict
4165 * @arg: the node set
4166 * @f: the value
4167 *
4168 * Implement the compare operation between a nodeset and a number
4169 * @ns < @val (1, 1, ...
4170 * @ns <= @val (1, 0, ...
4171 * @ns > @val (0, 1, ...
4172 * @ns >= @val (0, 0, ...
4173 *
4174 * If one object to be compared is a node-set and the other is a number,
4175 * then the comparison will be true if and only if there is a node in the
4176 * node-set such that the result of performing the comparison on the number
4177 * to be compared and on the result of converting the string-value of that
4178 * node to a number using the number function is true.
4179 *
4180 * Returns 0 or 1 depending on the results of the test.
4181 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004182static int
Owen Taylor3473f882001-02-23 17:55:21 +00004183xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict,
4184 xmlXPathObjectPtr arg, xmlXPathObjectPtr f) {
4185 int i, ret = 0;
4186 xmlNodeSetPtr ns;
4187 xmlChar *str2;
4188
4189 if ((f == NULL) || (arg == NULL) ||
4190 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) {
4191 xmlXPathFreeObject(arg);
4192 xmlXPathFreeObject(f);
4193 return(0);
4194 }
4195 ns = arg->nodesetval;
Daniel Veillard911f49a2001-04-07 15:39:35 +00004196 if (ns != NULL) {
4197 for (i = 0;i < ns->nodeNr;i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004198 str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
Daniel Veillard911f49a2001-04-07 15:39:35 +00004199 if (str2 != NULL) {
4200 valuePush(ctxt,
4201 xmlXPathNewString(str2));
4202 xmlFree(str2);
4203 xmlXPathNumberFunction(ctxt, 1);
4204 valuePush(ctxt, xmlXPathObjectCopy(f));
4205 ret = xmlXPathCompareValues(ctxt, inf, strict);
4206 if (ret)
4207 break;
4208 }
4209 }
Owen Taylor3473f882001-02-23 17:55:21 +00004210 }
4211 xmlXPathFreeObject(arg);
4212 xmlXPathFreeObject(f);
4213 return(ret);
4214}
4215
4216/**
4217 * xmlXPathCompareNodeSetString:
4218 * @ctxt: the XPath Parser context
4219 * @inf: less than (1) or greater than (0)
4220 * @strict: is the comparison strict
4221 * @arg: the node set
4222 * @s: the value
4223 *
4224 * Implement the compare operation between a nodeset and a string
4225 * @ns < @val (1, 1, ...
4226 * @ns <= @val (1, 0, ...
4227 * @ns > @val (0, 1, ...
4228 * @ns >= @val (0, 0, ...
4229 *
4230 * If one object to be compared is a node-set and the other is a string,
4231 * then the comparison will be true if and only if there is a node in
4232 * the node-set such that the result of performing the comparison on the
4233 * string-value of the node and the other string is true.
4234 *
4235 * Returns 0 or 1 depending on the results of the test.
4236 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004237static int
Owen Taylor3473f882001-02-23 17:55:21 +00004238xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict,
4239 xmlXPathObjectPtr arg, xmlXPathObjectPtr s) {
4240 int i, ret = 0;
4241 xmlNodeSetPtr ns;
4242 xmlChar *str2;
4243
4244 if ((s == NULL) || (arg == NULL) ||
4245 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) {
4246 xmlXPathFreeObject(arg);
4247 xmlXPathFreeObject(s);
4248 return(0);
4249 }
4250 ns = arg->nodesetval;
Daniel Veillard911f49a2001-04-07 15:39:35 +00004251 if (ns != NULL) {
4252 for (i = 0;i < ns->nodeNr;i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004253 str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
Daniel Veillard911f49a2001-04-07 15:39:35 +00004254 if (str2 != NULL) {
4255 valuePush(ctxt,
4256 xmlXPathNewString(str2));
4257 xmlFree(str2);
4258 valuePush(ctxt, xmlXPathObjectCopy(s));
4259 ret = xmlXPathCompareValues(ctxt, inf, strict);
4260 if (ret)
4261 break;
4262 }
4263 }
Owen Taylor3473f882001-02-23 17:55:21 +00004264 }
4265 xmlXPathFreeObject(arg);
4266 xmlXPathFreeObject(s);
4267 return(ret);
4268}
4269
4270/**
4271 * xmlXPathCompareNodeSets:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004272 * @inf: less than (1) or greater than (0)
Owen Taylor3473f882001-02-23 17:55:21 +00004273 * @strict: is the comparison strict
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004274 * @arg1: the first node set object
Owen Taylor3473f882001-02-23 17:55:21 +00004275 * @arg2: the second node set object
4276 *
4277 * Implement the compare operation on nodesets:
4278 *
4279 * If both objects to be compared are node-sets, then the comparison
4280 * will be true if and only if there is a node in the first node-set
4281 * and a node in the second node-set such that the result of performing
4282 * the comparison on the string-values of the two nodes is true.
4283 * ....
4284 * When neither object to be compared is a node-set and the operator
4285 * is <=, <, >= or >, then the objects are compared by converting both
4286 * objects to numbers and comparing the numbers according to IEEE 754.
4287 * ....
4288 * The number function converts its argument to a number as follows:
4289 * - a string that consists of optional whitespace followed by an
4290 * optional minus sign followed by a Number followed by whitespace
4291 * is converted to the IEEE 754 number that is nearest (according
4292 * to the IEEE 754 round-to-nearest rule) to the mathematical value
4293 * represented by the string; any other string is converted to NaN
4294 *
4295 * Conclusion all nodes need to be converted first to their string value
4296 * and then the comparison must be done when possible
4297 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004298static int
4299xmlXPathCompareNodeSets(int inf, int strict,
Owen Taylor3473f882001-02-23 17:55:21 +00004300 xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) {
4301 int i, j, init = 0;
4302 double val1;
4303 double *values2;
4304 int ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004305 xmlNodeSetPtr ns1;
4306 xmlNodeSetPtr ns2;
4307
4308 if ((arg1 == NULL) ||
Daniel Veillard4dd93462001-04-02 15:16:19 +00004309 ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) {
4310 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004311 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004312 }
Owen Taylor3473f882001-02-23 17:55:21 +00004313 if ((arg2 == NULL) ||
Daniel Veillard4dd93462001-04-02 15:16:19 +00004314 ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) {
4315 xmlXPathFreeObject(arg1);
4316 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004317 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004318 }
Owen Taylor3473f882001-02-23 17:55:21 +00004319
4320 ns1 = arg1->nodesetval;
4321 ns2 = arg2->nodesetval;
4322
Daniel Veillardd8df6c02001-04-05 16:54:14 +00004323 if ((ns1 == NULL) || (ns1->nodeNr <= 0)) {
Daniel Veillard4dd93462001-04-02 15:16:19 +00004324 xmlXPathFreeObject(arg1);
4325 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004326 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004327 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00004328 if ((ns2 == NULL) || (ns2->nodeNr <= 0)) {
Daniel Veillard4dd93462001-04-02 15:16:19 +00004329 xmlXPathFreeObject(arg1);
4330 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004331 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004332 }
Owen Taylor3473f882001-02-23 17:55:21 +00004333
4334 values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double));
4335 if (values2 == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004336 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Daniel Veillard4dd93462001-04-02 15:16:19 +00004337 xmlXPathFreeObject(arg1);
4338 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004339 return(0);
4340 }
4341 for (i = 0;i < ns1->nodeNr;i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004342 val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]);
Daniel Veillardcda96922001-08-21 10:56:31 +00004343 if (xmlXPathIsNaN(val1))
Owen Taylor3473f882001-02-23 17:55:21 +00004344 continue;
4345 for (j = 0;j < ns2->nodeNr;j++) {
4346 if (init == 0) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004347 values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]);
Owen Taylor3473f882001-02-23 17:55:21 +00004348 }
Daniel Veillardcda96922001-08-21 10:56:31 +00004349 if (xmlXPathIsNaN(values2[j]))
Owen Taylor3473f882001-02-23 17:55:21 +00004350 continue;
4351 if (inf && strict)
4352 ret = (val1 < values2[j]);
4353 else if (inf && !strict)
4354 ret = (val1 <= values2[j]);
4355 else if (!inf && strict)
4356 ret = (val1 > values2[j]);
4357 else if (!inf && !strict)
4358 ret = (val1 >= values2[j]);
4359 if (ret)
4360 break;
4361 }
4362 if (ret)
4363 break;
4364 init = 1;
4365 }
4366 xmlFree(values2);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004367 xmlXPathFreeObject(arg1);
4368 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004369 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004370}
4371
4372/**
4373 * xmlXPathCompareNodeSetValue:
4374 * @ctxt: the XPath Parser context
4375 * @inf: less than (1) or greater than (0)
4376 * @strict: is the comparison strict
4377 * @arg: the node set
4378 * @val: the value
4379 *
4380 * Implement the compare operation between a nodeset and a value
4381 * @ns < @val (1, 1, ...
4382 * @ns <= @val (1, 0, ...
4383 * @ns > @val (0, 1, ...
4384 * @ns >= @val (0, 0, ...
4385 *
4386 * If one object to be compared is a node-set and the other is a boolean,
4387 * then the comparison will be true if and only if the result of performing
4388 * the comparison on the boolean and on the result of converting
4389 * the node-set to a boolean using the boolean function is true.
4390 *
4391 * Returns 0 or 1 depending on the results of the test.
4392 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004393static int
Owen Taylor3473f882001-02-23 17:55:21 +00004394xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict,
4395 xmlXPathObjectPtr arg, xmlXPathObjectPtr val) {
4396 if ((val == NULL) || (arg == NULL) ||
4397 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
4398 return(0);
4399
4400 switch(val->type) {
4401 case XPATH_NUMBER:
4402 return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val));
4403 case XPATH_NODESET:
4404 case XPATH_XSLT_TREE:
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004405 return(xmlXPathCompareNodeSets(inf, strict, arg, val));
Owen Taylor3473f882001-02-23 17:55:21 +00004406 case XPATH_STRING:
4407 return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val));
4408 case XPATH_BOOLEAN:
4409 valuePush(ctxt, arg);
4410 xmlXPathBooleanFunction(ctxt, 1);
4411 valuePush(ctxt, val);
4412 return(xmlXPathCompareValues(ctxt, inf, strict));
4413 default:
4414 TODO
Owen Taylor3473f882001-02-23 17:55:21 +00004415 }
4416 return(0);
4417}
4418
4419/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004420 * xmlXPathEqualNodeSetString:
Owen Taylor3473f882001-02-23 17:55:21 +00004421 * @arg: the nodeset object argument
4422 * @str: the string to compare to.
William M. Brack0c022ad2002-07-12 00:56:01 +00004423 * @neq: flag to show whether for '=' (0) or '!=' (1)
Owen Taylor3473f882001-02-23 17:55:21 +00004424 *
4425 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4426 * If one object to be compared is a node-set and the other is a string,
4427 * then the comparison will be true if and only if there is a node in
4428 * the node-set such that the result of performing the comparison on the
4429 * string-value of the node and the other string is true.
4430 *
4431 * Returns 0 or 1 depending on the results of the test.
4432 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004433static int
William M. Brack0c022ad2002-07-12 00:56:01 +00004434xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str, int neq)
Daniel Veillardf06307e2001-07-03 10:35:50 +00004435{
Owen Taylor3473f882001-02-23 17:55:21 +00004436 int i;
4437 xmlNodeSetPtr ns;
4438 xmlChar *str2;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004439 unsigned int hash;
Owen Taylor3473f882001-02-23 17:55:21 +00004440
4441 if ((str == NULL) || (arg == NULL) ||
Daniel Veillardf06307e2001-07-03 10:35:50 +00004442 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
4443 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00004444 ns = arg->nodesetval;
William M. Brackc125a722003-11-16 08:06:19 +00004445 /*
4446 * A NULL nodeset compared with a string is always false
4447 * (since there is no node equal, and no node not equal)
4448 */
4449 if ((ns == NULL) || (ns->nodeNr <= 0) )
Daniel Veillardf06307e2001-07-03 10:35:50 +00004450 return (0);
William M. Brackc125a722003-11-16 08:06:19 +00004451 hash = xmlXPathStringHash(str);
Daniel Veillardf06307e2001-07-03 10:35:50 +00004452 for (i = 0; i < ns->nodeNr; i++) {
4453 if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) {
4454 str2 = xmlNodeGetContent(ns->nodeTab[i]);
4455 if ((str2 != NULL) && (xmlStrEqual(str, str2))) {
4456 xmlFree(str2);
William M. Brack0c022ad2002-07-12 00:56:01 +00004457 if (neq)
4458 continue;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004459 return (1);
Daniel Veillard9adc0462003-03-24 18:39:54 +00004460 } else if ((str2 == NULL) && (xmlStrEqual(str, BAD_CAST ""))) {
4461 if (neq)
4462 continue;
4463 return (1);
William M. Brack0c022ad2002-07-12 00:56:01 +00004464 } else if (neq) {
4465 if (str2 != NULL)
4466 xmlFree(str2);
4467 return (1);
4468 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004469 if (str2 != NULL)
4470 xmlFree(str2);
William M. Brack0c022ad2002-07-12 00:56:01 +00004471 } else if (neq)
4472 return (1);
Owen Taylor3473f882001-02-23 17:55:21 +00004473 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004474 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00004475}
4476
4477/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004478 * xmlXPathEqualNodeSetFloat:
Owen Taylor3473f882001-02-23 17:55:21 +00004479 * @arg: the nodeset object argument
4480 * @f: the float to compare to
William M. Brack0c022ad2002-07-12 00:56:01 +00004481 * @neq: flag to show whether to compare '=' (0) or '!=' (1)
Owen Taylor3473f882001-02-23 17:55:21 +00004482 *
4483 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4484 * If one object to be compared is a node-set and the other is a number,
4485 * then the comparison will be true if and only if there is a node in
4486 * the node-set such that the result of performing the comparison on the
4487 * number to be compared and on the result of converting the string-value
4488 * of that node to a number using the number function is true.
4489 *
4490 * Returns 0 or 1 depending on the results of the test.
4491 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004492static int
William M. Brack0c022ad2002-07-12 00:56:01 +00004493xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt,
4494 xmlXPathObjectPtr arg, double f, int neq) {
4495 int i, ret=0;
4496 xmlNodeSetPtr ns;
4497 xmlChar *str2;
4498 xmlXPathObjectPtr val;
4499 double v;
Owen Taylor3473f882001-02-23 17:55:21 +00004500
4501 if ((arg == NULL) ||
4502 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
4503 return(0);
4504
William M. Brack0c022ad2002-07-12 00:56:01 +00004505 ns = arg->nodesetval;
4506 if (ns != NULL) {
4507 for (i=0;i<ns->nodeNr;i++) {
4508 str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
4509 if (str2 != NULL) {
4510 valuePush(ctxt, xmlXPathNewString(str2));
4511 xmlFree(str2);
4512 xmlXPathNumberFunction(ctxt, 1);
4513 val = valuePop(ctxt);
4514 v = val->floatval;
4515 xmlXPathFreeObject(val);
4516 if (!xmlXPathIsNaN(v)) {
4517 if ((!neq) && (v==f)) {
4518 ret = 1;
4519 break;
4520 } else if ((neq) && (v!=f)) {
4521 ret = 1;
4522 break;
4523 }
4524 }
4525 }
4526 }
4527 }
4528
4529 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004530}
4531
4532
4533/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004534 * xmlXPathEqualNodeSets:
Owen Taylor3473f882001-02-23 17:55:21 +00004535 * @arg1: first nodeset object argument
4536 * @arg2: second nodeset object argument
William M. Brack0c022ad2002-07-12 00:56:01 +00004537 * @neq: flag to show whether to test '=' (0) or '!=' (1)
Owen Taylor3473f882001-02-23 17:55:21 +00004538 *
William M. Brack0c022ad2002-07-12 00:56:01 +00004539 * Implement the equal / not equal operation on XPath nodesets:
4540 * @arg1 == @arg2 or @arg1 != @arg2
Owen Taylor3473f882001-02-23 17:55:21 +00004541 * If both objects to be compared are node-sets, then the comparison
4542 * will be true if and only if there is a node in the first node-set and
4543 * a node in the second node-set such that the result of performing the
4544 * comparison on the string-values of the two nodes is true.
4545 *
4546 * (needless to say, this is a costly operation)
4547 *
4548 * Returns 0 or 1 depending on the results of the test.
4549 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004550static int
William M. Brack0c022ad2002-07-12 00:56:01 +00004551xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2, int neq) {
Owen Taylor3473f882001-02-23 17:55:21 +00004552 int i, j;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004553 unsigned int *hashs1;
4554 unsigned int *hashs2;
Owen Taylor3473f882001-02-23 17:55:21 +00004555 xmlChar **values1;
4556 xmlChar **values2;
4557 int ret = 0;
4558 xmlNodeSetPtr ns1;
4559 xmlNodeSetPtr ns2;
4560
4561 if ((arg1 == NULL) ||
4562 ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)))
4563 return(0);
4564 if ((arg2 == NULL) ||
4565 ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE)))
4566 return(0);
4567
4568 ns1 = arg1->nodesetval;
4569 ns2 = arg2->nodesetval;
4570
Daniel Veillard911f49a2001-04-07 15:39:35 +00004571 if ((ns1 == NULL) || (ns1->nodeNr <= 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004572 return(0);
Daniel Veillard911f49a2001-04-07 15:39:35 +00004573 if ((ns2 == NULL) || (ns2->nodeNr <= 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004574 return(0);
4575
4576 /*
William M. Brack0c022ad2002-07-12 00:56:01 +00004577 * for equal, check if there is a node pertaining to both sets
Owen Taylor3473f882001-02-23 17:55:21 +00004578 */
William M. Brack0c022ad2002-07-12 00:56:01 +00004579 if (neq == 0)
4580 for (i = 0;i < ns1->nodeNr;i++)
4581 for (j = 0;j < ns2->nodeNr;j++)
4582 if (ns1->nodeTab[i] == ns2->nodeTab[j])
4583 return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00004584
4585 values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *));
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004586 if (values1 == NULL) {
4587 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004588 return(0);
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004589 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004590 hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int));
4591 if (hashs1 == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004592 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Daniel Veillardf06307e2001-07-03 10:35:50 +00004593 xmlFree(values1);
4594 return(0);
4595 }
Owen Taylor3473f882001-02-23 17:55:21 +00004596 memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *));
4597 values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *));
4598 if (values2 == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004599 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Daniel Veillardf06307e2001-07-03 10:35:50 +00004600 xmlFree(hashs1);
Owen Taylor3473f882001-02-23 17:55:21 +00004601 xmlFree(values1);
4602 return(0);
4603 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004604 hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int));
4605 if (hashs2 == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004606 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Daniel Veillardf06307e2001-07-03 10:35:50 +00004607 xmlFree(hashs1);
4608 xmlFree(values1);
4609 xmlFree(values2);
4610 return(0);
4611 }
Owen Taylor3473f882001-02-23 17:55:21 +00004612 memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *));
4613 for (i = 0;i < ns1->nodeNr;i++) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00004614 hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00004615 for (j = 0;j < ns2->nodeNr;j++) {
4616 if (i == 0)
Daniel Veillardf06307e2001-07-03 10:35:50 +00004617 hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]);
William M. Brack0c022ad2002-07-12 00:56:01 +00004618 if (hashs1[i] != hashs2[j]) {
4619 if (neq) {
4620 ret = 1;
4621 break;
4622 }
4623 }
4624 else {
Daniel Veillardf06307e2001-07-03 10:35:50 +00004625 if (values1[i] == NULL)
4626 values1[i] = xmlNodeGetContent(ns1->nodeTab[i]);
4627 if (values2[j] == NULL)
4628 values2[j] = xmlNodeGetContent(ns2->nodeTab[j]);
William M. Brack0c022ad2002-07-12 00:56:01 +00004629 ret = xmlStrEqual(values1[i], values2[j]) ^ neq;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004630 if (ret)
4631 break;
4632 }
Owen Taylor3473f882001-02-23 17:55:21 +00004633 }
4634 if (ret)
4635 break;
4636 }
4637 for (i = 0;i < ns1->nodeNr;i++)
4638 if (values1[i] != NULL)
4639 xmlFree(values1[i]);
4640 for (j = 0;j < ns2->nodeNr;j++)
4641 if (values2[j] != NULL)
4642 xmlFree(values2[j]);
4643 xmlFree(values1);
4644 xmlFree(values2);
Daniel Veillardf06307e2001-07-03 10:35:50 +00004645 xmlFree(hashs1);
4646 xmlFree(hashs2);
Owen Taylor3473f882001-02-23 17:55:21 +00004647 return(ret);
4648}
4649
William M. Brack0c022ad2002-07-12 00:56:01 +00004650static int
4651xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt,
4652 xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) {
Owen Taylor3473f882001-02-23 17:55:21 +00004653 int ret = 0;
William M. Brack0c022ad2002-07-12 00:56:01 +00004654 /*
4655 *At this point we are assured neither arg1 nor arg2
4656 *is a nodeset, so we can just pick the appropriate routine.
4657 */
Owen Taylor3473f882001-02-23 17:55:21 +00004658 switch (arg1->type) {
4659 case XPATH_UNDEFINED:
4660#ifdef DEBUG_EXPR
4661 xmlGenericError(xmlGenericErrorContext,
4662 "Equal: undefined\n");
4663#endif
4664 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004665 case XPATH_BOOLEAN:
4666 switch (arg2->type) {
4667 case XPATH_UNDEFINED:
4668#ifdef DEBUG_EXPR
4669 xmlGenericError(xmlGenericErrorContext,
4670 "Equal: undefined\n");
4671#endif
4672 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004673 case XPATH_BOOLEAN:
4674#ifdef DEBUG_EXPR
4675 xmlGenericError(xmlGenericErrorContext,
4676 "Equal: %d boolean %d \n",
4677 arg1->boolval, arg2->boolval);
4678#endif
4679 ret = (arg1->boolval == arg2->boolval);
4680 break;
4681 case XPATH_NUMBER:
William M. Brackef61d202002-07-19 08:32:00 +00004682 ret = (arg1->boolval ==
4683 xmlXPathCastNumberToBoolean(arg2->floatval));
Owen Taylor3473f882001-02-23 17:55:21 +00004684 break;
4685 case XPATH_STRING:
4686 if ((arg2->stringval == NULL) ||
4687 (arg2->stringval[0] == 0)) ret = 0;
4688 else
4689 ret = 1;
4690 ret = (arg1->boolval == ret);
4691 break;
4692 case XPATH_USERS:
4693 case XPATH_POINT:
4694 case XPATH_RANGE:
4695 case XPATH_LOCATIONSET:
4696 TODO
4697 break;
William M. Brack0c022ad2002-07-12 00:56:01 +00004698 case XPATH_NODESET:
4699 case XPATH_XSLT_TREE:
4700 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004701 }
4702 break;
4703 case XPATH_NUMBER:
4704 switch (arg2->type) {
4705 case XPATH_UNDEFINED:
4706#ifdef DEBUG_EXPR
4707 xmlGenericError(xmlGenericErrorContext,
4708 "Equal: undefined\n");
4709#endif
4710 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004711 case XPATH_BOOLEAN:
William M. Brackef61d202002-07-19 08:32:00 +00004712 ret = (arg2->boolval==
4713 xmlXPathCastNumberToBoolean(arg1->floatval));
Owen Taylor3473f882001-02-23 17:55:21 +00004714 break;
4715 case XPATH_STRING:
4716 valuePush(ctxt, arg2);
4717 xmlXPathNumberFunction(ctxt, 1);
4718 arg2 = valuePop(ctxt);
4719 /* no break on purpose */
4720 case XPATH_NUMBER:
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004721 /* Hand check NaN and Infinity equalities */
William M. Brack08171912003-12-29 02:52:11 +00004722 if (xmlXPathIsNaN(arg1->floatval) ||
4723 xmlXPathIsNaN(arg2->floatval)) {
Daniel Veillard21458c82002-03-27 16:12:22 +00004724 ret = 0;
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004725 } else if (xmlXPathIsInf(arg1->floatval) == 1) {
4726 if (xmlXPathIsInf(arg2->floatval) == 1)
4727 ret = 1;
4728 else
4729 ret = 0;
4730 } else if (xmlXPathIsInf(arg1->floatval) == -1) {
4731 if (xmlXPathIsInf(arg2->floatval) == -1)
4732 ret = 1;
4733 else
4734 ret = 0;
4735 } else if (xmlXPathIsInf(arg2->floatval) == 1) {
4736 if (xmlXPathIsInf(arg1->floatval) == 1)
4737 ret = 1;
4738 else
4739 ret = 0;
4740 } else if (xmlXPathIsInf(arg2->floatval) == -1) {
4741 if (xmlXPathIsInf(arg1->floatval) == -1)
4742 ret = 1;
4743 else
4744 ret = 0;
Daniel Veillard21458c82002-03-27 16:12:22 +00004745 } else {
4746 ret = (arg1->floatval == arg2->floatval);
4747 }
Owen Taylor3473f882001-02-23 17:55:21 +00004748 break;
4749 case XPATH_USERS:
4750 case XPATH_POINT:
4751 case XPATH_RANGE:
4752 case XPATH_LOCATIONSET:
4753 TODO
4754 break;
William M. Brack0c022ad2002-07-12 00:56:01 +00004755 case XPATH_NODESET:
4756 case XPATH_XSLT_TREE:
4757 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004758 }
4759 break;
4760 case XPATH_STRING:
4761 switch (arg2->type) {
4762 case XPATH_UNDEFINED:
4763#ifdef DEBUG_EXPR
4764 xmlGenericError(xmlGenericErrorContext,
4765 "Equal: undefined\n");
4766#endif
4767 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004768 case XPATH_BOOLEAN:
4769 if ((arg1->stringval == NULL) ||
4770 (arg1->stringval[0] == 0)) ret = 0;
4771 else
4772 ret = 1;
4773 ret = (arg2->boolval == ret);
4774 break;
4775 case XPATH_STRING:
4776 ret = xmlStrEqual(arg1->stringval, arg2->stringval);
4777 break;
4778 case XPATH_NUMBER:
4779 valuePush(ctxt, arg1);
4780 xmlXPathNumberFunction(ctxt, 1);
4781 arg1 = valuePop(ctxt);
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004782 /* Hand check NaN and Infinity equalities */
William M. Brack08171912003-12-29 02:52:11 +00004783 if (xmlXPathIsNaN(arg1->floatval) ||
4784 xmlXPathIsNaN(arg2->floatval)) {
Daniel Veillard21458c82002-03-27 16:12:22 +00004785 ret = 0;
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004786 } else if (xmlXPathIsInf(arg1->floatval) == 1) {
4787 if (xmlXPathIsInf(arg2->floatval) == 1)
4788 ret = 1;
4789 else
4790 ret = 0;
4791 } else if (xmlXPathIsInf(arg1->floatval) == -1) {
4792 if (xmlXPathIsInf(arg2->floatval) == -1)
4793 ret = 1;
4794 else
4795 ret = 0;
4796 } else if (xmlXPathIsInf(arg2->floatval) == 1) {
4797 if (xmlXPathIsInf(arg1->floatval) == 1)
4798 ret = 1;
4799 else
4800 ret = 0;
4801 } else if (xmlXPathIsInf(arg2->floatval) == -1) {
4802 if (xmlXPathIsInf(arg1->floatval) == -1)
4803 ret = 1;
4804 else
4805 ret = 0;
Daniel Veillard21458c82002-03-27 16:12:22 +00004806 } else {
4807 ret = (arg1->floatval == arg2->floatval);
4808 }
Owen Taylor3473f882001-02-23 17:55:21 +00004809 break;
4810 case XPATH_USERS:
4811 case XPATH_POINT:
4812 case XPATH_RANGE:
4813 case XPATH_LOCATIONSET:
4814 TODO
4815 break;
William M. Brack0c022ad2002-07-12 00:56:01 +00004816 case XPATH_NODESET:
4817 case XPATH_XSLT_TREE:
4818 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004819 }
4820 break;
4821 case XPATH_USERS:
4822 case XPATH_POINT:
4823 case XPATH_RANGE:
4824 case XPATH_LOCATIONSET:
4825 TODO
4826 break;
William M. Brack0c022ad2002-07-12 00:56:01 +00004827 case XPATH_NODESET:
4828 case XPATH_XSLT_TREE:
4829 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004830 }
4831 xmlXPathFreeObject(arg1);
4832 xmlXPathFreeObject(arg2);
4833 return(ret);
4834}
4835
William M. Brack0c022ad2002-07-12 00:56:01 +00004836/**
4837 * xmlXPathEqualValues:
4838 * @ctxt: the XPath Parser context
4839 *
4840 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4841 *
4842 * Returns 0 or 1 depending on the results of the test.
4843 */
4844int
4845xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) {
4846 xmlXPathObjectPtr arg1, arg2, argtmp;
4847 int ret = 0;
4848
4849 arg2 = valuePop(ctxt);
4850 arg1 = valuePop(ctxt);
4851 if ((arg1 == NULL) || (arg2 == NULL)) {
4852 if (arg1 != NULL)
4853 xmlXPathFreeObject(arg1);
4854 else
4855 xmlXPathFreeObject(arg2);
4856 XP_ERROR0(XPATH_INVALID_OPERAND);
4857 }
4858
4859 if (arg1 == arg2) {
4860#ifdef DEBUG_EXPR
4861 xmlGenericError(xmlGenericErrorContext,
4862 "Equal: by pointer\n");
4863#endif
4864 return(1);
4865 }
4866
4867 /*
4868 *If either argument is a nodeset, it's a 'special case'
4869 */
4870 if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) ||
4871 (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
4872 /*
4873 *Hack it to assure arg1 is the nodeset
4874 */
4875 if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) {
4876 argtmp = arg2;
4877 arg2 = arg1;
4878 arg1 = argtmp;
4879 }
4880 switch (arg2->type) {
4881 case XPATH_UNDEFINED:
4882#ifdef DEBUG_EXPR
4883 xmlGenericError(xmlGenericErrorContext,
4884 "Equal: undefined\n");
4885#endif
4886 break;
4887 case XPATH_NODESET:
4888 case XPATH_XSLT_TREE:
4889 ret = xmlXPathEqualNodeSets(arg1, arg2, 0);
4890 break;
4891 case XPATH_BOOLEAN:
4892 if ((arg1->nodesetval == NULL) ||
4893 (arg1->nodesetval->nodeNr == 0)) ret = 0;
4894 else
4895 ret = 1;
4896 ret = (ret == arg2->boolval);
4897 break;
4898 case XPATH_NUMBER:
4899 ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 0);
4900 break;
4901 case XPATH_STRING:
4902 ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval, 0);
4903 break;
4904 case XPATH_USERS:
4905 case XPATH_POINT:
4906 case XPATH_RANGE:
4907 case XPATH_LOCATIONSET:
4908 TODO
4909 break;
4910 }
4911 xmlXPathFreeObject(arg1);
4912 xmlXPathFreeObject(arg2);
4913 return(ret);
4914 }
4915
4916 return (xmlXPathEqualValuesCommon(ctxt, arg1, arg2));
4917}
4918
4919/**
4920 * xmlXPathNotEqualValues:
4921 * @ctxt: the XPath Parser context
4922 *
4923 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4924 *
4925 * Returns 0 or 1 depending on the results of the test.
4926 */
4927int
4928xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) {
4929 xmlXPathObjectPtr arg1, arg2, argtmp;
4930 int ret = 0;
4931
4932 arg2 = valuePop(ctxt);
4933 arg1 = valuePop(ctxt);
4934 if ((arg1 == NULL) || (arg2 == NULL)) {
4935 if (arg1 != NULL)
4936 xmlXPathFreeObject(arg1);
4937 else
4938 xmlXPathFreeObject(arg2);
4939 XP_ERROR0(XPATH_INVALID_OPERAND);
4940 }
4941
4942 if (arg1 == arg2) {
4943#ifdef DEBUG_EXPR
4944 xmlGenericError(xmlGenericErrorContext,
4945 "NotEqual: by pointer\n");
4946#endif
4947 return(0);
4948 }
4949
4950 /*
4951 *If either argument is a nodeset, it's a 'special case'
4952 */
4953 if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) ||
4954 (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
4955 /*
4956 *Hack it to assure arg1 is the nodeset
4957 */
4958 if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) {
4959 argtmp = arg2;
4960 arg2 = arg1;
4961 arg1 = argtmp;
4962 }
4963 switch (arg2->type) {
4964 case XPATH_UNDEFINED:
4965#ifdef DEBUG_EXPR
4966 xmlGenericError(xmlGenericErrorContext,
4967 "NotEqual: undefined\n");
4968#endif
4969 break;
4970 case XPATH_NODESET:
4971 case XPATH_XSLT_TREE:
4972 ret = xmlXPathEqualNodeSets(arg1, arg2, 1);
4973 break;
4974 case XPATH_BOOLEAN:
4975 if ((arg1->nodesetval == NULL) ||
4976 (arg1->nodesetval->nodeNr == 0)) ret = 0;
4977 else
4978 ret = 1;
William M. Brackef61d202002-07-19 08:32:00 +00004979 ret = (ret != arg2->boolval);
William M. Brack0c022ad2002-07-12 00:56:01 +00004980 break;
4981 case XPATH_NUMBER:
4982 ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 1);
4983 break;
4984 case XPATH_STRING:
4985 ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval,1);
4986 break;
4987 case XPATH_USERS:
4988 case XPATH_POINT:
4989 case XPATH_RANGE:
4990 case XPATH_LOCATIONSET:
4991 TODO
4992 break;
4993 }
4994 xmlXPathFreeObject(arg1);
4995 xmlXPathFreeObject(arg2);
4996 return(ret);
4997 }
4998
4999 return (!xmlXPathEqualValuesCommon(ctxt, arg1, arg2));
5000}
Owen Taylor3473f882001-02-23 17:55:21 +00005001
5002/**
5003 * xmlXPathCompareValues:
5004 * @ctxt: the XPath Parser context
5005 * @inf: less than (1) or greater than (0)
5006 * @strict: is the comparison strict
5007 *
5008 * Implement the compare operation on XPath objects:
5009 * @arg1 < @arg2 (1, 1, ...
5010 * @arg1 <= @arg2 (1, 0, ...
5011 * @arg1 > @arg2 (0, 1, ...
5012 * @arg1 >= @arg2 (0, 0, ...
5013 *
5014 * When neither object to be compared is a node-set and the operator is
5015 * <=, <, >=, >, then the objects are compared by converted both objects
5016 * to numbers and comparing the numbers according to IEEE 754. The <
5017 * comparison will be true if and only if the first number is less than the
5018 * second number. The <= comparison will be true if and only if the first
5019 * number is less than or equal to the second number. The > comparison
5020 * will be true if and only if the first number is greater than the second
5021 * number. The >= comparison will be true if and only if the first number
5022 * is greater than or equal to the second number.
5023 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005024 * Returns 1 if the comparison succeeded, 0 if it failed
Owen Taylor3473f882001-02-23 17:55:21 +00005025 */
5026int
5027xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) {
Daniel Veillardd30be4a2002-03-28 18:25:31 +00005028 int ret = 0, arg1i = 0, arg2i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005029 xmlXPathObjectPtr arg1, arg2;
5030
William M. Brack0c022ad2002-07-12 00:56:01 +00005031 arg2 = valuePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005032 arg1 = valuePop(ctxt);
William M. Brack0c022ad2002-07-12 00:56:01 +00005033 if ((arg1 == NULL) || (arg2 == NULL)) {
5034 if (arg1 != NULL)
5035 xmlXPathFreeObject(arg1);
5036 else
5037 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00005038 XP_ERROR0(XPATH_INVALID_OPERAND);
5039 }
5040
William M. Brack0c022ad2002-07-12 00:56:01 +00005041 if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) ||
5042 (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
5043 if (((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE)) &&
5044 ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE))){
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005045 ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00005046 } else {
William M. Brack0c022ad2002-07-12 00:56:01 +00005047 if ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
Daniel Veillard4af6b6e2001-03-06 08:33:38 +00005048 ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict,
5049 arg1, arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00005050 } else {
Daniel Veillard4af6b6e2001-03-06 08:33:38 +00005051 ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict,
5052 arg2, arg1);
Owen Taylor3473f882001-02-23 17:55:21 +00005053 }
5054 }
5055 return(ret);
5056 }
5057
5058 if (arg1->type != XPATH_NUMBER) {
5059 valuePush(ctxt, arg1);
5060 xmlXPathNumberFunction(ctxt, 1);
5061 arg1 = valuePop(ctxt);
5062 }
5063 if (arg1->type != XPATH_NUMBER) {
5064 xmlXPathFreeObject(arg1);
5065 xmlXPathFreeObject(arg2);
5066 XP_ERROR0(XPATH_INVALID_OPERAND);
5067 }
5068 if (arg2->type != XPATH_NUMBER) {
5069 valuePush(ctxt, arg2);
5070 xmlXPathNumberFunction(ctxt, 1);
5071 arg2 = valuePop(ctxt);
5072 }
5073 if (arg2->type != XPATH_NUMBER) {
5074 xmlXPathFreeObject(arg1);
5075 xmlXPathFreeObject(arg2);
5076 XP_ERROR0(XPATH_INVALID_OPERAND);
5077 }
5078 /*
5079 * Add tests for infinity and nan
5080 * => feedback on 3.4 for Inf and NaN
5081 */
Daniel Veillardd30be4a2002-03-28 18:25:31 +00005082 /* Hand check NaN and Infinity comparisons */
Daniel Veillard21458c82002-03-27 16:12:22 +00005083 if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) {
Daniel Veillardd30be4a2002-03-28 18:25:31 +00005084 ret=0;
Daniel Veillard21458c82002-03-27 16:12:22 +00005085 } else {
Daniel Veillardd30be4a2002-03-28 18:25:31 +00005086 arg1i=xmlXPathIsInf(arg1->floatval);
5087 arg2i=xmlXPathIsInf(arg2->floatval);
5088 if (inf && strict) {
5089 if ((arg1i == -1 && arg2i != -1) ||
5090 (arg2i == 1 && arg1i != 1)) {
5091 ret = 1;
5092 } else if (arg1i == 0 && arg2i == 0) {
5093 ret = (arg1->floatval < arg2->floatval);
5094 } else {
5095 ret = 0;
5096 }
5097 }
5098 else if (inf && !strict) {
5099 if (arg1i == -1 || arg2i == 1) {
5100 ret = 1;
5101 } else if (arg1i == 0 && arg2i == 0) {
5102 ret = (arg1->floatval <= arg2->floatval);
5103 } else {
5104 ret = 0;
5105 }
5106 }
5107 else if (!inf && strict) {
5108 if ((arg1i == 1 && arg2i != 1) ||
5109 (arg2i == -1 && arg1i != -1)) {
5110 ret = 1;
5111 } else if (arg1i == 0 && arg2i == 0) {
5112 ret = (arg1->floatval > arg2->floatval);
5113 } else {
5114 ret = 0;
5115 }
5116 }
5117 else if (!inf && !strict) {
5118 if (arg1i == 1 || arg2i == -1) {
5119 ret = 1;
5120 } else if (arg1i == 0 && arg2i == 0) {
5121 ret = (arg1->floatval >= arg2->floatval);
5122 } else {
5123 ret = 0;
5124 }
5125 }
Daniel Veillard21458c82002-03-27 16:12:22 +00005126 }
Owen Taylor3473f882001-02-23 17:55:21 +00005127 xmlXPathFreeObject(arg1);
5128 xmlXPathFreeObject(arg2);
5129 return(ret);
5130}
5131
5132/**
5133 * xmlXPathValueFlipSign:
5134 * @ctxt: the XPath Parser context
5135 *
5136 * Implement the unary - operation on an XPath object
5137 * The numeric operators convert their operands to numbers as if
5138 * by calling the number function.
5139 */
5140void
5141xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005142 CAST_TO_NUMBER;
5143 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillardeca82812002-04-24 11:42:02 +00005144 if (xmlXPathIsNaN(ctxt->value->floatval))
5145 ctxt->value->floatval=xmlXPathNAN;
5146 else if (xmlXPathIsInf(ctxt->value->floatval) == 1)
5147 ctxt->value->floatval=xmlXPathNINF;
5148 else if (xmlXPathIsInf(ctxt->value->floatval) == -1)
5149 ctxt->value->floatval=xmlXPathPINF;
5150 else if (ctxt->value->floatval == 0) {
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005151 if (xmlXPathGetSign(ctxt->value->floatval) == 0)
5152 ctxt->value->floatval = xmlXPathNZERO;
5153 else
5154 ctxt->value->floatval = 0;
5155 }
5156 else
5157 ctxt->value->floatval = - ctxt->value->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00005158}
5159
5160/**
5161 * xmlXPathAddValues:
5162 * @ctxt: the XPath Parser context
5163 *
5164 * Implement the add operation on XPath objects:
5165 * The numeric operators convert their operands to numbers as if
5166 * by calling the number function.
5167 */
5168void
5169xmlXPathAddValues(xmlXPathParserContextPtr ctxt) {
5170 xmlXPathObjectPtr arg;
5171 double val;
5172
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005173 arg = valuePop(ctxt);
5174 if (arg == NULL)
5175 XP_ERROR(XPATH_INVALID_OPERAND);
5176 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005177 xmlXPathFreeObject(arg);
5178
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005179 CAST_TO_NUMBER;
5180 CHECK_TYPE(XPATH_NUMBER);
5181 ctxt->value->floatval += val;
Owen Taylor3473f882001-02-23 17:55:21 +00005182}
5183
5184/**
5185 * xmlXPathSubValues:
5186 * @ctxt: the XPath Parser context
5187 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005188 * Implement the subtraction operation on XPath objects:
Owen Taylor3473f882001-02-23 17:55:21 +00005189 * The numeric operators convert their operands to numbers as if
5190 * by calling the number function.
5191 */
5192void
5193xmlXPathSubValues(xmlXPathParserContextPtr ctxt) {
5194 xmlXPathObjectPtr arg;
5195 double val;
5196
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005197 arg = valuePop(ctxt);
5198 if (arg == NULL)
5199 XP_ERROR(XPATH_INVALID_OPERAND);
5200 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005201 xmlXPathFreeObject(arg);
5202
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005203 CAST_TO_NUMBER;
5204 CHECK_TYPE(XPATH_NUMBER);
5205 ctxt->value->floatval -= val;
Owen Taylor3473f882001-02-23 17:55:21 +00005206}
5207
5208/**
5209 * xmlXPathMultValues:
5210 * @ctxt: the XPath Parser context
5211 *
5212 * Implement the multiply operation on XPath objects:
5213 * The numeric operators convert their operands to numbers as if
5214 * by calling the number function.
5215 */
5216void
5217xmlXPathMultValues(xmlXPathParserContextPtr ctxt) {
5218 xmlXPathObjectPtr arg;
5219 double val;
5220
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005221 arg = valuePop(ctxt);
5222 if (arg == NULL)
5223 XP_ERROR(XPATH_INVALID_OPERAND);
5224 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005225 xmlXPathFreeObject(arg);
5226
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005227 CAST_TO_NUMBER;
5228 CHECK_TYPE(XPATH_NUMBER);
5229 ctxt->value->floatval *= val;
Owen Taylor3473f882001-02-23 17:55:21 +00005230}
5231
5232/**
5233 * xmlXPathDivValues:
5234 * @ctxt: the XPath Parser context
5235 *
5236 * Implement the div operation on XPath objects @arg1 / @arg2:
5237 * The numeric operators convert their operands to numbers as if
5238 * by calling the number function.
5239 */
5240void
5241xmlXPathDivValues(xmlXPathParserContextPtr ctxt) {
5242 xmlXPathObjectPtr arg;
5243 double val;
5244
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005245 arg = valuePop(ctxt);
5246 if (arg == NULL)
5247 XP_ERROR(XPATH_INVALID_OPERAND);
5248 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005249 xmlXPathFreeObject(arg);
5250
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005251 CAST_TO_NUMBER;
5252 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillardeca82812002-04-24 11:42:02 +00005253 if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval))
5254 ctxt->value->floatval = xmlXPathNAN;
5255 else if (val == 0 && xmlXPathGetSign(val) != 0) {
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005256 if (ctxt->value->floatval == 0)
5257 ctxt->value->floatval = xmlXPathNAN;
5258 else if (ctxt->value->floatval > 0)
5259 ctxt->value->floatval = xmlXPathNINF;
5260 else if (ctxt->value->floatval < 0)
5261 ctxt->value->floatval = xmlXPathPINF;
5262 }
5263 else if (val == 0) {
Daniel Veillard5f4b5992002-02-20 10:22:49 +00005264 if (ctxt->value->floatval == 0)
5265 ctxt->value->floatval = xmlXPathNAN;
5266 else if (ctxt->value->floatval > 0)
5267 ctxt->value->floatval = xmlXPathPINF;
5268 else if (ctxt->value->floatval < 0)
5269 ctxt->value->floatval = xmlXPathNINF;
5270 } else
5271 ctxt->value->floatval /= val;
Owen Taylor3473f882001-02-23 17:55:21 +00005272}
5273
5274/**
5275 * xmlXPathModValues:
5276 * @ctxt: the XPath Parser context
5277 *
5278 * Implement the mod operation on XPath objects: @arg1 / @arg2
5279 * The numeric operators convert their operands to numbers as if
5280 * by calling the number function.
5281 */
5282void
5283xmlXPathModValues(xmlXPathParserContextPtr ctxt) {
5284 xmlXPathObjectPtr arg;
Daniel Veillardfdc91562002-07-01 21:52:03 +00005285 double arg1, arg2;
Owen Taylor3473f882001-02-23 17:55:21 +00005286
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005287 arg = valuePop(ctxt);
5288 if (arg == NULL)
5289 XP_ERROR(XPATH_INVALID_OPERAND);
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005290 arg2 = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005291 xmlXPathFreeObject(arg);
5292
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005293 CAST_TO_NUMBER;
5294 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005295 arg1 = ctxt->value->floatval;
Daniel Veillard268fd1b2001-08-26 18:46:36 +00005296 if (arg2 == 0)
5297 ctxt->value->floatval = xmlXPathNAN;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005298 else {
Daniel Veillardfdc91562002-07-01 21:52:03 +00005299 ctxt->value->floatval = fmod(arg1, arg2);
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005300 }
Owen Taylor3473f882001-02-23 17:55:21 +00005301}
5302
5303/************************************************************************
5304 * *
5305 * The traversal functions *
5306 * *
5307 ************************************************************************/
5308
Owen Taylor3473f882001-02-23 17:55:21 +00005309/*
5310 * A traversal function enumerates nodes along an axis.
5311 * Initially it must be called with NULL, and it indicates
5312 * termination on the axis by returning NULL.
5313 */
5314typedef xmlNodePtr (*xmlXPathTraversalFunction)
5315 (xmlXPathParserContextPtr ctxt, xmlNodePtr cur);
5316
5317/**
5318 * xmlXPathNextSelf:
5319 * @ctxt: the XPath Parser context
5320 * @cur: the current node in the traversal
5321 *
5322 * Traversal function for the "self" direction
5323 * The self axis contains just the context node itself
5324 *
5325 * Returns the next element following that axis
5326 */
5327xmlNodePtr
5328xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5329 if (cur == NULL)
5330 return(ctxt->context->node);
5331 return(NULL);
5332}
5333
5334/**
5335 * xmlXPathNextChild:
5336 * @ctxt: the XPath Parser context
5337 * @cur: the current node in the traversal
5338 *
5339 * Traversal function for the "child" direction
5340 * The child axis contains the children of the context node in document order.
5341 *
5342 * Returns the next element following that axis
5343 */
5344xmlNodePtr
5345xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5346 if (cur == NULL) {
5347 if (ctxt->context->node == NULL) return(NULL);
5348 switch (ctxt->context->node->type) {
5349 case XML_ELEMENT_NODE:
5350 case XML_TEXT_NODE:
5351 case XML_CDATA_SECTION_NODE:
5352 case XML_ENTITY_REF_NODE:
5353 case XML_ENTITY_NODE:
5354 case XML_PI_NODE:
5355 case XML_COMMENT_NODE:
5356 case XML_NOTATION_NODE:
5357 case XML_DTD_NODE:
5358 return(ctxt->context->node->children);
5359 case XML_DOCUMENT_NODE:
5360 case XML_DOCUMENT_TYPE_NODE:
5361 case XML_DOCUMENT_FRAG_NODE:
5362 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005363#ifdef LIBXML_DOCB_ENABLED
5364 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005365#endif
5366 return(((xmlDocPtr) ctxt->context->node)->children);
5367 case XML_ELEMENT_DECL:
5368 case XML_ATTRIBUTE_DECL:
5369 case XML_ENTITY_DECL:
5370 case XML_ATTRIBUTE_NODE:
5371 case XML_NAMESPACE_DECL:
5372 case XML_XINCLUDE_START:
5373 case XML_XINCLUDE_END:
5374 return(NULL);
5375 }
5376 return(NULL);
5377 }
5378 if ((cur->type == XML_DOCUMENT_NODE) ||
5379 (cur->type == XML_HTML_DOCUMENT_NODE))
5380 return(NULL);
5381 return(cur->next);
5382}
5383
5384/**
5385 * xmlXPathNextDescendant:
5386 * @ctxt: the XPath Parser context
5387 * @cur: the current node in the traversal
5388 *
5389 * Traversal function for the "descendant" direction
5390 * the descendant axis contains the descendants of the context node in document
5391 * order; a descendant is a child or a child of a child and so on.
5392 *
5393 * Returns the next element following that axis
5394 */
5395xmlNodePtr
5396xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5397 if (cur == NULL) {
5398 if (ctxt->context->node == NULL)
5399 return(NULL);
5400 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5401 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5402 return(NULL);
5403
5404 if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc)
5405 return(ctxt->context->doc->children);
5406 return(ctxt->context->node->children);
5407 }
5408
Daniel Veillard567e1b42001-08-01 15:53:47 +00005409 if (cur->children != NULL) {
Daniel Veillard68e9e742002-11-16 15:35:11 +00005410 /*
5411 * Do not descend on entities declarations
5412 */
5413 if (cur->children->type != XML_ENTITY_DECL) {
5414 cur = cur->children;
5415 /*
5416 * Skip DTDs
5417 */
5418 if (cur->type != XML_DTD_NODE)
5419 return(cur);
5420 }
Daniel Veillard567e1b42001-08-01 15:53:47 +00005421 }
5422
5423 if (cur == ctxt->context->node) return(NULL);
5424
Daniel Veillard68e9e742002-11-16 15:35:11 +00005425 while (cur->next != NULL) {
5426 cur = cur->next;
5427 if ((cur->type != XML_ENTITY_DECL) &&
5428 (cur->type != XML_DTD_NODE))
5429 return(cur);
5430 }
Owen Taylor3473f882001-02-23 17:55:21 +00005431
5432 do {
5433 cur = cur->parent;
5434 if (cur == NULL) return(NULL);
5435 if (cur == ctxt->context->node) return(NULL);
5436 if (cur->next != NULL) {
5437 cur = cur->next;
5438 return(cur);
5439 }
5440 } while (cur != NULL);
5441 return(cur);
5442}
5443
5444/**
5445 * xmlXPathNextDescendantOrSelf:
5446 * @ctxt: the XPath Parser context
5447 * @cur: the current node in the traversal
5448 *
5449 * Traversal function for the "descendant-or-self" direction
5450 * the descendant-or-self axis contains the context node and the descendants
5451 * of the context node in document order; thus the context node is the first
5452 * node on the axis, and the first child of the context node is the second node
5453 * on the axis
5454 *
5455 * Returns the next element following that axis
5456 */
5457xmlNodePtr
5458xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5459 if (cur == NULL) {
5460 if (ctxt->context->node == NULL)
5461 return(NULL);
5462 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5463 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5464 return(NULL);
5465 return(ctxt->context->node);
5466 }
5467
5468 return(xmlXPathNextDescendant(ctxt, cur));
5469}
5470
5471/**
5472 * xmlXPathNextParent:
5473 * @ctxt: the XPath Parser context
5474 * @cur: the current node in the traversal
5475 *
5476 * Traversal function for the "parent" direction
5477 * The parent axis contains the parent of the context node, if there is one.
5478 *
5479 * Returns the next element following that axis
5480 */
5481xmlNodePtr
5482xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5483 /*
5484 * the parent of an attribute or namespace node is the element
5485 * to which the attribute or namespace node is attached
5486 * Namespace handling !!!
5487 */
5488 if (cur == NULL) {
5489 if (ctxt->context->node == NULL) return(NULL);
5490 switch (ctxt->context->node->type) {
5491 case XML_ELEMENT_NODE:
5492 case XML_TEXT_NODE:
5493 case XML_CDATA_SECTION_NODE:
5494 case XML_ENTITY_REF_NODE:
5495 case XML_ENTITY_NODE:
5496 case XML_PI_NODE:
5497 case XML_COMMENT_NODE:
5498 case XML_NOTATION_NODE:
5499 case XML_DTD_NODE:
5500 case XML_ELEMENT_DECL:
5501 case XML_ATTRIBUTE_DECL:
5502 case XML_XINCLUDE_START:
5503 case XML_XINCLUDE_END:
5504 case XML_ENTITY_DECL:
5505 if (ctxt->context->node->parent == NULL)
5506 return((xmlNodePtr) ctxt->context->doc);
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005507 if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) &&
Daniel Veillard652d8a92003-02-04 19:28:49 +00005508 ((ctxt->context->node->parent->name[0] == ' ') ||
5509 (xmlStrEqual(ctxt->context->node->parent->name,
5510 BAD_CAST "fake node libxslt"))))
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005511 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005512 return(ctxt->context->node->parent);
5513 case XML_ATTRIBUTE_NODE: {
5514 xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node;
5515
5516 return(att->parent);
5517 }
5518 case XML_DOCUMENT_NODE:
5519 case XML_DOCUMENT_TYPE_NODE:
5520 case XML_DOCUMENT_FRAG_NODE:
5521 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005522#ifdef LIBXML_DOCB_ENABLED
5523 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005524#endif
5525 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005526 case XML_NAMESPACE_DECL: {
5527 xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
5528
5529 if ((ns->next != NULL) &&
5530 (ns->next->type != XML_NAMESPACE_DECL))
5531 return((xmlNodePtr) ns->next);
Owen Taylor3473f882001-02-23 17:55:21 +00005532 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005533 }
Owen Taylor3473f882001-02-23 17:55:21 +00005534 }
5535 }
5536 return(NULL);
5537}
5538
5539/**
5540 * xmlXPathNextAncestor:
5541 * @ctxt: the XPath Parser context
5542 * @cur: the current node in the traversal
5543 *
5544 * Traversal function for the "ancestor" direction
5545 * the ancestor axis contains the ancestors of the context node; the ancestors
5546 * of the context node consist of the parent of context node and the parent's
5547 * parent and so on; the nodes are ordered in reverse document order; thus the
5548 * parent is the first node on the axis, and the parent's parent is the second
5549 * node on the axis
5550 *
5551 * Returns the next element following that axis
5552 */
5553xmlNodePtr
5554xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5555 /*
5556 * the parent of an attribute or namespace node is the element
5557 * to which the attribute or namespace node is attached
5558 * !!!!!!!!!!!!!
5559 */
5560 if (cur == NULL) {
5561 if (ctxt->context->node == NULL) return(NULL);
5562 switch (ctxt->context->node->type) {
5563 case XML_ELEMENT_NODE:
5564 case XML_TEXT_NODE:
5565 case XML_CDATA_SECTION_NODE:
5566 case XML_ENTITY_REF_NODE:
5567 case XML_ENTITY_NODE:
5568 case XML_PI_NODE:
5569 case XML_COMMENT_NODE:
5570 case XML_DTD_NODE:
5571 case XML_ELEMENT_DECL:
5572 case XML_ATTRIBUTE_DECL:
5573 case XML_ENTITY_DECL:
5574 case XML_NOTATION_NODE:
5575 case XML_XINCLUDE_START:
5576 case XML_XINCLUDE_END:
5577 if (ctxt->context->node->parent == NULL)
5578 return((xmlNodePtr) ctxt->context->doc);
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005579 if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) &&
Daniel Veillard652d8a92003-02-04 19:28:49 +00005580 ((ctxt->context->node->parent->name[0] == ' ') ||
5581 (xmlStrEqual(ctxt->context->node->parent->name,
5582 BAD_CAST "fake node libxslt"))))
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005583 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005584 return(ctxt->context->node->parent);
5585 case XML_ATTRIBUTE_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005586 xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node;
Owen Taylor3473f882001-02-23 17:55:21 +00005587
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005588 return(tmp->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005589 }
5590 case XML_DOCUMENT_NODE:
5591 case XML_DOCUMENT_TYPE_NODE:
5592 case XML_DOCUMENT_FRAG_NODE:
5593 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005594#ifdef LIBXML_DOCB_ENABLED
5595 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005596#endif
5597 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005598 case XML_NAMESPACE_DECL: {
5599 xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
5600
5601 if ((ns->next != NULL) &&
5602 (ns->next->type != XML_NAMESPACE_DECL))
5603 return((xmlNodePtr) ns->next);
William M. Brack08171912003-12-29 02:52:11 +00005604 /* Bad, how did that namespace end up here ? */
Owen Taylor3473f882001-02-23 17:55:21 +00005605 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005606 }
Owen Taylor3473f882001-02-23 17:55:21 +00005607 }
5608 return(NULL);
5609 }
5610 if (cur == ctxt->context->doc->children)
5611 return((xmlNodePtr) ctxt->context->doc);
5612 if (cur == (xmlNodePtr) ctxt->context->doc)
5613 return(NULL);
5614 switch (cur->type) {
5615 case XML_ELEMENT_NODE:
5616 case XML_TEXT_NODE:
5617 case XML_CDATA_SECTION_NODE:
5618 case XML_ENTITY_REF_NODE:
5619 case XML_ENTITY_NODE:
5620 case XML_PI_NODE:
5621 case XML_COMMENT_NODE:
5622 case XML_NOTATION_NODE:
5623 case XML_DTD_NODE:
5624 case XML_ELEMENT_DECL:
5625 case XML_ATTRIBUTE_DECL:
5626 case XML_ENTITY_DECL:
5627 case XML_XINCLUDE_START:
5628 case XML_XINCLUDE_END:
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005629 if (cur->parent == NULL)
5630 return(NULL);
5631 if ((cur->parent->type == XML_ELEMENT_NODE) &&
Daniel Veillard652d8a92003-02-04 19:28:49 +00005632 ((cur->parent->name[0] == ' ') ||
5633 (xmlStrEqual(cur->parent->name,
5634 BAD_CAST "fake node libxslt"))))
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005635 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005636 return(cur->parent);
5637 case XML_ATTRIBUTE_NODE: {
5638 xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node;
5639
5640 return(att->parent);
5641 }
Aleksey Sanindffd5c82002-05-31 04:24:13 +00005642 case XML_NAMESPACE_DECL: {
5643 xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
5644
5645 if ((ns->next != NULL) &&
5646 (ns->next->type != XML_NAMESPACE_DECL))
5647 return((xmlNodePtr) ns->next);
William M. Brack08171912003-12-29 02:52:11 +00005648 /* Bad, how did that namespace end up here ? */
Aleksey Sanindffd5c82002-05-31 04:24:13 +00005649 return(NULL);
5650 }
Owen Taylor3473f882001-02-23 17:55:21 +00005651 case XML_DOCUMENT_NODE:
5652 case XML_DOCUMENT_TYPE_NODE:
5653 case XML_DOCUMENT_FRAG_NODE:
5654 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005655#ifdef LIBXML_DOCB_ENABLED
5656 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005657#endif
5658 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005659 }
5660 return(NULL);
5661}
5662
5663/**
5664 * xmlXPathNextAncestorOrSelf:
5665 * @ctxt: the XPath Parser context
5666 * @cur: the current node in the traversal
5667 *
5668 * Traversal function for the "ancestor-or-self" direction
5669 * he ancestor-or-self axis contains the context node and ancestors of
5670 * the context node in reverse document order; thus the context node is
5671 * the first node on the axis, and the context node's parent the second;
5672 * parent here is defined the same as with the parent axis.
5673 *
5674 * Returns the next element following that axis
5675 */
5676xmlNodePtr
5677xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5678 if (cur == NULL)
5679 return(ctxt->context->node);
5680 return(xmlXPathNextAncestor(ctxt, cur));
5681}
5682
5683/**
5684 * xmlXPathNextFollowingSibling:
5685 * @ctxt: the XPath Parser context
5686 * @cur: the current node in the traversal
5687 *
5688 * Traversal function for the "following-sibling" direction
5689 * The following-sibling axis contains the following siblings of the context
5690 * node in document order.
5691 *
5692 * Returns the next element following that axis
5693 */
5694xmlNodePtr
5695xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5696 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5697 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5698 return(NULL);
5699 if (cur == (xmlNodePtr) ctxt->context->doc)
5700 return(NULL);
5701 if (cur == NULL)
5702 return(ctxt->context->node->next);
5703 return(cur->next);
5704}
5705
5706/**
5707 * xmlXPathNextPrecedingSibling:
5708 * @ctxt: the XPath Parser context
5709 * @cur: the current node in the traversal
5710 *
5711 * Traversal function for the "preceding-sibling" direction
5712 * The preceding-sibling axis contains the preceding siblings of the context
5713 * node in reverse document order; the first preceding sibling is first on the
5714 * axis; the sibling preceding that node is the second on the axis and so on.
5715 *
5716 * Returns the next element following that axis
5717 */
5718xmlNodePtr
5719xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5720 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5721 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5722 return(NULL);
5723 if (cur == (xmlNodePtr) ctxt->context->doc)
5724 return(NULL);
5725 if (cur == NULL)
5726 return(ctxt->context->node->prev);
Daniel Veillardf06307e2001-07-03 10:35:50 +00005727 if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) {
5728 cur = cur->prev;
5729 if (cur == NULL)
5730 return(ctxt->context->node->prev);
5731 }
Owen Taylor3473f882001-02-23 17:55:21 +00005732 return(cur->prev);
5733}
5734
5735/**
5736 * xmlXPathNextFollowing:
5737 * @ctxt: the XPath Parser context
5738 * @cur: the current node in the traversal
5739 *
5740 * Traversal function for the "following" direction
5741 * The following axis contains all nodes in the same document as the context
5742 * node that are after the context node in document order, excluding any
5743 * descendants and excluding attribute nodes and namespace nodes; the nodes
5744 * are ordered in document order
5745 *
5746 * Returns the next element following that axis
5747 */
5748xmlNodePtr
5749xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5750 if (cur != NULL && cur->children != NULL)
5751 return cur->children ;
5752 if (cur == NULL) cur = ctxt->context->node;
5753 if (cur == NULL) return(NULL) ; /* ERROR */
5754 if (cur->next != NULL) return(cur->next) ;
5755 do {
5756 cur = cur->parent;
5757 if (cur == NULL) return(NULL);
5758 if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL);
5759 if (cur->next != NULL) return(cur->next);
5760 } while (cur != NULL);
5761 return(cur);
5762}
5763
5764/*
5765 * xmlXPathIsAncestor:
5766 * @ancestor: the ancestor node
5767 * @node: the current node
5768 *
5769 * Check that @ancestor is a @node's ancestor
5770 *
5771 * returns 1 if @ancestor is a @node's ancestor, 0 otherwise.
5772 */
5773static int
5774xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) {
5775 if ((ancestor == NULL) || (node == NULL)) return(0);
5776 /* nodes need to be in the same document */
5777 if (ancestor->doc != node->doc) return(0);
5778 /* avoid searching if ancestor or node is the root node */
5779 if (ancestor == (xmlNodePtr) node->doc) return(1);
5780 if (node == (xmlNodePtr) ancestor->doc) return(0);
5781 while (node->parent != NULL) {
5782 if (node->parent == ancestor)
5783 return(1);
5784 node = node->parent;
5785 }
5786 return(0);
5787}
5788
5789/**
5790 * xmlXPathNextPreceding:
5791 * @ctxt: the XPath Parser context
5792 * @cur: the current node in the traversal
5793 *
5794 * Traversal function for the "preceding" direction
5795 * the preceding axis contains all nodes in the same document as the context
5796 * node that are before the context node in document order, excluding any
5797 * ancestors and excluding attribute nodes and namespace nodes; the nodes are
5798 * ordered in reverse document order
5799 *
5800 * Returns the next element following that axis
5801 */
5802xmlNodePtr
Daniel Veillardf06307e2001-07-03 10:35:50 +00005803xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur)
5804{
Owen Taylor3473f882001-02-23 17:55:21 +00005805 if (cur == NULL)
Daniel Veillardf06307e2001-07-03 10:35:50 +00005806 cur = ctxt->context->node;
5807 if (cur == NULL)
5808 return (NULL);
5809 if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
5810 cur = cur->prev;
Owen Taylor3473f882001-02-23 17:55:21 +00005811 do {
5812 if (cur->prev != NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00005813 for (cur = cur->prev; cur->last != NULL; cur = cur->last) ;
5814 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00005815 }
5816
5817 cur = cur->parent;
Daniel Veillardf06307e2001-07-03 10:35:50 +00005818 if (cur == NULL)
5819 return (NULL);
5820 if (cur == ctxt->context->doc->children)
5821 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005822 } while (xmlXPathIsAncestor(cur, ctxt->context->node));
Daniel Veillardf06307e2001-07-03 10:35:50 +00005823 return (cur);
5824}
5825
5826/**
5827 * xmlXPathNextPrecedingInternal:
5828 * @ctxt: the XPath Parser context
5829 * @cur: the current node in the traversal
5830 *
5831 * Traversal function for the "preceding" direction
5832 * the preceding axis contains all nodes in the same document as the context
5833 * node that are before the context node in document order, excluding any
5834 * ancestors and excluding attribute nodes and namespace nodes; the nodes are
5835 * ordered in reverse document order
5836 * This is a faster implementation but internal only since it requires a
5837 * state kept in the parser context: ctxt->ancestor.
5838 *
5839 * Returns the next element following that axis
5840 */
5841static xmlNodePtr
5842xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt,
5843 xmlNodePtr cur)
5844{
5845 if (cur == NULL) {
5846 cur = ctxt->context->node;
5847 if (cur == NULL)
5848 return (NULL);
William M. Brack40c22b42003-10-10 03:58:39 +00005849 if (cur->type == XML_NAMESPACE_DECL)
5850 cur = (xmlNodePtr)((xmlNsPtr)cur)->next;
Daniel Veillardf06307e2001-07-03 10:35:50 +00005851 ctxt->ancestor = cur->parent;
5852 }
5853 if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
5854 cur = cur->prev;
5855 while (cur->prev == NULL) {
5856 cur = cur->parent;
5857 if (cur == NULL)
5858 return (NULL);
5859 if (cur == ctxt->context->doc->children)
5860 return (NULL);
5861 if (cur != ctxt->ancestor)
5862 return (cur);
5863 ctxt->ancestor = cur->parent;
5864 }
5865 cur = cur->prev;
5866 while (cur->last != NULL)
5867 cur = cur->last;
5868 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00005869}
5870
5871/**
5872 * xmlXPathNextNamespace:
5873 * @ctxt: the XPath Parser context
5874 * @cur: the current attribute in the traversal
5875 *
5876 * Traversal function for the "namespace" direction
5877 * the namespace axis contains the namespace nodes of the context node;
5878 * the order of nodes on this axis is implementation-defined; the axis will
5879 * be empty unless the context node is an element
5880 *
Daniel Veillard20ee8c02001-10-05 09:18:14 +00005881 * We keep the XML namespace node at the end of the list.
5882 *
Owen Taylor3473f882001-02-23 17:55:21 +00005883 * Returns the next element following that axis
5884 */
5885xmlNodePtr
5886xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5887 if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL);
Daniel Veillardfdc91562002-07-01 21:52:03 +00005888 if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) {
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005889 if (ctxt->context->tmpNsList != NULL)
5890 xmlFree(ctxt->context->tmpNsList);
5891 ctxt->context->tmpNsList =
Owen Taylor3473f882001-02-23 17:55:21 +00005892 xmlGetNsList(ctxt->context->doc, ctxt->context->node);
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005893 ctxt->context->tmpNsNr = 0;
Daniel Veillardfdc91562002-07-01 21:52:03 +00005894 if (ctxt->context->tmpNsList != NULL) {
5895 while (ctxt->context->tmpNsList[ctxt->context->tmpNsNr] != NULL) {
5896 ctxt->context->tmpNsNr++;
5897 }
5898 }
Daniel Veillard20ee8c02001-10-05 09:18:14 +00005899 return((xmlNodePtr) xmlXPathXMLNamespace);
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005900 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00005901 if (ctxt->context->tmpNsNr > 0) {
5902 return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr];
5903 } else {
5904 if (ctxt->context->tmpNsList != NULL)
5905 xmlFree(ctxt->context->tmpNsList);
5906 ctxt->context->tmpNsList = NULL;
5907 return(NULL);
5908 }
Owen Taylor3473f882001-02-23 17:55:21 +00005909}
5910
5911/**
5912 * xmlXPathNextAttribute:
5913 * @ctxt: the XPath Parser context
5914 * @cur: the current attribute in the traversal
5915 *
5916 * Traversal function for the "attribute" direction
5917 * TODO: support DTD inherited default attributes
5918 *
5919 * Returns the next element following that axis
5920 */
5921xmlNodePtr
5922xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarde470df72001-04-18 21:41:07 +00005923 if (ctxt->context->node == NULL)
5924 return(NULL);
5925 if (ctxt->context->node->type != XML_ELEMENT_NODE)
5926 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005927 if (cur == NULL) {
5928 if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc)
5929 return(NULL);
5930 return((xmlNodePtr)ctxt->context->node->properties);
5931 }
5932 return((xmlNodePtr)cur->next);
5933}
5934
5935/************************************************************************
5936 * *
5937 * NodeTest Functions *
5938 * *
5939 ************************************************************************/
5940
Owen Taylor3473f882001-02-23 17:55:21 +00005941#define IS_FUNCTION 200
5942
Owen Taylor3473f882001-02-23 17:55:21 +00005943
5944/************************************************************************
5945 * *
5946 * Implicit tree core function library *
5947 * *
5948 ************************************************************************/
5949
5950/**
5951 * xmlXPathRoot:
5952 * @ctxt: the XPath Parser context
5953 *
5954 * Initialize the context to the root of the document
5955 */
5956void
5957xmlXPathRoot(xmlXPathParserContextPtr ctxt) {
5958 ctxt->context->node = (xmlNodePtr) ctxt->context->doc;
5959 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
5960}
5961
5962/************************************************************************
5963 * *
5964 * The explicit core function library *
5965 *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib *
5966 * *
5967 ************************************************************************/
5968
5969
5970/**
5971 * xmlXPathLastFunction:
5972 * @ctxt: the XPath Parser context
5973 * @nargs: the number of arguments
5974 *
5975 * Implement the last() XPath function
5976 * number last()
5977 * The last function returns the number of nodes in the context node list.
5978 */
5979void
5980xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) {
5981 CHECK_ARITY(0);
5982 if (ctxt->context->contextSize >= 0) {
5983 valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize));
5984#ifdef DEBUG_EXPR
5985 xmlGenericError(xmlGenericErrorContext,
5986 "last() : %d\n", ctxt->context->contextSize);
5987#endif
5988 } else {
5989 XP_ERROR(XPATH_INVALID_CTXT_SIZE);
5990 }
5991}
5992
5993/**
5994 * xmlXPathPositionFunction:
5995 * @ctxt: the XPath Parser context
5996 * @nargs: the number of arguments
5997 *
5998 * Implement the position() XPath function
5999 * number position()
6000 * The position function returns the position of the context node in the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006001 * context node list. The first position is 1, and so the last position
Owen Taylor3473f882001-02-23 17:55:21 +00006002 * will be equal to last().
6003 */
6004void
6005xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6006 CHECK_ARITY(0);
6007 if (ctxt->context->proximityPosition >= 0) {
6008 valuePush(ctxt,
6009 xmlXPathNewFloat((double) ctxt->context->proximityPosition));
6010#ifdef DEBUG_EXPR
6011 xmlGenericError(xmlGenericErrorContext, "position() : %d\n",
6012 ctxt->context->proximityPosition);
6013#endif
6014 } else {
6015 XP_ERROR(XPATH_INVALID_CTXT_POSITION);
6016 }
6017}
6018
6019/**
6020 * xmlXPathCountFunction:
6021 * @ctxt: the XPath Parser context
6022 * @nargs: the number of arguments
6023 *
6024 * Implement the count() XPath function
6025 * number count(node-set)
6026 */
6027void
6028xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6029 xmlXPathObjectPtr cur;
6030
6031 CHECK_ARITY(1);
6032 if ((ctxt->value == NULL) ||
6033 ((ctxt->value->type != XPATH_NODESET) &&
6034 (ctxt->value->type != XPATH_XSLT_TREE)))
6035 XP_ERROR(XPATH_INVALID_TYPE);
6036 cur = valuePop(ctxt);
6037
Daniel Veillard911f49a2001-04-07 15:39:35 +00006038 if ((cur == NULL) || (cur->nodesetval == NULL))
6039 valuePush(ctxt, xmlXPathNewFloat((double) 0));
William M. Brack0c022ad2002-07-12 00:56:01 +00006040 else if ((cur->type == XPATH_NODESET) || (cur->type == XPATH_XSLT_TREE)) {
Daniel Veillard911f49a2001-04-07 15:39:35 +00006041 valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr));
Daniel Veillardfe703322001-08-14 12:18:09 +00006042 } else {
6043 if ((cur->nodesetval->nodeNr != 1) ||
6044 (cur->nodesetval->nodeTab == NULL)) {
6045 valuePush(ctxt, xmlXPathNewFloat((double) 0));
6046 } else {
6047 xmlNodePtr tmp;
6048 int i = 0;
6049
6050 tmp = cur->nodesetval->nodeTab[0];
6051 if (tmp != NULL) {
6052 tmp = tmp->children;
6053 while (tmp != NULL) {
6054 tmp = tmp->next;
6055 i++;
6056 }
6057 }
6058 valuePush(ctxt, xmlXPathNewFloat((double) i));
6059 }
6060 }
Owen Taylor3473f882001-02-23 17:55:21 +00006061 xmlXPathFreeObject(cur);
6062}
6063
6064/**
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006065 * xmlXPathGetElementsByIds:
6066 * @doc: the document
6067 * @ids: a whitespace separated list of IDs
6068 *
6069 * Selects elements by their unique ID.
6070 *
6071 * Returns a node-set of selected elements.
6072 */
6073static xmlNodeSetPtr
6074xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) {
6075 xmlNodeSetPtr ret;
6076 const xmlChar *cur = ids;
6077 xmlChar *ID;
6078 xmlAttrPtr attr;
6079 xmlNodePtr elem = NULL;
6080
Daniel Veillard7a985a12003-07-06 17:57:42 +00006081 if (ids == NULL) return(NULL);
6082
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006083 ret = xmlXPathNodeSetCreate(NULL);
6084
William M. Brack76e95df2003-10-18 16:20:14 +00006085 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006086 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006087 while ((!IS_BLANK_CH(*cur)) && (*cur != 0))
Daniel Veillarde209b332003-03-26 21:40:13 +00006088 cur++;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006089
6090 ID = xmlStrndup(ids, cur - ids);
Daniel Veillarde209b332003-03-26 21:40:13 +00006091 if (ID != NULL) {
6092 if (xmlValidateNCName(ID, 1) == 0) {
6093 attr = xmlGetID(doc, ID);
6094 if (attr != NULL) {
6095 if (attr->type == XML_ATTRIBUTE_NODE)
6096 elem = attr->parent;
6097 else if (attr->type == XML_ELEMENT_NODE)
6098 elem = (xmlNodePtr) attr;
6099 else
6100 elem = NULL;
6101 if (elem != NULL)
6102 xmlXPathNodeSetAdd(ret, elem);
6103 }
6104 }
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006105 xmlFree(ID);
Daniel Veillarde209b332003-03-26 21:40:13 +00006106 }
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006107
William M. Brack76e95df2003-10-18 16:20:14 +00006108 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006109 ids = cur;
6110 }
6111 return(ret);
6112}
6113
6114/**
Owen Taylor3473f882001-02-23 17:55:21 +00006115 * xmlXPathIdFunction:
6116 * @ctxt: the XPath Parser context
6117 * @nargs: the number of arguments
6118 *
6119 * Implement the id() XPath function
6120 * node-set id(object)
6121 * The id function selects elements by their unique ID
6122 * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set,
6123 * then the result is the union of the result of applying id to the
6124 * string value of each of the nodes in the argument node-set. When the
6125 * argument to id is of any other type, the argument is converted to a
6126 * string as if by a call to the string function; the string is split
6127 * into a whitespace-separated list of tokens (whitespace is any sequence
6128 * of characters matching the production S); the result is a node-set
6129 * containing the elements in the same document as the context node that
6130 * have a unique ID equal to any of the tokens in the list.
6131 */
6132void
6133xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006134 xmlChar *tokens;
6135 xmlNodeSetPtr ret;
6136 xmlXPathObjectPtr obj;
Owen Taylor3473f882001-02-23 17:55:21 +00006137
6138 CHECK_ARITY(1);
6139 obj = valuePop(ctxt);
6140 if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
William M. Brack0c022ad2002-07-12 00:56:01 +00006141 if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006142 xmlNodeSetPtr ns;
Owen Taylor3473f882001-02-23 17:55:21 +00006143 int i;
6144
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006145 ret = xmlXPathNodeSetCreate(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006146
Daniel Veillard911f49a2001-04-07 15:39:35 +00006147 if (obj->nodesetval != NULL) {
6148 for (i = 0; i < obj->nodesetval->nodeNr; i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006149 tokens =
6150 xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]);
6151 ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens);
6152 ret = xmlXPathNodeSetMerge(ret, ns);
6153 xmlXPathFreeNodeSet(ns);
6154 if (tokens != NULL)
6155 xmlFree(tokens);
Daniel Veillard911f49a2001-04-07 15:39:35 +00006156 }
Owen Taylor3473f882001-02-23 17:55:21 +00006157 }
6158
6159 xmlXPathFreeObject(obj);
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006160 valuePush(ctxt, xmlXPathWrapNodeSet(ret));
Owen Taylor3473f882001-02-23 17:55:21 +00006161 return;
6162 }
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006163 obj = xmlXPathConvertString(obj);
Owen Taylor3473f882001-02-23 17:55:21 +00006164
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006165 ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval);
6166 valuePush(ctxt, xmlXPathWrapNodeSet(ret));
Owen Taylor3473f882001-02-23 17:55:21 +00006167
Owen Taylor3473f882001-02-23 17:55:21 +00006168 xmlXPathFreeObject(obj);
6169 return;
6170}
6171
6172/**
6173 * xmlXPathLocalNameFunction:
6174 * @ctxt: the XPath Parser context
6175 * @nargs: the number of arguments
6176 *
6177 * Implement the local-name() XPath function
6178 * string local-name(node-set?)
6179 * The local-name function returns a string containing the local part
6180 * of the name of the node in the argument node-set that is first in
6181 * document order. If the node-set is empty or the first node has no
6182 * name, an empty string is returned. If the argument is omitted it
6183 * defaults to the context node.
6184 */
6185void
6186xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6187 xmlXPathObjectPtr cur;
6188
6189 if (nargs == 0) {
6190 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
6191 nargs = 1;
6192 }
6193
6194 CHECK_ARITY(1);
6195 if ((ctxt->value == NULL) ||
6196 ((ctxt->value->type != XPATH_NODESET) &&
6197 (ctxt->value->type != XPATH_XSLT_TREE)))
6198 XP_ERROR(XPATH_INVALID_TYPE);
6199 cur = valuePop(ctxt);
6200
Daniel Veillard911f49a2001-04-07 15:39:35 +00006201 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006202 valuePush(ctxt, xmlXPathNewCString(""));
6203 } else {
6204 int i = 0; /* Should be first in document order !!!!! */
6205 switch (cur->nodesetval->nodeTab[i]->type) {
6206 case XML_ELEMENT_NODE:
6207 case XML_ATTRIBUTE_NODE:
6208 case XML_PI_NODE:
Daniel Veillard652d8a92003-02-04 19:28:49 +00006209 if (cur->nodesetval->nodeTab[i]->name[0] == ' ')
6210 valuePush(ctxt, xmlXPathNewCString(""));
6211 else
6212 valuePush(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00006213 xmlXPathNewString(cur->nodesetval->nodeTab[i]->name));
6214 break;
6215 case XML_NAMESPACE_DECL:
6216 valuePush(ctxt, xmlXPathNewString(
6217 ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix));
6218 break;
6219 default:
6220 valuePush(ctxt, xmlXPathNewCString(""));
6221 }
6222 }
6223 xmlXPathFreeObject(cur);
6224}
6225
6226/**
6227 * xmlXPathNamespaceURIFunction:
6228 * @ctxt: the XPath Parser context
6229 * @nargs: the number of arguments
6230 *
6231 * Implement the namespace-uri() XPath function
6232 * string namespace-uri(node-set?)
6233 * The namespace-uri function returns a string containing the
6234 * namespace URI of the expanded name of the node in the argument
6235 * node-set that is first in document order. If the node-set is empty,
6236 * the first node has no name, or the expanded name has no namespace
6237 * URI, an empty string is returned. If the argument is omitted it
6238 * defaults to the context node.
6239 */
6240void
6241xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6242 xmlXPathObjectPtr cur;
6243
6244 if (nargs == 0) {
6245 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
6246 nargs = 1;
6247 }
6248 CHECK_ARITY(1);
6249 if ((ctxt->value == NULL) ||
6250 ((ctxt->value->type != XPATH_NODESET) &&
6251 (ctxt->value->type != XPATH_XSLT_TREE)))
6252 XP_ERROR(XPATH_INVALID_TYPE);
6253 cur = valuePop(ctxt);
6254
Daniel Veillard911f49a2001-04-07 15:39:35 +00006255 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006256 valuePush(ctxt, xmlXPathNewCString(""));
6257 } else {
6258 int i = 0; /* Should be first in document order !!!!! */
6259 switch (cur->nodesetval->nodeTab[i]->type) {
6260 case XML_ELEMENT_NODE:
6261 case XML_ATTRIBUTE_NODE:
6262 if (cur->nodesetval->nodeTab[i]->ns == NULL)
6263 valuePush(ctxt, xmlXPathNewCString(""));
6264 else
6265 valuePush(ctxt, xmlXPathNewString(
6266 cur->nodesetval->nodeTab[i]->ns->href));
6267 break;
6268 default:
6269 valuePush(ctxt, xmlXPathNewCString(""));
6270 }
6271 }
6272 xmlXPathFreeObject(cur);
6273}
6274
6275/**
6276 * xmlXPathNameFunction:
6277 * @ctxt: the XPath Parser context
6278 * @nargs: the number of arguments
6279 *
6280 * Implement the name() XPath function
6281 * string name(node-set?)
6282 * The name function returns a string containing a QName representing
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006283 * the name of the node in the argument node-set that is first in document
Owen Taylor3473f882001-02-23 17:55:21 +00006284 * order. The QName must represent the name with respect to the namespace
6285 * declarations in effect on the node whose name is being represented.
6286 * Typically, this will be the form in which the name occurred in the XML
6287 * source. This need not be the case if there are namespace declarations
6288 * in effect on the node that associate multiple prefixes with the same
6289 * namespace. However, an implementation may include information about
6290 * the original prefix in its representation of nodes; in this case, an
6291 * implementation can ensure that the returned string is always the same
6292 * as the QName used in the XML source. If the argument it omitted it
6293 * defaults to the context node.
6294 * Libxml keep the original prefix so the "real qualified name" used is
6295 * returned.
6296 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006297static void
Daniel Veillard04383752001-07-08 14:27:15 +00006298xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs)
6299{
Owen Taylor3473f882001-02-23 17:55:21 +00006300 xmlXPathObjectPtr cur;
6301
6302 if (nargs == 0) {
Daniel Veillard04383752001-07-08 14:27:15 +00006303 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
6304 nargs = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006305 }
6306
6307 CHECK_ARITY(1);
Daniel Veillard04383752001-07-08 14:27:15 +00006308 if ((ctxt->value == NULL) ||
6309 ((ctxt->value->type != XPATH_NODESET) &&
6310 (ctxt->value->type != XPATH_XSLT_TREE)))
6311 XP_ERROR(XPATH_INVALID_TYPE);
Owen Taylor3473f882001-02-23 17:55:21 +00006312 cur = valuePop(ctxt);
6313
Daniel Veillard911f49a2001-04-07 15:39:35 +00006314 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Daniel Veillard04383752001-07-08 14:27:15 +00006315 valuePush(ctxt, xmlXPathNewCString(""));
Owen Taylor3473f882001-02-23 17:55:21 +00006316 } else {
Daniel Veillard04383752001-07-08 14:27:15 +00006317 int i = 0; /* Should be first in document order !!!!! */
Owen Taylor3473f882001-02-23 17:55:21 +00006318
Daniel Veillard04383752001-07-08 14:27:15 +00006319 switch (cur->nodesetval->nodeTab[i]->type) {
6320 case XML_ELEMENT_NODE:
6321 case XML_ATTRIBUTE_NODE:
Daniel Veillard652d8a92003-02-04 19:28:49 +00006322 if (cur->nodesetval->nodeTab[i]->name[0] == ' ')
6323 valuePush(ctxt, xmlXPathNewCString(""));
6324 else if ((cur->nodesetval->nodeTab[i]->ns == NULL) ||
6325 (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) {
Daniel Veillard04383752001-07-08 14:27:15 +00006326 valuePush(ctxt,
Daniel Veillardc00cda82003-04-07 10:22:39 +00006327 xmlXPathNewString(cur->nodesetval->nodeTab[i]->name));
Daniel Veillard04383752001-07-08 14:27:15 +00006328
Daniel Veillard652d8a92003-02-04 19:28:49 +00006329 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00006330 xmlChar *fullname;
6331
6332 fullname = xmlBuildQName(cur->nodesetval->nodeTab[i]->name,
6333 cur->nodesetval->nodeTab[i]->ns->prefix,
6334 NULL, 0);
6335 if (fullname == cur->nodesetval->nodeTab[i]->name)
6336 fullname = xmlStrdup(cur->nodesetval->nodeTab[i]->name);
6337 if (fullname == NULL) {
6338 XP_ERROR(XPATH_MEMORY_ERROR);
6339 }
6340 valuePush(ctxt, xmlXPathWrapString(fullname));
Daniel Veillard04383752001-07-08 14:27:15 +00006341 }
6342 break;
6343 default:
6344 valuePush(ctxt,
6345 xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i]));
6346 xmlXPathLocalNameFunction(ctxt, 1);
6347 }
Owen Taylor3473f882001-02-23 17:55:21 +00006348 }
6349 xmlXPathFreeObject(cur);
6350}
6351
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00006352
6353/**
Owen Taylor3473f882001-02-23 17:55:21 +00006354 * xmlXPathStringFunction:
6355 * @ctxt: the XPath Parser context
6356 * @nargs: the number of arguments
6357 *
6358 * Implement the string() XPath function
6359 * string string(object?)
William M. Brack08171912003-12-29 02:52:11 +00006360 * The string function converts an object to a string as follows:
Owen Taylor3473f882001-02-23 17:55:21 +00006361 * - A node-set is converted to a string by returning the value of
6362 * the node in the node-set that is first in document order.
6363 * If the node-set is empty, an empty string is returned.
6364 * - A number is converted to a string as follows
6365 * + NaN is converted to the string NaN
6366 * + positive zero is converted to the string 0
6367 * + negative zero is converted to the string 0
6368 * + positive infinity is converted to the string Infinity
6369 * + negative infinity is converted to the string -Infinity
6370 * + if the number is an integer, the number is represented in
6371 * decimal form as a Number with no decimal point and no leading
6372 * zeros, preceded by a minus sign (-) if the number is negative
6373 * + otherwise, the number is represented in decimal form as a
6374 * Number including a decimal point with at least one digit
6375 * before the decimal point and at least one digit after the
6376 * decimal point, preceded by a minus sign (-) if the number
6377 * is negative; there must be no leading zeros before the decimal
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006378 * point apart possibly from the one required digit immediately
Owen Taylor3473f882001-02-23 17:55:21 +00006379 * before the decimal point; beyond the one required digit
6380 * after the decimal point there must be as many, but only as
6381 * many, more digits as are needed to uniquely distinguish the
6382 * number from all other IEEE 754 numeric values.
6383 * - The boolean false value is converted to the string false.
6384 * The boolean true value is converted to the string true.
6385 *
6386 * If the argument is omitted, it defaults to a node-set with the
6387 * context node as its only member.
6388 */
6389void
6390xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6391 xmlXPathObjectPtr cur;
6392
6393 if (nargs == 0) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006394 valuePush(ctxt,
6395 xmlXPathWrapString(
6396 xmlXPathCastNodeToString(ctxt->context->node)));
6397 return;
Owen Taylor3473f882001-02-23 17:55:21 +00006398 }
6399
6400 CHECK_ARITY(1);
6401 cur = valuePop(ctxt);
6402 if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00006403 cur = xmlXPathConvertString(cur);
6404 valuePush(ctxt, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00006405}
6406
6407/**
6408 * xmlXPathStringLengthFunction:
6409 * @ctxt: the XPath Parser context
6410 * @nargs: the number of arguments
6411 *
6412 * Implement the string-length() XPath function
6413 * number string-length(string?)
6414 * The string-length returns the number of characters in the string
6415 * (see [3.6 Strings]). If the argument is omitted, it defaults to
6416 * the context node converted to a string, in other words the value
6417 * of the context node.
6418 */
6419void
6420xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6421 xmlXPathObjectPtr cur;
6422
6423 if (nargs == 0) {
6424 if (ctxt->context->node == NULL) {
6425 valuePush(ctxt, xmlXPathNewFloat(0));
6426 } else {
6427 xmlChar *content;
6428
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006429 content = xmlXPathCastNodeToString(ctxt->context->node);
Daniel Veillarde043ee12001-04-16 14:08:07 +00006430 valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content)));
Owen Taylor3473f882001-02-23 17:55:21 +00006431 xmlFree(content);
6432 }
6433 return;
6434 }
6435 CHECK_ARITY(1);
6436 CAST_TO_STRING;
6437 CHECK_TYPE(XPATH_STRING);
6438 cur = valuePop(ctxt);
Daniel Veillarde043ee12001-04-16 14:08:07 +00006439 valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(cur->stringval)));
Owen Taylor3473f882001-02-23 17:55:21 +00006440 xmlXPathFreeObject(cur);
6441}
6442
6443/**
6444 * xmlXPathConcatFunction:
6445 * @ctxt: the XPath Parser context
6446 * @nargs: the number of arguments
6447 *
6448 * Implement the concat() XPath function
6449 * string concat(string, string, string*)
6450 * The concat function returns the concatenation of its arguments.
6451 */
6452void
6453xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6454 xmlXPathObjectPtr cur, newobj;
6455 xmlChar *tmp;
6456
6457 if (nargs < 2) {
6458 CHECK_ARITY(2);
6459 }
6460
6461 CAST_TO_STRING;
6462 cur = valuePop(ctxt);
6463 if ((cur == NULL) || (cur->type != XPATH_STRING)) {
6464 xmlXPathFreeObject(cur);
6465 return;
6466 }
6467 nargs--;
6468
6469 while (nargs > 0) {
6470 CAST_TO_STRING;
6471 newobj = valuePop(ctxt);
6472 if ((newobj == NULL) || (newobj->type != XPATH_STRING)) {
6473 xmlXPathFreeObject(newobj);
6474 xmlXPathFreeObject(cur);
6475 XP_ERROR(XPATH_INVALID_TYPE);
6476 }
6477 tmp = xmlStrcat(newobj->stringval, cur->stringval);
6478 newobj->stringval = cur->stringval;
6479 cur->stringval = tmp;
6480
6481 xmlXPathFreeObject(newobj);
6482 nargs--;
6483 }
6484 valuePush(ctxt, cur);
6485}
6486
6487/**
6488 * xmlXPathContainsFunction:
6489 * @ctxt: the XPath Parser context
6490 * @nargs: the number of arguments
6491 *
6492 * Implement the contains() XPath function
6493 * boolean contains(string, string)
6494 * The contains function returns true if the first argument string
6495 * contains the second argument string, and otherwise returns false.
6496 */
6497void
6498xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6499 xmlXPathObjectPtr hay, needle;
6500
6501 CHECK_ARITY(2);
6502 CAST_TO_STRING;
6503 CHECK_TYPE(XPATH_STRING);
6504 needle = valuePop(ctxt);
6505 CAST_TO_STRING;
6506 hay = valuePop(ctxt);
6507 if ((hay == NULL) || (hay->type != XPATH_STRING)) {
6508 xmlXPathFreeObject(hay);
6509 xmlXPathFreeObject(needle);
6510 XP_ERROR(XPATH_INVALID_TYPE);
6511 }
6512 if (xmlStrstr(hay->stringval, needle->stringval))
6513 valuePush(ctxt, xmlXPathNewBoolean(1));
6514 else
6515 valuePush(ctxt, xmlXPathNewBoolean(0));
6516 xmlXPathFreeObject(hay);
6517 xmlXPathFreeObject(needle);
6518}
6519
6520/**
6521 * xmlXPathStartsWithFunction:
6522 * @ctxt: the XPath Parser context
6523 * @nargs: the number of arguments
6524 *
6525 * Implement the starts-with() XPath function
6526 * boolean starts-with(string, string)
6527 * The starts-with function returns true if the first argument string
6528 * starts with the second argument string, and otherwise returns false.
6529 */
6530void
6531xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6532 xmlXPathObjectPtr hay, needle;
6533 int n;
6534
6535 CHECK_ARITY(2);
6536 CAST_TO_STRING;
6537 CHECK_TYPE(XPATH_STRING);
6538 needle = valuePop(ctxt);
6539 CAST_TO_STRING;
6540 hay = valuePop(ctxt);
6541 if ((hay == NULL) || (hay->type != XPATH_STRING)) {
6542 xmlXPathFreeObject(hay);
6543 xmlXPathFreeObject(needle);
6544 XP_ERROR(XPATH_INVALID_TYPE);
6545 }
6546 n = xmlStrlen(needle->stringval);
6547 if (xmlStrncmp(hay->stringval, needle->stringval, n))
6548 valuePush(ctxt, xmlXPathNewBoolean(0));
6549 else
6550 valuePush(ctxt, xmlXPathNewBoolean(1));
6551 xmlXPathFreeObject(hay);
6552 xmlXPathFreeObject(needle);
6553}
6554
6555/**
6556 * xmlXPathSubstringFunction:
6557 * @ctxt: the XPath Parser context
6558 * @nargs: the number of arguments
6559 *
6560 * Implement the substring() XPath function
6561 * string substring(string, number, number?)
6562 * The substring function returns the substring of the first argument
6563 * starting at the position specified in the second argument with
6564 * length specified in the third argument. For example,
6565 * substring("12345",2,3) returns "234". If the third argument is not
6566 * specified, it returns the substring starting at the position specified
6567 * in the second argument and continuing to the end of the string. For
6568 * example, substring("12345",2) returns "2345". More precisely, each
6569 * character in the string (see [3.6 Strings]) is considered to have a
6570 * numeric position: the position of the first character is 1, the position
6571 * of the second character is 2 and so on. The returned substring contains
6572 * those characters for which the position of the character is greater than
6573 * or equal to the second argument and, if the third argument is specified,
6574 * less than the sum of the second and third arguments; the comparisons
6575 * and addition used for the above follow the standard IEEE 754 rules. Thus:
6576 * - substring("12345", 1.5, 2.6) returns "234"
6577 * - substring("12345", 0, 3) returns "12"
6578 * - substring("12345", 0 div 0, 3) returns ""
6579 * - substring("12345", 1, 0 div 0) returns ""
6580 * - substring("12345", -42, 1 div 0) returns "12345"
6581 * - substring("12345", -1 div 0, 1 div 0) returns ""
6582 */
6583void
6584xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6585 xmlXPathObjectPtr str, start, len;
Daniel Veillard97ac1312001-05-30 19:14:17 +00006586 double le=0, in;
6587 int i, l, m;
Owen Taylor3473f882001-02-23 17:55:21 +00006588 xmlChar *ret;
6589
Owen Taylor3473f882001-02-23 17:55:21 +00006590 if (nargs < 2) {
6591 CHECK_ARITY(2);
6592 }
6593 if (nargs > 3) {
6594 CHECK_ARITY(3);
6595 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006596 /*
6597 * take care of possible last (position) argument
6598 */
Owen Taylor3473f882001-02-23 17:55:21 +00006599 if (nargs == 3) {
6600 CAST_TO_NUMBER;
6601 CHECK_TYPE(XPATH_NUMBER);
6602 len = valuePop(ctxt);
6603 le = len->floatval;
6604 xmlXPathFreeObject(len);
Owen Taylor3473f882001-02-23 17:55:21 +00006605 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006606
Owen Taylor3473f882001-02-23 17:55:21 +00006607 CAST_TO_NUMBER;
6608 CHECK_TYPE(XPATH_NUMBER);
6609 start = valuePop(ctxt);
6610 in = start->floatval;
6611 xmlXPathFreeObject(start);
6612 CAST_TO_STRING;
6613 CHECK_TYPE(XPATH_STRING);
6614 str = valuePop(ctxt);
Daniel Veillard97ac1312001-05-30 19:14:17 +00006615 m = xmlUTF8Strlen((const unsigned char *)str->stringval);
Owen Taylor3473f882001-02-23 17:55:21 +00006616
Daniel Veillard97ac1312001-05-30 19:14:17 +00006617 /*
6618 * If last pos not present, calculate last position
6619 */
Daniel Veillard9e412302002-06-10 15:59:44 +00006620 if (nargs != 3) {
6621 le = (double)m;
6622 if (in < 1.0)
6623 in = 1.0;
6624 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006625
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006626 /* Need to check for the special cases where either
6627 * the index is NaN, the length is NaN, or both
6628 * arguments are infinity (relying on Inf + -Inf = NaN)
Daniel Veillard97ac1312001-05-30 19:14:17 +00006629 */
Daniel Veillard9e412302002-06-10 15:59:44 +00006630 if (!xmlXPathIsNaN(in + le) && !xmlXPathIsInf(in)) {
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006631 /*
Daniel Veillard9e412302002-06-10 15:59:44 +00006632 * To meet the requirements of the spec, the arguments
6633 * must be converted to integer format before
6634 * initial index calculations are done
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006635 *
Daniel Veillard9e412302002-06-10 15:59:44 +00006636 * First we go to integer form, rounding up
6637 * and checking for special cases
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006638 */
6639 i = (int) in;
Daniel Veillard9e412302002-06-10 15:59:44 +00006640 if (((double)i)+0.5 <= in) i++;
Owen Taylor3473f882001-02-23 17:55:21 +00006641
Daniel Veillard9e412302002-06-10 15:59:44 +00006642 if (xmlXPathIsInf(le) == 1) {
6643 l = m;
6644 if (i < 1)
6645 i = 1;
6646 }
6647 else if (xmlXPathIsInf(le) == -1 || le < 0.0)
6648 l = 0;
6649 else {
6650 l = (int) le;
6651 if (((double)l)+0.5 <= le) l++;
6652 }
6653
6654 /* Now we normalize inidices */
6655 i -= 1;
6656 l += i;
6657 if (i < 0)
6658 i = 0;
6659 if (l > m)
6660 l = m;
Owen Taylor3473f882001-02-23 17:55:21 +00006661
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006662 /* number of chars to copy */
6663 l -= i;
Owen Taylor3473f882001-02-23 17:55:21 +00006664
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006665 ret = xmlUTF8Strsub(str->stringval, i, l);
6666 }
6667 else {
6668 ret = NULL;
6669 }
6670
Owen Taylor3473f882001-02-23 17:55:21 +00006671 if (ret == NULL)
6672 valuePush(ctxt, xmlXPathNewCString(""));
6673 else {
6674 valuePush(ctxt, xmlXPathNewString(ret));
6675 xmlFree(ret);
6676 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006677
Owen Taylor3473f882001-02-23 17:55:21 +00006678 xmlXPathFreeObject(str);
6679}
6680
6681/**
6682 * xmlXPathSubstringBeforeFunction:
6683 * @ctxt: the XPath Parser context
6684 * @nargs: the number of arguments
6685 *
6686 * Implement the substring-before() XPath function
6687 * string substring-before(string, string)
6688 * The substring-before function returns the substring of the first
6689 * argument string that precedes the first occurrence of the second
6690 * argument string in the first argument string, or the empty string
6691 * if the first argument string does not contain the second argument
6692 * string. For example, substring-before("1999/04/01","/") returns 1999.
6693 */
6694void
6695xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6696 xmlXPathObjectPtr str;
6697 xmlXPathObjectPtr find;
6698 xmlBufferPtr target;
6699 const xmlChar *point;
6700 int offset;
6701
6702 CHECK_ARITY(2);
6703 CAST_TO_STRING;
6704 find = valuePop(ctxt);
6705 CAST_TO_STRING;
6706 str = valuePop(ctxt);
6707
6708 target = xmlBufferCreate();
6709 if (target) {
6710 point = xmlStrstr(str->stringval, find->stringval);
6711 if (point) {
6712 offset = (int)(point - str->stringval);
6713 xmlBufferAdd(target, str->stringval, offset);
6714 }
6715 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6716 xmlBufferFree(target);
6717 }
6718
6719 xmlXPathFreeObject(str);
6720 xmlXPathFreeObject(find);
6721}
6722
6723/**
6724 * xmlXPathSubstringAfterFunction:
6725 * @ctxt: the XPath Parser context
6726 * @nargs: the number of arguments
6727 *
6728 * Implement the substring-after() XPath function
6729 * string substring-after(string, string)
6730 * The substring-after function returns the substring of the first
6731 * argument string that follows the first occurrence of the second
6732 * argument string in the first argument string, or the empty stringi
6733 * if the first argument string does not contain the second argument
6734 * string. For example, substring-after("1999/04/01","/") returns 04/01,
6735 * and substring-after("1999/04/01","19") returns 99/04/01.
6736 */
6737void
6738xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6739 xmlXPathObjectPtr str;
6740 xmlXPathObjectPtr find;
6741 xmlBufferPtr target;
6742 const xmlChar *point;
6743 int offset;
6744
6745 CHECK_ARITY(2);
6746 CAST_TO_STRING;
6747 find = valuePop(ctxt);
6748 CAST_TO_STRING;
6749 str = valuePop(ctxt);
6750
6751 target = xmlBufferCreate();
6752 if (target) {
6753 point = xmlStrstr(str->stringval, find->stringval);
6754 if (point) {
6755 offset = (int)(point - str->stringval) + xmlStrlen(find->stringval);
6756 xmlBufferAdd(target, &str->stringval[offset],
6757 xmlStrlen(str->stringval) - offset);
6758 }
6759 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6760 xmlBufferFree(target);
6761 }
6762
6763 xmlXPathFreeObject(str);
6764 xmlXPathFreeObject(find);
6765}
6766
6767/**
6768 * xmlXPathNormalizeFunction:
6769 * @ctxt: the XPath Parser context
6770 * @nargs: the number of arguments
6771 *
6772 * Implement the normalize-space() XPath function
6773 * string normalize-space(string?)
6774 * The normalize-space function returns the argument string with white
6775 * space normalized by stripping leading and trailing whitespace
6776 * and replacing sequences of whitespace characters by a single
6777 * space. Whitespace characters are the same allowed by the S production
6778 * in XML. If the argument is omitted, it defaults to the context
6779 * node converted to a string, in other words the value of the context node.
6780 */
6781void
6782xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6783 xmlXPathObjectPtr obj = NULL;
6784 xmlChar *source = NULL;
6785 xmlBufferPtr target;
6786 xmlChar blank;
6787
6788 if (nargs == 0) {
6789 /* Use current context node */
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006790 valuePush(ctxt,
6791 xmlXPathWrapString(
6792 xmlXPathCastNodeToString(ctxt->context->node)));
Owen Taylor3473f882001-02-23 17:55:21 +00006793 nargs = 1;
6794 }
6795
6796 CHECK_ARITY(1);
6797 CAST_TO_STRING;
6798 CHECK_TYPE(XPATH_STRING);
6799 obj = valuePop(ctxt);
6800 source = obj->stringval;
6801
6802 target = xmlBufferCreate();
6803 if (target && source) {
6804
6805 /* Skip leading whitespaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006806 while (IS_BLANK_CH(*source))
Owen Taylor3473f882001-02-23 17:55:21 +00006807 source++;
6808
6809 /* Collapse intermediate whitespaces, and skip trailing whitespaces */
6810 blank = 0;
6811 while (*source) {
William M. Brack76e95df2003-10-18 16:20:14 +00006812 if (IS_BLANK_CH(*source)) {
Daniel Veillard97ac1312001-05-30 19:14:17 +00006813 blank = 0x20;
Owen Taylor3473f882001-02-23 17:55:21 +00006814 } else {
6815 if (blank) {
6816 xmlBufferAdd(target, &blank, 1);
6817 blank = 0;
6818 }
6819 xmlBufferAdd(target, source, 1);
6820 }
6821 source++;
6822 }
6823
6824 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6825 xmlBufferFree(target);
6826 }
6827 xmlXPathFreeObject(obj);
6828}
6829
6830/**
6831 * xmlXPathTranslateFunction:
6832 * @ctxt: the XPath Parser context
6833 * @nargs: the number of arguments
6834 *
6835 * Implement the translate() XPath function
6836 * string translate(string, string, string)
6837 * The translate function returns the first argument string with
6838 * occurrences of characters in the second argument string replaced
6839 * by the character at the corresponding position in the third argument
6840 * string. For example, translate("bar","abc","ABC") returns the string
6841 * BAr. If there is a character in the second argument string with no
6842 * character at a corresponding position in the third argument string
6843 * (because the second argument string is longer than the third argument
6844 * string), then occurrences of that character in the first argument
6845 * string are removed. For example, translate("--aaa--","abc-","ABC")
6846 * returns "AAA". If a character occurs more than once in second
6847 * argument string, then the first occurrence determines the replacement
6848 * character. If the third argument string is longer than the second
6849 * argument string, then excess characters are ignored.
6850 */
6851void
6852xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillarde043ee12001-04-16 14:08:07 +00006853 xmlXPathObjectPtr str;
6854 xmlXPathObjectPtr from;
6855 xmlXPathObjectPtr to;
6856 xmlBufferPtr target;
Daniel Veillard97ac1312001-05-30 19:14:17 +00006857 int offset, max;
Daniel Veillarde043ee12001-04-16 14:08:07 +00006858 xmlChar ch;
Daniel Veillard97ac1312001-05-30 19:14:17 +00006859 xmlChar *point;
6860 xmlChar *cptr;
Owen Taylor3473f882001-02-23 17:55:21 +00006861
Daniel Veillarde043ee12001-04-16 14:08:07 +00006862 CHECK_ARITY(3);
Owen Taylor3473f882001-02-23 17:55:21 +00006863
Daniel Veillarde043ee12001-04-16 14:08:07 +00006864 CAST_TO_STRING;
6865 to = valuePop(ctxt);
6866 CAST_TO_STRING;
6867 from = valuePop(ctxt);
6868 CAST_TO_STRING;
6869 str = valuePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006870
Daniel Veillarde043ee12001-04-16 14:08:07 +00006871 target = xmlBufferCreate();
6872 if (target) {
Daniel Veillard97ac1312001-05-30 19:14:17 +00006873 max = xmlUTF8Strlen(to->stringval);
6874 for (cptr = str->stringval; (ch=*cptr); ) {
6875 offset = xmlUTF8Strloc(from->stringval, cptr);
6876 if (offset >= 0) {
6877 if (offset < max) {
6878 point = xmlUTF8Strpos(to->stringval, offset);
6879 if (point)
6880 xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1));
6881 }
6882 } else
6883 xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1));
6884
6885 /* Step to next character in input */
6886 cptr++;
6887 if ( ch & 0x80 ) {
6888 /* if not simple ascii, verify proper format */
6889 if ( (ch & 0xc0) != 0xc0 ) {
6890 xmlGenericError(xmlGenericErrorContext,
6891 "xmlXPathTranslateFunction: Invalid UTF8 string\n");
6892 break;
6893 }
6894 /* then skip over remaining bytes for this char */
6895 while ( (ch <<= 1) & 0x80 )
6896 if ( (*cptr++ & 0xc0) != 0x80 ) {
6897 xmlGenericError(xmlGenericErrorContext,
6898 "xmlXPathTranslateFunction: Invalid UTF8 string\n");
6899 break;
6900 }
6901 if (ch & 0x80) /* must have had error encountered */
6902 break;
6903 }
Daniel Veillarde043ee12001-04-16 14:08:07 +00006904 }
Owen Taylor3473f882001-02-23 17:55:21 +00006905 }
Daniel Veillarde043ee12001-04-16 14:08:07 +00006906 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6907 xmlBufferFree(target);
6908 xmlXPathFreeObject(str);
6909 xmlXPathFreeObject(from);
6910 xmlXPathFreeObject(to);
Owen Taylor3473f882001-02-23 17:55:21 +00006911}
6912
6913/**
6914 * xmlXPathBooleanFunction:
6915 * @ctxt: the XPath Parser context
6916 * @nargs: the number of arguments
6917 *
6918 * Implement the boolean() XPath function
6919 * boolean boolean(object)
William M. Brack08171912003-12-29 02:52:11 +00006920 * The boolean function converts its argument to a boolean as follows:
Owen Taylor3473f882001-02-23 17:55:21 +00006921 * - a number is true if and only if it is neither positive or
6922 * negative zero nor NaN
6923 * - a node-set is true if and only if it is non-empty
6924 * - a string is true if and only if its length is non-zero
6925 */
6926void
6927xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6928 xmlXPathObjectPtr cur;
Owen Taylor3473f882001-02-23 17:55:21 +00006929
6930 CHECK_ARITY(1);
6931 cur = valuePop(ctxt);
6932 if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00006933 cur = xmlXPathConvertBoolean(cur);
6934 valuePush(ctxt, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00006935}
6936
6937/**
6938 * xmlXPathNotFunction:
6939 * @ctxt: the XPath Parser context
6940 * @nargs: the number of arguments
6941 *
6942 * Implement the not() XPath function
6943 * boolean not(boolean)
6944 * The not function returns true if its argument is false,
6945 * and false otherwise.
6946 */
6947void
6948xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6949 CHECK_ARITY(1);
6950 CAST_TO_BOOLEAN;
6951 CHECK_TYPE(XPATH_BOOLEAN);
6952 ctxt->value->boolval = ! ctxt->value->boolval;
6953}
6954
6955/**
6956 * xmlXPathTrueFunction:
6957 * @ctxt: the XPath Parser context
6958 * @nargs: the number of arguments
6959 *
6960 * Implement the true() XPath function
6961 * boolean true()
6962 */
6963void
6964xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6965 CHECK_ARITY(0);
6966 valuePush(ctxt, xmlXPathNewBoolean(1));
6967}
6968
6969/**
6970 * xmlXPathFalseFunction:
6971 * @ctxt: the XPath Parser context
6972 * @nargs: the number of arguments
6973 *
6974 * Implement the false() XPath function
6975 * boolean false()
6976 */
6977void
6978xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6979 CHECK_ARITY(0);
6980 valuePush(ctxt, xmlXPathNewBoolean(0));
6981}
6982
6983/**
6984 * xmlXPathLangFunction:
6985 * @ctxt: the XPath Parser context
6986 * @nargs: the number of arguments
6987 *
6988 * Implement the lang() XPath function
6989 * boolean lang(string)
6990 * The lang function returns true or false depending on whether the
6991 * language of the context node as specified by xml:lang attributes
6992 * is the same as or is a sublanguage of the language specified by
6993 * the argument string. The language of the context node is determined
6994 * by the value of the xml:lang attribute on the context node, or, if
6995 * the context node has no xml:lang attribute, by the value of the
6996 * xml:lang attribute on the nearest ancestor of the context node that
6997 * has an xml:lang attribute. If there is no such attribute, then lang
6998 * returns false. If there is such an attribute, then lang returns
6999 * true if the attribute value is equal to the argument ignoring case,
7000 * or if there is some suffix starting with - such that the attribute
7001 * value is equal to the argument ignoring that suffix of the attribute
7002 * value and ignoring case.
7003 */
7004void
7005xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7006 xmlXPathObjectPtr val;
7007 const xmlChar *theLang;
7008 const xmlChar *lang;
7009 int ret = 0;
7010 int i;
7011
7012 CHECK_ARITY(1);
7013 CAST_TO_STRING;
7014 CHECK_TYPE(XPATH_STRING);
7015 val = valuePop(ctxt);
7016 lang = val->stringval;
7017 theLang = xmlNodeGetLang(ctxt->context->node);
7018 if ((theLang != NULL) && (lang != NULL)) {
7019 for (i = 0;lang[i] != 0;i++)
7020 if (toupper(lang[i]) != toupper(theLang[i]))
7021 goto not_equal;
7022 ret = 1;
7023 }
7024not_equal:
7025 xmlXPathFreeObject(val);
7026 valuePush(ctxt, xmlXPathNewBoolean(ret));
7027}
7028
7029/**
7030 * xmlXPathNumberFunction:
7031 * @ctxt: the XPath Parser context
7032 * @nargs: the number of arguments
7033 *
7034 * Implement the number() XPath function
7035 * number number(object?)
7036 */
7037void
7038xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7039 xmlXPathObjectPtr cur;
7040 double res;
7041
7042 if (nargs == 0) {
7043 if (ctxt->context->node == NULL) {
7044 valuePush(ctxt, xmlXPathNewFloat(0.0));
7045 } else {
7046 xmlChar* content = xmlNodeGetContent(ctxt->context->node);
7047
7048 res = xmlXPathStringEvalNumber(content);
7049 valuePush(ctxt, xmlXPathNewFloat(res));
7050 xmlFree(content);
7051 }
7052 return;
7053 }
7054
7055 CHECK_ARITY(1);
7056 cur = valuePop(ctxt);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007057 cur = xmlXPathConvertNumber(cur);
7058 valuePush(ctxt, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00007059}
7060
7061/**
7062 * xmlXPathSumFunction:
7063 * @ctxt: the XPath Parser context
7064 * @nargs: the number of arguments
7065 *
7066 * Implement the sum() XPath function
7067 * number sum(node-set)
7068 * The sum function returns the sum of the values of the nodes in
7069 * the argument node-set.
7070 */
7071void
7072xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7073 xmlXPathObjectPtr cur;
7074 int i;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00007075 double res = 0.0;
Owen Taylor3473f882001-02-23 17:55:21 +00007076
7077 CHECK_ARITY(1);
7078 if ((ctxt->value == NULL) ||
7079 ((ctxt->value->type != XPATH_NODESET) &&
7080 (ctxt->value->type != XPATH_XSLT_TREE)))
7081 XP_ERROR(XPATH_INVALID_TYPE);
7082 cur = valuePop(ctxt);
7083
William M. Brack08171912003-12-29 02:52:11 +00007084 if ((cur->nodesetval != NULL) && (cur->nodesetval->nodeNr != 0)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00007085 for (i = 0; i < cur->nodesetval->nodeNr; i++) {
7086 res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00007087 }
7088 }
William M. Brack08171912003-12-29 02:52:11 +00007089 valuePush(ctxt, xmlXPathNewFloat(res));
Owen Taylor3473f882001-02-23 17:55:21 +00007090 xmlXPathFreeObject(cur);
7091}
7092
7093/**
7094 * xmlXPathFloorFunction:
7095 * @ctxt: the XPath Parser context
7096 * @nargs: the number of arguments
7097 *
7098 * Implement the floor() XPath function
7099 * number floor(number)
7100 * The floor function returns the largest (closest to positive infinity)
7101 * number that is not greater than the argument and that is an integer.
7102 */
7103void
7104xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007105 double f;
7106
Owen Taylor3473f882001-02-23 17:55:21 +00007107 CHECK_ARITY(1);
7108 CAST_TO_NUMBER;
7109 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007110
7111 f = (double)((int) ctxt->value->floatval);
7112 if (f != ctxt->value->floatval) {
7113 if (ctxt->value->floatval > 0)
7114 ctxt->value->floatval = f;
7115 else
7116 ctxt->value->floatval = f - 1;
7117 }
Owen Taylor3473f882001-02-23 17:55:21 +00007118}
7119
7120/**
7121 * xmlXPathCeilingFunction:
7122 * @ctxt: the XPath Parser context
7123 * @nargs: the number of arguments
7124 *
7125 * Implement the ceiling() XPath function
7126 * number ceiling(number)
7127 * The ceiling function returns the smallest (closest to negative infinity)
7128 * number that is not less than the argument and that is an integer.
7129 */
7130void
7131xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7132 double f;
7133
7134 CHECK_ARITY(1);
7135 CAST_TO_NUMBER;
7136 CHECK_TYPE(XPATH_NUMBER);
7137
7138#if 0
7139 ctxt->value->floatval = ceil(ctxt->value->floatval);
7140#else
7141 f = (double)((int) ctxt->value->floatval);
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007142 if (f != ctxt->value->floatval) {
7143 if (ctxt->value->floatval > 0)
7144 ctxt->value->floatval = f + 1;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00007145 else {
7146 if (ctxt->value->floatval < 0 && f == 0)
7147 ctxt->value->floatval = xmlXPathNZERO;
7148 else
7149 ctxt->value->floatval = f;
7150 }
7151
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007152 }
Owen Taylor3473f882001-02-23 17:55:21 +00007153#endif
7154}
7155
7156/**
7157 * xmlXPathRoundFunction:
7158 * @ctxt: the XPath Parser context
7159 * @nargs: the number of arguments
7160 *
7161 * Implement the round() XPath function
7162 * number round(number)
7163 * The round function returns the number that is closest to the
7164 * argument and that is an integer. If there are two such numbers,
7165 * then the one that is even is returned.
7166 */
7167void
7168xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7169 double f;
7170
7171 CHECK_ARITY(1);
7172 CAST_TO_NUMBER;
7173 CHECK_TYPE(XPATH_NUMBER);
7174
Daniel Veillardcda96922001-08-21 10:56:31 +00007175 if ((xmlXPathIsNaN(ctxt->value->floatval)) ||
7176 (xmlXPathIsInf(ctxt->value->floatval) == 1) ||
7177 (xmlXPathIsInf(ctxt->value->floatval) == -1) ||
Owen Taylor3473f882001-02-23 17:55:21 +00007178 (ctxt->value->floatval == 0.0))
7179 return;
7180
Owen Taylor3473f882001-02-23 17:55:21 +00007181 f = (double)((int) ctxt->value->floatval);
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007182 if (ctxt->value->floatval < 0) {
7183 if (ctxt->value->floatval < f - 0.5)
7184 ctxt->value->floatval = f - 1;
7185 else
7186 ctxt->value->floatval = f;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00007187 if (ctxt->value->floatval == 0)
7188 ctxt->value->floatval = xmlXPathNZERO;
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007189 } else {
7190 if (ctxt->value->floatval < f + 0.5)
7191 ctxt->value->floatval = f;
7192 else
7193 ctxt->value->floatval = f + 1;
7194 }
Owen Taylor3473f882001-02-23 17:55:21 +00007195}
7196
7197/************************************************************************
7198 * *
7199 * The Parser *
7200 * *
7201 ************************************************************************/
7202
7203/*
William M. Brack08171912003-12-29 02:52:11 +00007204 * a few forward declarations since we use a recursive call based
Owen Taylor3473f882001-02-23 17:55:21 +00007205 * implementation.
7206 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007207static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00007208static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007209static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007210static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt);
Daniel Veillard2156a562001-04-28 12:24:34 +00007211static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt,
7212 int qualified);
Owen Taylor3473f882001-02-23 17:55:21 +00007213
7214/**
Daniel Veillard61d80a22001-04-27 17:13:01 +00007215 * xmlXPathCurrentChar:
7216 * @ctxt: the XPath parser context
7217 * @cur: pointer to the beginning of the char
7218 * @len: pointer to the length of the char read
7219 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00007220 * The current char value, if using UTF-8 this may actually span multiple
Daniel Veillard61d80a22001-04-27 17:13:01 +00007221 * bytes in the input buffer.
7222 *
Daniel Veillard60087f32001-10-10 09:45:09 +00007223 * Returns the current char value and its length
Daniel Veillard61d80a22001-04-27 17:13:01 +00007224 */
7225
7226static int
7227xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) {
7228 unsigned char c;
7229 unsigned int val;
7230 const xmlChar *cur;
7231
7232 if (ctxt == NULL)
7233 return(0);
7234 cur = ctxt->cur;
7235
7236 /*
7237 * We are supposed to handle UTF8, check it's valid
7238 * From rfc2044: encoding of the Unicode values on UTF-8:
7239 *
7240 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
7241 * 0000 0000-0000 007F 0xxxxxxx
7242 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
7243 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
7244 *
7245 * Check for the 0x110000 limit too
7246 */
7247 c = *cur;
7248 if (c & 0x80) {
7249 if ((cur[1] & 0xc0) != 0x80)
7250 goto encoding_error;
7251 if ((c & 0xe0) == 0xe0) {
7252
7253 if ((cur[2] & 0xc0) != 0x80)
7254 goto encoding_error;
7255 if ((c & 0xf0) == 0xf0) {
7256 if (((c & 0xf8) != 0xf0) ||
7257 ((cur[3] & 0xc0) != 0x80))
7258 goto encoding_error;
7259 /* 4-byte code */
7260 *len = 4;
7261 val = (cur[0] & 0x7) << 18;
7262 val |= (cur[1] & 0x3f) << 12;
7263 val |= (cur[2] & 0x3f) << 6;
7264 val |= cur[3] & 0x3f;
7265 } else {
7266 /* 3-byte code */
7267 *len = 3;
7268 val = (cur[0] & 0xf) << 12;
7269 val |= (cur[1] & 0x3f) << 6;
7270 val |= cur[2] & 0x3f;
7271 }
7272 } else {
7273 /* 2-byte code */
7274 *len = 2;
7275 val = (cur[0] & 0x1f) << 6;
7276 val |= cur[1] & 0x3f;
7277 }
7278 if (!IS_CHAR(val)) {
7279 XP_ERROR0(XPATH_INVALID_CHAR_ERROR);
7280 }
7281 return(val);
7282 } else {
7283 /* 1-byte code */
7284 *len = 1;
7285 return((int) *cur);
7286 }
7287encoding_error:
7288 /*
William M. Brack08171912003-12-29 02:52:11 +00007289 * If we detect an UTF8 error that probably means that the
7290 * input encoding didn't get properly advertised in the
Daniel Veillard61d80a22001-04-27 17:13:01 +00007291 * declaration header. Report the error and switch the encoding
7292 * to ISO-Latin-1 (if you don't like this policy, just declare the
7293 * encoding !)
7294 */
Daniel Veillard42596ad2001-05-22 16:57:14 +00007295 *len = 0;
Daniel Veillard61d80a22001-04-27 17:13:01 +00007296 XP_ERROR0(XPATH_ENCODING_ERROR);
Daniel Veillard61d80a22001-04-27 17:13:01 +00007297}
7298
7299/**
Owen Taylor3473f882001-02-23 17:55:21 +00007300 * xmlXPathParseNCName:
7301 * @ctxt: the XPath Parser context
7302 *
7303 * parse an XML namespace non qualified name.
7304 *
7305 * [NS 3] NCName ::= (Letter | '_') (NCNameChar)*
7306 *
7307 * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
7308 * CombiningChar | Extender
7309 *
7310 * Returns the namespace name or NULL
7311 */
7312
7313xmlChar *
7314xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) {
Daniel Veillard2156a562001-04-28 12:24:34 +00007315 const xmlChar *in;
7316 xmlChar *ret;
7317 int count = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007318
Daniel Veillard2156a562001-04-28 12:24:34 +00007319 /*
7320 * Accelerator for simple ASCII names
7321 */
7322 in = ctxt->cur;
7323 if (((*in >= 0x61) && (*in <= 0x7A)) ||
7324 ((*in >= 0x41) && (*in <= 0x5A)) ||
7325 (*in == '_')) {
7326 in++;
7327 while (((*in >= 0x61) && (*in <= 0x7A)) ||
7328 ((*in >= 0x41) && (*in <= 0x5A)) ||
7329 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard9a89a8a2001-06-27 11:13:35 +00007330 (*in == '_') || (*in == '.') ||
7331 (*in == '-'))
Daniel Veillard2156a562001-04-28 12:24:34 +00007332 in++;
7333 if ((*in == ' ') || (*in == '>') || (*in == '/') ||
7334 (*in == '[') || (*in == ']') || (*in == ':') ||
7335 (*in == '@') || (*in == '*')) {
7336 count = in - ctxt->cur;
7337 if (count == 0)
7338 return(NULL);
7339 ret = xmlStrndup(ctxt->cur, count);
7340 ctxt->cur = in;
7341 return(ret);
7342 }
7343 }
7344 return(xmlXPathParseNameComplex(ctxt, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00007345}
7346
Daniel Veillard2156a562001-04-28 12:24:34 +00007347
Owen Taylor3473f882001-02-23 17:55:21 +00007348/**
7349 * xmlXPathParseQName:
7350 * @ctxt: the XPath Parser context
7351 * @prefix: a xmlChar **
7352 *
7353 * parse an XML qualified name
7354 *
7355 * [NS 5] QName ::= (Prefix ':')? LocalPart
7356 *
7357 * [NS 6] Prefix ::= NCName
7358 *
7359 * [NS 7] LocalPart ::= NCName
7360 *
7361 * Returns the function returns the local part, and prefix is updated
7362 * to get the Prefix if any.
7363 */
7364
Daniel Veillard56a4cb82001-03-24 17:00:36 +00007365static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00007366xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) {
7367 xmlChar *ret = NULL;
7368
7369 *prefix = NULL;
7370 ret = xmlXPathParseNCName(ctxt);
7371 if (CUR == ':') {
7372 *prefix = ret;
7373 NEXT;
7374 ret = xmlXPathParseNCName(ctxt);
7375 }
7376 return(ret);
7377}
7378
7379/**
7380 * xmlXPathParseName:
7381 * @ctxt: the XPath Parser context
7382 *
7383 * parse an XML name
7384 *
7385 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
7386 * CombiningChar | Extender
7387 *
7388 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
7389 *
7390 * Returns the namespace name or NULL
7391 */
7392
7393xmlChar *
7394xmlXPathParseName(xmlXPathParserContextPtr ctxt) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00007395 const xmlChar *in;
7396 xmlChar *ret;
7397 int count = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007398
Daniel Veillard61d80a22001-04-27 17:13:01 +00007399 /*
7400 * Accelerator for simple ASCII names
7401 */
7402 in = ctxt->cur;
7403 if (((*in >= 0x61) && (*in <= 0x7A)) ||
7404 ((*in >= 0x41) && (*in <= 0x5A)) ||
7405 (*in == '_') || (*in == ':')) {
7406 in++;
7407 while (((*in >= 0x61) && (*in <= 0x7A)) ||
7408 ((*in >= 0x41) && (*in <= 0x5A)) ||
7409 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard76d66f42001-05-16 21:05:17 +00007410 (*in == '_') || (*in == '-') ||
7411 (*in == ':') || (*in == '.'))
Daniel Veillard61d80a22001-04-27 17:13:01 +00007412 in++;
Daniel Veillard76d66f42001-05-16 21:05:17 +00007413 if ((*in > 0) && (*in < 0x80)) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00007414 count = in - ctxt->cur;
7415 ret = xmlStrndup(ctxt->cur, count);
7416 ctxt->cur = in;
7417 return(ret);
7418 }
7419 }
Daniel Veillard2156a562001-04-28 12:24:34 +00007420 return(xmlXPathParseNameComplex(ctxt, 1));
Owen Taylor3473f882001-02-23 17:55:21 +00007421}
7422
Daniel Veillard61d80a22001-04-27 17:13:01 +00007423static xmlChar *
Daniel Veillard2156a562001-04-28 12:24:34 +00007424xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00007425 xmlChar buf[XML_MAX_NAMELEN + 5];
7426 int len = 0, l;
7427 int c;
7428
7429 /*
7430 * Handler for more complex cases
7431 */
7432 c = CUR_CHAR(l);
7433 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
Daniel Veillard2156a562001-04-28 12:24:34 +00007434 (c == '[') || (c == ']') || (c == '@') || /* accelerators */
7435 (c == '*') || /* accelerators */
Daniel Veillard61d80a22001-04-27 17:13:01 +00007436 (!IS_LETTER(c) && (c != '_') &&
Daniel Veillard2156a562001-04-28 12:24:34 +00007437 ((qualified) && (c != ':')))) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00007438 return(NULL);
7439 }
7440
7441 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
7442 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
7443 (c == '.') || (c == '-') ||
Daniel Veillard2156a562001-04-28 12:24:34 +00007444 (c == '_') || ((qualified) && (c == ':')) ||
Daniel Veillard61d80a22001-04-27 17:13:01 +00007445 (IS_COMBINING(c)) ||
7446 (IS_EXTENDER(c)))) {
7447 COPY_BUF(l,buf,len,c);
7448 NEXTL(l);
7449 c = CUR_CHAR(l);
7450 if (len >= XML_MAX_NAMELEN) {
7451 /*
7452 * Okay someone managed to make a huge name, so he's ready to pay
7453 * for the processing speed.
7454 */
7455 xmlChar *buffer;
7456 int max = len * 2;
7457
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007458 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Daniel Veillard61d80a22001-04-27 17:13:01 +00007459 if (buffer == NULL) {
7460 XP_ERROR0(XPATH_MEMORY_ERROR);
7461 }
7462 memcpy(buffer, buf, len);
7463 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */
7464 (c == '.') || (c == '-') ||
Daniel Veillard2156a562001-04-28 12:24:34 +00007465 (c == '_') || ((qualified) && (c == ':')) ||
Daniel Veillard61d80a22001-04-27 17:13:01 +00007466 (IS_COMBINING(c)) ||
7467 (IS_EXTENDER(c))) {
7468 if (len + 10 > max) {
7469 max *= 2;
7470 buffer = (xmlChar *) xmlRealloc(buffer,
7471 max * sizeof(xmlChar));
Daniel Veillard61d80a22001-04-27 17:13:01 +00007472 if (buffer == NULL) {
7473 XP_ERROR0(XPATH_MEMORY_ERROR);
7474 }
7475 }
7476 COPY_BUF(l,buffer,len,c);
7477 NEXTL(l);
7478 c = CUR_CHAR(l);
7479 }
7480 buffer[len] = 0;
7481 return(buffer);
7482 }
7483 }
Daniel Veillard2156a562001-04-28 12:24:34 +00007484 if (len == 0)
7485 return(NULL);
Daniel Veillard61d80a22001-04-27 17:13:01 +00007486 return(xmlStrndup(buf, len));
7487}
Daniel Veillard3cd72402002-05-13 10:33:30 +00007488
7489#define MAX_FRAC 20
7490
William M. Brack372a4452004-02-17 13:09:23 +00007491/*
7492 * These are used as divisors for the fractional part of a number.
7493 * Since the table includes 1.0 (representing '0' fractional digits),
7494 * it must be dimensioned at MAX_FRAC+1 (bug 133921)
7495 */
7496static double my_pow10[MAX_FRAC+1] = {
Daniel Veillard3cd72402002-05-13 10:33:30 +00007497 1.0, 10.0, 100.0, 1000.0, 10000.0,
7498 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0,
7499 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0,
7500 100000000000000.0,
7501 1000000000000000.0, 10000000000000000.0, 100000000000000000.0,
William M. Brack372a4452004-02-17 13:09:23 +00007502 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0
Daniel Veillard3cd72402002-05-13 10:33:30 +00007503};
7504
Owen Taylor3473f882001-02-23 17:55:21 +00007505/**
7506 * xmlXPathStringEvalNumber:
7507 * @str: A string to scan
7508 *
Bjorn Reese70a9da52001-04-21 16:57:29 +00007509 * [30a] Float ::= Number ('e' Digits?)?
7510 *
Owen Taylor3473f882001-02-23 17:55:21 +00007511 * [30] Number ::= Digits ('.' Digits?)?
7512 * | '.' Digits
7513 * [31] Digits ::= [0-9]+
7514 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007515 * Compile a Number in the string
Owen Taylor3473f882001-02-23 17:55:21 +00007516 * In complement of the Number expression, this function also handles
7517 * negative values : '-' Number.
7518 *
7519 * Returns the double value.
7520 */
7521double
7522xmlXPathStringEvalNumber(const xmlChar *str) {
7523 const xmlChar *cur = str;
Daniel Veillard7b416132002-03-07 08:36:03 +00007524 double ret;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007525 int ok = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007526 int isneg = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007527 int exponent = 0;
7528 int is_exponent_negative = 0;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007529#ifdef __GNUC__
7530 unsigned long tmp = 0;
Daniel Veillard7b416132002-03-07 08:36:03 +00007531 double temp;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007532#endif
Daniel Veillardeca82812002-04-24 11:42:02 +00007533 if (cur == NULL) return(0);
William M. Brack76e95df2003-10-18 16:20:14 +00007534 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00007535 if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) {
7536 return(xmlXPathNAN);
7537 }
7538 if (*cur == '-') {
7539 isneg = 1;
7540 cur++;
7541 }
Daniel Veillardb06c6142001-08-27 14:26:30 +00007542
7543#ifdef __GNUC__
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007544 /*
Daniel Veillard7b416132002-03-07 08:36:03 +00007545 * tmp/temp is a workaround against a gcc compiler bug
7546 * http://veillard.com/gcc.bug
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007547 */
Daniel Veillard7b416132002-03-07 08:36:03 +00007548 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007549 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillard7b416132002-03-07 08:36:03 +00007550 ret = ret * 10;
7551 tmp = (*cur - '0');
Owen Taylor3473f882001-02-23 17:55:21 +00007552 ok = 1;
7553 cur++;
Daniel Veillard7b416132002-03-07 08:36:03 +00007554 temp = (double) tmp;
7555 ret = ret + temp;
Owen Taylor3473f882001-02-23 17:55:21 +00007556 }
Daniel Veillardb06c6142001-08-27 14:26:30 +00007557#else
Daniel Veillard7b416132002-03-07 08:36:03 +00007558 ret = 0;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007559 while ((*cur >= '0') && (*cur <= '9')) {
7560 ret = ret * 10 + (*cur - '0');
7561 ok = 1;
7562 cur++;
7563 }
7564#endif
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007565
Owen Taylor3473f882001-02-23 17:55:21 +00007566 if (*cur == '.') {
Daniel Veillard3cd72402002-05-13 10:33:30 +00007567 int v, frac = 0;
7568 double fraction = 0;
7569
Owen Taylor3473f882001-02-23 17:55:21 +00007570 cur++;
7571 if (((*cur < '0') || (*cur > '9')) && (!ok)) {
7572 return(xmlXPathNAN);
7573 }
Daniel Veillard3cd72402002-05-13 10:33:30 +00007574 while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) {
7575 v = (*cur - '0');
7576 fraction = fraction * 10 + v;
7577 frac = frac + 1;
Owen Taylor3473f882001-02-23 17:55:21 +00007578 cur++;
7579 }
Daniel Veillard3cd72402002-05-13 10:33:30 +00007580 fraction /= my_pow10[frac];
7581 ret = ret + fraction;
7582 while ((*cur >= '0') && (*cur <= '9'))
7583 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00007584 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00007585 if ((*cur == 'e') || (*cur == 'E')) {
7586 cur++;
7587 if (*cur == '-') {
7588 is_exponent_negative = 1;
7589 cur++;
7590 }
7591 while ((*cur >= '0') && (*cur <= '9')) {
7592 exponent = exponent * 10 + (*cur - '0');
7593 cur++;
7594 }
7595 }
William M. Brack76e95df2003-10-18 16:20:14 +00007596 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00007597 if (*cur != 0) return(xmlXPathNAN);
7598 if (isneg) ret = -ret;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007599 if (is_exponent_negative) exponent = -exponent;
7600 ret *= pow(10.0, (double)exponent);
Owen Taylor3473f882001-02-23 17:55:21 +00007601 return(ret);
7602}
7603
7604/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007605 * xmlXPathCompNumber:
Owen Taylor3473f882001-02-23 17:55:21 +00007606 * @ctxt: the XPath Parser context
7607 *
7608 * [30] Number ::= Digits ('.' Digits?)?
7609 * | '.' Digits
7610 * [31] Digits ::= [0-9]+
7611 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007612 * Compile a Number, then push it on the stack
Owen Taylor3473f882001-02-23 17:55:21 +00007613 *
7614 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007615static void
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007616xmlXPathCompNumber(xmlXPathParserContextPtr ctxt)
7617{
Owen Taylor3473f882001-02-23 17:55:21 +00007618 double ret = 0.0;
7619 double mult = 1;
Daniel Veillard7b416132002-03-07 08:36:03 +00007620 int ok = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007621 int exponent = 0;
7622 int is_exponent_negative = 0;
Daniel Veillard7b416132002-03-07 08:36:03 +00007623#ifdef __GNUC__
7624 unsigned long tmp = 0;
7625 double temp;
7626#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007627
7628 CHECK_ERROR;
7629 if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) {
7630 XP_ERROR(XPATH_NUMBER_ERROR);
7631 }
Daniel Veillard7b416132002-03-07 08:36:03 +00007632#ifdef __GNUC__
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007633 /*
Daniel Veillard7b416132002-03-07 08:36:03 +00007634 * tmp/temp is a workaround against a gcc compiler bug
7635 * http://veillard.com/gcc.bug
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007636 */
Daniel Veillard7b416132002-03-07 08:36:03 +00007637 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007638 while ((CUR >= '0') && (CUR <= '9')) {
Daniel Veillard7b416132002-03-07 08:36:03 +00007639 ret = ret * 10;
7640 tmp = (CUR - '0');
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007641 ok = 1;
7642 NEXT;
Daniel Veillard7b416132002-03-07 08:36:03 +00007643 temp = (double) tmp;
7644 ret = ret + temp;
Owen Taylor3473f882001-02-23 17:55:21 +00007645 }
Daniel Veillard7b416132002-03-07 08:36:03 +00007646#else
7647 ret = 0;
7648 while ((CUR >= '0') && (CUR <= '9')) {
7649 ret = ret * 10 + (CUR - '0');
7650 ok = 1;
7651 NEXT;
7652 }
7653#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007654 if (CUR == '.') {
7655 NEXT;
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007656 if (((CUR < '0') || (CUR > '9')) && (!ok)) {
7657 XP_ERROR(XPATH_NUMBER_ERROR);
7658 }
7659 while ((CUR >= '0') && (CUR <= '9')) {
7660 mult /= 10;
7661 ret = ret + (CUR - '0') * mult;
7662 NEXT;
7663 }
Owen Taylor3473f882001-02-23 17:55:21 +00007664 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00007665 if ((CUR == 'e') || (CUR == 'E')) {
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007666 NEXT;
7667 if (CUR == '-') {
7668 is_exponent_negative = 1;
7669 NEXT;
7670 }
7671 while ((CUR >= '0') && (CUR <= '9')) {
7672 exponent = exponent * 10 + (CUR - '0');
7673 NEXT;
7674 }
7675 if (is_exponent_negative)
7676 exponent = -exponent;
7677 ret *= pow(10.0, (double) exponent);
Bjorn Reese70a9da52001-04-21 16:57:29 +00007678 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007679 PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0,
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007680 xmlXPathNewFloat(ret), NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007681}
7682
7683/**
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007684 * xmlXPathParseLiteral:
7685 * @ctxt: the XPath Parser context
7686 *
7687 * Parse a Literal
7688 *
7689 * [29] Literal ::= '"' [^"]* '"'
7690 * | "'" [^']* "'"
7691 *
7692 * Returns the value found or NULL in case of error
7693 */
7694static xmlChar *
7695xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) {
7696 const xmlChar *q;
7697 xmlChar *ret = NULL;
7698
7699 if (CUR == '"') {
7700 NEXT;
7701 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00007702 while ((IS_CHAR_CH(CUR)) && (CUR != '"'))
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007703 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00007704 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007705 XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR);
7706 } else {
7707 ret = xmlStrndup(q, CUR_PTR - q);
7708 NEXT;
7709 }
7710 } else if (CUR == '\'') {
7711 NEXT;
7712 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00007713 while ((IS_CHAR_CH(CUR)) && (CUR != '\''))
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007714 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00007715 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007716 XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR);
7717 } else {
7718 ret = xmlStrndup(q, CUR_PTR - q);
7719 NEXT;
7720 }
7721 } else {
7722 XP_ERROR0(XPATH_START_LITERAL_ERROR);
7723 }
7724 return(ret);
7725}
7726
7727/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007728 * xmlXPathCompLiteral:
Owen Taylor3473f882001-02-23 17:55:21 +00007729 * @ctxt: the XPath Parser context
7730 *
7731 * Parse a Literal and push it on the stack.
7732 *
7733 * [29] Literal ::= '"' [^"]* '"'
7734 * | "'" [^']* "'"
7735 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007736 * TODO: xmlXPathCompLiteral memory allocation could be improved.
Owen Taylor3473f882001-02-23 17:55:21 +00007737 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007738static void
7739xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007740 const xmlChar *q;
7741 xmlChar *ret = NULL;
7742
7743 if (CUR == '"') {
7744 NEXT;
7745 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00007746 while ((IS_CHAR_CH(CUR)) && (CUR != '"'))
Owen Taylor3473f882001-02-23 17:55:21 +00007747 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00007748 if (!IS_CHAR_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007749 XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR);
7750 } else {
7751 ret = xmlStrndup(q, CUR_PTR - q);
7752 NEXT;
7753 }
7754 } else if (CUR == '\'') {
7755 NEXT;
7756 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00007757 while ((IS_CHAR_CH(CUR)) && (CUR != '\''))
Owen Taylor3473f882001-02-23 17:55:21 +00007758 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00007759 if (!IS_CHAR_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007760 XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR);
7761 } else {
7762 ret = xmlStrndup(q, CUR_PTR - q);
7763 NEXT;
7764 }
7765 } else {
7766 XP_ERROR(XPATH_START_LITERAL_ERROR);
7767 }
7768 if (ret == NULL) return;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007769 PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0,
7770 xmlXPathNewString(ret), NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007771 xmlFree(ret);
7772}
7773
7774/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007775 * xmlXPathCompVariableReference:
Owen Taylor3473f882001-02-23 17:55:21 +00007776 * @ctxt: the XPath Parser context
7777 *
7778 * Parse a VariableReference, evaluate it and push it on the stack.
7779 *
7780 * The variable bindings consist of a mapping from variable names
William M. Brack08171912003-12-29 02:52:11 +00007781 * to variable values. The value of a variable is an object, which can be
Owen Taylor3473f882001-02-23 17:55:21 +00007782 * of any of the types that are possible for the value of an expression,
7783 * and may also be of additional types not specified here.
7784 *
7785 * Early evaluation is possible since:
7786 * The variable bindings [...] used to evaluate a subexpression are
7787 * always the same as those used to evaluate the containing expression.
7788 *
7789 * [36] VariableReference ::= '$' QName
7790 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007791static void
7792xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007793 xmlChar *name;
7794 xmlChar *prefix;
Owen Taylor3473f882001-02-23 17:55:21 +00007795
7796 SKIP_BLANKS;
7797 if (CUR != '$') {
7798 XP_ERROR(XPATH_VARIABLE_REF_ERROR);
7799 }
7800 NEXT;
7801 name = xmlXPathParseQName(ctxt, &prefix);
7802 if (name == NULL) {
7803 XP_ERROR(XPATH_VARIABLE_REF_ERROR);
7804 }
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007805 ctxt->comp->last = -1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007806 PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0,
7807 name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00007808 SKIP_BLANKS;
7809}
7810
7811/**
7812 * xmlXPathIsNodeType:
Owen Taylor3473f882001-02-23 17:55:21 +00007813 * @name: a name string
7814 *
7815 * Is the name given a NodeType one.
7816 *
7817 * [38] NodeType ::= 'comment'
7818 * | 'text'
7819 * | 'processing-instruction'
7820 * | 'node'
7821 *
7822 * Returns 1 if true 0 otherwise
7823 */
7824int
7825xmlXPathIsNodeType(const xmlChar *name) {
7826 if (name == NULL)
7827 return(0);
7828
Daniel Veillard1971ee22002-01-31 20:29:19 +00007829 if (xmlStrEqual(name, BAD_CAST "node"))
Owen Taylor3473f882001-02-23 17:55:21 +00007830 return(1);
7831 if (xmlStrEqual(name, BAD_CAST "text"))
7832 return(1);
Daniel Veillard1971ee22002-01-31 20:29:19 +00007833 if (xmlStrEqual(name, BAD_CAST "comment"))
Owen Taylor3473f882001-02-23 17:55:21 +00007834 return(1);
Daniel Veillard1971ee22002-01-31 20:29:19 +00007835 if (xmlStrEqual(name, BAD_CAST "processing-instruction"))
Owen Taylor3473f882001-02-23 17:55:21 +00007836 return(1);
7837 return(0);
7838}
7839
7840/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007841 * xmlXPathCompFunctionCall:
Owen Taylor3473f882001-02-23 17:55:21 +00007842 * @ctxt: the XPath Parser context
7843 *
7844 * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')'
7845 * [17] Argument ::= Expr
7846 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007847 * Compile a function call, the evaluation of all arguments are
Owen Taylor3473f882001-02-23 17:55:21 +00007848 * pushed on the stack
7849 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007850static void
7851xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007852 xmlChar *name;
7853 xmlChar *prefix;
Owen Taylor3473f882001-02-23 17:55:21 +00007854 int nbargs = 0;
7855
7856 name = xmlXPathParseQName(ctxt, &prefix);
7857 if (name == NULL) {
7858 XP_ERROR(XPATH_EXPR_ERROR);
7859 }
7860 SKIP_BLANKS;
Owen Taylor3473f882001-02-23 17:55:21 +00007861#ifdef DEBUG_EXPR
7862 if (prefix == NULL)
7863 xmlGenericError(xmlGenericErrorContext, "Calling function %s\n",
7864 name);
7865 else
7866 xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n",
7867 prefix, name);
7868#endif
7869
Owen Taylor3473f882001-02-23 17:55:21 +00007870 if (CUR != '(') {
7871 XP_ERROR(XPATH_EXPR_ERROR);
7872 }
7873 NEXT;
7874 SKIP_BLANKS;
7875
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007876 ctxt->comp->last = -1;
Daniel Veillard71f9d732003-01-14 16:07:16 +00007877 if (CUR != ')') {
7878 while (CUR != 0) {
7879 int op1 = ctxt->comp->last;
7880 ctxt->comp->last = -1;
7881 xmlXPathCompileExpr(ctxt);
7882 CHECK_ERROR;
7883 PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0);
7884 nbargs++;
7885 if (CUR == ')') break;
7886 if (CUR != ',') {
7887 XP_ERROR(XPATH_EXPR_ERROR);
7888 }
7889 NEXT;
7890 SKIP_BLANKS;
Owen Taylor3473f882001-02-23 17:55:21 +00007891 }
Owen Taylor3473f882001-02-23 17:55:21 +00007892 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007893 PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0,
7894 name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00007895 NEXT;
7896 SKIP_BLANKS;
Owen Taylor3473f882001-02-23 17:55:21 +00007897}
7898
7899/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007900 * xmlXPathCompPrimaryExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007901 * @ctxt: the XPath Parser context
7902 *
7903 * [15] PrimaryExpr ::= VariableReference
7904 * | '(' Expr ')'
7905 * | Literal
7906 * | Number
7907 * | FunctionCall
7908 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007909 * Compile a primary expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007910 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007911static void
7912xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007913 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007914 if (CUR == '$') xmlXPathCompVariableReference(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007915 else if (CUR == '(') {
7916 NEXT;
7917 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007918 xmlXPathCompileExpr(ctxt);
Aleksey Sanin50fe8b12002-05-07 16:21:36 +00007919 CHECK_ERROR;
Owen Taylor3473f882001-02-23 17:55:21 +00007920 if (CUR != ')') {
7921 XP_ERROR(XPATH_EXPR_ERROR);
7922 }
7923 NEXT;
7924 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00007925 } else if (IS_DIGIT_CH(CUR) || (CUR == '.' && IS_DIGIT_CH(NXT(1)))) {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007926 xmlXPathCompNumber(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007927 } else if ((CUR == '\'') || (CUR == '"')) {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007928 xmlXPathCompLiteral(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007929 } else {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007930 xmlXPathCompFunctionCall(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007931 }
7932 SKIP_BLANKS;
7933}
7934
7935/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007936 * xmlXPathCompFilterExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007937 * @ctxt: the XPath Parser context
7938 *
7939 * [20] FilterExpr ::= PrimaryExpr
7940 * | FilterExpr Predicate
7941 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007942 * Compile a filter expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007943 * Square brackets are used to filter expressions in the same way that
7944 * they are used in location paths. It is an error if the expression to
7945 * be filtered does not evaluate to a node-set. The context node list
7946 * used for evaluating the expression in square brackets is the node-set
7947 * to be filtered listed in document order.
7948 */
7949
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007950static void
7951xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) {
7952 xmlXPathCompPrimaryExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007953 CHECK_ERROR;
7954 SKIP_BLANKS;
7955
7956 while (CUR == '[') {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00007957 xmlXPathCompPredicate(ctxt, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00007958 SKIP_BLANKS;
7959 }
7960
7961
7962}
7963
7964/**
7965 * xmlXPathScanName:
7966 * @ctxt: the XPath Parser context
7967 *
7968 * Trickery: parse an XML name but without consuming the input flow
7969 * Needed to avoid insanity in the parser state.
7970 *
7971 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
7972 * CombiningChar | Extender
7973 *
7974 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
7975 *
7976 * [6] Names ::= Name (S Name)*
7977 *
7978 * Returns the Name parsed or NULL
7979 */
7980
Daniel Veillard56a4cb82001-03-24 17:00:36 +00007981static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00007982xmlXPathScanName(xmlXPathParserContextPtr ctxt) {
7983 xmlChar buf[XML_MAX_NAMELEN];
7984 int len = 0;
7985
7986 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00007987 if (!IS_LETTER_CH(CUR) && (CUR != '_') &&
Owen Taylor3473f882001-02-23 17:55:21 +00007988 (CUR != ':')) {
7989 return(NULL);
7990 }
7991
William M. Brack76e95df2003-10-18 16:20:14 +00007992 while ((IS_LETTER_CH(NXT(len))) || (IS_DIGIT_CH(NXT(len))) ||
Owen Taylor3473f882001-02-23 17:55:21 +00007993 (NXT(len) == '.') || (NXT(len) == '-') ||
7994 (NXT(len) == '_') || (NXT(len) == ':') ||
William M. Brack76e95df2003-10-18 16:20:14 +00007995 (IS_COMBINING_CH(NXT(len))) ||
7996 (IS_EXTENDER_CH(NXT(len)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00007997 buf[len] = NXT(len);
7998 len++;
7999 if (len >= XML_MAX_NAMELEN) {
8000 xmlGenericError(xmlGenericErrorContext,
8001 "xmlScanName: reached XML_MAX_NAMELEN limit\n");
William M. Brack76e95df2003-10-18 16:20:14 +00008002 while ((IS_LETTER_CH(NXT(len))) || (IS_DIGIT_CH(NXT(len))) ||
Owen Taylor3473f882001-02-23 17:55:21 +00008003 (NXT(len) == '.') || (NXT(len) == '-') ||
8004 (NXT(len) == '_') || (NXT(len) == ':') ||
William M. Brack76e95df2003-10-18 16:20:14 +00008005 (IS_COMBINING_CH(NXT(len))) ||
8006 (IS_EXTENDER_CH(NXT(len))))
Owen Taylor3473f882001-02-23 17:55:21 +00008007 len++;
8008 break;
8009 }
8010 }
8011 return(xmlStrndup(buf, len));
8012}
8013
8014/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008015 * xmlXPathCompPathExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008016 * @ctxt: the XPath Parser context
8017 *
8018 * [19] PathExpr ::= LocationPath
8019 * | FilterExpr
8020 * | FilterExpr '/' RelativeLocationPath
8021 * | FilterExpr '//' RelativeLocationPath
8022 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008023 * Compile a path expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008024 * The / operator and // operators combine an arbitrary expression
8025 * and a relative location path. It is an error if the expression
8026 * does not evaluate to a node-set.
8027 * The / operator does composition in the same way as when / is
8028 * used in a location path. As in location paths, // is short for
8029 * /descendant-or-self::node()/.
8030 */
8031
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008032static void
8033xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00008034 int lc = 1; /* Should we branch to LocationPath ? */
8035 xmlChar *name = NULL; /* we may have to preparse a name to find out */
8036
8037 SKIP_BLANKS;
William M. Brack76e95df2003-10-18 16:20:14 +00008038 if ((CUR == '$') || (CUR == '(') || (IS_DIGIT_CH(CUR)) ||
8039 (CUR == '\'') || (CUR == '"') || (CUR == '.' && IS_DIGIT_CH(NXT(1)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008040 lc = 0;
8041 } else if (CUR == '*') {
8042 /* relative or absolute location path */
8043 lc = 1;
8044 } else if (CUR == '/') {
8045 /* relative or absolute location path */
8046 lc = 1;
8047 } else if (CUR == '@') {
8048 /* relative abbreviated attribute location path */
8049 lc = 1;
8050 } else if (CUR == '.') {
8051 /* relative abbreviated attribute location path */
8052 lc = 1;
8053 } else {
8054 /*
8055 * Problem is finding if we have a name here whether it's:
8056 * - a nodetype
8057 * - a function call in which case it's followed by '('
8058 * - an axis in which case it's followed by ':'
8059 * - a element name
8060 * We do an a priori analysis here rather than having to
8061 * maintain parsed token content through the recursive function
William M. Brack08171912003-12-29 02:52:11 +00008062 * calls. This looks uglier but makes the code easier to
Owen Taylor3473f882001-02-23 17:55:21 +00008063 * read/write/debug.
8064 */
8065 SKIP_BLANKS;
8066 name = xmlXPathScanName(ctxt);
8067 if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) {
8068#ifdef DEBUG_STEP
8069 xmlGenericError(xmlGenericErrorContext,
8070 "PathExpr: Axis\n");
8071#endif
8072 lc = 1;
8073 xmlFree(name);
8074 } else if (name != NULL) {
8075 int len =xmlStrlen(name);
Owen Taylor3473f882001-02-23 17:55:21 +00008076
8077
8078 while (NXT(len) != 0) {
8079 if (NXT(len) == '/') {
8080 /* element name */
8081#ifdef DEBUG_STEP
8082 xmlGenericError(xmlGenericErrorContext,
8083 "PathExpr: AbbrRelLocation\n");
8084#endif
8085 lc = 1;
8086 break;
William M. Brack76e95df2003-10-18 16:20:14 +00008087 } else if (IS_BLANK_CH(NXT(len))) {
William M. Brack78637da2003-07-31 14:47:38 +00008088 /* ignore blanks */
8089 ;
Owen Taylor3473f882001-02-23 17:55:21 +00008090 } else if (NXT(len) == ':') {
8091#ifdef DEBUG_STEP
8092 xmlGenericError(xmlGenericErrorContext,
8093 "PathExpr: AbbrRelLocation\n");
8094#endif
8095 lc = 1;
8096 break;
8097 } else if ((NXT(len) == '(')) {
8098 /* Note Type or Function */
8099 if (xmlXPathIsNodeType(name)) {
8100#ifdef DEBUG_STEP
8101 xmlGenericError(xmlGenericErrorContext,
8102 "PathExpr: Type search\n");
8103#endif
8104 lc = 1;
8105 } else {
8106#ifdef DEBUG_STEP
8107 xmlGenericError(xmlGenericErrorContext,
8108 "PathExpr: function call\n");
8109#endif
8110 lc = 0;
8111 }
8112 break;
8113 } else if ((NXT(len) == '[')) {
8114 /* element name */
8115#ifdef DEBUG_STEP
8116 xmlGenericError(xmlGenericErrorContext,
8117 "PathExpr: AbbrRelLocation\n");
8118#endif
8119 lc = 1;
8120 break;
8121 } else if ((NXT(len) == '<') || (NXT(len) == '>') ||
8122 (NXT(len) == '=')) {
8123 lc = 1;
8124 break;
8125 } else {
8126 lc = 1;
8127 break;
8128 }
8129 len++;
8130 }
8131 if (NXT(len) == 0) {
8132#ifdef DEBUG_STEP
8133 xmlGenericError(xmlGenericErrorContext,
8134 "PathExpr: AbbrRelLocation\n");
8135#endif
8136 /* element name */
8137 lc = 1;
8138 }
8139 xmlFree(name);
8140 } else {
William M. Brack08171912003-12-29 02:52:11 +00008141 /* make sure all cases are covered explicitly */
Owen Taylor3473f882001-02-23 17:55:21 +00008142 XP_ERROR(XPATH_EXPR_ERROR);
8143 }
8144 }
8145
8146 if (lc) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008147 if (CUR == '/') {
8148 PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0);
8149 } else {
8150 PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008151 }
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008152 xmlXPathCompLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008153 } else {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008154 xmlXPathCompFilterExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008155 CHECK_ERROR;
8156 if ((CUR == '/') && (NXT(1) == '/')) {
8157 SKIP(2);
8158 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008159
8160 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
8161 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
8162 PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0);
8163
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008164 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008165 } else if (CUR == '/') {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008166 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008167 }
8168 }
8169 SKIP_BLANKS;
8170}
8171
8172/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008173 * xmlXPathCompUnionExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008174 * @ctxt: the XPath Parser context
8175 *
8176 * [18] UnionExpr ::= PathExpr
8177 * | UnionExpr '|' PathExpr
8178 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008179 * Compile an union expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008180 */
8181
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008182static void
8183xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) {
8184 xmlXPathCompPathExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008185 CHECK_ERROR;
8186 SKIP_BLANKS;
8187 while (CUR == '|') {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008188 int op1 = ctxt->comp->last;
8189 PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008190
8191 NEXT;
8192 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008193 xmlXPathCompPathExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008194
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008195 PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0);
8196
Owen Taylor3473f882001-02-23 17:55:21 +00008197 SKIP_BLANKS;
8198 }
Owen Taylor3473f882001-02-23 17:55:21 +00008199}
8200
8201/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008202 * xmlXPathCompUnaryExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008203 * @ctxt: the XPath Parser context
8204 *
8205 * [27] UnaryExpr ::= UnionExpr
8206 * | '-' UnaryExpr
8207 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008208 * Compile an unary expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008209 */
8210
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008211static void
8212xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00008213 int minus = 0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008214 int found = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008215
8216 SKIP_BLANKS;
Daniel Veillard68d7b672001-03-12 18:22:04 +00008217 while (CUR == '-') {
8218 minus = 1 - minus;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008219 found = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00008220 NEXT;
8221 SKIP_BLANKS;
8222 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008223
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008224 xmlXPathCompUnionExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008225 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008226 if (found) {
8227 if (minus)
8228 PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0);
8229 else
8230 PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008231 }
8232}
8233
8234/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008235 * xmlXPathCompMultiplicativeExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008236 * @ctxt: the XPath Parser context
8237 *
8238 * [26] MultiplicativeExpr ::= UnaryExpr
8239 * | MultiplicativeExpr MultiplyOperator UnaryExpr
8240 * | MultiplicativeExpr 'div' UnaryExpr
8241 * | MultiplicativeExpr 'mod' UnaryExpr
8242 * [34] MultiplyOperator ::= '*'
8243 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008244 * Compile an Additive expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008245 */
8246
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008247static void
8248xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) {
8249 xmlXPathCompUnaryExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008250 CHECK_ERROR;
8251 SKIP_BLANKS;
8252 while ((CUR == '*') ||
8253 ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) ||
8254 ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) {
8255 int op = -1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008256 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008257
8258 if (CUR == '*') {
8259 op = 0;
8260 NEXT;
8261 } else if (CUR == 'd') {
8262 op = 1;
8263 SKIP(3);
8264 } else if (CUR == 'm') {
8265 op = 2;
8266 SKIP(3);
8267 }
8268 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008269 xmlXPathCompUnaryExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008270 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008271 PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008272 SKIP_BLANKS;
8273 }
8274}
8275
8276/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008277 * xmlXPathCompAdditiveExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008278 * @ctxt: the XPath Parser context
8279 *
8280 * [25] AdditiveExpr ::= MultiplicativeExpr
8281 * | AdditiveExpr '+' MultiplicativeExpr
8282 * | AdditiveExpr '-' MultiplicativeExpr
8283 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008284 * Compile an Additive expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008285 */
8286
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008287static void
8288xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008289
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008290 xmlXPathCompMultiplicativeExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008291 CHECK_ERROR;
8292 SKIP_BLANKS;
8293 while ((CUR == '+') || (CUR == '-')) {
8294 int plus;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008295 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008296
8297 if (CUR == '+') plus = 1;
8298 else plus = 0;
8299 NEXT;
8300 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008301 xmlXPathCompMultiplicativeExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008302 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008303 PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008304 SKIP_BLANKS;
8305 }
8306}
8307
8308/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008309 * xmlXPathCompRelationalExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008310 * @ctxt: the XPath Parser context
8311 *
8312 * [24] RelationalExpr ::= AdditiveExpr
8313 * | RelationalExpr '<' AdditiveExpr
8314 * | RelationalExpr '>' AdditiveExpr
8315 * | RelationalExpr '<=' AdditiveExpr
8316 * | RelationalExpr '>=' AdditiveExpr
8317 *
8318 * A <= B > C is allowed ? Answer from James, yes with
8319 * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr
8320 * which is basically what got implemented.
8321 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008322 * Compile a Relational expression, then push the result
Owen Taylor3473f882001-02-23 17:55:21 +00008323 * on the stack
8324 */
8325
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008326static void
8327xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) {
8328 xmlXPathCompAdditiveExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008329 CHECK_ERROR;
8330 SKIP_BLANKS;
8331 while ((CUR == '<') ||
8332 (CUR == '>') ||
8333 ((CUR == '<') && (NXT(1) == '=')) ||
8334 ((CUR == '>') && (NXT(1) == '='))) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008335 int inf, strict;
8336 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008337
8338 if (CUR == '<') inf = 1;
8339 else inf = 0;
8340 if (NXT(1) == '=') strict = 0;
8341 else strict = 1;
8342 NEXT;
8343 if (!strict) NEXT;
8344 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008345 xmlXPathCompAdditiveExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008346 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008347 PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict);
Owen Taylor3473f882001-02-23 17:55:21 +00008348 SKIP_BLANKS;
8349 }
8350}
8351
8352/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008353 * xmlXPathCompEqualityExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008354 * @ctxt: the XPath Parser context
8355 *
8356 * [23] EqualityExpr ::= RelationalExpr
8357 * | EqualityExpr '=' RelationalExpr
8358 * | EqualityExpr '!=' RelationalExpr
8359 *
8360 * A != B != C is allowed ? Answer from James, yes with
8361 * (RelationalExpr = RelationalExpr) = RelationalExpr
8362 * (RelationalExpr != RelationalExpr) != RelationalExpr
8363 * which is basically what got implemented.
8364 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008365 * Compile an Equality expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008366 *
8367 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008368static void
8369xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) {
8370 xmlXPathCompRelationalExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008371 CHECK_ERROR;
8372 SKIP_BLANKS;
8373 while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008374 int eq;
8375 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008376
8377 if (CUR == '=') eq = 1;
8378 else eq = 0;
8379 NEXT;
8380 if (!eq) NEXT;
8381 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008382 xmlXPathCompRelationalExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008383 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008384 PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008385 SKIP_BLANKS;
8386 }
8387}
8388
8389/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008390 * xmlXPathCompAndExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008391 * @ctxt: the XPath Parser context
8392 *
8393 * [22] AndExpr ::= EqualityExpr
8394 * | AndExpr 'and' EqualityExpr
8395 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008396 * Compile an AND expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008397 *
8398 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008399static void
8400xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) {
8401 xmlXPathCompEqualityExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008402 CHECK_ERROR;
8403 SKIP_BLANKS;
8404 while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008405 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008406 SKIP(3);
8407 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008408 xmlXPathCompEqualityExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008409 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008410 PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008411 SKIP_BLANKS;
8412 }
8413}
8414
8415/**
Daniel Veillard591b4be2003-02-09 23:33:36 +00008416 * xmlXPathCompileExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008417 * @ctxt: the XPath Parser context
8418 *
8419 * [14] Expr ::= OrExpr
8420 * [21] OrExpr ::= AndExpr
8421 * | OrExpr 'or' AndExpr
8422 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008423 * Parse and compile an expression
Owen Taylor3473f882001-02-23 17:55:21 +00008424 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008425static void
8426xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt) {
8427 xmlXPathCompAndExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008428 CHECK_ERROR;
8429 SKIP_BLANKS;
8430 while ((CUR == 'o') && (NXT(1) == 'r')) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008431 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008432 SKIP(2);
8433 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008434 xmlXPathCompAndExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008435 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008436 PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0);
8437 op1 = ctxt->comp->nbStep;
Owen Taylor3473f882001-02-23 17:55:21 +00008438 SKIP_BLANKS;
8439 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008440 if (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE) {
8441 /* more ops could be optimized too */
8442 PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0);
8443 }
Owen Taylor3473f882001-02-23 17:55:21 +00008444}
8445
8446/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008447 * xmlXPathCompPredicate:
Owen Taylor3473f882001-02-23 17:55:21 +00008448 * @ctxt: the XPath Parser context
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008449 * @filter: act as a filter
Owen Taylor3473f882001-02-23 17:55:21 +00008450 *
8451 * [8] Predicate ::= '[' PredicateExpr ']'
8452 * [9] PredicateExpr ::= Expr
8453 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008454 * Compile a predicate expression
Owen Taylor3473f882001-02-23 17:55:21 +00008455 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008456static void
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008457xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008458 int op1 = ctxt->comp->last;
8459
8460 SKIP_BLANKS;
8461 if (CUR != '[') {
8462 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
8463 }
8464 NEXT;
8465 SKIP_BLANKS;
8466
8467 ctxt->comp->last = -1;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008468 xmlXPathCompileExpr(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008469 CHECK_ERROR;
8470
8471 if (CUR != ']') {
8472 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
8473 }
8474
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008475 if (filter)
8476 PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0);
8477 else
8478 PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0);
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008479
8480 NEXT;
8481 SKIP_BLANKS;
8482}
8483
8484/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008485 * xmlXPathCompNodeTest:
Owen Taylor3473f882001-02-23 17:55:21 +00008486 * @ctxt: the XPath Parser context
8487 * @test: pointer to a xmlXPathTestVal
8488 * @type: pointer to a xmlXPathTypeVal
8489 * @prefix: placeholder for a possible name prefix
8490 *
8491 * [7] NodeTest ::= NameTest
8492 * | NodeType '(' ')'
8493 * | 'processing-instruction' '(' Literal ')'
8494 *
8495 * [37] NameTest ::= '*'
8496 * | NCName ':' '*'
8497 * | QName
8498 * [38] NodeType ::= 'comment'
8499 * | 'text'
8500 * | 'processing-instruction'
8501 * | 'node'
8502 *
William M. Brack08171912003-12-29 02:52:11 +00008503 * Returns the name found and updates @test, @type and @prefix appropriately
Owen Taylor3473f882001-02-23 17:55:21 +00008504 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008505static xmlChar *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008506xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test,
8507 xmlXPathTypeVal *type, const xmlChar **prefix,
8508 xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00008509 int blanks;
8510
8511 if ((test == NULL) || (type == NULL) || (prefix == NULL)) {
8512 STRANGE;
8513 return(NULL);
8514 }
William M. Brack78637da2003-07-31 14:47:38 +00008515 *type = (xmlXPathTypeVal) 0;
8516 *test = (xmlXPathTestVal) 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008517 *prefix = NULL;
8518 SKIP_BLANKS;
8519
8520 if ((name == NULL) && (CUR == '*')) {
8521 /*
8522 * All elements
8523 */
8524 NEXT;
8525 *test = NODE_TEST_ALL;
8526 return(NULL);
8527 }
8528
8529 if (name == NULL)
8530 name = xmlXPathParseNCName(ctxt);
8531 if (name == NULL) {
8532 XP_ERROR0(XPATH_EXPR_ERROR);
8533 }
8534
William M. Brack76e95df2003-10-18 16:20:14 +00008535 blanks = IS_BLANK_CH(CUR);
Owen Taylor3473f882001-02-23 17:55:21 +00008536 SKIP_BLANKS;
8537 if (CUR == '(') {
8538 NEXT;
8539 /*
8540 * NodeType or PI search
8541 */
8542 if (xmlStrEqual(name, BAD_CAST "comment"))
8543 *type = NODE_TYPE_COMMENT;
8544 else if (xmlStrEqual(name, BAD_CAST "node"))
8545 *type = NODE_TYPE_NODE;
8546 else if (xmlStrEqual(name, BAD_CAST "processing-instruction"))
8547 *type = NODE_TYPE_PI;
8548 else if (xmlStrEqual(name, BAD_CAST "text"))
8549 *type = NODE_TYPE_TEXT;
8550 else {
8551 if (name != NULL)
8552 xmlFree(name);
8553 XP_ERROR0(XPATH_EXPR_ERROR);
8554 }
8555
8556 *test = NODE_TEST_TYPE;
8557
8558 SKIP_BLANKS;
8559 if (*type == NODE_TYPE_PI) {
8560 /*
8561 * Specific case: search a PI by name.
8562 */
Owen Taylor3473f882001-02-23 17:55:21 +00008563 if (name != NULL)
8564 xmlFree(name);
Daniel Veillard82e49712001-04-26 14:38:03 +00008565 name = NULL;
8566 if (CUR != ')') {
8567 name = xmlXPathParseLiteral(ctxt);
8568 CHECK_ERROR 0;
Daniel Veillarded23b7d2002-05-27 12:16:02 +00008569 *test = NODE_TEST_PI;
Daniel Veillard82e49712001-04-26 14:38:03 +00008570 SKIP_BLANKS;
8571 }
Owen Taylor3473f882001-02-23 17:55:21 +00008572 }
8573 if (CUR != ')') {
8574 if (name != NULL)
8575 xmlFree(name);
8576 XP_ERROR0(XPATH_UNCLOSED_ERROR);
8577 }
8578 NEXT;
8579 return(name);
8580 }
8581 *test = NODE_TEST_NAME;
8582 if ((!blanks) && (CUR == ':')) {
8583 NEXT;
8584
8585 /*
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008586 * Since currently the parser context don't have a
8587 * namespace list associated:
8588 * The namespace name for this prefix can be computed
8589 * only at evaluation time. The compilation is done
8590 * outside of any context.
Owen Taylor3473f882001-02-23 17:55:21 +00008591 */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008592#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00008593 *prefix = xmlXPathNsLookup(ctxt->context, name);
8594 if (name != NULL)
8595 xmlFree(name);
8596 if (*prefix == NULL) {
8597 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
8598 }
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008599#else
8600 *prefix = name;
8601#endif
Owen Taylor3473f882001-02-23 17:55:21 +00008602
8603 if (CUR == '*') {
8604 /*
8605 * All elements
8606 */
8607 NEXT;
8608 *test = NODE_TEST_ALL;
8609 return(NULL);
8610 }
8611
8612 name = xmlXPathParseNCName(ctxt);
8613 if (name == NULL) {
8614 XP_ERROR0(XPATH_EXPR_ERROR);
8615 }
8616 }
8617 return(name);
8618}
8619
8620/**
8621 * xmlXPathIsAxisName:
8622 * @name: a preparsed name token
8623 *
8624 * [6] AxisName ::= 'ancestor'
8625 * | 'ancestor-or-self'
8626 * | 'attribute'
8627 * | 'child'
8628 * | 'descendant'
8629 * | 'descendant-or-self'
8630 * | 'following'
8631 * | 'following-sibling'
8632 * | 'namespace'
8633 * | 'parent'
8634 * | 'preceding'
8635 * | 'preceding-sibling'
8636 * | 'self'
8637 *
8638 * Returns the axis or 0
8639 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008640static xmlXPathAxisVal
Owen Taylor3473f882001-02-23 17:55:21 +00008641xmlXPathIsAxisName(const xmlChar *name) {
William M. Brack78637da2003-07-31 14:47:38 +00008642 xmlXPathAxisVal ret = (xmlXPathAxisVal) 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008643 switch (name[0]) {
8644 case 'a':
8645 if (xmlStrEqual(name, BAD_CAST "ancestor"))
8646 ret = AXIS_ANCESTOR;
8647 if (xmlStrEqual(name, BAD_CAST "ancestor-or-self"))
8648 ret = AXIS_ANCESTOR_OR_SELF;
8649 if (xmlStrEqual(name, BAD_CAST "attribute"))
8650 ret = AXIS_ATTRIBUTE;
8651 break;
8652 case 'c':
8653 if (xmlStrEqual(name, BAD_CAST "child"))
8654 ret = AXIS_CHILD;
8655 break;
8656 case 'd':
8657 if (xmlStrEqual(name, BAD_CAST "descendant"))
8658 ret = AXIS_DESCENDANT;
8659 if (xmlStrEqual(name, BAD_CAST "descendant-or-self"))
8660 ret = AXIS_DESCENDANT_OR_SELF;
8661 break;
8662 case 'f':
8663 if (xmlStrEqual(name, BAD_CAST "following"))
8664 ret = AXIS_FOLLOWING;
8665 if (xmlStrEqual(name, BAD_CAST "following-sibling"))
8666 ret = AXIS_FOLLOWING_SIBLING;
8667 break;
8668 case 'n':
8669 if (xmlStrEqual(name, BAD_CAST "namespace"))
8670 ret = AXIS_NAMESPACE;
8671 break;
8672 case 'p':
8673 if (xmlStrEqual(name, BAD_CAST "parent"))
8674 ret = AXIS_PARENT;
8675 if (xmlStrEqual(name, BAD_CAST "preceding"))
8676 ret = AXIS_PRECEDING;
8677 if (xmlStrEqual(name, BAD_CAST "preceding-sibling"))
8678 ret = AXIS_PRECEDING_SIBLING;
8679 break;
8680 case 's':
8681 if (xmlStrEqual(name, BAD_CAST "self"))
8682 ret = AXIS_SELF;
8683 break;
8684 }
8685 return(ret);
8686}
8687
8688/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008689 * xmlXPathCompStep:
Owen Taylor3473f882001-02-23 17:55:21 +00008690 * @ctxt: the XPath Parser context
8691 *
8692 * [4] Step ::= AxisSpecifier NodeTest Predicate*
8693 * | AbbreviatedStep
8694 *
8695 * [12] AbbreviatedStep ::= '.' | '..'
8696 *
8697 * [5] AxisSpecifier ::= AxisName '::'
8698 * | AbbreviatedAxisSpecifier
8699 *
8700 * [13] AbbreviatedAxisSpecifier ::= '@'?
8701 *
8702 * Modified for XPtr range support as:
8703 *
8704 * [4xptr] Step ::= AxisSpecifier NodeTest Predicate*
8705 * | AbbreviatedStep
8706 * | 'range-to' '(' Expr ')' Predicate*
8707 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008708 * Compile one step in a Location Path
Owen Taylor3473f882001-02-23 17:55:21 +00008709 * A location step of . is short for self::node(). This is
8710 * particularly useful in conjunction with //. For example, the
8711 * location path .//para is short for
8712 * self::node()/descendant-or-self::node()/child::para
8713 * and so will select all para descendant elements of the context
8714 * node.
8715 * Similarly, a location step of .. is short for parent::node().
8716 * For example, ../title is short for parent::node()/child::title
8717 * and so will select the title children of the parent of the context
8718 * node.
8719 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008720static void
8721xmlXPathCompStep(xmlXPathParserContextPtr ctxt) {
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008722#ifdef LIBXML_XPTR_ENABLED
8723 int rangeto = 0;
8724 int op2 = -1;
8725#endif
8726
Owen Taylor3473f882001-02-23 17:55:21 +00008727 SKIP_BLANKS;
8728 if ((CUR == '.') && (NXT(1) == '.')) {
8729 SKIP(2);
8730 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008731 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT,
8732 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008733 } else if (CUR == '.') {
8734 NEXT;
8735 SKIP_BLANKS;
8736 } else {
8737 xmlChar *name = NULL;
8738 const xmlChar *prefix = NULL;
8739 xmlXPathTestVal test;
William M. Brack78637da2003-07-31 14:47:38 +00008740 xmlXPathAxisVal axis = (xmlXPathAxisVal) 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008741 xmlXPathTypeVal type;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008742 int op1;
Owen Taylor3473f882001-02-23 17:55:21 +00008743
8744 /*
8745 * The modification needed for XPointer change to the production
8746 */
8747#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008748 if (ctxt->xptr) {
Owen Taylor3473f882001-02-23 17:55:21 +00008749 name = xmlXPathParseNCName(ctxt);
8750 if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) {
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008751 op2 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008752 xmlFree(name);
8753 SKIP_BLANKS;
8754 if (CUR != '(') {
8755 XP_ERROR(XPATH_EXPR_ERROR);
8756 }
8757 NEXT;
8758 SKIP_BLANKS;
8759
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008760 xmlXPathCompileExpr(ctxt);
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008761 /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */
Owen Taylor3473f882001-02-23 17:55:21 +00008762 CHECK_ERROR;
8763
8764 SKIP_BLANKS;
8765 if (CUR != ')') {
8766 XP_ERROR(XPATH_EXPR_ERROR);
8767 }
8768 NEXT;
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008769 rangeto = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00008770 goto eval_predicates;
8771 }
8772 }
8773#endif
Daniel Veillard2156a562001-04-28 12:24:34 +00008774 if (CUR == '*') {
8775 axis = AXIS_CHILD;
8776 } else {
8777 if (name == NULL)
8778 name = xmlXPathParseNCName(ctxt);
8779 if (name != NULL) {
8780 axis = xmlXPathIsAxisName(name);
8781 if (axis != 0) {
8782 SKIP_BLANKS;
8783 if ((CUR == ':') && (NXT(1) == ':')) {
8784 SKIP(2);
8785 xmlFree(name);
8786 name = NULL;
8787 } else {
8788 /* an element name can conflict with an axis one :-\ */
8789 axis = AXIS_CHILD;
8790 }
Owen Taylor3473f882001-02-23 17:55:21 +00008791 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00008792 axis = AXIS_CHILD;
8793 }
Daniel Veillard2156a562001-04-28 12:24:34 +00008794 } else if (CUR == '@') {
8795 NEXT;
8796 axis = AXIS_ATTRIBUTE;
Owen Taylor3473f882001-02-23 17:55:21 +00008797 } else {
Daniel Veillard2156a562001-04-28 12:24:34 +00008798 axis = AXIS_CHILD;
Owen Taylor3473f882001-02-23 17:55:21 +00008799 }
Owen Taylor3473f882001-02-23 17:55:21 +00008800 }
8801
8802 CHECK_ERROR;
8803
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008804 name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name);
Owen Taylor3473f882001-02-23 17:55:21 +00008805 if (test == 0)
8806 return;
8807
8808#ifdef DEBUG_STEP
8809 xmlGenericError(xmlGenericErrorContext,
8810 "Basis : computing new set\n");
8811#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008812
Owen Taylor3473f882001-02-23 17:55:21 +00008813#ifdef DEBUG_STEP
8814 xmlGenericError(xmlGenericErrorContext, "Basis : ");
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008815 if (ctxt->value == NULL)
8816 xmlGenericError(xmlGenericErrorContext, "no value\n");
8817 else if (ctxt->value->nodesetval == NULL)
8818 xmlGenericError(xmlGenericErrorContext, "Empty\n");
8819 else
8820 xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval);
Owen Taylor3473f882001-02-23 17:55:21 +00008821#endif
Owen Taylor3473f882001-02-23 17:55:21 +00008822
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00008823#ifdef LIBXML_XPTR_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00008824eval_predicates:
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00008825#endif
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008826 op1 = ctxt->comp->last;
8827 ctxt->comp->last = -1;
8828
Owen Taylor3473f882001-02-23 17:55:21 +00008829 SKIP_BLANKS;
8830 while (CUR == '[') {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008831 xmlXPathCompPredicate(ctxt, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008832 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008833
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008834#ifdef LIBXML_XPTR_ENABLED
8835 if (rangeto) {
8836 PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0);
8837 } else
8838#endif
8839 PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis,
8840 test, type, (void *)prefix, (void *)name);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008841
Owen Taylor3473f882001-02-23 17:55:21 +00008842 }
8843#ifdef DEBUG_STEP
8844 xmlGenericError(xmlGenericErrorContext, "Step : ");
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008845 if (ctxt->value == NULL)
8846 xmlGenericError(xmlGenericErrorContext, "no value\n");
8847 else if (ctxt->value->nodesetval == NULL)
8848 xmlGenericError(xmlGenericErrorContext, "Empty\n");
8849 else
8850 xmlGenericErrorContextNodeSet(xmlGenericErrorContext,
8851 ctxt->value->nodesetval);
Owen Taylor3473f882001-02-23 17:55:21 +00008852#endif
8853}
8854
8855/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008856 * xmlXPathCompRelativeLocationPath:
Owen Taylor3473f882001-02-23 17:55:21 +00008857 * @ctxt: the XPath Parser context
8858 *
8859 * [3] RelativeLocationPath ::= Step
8860 * | RelativeLocationPath '/' Step
8861 * | AbbreviatedRelativeLocationPath
8862 * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step
8863 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008864 * Compile a relative location path.
Owen Taylor3473f882001-02-23 17:55:21 +00008865 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008866static void
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008867xmlXPathCompRelativeLocationPath
Owen Taylor3473f882001-02-23 17:55:21 +00008868(xmlXPathParserContextPtr ctxt) {
8869 SKIP_BLANKS;
8870 if ((CUR == '/') && (NXT(1) == '/')) {
8871 SKIP(2);
8872 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008873 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
8874 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008875 } else if (CUR == '/') {
8876 NEXT;
8877 SKIP_BLANKS;
8878 }
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008879 xmlXPathCompStep(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008880 SKIP_BLANKS;
8881 while (CUR == '/') {
8882 if ((CUR == '/') && (NXT(1) == '/')) {
8883 SKIP(2);
8884 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008885 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
Owen Taylor3473f882001-02-23 17:55:21 +00008886 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008887 xmlXPathCompStep(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008888 } else if (CUR == '/') {
8889 NEXT;
8890 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008891 xmlXPathCompStep(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008892 }
8893 SKIP_BLANKS;
8894 }
8895}
8896
8897/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008898 * xmlXPathCompLocationPath:
Owen Taylor3473f882001-02-23 17:55:21 +00008899 * @ctxt: the XPath Parser context
8900 *
8901 * [1] LocationPath ::= RelativeLocationPath
8902 * | AbsoluteLocationPath
8903 * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath?
8904 * | AbbreviatedAbsoluteLocationPath
8905 * [10] AbbreviatedAbsoluteLocationPath ::=
8906 * '//' RelativeLocationPath
8907 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008908 * Compile a location path
8909 *
Owen Taylor3473f882001-02-23 17:55:21 +00008910 * // is short for /descendant-or-self::node()/. For example,
8911 * //para is short for /descendant-or-self::node()/child::para and
8912 * so will select any para element in the document (even a para element
8913 * that is a document element will be selected by //para since the
8914 * document element node is a child of the root node); div//para is
8915 * short for div/descendant-or-self::node()/child::para and so will
8916 * select all para descendants of div children.
8917 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008918static void
8919xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00008920 SKIP_BLANKS;
8921 if (CUR != '/') {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008922 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008923 } else {
8924 while (CUR == '/') {
8925 if ((CUR == '/') && (NXT(1) == '/')) {
8926 SKIP(2);
8927 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008928 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
8929 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008930 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008931 } else if (CUR == '/') {
8932 NEXT;
Daniel Veillard608ad072001-06-14 08:32:28 +00008933 SKIP_BLANKS;
8934 if ((CUR != 0 ) &&
William M. Brack76e95df2003-10-18 16:20:14 +00008935 ((IS_LETTER_CH(CUR)) || (CUR == '_') || (CUR == '.') ||
Daniel Veillard608ad072001-06-14 08:32:28 +00008936 (CUR == '@') || (CUR == '*')))
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008937 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008938 }
8939 }
8940 }
8941}
8942
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008943/************************************************************************
8944 * *
8945 * XPath precompiled expression evaluation *
8946 * *
8947 ************************************************************************/
8948
Daniel Veillardf06307e2001-07-03 10:35:50 +00008949static int
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008950xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op);
8951
8952/**
8953 * xmlXPathNodeCollectAndTest:
8954 * @ctxt: the XPath Parser context
8955 * @op: the XPath precompiled step operation
Daniel Veillardf06307e2001-07-03 10:35:50 +00008956 * @first: pointer to the first element in document order
8957 * @last: pointer to the last element in document order
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008958 *
8959 * This is the function implementing a step: based on the current list
8960 * of nodes, it builds up a new list, looking at all nodes under that
William M. Brack08171912003-12-29 02:52:11 +00008961 * axis and selecting them. It also does the predicate filtering
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008962 *
8963 * Pushes the new NodeSet resulting from the search.
Daniel Veillardf06307e2001-07-03 10:35:50 +00008964 *
William M. Brack08171912003-12-29 02:52:11 +00008965 * Returns the number of nodes traversed
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008966 */
Daniel Veillardf06307e2001-07-03 10:35:50 +00008967static int
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008968xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt,
Daniel Veillardf06307e2001-07-03 10:35:50 +00008969 xmlXPathStepOpPtr op,
8970 xmlNodePtr * first, xmlNodePtr * last)
8971{
William M. Brack78637da2003-07-31 14:47:38 +00008972 xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value;
8973 xmlXPathTestVal test = (xmlXPathTestVal) op->value2;
8974 xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008975 const xmlChar *prefix = op->value4;
8976 const xmlChar *name = op->value5;
Daniel Veillarde043ee12001-04-16 14:08:07 +00008977 const xmlChar *URI = NULL;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008978
8979#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008980 int n = 0;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008981#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008982 int i, t = 0;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008983 xmlNodeSetPtr ret, list;
8984 xmlXPathTraversalFunction next = NULL;
Daniel Veillardf06307e2001-07-03 10:35:50 +00008985 void (*addNode) (xmlNodeSetPtr, xmlNodePtr);
Daniel Veillard75be0132002-03-13 10:03:35 +00008986 xmlNodeSetPtr (*mergeNodeSet) (xmlNodeSetPtr, xmlNodeSetPtr);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008987 xmlNodePtr cur = NULL;
8988 xmlXPathObjectPtr obj;
8989 xmlNodeSetPtr nodelist;
8990 xmlNodePtr tmp;
8991
Daniel Veillardf06307e2001-07-03 10:35:50 +00008992 CHECK_TYPE0(XPATH_NODESET);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008993 obj = valuePop(ctxt);
8994 addNode = xmlXPathNodeSetAdd;
Daniel Veillard75be0132002-03-13 10:03:35 +00008995 mergeNodeSet = xmlXPathNodeSetMerge;
Daniel Veillarde043ee12001-04-16 14:08:07 +00008996 if (prefix != NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00008997 URI = xmlXPathNsLookup(ctxt->context, prefix);
8998 if (URI == NULL)
8999 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
Daniel Veillarde043ee12001-04-16 14:08:07 +00009000 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009001#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009002 xmlGenericError(xmlGenericErrorContext, "new step : ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009003#endif
9004 switch (axis) {
9005 case AXIS_ANCESTOR:
9006#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009007 xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009008#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009009 first = NULL;
9010 next = xmlXPathNextAncestor;
9011 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009012 case AXIS_ANCESTOR_OR_SELF:
9013#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009014 xmlGenericError(xmlGenericErrorContext,
9015 "axis 'ancestors-or-self' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009016#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009017 first = NULL;
9018 next = xmlXPathNextAncestorOrSelf;
9019 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009020 case AXIS_ATTRIBUTE:
9021#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009022 xmlGenericError(xmlGenericErrorContext, "axis 'attributes' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009023#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009024 first = NULL;
9025 last = NULL;
9026 next = xmlXPathNextAttribute;
Daniel Veillard75be0132002-03-13 10:03:35 +00009027 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009028 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009029 case AXIS_CHILD:
9030#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009031 xmlGenericError(xmlGenericErrorContext, "axis 'child' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009032#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009033 last = NULL;
9034 next = xmlXPathNextChild;
Daniel Veillard75be0132002-03-13 10:03:35 +00009035 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009036 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009037 case AXIS_DESCENDANT:
9038#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009039 xmlGenericError(xmlGenericErrorContext, "axis 'descendant' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009040#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009041 last = NULL;
9042 next = xmlXPathNextDescendant;
9043 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009044 case AXIS_DESCENDANT_OR_SELF:
9045#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009046 xmlGenericError(xmlGenericErrorContext,
9047 "axis 'descendant-or-self' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009048#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009049 last = NULL;
9050 next = xmlXPathNextDescendantOrSelf;
9051 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009052 case AXIS_FOLLOWING:
9053#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009054 xmlGenericError(xmlGenericErrorContext, "axis 'following' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009055#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009056 last = NULL;
9057 next = xmlXPathNextFollowing;
9058 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009059 case AXIS_FOLLOWING_SIBLING:
9060#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009061 xmlGenericError(xmlGenericErrorContext,
9062 "axis 'following-siblings' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009063#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009064 last = NULL;
9065 next = xmlXPathNextFollowingSibling;
9066 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009067 case AXIS_NAMESPACE:
9068#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009069 xmlGenericError(xmlGenericErrorContext, "axis 'namespace' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009070#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009071 first = NULL;
9072 last = NULL;
9073 next = (xmlXPathTraversalFunction) xmlXPathNextNamespace;
Daniel Veillard75be0132002-03-13 10:03:35 +00009074 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009075 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009076 case AXIS_PARENT:
9077#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009078 xmlGenericError(xmlGenericErrorContext, "axis 'parent' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009079#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009080 first = NULL;
9081 next = xmlXPathNextParent;
9082 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009083 case AXIS_PRECEDING:
9084#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009085 xmlGenericError(xmlGenericErrorContext, "axis 'preceding' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009086#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009087 first = NULL;
9088 next = xmlXPathNextPrecedingInternal;
9089 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009090 case AXIS_PRECEDING_SIBLING:
9091#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009092 xmlGenericError(xmlGenericErrorContext,
9093 "axis 'preceding-sibling' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009094#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009095 first = NULL;
9096 next = xmlXPathNextPrecedingSibling;
9097 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009098 case AXIS_SELF:
9099#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009100 xmlGenericError(xmlGenericErrorContext, "axis 'self' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009101#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009102 first = NULL;
9103 last = NULL;
9104 next = xmlXPathNextSelf;
Daniel Veillard75be0132002-03-13 10:03:35 +00009105 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009106 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009107 }
9108 if (next == NULL)
Daniel Veillardf06307e2001-07-03 10:35:50 +00009109 return(0);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009110
9111 nodelist = obj->nodesetval;
9112 if (nodelist == NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009113 xmlXPathFreeObject(obj);
9114 valuePush(ctxt, xmlXPathWrapNodeSet(NULL));
9115 return(0);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009116 }
9117 addNode = xmlXPathNodeSetAddUnique;
9118 ret = NULL;
9119#ifdef DEBUG_STEP
9120 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf06307e2001-07-03 10:35:50 +00009121 " context contains %d nodes\n", nodelist->nodeNr);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009122 switch (test) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009123 case NODE_TEST_NONE:
9124 xmlGenericError(xmlGenericErrorContext,
9125 " searching for none !!!\n");
9126 break;
9127 case NODE_TEST_TYPE:
9128 xmlGenericError(xmlGenericErrorContext,
9129 " searching for type %d\n", type);
9130 break;
9131 case NODE_TEST_PI:
9132 xmlGenericError(xmlGenericErrorContext,
9133 " searching for PI !!!\n");
9134 break;
9135 case NODE_TEST_ALL:
9136 xmlGenericError(xmlGenericErrorContext,
9137 " searching for *\n");
9138 break;
9139 case NODE_TEST_NS:
9140 xmlGenericError(xmlGenericErrorContext,
9141 " searching for namespace %s\n",
9142 prefix);
9143 break;
9144 case NODE_TEST_NAME:
9145 xmlGenericError(xmlGenericErrorContext,
9146 " searching for name %s\n", name);
9147 if (prefix != NULL)
9148 xmlGenericError(xmlGenericErrorContext,
9149 " with namespace %s\n", prefix);
9150 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009151 }
9152 xmlGenericError(xmlGenericErrorContext, "Testing : ");
9153#endif
9154 /*
9155 * 2.3 Node Tests
9156 * - For the attribute axis, the principal node type is attribute.
9157 * - For the namespace axis, the principal node type is namespace.
9158 * - For other axes, the principal node type is element.
9159 *
9160 * A node test * is true for any node of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009161 * principal node type. For example, child::* will
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009162 * select all element children of the context node
9163 */
9164 tmp = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009165 for (i = 0; i < nodelist->nodeNr; i++) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009166 ctxt->context->node = nodelist->nodeTab[i];
9167
Daniel Veillardf06307e2001-07-03 10:35:50 +00009168 cur = NULL;
9169 list = xmlXPathNodeSetCreate(NULL);
9170 do {
9171 cur = next(ctxt, cur);
9172 if (cur == NULL)
9173 break;
9174 if ((first != NULL) && (*first == cur))
9175 break;
9176 if (((t % 256) == 0) &&
9177 (first != NULL) && (*first != NULL) &&
9178 (xmlXPathCmpNodes(*first, cur) >= 0))
9179 break;
9180 if ((last != NULL) && (*last == cur))
9181 break;
9182 if (((t % 256) == 0) &&
9183 (last != NULL) && (*last != NULL) &&
9184 (xmlXPathCmpNodes(cur, *last) >= 0))
9185 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009186 t++;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009187#ifdef DEBUG_STEP
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009188 xmlGenericError(xmlGenericErrorContext, " %s", cur->name);
9189#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009190 switch (test) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009191 case NODE_TEST_NONE:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009192 ctxt->context->node = tmp;
9193 STRANGE return(t);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009194 case NODE_TEST_TYPE:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009195 if ((cur->type == type) ||
9196 ((type == NODE_TYPE_NODE) &&
9197 ((cur->type == XML_DOCUMENT_NODE) ||
9198 (cur->type == XML_HTML_DOCUMENT_NODE) ||
9199 (cur->type == XML_ELEMENT_NODE) ||
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00009200 (cur->type == XML_NAMESPACE_DECL) ||
9201 (cur->type == XML_ATTRIBUTE_NODE) ||
Daniel Veillardf06307e2001-07-03 10:35:50 +00009202 (cur->type == XML_PI_NODE) ||
9203 (cur->type == XML_COMMENT_NODE) ||
9204 (cur->type == XML_CDATA_SECTION_NODE) ||
Daniel Veillard7583a592001-07-08 13:15:55 +00009205 (cur->type == XML_TEXT_NODE))) ||
9206 ((type == NODE_TYPE_TEXT) &&
9207 (cur->type == XML_CDATA_SECTION_NODE))) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009208#ifdef DEBUG_STEP
9209 n++;
9210#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009211 addNode(list, cur);
9212 }
9213 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009214 case NODE_TEST_PI:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009215 if (cur->type == XML_PI_NODE) {
9216 if ((name != NULL) &&
9217 (!xmlStrEqual(name, cur->name)))
9218 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009219#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009220 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009221#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009222 addNode(list, cur);
9223 }
9224 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009225 case NODE_TEST_ALL:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009226 if (axis == AXIS_ATTRIBUTE) {
9227 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009228#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009229 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009230#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009231 addNode(list, cur);
9232 }
9233 } else if (axis == AXIS_NAMESPACE) {
9234 if (cur->type == XML_NAMESPACE_DECL) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009235#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009236 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009237#endif
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009238 xmlXPathNodeSetAddNs(list, ctxt->context->node,
9239 (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009240 }
9241 } else {
9242 if (cur->type == XML_ELEMENT_NODE) {
9243 if (prefix == NULL) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009244#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009245 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009246#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009247 addNode(list, cur);
9248 } else if ((cur->ns != NULL) &&
9249 (xmlStrEqual(URI, cur->ns->href))) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009250#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009251 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009252#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009253 addNode(list, cur);
9254 }
9255 }
9256 }
9257 break;
9258 case NODE_TEST_NS:{
9259 TODO;
9260 break;
9261 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009262 case NODE_TEST_NAME:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009263 switch (cur->type) {
9264 case XML_ELEMENT_NODE:
9265 if (xmlStrEqual(name, cur->name)) {
9266 if (prefix == NULL) {
9267 if (cur->ns == NULL) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009268#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009269 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009270#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009271 addNode(list, cur);
9272 }
9273 } else {
9274 if ((cur->ns != NULL) &&
9275 (xmlStrEqual(URI,
9276 cur->ns->href))) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009277#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009278 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009279#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009280 addNode(list, cur);
9281 }
9282 }
9283 }
9284 break;
9285 case XML_ATTRIBUTE_NODE:{
9286 xmlAttrPtr attr = (xmlAttrPtr) cur;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009287
Daniel Veillardf06307e2001-07-03 10:35:50 +00009288 if (xmlStrEqual(name, attr->name)) {
9289 if (prefix == NULL) {
9290 if ((attr->ns == NULL) ||
9291 (attr->ns->prefix == NULL)) {
9292#ifdef DEBUG_STEP
9293 n++;
9294#endif
9295 addNode(list,
9296 (xmlNodePtr) attr);
9297 }
9298 } else {
9299 if ((attr->ns != NULL) &&
9300 (xmlStrEqual(URI,
9301 attr->ns->
9302 href))) {
9303#ifdef DEBUG_STEP
9304 n++;
9305#endif
9306 addNode(list,
9307 (xmlNodePtr) attr);
9308 }
9309 }
9310 }
9311 break;
9312 }
9313 case XML_NAMESPACE_DECL:
9314 if (cur->type == XML_NAMESPACE_DECL) {
9315 xmlNsPtr ns = (xmlNsPtr) cur;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009316
Daniel Veillardf06307e2001-07-03 10:35:50 +00009317 if ((ns->prefix != NULL) && (name != NULL)
9318 && (xmlStrEqual(ns->prefix, name))) {
9319#ifdef DEBUG_STEP
9320 n++;
9321#endif
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009322 xmlXPathNodeSetAddNs(list,
9323 ctxt->context->node, (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009324 }
9325 }
9326 break;
9327 default:
9328 break;
9329 }
9330 break;
9331 break;
9332 }
9333 } while (cur != NULL);
9334
9335 /*
9336 * If there is some predicate filtering do it now
9337 */
Daniel Veillard6fbcf422002-03-21 12:32:59 +00009338 if ((op->ch2 != -1) && (list != NULL) && (list->nodeNr > 0)) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009339 xmlXPathObjectPtr obj2;
9340
9341 valuePush(ctxt, xmlXPathWrapNodeSet(list));
9342 xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch2]);
9343 CHECK_TYPE0(XPATH_NODESET);
9344 obj2 = valuePop(ctxt);
9345 list = obj2->nodesetval;
9346 obj2->nodesetval = NULL;
9347 xmlXPathFreeObject(obj2);
9348 }
9349 if (ret == NULL) {
9350 ret = list;
9351 } else {
Daniel Veillard75be0132002-03-13 10:03:35 +00009352 ret = mergeNodeSet(ret, list);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009353 xmlXPathFreeNodeSet(list);
9354 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009355 }
9356 ctxt->context->node = tmp;
9357#ifdef DEBUG_STEP
9358 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf06307e2001-07-03 10:35:50 +00009359 "\nExamined %d nodes, found %d nodes at that step\n",
9360 t, n);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009361#endif
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009362 valuePush(ctxt, xmlXPathWrapNodeSet(ret));
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00009363 if ((obj->boolval) && (obj->user != NULL)) {
9364 ctxt->value->boolval = 1;
9365 ctxt->value->user = obj->user;
9366 obj->user = NULL;
9367 obj->boolval = 0;
9368 }
9369 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009370 return(t);
9371}
9372
9373/**
9374 * xmlXPathNodeCollectAndTestNth:
9375 * @ctxt: the XPath Parser context
9376 * @op: the XPath precompiled step operation
9377 * @indx: the index to collect
9378 * @first: pointer to the first element in document order
9379 * @last: pointer to the last element in document order
9380 *
9381 * This is the function implementing a step: based on the current list
9382 * of nodes, it builds up a new list, looking at all nodes under that
William M. Brack08171912003-12-29 02:52:11 +00009383 * axis and selecting them. It also does the predicate filtering
Daniel Veillardf06307e2001-07-03 10:35:50 +00009384 *
9385 * Pushes the new NodeSet resulting from the search.
9386 * Returns the number of node traversed
9387 */
9388static int
9389xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt,
9390 xmlXPathStepOpPtr op, int indx,
9391 xmlNodePtr * first, xmlNodePtr * last)
9392{
William M. Brack78637da2003-07-31 14:47:38 +00009393 xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value;
9394 xmlXPathTestVal test = (xmlXPathTestVal) op->value2;
9395 xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009396 const xmlChar *prefix = op->value4;
9397 const xmlChar *name = op->value5;
9398 const xmlChar *URI = NULL;
9399 int n = 0, t = 0;
9400
9401 int i;
9402 xmlNodeSetPtr list;
9403 xmlXPathTraversalFunction next = NULL;
9404 void (*addNode) (xmlNodeSetPtr, xmlNodePtr);
9405 xmlNodePtr cur = NULL;
9406 xmlXPathObjectPtr obj;
9407 xmlNodeSetPtr nodelist;
9408 xmlNodePtr tmp;
9409
9410 CHECK_TYPE0(XPATH_NODESET);
9411 obj = valuePop(ctxt);
9412 addNode = xmlXPathNodeSetAdd;
9413 if (prefix != NULL) {
9414 URI = xmlXPathNsLookup(ctxt->context, prefix);
9415 if (URI == NULL)
9416 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
9417 }
9418#ifdef DEBUG_STEP_NTH
9419 xmlGenericError(xmlGenericErrorContext, "new step : ");
9420 if (first != NULL) {
9421 if (*first != NULL)
9422 xmlGenericError(xmlGenericErrorContext, "first = %s ",
9423 (*first)->name);
9424 else
9425 xmlGenericError(xmlGenericErrorContext, "first = NULL ");
9426 }
9427 if (last != NULL) {
9428 if (*last != NULL)
9429 xmlGenericError(xmlGenericErrorContext, "last = %s ",
9430 (*last)->name);
9431 else
9432 xmlGenericError(xmlGenericErrorContext, "last = NULL ");
9433 }
9434#endif
9435 switch (axis) {
9436 case AXIS_ANCESTOR:
9437#ifdef DEBUG_STEP_NTH
9438 xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' ");
9439#endif
9440 first = NULL;
9441 next = xmlXPathNextAncestor;
9442 break;
9443 case AXIS_ANCESTOR_OR_SELF:
9444#ifdef DEBUG_STEP_NTH
9445 xmlGenericError(xmlGenericErrorContext,
9446 "axis 'ancestors-or-self' ");
9447#endif
9448 first = NULL;
9449 next = xmlXPathNextAncestorOrSelf;
9450 break;
9451 case AXIS_ATTRIBUTE:
9452#ifdef DEBUG_STEP_NTH
9453 xmlGenericError(xmlGenericErrorContext, "axis 'attributes' ");
9454#endif
9455 first = NULL;
9456 last = NULL;
9457 next = xmlXPathNextAttribute;
9458 break;
9459 case AXIS_CHILD:
9460#ifdef DEBUG_STEP_NTH
9461 xmlGenericError(xmlGenericErrorContext, "axis 'child' ");
9462#endif
9463 last = NULL;
9464 next = xmlXPathNextChild;
9465 break;
9466 case AXIS_DESCENDANT:
9467#ifdef DEBUG_STEP_NTH
9468 xmlGenericError(xmlGenericErrorContext, "axis 'descendant' ");
9469#endif
9470 last = NULL;
9471 next = xmlXPathNextDescendant;
9472 break;
9473 case AXIS_DESCENDANT_OR_SELF:
9474#ifdef DEBUG_STEP_NTH
9475 xmlGenericError(xmlGenericErrorContext,
9476 "axis 'descendant-or-self' ");
9477#endif
9478 last = NULL;
9479 next = xmlXPathNextDescendantOrSelf;
9480 break;
9481 case AXIS_FOLLOWING:
9482#ifdef DEBUG_STEP_NTH
9483 xmlGenericError(xmlGenericErrorContext, "axis 'following' ");
9484#endif
9485 last = NULL;
9486 next = xmlXPathNextFollowing;
9487 break;
9488 case AXIS_FOLLOWING_SIBLING:
9489#ifdef DEBUG_STEP_NTH
9490 xmlGenericError(xmlGenericErrorContext,
9491 "axis 'following-siblings' ");
9492#endif
9493 last = NULL;
9494 next = xmlXPathNextFollowingSibling;
9495 break;
9496 case AXIS_NAMESPACE:
9497#ifdef DEBUG_STEP_NTH
9498 xmlGenericError(xmlGenericErrorContext, "axis 'namespace' ");
9499#endif
9500 last = NULL;
9501 first = NULL;
9502 next = (xmlXPathTraversalFunction) xmlXPathNextNamespace;
9503 break;
9504 case AXIS_PARENT:
9505#ifdef DEBUG_STEP_NTH
9506 xmlGenericError(xmlGenericErrorContext, "axis 'parent' ");
9507#endif
9508 first = NULL;
9509 next = xmlXPathNextParent;
9510 break;
9511 case AXIS_PRECEDING:
9512#ifdef DEBUG_STEP_NTH
9513 xmlGenericError(xmlGenericErrorContext, "axis 'preceding' ");
9514#endif
9515 first = NULL;
9516 next = xmlXPathNextPrecedingInternal;
9517 break;
9518 case AXIS_PRECEDING_SIBLING:
9519#ifdef DEBUG_STEP_NTH
9520 xmlGenericError(xmlGenericErrorContext,
9521 "axis 'preceding-sibling' ");
9522#endif
9523 first = NULL;
9524 next = xmlXPathNextPrecedingSibling;
9525 break;
9526 case AXIS_SELF:
9527#ifdef DEBUG_STEP_NTH
9528 xmlGenericError(xmlGenericErrorContext, "axis 'self' ");
9529#endif
9530 first = NULL;
9531 last = NULL;
9532 next = xmlXPathNextSelf;
9533 break;
9534 }
9535 if (next == NULL)
9536 return(0);
9537
9538 nodelist = obj->nodesetval;
9539 if (nodelist == NULL) {
9540 xmlXPathFreeObject(obj);
9541 valuePush(ctxt, xmlXPathWrapNodeSet(NULL));
9542 return(0);
9543 }
9544 addNode = xmlXPathNodeSetAddUnique;
9545#ifdef DEBUG_STEP_NTH
9546 xmlGenericError(xmlGenericErrorContext,
9547 " context contains %d nodes\n", nodelist->nodeNr);
9548 switch (test) {
9549 case NODE_TEST_NONE:
9550 xmlGenericError(xmlGenericErrorContext,
9551 " searching for none !!!\n");
9552 break;
9553 case NODE_TEST_TYPE:
9554 xmlGenericError(xmlGenericErrorContext,
9555 " searching for type %d\n", type);
9556 break;
9557 case NODE_TEST_PI:
9558 xmlGenericError(xmlGenericErrorContext,
9559 " searching for PI !!!\n");
9560 break;
9561 case NODE_TEST_ALL:
9562 xmlGenericError(xmlGenericErrorContext,
9563 " searching for *\n");
9564 break;
9565 case NODE_TEST_NS:
9566 xmlGenericError(xmlGenericErrorContext,
9567 " searching for namespace %s\n",
9568 prefix);
9569 break;
9570 case NODE_TEST_NAME:
9571 xmlGenericError(xmlGenericErrorContext,
9572 " searching for name %s\n", name);
9573 if (prefix != NULL)
9574 xmlGenericError(xmlGenericErrorContext,
9575 " with namespace %s\n", prefix);
9576 break;
9577 }
9578 xmlGenericError(xmlGenericErrorContext, "Testing : ");
9579#endif
9580 /*
9581 * 2.3 Node Tests
9582 * - For the attribute axis, the principal node type is attribute.
9583 * - For the namespace axis, the principal node type is namespace.
9584 * - For other axes, the principal node type is element.
9585 *
9586 * A node test * is true for any node of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009587 * principal node type. For example, child::* will
Daniel Veillardf06307e2001-07-03 10:35:50 +00009588 * select all element children of the context node
9589 */
9590 tmp = ctxt->context->node;
9591 list = xmlXPathNodeSetCreate(NULL);
9592 for (i = 0; i < nodelist->nodeNr; i++) {
9593 ctxt->context->node = nodelist->nodeTab[i];
9594
9595 cur = NULL;
9596 n = 0;
9597 do {
9598 cur = next(ctxt, cur);
9599 if (cur == NULL)
9600 break;
9601 if ((first != NULL) && (*first == cur))
9602 break;
9603 if (((t % 256) == 0) &&
9604 (first != NULL) && (*first != NULL) &&
9605 (xmlXPathCmpNodes(*first, cur) >= 0))
9606 break;
9607 if ((last != NULL) && (*last == cur))
9608 break;
9609 if (((t % 256) == 0) &&
9610 (last != NULL) && (*last != NULL) &&
9611 (xmlXPathCmpNodes(cur, *last) >= 0))
9612 break;
9613 t++;
9614 switch (test) {
9615 case NODE_TEST_NONE:
9616 ctxt->context->node = tmp;
9617 STRANGE return(0);
9618 case NODE_TEST_TYPE:
9619 if ((cur->type == type) ||
9620 ((type == NODE_TYPE_NODE) &&
9621 ((cur->type == XML_DOCUMENT_NODE) ||
9622 (cur->type == XML_HTML_DOCUMENT_NODE) ||
9623 (cur->type == XML_ELEMENT_NODE) ||
9624 (cur->type == XML_PI_NODE) ||
9625 (cur->type == XML_COMMENT_NODE) ||
9626 (cur->type == XML_CDATA_SECTION_NODE) ||
Daniel Veillard8606bbb2002-11-12 12:36:52 +00009627 (cur->type == XML_TEXT_NODE))) ||
9628 ((type == NODE_TYPE_TEXT) &&
9629 (cur->type == XML_CDATA_SECTION_NODE))) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009630 n++;
9631 if (n == indx)
9632 addNode(list, cur);
9633 }
9634 break;
9635 case NODE_TEST_PI:
9636 if (cur->type == XML_PI_NODE) {
9637 if ((name != NULL) &&
9638 (!xmlStrEqual(name, cur->name)))
9639 break;
9640 n++;
9641 if (n == indx)
9642 addNode(list, cur);
9643 }
9644 break;
9645 case NODE_TEST_ALL:
9646 if (axis == AXIS_ATTRIBUTE) {
9647 if (cur->type == XML_ATTRIBUTE_NODE) {
9648 n++;
9649 if (n == indx)
9650 addNode(list, cur);
9651 }
9652 } else if (axis == AXIS_NAMESPACE) {
9653 if (cur->type == XML_NAMESPACE_DECL) {
9654 n++;
9655 if (n == indx)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009656 xmlXPathNodeSetAddNs(list, ctxt->context->node,
9657 (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009658 }
9659 } else {
9660 if (cur->type == XML_ELEMENT_NODE) {
9661 if (prefix == NULL) {
9662 n++;
9663 if (n == indx)
9664 addNode(list, cur);
9665 } else if ((cur->ns != NULL) &&
9666 (xmlStrEqual(URI, cur->ns->href))) {
9667 n++;
9668 if (n == indx)
9669 addNode(list, cur);
9670 }
9671 }
9672 }
9673 break;
9674 case NODE_TEST_NS:{
9675 TODO;
9676 break;
9677 }
9678 case NODE_TEST_NAME:
9679 switch (cur->type) {
9680 case XML_ELEMENT_NODE:
9681 if (xmlStrEqual(name, cur->name)) {
9682 if (prefix == NULL) {
9683 if (cur->ns == NULL) {
9684 n++;
9685 if (n == indx)
9686 addNode(list, cur);
9687 }
9688 } else {
9689 if ((cur->ns != NULL) &&
9690 (xmlStrEqual(URI,
9691 cur->ns->href))) {
9692 n++;
9693 if (n == indx)
9694 addNode(list, cur);
9695 }
9696 }
9697 }
9698 break;
9699 case XML_ATTRIBUTE_NODE:{
9700 xmlAttrPtr attr = (xmlAttrPtr) cur;
9701
9702 if (xmlStrEqual(name, attr->name)) {
9703 if (prefix == NULL) {
9704 if ((attr->ns == NULL) ||
9705 (attr->ns->prefix == NULL)) {
9706 n++;
9707 if (n == indx)
9708 addNode(list, cur);
9709 }
9710 } else {
9711 if ((attr->ns != NULL) &&
9712 (xmlStrEqual(URI,
9713 attr->ns->
9714 href))) {
9715 n++;
9716 if (n == indx)
9717 addNode(list, cur);
9718 }
9719 }
9720 }
9721 break;
9722 }
9723 case XML_NAMESPACE_DECL:
9724 if (cur->type == XML_NAMESPACE_DECL) {
9725 xmlNsPtr ns = (xmlNsPtr) cur;
9726
9727 if ((ns->prefix != NULL) && (name != NULL)
9728 && (xmlStrEqual(ns->prefix, name))) {
9729 n++;
9730 if (n == indx)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009731 xmlXPathNodeSetAddNs(list,
9732 ctxt->context->node, (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009733 }
9734 }
9735 break;
9736 default:
9737 break;
9738 }
9739 break;
9740 break;
9741 }
9742 } while (n < indx);
9743 }
9744 ctxt->context->node = tmp;
9745#ifdef DEBUG_STEP_NTH
9746 xmlGenericError(xmlGenericErrorContext,
9747 "\nExamined %d nodes, found %d nodes at that step\n",
9748 t, list->nodeNr);
9749#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009750 valuePush(ctxt, xmlXPathWrapNodeSet(list));
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00009751 if ((obj->boolval) && (obj->user != NULL)) {
9752 ctxt->value->boolval = 1;
9753 ctxt->value->user = obj->user;
9754 obj->user = NULL;
9755 obj->boolval = 0;
9756 }
9757 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009758 return(t);
9759}
9760
9761/**
9762 * xmlXPathCompOpEvalFirst:
9763 * @ctxt: the XPath parser context with the compiled expression
9764 * @op: an XPath compiled operation
9765 * @first: the first elem found so far
9766 *
9767 * Evaluate the Precompiled XPath operation searching only the first
9768 * element in document order
9769 *
9770 * Returns the number of examined objects.
9771 */
9772static int
9773xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt,
9774 xmlXPathStepOpPtr op, xmlNodePtr * first)
9775{
9776 int total = 0, cur;
9777 xmlXPathCompExprPtr comp;
9778 xmlXPathObjectPtr arg1, arg2;
9779
Daniel Veillard556c6682001-10-06 09:59:51 +00009780 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009781 comp = ctxt->comp;
9782 switch (op->op) {
9783 case XPATH_OP_END:
9784 return (0);
9785 case XPATH_OP_UNION:
9786 total =
9787 xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1],
9788 first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009789 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009790 if ((ctxt->value != NULL)
9791 && (ctxt->value->type == XPATH_NODESET)
9792 && (ctxt->value->nodesetval != NULL)
9793 && (ctxt->value->nodesetval->nodeNr >= 1)) {
9794 /*
9795 * limit tree traversing to first node in the result
9796 */
9797 xmlXPathNodeSetSort(ctxt->value->nodesetval);
9798 *first = ctxt->value->nodesetval->nodeTab[0];
9799 }
9800 cur =
9801 xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2],
9802 first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009803 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009804 CHECK_TYPE0(XPATH_NODESET);
9805 arg2 = valuePop(ctxt);
9806
9807 CHECK_TYPE0(XPATH_NODESET);
9808 arg1 = valuePop(ctxt);
9809
9810 arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
9811 arg2->nodesetval);
9812 valuePush(ctxt, arg1);
9813 xmlXPathFreeObject(arg2);
9814 /* optimizer */
9815 if (total > cur)
9816 xmlXPathCompSwap(op);
9817 return (total + cur);
9818 case XPATH_OP_ROOT:
9819 xmlXPathRoot(ctxt);
9820 return (0);
9821 case XPATH_OP_NODE:
9822 if (op->ch1 != -1)
9823 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009824 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009825 if (op->ch2 != -1)
9826 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009827 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009828 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
9829 return (total);
9830 case XPATH_OP_RESET:
9831 if (op->ch1 != -1)
9832 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009833 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009834 if (op->ch2 != -1)
9835 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009836 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009837 ctxt->context->node = NULL;
9838 return (total);
9839 case XPATH_OP_COLLECT:{
9840 if (op->ch1 == -1)
9841 return (total);
9842
9843 total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009844 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009845
9846 /*
9847 * Optimization for [n] selection where n is a number
9848 */
9849 if ((op->ch2 != -1) &&
9850 (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) &&
9851 (comp->steps[op->ch2].ch1 == -1) &&
9852 (comp->steps[op->ch2].ch2 != -1) &&
9853 (comp->steps[comp->steps[op->ch2].ch2].op ==
9854 XPATH_OP_VALUE)) {
9855 xmlXPathObjectPtr val;
9856
9857 val = comp->steps[comp->steps[op->ch2].ch2].value4;
9858 if ((val != NULL) && (val->type == XPATH_NUMBER)) {
9859 int indx = (int) val->floatval;
9860
9861 if (val->floatval == (float) indx) {
9862 xmlXPathNodeCollectAndTestNth(ctxt, op, indx,
9863 first, NULL);
9864 return (total);
9865 }
9866 }
9867 }
9868 total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL);
9869 return (total);
9870 }
9871 case XPATH_OP_VALUE:
9872 valuePush(ctxt,
9873 xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4));
9874 return (0);
9875 case XPATH_OP_SORT:
9876 if (op->ch1 != -1)
9877 total +=
9878 xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1],
9879 first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009880 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009881 if ((ctxt->value != NULL)
9882 && (ctxt->value->type == XPATH_NODESET)
9883 && (ctxt->value->nodesetval != NULL))
9884 xmlXPathNodeSetSort(ctxt->value->nodesetval);
9885 return (total);
9886 default:
9887 return (xmlXPathCompOpEval(ctxt, op));
9888 }
9889}
9890
9891/**
9892 * xmlXPathCompOpEvalLast:
9893 * @ctxt: the XPath parser context with the compiled expression
9894 * @op: an XPath compiled operation
9895 * @last: the last elem found so far
9896 *
9897 * Evaluate the Precompiled XPath operation searching only the last
9898 * element in document order
9899 *
William M. Brack08171912003-12-29 02:52:11 +00009900 * Returns the number of nodes traversed
Daniel Veillardf06307e2001-07-03 10:35:50 +00009901 */
9902static int
9903xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op,
9904 xmlNodePtr * last)
9905{
9906 int total = 0, cur;
9907 xmlXPathCompExprPtr comp;
9908 xmlXPathObjectPtr arg1, arg2;
William M. Brackce4fc562004-01-22 02:47:18 +00009909 xmlNodePtr bak;
9910 xmlDocPtr bakd;
9911 int pp;
9912 int cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009913
Daniel Veillard556c6682001-10-06 09:59:51 +00009914 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009915 comp = ctxt->comp;
9916 switch (op->op) {
9917 case XPATH_OP_END:
9918 return (0);
9919 case XPATH_OP_UNION:
William M. Brackce4fc562004-01-22 02:47:18 +00009920 bakd = ctxt->context->doc;
9921 bak = ctxt->context->node;
9922 pp = ctxt->context->proximityPosition;
9923 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009924 total =
9925 xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last);
Daniel Veillard556c6682001-10-06 09:59:51 +00009926 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009927 if ((ctxt->value != NULL)
9928 && (ctxt->value->type == XPATH_NODESET)
9929 && (ctxt->value->nodesetval != NULL)
9930 && (ctxt->value->nodesetval->nodeNr >= 1)) {
9931 /*
9932 * limit tree traversing to first node in the result
9933 */
9934 xmlXPathNodeSetSort(ctxt->value->nodesetval);
9935 *last =
9936 ctxt->value->nodesetval->nodeTab[ctxt->value->
9937 nodesetval->nodeNr -
9938 1];
9939 }
William M. Brackce4fc562004-01-22 02:47:18 +00009940 ctxt->context->doc = bakd;
9941 ctxt->context->node = bak;
9942 ctxt->context->proximityPosition = pp;
9943 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009944 cur =
9945 xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last);
Daniel Veillard556c6682001-10-06 09:59:51 +00009946 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009947 if ((ctxt->value != NULL)
9948 && (ctxt->value->type == XPATH_NODESET)
9949 && (ctxt->value->nodesetval != NULL)
9950 && (ctxt->value->nodesetval->nodeNr >= 1)) {
9951 }
9952 CHECK_TYPE0(XPATH_NODESET);
9953 arg2 = valuePop(ctxt);
9954
9955 CHECK_TYPE0(XPATH_NODESET);
9956 arg1 = valuePop(ctxt);
9957
9958 arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
9959 arg2->nodesetval);
9960 valuePush(ctxt, arg1);
9961 xmlXPathFreeObject(arg2);
9962 /* optimizer */
9963 if (total > cur)
9964 xmlXPathCompSwap(op);
9965 return (total + cur);
9966 case XPATH_OP_ROOT:
9967 xmlXPathRoot(ctxt);
9968 return (0);
9969 case XPATH_OP_NODE:
9970 if (op->ch1 != -1)
9971 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009972 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009973 if (op->ch2 != -1)
9974 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009975 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009976 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
9977 return (total);
9978 case XPATH_OP_RESET:
9979 if (op->ch1 != -1)
9980 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009981 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009982 if (op->ch2 != -1)
9983 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009984 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009985 ctxt->context->node = NULL;
9986 return (total);
9987 case XPATH_OP_COLLECT:{
9988 if (op->ch1 == -1)
9989 return (0);
9990
9991 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009992 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009993
9994 /*
9995 * Optimization for [n] selection where n is a number
9996 */
9997 if ((op->ch2 != -1) &&
9998 (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) &&
9999 (comp->steps[op->ch2].ch1 == -1) &&
10000 (comp->steps[op->ch2].ch2 != -1) &&
10001 (comp->steps[comp->steps[op->ch2].ch2].op ==
10002 XPATH_OP_VALUE)) {
10003 xmlXPathObjectPtr val;
10004
10005 val = comp->steps[comp->steps[op->ch2].ch2].value4;
10006 if ((val != NULL) && (val->type == XPATH_NUMBER)) {
10007 int indx = (int) val->floatval;
10008
10009 if (val->floatval == (float) indx) {
10010 total +=
10011 xmlXPathNodeCollectAndTestNth(ctxt, op,
10012 indx, NULL,
10013 last);
10014 return (total);
10015 }
10016 }
10017 }
10018 total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last);
10019 return (total);
10020 }
10021 case XPATH_OP_VALUE:
10022 valuePush(ctxt,
10023 xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4));
10024 return (0);
10025 case XPATH_OP_SORT:
10026 if (op->ch1 != -1)
10027 total +=
10028 xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1],
10029 last);
Daniel Veillard556c6682001-10-06 09:59:51 +000010030 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010031 if ((ctxt->value != NULL)
10032 && (ctxt->value->type == XPATH_NODESET)
10033 && (ctxt->value->nodesetval != NULL))
10034 xmlXPathNodeSetSort(ctxt->value->nodesetval);
10035 return (total);
10036 default:
10037 return (xmlXPathCompOpEval(ctxt, op));
10038 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +000010039}
10040
Owen Taylor3473f882001-02-23 17:55:21 +000010041/**
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010042 * xmlXPathCompOpEval:
10043 * @ctxt: the XPath parser context with the compiled expression
10044 * @op: an XPath compiled operation
10045 *
10046 * Evaluate the Precompiled XPath operation
William M. Brack08171912003-12-29 02:52:11 +000010047 * Returns the number of nodes traversed
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010048 */
Daniel Veillardf06307e2001-07-03 10:35:50 +000010049static int
10050xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op)
10051{
10052 int total = 0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010053 int equal, ret;
10054 xmlXPathCompExprPtr comp;
10055 xmlXPathObjectPtr arg1, arg2;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010056 xmlNodePtr bak;
10057 xmlDocPtr bakd;
William M. Brack6000af52002-06-28 11:43:13 +000010058 int pp;
William M. Brack692092b2002-06-28 15:01:24 +000010059 int cs;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010060
Daniel Veillard556c6682001-10-06 09:59:51 +000010061 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010062 comp = ctxt->comp;
10063 switch (op->op) {
Daniel Veillardf06307e2001-07-03 10:35:50 +000010064 case XPATH_OP_END:
10065 return (0);
10066 case XPATH_OP_AND:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010067 bakd = ctxt->context->doc;
10068 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010069 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010070 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010071 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010072 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010073 xmlXPathBooleanFunction(ctxt, 1);
10074 if ((ctxt->value == NULL) || (ctxt->value->boolval == 0))
10075 return (total);
10076 arg2 = valuePop(ctxt);
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010077 ctxt->context->doc = bakd;
10078 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010079 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010080 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010081 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010082 if (ctxt->error) {
10083 xmlXPathFreeObject(arg2);
10084 return(0);
10085 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010086 xmlXPathBooleanFunction(ctxt, 1);
10087 arg1 = valuePop(ctxt);
10088 arg1->boolval &= arg2->boolval;
10089 valuePush(ctxt, arg1);
10090 xmlXPathFreeObject(arg2);
10091 return (total);
10092 case XPATH_OP_OR:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010093 bakd = ctxt->context->doc;
10094 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010095 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010096 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010097 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010098 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010099 xmlXPathBooleanFunction(ctxt, 1);
10100 if ((ctxt->value == NULL) || (ctxt->value->boolval == 1))
10101 return (total);
10102 arg2 = valuePop(ctxt);
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010103 ctxt->context->doc = bakd;
10104 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010105 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010106 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010107 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010108 if (ctxt->error) {
10109 xmlXPathFreeObject(arg2);
10110 return(0);
10111 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010112 xmlXPathBooleanFunction(ctxt, 1);
10113 arg1 = valuePop(ctxt);
10114 arg1->boolval |= arg2->boolval;
10115 valuePush(ctxt, arg1);
10116 xmlXPathFreeObject(arg2);
10117 return (total);
10118 case XPATH_OP_EQUAL:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010119 bakd = ctxt->context->doc;
10120 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010121 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010122 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010123 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010124 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010125 ctxt->context->doc = bakd;
10126 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010127 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010128 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010129 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010130 CHECK_ERROR0;
William M. Brack0c022ad2002-07-12 00:56:01 +000010131 if (op->value)
10132 equal = xmlXPathEqualValues(ctxt);
10133 else
10134 equal = xmlXPathNotEqualValues(ctxt);
10135 valuePush(ctxt, xmlXPathNewBoolean(equal));
Daniel Veillardf06307e2001-07-03 10:35:50 +000010136 return (total);
10137 case XPATH_OP_CMP:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010138 bakd = ctxt->context->doc;
10139 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010140 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010141 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010142 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010143 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010144 ctxt->context->doc = bakd;
10145 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010146 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010147 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010148 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010149 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010150 ret = xmlXPathCompareValues(ctxt, op->value, op->value2);
10151 valuePush(ctxt, xmlXPathNewBoolean(ret));
10152 return (total);
10153 case XPATH_OP_PLUS:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010154 bakd = ctxt->context->doc;
10155 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010156 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010157 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010158 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010159 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010160 if (op->ch2 != -1) {
10161 ctxt->context->doc = bakd;
10162 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010163 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010164 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010165 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010166 }
Daniel Veillard556c6682001-10-06 09:59:51 +000010167 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010168 if (op->value == 0)
10169 xmlXPathSubValues(ctxt);
10170 else if (op->value == 1)
10171 xmlXPathAddValues(ctxt);
10172 else if (op->value == 2)
10173 xmlXPathValueFlipSign(ctxt);
10174 else if (op->value == 3) {
10175 CAST_TO_NUMBER;
10176 CHECK_TYPE0(XPATH_NUMBER);
10177 }
10178 return (total);
10179 case XPATH_OP_MULT:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010180 bakd = ctxt->context->doc;
10181 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010182 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010183 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010184 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010185 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010186 ctxt->context->doc = bakd;
10187 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010188 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010189 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010190 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010191 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010192 if (op->value == 0)
10193 xmlXPathMultValues(ctxt);
10194 else if (op->value == 1)
10195 xmlXPathDivValues(ctxt);
10196 else if (op->value == 2)
10197 xmlXPathModValues(ctxt);
10198 return (total);
10199 case XPATH_OP_UNION:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010200 bakd = ctxt->context->doc;
10201 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010202 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010203 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010204 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010205 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010206 ctxt->context->doc = bakd;
10207 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010208 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010209 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010210 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010211 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010212 CHECK_TYPE0(XPATH_NODESET);
10213 arg2 = valuePop(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010214
Daniel Veillardf06307e2001-07-03 10:35:50 +000010215 CHECK_TYPE0(XPATH_NODESET);
10216 arg1 = valuePop(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010217
Daniel Veillardf06307e2001-07-03 10:35:50 +000010218 arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
10219 arg2->nodesetval);
10220 valuePush(ctxt, arg1);
10221 xmlXPathFreeObject(arg2);
10222 return (total);
10223 case XPATH_OP_ROOT:
10224 xmlXPathRoot(ctxt);
10225 return (total);
10226 case XPATH_OP_NODE:
10227 if (op->ch1 != -1)
10228 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010229 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010230 if (op->ch2 != -1)
10231 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010232 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010233 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
10234 return (total);
10235 case XPATH_OP_RESET:
10236 if (op->ch1 != -1)
10237 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010238 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010239 if (op->ch2 != -1)
10240 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010241 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010242 ctxt->context->node = NULL;
10243 return (total);
10244 case XPATH_OP_COLLECT:{
10245 if (op->ch1 == -1)
10246 return (total);
Daniel Veillardd8df6c02001-04-05 16:54:14 +000010247
Daniel Veillardf06307e2001-07-03 10:35:50 +000010248 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010249 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010250
Daniel Veillardf06307e2001-07-03 10:35:50 +000010251 /*
10252 * Optimization for [n] selection where n is a number
10253 */
10254 if ((op->ch2 != -1) &&
10255 (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) &&
10256 (comp->steps[op->ch2].ch1 == -1) &&
10257 (comp->steps[op->ch2].ch2 != -1) &&
10258 (comp->steps[comp->steps[op->ch2].ch2].op ==
10259 XPATH_OP_VALUE)) {
10260 xmlXPathObjectPtr val;
Daniel Veillard42596ad2001-05-22 16:57:14 +000010261
Daniel Veillardf06307e2001-07-03 10:35:50 +000010262 val = comp->steps[comp->steps[op->ch2].ch2].value4;
10263 if ((val != NULL) && (val->type == XPATH_NUMBER)) {
10264 int indx = (int) val->floatval;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010265
Daniel Veillardf06307e2001-07-03 10:35:50 +000010266 if (val->floatval == (float) indx) {
10267 total +=
10268 xmlXPathNodeCollectAndTestNth(ctxt, op,
10269 indx, NULL,
10270 NULL);
10271 return (total);
10272 }
10273 }
10274 }
10275 total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL);
10276 return (total);
10277 }
10278 case XPATH_OP_VALUE:
10279 valuePush(ctxt,
10280 xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4));
10281 return (total);
10282 case XPATH_OP_VARIABLE:{
Daniel Veillard556c6682001-10-06 09:59:51 +000010283 xmlXPathObjectPtr val;
10284
Daniel Veillardf06307e2001-07-03 10:35:50 +000010285 if (op->ch1 != -1)
10286 total +=
10287 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010288 if (op->value5 == NULL) {
10289 val = xmlXPathVariableLookup(ctxt->context, op->value4);
10290 if (val == NULL) {
10291 ctxt->error = XPATH_UNDEF_VARIABLE_ERROR;
10292 return(0);
10293 }
10294 valuePush(ctxt, val);
10295 } else {
Daniel Veillardf06307e2001-07-03 10:35:50 +000010296 const xmlChar *URI;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010297
Daniel Veillardf06307e2001-07-03 10:35:50 +000010298 URI = xmlXPathNsLookup(ctxt->context, op->value5);
10299 if (URI == NULL) {
10300 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010301 "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n",
Daniel Veillardf06307e2001-07-03 10:35:50 +000010302 op->value4, op->value5);
10303 return (total);
10304 }
Daniel Veillard556c6682001-10-06 09:59:51 +000010305 val = xmlXPathVariableLookupNS(ctxt->context,
10306 op->value4, URI);
10307 if (val == NULL) {
10308 ctxt->error = XPATH_UNDEF_VARIABLE_ERROR;
10309 return(0);
10310 }
10311 valuePush(ctxt, val);
Daniel Veillardf06307e2001-07-03 10:35:50 +000010312 }
10313 return (total);
10314 }
10315 case XPATH_OP_FUNCTION:{
10316 xmlXPathFunction func;
10317 const xmlChar *oldFunc, *oldFuncURI;
Daniel Veillard556c6682001-10-06 09:59:51 +000010318 int i;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010319
10320 if (op->ch1 != -1)
10321 total +=
10322 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010323 if (ctxt->valueNr < op->value) {
10324 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010325 "xmlXPathCompOpEval: parameter error\n");
Daniel Veillard556c6682001-10-06 09:59:51 +000010326 ctxt->error = XPATH_INVALID_OPERAND;
10327 return (total);
10328 }
10329 for (i = 0; i < op->value; i++)
10330 if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) {
10331 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010332 "xmlXPathCompOpEval: parameter error\n");
Daniel Veillard556c6682001-10-06 09:59:51 +000010333 ctxt->error = XPATH_INVALID_OPERAND;
10334 return (total);
10335 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010336 if (op->cache != NULL)
10337 func = (xmlXPathFunction) op->cache;
10338 else {
10339 const xmlChar *URI = NULL;
10340
10341 if (op->value5 == NULL)
10342 func =
10343 xmlXPathFunctionLookup(ctxt->context,
10344 op->value4);
10345 else {
10346 URI = xmlXPathNsLookup(ctxt->context, op->value5);
10347 if (URI == NULL) {
10348 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010349 "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n",
Daniel Veillardf06307e2001-07-03 10:35:50 +000010350 op->value4, op->value5);
10351 return (total);
10352 }
10353 func = xmlXPathFunctionLookupNS(ctxt->context,
10354 op->value4, URI);
10355 }
10356 if (func == NULL) {
10357 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010358 "xmlXPathCompOpEval: function %s not found\n",
Daniel Veillardf06307e2001-07-03 10:35:50 +000010359 op->value4);
10360 XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR);
Daniel Veillardf06307e2001-07-03 10:35:50 +000010361 }
10362 op->cache = (void *) func;
10363 op->cacheURI = (void *) URI;
10364 }
10365 oldFunc = ctxt->context->function;
10366 oldFuncURI = ctxt->context->functionURI;
10367 ctxt->context->function = op->value4;
10368 ctxt->context->functionURI = op->cacheURI;
10369 func(ctxt, op->value);
10370 ctxt->context->function = oldFunc;
10371 ctxt->context->functionURI = oldFuncURI;
10372 return (total);
10373 }
10374 case XPATH_OP_ARG:
Daniel Veillard088bf112002-05-14 11:03:59 +000010375 bakd = ctxt->context->doc;
10376 bak = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010377 if (op->ch1 != -1)
10378 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard088bf112002-05-14 11:03:59 +000010379 ctxt->context->doc = bakd;
10380 ctxt->context->node = bak;
Daniel Veillard556c6682001-10-06 09:59:51 +000010381 CHECK_ERROR0;
William M. Brack72ee48d2003-12-30 08:30:19 +000010382 if (op->ch2 != -1) {
Daniel Veillardf06307e2001-07-03 10:35:50 +000010383 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
William M. Brack72ee48d2003-12-30 08:30:19 +000010384 ctxt->context->doc = bakd;
10385 ctxt->context->node = bak;
10386 CHECK_ERROR0;
10387 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010388 return (total);
10389 case XPATH_OP_PREDICATE:
10390 case XPATH_OP_FILTER:{
10391 xmlXPathObjectPtr res;
10392 xmlXPathObjectPtr obj, tmp;
10393 xmlNodeSetPtr newset = NULL;
10394 xmlNodeSetPtr oldset;
10395 xmlNodePtr oldnode;
10396 int i;
10397
10398 /*
10399 * Optimization for ()[1] selection i.e. the first elem
10400 */
10401 if ((op->ch1 != -1) && (op->ch2 != -1) &&
10402 (comp->steps[op->ch1].op == XPATH_OP_SORT) &&
10403 (comp->steps[op->ch2].op == XPATH_OP_VALUE)) {
10404 xmlXPathObjectPtr val;
10405
10406 val = comp->steps[op->ch2].value4;
10407 if ((val != NULL) && (val->type == XPATH_NUMBER) &&
10408 (val->floatval == 1.0)) {
10409 xmlNodePtr first = NULL;
10410
10411 total +=
10412 xmlXPathCompOpEvalFirst(ctxt,
10413 &comp->steps[op->ch1],
10414 &first);
Daniel Veillard556c6682001-10-06 09:59:51 +000010415 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010416 /*
10417 * The nodeset should be in document order,
10418 * Keep only the first value
10419 */
10420 if ((ctxt->value != NULL) &&
10421 (ctxt->value->type == XPATH_NODESET) &&
10422 (ctxt->value->nodesetval != NULL) &&
10423 (ctxt->value->nodesetval->nodeNr > 1))
10424 ctxt->value->nodesetval->nodeNr = 1;
10425 return (total);
10426 }
10427 }
10428 /*
10429 * Optimization for ()[last()] selection i.e. the last elem
10430 */
10431 if ((op->ch1 != -1) && (op->ch2 != -1) &&
10432 (comp->steps[op->ch1].op == XPATH_OP_SORT) &&
10433 (comp->steps[op->ch2].op == XPATH_OP_SORT)) {
10434 int f = comp->steps[op->ch2].ch1;
10435
10436 if ((f != -1) &&
10437 (comp->steps[f].op == XPATH_OP_FUNCTION) &&
10438 (comp->steps[f].value5 == NULL) &&
10439 (comp->steps[f].value == 0) &&
10440 (comp->steps[f].value4 != NULL) &&
10441 (xmlStrEqual
10442 (comp->steps[f].value4, BAD_CAST "last"))) {
10443 xmlNodePtr last = NULL;
10444
10445 total +=
10446 xmlXPathCompOpEvalLast(ctxt,
10447 &comp->steps[op->ch1],
10448 &last);
Daniel Veillard556c6682001-10-06 09:59:51 +000010449 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010450 /*
10451 * The nodeset should be in document order,
10452 * Keep only the last value
10453 */
10454 if ((ctxt->value != NULL) &&
10455 (ctxt->value->type == XPATH_NODESET) &&
10456 (ctxt->value->nodesetval != NULL) &&
10457 (ctxt->value->nodesetval->nodeTab != NULL) &&
10458 (ctxt->value->nodesetval->nodeNr > 1)) {
10459 ctxt->value->nodesetval->nodeTab[0] =
10460 ctxt->value->nodesetval->nodeTab[ctxt->
10461 value->
10462 nodesetval->
10463 nodeNr -
10464 1];
10465 ctxt->value->nodesetval->nodeNr = 1;
10466 }
10467 return (total);
10468 }
10469 }
10470
10471 if (op->ch1 != -1)
10472 total +=
10473 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010474 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010475 if (op->ch2 == -1)
10476 return (total);
10477 if (ctxt->value == NULL)
10478 return (total);
10479
10480 oldnode = ctxt->context->node;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010481
10482#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardf06307e2001-07-03 10:35:50 +000010483 /*
10484 * Hum are we filtering the result of an XPointer expression
10485 */
10486 if (ctxt->value->type == XPATH_LOCATIONSET) {
10487 xmlLocationSetPtr newlocset = NULL;
10488 xmlLocationSetPtr oldlocset;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010489
Daniel Veillardf06307e2001-07-03 10:35:50 +000010490 /*
10491 * Extract the old locset, and then evaluate the result of the
10492 * expression for all the element in the locset. use it to grow
10493 * up a new locset.
10494 */
10495 CHECK_TYPE0(XPATH_LOCATIONSET);
10496 obj = valuePop(ctxt);
10497 oldlocset = obj->user;
10498 ctxt->context->node = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010499
Daniel Veillardf06307e2001-07-03 10:35:50 +000010500 if ((oldlocset == NULL) || (oldlocset->locNr == 0)) {
10501 ctxt->context->contextSize = 0;
10502 ctxt->context->proximityPosition = 0;
10503 if (op->ch2 != -1)
10504 total +=
10505 xmlXPathCompOpEval(ctxt,
10506 &comp->steps[op->ch2]);
10507 res = valuePop(ctxt);
10508 if (res != NULL)
10509 xmlXPathFreeObject(res);
10510 valuePush(ctxt, obj);
10511 CHECK_ERROR0;
10512 return (total);
10513 }
10514 newlocset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010515
Daniel Veillardf06307e2001-07-03 10:35:50 +000010516 for (i = 0; i < oldlocset->locNr; i++) {
10517 /*
10518 * Run the evaluation with a node list made of a
10519 * single item in the nodelocset.
10520 */
10521 ctxt->context->node = oldlocset->locTab[i]->user;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010522 ctxt->context->contextSize = oldlocset->locNr;
10523 ctxt->context->proximityPosition = i + 1;
William M. Brackf7eb7942003-12-31 07:59:17 +000010524 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10525 valuePush(ctxt, tmp);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010526
Daniel Veillardf06307e2001-07-03 10:35:50 +000010527 if (op->ch2 != -1)
10528 total +=
10529 xmlXPathCompOpEval(ctxt,
10530 &comp->steps[op->ch2]);
10531 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010532
Daniel Veillardf06307e2001-07-03 10:35:50 +000010533 /*
10534 * The result of the evaluation need to be tested to
10535 * decided whether the filter succeeded or not
10536 */
10537 res = valuePop(ctxt);
10538 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
10539 xmlXPtrLocationSetAdd(newlocset,
10540 xmlXPathObjectCopy
10541 (oldlocset->locTab[i]));
10542 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010543
Daniel Veillardf06307e2001-07-03 10:35:50 +000010544 /*
10545 * Cleanup
10546 */
10547 if (res != NULL)
10548 xmlXPathFreeObject(res);
10549 if (ctxt->value == tmp) {
10550 res = valuePop(ctxt);
10551 xmlXPathFreeObject(res);
10552 }
10553
10554 ctxt->context->node = NULL;
10555 }
10556
10557 /*
10558 * The result is used as the new evaluation locset.
10559 */
10560 xmlXPathFreeObject(obj);
10561 ctxt->context->node = NULL;
10562 ctxt->context->contextSize = -1;
10563 ctxt->context->proximityPosition = -1;
10564 valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset));
10565 ctxt->context->node = oldnode;
10566 return (total);
10567 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010568#endif /* LIBXML_XPTR_ENABLED */
10569
Daniel Veillardf06307e2001-07-03 10:35:50 +000010570 /*
10571 * Extract the old set, and then evaluate the result of the
10572 * expression for all the element in the set. use it to grow
10573 * up a new set.
10574 */
10575 CHECK_TYPE0(XPATH_NODESET);
10576 obj = valuePop(ctxt);
10577 oldset = obj->nodesetval;
Daniel Veillard911f49a2001-04-07 15:39:35 +000010578
Daniel Veillardf06307e2001-07-03 10:35:50 +000010579 oldnode = ctxt->context->node;
10580 ctxt->context->node = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010581
Daniel Veillardf06307e2001-07-03 10:35:50 +000010582 if ((oldset == NULL) || (oldset->nodeNr == 0)) {
10583 ctxt->context->contextSize = 0;
10584 ctxt->context->proximityPosition = 0;
10585 if (op->ch2 != -1)
10586 total +=
10587 xmlXPathCompOpEval(ctxt,
10588 &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010589 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010590 res = valuePop(ctxt);
10591 if (res != NULL)
10592 xmlXPathFreeObject(res);
10593 valuePush(ctxt, obj);
10594 ctxt->context->node = oldnode;
10595 CHECK_ERROR0;
10596 } else {
10597 /*
10598 * Initialize the new set.
10599 */
10600 newset = xmlXPathNodeSetCreate(NULL);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010601
Daniel Veillardf06307e2001-07-03 10:35:50 +000010602 for (i = 0; i < oldset->nodeNr; i++) {
10603 /*
10604 * Run the evaluation with a node list made of
10605 * a single item in the nodeset.
10606 */
10607 ctxt->context->node = oldset->nodeTab[i];
10608 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10609 valuePush(ctxt, tmp);
10610 ctxt->context->contextSize = oldset->nodeNr;
10611 ctxt->context->proximityPosition = i + 1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010612
Daniel Veillardf06307e2001-07-03 10:35:50 +000010613 if (op->ch2 != -1)
10614 total +=
10615 xmlXPathCompOpEval(ctxt,
10616 &comp->steps[op->ch2]);
10617 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010618
Daniel Veillardf06307e2001-07-03 10:35:50 +000010619 /*
William M. Brack08171912003-12-29 02:52:11 +000010620 * The result of the evaluation needs to be tested to
10621 * decide whether the filter succeeded or not
Daniel Veillardf06307e2001-07-03 10:35:50 +000010622 */
10623 res = valuePop(ctxt);
10624 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
10625 xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]);
10626 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010627
Daniel Veillardf06307e2001-07-03 10:35:50 +000010628 /*
10629 * Cleanup
10630 */
10631 if (res != NULL)
10632 xmlXPathFreeObject(res);
10633 if (ctxt->value == tmp) {
10634 res = valuePop(ctxt);
10635 xmlXPathFreeObject(res);
10636 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010637
Daniel Veillardf06307e2001-07-03 10:35:50 +000010638 ctxt->context->node = NULL;
10639 }
10640
10641 /*
10642 * The result is used as the new evaluation set.
10643 */
10644 xmlXPathFreeObject(obj);
10645 ctxt->context->node = NULL;
10646 ctxt->context->contextSize = -1;
10647 ctxt->context->proximityPosition = -1;
10648 valuePush(ctxt, xmlXPathWrapNodeSet(newset));
10649 }
10650 ctxt->context->node = oldnode;
10651 return (total);
10652 }
10653 case XPATH_OP_SORT:
10654 if (op->ch1 != -1)
10655 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010656 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010657 if ((ctxt->value != NULL) &&
10658 (ctxt->value->type == XPATH_NODESET) &&
10659 (ctxt->value->nodesetval != NULL))
10660 xmlXPathNodeSetSort(ctxt->value->nodesetval);
10661 return (total);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010662#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardf06307e2001-07-03 10:35:50 +000010663 case XPATH_OP_RANGETO:{
10664 xmlXPathObjectPtr range;
10665 xmlXPathObjectPtr res, obj;
10666 xmlXPathObjectPtr tmp;
William M. Brack08171912003-12-29 02:52:11 +000010667 xmlLocationSetPtr newlocset = NULL;
10668 xmlLocationSetPtr oldlocset;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010669 xmlNodeSetPtr oldset;
William M. Brack72ee48d2003-12-30 08:30:19 +000010670 int i, j;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010671
Daniel Veillardf06307e2001-07-03 10:35:50 +000010672 if (op->ch1 != -1)
10673 total +=
10674 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
10675 if (op->ch2 == -1)
10676 return (total);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010677
William M. Brack08171912003-12-29 02:52:11 +000010678 if (ctxt->value->type == XPATH_LOCATIONSET) {
10679 /*
10680 * Extract the old locset, and then evaluate the result of the
10681 * expression for all the element in the locset. use it to grow
10682 * up a new locset.
10683 */
10684 CHECK_TYPE0(XPATH_LOCATIONSET);
10685 obj = valuePop(ctxt);
10686 oldlocset = obj->user;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010687
William M. Brack08171912003-12-29 02:52:11 +000010688 if ((oldlocset == NULL) || (oldlocset->locNr == 0)) {
William M. Brack72ee48d2003-12-30 08:30:19 +000010689 ctxt->context->node = NULL;
William M. Brack08171912003-12-29 02:52:11 +000010690 ctxt->context->contextSize = 0;
10691 ctxt->context->proximityPosition = 0;
10692 total += xmlXPathCompOpEval(ctxt,&comp->steps[op->ch2]);
10693 res = valuePop(ctxt);
10694 if (res != NULL)
10695 xmlXPathFreeObject(res);
10696 valuePush(ctxt, obj);
10697 CHECK_ERROR0;
10698 return (total);
10699 }
10700 newlocset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010701
William M. Brack08171912003-12-29 02:52:11 +000010702 for (i = 0; i < oldlocset->locNr; i++) {
Daniel Veillardf06307e2001-07-03 10:35:50 +000010703 /*
William M. Brack08171912003-12-29 02:52:11 +000010704 * Run the evaluation with a node list made of a
10705 * single item in the nodelocset.
Daniel Veillardf06307e2001-07-03 10:35:50 +000010706 */
William M. Brackf7eb7942003-12-31 07:59:17 +000010707 ctxt->context->node = oldlocset->locTab[i]->user;
10708 ctxt->context->contextSize = oldlocset->locNr;
10709 ctxt->context->proximityPosition = i + 1;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010710 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10711 valuePush(ctxt, tmp);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010712
Daniel Veillardf06307e2001-07-03 10:35:50 +000010713 if (op->ch2 != -1)
10714 total +=
10715 xmlXPathCompOpEval(ctxt,
10716 &comp->steps[op->ch2]);
10717 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010718
Daniel Veillardf06307e2001-07-03 10:35:50 +000010719 res = valuePop(ctxt);
William M. Brack72ee48d2003-12-30 08:30:19 +000010720 if (res->type == XPATH_LOCATIONSET) {
10721 xmlLocationSetPtr rloc =
10722 (xmlLocationSetPtr)res->user;
10723 for (j=0; j<rloc->locNr; j++) {
10724 range = xmlXPtrNewRange(
10725 oldlocset->locTab[i]->user,
10726 oldlocset->locTab[i]->index,
10727 rloc->locTab[j]->user2,
10728 rloc->locTab[j]->index2);
10729 if (range != NULL) {
10730 xmlXPtrLocationSetAdd(newlocset, range);
10731 }
10732 }
10733 } else {
10734 range = xmlXPtrNewRangeNodeObject(
10735 (xmlNodePtr)oldlocset->locTab[i]->user, res);
10736 if (range != NULL) {
10737 xmlXPtrLocationSetAdd(newlocset,range);
10738 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010739 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010740
Daniel Veillardf06307e2001-07-03 10:35:50 +000010741 /*
10742 * Cleanup
10743 */
10744 if (res != NULL)
10745 xmlXPathFreeObject(res);
10746 if (ctxt->value == tmp) {
10747 res = valuePop(ctxt);
10748 xmlXPathFreeObject(res);
10749 }
10750
10751 ctxt->context->node = NULL;
10752 }
William M. Brack72ee48d2003-12-30 08:30:19 +000010753 } else { /* Not a location set */
William M. Brack08171912003-12-29 02:52:11 +000010754 CHECK_TYPE0(XPATH_NODESET);
10755 obj = valuePop(ctxt);
10756 oldset = obj->nodesetval;
10757 ctxt->context->node = NULL;
10758
10759 newlocset = xmlXPtrLocationSetCreate(NULL);
10760
10761 if (oldset != NULL) {
10762 for (i = 0; i < oldset->nodeNr; i++) {
10763 /*
10764 * Run the evaluation with a node list made of a single item
10765 * in the nodeset.
10766 */
10767 ctxt->context->node = oldset->nodeTab[i];
10768 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10769 valuePush(ctxt, tmp);
10770
10771 if (op->ch2 != -1)
10772 total +=
10773 xmlXPathCompOpEval(ctxt,
10774 &comp->steps[op->ch2]);
10775 CHECK_ERROR0;
10776
William M. Brack08171912003-12-29 02:52:11 +000010777 res = valuePop(ctxt);
10778 range =
10779 xmlXPtrNewRangeNodeObject(oldset->nodeTab[i],
10780 res);
10781 if (range != NULL) {
10782 xmlXPtrLocationSetAdd(newlocset, range);
10783 }
10784
10785 /*
10786 * Cleanup
10787 */
10788 if (res != NULL)
10789 xmlXPathFreeObject(res);
10790 if (ctxt->value == tmp) {
10791 res = valuePop(ctxt);
10792 xmlXPathFreeObject(res);
10793 }
10794
10795 ctxt->context->node = NULL;
10796 }
10797 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010798 }
10799
10800 /*
10801 * The result is used as the new evaluation set.
10802 */
10803 xmlXPathFreeObject(obj);
10804 ctxt->context->node = NULL;
10805 ctxt->context->contextSize = -1;
10806 ctxt->context->proximityPosition = -1;
William M. Brack08171912003-12-29 02:52:11 +000010807 valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset));
Daniel Veillardf06307e2001-07-03 10:35:50 +000010808 return (total);
10809 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010810#endif /* LIBXML_XPTR_ENABLED */
10811 }
10812 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf06307e2001-07-03 10:35:50 +000010813 "XPath: unknown precompiled operation %d\n", op->op);
10814 return (total);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010815}
10816
10817/**
10818 * xmlXPathRunEval:
10819 * @ctxt: the XPath parser context with the compiled expression
10820 *
10821 * Evaluate the Precompiled XPath expression in the given context.
10822 */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000010823static void
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010824xmlXPathRunEval(xmlXPathParserContextPtr ctxt) {
10825 xmlXPathCompExprPtr comp;
10826
10827 if ((ctxt == NULL) || (ctxt->comp == NULL))
10828 return;
10829
10830 if (ctxt->valueTab == NULL) {
10831 /* Allocate the value stack */
10832 ctxt->valueTab = (xmlXPathObjectPtr *)
10833 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
10834 if (ctxt->valueTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +000010835 xmlXPathPErrMemory(ctxt, "creating evaluation context\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010836 xmlFree(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010837 }
10838 ctxt->valueNr = 0;
10839 ctxt->valueMax = 10;
10840 ctxt->value = NULL;
10841 }
10842 comp = ctxt->comp;
Aleksey Sanin29b6f762002-05-05 06:59:57 +000010843 if(comp->last < 0) {
10844 xmlGenericError(xmlGenericErrorContext,
10845 "xmlXPathRunEval: last is less than zero\n");
10846 return;
10847 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010848 xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]);
10849}
10850
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010851/************************************************************************
10852 * *
10853 * Public interfaces *
10854 * *
10855 ************************************************************************/
10856
10857/**
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000010858 * xmlXPathEvalPredicate:
10859 * @ctxt: the XPath context
10860 * @res: the Predicate Expression evaluation result
10861 *
10862 * Evaluate a predicate result for the current node.
10863 * A PredicateExpr is evaluated by evaluating the Expr and converting
10864 * the result to a boolean. If the result is a number, the result will
10865 * be converted to true if the number is equal to the position of the
10866 * context node in the context node list (as returned by the position
10867 * function) and will be converted to false otherwise; if the result
10868 * is not a number, then the result will be converted as if by a call
10869 * to the boolean function.
10870 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010871 * Returns 1 if predicate is true, 0 otherwise
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000010872 */
10873int
10874xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) {
10875 if (res == NULL) return(0);
10876 switch (res->type) {
10877 case XPATH_BOOLEAN:
10878 return(res->boolval);
10879 case XPATH_NUMBER:
10880 return(res->floatval == ctxt->proximityPosition);
10881 case XPATH_NODESET:
10882 case XPATH_XSLT_TREE:
Daniel Veillardd8df6c02001-04-05 16:54:14 +000010883 if (res->nodesetval == NULL)
10884 return(0);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000010885 return(res->nodesetval->nodeNr != 0);
10886 case XPATH_STRING:
10887 return((res->stringval != NULL) &&
10888 (xmlStrlen(res->stringval) != 0));
10889 default:
10890 STRANGE
10891 }
10892 return(0);
10893}
10894
10895/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010896 * xmlXPathEvaluatePredicateResult:
10897 * @ctxt: the XPath Parser context
10898 * @res: the Predicate Expression evaluation result
10899 *
10900 * Evaluate a predicate result for the current node.
10901 * A PredicateExpr is evaluated by evaluating the Expr and converting
10902 * the result to a boolean. If the result is a number, the result will
10903 * be converted to true if the number is equal to the position of the
10904 * context node in the context node list (as returned by the position
10905 * function) and will be converted to false otherwise; if the result
10906 * is not a number, then the result will be converted as if by a call
10907 * to the boolean function.
10908 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010909 * Returns 1 if predicate is true, 0 otherwise
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010910 */
10911int
10912xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt,
10913 xmlXPathObjectPtr res) {
10914 if (res == NULL) return(0);
10915 switch (res->type) {
10916 case XPATH_BOOLEAN:
10917 return(res->boolval);
10918 case XPATH_NUMBER:
10919 return(res->floatval == ctxt->context->proximityPosition);
10920 case XPATH_NODESET:
10921 case XPATH_XSLT_TREE:
Daniel Veillard73639a72001-04-10 14:31:39 +000010922 if (res->nodesetval == NULL)
Daniel Veillard911f49a2001-04-07 15:39:35 +000010923 return(0);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010924 return(res->nodesetval->nodeNr != 0);
10925 case XPATH_STRING:
10926 return((res->stringval != NULL) &&
10927 (xmlStrlen(res->stringval) != 0));
William M. Brack08171912003-12-29 02:52:11 +000010928#ifdef LIBXML_XPTR_ENABLED
10929 case XPATH_LOCATIONSET:{
10930 xmlLocationSetPtr ptr = res->user;
10931 if (ptr == NULL)
10932 return(0);
10933 return (ptr->locNr != 0);
10934 }
10935#endif
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010936 default:
10937 STRANGE
10938 }
10939 return(0);
10940}
10941
10942/**
Daniel Veillard4773df22004-01-23 13:15:13 +000010943 * xmlXPathCtxtCompile:
10944 * @ctxt: an XPath context
10945 * @str: the XPath expression
10946 *
10947 * Compile an XPath expression
10948 *
10949 * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL.
10950 * the caller has to free the object.
10951 */
10952xmlXPathCompExprPtr
10953xmlXPathCtxtCompile(xmlXPathContextPtr ctxt, const xmlChar *str) {
10954 xmlXPathParserContextPtr pctxt;
10955 xmlXPathCompExprPtr comp;
10956
10957 xmlXPathInit();
10958
10959 pctxt = xmlXPathNewParserContext(str, ctxt);
10960 xmlXPathCompileExpr(pctxt);
10961
10962 if( pctxt->error != XPATH_EXPRESSION_OK )
10963 {
10964 xmlXPathFreeParserContext(pctxt);
10965 return (0);
10966 }
10967
10968 if (*pctxt->cur != 0) {
10969 /*
10970 * aleksey: in some cases this line prints *second* error message
10971 * (see bug #78858) and probably this should be fixed.
10972 * However, we are not sure that all error messages are printed
10973 * out in other places. It's not critical so we leave it as-is for now
10974 */
10975 xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
10976 comp = NULL;
10977 } else {
10978 comp = pctxt->comp;
10979 pctxt->comp = NULL;
10980 }
10981 xmlXPathFreeParserContext(pctxt);
10982 if (comp != NULL) {
10983 comp->expr = xmlStrdup(str);
10984#ifdef DEBUG_EVAL_COUNTS
10985 comp->string = xmlStrdup(str);
10986 comp->nb = 0;
10987#endif
10988 }
10989 return(comp);
10990}
10991
10992/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010993 * xmlXPathCompile:
10994 * @str: the XPath expression
10995 *
10996 * Compile an XPath expression
10997 *
Daniel Veillard591b4be2003-02-09 23:33:36 +000010998 * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL.
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010999 * the caller has to free the object.
11000 */
11001xmlXPathCompExprPtr
11002xmlXPathCompile(const xmlChar *str) {
Daniel Veillard4773df22004-01-23 13:15:13 +000011003 return(xmlXPathCtxtCompile(NULL, str));
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011004}
11005
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011006/**
11007 * xmlXPathCompiledEval:
11008 * @comp: the compiled XPath expression
Owen Taylor3473f882001-02-23 17:55:21 +000011009 * @ctx: the XPath context
11010 *
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011011 * Evaluate the Precompiled XPath expression in the given context.
Owen Taylor3473f882001-02-23 17:55:21 +000011012 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011013 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Owen Taylor3473f882001-02-23 17:55:21 +000011014 * the caller has to free the object.
11015 */
11016xmlXPathObjectPtr
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011017xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) {
Owen Taylor3473f882001-02-23 17:55:21 +000011018 xmlXPathParserContextPtr ctxt;
11019 xmlXPathObjectPtr res, tmp, init = NULL;
11020 int stack = 0;
Daniel Veillard81463942001-10-16 12:34:39 +000011021#ifndef LIBXML_THREAD_ENABLED
11022 static int reentance = 0;
11023#endif
Owen Taylor3473f882001-02-23 17:55:21 +000011024
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011025 if ((comp == NULL) || (ctx == NULL))
11026 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011027 xmlXPathInit();
11028
11029 CHECK_CONTEXT(ctx)
11030
Daniel Veillard81463942001-10-16 12:34:39 +000011031#ifndef LIBXML_THREAD_ENABLED
11032 reentance++;
11033 if (reentance > 1)
11034 xmlXPathDisableOptimizer = 1;
11035#endif
11036
Daniel Veillardf06307e2001-07-03 10:35:50 +000011037#ifdef DEBUG_EVAL_COUNTS
11038 comp->nb++;
11039 if ((comp->string != NULL) && (comp->nb > 100)) {
11040 fprintf(stderr, "100 x %s\n", comp->string);
11041 comp->nb = 0;
11042 }
11043#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011044 ctxt = xmlXPathCompParserContext(comp, ctx);
11045 xmlXPathRunEval(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011046
11047 if (ctxt->value == NULL) {
11048 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011049 "xmlXPathCompiledEval: evaluation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +000011050 res = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000011051 } else {
11052 res = valuePop(ctxt);
11053 }
11054
Daniel Veillardf06307e2001-07-03 10:35:50 +000011055
Owen Taylor3473f882001-02-23 17:55:21 +000011056 do {
11057 tmp = valuePop(ctxt);
11058 if (tmp != NULL) {
11059 if (tmp != init)
11060 stack++;
11061 xmlXPathFreeObject(tmp);
11062 }
11063 } while (tmp != NULL);
11064 if ((stack != 0) && (res != NULL)) {
11065 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011066 "xmlXPathCompiledEval: %d object left on the stack\n",
Owen Taylor3473f882001-02-23 17:55:21 +000011067 stack);
11068 }
11069 if (ctxt->error != XPATH_EXPRESSION_OK) {
11070 xmlXPathFreeObject(res);
11071 res = NULL;
11072 }
11073
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011074
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011075 ctxt->comp = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011076 xmlXPathFreeParserContext(ctxt);
Daniel Veillard81463942001-10-16 12:34:39 +000011077#ifndef LIBXML_THREAD_ENABLED
11078 reentance--;
11079#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011080 return(res);
11081}
11082
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011083/**
11084 * xmlXPathEvalExpr:
11085 * @ctxt: the XPath Parser context
11086 *
11087 * Parse and evaluate an XPath expression in the given context,
11088 * then push the result on the context stack
11089 */
11090void
11091xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) {
11092 xmlXPathCompileExpr(ctxt);
Aleksey Sanin50fe8b12002-05-07 16:21:36 +000011093 CHECK_ERROR;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011094 xmlXPathRunEval(ctxt);
11095}
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011096
11097/**
11098 * xmlXPathEval:
11099 * @str: the XPath expression
11100 * @ctx: the XPath context
11101 *
11102 * Evaluate the XPath Location Path in the given context.
11103 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011104 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011105 * the caller has to free the object.
11106 */
11107xmlXPathObjectPtr
11108xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) {
11109 xmlXPathParserContextPtr ctxt;
11110 xmlXPathObjectPtr res, tmp, init = NULL;
11111 int stack = 0;
11112
11113 xmlXPathInit();
11114
11115 CHECK_CONTEXT(ctx)
11116
11117 ctxt = xmlXPathNewParserContext(str, ctx);
11118 xmlXPathEvalExpr(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011119
11120 if (ctxt->value == NULL) {
11121 xmlGenericError(xmlGenericErrorContext,
11122 "xmlXPathEval: evaluation failed\n");
11123 res = NULL;
11124 } else if (*ctxt->cur != 0) {
11125 xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
11126 res = NULL;
11127 } else {
11128 res = valuePop(ctxt);
11129 }
11130
11131 do {
11132 tmp = valuePop(ctxt);
11133 if (tmp != NULL) {
11134 if (tmp != init)
11135 stack++;
11136 xmlXPathFreeObject(tmp);
11137 }
11138 } while (tmp != NULL);
11139 if ((stack != 0) && (res != NULL)) {
11140 xmlGenericError(xmlGenericErrorContext,
11141 "xmlXPathEval: %d object left on the stack\n",
11142 stack);
11143 }
11144 if (ctxt->error != XPATH_EXPRESSION_OK) {
11145 xmlXPathFreeObject(res);
11146 res = NULL;
11147 }
11148
Owen Taylor3473f882001-02-23 17:55:21 +000011149 xmlXPathFreeParserContext(ctxt);
11150 return(res);
11151}
11152
11153/**
11154 * xmlXPathEvalExpression:
11155 * @str: the XPath expression
11156 * @ctxt: the XPath context
11157 *
11158 * Evaluate the XPath expression in the given context.
11159 *
11160 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
11161 * the caller has to free the object.
11162 */
11163xmlXPathObjectPtr
11164xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) {
11165 xmlXPathParserContextPtr pctxt;
11166 xmlXPathObjectPtr res, tmp;
11167 int stack = 0;
11168
11169 xmlXPathInit();
11170
11171 CHECK_CONTEXT(ctxt)
11172
11173 pctxt = xmlXPathNewParserContext(str, ctxt);
11174 xmlXPathEvalExpr(pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011175
11176 if (*pctxt->cur != 0) {
11177 xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
11178 res = NULL;
11179 } else {
11180 res = valuePop(pctxt);
11181 }
11182 do {
11183 tmp = valuePop(pctxt);
11184 if (tmp != NULL) {
11185 xmlXPathFreeObject(tmp);
11186 stack++;
11187 }
11188 } while (tmp != NULL);
11189 if ((stack != 0) && (res != NULL)) {
11190 xmlGenericError(xmlGenericErrorContext,
11191 "xmlXPathEvalExpression: %d object left on the stack\n",
11192 stack);
11193 }
11194 xmlXPathFreeParserContext(pctxt);
11195 return(res);
11196}
11197
Daniel Veillard42766c02002-08-22 20:52:17 +000011198/************************************************************************
11199 * *
11200 * Extra functions not pertaining to the XPath spec *
11201 * *
11202 ************************************************************************/
11203/**
11204 * xmlXPathEscapeUriFunction:
11205 * @ctxt: the XPath Parser context
11206 * @nargs: the number of arguments
11207 *
11208 * Implement the escape-uri() XPath function
11209 * string escape-uri(string $str, bool $escape-reserved)
11210 *
11211 * This function applies the URI escaping rules defined in section 2 of [RFC
11212 * 2396] to the string supplied as $uri-part, which typically represents all
11213 * or part of a URI. The effect of the function is to replace any special
11214 * character in the string by an escape sequence of the form %xx%yy...,
11215 * where xxyy... is the hexadecimal representation of the octets used to
11216 * represent the character in UTF-8.
11217 *
11218 * The set of characters that are escaped depends on the setting of the
11219 * boolean argument $escape-reserved.
11220 *
11221 * If $escape-reserved is true, all characters are escaped other than lower
11222 * case letters a-z, upper case letters A-Z, digits 0-9, and the characters
11223 * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!"
11224 * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only
11225 * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and
11226 * A-F).
11227 *
11228 * If $escape-reserved is false, the behavior differs in that characters
11229 * referred to in [RFC 2396] as reserved characters are not escaped. These
11230 * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",".
11231 *
11232 * [RFC 2396] does not define whether escaped URIs should use lower case or
11233 * upper case for hexadecimal digits. To ensure that escaped URIs can be
11234 * compared using string comparison functions, this function must always use
11235 * the upper-case letters A-F.
11236 *
11237 * Generally, $escape-reserved should be set to true when escaping a string
11238 * that is to form a single part of a URI, and to false when escaping an
11239 * entire URI or URI reference.
11240 *
11241 * In the case of non-ascii characters, the string is encoded according to
11242 * utf-8 and then converted according to RFC 2396.
11243 *
11244 * Examples
11245 * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true())
11246 * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean"
11247 * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false())
11248 * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean"
11249 *
11250 */
Daniel Veillard118aed72002-09-24 14:13:13 +000011251static void
Daniel Veillard42766c02002-08-22 20:52:17 +000011252xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) {
11253 xmlXPathObjectPtr str;
11254 int escape_reserved;
11255 xmlBufferPtr target;
11256 xmlChar *cptr;
11257 xmlChar escape[4];
11258
11259 CHECK_ARITY(2);
11260
11261 escape_reserved = xmlXPathPopBoolean(ctxt);
11262
11263 CAST_TO_STRING;
11264 str = valuePop(ctxt);
11265
11266 target = xmlBufferCreate();
11267
11268 escape[0] = '%';
11269 escape[3] = 0;
11270
11271 if (target) {
11272 for (cptr = str->stringval; *cptr; cptr++) {
11273 if ((*cptr >= 'A' && *cptr <= 'Z') ||
11274 (*cptr >= 'a' && *cptr <= 'z') ||
11275 (*cptr >= '0' && *cptr <= '9') ||
11276 *cptr == '-' || *cptr == '_' || *cptr == '.' ||
11277 *cptr == '!' || *cptr == '~' || *cptr == '*' ||
11278 *cptr == '\''|| *cptr == '(' || *cptr == ')' ||
11279 (*cptr == '%' &&
11280 ((cptr[1] >= 'A' && cptr[1] <= 'F') ||
11281 (cptr[1] >= 'a' && cptr[1] <= 'f') ||
11282 (cptr[1] >= '0' && cptr[1] <= '9')) &&
11283 ((cptr[2] >= 'A' && cptr[2] <= 'F') ||
11284 (cptr[2] >= 'a' && cptr[2] <= 'f') ||
11285 (cptr[2] >= '0' && cptr[2] <= '9'))) ||
11286 (!escape_reserved &&
11287 (*cptr == ';' || *cptr == '/' || *cptr == '?' ||
11288 *cptr == ':' || *cptr == '@' || *cptr == '&' ||
11289 *cptr == '=' || *cptr == '+' || *cptr == '$' ||
11290 *cptr == ','))) {
11291 xmlBufferAdd(target, cptr, 1);
11292 } else {
11293 if ((*cptr >> 4) < 10)
11294 escape[1] = '0' + (*cptr >> 4);
11295 else
11296 escape[1] = 'A' - 10 + (*cptr >> 4);
11297 if ((*cptr & 0xF) < 10)
11298 escape[2] = '0' + (*cptr & 0xF);
11299 else
11300 escape[2] = 'A' - 10 + (*cptr & 0xF);
11301
11302 xmlBufferAdd(target, &escape[0], 3);
11303 }
11304 }
11305 }
11306 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
11307 xmlBufferFree(target);
11308 xmlXPathFreeObject(str);
11309}
11310
Owen Taylor3473f882001-02-23 17:55:21 +000011311/**
11312 * xmlXPathRegisterAllFunctions:
11313 * @ctxt: the XPath context
11314 *
11315 * Registers all default XPath functions in this context
11316 */
11317void
11318xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt)
11319{
11320 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean",
11321 xmlXPathBooleanFunction);
11322 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling",
11323 xmlXPathCeilingFunction);
11324 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count",
11325 xmlXPathCountFunction);
11326 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat",
11327 xmlXPathConcatFunction);
11328 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains",
11329 xmlXPathContainsFunction);
11330 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id",
11331 xmlXPathIdFunction);
11332 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false",
11333 xmlXPathFalseFunction);
11334 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor",
11335 xmlXPathFloorFunction);
11336 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last",
11337 xmlXPathLastFunction);
11338 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang",
11339 xmlXPathLangFunction);
11340 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name",
11341 xmlXPathLocalNameFunction);
11342 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not",
11343 xmlXPathNotFunction);
11344 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name",
11345 xmlXPathNameFunction);
11346 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri",
11347 xmlXPathNamespaceURIFunction);
11348 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space",
11349 xmlXPathNormalizeFunction);
11350 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number",
11351 xmlXPathNumberFunction);
11352 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position",
11353 xmlXPathPositionFunction);
11354 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round",
11355 xmlXPathRoundFunction);
11356 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string",
11357 xmlXPathStringFunction);
11358 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length",
11359 xmlXPathStringLengthFunction);
11360 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with",
11361 xmlXPathStartsWithFunction);
11362 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring",
11363 xmlXPathSubstringFunction);
11364 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before",
11365 xmlXPathSubstringBeforeFunction);
11366 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after",
11367 xmlXPathSubstringAfterFunction);
11368 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum",
11369 xmlXPathSumFunction);
11370 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true",
11371 xmlXPathTrueFunction);
11372 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate",
11373 xmlXPathTranslateFunction);
Daniel Veillard42766c02002-08-22 20:52:17 +000011374
11375 xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri",
11376 (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions",
11377 xmlXPathEscapeUriFunction);
Owen Taylor3473f882001-02-23 17:55:21 +000011378}
11379
11380#endif /* LIBXML_XPATH_ENABLED */