blob: 1be6f316aeaf754cf3c307eb35dd7423a073efa4 [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
William M. Brackd1757ab2004-10-02 22:07:48 +000060/*
61 * TODO:
62 * There are a few spots where some tests are done which depend upon ascii
63 * data. These should be enhanced for full UTF8 support (see particularly
64 * any use of the macros IS_ASCII_CHARACTER and IS_ASCII_DIGIT)
65 */
66
Daniel Veillard4432df22003-09-28 18:58:27 +000067#if defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XPATH_ENABLED)
Daniel Veillard9e7160d2001-03-18 23:17:47 +000068/************************************************************************
69 * *
70 * Floating point stuff *
71 * *
72 ************************************************************************/
73
Daniel Veillardc0631a62001-09-20 13:56:06 +000074#ifndef TRIO_REPLACE_STDIO
Daniel Veillardcda96922001-08-21 10:56:31 +000075#define TRIO_PUBLIC static
Daniel Veillardc0631a62001-09-20 13:56:06 +000076#endif
Daniel Veillardcda96922001-08-21 10:56:31 +000077#include "trionan.c"
78
Owen Taylor3473f882001-02-23 17:55:21 +000079/*
Owen Taylor3473f882001-02-23 17:55:21 +000080 * The lack of portability of this section of the libc is annoying !
81 */
82double xmlXPathNAN = 0;
83double xmlXPathPINF = 1;
84double xmlXPathNINF = -1;
Daniel Veillard5fc1f082002-03-27 09:05:40 +000085double xmlXPathNZERO = 0;
Daniel Veillard20ee8c02001-10-05 09:18:14 +000086static int xmlXPathInitialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +000087
Owen Taylor3473f882001-02-23 17:55:21 +000088/**
89 * xmlXPathInit:
90 *
91 * Initialize the XPath environment
92 */
93void
94xmlXPathInit(void) {
Daniel Veillard20ee8c02001-10-05 09:18:14 +000095 if (xmlXPathInitialized) return;
Owen Taylor3473f882001-02-23 17:55:21 +000096
Bjorn Reese45029602001-08-21 09:23:53 +000097 xmlXPathPINF = trio_pinf();
98 xmlXPathNINF = trio_ninf();
99 xmlXPathNAN = trio_nan();
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000100 xmlXPathNZERO = trio_nzero();
Owen Taylor3473f882001-02-23 17:55:21 +0000101
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000102 xmlXPathInitialized = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000103}
104
Daniel Veillardcda96922001-08-21 10:56:31 +0000105/**
106 * xmlXPathIsNaN:
107 * @val: a double value
108 *
109 * Provides a portable isnan() function to detect whether a double
110 * is a NotaNumber. Based on trio code
111 * http://sourceforge.net/projects/ctrio/
112 *
113 * Returns 1 if the value is a NaN, 0 otherwise
114 */
115int
116xmlXPathIsNaN(double val) {
117 return(trio_isnan(val));
118}
119
120/**
121 * xmlXPathIsInf:
122 * @val: a double value
123 *
124 * Provides a portable isinf() function to detect whether a double
125 * is a +Infinite or -Infinite. Based on trio code
126 * http://sourceforge.net/projects/ctrio/
127 *
128 * Returns 1 vi the value is +Infinite, -1 if -Infinite, 0 otherwise
129 */
130int
131xmlXPathIsInf(double val) {
132 return(trio_isinf(val));
133}
134
Daniel Veillard4432df22003-09-28 18:58:27 +0000135#endif /* SCHEMAS or XPATH */
136#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000137/**
138 * xmlXPathGetSign:
139 * @val: a double value
140 *
141 * Provides a portable function to detect the sign of a double
142 * Modified from trio code
143 * http://sourceforge.net/projects/ctrio/
144 *
145 * Returns 1 if the value is Negative, 0 if positive
146 */
Daniel Veillard21458c82002-03-27 16:12:22 +0000147static int
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000148xmlXPathGetSign(double val) {
Daniel Veillard21458c82002-03-27 16:12:22 +0000149 return(trio_signbit(val));
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000150}
151
152
Daniel Veillardd9d32ae2003-07-05 20:32:43 +0000153/*
154 * TODO: when compatibility allows remove all "fake node libxslt" strings
155 * the test should just be name[0] = ' '
156 */
157/* #define DEBUG */
158/* #define DEBUG_STEP */
159/* #define DEBUG_STEP_NTH */
160/* #define DEBUG_EXPR */
161/* #define DEBUG_EVAL_COUNTS */
162
163static xmlNs xmlXPathXMLNamespaceStruct = {
164 NULL,
165 XML_NAMESPACE_DECL,
166 XML_XML_NAMESPACE,
167 BAD_CAST "xml",
168 NULL
169};
170static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct;
171#ifndef LIBXML_THREAD_ENABLED
172/*
173 * Optimizer is disabled only when threaded apps are detected while
174 * the library ain't compiled for thread safety.
175 */
176static int xmlXPathDisableOptimizer = 0;
177#endif
178
Owen Taylor3473f882001-02-23 17:55:21 +0000179/************************************************************************
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000180 * *
181 * Error handling routines *
182 * *
183 ************************************************************************/
184
William M. Brack08171912003-12-29 02:52:11 +0000185/*
186 * The array xmlXPathErrorMessages corresponds to the enum xmlXPathError
187 */
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000188static const char *xmlXPathErrorMessages[] = {
189 "Ok\n",
190 "Number encoding\n",
191 "Unfinished literal\n",
192 "Start of literal\n",
193 "Expected $ for variable reference\n",
194 "Undefined variable\n",
195 "Invalid predicate\n",
196 "Invalid expression\n",
197 "Missing closing curly brace\n",
198 "Unregistered function\n",
199 "Invalid operand\n",
200 "Invalid type\n",
201 "Invalid number of arguments\n",
202 "Invalid context size\n",
203 "Invalid context position\n",
204 "Memory allocation error\n",
205 "Syntax error\n",
206 "Resource error\n",
207 "Sub resource error\n",
208 "Undefined namespace prefix\n",
209 "Encoding error\n",
210 "Char out of XML range\n"
211};
212
213
214/**
215 * xmlXPathErrMemory:
216 * @ctxt: an XPath context
217 * @extra: extra informations
218 *
219 * Handle a redefinition of attribute error
220 */
221static void
222xmlXPathErrMemory(xmlXPathContextPtr ctxt, const char *extra)
223{
224 if (ctxt != NULL) {
225 if (extra) {
226 xmlChar buf[200];
227
228 xmlStrPrintf(buf, 200,
229 BAD_CAST "Memory allocation failed : %s\n",
230 extra);
231 ctxt->lastError.message = (char *) xmlStrdup(buf);
232 } else {
233 ctxt->lastError.message = (char *)
234 xmlStrdup(BAD_CAST "Memory allocation failed\n");
235 }
236 ctxt->lastError.domain = XML_FROM_XPATH;
237 ctxt->lastError.code = XML_ERR_NO_MEMORY;
238 if (ctxt->error != NULL)
239 ctxt->error(ctxt->userData, &ctxt->lastError);
240 } else {
241 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000242 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000243 NULL, NULL, XML_FROM_XPATH,
244 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0,
245 extra, NULL, NULL, 0, 0,
246 "Memory allocation failed : %s\n", extra);
247 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000248 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000249 NULL, NULL, XML_FROM_XPATH,
250 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0,
251 NULL, NULL, NULL, 0, 0,
252 "Memory allocation failed\n");
253 }
254}
255
256/**
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000257 * xmlXPathPErrMemory:
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000258 * @ctxt: an XPath parser context
259 * @extra: extra informations
260 *
261 * Handle a redefinition of attribute error
262 */
263static void
264xmlXPathPErrMemory(xmlXPathParserContextPtr ctxt, const char *extra)
265{
266 ctxt->error = XPATH_MEMORY_ERROR;
267 if (ctxt == NULL)
268 xmlXPathErrMemory(NULL, extra);
269 else
270 xmlXPathErrMemory(ctxt->context, extra);
271}
272
273/**
274 * xmlXPathErr:
275 * @ctxt: a XPath parser context
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000276 * @error: the error code
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000277 *
278 * Handle a Relax NG Parsing error
279 */
280void
281xmlXPathErr(xmlXPathParserContextPtr ctxt, int error)
282{
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000283 if (ctxt == NULL) {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000284 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000285 NULL, NULL, XML_FROM_XPATH,
286 error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
287 XML_ERR_ERROR, NULL, 0,
288 NULL, NULL, NULL, 0, 0,
289 xmlXPathErrorMessages[error]);
290 return;
291 }
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000292 ctxt->error = error;
293 if (ctxt->context == NULL) {
294 __xmlRaiseError(NULL, NULL, NULL,
295 NULL, NULL, XML_FROM_XPATH,
296 error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
297 XML_ERR_ERROR, NULL, 0,
298 (const char *) ctxt->base, NULL, NULL,
299 ctxt->cur - ctxt->base, 0,
300 xmlXPathErrorMessages[error]);
301 return;
302 }
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000303 ctxt->context->lastError.domain = XML_FROM_XPATH;
304 ctxt->context->lastError.code = error + XML_XPATH_EXPRESSION_OK -
305 XPATH_EXPRESSION_OK;
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000306 ctxt->context->lastError.level = XML_ERR_ERROR;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000307 ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base);
308 ctxt->context->lastError.int1 = ctxt->cur - ctxt->base;
309 ctxt->context->lastError.node = ctxt->context->debugNode;
310 if (ctxt->context->error != NULL) {
311 ctxt->context->error(ctxt->context->userData,
312 &ctxt->context->lastError);
313 } else {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000314 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000315 NULL, ctxt->context->debugNode, XML_FROM_XPATH,
316 error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
317 XML_ERR_ERROR, NULL, 0,
318 (const char *) ctxt->base, NULL, NULL,
319 ctxt->cur - ctxt->base, 0,
320 xmlXPathErrorMessages[error]);
321 }
322
323}
324
325/**
326 * xmlXPatherror:
327 * @ctxt: the XPath Parser context
328 * @file: the file name
329 * @line: the line number
330 * @no: the error number
331 *
332 * Formats an error message.
333 */
334void
335xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file ATTRIBUTE_UNUSED,
336 int line ATTRIBUTE_UNUSED, int no) {
337 xmlXPathErr(ctxt, no);
338}
339
340
341/************************************************************************
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000342 * *
343 * Parser Types *
344 * *
345 ************************************************************************/
346
347/*
348 * Types are private:
349 */
350
351typedef enum {
352 XPATH_OP_END=0,
353 XPATH_OP_AND,
354 XPATH_OP_OR,
355 XPATH_OP_EQUAL,
356 XPATH_OP_CMP,
357 XPATH_OP_PLUS,
358 XPATH_OP_MULT,
359 XPATH_OP_UNION,
360 XPATH_OP_ROOT,
361 XPATH_OP_NODE,
362 XPATH_OP_RESET,
363 XPATH_OP_COLLECT,
364 XPATH_OP_VALUE,
365 XPATH_OP_VARIABLE,
366 XPATH_OP_FUNCTION,
367 XPATH_OP_ARG,
368 XPATH_OP_PREDICATE,
Daniel Veillardd8df6c02001-04-05 16:54:14 +0000369 XPATH_OP_FILTER,
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000370 XPATH_OP_SORT
371#ifdef LIBXML_XPTR_ENABLED
372 ,XPATH_OP_RANGETO
373#endif
374} xmlXPathOp;
375
376typedef enum {
377 AXIS_ANCESTOR = 1,
378 AXIS_ANCESTOR_OR_SELF,
379 AXIS_ATTRIBUTE,
380 AXIS_CHILD,
381 AXIS_DESCENDANT,
382 AXIS_DESCENDANT_OR_SELF,
383 AXIS_FOLLOWING,
384 AXIS_FOLLOWING_SIBLING,
385 AXIS_NAMESPACE,
386 AXIS_PARENT,
387 AXIS_PRECEDING,
388 AXIS_PRECEDING_SIBLING,
389 AXIS_SELF
390} xmlXPathAxisVal;
391
392typedef enum {
393 NODE_TEST_NONE = 0,
394 NODE_TEST_TYPE = 1,
395 NODE_TEST_PI = 2,
396 NODE_TEST_ALL = 3,
397 NODE_TEST_NS = 4,
398 NODE_TEST_NAME = 5
399} xmlXPathTestVal;
400
401typedef enum {
402 NODE_TYPE_NODE = 0,
403 NODE_TYPE_COMMENT = XML_COMMENT_NODE,
404 NODE_TYPE_TEXT = XML_TEXT_NODE,
405 NODE_TYPE_PI = XML_PI_NODE
406} xmlXPathTypeVal;
407
408
409typedef struct _xmlXPathStepOp xmlXPathStepOp;
410typedef xmlXPathStepOp *xmlXPathStepOpPtr;
411struct _xmlXPathStepOp {
William M. Brack08171912003-12-29 02:52:11 +0000412 xmlXPathOp op; /* The identifier of the operation */
413 int ch1; /* First child */
414 int ch2; /* Second child */
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000415 int value;
416 int value2;
417 int value3;
418 void *value4;
419 void *value5;
Daniel Veillarde39a93d2001-04-28 14:35:02 +0000420 void *cache;
Daniel Veillard42596ad2001-05-22 16:57:14 +0000421 void *cacheURI;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000422};
423
424struct _xmlXPathCompExpr {
William M. Brack08171912003-12-29 02:52:11 +0000425 int nbStep; /* Number of steps in this expression */
426 int maxStep; /* Maximum number of steps allocated */
427 xmlXPathStepOp *steps; /* ops for computation of this expression */
428 int last; /* index of last step in expression */
429 xmlChar *expr; /* the expression being computed */
Daniel Veillard4773df22004-01-23 13:15:13 +0000430 xmlDictPtr dict; /* the dictionnary to use if any */
Daniel Veillardf06307e2001-07-03 10:35:50 +0000431#ifdef DEBUG_EVAL_COUNTS
432 int nb;
433 xmlChar *string;
434#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000435};
436
437/************************************************************************
438 * *
439 * Parser Type functions *
440 * *
441 ************************************************************************/
442
443/**
444 * xmlXPathNewCompExpr:
445 *
446 * Create a new Xpath component
447 *
448 * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error
449 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000450static xmlXPathCompExprPtr
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000451xmlXPathNewCompExpr(void) {
452 xmlXPathCompExprPtr cur;
453
454 cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr));
455 if (cur == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000456 xmlXPathErrMemory(NULL, "allocating component\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000457 return(NULL);
458 }
459 memset(cur, 0, sizeof(xmlXPathCompExpr));
460 cur->maxStep = 10;
461 cur->nbStep = 0;
462 cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep *
463 sizeof(xmlXPathStepOp));
464 if (cur->steps == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000465 xmlXPathErrMemory(NULL, "allocating steps\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000466 xmlFree(cur);
467 return(NULL);
468 }
469 memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp));
470 cur->last = -1;
Daniel Veillardf06307e2001-07-03 10:35:50 +0000471#ifdef DEBUG_EVAL_COUNTS
472 cur->nb = 0;
473#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000474 return(cur);
475}
476
477/**
478 * xmlXPathFreeCompExpr:
479 * @comp: an XPATH comp
480 *
481 * Free up the memory allocated by @comp
482 */
483void
Daniel Veillardf06307e2001-07-03 10:35:50 +0000484xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp)
485{
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000486 xmlXPathStepOpPtr op;
487 int i;
488
489 if (comp == NULL)
Daniel Veillardf06307e2001-07-03 10:35:50 +0000490 return;
Daniel Veillard4773df22004-01-23 13:15:13 +0000491 if (comp->dict == NULL) {
492 for (i = 0; i < comp->nbStep; i++) {
493 op = &comp->steps[i];
494 if (op->value4 != NULL) {
495 if (op->op == XPATH_OP_VALUE)
496 xmlXPathFreeObject(op->value4);
497 else
498 xmlFree(op->value4);
499 }
500 if (op->value5 != NULL)
501 xmlFree(op->value5);
502 }
503 } else {
504 for (i = 0; i < comp->nbStep; i++) {
505 op = &comp->steps[i];
506 if (op->value4 != NULL) {
507 if (op->op == XPATH_OP_VALUE)
508 xmlXPathFreeObject(op->value4);
509 }
510 }
511 xmlDictFree(comp->dict);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000512 }
513 if (comp->steps != NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +0000514 xmlFree(comp->steps);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000515 }
Daniel Veillardf06307e2001-07-03 10:35:50 +0000516#ifdef DEBUG_EVAL_COUNTS
517 if (comp->string != NULL) {
518 xmlFree(comp->string);
519 }
520#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000521 if (comp->expr != NULL) {
522 xmlFree(comp->expr);
523 }
Daniel Veillardf06307e2001-07-03 10:35:50 +0000524
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000525 xmlFree(comp);
526}
527
528/**
529 * xmlXPathCompExprAdd:
530 * @comp: the compiled expression
531 * @ch1: first child index
532 * @ch2: second child index
533 * @op: an op
534 * @value: the first int value
535 * @value2: the second int value
536 * @value3: the third int value
537 * @value4: the first string value
538 * @value5: the second string value
539 *
William M. Brack08171912003-12-29 02:52:11 +0000540 * Add a step to an XPath Compiled Expression
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000541 *
542 * Returns -1 in case of failure, the index otherwise
543 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000544static int
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000545xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2,
546 xmlXPathOp op, int value,
547 int value2, int value3, void *value4, void *value5) {
548 if (comp->nbStep >= comp->maxStep) {
549 xmlXPathStepOp *real;
550
551 comp->maxStep *= 2;
552 real = (xmlXPathStepOp *) xmlRealloc(comp->steps,
553 comp->maxStep * sizeof(xmlXPathStepOp));
554 if (real == NULL) {
555 comp->maxStep /= 2;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000556 xmlXPathErrMemory(NULL, "adding step\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000557 return(-1);
558 }
559 comp->steps = real;
560 }
561 comp->last = comp->nbStep;
562 comp->steps[comp->nbStep].ch1 = ch1;
563 comp->steps[comp->nbStep].ch2 = ch2;
564 comp->steps[comp->nbStep].op = op;
565 comp->steps[comp->nbStep].value = value;
566 comp->steps[comp->nbStep].value2 = value2;
567 comp->steps[comp->nbStep].value3 = value3;
Daniel Veillard4773df22004-01-23 13:15:13 +0000568 if ((comp->dict != NULL) &&
569 ((op == XPATH_OP_FUNCTION) || (op == XPATH_OP_VARIABLE) ||
570 (op == XPATH_OP_COLLECT))) {
571 if (value4 != NULL) {
Daniel Veillardb3377952004-02-09 12:48:55 +0000572 comp->steps[comp->nbStep].value4 = (xmlChar *)
William M. Brackc07ed5e2004-01-30 07:52:48 +0000573 (void *)xmlDictLookup(comp->dict, value4, -1);
Daniel Veillard4773df22004-01-23 13:15:13 +0000574 xmlFree(value4);
575 } else
576 comp->steps[comp->nbStep].value4 = NULL;
577 if (value5 != NULL) {
Daniel Veillardb3377952004-02-09 12:48:55 +0000578 comp->steps[comp->nbStep].value5 = (xmlChar *)
William M. Brackc07ed5e2004-01-30 07:52:48 +0000579 (void *)xmlDictLookup(comp->dict, value5, -1);
Daniel Veillard4773df22004-01-23 13:15:13 +0000580 xmlFree(value5);
581 } else
582 comp->steps[comp->nbStep].value5 = NULL;
583 } else {
584 comp->steps[comp->nbStep].value4 = value4;
585 comp->steps[comp->nbStep].value5 = value5;
586 }
Daniel Veillarde39a93d2001-04-28 14:35:02 +0000587 comp->steps[comp->nbStep].cache = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000588 return(comp->nbStep++);
589}
590
Daniel Veillardf06307e2001-07-03 10:35:50 +0000591/**
592 * xmlXPathCompSwap:
593 * @comp: the compiled expression
594 * @op: operation index
595 *
596 * Swaps 2 operations in the compiled expression
Daniel Veillardf06307e2001-07-03 10:35:50 +0000597 */
598static void
599xmlXPathCompSwap(xmlXPathStepOpPtr op) {
600 int tmp;
601
Daniel Veillardbc6f7592002-04-16 07:49:59 +0000602#ifndef LIBXML_THREAD_ENABLED
Daniel Veillard81463942001-10-16 12:34:39 +0000603 /*
604 * Since this manipulates possibly shared variables, this is
William M. Brack08171912003-12-29 02:52:11 +0000605 * disabled if one detects that the library is used in a multithreaded
Daniel Veillard81463942001-10-16 12:34:39 +0000606 * application
607 */
608 if (xmlXPathDisableOptimizer)
609 return;
610#endif
611
Daniel Veillardf06307e2001-07-03 10:35:50 +0000612 tmp = op->ch1;
613 op->ch1 = op->ch2;
614 op->ch2 = tmp;
615}
616
Daniel Veillardd8df6c02001-04-05 16:54:14 +0000617#define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \
618 xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \
619 (op), (val), (val2), (val3), (val4), (val5))
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000620#define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \
621 xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \
622 (op), (val), (val2), (val3), (val4), (val5))
623
624#define PUSH_LEAVE_EXPR(op, val, val2) \
625xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL)
626
627#define PUSH_UNARY_EXPR(op, ch, val, val2) \
628xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL)
629
630#define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \
William M. Brack08171912003-12-29 02:52:11 +0000631xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \
632 (val), (val2), 0 ,NULL ,NULL)
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000633
634/************************************************************************
Owen Taylor3473f882001-02-23 17:55:21 +0000635 * *
636 * Debugging related functions *
637 * *
638 ************************************************************************/
639
Owen Taylor3473f882001-02-23 17:55:21 +0000640#define STRANGE \
641 xmlGenericError(xmlGenericErrorContext, \
642 "Internal error at %s:%d\n", \
643 __FILE__, __LINE__);
644
645#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000646static void
647xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000648 int i;
649 char shift[100];
650
651 for (i = 0;((i < depth) && (i < 25));i++)
652 shift[2 * i] = shift[2 * i + 1] = ' ';
653 shift[2 * i] = shift[2 * i + 1] = 0;
654 if (cur == NULL) {
655 fprintf(output, shift);
656 fprintf(output, "Node is NULL !\n");
657 return;
658
659 }
660
661 if ((cur->type == XML_DOCUMENT_NODE) ||
662 (cur->type == XML_HTML_DOCUMENT_NODE)) {
663 fprintf(output, shift);
664 fprintf(output, " /\n");
665 } else if (cur->type == XML_ATTRIBUTE_NODE)
666 xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth);
667 else
668 xmlDebugDumpOneNode(output, cur, depth);
669}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000670static void
671xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000672 xmlNodePtr tmp;
673 int i;
674 char shift[100];
675
676 for (i = 0;((i < depth) && (i < 25));i++)
677 shift[2 * i] = shift[2 * i + 1] = ' ';
678 shift[2 * i] = shift[2 * i + 1] = 0;
679 if (cur == NULL) {
680 fprintf(output, shift);
681 fprintf(output, "Node is NULL !\n");
682 return;
683
684 }
685
686 while (cur != NULL) {
687 tmp = cur;
688 cur = cur->next;
689 xmlDebugDumpOneNode(output, tmp, depth);
690 }
691}
Owen Taylor3473f882001-02-23 17:55:21 +0000692
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000693static void
694xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000695 int i;
696 char shift[100];
697
698 for (i = 0;((i < depth) && (i < 25));i++)
699 shift[2 * i] = shift[2 * i + 1] = ' ';
700 shift[2 * i] = shift[2 * i + 1] = 0;
701
702 if (cur == NULL) {
703 fprintf(output, shift);
704 fprintf(output, "NodeSet is NULL !\n");
705 return;
706
707 }
708
Daniel Veillard911f49a2001-04-07 15:39:35 +0000709 if (cur != NULL) {
710 fprintf(output, "Set contains %d nodes:\n", cur->nodeNr);
711 for (i = 0;i < cur->nodeNr;i++) {
712 fprintf(output, shift);
713 fprintf(output, "%d", i + 1);
714 xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1);
715 }
Owen Taylor3473f882001-02-23 17:55:21 +0000716 }
717}
718
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000719static void
720xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000721 int i;
722 char shift[100];
723
724 for (i = 0;((i < depth) && (i < 25));i++)
725 shift[2 * i] = shift[2 * i + 1] = ' ';
726 shift[2 * i] = shift[2 * i + 1] = 0;
727
728 if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) {
729 fprintf(output, shift);
730 fprintf(output, "Value Tree is NULL !\n");
731 return;
732
733 }
734
735 fprintf(output, shift);
736 fprintf(output, "%d", i + 1);
737 xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1);
738}
Owen Taylor3473f882001-02-23 17:55:21 +0000739#if defined(LIBXML_XPTR_ENABLED)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000740static void
741xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000742 int i;
743 char shift[100];
744
745 for (i = 0;((i < depth) && (i < 25));i++)
746 shift[2 * i] = shift[2 * i + 1] = ' ';
747 shift[2 * i] = shift[2 * i + 1] = 0;
748
749 if (cur == NULL) {
750 fprintf(output, shift);
751 fprintf(output, "LocationSet is NULL !\n");
752 return;
753
754 }
755
756 for (i = 0;i < cur->locNr;i++) {
757 fprintf(output, shift);
758 fprintf(output, "%d : ", i + 1);
759 xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1);
760 }
761}
Daniel Veillard017b1082001-06-21 11:20:21 +0000762#endif /* LIBXML_XPTR_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000763
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000764/**
765 * xmlXPathDebugDumpObject:
766 * @output: the FILE * to dump the output
767 * @cur: the object to inspect
768 * @depth: indentation level
769 *
770 * Dump the content of the object for debugging purposes
771 */
772void
773xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000774 int i;
775 char shift[100];
776
777 for (i = 0;((i < depth) && (i < 25));i++)
778 shift[2 * i] = shift[2 * i + 1] = ' ';
779 shift[2 * i] = shift[2 * i + 1] = 0;
780
781 fprintf(output, shift);
782
783 if (cur == NULL) {
784 fprintf(output, "Object is empty (NULL)\n");
785 return;
786 }
787 switch(cur->type) {
788 case XPATH_UNDEFINED:
789 fprintf(output, "Object is uninitialized\n");
790 break;
791 case XPATH_NODESET:
792 fprintf(output, "Object is a Node Set :\n");
793 xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth);
794 break;
795 case XPATH_XSLT_TREE:
796 fprintf(output, "Object is an XSLT value tree :\n");
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000797 xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth);
Owen Taylor3473f882001-02-23 17:55:21 +0000798 break;
799 case XPATH_BOOLEAN:
800 fprintf(output, "Object is a Boolean : ");
801 if (cur->boolval) fprintf(output, "true\n");
802 else fprintf(output, "false\n");
803 break;
804 case XPATH_NUMBER:
Daniel Veillardcda96922001-08-21 10:56:31 +0000805 switch (xmlXPathIsInf(cur->floatval)) {
Daniel Veillard357c9602001-05-03 10:49:20 +0000806 case 1:
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000807 fprintf(output, "Object is a number : Infinity\n");
Daniel Veillard357c9602001-05-03 10:49:20 +0000808 break;
809 case -1:
810 fprintf(output, "Object is a number : -Infinity\n");
811 break;
812 default:
Daniel Veillardcda96922001-08-21 10:56:31 +0000813 if (xmlXPathIsNaN(cur->floatval)) {
Daniel Veillard357c9602001-05-03 10:49:20 +0000814 fprintf(output, "Object is a number : NaN\n");
Daniel Veillardd30be4a2002-03-28 18:25:31 +0000815 } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) {
816 fprintf(output, "Object is a number : 0\n");
Daniel Veillard357c9602001-05-03 10:49:20 +0000817 } else {
818 fprintf(output, "Object is a number : %0g\n", cur->floatval);
819 }
820 }
Owen Taylor3473f882001-02-23 17:55:21 +0000821 break;
822 case XPATH_STRING:
823 fprintf(output, "Object is a string : ");
824 xmlDebugDumpString(output, cur->stringval);
825 fprintf(output, "\n");
826 break;
827 case XPATH_POINT:
828 fprintf(output, "Object is a point : index %d in node", cur->index);
829 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1);
830 fprintf(output, "\n");
831 break;
832 case XPATH_RANGE:
833 if ((cur->user2 == NULL) ||
834 ((cur->user2 == cur->user) && (cur->index == cur->index2))) {
835 fprintf(output, "Object is a collapsed range :\n");
836 fprintf(output, shift);
837 if (cur->index >= 0)
838 fprintf(output, "index %d in ", cur->index);
839 fprintf(output, "node\n");
840 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user,
841 depth + 1);
842 } else {
843 fprintf(output, "Object is a range :\n");
844 fprintf(output, shift);
845 fprintf(output, "From ");
846 if (cur->index >= 0)
847 fprintf(output, "index %d in ", cur->index);
848 fprintf(output, "node\n");
849 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user,
850 depth + 1);
851 fprintf(output, shift);
852 fprintf(output, "To ");
853 if (cur->index2 >= 0)
854 fprintf(output, "index %d in ", cur->index2);
855 fprintf(output, "node\n");
856 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2,
857 depth + 1);
858 fprintf(output, "\n");
859 }
860 break;
861 case XPATH_LOCATIONSET:
862#if defined(LIBXML_XPTR_ENABLED)
863 fprintf(output, "Object is a Location Set:\n");
864 xmlXPathDebugDumpLocationSet(output,
865 (xmlLocationSetPtr) cur->user, depth);
866#endif
867 break;
868 case XPATH_USERS:
869 fprintf(output, "Object is user defined\n");
870 break;
871 }
872}
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000873
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000874static void
875xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp,
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000876 xmlXPathStepOpPtr op, int depth) {
877 int i;
878 char shift[100];
879
880 for (i = 0;((i < depth) && (i < 25));i++)
881 shift[2 * i] = shift[2 * i + 1] = ' ';
882 shift[2 * i] = shift[2 * i + 1] = 0;
883
884 fprintf(output, shift);
885 if (op == NULL) {
886 fprintf(output, "Step is NULL\n");
887 return;
888 }
889 switch (op->op) {
890 case XPATH_OP_END:
891 fprintf(output, "END"); break;
892 case XPATH_OP_AND:
893 fprintf(output, "AND"); break;
894 case XPATH_OP_OR:
895 fprintf(output, "OR"); break;
896 case XPATH_OP_EQUAL:
897 if (op->value)
898 fprintf(output, "EQUAL =");
899 else
900 fprintf(output, "EQUAL !=");
901 break;
902 case XPATH_OP_CMP:
903 if (op->value)
904 fprintf(output, "CMP <");
905 else
906 fprintf(output, "CMP >");
907 if (!op->value2)
908 fprintf(output, "=");
909 break;
910 case XPATH_OP_PLUS:
911 if (op->value == 0)
912 fprintf(output, "PLUS -");
913 else if (op->value == 1)
914 fprintf(output, "PLUS +");
915 else if (op->value == 2)
916 fprintf(output, "PLUS unary -");
917 else if (op->value == 3)
918 fprintf(output, "PLUS unary - -");
919 break;
920 case XPATH_OP_MULT:
921 if (op->value == 0)
922 fprintf(output, "MULT *");
923 else if (op->value == 1)
924 fprintf(output, "MULT div");
925 else
926 fprintf(output, "MULT mod");
927 break;
928 case XPATH_OP_UNION:
929 fprintf(output, "UNION"); break;
930 case XPATH_OP_ROOT:
931 fprintf(output, "ROOT"); break;
932 case XPATH_OP_NODE:
933 fprintf(output, "NODE"); break;
934 case XPATH_OP_RESET:
935 fprintf(output, "RESET"); break;
936 case XPATH_OP_SORT:
937 fprintf(output, "SORT"); break;
938 case XPATH_OP_COLLECT: {
William M. Brack78637da2003-07-31 14:47:38 +0000939 xmlXPathAxisVal axis = (xmlXPathAxisVal)op->value;
940 xmlXPathTestVal test = (xmlXPathTestVal)op->value2;
941 xmlXPathTypeVal type = (xmlXPathTypeVal)op->value3;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000942 const xmlChar *prefix = op->value4;
943 const xmlChar *name = op->value5;
944
945 fprintf(output, "COLLECT ");
946 switch (axis) {
947 case AXIS_ANCESTOR:
948 fprintf(output, " 'ancestors' "); break;
949 case AXIS_ANCESTOR_OR_SELF:
950 fprintf(output, " 'ancestors-or-self' "); break;
951 case AXIS_ATTRIBUTE:
952 fprintf(output, " 'attributes' "); break;
953 case AXIS_CHILD:
954 fprintf(output, " 'child' "); break;
955 case AXIS_DESCENDANT:
956 fprintf(output, " 'descendant' "); break;
957 case AXIS_DESCENDANT_OR_SELF:
958 fprintf(output, " 'descendant-or-self' "); break;
959 case AXIS_FOLLOWING:
960 fprintf(output, " 'following' "); break;
961 case AXIS_FOLLOWING_SIBLING:
962 fprintf(output, " 'following-siblings' "); break;
963 case AXIS_NAMESPACE:
964 fprintf(output, " 'namespace' "); break;
965 case AXIS_PARENT:
966 fprintf(output, " 'parent' "); break;
967 case AXIS_PRECEDING:
968 fprintf(output, " 'preceding' "); break;
969 case AXIS_PRECEDING_SIBLING:
970 fprintf(output, " 'preceding-sibling' "); break;
971 case AXIS_SELF:
972 fprintf(output, " 'self' "); break;
973 }
974 switch (test) {
975 case NODE_TEST_NONE:
976 fprintf(output, "'none' "); break;
977 case NODE_TEST_TYPE:
978 fprintf(output, "'type' "); break;
979 case NODE_TEST_PI:
980 fprintf(output, "'PI' "); break;
981 case NODE_TEST_ALL:
982 fprintf(output, "'all' "); break;
983 case NODE_TEST_NS:
984 fprintf(output, "'namespace' "); break;
985 case NODE_TEST_NAME:
986 fprintf(output, "'name' "); break;
987 }
988 switch (type) {
989 case NODE_TYPE_NODE:
990 fprintf(output, "'node' "); break;
991 case NODE_TYPE_COMMENT:
992 fprintf(output, "'comment' "); break;
993 case NODE_TYPE_TEXT:
994 fprintf(output, "'text' "); break;
995 case NODE_TYPE_PI:
996 fprintf(output, "'PI' "); break;
997 }
998 if (prefix != NULL)
999 fprintf(output, "%s:", prefix);
1000 if (name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001001 fprintf(output, "%s", (const char *) name);
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001002 break;
1003
1004 }
1005 case XPATH_OP_VALUE: {
1006 xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4;
1007
1008 fprintf(output, "ELEM ");
1009 xmlXPathDebugDumpObject(output, object, 0);
1010 goto finish;
1011 }
1012 case XPATH_OP_VARIABLE: {
1013 const xmlChar *prefix = op->value5;
1014 const xmlChar *name = op->value4;
1015
1016 if (prefix != NULL)
1017 fprintf(output, "VARIABLE %s:%s", prefix, name);
1018 else
1019 fprintf(output, "VARIABLE %s", name);
1020 break;
1021 }
1022 case XPATH_OP_FUNCTION: {
1023 int nbargs = op->value;
1024 const xmlChar *prefix = op->value5;
1025 const xmlChar *name = op->value4;
1026
1027 if (prefix != NULL)
1028 fprintf(output, "FUNCTION %s:%s(%d args)",
1029 prefix, name, nbargs);
1030 else
1031 fprintf(output, "FUNCTION %s(%d args)", name, nbargs);
1032 break;
1033 }
1034 case XPATH_OP_ARG: fprintf(output, "ARG"); break;
1035 case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00001036 case XPATH_OP_FILTER: fprintf(output, "FILTER"); break;
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00001037#ifdef LIBXML_XPTR_ENABLED
1038 case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break;
1039#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001040 default:
1041 fprintf(output, "UNKNOWN %d\n", op->op); return;
1042 }
1043 fprintf(output, "\n");
1044finish:
1045 if (op->ch1 >= 0)
1046 xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1);
1047 if (op->ch2 >= 0)
1048 xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1);
1049}
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001050
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001051/**
1052 * xmlXPathDebugDumpCompExpr:
1053 * @output: the FILE * for the output
1054 * @comp: the precompiled XPath expression
1055 * @depth: the indentation level.
1056 *
1057 * Dumps the tree of the compiled XPath expression.
1058 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001059void
1060xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp,
1061 int depth) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001062 int i;
1063 char shift[100];
1064
1065 for (i = 0;((i < depth) && (i < 25));i++)
1066 shift[2 * i] = shift[2 * i + 1] = ' ';
1067 shift[2 * i] = shift[2 * i + 1] = 0;
1068
1069 fprintf(output, shift);
1070
1071 if (comp == NULL) {
1072 fprintf(output, "Compiled Expression is NULL\n");
1073 return;
1074 }
1075 fprintf(output, "Compiled Expression : %d elements\n",
1076 comp->nbStep);
1077 i = comp->last;
1078 xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1);
1079}
Daniel Veillard017b1082001-06-21 11:20:21 +00001080#endif /* LIBXML_DEBUG_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001081
1082/************************************************************************
1083 * *
1084 * Parser stacks related functions and macros *
1085 * *
1086 ************************************************************************/
1087
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001088/**
1089 * valuePop:
1090 * @ctxt: an XPath evaluation context
1091 *
1092 * Pops the top XPath object from the value stack
1093 *
1094 * Returns the XPath object just removed
1095 */
Daniel Veillard1c732d22002-11-30 11:22:59 +00001096extern xmlXPathObjectPtr
1097valuePop(xmlXPathParserContextPtr ctxt)
1098{
1099 xmlXPathObjectPtr ret;
1100
1101 if (ctxt->valueNr <= 0)
1102 return (0);
1103 ctxt->valueNr--;
1104 if (ctxt->valueNr > 0)
1105 ctxt->value = ctxt->valueTab[ctxt->valueNr - 1];
1106 else
1107 ctxt->value = NULL;
1108 ret = ctxt->valueTab[ctxt->valueNr];
1109 ctxt->valueTab[ctxt->valueNr] = 0;
1110 return (ret);
1111}
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001112/**
1113 * valuePush:
1114 * @ctxt: an XPath evaluation context
1115 * @value: the XPath object
1116 *
1117 * Pushes a new XPath object on top of the value stack
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001118 *
1119 * returns the number of items on the value stack
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001120 */
Daniel Veillard1c732d22002-11-30 11:22:59 +00001121extern int
1122valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value)
1123{
1124 if (ctxt->valueNr >= ctxt->valueMax) {
Daniel Veillarda918b5b2004-09-26 14:25:37 +00001125 xmlXPathObjectPtr *tmp;
1126
1127 tmp = (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab,
1128 2 * ctxt->valueMax *
Daniel Veillard1c732d22002-11-30 11:22:59 +00001129 sizeof(ctxt->valueTab[0]));
Daniel Veillarda918b5b2004-09-26 14:25:37 +00001130 if (tmp == NULL) {
Daniel Veillard1c732d22002-11-30 11:22:59 +00001131 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1132 return (0);
1133 }
Daniel Veillarda918b5b2004-09-26 14:25:37 +00001134 ctxt->valueMax *= 2;
1135 ctxt->valueTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001136 }
1137 ctxt->valueTab[ctxt->valueNr] = value;
1138 ctxt->value = value;
1139 return (ctxt->valueNr++);
1140}
Owen Taylor3473f882001-02-23 17:55:21 +00001141
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001142/**
1143 * xmlXPathPopBoolean:
1144 * @ctxt: an XPath parser context
1145 *
1146 * Pops a boolean from the stack, handling conversion if needed.
1147 * Check error with #xmlXPathCheckError.
1148 *
1149 * Returns the boolean
1150 */
1151int
1152xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) {
1153 xmlXPathObjectPtr obj;
1154 int ret;
1155
1156 obj = valuePop(ctxt);
1157 if (obj == NULL) {
1158 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1159 return(0);
1160 }
William M. Brack08171912003-12-29 02:52:11 +00001161 if (obj->type != XPATH_BOOLEAN)
1162 ret = xmlXPathCastToBoolean(obj);
1163 else
1164 ret = obj->boolval;
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001165 xmlXPathFreeObject(obj);
1166 return(ret);
1167}
1168
1169/**
1170 * xmlXPathPopNumber:
1171 * @ctxt: an XPath parser context
1172 *
1173 * Pops a number from the stack, handling conversion if needed.
1174 * Check error with #xmlXPathCheckError.
1175 *
1176 * Returns the number
1177 */
1178double
1179xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) {
1180 xmlXPathObjectPtr obj;
1181 double ret;
1182
1183 obj = valuePop(ctxt);
1184 if (obj == NULL) {
1185 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1186 return(0);
1187 }
William M. Brack08171912003-12-29 02:52:11 +00001188 if (obj->type != XPATH_NUMBER)
1189 ret = xmlXPathCastToNumber(obj);
1190 else
1191 ret = obj->floatval;
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001192 xmlXPathFreeObject(obj);
1193 return(ret);
1194}
1195
1196/**
1197 * xmlXPathPopString:
1198 * @ctxt: an XPath parser context
1199 *
1200 * Pops a string from the stack, handling conversion if needed.
1201 * Check error with #xmlXPathCheckError.
1202 *
1203 * Returns the string
1204 */
1205xmlChar *
1206xmlXPathPopString (xmlXPathParserContextPtr ctxt) {
1207 xmlXPathObjectPtr obj;
1208 xmlChar * ret;
1209
1210 obj = valuePop(ctxt);
1211 if (obj == NULL) {
1212 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1213 return(NULL);
1214 }
William M. Brack08171912003-12-29 02:52:11 +00001215 ret = xmlXPathCastToString(obj); /* this does required strdup */
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001216 /* TODO: needs refactoring somewhere else */
1217 if (obj->stringval == ret)
1218 obj->stringval = NULL;
1219 xmlXPathFreeObject(obj);
1220 return(ret);
1221}
1222
1223/**
1224 * xmlXPathPopNodeSet:
1225 * @ctxt: an XPath parser context
1226 *
1227 * Pops a node-set from the stack, handling conversion if needed.
1228 * Check error with #xmlXPathCheckError.
1229 *
1230 * Returns the node-set
1231 */
1232xmlNodeSetPtr
1233xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) {
1234 xmlXPathObjectPtr obj;
1235 xmlNodeSetPtr ret;
1236
1237 if (ctxt->value == NULL) {
1238 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1239 return(NULL);
1240 }
1241 if (!xmlXPathStackIsNodeSet(ctxt)) {
1242 xmlXPathSetTypeError(ctxt);
1243 return(NULL);
1244 }
1245 obj = valuePop(ctxt);
1246 ret = obj->nodesetval;
William M. Bracke9449c52004-07-11 14:41:20 +00001247#if 0
Daniel Veillard9deb2422003-07-28 20:40:59 +00001248 /* to fix memory leak of not clearing obj->user */
1249 if (obj->boolval && obj->user != NULL)
1250 xmlFreeNodeList((xmlNodePtr) obj->user);
William M. Bracke9449c52004-07-11 14:41:20 +00001251#endif
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001252 xmlXPathFreeNodeSetList(obj);
1253 return(ret);
1254}
1255
1256/**
1257 * xmlXPathPopExternal:
1258 * @ctxt: an XPath parser context
1259 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001260 * Pops an external object from the stack, handling conversion if needed.
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001261 * Check error with #xmlXPathCheckError.
1262 *
1263 * Returns the object
1264 */
1265void *
1266xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) {
1267 xmlXPathObjectPtr obj;
1268 void * ret;
1269
1270 if (ctxt->value == NULL) {
1271 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1272 return(NULL);
1273 }
1274 if (ctxt->value->type != XPATH_USERS) {
1275 xmlXPathSetTypeError(ctxt);
1276 return(NULL);
1277 }
1278 obj = valuePop(ctxt);
1279 ret = obj->user;
1280 xmlXPathFreeObject(obj);
1281 return(ret);
1282}
1283
Owen Taylor3473f882001-02-23 17:55:21 +00001284/*
1285 * Macros for accessing the content. Those should be used only by the parser,
1286 * and not exported.
1287 *
1288 * Dirty macros, i.e. one need to make assumption on the context to use them
1289 *
1290 * CUR_PTR return the current pointer to the xmlChar to be parsed.
1291 * CUR returns the current xmlChar value, i.e. a 8 bit value
1292 * in ISO-Latin or UTF-8.
1293 * This should be used internally by the parser
1294 * only to compare to ASCII values otherwise it would break when
1295 * running with UTF-8 encoding.
1296 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
1297 * to compare on ASCII based substring.
1298 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
1299 * strings within the parser.
1300 * CURRENT Returns the current char value, with the full decoding of
1301 * UTF-8 if we are using this mode. It returns an int.
1302 * NEXT Skip to the next character, this does the proper decoding
1303 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
1304 * It returns the pointer to the current xmlChar.
1305 */
1306
1307#define CUR (*ctxt->cur)
1308#define SKIP(val) ctxt->cur += (val)
1309#define NXT(val) ctxt->cur[(val)]
1310#define CUR_PTR ctxt->cur
Daniel Veillard61d80a22001-04-27 17:13:01 +00001311#define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l)
1312
1313#define COPY_BUF(l,b,i,v) \
1314 if (l == 1) b[i++] = (xmlChar) v; \
1315 else i += xmlCopyChar(l,&b[i],v)
1316
1317#define NEXTL(l) ctxt->cur += l
Owen Taylor3473f882001-02-23 17:55:21 +00001318
1319#define SKIP_BLANKS \
William M. Brack76e95df2003-10-18 16:20:14 +00001320 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
Owen Taylor3473f882001-02-23 17:55:21 +00001321
1322#define CURRENT (*ctxt->cur)
1323#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1324
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001325
1326#ifndef DBL_DIG
1327#define DBL_DIG 16
1328#endif
1329#ifndef DBL_EPSILON
1330#define DBL_EPSILON 1E-9
1331#endif
1332
1333#define UPPER_DOUBLE 1E9
1334#define LOWER_DOUBLE 1E-5
1335
1336#define INTEGER_DIGITS DBL_DIG
1337#define FRACTION_DIGITS (DBL_DIG + 1)
1338#define EXPONENT_DIGITS (3 + 2)
1339
1340/**
1341 * xmlXPathFormatNumber:
1342 * @number: number to format
1343 * @buffer: output buffer
1344 * @buffersize: size of output buffer
1345 *
1346 * Convert the number into a string representation.
1347 */
1348static void
1349xmlXPathFormatNumber(double number, char buffer[], int buffersize)
1350{
Daniel Veillardcda96922001-08-21 10:56:31 +00001351 switch (xmlXPathIsInf(number)) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001352 case 1:
Daniel Veillard5fc1f082002-03-27 09:05:40 +00001353 if (buffersize > (int)sizeof("Infinity"))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001354 snprintf(buffer, buffersize, "Infinity");
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001355 break;
1356 case -1:
1357 if (buffersize > (int)sizeof("-Infinity"))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001358 snprintf(buffer, buffersize, "-Infinity");
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001359 break;
1360 default:
Daniel Veillardcda96922001-08-21 10:56:31 +00001361 if (xmlXPathIsNaN(number)) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001362 if (buffersize > (int)sizeof("NaN"))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001363 snprintf(buffer, buffersize, "NaN");
Daniel Veillardd30be4a2002-03-28 18:25:31 +00001364 } else if (number == 0 && xmlXPathGetSign(number) != 0) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001365 snprintf(buffer, buffersize, "0");
Daniel Veillard28cac6b2002-03-19 11:25:30 +00001366 } else if (number == ((int) number)) {
1367 char work[30];
1368 char *ptr, *cur;
1369 int res, value = (int) number;
1370
1371 ptr = &buffer[0];
1372 if (value < 0) {
1373 *ptr++ = '-';
1374 value = -value;
1375 }
1376 if (value == 0) {
1377 *ptr++ = '0';
1378 } else {
1379 cur = &work[0];
1380 while (value != 0) {
1381 res = value % 10;
1382 value = value / 10;
1383 *cur++ = '0' + res;
1384 }
1385 cur--;
1386 while ((cur >= &work[0]) && (ptr - buffer < buffersize)) {
1387 *ptr++ = *cur--;
1388 }
1389 }
1390 if (ptr - buffer < buffersize) {
1391 *ptr = 0;
1392 } else if (buffersize > 0) {
1393 ptr--;
1394 *ptr = 0;
1395 }
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001396 } else {
Bjorn Reese70a9da52001-04-21 16:57:29 +00001397 /* 3 is sign, decimal point, and terminating zero */
1398 char work[DBL_DIG + EXPONENT_DIGITS + 3];
1399 int integer_place, fraction_place;
1400 char *ptr;
1401 char *after_fraction;
1402 double absolute_value;
1403 int size;
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001404
Bjorn Reese70a9da52001-04-21 16:57:29 +00001405 absolute_value = fabs(number);
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001406
Bjorn Reese70a9da52001-04-21 16:57:29 +00001407 /*
1408 * First choose format - scientific or regular floating point.
1409 * In either case, result is in work, and after_fraction points
1410 * just past the fractional part.
1411 */
1412 if ( ((absolute_value > UPPER_DOUBLE) ||
1413 (absolute_value < LOWER_DOUBLE)) &&
1414 (absolute_value != 0.0) ) {
1415 /* Use scientific notation */
1416 integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
1417 fraction_place = DBL_DIG - 1;
1418 snprintf(work, sizeof(work),"%*.*e",
1419 integer_place, fraction_place, number);
1420 after_fraction = strchr(work + DBL_DIG, 'e');
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001421 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00001422 else {
1423 /* Use regular notation */
Daniel Veillard56f06462001-06-24 21:34:03 +00001424 if (absolute_value > 0.0)
1425 integer_place = 1 + (int)log10(absolute_value);
1426 else
Daniel Veillarda3067d12001-06-24 21:39:39 +00001427 integer_place = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00001428 fraction_place = (integer_place > 0)
1429 ? DBL_DIG - integer_place
1430 : DBL_DIG;
1431 size = snprintf(work, sizeof(work), "%0.*f",
1432 fraction_place, number);
1433 after_fraction = work + size;
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001434 }
1435
Bjorn Reese70a9da52001-04-21 16:57:29 +00001436 /* Remove fractional trailing zeroes */
1437 ptr = after_fraction;
1438 while (*(--ptr) == '0')
1439 ;
1440 if (*ptr != '.')
1441 ptr++;
Daniel Veillard5dd3c962003-09-12 15:32:16 +00001442 while ((*ptr++ = *after_fraction++) != 0);
Bjorn Reese70a9da52001-04-21 16:57:29 +00001443
1444 /* Finally copy result back to caller */
1445 size = strlen(work) + 1;
1446 if (size > buffersize) {
1447 work[buffersize - 1] = 0;
1448 size = buffersize;
1449 }
Daniel Veillard5dd3c962003-09-12 15:32:16 +00001450 memmove(buffer, work, size);
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001451 }
1452 break;
1453 }
1454}
1455
Owen Taylor3473f882001-02-23 17:55:21 +00001456
1457/************************************************************************
1458 * *
1459 * Routines to handle NodeSets *
1460 * *
1461 ************************************************************************/
1462
1463/**
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001464 * xmlXPathOrderDocElems:
1465 * @doc: an input document
1466 *
1467 * Call this routine to speed up XPath computation on static documents.
1468 * This stamps all the element nodes with the document order
1469 * Like for line information, the order is kept in the element->content
William M. Brack08171912003-12-29 02:52:11 +00001470 * field, the value stored is actually - the node number (starting at -1)
1471 * to be able to differentiate from line numbers.
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001472 *
William M. Brack08171912003-12-29 02:52:11 +00001473 * Returns the number of elements found in the document or -1 in case
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001474 * of error.
1475 */
1476long
1477xmlXPathOrderDocElems(xmlDocPtr doc) {
1478 long count = 0;
1479 xmlNodePtr cur;
1480
1481 if (doc == NULL)
1482 return(-1);
1483 cur = doc->children;
1484 while (cur != NULL) {
1485 if (cur->type == XML_ELEMENT_NODE) {
1486 cur->content = (void *) (-(++count));
1487 if (cur->children != NULL) {
1488 cur = cur->children;
1489 continue;
1490 }
1491 }
1492 if (cur->next != NULL) {
1493 cur = cur->next;
1494 continue;
1495 }
1496 do {
1497 cur = cur->parent;
1498 if (cur == NULL)
1499 break;
1500 if (cur == (xmlNodePtr) doc) {
1501 cur = NULL;
1502 break;
1503 }
1504 if (cur->next != NULL) {
1505 cur = cur->next;
1506 break;
1507 }
1508 } while (cur != NULL);
1509 }
1510 return(count);
1511}
1512
1513/**
Owen Taylor3473f882001-02-23 17:55:21 +00001514 * xmlXPathCmpNodes:
1515 * @node1: the first node
1516 * @node2: the second node
1517 *
1518 * Compare two nodes w.r.t document order
1519 *
1520 * Returns -2 in case of error 1 if first point < second point, 0 if
William M. Brack08171912003-12-29 02:52:11 +00001521 * it's the same node, -1 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00001522 */
1523int
1524xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) {
1525 int depth1, depth2;
Daniel Veillardedfd5882003-03-07 14:20:40 +00001526 int attr1 = 0, attr2 = 0;
William M. Bracke8d1bd92003-12-23 01:28:58 +00001527 xmlNodePtr attrNode1 = NULL, attrNode2 = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001528 xmlNodePtr cur, root;
1529
1530 if ((node1 == NULL) || (node2 == NULL))
1531 return(-2);
1532 /*
1533 * a couple of optimizations which will avoid computations in most cases
1534 */
Daniel Veillardedfd5882003-03-07 14:20:40 +00001535 if (node1->type == XML_ATTRIBUTE_NODE) {
1536 attr1 = 1;
William M. Bracke8d1bd92003-12-23 01:28:58 +00001537 attrNode1 = node1;
Daniel Veillardedfd5882003-03-07 14:20:40 +00001538 node1 = node1->parent;
1539 }
1540 if (node2->type == XML_ATTRIBUTE_NODE) {
1541 attr2 = 1;
William M. Bracke8d1bd92003-12-23 01:28:58 +00001542 attrNode2 = node2;
Daniel Veillardedfd5882003-03-07 14:20:40 +00001543 node2 = node2->parent;
1544 }
1545 if (node1 == node2) {
William M. Bracke8d1bd92003-12-23 01:28:58 +00001546 if (attr1 == attr2) {
1547 /* not required, but we keep attributes in order */
1548 if (attr1 != 0) {
1549 cur = attrNode2->prev;
1550 while (cur != NULL) {
1551 if (cur == attrNode1)
1552 return (1);
1553 cur = cur->prev;
1554 }
1555 return (-1);
1556 }
Daniel Veillardedfd5882003-03-07 14:20:40 +00001557 return(0);
William M. Bracke8d1bd92003-12-23 01:28:58 +00001558 }
Daniel Veillardedfd5882003-03-07 14:20:40 +00001559 if (attr2 == 1)
1560 return(1);
1561 return(-1);
1562 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00001563 if ((node1->type == XML_NAMESPACE_DECL) ||
1564 (node2->type == XML_NAMESPACE_DECL))
1565 return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00001566 if (node1 == node2->prev)
1567 return(1);
1568 if (node1 == node2->next)
1569 return(-1);
1570
1571 /*
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001572 * Speedup using document order if availble.
Daniel Veillard7216cfd2002-11-08 15:10:00 +00001573 */
1574 if ((node1->type == XML_ELEMENT_NODE) &&
1575 (node2->type == XML_ELEMENT_NODE) &&
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001576 (0 > (long) node1->content) &&
1577 (0 > (long) node2->content) &&
1578 (node1->doc == node2->doc)) {
1579 long l1, l2;
1580
1581 l1 = -((long) node1->content);
1582 l2 = -((long) node2->content);
Daniel Veillard7216cfd2002-11-08 15:10:00 +00001583 if (l1 < l2)
1584 return(1);
1585 if (l1 > l2)
1586 return(-1);
1587 }
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001588
Daniel Veillard7216cfd2002-11-08 15:10:00 +00001589 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001590 * compute depth to root
1591 */
1592 for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) {
1593 if (cur == node1)
1594 return(1);
1595 depth2++;
1596 }
1597 root = cur;
1598 for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) {
1599 if (cur == node2)
1600 return(-1);
1601 depth1++;
1602 }
1603 /*
1604 * Distinct document (or distinct entities :-( ) case.
1605 */
1606 if (root != cur) {
1607 return(-2);
1608 }
1609 /*
1610 * get the nearest common ancestor.
1611 */
1612 while (depth1 > depth2) {
1613 depth1--;
1614 node1 = node1->parent;
1615 }
1616 while (depth2 > depth1) {
1617 depth2--;
1618 node2 = node2->parent;
1619 }
1620 while (node1->parent != node2->parent) {
1621 node1 = node1->parent;
1622 node2 = node2->parent;
1623 /* should not happen but just in case ... */
1624 if ((node1 == NULL) || (node2 == NULL))
1625 return(-2);
1626 }
1627 /*
1628 * Find who's first.
1629 */
Daniel Veillardf49be472004-02-17 11:48:18 +00001630 if (node1 == node2->prev)
1631 return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00001632 if (node1 == node2->next)
1633 return(-1);
Daniel Veillardf49be472004-02-17 11:48:18 +00001634 /*
1635 * Speedup using document order if availble.
1636 */
1637 if ((node1->type == XML_ELEMENT_NODE) &&
1638 (node2->type == XML_ELEMENT_NODE) &&
1639 (0 > (long) node1->content) &&
1640 (0 > (long) node2->content) &&
1641 (node1->doc == node2->doc)) {
1642 long l1, l2;
1643
1644 l1 = -((long) node1->content);
1645 l2 = -((long) node2->content);
1646 if (l1 < l2)
1647 return(1);
1648 if (l1 > l2)
1649 return(-1);
1650 }
1651
Owen Taylor3473f882001-02-23 17:55:21 +00001652 for (cur = node1->next;cur != NULL;cur = cur->next)
1653 if (cur == node2)
1654 return(1);
1655 return(-1); /* assume there is no sibling list corruption */
1656}
1657
1658/**
1659 * xmlXPathNodeSetSort:
1660 * @set: the node set
1661 *
1662 * Sort the node set in document order
1663 */
1664void
1665xmlXPathNodeSetSort(xmlNodeSetPtr set) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001666 int i, j, incr, len;
Owen Taylor3473f882001-02-23 17:55:21 +00001667 xmlNodePtr tmp;
1668
1669 if (set == NULL)
1670 return;
1671
1672 /* Use Shell's sort to sort the node-set */
1673 len = set->nodeNr;
1674 for (incr = len / 2; incr > 0; incr /= 2) {
1675 for (i = incr; i < len; i++) {
1676 j = i - incr;
1677 while (j >= 0) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001678 if (xmlXPathCmpNodes(set->nodeTab[j],
1679 set->nodeTab[j + incr]) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +00001680 tmp = set->nodeTab[j];
1681 set->nodeTab[j] = set->nodeTab[j + incr];
1682 set->nodeTab[j + incr] = tmp;
1683 j -= incr;
1684 } else
1685 break;
1686 }
1687 }
1688 }
1689}
1690
1691#define XML_NODESET_DEFAULT 10
1692/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001693 * xmlXPathNodeSetDupNs:
1694 * @node: the parent node of the namespace XPath node
1695 * @ns: the libxml namespace declaration node.
1696 *
1697 * Namespace node in libxml don't match the XPath semantic. In a node set
1698 * the namespace nodes are duplicated and the next pointer is set to the
1699 * parent node in the XPath semantic.
1700 *
1701 * Returns the newly created object.
1702 */
1703static xmlNodePtr
1704xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) {
1705 xmlNsPtr cur;
1706
1707 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
1708 return(NULL);
1709 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
1710 return((xmlNodePtr) ns);
1711
1712 /*
1713 * Allocate a new Namespace and fill the fields.
1714 */
1715 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
1716 if (cur == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001717 xmlXPathErrMemory(NULL, "duplicating namespace\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001718 return(NULL);
1719 }
1720 memset(cur, 0, sizeof(xmlNs));
1721 cur->type = XML_NAMESPACE_DECL;
1722 if (ns->href != NULL)
1723 cur->href = xmlStrdup(ns->href);
1724 if (ns->prefix != NULL)
1725 cur->prefix = xmlStrdup(ns->prefix);
1726 cur->next = (xmlNsPtr) node;
1727 return((xmlNodePtr) cur);
1728}
1729
1730/**
1731 * xmlXPathNodeSetFreeNs:
1732 * @ns: the XPath namespace node found in a nodeset.
1733 *
William M. Brack08171912003-12-29 02:52:11 +00001734 * Namespace nodes in libxml don't match the XPath semantic. In a node set
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001735 * the namespace nodes are duplicated and the next pointer is set to the
William M. Brack08171912003-12-29 02:52:11 +00001736 * parent node in the XPath semantic. Check if such a node needs to be freed
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001737 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001738void
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001739xmlXPathNodeSetFreeNs(xmlNsPtr ns) {
1740 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
1741 return;
1742
1743 if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) {
1744 if (ns->href != NULL)
1745 xmlFree((xmlChar *)ns->href);
1746 if (ns->prefix != NULL)
1747 xmlFree((xmlChar *)ns->prefix);
1748 xmlFree(ns);
1749 }
1750}
1751
1752/**
Owen Taylor3473f882001-02-23 17:55:21 +00001753 * xmlXPathNodeSetCreate:
1754 * @val: an initial xmlNodePtr, or NULL
1755 *
1756 * Create a new xmlNodeSetPtr of type double and of value @val
1757 *
1758 * Returns the newly created object.
1759 */
1760xmlNodeSetPtr
1761xmlXPathNodeSetCreate(xmlNodePtr val) {
1762 xmlNodeSetPtr ret;
1763
1764 ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
1765 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001766 xmlXPathErrMemory(NULL, "creating nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001767 return(NULL);
1768 }
1769 memset(ret, 0 , (size_t) sizeof(xmlNodeSet));
1770 if (val != NULL) {
1771 ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1772 sizeof(xmlNodePtr));
1773 if (ret->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001774 xmlXPathErrMemory(NULL, "creating nodeset\n");
1775 xmlFree(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001776 return(NULL);
1777 }
1778 memset(ret->nodeTab, 0 ,
1779 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1780 ret->nodeMax = XML_NODESET_DEFAULT;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001781 if (val->type == XML_NAMESPACE_DECL) {
1782 xmlNsPtr ns = (xmlNsPtr) val;
1783
1784 ret->nodeTab[ret->nodeNr++] =
1785 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1786 } else
1787 ret->nodeTab[ret->nodeNr++] = val;
Owen Taylor3473f882001-02-23 17:55:21 +00001788 }
1789 return(ret);
1790}
1791
1792/**
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001793 * xmlXPathNodeSetContains:
1794 * @cur: the node-set
1795 * @val: the node
1796 *
1797 * checks whether @cur contains @val
1798 *
1799 * Returns true (1) if @cur contains @val, false (0) otherwise
1800 */
1801int
1802xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) {
1803 int i;
1804
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001805 if (val->type == XML_NAMESPACE_DECL) {
1806 for (i = 0; i < cur->nodeNr; i++) {
1807 if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) {
1808 xmlNsPtr ns1, ns2;
1809
1810 ns1 = (xmlNsPtr) val;
1811 ns2 = (xmlNsPtr) cur->nodeTab[i];
1812 if (ns1 == ns2)
1813 return(1);
1814 if ((ns1->next != NULL) && (ns2->next == ns1->next) &&
1815 (xmlStrEqual(ns1->prefix, ns2->prefix)))
1816 return(1);
1817 }
1818 }
1819 } else {
1820 for (i = 0; i < cur->nodeNr; i++) {
1821 if (cur->nodeTab[i] == val)
1822 return(1);
1823 }
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001824 }
1825 return(0);
1826}
1827
1828/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001829 * xmlXPathNodeSetAddNs:
1830 * @cur: the initial node set
1831 * @node: the hosting node
1832 * @ns: a the namespace node
1833 *
1834 * add a new namespace node to an existing NodeSet
1835 */
Aleksey Sanin79376ba2002-05-14 06:41:32 +00001836void
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001837xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) {
1838 int i;
1839
1840 if ((ns == NULL) || (node == NULL) || (ns->type != XML_NAMESPACE_DECL) ||
1841 (node->type != XML_ELEMENT_NODE))
1842 return;
1843
William M. Brack08171912003-12-29 02:52:11 +00001844 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001845 /*
William M. Brack08171912003-12-29 02:52:11 +00001846 * prevent duplicates
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001847 */
1848 for (i = 0;i < cur->nodeNr;i++) {
1849 if ((cur->nodeTab[i] != NULL) &&
1850 (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) &&
Daniel Veillardc62a1472002-03-19 18:35:12 +00001851 (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) &&
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001852 (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix)))
1853 return;
1854 }
1855
1856 /*
1857 * grow the nodeTab if needed
1858 */
1859 if (cur->nodeMax == 0) {
1860 cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1861 sizeof(xmlNodePtr));
1862 if (cur->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001863 xmlXPathErrMemory(NULL, "growing nodeset\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001864 return;
1865 }
1866 memset(cur->nodeTab, 0 ,
1867 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1868 cur->nodeMax = XML_NODESET_DEFAULT;
1869 } else if (cur->nodeNr == cur->nodeMax) {
1870 xmlNodePtr *temp;
1871
1872 cur->nodeMax *= 2;
1873 temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
1874 sizeof(xmlNodePtr));
1875 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001876 xmlXPathErrMemory(NULL, "growing nodeset\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001877 return;
1878 }
1879 cur->nodeTab = temp;
1880 }
1881 cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns);
1882}
1883
1884/**
Owen Taylor3473f882001-02-23 17:55:21 +00001885 * xmlXPathNodeSetAdd:
1886 * @cur: the initial node set
1887 * @val: a new xmlNodePtr
1888 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001889 * add a new xmlNodePtr to an existing NodeSet
Owen Taylor3473f882001-02-23 17:55:21 +00001890 */
1891void
1892xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) {
1893 int i;
1894
1895 if (val == NULL) return;
1896
Daniel Veillardef0b4502003-03-24 13:57:34 +00001897#if 0
Daniel Veillard652d8a92003-02-04 19:28:49 +00001898 if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' '))
1899 return; /* an XSLT fake node */
Daniel Veillardef0b4502003-03-24 13:57:34 +00001900#endif
Daniel Veillard652d8a92003-02-04 19:28:49 +00001901
William M. Brack08171912003-12-29 02:52:11 +00001902 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00001903 /*
William M. Brack08171912003-12-29 02:52:11 +00001904 * prevent duplcates
Owen Taylor3473f882001-02-23 17:55:21 +00001905 */
1906 for (i = 0;i < cur->nodeNr;i++)
1907 if (cur->nodeTab[i] == val) return;
1908
1909 /*
1910 * grow the nodeTab if needed
1911 */
1912 if (cur->nodeMax == 0) {
1913 cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1914 sizeof(xmlNodePtr));
1915 if (cur->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001916 xmlXPathErrMemory(NULL, "growing nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001917 return;
1918 }
1919 memset(cur->nodeTab, 0 ,
1920 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1921 cur->nodeMax = XML_NODESET_DEFAULT;
1922 } else if (cur->nodeNr == cur->nodeMax) {
1923 xmlNodePtr *temp;
1924
1925 cur->nodeMax *= 2;
1926 temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
1927 sizeof(xmlNodePtr));
1928 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001929 xmlXPathErrMemory(NULL, "growing nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001930 return;
1931 }
1932 cur->nodeTab = temp;
1933 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001934 if (val->type == XML_NAMESPACE_DECL) {
1935 xmlNsPtr ns = (xmlNsPtr) val;
1936
1937 cur->nodeTab[cur->nodeNr++] =
1938 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1939 } else
1940 cur->nodeTab[cur->nodeNr++] = val;
Owen Taylor3473f882001-02-23 17:55:21 +00001941}
1942
1943/**
1944 * xmlXPathNodeSetAddUnique:
1945 * @cur: the initial node set
1946 * @val: a new xmlNodePtr
1947 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001948 * add a new xmlNodePtr to an existing NodeSet, optimized version
Owen Taylor3473f882001-02-23 17:55:21 +00001949 * when we are sure the node is not already in the set.
1950 */
1951void
1952xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) {
1953 if (val == NULL) return;
1954
Daniel Veillardef0b4502003-03-24 13:57:34 +00001955#if 0
Daniel Veillard652d8a92003-02-04 19:28:49 +00001956 if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' '))
1957 return; /* an XSLT fake node */
Daniel Veillardef0b4502003-03-24 13:57:34 +00001958#endif
Daniel Veillard652d8a92003-02-04 19:28:49 +00001959
William M. Brack08171912003-12-29 02:52:11 +00001960 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00001961 /*
1962 * grow the nodeTab if needed
1963 */
1964 if (cur->nodeMax == 0) {
1965 cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1966 sizeof(xmlNodePtr));
1967 if (cur->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001968 xmlXPathErrMemory(NULL, "growing nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001969 return;
1970 }
1971 memset(cur->nodeTab, 0 ,
1972 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1973 cur->nodeMax = XML_NODESET_DEFAULT;
1974 } else if (cur->nodeNr == cur->nodeMax) {
1975 xmlNodePtr *temp;
1976
1977 cur->nodeMax *= 2;
1978 temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
1979 sizeof(xmlNodePtr));
1980 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001981 xmlXPathErrMemory(NULL, "growing nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001982 return;
1983 }
1984 cur->nodeTab = temp;
1985 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001986 if (val->type == XML_NAMESPACE_DECL) {
1987 xmlNsPtr ns = (xmlNsPtr) val;
1988
1989 cur->nodeTab[cur->nodeNr++] =
1990 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1991 } else
1992 cur->nodeTab[cur->nodeNr++] = val;
Owen Taylor3473f882001-02-23 17:55:21 +00001993}
1994
1995/**
1996 * xmlXPathNodeSetMerge:
1997 * @val1: the first NodeSet or NULL
1998 * @val2: the second NodeSet
1999 *
2000 * Merges two nodesets, all nodes from @val2 are added to @val1
2001 * if @val1 is NULL, a new set is created and copied from @val2
2002 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002003 * Returns @val1 once extended or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002004 */
2005xmlNodeSetPtr
2006xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00002007 int i, j, initNr, skip;
Owen Taylor3473f882001-02-23 17:55:21 +00002008
2009 if (val2 == NULL) return(val1);
2010 if (val1 == NULL) {
2011 val1 = xmlXPathNodeSetCreate(NULL);
2012 }
2013
William M. Brack08171912003-12-29 02:52:11 +00002014 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00002015 initNr = val1->nodeNr;
2016
2017 for (i = 0;i < val2->nodeNr;i++) {
2018 /*
William M. Brack08171912003-12-29 02:52:11 +00002019 * check against duplicates
Owen Taylor3473f882001-02-23 17:55:21 +00002020 */
Daniel Veillardd8df6c02001-04-05 16:54:14 +00002021 skip = 0;
2022 for (j = 0; j < initNr; j++) {
2023 if (val1->nodeTab[j] == val2->nodeTab[i]) {
2024 skip = 1;
2025 break;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002026 } else if ((val1->nodeTab[j]->type == XML_NAMESPACE_DECL) &&
2027 (val2->nodeTab[i]->type == XML_NAMESPACE_DECL)) {
2028 xmlNsPtr ns1, ns2;
2029 ns1 = (xmlNsPtr) val1->nodeTab[j];
2030 ns2 = (xmlNsPtr) val2->nodeTab[i];
2031 if ((ns1->next == ns2->next) &&
2032 (xmlStrEqual(ns1->prefix, ns2->prefix))) {
2033 skip = 1;
2034 break;
2035 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00002036 }
2037 }
2038 if (skip)
2039 continue;
Owen Taylor3473f882001-02-23 17:55:21 +00002040
2041 /*
2042 * grow the nodeTab if needed
2043 */
2044 if (val1->nodeMax == 0) {
2045 val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
2046 sizeof(xmlNodePtr));
2047 if (val1->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002048 xmlXPathErrMemory(NULL, "merging nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002049 return(NULL);
2050 }
2051 memset(val1->nodeTab, 0 ,
2052 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
2053 val1->nodeMax = XML_NODESET_DEFAULT;
2054 } else if (val1->nodeNr == val1->nodeMax) {
2055 xmlNodePtr *temp;
2056
2057 val1->nodeMax *= 2;
2058 temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax *
2059 sizeof(xmlNodePtr));
2060 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002061 xmlXPathErrMemory(NULL, "merging nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002062 return(NULL);
2063 }
2064 val1->nodeTab = temp;
2065 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002066 if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) {
2067 xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i];
2068
2069 val1->nodeTab[val1->nodeNr++] =
2070 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
2071 } else
2072 val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i];
Owen Taylor3473f882001-02-23 17:55:21 +00002073 }
2074
2075 return(val1);
2076}
2077
2078/**
Daniel Veillard75be0132002-03-13 10:03:35 +00002079 * xmlXPathNodeSetMergeUnique:
2080 * @val1: the first NodeSet or NULL
2081 * @val2: the second NodeSet
2082 *
2083 * Merges two nodesets, all nodes from @val2 are added to @val1
2084 * if @val1 is NULL, a new set is created and copied from @val2
2085 *
2086 * Returns @val1 once extended or NULL in case of error.
2087 */
2088static xmlNodeSetPtr
2089xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
William M. Brack78637da2003-07-31 14:47:38 +00002090 int i;
Daniel Veillard75be0132002-03-13 10:03:35 +00002091
2092 if (val2 == NULL) return(val1);
2093 if (val1 == NULL) {
2094 val1 = xmlXPathNodeSetCreate(NULL);
2095 }
2096
William M. Brack08171912003-12-29 02:52:11 +00002097 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Daniel Veillard75be0132002-03-13 10:03:35 +00002098
2099 for (i = 0;i < val2->nodeNr;i++) {
2100 /*
2101 * grow the nodeTab if needed
2102 */
2103 if (val1->nodeMax == 0) {
2104 val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
2105 sizeof(xmlNodePtr));
2106 if (val1->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002107 xmlXPathErrMemory(NULL, "merging nodeset\n");
Daniel Veillard75be0132002-03-13 10:03:35 +00002108 return(NULL);
2109 }
2110 memset(val1->nodeTab, 0 ,
2111 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
2112 val1->nodeMax = XML_NODESET_DEFAULT;
2113 } else if (val1->nodeNr == val1->nodeMax) {
2114 xmlNodePtr *temp;
2115
2116 val1->nodeMax *= 2;
2117 temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax *
2118 sizeof(xmlNodePtr));
2119 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002120 xmlXPathErrMemory(NULL, "merging nodeset\n");
Daniel Veillard75be0132002-03-13 10:03:35 +00002121 return(NULL);
2122 }
2123 val1->nodeTab = temp;
2124 }
2125 if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) {
2126 xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i];
2127
2128 val1->nodeTab[val1->nodeNr++] =
2129 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
2130 } else
2131 val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i];
2132 }
2133
2134 return(val1);
2135}
2136
2137/**
Owen Taylor3473f882001-02-23 17:55:21 +00002138 * xmlXPathNodeSetDel:
2139 * @cur: the initial node set
2140 * @val: an xmlNodePtr
2141 *
2142 * Removes an xmlNodePtr from an existing NodeSet
2143 */
2144void
2145xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) {
2146 int i;
2147
2148 if (cur == NULL) return;
2149 if (val == NULL) return;
2150
2151 /*
William M. Brack08171912003-12-29 02:52:11 +00002152 * find node in nodeTab
Owen Taylor3473f882001-02-23 17:55:21 +00002153 */
2154 for (i = 0;i < cur->nodeNr;i++)
2155 if (cur->nodeTab[i] == val) break;
2156
William M. Brack08171912003-12-29 02:52:11 +00002157 if (i >= cur->nodeNr) { /* not found */
Owen Taylor3473f882001-02-23 17:55:21 +00002158#ifdef DEBUG
2159 xmlGenericError(xmlGenericErrorContext,
2160 "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n",
2161 val->name);
2162#endif
2163 return;
2164 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002165 if ((cur->nodeTab[i] != NULL) &&
2166 (cur->nodeTab[i]->type == XML_NAMESPACE_DECL))
2167 xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00002168 cur->nodeNr--;
2169 for (;i < cur->nodeNr;i++)
2170 cur->nodeTab[i] = cur->nodeTab[i + 1];
2171 cur->nodeTab[cur->nodeNr] = NULL;
2172}
2173
2174/**
2175 * xmlXPathNodeSetRemove:
2176 * @cur: the initial node set
2177 * @val: the index to remove
2178 *
2179 * Removes an entry from an existing NodeSet list.
2180 */
2181void
2182xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) {
2183 if (cur == NULL) return;
2184 if (val >= cur->nodeNr) return;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002185 if ((cur->nodeTab[val] != NULL) &&
2186 (cur->nodeTab[val]->type == XML_NAMESPACE_DECL))
2187 xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]);
Owen Taylor3473f882001-02-23 17:55:21 +00002188 cur->nodeNr--;
2189 for (;val < cur->nodeNr;val++)
2190 cur->nodeTab[val] = cur->nodeTab[val + 1];
2191 cur->nodeTab[cur->nodeNr] = NULL;
2192}
2193
2194/**
2195 * xmlXPathFreeNodeSet:
2196 * @obj: the xmlNodeSetPtr to free
2197 *
2198 * Free the NodeSet compound (not the actual nodes !).
2199 */
2200void
2201xmlXPathFreeNodeSet(xmlNodeSetPtr obj) {
2202 if (obj == NULL) return;
2203 if (obj->nodeTab != NULL) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002204 int i;
2205
William M. Brack08171912003-12-29 02:52:11 +00002206 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002207 for (i = 0;i < obj->nodeNr;i++)
2208 if ((obj->nodeTab[i] != NULL) &&
2209 (obj->nodeTab[i]->type == XML_NAMESPACE_DECL))
2210 xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00002211 xmlFree(obj->nodeTab);
2212 }
Owen Taylor3473f882001-02-23 17:55:21 +00002213 xmlFree(obj);
2214}
2215
2216/**
2217 * xmlXPathFreeValueTree:
2218 * @obj: the xmlNodeSetPtr to free
2219 *
2220 * Free the NodeSet compound and the actual tree, this is different
2221 * from xmlXPathFreeNodeSet()
2222 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002223static void
Owen Taylor3473f882001-02-23 17:55:21 +00002224xmlXPathFreeValueTree(xmlNodeSetPtr obj) {
2225 int i;
2226
2227 if (obj == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002228
2229 if (obj->nodeTab != NULL) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002230 for (i = 0;i < obj->nodeNr;i++) {
2231 if (obj->nodeTab[i] != NULL) {
2232 if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) {
2233 xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]);
2234 } else {
2235 xmlFreeNodeList(obj->nodeTab[i]);
2236 }
2237 }
2238 }
Owen Taylor3473f882001-02-23 17:55:21 +00002239 xmlFree(obj->nodeTab);
2240 }
Owen Taylor3473f882001-02-23 17:55:21 +00002241 xmlFree(obj);
2242}
2243
2244#if defined(DEBUG) || defined(DEBUG_STEP)
2245/**
2246 * xmlGenericErrorContextNodeSet:
2247 * @output: a FILE * for the output
William M. Brack08171912003-12-29 02:52:11 +00002248 * @obj: the xmlNodeSetPtr to display
Owen Taylor3473f882001-02-23 17:55:21 +00002249 *
2250 * Quick display of a NodeSet
2251 */
2252void
2253xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) {
2254 int i;
2255
2256 if (output == NULL) output = xmlGenericErrorContext;
2257 if (obj == NULL) {
2258 fprintf(output, "NodeSet == NULL !\n");
2259 return;
2260 }
2261 if (obj->nodeNr == 0) {
2262 fprintf(output, "NodeSet is empty\n");
2263 return;
2264 }
2265 if (obj->nodeTab == NULL) {
2266 fprintf(output, " nodeTab == NULL !\n");
2267 return;
2268 }
2269 for (i = 0; i < obj->nodeNr; i++) {
2270 if (obj->nodeTab[i] == NULL) {
2271 fprintf(output, " NULL !\n");
2272 return;
2273 }
2274 if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) ||
2275 (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE))
2276 fprintf(output, " /");
2277 else if (obj->nodeTab[i]->name == NULL)
2278 fprintf(output, " noname!");
2279 else fprintf(output, " %s", obj->nodeTab[i]->name);
2280 }
2281 fprintf(output, "\n");
2282}
2283#endif
2284
2285/**
2286 * xmlXPathNewNodeSet:
2287 * @val: the NodePtr value
2288 *
2289 * Create a new xmlXPathObjectPtr of type NodeSet and initialize
2290 * it with the single Node @val
2291 *
2292 * Returns the newly created object.
2293 */
2294xmlXPathObjectPtr
2295xmlXPathNewNodeSet(xmlNodePtr val) {
2296 xmlXPathObjectPtr ret;
2297
2298 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2299 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002300 xmlXPathErrMemory(NULL, "creating nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002301 return(NULL);
2302 }
2303 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2304 ret->type = XPATH_NODESET;
Daniel Veillard77851712001-02-27 21:54:07 +00002305 ret->boolval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002306 ret->nodesetval = xmlXPathNodeSetCreate(val);
William M. Brack08171912003-12-29 02:52:11 +00002307 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00002308 return(ret);
2309}
2310
2311/**
2312 * xmlXPathNewValueTree:
2313 * @val: the NodePtr value
2314 *
2315 * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize
2316 * it with the tree root @val
2317 *
2318 * Returns the newly created object.
2319 */
2320xmlXPathObjectPtr
2321xmlXPathNewValueTree(xmlNodePtr val) {
2322 xmlXPathObjectPtr ret;
2323
2324 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2325 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002326 xmlXPathErrMemory(NULL, "creating result value tree\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002327 return(NULL);
2328 }
2329 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2330 ret->type = XPATH_XSLT_TREE;
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00002331 ret->boolval = 1;
2332 ret->user = (void *) val;
Owen Taylor3473f882001-02-23 17:55:21 +00002333 ret->nodesetval = xmlXPathNodeSetCreate(val);
2334 return(ret);
2335}
2336
2337/**
2338 * xmlXPathNewNodeSetList:
2339 * @val: an existing NodeSet
2340 *
2341 * Create a new xmlXPathObjectPtr of type NodeSet and initialize
2342 * it with the Nodeset @val
2343 *
2344 * Returns the newly created object.
2345 */
2346xmlXPathObjectPtr
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002347xmlXPathNewNodeSetList(xmlNodeSetPtr val)
2348{
Owen Taylor3473f882001-02-23 17:55:21 +00002349 xmlXPathObjectPtr ret;
2350 int i;
2351
2352 if (val == NULL)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002353 ret = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002354 else if (val->nodeTab == NULL)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002355 ret = xmlXPathNewNodeSet(NULL);
2356 else {
2357 ret = xmlXPathNewNodeSet(val->nodeTab[0]);
2358 for (i = 1; i < val->nodeNr; ++i)
2359 xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]);
2360 }
Owen Taylor3473f882001-02-23 17:55:21 +00002361
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002362 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002363}
2364
2365/**
2366 * xmlXPathWrapNodeSet:
2367 * @val: the NodePtr value
2368 *
2369 * Wrap the Nodeset @val in a new xmlXPathObjectPtr
2370 *
2371 * Returns the newly created object.
2372 */
2373xmlXPathObjectPtr
2374xmlXPathWrapNodeSet(xmlNodeSetPtr val) {
2375 xmlXPathObjectPtr ret;
2376
2377 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2378 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002379 xmlXPathErrMemory(NULL, "creating node set object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002380 return(NULL);
2381 }
2382 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2383 ret->type = XPATH_NODESET;
2384 ret->nodesetval = val;
2385 return(ret);
2386}
2387
2388/**
2389 * xmlXPathFreeNodeSetList:
2390 * @obj: an existing NodeSetList object
2391 *
2392 * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in
2393 * the list contrary to xmlXPathFreeObject().
2394 */
2395void
2396xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) {
2397 if (obj == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002398 xmlFree(obj);
2399}
2400
Thomas Broyerf06a3d82001-07-16 04:52:57 +00002401/**
2402 * xmlXPathDifference:
2403 * @nodes1: a node-set
2404 * @nodes2: a node-set
2405 *
2406 * Implements the EXSLT - Sets difference() function:
2407 * node-set set:difference (node-set, node-set)
2408 *
2409 * Returns the difference between the two node sets, or nodes1 if
2410 * nodes2 is empty
2411 */
2412xmlNodeSetPtr
2413xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2414 xmlNodeSetPtr ret;
2415 int i, l1;
2416 xmlNodePtr cur;
2417
2418 if (xmlXPathNodeSetIsEmpty(nodes2))
2419 return(nodes1);
2420
2421 ret = xmlXPathNodeSetCreate(NULL);
2422 if (xmlXPathNodeSetIsEmpty(nodes1))
2423 return(ret);
2424
2425 l1 = xmlXPathNodeSetGetLength(nodes1);
2426
2427 for (i = 0; i < l1; i++) {
2428 cur = xmlXPathNodeSetItem(nodes1, i);
2429 if (!xmlXPathNodeSetContains(nodes2, cur))
2430 xmlXPathNodeSetAddUnique(ret, cur);
2431 }
2432 return(ret);
2433}
2434
2435/**
2436 * xmlXPathIntersection:
2437 * @nodes1: a node-set
2438 * @nodes2: a node-set
2439 *
2440 * Implements the EXSLT - Sets intersection() function:
2441 * node-set set:intersection (node-set, node-set)
2442 *
2443 * Returns a node set comprising the nodes that are within both the
2444 * node sets passed as arguments
2445 */
2446xmlNodeSetPtr
2447xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2448 xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL);
2449 int i, l1;
2450 xmlNodePtr cur;
2451
2452 if (xmlXPathNodeSetIsEmpty(nodes1))
2453 return(ret);
2454 if (xmlXPathNodeSetIsEmpty(nodes2))
2455 return(ret);
2456
2457 l1 = xmlXPathNodeSetGetLength(nodes1);
2458
2459 for (i = 0; i < l1; i++) {
2460 cur = xmlXPathNodeSetItem(nodes1, i);
2461 if (xmlXPathNodeSetContains(nodes2, cur))
2462 xmlXPathNodeSetAddUnique(ret, cur);
2463 }
2464 return(ret);
2465}
2466
2467/**
2468 * xmlXPathDistinctSorted:
2469 * @nodes: a node-set, sorted by document order
2470 *
2471 * Implements the EXSLT - Sets distinct() function:
2472 * node-set set:distinct (node-set)
2473 *
2474 * Returns a subset of the nodes contained in @nodes, or @nodes if
2475 * it is empty
2476 */
2477xmlNodeSetPtr
2478xmlXPathDistinctSorted (xmlNodeSetPtr nodes) {
2479 xmlNodeSetPtr ret;
2480 xmlHashTablePtr hash;
2481 int i, l;
2482 xmlChar * strval;
2483 xmlNodePtr cur;
2484
2485 if (xmlXPathNodeSetIsEmpty(nodes))
2486 return(nodes);
2487
2488 ret = xmlXPathNodeSetCreate(NULL);
2489 l = xmlXPathNodeSetGetLength(nodes);
2490 hash = xmlHashCreate (l);
2491 for (i = 0; i < l; i++) {
2492 cur = xmlXPathNodeSetItem(nodes, i);
2493 strval = xmlXPathCastNodeToString(cur);
2494 if (xmlHashLookup(hash, strval) == NULL) {
2495 xmlHashAddEntry(hash, strval, strval);
2496 xmlXPathNodeSetAddUnique(ret, cur);
2497 } else {
2498 xmlFree(strval);
2499 }
2500 }
2501 xmlHashFree(hash, (xmlHashDeallocator) xmlFree);
2502 return(ret);
2503}
2504
2505/**
2506 * xmlXPathDistinct:
2507 * @nodes: a node-set
2508 *
2509 * Implements the EXSLT - Sets distinct() function:
2510 * node-set set:distinct (node-set)
2511 * @nodes is sorted by document order, then #exslSetsDistinctSorted
2512 * is called with the sorted node-set
2513 *
2514 * Returns a subset of the nodes contained in @nodes, or @nodes if
2515 * it is empty
2516 */
2517xmlNodeSetPtr
2518xmlXPathDistinct (xmlNodeSetPtr nodes) {
2519 if (xmlXPathNodeSetIsEmpty(nodes))
2520 return(nodes);
2521
2522 xmlXPathNodeSetSort(nodes);
2523 return(xmlXPathDistinctSorted(nodes));
2524}
2525
2526/**
2527 * xmlXPathHasSameNodes:
2528 * @nodes1: a node-set
2529 * @nodes2: a node-set
2530 *
2531 * Implements the EXSLT - Sets has-same-nodes function:
2532 * boolean set:has-same-node(node-set, node-set)
2533 *
2534 * Returns true (1) if @nodes1 shares any node with @nodes2, false (0)
2535 * otherwise
2536 */
2537int
2538xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2539 int i, l;
2540 xmlNodePtr cur;
2541
2542 if (xmlXPathNodeSetIsEmpty(nodes1) ||
2543 xmlXPathNodeSetIsEmpty(nodes2))
2544 return(0);
2545
2546 l = xmlXPathNodeSetGetLength(nodes1);
2547 for (i = 0; i < l; i++) {
2548 cur = xmlXPathNodeSetItem(nodes1, i);
2549 if (xmlXPathNodeSetContains(nodes2, cur))
2550 return(1);
2551 }
2552 return(0);
2553}
2554
2555/**
2556 * xmlXPathNodeLeadingSorted:
2557 * @nodes: a node-set, sorted by document order
2558 * @node: a node
2559 *
2560 * Implements the EXSLT - Sets leading() function:
2561 * node-set set:leading (node-set, node-set)
2562 *
2563 * Returns the nodes in @nodes that precede @node in document order,
2564 * @nodes if @node is NULL or an empty node-set if @nodes
2565 * doesn't contain @node
2566 */
2567xmlNodeSetPtr
2568xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) {
2569 int i, l;
2570 xmlNodePtr cur;
2571 xmlNodeSetPtr ret;
2572
2573 if (node == NULL)
2574 return(nodes);
2575
2576 ret = xmlXPathNodeSetCreate(NULL);
2577 if (xmlXPathNodeSetIsEmpty(nodes) ||
2578 (!xmlXPathNodeSetContains(nodes, node)))
2579 return(ret);
2580
2581 l = xmlXPathNodeSetGetLength(nodes);
2582 for (i = 0; i < l; i++) {
2583 cur = xmlXPathNodeSetItem(nodes, i);
2584 if (cur == node)
2585 break;
2586 xmlXPathNodeSetAddUnique(ret, cur);
2587 }
2588 return(ret);
2589}
2590
2591/**
2592 * xmlXPathNodeLeading:
2593 * @nodes: a node-set
2594 * @node: a node
2595 *
2596 * Implements the EXSLT - Sets leading() function:
2597 * node-set set:leading (node-set, node-set)
2598 * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted
2599 * is called.
2600 *
2601 * Returns the nodes in @nodes that precede @node in document order,
2602 * @nodes if @node is NULL or an empty node-set if @nodes
2603 * doesn't contain @node
2604 */
2605xmlNodeSetPtr
2606xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) {
2607 xmlXPathNodeSetSort(nodes);
2608 return(xmlXPathNodeLeadingSorted(nodes, node));
2609}
2610
2611/**
2612 * xmlXPathLeadingSorted:
2613 * @nodes1: a node-set, sorted by document order
2614 * @nodes2: a node-set, sorted by document order
2615 *
2616 * Implements the EXSLT - Sets leading() function:
2617 * node-set set:leading (node-set, node-set)
2618 *
2619 * Returns the nodes in @nodes1 that precede the first node in @nodes2
2620 * in document order, @nodes1 if @nodes2 is NULL or empty or
2621 * an empty node-set if @nodes1 doesn't contain @nodes2
2622 */
2623xmlNodeSetPtr
2624xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2625 if (xmlXPathNodeSetIsEmpty(nodes2))
2626 return(nodes1);
2627 return(xmlXPathNodeLeadingSorted(nodes1,
2628 xmlXPathNodeSetItem(nodes2, 1)));
2629}
2630
2631/**
2632 * xmlXPathLeading:
2633 * @nodes1: a node-set
2634 * @nodes2: a node-set
2635 *
2636 * Implements the EXSLT - Sets leading() function:
2637 * node-set set:leading (node-set, node-set)
2638 * @nodes1 and @nodes2 are sorted by document order, then
2639 * #exslSetsLeadingSorted is called.
2640 *
2641 * Returns the nodes in @nodes1 that precede the first node in @nodes2
2642 * in document order, @nodes1 if @nodes2 is NULL or empty or
2643 * an empty node-set if @nodes1 doesn't contain @nodes2
2644 */
2645xmlNodeSetPtr
2646xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2647 if (xmlXPathNodeSetIsEmpty(nodes2))
2648 return(nodes1);
2649 if (xmlXPathNodeSetIsEmpty(nodes1))
2650 return(xmlXPathNodeSetCreate(NULL));
2651 xmlXPathNodeSetSort(nodes1);
2652 xmlXPathNodeSetSort(nodes2);
2653 return(xmlXPathNodeLeadingSorted(nodes1,
2654 xmlXPathNodeSetItem(nodes2, 1)));
2655}
2656
2657/**
2658 * xmlXPathNodeTrailingSorted:
2659 * @nodes: a node-set, sorted by document order
2660 * @node: a node
2661 *
2662 * Implements the EXSLT - Sets trailing() function:
2663 * node-set set:trailing (node-set, node-set)
2664 *
2665 * Returns the nodes in @nodes that follow @node in document order,
2666 * @nodes if @node is NULL or an empty node-set if @nodes
2667 * doesn't contain @node
2668 */
2669xmlNodeSetPtr
2670xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) {
2671 int i, l;
2672 xmlNodePtr cur;
2673 xmlNodeSetPtr ret;
2674
2675 if (node == NULL)
2676 return(nodes);
2677
2678 ret = xmlXPathNodeSetCreate(NULL);
2679 if (xmlXPathNodeSetIsEmpty(nodes) ||
2680 (!xmlXPathNodeSetContains(nodes, node)))
2681 return(ret);
2682
2683 l = xmlXPathNodeSetGetLength(nodes);
Thomas Broyerf186c822001-07-31 23:30:37 +00002684 for (i = l; i > 0; i--) {
Thomas Broyerf06a3d82001-07-16 04:52:57 +00002685 cur = xmlXPathNodeSetItem(nodes, i);
2686 if (cur == node)
2687 break;
2688 xmlXPathNodeSetAddUnique(ret, cur);
2689 }
2690 return(ret);
2691}
2692
2693/**
2694 * xmlXPathNodeTrailing:
2695 * @nodes: a node-set
2696 * @node: a node
2697 *
2698 * Implements the EXSLT - Sets trailing() function:
2699 * node-set set:trailing (node-set, node-set)
2700 * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted
2701 * is called.
2702 *
2703 * Returns the nodes in @nodes that follow @node in document order,
2704 * @nodes if @node is NULL or an empty node-set if @nodes
2705 * doesn't contain @node
2706 */
2707xmlNodeSetPtr
2708xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) {
2709 xmlXPathNodeSetSort(nodes);
2710 return(xmlXPathNodeTrailingSorted(nodes, node));
2711}
2712
2713/**
2714 * xmlXPathTrailingSorted:
2715 * @nodes1: a node-set, sorted by document order
2716 * @nodes2: a node-set, sorted by document order
2717 *
2718 * Implements the EXSLT - Sets trailing() function:
2719 * node-set set:trailing (node-set, node-set)
2720 *
2721 * Returns the nodes in @nodes1 that follow the first node in @nodes2
2722 * in document order, @nodes1 if @nodes2 is NULL or empty or
2723 * an empty node-set if @nodes1 doesn't contain @nodes2
2724 */
2725xmlNodeSetPtr
2726xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2727 if (xmlXPathNodeSetIsEmpty(nodes2))
2728 return(nodes1);
2729 return(xmlXPathNodeTrailingSorted(nodes1,
2730 xmlXPathNodeSetItem(nodes2, 0)));
2731}
2732
2733/**
2734 * xmlXPathTrailing:
2735 * @nodes1: a node-set
2736 * @nodes2: a node-set
2737 *
2738 * Implements the EXSLT - Sets trailing() function:
2739 * node-set set:trailing (node-set, node-set)
2740 * @nodes1 and @nodes2 are sorted by document order, then
2741 * #xmlXPathTrailingSorted is called.
2742 *
2743 * Returns the nodes in @nodes1 that follow the first node in @nodes2
2744 * in document order, @nodes1 if @nodes2 is NULL or empty or
2745 * an empty node-set if @nodes1 doesn't contain @nodes2
2746 */
2747xmlNodeSetPtr
2748xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2749 if (xmlXPathNodeSetIsEmpty(nodes2))
2750 return(nodes1);
2751 if (xmlXPathNodeSetIsEmpty(nodes1))
2752 return(xmlXPathNodeSetCreate(NULL));
2753 xmlXPathNodeSetSort(nodes1);
2754 xmlXPathNodeSetSort(nodes2);
2755 return(xmlXPathNodeTrailingSorted(nodes1,
2756 xmlXPathNodeSetItem(nodes2, 0)));
2757}
2758
Owen Taylor3473f882001-02-23 17:55:21 +00002759/************************************************************************
2760 * *
2761 * Routines to handle extra functions *
2762 * *
2763 ************************************************************************/
2764
2765/**
2766 * xmlXPathRegisterFunc:
2767 * @ctxt: the XPath context
2768 * @name: the function name
2769 * @f: the function implementation or NULL
2770 *
2771 * Register a new function. If @f is NULL it unregisters the function
2772 *
2773 * Returns 0 in case of success, -1 in case of error
2774 */
2775int
2776xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name,
2777 xmlXPathFunction f) {
2778 return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f));
2779}
2780
2781/**
2782 * xmlXPathRegisterFuncNS:
2783 * @ctxt: the XPath context
2784 * @name: the function name
2785 * @ns_uri: the function namespace URI
2786 * @f: the function implementation or NULL
2787 *
2788 * Register a new function. If @f is NULL it unregisters the function
2789 *
2790 * Returns 0 in case of success, -1 in case of error
2791 */
2792int
2793xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2794 const xmlChar *ns_uri, xmlXPathFunction f) {
2795 if (ctxt == NULL)
2796 return(-1);
2797 if (name == NULL)
2798 return(-1);
2799
2800 if (ctxt->funcHash == NULL)
2801 ctxt->funcHash = xmlHashCreate(0);
2802 if (ctxt->funcHash == NULL)
2803 return(-1);
Daniel Veillard94394cd2003-10-29 17:07:51 +00002804 if (f == NULL)
2805 return(xmlHashRemoveEntry2(ctxt->funcHash, name, ns_uri, NULL));
Owen Taylor3473f882001-02-23 17:55:21 +00002806 return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, (void *) f));
2807}
2808
2809/**
Thomas Broyerba4ad322001-07-26 16:55:21 +00002810 * xmlXPathRegisterFuncLookup:
2811 * @ctxt: the XPath context
2812 * @f: the lookup function
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002813 * @funcCtxt: the lookup data
Thomas Broyerba4ad322001-07-26 16:55:21 +00002814 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002815 * Registers an external mechanism to do function lookup.
Thomas Broyerba4ad322001-07-26 16:55:21 +00002816 */
2817void
2818xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt,
2819 xmlXPathFuncLookupFunc f,
2820 void *funcCtxt) {
2821 if (ctxt == NULL)
2822 return;
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00002823 ctxt->funcLookupFunc = f;
Thomas Broyerba4ad322001-07-26 16:55:21 +00002824 ctxt->funcLookupData = funcCtxt;
2825}
2826
2827/**
Owen Taylor3473f882001-02-23 17:55:21 +00002828 * xmlXPathFunctionLookup:
2829 * @ctxt: the XPath context
2830 * @name: the function name
2831 *
2832 * Search in the Function array of the context for the given
2833 * function.
2834 *
2835 * Returns the xmlXPathFunction or NULL if not found
2836 */
2837xmlXPathFunction
2838xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) {
Thomas Broyerba4ad322001-07-26 16:55:21 +00002839 if (ctxt == NULL)
2840 return (NULL);
2841
2842 if (ctxt->funcLookupFunc != NULL) {
2843 xmlXPathFunction ret;
Daniel Veillard99e55eb2002-01-21 08:56:29 +00002844 xmlXPathFuncLookupFunc f;
Thomas Broyerba4ad322001-07-26 16:55:21 +00002845
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00002846 f = ctxt->funcLookupFunc;
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002847 ret = f(ctxt->funcLookupData, name, NULL);
Thomas Broyerba4ad322001-07-26 16:55:21 +00002848 if (ret != NULL)
2849 return(ret);
2850 }
Owen Taylor3473f882001-02-23 17:55:21 +00002851 return(xmlXPathFunctionLookupNS(ctxt, name, NULL));
2852}
2853
2854/**
2855 * xmlXPathFunctionLookupNS:
2856 * @ctxt: the XPath context
2857 * @name: the function name
2858 * @ns_uri: the function namespace URI
2859 *
2860 * Search in the Function array of the context for the given
2861 * function.
2862 *
2863 * Returns the xmlXPathFunction or NULL if not found
2864 */
2865xmlXPathFunction
2866xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2867 const xmlChar *ns_uri) {
2868 if (ctxt == NULL)
2869 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002870 if (name == NULL)
2871 return(NULL);
2872
Thomas Broyerba4ad322001-07-26 16:55:21 +00002873 if (ctxt->funcLookupFunc != NULL) {
2874 xmlXPathFunction ret;
Daniel Veillard99e55eb2002-01-21 08:56:29 +00002875 xmlXPathFuncLookupFunc f;
Thomas Broyerba4ad322001-07-26 16:55:21 +00002876
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00002877 f = ctxt->funcLookupFunc;
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002878 ret = f(ctxt->funcLookupData, name, ns_uri);
Thomas Broyerba4ad322001-07-26 16:55:21 +00002879 if (ret != NULL)
2880 return(ret);
2881 }
2882
2883 if (ctxt->funcHash == NULL)
2884 return(NULL);
2885
Owen Taylor3473f882001-02-23 17:55:21 +00002886 return((xmlXPathFunction) xmlHashLookup2(ctxt->funcHash, name, ns_uri));
2887}
2888
2889/**
2890 * xmlXPathRegisteredFuncsCleanup:
2891 * @ctxt: the XPath context
2892 *
2893 * Cleanup the XPath context data associated to registered functions
2894 */
2895void
2896xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) {
2897 if (ctxt == NULL)
2898 return;
2899
2900 xmlHashFree(ctxt->funcHash, NULL);
2901 ctxt->funcHash = NULL;
2902}
2903
2904/************************************************************************
2905 * *
William M. Brack08171912003-12-29 02:52:11 +00002906 * Routines to handle Variables *
Owen Taylor3473f882001-02-23 17:55:21 +00002907 * *
2908 ************************************************************************/
2909
2910/**
2911 * xmlXPathRegisterVariable:
2912 * @ctxt: the XPath context
2913 * @name: the variable name
2914 * @value: the variable value or NULL
2915 *
2916 * Register a new variable value. If @value is NULL it unregisters
2917 * the variable
2918 *
2919 * Returns 0 in case of success, -1 in case of error
2920 */
2921int
2922xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name,
2923 xmlXPathObjectPtr value) {
2924 return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value));
2925}
2926
2927/**
2928 * xmlXPathRegisterVariableNS:
2929 * @ctxt: the XPath context
2930 * @name: the variable name
2931 * @ns_uri: the variable namespace URI
2932 * @value: the variable value or NULL
2933 *
2934 * Register a new variable value. If @value is NULL it unregisters
2935 * the variable
2936 *
2937 * Returns 0 in case of success, -1 in case of error
2938 */
2939int
2940xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2941 const xmlChar *ns_uri,
2942 xmlXPathObjectPtr value) {
2943 if (ctxt == NULL)
2944 return(-1);
2945 if (name == NULL)
2946 return(-1);
2947
2948 if (ctxt->varHash == NULL)
2949 ctxt->varHash = xmlHashCreate(0);
2950 if (ctxt->varHash == NULL)
2951 return(-1);
Daniel Veillard94394cd2003-10-29 17:07:51 +00002952 if (value == NULL)
2953 return(xmlHashRemoveEntry2(ctxt->varHash, name, ns_uri,
2954 (xmlHashDeallocator)xmlXPathFreeObject));
Owen Taylor3473f882001-02-23 17:55:21 +00002955 return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri,
2956 (void *) value,
2957 (xmlHashDeallocator)xmlXPathFreeObject));
2958}
2959
2960/**
2961 * xmlXPathRegisterVariableLookup:
2962 * @ctxt: the XPath context
2963 * @f: the lookup function
2964 * @data: the lookup data
2965 *
2966 * register an external mechanism to do variable lookup
2967 */
2968void
2969xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt,
2970 xmlXPathVariableLookupFunc f, void *data) {
2971 if (ctxt == NULL)
2972 return;
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00002973 ctxt->varLookupFunc = f;
Owen Taylor3473f882001-02-23 17:55:21 +00002974 ctxt->varLookupData = data;
2975}
2976
2977/**
2978 * xmlXPathVariableLookup:
2979 * @ctxt: the XPath context
2980 * @name: the variable name
2981 *
2982 * Search in the Variable array of the context for the given
2983 * variable value.
2984 *
Daniel Veillard73c9c042001-07-05 20:02:54 +00002985 * Returns a copy of the value or NULL if not found
Owen Taylor3473f882001-02-23 17:55:21 +00002986 */
2987xmlXPathObjectPtr
2988xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) {
2989 if (ctxt == NULL)
2990 return(NULL);
2991
2992 if (ctxt->varLookupFunc != NULL) {
2993 xmlXPathObjectPtr ret;
2994
2995 ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc)
2996 (ctxt->varLookupData, name, NULL);
Daniel Veillard556c6682001-10-06 09:59:51 +00002997 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002998 }
2999 return(xmlXPathVariableLookupNS(ctxt, name, NULL));
3000}
3001
3002/**
3003 * xmlXPathVariableLookupNS:
3004 * @ctxt: the XPath context
3005 * @name: the variable name
3006 * @ns_uri: the variable namespace URI
3007 *
3008 * Search in the Variable array of the context for the given
Daniel Veillard73c9c042001-07-05 20:02:54 +00003009 * variable value.
Owen Taylor3473f882001-02-23 17:55:21 +00003010 *
Daniel Veillard73c9c042001-07-05 20:02:54 +00003011 * Returns the a copy of the value or NULL if not found
Owen Taylor3473f882001-02-23 17:55:21 +00003012 */
3013xmlXPathObjectPtr
3014xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name,
3015 const xmlChar *ns_uri) {
3016 if (ctxt == NULL)
3017 return(NULL);
3018
3019 if (ctxt->varLookupFunc != NULL) {
3020 xmlXPathObjectPtr ret;
3021
3022 ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc)
3023 (ctxt->varLookupData, name, ns_uri);
3024 if (ret != NULL) return(ret);
3025 }
3026
3027 if (ctxt->varHash == NULL)
3028 return(NULL);
3029 if (name == NULL)
3030 return(NULL);
3031
Daniel Veillard8c357d52001-07-03 23:43:33 +00003032 return(xmlXPathObjectCopy((xmlXPathObjectPtr)
3033 xmlHashLookup2(ctxt->varHash, name, ns_uri)));
Owen Taylor3473f882001-02-23 17:55:21 +00003034}
3035
3036/**
3037 * xmlXPathRegisteredVariablesCleanup:
3038 * @ctxt: the XPath context
3039 *
3040 * Cleanup the XPath context data associated to registered variables
3041 */
3042void
3043xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) {
3044 if (ctxt == NULL)
3045 return;
3046
Daniel Veillard76d66f42001-05-16 21:05:17 +00003047 xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject);
Owen Taylor3473f882001-02-23 17:55:21 +00003048 ctxt->varHash = NULL;
3049}
3050
3051/**
3052 * xmlXPathRegisterNs:
3053 * @ctxt: the XPath context
3054 * @prefix: the namespace prefix
3055 * @ns_uri: the namespace name
3056 *
3057 * Register a new namespace. If @ns_uri is NULL it unregisters
3058 * the namespace
3059 *
3060 * Returns 0 in case of success, -1 in case of error
3061 */
3062int
3063xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix,
3064 const xmlChar *ns_uri) {
3065 if (ctxt == NULL)
3066 return(-1);
3067 if (prefix == NULL)
3068 return(-1);
3069
3070 if (ctxt->nsHash == NULL)
3071 ctxt->nsHash = xmlHashCreate(10);
3072 if (ctxt->nsHash == NULL)
3073 return(-1);
Daniel Veillarde991fe92003-10-29 11:18:37 +00003074 if (ns_uri == NULL)
Daniel Veillard94394cd2003-10-29 17:07:51 +00003075 return(xmlHashRemoveEntry(ctxt->nsHash, prefix,
Daniel Veillarde991fe92003-10-29 11:18:37 +00003076 (xmlHashDeallocator)xmlFree));
Daniel Veillard42766c02002-08-22 20:52:17 +00003077 return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri),
Owen Taylor3473f882001-02-23 17:55:21 +00003078 (xmlHashDeallocator)xmlFree));
3079}
3080
3081/**
3082 * xmlXPathNsLookup:
3083 * @ctxt: the XPath context
3084 * @prefix: the namespace prefix value
3085 *
3086 * Search in the namespace declaration array of the context for the given
3087 * namespace name associated to the given prefix
3088 *
3089 * Returns the value or NULL if not found
3090 */
3091const xmlChar *
3092xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) {
3093 if (ctxt == NULL)
3094 return(NULL);
3095 if (prefix == NULL)
3096 return(NULL);
3097
3098#ifdef XML_XML_NAMESPACE
3099 if (xmlStrEqual(prefix, (const xmlChar *) "xml"))
3100 return(XML_XML_NAMESPACE);
3101#endif
3102
Daniel Veillardc8f620b2001-04-30 20:31:33 +00003103 if (ctxt->namespaces != NULL) {
3104 int i;
3105
3106 for (i = 0;i < ctxt->nsNr;i++) {
3107 if ((ctxt->namespaces[i] != NULL) &&
3108 (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix)))
3109 return(ctxt->namespaces[i]->href);
3110 }
3111 }
Owen Taylor3473f882001-02-23 17:55:21 +00003112
3113 return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix));
3114}
3115
3116/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003117 * xmlXPathRegisteredNsCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +00003118 * @ctxt: the XPath context
3119 *
3120 * Cleanup the XPath context data associated to registered variables
3121 */
3122void
3123xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) {
3124 if (ctxt == NULL)
3125 return;
3126
Daniel Veillard42766c02002-08-22 20:52:17 +00003127 xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree);
Owen Taylor3473f882001-02-23 17:55:21 +00003128 ctxt->nsHash = NULL;
3129}
3130
3131/************************************************************************
3132 * *
3133 * Routines to handle Values *
3134 * *
3135 ************************************************************************/
3136
William M. Brack08171912003-12-29 02:52:11 +00003137/* Allocations are terrible, one needs to optimize all this !!! */
Owen Taylor3473f882001-02-23 17:55:21 +00003138
3139/**
3140 * xmlXPathNewFloat:
3141 * @val: the double value
3142 *
3143 * Create a new xmlXPathObjectPtr of type double and of value @val
3144 *
3145 * Returns the newly created object.
3146 */
3147xmlXPathObjectPtr
3148xmlXPathNewFloat(double val) {
3149 xmlXPathObjectPtr ret;
3150
3151 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3152 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003153 xmlXPathErrMemory(NULL, "creating float object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003154 return(NULL);
3155 }
3156 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3157 ret->type = XPATH_NUMBER;
3158 ret->floatval = val;
3159 return(ret);
3160}
3161
3162/**
3163 * xmlXPathNewBoolean:
3164 * @val: the boolean value
3165 *
3166 * Create a new xmlXPathObjectPtr of type boolean and of value @val
3167 *
3168 * Returns the newly created object.
3169 */
3170xmlXPathObjectPtr
3171xmlXPathNewBoolean(int val) {
3172 xmlXPathObjectPtr ret;
3173
3174 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3175 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003176 xmlXPathErrMemory(NULL, "creating boolean object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003177 return(NULL);
3178 }
3179 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3180 ret->type = XPATH_BOOLEAN;
3181 ret->boolval = (val != 0);
3182 return(ret);
3183}
3184
3185/**
3186 * xmlXPathNewString:
3187 * @val: the xmlChar * value
3188 *
3189 * Create a new xmlXPathObjectPtr of type string and of value @val
3190 *
3191 * Returns the newly created object.
3192 */
3193xmlXPathObjectPtr
3194xmlXPathNewString(const xmlChar *val) {
3195 xmlXPathObjectPtr ret;
3196
3197 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3198 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003199 xmlXPathErrMemory(NULL, "creating string object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003200 return(NULL);
3201 }
3202 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3203 ret->type = XPATH_STRING;
3204 if (val != NULL)
3205 ret->stringval = xmlStrdup(val);
3206 else
3207 ret->stringval = xmlStrdup((const xmlChar *)"");
3208 return(ret);
3209}
3210
3211/**
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003212 * xmlXPathWrapString:
3213 * @val: the xmlChar * value
3214 *
3215 * Wraps the @val string into an XPath object.
3216 *
3217 * Returns the newly created object.
3218 */
3219xmlXPathObjectPtr
3220xmlXPathWrapString (xmlChar *val) {
3221 xmlXPathObjectPtr ret;
3222
3223 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3224 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003225 xmlXPathErrMemory(NULL, "creating string object\n");
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003226 return(NULL);
3227 }
3228 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3229 ret->type = XPATH_STRING;
3230 ret->stringval = val;
3231 return(ret);
3232}
3233
3234/**
Owen Taylor3473f882001-02-23 17:55:21 +00003235 * xmlXPathNewCString:
3236 * @val: the char * value
3237 *
3238 * Create a new xmlXPathObjectPtr of type string and of value @val
3239 *
3240 * Returns the newly created object.
3241 */
3242xmlXPathObjectPtr
3243xmlXPathNewCString(const char *val) {
3244 xmlXPathObjectPtr ret;
3245
3246 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3247 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003248 xmlXPathErrMemory(NULL, "creating string object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003249 return(NULL);
3250 }
3251 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3252 ret->type = XPATH_STRING;
3253 ret->stringval = xmlStrdup(BAD_CAST val);
3254 return(ret);
3255}
3256
3257/**
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003258 * xmlXPathWrapCString:
3259 * @val: the char * value
3260 *
3261 * Wraps a string into an XPath object.
3262 *
3263 * Returns the newly created object.
3264 */
3265xmlXPathObjectPtr
3266xmlXPathWrapCString (char * val) {
3267 return(xmlXPathWrapString((xmlChar *)(val)));
3268}
3269
3270/**
Thomas Broyerf06a3d82001-07-16 04:52:57 +00003271 * xmlXPathWrapExternal:
3272 * @val: the user data
3273 *
3274 * Wraps the @val data into an XPath object.
3275 *
3276 * Returns the newly created object.
3277 */
3278xmlXPathObjectPtr
3279xmlXPathWrapExternal (void *val) {
3280 xmlXPathObjectPtr ret;
3281
3282 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3283 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003284 xmlXPathErrMemory(NULL, "creating user object\n");
Thomas Broyerf06a3d82001-07-16 04:52:57 +00003285 return(NULL);
3286 }
3287 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3288 ret->type = XPATH_USERS;
3289 ret->user = val;
3290 return(ret);
3291}
3292
3293/**
Owen Taylor3473f882001-02-23 17:55:21 +00003294 * xmlXPathObjectCopy:
3295 * @val: the original object
3296 *
3297 * allocate a new copy of a given object
3298 *
3299 * Returns the newly created object.
3300 */
3301xmlXPathObjectPtr
3302xmlXPathObjectCopy(xmlXPathObjectPtr val) {
3303 xmlXPathObjectPtr ret;
3304
3305 if (val == NULL)
3306 return(NULL);
3307
3308 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3309 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003310 xmlXPathErrMemory(NULL, "copying object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003311 return(NULL);
3312 }
3313 memcpy(ret, val , (size_t) sizeof(xmlXPathObject));
3314 switch (val->type) {
3315 case XPATH_BOOLEAN:
3316 case XPATH_NUMBER:
3317 case XPATH_POINT:
3318 case XPATH_RANGE:
3319 break;
3320 case XPATH_STRING:
3321 ret->stringval = xmlStrdup(val->stringval);
3322 break;
3323 case XPATH_XSLT_TREE:
William M. Bracke9449c52004-07-11 14:41:20 +00003324#if 0
3325/*
3326 Removed 11 July 2004 - the current handling of xslt tmpRVT nodes means that
3327 this previous handling is no longer correct, and can cause some serious
3328 problems (ref. bug 145547)
3329*/
Owen Taylor3473f882001-02-23 17:55:21 +00003330 if ((val->nodesetval != NULL) &&
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003331 (val->nodesetval->nodeTab != NULL)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003332 xmlNodePtr cur, tmp;
3333 xmlDocPtr top;
Daniel Veillardef0b4502003-03-24 13:57:34 +00003334
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003335 ret->boolval = 1;
Daniel Veillard9adc0462003-03-24 18:39:54 +00003336 top = xmlNewDoc(NULL);
3337 top->name = (char *)
3338 xmlStrdup(val->nodesetval->nodeTab[0]->name);
Daniel Veillardef0b4502003-03-24 13:57:34 +00003339 ret->user = top;
3340 if (top != NULL) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003341 top->doc = top;
Daniel Veillardef0b4502003-03-24 13:57:34 +00003342 cur = val->nodesetval->nodeTab[0]->children;
3343 while (cur != NULL) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003344 tmp = xmlDocCopyNode(cur, top, 1);
3345 xmlAddChild((xmlNodePtr) top, tmp);
Daniel Veillardef0b4502003-03-24 13:57:34 +00003346 cur = cur->next;
3347 }
3348 }
William M. Bracke9449c52004-07-11 14:41:20 +00003349
Daniel Veillard9adc0462003-03-24 18:39:54 +00003350 ret->nodesetval = xmlXPathNodeSetCreate((xmlNodePtr) top);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003351 } else
Owen Taylor3473f882001-02-23 17:55:21 +00003352 ret->nodesetval = xmlXPathNodeSetCreate(NULL);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003353 /* Deallocate the copied tree value */
Owen Taylor3473f882001-02-23 17:55:21 +00003354 break;
William M. Bracke9449c52004-07-11 14:41:20 +00003355#endif
Owen Taylor3473f882001-02-23 17:55:21 +00003356 case XPATH_NODESET:
3357 ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003358 /* Do not deallocate the copied tree value */
3359 ret->boolval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003360 break;
3361 case XPATH_LOCATIONSET:
3362#ifdef LIBXML_XPTR_ENABLED
3363 {
3364 xmlLocationSetPtr loc = val->user;
3365 ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc);
3366 break;
3367 }
3368#endif
Thomas Broyer47334c02001-10-07 16:41:52 +00003369 case XPATH_USERS:
3370 ret->user = val->user;
3371 break;
3372 case XPATH_UNDEFINED:
Owen Taylor3473f882001-02-23 17:55:21 +00003373 xmlGenericError(xmlGenericErrorContext,
3374 "xmlXPathObjectCopy: unsupported type %d\n",
3375 val->type);
3376 break;
3377 }
3378 return(ret);
3379}
3380
3381/**
3382 * xmlXPathFreeObject:
3383 * @obj: the object to free
3384 *
3385 * Free up an xmlXPathObjectPtr object.
3386 */
3387void
3388xmlXPathFreeObject(xmlXPathObjectPtr obj) {
3389 if (obj == NULL) return;
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003390 if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) {
Daniel Veillard77851712001-02-27 21:54:07 +00003391 if (obj->boolval) {
William M. Bracke9449c52004-07-11 14:41:20 +00003392#if 0
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003393 if (obj->user != NULL) {
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003394 xmlXPathFreeNodeSet(obj->nodesetval);
Daniel Veillard38bf6f02002-03-16 22:03:31 +00003395 xmlFreeNodeList((xmlNodePtr) obj->user);
William M. Bracke9449c52004-07-11 14:41:20 +00003396 } else
3397#endif
3398 if (obj->nodesetval != NULL)
Daniel Veillard77851712001-02-27 21:54:07 +00003399 xmlXPathFreeValueTree(obj->nodesetval);
3400 } else {
3401 if (obj->nodesetval != NULL)
3402 xmlXPathFreeNodeSet(obj->nodesetval);
3403 }
Owen Taylor3473f882001-02-23 17:55:21 +00003404#ifdef LIBXML_XPTR_ENABLED
3405 } else if (obj->type == XPATH_LOCATIONSET) {
3406 if (obj->user != NULL)
3407 xmlXPtrFreeLocationSet(obj->user);
3408#endif
3409 } else if (obj->type == XPATH_STRING) {
3410 if (obj->stringval != NULL)
3411 xmlFree(obj->stringval);
Owen Taylor3473f882001-02-23 17:55:21 +00003412 }
3413
Owen Taylor3473f882001-02-23 17:55:21 +00003414 xmlFree(obj);
3415}
3416
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003417
3418/************************************************************************
3419 * *
3420 * Type Casting Routines *
3421 * *
3422 ************************************************************************/
3423
3424/**
3425 * xmlXPathCastBooleanToString:
3426 * @val: a boolean
3427 *
3428 * Converts a boolean to its string value.
3429 *
3430 * Returns a newly allocated string.
3431 */
3432xmlChar *
3433xmlXPathCastBooleanToString (int val) {
3434 xmlChar *ret;
3435 if (val)
3436 ret = xmlStrdup((const xmlChar *) "true");
3437 else
3438 ret = xmlStrdup((const xmlChar *) "false");
3439 return(ret);
3440}
3441
3442/**
3443 * xmlXPathCastNumberToString:
3444 * @val: a number
3445 *
3446 * Converts a number to its string value.
3447 *
3448 * Returns a newly allocated string.
3449 */
3450xmlChar *
3451xmlXPathCastNumberToString (double val) {
3452 xmlChar *ret;
Daniel Veillardcda96922001-08-21 10:56:31 +00003453 switch (xmlXPathIsInf(val)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003454 case 1:
Daniel Veillard5fc1f082002-03-27 09:05:40 +00003455 ret = xmlStrdup((const xmlChar *) "Infinity");
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003456 break;
3457 case -1:
3458 ret = xmlStrdup((const xmlChar *) "-Infinity");
3459 break;
3460 default:
Daniel Veillardcda96922001-08-21 10:56:31 +00003461 if (xmlXPathIsNaN(val)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003462 ret = xmlStrdup((const xmlChar *) "NaN");
Daniel Veillardd30be4a2002-03-28 18:25:31 +00003463 } else if (val == 0 && xmlXPathGetSign(val) != 0) {
3464 ret = xmlStrdup((const xmlChar *) "0");
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003465 } else {
3466 /* could be improved */
3467 char buf[100];
3468 xmlXPathFormatNumber(val, buf, 100);
3469 ret = xmlStrdup((const xmlChar *) buf);
3470 }
3471 }
3472 return(ret);
3473}
3474
3475/**
3476 * xmlXPathCastNodeToString:
3477 * @node: a node
3478 *
3479 * Converts a node to its string value.
3480 *
3481 * Returns a newly allocated string.
3482 */
3483xmlChar *
3484xmlXPathCastNodeToString (xmlNodePtr node) {
3485 return(xmlNodeGetContent(node));
3486}
3487
3488/**
3489 * xmlXPathCastNodeSetToString:
3490 * @ns: a node-set
3491 *
3492 * Converts a node-set to its string value.
3493 *
3494 * Returns a newly allocated string.
3495 */
3496xmlChar *
3497xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) {
3498 if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL))
3499 return(xmlStrdup((const xmlChar *) ""));
3500
3501 xmlXPathNodeSetSort(ns);
3502 return(xmlXPathCastNodeToString(ns->nodeTab[0]));
3503}
3504
3505/**
3506 * xmlXPathCastToString:
3507 * @val: an XPath object
3508 *
3509 * Converts an existing object to its string() equivalent
3510 *
3511 * Returns the string value of the object, NULL in case of error.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003512 * A new string is allocated only if needed (@val isn't a
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003513 * string object).
3514 */
3515xmlChar *
3516xmlXPathCastToString(xmlXPathObjectPtr val) {
3517 xmlChar *ret = NULL;
3518
3519 if (val == NULL)
3520 return(xmlStrdup((const xmlChar *) ""));
3521 switch (val->type) {
3522 case XPATH_UNDEFINED:
3523#ifdef DEBUG_EXPR
3524 xmlGenericError(xmlGenericErrorContext, "String: undefined\n");
3525#endif
3526 ret = xmlStrdup((const xmlChar *) "");
3527 break;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003528 case XPATH_NODESET:
William M. Brack0c022ad2002-07-12 00:56:01 +00003529 case XPATH_XSLT_TREE:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003530 ret = xmlXPathCastNodeSetToString(val->nodesetval);
3531 break;
3532 case XPATH_STRING:
Daniel Veillard4e2df542002-03-22 12:23:14 +00003533 return(xmlStrdup(val->stringval));
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003534 case XPATH_BOOLEAN:
3535 ret = xmlXPathCastBooleanToString(val->boolval);
3536 break;
3537 case XPATH_NUMBER: {
3538 ret = xmlXPathCastNumberToString(val->floatval);
3539 break;
3540 }
3541 case XPATH_USERS:
3542 case XPATH_POINT:
3543 case XPATH_RANGE:
3544 case XPATH_LOCATIONSET:
3545 TODO
3546 ret = xmlStrdup((const xmlChar *) "");
3547 break;
3548 }
3549 return(ret);
3550}
3551
3552/**
3553 * xmlXPathConvertString:
3554 * @val: an XPath object
3555 *
3556 * Converts an existing object to its string() equivalent
3557 *
3558 * Returns the new object, the old one is freed (or the operation
3559 * is done directly on @val)
3560 */
3561xmlXPathObjectPtr
3562xmlXPathConvertString(xmlXPathObjectPtr val) {
3563 xmlChar *res = NULL;
3564
3565 if (val == NULL)
3566 return(xmlXPathNewCString(""));
3567
3568 switch (val->type) {
3569 case XPATH_UNDEFINED:
3570#ifdef DEBUG_EXPR
3571 xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n");
3572#endif
3573 break;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003574 case XPATH_NODESET:
William M. Brack0c022ad2002-07-12 00:56:01 +00003575 case XPATH_XSLT_TREE:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003576 res = xmlXPathCastNodeSetToString(val->nodesetval);
3577 break;
3578 case XPATH_STRING:
3579 return(val);
3580 case XPATH_BOOLEAN:
3581 res = xmlXPathCastBooleanToString(val->boolval);
3582 break;
3583 case XPATH_NUMBER:
3584 res = xmlXPathCastNumberToString(val->floatval);
3585 break;
3586 case XPATH_USERS:
3587 case XPATH_POINT:
3588 case XPATH_RANGE:
3589 case XPATH_LOCATIONSET:
3590 TODO;
3591 break;
3592 }
3593 xmlXPathFreeObject(val);
3594 if (res == NULL)
3595 return(xmlXPathNewCString(""));
3596 return(xmlXPathWrapString(res));
3597}
3598
3599/**
3600 * xmlXPathCastBooleanToNumber:
3601 * @val: a boolean
3602 *
3603 * Converts a boolean to its number value
3604 *
3605 * Returns the number value
3606 */
3607double
3608xmlXPathCastBooleanToNumber(int val) {
3609 if (val)
3610 return(1.0);
3611 return(0.0);
3612}
3613
3614/**
3615 * xmlXPathCastStringToNumber:
3616 * @val: a string
3617 *
3618 * Converts a string to its number value
3619 *
3620 * Returns the number value
3621 */
3622double
3623xmlXPathCastStringToNumber(const xmlChar * val) {
3624 return(xmlXPathStringEvalNumber(val));
3625}
3626
3627/**
3628 * xmlXPathCastNodeToNumber:
3629 * @node: a node
3630 *
3631 * Converts a node to its number value
3632 *
3633 * Returns the number value
3634 */
3635double
3636xmlXPathCastNodeToNumber (xmlNodePtr node) {
3637 xmlChar *strval;
3638 double ret;
3639
3640 if (node == NULL)
3641 return(xmlXPathNAN);
3642 strval = xmlXPathCastNodeToString(node);
3643 if (strval == NULL)
3644 return(xmlXPathNAN);
3645 ret = xmlXPathCastStringToNumber(strval);
3646 xmlFree(strval);
3647
3648 return(ret);
3649}
3650
3651/**
3652 * xmlXPathCastNodeSetToNumber:
3653 * @ns: a node-set
3654 *
3655 * Converts a node-set to its number value
3656 *
3657 * Returns the number value
3658 */
3659double
3660xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) {
3661 xmlChar *str;
3662 double ret;
3663
3664 if (ns == NULL)
3665 return(xmlXPathNAN);
3666 str = xmlXPathCastNodeSetToString(ns);
3667 ret = xmlXPathCastStringToNumber(str);
3668 xmlFree(str);
3669 return(ret);
3670}
3671
3672/**
3673 * xmlXPathCastToNumber:
3674 * @val: an XPath object
3675 *
3676 * Converts an XPath object to its number value
3677 *
3678 * Returns the number value
3679 */
3680double
3681xmlXPathCastToNumber(xmlXPathObjectPtr val) {
3682 double ret = 0.0;
3683
3684 if (val == NULL)
3685 return(xmlXPathNAN);
3686 switch (val->type) {
3687 case XPATH_UNDEFINED:
3688#ifdef DEGUB_EXPR
3689 xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n");
3690#endif
3691 ret = xmlXPathNAN;
3692 break;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003693 case XPATH_NODESET:
William M. Brack0c022ad2002-07-12 00:56:01 +00003694 case XPATH_XSLT_TREE:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003695 ret = xmlXPathCastNodeSetToNumber(val->nodesetval);
3696 break;
3697 case XPATH_STRING:
3698 ret = xmlXPathCastStringToNumber(val->stringval);
3699 break;
3700 case XPATH_NUMBER:
3701 ret = val->floatval;
3702 break;
3703 case XPATH_BOOLEAN:
3704 ret = xmlXPathCastBooleanToNumber(val->boolval);
3705 break;
3706 case XPATH_USERS:
3707 case XPATH_POINT:
3708 case XPATH_RANGE:
3709 case XPATH_LOCATIONSET:
3710 TODO;
3711 ret = xmlXPathNAN;
3712 break;
3713 }
3714 return(ret);
3715}
3716
3717/**
3718 * xmlXPathConvertNumber:
3719 * @val: an XPath object
3720 *
3721 * Converts an existing object to its number() equivalent
3722 *
3723 * Returns the new object, the old one is freed (or the operation
3724 * is done directly on @val)
3725 */
3726xmlXPathObjectPtr
3727xmlXPathConvertNumber(xmlXPathObjectPtr val) {
3728 xmlXPathObjectPtr ret;
3729
3730 if (val == NULL)
3731 return(xmlXPathNewFloat(0.0));
3732 if (val->type == XPATH_NUMBER)
3733 return(val);
3734 ret = xmlXPathNewFloat(xmlXPathCastToNumber(val));
3735 xmlXPathFreeObject(val);
3736 return(ret);
3737}
3738
3739/**
3740 * xmlXPathCastNumberToBoolean:
3741 * @val: a number
3742 *
3743 * Converts a number to its boolean value
3744 *
3745 * Returns the boolean value
3746 */
3747int
3748xmlXPathCastNumberToBoolean (double val) {
Daniel Veillardcda96922001-08-21 10:56:31 +00003749 if (xmlXPathIsNaN(val) || (val == 0.0))
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003750 return(0);
3751 return(1);
3752}
3753
3754/**
3755 * xmlXPathCastStringToBoolean:
3756 * @val: a string
3757 *
3758 * Converts a string to its boolean value
3759 *
3760 * Returns the boolean value
3761 */
3762int
3763xmlXPathCastStringToBoolean (const xmlChar *val) {
3764 if ((val == NULL) || (xmlStrlen(val) == 0))
3765 return(0);
3766 return(1);
3767}
3768
3769/**
3770 * xmlXPathCastNodeSetToBoolean:
3771 * @ns: a node-set
3772 *
3773 * Converts a node-set to its boolean value
3774 *
3775 * Returns the boolean value
3776 */
3777int
3778xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) {
3779 if ((ns == NULL) || (ns->nodeNr == 0))
3780 return(0);
3781 return(1);
3782}
3783
3784/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003785 * xmlXPathCastToBoolean:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003786 * @val: an XPath object
3787 *
3788 * Converts an XPath object to its boolean value
3789 *
3790 * Returns the boolean value
3791 */
3792int
3793xmlXPathCastToBoolean (xmlXPathObjectPtr val) {
3794 int ret = 0;
3795
3796 if (val == NULL)
3797 return(0);
3798 switch (val->type) {
3799 case XPATH_UNDEFINED:
3800#ifdef DEBUG_EXPR
3801 xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n");
3802#endif
3803 ret = 0;
3804 break;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003805 case XPATH_NODESET:
William M. Brack0c022ad2002-07-12 00:56:01 +00003806 case XPATH_XSLT_TREE:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003807 ret = xmlXPathCastNodeSetToBoolean(val->nodesetval);
3808 break;
3809 case XPATH_STRING:
3810 ret = xmlXPathCastStringToBoolean(val->stringval);
3811 break;
3812 case XPATH_NUMBER:
3813 ret = xmlXPathCastNumberToBoolean(val->floatval);
3814 break;
3815 case XPATH_BOOLEAN:
3816 ret = val->boolval;
3817 break;
3818 case XPATH_USERS:
3819 case XPATH_POINT:
3820 case XPATH_RANGE:
3821 case XPATH_LOCATIONSET:
3822 TODO;
3823 ret = 0;
3824 break;
3825 }
3826 return(ret);
3827}
3828
3829
3830/**
3831 * xmlXPathConvertBoolean:
3832 * @val: an XPath object
3833 *
3834 * Converts an existing object to its boolean() equivalent
3835 *
3836 * Returns the new object, the old one is freed (or the operation
3837 * is done directly on @val)
3838 */
3839xmlXPathObjectPtr
3840xmlXPathConvertBoolean(xmlXPathObjectPtr val) {
3841 xmlXPathObjectPtr ret;
3842
3843 if (val == NULL)
3844 return(xmlXPathNewBoolean(0));
3845 if (val->type == XPATH_BOOLEAN)
3846 return(val);
3847 ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val));
3848 xmlXPathFreeObject(val);
3849 return(ret);
3850}
3851
Owen Taylor3473f882001-02-23 17:55:21 +00003852/************************************************************************
3853 * *
3854 * Routines to handle XPath contexts *
3855 * *
3856 ************************************************************************/
3857
3858/**
3859 * xmlXPathNewContext:
3860 * @doc: the XML document
3861 *
3862 * Create a new xmlXPathContext
3863 *
Daniel Veillardaf43f632002-03-08 15:05:20 +00003864 * Returns the xmlXPathContext just allocated. The caller will need to free it.
Owen Taylor3473f882001-02-23 17:55:21 +00003865 */
3866xmlXPathContextPtr
3867xmlXPathNewContext(xmlDocPtr doc) {
3868 xmlXPathContextPtr ret;
3869
3870 ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext));
3871 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003872 xmlXPathErrMemory(NULL, "creating context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003873 return(NULL);
3874 }
3875 memset(ret, 0 , (size_t) sizeof(xmlXPathContext));
3876 ret->doc = doc;
3877 ret->node = NULL;
3878
3879 ret->varHash = NULL;
3880
3881 ret->nb_types = 0;
3882 ret->max_types = 0;
3883 ret->types = NULL;
3884
3885 ret->funcHash = xmlHashCreate(0);
3886
3887 ret->nb_axis = 0;
3888 ret->max_axis = 0;
3889 ret->axis = NULL;
3890
3891 ret->nsHash = NULL;
3892 ret->user = NULL;
3893
3894 ret->contextSize = -1;
3895 ret->proximityPosition = -1;
3896
3897 xmlXPathRegisterAllFunctions(ret);
3898
3899 return(ret);
3900}
3901
3902/**
3903 * xmlXPathFreeContext:
3904 * @ctxt: the context to free
3905 *
3906 * Free up an xmlXPathContext
3907 */
3908void
3909xmlXPathFreeContext(xmlXPathContextPtr ctxt) {
3910 xmlXPathRegisteredNsCleanup(ctxt);
3911 xmlXPathRegisteredFuncsCleanup(ctxt);
3912 xmlXPathRegisteredVariablesCleanup(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003913 xmlFree(ctxt);
3914}
3915
3916/************************************************************************
3917 * *
3918 * Routines to handle XPath parser contexts *
3919 * *
3920 ************************************************************************/
3921
3922#define CHECK_CTXT(ctxt) \
3923 if (ctxt == NULL) { \
3924 xmlGenericError(xmlGenericErrorContext, \
3925 "%s:%d Internal error: ctxt == NULL\n", \
3926 __FILE__, __LINE__); \
3927 } \
3928
3929
3930#define CHECK_CONTEXT(ctxt) \
3931 if (ctxt == NULL) { \
3932 xmlGenericError(xmlGenericErrorContext, \
3933 "%s:%d Internal error: no context\n", \
3934 __FILE__, __LINE__); \
3935 } \
3936 else if (ctxt->doc == NULL) { \
3937 xmlGenericError(xmlGenericErrorContext, \
3938 "%s:%d Internal error: no document\n", \
3939 __FILE__, __LINE__); \
3940 } \
3941 else if (ctxt->doc->children == NULL) { \
3942 xmlGenericError(xmlGenericErrorContext, \
3943 "%s:%d Internal error: document without root\n", \
3944 __FILE__, __LINE__); \
3945 } \
3946
3947
3948/**
3949 * xmlXPathNewParserContext:
3950 * @str: the XPath expression
3951 * @ctxt: the XPath context
3952 *
3953 * Create a new xmlXPathParserContext
3954 *
3955 * Returns the xmlXPathParserContext just allocated.
3956 */
3957xmlXPathParserContextPtr
3958xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) {
3959 xmlXPathParserContextPtr ret;
3960
3961 ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext));
3962 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003963 xmlXPathErrMemory(ctxt, "creating parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003964 return(NULL);
3965 }
3966 memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext));
3967 ret->cur = ret->base = str;
3968 ret->context = ctxt;
3969
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003970 ret->comp = xmlXPathNewCompExpr();
3971 if (ret->comp == NULL) {
3972 xmlFree(ret->valueTab);
3973 xmlFree(ret);
3974 return(NULL);
3975 }
Daniel Veillard4773df22004-01-23 13:15:13 +00003976 if ((ctxt != NULL) && (ctxt->dict != NULL)) {
3977 ret->comp->dict = ctxt->dict;
3978 xmlDictReference(ret->comp->dict);
3979 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003980
3981 return(ret);
3982}
3983
3984/**
3985 * xmlXPathCompParserContext:
3986 * @comp: the XPath compiled expression
3987 * @ctxt: the XPath context
3988 *
3989 * Create a new xmlXPathParserContext when processing a compiled expression
3990 *
3991 * Returns the xmlXPathParserContext just allocated.
3992 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003993static xmlXPathParserContextPtr
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003994xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) {
3995 xmlXPathParserContextPtr ret;
3996
3997 ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext));
3998 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003999 xmlXPathErrMemory(ctxt, "creating evaluation context\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004000 return(NULL);
4001 }
4002 memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext));
4003
Owen Taylor3473f882001-02-23 17:55:21 +00004004 /* Allocate the value stack */
4005 ret->valueTab = (xmlXPathObjectPtr *)
4006 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004007 if (ret->valueTab == NULL) {
4008 xmlFree(ret);
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004009 xmlXPathErrMemory(ctxt, "creating evaluation context\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004010 return(NULL);
4011 }
Owen Taylor3473f882001-02-23 17:55:21 +00004012 ret->valueNr = 0;
4013 ret->valueMax = 10;
4014 ret->value = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004015
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00004016 ret->context = ctxt;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004017 ret->comp = comp;
4018
Owen Taylor3473f882001-02-23 17:55:21 +00004019 return(ret);
4020}
4021
4022/**
4023 * xmlXPathFreeParserContext:
4024 * @ctxt: the context to free
4025 *
4026 * Free up an xmlXPathParserContext
4027 */
4028void
4029xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) {
4030 if (ctxt->valueTab != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004031 xmlFree(ctxt->valueTab);
4032 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004033 if (ctxt->comp)
4034 xmlXPathFreeCompExpr(ctxt->comp);
Owen Taylor3473f882001-02-23 17:55:21 +00004035 xmlFree(ctxt);
4036}
4037
4038/************************************************************************
4039 * *
4040 * The implicit core function library *
4041 * *
4042 ************************************************************************/
4043
Owen Taylor3473f882001-02-23 17:55:21 +00004044/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004045 * xmlXPathNodeValHash:
Daniel Veillardf06307e2001-07-03 10:35:50 +00004046 * @node: a node pointer
4047 *
4048 * Function computing the beginning of the string value of the node,
4049 * used to speed up comparisons
4050 *
4051 * Returns an int usable as a hash
4052 */
4053static unsigned int
4054xmlXPathNodeValHash(xmlNodePtr node) {
4055 int len = 2;
4056 const xmlChar * string = NULL;
4057 xmlNodePtr tmp = NULL;
4058 unsigned int ret = 0;
4059
4060 if (node == NULL)
4061 return(0);
4062
Daniel Veillard9adc0462003-03-24 18:39:54 +00004063 if (node->type == XML_DOCUMENT_NODE) {
4064 tmp = xmlDocGetRootElement((xmlDocPtr) node);
4065 if (tmp == NULL)
4066 node = node->children;
4067 else
4068 node = tmp;
4069
4070 if (node == NULL)
4071 return(0);
4072 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004073
4074 switch (node->type) {
4075 case XML_COMMENT_NODE:
4076 case XML_PI_NODE:
4077 case XML_CDATA_SECTION_NODE:
4078 case XML_TEXT_NODE:
4079 string = node->content;
4080 if (string == NULL)
4081 return(0);
4082 if (string[0] == 0)
4083 return(0);
4084 return(((unsigned int) string[0]) +
4085 (((unsigned int) string[1]) << 8));
4086 case XML_NAMESPACE_DECL:
4087 string = ((xmlNsPtr)node)->href;
4088 if (string == NULL)
4089 return(0);
4090 if (string[0] == 0)
4091 return(0);
4092 return(((unsigned int) string[0]) +
4093 (((unsigned int) string[1]) << 8));
4094 case XML_ATTRIBUTE_NODE:
4095 tmp = ((xmlAttrPtr) node)->children;
4096 break;
4097 case XML_ELEMENT_NODE:
4098 tmp = node->children;
4099 break;
4100 default:
4101 return(0);
4102 }
4103 while (tmp != NULL) {
4104 switch (tmp->type) {
4105 case XML_COMMENT_NODE:
4106 case XML_PI_NODE:
4107 case XML_CDATA_SECTION_NODE:
4108 case XML_TEXT_NODE:
4109 string = tmp->content;
4110 break;
4111 case XML_NAMESPACE_DECL:
4112 string = ((xmlNsPtr)tmp)->href;
4113 break;
4114 default:
4115 break;
4116 }
4117 if ((string != NULL) && (string[0] != 0)) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00004118 if (len == 1) {
4119 return(ret + (((unsigned int) string[0]) << 8));
4120 }
4121 if (string[1] == 0) {
4122 len = 1;
4123 ret = (unsigned int) string[0];
4124 } else {
4125 return(((unsigned int) string[0]) +
4126 (((unsigned int) string[1]) << 8));
4127 }
4128 }
4129 /*
4130 * Skip to next node
4131 */
4132 if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) {
4133 if (tmp->children->type != XML_ENTITY_DECL) {
4134 tmp = tmp->children;
4135 continue;
4136 }
4137 }
4138 if (tmp == node)
4139 break;
4140
4141 if (tmp->next != NULL) {
4142 tmp = tmp->next;
4143 continue;
4144 }
4145
4146 do {
4147 tmp = tmp->parent;
4148 if (tmp == NULL)
4149 break;
4150 if (tmp == node) {
4151 tmp = NULL;
4152 break;
4153 }
4154 if (tmp->next != NULL) {
4155 tmp = tmp->next;
4156 break;
4157 }
4158 } while (tmp != NULL);
4159 }
4160 return(ret);
4161}
4162
4163/**
4164 * xmlXPathStringHash:
4165 * @string: a string
4166 *
4167 * Function computing the beginning of the string value of the node,
4168 * used to speed up comparisons
4169 *
4170 * Returns an int usable as a hash
4171 */
4172static unsigned int
4173xmlXPathStringHash(const xmlChar * string) {
4174 if (string == NULL)
4175 return((unsigned int) 0);
4176 if (string[0] == 0)
4177 return(0);
4178 return(((unsigned int) string[0]) +
4179 (((unsigned int) string[1]) << 8));
4180}
4181
4182/**
Owen Taylor3473f882001-02-23 17:55:21 +00004183 * xmlXPathCompareNodeSetFloat:
4184 * @ctxt: the XPath Parser context
4185 * @inf: less than (1) or greater than (0)
4186 * @strict: is the comparison strict
4187 * @arg: the node set
4188 * @f: the value
4189 *
4190 * Implement the compare operation between a nodeset and a number
4191 * @ns < @val (1, 1, ...
4192 * @ns <= @val (1, 0, ...
4193 * @ns > @val (0, 1, ...
4194 * @ns >= @val (0, 0, ...
4195 *
4196 * If one object to be compared is a node-set and the other is a number,
4197 * then the comparison will be true if and only if there is a node in the
4198 * node-set such that the result of performing the comparison on the number
4199 * to be compared and on the result of converting the string-value of that
4200 * node to a number using the number function is true.
4201 *
4202 * Returns 0 or 1 depending on the results of the test.
4203 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004204static int
Owen Taylor3473f882001-02-23 17:55:21 +00004205xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict,
4206 xmlXPathObjectPtr arg, xmlXPathObjectPtr f) {
4207 int i, ret = 0;
4208 xmlNodeSetPtr ns;
4209 xmlChar *str2;
4210
4211 if ((f == NULL) || (arg == NULL) ||
4212 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) {
4213 xmlXPathFreeObject(arg);
4214 xmlXPathFreeObject(f);
4215 return(0);
4216 }
4217 ns = arg->nodesetval;
Daniel Veillard911f49a2001-04-07 15:39:35 +00004218 if (ns != NULL) {
4219 for (i = 0;i < ns->nodeNr;i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004220 str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
Daniel Veillard911f49a2001-04-07 15:39:35 +00004221 if (str2 != NULL) {
4222 valuePush(ctxt,
4223 xmlXPathNewString(str2));
4224 xmlFree(str2);
4225 xmlXPathNumberFunction(ctxt, 1);
4226 valuePush(ctxt, xmlXPathObjectCopy(f));
4227 ret = xmlXPathCompareValues(ctxt, inf, strict);
4228 if (ret)
4229 break;
4230 }
4231 }
Owen Taylor3473f882001-02-23 17:55:21 +00004232 }
4233 xmlXPathFreeObject(arg);
4234 xmlXPathFreeObject(f);
4235 return(ret);
4236}
4237
4238/**
4239 * xmlXPathCompareNodeSetString:
4240 * @ctxt: the XPath Parser context
4241 * @inf: less than (1) or greater than (0)
4242 * @strict: is the comparison strict
4243 * @arg: the node set
4244 * @s: the value
4245 *
4246 * Implement the compare operation between a nodeset and a string
4247 * @ns < @val (1, 1, ...
4248 * @ns <= @val (1, 0, ...
4249 * @ns > @val (0, 1, ...
4250 * @ns >= @val (0, 0, ...
4251 *
4252 * If one object to be compared is a node-set and the other is a string,
4253 * then the comparison will be true if and only if there is a node in
4254 * the node-set such that the result of performing the comparison on the
4255 * string-value of the node and the other string is true.
4256 *
4257 * Returns 0 or 1 depending on the results of the test.
4258 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004259static int
Owen Taylor3473f882001-02-23 17:55:21 +00004260xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict,
4261 xmlXPathObjectPtr arg, xmlXPathObjectPtr s) {
4262 int i, ret = 0;
4263 xmlNodeSetPtr ns;
4264 xmlChar *str2;
4265
4266 if ((s == NULL) || (arg == NULL) ||
4267 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) {
4268 xmlXPathFreeObject(arg);
4269 xmlXPathFreeObject(s);
4270 return(0);
4271 }
4272 ns = arg->nodesetval;
Daniel Veillard911f49a2001-04-07 15:39:35 +00004273 if (ns != NULL) {
4274 for (i = 0;i < ns->nodeNr;i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004275 str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
Daniel Veillard911f49a2001-04-07 15:39:35 +00004276 if (str2 != NULL) {
4277 valuePush(ctxt,
4278 xmlXPathNewString(str2));
4279 xmlFree(str2);
4280 valuePush(ctxt, xmlXPathObjectCopy(s));
4281 ret = xmlXPathCompareValues(ctxt, inf, strict);
4282 if (ret)
4283 break;
4284 }
4285 }
Owen Taylor3473f882001-02-23 17:55:21 +00004286 }
4287 xmlXPathFreeObject(arg);
4288 xmlXPathFreeObject(s);
4289 return(ret);
4290}
4291
4292/**
4293 * xmlXPathCompareNodeSets:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004294 * @inf: less than (1) or greater than (0)
Owen Taylor3473f882001-02-23 17:55:21 +00004295 * @strict: is the comparison strict
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004296 * @arg1: the first node set object
Owen Taylor3473f882001-02-23 17:55:21 +00004297 * @arg2: the second node set object
4298 *
4299 * Implement the compare operation on nodesets:
4300 *
4301 * If both objects to be compared are node-sets, then the comparison
4302 * will be true if and only if there is a node in the first node-set
4303 * and a node in the second node-set such that the result of performing
4304 * the comparison on the string-values of the two nodes is true.
4305 * ....
4306 * When neither object to be compared is a node-set and the operator
4307 * is <=, <, >= or >, then the objects are compared by converting both
4308 * objects to numbers and comparing the numbers according to IEEE 754.
4309 * ....
4310 * The number function converts its argument to a number as follows:
4311 * - a string that consists of optional whitespace followed by an
4312 * optional minus sign followed by a Number followed by whitespace
4313 * is converted to the IEEE 754 number that is nearest (according
4314 * to the IEEE 754 round-to-nearest rule) to the mathematical value
4315 * represented by the string; any other string is converted to NaN
4316 *
4317 * Conclusion all nodes need to be converted first to their string value
4318 * and then the comparison must be done when possible
4319 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004320static int
4321xmlXPathCompareNodeSets(int inf, int strict,
Owen Taylor3473f882001-02-23 17:55:21 +00004322 xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) {
4323 int i, j, init = 0;
4324 double val1;
4325 double *values2;
4326 int ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004327 xmlNodeSetPtr ns1;
4328 xmlNodeSetPtr ns2;
4329
4330 if ((arg1 == NULL) ||
Daniel Veillard4dd93462001-04-02 15:16:19 +00004331 ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) {
4332 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004333 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004334 }
Owen Taylor3473f882001-02-23 17:55:21 +00004335 if ((arg2 == NULL) ||
Daniel Veillard4dd93462001-04-02 15:16:19 +00004336 ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) {
4337 xmlXPathFreeObject(arg1);
4338 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004339 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004340 }
Owen Taylor3473f882001-02-23 17:55:21 +00004341
4342 ns1 = arg1->nodesetval;
4343 ns2 = arg2->nodesetval;
4344
Daniel Veillardd8df6c02001-04-05 16:54:14 +00004345 if ((ns1 == NULL) || (ns1->nodeNr <= 0)) {
Daniel Veillard4dd93462001-04-02 15:16:19 +00004346 xmlXPathFreeObject(arg1);
4347 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004348 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004349 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00004350 if ((ns2 == NULL) || (ns2->nodeNr <= 0)) {
Daniel Veillard4dd93462001-04-02 15:16:19 +00004351 xmlXPathFreeObject(arg1);
4352 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004353 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004354 }
Owen Taylor3473f882001-02-23 17:55:21 +00004355
4356 values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double));
4357 if (values2 == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004358 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Daniel Veillard4dd93462001-04-02 15:16:19 +00004359 xmlXPathFreeObject(arg1);
4360 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004361 return(0);
4362 }
4363 for (i = 0;i < ns1->nodeNr;i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004364 val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]);
Daniel Veillardcda96922001-08-21 10:56:31 +00004365 if (xmlXPathIsNaN(val1))
Owen Taylor3473f882001-02-23 17:55:21 +00004366 continue;
4367 for (j = 0;j < ns2->nodeNr;j++) {
4368 if (init == 0) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004369 values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]);
Owen Taylor3473f882001-02-23 17:55:21 +00004370 }
Daniel Veillardcda96922001-08-21 10:56:31 +00004371 if (xmlXPathIsNaN(values2[j]))
Owen Taylor3473f882001-02-23 17:55:21 +00004372 continue;
4373 if (inf && strict)
4374 ret = (val1 < values2[j]);
4375 else if (inf && !strict)
4376 ret = (val1 <= values2[j]);
4377 else if (!inf && strict)
4378 ret = (val1 > values2[j]);
4379 else if (!inf && !strict)
4380 ret = (val1 >= values2[j]);
4381 if (ret)
4382 break;
4383 }
4384 if (ret)
4385 break;
4386 init = 1;
4387 }
4388 xmlFree(values2);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004389 xmlXPathFreeObject(arg1);
4390 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004391 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004392}
4393
4394/**
4395 * xmlXPathCompareNodeSetValue:
4396 * @ctxt: the XPath Parser context
4397 * @inf: less than (1) or greater than (0)
4398 * @strict: is the comparison strict
4399 * @arg: the node set
4400 * @val: the value
4401 *
4402 * Implement the compare operation between a nodeset and a value
4403 * @ns < @val (1, 1, ...
4404 * @ns <= @val (1, 0, ...
4405 * @ns > @val (0, 1, ...
4406 * @ns >= @val (0, 0, ...
4407 *
4408 * If one object to be compared is a node-set and the other is a boolean,
4409 * then the comparison will be true if and only if the result of performing
4410 * the comparison on the boolean and on the result of converting
4411 * the node-set to a boolean using the boolean function is true.
4412 *
4413 * Returns 0 or 1 depending on the results of the test.
4414 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004415static int
Owen Taylor3473f882001-02-23 17:55:21 +00004416xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict,
4417 xmlXPathObjectPtr arg, xmlXPathObjectPtr val) {
4418 if ((val == NULL) || (arg == NULL) ||
4419 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
4420 return(0);
4421
4422 switch(val->type) {
4423 case XPATH_NUMBER:
4424 return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val));
4425 case XPATH_NODESET:
4426 case XPATH_XSLT_TREE:
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004427 return(xmlXPathCompareNodeSets(inf, strict, arg, val));
Owen Taylor3473f882001-02-23 17:55:21 +00004428 case XPATH_STRING:
4429 return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val));
4430 case XPATH_BOOLEAN:
4431 valuePush(ctxt, arg);
4432 xmlXPathBooleanFunction(ctxt, 1);
4433 valuePush(ctxt, val);
4434 return(xmlXPathCompareValues(ctxt, inf, strict));
4435 default:
4436 TODO
Owen Taylor3473f882001-02-23 17:55:21 +00004437 }
4438 return(0);
4439}
4440
4441/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004442 * xmlXPathEqualNodeSetString:
Owen Taylor3473f882001-02-23 17:55:21 +00004443 * @arg: the nodeset object argument
4444 * @str: the string to compare to.
William M. Brack0c022ad2002-07-12 00:56:01 +00004445 * @neq: flag to show whether for '=' (0) or '!=' (1)
Owen Taylor3473f882001-02-23 17:55:21 +00004446 *
4447 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4448 * If one object to be compared is a node-set and the other is a string,
4449 * then the comparison will be true if and only if there is a node in
4450 * the node-set such that the result of performing the comparison on the
4451 * string-value of the node and the other string is true.
4452 *
4453 * Returns 0 or 1 depending on the results of the test.
4454 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004455static int
William M. Brack0c022ad2002-07-12 00:56:01 +00004456xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str, int neq)
Daniel Veillardf06307e2001-07-03 10:35:50 +00004457{
Owen Taylor3473f882001-02-23 17:55:21 +00004458 int i;
4459 xmlNodeSetPtr ns;
4460 xmlChar *str2;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004461 unsigned int hash;
Owen Taylor3473f882001-02-23 17:55:21 +00004462
4463 if ((str == NULL) || (arg == NULL) ||
Daniel Veillardf06307e2001-07-03 10:35:50 +00004464 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
4465 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00004466 ns = arg->nodesetval;
William M. Brackc125a722003-11-16 08:06:19 +00004467 /*
4468 * A NULL nodeset compared with a string is always false
4469 * (since there is no node equal, and no node not equal)
4470 */
4471 if ((ns == NULL) || (ns->nodeNr <= 0) )
Daniel Veillardf06307e2001-07-03 10:35:50 +00004472 return (0);
William M. Brackc125a722003-11-16 08:06:19 +00004473 hash = xmlXPathStringHash(str);
Daniel Veillardf06307e2001-07-03 10:35:50 +00004474 for (i = 0; i < ns->nodeNr; i++) {
4475 if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) {
4476 str2 = xmlNodeGetContent(ns->nodeTab[i]);
4477 if ((str2 != NULL) && (xmlStrEqual(str, str2))) {
4478 xmlFree(str2);
William M. Brack0c022ad2002-07-12 00:56:01 +00004479 if (neq)
4480 continue;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004481 return (1);
Daniel Veillard9adc0462003-03-24 18:39:54 +00004482 } else if ((str2 == NULL) && (xmlStrEqual(str, BAD_CAST ""))) {
4483 if (neq)
4484 continue;
4485 return (1);
William M. Brack0c022ad2002-07-12 00:56:01 +00004486 } else if (neq) {
4487 if (str2 != NULL)
4488 xmlFree(str2);
4489 return (1);
4490 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004491 if (str2 != NULL)
4492 xmlFree(str2);
William M. Brack0c022ad2002-07-12 00:56:01 +00004493 } else if (neq)
4494 return (1);
Owen Taylor3473f882001-02-23 17:55:21 +00004495 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004496 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00004497}
4498
4499/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004500 * xmlXPathEqualNodeSetFloat:
Owen Taylor3473f882001-02-23 17:55:21 +00004501 * @arg: the nodeset object argument
4502 * @f: the float to compare to
William M. Brack0c022ad2002-07-12 00:56:01 +00004503 * @neq: flag to show whether to compare '=' (0) or '!=' (1)
Owen Taylor3473f882001-02-23 17:55:21 +00004504 *
4505 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4506 * If one object to be compared is a node-set and the other is a number,
4507 * then the comparison will be true if and only if there is a node in
4508 * the node-set such that the result of performing the comparison on the
4509 * number to be compared and on the result of converting the string-value
4510 * of that node to a number using the number function is true.
4511 *
4512 * Returns 0 or 1 depending on the results of the test.
4513 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004514static int
William M. Brack0c022ad2002-07-12 00:56:01 +00004515xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt,
4516 xmlXPathObjectPtr arg, double f, int neq) {
4517 int i, ret=0;
4518 xmlNodeSetPtr ns;
4519 xmlChar *str2;
4520 xmlXPathObjectPtr val;
4521 double v;
Owen Taylor3473f882001-02-23 17:55:21 +00004522
4523 if ((arg == NULL) ||
4524 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
4525 return(0);
4526
William M. Brack0c022ad2002-07-12 00:56:01 +00004527 ns = arg->nodesetval;
4528 if (ns != NULL) {
4529 for (i=0;i<ns->nodeNr;i++) {
4530 str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
4531 if (str2 != NULL) {
4532 valuePush(ctxt, xmlXPathNewString(str2));
4533 xmlFree(str2);
4534 xmlXPathNumberFunction(ctxt, 1);
4535 val = valuePop(ctxt);
4536 v = val->floatval;
4537 xmlXPathFreeObject(val);
4538 if (!xmlXPathIsNaN(v)) {
4539 if ((!neq) && (v==f)) {
4540 ret = 1;
4541 break;
4542 } else if ((neq) && (v!=f)) {
4543 ret = 1;
4544 break;
4545 }
4546 }
4547 }
4548 }
4549 }
4550
4551 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004552}
4553
4554
4555/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004556 * xmlXPathEqualNodeSets:
Owen Taylor3473f882001-02-23 17:55:21 +00004557 * @arg1: first nodeset object argument
4558 * @arg2: second nodeset object argument
William M. Brack0c022ad2002-07-12 00:56:01 +00004559 * @neq: flag to show whether to test '=' (0) or '!=' (1)
Owen Taylor3473f882001-02-23 17:55:21 +00004560 *
William M. Brack0c022ad2002-07-12 00:56:01 +00004561 * Implement the equal / not equal operation on XPath nodesets:
4562 * @arg1 == @arg2 or @arg1 != @arg2
Owen Taylor3473f882001-02-23 17:55:21 +00004563 * If both objects to be compared are node-sets, then the comparison
4564 * will be true if and only if there is a node in the first node-set and
4565 * a node in the second node-set such that the result of performing the
4566 * comparison on the string-values of the two nodes is true.
4567 *
4568 * (needless to say, this is a costly operation)
4569 *
4570 * Returns 0 or 1 depending on the results of the test.
4571 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004572static int
William M. Brack0c022ad2002-07-12 00:56:01 +00004573xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2, int neq) {
Owen Taylor3473f882001-02-23 17:55:21 +00004574 int i, j;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004575 unsigned int *hashs1;
4576 unsigned int *hashs2;
Owen Taylor3473f882001-02-23 17:55:21 +00004577 xmlChar **values1;
4578 xmlChar **values2;
4579 int ret = 0;
4580 xmlNodeSetPtr ns1;
4581 xmlNodeSetPtr ns2;
4582
4583 if ((arg1 == NULL) ||
4584 ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)))
4585 return(0);
4586 if ((arg2 == NULL) ||
4587 ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE)))
4588 return(0);
4589
4590 ns1 = arg1->nodesetval;
4591 ns2 = arg2->nodesetval;
4592
Daniel Veillard911f49a2001-04-07 15:39:35 +00004593 if ((ns1 == NULL) || (ns1->nodeNr <= 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004594 return(0);
Daniel Veillard911f49a2001-04-07 15:39:35 +00004595 if ((ns2 == NULL) || (ns2->nodeNr <= 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004596 return(0);
4597
4598 /*
William M. Brack0c022ad2002-07-12 00:56:01 +00004599 * for equal, check if there is a node pertaining to both sets
Owen Taylor3473f882001-02-23 17:55:21 +00004600 */
William M. Brack0c022ad2002-07-12 00:56:01 +00004601 if (neq == 0)
4602 for (i = 0;i < ns1->nodeNr;i++)
4603 for (j = 0;j < ns2->nodeNr;j++)
4604 if (ns1->nodeTab[i] == ns2->nodeTab[j])
4605 return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00004606
4607 values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *));
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004608 if (values1 == NULL) {
4609 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004610 return(0);
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004611 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004612 hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int));
4613 if (hashs1 == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004614 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Daniel Veillardf06307e2001-07-03 10:35:50 +00004615 xmlFree(values1);
4616 return(0);
4617 }
Owen Taylor3473f882001-02-23 17:55:21 +00004618 memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *));
4619 values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *));
4620 if (values2 == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004621 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Daniel Veillardf06307e2001-07-03 10:35:50 +00004622 xmlFree(hashs1);
Owen Taylor3473f882001-02-23 17:55:21 +00004623 xmlFree(values1);
4624 return(0);
4625 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004626 hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int));
4627 if (hashs2 == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004628 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Daniel Veillardf06307e2001-07-03 10:35:50 +00004629 xmlFree(hashs1);
4630 xmlFree(values1);
4631 xmlFree(values2);
4632 return(0);
4633 }
Owen Taylor3473f882001-02-23 17:55:21 +00004634 memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *));
4635 for (i = 0;i < ns1->nodeNr;i++) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00004636 hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00004637 for (j = 0;j < ns2->nodeNr;j++) {
4638 if (i == 0)
Daniel Veillardf06307e2001-07-03 10:35:50 +00004639 hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]);
William M. Brack0c022ad2002-07-12 00:56:01 +00004640 if (hashs1[i] != hashs2[j]) {
4641 if (neq) {
4642 ret = 1;
4643 break;
4644 }
4645 }
4646 else {
Daniel Veillardf06307e2001-07-03 10:35:50 +00004647 if (values1[i] == NULL)
4648 values1[i] = xmlNodeGetContent(ns1->nodeTab[i]);
4649 if (values2[j] == NULL)
4650 values2[j] = xmlNodeGetContent(ns2->nodeTab[j]);
William M. Brack0c022ad2002-07-12 00:56:01 +00004651 ret = xmlStrEqual(values1[i], values2[j]) ^ neq;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004652 if (ret)
4653 break;
4654 }
Owen Taylor3473f882001-02-23 17:55:21 +00004655 }
4656 if (ret)
4657 break;
4658 }
4659 for (i = 0;i < ns1->nodeNr;i++)
4660 if (values1[i] != NULL)
4661 xmlFree(values1[i]);
4662 for (j = 0;j < ns2->nodeNr;j++)
4663 if (values2[j] != NULL)
4664 xmlFree(values2[j]);
4665 xmlFree(values1);
4666 xmlFree(values2);
Daniel Veillardf06307e2001-07-03 10:35:50 +00004667 xmlFree(hashs1);
4668 xmlFree(hashs2);
Owen Taylor3473f882001-02-23 17:55:21 +00004669 return(ret);
4670}
4671
William M. Brack0c022ad2002-07-12 00:56:01 +00004672static int
4673xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt,
4674 xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) {
Owen Taylor3473f882001-02-23 17:55:21 +00004675 int ret = 0;
William M. Brack0c022ad2002-07-12 00:56:01 +00004676 /*
4677 *At this point we are assured neither arg1 nor arg2
4678 *is a nodeset, so we can just pick the appropriate routine.
4679 */
Owen Taylor3473f882001-02-23 17:55:21 +00004680 switch (arg1->type) {
4681 case XPATH_UNDEFINED:
4682#ifdef DEBUG_EXPR
4683 xmlGenericError(xmlGenericErrorContext,
4684 "Equal: undefined\n");
4685#endif
4686 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004687 case XPATH_BOOLEAN:
4688 switch (arg2->type) {
4689 case XPATH_UNDEFINED:
4690#ifdef DEBUG_EXPR
4691 xmlGenericError(xmlGenericErrorContext,
4692 "Equal: undefined\n");
4693#endif
4694 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004695 case XPATH_BOOLEAN:
4696#ifdef DEBUG_EXPR
4697 xmlGenericError(xmlGenericErrorContext,
4698 "Equal: %d boolean %d \n",
4699 arg1->boolval, arg2->boolval);
4700#endif
4701 ret = (arg1->boolval == arg2->boolval);
4702 break;
4703 case XPATH_NUMBER:
William M. Brackef61d202002-07-19 08:32:00 +00004704 ret = (arg1->boolval ==
4705 xmlXPathCastNumberToBoolean(arg2->floatval));
Owen Taylor3473f882001-02-23 17:55:21 +00004706 break;
4707 case XPATH_STRING:
4708 if ((arg2->stringval == NULL) ||
4709 (arg2->stringval[0] == 0)) ret = 0;
4710 else
4711 ret = 1;
4712 ret = (arg1->boolval == ret);
4713 break;
4714 case XPATH_USERS:
4715 case XPATH_POINT:
4716 case XPATH_RANGE:
4717 case XPATH_LOCATIONSET:
4718 TODO
4719 break;
William M. Brack0c022ad2002-07-12 00:56:01 +00004720 case XPATH_NODESET:
4721 case XPATH_XSLT_TREE:
4722 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004723 }
4724 break;
4725 case XPATH_NUMBER:
4726 switch (arg2->type) {
4727 case XPATH_UNDEFINED:
4728#ifdef DEBUG_EXPR
4729 xmlGenericError(xmlGenericErrorContext,
4730 "Equal: undefined\n");
4731#endif
4732 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004733 case XPATH_BOOLEAN:
William M. Brackef61d202002-07-19 08:32:00 +00004734 ret = (arg2->boolval==
4735 xmlXPathCastNumberToBoolean(arg1->floatval));
Owen Taylor3473f882001-02-23 17:55:21 +00004736 break;
4737 case XPATH_STRING:
4738 valuePush(ctxt, arg2);
4739 xmlXPathNumberFunction(ctxt, 1);
4740 arg2 = valuePop(ctxt);
4741 /* no break on purpose */
4742 case XPATH_NUMBER:
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004743 /* Hand check NaN and Infinity equalities */
William M. Brack08171912003-12-29 02:52:11 +00004744 if (xmlXPathIsNaN(arg1->floatval) ||
4745 xmlXPathIsNaN(arg2->floatval)) {
Daniel Veillard21458c82002-03-27 16:12:22 +00004746 ret = 0;
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004747 } else if (xmlXPathIsInf(arg1->floatval) == 1) {
4748 if (xmlXPathIsInf(arg2->floatval) == 1)
4749 ret = 1;
4750 else
4751 ret = 0;
4752 } else if (xmlXPathIsInf(arg1->floatval) == -1) {
4753 if (xmlXPathIsInf(arg2->floatval) == -1)
4754 ret = 1;
4755 else
4756 ret = 0;
4757 } else if (xmlXPathIsInf(arg2->floatval) == 1) {
4758 if (xmlXPathIsInf(arg1->floatval) == 1)
4759 ret = 1;
4760 else
4761 ret = 0;
4762 } else if (xmlXPathIsInf(arg2->floatval) == -1) {
4763 if (xmlXPathIsInf(arg1->floatval) == -1)
4764 ret = 1;
4765 else
4766 ret = 0;
Daniel Veillard21458c82002-03-27 16:12:22 +00004767 } else {
4768 ret = (arg1->floatval == arg2->floatval);
4769 }
Owen Taylor3473f882001-02-23 17:55:21 +00004770 break;
4771 case XPATH_USERS:
4772 case XPATH_POINT:
4773 case XPATH_RANGE:
4774 case XPATH_LOCATIONSET:
4775 TODO
4776 break;
William M. Brack0c022ad2002-07-12 00:56:01 +00004777 case XPATH_NODESET:
4778 case XPATH_XSLT_TREE:
4779 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004780 }
4781 break;
4782 case XPATH_STRING:
4783 switch (arg2->type) {
4784 case XPATH_UNDEFINED:
4785#ifdef DEBUG_EXPR
4786 xmlGenericError(xmlGenericErrorContext,
4787 "Equal: undefined\n");
4788#endif
4789 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004790 case XPATH_BOOLEAN:
4791 if ((arg1->stringval == NULL) ||
4792 (arg1->stringval[0] == 0)) ret = 0;
4793 else
4794 ret = 1;
4795 ret = (arg2->boolval == ret);
4796 break;
4797 case XPATH_STRING:
4798 ret = xmlStrEqual(arg1->stringval, arg2->stringval);
4799 break;
4800 case XPATH_NUMBER:
4801 valuePush(ctxt, arg1);
4802 xmlXPathNumberFunction(ctxt, 1);
4803 arg1 = valuePop(ctxt);
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004804 /* Hand check NaN and Infinity equalities */
William M. Brack08171912003-12-29 02:52:11 +00004805 if (xmlXPathIsNaN(arg1->floatval) ||
4806 xmlXPathIsNaN(arg2->floatval)) {
Daniel Veillard21458c82002-03-27 16:12:22 +00004807 ret = 0;
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004808 } else if (xmlXPathIsInf(arg1->floatval) == 1) {
4809 if (xmlXPathIsInf(arg2->floatval) == 1)
4810 ret = 1;
4811 else
4812 ret = 0;
4813 } else if (xmlXPathIsInf(arg1->floatval) == -1) {
4814 if (xmlXPathIsInf(arg2->floatval) == -1)
4815 ret = 1;
4816 else
4817 ret = 0;
4818 } else if (xmlXPathIsInf(arg2->floatval) == 1) {
4819 if (xmlXPathIsInf(arg1->floatval) == 1)
4820 ret = 1;
4821 else
4822 ret = 0;
4823 } else if (xmlXPathIsInf(arg2->floatval) == -1) {
4824 if (xmlXPathIsInf(arg1->floatval) == -1)
4825 ret = 1;
4826 else
4827 ret = 0;
Daniel Veillard21458c82002-03-27 16:12:22 +00004828 } else {
4829 ret = (arg1->floatval == arg2->floatval);
4830 }
Owen Taylor3473f882001-02-23 17:55:21 +00004831 break;
4832 case XPATH_USERS:
4833 case XPATH_POINT:
4834 case XPATH_RANGE:
4835 case XPATH_LOCATIONSET:
4836 TODO
4837 break;
William M. Brack0c022ad2002-07-12 00:56:01 +00004838 case XPATH_NODESET:
4839 case XPATH_XSLT_TREE:
4840 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004841 }
4842 break;
4843 case XPATH_USERS:
4844 case XPATH_POINT:
4845 case XPATH_RANGE:
4846 case XPATH_LOCATIONSET:
4847 TODO
4848 break;
William M. Brack0c022ad2002-07-12 00:56:01 +00004849 case XPATH_NODESET:
4850 case XPATH_XSLT_TREE:
4851 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004852 }
4853 xmlXPathFreeObject(arg1);
4854 xmlXPathFreeObject(arg2);
4855 return(ret);
4856}
4857
William M. Brack0c022ad2002-07-12 00:56:01 +00004858/**
4859 * xmlXPathEqualValues:
4860 * @ctxt: the XPath Parser context
4861 *
4862 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4863 *
4864 * Returns 0 or 1 depending on the results of the test.
4865 */
4866int
4867xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) {
4868 xmlXPathObjectPtr arg1, arg2, argtmp;
4869 int ret = 0;
4870
4871 arg2 = valuePop(ctxt);
4872 arg1 = valuePop(ctxt);
4873 if ((arg1 == NULL) || (arg2 == NULL)) {
4874 if (arg1 != NULL)
4875 xmlXPathFreeObject(arg1);
4876 else
4877 xmlXPathFreeObject(arg2);
4878 XP_ERROR0(XPATH_INVALID_OPERAND);
4879 }
4880
4881 if (arg1 == arg2) {
4882#ifdef DEBUG_EXPR
4883 xmlGenericError(xmlGenericErrorContext,
4884 "Equal: by pointer\n");
4885#endif
4886 return(1);
4887 }
4888
4889 /*
4890 *If either argument is a nodeset, it's a 'special case'
4891 */
4892 if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) ||
4893 (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
4894 /*
4895 *Hack it to assure arg1 is the nodeset
4896 */
4897 if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) {
4898 argtmp = arg2;
4899 arg2 = arg1;
4900 arg1 = argtmp;
4901 }
4902 switch (arg2->type) {
4903 case XPATH_UNDEFINED:
4904#ifdef DEBUG_EXPR
4905 xmlGenericError(xmlGenericErrorContext,
4906 "Equal: undefined\n");
4907#endif
4908 break;
4909 case XPATH_NODESET:
4910 case XPATH_XSLT_TREE:
4911 ret = xmlXPathEqualNodeSets(arg1, arg2, 0);
4912 break;
4913 case XPATH_BOOLEAN:
4914 if ((arg1->nodesetval == NULL) ||
4915 (arg1->nodesetval->nodeNr == 0)) ret = 0;
4916 else
4917 ret = 1;
4918 ret = (ret == arg2->boolval);
4919 break;
4920 case XPATH_NUMBER:
4921 ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 0);
4922 break;
4923 case XPATH_STRING:
4924 ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval, 0);
4925 break;
4926 case XPATH_USERS:
4927 case XPATH_POINT:
4928 case XPATH_RANGE:
4929 case XPATH_LOCATIONSET:
4930 TODO
4931 break;
4932 }
4933 xmlXPathFreeObject(arg1);
4934 xmlXPathFreeObject(arg2);
4935 return(ret);
4936 }
4937
4938 return (xmlXPathEqualValuesCommon(ctxt, arg1, arg2));
4939}
4940
4941/**
4942 * xmlXPathNotEqualValues:
4943 * @ctxt: the XPath Parser context
4944 *
4945 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4946 *
4947 * Returns 0 or 1 depending on the results of the test.
4948 */
4949int
4950xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) {
4951 xmlXPathObjectPtr arg1, arg2, argtmp;
4952 int ret = 0;
4953
4954 arg2 = valuePop(ctxt);
4955 arg1 = valuePop(ctxt);
4956 if ((arg1 == NULL) || (arg2 == NULL)) {
4957 if (arg1 != NULL)
4958 xmlXPathFreeObject(arg1);
4959 else
4960 xmlXPathFreeObject(arg2);
4961 XP_ERROR0(XPATH_INVALID_OPERAND);
4962 }
4963
4964 if (arg1 == arg2) {
4965#ifdef DEBUG_EXPR
4966 xmlGenericError(xmlGenericErrorContext,
4967 "NotEqual: by pointer\n");
4968#endif
4969 return(0);
4970 }
4971
4972 /*
4973 *If either argument is a nodeset, it's a 'special case'
4974 */
4975 if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) ||
4976 (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
4977 /*
4978 *Hack it to assure arg1 is the nodeset
4979 */
4980 if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) {
4981 argtmp = arg2;
4982 arg2 = arg1;
4983 arg1 = argtmp;
4984 }
4985 switch (arg2->type) {
4986 case XPATH_UNDEFINED:
4987#ifdef DEBUG_EXPR
4988 xmlGenericError(xmlGenericErrorContext,
4989 "NotEqual: undefined\n");
4990#endif
4991 break;
4992 case XPATH_NODESET:
4993 case XPATH_XSLT_TREE:
4994 ret = xmlXPathEqualNodeSets(arg1, arg2, 1);
4995 break;
4996 case XPATH_BOOLEAN:
4997 if ((arg1->nodesetval == NULL) ||
4998 (arg1->nodesetval->nodeNr == 0)) ret = 0;
4999 else
5000 ret = 1;
William M. Brackef61d202002-07-19 08:32:00 +00005001 ret = (ret != arg2->boolval);
William M. Brack0c022ad2002-07-12 00:56:01 +00005002 break;
5003 case XPATH_NUMBER:
5004 ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 1);
5005 break;
5006 case XPATH_STRING:
5007 ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval,1);
5008 break;
5009 case XPATH_USERS:
5010 case XPATH_POINT:
5011 case XPATH_RANGE:
5012 case XPATH_LOCATIONSET:
5013 TODO
5014 break;
5015 }
5016 xmlXPathFreeObject(arg1);
5017 xmlXPathFreeObject(arg2);
5018 return(ret);
5019 }
5020
5021 return (!xmlXPathEqualValuesCommon(ctxt, arg1, arg2));
5022}
Owen Taylor3473f882001-02-23 17:55:21 +00005023
5024/**
5025 * xmlXPathCompareValues:
5026 * @ctxt: the XPath Parser context
5027 * @inf: less than (1) or greater than (0)
5028 * @strict: is the comparison strict
5029 *
5030 * Implement the compare operation on XPath objects:
5031 * @arg1 < @arg2 (1, 1, ...
5032 * @arg1 <= @arg2 (1, 0, ...
5033 * @arg1 > @arg2 (0, 1, ...
5034 * @arg1 >= @arg2 (0, 0, ...
5035 *
5036 * When neither object to be compared is a node-set and the operator is
5037 * <=, <, >=, >, then the objects are compared by converted both objects
5038 * to numbers and comparing the numbers according to IEEE 754. The <
5039 * comparison will be true if and only if the first number is less than the
5040 * second number. The <= comparison will be true if and only if the first
5041 * number is less than or equal to the second number. The > comparison
5042 * will be true if and only if the first number is greater than the second
5043 * number. The >= comparison will be true if and only if the first number
5044 * is greater than or equal to the second number.
5045 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005046 * Returns 1 if the comparison succeeded, 0 if it failed
Owen Taylor3473f882001-02-23 17:55:21 +00005047 */
5048int
5049xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) {
Daniel Veillardd30be4a2002-03-28 18:25:31 +00005050 int ret = 0, arg1i = 0, arg2i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005051 xmlXPathObjectPtr arg1, arg2;
5052
William M. Brack0c022ad2002-07-12 00:56:01 +00005053 arg2 = valuePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005054 arg1 = valuePop(ctxt);
William M. Brack0c022ad2002-07-12 00:56:01 +00005055 if ((arg1 == NULL) || (arg2 == NULL)) {
5056 if (arg1 != NULL)
5057 xmlXPathFreeObject(arg1);
5058 else
5059 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00005060 XP_ERROR0(XPATH_INVALID_OPERAND);
5061 }
5062
William M. Brack0c022ad2002-07-12 00:56:01 +00005063 if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) ||
5064 (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
5065 if (((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE)) &&
5066 ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE))){
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005067 ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00005068 } else {
William M. Brack0c022ad2002-07-12 00:56:01 +00005069 if ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
Daniel Veillard4af6b6e2001-03-06 08:33:38 +00005070 ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict,
5071 arg1, arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00005072 } else {
Daniel Veillard4af6b6e2001-03-06 08:33:38 +00005073 ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict,
5074 arg2, arg1);
Owen Taylor3473f882001-02-23 17:55:21 +00005075 }
5076 }
5077 return(ret);
5078 }
5079
5080 if (arg1->type != XPATH_NUMBER) {
5081 valuePush(ctxt, arg1);
5082 xmlXPathNumberFunction(ctxt, 1);
5083 arg1 = valuePop(ctxt);
5084 }
5085 if (arg1->type != XPATH_NUMBER) {
5086 xmlXPathFreeObject(arg1);
5087 xmlXPathFreeObject(arg2);
5088 XP_ERROR0(XPATH_INVALID_OPERAND);
5089 }
5090 if (arg2->type != XPATH_NUMBER) {
5091 valuePush(ctxt, arg2);
5092 xmlXPathNumberFunction(ctxt, 1);
5093 arg2 = valuePop(ctxt);
5094 }
5095 if (arg2->type != XPATH_NUMBER) {
5096 xmlXPathFreeObject(arg1);
5097 xmlXPathFreeObject(arg2);
5098 XP_ERROR0(XPATH_INVALID_OPERAND);
5099 }
5100 /*
5101 * Add tests for infinity and nan
5102 * => feedback on 3.4 for Inf and NaN
5103 */
Daniel Veillardd30be4a2002-03-28 18:25:31 +00005104 /* Hand check NaN and Infinity comparisons */
Daniel Veillard21458c82002-03-27 16:12:22 +00005105 if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) {
Daniel Veillardd30be4a2002-03-28 18:25:31 +00005106 ret=0;
Daniel Veillard21458c82002-03-27 16:12:22 +00005107 } else {
Daniel Veillardd30be4a2002-03-28 18:25:31 +00005108 arg1i=xmlXPathIsInf(arg1->floatval);
5109 arg2i=xmlXPathIsInf(arg2->floatval);
5110 if (inf && strict) {
5111 if ((arg1i == -1 && arg2i != -1) ||
5112 (arg2i == 1 && arg1i != 1)) {
5113 ret = 1;
5114 } else if (arg1i == 0 && arg2i == 0) {
5115 ret = (arg1->floatval < arg2->floatval);
5116 } else {
5117 ret = 0;
5118 }
5119 }
5120 else if (inf && !strict) {
5121 if (arg1i == -1 || arg2i == 1) {
5122 ret = 1;
5123 } else if (arg1i == 0 && arg2i == 0) {
5124 ret = (arg1->floatval <= arg2->floatval);
5125 } else {
5126 ret = 0;
5127 }
5128 }
5129 else if (!inf && strict) {
5130 if ((arg1i == 1 && arg2i != 1) ||
5131 (arg2i == -1 && arg1i != -1)) {
5132 ret = 1;
5133 } else if (arg1i == 0 && arg2i == 0) {
5134 ret = (arg1->floatval > arg2->floatval);
5135 } else {
5136 ret = 0;
5137 }
5138 }
5139 else if (!inf && !strict) {
5140 if (arg1i == 1 || arg2i == -1) {
5141 ret = 1;
5142 } else if (arg1i == 0 && arg2i == 0) {
5143 ret = (arg1->floatval >= arg2->floatval);
5144 } else {
5145 ret = 0;
5146 }
5147 }
Daniel Veillard21458c82002-03-27 16:12:22 +00005148 }
Owen Taylor3473f882001-02-23 17:55:21 +00005149 xmlXPathFreeObject(arg1);
5150 xmlXPathFreeObject(arg2);
5151 return(ret);
5152}
5153
5154/**
5155 * xmlXPathValueFlipSign:
5156 * @ctxt: the XPath Parser context
5157 *
5158 * Implement the unary - operation on an XPath object
5159 * The numeric operators convert their operands to numbers as if
5160 * by calling the number function.
5161 */
5162void
5163xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005164 CAST_TO_NUMBER;
5165 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillardeca82812002-04-24 11:42:02 +00005166 if (xmlXPathIsNaN(ctxt->value->floatval))
5167 ctxt->value->floatval=xmlXPathNAN;
5168 else if (xmlXPathIsInf(ctxt->value->floatval) == 1)
5169 ctxt->value->floatval=xmlXPathNINF;
5170 else if (xmlXPathIsInf(ctxt->value->floatval) == -1)
5171 ctxt->value->floatval=xmlXPathPINF;
5172 else if (ctxt->value->floatval == 0) {
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005173 if (xmlXPathGetSign(ctxt->value->floatval) == 0)
5174 ctxt->value->floatval = xmlXPathNZERO;
5175 else
5176 ctxt->value->floatval = 0;
5177 }
5178 else
5179 ctxt->value->floatval = - ctxt->value->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00005180}
5181
5182/**
5183 * xmlXPathAddValues:
5184 * @ctxt: the XPath Parser context
5185 *
5186 * Implement the add operation on XPath objects:
5187 * The numeric operators convert their operands to numbers as if
5188 * by calling the number function.
5189 */
5190void
5191xmlXPathAddValues(xmlXPathParserContextPtr ctxt) {
5192 xmlXPathObjectPtr arg;
5193 double val;
5194
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005195 arg = valuePop(ctxt);
5196 if (arg == NULL)
5197 XP_ERROR(XPATH_INVALID_OPERAND);
5198 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005199 xmlXPathFreeObject(arg);
5200
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005201 CAST_TO_NUMBER;
5202 CHECK_TYPE(XPATH_NUMBER);
5203 ctxt->value->floatval += val;
Owen Taylor3473f882001-02-23 17:55:21 +00005204}
5205
5206/**
5207 * xmlXPathSubValues:
5208 * @ctxt: the XPath Parser context
5209 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005210 * Implement the subtraction operation on XPath objects:
Owen Taylor3473f882001-02-23 17:55:21 +00005211 * The numeric operators convert their operands to numbers as if
5212 * by calling the number function.
5213 */
5214void
5215xmlXPathSubValues(xmlXPathParserContextPtr ctxt) {
5216 xmlXPathObjectPtr arg;
5217 double val;
5218
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005219 arg = valuePop(ctxt);
5220 if (arg == NULL)
5221 XP_ERROR(XPATH_INVALID_OPERAND);
5222 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005223 xmlXPathFreeObject(arg);
5224
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005225 CAST_TO_NUMBER;
5226 CHECK_TYPE(XPATH_NUMBER);
5227 ctxt->value->floatval -= val;
Owen Taylor3473f882001-02-23 17:55:21 +00005228}
5229
5230/**
5231 * xmlXPathMultValues:
5232 * @ctxt: the XPath Parser context
5233 *
5234 * Implement the multiply operation on XPath objects:
5235 * The numeric operators convert their operands to numbers as if
5236 * by calling the number function.
5237 */
5238void
5239xmlXPathMultValues(xmlXPathParserContextPtr ctxt) {
5240 xmlXPathObjectPtr arg;
5241 double val;
5242
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005243 arg = valuePop(ctxt);
5244 if (arg == NULL)
5245 XP_ERROR(XPATH_INVALID_OPERAND);
5246 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005247 xmlXPathFreeObject(arg);
5248
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005249 CAST_TO_NUMBER;
5250 CHECK_TYPE(XPATH_NUMBER);
5251 ctxt->value->floatval *= val;
Owen Taylor3473f882001-02-23 17:55:21 +00005252}
5253
5254/**
5255 * xmlXPathDivValues:
5256 * @ctxt: the XPath Parser context
5257 *
5258 * Implement the div operation on XPath objects @arg1 / @arg2:
5259 * The numeric operators convert their operands to numbers as if
5260 * by calling the number function.
5261 */
5262void
5263xmlXPathDivValues(xmlXPathParserContextPtr ctxt) {
5264 xmlXPathObjectPtr arg;
5265 double val;
5266
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005267 arg = valuePop(ctxt);
5268 if (arg == NULL)
5269 XP_ERROR(XPATH_INVALID_OPERAND);
5270 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005271 xmlXPathFreeObject(arg);
5272
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005273 CAST_TO_NUMBER;
5274 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillardeca82812002-04-24 11:42:02 +00005275 if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval))
5276 ctxt->value->floatval = xmlXPathNAN;
5277 else if (val == 0 && xmlXPathGetSign(val) != 0) {
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005278 if (ctxt->value->floatval == 0)
5279 ctxt->value->floatval = xmlXPathNAN;
5280 else if (ctxt->value->floatval > 0)
5281 ctxt->value->floatval = xmlXPathNINF;
5282 else if (ctxt->value->floatval < 0)
5283 ctxt->value->floatval = xmlXPathPINF;
5284 }
5285 else if (val == 0) {
Daniel Veillard5f4b5992002-02-20 10:22:49 +00005286 if (ctxt->value->floatval == 0)
5287 ctxt->value->floatval = xmlXPathNAN;
5288 else if (ctxt->value->floatval > 0)
5289 ctxt->value->floatval = xmlXPathPINF;
5290 else if (ctxt->value->floatval < 0)
5291 ctxt->value->floatval = xmlXPathNINF;
5292 } else
5293 ctxt->value->floatval /= val;
Owen Taylor3473f882001-02-23 17:55:21 +00005294}
5295
5296/**
5297 * xmlXPathModValues:
5298 * @ctxt: the XPath Parser context
5299 *
5300 * Implement the mod operation on XPath objects: @arg1 / @arg2
5301 * The numeric operators convert their operands to numbers as if
5302 * by calling the number function.
5303 */
5304void
5305xmlXPathModValues(xmlXPathParserContextPtr ctxt) {
5306 xmlXPathObjectPtr arg;
Daniel Veillardfdc91562002-07-01 21:52:03 +00005307 double arg1, arg2;
Owen Taylor3473f882001-02-23 17:55:21 +00005308
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005309 arg = valuePop(ctxt);
5310 if (arg == NULL)
5311 XP_ERROR(XPATH_INVALID_OPERAND);
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005312 arg2 = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005313 xmlXPathFreeObject(arg);
5314
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005315 CAST_TO_NUMBER;
5316 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005317 arg1 = ctxt->value->floatval;
Daniel Veillard268fd1b2001-08-26 18:46:36 +00005318 if (arg2 == 0)
5319 ctxt->value->floatval = xmlXPathNAN;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005320 else {
Daniel Veillardfdc91562002-07-01 21:52:03 +00005321 ctxt->value->floatval = fmod(arg1, arg2);
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005322 }
Owen Taylor3473f882001-02-23 17:55:21 +00005323}
5324
5325/************************************************************************
5326 * *
5327 * The traversal functions *
5328 * *
5329 ************************************************************************/
5330
Owen Taylor3473f882001-02-23 17:55:21 +00005331/*
5332 * A traversal function enumerates nodes along an axis.
5333 * Initially it must be called with NULL, and it indicates
5334 * termination on the axis by returning NULL.
5335 */
5336typedef xmlNodePtr (*xmlXPathTraversalFunction)
5337 (xmlXPathParserContextPtr ctxt, xmlNodePtr cur);
5338
5339/**
5340 * xmlXPathNextSelf:
5341 * @ctxt: the XPath Parser context
5342 * @cur: the current node in the traversal
5343 *
5344 * Traversal function for the "self" direction
5345 * The self axis contains just the context node itself
5346 *
5347 * Returns the next element following that axis
5348 */
5349xmlNodePtr
5350xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5351 if (cur == NULL)
5352 return(ctxt->context->node);
5353 return(NULL);
5354}
5355
5356/**
5357 * xmlXPathNextChild:
5358 * @ctxt: the XPath Parser context
5359 * @cur: the current node in the traversal
5360 *
5361 * Traversal function for the "child" direction
5362 * The child axis contains the children of the context node in document order.
5363 *
5364 * Returns the next element following that axis
5365 */
5366xmlNodePtr
5367xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5368 if (cur == NULL) {
5369 if (ctxt->context->node == NULL) return(NULL);
5370 switch (ctxt->context->node->type) {
5371 case XML_ELEMENT_NODE:
5372 case XML_TEXT_NODE:
5373 case XML_CDATA_SECTION_NODE:
5374 case XML_ENTITY_REF_NODE:
5375 case XML_ENTITY_NODE:
5376 case XML_PI_NODE:
5377 case XML_COMMENT_NODE:
5378 case XML_NOTATION_NODE:
5379 case XML_DTD_NODE:
5380 return(ctxt->context->node->children);
5381 case XML_DOCUMENT_NODE:
5382 case XML_DOCUMENT_TYPE_NODE:
5383 case XML_DOCUMENT_FRAG_NODE:
5384 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005385#ifdef LIBXML_DOCB_ENABLED
5386 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005387#endif
5388 return(((xmlDocPtr) ctxt->context->node)->children);
5389 case XML_ELEMENT_DECL:
5390 case XML_ATTRIBUTE_DECL:
5391 case XML_ENTITY_DECL:
5392 case XML_ATTRIBUTE_NODE:
5393 case XML_NAMESPACE_DECL:
5394 case XML_XINCLUDE_START:
5395 case XML_XINCLUDE_END:
5396 return(NULL);
5397 }
5398 return(NULL);
5399 }
5400 if ((cur->type == XML_DOCUMENT_NODE) ||
5401 (cur->type == XML_HTML_DOCUMENT_NODE))
5402 return(NULL);
5403 return(cur->next);
5404}
5405
5406/**
5407 * xmlXPathNextDescendant:
5408 * @ctxt: the XPath Parser context
5409 * @cur: the current node in the traversal
5410 *
5411 * Traversal function for the "descendant" direction
5412 * the descendant axis contains the descendants of the context node in document
5413 * order; a descendant is a child or a child of a child and so on.
5414 *
5415 * Returns the next element following that axis
5416 */
5417xmlNodePtr
5418xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5419 if (cur == NULL) {
5420 if (ctxt->context->node == NULL)
5421 return(NULL);
5422 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5423 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5424 return(NULL);
5425
5426 if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc)
5427 return(ctxt->context->doc->children);
5428 return(ctxt->context->node->children);
5429 }
5430
Daniel Veillard567e1b42001-08-01 15:53:47 +00005431 if (cur->children != NULL) {
Daniel Veillard68e9e742002-11-16 15:35:11 +00005432 /*
5433 * Do not descend on entities declarations
5434 */
5435 if (cur->children->type != XML_ENTITY_DECL) {
5436 cur = cur->children;
5437 /*
5438 * Skip DTDs
5439 */
5440 if (cur->type != XML_DTD_NODE)
5441 return(cur);
5442 }
Daniel Veillard567e1b42001-08-01 15:53:47 +00005443 }
5444
5445 if (cur == ctxt->context->node) return(NULL);
5446
Daniel Veillard68e9e742002-11-16 15:35:11 +00005447 while (cur->next != NULL) {
5448 cur = cur->next;
5449 if ((cur->type != XML_ENTITY_DECL) &&
5450 (cur->type != XML_DTD_NODE))
5451 return(cur);
5452 }
Owen Taylor3473f882001-02-23 17:55:21 +00005453
5454 do {
5455 cur = cur->parent;
5456 if (cur == NULL) return(NULL);
5457 if (cur == ctxt->context->node) return(NULL);
5458 if (cur->next != NULL) {
5459 cur = cur->next;
5460 return(cur);
5461 }
5462 } while (cur != NULL);
5463 return(cur);
5464}
5465
5466/**
5467 * xmlXPathNextDescendantOrSelf:
5468 * @ctxt: the XPath Parser context
5469 * @cur: the current node in the traversal
5470 *
5471 * Traversal function for the "descendant-or-self" direction
5472 * the descendant-or-self axis contains the context node and the descendants
5473 * of the context node in document order; thus the context node is the first
5474 * node on the axis, and the first child of the context node is the second node
5475 * on the axis
5476 *
5477 * Returns the next element following that axis
5478 */
5479xmlNodePtr
5480xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5481 if (cur == NULL) {
5482 if (ctxt->context->node == NULL)
5483 return(NULL);
5484 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5485 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5486 return(NULL);
5487 return(ctxt->context->node);
5488 }
5489
5490 return(xmlXPathNextDescendant(ctxt, cur));
5491}
5492
5493/**
5494 * xmlXPathNextParent:
5495 * @ctxt: the XPath Parser context
5496 * @cur: the current node in the traversal
5497 *
5498 * Traversal function for the "parent" direction
5499 * The parent axis contains the parent of the context node, if there is one.
5500 *
5501 * Returns the next element following that axis
5502 */
5503xmlNodePtr
5504xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5505 /*
5506 * the parent of an attribute or namespace node is the element
5507 * to which the attribute or namespace node is attached
5508 * Namespace handling !!!
5509 */
5510 if (cur == NULL) {
5511 if (ctxt->context->node == NULL) return(NULL);
5512 switch (ctxt->context->node->type) {
5513 case XML_ELEMENT_NODE:
5514 case XML_TEXT_NODE:
5515 case XML_CDATA_SECTION_NODE:
5516 case XML_ENTITY_REF_NODE:
5517 case XML_ENTITY_NODE:
5518 case XML_PI_NODE:
5519 case XML_COMMENT_NODE:
5520 case XML_NOTATION_NODE:
5521 case XML_DTD_NODE:
5522 case XML_ELEMENT_DECL:
5523 case XML_ATTRIBUTE_DECL:
5524 case XML_XINCLUDE_START:
5525 case XML_XINCLUDE_END:
5526 case XML_ENTITY_DECL:
5527 if (ctxt->context->node->parent == NULL)
5528 return((xmlNodePtr) ctxt->context->doc);
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005529 if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) &&
Daniel Veillard652d8a92003-02-04 19:28:49 +00005530 ((ctxt->context->node->parent->name[0] == ' ') ||
5531 (xmlStrEqual(ctxt->context->node->parent->name,
5532 BAD_CAST "fake node libxslt"))))
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005533 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005534 return(ctxt->context->node->parent);
5535 case XML_ATTRIBUTE_NODE: {
5536 xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node;
5537
5538 return(att->parent);
5539 }
5540 case XML_DOCUMENT_NODE:
5541 case XML_DOCUMENT_TYPE_NODE:
5542 case XML_DOCUMENT_FRAG_NODE:
5543 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005544#ifdef LIBXML_DOCB_ENABLED
5545 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005546#endif
5547 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005548 case XML_NAMESPACE_DECL: {
5549 xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
5550
5551 if ((ns->next != NULL) &&
5552 (ns->next->type != XML_NAMESPACE_DECL))
5553 return((xmlNodePtr) ns->next);
Owen Taylor3473f882001-02-23 17:55:21 +00005554 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005555 }
Owen Taylor3473f882001-02-23 17:55:21 +00005556 }
5557 }
5558 return(NULL);
5559}
5560
5561/**
5562 * xmlXPathNextAncestor:
5563 * @ctxt: the XPath Parser context
5564 * @cur: the current node in the traversal
5565 *
5566 * Traversal function for the "ancestor" direction
5567 * the ancestor axis contains the ancestors of the context node; the ancestors
5568 * of the context node consist of the parent of context node and the parent's
5569 * parent and so on; the nodes are ordered in reverse document order; thus the
5570 * parent is the first node on the axis, and the parent's parent is the second
5571 * node on the axis
5572 *
5573 * Returns the next element following that axis
5574 */
5575xmlNodePtr
5576xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5577 /*
5578 * the parent of an attribute or namespace node is the element
5579 * to which the attribute or namespace node is attached
5580 * !!!!!!!!!!!!!
5581 */
5582 if (cur == NULL) {
5583 if (ctxt->context->node == NULL) return(NULL);
5584 switch (ctxt->context->node->type) {
5585 case XML_ELEMENT_NODE:
5586 case XML_TEXT_NODE:
5587 case XML_CDATA_SECTION_NODE:
5588 case XML_ENTITY_REF_NODE:
5589 case XML_ENTITY_NODE:
5590 case XML_PI_NODE:
5591 case XML_COMMENT_NODE:
5592 case XML_DTD_NODE:
5593 case XML_ELEMENT_DECL:
5594 case XML_ATTRIBUTE_DECL:
5595 case XML_ENTITY_DECL:
5596 case XML_NOTATION_NODE:
5597 case XML_XINCLUDE_START:
5598 case XML_XINCLUDE_END:
5599 if (ctxt->context->node->parent == NULL)
5600 return((xmlNodePtr) ctxt->context->doc);
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005601 if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) &&
Daniel Veillard652d8a92003-02-04 19:28:49 +00005602 ((ctxt->context->node->parent->name[0] == ' ') ||
5603 (xmlStrEqual(ctxt->context->node->parent->name,
5604 BAD_CAST "fake node libxslt"))))
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005605 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005606 return(ctxt->context->node->parent);
5607 case XML_ATTRIBUTE_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005608 xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node;
Owen Taylor3473f882001-02-23 17:55:21 +00005609
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005610 return(tmp->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005611 }
5612 case XML_DOCUMENT_NODE:
5613 case XML_DOCUMENT_TYPE_NODE:
5614 case XML_DOCUMENT_FRAG_NODE:
5615 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005616#ifdef LIBXML_DOCB_ENABLED
5617 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005618#endif
5619 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005620 case XML_NAMESPACE_DECL: {
5621 xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
5622
5623 if ((ns->next != NULL) &&
5624 (ns->next->type != XML_NAMESPACE_DECL))
5625 return((xmlNodePtr) ns->next);
William M. Brack08171912003-12-29 02:52:11 +00005626 /* Bad, how did that namespace end up here ? */
Owen Taylor3473f882001-02-23 17:55:21 +00005627 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005628 }
Owen Taylor3473f882001-02-23 17:55:21 +00005629 }
5630 return(NULL);
5631 }
5632 if (cur == ctxt->context->doc->children)
5633 return((xmlNodePtr) ctxt->context->doc);
5634 if (cur == (xmlNodePtr) ctxt->context->doc)
5635 return(NULL);
5636 switch (cur->type) {
5637 case XML_ELEMENT_NODE:
5638 case XML_TEXT_NODE:
5639 case XML_CDATA_SECTION_NODE:
5640 case XML_ENTITY_REF_NODE:
5641 case XML_ENTITY_NODE:
5642 case XML_PI_NODE:
5643 case XML_COMMENT_NODE:
5644 case XML_NOTATION_NODE:
5645 case XML_DTD_NODE:
5646 case XML_ELEMENT_DECL:
5647 case XML_ATTRIBUTE_DECL:
5648 case XML_ENTITY_DECL:
5649 case XML_XINCLUDE_START:
5650 case XML_XINCLUDE_END:
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005651 if (cur->parent == NULL)
5652 return(NULL);
5653 if ((cur->parent->type == XML_ELEMENT_NODE) &&
Daniel Veillard652d8a92003-02-04 19:28:49 +00005654 ((cur->parent->name[0] == ' ') ||
5655 (xmlStrEqual(cur->parent->name,
5656 BAD_CAST "fake node libxslt"))))
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005657 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005658 return(cur->parent);
5659 case XML_ATTRIBUTE_NODE: {
5660 xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node;
5661
5662 return(att->parent);
5663 }
Aleksey Sanindffd5c82002-05-31 04:24:13 +00005664 case XML_NAMESPACE_DECL: {
5665 xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
5666
5667 if ((ns->next != NULL) &&
5668 (ns->next->type != XML_NAMESPACE_DECL))
5669 return((xmlNodePtr) ns->next);
William M. Brack08171912003-12-29 02:52:11 +00005670 /* Bad, how did that namespace end up here ? */
Aleksey Sanindffd5c82002-05-31 04:24:13 +00005671 return(NULL);
5672 }
Owen Taylor3473f882001-02-23 17:55:21 +00005673 case XML_DOCUMENT_NODE:
5674 case XML_DOCUMENT_TYPE_NODE:
5675 case XML_DOCUMENT_FRAG_NODE:
5676 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005677#ifdef LIBXML_DOCB_ENABLED
5678 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005679#endif
5680 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005681 }
5682 return(NULL);
5683}
5684
5685/**
5686 * xmlXPathNextAncestorOrSelf:
5687 * @ctxt: the XPath Parser context
5688 * @cur: the current node in the traversal
5689 *
5690 * Traversal function for the "ancestor-or-self" direction
5691 * he ancestor-or-self axis contains the context node and ancestors of
5692 * the context node in reverse document order; thus the context node is
5693 * the first node on the axis, and the context node's parent the second;
5694 * parent here is defined the same as with the parent axis.
5695 *
5696 * Returns the next element following that axis
5697 */
5698xmlNodePtr
5699xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5700 if (cur == NULL)
5701 return(ctxt->context->node);
5702 return(xmlXPathNextAncestor(ctxt, cur));
5703}
5704
5705/**
5706 * xmlXPathNextFollowingSibling:
5707 * @ctxt: the XPath Parser context
5708 * @cur: the current node in the traversal
5709 *
5710 * Traversal function for the "following-sibling" direction
5711 * The following-sibling axis contains the following siblings of the context
5712 * node in document order.
5713 *
5714 * Returns the next element following that axis
5715 */
5716xmlNodePtr
5717xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5718 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5719 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5720 return(NULL);
5721 if (cur == (xmlNodePtr) ctxt->context->doc)
5722 return(NULL);
5723 if (cur == NULL)
5724 return(ctxt->context->node->next);
5725 return(cur->next);
5726}
5727
5728/**
5729 * xmlXPathNextPrecedingSibling:
5730 * @ctxt: the XPath Parser context
5731 * @cur: the current node in the traversal
5732 *
5733 * Traversal function for the "preceding-sibling" direction
5734 * The preceding-sibling axis contains the preceding siblings of the context
5735 * node in reverse document order; the first preceding sibling is first on the
5736 * axis; the sibling preceding that node is the second on the axis and so on.
5737 *
5738 * Returns the next element following that axis
5739 */
5740xmlNodePtr
5741xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5742 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5743 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5744 return(NULL);
5745 if (cur == (xmlNodePtr) ctxt->context->doc)
5746 return(NULL);
5747 if (cur == NULL)
5748 return(ctxt->context->node->prev);
Daniel Veillardf06307e2001-07-03 10:35:50 +00005749 if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) {
5750 cur = cur->prev;
5751 if (cur == NULL)
5752 return(ctxt->context->node->prev);
5753 }
Owen Taylor3473f882001-02-23 17:55:21 +00005754 return(cur->prev);
5755}
5756
5757/**
5758 * xmlXPathNextFollowing:
5759 * @ctxt: the XPath Parser context
5760 * @cur: the current node in the traversal
5761 *
5762 * Traversal function for the "following" direction
5763 * The following axis contains all nodes in the same document as the context
5764 * node that are after the context node in document order, excluding any
5765 * descendants and excluding attribute nodes and namespace nodes; the nodes
5766 * are ordered in document order
5767 *
5768 * Returns the next element following that axis
5769 */
5770xmlNodePtr
5771xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5772 if (cur != NULL && cur->children != NULL)
5773 return cur->children ;
5774 if (cur == NULL) cur = ctxt->context->node;
5775 if (cur == NULL) return(NULL) ; /* ERROR */
5776 if (cur->next != NULL) return(cur->next) ;
5777 do {
5778 cur = cur->parent;
5779 if (cur == NULL) return(NULL);
5780 if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL);
5781 if (cur->next != NULL) return(cur->next);
5782 } while (cur != NULL);
5783 return(cur);
5784}
5785
5786/*
5787 * xmlXPathIsAncestor:
5788 * @ancestor: the ancestor node
5789 * @node: the current node
5790 *
5791 * Check that @ancestor is a @node's ancestor
5792 *
5793 * returns 1 if @ancestor is a @node's ancestor, 0 otherwise.
5794 */
5795static int
5796xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) {
5797 if ((ancestor == NULL) || (node == NULL)) return(0);
5798 /* nodes need to be in the same document */
5799 if (ancestor->doc != node->doc) return(0);
5800 /* avoid searching if ancestor or node is the root node */
5801 if (ancestor == (xmlNodePtr) node->doc) return(1);
5802 if (node == (xmlNodePtr) ancestor->doc) return(0);
5803 while (node->parent != NULL) {
5804 if (node->parent == ancestor)
5805 return(1);
5806 node = node->parent;
5807 }
5808 return(0);
5809}
5810
5811/**
5812 * xmlXPathNextPreceding:
5813 * @ctxt: the XPath Parser context
5814 * @cur: the current node in the traversal
5815 *
5816 * Traversal function for the "preceding" direction
5817 * the preceding axis contains all nodes in the same document as the context
5818 * node that are before the context node in document order, excluding any
5819 * ancestors and excluding attribute nodes and namespace nodes; the nodes are
5820 * ordered in reverse document order
5821 *
5822 * Returns the next element following that axis
5823 */
5824xmlNodePtr
Daniel Veillardf06307e2001-07-03 10:35:50 +00005825xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur)
5826{
Owen Taylor3473f882001-02-23 17:55:21 +00005827 if (cur == NULL)
Daniel Veillardf06307e2001-07-03 10:35:50 +00005828 cur = ctxt->context->node;
5829 if (cur == NULL)
5830 return (NULL);
5831 if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
5832 cur = cur->prev;
Owen Taylor3473f882001-02-23 17:55:21 +00005833 do {
5834 if (cur->prev != NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00005835 for (cur = cur->prev; cur->last != NULL; cur = cur->last) ;
5836 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00005837 }
5838
5839 cur = cur->parent;
Daniel Veillardf06307e2001-07-03 10:35:50 +00005840 if (cur == NULL)
5841 return (NULL);
5842 if (cur == ctxt->context->doc->children)
5843 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005844 } while (xmlXPathIsAncestor(cur, ctxt->context->node));
Daniel Veillardf06307e2001-07-03 10:35:50 +00005845 return (cur);
5846}
5847
5848/**
5849 * xmlXPathNextPrecedingInternal:
5850 * @ctxt: the XPath Parser context
5851 * @cur: the current node in the traversal
5852 *
5853 * Traversal function for the "preceding" direction
5854 * the preceding axis contains all nodes in the same document as the context
5855 * node that are before the context node in document order, excluding any
5856 * ancestors and excluding attribute nodes and namespace nodes; the nodes are
5857 * ordered in reverse document order
5858 * This is a faster implementation but internal only since it requires a
5859 * state kept in the parser context: ctxt->ancestor.
5860 *
5861 * Returns the next element following that axis
5862 */
5863static xmlNodePtr
5864xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt,
5865 xmlNodePtr cur)
5866{
5867 if (cur == NULL) {
5868 cur = ctxt->context->node;
5869 if (cur == NULL)
5870 return (NULL);
William M. Brack40c22b42003-10-10 03:58:39 +00005871 if (cur->type == XML_NAMESPACE_DECL)
5872 cur = (xmlNodePtr)((xmlNsPtr)cur)->next;
Daniel Veillardf06307e2001-07-03 10:35:50 +00005873 ctxt->ancestor = cur->parent;
5874 }
5875 if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
5876 cur = cur->prev;
5877 while (cur->prev == NULL) {
5878 cur = cur->parent;
5879 if (cur == NULL)
5880 return (NULL);
5881 if (cur == ctxt->context->doc->children)
5882 return (NULL);
5883 if (cur != ctxt->ancestor)
5884 return (cur);
5885 ctxt->ancestor = cur->parent;
5886 }
5887 cur = cur->prev;
5888 while (cur->last != NULL)
5889 cur = cur->last;
5890 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00005891}
5892
5893/**
5894 * xmlXPathNextNamespace:
5895 * @ctxt: the XPath Parser context
5896 * @cur: the current attribute in the traversal
5897 *
5898 * Traversal function for the "namespace" direction
5899 * the namespace axis contains the namespace nodes of the context node;
5900 * the order of nodes on this axis is implementation-defined; the axis will
5901 * be empty unless the context node is an element
5902 *
Daniel Veillard20ee8c02001-10-05 09:18:14 +00005903 * We keep the XML namespace node at the end of the list.
5904 *
Owen Taylor3473f882001-02-23 17:55:21 +00005905 * Returns the next element following that axis
5906 */
5907xmlNodePtr
5908xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5909 if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL);
Daniel Veillardfdc91562002-07-01 21:52:03 +00005910 if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) {
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005911 if (ctxt->context->tmpNsList != NULL)
5912 xmlFree(ctxt->context->tmpNsList);
5913 ctxt->context->tmpNsList =
Owen Taylor3473f882001-02-23 17:55:21 +00005914 xmlGetNsList(ctxt->context->doc, ctxt->context->node);
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005915 ctxt->context->tmpNsNr = 0;
Daniel Veillardfdc91562002-07-01 21:52:03 +00005916 if (ctxt->context->tmpNsList != NULL) {
5917 while (ctxt->context->tmpNsList[ctxt->context->tmpNsNr] != NULL) {
5918 ctxt->context->tmpNsNr++;
5919 }
5920 }
Daniel Veillard20ee8c02001-10-05 09:18:14 +00005921 return((xmlNodePtr) xmlXPathXMLNamespace);
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005922 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00005923 if (ctxt->context->tmpNsNr > 0) {
5924 return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr];
5925 } else {
5926 if (ctxt->context->tmpNsList != NULL)
5927 xmlFree(ctxt->context->tmpNsList);
5928 ctxt->context->tmpNsList = NULL;
5929 return(NULL);
5930 }
Owen Taylor3473f882001-02-23 17:55:21 +00005931}
5932
5933/**
5934 * xmlXPathNextAttribute:
5935 * @ctxt: the XPath Parser context
5936 * @cur: the current attribute in the traversal
5937 *
5938 * Traversal function for the "attribute" direction
5939 * TODO: support DTD inherited default attributes
5940 *
5941 * Returns the next element following that axis
5942 */
5943xmlNodePtr
5944xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarde470df72001-04-18 21:41:07 +00005945 if (ctxt->context->node == NULL)
5946 return(NULL);
5947 if (ctxt->context->node->type != XML_ELEMENT_NODE)
5948 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005949 if (cur == NULL) {
5950 if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc)
5951 return(NULL);
5952 return((xmlNodePtr)ctxt->context->node->properties);
5953 }
5954 return((xmlNodePtr)cur->next);
5955}
5956
5957/************************************************************************
5958 * *
5959 * NodeTest Functions *
5960 * *
5961 ************************************************************************/
5962
Owen Taylor3473f882001-02-23 17:55:21 +00005963#define IS_FUNCTION 200
5964
Owen Taylor3473f882001-02-23 17:55:21 +00005965
5966/************************************************************************
5967 * *
5968 * Implicit tree core function library *
5969 * *
5970 ************************************************************************/
5971
5972/**
5973 * xmlXPathRoot:
5974 * @ctxt: the XPath Parser context
5975 *
5976 * Initialize the context to the root of the document
5977 */
5978void
5979xmlXPathRoot(xmlXPathParserContextPtr ctxt) {
5980 ctxt->context->node = (xmlNodePtr) ctxt->context->doc;
5981 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
5982}
5983
5984/************************************************************************
5985 * *
5986 * The explicit core function library *
5987 *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib *
5988 * *
5989 ************************************************************************/
5990
5991
5992/**
5993 * xmlXPathLastFunction:
5994 * @ctxt: the XPath Parser context
5995 * @nargs: the number of arguments
5996 *
5997 * Implement the last() XPath function
5998 * number last()
5999 * The last function returns the number of nodes in the context node list.
6000 */
6001void
6002xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6003 CHECK_ARITY(0);
6004 if (ctxt->context->contextSize >= 0) {
6005 valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize));
6006#ifdef DEBUG_EXPR
6007 xmlGenericError(xmlGenericErrorContext,
6008 "last() : %d\n", ctxt->context->contextSize);
6009#endif
6010 } else {
6011 XP_ERROR(XPATH_INVALID_CTXT_SIZE);
6012 }
6013}
6014
6015/**
6016 * xmlXPathPositionFunction:
6017 * @ctxt: the XPath Parser context
6018 * @nargs: the number of arguments
6019 *
6020 * Implement the position() XPath function
6021 * number position()
6022 * The position function returns the position of the context node in the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006023 * context node list. The first position is 1, and so the last position
Owen Taylor3473f882001-02-23 17:55:21 +00006024 * will be equal to last().
6025 */
6026void
6027xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6028 CHECK_ARITY(0);
6029 if (ctxt->context->proximityPosition >= 0) {
6030 valuePush(ctxt,
6031 xmlXPathNewFloat((double) ctxt->context->proximityPosition));
6032#ifdef DEBUG_EXPR
6033 xmlGenericError(xmlGenericErrorContext, "position() : %d\n",
6034 ctxt->context->proximityPosition);
6035#endif
6036 } else {
6037 XP_ERROR(XPATH_INVALID_CTXT_POSITION);
6038 }
6039}
6040
6041/**
6042 * xmlXPathCountFunction:
6043 * @ctxt: the XPath Parser context
6044 * @nargs: the number of arguments
6045 *
6046 * Implement the count() XPath function
6047 * number count(node-set)
6048 */
6049void
6050xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6051 xmlXPathObjectPtr cur;
6052
6053 CHECK_ARITY(1);
6054 if ((ctxt->value == NULL) ||
6055 ((ctxt->value->type != XPATH_NODESET) &&
6056 (ctxt->value->type != XPATH_XSLT_TREE)))
6057 XP_ERROR(XPATH_INVALID_TYPE);
6058 cur = valuePop(ctxt);
6059
Daniel Veillard911f49a2001-04-07 15:39:35 +00006060 if ((cur == NULL) || (cur->nodesetval == NULL))
6061 valuePush(ctxt, xmlXPathNewFloat((double) 0));
William M. Brack0c022ad2002-07-12 00:56:01 +00006062 else if ((cur->type == XPATH_NODESET) || (cur->type == XPATH_XSLT_TREE)) {
Daniel Veillard911f49a2001-04-07 15:39:35 +00006063 valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr));
Daniel Veillardfe703322001-08-14 12:18:09 +00006064 } else {
6065 if ((cur->nodesetval->nodeNr != 1) ||
6066 (cur->nodesetval->nodeTab == NULL)) {
6067 valuePush(ctxt, xmlXPathNewFloat((double) 0));
6068 } else {
6069 xmlNodePtr tmp;
6070 int i = 0;
6071
6072 tmp = cur->nodesetval->nodeTab[0];
6073 if (tmp != NULL) {
6074 tmp = tmp->children;
6075 while (tmp != NULL) {
6076 tmp = tmp->next;
6077 i++;
6078 }
6079 }
6080 valuePush(ctxt, xmlXPathNewFloat((double) i));
6081 }
6082 }
Owen Taylor3473f882001-02-23 17:55:21 +00006083 xmlXPathFreeObject(cur);
6084}
6085
6086/**
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006087 * xmlXPathGetElementsByIds:
6088 * @doc: the document
6089 * @ids: a whitespace separated list of IDs
6090 *
6091 * Selects elements by their unique ID.
6092 *
6093 * Returns a node-set of selected elements.
6094 */
6095static xmlNodeSetPtr
6096xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) {
6097 xmlNodeSetPtr ret;
6098 const xmlChar *cur = ids;
6099 xmlChar *ID;
6100 xmlAttrPtr attr;
6101 xmlNodePtr elem = NULL;
6102
Daniel Veillard7a985a12003-07-06 17:57:42 +00006103 if (ids == NULL) return(NULL);
6104
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006105 ret = xmlXPathNodeSetCreate(NULL);
6106
William M. Brack76e95df2003-10-18 16:20:14 +00006107 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006108 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006109 while ((!IS_BLANK_CH(*cur)) && (*cur != 0))
Daniel Veillarde209b332003-03-26 21:40:13 +00006110 cur++;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006111
6112 ID = xmlStrndup(ids, cur - ids);
Daniel Veillarde209b332003-03-26 21:40:13 +00006113 if (ID != NULL) {
Daniel Veillard68cb4b22004-04-18 20:55:39 +00006114 /*
6115 * We used to check the fact that the value passed
6116 * was an NCName, but this generated much troubles for
6117 * me and Aleksey Sanin, people blatantly violated that
6118 * constaint, like Visa3D spec.
6119 * if (xmlValidateNCName(ID, 1) == 0)
6120 */
6121 attr = xmlGetID(doc, ID);
6122 if (attr != NULL) {
6123 if (attr->type == XML_ATTRIBUTE_NODE)
6124 elem = attr->parent;
6125 else if (attr->type == XML_ELEMENT_NODE)
6126 elem = (xmlNodePtr) attr;
6127 else
6128 elem = NULL;
6129 if (elem != NULL)
6130 xmlXPathNodeSetAdd(ret, elem);
Daniel Veillarde209b332003-03-26 21:40:13 +00006131 }
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006132 xmlFree(ID);
Daniel Veillarde209b332003-03-26 21:40:13 +00006133 }
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006134
William M. Brack76e95df2003-10-18 16:20:14 +00006135 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006136 ids = cur;
6137 }
6138 return(ret);
6139}
6140
6141/**
Owen Taylor3473f882001-02-23 17:55:21 +00006142 * xmlXPathIdFunction:
6143 * @ctxt: the XPath Parser context
6144 * @nargs: the number of arguments
6145 *
6146 * Implement the id() XPath function
6147 * node-set id(object)
6148 * The id function selects elements by their unique ID
6149 * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set,
6150 * then the result is the union of the result of applying id to the
6151 * string value of each of the nodes in the argument node-set. When the
6152 * argument to id is of any other type, the argument is converted to a
6153 * string as if by a call to the string function; the string is split
6154 * into a whitespace-separated list of tokens (whitespace is any sequence
6155 * of characters matching the production S); the result is a node-set
6156 * containing the elements in the same document as the context node that
6157 * have a unique ID equal to any of the tokens in the list.
6158 */
6159void
6160xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006161 xmlChar *tokens;
6162 xmlNodeSetPtr ret;
6163 xmlXPathObjectPtr obj;
Owen Taylor3473f882001-02-23 17:55:21 +00006164
6165 CHECK_ARITY(1);
6166 obj = valuePop(ctxt);
6167 if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
William M. Brack0c022ad2002-07-12 00:56:01 +00006168 if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006169 xmlNodeSetPtr ns;
Owen Taylor3473f882001-02-23 17:55:21 +00006170 int i;
6171
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006172 ret = xmlXPathNodeSetCreate(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006173
Daniel Veillard911f49a2001-04-07 15:39:35 +00006174 if (obj->nodesetval != NULL) {
6175 for (i = 0; i < obj->nodesetval->nodeNr; i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006176 tokens =
6177 xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]);
6178 ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens);
6179 ret = xmlXPathNodeSetMerge(ret, ns);
6180 xmlXPathFreeNodeSet(ns);
6181 if (tokens != NULL)
6182 xmlFree(tokens);
Daniel Veillard911f49a2001-04-07 15:39:35 +00006183 }
Owen Taylor3473f882001-02-23 17:55:21 +00006184 }
6185
6186 xmlXPathFreeObject(obj);
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006187 valuePush(ctxt, xmlXPathWrapNodeSet(ret));
Owen Taylor3473f882001-02-23 17:55:21 +00006188 return;
6189 }
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006190 obj = xmlXPathConvertString(obj);
Owen Taylor3473f882001-02-23 17:55:21 +00006191
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006192 ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval);
6193 valuePush(ctxt, xmlXPathWrapNodeSet(ret));
Owen Taylor3473f882001-02-23 17:55:21 +00006194
Owen Taylor3473f882001-02-23 17:55:21 +00006195 xmlXPathFreeObject(obj);
6196 return;
6197}
6198
6199/**
6200 * xmlXPathLocalNameFunction:
6201 * @ctxt: the XPath Parser context
6202 * @nargs: the number of arguments
6203 *
6204 * Implement the local-name() XPath function
6205 * string local-name(node-set?)
6206 * The local-name function returns a string containing the local part
6207 * of the name of the node in the argument node-set that is first in
6208 * document order. If the node-set is empty or the first node has no
6209 * name, an empty string is returned. If the argument is omitted it
6210 * defaults to the context node.
6211 */
6212void
6213xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6214 xmlXPathObjectPtr cur;
6215
6216 if (nargs == 0) {
6217 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
6218 nargs = 1;
6219 }
6220
6221 CHECK_ARITY(1);
6222 if ((ctxt->value == NULL) ||
6223 ((ctxt->value->type != XPATH_NODESET) &&
6224 (ctxt->value->type != XPATH_XSLT_TREE)))
6225 XP_ERROR(XPATH_INVALID_TYPE);
6226 cur = valuePop(ctxt);
6227
Daniel Veillard911f49a2001-04-07 15:39:35 +00006228 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006229 valuePush(ctxt, xmlXPathNewCString(""));
6230 } else {
6231 int i = 0; /* Should be first in document order !!!!! */
6232 switch (cur->nodesetval->nodeTab[i]->type) {
6233 case XML_ELEMENT_NODE:
6234 case XML_ATTRIBUTE_NODE:
6235 case XML_PI_NODE:
Daniel Veillard652d8a92003-02-04 19:28:49 +00006236 if (cur->nodesetval->nodeTab[i]->name[0] == ' ')
6237 valuePush(ctxt, xmlXPathNewCString(""));
6238 else
6239 valuePush(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00006240 xmlXPathNewString(cur->nodesetval->nodeTab[i]->name));
6241 break;
6242 case XML_NAMESPACE_DECL:
6243 valuePush(ctxt, xmlXPathNewString(
6244 ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix));
6245 break;
6246 default:
6247 valuePush(ctxt, xmlXPathNewCString(""));
6248 }
6249 }
6250 xmlXPathFreeObject(cur);
6251}
6252
6253/**
6254 * xmlXPathNamespaceURIFunction:
6255 * @ctxt: the XPath Parser context
6256 * @nargs: the number of arguments
6257 *
6258 * Implement the namespace-uri() XPath function
6259 * string namespace-uri(node-set?)
6260 * The namespace-uri function returns a string containing the
6261 * namespace URI of the expanded name of the node in the argument
6262 * node-set that is first in document order. If the node-set is empty,
6263 * the first node has no name, or the expanded name has no namespace
6264 * URI, an empty string is returned. If the argument is omitted it
6265 * defaults to the context node.
6266 */
6267void
6268xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6269 xmlXPathObjectPtr cur;
6270
6271 if (nargs == 0) {
6272 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
6273 nargs = 1;
6274 }
6275 CHECK_ARITY(1);
6276 if ((ctxt->value == NULL) ||
6277 ((ctxt->value->type != XPATH_NODESET) &&
6278 (ctxt->value->type != XPATH_XSLT_TREE)))
6279 XP_ERROR(XPATH_INVALID_TYPE);
6280 cur = valuePop(ctxt);
6281
Daniel Veillard911f49a2001-04-07 15:39:35 +00006282 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006283 valuePush(ctxt, xmlXPathNewCString(""));
6284 } else {
6285 int i = 0; /* Should be first in document order !!!!! */
6286 switch (cur->nodesetval->nodeTab[i]->type) {
6287 case XML_ELEMENT_NODE:
6288 case XML_ATTRIBUTE_NODE:
6289 if (cur->nodesetval->nodeTab[i]->ns == NULL)
6290 valuePush(ctxt, xmlXPathNewCString(""));
6291 else
6292 valuePush(ctxt, xmlXPathNewString(
6293 cur->nodesetval->nodeTab[i]->ns->href));
6294 break;
6295 default:
6296 valuePush(ctxt, xmlXPathNewCString(""));
6297 }
6298 }
6299 xmlXPathFreeObject(cur);
6300}
6301
6302/**
6303 * xmlXPathNameFunction:
6304 * @ctxt: the XPath Parser context
6305 * @nargs: the number of arguments
6306 *
6307 * Implement the name() XPath function
6308 * string name(node-set?)
6309 * The name function returns a string containing a QName representing
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006310 * the name of the node in the argument node-set that is first in document
Owen Taylor3473f882001-02-23 17:55:21 +00006311 * order. The QName must represent the name with respect to the namespace
6312 * declarations in effect on the node whose name is being represented.
6313 * Typically, this will be the form in which the name occurred in the XML
6314 * source. This need not be the case if there are namespace declarations
6315 * in effect on the node that associate multiple prefixes with the same
6316 * namespace. However, an implementation may include information about
6317 * the original prefix in its representation of nodes; in this case, an
6318 * implementation can ensure that the returned string is always the same
6319 * as the QName used in the XML source. If the argument it omitted it
6320 * defaults to the context node.
6321 * Libxml keep the original prefix so the "real qualified name" used is
6322 * returned.
6323 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006324static void
Daniel Veillard04383752001-07-08 14:27:15 +00006325xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs)
6326{
Owen Taylor3473f882001-02-23 17:55:21 +00006327 xmlXPathObjectPtr cur;
6328
6329 if (nargs == 0) {
Daniel Veillard04383752001-07-08 14:27:15 +00006330 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
6331 nargs = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006332 }
6333
6334 CHECK_ARITY(1);
Daniel Veillard04383752001-07-08 14:27:15 +00006335 if ((ctxt->value == NULL) ||
6336 ((ctxt->value->type != XPATH_NODESET) &&
6337 (ctxt->value->type != XPATH_XSLT_TREE)))
6338 XP_ERROR(XPATH_INVALID_TYPE);
Owen Taylor3473f882001-02-23 17:55:21 +00006339 cur = valuePop(ctxt);
6340
Daniel Veillard911f49a2001-04-07 15:39:35 +00006341 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Daniel Veillard04383752001-07-08 14:27:15 +00006342 valuePush(ctxt, xmlXPathNewCString(""));
Owen Taylor3473f882001-02-23 17:55:21 +00006343 } else {
Daniel Veillard04383752001-07-08 14:27:15 +00006344 int i = 0; /* Should be first in document order !!!!! */
Owen Taylor3473f882001-02-23 17:55:21 +00006345
Daniel Veillard04383752001-07-08 14:27:15 +00006346 switch (cur->nodesetval->nodeTab[i]->type) {
6347 case XML_ELEMENT_NODE:
6348 case XML_ATTRIBUTE_NODE:
Daniel Veillard652d8a92003-02-04 19:28:49 +00006349 if (cur->nodesetval->nodeTab[i]->name[0] == ' ')
6350 valuePush(ctxt, xmlXPathNewCString(""));
6351 else if ((cur->nodesetval->nodeTab[i]->ns == NULL) ||
6352 (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) {
Daniel Veillard04383752001-07-08 14:27:15 +00006353 valuePush(ctxt,
Daniel Veillardc00cda82003-04-07 10:22:39 +00006354 xmlXPathNewString(cur->nodesetval->nodeTab[i]->name));
Daniel Veillard04383752001-07-08 14:27:15 +00006355
Daniel Veillard652d8a92003-02-04 19:28:49 +00006356 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00006357 xmlChar *fullname;
6358
6359 fullname = xmlBuildQName(cur->nodesetval->nodeTab[i]->name,
6360 cur->nodesetval->nodeTab[i]->ns->prefix,
6361 NULL, 0);
6362 if (fullname == cur->nodesetval->nodeTab[i]->name)
6363 fullname = xmlStrdup(cur->nodesetval->nodeTab[i]->name);
6364 if (fullname == NULL) {
6365 XP_ERROR(XPATH_MEMORY_ERROR);
6366 }
6367 valuePush(ctxt, xmlXPathWrapString(fullname));
Daniel Veillard04383752001-07-08 14:27:15 +00006368 }
6369 break;
6370 default:
6371 valuePush(ctxt,
6372 xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i]));
6373 xmlXPathLocalNameFunction(ctxt, 1);
6374 }
Owen Taylor3473f882001-02-23 17:55:21 +00006375 }
6376 xmlXPathFreeObject(cur);
6377}
6378
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00006379
6380/**
Owen Taylor3473f882001-02-23 17:55:21 +00006381 * xmlXPathStringFunction:
6382 * @ctxt: the XPath Parser context
6383 * @nargs: the number of arguments
6384 *
6385 * Implement the string() XPath function
6386 * string string(object?)
William M. Brack08171912003-12-29 02:52:11 +00006387 * The string function converts an object to a string as follows:
Owen Taylor3473f882001-02-23 17:55:21 +00006388 * - A node-set is converted to a string by returning the value of
6389 * the node in the node-set that is first in document order.
6390 * If the node-set is empty, an empty string is returned.
6391 * - A number is converted to a string as follows
6392 * + NaN is converted to the string NaN
6393 * + positive zero is converted to the string 0
6394 * + negative zero is converted to the string 0
6395 * + positive infinity is converted to the string Infinity
6396 * + negative infinity is converted to the string -Infinity
6397 * + if the number is an integer, the number is represented in
6398 * decimal form as a Number with no decimal point and no leading
6399 * zeros, preceded by a minus sign (-) if the number is negative
6400 * + otherwise, the number is represented in decimal form as a
6401 * Number including a decimal point with at least one digit
6402 * before the decimal point and at least one digit after the
6403 * decimal point, preceded by a minus sign (-) if the number
6404 * is negative; there must be no leading zeros before the decimal
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006405 * point apart possibly from the one required digit immediately
Owen Taylor3473f882001-02-23 17:55:21 +00006406 * before the decimal point; beyond the one required digit
6407 * after the decimal point there must be as many, but only as
6408 * many, more digits as are needed to uniquely distinguish the
6409 * number from all other IEEE 754 numeric values.
6410 * - The boolean false value is converted to the string false.
6411 * The boolean true value is converted to the string true.
6412 *
6413 * If the argument is omitted, it defaults to a node-set with the
6414 * context node as its only member.
6415 */
6416void
6417xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6418 xmlXPathObjectPtr cur;
6419
6420 if (nargs == 0) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006421 valuePush(ctxt,
6422 xmlXPathWrapString(
6423 xmlXPathCastNodeToString(ctxt->context->node)));
6424 return;
Owen Taylor3473f882001-02-23 17:55:21 +00006425 }
6426
6427 CHECK_ARITY(1);
6428 cur = valuePop(ctxt);
6429 if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00006430 cur = xmlXPathConvertString(cur);
6431 valuePush(ctxt, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00006432}
6433
6434/**
6435 * xmlXPathStringLengthFunction:
6436 * @ctxt: the XPath Parser context
6437 * @nargs: the number of arguments
6438 *
6439 * Implement the string-length() XPath function
6440 * number string-length(string?)
6441 * The string-length returns the number of characters in the string
6442 * (see [3.6 Strings]). If the argument is omitted, it defaults to
6443 * the context node converted to a string, in other words the value
6444 * of the context node.
6445 */
6446void
6447xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6448 xmlXPathObjectPtr cur;
6449
6450 if (nargs == 0) {
6451 if (ctxt->context->node == NULL) {
6452 valuePush(ctxt, xmlXPathNewFloat(0));
6453 } else {
6454 xmlChar *content;
6455
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006456 content = xmlXPathCastNodeToString(ctxt->context->node);
Daniel Veillarde043ee12001-04-16 14:08:07 +00006457 valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content)));
Owen Taylor3473f882001-02-23 17:55:21 +00006458 xmlFree(content);
6459 }
6460 return;
6461 }
6462 CHECK_ARITY(1);
6463 CAST_TO_STRING;
6464 CHECK_TYPE(XPATH_STRING);
6465 cur = valuePop(ctxt);
Daniel Veillarde043ee12001-04-16 14:08:07 +00006466 valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(cur->stringval)));
Owen Taylor3473f882001-02-23 17:55:21 +00006467 xmlXPathFreeObject(cur);
6468}
6469
6470/**
6471 * xmlXPathConcatFunction:
6472 * @ctxt: the XPath Parser context
6473 * @nargs: the number of arguments
6474 *
6475 * Implement the concat() XPath function
6476 * string concat(string, string, string*)
6477 * The concat function returns the concatenation of its arguments.
6478 */
6479void
6480xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6481 xmlXPathObjectPtr cur, newobj;
6482 xmlChar *tmp;
6483
6484 if (nargs < 2) {
6485 CHECK_ARITY(2);
6486 }
6487
6488 CAST_TO_STRING;
6489 cur = valuePop(ctxt);
6490 if ((cur == NULL) || (cur->type != XPATH_STRING)) {
6491 xmlXPathFreeObject(cur);
6492 return;
6493 }
6494 nargs--;
6495
6496 while (nargs > 0) {
6497 CAST_TO_STRING;
6498 newobj = valuePop(ctxt);
6499 if ((newobj == NULL) || (newobj->type != XPATH_STRING)) {
6500 xmlXPathFreeObject(newobj);
6501 xmlXPathFreeObject(cur);
6502 XP_ERROR(XPATH_INVALID_TYPE);
6503 }
6504 tmp = xmlStrcat(newobj->stringval, cur->stringval);
6505 newobj->stringval = cur->stringval;
6506 cur->stringval = tmp;
6507
6508 xmlXPathFreeObject(newobj);
6509 nargs--;
6510 }
6511 valuePush(ctxt, cur);
6512}
6513
6514/**
6515 * xmlXPathContainsFunction:
6516 * @ctxt: the XPath Parser context
6517 * @nargs: the number of arguments
6518 *
6519 * Implement the contains() XPath function
6520 * boolean contains(string, string)
6521 * The contains function returns true if the first argument string
6522 * contains the second argument string, and otherwise returns false.
6523 */
6524void
6525xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6526 xmlXPathObjectPtr hay, needle;
6527
6528 CHECK_ARITY(2);
6529 CAST_TO_STRING;
6530 CHECK_TYPE(XPATH_STRING);
6531 needle = valuePop(ctxt);
6532 CAST_TO_STRING;
6533 hay = valuePop(ctxt);
6534 if ((hay == NULL) || (hay->type != XPATH_STRING)) {
6535 xmlXPathFreeObject(hay);
6536 xmlXPathFreeObject(needle);
6537 XP_ERROR(XPATH_INVALID_TYPE);
6538 }
6539 if (xmlStrstr(hay->stringval, needle->stringval))
6540 valuePush(ctxt, xmlXPathNewBoolean(1));
6541 else
6542 valuePush(ctxt, xmlXPathNewBoolean(0));
6543 xmlXPathFreeObject(hay);
6544 xmlXPathFreeObject(needle);
6545}
6546
6547/**
6548 * xmlXPathStartsWithFunction:
6549 * @ctxt: the XPath Parser context
6550 * @nargs: the number of arguments
6551 *
6552 * Implement the starts-with() XPath function
6553 * boolean starts-with(string, string)
6554 * The starts-with function returns true if the first argument string
6555 * starts with the second argument string, and otherwise returns false.
6556 */
6557void
6558xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6559 xmlXPathObjectPtr hay, needle;
6560 int n;
6561
6562 CHECK_ARITY(2);
6563 CAST_TO_STRING;
6564 CHECK_TYPE(XPATH_STRING);
6565 needle = valuePop(ctxt);
6566 CAST_TO_STRING;
6567 hay = valuePop(ctxt);
6568 if ((hay == NULL) || (hay->type != XPATH_STRING)) {
6569 xmlXPathFreeObject(hay);
6570 xmlXPathFreeObject(needle);
6571 XP_ERROR(XPATH_INVALID_TYPE);
6572 }
6573 n = xmlStrlen(needle->stringval);
6574 if (xmlStrncmp(hay->stringval, needle->stringval, n))
6575 valuePush(ctxt, xmlXPathNewBoolean(0));
6576 else
6577 valuePush(ctxt, xmlXPathNewBoolean(1));
6578 xmlXPathFreeObject(hay);
6579 xmlXPathFreeObject(needle);
6580}
6581
6582/**
6583 * xmlXPathSubstringFunction:
6584 * @ctxt: the XPath Parser context
6585 * @nargs: the number of arguments
6586 *
6587 * Implement the substring() XPath function
6588 * string substring(string, number, number?)
6589 * The substring function returns the substring of the first argument
6590 * starting at the position specified in the second argument with
6591 * length specified in the third argument. For example,
6592 * substring("12345",2,3) returns "234". If the third argument is not
6593 * specified, it returns the substring starting at the position specified
6594 * in the second argument and continuing to the end of the string. For
6595 * example, substring("12345",2) returns "2345". More precisely, each
6596 * character in the string (see [3.6 Strings]) is considered to have a
6597 * numeric position: the position of the first character is 1, the position
6598 * of the second character is 2 and so on. The returned substring contains
6599 * those characters for which the position of the character is greater than
6600 * or equal to the second argument and, if the third argument is specified,
6601 * less than the sum of the second and third arguments; the comparisons
6602 * and addition used for the above follow the standard IEEE 754 rules. Thus:
6603 * - substring("12345", 1.5, 2.6) returns "234"
6604 * - substring("12345", 0, 3) returns "12"
6605 * - substring("12345", 0 div 0, 3) returns ""
6606 * - substring("12345", 1, 0 div 0) returns ""
6607 * - substring("12345", -42, 1 div 0) returns "12345"
6608 * - substring("12345", -1 div 0, 1 div 0) returns ""
6609 */
6610void
6611xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6612 xmlXPathObjectPtr str, start, len;
Daniel Veillard97ac1312001-05-30 19:14:17 +00006613 double le=0, in;
6614 int i, l, m;
Owen Taylor3473f882001-02-23 17:55:21 +00006615 xmlChar *ret;
6616
Owen Taylor3473f882001-02-23 17:55:21 +00006617 if (nargs < 2) {
6618 CHECK_ARITY(2);
6619 }
6620 if (nargs > 3) {
6621 CHECK_ARITY(3);
6622 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006623 /*
6624 * take care of possible last (position) argument
6625 */
Owen Taylor3473f882001-02-23 17:55:21 +00006626 if (nargs == 3) {
6627 CAST_TO_NUMBER;
6628 CHECK_TYPE(XPATH_NUMBER);
6629 len = valuePop(ctxt);
6630 le = len->floatval;
6631 xmlXPathFreeObject(len);
Owen Taylor3473f882001-02-23 17:55:21 +00006632 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006633
Owen Taylor3473f882001-02-23 17:55:21 +00006634 CAST_TO_NUMBER;
6635 CHECK_TYPE(XPATH_NUMBER);
6636 start = valuePop(ctxt);
6637 in = start->floatval;
6638 xmlXPathFreeObject(start);
6639 CAST_TO_STRING;
6640 CHECK_TYPE(XPATH_STRING);
6641 str = valuePop(ctxt);
Daniel Veillard97ac1312001-05-30 19:14:17 +00006642 m = xmlUTF8Strlen((const unsigned char *)str->stringval);
Owen Taylor3473f882001-02-23 17:55:21 +00006643
Daniel Veillard97ac1312001-05-30 19:14:17 +00006644 /*
6645 * If last pos not present, calculate last position
6646 */
Daniel Veillard9e412302002-06-10 15:59:44 +00006647 if (nargs != 3) {
6648 le = (double)m;
6649 if (in < 1.0)
6650 in = 1.0;
6651 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006652
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006653 /* Need to check for the special cases where either
6654 * the index is NaN, the length is NaN, or both
6655 * arguments are infinity (relying on Inf + -Inf = NaN)
Daniel Veillard97ac1312001-05-30 19:14:17 +00006656 */
Daniel Veillard9e412302002-06-10 15:59:44 +00006657 if (!xmlXPathIsNaN(in + le) && !xmlXPathIsInf(in)) {
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006658 /*
Daniel Veillard9e412302002-06-10 15:59:44 +00006659 * To meet the requirements of the spec, the arguments
6660 * must be converted to integer format before
6661 * initial index calculations are done
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006662 *
Daniel Veillard9e412302002-06-10 15:59:44 +00006663 * First we go to integer form, rounding up
6664 * and checking for special cases
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006665 */
6666 i = (int) in;
Daniel Veillard9e412302002-06-10 15:59:44 +00006667 if (((double)i)+0.5 <= in) i++;
Owen Taylor3473f882001-02-23 17:55:21 +00006668
Daniel Veillard9e412302002-06-10 15:59:44 +00006669 if (xmlXPathIsInf(le) == 1) {
6670 l = m;
6671 if (i < 1)
6672 i = 1;
6673 }
6674 else if (xmlXPathIsInf(le) == -1 || le < 0.0)
6675 l = 0;
6676 else {
6677 l = (int) le;
6678 if (((double)l)+0.5 <= le) l++;
6679 }
6680
6681 /* Now we normalize inidices */
6682 i -= 1;
6683 l += i;
6684 if (i < 0)
6685 i = 0;
6686 if (l > m)
6687 l = m;
Owen Taylor3473f882001-02-23 17:55:21 +00006688
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006689 /* number of chars to copy */
6690 l -= i;
Owen Taylor3473f882001-02-23 17:55:21 +00006691
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006692 ret = xmlUTF8Strsub(str->stringval, i, l);
6693 }
6694 else {
6695 ret = NULL;
6696 }
6697
Owen Taylor3473f882001-02-23 17:55:21 +00006698 if (ret == NULL)
6699 valuePush(ctxt, xmlXPathNewCString(""));
6700 else {
6701 valuePush(ctxt, xmlXPathNewString(ret));
6702 xmlFree(ret);
6703 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006704
Owen Taylor3473f882001-02-23 17:55:21 +00006705 xmlXPathFreeObject(str);
6706}
6707
6708/**
6709 * xmlXPathSubstringBeforeFunction:
6710 * @ctxt: the XPath Parser context
6711 * @nargs: the number of arguments
6712 *
6713 * Implement the substring-before() XPath function
6714 * string substring-before(string, string)
6715 * The substring-before function returns the substring of the first
6716 * argument string that precedes the first occurrence of the second
6717 * argument string in the first argument string, or the empty string
6718 * if the first argument string does not contain the second argument
6719 * string. For example, substring-before("1999/04/01","/") returns 1999.
6720 */
6721void
6722xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6723 xmlXPathObjectPtr str;
6724 xmlXPathObjectPtr find;
6725 xmlBufferPtr target;
6726 const xmlChar *point;
6727 int offset;
6728
6729 CHECK_ARITY(2);
6730 CAST_TO_STRING;
6731 find = valuePop(ctxt);
6732 CAST_TO_STRING;
6733 str = valuePop(ctxt);
6734
6735 target = xmlBufferCreate();
6736 if (target) {
6737 point = xmlStrstr(str->stringval, find->stringval);
6738 if (point) {
6739 offset = (int)(point - str->stringval);
6740 xmlBufferAdd(target, str->stringval, offset);
6741 }
6742 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6743 xmlBufferFree(target);
6744 }
6745
6746 xmlXPathFreeObject(str);
6747 xmlXPathFreeObject(find);
6748}
6749
6750/**
6751 * xmlXPathSubstringAfterFunction:
6752 * @ctxt: the XPath Parser context
6753 * @nargs: the number of arguments
6754 *
6755 * Implement the substring-after() XPath function
6756 * string substring-after(string, string)
6757 * The substring-after function returns the substring of the first
6758 * argument string that follows the first occurrence of the second
6759 * argument string in the first argument string, or the empty stringi
6760 * if the first argument string does not contain the second argument
6761 * string. For example, substring-after("1999/04/01","/") returns 04/01,
6762 * and substring-after("1999/04/01","19") returns 99/04/01.
6763 */
6764void
6765xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6766 xmlXPathObjectPtr str;
6767 xmlXPathObjectPtr find;
6768 xmlBufferPtr target;
6769 const xmlChar *point;
6770 int offset;
6771
6772 CHECK_ARITY(2);
6773 CAST_TO_STRING;
6774 find = valuePop(ctxt);
6775 CAST_TO_STRING;
6776 str = valuePop(ctxt);
6777
6778 target = xmlBufferCreate();
6779 if (target) {
6780 point = xmlStrstr(str->stringval, find->stringval);
6781 if (point) {
6782 offset = (int)(point - str->stringval) + xmlStrlen(find->stringval);
6783 xmlBufferAdd(target, &str->stringval[offset],
6784 xmlStrlen(str->stringval) - offset);
6785 }
6786 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6787 xmlBufferFree(target);
6788 }
6789
6790 xmlXPathFreeObject(str);
6791 xmlXPathFreeObject(find);
6792}
6793
6794/**
6795 * xmlXPathNormalizeFunction:
6796 * @ctxt: the XPath Parser context
6797 * @nargs: the number of arguments
6798 *
6799 * Implement the normalize-space() XPath function
6800 * string normalize-space(string?)
6801 * The normalize-space function returns the argument string with white
6802 * space normalized by stripping leading and trailing whitespace
6803 * and replacing sequences of whitespace characters by a single
6804 * space. Whitespace characters are the same allowed by the S production
6805 * in XML. If the argument is omitted, it defaults to the context
6806 * node converted to a string, in other words the value of the context node.
6807 */
6808void
6809xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6810 xmlXPathObjectPtr obj = NULL;
6811 xmlChar *source = NULL;
6812 xmlBufferPtr target;
6813 xmlChar blank;
6814
6815 if (nargs == 0) {
6816 /* Use current context node */
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006817 valuePush(ctxt,
6818 xmlXPathWrapString(
6819 xmlXPathCastNodeToString(ctxt->context->node)));
Owen Taylor3473f882001-02-23 17:55:21 +00006820 nargs = 1;
6821 }
6822
6823 CHECK_ARITY(1);
6824 CAST_TO_STRING;
6825 CHECK_TYPE(XPATH_STRING);
6826 obj = valuePop(ctxt);
6827 source = obj->stringval;
6828
6829 target = xmlBufferCreate();
6830 if (target && source) {
6831
6832 /* Skip leading whitespaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006833 while (IS_BLANK_CH(*source))
Owen Taylor3473f882001-02-23 17:55:21 +00006834 source++;
6835
6836 /* Collapse intermediate whitespaces, and skip trailing whitespaces */
6837 blank = 0;
6838 while (*source) {
William M. Brack76e95df2003-10-18 16:20:14 +00006839 if (IS_BLANK_CH(*source)) {
Daniel Veillard97ac1312001-05-30 19:14:17 +00006840 blank = 0x20;
Owen Taylor3473f882001-02-23 17:55:21 +00006841 } else {
6842 if (blank) {
6843 xmlBufferAdd(target, &blank, 1);
6844 blank = 0;
6845 }
6846 xmlBufferAdd(target, source, 1);
6847 }
6848 source++;
6849 }
6850
6851 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6852 xmlBufferFree(target);
6853 }
6854 xmlXPathFreeObject(obj);
6855}
6856
6857/**
6858 * xmlXPathTranslateFunction:
6859 * @ctxt: the XPath Parser context
6860 * @nargs: the number of arguments
6861 *
6862 * Implement the translate() XPath function
6863 * string translate(string, string, string)
6864 * The translate function returns the first argument string with
6865 * occurrences of characters in the second argument string replaced
6866 * by the character at the corresponding position in the third argument
6867 * string. For example, translate("bar","abc","ABC") returns the string
6868 * BAr. If there is a character in the second argument string with no
6869 * character at a corresponding position in the third argument string
6870 * (because the second argument string is longer than the third argument
6871 * string), then occurrences of that character in the first argument
6872 * string are removed. For example, translate("--aaa--","abc-","ABC")
6873 * returns "AAA". If a character occurs more than once in second
6874 * argument string, then the first occurrence determines the replacement
6875 * character. If the third argument string is longer than the second
6876 * argument string, then excess characters are ignored.
6877 */
6878void
6879xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillarde043ee12001-04-16 14:08:07 +00006880 xmlXPathObjectPtr str;
6881 xmlXPathObjectPtr from;
6882 xmlXPathObjectPtr to;
6883 xmlBufferPtr target;
Daniel Veillard97ac1312001-05-30 19:14:17 +00006884 int offset, max;
Daniel Veillarde043ee12001-04-16 14:08:07 +00006885 xmlChar ch;
Daniel Veillard97ac1312001-05-30 19:14:17 +00006886 xmlChar *point;
6887 xmlChar *cptr;
Owen Taylor3473f882001-02-23 17:55:21 +00006888
Daniel Veillarde043ee12001-04-16 14:08:07 +00006889 CHECK_ARITY(3);
Owen Taylor3473f882001-02-23 17:55:21 +00006890
Daniel Veillarde043ee12001-04-16 14:08:07 +00006891 CAST_TO_STRING;
6892 to = valuePop(ctxt);
6893 CAST_TO_STRING;
6894 from = valuePop(ctxt);
6895 CAST_TO_STRING;
6896 str = valuePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006897
Daniel Veillarde043ee12001-04-16 14:08:07 +00006898 target = xmlBufferCreate();
6899 if (target) {
Daniel Veillard97ac1312001-05-30 19:14:17 +00006900 max = xmlUTF8Strlen(to->stringval);
6901 for (cptr = str->stringval; (ch=*cptr); ) {
6902 offset = xmlUTF8Strloc(from->stringval, cptr);
6903 if (offset >= 0) {
6904 if (offset < max) {
6905 point = xmlUTF8Strpos(to->stringval, offset);
6906 if (point)
6907 xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1));
6908 }
6909 } else
6910 xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1));
6911
6912 /* Step to next character in input */
6913 cptr++;
6914 if ( ch & 0x80 ) {
6915 /* if not simple ascii, verify proper format */
6916 if ( (ch & 0xc0) != 0xc0 ) {
6917 xmlGenericError(xmlGenericErrorContext,
6918 "xmlXPathTranslateFunction: Invalid UTF8 string\n");
6919 break;
6920 }
6921 /* then skip over remaining bytes for this char */
6922 while ( (ch <<= 1) & 0x80 )
6923 if ( (*cptr++ & 0xc0) != 0x80 ) {
6924 xmlGenericError(xmlGenericErrorContext,
6925 "xmlXPathTranslateFunction: Invalid UTF8 string\n");
6926 break;
6927 }
6928 if (ch & 0x80) /* must have had error encountered */
6929 break;
6930 }
Daniel Veillarde043ee12001-04-16 14:08:07 +00006931 }
Owen Taylor3473f882001-02-23 17:55:21 +00006932 }
Daniel Veillarde043ee12001-04-16 14:08:07 +00006933 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6934 xmlBufferFree(target);
6935 xmlXPathFreeObject(str);
6936 xmlXPathFreeObject(from);
6937 xmlXPathFreeObject(to);
Owen Taylor3473f882001-02-23 17:55:21 +00006938}
6939
6940/**
6941 * xmlXPathBooleanFunction:
6942 * @ctxt: the XPath Parser context
6943 * @nargs: the number of arguments
6944 *
6945 * Implement the boolean() XPath function
6946 * boolean boolean(object)
William M. Brack08171912003-12-29 02:52:11 +00006947 * The boolean function converts its argument to a boolean as follows:
Owen Taylor3473f882001-02-23 17:55:21 +00006948 * - a number is true if and only if it is neither positive or
6949 * negative zero nor NaN
6950 * - a node-set is true if and only if it is non-empty
6951 * - a string is true if and only if its length is non-zero
6952 */
6953void
6954xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6955 xmlXPathObjectPtr cur;
Owen Taylor3473f882001-02-23 17:55:21 +00006956
6957 CHECK_ARITY(1);
6958 cur = valuePop(ctxt);
6959 if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00006960 cur = xmlXPathConvertBoolean(cur);
6961 valuePush(ctxt, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00006962}
6963
6964/**
6965 * xmlXPathNotFunction:
6966 * @ctxt: the XPath Parser context
6967 * @nargs: the number of arguments
6968 *
6969 * Implement the not() XPath function
6970 * boolean not(boolean)
6971 * The not function returns true if its argument is false,
6972 * and false otherwise.
6973 */
6974void
6975xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6976 CHECK_ARITY(1);
6977 CAST_TO_BOOLEAN;
6978 CHECK_TYPE(XPATH_BOOLEAN);
6979 ctxt->value->boolval = ! ctxt->value->boolval;
6980}
6981
6982/**
6983 * xmlXPathTrueFunction:
6984 * @ctxt: the XPath Parser context
6985 * @nargs: the number of arguments
6986 *
6987 * Implement the true() XPath function
6988 * boolean true()
6989 */
6990void
6991xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6992 CHECK_ARITY(0);
6993 valuePush(ctxt, xmlXPathNewBoolean(1));
6994}
6995
6996/**
6997 * xmlXPathFalseFunction:
6998 * @ctxt: the XPath Parser context
6999 * @nargs: the number of arguments
7000 *
7001 * Implement the false() XPath function
7002 * boolean false()
7003 */
7004void
7005xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7006 CHECK_ARITY(0);
7007 valuePush(ctxt, xmlXPathNewBoolean(0));
7008}
7009
7010/**
7011 * xmlXPathLangFunction:
7012 * @ctxt: the XPath Parser context
7013 * @nargs: the number of arguments
7014 *
7015 * Implement the lang() XPath function
7016 * boolean lang(string)
7017 * The lang function returns true or false depending on whether the
7018 * language of the context node as specified by xml:lang attributes
7019 * is the same as or is a sublanguage of the language specified by
7020 * the argument string. The language of the context node is determined
7021 * by the value of the xml:lang attribute on the context node, or, if
7022 * the context node has no xml:lang attribute, by the value of the
7023 * xml:lang attribute on the nearest ancestor of the context node that
7024 * has an xml:lang attribute. If there is no such attribute, then lang
7025 * returns false. If there is such an attribute, then lang returns
7026 * true if the attribute value is equal to the argument ignoring case,
7027 * or if there is some suffix starting with - such that the attribute
7028 * value is equal to the argument ignoring that suffix of the attribute
7029 * value and ignoring case.
7030 */
7031void
7032xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7033 xmlXPathObjectPtr val;
7034 const xmlChar *theLang;
7035 const xmlChar *lang;
7036 int ret = 0;
7037 int i;
7038
7039 CHECK_ARITY(1);
7040 CAST_TO_STRING;
7041 CHECK_TYPE(XPATH_STRING);
7042 val = valuePop(ctxt);
7043 lang = val->stringval;
7044 theLang = xmlNodeGetLang(ctxt->context->node);
7045 if ((theLang != NULL) && (lang != NULL)) {
7046 for (i = 0;lang[i] != 0;i++)
7047 if (toupper(lang[i]) != toupper(theLang[i]))
7048 goto not_equal;
7049 ret = 1;
7050 }
7051not_equal:
William M. Bracka59ddb52004-02-25 08:12:32 +00007052 xmlFree((void *)theLang);
Owen Taylor3473f882001-02-23 17:55:21 +00007053 xmlXPathFreeObject(val);
7054 valuePush(ctxt, xmlXPathNewBoolean(ret));
7055}
7056
7057/**
7058 * xmlXPathNumberFunction:
7059 * @ctxt: the XPath Parser context
7060 * @nargs: the number of arguments
7061 *
7062 * Implement the number() XPath function
7063 * number number(object?)
7064 */
7065void
7066xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7067 xmlXPathObjectPtr cur;
7068 double res;
7069
7070 if (nargs == 0) {
7071 if (ctxt->context->node == NULL) {
7072 valuePush(ctxt, xmlXPathNewFloat(0.0));
7073 } else {
7074 xmlChar* content = xmlNodeGetContent(ctxt->context->node);
7075
7076 res = xmlXPathStringEvalNumber(content);
7077 valuePush(ctxt, xmlXPathNewFloat(res));
7078 xmlFree(content);
7079 }
7080 return;
7081 }
7082
7083 CHECK_ARITY(1);
7084 cur = valuePop(ctxt);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007085 cur = xmlXPathConvertNumber(cur);
7086 valuePush(ctxt, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00007087}
7088
7089/**
7090 * xmlXPathSumFunction:
7091 * @ctxt: the XPath Parser context
7092 * @nargs: the number of arguments
7093 *
7094 * Implement the sum() XPath function
7095 * number sum(node-set)
7096 * The sum function returns the sum of the values of the nodes in
7097 * the argument node-set.
7098 */
7099void
7100xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7101 xmlXPathObjectPtr cur;
7102 int i;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00007103 double res = 0.0;
Owen Taylor3473f882001-02-23 17:55:21 +00007104
7105 CHECK_ARITY(1);
7106 if ((ctxt->value == NULL) ||
7107 ((ctxt->value->type != XPATH_NODESET) &&
7108 (ctxt->value->type != XPATH_XSLT_TREE)))
7109 XP_ERROR(XPATH_INVALID_TYPE);
7110 cur = valuePop(ctxt);
7111
William M. Brack08171912003-12-29 02:52:11 +00007112 if ((cur->nodesetval != NULL) && (cur->nodesetval->nodeNr != 0)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00007113 for (i = 0; i < cur->nodesetval->nodeNr; i++) {
7114 res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00007115 }
7116 }
William M. Brack08171912003-12-29 02:52:11 +00007117 valuePush(ctxt, xmlXPathNewFloat(res));
Owen Taylor3473f882001-02-23 17:55:21 +00007118 xmlXPathFreeObject(cur);
7119}
7120
7121/**
7122 * xmlXPathFloorFunction:
7123 * @ctxt: the XPath Parser context
7124 * @nargs: the number of arguments
7125 *
7126 * Implement the floor() XPath function
7127 * number floor(number)
7128 * The floor function returns the largest (closest to positive infinity)
7129 * number that is not greater than the argument and that is an integer.
7130 */
7131void
7132xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007133 double f;
7134
Owen Taylor3473f882001-02-23 17:55:21 +00007135 CHECK_ARITY(1);
7136 CAST_TO_NUMBER;
7137 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007138
7139 f = (double)((int) ctxt->value->floatval);
7140 if (f != ctxt->value->floatval) {
7141 if (ctxt->value->floatval > 0)
7142 ctxt->value->floatval = f;
7143 else
7144 ctxt->value->floatval = f - 1;
7145 }
Owen Taylor3473f882001-02-23 17:55:21 +00007146}
7147
7148/**
7149 * xmlXPathCeilingFunction:
7150 * @ctxt: the XPath Parser context
7151 * @nargs: the number of arguments
7152 *
7153 * Implement the ceiling() XPath function
7154 * number ceiling(number)
7155 * The ceiling function returns the smallest (closest to negative infinity)
7156 * number that is not less than the argument and that is an integer.
7157 */
7158void
7159xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7160 double f;
7161
7162 CHECK_ARITY(1);
7163 CAST_TO_NUMBER;
7164 CHECK_TYPE(XPATH_NUMBER);
7165
7166#if 0
7167 ctxt->value->floatval = ceil(ctxt->value->floatval);
7168#else
7169 f = (double)((int) ctxt->value->floatval);
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007170 if (f != ctxt->value->floatval) {
7171 if (ctxt->value->floatval > 0)
7172 ctxt->value->floatval = f + 1;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00007173 else {
7174 if (ctxt->value->floatval < 0 && f == 0)
7175 ctxt->value->floatval = xmlXPathNZERO;
7176 else
7177 ctxt->value->floatval = f;
7178 }
7179
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007180 }
Owen Taylor3473f882001-02-23 17:55:21 +00007181#endif
7182}
7183
7184/**
7185 * xmlXPathRoundFunction:
7186 * @ctxt: the XPath Parser context
7187 * @nargs: the number of arguments
7188 *
7189 * Implement the round() XPath function
7190 * number round(number)
7191 * The round function returns the number that is closest to the
7192 * argument and that is an integer. If there are two such numbers,
7193 * then the one that is even is returned.
7194 */
7195void
7196xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7197 double f;
7198
7199 CHECK_ARITY(1);
7200 CAST_TO_NUMBER;
7201 CHECK_TYPE(XPATH_NUMBER);
7202
Daniel Veillardcda96922001-08-21 10:56:31 +00007203 if ((xmlXPathIsNaN(ctxt->value->floatval)) ||
7204 (xmlXPathIsInf(ctxt->value->floatval) == 1) ||
7205 (xmlXPathIsInf(ctxt->value->floatval) == -1) ||
Owen Taylor3473f882001-02-23 17:55:21 +00007206 (ctxt->value->floatval == 0.0))
7207 return;
7208
Owen Taylor3473f882001-02-23 17:55:21 +00007209 f = (double)((int) ctxt->value->floatval);
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007210 if (ctxt->value->floatval < 0) {
7211 if (ctxt->value->floatval < f - 0.5)
7212 ctxt->value->floatval = f - 1;
7213 else
7214 ctxt->value->floatval = f;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00007215 if (ctxt->value->floatval == 0)
7216 ctxt->value->floatval = xmlXPathNZERO;
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007217 } else {
7218 if (ctxt->value->floatval < f + 0.5)
7219 ctxt->value->floatval = f;
7220 else
7221 ctxt->value->floatval = f + 1;
7222 }
Owen Taylor3473f882001-02-23 17:55:21 +00007223}
7224
7225/************************************************************************
7226 * *
7227 * The Parser *
7228 * *
7229 ************************************************************************/
7230
7231/*
William M. Brack08171912003-12-29 02:52:11 +00007232 * a few forward declarations since we use a recursive call based
Owen Taylor3473f882001-02-23 17:55:21 +00007233 * implementation.
7234 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007235static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00007236static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007237static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007238static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt);
Daniel Veillard2156a562001-04-28 12:24:34 +00007239static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt,
7240 int qualified);
Owen Taylor3473f882001-02-23 17:55:21 +00007241
7242/**
Daniel Veillard61d80a22001-04-27 17:13:01 +00007243 * xmlXPathCurrentChar:
7244 * @ctxt: the XPath parser context
7245 * @cur: pointer to the beginning of the char
7246 * @len: pointer to the length of the char read
7247 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00007248 * The current char value, if using UTF-8 this may actually span multiple
Daniel Veillard61d80a22001-04-27 17:13:01 +00007249 * bytes in the input buffer.
7250 *
Daniel Veillard60087f32001-10-10 09:45:09 +00007251 * Returns the current char value and its length
Daniel Veillard61d80a22001-04-27 17:13:01 +00007252 */
7253
7254static int
7255xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) {
7256 unsigned char c;
7257 unsigned int val;
7258 const xmlChar *cur;
7259
7260 if (ctxt == NULL)
7261 return(0);
7262 cur = ctxt->cur;
7263
7264 /*
7265 * We are supposed to handle UTF8, check it's valid
7266 * From rfc2044: encoding of the Unicode values on UTF-8:
7267 *
7268 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
7269 * 0000 0000-0000 007F 0xxxxxxx
7270 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
7271 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
7272 *
7273 * Check for the 0x110000 limit too
7274 */
7275 c = *cur;
7276 if (c & 0x80) {
7277 if ((cur[1] & 0xc0) != 0x80)
7278 goto encoding_error;
7279 if ((c & 0xe0) == 0xe0) {
7280
7281 if ((cur[2] & 0xc0) != 0x80)
7282 goto encoding_error;
7283 if ((c & 0xf0) == 0xf0) {
7284 if (((c & 0xf8) != 0xf0) ||
7285 ((cur[3] & 0xc0) != 0x80))
7286 goto encoding_error;
7287 /* 4-byte code */
7288 *len = 4;
7289 val = (cur[0] & 0x7) << 18;
7290 val |= (cur[1] & 0x3f) << 12;
7291 val |= (cur[2] & 0x3f) << 6;
7292 val |= cur[3] & 0x3f;
7293 } else {
7294 /* 3-byte code */
7295 *len = 3;
7296 val = (cur[0] & 0xf) << 12;
7297 val |= (cur[1] & 0x3f) << 6;
7298 val |= cur[2] & 0x3f;
7299 }
7300 } else {
7301 /* 2-byte code */
7302 *len = 2;
7303 val = (cur[0] & 0x1f) << 6;
7304 val |= cur[1] & 0x3f;
7305 }
7306 if (!IS_CHAR(val)) {
7307 XP_ERROR0(XPATH_INVALID_CHAR_ERROR);
7308 }
7309 return(val);
7310 } else {
7311 /* 1-byte code */
7312 *len = 1;
7313 return((int) *cur);
7314 }
7315encoding_error:
7316 /*
William M. Brack08171912003-12-29 02:52:11 +00007317 * If we detect an UTF8 error that probably means that the
7318 * input encoding didn't get properly advertised in the
Daniel Veillard61d80a22001-04-27 17:13:01 +00007319 * declaration header. Report the error and switch the encoding
7320 * to ISO-Latin-1 (if you don't like this policy, just declare the
7321 * encoding !)
7322 */
Daniel Veillard42596ad2001-05-22 16:57:14 +00007323 *len = 0;
Daniel Veillard61d80a22001-04-27 17:13:01 +00007324 XP_ERROR0(XPATH_ENCODING_ERROR);
Daniel Veillard61d80a22001-04-27 17:13:01 +00007325}
7326
7327/**
Owen Taylor3473f882001-02-23 17:55:21 +00007328 * xmlXPathParseNCName:
7329 * @ctxt: the XPath Parser context
7330 *
7331 * parse an XML namespace non qualified name.
7332 *
7333 * [NS 3] NCName ::= (Letter | '_') (NCNameChar)*
7334 *
7335 * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
7336 * CombiningChar | Extender
7337 *
7338 * Returns the namespace name or NULL
7339 */
7340
7341xmlChar *
7342xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) {
Daniel Veillard2156a562001-04-28 12:24:34 +00007343 const xmlChar *in;
7344 xmlChar *ret;
7345 int count = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007346
Daniel Veillard2156a562001-04-28 12:24:34 +00007347 /*
7348 * Accelerator for simple ASCII names
7349 */
7350 in = ctxt->cur;
7351 if (((*in >= 0x61) && (*in <= 0x7A)) ||
7352 ((*in >= 0x41) && (*in <= 0x5A)) ||
7353 (*in == '_')) {
7354 in++;
7355 while (((*in >= 0x61) && (*in <= 0x7A)) ||
7356 ((*in >= 0x41) && (*in <= 0x5A)) ||
7357 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard9a89a8a2001-06-27 11:13:35 +00007358 (*in == '_') || (*in == '.') ||
7359 (*in == '-'))
Daniel Veillard2156a562001-04-28 12:24:34 +00007360 in++;
7361 if ((*in == ' ') || (*in == '>') || (*in == '/') ||
7362 (*in == '[') || (*in == ']') || (*in == ':') ||
7363 (*in == '@') || (*in == '*')) {
7364 count = in - ctxt->cur;
7365 if (count == 0)
7366 return(NULL);
7367 ret = xmlStrndup(ctxt->cur, count);
7368 ctxt->cur = in;
7369 return(ret);
7370 }
7371 }
7372 return(xmlXPathParseNameComplex(ctxt, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00007373}
7374
Daniel Veillard2156a562001-04-28 12:24:34 +00007375
Owen Taylor3473f882001-02-23 17:55:21 +00007376/**
7377 * xmlXPathParseQName:
7378 * @ctxt: the XPath Parser context
7379 * @prefix: a xmlChar **
7380 *
7381 * parse an XML qualified name
7382 *
7383 * [NS 5] QName ::= (Prefix ':')? LocalPart
7384 *
7385 * [NS 6] Prefix ::= NCName
7386 *
7387 * [NS 7] LocalPart ::= NCName
7388 *
7389 * Returns the function returns the local part, and prefix is updated
7390 * to get the Prefix if any.
7391 */
7392
Daniel Veillard56a4cb82001-03-24 17:00:36 +00007393static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00007394xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) {
7395 xmlChar *ret = NULL;
7396
7397 *prefix = NULL;
7398 ret = xmlXPathParseNCName(ctxt);
7399 if (CUR == ':') {
7400 *prefix = ret;
7401 NEXT;
7402 ret = xmlXPathParseNCName(ctxt);
7403 }
7404 return(ret);
7405}
7406
7407/**
7408 * xmlXPathParseName:
7409 * @ctxt: the XPath Parser context
7410 *
7411 * parse an XML name
7412 *
7413 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
7414 * CombiningChar | Extender
7415 *
7416 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
7417 *
7418 * Returns the namespace name or NULL
7419 */
7420
7421xmlChar *
7422xmlXPathParseName(xmlXPathParserContextPtr ctxt) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00007423 const xmlChar *in;
7424 xmlChar *ret;
7425 int count = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007426
Daniel Veillard61d80a22001-04-27 17:13:01 +00007427 /*
7428 * Accelerator for simple ASCII names
7429 */
7430 in = ctxt->cur;
7431 if (((*in >= 0x61) && (*in <= 0x7A)) ||
7432 ((*in >= 0x41) && (*in <= 0x5A)) ||
7433 (*in == '_') || (*in == ':')) {
7434 in++;
7435 while (((*in >= 0x61) && (*in <= 0x7A)) ||
7436 ((*in >= 0x41) && (*in <= 0x5A)) ||
7437 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard76d66f42001-05-16 21:05:17 +00007438 (*in == '_') || (*in == '-') ||
7439 (*in == ':') || (*in == '.'))
Daniel Veillard61d80a22001-04-27 17:13:01 +00007440 in++;
Daniel Veillard76d66f42001-05-16 21:05:17 +00007441 if ((*in > 0) && (*in < 0x80)) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00007442 count = in - ctxt->cur;
7443 ret = xmlStrndup(ctxt->cur, count);
7444 ctxt->cur = in;
7445 return(ret);
7446 }
7447 }
Daniel Veillard2156a562001-04-28 12:24:34 +00007448 return(xmlXPathParseNameComplex(ctxt, 1));
Owen Taylor3473f882001-02-23 17:55:21 +00007449}
7450
Daniel Veillard61d80a22001-04-27 17:13:01 +00007451static xmlChar *
Daniel Veillard2156a562001-04-28 12:24:34 +00007452xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00007453 xmlChar buf[XML_MAX_NAMELEN + 5];
7454 int len = 0, l;
7455 int c;
7456
7457 /*
7458 * Handler for more complex cases
7459 */
7460 c = CUR_CHAR(l);
7461 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
Daniel Veillard2156a562001-04-28 12:24:34 +00007462 (c == '[') || (c == ']') || (c == '@') || /* accelerators */
7463 (c == '*') || /* accelerators */
Daniel Veillard61d80a22001-04-27 17:13:01 +00007464 (!IS_LETTER(c) && (c != '_') &&
Daniel Veillard2156a562001-04-28 12:24:34 +00007465 ((qualified) && (c != ':')))) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00007466 return(NULL);
7467 }
7468
7469 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
7470 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
7471 (c == '.') || (c == '-') ||
Daniel Veillard2156a562001-04-28 12:24:34 +00007472 (c == '_') || ((qualified) && (c == ':')) ||
Daniel Veillard61d80a22001-04-27 17:13:01 +00007473 (IS_COMBINING(c)) ||
7474 (IS_EXTENDER(c)))) {
7475 COPY_BUF(l,buf,len,c);
7476 NEXTL(l);
7477 c = CUR_CHAR(l);
7478 if (len >= XML_MAX_NAMELEN) {
7479 /*
7480 * Okay someone managed to make a huge name, so he's ready to pay
7481 * for the processing speed.
7482 */
7483 xmlChar *buffer;
7484 int max = len * 2;
7485
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007486 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Daniel Veillard61d80a22001-04-27 17:13:01 +00007487 if (buffer == NULL) {
7488 XP_ERROR0(XPATH_MEMORY_ERROR);
7489 }
7490 memcpy(buffer, buf, len);
7491 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */
7492 (c == '.') || (c == '-') ||
Daniel Veillard2156a562001-04-28 12:24:34 +00007493 (c == '_') || ((qualified) && (c == ':')) ||
Daniel Veillard61d80a22001-04-27 17:13:01 +00007494 (IS_COMBINING(c)) ||
7495 (IS_EXTENDER(c))) {
7496 if (len + 10 > max) {
7497 max *= 2;
7498 buffer = (xmlChar *) xmlRealloc(buffer,
7499 max * sizeof(xmlChar));
Daniel Veillard61d80a22001-04-27 17:13:01 +00007500 if (buffer == NULL) {
7501 XP_ERROR0(XPATH_MEMORY_ERROR);
7502 }
7503 }
7504 COPY_BUF(l,buffer,len,c);
7505 NEXTL(l);
7506 c = CUR_CHAR(l);
7507 }
7508 buffer[len] = 0;
7509 return(buffer);
7510 }
7511 }
Daniel Veillard2156a562001-04-28 12:24:34 +00007512 if (len == 0)
7513 return(NULL);
Daniel Veillard61d80a22001-04-27 17:13:01 +00007514 return(xmlStrndup(buf, len));
7515}
Daniel Veillard3cd72402002-05-13 10:33:30 +00007516
7517#define MAX_FRAC 20
7518
William M. Brack372a4452004-02-17 13:09:23 +00007519/*
7520 * These are used as divisors for the fractional part of a number.
7521 * Since the table includes 1.0 (representing '0' fractional digits),
7522 * it must be dimensioned at MAX_FRAC+1 (bug 133921)
7523 */
7524static double my_pow10[MAX_FRAC+1] = {
Daniel Veillard3cd72402002-05-13 10:33:30 +00007525 1.0, 10.0, 100.0, 1000.0, 10000.0,
7526 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0,
7527 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0,
7528 100000000000000.0,
7529 1000000000000000.0, 10000000000000000.0, 100000000000000000.0,
William M. Brack372a4452004-02-17 13:09:23 +00007530 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0
Daniel Veillard3cd72402002-05-13 10:33:30 +00007531};
7532
Owen Taylor3473f882001-02-23 17:55:21 +00007533/**
7534 * xmlXPathStringEvalNumber:
7535 * @str: A string to scan
7536 *
Bjorn Reese70a9da52001-04-21 16:57:29 +00007537 * [30a] Float ::= Number ('e' Digits?)?
7538 *
Owen Taylor3473f882001-02-23 17:55:21 +00007539 * [30] Number ::= Digits ('.' Digits?)?
7540 * | '.' Digits
7541 * [31] Digits ::= [0-9]+
7542 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007543 * Compile a Number in the string
Owen Taylor3473f882001-02-23 17:55:21 +00007544 * In complement of the Number expression, this function also handles
7545 * negative values : '-' Number.
7546 *
7547 * Returns the double value.
7548 */
7549double
7550xmlXPathStringEvalNumber(const xmlChar *str) {
7551 const xmlChar *cur = str;
Daniel Veillard7b416132002-03-07 08:36:03 +00007552 double ret;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007553 int ok = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007554 int isneg = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007555 int exponent = 0;
7556 int is_exponent_negative = 0;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007557#ifdef __GNUC__
7558 unsigned long tmp = 0;
Daniel Veillard7b416132002-03-07 08:36:03 +00007559 double temp;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007560#endif
Daniel Veillardeca82812002-04-24 11:42:02 +00007561 if (cur == NULL) return(0);
William M. Brack76e95df2003-10-18 16:20:14 +00007562 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00007563 if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) {
7564 return(xmlXPathNAN);
7565 }
7566 if (*cur == '-') {
7567 isneg = 1;
7568 cur++;
7569 }
Daniel Veillardb06c6142001-08-27 14:26:30 +00007570
7571#ifdef __GNUC__
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007572 /*
Daniel Veillard7b416132002-03-07 08:36:03 +00007573 * tmp/temp is a workaround against a gcc compiler bug
7574 * http://veillard.com/gcc.bug
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007575 */
Daniel Veillard7b416132002-03-07 08:36:03 +00007576 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007577 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillard7b416132002-03-07 08:36:03 +00007578 ret = ret * 10;
7579 tmp = (*cur - '0');
Owen Taylor3473f882001-02-23 17:55:21 +00007580 ok = 1;
7581 cur++;
Daniel Veillard7b416132002-03-07 08:36:03 +00007582 temp = (double) tmp;
7583 ret = ret + temp;
Owen Taylor3473f882001-02-23 17:55:21 +00007584 }
Daniel Veillardb06c6142001-08-27 14:26:30 +00007585#else
Daniel Veillard7b416132002-03-07 08:36:03 +00007586 ret = 0;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007587 while ((*cur >= '0') && (*cur <= '9')) {
7588 ret = ret * 10 + (*cur - '0');
7589 ok = 1;
7590 cur++;
7591 }
7592#endif
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007593
Owen Taylor3473f882001-02-23 17:55:21 +00007594 if (*cur == '.') {
Daniel Veillard3cd72402002-05-13 10:33:30 +00007595 int v, frac = 0;
7596 double fraction = 0;
7597
Owen Taylor3473f882001-02-23 17:55:21 +00007598 cur++;
7599 if (((*cur < '0') || (*cur > '9')) && (!ok)) {
7600 return(xmlXPathNAN);
7601 }
Daniel Veillard3cd72402002-05-13 10:33:30 +00007602 while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) {
7603 v = (*cur - '0');
7604 fraction = fraction * 10 + v;
7605 frac = frac + 1;
Owen Taylor3473f882001-02-23 17:55:21 +00007606 cur++;
7607 }
Daniel Veillard3cd72402002-05-13 10:33:30 +00007608 fraction /= my_pow10[frac];
7609 ret = ret + fraction;
7610 while ((*cur >= '0') && (*cur <= '9'))
7611 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00007612 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00007613 if ((*cur == 'e') || (*cur == 'E')) {
7614 cur++;
7615 if (*cur == '-') {
7616 is_exponent_negative = 1;
7617 cur++;
William M. Brack99127052004-05-24 02:52:28 +00007618 } else if (*cur == '+') {
7619 cur++;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007620 }
7621 while ((*cur >= '0') && (*cur <= '9')) {
7622 exponent = exponent * 10 + (*cur - '0');
7623 cur++;
7624 }
7625 }
William M. Brack76e95df2003-10-18 16:20:14 +00007626 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00007627 if (*cur != 0) return(xmlXPathNAN);
7628 if (isneg) ret = -ret;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007629 if (is_exponent_negative) exponent = -exponent;
7630 ret *= pow(10.0, (double)exponent);
Owen Taylor3473f882001-02-23 17:55:21 +00007631 return(ret);
7632}
7633
7634/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007635 * xmlXPathCompNumber:
Owen Taylor3473f882001-02-23 17:55:21 +00007636 * @ctxt: the XPath Parser context
7637 *
7638 * [30] Number ::= Digits ('.' Digits?)?
7639 * | '.' Digits
7640 * [31] Digits ::= [0-9]+
7641 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007642 * Compile a Number, then push it on the stack
Owen Taylor3473f882001-02-23 17:55:21 +00007643 *
7644 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007645static void
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007646xmlXPathCompNumber(xmlXPathParserContextPtr ctxt)
7647{
Owen Taylor3473f882001-02-23 17:55:21 +00007648 double ret = 0.0;
7649 double mult = 1;
Daniel Veillard7b416132002-03-07 08:36:03 +00007650 int ok = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007651 int exponent = 0;
7652 int is_exponent_negative = 0;
Daniel Veillard7b416132002-03-07 08:36:03 +00007653#ifdef __GNUC__
7654 unsigned long tmp = 0;
7655 double temp;
7656#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007657
7658 CHECK_ERROR;
7659 if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) {
7660 XP_ERROR(XPATH_NUMBER_ERROR);
7661 }
Daniel Veillard7b416132002-03-07 08:36:03 +00007662#ifdef __GNUC__
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007663 /*
Daniel Veillard7b416132002-03-07 08:36:03 +00007664 * tmp/temp is a workaround against a gcc compiler bug
7665 * http://veillard.com/gcc.bug
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007666 */
Daniel Veillard7b416132002-03-07 08:36:03 +00007667 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007668 while ((CUR >= '0') && (CUR <= '9')) {
Daniel Veillard7b416132002-03-07 08:36:03 +00007669 ret = ret * 10;
7670 tmp = (CUR - '0');
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007671 ok = 1;
7672 NEXT;
Daniel Veillard7b416132002-03-07 08:36:03 +00007673 temp = (double) tmp;
7674 ret = ret + temp;
Owen Taylor3473f882001-02-23 17:55:21 +00007675 }
Daniel Veillard7b416132002-03-07 08:36:03 +00007676#else
7677 ret = 0;
7678 while ((CUR >= '0') && (CUR <= '9')) {
7679 ret = ret * 10 + (CUR - '0');
7680 ok = 1;
7681 NEXT;
7682 }
7683#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007684 if (CUR == '.') {
7685 NEXT;
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007686 if (((CUR < '0') || (CUR > '9')) && (!ok)) {
7687 XP_ERROR(XPATH_NUMBER_ERROR);
7688 }
7689 while ((CUR >= '0') && (CUR <= '9')) {
7690 mult /= 10;
7691 ret = ret + (CUR - '0') * mult;
7692 NEXT;
7693 }
Owen Taylor3473f882001-02-23 17:55:21 +00007694 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00007695 if ((CUR == 'e') || (CUR == 'E')) {
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007696 NEXT;
7697 if (CUR == '-') {
7698 is_exponent_negative = 1;
7699 NEXT;
William M. Brack99127052004-05-24 02:52:28 +00007700 } else if (CUR == '+') {
7701 NEXT;
7702 }
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007703 while ((CUR >= '0') && (CUR <= '9')) {
7704 exponent = exponent * 10 + (CUR - '0');
7705 NEXT;
7706 }
7707 if (is_exponent_negative)
7708 exponent = -exponent;
7709 ret *= pow(10.0, (double) exponent);
Bjorn Reese70a9da52001-04-21 16:57:29 +00007710 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007711 PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0,
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007712 xmlXPathNewFloat(ret), NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007713}
7714
7715/**
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007716 * xmlXPathParseLiteral:
7717 * @ctxt: the XPath Parser context
7718 *
7719 * Parse a Literal
7720 *
7721 * [29] Literal ::= '"' [^"]* '"'
7722 * | "'" [^']* "'"
7723 *
7724 * Returns the value found or NULL in case of error
7725 */
7726static xmlChar *
7727xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) {
7728 const xmlChar *q;
7729 xmlChar *ret = NULL;
7730
7731 if (CUR == '"') {
7732 NEXT;
7733 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00007734 while ((IS_CHAR_CH(CUR)) && (CUR != '"'))
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007735 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00007736 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007737 XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR);
7738 } else {
7739 ret = xmlStrndup(q, CUR_PTR - q);
7740 NEXT;
7741 }
7742 } else if (CUR == '\'') {
7743 NEXT;
7744 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00007745 while ((IS_CHAR_CH(CUR)) && (CUR != '\''))
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007746 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00007747 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007748 XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR);
7749 } else {
7750 ret = xmlStrndup(q, CUR_PTR - q);
7751 NEXT;
7752 }
7753 } else {
7754 XP_ERROR0(XPATH_START_LITERAL_ERROR);
7755 }
7756 return(ret);
7757}
7758
7759/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007760 * xmlXPathCompLiteral:
Owen Taylor3473f882001-02-23 17:55:21 +00007761 * @ctxt: the XPath Parser context
7762 *
7763 * Parse a Literal and push it on the stack.
7764 *
7765 * [29] Literal ::= '"' [^"]* '"'
7766 * | "'" [^']* "'"
7767 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007768 * TODO: xmlXPathCompLiteral memory allocation could be improved.
Owen Taylor3473f882001-02-23 17:55:21 +00007769 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007770static void
7771xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007772 const xmlChar *q;
7773 xmlChar *ret = NULL;
7774
7775 if (CUR == '"') {
7776 NEXT;
7777 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00007778 while ((IS_CHAR_CH(CUR)) && (CUR != '"'))
Owen Taylor3473f882001-02-23 17:55:21 +00007779 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00007780 if (!IS_CHAR_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007781 XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR);
7782 } else {
7783 ret = xmlStrndup(q, CUR_PTR - q);
7784 NEXT;
7785 }
7786 } else if (CUR == '\'') {
7787 NEXT;
7788 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00007789 while ((IS_CHAR_CH(CUR)) && (CUR != '\''))
Owen Taylor3473f882001-02-23 17:55:21 +00007790 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00007791 if (!IS_CHAR_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007792 XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR);
7793 } else {
7794 ret = xmlStrndup(q, CUR_PTR - q);
7795 NEXT;
7796 }
7797 } else {
7798 XP_ERROR(XPATH_START_LITERAL_ERROR);
7799 }
7800 if (ret == NULL) return;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007801 PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0,
7802 xmlXPathNewString(ret), NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007803 xmlFree(ret);
7804}
7805
7806/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007807 * xmlXPathCompVariableReference:
Owen Taylor3473f882001-02-23 17:55:21 +00007808 * @ctxt: the XPath Parser context
7809 *
7810 * Parse a VariableReference, evaluate it and push it on the stack.
7811 *
7812 * The variable bindings consist of a mapping from variable names
William M. Brack08171912003-12-29 02:52:11 +00007813 * to variable values. The value of a variable is an object, which can be
Owen Taylor3473f882001-02-23 17:55:21 +00007814 * of any of the types that are possible for the value of an expression,
7815 * and may also be of additional types not specified here.
7816 *
7817 * Early evaluation is possible since:
7818 * The variable bindings [...] used to evaluate a subexpression are
7819 * always the same as those used to evaluate the containing expression.
7820 *
7821 * [36] VariableReference ::= '$' QName
7822 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007823static void
7824xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007825 xmlChar *name;
7826 xmlChar *prefix;
Owen Taylor3473f882001-02-23 17:55:21 +00007827
7828 SKIP_BLANKS;
7829 if (CUR != '$') {
7830 XP_ERROR(XPATH_VARIABLE_REF_ERROR);
7831 }
7832 NEXT;
7833 name = xmlXPathParseQName(ctxt, &prefix);
7834 if (name == NULL) {
7835 XP_ERROR(XPATH_VARIABLE_REF_ERROR);
7836 }
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007837 ctxt->comp->last = -1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007838 PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0,
7839 name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00007840 SKIP_BLANKS;
7841}
7842
7843/**
7844 * xmlXPathIsNodeType:
Owen Taylor3473f882001-02-23 17:55:21 +00007845 * @name: a name string
7846 *
7847 * Is the name given a NodeType one.
7848 *
7849 * [38] NodeType ::= 'comment'
7850 * | 'text'
7851 * | 'processing-instruction'
7852 * | 'node'
7853 *
7854 * Returns 1 if true 0 otherwise
7855 */
7856int
7857xmlXPathIsNodeType(const xmlChar *name) {
7858 if (name == NULL)
7859 return(0);
7860
Daniel Veillard1971ee22002-01-31 20:29:19 +00007861 if (xmlStrEqual(name, BAD_CAST "node"))
Owen Taylor3473f882001-02-23 17:55:21 +00007862 return(1);
7863 if (xmlStrEqual(name, BAD_CAST "text"))
7864 return(1);
Daniel Veillard1971ee22002-01-31 20:29:19 +00007865 if (xmlStrEqual(name, BAD_CAST "comment"))
Owen Taylor3473f882001-02-23 17:55:21 +00007866 return(1);
Daniel Veillard1971ee22002-01-31 20:29:19 +00007867 if (xmlStrEqual(name, BAD_CAST "processing-instruction"))
Owen Taylor3473f882001-02-23 17:55:21 +00007868 return(1);
7869 return(0);
7870}
7871
7872/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007873 * xmlXPathCompFunctionCall:
Owen Taylor3473f882001-02-23 17:55:21 +00007874 * @ctxt: the XPath Parser context
7875 *
7876 * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')'
7877 * [17] Argument ::= Expr
7878 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007879 * Compile a function call, the evaluation of all arguments are
Owen Taylor3473f882001-02-23 17:55:21 +00007880 * pushed on the stack
7881 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007882static void
7883xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007884 xmlChar *name;
7885 xmlChar *prefix;
Owen Taylor3473f882001-02-23 17:55:21 +00007886 int nbargs = 0;
7887
7888 name = xmlXPathParseQName(ctxt, &prefix);
7889 if (name == NULL) {
7890 XP_ERROR(XPATH_EXPR_ERROR);
7891 }
7892 SKIP_BLANKS;
Owen Taylor3473f882001-02-23 17:55:21 +00007893#ifdef DEBUG_EXPR
7894 if (prefix == NULL)
7895 xmlGenericError(xmlGenericErrorContext, "Calling function %s\n",
7896 name);
7897 else
7898 xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n",
7899 prefix, name);
7900#endif
7901
Owen Taylor3473f882001-02-23 17:55:21 +00007902 if (CUR != '(') {
7903 XP_ERROR(XPATH_EXPR_ERROR);
7904 }
7905 NEXT;
7906 SKIP_BLANKS;
7907
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007908 ctxt->comp->last = -1;
Daniel Veillard71f9d732003-01-14 16:07:16 +00007909 if (CUR != ')') {
7910 while (CUR != 0) {
7911 int op1 = ctxt->comp->last;
7912 ctxt->comp->last = -1;
7913 xmlXPathCompileExpr(ctxt);
7914 CHECK_ERROR;
7915 PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0);
7916 nbargs++;
7917 if (CUR == ')') break;
7918 if (CUR != ',') {
7919 XP_ERROR(XPATH_EXPR_ERROR);
7920 }
7921 NEXT;
7922 SKIP_BLANKS;
Owen Taylor3473f882001-02-23 17:55:21 +00007923 }
Owen Taylor3473f882001-02-23 17:55:21 +00007924 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007925 PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0,
7926 name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00007927 NEXT;
7928 SKIP_BLANKS;
Owen Taylor3473f882001-02-23 17:55:21 +00007929}
7930
7931/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007932 * xmlXPathCompPrimaryExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007933 * @ctxt: the XPath Parser context
7934 *
7935 * [15] PrimaryExpr ::= VariableReference
7936 * | '(' Expr ')'
7937 * | Literal
7938 * | Number
7939 * | FunctionCall
7940 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007941 * Compile a primary expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007942 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007943static void
7944xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007945 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007946 if (CUR == '$') xmlXPathCompVariableReference(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007947 else if (CUR == '(') {
7948 NEXT;
7949 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007950 xmlXPathCompileExpr(ctxt);
Aleksey Sanin50fe8b12002-05-07 16:21:36 +00007951 CHECK_ERROR;
Owen Taylor3473f882001-02-23 17:55:21 +00007952 if (CUR != ')') {
7953 XP_ERROR(XPATH_EXPR_ERROR);
7954 }
7955 NEXT;
7956 SKIP_BLANKS;
William M. Brackd1757ab2004-10-02 22:07:48 +00007957 } else if (IS_ASCII_DIGIT(CUR) || (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007958 xmlXPathCompNumber(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007959 } else if ((CUR == '\'') || (CUR == '"')) {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007960 xmlXPathCompLiteral(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007961 } else {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007962 xmlXPathCompFunctionCall(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007963 }
7964 SKIP_BLANKS;
7965}
7966
7967/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007968 * xmlXPathCompFilterExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007969 * @ctxt: the XPath Parser context
7970 *
7971 * [20] FilterExpr ::= PrimaryExpr
7972 * | FilterExpr Predicate
7973 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007974 * Compile a filter expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007975 * Square brackets are used to filter expressions in the same way that
7976 * they are used in location paths. It is an error if the expression to
7977 * be filtered does not evaluate to a node-set. The context node list
7978 * used for evaluating the expression in square brackets is the node-set
7979 * to be filtered listed in document order.
7980 */
7981
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007982static void
7983xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) {
7984 xmlXPathCompPrimaryExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007985 CHECK_ERROR;
7986 SKIP_BLANKS;
7987
7988 while (CUR == '[') {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00007989 xmlXPathCompPredicate(ctxt, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00007990 SKIP_BLANKS;
7991 }
7992
7993
7994}
7995
7996/**
7997 * xmlXPathScanName:
7998 * @ctxt: the XPath Parser context
7999 *
8000 * Trickery: parse an XML name but without consuming the input flow
8001 * Needed to avoid insanity in the parser state.
8002 *
8003 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
8004 * CombiningChar | Extender
8005 *
8006 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
8007 *
8008 * [6] Names ::= Name (S Name)*
8009 *
8010 * Returns the Name parsed or NULL
8011 */
8012
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008013static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00008014xmlXPathScanName(xmlXPathParserContextPtr ctxt) {
Daniel Veillard03226812004-11-01 14:55:21 +00008015 int len = 0, l;
8016 int c;
8017 int count = 0;
8018 const xmlChar *cur;
8019 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00008020
Daniel Veillard03226812004-11-01 14:55:21 +00008021 cur = ctxt->cur;
8022
8023 c = CUR_CHAR(l);
8024 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
8025 (!IS_LETTER(c) && (c != '_') &&
8026 (c != ':'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008027 return(NULL);
8028 }
8029
Daniel Veillard03226812004-11-01 14:55:21 +00008030 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
8031 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
8032 (c == '.') || (c == '-') ||
8033 (c == '_') || (c == ':') ||
8034 (IS_COMBINING(c)) ||
8035 (IS_EXTENDER(c)))) {
8036 len += l;
8037 NEXTL(l);
8038 c = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00008039 }
Daniel Veillard03226812004-11-01 14:55:21 +00008040 ret = xmlStrndup(cur, ctxt->cur - cur);
8041 ctxt->cur = cur;
8042 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00008043}
8044
8045/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008046 * xmlXPathCompPathExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008047 * @ctxt: the XPath Parser context
8048 *
8049 * [19] PathExpr ::= LocationPath
8050 * | FilterExpr
8051 * | FilterExpr '/' RelativeLocationPath
8052 * | FilterExpr '//' RelativeLocationPath
8053 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008054 * Compile a path expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008055 * The / operator and // operators combine an arbitrary expression
8056 * and a relative location path. It is an error if the expression
8057 * does not evaluate to a node-set.
8058 * The / operator does composition in the same way as when / is
8059 * used in a location path. As in location paths, // is short for
8060 * /descendant-or-self::node()/.
8061 */
8062
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008063static void
8064xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00008065 int lc = 1; /* Should we branch to LocationPath ? */
8066 xmlChar *name = NULL; /* we may have to preparse a name to find out */
8067
8068 SKIP_BLANKS;
William M. Brackd1757ab2004-10-02 22:07:48 +00008069 if ((CUR == '$') || (CUR == '(') ||
8070 (IS_ASCII_DIGIT(CUR)) ||
8071 (CUR == '\'') || (CUR == '"') ||
8072 (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008073 lc = 0;
8074 } else if (CUR == '*') {
8075 /* relative or absolute location path */
8076 lc = 1;
8077 } else if (CUR == '/') {
8078 /* relative or absolute location path */
8079 lc = 1;
8080 } else if (CUR == '@') {
8081 /* relative abbreviated attribute location path */
8082 lc = 1;
8083 } else if (CUR == '.') {
8084 /* relative abbreviated attribute location path */
8085 lc = 1;
8086 } else {
8087 /*
8088 * Problem is finding if we have a name here whether it's:
8089 * - a nodetype
8090 * - a function call in which case it's followed by '('
8091 * - an axis in which case it's followed by ':'
8092 * - a element name
8093 * We do an a priori analysis here rather than having to
8094 * maintain parsed token content through the recursive function
William M. Brack08171912003-12-29 02:52:11 +00008095 * calls. This looks uglier but makes the code easier to
Owen Taylor3473f882001-02-23 17:55:21 +00008096 * read/write/debug.
8097 */
8098 SKIP_BLANKS;
8099 name = xmlXPathScanName(ctxt);
8100 if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) {
8101#ifdef DEBUG_STEP
8102 xmlGenericError(xmlGenericErrorContext,
8103 "PathExpr: Axis\n");
8104#endif
8105 lc = 1;
8106 xmlFree(name);
8107 } else if (name != NULL) {
8108 int len =xmlStrlen(name);
Owen Taylor3473f882001-02-23 17:55:21 +00008109
8110
8111 while (NXT(len) != 0) {
8112 if (NXT(len) == '/') {
8113 /* element name */
8114#ifdef DEBUG_STEP
8115 xmlGenericError(xmlGenericErrorContext,
8116 "PathExpr: AbbrRelLocation\n");
8117#endif
8118 lc = 1;
8119 break;
William M. Brack76e95df2003-10-18 16:20:14 +00008120 } else if (IS_BLANK_CH(NXT(len))) {
William M. Brack78637da2003-07-31 14:47:38 +00008121 /* ignore blanks */
8122 ;
Owen Taylor3473f882001-02-23 17:55:21 +00008123 } else if (NXT(len) == ':') {
8124#ifdef DEBUG_STEP
8125 xmlGenericError(xmlGenericErrorContext,
8126 "PathExpr: AbbrRelLocation\n");
8127#endif
8128 lc = 1;
8129 break;
8130 } else if ((NXT(len) == '(')) {
8131 /* Note Type or Function */
8132 if (xmlXPathIsNodeType(name)) {
8133#ifdef DEBUG_STEP
8134 xmlGenericError(xmlGenericErrorContext,
8135 "PathExpr: Type search\n");
8136#endif
8137 lc = 1;
8138 } else {
8139#ifdef DEBUG_STEP
8140 xmlGenericError(xmlGenericErrorContext,
8141 "PathExpr: function call\n");
8142#endif
8143 lc = 0;
8144 }
8145 break;
8146 } else if ((NXT(len) == '[')) {
8147 /* element name */
8148#ifdef DEBUG_STEP
8149 xmlGenericError(xmlGenericErrorContext,
8150 "PathExpr: AbbrRelLocation\n");
8151#endif
8152 lc = 1;
8153 break;
8154 } else if ((NXT(len) == '<') || (NXT(len) == '>') ||
8155 (NXT(len) == '=')) {
8156 lc = 1;
8157 break;
8158 } else {
8159 lc = 1;
8160 break;
8161 }
8162 len++;
8163 }
8164 if (NXT(len) == 0) {
8165#ifdef DEBUG_STEP
8166 xmlGenericError(xmlGenericErrorContext,
8167 "PathExpr: AbbrRelLocation\n");
8168#endif
8169 /* element name */
8170 lc = 1;
8171 }
8172 xmlFree(name);
8173 } else {
William M. Brack08171912003-12-29 02:52:11 +00008174 /* make sure all cases are covered explicitly */
Owen Taylor3473f882001-02-23 17:55:21 +00008175 XP_ERROR(XPATH_EXPR_ERROR);
8176 }
8177 }
8178
8179 if (lc) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008180 if (CUR == '/') {
8181 PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0);
8182 } else {
8183 PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008184 }
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008185 xmlXPathCompLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008186 } else {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008187 xmlXPathCompFilterExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008188 CHECK_ERROR;
8189 if ((CUR == '/') && (NXT(1) == '/')) {
8190 SKIP(2);
8191 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008192
8193 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
8194 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
8195 PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0);
8196
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008197 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008198 } else if (CUR == '/') {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008199 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008200 }
8201 }
8202 SKIP_BLANKS;
8203}
8204
8205/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008206 * xmlXPathCompUnionExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008207 * @ctxt: the XPath Parser context
8208 *
8209 * [18] UnionExpr ::= PathExpr
8210 * | UnionExpr '|' PathExpr
8211 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008212 * Compile an union expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008213 */
8214
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008215static void
8216xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) {
8217 xmlXPathCompPathExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008218 CHECK_ERROR;
8219 SKIP_BLANKS;
8220 while (CUR == '|') {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008221 int op1 = ctxt->comp->last;
8222 PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008223
8224 NEXT;
8225 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008226 xmlXPathCompPathExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008227
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008228 PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0);
8229
Owen Taylor3473f882001-02-23 17:55:21 +00008230 SKIP_BLANKS;
8231 }
Owen Taylor3473f882001-02-23 17:55:21 +00008232}
8233
8234/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008235 * xmlXPathCompUnaryExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008236 * @ctxt: the XPath Parser context
8237 *
8238 * [27] UnaryExpr ::= UnionExpr
8239 * | '-' UnaryExpr
8240 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008241 * Compile an unary expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008242 */
8243
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008244static void
8245xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00008246 int minus = 0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008247 int found = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008248
8249 SKIP_BLANKS;
Daniel Veillard68d7b672001-03-12 18:22:04 +00008250 while (CUR == '-') {
8251 minus = 1 - minus;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008252 found = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00008253 NEXT;
8254 SKIP_BLANKS;
8255 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008256
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008257 xmlXPathCompUnionExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008258 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008259 if (found) {
8260 if (minus)
8261 PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0);
8262 else
8263 PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008264 }
8265}
8266
8267/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008268 * xmlXPathCompMultiplicativeExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008269 * @ctxt: the XPath Parser context
8270 *
8271 * [26] MultiplicativeExpr ::= UnaryExpr
8272 * | MultiplicativeExpr MultiplyOperator UnaryExpr
8273 * | MultiplicativeExpr 'div' UnaryExpr
8274 * | MultiplicativeExpr 'mod' UnaryExpr
8275 * [34] MultiplyOperator ::= '*'
8276 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008277 * Compile an Additive expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008278 */
8279
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008280static void
8281xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) {
8282 xmlXPathCompUnaryExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008283 CHECK_ERROR;
8284 SKIP_BLANKS;
8285 while ((CUR == '*') ||
8286 ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) ||
8287 ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) {
8288 int op = -1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008289 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008290
8291 if (CUR == '*') {
8292 op = 0;
8293 NEXT;
8294 } else if (CUR == 'd') {
8295 op = 1;
8296 SKIP(3);
8297 } else if (CUR == 'm') {
8298 op = 2;
8299 SKIP(3);
8300 }
8301 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008302 xmlXPathCompUnaryExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008303 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008304 PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008305 SKIP_BLANKS;
8306 }
8307}
8308
8309/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008310 * xmlXPathCompAdditiveExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008311 * @ctxt: the XPath Parser context
8312 *
8313 * [25] AdditiveExpr ::= MultiplicativeExpr
8314 * | AdditiveExpr '+' MultiplicativeExpr
8315 * | AdditiveExpr '-' MultiplicativeExpr
8316 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008317 * Compile an Additive expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008318 */
8319
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008320static void
8321xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008322
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008323 xmlXPathCompMultiplicativeExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008324 CHECK_ERROR;
8325 SKIP_BLANKS;
8326 while ((CUR == '+') || (CUR == '-')) {
8327 int plus;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008328 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008329
8330 if (CUR == '+') plus = 1;
8331 else plus = 0;
8332 NEXT;
8333 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008334 xmlXPathCompMultiplicativeExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008335 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008336 PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008337 SKIP_BLANKS;
8338 }
8339}
8340
8341/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008342 * xmlXPathCompRelationalExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008343 * @ctxt: the XPath Parser context
8344 *
8345 * [24] RelationalExpr ::= AdditiveExpr
8346 * | RelationalExpr '<' AdditiveExpr
8347 * | RelationalExpr '>' AdditiveExpr
8348 * | RelationalExpr '<=' AdditiveExpr
8349 * | RelationalExpr '>=' AdditiveExpr
8350 *
8351 * A <= B > C is allowed ? Answer from James, yes with
8352 * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr
8353 * which is basically what got implemented.
8354 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008355 * Compile a Relational expression, then push the result
Owen Taylor3473f882001-02-23 17:55:21 +00008356 * on the stack
8357 */
8358
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008359static void
8360xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) {
8361 xmlXPathCompAdditiveExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008362 CHECK_ERROR;
8363 SKIP_BLANKS;
8364 while ((CUR == '<') ||
8365 (CUR == '>') ||
8366 ((CUR == '<') && (NXT(1) == '=')) ||
8367 ((CUR == '>') && (NXT(1) == '='))) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008368 int inf, strict;
8369 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008370
8371 if (CUR == '<') inf = 1;
8372 else inf = 0;
8373 if (NXT(1) == '=') strict = 0;
8374 else strict = 1;
8375 NEXT;
8376 if (!strict) NEXT;
8377 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008378 xmlXPathCompAdditiveExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008379 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008380 PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict);
Owen Taylor3473f882001-02-23 17:55:21 +00008381 SKIP_BLANKS;
8382 }
8383}
8384
8385/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008386 * xmlXPathCompEqualityExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008387 * @ctxt: the XPath Parser context
8388 *
8389 * [23] EqualityExpr ::= RelationalExpr
8390 * | EqualityExpr '=' RelationalExpr
8391 * | EqualityExpr '!=' RelationalExpr
8392 *
8393 * A != B != C is allowed ? Answer from James, yes with
8394 * (RelationalExpr = RelationalExpr) = RelationalExpr
8395 * (RelationalExpr != RelationalExpr) != RelationalExpr
8396 * which is basically what got implemented.
8397 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008398 * Compile an Equality expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008399 *
8400 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008401static void
8402xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) {
8403 xmlXPathCompRelationalExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008404 CHECK_ERROR;
8405 SKIP_BLANKS;
8406 while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008407 int eq;
8408 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008409
8410 if (CUR == '=') eq = 1;
8411 else eq = 0;
8412 NEXT;
8413 if (!eq) NEXT;
8414 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008415 xmlXPathCompRelationalExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008416 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008417 PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008418 SKIP_BLANKS;
8419 }
8420}
8421
8422/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008423 * xmlXPathCompAndExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008424 * @ctxt: the XPath Parser context
8425 *
8426 * [22] AndExpr ::= EqualityExpr
8427 * | AndExpr 'and' EqualityExpr
8428 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008429 * Compile an AND expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008430 *
8431 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008432static void
8433xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) {
8434 xmlXPathCompEqualityExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008435 CHECK_ERROR;
8436 SKIP_BLANKS;
8437 while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008438 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008439 SKIP(3);
8440 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008441 xmlXPathCompEqualityExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008442 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008443 PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008444 SKIP_BLANKS;
8445 }
8446}
8447
8448/**
Daniel Veillard591b4be2003-02-09 23:33:36 +00008449 * xmlXPathCompileExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008450 * @ctxt: the XPath Parser context
8451 *
8452 * [14] Expr ::= OrExpr
8453 * [21] OrExpr ::= AndExpr
8454 * | OrExpr 'or' AndExpr
8455 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008456 * Parse and compile an expression
Owen Taylor3473f882001-02-23 17:55:21 +00008457 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008458static void
8459xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt) {
8460 xmlXPathCompAndExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008461 CHECK_ERROR;
8462 SKIP_BLANKS;
8463 while ((CUR == 'o') && (NXT(1) == 'r')) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008464 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008465 SKIP(2);
8466 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008467 xmlXPathCompAndExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008468 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008469 PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0);
8470 op1 = ctxt->comp->nbStep;
Owen Taylor3473f882001-02-23 17:55:21 +00008471 SKIP_BLANKS;
8472 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008473 if (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE) {
8474 /* more ops could be optimized too */
8475 PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0);
8476 }
Owen Taylor3473f882001-02-23 17:55:21 +00008477}
8478
8479/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008480 * xmlXPathCompPredicate:
Owen Taylor3473f882001-02-23 17:55:21 +00008481 * @ctxt: the XPath Parser context
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008482 * @filter: act as a filter
Owen Taylor3473f882001-02-23 17:55:21 +00008483 *
8484 * [8] Predicate ::= '[' PredicateExpr ']'
8485 * [9] PredicateExpr ::= Expr
8486 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008487 * Compile a predicate expression
Owen Taylor3473f882001-02-23 17:55:21 +00008488 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008489static void
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008490xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008491 int op1 = ctxt->comp->last;
8492
8493 SKIP_BLANKS;
8494 if (CUR != '[') {
8495 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
8496 }
8497 NEXT;
8498 SKIP_BLANKS;
8499
8500 ctxt->comp->last = -1;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008501 xmlXPathCompileExpr(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008502 CHECK_ERROR;
8503
8504 if (CUR != ']') {
8505 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
8506 }
8507
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008508 if (filter)
8509 PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0);
8510 else
8511 PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0);
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008512
8513 NEXT;
8514 SKIP_BLANKS;
8515}
8516
8517/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008518 * xmlXPathCompNodeTest:
Owen Taylor3473f882001-02-23 17:55:21 +00008519 * @ctxt: the XPath Parser context
8520 * @test: pointer to a xmlXPathTestVal
8521 * @type: pointer to a xmlXPathTypeVal
8522 * @prefix: placeholder for a possible name prefix
8523 *
8524 * [7] NodeTest ::= NameTest
8525 * | NodeType '(' ')'
8526 * | 'processing-instruction' '(' Literal ')'
8527 *
8528 * [37] NameTest ::= '*'
8529 * | NCName ':' '*'
8530 * | QName
8531 * [38] NodeType ::= 'comment'
8532 * | 'text'
8533 * | 'processing-instruction'
8534 * | 'node'
8535 *
William M. Brack08171912003-12-29 02:52:11 +00008536 * Returns the name found and updates @test, @type and @prefix appropriately
Owen Taylor3473f882001-02-23 17:55:21 +00008537 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008538static xmlChar *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008539xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test,
8540 xmlXPathTypeVal *type, const xmlChar **prefix,
8541 xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00008542 int blanks;
8543
8544 if ((test == NULL) || (type == NULL) || (prefix == NULL)) {
8545 STRANGE;
8546 return(NULL);
8547 }
William M. Brack78637da2003-07-31 14:47:38 +00008548 *type = (xmlXPathTypeVal) 0;
8549 *test = (xmlXPathTestVal) 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008550 *prefix = NULL;
8551 SKIP_BLANKS;
8552
8553 if ((name == NULL) && (CUR == '*')) {
8554 /*
8555 * All elements
8556 */
8557 NEXT;
8558 *test = NODE_TEST_ALL;
8559 return(NULL);
8560 }
8561
8562 if (name == NULL)
8563 name = xmlXPathParseNCName(ctxt);
8564 if (name == NULL) {
8565 XP_ERROR0(XPATH_EXPR_ERROR);
8566 }
8567
William M. Brack76e95df2003-10-18 16:20:14 +00008568 blanks = IS_BLANK_CH(CUR);
Owen Taylor3473f882001-02-23 17:55:21 +00008569 SKIP_BLANKS;
8570 if (CUR == '(') {
8571 NEXT;
8572 /*
8573 * NodeType or PI search
8574 */
8575 if (xmlStrEqual(name, BAD_CAST "comment"))
8576 *type = NODE_TYPE_COMMENT;
8577 else if (xmlStrEqual(name, BAD_CAST "node"))
8578 *type = NODE_TYPE_NODE;
8579 else if (xmlStrEqual(name, BAD_CAST "processing-instruction"))
8580 *type = NODE_TYPE_PI;
8581 else if (xmlStrEqual(name, BAD_CAST "text"))
8582 *type = NODE_TYPE_TEXT;
8583 else {
8584 if (name != NULL)
8585 xmlFree(name);
8586 XP_ERROR0(XPATH_EXPR_ERROR);
8587 }
8588
8589 *test = NODE_TEST_TYPE;
8590
8591 SKIP_BLANKS;
8592 if (*type == NODE_TYPE_PI) {
8593 /*
8594 * Specific case: search a PI by name.
8595 */
Owen Taylor3473f882001-02-23 17:55:21 +00008596 if (name != NULL)
8597 xmlFree(name);
Daniel Veillard82e49712001-04-26 14:38:03 +00008598 name = NULL;
8599 if (CUR != ')') {
8600 name = xmlXPathParseLiteral(ctxt);
8601 CHECK_ERROR 0;
Daniel Veillarded23b7d2002-05-27 12:16:02 +00008602 *test = NODE_TEST_PI;
Daniel Veillard82e49712001-04-26 14:38:03 +00008603 SKIP_BLANKS;
8604 }
Owen Taylor3473f882001-02-23 17:55:21 +00008605 }
8606 if (CUR != ')') {
8607 if (name != NULL)
8608 xmlFree(name);
8609 XP_ERROR0(XPATH_UNCLOSED_ERROR);
8610 }
8611 NEXT;
8612 return(name);
8613 }
8614 *test = NODE_TEST_NAME;
8615 if ((!blanks) && (CUR == ':')) {
8616 NEXT;
8617
8618 /*
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008619 * Since currently the parser context don't have a
8620 * namespace list associated:
8621 * The namespace name for this prefix can be computed
8622 * only at evaluation time. The compilation is done
8623 * outside of any context.
Owen Taylor3473f882001-02-23 17:55:21 +00008624 */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008625#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00008626 *prefix = xmlXPathNsLookup(ctxt->context, name);
8627 if (name != NULL)
8628 xmlFree(name);
8629 if (*prefix == NULL) {
8630 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
8631 }
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008632#else
8633 *prefix = name;
8634#endif
Owen Taylor3473f882001-02-23 17:55:21 +00008635
8636 if (CUR == '*') {
8637 /*
8638 * All elements
8639 */
8640 NEXT;
8641 *test = NODE_TEST_ALL;
8642 return(NULL);
8643 }
8644
8645 name = xmlXPathParseNCName(ctxt);
8646 if (name == NULL) {
8647 XP_ERROR0(XPATH_EXPR_ERROR);
8648 }
8649 }
8650 return(name);
8651}
8652
8653/**
8654 * xmlXPathIsAxisName:
8655 * @name: a preparsed name token
8656 *
8657 * [6] AxisName ::= 'ancestor'
8658 * | 'ancestor-or-self'
8659 * | 'attribute'
8660 * | 'child'
8661 * | 'descendant'
8662 * | 'descendant-or-self'
8663 * | 'following'
8664 * | 'following-sibling'
8665 * | 'namespace'
8666 * | 'parent'
8667 * | 'preceding'
8668 * | 'preceding-sibling'
8669 * | 'self'
8670 *
8671 * Returns the axis or 0
8672 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008673static xmlXPathAxisVal
Owen Taylor3473f882001-02-23 17:55:21 +00008674xmlXPathIsAxisName(const xmlChar *name) {
William M. Brack78637da2003-07-31 14:47:38 +00008675 xmlXPathAxisVal ret = (xmlXPathAxisVal) 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008676 switch (name[0]) {
8677 case 'a':
8678 if (xmlStrEqual(name, BAD_CAST "ancestor"))
8679 ret = AXIS_ANCESTOR;
8680 if (xmlStrEqual(name, BAD_CAST "ancestor-or-self"))
8681 ret = AXIS_ANCESTOR_OR_SELF;
8682 if (xmlStrEqual(name, BAD_CAST "attribute"))
8683 ret = AXIS_ATTRIBUTE;
8684 break;
8685 case 'c':
8686 if (xmlStrEqual(name, BAD_CAST "child"))
8687 ret = AXIS_CHILD;
8688 break;
8689 case 'd':
8690 if (xmlStrEqual(name, BAD_CAST "descendant"))
8691 ret = AXIS_DESCENDANT;
8692 if (xmlStrEqual(name, BAD_CAST "descendant-or-self"))
8693 ret = AXIS_DESCENDANT_OR_SELF;
8694 break;
8695 case 'f':
8696 if (xmlStrEqual(name, BAD_CAST "following"))
8697 ret = AXIS_FOLLOWING;
8698 if (xmlStrEqual(name, BAD_CAST "following-sibling"))
8699 ret = AXIS_FOLLOWING_SIBLING;
8700 break;
8701 case 'n':
8702 if (xmlStrEqual(name, BAD_CAST "namespace"))
8703 ret = AXIS_NAMESPACE;
8704 break;
8705 case 'p':
8706 if (xmlStrEqual(name, BAD_CAST "parent"))
8707 ret = AXIS_PARENT;
8708 if (xmlStrEqual(name, BAD_CAST "preceding"))
8709 ret = AXIS_PRECEDING;
8710 if (xmlStrEqual(name, BAD_CAST "preceding-sibling"))
8711 ret = AXIS_PRECEDING_SIBLING;
8712 break;
8713 case 's':
8714 if (xmlStrEqual(name, BAD_CAST "self"))
8715 ret = AXIS_SELF;
8716 break;
8717 }
8718 return(ret);
8719}
8720
8721/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008722 * xmlXPathCompStep:
Owen Taylor3473f882001-02-23 17:55:21 +00008723 * @ctxt: the XPath Parser context
8724 *
8725 * [4] Step ::= AxisSpecifier NodeTest Predicate*
8726 * | AbbreviatedStep
8727 *
8728 * [12] AbbreviatedStep ::= '.' | '..'
8729 *
8730 * [5] AxisSpecifier ::= AxisName '::'
8731 * | AbbreviatedAxisSpecifier
8732 *
8733 * [13] AbbreviatedAxisSpecifier ::= '@'?
8734 *
8735 * Modified for XPtr range support as:
8736 *
8737 * [4xptr] Step ::= AxisSpecifier NodeTest Predicate*
8738 * | AbbreviatedStep
8739 * | 'range-to' '(' Expr ')' Predicate*
8740 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008741 * Compile one step in a Location Path
Owen Taylor3473f882001-02-23 17:55:21 +00008742 * A location step of . is short for self::node(). This is
8743 * particularly useful in conjunction with //. For example, the
8744 * location path .//para is short for
8745 * self::node()/descendant-or-self::node()/child::para
8746 * and so will select all para descendant elements of the context
8747 * node.
8748 * Similarly, a location step of .. is short for parent::node().
8749 * For example, ../title is short for parent::node()/child::title
8750 * and so will select the title children of the parent of the context
8751 * node.
8752 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008753static void
8754xmlXPathCompStep(xmlXPathParserContextPtr ctxt) {
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008755#ifdef LIBXML_XPTR_ENABLED
8756 int rangeto = 0;
8757 int op2 = -1;
8758#endif
8759
Owen Taylor3473f882001-02-23 17:55:21 +00008760 SKIP_BLANKS;
8761 if ((CUR == '.') && (NXT(1) == '.')) {
8762 SKIP(2);
8763 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008764 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT,
8765 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008766 } else if (CUR == '.') {
8767 NEXT;
8768 SKIP_BLANKS;
8769 } else {
8770 xmlChar *name = NULL;
8771 const xmlChar *prefix = NULL;
8772 xmlXPathTestVal test;
William M. Brack78637da2003-07-31 14:47:38 +00008773 xmlXPathAxisVal axis = (xmlXPathAxisVal) 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008774 xmlXPathTypeVal type;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008775 int op1;
Owen Taylor3473f882001-02-23 17:55:21 +00008776
8777 /*
8778 * The modification needed for XPointer change to the production
8779 */
8780#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008781 if (ctxt->xptr) {
Owen Taylor3473f882001-02-23 17:55:21 +00008782 name = xmlXPathParseNCName(ctxt);
8783 if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) {
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008784 op2 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008785 xmlFree(name);
8786 SKIP_BLANKS;
8787 if (CUR != '(') {
8788 XP_ERROR(XPATH_EXPR_ERROR);
8789 }
8790 NEXT;
8791 SKIP_BLANKS;
8792
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008793 xmlXPathCompileExpr(ctxt);
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008794 /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */
Owen Taylor3473f882001-02-23 17:55:21 +00008795 CHECK_ERROR;
8796
8797 SKIP_BLANKS;
8798 if (CUR != ')') {
8799 XP_ERROR(XPATH_EXPR_ERROR);
8800 }
8801 NEXT;
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008802 rangeto = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00008803 goto eval_predicates;
8804 }
8805 }
8806#endif
Daniel Veillard2156a562001-04-28 12:24:34 +00008807 if (CUR == '*') {
8808 axis = AXIS_CHILD;
8809 } else {
8810 if (name == NULL)
8811 name = xmlXPathParseNCName(ctxt);
8812 if (name != NULL) {
8813 axis = xmlXPathIsAxisName(name);
8814 if (axis != 0) {
8815 SKIP_BLANKS;
8816 if ((CUR == ':') && (NXT(1) == ':')) {
8817 SKIP(2);
8818 xmlFree(name);
8819 name = NULL;
8820 } else {
8821 /* an element name can conflict with an axis one :-\ */
8822 axis = AXIS_CHILD;
8823 }
Owen Taylor3473f882001-02-23 17:55:21 +00008824 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00008825 axis = AXIS_CHILD;
8826 }
Daniel Veillard2156a562001-04-28 12:24:34 +00008827 } else if (CUR == '@') {
8828 NEXT;
8829 axis = AXIS_ATTRIBUTE;
Owen Taylor3473f882001-02-23 17:55:21 +00008830 } else {
Daniel Veillard2156a562001-04-28 12:24:34 +00008831 axis = AXIS_CHILD;
Owen Taylor3473f882001-02-23 17:55:21 +00008832 }
Owen Taylor3473f882001-02-23 17:55:21 +00008833 }
8834
8835 CHECK_ERROR;
8836
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008837 name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name);
Owen Taylor3473f882001-02-23 17:55:21 +00008838 if (test == 0)
8839 return;
8840
8841#ifdef DEBUG_STEP
8842 xmlGenericError(xmlGenericErrorContext,
8843 "Basis : computing new set\n");
8844#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008845
Owen Taylor3473f882001-02-23 17:55:21 +00008846#ifdef DEBUG_STEP
8847 xmlGenericError(xmlGenericErrorContext, "Basis : ");
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008848 if (ctxt->value == NULL)
8849 xmlGenericError(xmlGenericErrorContext, "no value\n");
8850 else if (ctxt->value->nodesetval == NULL)
8851 xmlGenericError(xmlGenericErrorContext, "Empty\n");
8852 else
8853 xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval);
Owen Taylor3473f882001-02-23 17:55:21 +00008854#endif
Owen Taylor3473f882001-02-23 17:55:21 +00008855
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00008856#ifdef LIBXML_XPTR_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00008857eval_predicates:
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00008858#endif
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008859 op1 = ctxt->comp->last;
8860 ctxt->comp->last = -1;
8861
Owen Taylor3473f882001-02-23 17:55:21 +00008862 SKIP_BLANKS;
8863 while (CUR == '[') {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008864 xmlXPathCompPredicate(ctxt, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008865 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008866
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008867#ifdef LIBXML_XPTR_ENABLED
8868 if (rangeto) {
8869 PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0);
8870 } else
8871#endif
8872 PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis,
8873 test, type, (void *)prefix, (void *)name);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008874
Owen Taylor3473f882001-02-23 17:55:21 +00008875 }
8876#ifdef DEBUG_STEP
8877 xmlGenericError(xmlGenericErrorContext, "Step : ");
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008878 if (ctxt->value == NULL)
8879 xmlGenericError(xmlGenericErrorContext, "no value\n");
8880 else if (ctxt->value->nodesetval == NULL)
8881 xmlGenericError(xmlGenericErrorContext, "Empty\n");
8882 else
8883 xmlGenericErrorContextNodeSet(xmlGenericErrorContext,
8884 ctxt->value->nodesetval);
Owen Taylor3473f882001-02-23 17:55:21 +00008885#endif
8886}
8887
8888/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008889 * xmlXPathCompRelativeLocationPath:
Owen Taylor3473f882001-02-23 17:55:21 +00008890 * @ctxt: the XPath Parser context
8891 *
8892 * [3] RelativeLocationPath ::= Step
8893 * | RelativeLocationPath '/' Step
8894 * | AbbreviatedRelativeLocationPath
8895 * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step
8896 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008897 * Compile a relative location path.
Owen Taylor3473f882001-02-23 17:55:21 +00008898 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008899static void
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008900xmlXPathCompRelativeLocationPath
Owen Taylor3473f882001-02-23 17:55:21 +00008901(xmlXPathParserContextPtr ctxt) {
8902 SKIP_BLANKS;
8903 if ((CUR == '/') && (NXT(1) == '/')) {
8904 SKIP(2);
8905 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008906 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
8907 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008908 } else if (CUR == '/') {
8909 NEXT;
8910 SKIP_BLANKS;
8911 }
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008912 xmlXPathCompStep(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008913 SKIP_BLANKS;
8914 while (CUR == '/') {
8915 if ((CUR == '/') && (NXT(1) == '/')) {
8916 SKIP(2);
8917 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008918 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
Owen Taylor3473f882001-02-23 17:55:21 +00008919 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008920 xmlXPathCompStep(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008921 } else if (CUR == '/') {
8922 NEXT;
8923 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008924 xmlXPathCompStep(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008925 }
8926 SKIP_BLANKS;
8927 }
8928}
8929
8930/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008931 * xmlXPathCompLocationPath:
Owen Taylor3473f882001-02-23 17:55:21 +00008932 * @ctxt: the XPath Parser context
8933 *
8934 * [1] LocationPath ::= RelativeLocationPath
8935 * | AbsoluteLocationPath
8936 * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath?
8937 * | AbbreviatedAbsoluteLocationPath
8938 * [10] AbbreviatedAbsoluteLocationPath ::=
8939 * '//' RelativeLocationPath
8940 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008941 * Compile a location path
8942 *
Owen Taylor3473f882001-02-23 17:55:21 +00008943 * // is short for /descendant-or-self::node()/. For example,
8944 * //para is short for /descendant-or-self::node()/child::para and
8945 * so will select any para element in the document (even a para element
8946 * that is a document element will be selected by //para since the
8947 * document element node is a child of the root node); div//para is
8948 * short for div/descendant-or-self::node()/child::para and so will
8949 * select all para descendants of div children.
8950 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008951static void
8952xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00008953 SKIP_BLANKS;
8954 if (CUR != '/') {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008955 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008956 } else {
8957 while (CUR == '/') {
8958 if ((CUR == '/') && (NXT(1) == '/')) {
8959 SKIP(2);
8960 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008961 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
8962 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008963 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008964 } else if (CUR == '/') {
8965 NEXT;
Daniel Veillard608ad072001-06-14 08:32:28 +00008966 SKIP_BLANKS;
8967 if ((CUR != 0 ) &&
William M. Brackd1757ab2004-10-02 22:07:48 +00008968 ((IS_ASCII_LETTER(CUR)) || (CUR == '_') || (CUR == '.') ||
Daniel Veillard608ad072001-06-14 08:32:28 +00008969 (CUR == '@') || (CUR == '*')))
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008970 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008971 }
8972 }
8973 }
8974}
8975
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008976/************************************************************************
8977 * *
8978 * XPath precompiled expression evaluation *
8979 * *
8980 ************************************************************************/
8981
Daniel Veillardf06307e2001-07-03 10:35:50 +00008982static int
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008983xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op);
8984
8985/**
8986 * xmlXPathNodeCollectAndTest:
8987 * @ctxt: the XPath Parser context
8988 * @op: the XPath precompiled step operation
Daniel Veillardf06307e2001-07-03 10:35:50 +00008989 * @first: pointer to the first element in document order
8990 * @last: pointer to the last element in document order
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008991 *
8992 * This is the function implementing a step: based on the current list
8993 * of nodes, it builds up a new list, looking at all nodes under that
William M. Brack08171912003-12-29 02:52:11 +00008994 * axis and selecting them. It also does the predicate filtering
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008995 *
8996 * Pushes the new NodeSet resulting from the search.
Daniel Veillardf06307e2001-07-03 10:35:50 +00008997 *
William M. Brack08171912003-12-29 02:52:11 +00008998 * Returns the number of nodes traversed
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008999 */
Daniel Veillardf06307e2001-07-03 10:35:50 +00009000static int
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009001xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt,
Daniel Veillardf06307e2001-07-03 10:35:50 +00009002 xmlXPathStepOpPtr op,
9003 xmlNodePtr * first, xmlNodePtr * last)
9004{
William M. Brack78637da2003-07-31 14:47:38 +00009005 xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value;
9006 xmlXPathTestVal test = (xmlXPathTestVal) op->value2;
9007 xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009008 const xmlChar *prefix = op->value4;
9009 const xmlChar *name = op->value5;
Daniel Veillarde043ee12001-04-16 14:08:07 +00009010 const xmlChar *URI = NULL;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009011
9012#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009013 int n = 0;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009014#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009015 int i, t = 0;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009016 xmlNodeSetPtr ret, list;
9017 xmlXPathTraversalFunction next = NULL;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009018 void (*addNode) (xmlNodeSetPtr, xmlNodePtr);
Daniel Veillard75be0132002-03-13 10:03:35 +00009019 xmlNodeSetPtr (*mergeNodeSet) (xmlNodeSetPtr, xmlNodeSetPtr);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009020 xmlNodePtr cur = NULL;
9021 xmlXPathObjectPtr obj;
9022 xmlNodeSetPtr nodelist;
9023 xmlNodePtr tmp;
9024
Daniel Veillardf06307e2001-07-03 10:35:50 +00009025 CHECK_TYPE0(XPATH_NODESET);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009026 obj = valuePop(ctxt);
9027 addNode = xmlXPathNodeSetAdd;
Daniel Veillard75be0132002-03-13 10:03:35 +00009028 mergeNodeSet = xmlXPathNodeSetMerge;
Daniel Veillarde043ee12001-04-16 14:08:07 +00009029 if (prefix != NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009030 URI = xmlXPathNsLookup(ctxt->context, prefix);
9031 if (URI == NULL)
9032 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
Daniel Veillarde043ee12001-04-16 14:08:07 +00009033 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009034#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009035 xmlGenericError(xmlGenericErrorContext, "new step : ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009036#endif
9037 switch (axis) {
9038 case AXIS_ANCESTOR:
9039#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009040 xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009041#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009042 first = NULL;
9043 next = xmlXPathNextAncestor;
9044 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009045 case AXIS_ANCESTOR_OR_SELF:
9046#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009047 xmlGenericError(xmlGenericErrorContext,
9048 "axis 'ancestors-or-self' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009049#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009050 first = NULL;
9051 next = xmlXPathNextAncestorOrSelf;
9052 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009053 case AXIS_ATTRIBUTE:
9054#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009055 xmlGenericError(xmlGenericErrorContext, "axis 'attributes' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009056#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009057 first = NULL;
9058 last = NULL;
9059 next = xmlXPathNextAttribute;
Daniel Veillard75be0132002-03-13 10:03:35 +00009060 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009061 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009062 case AXIS_CHILD:
9063#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009064 xmlGenericError(xmlGenericErrorContext, "axis 'child' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009065#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009066 last = NULL;
9067 next = xmlXPathNextChild;
Daniel Veillard75be0132002-03-13 10:03:35 +00009068 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009069 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009070 case AXIS_DESCENDANT:
9071#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009072 xmlGenericError(xmlGenericErrorContext, "axis 'descendant' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009073#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009074 last = NULL;
9075 next = xmlXPathNextDescendant;
9076 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009077 case AXIS_DESCENDANT_OR_SELF:
9078#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009079 xmlGenericError(xmlGenericErrorContext,
9080 "axis 'descendant-or-self' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009081#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009082 last = NULL;
9083 next = xmlXPathNextDescendantOrSelf;
9084 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009085 case AXIS_FOLLOWING:
9086#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009087 xmlGenericError(xmlGenericErrorContext, "axis 'following' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009088#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009089 last = NULL;
9090 next = xmlXPathNextFollowing;
9091 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009092 case AXIS_FOLLOWING_SIBLING:
9093#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009094 xmlGenericError(xmlGenericErrorContext,
9095 "axis 'following-siblings' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009096#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009097 last = NULL;
9098 next = xmlXPathNextFollowingSibling;
9099 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009100 case AXIS_NAMESPACE:
9101#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009102 xmlGenericError(xmlGenericErrorContext, "axis 'namespace' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009103#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009104 first = NULL;
9105 last = NULL;
9106 next = (xmlXPathTraversalFunction) xmlXPathNextNamespace;
Daniel Veillard75be0132002-03-13 10:03:35 +00009107 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009108 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009109 case AXIS_PARENT:
9110#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009111 xmlGenericError(xmlGenericErrorContext, "axis 'parent' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009112#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009113 first = NULL;
9114 next = xmlXPathNextParent;
9115 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009116 case AXIS_PRECEDING:
9117#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009118 xmlGenericError(xmlGenericErrorContext, "axis 'preceding' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009119#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009120 first = NULL;
9121 next = xmlXPathNextPrecedingInternal;
9122 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009123 case AXIS_PRECEDING_SIBLING:
9124#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009125 xmlGenericError(xmlGenericErrorContext,
9126 "axis 'preceding-sibling' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009127#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009128 first = NULL;
9129 next = xmlXPathNextPrecedingSibling;
9130 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009131 case AXIS_SELF:
9132#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009133 xmlGenericError(xmlGenericErrorContext, "axis 'self' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009134#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009135 first = NULL;
9136 last = NULL;
9137 next = xmlXPathNextSelf;
Daniel Veillard75be0132002-03-13 10:03:35 +00009138 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009139 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009140 }
9141 if (next == NULL)
Daniel Veillardf06307e2001-07-03 10:35:50 +00009142 return(0);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009143
9144 nodelist = obj->nodesetval;
9145 if (nodelist == NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009146 xmlXPathFreeObject(obj);
9147 valuePush(ctxt, xmlXPathWrapNodeSet(NULL));
9148 return(0);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009149 }
9150 addNode = xmlXPathNodeSetAddUnique;
9151 ret = NULL;
9152#ifdef DEBUG_STEP
9153 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf06307e2001-07-03 10:35:50 +00009154 " context contains %d nodes\n", nodelist->nodeNr);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009155 switch (test) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009156 case NODE_TEST_NONE:
9157 xmlGenericError(xmlGenericErrorContext,
9158 " searching for none !!!\n");
9159 break;
9160 case NODE_TEST_TYPE:
9161 xmlGenericError(xmlGenericErrorContext,
9162 " searching for type %d\n", type);
9163 break;
9164 case NODE_TEST_PI:
9165 xmlGenericError(xmlGenericErrorContext,
9166 " searching for PI !!!\n");
9167 break;
9168 case NODE_TEST_ALL:
9169 xmlGenericError(xmlGenericErrorContext,
9170 " searching for *\n");
9171 break;
9172 case NODE_TEST_NS:
9173 xmlGenericError(xmlGenericErrorContext,
9174 " searching for namespace %s\n",
9175 prefix);
9176 break;
9177 case NODE_TEST_NAME:
9178 xmlGenericError(xmlGenericErrorContext,
9179 " searching for name %s\n", name);
9180 if (prefix != NULL)
9181 xmlGenericError(xmlGenericErrorContext,
9182 " with namespace %s\n", prefix);
9183 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009184 }
9185 xmlGenericError(xmlGenericErrorContext, "Testing : ");
9186#endif
9187 /*
9188 * 2.3 Node Tests
9189 * - For the attribute axis, the principal node type is attribute.
9190 * - For the namespace axis, the principal node type is namespace.
9191 * - For other axes, the principal node type is element.
9192 *
9193 * A node test * is true for any node of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009194 * principal node type. For example, child::* will
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009195 * select all element children of the context node
9196 */
9197 tmp = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009198 for (i = 0; i < nodelist->nodeNr; i++) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009199 ctxt->context->node = nodelist->nodeTab[i];
9200
Daniel Veillardf06307e2001-07-03 10:35:50 +00009201 cur = NULL;
9202 list = xmlXPathNodeSetCreate(NULL);
9203 do {
9204 cur = next(ctxt, cur);
9205 if (cur == NULL)
9206 break;
9207 if ((first != NULL) && (*first == cur))
9208 break;
9209 if (((t % 256) == 0) &&
9210 (first != NULL) && (*first != NULL) &&
9211 (xmlXPathCmpNodes(*first, cur) >= 0))
9212 break;
9213 if ((last != NULL) && (*last == cur))
9214 break;
9215 if (((t % 256) == 0) &&
9216 (last != NULL) && (*last != NULL) &&
9217 (xmlXPathCmpNodes(cur, *last) >= 0))
9218 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009219 t++;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009220#ifdef DEBUG_STEP
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009221 xmlGenericError(xmlGenericErrorContext, " %s", cur->name);
9222#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009223 switch (test) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009224 case NODE_TEST_NONE:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009225 ctxt->context->node = tmp;
9226 STRANGE return(t);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009227 case NODE_TEST_TYPE:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009228 if ((cur->type == type) ||
9229 ((type == NODE_TYPE_NODE) &&
9230 ((cur->type == XML_DOCUMENT_NODE) ||
9231 (cur->type == XML_HTML_DOCUMENT_NODE) ||
9232 (cur->type == XML_ELEMENT_NODE) ||
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00009233 (cur->type == XML_NAMESPACE_DECL) ||
9234 (cur->type == XML_ATTRIBUTE_NODE) ||
Daniel Veillardf06307e2001-07-03 10:35:50 +00009235 (cur->type == XML_PI_NODE) ||
9236 (cur->type == XML_COMMENT_NODE) ||
9237 (cur->type == XML_CDATA_SECTION_NODE) ||
Daniel Veillard7583a592001-07-08 13:15:55 +00009238 (cur->type == XML_TEXT_NODE))) ||
9239 ((type == NODE_TYPE_TEXT) &&
9240 (cur->type == XML_CDATA_SECTION_NODE))) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009241#ifdef DEBUG_STEP
9242 n++;
9243#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009244 addNode(list, cur);
9245 }
9246 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009247 case NODE_TEST_PI:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009248 if (cur->type == XML_PI_NODE) {
9249 if ((name != NULL) &&
9250 (!xmlStrEqual(name, cur->name)))
9251 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009252#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009253 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009254#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009255 addNode(list, cur);
9256 }
9257 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009258 case NODE_TEST_ALL:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009259 if (axis == AXIS_ATTRIBUTE) {
9260 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009261#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009262 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009263#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009264 addNode(list, cur);
9265 }
9266 } else if (axis == AXIS_NAMESPACE) {
9267 if (cur->type == XML_NAMESPACE_DECL) {
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 Veillard044fc6b2002-03-04 17:09:44 +00009271 xmlXPathNodeSetAddNs(list, ctxt->context->node,
9272 (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009273 }
9274 } else {
9275 if (cur->type == XML_ELEMENT_NODE) {
9276 if (prefix == NULL) {
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 } else if ((cur->ns != NULL) &&
9282 (xmlStrEqual(URI, cur->ns->href))) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009283#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009284 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009285#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009286 addNode(list, cur);
9287 }
9288 }
9289 }
9290 break;
9291 case NODE_TEST_NS:{
9292 TODO;
9293 break;
9294 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009295 case NODE_TEST_NAME:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009296 switch (cur->type) {
9297 case XML_ELEMENT_NODE:
9298 if (xmlStrEqual(name, cur->name)) {
9299 if (prefix == NULL) {
9300 if (cur->ns == NULL) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009301#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009302 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009303#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009304 addNode(list, cur);
9305 }
9306 } else {
9307 if ((cur->ns != NULL) &&
9308 (xmlStrEqual(URI,
9309 cur->ns->href))) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009310#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009311 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009312#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009313 addNode(list, cur);
9314 }
9315 }
9316 }
9317 break;
9318 case XML_ATTRIBUTE_NODE:{
9319 xmlAttrPtr attr = (xmlAttrPtr) cur;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009320
Daniel Veillardf06307e2001-07-03 10:35:50 +00009321 if (xmlStrEqual(name, attr->name)) {
9322 if (prefix == NULL) {
9323 if ((attr->ns == NULL) ||
9324 (attr->ns->prefix == NULL)) {
9325#ifdef DEBUG_STEP
9326 n++;
9327#endif
9328 addNode(list,
9329 (xmlNodePtr) attr);
9330 }
9331 } else {
9332 if ((attr->ns != NULL) &&
9333 (xmlStrEqual(URI,
9334 attr->ns->
9335 href))) {
9336#ifdef DEBUG_STEP
9337 n++;
9338#endif
9339 addNode(list,
9340 (xmlNodePtr) attr);
9341 }
9342 }
9343 }
9344 break;
9345 }
9346 case XML_NAMESPACE_DECL:
9347 if (cur->type == XML_NAMESPACE_DECL) {
9348 xmlNsPtr ns = (xmlNsPtr) cur;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009349
Daniel Veillardf06307e2001-07-03 10:35:50 +00009350 if ((ns->prefix != NULL) && (name != NULL)
9351 && (xmlStrEqual(ns->prefix, name))) {
9352#ifdef DEBUG_STEP
9353 n++;
9354#endif
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009355 xmlXPathNodeSetAddNs(list,
9356 ctxt->context->node, (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009357 }
9358 }
9359 break;
9360 default:
9361 break;
9362 }
9363 break;
9364 break;
9365 }
9366 } while (cur != NULL);
9367
9368 /*
9369 * If there is some predicate filtering do it now
9370 */
Daniel Veillard6fbcf422002-03-21 12:32:59 +00009371 if ((op->ch2 != -1) && (list != NULL) && (list->nodeNr > 0)) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009372 xmlXPathObjectPtr obj2;
9373
9374 valuePush(ctxt, xmlXPathWrapNodeSet(list));
9375 xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch2]);
9376 CHECK_TYPE0(XPATH_NODESET);
9377 obj2 = valuePop(ctxt);
9378 list = obj2->nodesetval;
9379 obj2->nodesetval = NULL;
9380 xmlXPathFreeObject(obj2);
9381 }
9382 if (ret == NULL) {
9383 ret = list;
9384 } else {
Daniel Veillard75be0132002-03-13 10:03:35 +00009385 ret = mergeNodeSet(ret, list);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009386 xmlXPathFreeNodeSet(list);
9387 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009388 }
9389 ctxt->context->node = tmp;
9390#ifdef DEBUG_STEP
9391 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf06307e2001-07-03 10:35:50 +00009392 "\nExamined %d nodes, found %d nodes at that step\n",
9393 t, n);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009394#endif
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009395 valuePush(ctxt, xmlXPathWrapNodeSet(ret));
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00009396 if ((obj->boolval) && (obj->user != NULL)) {
9397 ctxt->value->boolval = 1;
9398 ctxt->value->user = obj->user;
9399 obj->user = NULL;
9400 obj->boolval = 0;
9401 }
9402 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009403 return(t);
9404}
9405
9406/**
9407 * xmlXPathNodeCollectAndTestNth:
9408 * @ctxt: the XPath Parser context
9409 * @op: the XPath precompiled step operation
9410 * @indx: the index to collect
9411 * @first: pointer to the first element in document order
9412 * @last: pointer to the last element in document order
9413 *
9414 * This is the function implementing a step: based on the current list
9415 * of nodes, it builds up a new list, looking at all nodes under that
William M. Brack08171912003-12-29 02:52:11 +00009416 * axis and selecting them. It also does the predicate filtering
Daniel Veillardf06307e2001-07-03 10:35:50 +00009417 *
9418 * Pushes the new NodeSet resulting from the search.
9419 * Returns the number of node traversed
9420 */
9421static int
9422xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt,
9423 xmlXPathStepOpPtr op, int indx,
9424 xmlNodePtr * first, xmlNodePtr * last)
9425{
William M. Brack78637da2003-07-31 14:47:38 +00009426 xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value;
9427 xmlXPathTestVal test = (xmlXPathTestVal) op->value2;
9428 xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009429 const xmlChar *prefix = op->value4;
9430 const xmlChar *name = op->value5;
9431 const xmlChar *URI = NULL;
9432 int n = 0, t = 0;
9433
9434 int i;
9435 xmlNodeSetPtr list;
9436 xmlXPathTraversalFunction next = NULL;
9437 void (*addNode) (xmlNodeSetPtr, xmlNodePtr);
9438 xmlNodePtr cur = NULL;
9439 xmlXPathObjectPtr obj;
9440 xmlNodeSetPtr nodelist;
9441 xmlNodePtr tmp;
9442
9443 CHECK_TYPE0(XPATH_NODESET);
9444 obj = valuePop(ctxt);
9445 addNode = xmlXPathNodeSetAdd;
9446 if (prefix != NULL) {
9447 URI = xmlXPathNsLookup(ctxt->context, prefix);
9448 if (URI == NULL)
9449 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
9450 }
9451#ifdef DEBUG_STEP_NTH
9452 xmlGenericError(xmlGenericErrorContext, "new step : ");
9453 if (first != NULL) {
9454 if (*first != NULL)
9455 xmlGenericError(xmlGenericErrorContext, "first = %s ",
9456 (*first)->name);
9457 else
9458 xmlGenericError(xmlGenericErrorContext, "first = NULL ");
9459 }
9460 if (last != NULL) {
9461 if (*last != NULL)
9462 xmlGenericError(xmlGenericErrorContext, "last = %s ",
9463 (*last)->name);
9464 else
9465 xmlGenericError(xmlGenericErrorContext, "last = NULL ");
9466 }
9467#endif
9468 switch (axis) {
9469 case AXIS_ANCESTOR:
9470#ifdef DEBUG_STEP_NTH
9471 xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' ");
9472#endif
9473 first = NULL;
9474 next = xmlXPathNextAncestor;
9475 break;
9476 case AXIS_ANCESTOR_OR_SELF:
9477#ifdef DEBUG_STEP_NTH
9478 xmlGenericError(xmlGenericErrorContext,
9479 "axis 'ancestors-or-self' ");
9480#endif
9481 first = NULL;
9482 next = xmlXPathNextAncestorOrSelf;
9483 break;
9484 case AXIS_ATTRIBUTE:
9485#ifdef DEBUG_STEP_NTH
9486 xmlGenericError(xmlGenericErrorContext, "axis 'attributes' ");
9487#endif
9488 first = NULL;
9489 last = NULL;
9490 next = xmlXPathNextAttribute;
9491 break;
9492 case AXIS_CHILD:
9493#ifdef DEBUG_STEP_NTH
9494 xmlGenericError(xmlGenericErrorContext, "axis 'child' ");
9495#endif
9496 last = NULL;
9497 next = xmlXPathNextChild;
9498 break;
9499 case AXIS_DESCENDANT:
9500#ifdef DEBUG_STEP_NTH
9501 xmlGenericError(xmlGenericErrorContext, "axis 'descendant' ");
9502#endif
9503 last = NULL;
9504 next = xmlXPathNextDescendant;
9505 break;
9506 case AXIS_DESCENDANT_OR_SELF:
9507#ifdef DEBUG_STEP_NTH
9508 xmlGenericError(xmlGenericErrorContext,
9509 "axis 'descendant-or-self' ");
9510#endif
9511 last = NULL;
9512 next = xmlXPathNextDescendantOrSelf;
9513 break;
9514 case AXIS_FOLLOWING:
9515#ifdef DEBUG_STEP_NTH
9516 xmlGenericError(xmlGenericErrorContext, "axis 'following' ");
9517#endif
9518 last = NULL;
9519 next = xmlXPathNextFollowing;
9520 break;
9521 case AXIS_FOLLOWING_SIBLING:
9522#ifdef DEBUG_STEP_NTH
9523 xmlGenericError(xmlGenericErrorContext,
9524 "axis 'following-siblings' ");
9525#endif
9526 last = NULL;
9527 next = xmlXPathNextFollowingSibling;
9528 break;
9529 case AXIS_NAMESPACE:
9530#ifdef DEBUG_STEP_NTH
9531 xmlGenericError(xmlGenericErrorContext, "axis 'namespace' ");
9532#endif
9533 last = NULL;
9534 first = NULL;
9535 next = (xmlXPathTraversalFunction) xmlXPathNextNamespace;
9536 break;
9537 case AXIS_PARENT:
9538#ifdef DEBUG_STEP_NTH
9539 xmlGenericError(xmlGenericErrorContext, "axis 'parent' ");
9540#endif
9541 first = NULL;
9542 next = xmlXPathNextParent;
9543 break;
9544 case AXIS_PRECEDING:
9545#ifdef DEBUG_STEP_NTH
9546 xmlGenericError(xmlGenericErrorContext, "axis 'preceding' ");
9547#endif
9548 first = NULL;
9549 next = xmlXPathNextPrecedingInternal;
9550 break;
9551 case AXIS_PRECEDING_SIBLING:
9552#ifdef DEBUG_STEP_NTH
9553 xmlGenericError(xmlGenericErrorContext,
9554 "axis 'preceding-sibling' ");
9555#endif
9556 first = NULL;
9557 next = xmlXPathNextPrecedingSibling;
9558 break;
9559 case AXIS_SELF:
9560#ifdef DEBUG_STEP_NTH
9561 xmlGenericError(xmlGenericErrorContext, "axis 'self' ");
9562#endif
9563 first = NULL;
9564 last = NULL;
9565 next = xmlXPathNextSelf;
9566 break;
9567 }
9568 if (next == NULL)
9569 return(0);
9570
9571 nodelist = obj->nodesetval;
9572 if (nodelist == NULL) {
9573 xmlXPathFreeObject(obj);
9574 valuePush(ctxt, xmlXPathWrapNodeSet(NULL));
9575 return(0);
9576 }
9577 addNode = xmlXPathNodeSetAddUnique;
9578#ifdef DEBUG_STEP_NTH
9579 xmlGenericError(xmlGenericErrorContext,
9580 " context contains %d nodes\n", nodelist->nodeNr);
9581 switch (test) {
9582 case NODE_TEST_NONE:
9583 xmlGenericError(xmlGenericErrorContext,
9584 " searching for none !!!\n");
9585 break;
9586 case NODE_TEST_TYPE:
9587 xmlGenericError(xmlGenericErrorContext,
9588 " searching for type %d\n", type);
9589 break;
9590 case NODE_TEST_PI:
9591 xmlGenericError(xmlGenericErrorContext,
9592 " searching for PI !!!\n");
9593 break;
9594 case NODE_TEST_ALL:
9595 xmlGenericError(xmlGenericErrorContext,
9596 " searching for *\n");
9597 break;
9598 case NODE_TEST_NS:
9599 xmlGenericError(xmlGenericErrorContext,
9600 " searching for namespace %s\n",
9601 prefix);
9602 break;
9603 case NODE_TEST_NAME:
9604 xmlGenericError(xmlGenericErrorContext,
9605 " searching for name %s\n", name);
9606 if (prefix != NULL)
9607 xmlGenericError(xmlGenericErrorContext,
9608 " with namespace %s\n", prefix);
9609 break;
9610 }
9611 xmlGenericError(xmlGenericErrorContext, "Testing : ");
9612#endif
9613 /*
9614 * 2.3 Node Tests
9615 * - For the attribute axis, the principal node type is attribute.
9616 * - For the namespace axis, the principal node type is namespace.
9617 * - For other axes, the principal node type is element.
9618 *
9619 * A node test * is true for any node of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009620 * principal node type. For example, child::* will
Daniel Veillardf06307e2001-07-03 10:35:50 +00009621 * select all element children of the context node
9622 */
9623 tmp = ctxt->context->node;
9624 list = xmlXPathNodeSetCreate(NULL);
9625 for (i = 0; i < nodelist->nodeNr; i++) {
9626 ctxt->context->node = nodelist->nodeTab[i];
9627
9628 cur = NULL;
9629 n = 0;
9630 do {
9631 cur = next(ctxt, cur);
9632 if (cur == NULL)
9633 break;
9634 if ((first != NULL) && (*first == cur))
9635 break;
9636 if (((t % 256) == 0) &&
9637 (first != NULL) && (*first != NULL) &&
9638 (xmlXPathCmpNodes(*first, cur) >= 0))
9639 break;
9640 if ((last != NULL) && (*last == cur))
9641 break;
9642 if (((t % 256) == 0) &&
9643 (last != NULL) && (*last != NULL) &&
9644 (xmlXPathCmpNodes(cur, *last) >= 0))
9645 break;
9646 t++;
9647 switch (test) {
9648 case NODE_TEST_NONE:
9649 ctxt->context->node = tmp;
9650 STRANGE return(0);
9651 case NODE_TEST_TYPE:
9652 if ((cur->type == type) ||
9653 ((type == NODE_TYPE_NODE) &&
9654 ((cur->type == XML_DOCUMENT_NODE) ||
9655 (cur->type == XML_HTML_DOCUMENT_NODE) ||
9656 (cur->type == XML_ELEMENT_NODE) ||
9657 (cur->type == XML_PI_NODE) ||
9658 (cur->type == XML_COMMENT_NODE) ||
9659 (cur->type == XML_CDATA_SECTION_NODE) ||
Daniel Veillard8606bbb2002-11-12 12:36:52 +00009660 (cur->type == XML_TEXT_NODE))) ||
9661 ((type == NODE_TYPE_TEXT) &&
9662 (cur->type == XML_CDATA_SECTION_NODE))) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009663 n++;
9664 if (n == indx)
9665 addNode(list, cur);
9666 }
9667 break;
9668 case NODE_TEST_PI:
9669 if (cur->type == XML_PI_NODE) {
9670 if ((name != NULL) &&
9671 (!xmlStrEqual(name, cur->name)))
9672 break;
9673 n++;
9674 if (n == indx)
9675 addNode(list, cur);
9676 }
9677 break;
9678 case NODE_TEST_ALL:
9679 if (axis == AXIS_ATTRIBUTE) {
9680 if (cur->type == XML_ATTRIBUTE_NODE) {
9681 n++;
9682 if (n == indx)
9683 addNode(list, cur);
9684 }
9685 } else if (axis == AXIS_NAMESPACE) {
9686 if (cur->type == XML_NAMESPACE_DECL) {
9687 n++;
9688 if (n == indx)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009689 xmlXPathNodeSetAddNs(list, ctxt->context->node,
9690 (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009691 }
9692 } else {
9693 if (cur->type == XML_ELEMENT_NODE) {
9694 if (prefix == NULL) {
9695 n++;
9696 if (n == indx)
9697 addNode(list, cur);
9698 } else if ((cur->ns != NULL) &&
9699 (xmlStrEqual(URI, cur->ns->href))) {
9700 n++;
9701 if (n == indx)
9702 addNode(list, cur);
9703 }
9704 }
9705 }
9706 break;
9707 case NODE_TEST_NS:{
9708 TODO;
9709 break;
9710 }
9711 case NODE_TEST_NAME:
9712 switch (cur->type) {
9713 case XML_ELEMENT_NODE:
9714 if (xmlStrEqual(name, cur->name)) {
9715 if (prefix == NULL) {
9716 if (cur->ns == NULL) {
9717 n++;
9718 if (n == indx)
9719 addNode(list, cur);
9720 }
9721 } else {
9722 if ((cur->ns != NULL) &&
9723 (xmlStrEqual(URI,
9724 cur->ns->href))) {
9725 n++;
9726 if (n == indx)
9727 addNode(list, cur);
9728 }
9729 }
9730 }
9731 break;
9732 case XML_ATTRIBUTE_NODE:{
9733 xmlAttrPtr attr = (xmlAttrPtr) cur;
9734
9735 if (xmlStrEqual(name, attr->name)) {
9736 if (prefix == NULL) {
9737 if ((attr->ns == NULL) ||
9738 (attr->ns->prefix == NULL)) {
9739 n++;
9740 if (n == indx)
9741 addNode(list, cur);
9742 }
9743 } else {
9744 if ((attr->ns != NULL) &&
9745 (xmlStrEqual(URI,
9746 attr->ns->
9747 href))) {
9748 n++;
9749 if (n == indx)
9750 addNode(list, cur);
9751 }
9752 }
9753 }
9754 break;
9755 }
9756 case XML_NAMESPACE_DECL:
9757 if (cur->type == XML_NAMESPACE_DECL) {
9758 xmlNsPtr ns = (xmlNsPtr) cur;
9759
9760 if ((ns->prefix != NULL) && (name != NULL)
9761 && (xmlStrEqual(ns->prefix, name))) {
9762 n++;
9763 if (n == indx)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009764 xmlXPathNodeSetAddNs(list,
9765 ctxt->context->node, (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009766 }
9767 }
9768 break;
9769 default:
9770 break;
9771 }
9772 break;
9773 break;
9774 }
9775 } while (n < indx);
9776 }
9777 ctxt->context->node = tmp;
9778#ifdef DEBUG_STEP_NTH
9779 xmlGenericError(xmlGenericErrorContext,
9780 "\nExamined %d nodes, found %d nodes at that step\n",
9781 t, list->nodeNr);
9782#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009783 valuePush(ctxt, xmlXPathWrapNodeSet(list));
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00009784 if ((obj->boolval) && (obj->user != NULL)) {
9785 ctxt->value->boolval = 1;
9786 ctxt->value->user = obj->user;
9787 obj->user = NULL;
9788 obj->boolval = 0;
9789 }
9790 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009791 return(t);
9792}
9793
9794/**
9795 * xmlXPathCompOpEvalFirst:
9796 * @ctxt: the XPath parser context with the compiled expression
9797 * @op: an XPath compiled operation
9798 * @first: the first elem found so far
9799 *
9800 * Evaluate the Precompiled XPath operation searching only the first
9801 * element in document order
9802 *
9803 * Returns the number of examined objects.
9804 */
9805static int
9806xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt,
9807 xmlXPathStepOpPtr op, xmlNodePtr * first)
9808{
9809 int total = 0, cur;
9810 xmlXPathCompExprPtr comp;
9811 xmlXPathObjectPtr arg1, arg2;
9812
Daniel Veillard556c6682001-10-06 09:59:51 +00009813 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009814 comp = ctxt->comp;
9815 switch (op->op) {
9816 case XPATH_OP_END:
9817 return (0);
9818 case XPATH_OP_UNION:
9819 total =
9820 xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1],
9821 first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009822 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009823 if ((ctxt->value != NULL)
9824 && (ctxt->value->type == XPATH_NODESET)
9825 && (ctxt->value->nodesetval != NULL)
9826 && (ctxt->value->nodesetval->nodeNr >= 1)) {
9827 /*
9828 * limit tree traversing to first node in the result
9829 */
9830 xmlXPathNodeSetSort(ctxt->value->nodesetval);
9831 *first = ctxt->value->nodesetval->nodeTab[0];
9832 }
9833 cur =
9834 xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2],
9835 first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009836 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009837 CHECK_TYPE0(XPATH_NODESET);
9838 arg2 = valuePop(ctxt);
9839
9840 CHECK_TYPE0(XPATH_NODESET);
9841 arg1 = valuePop(ctxt);
9842
9843 arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
9844 arg2->nodesetval);
9845 valuePush(ctxt, arg1);
9846 xmlXPathFreeObject(arg2);
9847 /* optimizer */
9848 if (total > cur)
9849 xmlXPathCompSwap(op);
9850 return (total + cur);
9851 case XPATH_OP_ROOT:
9852 xmlXPathRoot(ctxt);
9853 return (0);
9854 case XPATH_OP_NODE:
9855 if (op->ch1 != -1)
9856 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009857 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009858 if (op->ch2 != -1)
9859 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009860 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009861 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
9862 return (total);
9863 case XPATH_OP_RESET:
9864 if (op->ch1 != -1)
9865 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009866 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009867 if (op->ch2 != -1)
9868 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009869 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009870 ctxt->context->node = NULL;
9871 return (total);
9872 case XPATH_OP_COLLECT:{
9873 if (op->ch1 == -1)
9874 return (total);
9875
9876 total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009877 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009878
9879 /*
9880 * Optimization for [n] selection where n is a number
9881 */
9882 if ((op->ch2 != -1) &&
9883 (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) &&
9884 (comp->steps[op->ch2].ch1 == -1) &&
9885 (comp->steps[op->ch2].ch2 != -1) &&
9886 (comp->steps[comp->steps[op->ch2].ch2].op ==
9887 XPATH_OP_VALUE)) {
9888 xmlXPathObjectPtr val;
9889
9890 val = comp->steps[comp->steps[op->ch2].ch2].value4;
9891 if ((val != NULL) && (val->type == XPATH_NUMBER)) {
9892 int indx = (int) val->floatval;
9893
9894 if (val->floatval == (float) indx) {
9895 xmlXPathNodeCollectAndTestNth(ctxt, op, indx,
9896 first, NULL);
9897 return (total);
9898 }
9899 }
9900 }
9901 total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL);
9902 return (total);
9903 }
9904 case XPATH_OP_VALUE:
9905 valuePush(ctxt,
9906 xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4));
9907 return (0);
9908 case XPATH_OP_SORT:
9909 if (op->ch1 != -1)
9910 total +=
9911 xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1],
9912 first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009913 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009914 if ((ctxt->value != NULL)
9915 && (ctxt->value->type == XPATH_NODESET)
9916 && (ctxt->value->nodesetval != NULL))
9917 xmlXPathNodeSetSort(ctxt->value->nodesetval);
9918 return (total);
9919 default:
9920 return (xmlXPathCompOpEval(ctxt, op));
9921 }
9922}
9923
9924/**
9925 * xmlXPathCompOpEvalLast:
9926 * @ctxt: the XPath parser context with the compiled expression
9927 * @op: an XPath compiled operation
9928 * @last: the last elem found so far
9929 *
9930 * Evaluate the Precompiled XPath operation searching only the last
9931 * element in document order
9932 *
William M. Brack08171912003-12-29 02:52:11 +00009933 * Returns the number of nodes traversed
Daniel Veillardf06307e2001-07-03 10:35:50 +00009934 */
9935static int
9936xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op,
9937 xmlNodePtr * last)
9938{
9939 int total = 0, cur;
9940 xmlXPathCompExprPtr comp;
9941 xmlXPathObjectPtr arg1, arg2;
William M. Brackce4fc562004-01-22 02:47:18 +00009942 xmlNodePtr bak;
9943 xmlDocPtr bakd;
9944 int pp;
9945 int cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009946
Daniel Veillard556c6682001-10-06 09:59:51 +00009947 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009948 comp = ctxt->comp;
9949 switch (op->op) {
9950 case XPATH_OP_END:
9951 return (0);
9952 case XPATH_OP_UNION:
William M. Brackce4fc562004-01-22 02:47:18 +00009953 bakd = ctxt->context->doc;
9954 bak = ctxt->context->node;
9955 pp = ctxt->context->proximityPosition;
9956 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009957 total =
9958 xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last);
Daniel Veillard556c6682001-10-06 09:59:51 +00009959 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009960 if ((ctxt->value != NULL)
9961 && (ctxt->value->type == XPATH_NODESET)
9962 && (ctxt->value->nodesetval != NULL)
9963 && (ctxt->value->nodesetval->nodeNr >= 1)) {
9964 /*
9965 * limit tree traversing to first node in the result
9966 */
9967 xmlXPathNodeSetSort(ctxt->value->nodesetval);
9968 *last =
9969 ctxt->value->nodesetval->nodeTab[ctxt->value->
9970 nodesetval->nodeNr -
9971 1];
9972 }
William M. Brackce4fc562004-01-22 02:47:18 +00009973 ctxt->context->doc = bakd;
9974 ctxt->context->node = bak;
9975 ctxt->context->proximityPosition = pp;
9976 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009977 cur =
9978 xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last);
Daniel Veillard556c6682001-10-06 09:59:51 +00009979 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009980 if ((ctxt->value != NULL)
9981 && (ctxt->value->type == XPATH_NODESET)
9982 && (ctxt->value->nodesetval != NULL)
9983 && (ctxt->value->nodesetval->nodeNr >= 1)) {
9984 }
9985 CHECK_TYPE0(XPATH_NODESET);
9986 arg2 = valuePop(ctxt);
9987
9988 CHECK_TYPE0(XPATH_NODESET);
9989 arg1 = valuePop(ctxt);
9990
9991 arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
9992 arg2->nodesetval);
9993 valuePush(ctxt, arg1);
9994 xmlXPathFreeObject(arg2);
9995 /* optimizer */
9996 if (total > cur)
9997 xmlXPathCompSwap(op);
9998 return (total + cur);
9999 case XPATH_OP_ROOT:
10000 xmlXPathRoot(ctxt);
10001 return (0);
10002 case XPATH_OP_NODE:
10003 if (op->ch1 != -1)
10004 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010005 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010006 if (op->ch2 != -1)
10007 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010008 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010009 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
10010 return (total);
10011 case XPATH_OP_RESET:
10012 if (op->ch1 != -1)
10013 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010014 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010015 if (op->ch2 != -1)
10016 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010017 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010018 ctxt->context->node = NULL;
10019 return (total);
10020 case XPATH_OP_COLLECT:{
10021 if (op->ch1 == -1)
10022 return (0);
10023
10024 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010025 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010026
10027 /*
10028 * Optimization for [n] selection where n is a number
10029 */
10030 if ((op->ch2 != -1) &&
10031 (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) &&
10032 (comp->steps[op->ch2].ch1 == -1) &&
10033 (comp->steps[op->ch2].ch2 != -1) &&
10034 (comp->steps[comp->steps[op->ch2].ch2].op ==
10035 XPATH_OP_VALUE)) {
10036 xmlXPathObjectPtr val;
10037
10038 val = comp->steps[comp->steps[op->ch2].ch2].value4;
10039 if ((val != NULL) && (val->type == XPATH_NUMBER)) {
10040 int indx = (int) val->floatval;
10041
10042 if (val->floatval == (float) indx) {
10043 total +=
10044 xmlXPathNodeCollectAndTestNth(ctxt, op,
10045 indx, NULL,
10046 last);
10047 return (total);
10048 }
10049 }
10050 }
10051 total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last);
10052 return (total);
10053 }
10054 case XPATH_OP_VALUE:
10055 valuePush(ctxt,
10056 xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4));
10057 return (0);
10058 case XPATH_OP_SORT:
10059 if (op->ch1 != -1)
10060 total +=
10061 xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1],
10062 last);
Daniel Veillard556c6682001-10-06 09:59:51 +000010063 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010064 if ((ctxt->value != NULL)
10065 && (ctxt->value->type == XPATH_NODESET)
10066 && (ctxt->value->nodesetval != NULL))
10067 xmlXPathNodeSetSort(ctxt->value->nodesetval);
10068 return (total);
10069 default:
10070 return (xmlXPathCompOpEval(ctxt, op));
10071 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +000010072}
10073
Owen Taylor3473f882001-02-23 17:55:21 +000010074/**
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010075 * xmlXPathCompOpEval:
10076 * @ctxt: the XPath parser context with the compiled expression
10077 * @op: an XPath compiled operation
10078 *
10079 * Evaluate the Precompiled XPath operation
William M. Brack08171912003-12-29 02:52:11 +000010080 * Returns the number of nodes traversed
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010081 */
Daniel Veillardf06307e2001-07-03 10:35:50 +000010082static int
10083xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op)
10084{
10085 int total = 0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010086 int equal, ret;
10087 xmlXPathCompExprPtr comp;
10088 xmlXPathObjectPtr arg1, arg2;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010089 xmlNodePtr bak;
10090 xmlDocPtr bakd;
William M. Brack6000af52002-06-28 11:43:13 +000010091 int pp;
William M. Brack692092b2002-06-28 15:01:24 +000010092 int cs;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010093
Daniel Veillard556c6682001-10-06 09:59:51 +000010094 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010095 comp = ctxt->comp;
10096 switch (op->op) {
Daniel Veillardf06307e2001-07-03 10:35:50 +000010097 case XPATH_OP_END:
10098 return (0);
10099 case XPATH_OP_AND:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010100 bakd = ctxt->context->doc;
10101 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010102 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010103 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010104 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010105 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010106 xmlXPathBooleanFunction(ctxt, 1);
10107 if ((ctxt->value == NULL) || (ctxt->value->boolval == 0))
10108 return (total);
10109 arg2 = valuePop(ctxt);
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010110 ctxt->context->doc = bakd;
10111 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010112 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010113 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010114 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010115 if (ctxt->error) {
10116 xmlXPathFreeObject(arg2);
10117 return(0);
10118 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010119 xmlXPathBooleanFunction(ctxt, 1);
10120 arg1 = valuePop(ctxt);
10121 arg1->boolval &= arg2->boolval;
10122 valuePush(ctxt, arg1);
10123 xmlXPathFreeObject(arg2);
10124 return (total);
10125 case XPATH_OP_OR:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010126 bakd = ctxt->context->doc;
10127 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010128 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010129 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010130 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010131 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010132 xmlXPathBooleanFunction(ctxt, 1);
10133 if ((ctxt->value == NULL) || (ctxt->value->boolval == 1))
10134 return (total);
10135 arg2 = valuePop(ctxt);
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010136 ctxt->context->doc = bakd;
10137 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010138 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010139 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010140 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010141 if (ctxt->error) {
10142 xmlXPathFreeObject(arg2);
10143 return(0);
10144 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010145 xmlXPathBooleanFunction(ctxt, 1);
10146 arg1 = valuePop(ctxt);
10147 arg1->boolval |= arg2->boolval;
10148 valuePush(ctxt, arg1);
10149 xmlXPathFreeObject(arg2);
10150 return (total);
10151 case XPATH_OP_EQUAL:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010152 bakd = ctxt->context->doc;
10153 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010154 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010155 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010156 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010157 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010158 ctxt->context->doc = bakd;
10159 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010160 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010161 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010162 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010163 CHECK_ERROR0;
William M. Brack0c022ad2002-07-12 00:56:01 +000010164 if (op->value)
10165 equal = xmlXPathEqualValues(ctxt);
10166 else
10167 equal = xmlXPathNotEqualValues(ctxt);
10168 valuePush(ctxt, xmlXPathNewBoolean(equal));
Daniel Veillardf06307e2001-07-03 10:35:50 +000010169 return (total);
10170 case XPATH_OP_CMP:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010171 bakd = ctxt->context->doc;
10172 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010173 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010174 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010175 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010176 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010177 ctxt->context->doc = bakd;
10178 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010179 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010180 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010181 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010182 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010183 ret = xmlXPathCompareValues(ctxt, op->value, op->value2);
10184 valuePush(ctxt, xmlXPathNewBoolean(ret));
10185 return (total);
10186 case XPATH_OP_PLUS:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010187 bakd = ctxt->context->doc;
10188 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010189 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010190 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010191 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010192 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010193 if (op->ch2 != -1) {
10194 ctxt->context->doc = bakd;
10195 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010196 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010197 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010198 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010199 }
Daniel Veillard556c6682001-10-06 09:59:51 +000010200 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010201 if (op->value == 0)
10202 xmlXPathSubValues(ctxt);
10203 else if (op->value == 1)
10204 xmlXPathAddValues(ctxt);
10205 else if (op->value == 2)
10206 xmlXPathValueFlipSign(ctxt);
10207 else if (op->value == 3) {
10208 CAST_TO_NUMBER;
10209 CHECK_TYPE0(XPATH_NUMBER);
10210 }
10211 return (total);
10212 case XPATH_OP_MULT:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010213 bakd = ctxt->context->doc;
10214 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010215 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010216 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010217 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010218 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010219 ctxt->context->doc = bakd;
10220 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010221 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010222 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010223 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010224 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010225 if (op->value == 0)
10226 xmlXPathMultValues(ctxt);
10227 else if (op->value == 1)
10228 xmlXPathDivValues(ctxt);
10229 else if (op->value == 2)
10230 xmlXPathModValues(ctxt);
10231 return (total);
10232 case XPATH_OP_UNION:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010233 bakd = ctxt->context->doc;
10234 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010235 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010236 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010237 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010238 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010239 ctxt->context->doc = bakd;
10240 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010241 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010242 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010243 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010244 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010245 CHECK_TYPE0(XPATH_NODESET);
10246 arg2 = valuePop(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010247
Daniel Veillardf06307e2001-07-03 10:35:50 +000010248 CHECK_TYPE0(XPATH_NODESET);
10249 arg1 = valuePop(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010250
Daniel Veillardf06307e2001-07-03 10:35:50 +000010251 arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
10252 arg2->nodesetval);
10253 valuePush(ctxt, arg1);
10254 xmlXPathFreeObject(arg2);
10255 return (total);
10256 case XPATH_OP_ROOT:
10257 xmlXPathRoot(ctxt);
10258 return (total);
10259 case XPATH_OP_NODE:
10260 if (op->ch1 != -1)
10261 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010262 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010263 if (op->ch2 != -1)
10264 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010265 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010266 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
10267 return (total);
10268 case XPATH_OP_RESET:
10269 if (op->ch1 != -1)
10270 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010271 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010272 if (op->ch2 != -1)
10273 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010274 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010275 ctxt->context->node = NULL;
10276 return (total);
10277 case XPATH_OP_COLLECT:{
10278 if (op->ch1 == -1)
10279 return (total);
Daniel Veillardd8df6c02001-04-05 16:54:14 +000010280
Daniel Veillardf06307e2001-07-03 10:35:50 +000010281 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010282 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010283
Daniel Veillardf06307e2001-07-03 10:35:50 +000010284 /*
10285 * Optimization for [n] selection where n is a number
10286 */
10287 if ((op->ch2 != -1) &&
10288 (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) &&
10289 (comp->steps[op->ch2].ch1 == -1) &&
10290 (comp->steps[op->ch2].ch2 != -1) &&
10291 (comp->steps[comp->steps[op->ch2].ch2].op ==
10292 XPATH_OP_VALUE)) {
10293 xmlXPathObjectPtr val;
Daniel Veillard42596ad2001-05-22 16:57:14 +000010294
Daniel Veillardf06307e2001-07-03 10:35:50 +000010295 val = comp->steps[comp->steps[op->ch2].ch2].value4;
10296 if ((val != NULL) && (val->type == XPATH_NUMBER)) {
10297 int indx = (int) val->floatval;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010298
Daniel Veillardf06307e2001-07-03 10:35:50 +000010299 if (val->floatval == (float) indx) {
10300 total +=
10301 xmlXPathNodeCollectAndTestNth(ctxt, op,
10302 indx, NULL,
10303 NULL);
10304 return (total);
10305 }
10306 }
10307 }
10308 total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL);
10309 return (total);
10310 }
10311 case XPATH_OP_VALUE:
10312 valuePush(ctxt,
10313 xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4));
10314 return (total);
10315 case XPATH_OP_VARIABLE:{
Daniel Veillard556c6682001-10-06 09:59:51 +000010316 xmlXPathObjectPtr val;
10317
Daniel Veillardf06307e2001-07-03 10:35:50 +000010318 if (op->ch1 != -1)
10319 total +=
10320 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010321 if (op->value5 == NULL) {
10322 val = xmlXPathVariableLookup(ctxt->context, op->value4);
10323 if (val == NULL) {
10324 ctxt->error = XPATH_UNDEF_VARIABLE_ERROR;
10325 return(0);
10326 }
10327 valuePush(ctxt, val);
10328 } else {
Daniel Veillardf06307e2001-07-03 10:35:50 +000010329 const xmlChar *URI;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010330
Daniel Veillardf06307e2001-07-03 10:35:50 +000010331 URI = xmlXPathNsLookup(ctxt->context, op->value5);
10332 if (URI == NULL) {
10333 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010334 "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n",
Daniel Veillardf06307e2001-07-03 10:35:50 +000010335 op->value4, op->value5);
10336 return (total);
10337 }
Daniel Veillard556c6682001-10-06 09:59:51 +000010338 val = xmlXPathVariableLookupNS(ctxt->context,
10339 op->value4, URI);
10340 if (val == NULL) {
10341 ctxt->error = XPATH_UNDEF_VARIABLE_ERROR;
10342 return(0);
10343 }
10344 valuePush(ctxt, val);
Daniel Veillardf06307e2001-07-03 10:35:50 +000010345 }
10346 return (total);
10347 }
10348 case XPATH_OP_FUNCTION:{
10349 xmlXPathFunction func;
10350 const xmlChar *oldFunc, *oldFuncURI;
Daniel Veillard556c6682001-10-06 09:59:51 +000010351 int i;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010352
10353 if (op->ch1 != -1)
10354 total +=
10355 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010356 if (ctxt->valueNr < op->value) {
10357 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010358 "xmlXPathCompOpEval: parameter error\n");
Daniel Veillard556c6682001-10-06 09:59:51 +000010359 ctxt->error = XPATH_INVALID_OPERAND;
10360 return (total);
10361 }
10362 for (i = 0; i < op->value; i++)
10363 if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) {
10364 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010365 "xmlXPathCompOpEval: parameter error\n");
Daniel Veillard556c6682001-10-06 09:59:51 +000010366 ctxt->error = XPATH_INVALID_OPERAND;
10367 return (total);
10368 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010369 if (op->cache != NULL)
10370 func = (xmlXPathFunction) op->cache;
10371 else {
10372 const xmlChar *URI = NULL;
10373
10374 if (op->value5 == NULL)
10375 func =
10376 xmlXPathFunctionLookup(ctxt->context,
10377 op->value4);
10378 else {
10379 URI = xmlXPathNsLookup(ctxt->context, op->value5);
10380 if (URI == NULL) {
10381 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010382 "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n",
Daniel Veillardf06307e2001-07-03 10:35:50 +000010383 op->value4, op->value5);
10384 return (total);
10385 }
10386 func = xmlXPathFunctionLookupNS(ctxt->context,
10387 op->value4, URI);
10388 }
10389 if (func == NULL) {
10390 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010391 "xmlXPathCompOpEval: function %s not found\n",
Daniel Veillardf06307e2001-07-03 10:35:50 +000010392 op->value4);
10393 XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR);
Daniel Veillardf06307e2001-07-03 10:35:50 +000010394 }
10395 op->cache = (void *) func;
10396 op->cacheURI = (void *) URI;
10397 }
10398 oldFunc = ctxt->context->function;
10399 oldFuncURI = ctxt->context->functionURI;
10400 ctxt->context->function = op->value4;
10401 ctxt->context->functionURI = op->cacheURI;
10402 func(ctxt, op->value);
10403 ctxt->context->function = oldFunc;
10404 ctxt->context->functionURI = oldFuncURI;
10405 return (total);
10406 }
10407 case XPATH_OP_ARG:
Daniel Veillard088bf112002-05-14 11:03:59 +000010408 bakd = ctxt->context->doc;
10409 bak = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010410 if (op->ch1 != -1)
10411 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard088bf112002-05-14 11:03:59 +000010412 ctxt->context->doc = bakd;
10413 ctxt->context->node = bak;
Daniel Veillard556c6682001-10-06 09:59:51 +000010414 CHECK_ERROR0;
William M. Brack72ee48d2003-12-30 08:30:19 +000010415 if (op->ch2 != -1) {
Daniel Veillardf06307e2001-07-03 10:35:50 +000010416 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
William M. Brack72ee48d2003-12-30 08:30:19 +000010417 ctxt->context->doc = bakd;
10418 ctxt->context->node = bak;
10419 CHECK_ERROR0;
10420 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010421 return (total);
10422 case XPATH_OP_PREDICATE:
10423 case XPATH_OP_FILTER:{
10424 xmlXPathObjectPtr res;
10425 xmlXPathObjectPtr obj, tmp;
10426 xmlNodeSetPtr newset = NULL;
10427 xmlNodeSetPtr oldset;
10428 xmlNodePtr oldnode;
William M. Brack3794b9e2004-07-13 15:06:20 +000010429 xmlDocPtr oldDoc;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010430 int i;
10431
10432 /*
10433 * Optimization for ()[1] selection i.e. the first elem
10434 */
10435 if ((op->ch1 != -1) && (op->ch2 != -1) &&
10436 (comp->steps[op->ch1].op == XPATH_OP_SORT) &&
10437 (comp->steps[op->ch2].op == XPATH_OP_VALUE)) {
10438 xmlXPathObjectPtr val;
10439
10440 val = comp->steps[op->ch2].value4;
10441 if ((val != NULL) && (val->type == XPATH_NUMBER) &&
10442 (val->floatval == 1.0)) {
10443 xmlNodePtr first = NULL;
10444
10445 total +=
10446 xmlXPathCompOpEvalFirst(ctxt,
10447 &comp->steps[op->ch1],
10448 &first);
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 first value
10453 */
10454 if ((ctxt->value != NULL) &&
10455 (ctxt->value->type == XPATH_NODESET) &&
10456 (ctxt->value->nodesetval != NULL) &&
10457 (ctxt->value->nodesetval->nodeNr > 1))
10458 ctxt->value->nodesetval->nodeNr = 1;
10459 return (total);
10460 }
10461 }
10462 /*
10463 * Optimization for ()[last()] selection i.e. the last elem
10464 */
10465 if ((op->ch1 != -1) && (op->ch2 != -1) &&
10466 (comp->steps[op->ch1].op == XPATH_OP_SORT) &&
10467 (comp->steps[op->ch2].op == XPATH_OP_SORT)) {
10468 int f = comp->steps[op->ch2].ch1;
10469
10470 if ((f != -1) &&
10471 (comp->steps[f].op == XPATH_OP_FUNCTION) &&
10472 (comp->steps[f].value5 == NULL) &&
10473 (comp->steps[f].value == 0) &&
10474 (comp->steps[f].value4 != NULL) &&
10475 (xmlStrEqual
10476 (comp->steps[f].value4, BAD_CAST "last"))) {
10477 xmlNodePtr last = NULL;
10478
10479 total +=
10480 xmlXPathCompOpEvalLast(ctxt,
10481 &comp->steps[op->ch1],
10482 &last);
Daniel Veillard556c6682001-10-06 09:59:51 +000010483 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010484 /*
10485 * The nodeset should be in document order,
10486 * Keep only the last value
10487 */
10488 if ((ctxt->value != NULL) &&
10489 (ctxt->value->type == XPATH_NODESET) &&
10490 (ctxt->value->nodesetval != NULL) &&
10491 (ctxt->value->nodesetval->nodeTab != NULL) &&
10492 (ctxt->value->nodesetval->nodeNr > 1)) {
10493 ctxt->value->nodesetval->nodeTab[0] =
10494 ctxt->value->nodesetval->nodeTab[ctxt->
10495 value->
10496 nodesetval->
10497 nodeNr -
10498 1];
10499 ctxt->value->nodesetval->nodeNr = 1;
10500 }
10501 return (total);
10502 }
10503 }
10504
10505 if (op->ch1 != -1)
10506 total +=
10507 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010508 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010509 if (op->ch2 == -1)
10510 return (total);
10511 if (ctxt->value == NULL)
10512 return (total);
10513
10514 oldnode = ctxt->context->node;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010515
10516#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardf06307e2001-07-03 10:35:50 +000010517 /*
10518 * Hum are we filtering the result of an XPointer expression
10519 */
10520 if (ctxt->value->type == XPATH_LOCATIONSET) {
10521 xmlLocationSetPtr newlocset = NULL;
10522 xmlLocationSetPtr oldlocset;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010523
Daniel Veillardf06307e2001-07-03 10:35:50 +000010524 /*
10525 * Extract the old locset, and then evaluate the result of the
10526 * expression for all the element in the locset. use it to grow
10527 * up a new locset.
10528 */
10529 CHECK_TYPE0(XPATH_LOCATIONSET);
10530 obj = valuePop(ctxt);
10531 oldlocset = obj->user;
10532 ctxt->context->node = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010533
Daniel Veillardf06307e2001-07-03 10:35:50 +000010534 if ((oldlocset == NULL) || (oldlocset->locNr == 0)) {
10535 ctxt->context->contextSize = 0;
10536 ctxt->context->proximityPosition = 0;
10537 if (op->ch2 != -1)
10538 total +=
10539 xmlXPathCompOpEval(ctxt,
10540 &comp->steps[op->ch2]);
10541 res = valuePop(ctxt);
10542 if (res != NULL)
10543 xmlXPathFreeObject(res);
10544 valuePush(ctxt, obj);
10545 CHECK_ERROR0;
10546 return (total);
10547 }
10548 newlocset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010549
Daniel Veillardf06307e2001-07-03 10:35:50 +000010550 for (i = 0; i < oldlocset->locNr; i++) {
10551 /*
10552 * Run the evaluation with a node list made of a
10553 * single item in the nodelocset.
10554 */
10555 ctxt->context->node = oldlocset->locTab[i]->user;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010556 ctxt->context->contextSize = oldlocset->locNr;
10557 ctxt->context->proximityPosition = i + 1;
William M. Brackf7eb7942003-12-31 07:59:17 +000010558 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10559 valuePush(ctxt, tmp);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010560
Daniel Veillardf06307e2001-07-03 10:35:50 +000010561 if (op->ch2 != -1)
10562 total +=
10563 xmlXPathCompOpEval(ctxt,
10564 &comp->steps[op->ch2]);
10565 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010566
Daniel Veillardf06307e2001-07-03 10:35:50 +000010567 /*
10568 * The result of the evaluation need to be tested to
10569 * decided whether the filter succeeded or not
10570 */
10571 res = valuePop(ctxt);
10572 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
10573 xmlXPtrLocationSetAdd(newlocset,
10574 xmlXPathObjectCopy
10575 (oldlocset->locTab[i]));
10576 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010577
Daniel Veillardf06307e2001-07-03 10:35:50 +000010578 /*
10579 * Cleanup
10580 */
10581 if (res != NULL)
10582 xmlXPathFreeObject(res);
10583 if (ctxt->value == tmp) {
10584 res = valuePop(ctxt);
10585 xmlXPathFreeObject(res);
10586 }
10587
10588 ctxt->context->node = NULL;
10589 }
10590
10591 /*
10592 * The result is used as the new evaluation locset.
10593 */
10594 xmlXPathFreeObject(obj);
10595 ctxt->context->node = NULL;
10596 ctxt->context->contextSize = -1;
10597 ctxt->context->proximityPosition = -1;
10598 valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset));
10599 ctxt->context->node = oldnode;
10600 return (total);
10601 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010602#endif /* LIBXML_XPTR_ENABLED */
10603
Daniel Veillardf06307e2001-07-03 10:35:50 +000010604 /*
10605 * Extract the old set, and then evaluate the result of the
10606 * expression for all the element in the set. use it to grow
10607 * up a new set.
10608 */
10609 CHECK_TYPE0(XPATH_NODESET);
10610 obj = valuePop(ctxt);
10611 oldset = obj->nodesetval;
Daniel Veillard911f49a2001-04-07 15:39:35 +000010612
Daniel Veillardf06307e2001-07-03 10:35:50 +000010613 oldnode = ctxt->context->node;
William M. Brack3794b9e2004-07-13 15:06:20 +000010614 oldDoc = ctxt->context->doc;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010615 ctxt->context->node = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010616
Daniel Veillardf06307e2001-07-03 10:35:50 +000010617 if ((oldset == NULL) || (oldset->nodeNr == 0)) {
10618 ctxt->context->contextSize = 0;
10619 ctxt->context->proximityPosition = 0;
William M. Brack8fad8bf2004-06-02 08:26:25 +000010620/*
Daniel Veillardf06307e2001-07-03 10:35:50 +000010621 if (op->ch2 != -1)
10622 total +=
10623 xmlXPathCompOpEval(ctxt,
10624 &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010625 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010626 res = valuePop(ctxt);
10627 if (res != NULL)
10628 xmlXPathFreeObject(res);
William M. Brack8fad8bf2004-06-02 08:26:25 +000010629*/
Daniel Veillardf06307e2001-07-03 10:35:50 +000010630 valuePush(ctxt, obj);
10631 ctxt->context->node = oldnode;
10632 CHECK_ERROR0;
10633 } else {
10634 /*
10635 * Initialize the new set.
William M. Brack3794b9e2004-07-13 15:06:20 +000010636 * Also set the xpath document in case things like
10637 * key() evaluation are attempted on the predicate
Daniel Veillardf06307e2001-07-03 10:35:50 +000010638 */
10639 newset = xmlXPathNodeSetCreate(NULL);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010640
Daniel Veillardf06307e2001-07-03 10:35:50 +000010641 for (i = 0; i < oldset->nodeNr; i++) {
10642 /*
10643 * Run the evaluation with a node list made of
10644 * a single item in the nodeset.
10645 */
10646 ctxt->context->node = oldset->nodeTab[i];
William M. Brack3794b9e2004-07-13 15:06:20 +000010647 if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) &&
10648 (oldset->nodeTab[i]->doc != NULL))
10649 ctxt->context->doc = oldset->nodeTab[i]->doc;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010650 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10651 valuePush(ctxt, tmp);
10652 ctxt->context->contextSize = oldset->nodeNr;
10653 ctxt->context->proximityPosition = i + 1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010654
Daniel Veillardf06307e2001-07-03 10:35:50 +000010655 if (op->ch2 != -1)
10656 total +=
10657 xmlXPathCompOpEval(ctxt,
10658 &comp->steps[op->ch2]);
10659 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010660
Daniel Veillardf06307e2001-07-03 10:35:50 +000010661 /*
William M. Brack08171912003-12-29 02:52:11 +000010662 * The result of the evaluation needs to be tested to
10663 * decide whether the filter succeeded or not
Daniel Veillardf06307e2001-07-03 10:35:50 +000010664 */
10665 res = valuePop(ctxt);
10666 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
10667 xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]);
10668 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010669
Daniel Veillardf06307e2001-07-03 10:35:50 +000010670 /*
10671 * Cleanup
10672 */
10673 if (res != NULL)
10674 xmlXPathFreeObject(res);
10675 if (ctxt->value == tmp) {
10676 res = valuePop(ctxt);
10677 xmlXPathFreeObject(res);
10678 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010679
Daniel Veillardf06307e2001-07-03 10:35:50 +000010680 ctxt->context->node = NULL;
10681 }
10682
10683 /*
10684 * The result is used as the new evaluation set.
10685 */
10686 xmlXPathFreeObject(obj);
10687 ctxt->context->node = NULL;
10688 ctxt->context->contextSize = -1;
10689 ctxt->context->proximityPosition = -1;
William M. Brack3794b9e2004-07-13 15:06:20 +000010690 /* may want to move this past the '}' later */
10691 ctxt->context->doc = oldDoc;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010692 valuePush(ctxt, xmlXPathWrapNodeSet(newset));
10693 }
10694 ctxt->context->node = oldnode;
10695 return (total);
10696 }
10697 case XPATH_OP_SORT:
10698 if (op->ch1 != -1)
10699 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010700 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010701 if ((ctxt->value != NULL) &&
10702 (ctxt->value->type == XPATH_NODESET) &&
10703 (ctxt->value->nodesetval != NULL))
10704 xmlXPathNodeSetSort(ctxt->value->nodesetval);
10705 return (total);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010706#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardf06307e2001-07-03 10:35:50 +000010707 case XPATH_OP_RANGETO:{
10708 xmlXPathObjectPtr range;
10709 xmlXPathObjectPtr res, obj;
10710 xmlXPathObjectPtr tmp;
William M. Brack08171912003-12-29 02:52:11 +000010711 xmlLocationSetPtr newlocset = NULL;
10712 xmlLocationSetPtr oldlocset;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010713 xmlNodeSetPtr oldset;
William M. Brack72ee48d2003-12-30 08:30:19 +000010714 int i, j;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010715
Daniel Veillardf06307e2001-07-03 10:35:50 +000010716 if (op->ch1 != -1)
10717 total +=
10718 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
10719 if (op->ch2 == -1)
10720 return (total);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010721
William M. Brack08171912003-12-29 02:52:11 +000010722 if (ctxt->value->type == XPATH_LOCATIONSET) {
10723 /*
10724 * Extract the old locset, and then evaluate the result of the
10725 * expression for all the element in the locset. use it to grow
10726 * up a new locset.
10727 */
10728 CHECK_TYPE0(XPATH_LOCATIONSET);
10729 obj = valuePop(ctxt);
10730 oldlocset = obj->user;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010731
William M. Brack08171912003-12-29 02:52:11 +000010732 if ((oldlocset == NULL) || (oldlocset->locNr == 0)) {
William M. Brack72ee48d2003-12-30 08:30:19 +000010733 ctxt->context->node = NULL;
William M. Brack08171912003-12-29 02:52:11 +000010734 ctxt->context->contextSize = 0;
10735 ctxt->context->proximityPosition = 0;
10736 total += xmlXPathCompOpEval(ctxt,&comp->steps[op->ch2]);
10737 res = valuePop(ctxt);
10738 if (res != NULL)
10739 xmlXPathFreeObject(res);
10740 valuePush(ctxt, obj);
10741 CHECK_ERROR0;
10742 return (total);
10743 }
10744 newlocset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010745
William M. Brack08171912003-12-29 02:52:11 +000010746 for (i = 0; i < oldlocset->locNr; i++) {
Daniel Veillardf06307e2001-07-03 10:35:50 +000010747 /*
William M. Brack08171912003-12-29 02:52:11 +000010748 * Run the evaluation with a node list made of a
10749 * single item in the nodelocset.
Daniel Veillardf06307e2001-07-03 10:35:50 +000010750 */
William M. Brackf7eb7942003-12-31 07:59:17 +000010751 ctxt->context->node = oldlocset->locTab[i]->user;
10752 ctxt->context->contextSize = oldlocset->locNr;
10753 ctxt->context->proximityPosition = i + 1;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010754 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10755 valuePush(ctxt, tmp);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010756
Daniel Veillardf06307e2001-07-03 10:35:50 +000010757 if (op->ch2 != -1)
10758 total +=
10759 xmlXPathCompOpEval(ctxt,
10760 &comp->steps[op->ch2]);
10761 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010762
Daniel Veillardf06307e2001-07-03 10:35:50 +000010763 res = valuePop(ctxt);
William M. Brack72ee48d2003-12-30 08:30:19 +000010764 if (res->type == XPATH_LOCATIONSET) {
10765 xmlLocationSetPtr rloc =
10766 (xmlLocationSetPtr)res->user;
10767 for (j=0; j<rloc->locNr; j++) {
10768 range = xmlXPtrNewRange(
10769 oldlocset->locTab[i]->user,
10770 oldlocset->locTab[i]->index,
10771 rloc->locTab[j]->user2,
10772 rloc->locTab[j]->index2);
10773 if (range != NULL) {
10774 xmlXPtrLocationSetAdd(newlocset, range);
10775 }
10776 }
10777 } else {
10778 range = xmlXPtrNewRangeNodeObject(
10779 (xmlNodePtr)oldlocset->locTab[i]->user, res);
10780 if (range != NULL) {
10781 xmlXPtrLocationSetAdd(newlocset,range);
10782 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010783 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010784
Daniel Veillardf06307e2001-07-03 10:35:50 +000010785 /*
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 }
William M. Brack72ee48d2003-12-30 08:30:19 +000010797 } else { /* Not a location set */
William M. Brack08171912003-12-29 02:52:11 +000010798 CHECK_TYPE0(XPATH_NODESET);
10799 obj = valuePop(ctxt);
10800 oldset = obj->nodesetval;
10801 ctxt->context->node = NULL;
10802
10803 newlocset = xmlXPtrLocationSetCreate(NULL);
10804
10805 if (oldset != NULL) {
10806 for (i = 0; i < oldset->nodeNr; i++) {
10807 /*
10808 * Run the evaluation with a node list made of a single item
10809 * in the nodeset.
10810 */
10811 ctxt->context->node = oldset->nodeTab[i];
10812 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10813 valuePush(ctxt, tmp);
10814
10815 if (op->ch2 != -1)
10816 total +=
10817 xmlXPathCompOpEval(ctxt,
10818 &comp->steps[op->ch2]);
10819 CHECK_ERROR0;
10820
William M. Brack08171912003-12-29 02:52:11 +000010821 res = valuePop(ctxt);
10822 range =
10823 xmlXPtrNewRangeNodeObject(oldset->nodeTab[i],
10824 res);
10825 if (range != NULL) {
10826 xmlXPtrLocationSetAdd(newlocset, range);
10827 }
10828
10829 /*
10830 * Cleanup
10831 */
10832 if (res != NULL)
10833 xmlXPathFreeObject(res);
10834 if (ctxt->value == tmp) {
10835 res = valuePop(ctxt);
10836 xmlXPathFreeObject(res);
10837 }
10838
10839 ctxt->context->node = NULL;
10840 }
10841 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010842 }
10843
10844 /*
10845 * The result is used as the new evaluation set.
10846 */
10847 xmlXPathFreeObject(obj);
10848 ctxt->context->node = NULL;
10849 ctxt->context->contextSize = -1;
10850 ctxt->context->proximityPosition = -1;
William M. Brack08171912003-12-29 02:52:11 +000010851 valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset));
Daniel Veillardf06307e2001-07-03 10:35:50 +000010852 return (total);
10853 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010854#endif /* LIBXML_XPTR_ENABLED */
10855 }
10856 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf06307e2001-07-03 10:35:50 +000010857 "XPath: unknown precompiled operation %d\n", op->op);
10858 return (total);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010859}
10860
10861/**
10862 * xmlXPathRunEval:
10863 * @ctxt: the XPath parser context with the compiled expression
10864 *
10865 * Evaluate the Precompiled XPath expression in the given context.
10866 */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000010867static void
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010868xmlXPathRunEval(xmlXPathParserContextPtr ctxt) {
10869 xmlXPathCompExprPtr comp;
10870
10871 if ((ctxt == NULL) || (ctxt->comp == NULL))
10872 return;
10873
10874 if (ctxt->valueTab == NULL) {
10875 /* Allocate the value stack */
10876 ctxt->valueTab = (xmlXPathObjectPtr *)
10877 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
10878 if (ctxt->valueTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +000010879 xmlXPathPErrMemory(ctxt, "creating evaluation context\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010880 xmlFree(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010881 }
10882 ctxt->valueNr = 0;
10883 ctxt->valueMax = 10;
10884 ctxt->value = NULL;
10885 }
10886 comp = ctxt->comp;
Aleksey Sanin29b6f762002-05-05 06:59:57 +000010887 if(comp->last < 0) {
10888 xmlGenericError(xmlGenericErrorContext,
10889 "xmlXPathRunEval: last is less than zero\n");
10890 return;
10891 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010892 xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]);
10893}
10894
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010895/************************************************************************
10896 * *
10897 * Public interfaces *
10898 * *
10899 ************************************************************************/
10900
10901/**
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000010902 * xmlXPathEvalPredicate:
10903 * @ctxt: the XPath context
10904 * @res: the Predicate Expression evaluation result
10905 *
10906 * Evaluate a predicate result for the current node.
10907 * A PredicateExpr is evaluated by evaluating the Expr and converting
10908 * the result to a boolean. If the result is a number, the result will
10909 * be converted to true if the number is equal to the position of the
10910 * context node in the context node list (as returned by the position
10911 * function) and will be converted to false otherwise; if the result
10912 * is not a number, then the result will be converted as if by a call
10913 * to the boolean function.
10914 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010915 * Returns 1 if predicate is true, 0 otherwise
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000010916 */
10917int
10918xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) {
10919 if (res == NULL) return(0);
10920 switch (res->type) {
10921 case XPATH_BOOLEAN:
10922 return(res->boolval);
10923 case XPATH_NUMBER:
10924 return(res->floatval == ctxt->proximityPosition);
10925 case XPATH_NODESET:
10926 case XPATH_XSLT_TREE:
Daniel Veillardd8df6c02001-04-05 16:54:14 +000010927 if (res->nodesetval == NULL)
10928 return(0);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000010929 return(res->nodesetval->nodeNr != 0);
10930 case XPATH_STRING:
10931 return((res->stringval != NULL) &&
10932 (xmlStrlen(res->stringval) != 0));
10933 default:
10934 STRANGE
10935 }
10936 return(0);
10937}
10938
10939/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010940 * xmlXPathEvaluatePredicateResult:
10941 * @ctxt: the XPath Parser context
10942 * @res: the Predicate Expression evaluation result
10943 *
10944 * Evaluate a predicate result for the current node.
10945 * A PredicateExpr is evaluated by evaluating the Expr and converting
10946 * the result to a boolean. If the result is a number, the result will
10947 * be converted to true if the number is equal to the position of the
10948 * context node in the context node list (as returned by the position
10949 * function) and will be converted to false otherwise; if the result
10950 * is not a number, then the result will be converted as if by a call
10951 * to the boolean function.
10952 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010953 * Returns 1 if predicate is true, 0 otherwise
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010954 */
10955int
10956xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt,
10957 xmlXPathObjectPtr res) {
10958 if (res == NULL) return(0);
10959 switch (res->type) {
10960 case XPATH_BOOLEAN:
10961 return(res->boolval);
10962 case XPATH_NUMBER:
Daniel Veillard9ea62312004-04-29 14:04:09 +000010963#if defined(__BORLANDC__) || (defined(_MSC_VER) && (_MSC_VER == 1200))
Daniel Veillard7c4eb632004-04-19 21:29:12 +000010964 return((res->floatval == ctxt->context->proximityPosition) &&
10965 (!xmlXPathIsNaN(res->floatval))); /* MSC pbm Mark Vakoc !*/
Daniel Veillard2582a332004-04-18 19:49:46 +000010966#else
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010967 return(res->floatval == ctxt->context->proximityPosition);
Daniel Veillard2582a332004-04-18 19:49:46 +000010968#endif
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010969 case XPATH_NODESET:
10970 case XPATH_XSLT_TREE:
Daniel Veillard73639a72001-04-10 14:31:39 +000010971 if (res->nodesetval == NULL)
Daniel Veillard911f49a2001-04-07 15:39:35 +000010972 return(0);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010973 return(res->nodesetval->nodeNr != 0);
10974 case XPATH_STRING:
10975 return((res->stringval != NULL) &&
10976 (xmlStrlen(res->stringval) != 0));
William M. Brack08171912003-12-29 02:52:11 +000010977#ifdef LIBXML_XPTR_ENABLED
10978 case XPATH_LOCATIONSET:{
10979 xmlLocationSetPtr ptr = res->user;
10980 if (ptr == NULL)
10981 return(0);
10982 return (ptr->locNr != 0);
10983 }
10984#endif
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010985 default:
10986 STRANGE
10987 }
10988 return(0);
10989}
10990
10991/**
Daniel Veillard4773df22004-01-23 13:15:13 +000010992 * xmlXPathCtxtCompile:
10993 * @ctxt: an XPath context
10994 * @str: the XPath expression
10995 *
10996 * Compile an XPath expression
10997 *
10998 * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL.
10999 * the caller has to free the object.
11000 */
11001xmlXPathCompExprPtr
11002xmlXPathCtxtCompile(xmlXPathContextPtr ctxt, const xmlChar *str) {
11003 xmlXPathParserContextPtr pctxt;
11004 xmlXPathCompExprPtr comp;
11005
11006 xmlXPathInit();
11007
11008 pctxt = xmlXPathNewParserContext(str, ctxt);
11009 xmlXPathCompileExpr(pctxt);
11010
11011 if( pctxt->error != XPATH_EXPRESSION_OK )
11012 {
11013 xmlXPathFreeParserContext(pctxt);
11014 return (0);
11015 }
11016
11017 if (*pctxt->cur != 0) {
11018 /*
11019 * aleksey: in some cases this line prints *second* error message
11020 * (see bug #78858) and probably this should be fixed.
11021 * However, we are not sure that all error messages are printed
11022 * out in other places. It's not critical so we leave it as-is for now
11023 */
11024 xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
11025 comp = NULL;
11026 } else {
11027 comp = pctxt->comp;
11028 pctxt->comp = NULL;
11029 }
11030 xmlXPathFreeParserContext(pctxt);
11031 if (comp != NULL) {
11032 comp->expr = xmlStrdup(str);
11033#ifdef DEBUG_EVAL_COUNTS
11034 comp->string = xmlStrdup(str);
11035 comp->nb = 0;
11036#endif
11037 }
11038 return(comp);
11039}
11040
11041/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011042 * xmlXPathCompile:
11043 * @str: the XPath expression
11044 *
11045 * Compile an XPath expression
11046 *
Daniel Veillard591b4be2003-02-09 23:33:36 +000011047 * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL.
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011048 * the caller has to free the object.
11049 */
11050xmlXPathCompExprPtr
11051xmlXPathCompile(const xmlChar *str) {
Daniel Veillard4773df22004-01-23 13:15:13 +000011052 return(xmlXPathCtxtCompile(NULL, str));
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011053}
11054
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011055/**
11056 * xmlXPathCompiledEval:
11057 * @comp: the compiled XPath expression
Owen Taylor3473f882001-02-23 17:55:21 +000011058 * @ctx: the XPath context
11059 *
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011060 * Evaluate the Precompiled XPath expression in the given context.
Owen Taylor3473f882001-02-23 17:55:21 +000011061 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011062 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Owen Taylor3473f882001-02-23 17:55:21 +000011063 * the caller has to free the object.
11064 */
11065xmlXPathObjectPtr
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011066xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) {
Owen Taylor3473f882001-02-23 17:55:21 +000011067 xmlXPathParserContextPtr ctxt;
11068 xmlXPathObjectPtr res, tmp, init = NULL;
11069 int stack = 0;
Daniel Veillard81463942001-10-16 12:34:39 +000011070#ifndef LIBXML_THREAD_ENABLED
11071 static int reentance = 0;
11072#endif
Owen Taylor3473f882001-02-23 17:55:21 +000011073
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011074 if ((comp == NULL) || (ctx == NULL))
11075 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011076 xmlXPathInit();
11077
11078 CHECK_CONTEXT(ctx)
11079
Daniel Veillard81463942001-10-16 12:34:39 +000011080#ifndef LIBXML_THREAD_ENABLED
11081 reentance++;
11082 if (reentance > 1)
11083 xmlXPathDisableOptimizer = 1;
11084#endif
11085
Daniel Veillardf06307e2001-07-03 10:35:50 +000011086#ifdef DEBUG_EVAL_COUNTS
11087 comp->nb++;
11088 if ((comp->string != NULL) && (comp->nb > 100)) {
11089 fprintf(stderr, "100 x %s\n", comp->string);
11090 comp->nb = 0;
11091 }
11092#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011093 ctxt = xmlXPathCompParserContext(comp, ctx);
11094 xmlXPathRunEval(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011095
11096 if (ctxt->value == NULL) {
11097 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011098 "xmlXPathCompiledEval: evaluation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +000011099 res = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000011100 } else {
11101 res = valuePop(ctxt);
11102 }
11103
Daniel Veillardf06307e2001-07-03 10:35:50 +000011104
Owen Taylor3473f882001-02-23 17:55:21 +000011105 do {
11106 tmp = valuePop(ctxt);
11107 if (tmp != NULL) {
11108 if (tmp != init)
11109 stack++;
11110 xmlXPathFreeObject(tmp);
11111 }
11112 } while (tmp != NULL);
11113 if ((stack != 0) && (res != NULL)) {
11114 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011115 "xmlXPathCompiledEval: %d object left on the stack\n",
Owen Taylor3473f882001-02-23 17:55:21 +000011116 stack);
11117 }
11118 if (ctxt->error != XPATH_EXPRESSION_OK) {
11119 xmlXPathFreeObject(res);
11120 res = NULL;
11121 }
11122
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011123
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011124 ctxt->comp = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011125 xmlXPathFreeParserContext(ctxt);
Daniel Veillard81463942001-10-16 12:34:39 +000011126#ifndef LIBXML_THREAD_ENABLED
11127 reentance--;
11128#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011129 return(res);
11130}
11131
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011132/**
11133 * xmlXPathEvalExpr:
11134 * @ctxt: the XPath Parser context
11135 *
11136 * Parse and evaluate an XPath expression in the given context,
11137 * then push the result on the context stack
11138 */
11139void
11140xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) {
11141 xmlXPathCompileExpr(ctxt);
Aleksey Sanin50fe8b12002-05-07 16:21:36 +000011142 CHECK_ERROR;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011143 xmlXPathRunEval(ctxt);
11144}
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011145
11146/**
11147 * xmlXPathEval:
11148 * @str: the XPath expression
11149 * @ctx: the XPath context
11150 *
11151 * Evaluate the XPath Location Path in the given context.
11152 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011153 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011154 * the caller has to free the object.
11155 */
11156xmlXPathObjectPtr
11157xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) {
11158 xmlXPathParserContextPtr ctxt;
11159 xmlXPathObjectPtr res, tmp, init = NULL;
11160 int stack = 0;
11161
11162 xmlXPathInit();
11163
11164 CHECK_CONTEXT(ctx)
11165
11166 ctxt = xmlXPathNewParserContext(str, ctx);
11167 xmlXPathEvalExpr(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011168
11169 if (ctxt->value == NULL) {
11170 xmlGenericError(xmlGenericErrorContext,
11171 "xmlXPathEval: evaluation failed\n");
11172 res = NULL;
11173 } else if (*ctxt->cur != 0) {
11174 xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
11175 res = NULL;
11176 } else {
11177 res = valuePop(ctxt);
11178 }
11179
11180 do {
11181 tmp = valuePop(ctxt);
11182 if (tmp != NULL) {
11183 if (tmp != init)
11184 stack++;
11185 xmlXPathFreeObject(tmp);
11186 }
11187 } while (tmp != NULL);
11188 if ((stack != 0) && (res != NULL)) {
11189 xmlGenericError(xmlGenericErrorContext,
11190 "xmlXPathEval: %d object left on the stack\n",
11191 stack);
11192 }
11193 if (ctxt->error != XPATH_EXPRESSION_OK) {
11194 xmlXPathFreeObject(res);
11195 res = NULL;
11196 }
11197
Owen Taylor3473f882001-02-23 17:55:21 +000011198 xmlXPathFreeParserContext(ctxt);
11199 return(res);
11200}
11201
11202/**
11203 * xmlXPathEvalExpression:
11204 * @str: the XPath expression
11205 * @ctxt: the XPath context
11206 *
11207 * Evaluate the XPath expression in the given context.
11208 *
11209 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
11210 * the caller has to free the object.
11211 */
11212xmlXPathObjectPtr
11213xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) {
11214 xmlXPathParserContextPtr pctxt;
11215 xmlXPathObjectPtr res, tmp;
11216 int stack = 0;
11217
11218 xmlXPathInit();
11219
11220 CHECK_CONTEXT(ctxt)
11221
11222 pctxt = xmlXPathNewParserContext(str, ctxt);
11223 xmlXPathEvalExpr(pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011224
11225 if (*pctxt->cur != 0) {
11226 xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
11227 res = NULL;
11228 } else {
11229 res = valuePop(pctxt);
11230 }
11231 do {
11232 tmp = valuePop(pctxt);
11233 if (tmp != NULL) {
11234 xmlXPathFreeObject(tmp);
11235 stack++;
11236 }
11237 } while (tmp != NULL);
11238 if ((stack != 0) && (res != NULL)) {
11239 xmlGenericError(xmlGenericErrorContext,
11240 "xmlXPathEvalExpression: %d object left on the stack\n",
11241 stack);
11242 }
11243 xmlXPathFreeParserContext(pctxt);
11244 return(res);
11245}
11246
Daniel Veillard42766c02002-08-22 20:52:17 +000011247/************************************************************************
11248 * *
11249 * Extra functions not pertaining to the XPath spec *
11250 * *
11251 ************************************************************************/
11252/**
11253 * xmlXPathEscapeUriFunction:
11254 * @ctxt: the XPath Parser context
11255 * @nargs: the number of arguments
11256 *
11257 * Implement the escape-uri() XPath function
11258 * string escape-uri(string $str, bool $escape-reserved)
11259 *
11260 * This function applies the URI escaping rules defined in section 2 of [RFC
11261 * 2396] to the string supplied as $uri-part, which typically represents all
11262 * or part of a URI. The effect of the function is to replace any special
11263 * character in the string by an escape sequence of the form %xx%yy...,
11264 * where xxyy... is the hexadecimal representation of the octets used to
11265 * represent the character in UTF-8.
11266 *
11267 * The set of characters that are escaped depends on the setting of the
11268 * boolean argument $escape-reserved.
11269 *
11270 * If $escape-reserved is true, all characters are escaped other than lower
11271 * case letters a-z, upper case letters A-Z, digits 0-9, and the characters
11272 * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!"
11273 * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only
11274 * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and
11275 * A-F).
11276 *
11277 * If $escape-reserved is false, the behavior differs in that characters
11278 * referred to in [RFC 2396] as reserved characters are not escaped. These
11279 * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",".
11280 *
11281 * [RFC 2396] does not define whether escaped URIs should use lower case or
11282 * upper case for hexadecimal digits. To ensure that escaped URIs can be
11283 * compared using string comparison functions, this function must always use
11284 * the upper-case letters A-F.
11285 *
11286 * Generally, $escape-reserved should be set to true when escaping a string
11287 * that is to form a single part of a URI, and to false when escaping an
11288 * entire URI or URI reference.
11289 *
11290 * In the case of non-ascii characters, the string is encoded according to
11291 * utf-8 and then converted according to RFC 2396.
11292 *
11293 * Examples
11294 * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true())
11295 * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean"
11296 * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false())
11297 * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean"
11298 *
11299 */
Daniel Veillard118aed72002-09-24 14:13:13 +000011300static void
Daniel Veillard42766c02002-08-22 20:52:17 +000011301xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) {
11302 xmlXPathObjectPtr str;
11303 int escape_reserved;
11304 xmlBufferPtr target;
11305 xmlChar *cptr;
11306 xmlChar escape[4];
11307
11308 CHECK_ARITY(2);
11309
11310 escape_reserved = xmlXPathPopBoolean(ctxt);
11311
11312 CAST_TO_STRING;
11313 str = valuePop(ctxt);
11314
11315 target = xmlBufferCreate();
11316
11317 escape[0] = '%';
11318 escape[3] = 0;
11319
11320 if (target) {
11321 for (cptr = str->stringval; *cptr; cptr++) {
11322 if ((*cptr >= 'A' && *cptr <= 'Z') ||
11323 (*cptr >= 'a' && *cptr <= 'z') ||
11324 (*cptr >= '0' && *cptr <= '9') ||
11325 *cptr == '-' || *cptr == '_' || *cptr == '.' ||
11326 *cptr == '!' || *cptr == '~' || *cptr == '*' ||
11327 *cptr == '\''|| *cptr == '(' || *cptr == ')' ||
11328 (*cptr == '%' &&
11329 ((cptr[1] >= 'A' && cptr[1] <= 'F') ||
11330 (cptr[1] >= 'a' && cptr[1] <= 'f') ||
11331 (cptr[1] >= '0' && cptr[1] <= '9')) &&
11332 ((cptr[2] >= 'A' && cptr[2] <= 'F') ||
11333 (cptr[2] >= 'a' && cptr[2] <= 'f') ||
11334 (cptr[2] >= '0' && cptr[2] <= '9'))) ||
11335 (!escape_reserved &&
11336 (*cptr == ';' || *cptr == '/' || *cptr == '?' ||
11337 *cptr == ':' || *cptr == '@' || *cptr == '&' ||
11338 *cptr == '=' || *cptr == '+' || *cptr == '$' ||
11339 *cptr == ','))) {
11340 xmlBufferAdd(target, cptr, 1);
11341 } else {
11342 if ((*cptr >> 4) < 10)
11343 escape[1] = '0' + (*cptr >> 4);
11344 else
11345 escape[1] = 'A' - 10 + (*cptr >> 4);
11346 if ((*cptr & 0xF) < 10)
11347 escape[2] = '0' + (*cptr & 0xF);
11348 else
11349 escape[2] = 'A' - 10 + (*cptr & 0xF);
11350
11351 xmlBufferAdd(target, &escape[0], 3);
11352 }
11353 }
11354 }
11355 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
11356 xmlBufferFree(target);
11357 xmlXPathFreeObject(str);
11358}
11359
Owen Taylor3473f882001-02-23 17:55:21 +000011360/**
11361 * xmlXPathRegisterAllFunctions:
11362 * @ctxt: the XPath context
11363 *
11364 * Registers all default XPath functions in this context
11365 */
11366void
11367xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt)
11368{
11369 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean",
11370 xmlXPathBooleanFunction);
11371 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling",
11372 xmlXPathCeilingFunction);
11373 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count",
11374 xmlXPathCountFunction);
11375 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat",
11376 xmlXPathConcatFunction);
11377 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains",
11378 xmlXPathContainsFunction);
11379 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id",
11380 xmlXPathIdFunction);
11381 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false",
11382 xmlXPathFalseFunction);
11383 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor",
11384 xmlXPathFloorFunction);
11385 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last",
11386 xmlXPathLastFunction);
11387 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang",
11388 xmlXPathLangFunction);
11389 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name",
11390 xmlXPathLocalNameFunction);
11391 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not",
11392 xmlXPathNotFunction);
11393 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name",
11394 xmlXPathNameFunction);
11395 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri",
11396 xmlXPathNamespaceURIFunction);
11397 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space",
11398 xmlXPathNormalizeFunction);
11399 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number",
11400 xmlXPathNumberFunction);
11401 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position",
11402 xmlXPathPositionFunction);
11403 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round",
11404 xmlXPathRoundFunction);
11405 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string",
11406 xmlXPathStringFunction);
11407 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length",
11408 xmlXPathStringLengthFunction);
11409 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with",
11410 xmlXPathStartsWithFunction);
11411 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring",
11412 xmlXPathSubstringFunction);
11413 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before",
11414 xmlXPathSubstringBeforeFunction);
11415 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after",
11416 xmlXPathSubstringAfterFunction);
11417 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum",
11418 xmlXPathSumFunction);
11419 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true",
11420 xmlXPathTrueFunction);
11421 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate",
11422 xmlXPathTranslateFunction);
Daniel Veillard42766c02002-08-22 20:52:17 +000011423
11424 xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri",
11425 (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions",
11426 xmlXPathEscapeUriFunction);
Owen Taylor3473f882001-02-23 17:55:21 +000011427}
11428
11429#endif /* LIBXML_XPATH_ENABLED */