blob: 7ce29010430f2e695b806e18ee2aa22ce21c630b [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>
Daniel Veillard56de87e2005-02-16 00:22:29 +000054#ifdef LIBXML_PATTERN_ENABLED
55#include <libxml/pattern.h>
56#endif
57
58#ifdef LIBXML_PATTERN_ENABLED
Daniel Veillardfa1f77f2005-02-21 10:44:36 +000059#define XPATH_STREAMING
Daniel Veillard56de87e2005-02-16 00:22:29 +000060#endif
Owen Taylor3473f882001-02-23 17:55:21 +000061
Daniel Veillardd96f6d32003-10-07 21:25:12 +000062#define TODO \
63 xmlGenericError(xmlGenericErrorContext, \
64 "Unimplemented block at %s:%d\n", \
65 __FILE__, __LINE__);
66
William M. Brackd1757ab2004-10-02 22:07:48 +000067/*
68 * TODO:
69 * There are a few spots where some tests are done which depend upon ascii
70 * data. These should be enhanced for full UTF8 support (see particularly
71 * any use of the macros IS_ASCII_CHARACTER and IS_ASCII_DIGIT)
72 */
73
William M. Brack21e4ef22005-01-02 09:53:13 +000074#if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
Daniel Veillard9e7160d2001-03-18 23:17:47 +000075/************************************************************************
76 * *
77 * Floating point stuff *
78 * *
79 ************************************************************************/
80
Daniel Veillardc0631a62001-09-20 13:56:06 +000081#ifndef TRIO_REPLACE_STDIO
Daniel Veillardcda96922001-08-21 10:56:31 +000082#define TRIO_PUBLIC static
Daniel Veillardc0631a62001-09-20 13:56:06 +000083#endif
Daniel Veillardcda96922001-08-21 10:56:31 +000084#include "trionan.c"
85
Owen Taylor3473f882001-02-23 17:55:21 +000086/*
Owen Taylor3473f882001-02-23 17:55:21 +000087 * The lack of portability of this section of the libc is annoying !
88 */
89double xmlXPathNAN = 0;
90double xmlXPathPINF = 1;
91double xmlXPathNINF = -1;
Daniel Veillard5fc1f082002-03-27 09:05:40 +000092double xmlXPathNZERO = 0;
Daniel Veillard20ee8c02001-10-05 09:18:14 +000093static int xmlXPathInitialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +000094
Owen Taylor3473f882001-02-23 17:55:21 +000095/**
96 * xmlXPathInit:
97 *
98 * Initialize the XPath environment
99 */
100void
101xmlXPathInit(void) {
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000102 if (xmlXPathInitialized) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000103
Bjorn Reese45029602001-08-21 09:23:53 +0000104 xmlXPathPINF = trio_pinf();
105 xmlXPathNINF = trio_ninf();
106 xmlXPathNAN = trio_nan();
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000107 xmlXPathNZERO = trio_nzero();
Owen Taylor3473f882001-02-23 17:55:21 +0000108
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000109 xmlXPathInitialized = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000110}
111
Daniel Veillardcda96922001-08-21 10:56:31 +0000112/**
113 * xmlXPathIsNaN:
114 * @val: a double value
115 *
116 * Provides a portable isnan() function to detect whether a double
117 * is a NotaNumber. Based on trio code
118 * http://sourceforge.net/projects/ctrio/
119 *
120 * Returns 1 if the value is a NaN, 0 otherwise
121 */
122int
123xmlXPathIsNaN(double val) {
124 return(trio_isnan(val));
125}
126
127/**
128 * xmlXPathIsInf:
129 * @val: a double value
130 *
131 * Provides a portable isinf() function to detect whether a double
132 * is a +Infinite or -Infinite. Based on trio code
133 * http://sourceforge.net/projects/ctrio/
134 *
135 * Returns 1 vi the value is +Infinite, -1 if -Infinite, 0 otherwise
136 */
137int
138xmlXPathIsInf(double val) {
139 return(trio_isinf(val));
140}
141
Daniel Veillard4432df22003-09-28 18:58:27 +0000142#endif /* SCHEMAS or XPATH */
143#ifdef LIBXML_XPATH_ENABLED
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000144/**
145 * xmlXPathGetSign:
146 * @val: a double value
147 *
148 * Provides a portable function to detect the sign of a double
149 * Modified from trio code
150 * http://sourceforge.net/projects/ctrio/
151 *
152 * Returns 1 if the value is Negative, 0 if positive
153 */
Daniel Veillard21458c82002-03-27 16:12:22 +0000154static int
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000155xmlXPathGetSign(double val) {
Daniel Veillard21458c82002-03-27 16:12:22 +0000156 return(trio_signbit(val));
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000157}
158
159
Daniel Veillardd9d32ae2003-07-05 20:32:43 +0000160/*
161 * TODO: when compatibility allows remove all "fake node libxslt" strings
162 * the test should just be name[0] = ' '
163 */
164/* #define DEBUG */
165/* #define DEBUG_STEP */
166/* #define DEBUG_STEP_NTH */
167/* #define DEBUG_EXPR */
168/* #define DEBUG_EVAL_COUNTS */
169
170static xmlNs xmlXPathXMLNamespaceStruct = {
171 NULL,
172 XML_NAMESPACE_DECL,
173 XML_XML_NAMESPACE,
174 BAD_CAST "xml",
175 NULL
176};
177static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct;
178#ifndef LIBXML_THREAD_ENABLED
179/*
180 * Optimizer is disabled only when threaded apps are detected while
181 * the library ain't compiled for thread safety.
182 */
183static int xmlXPathDisableOptimizer = 0;
184#endif
185
Owen Taylor3473f882001-02-23 17:55:21 +0000186/************************************************************************
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000187 * *
188 * Error handling routines *
189 * *
190 ************************************************************************/
191
William M. Brack08171912003-12-29 02:52:11 +0000192/*
193 * The array xmlXPathErrorMessages corresponds to the enum xmlXPathError
194 */
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000195static const char *xmlXPathErrorMessages[] = {
196 "Ok\n",
197 "Number encoding\n",
198 "Unfinished literal\n",
199 "Start of literal\n",
200 "Expected $ for variable reference\n",
201 "Undefined variable\n",
202 "Invalid predicate\n",
203 "Invalid expression\n",
204 "Missing closing curly brace\n",
205 "Unregistered function\n",
206 "Invalid operand\n",
207 "Invalid type\n",
208 "Invalid number of arguments\n",
209 "Invalid context size\n",
210 "Invalid context position\n",
211 "Memory allocation error\n",
212 "Syntax error\n",
213 "Resource error\n",
214 "Sub resource error\n",
215 "Undefined namespace prefix\n",
216 "Encoding error\n",
Daniel Veillard57b25162004-11-06 14:50:18 +0000217 "Char out of XML range\n",
William M. Brackcd65bc92005-01-06 09:39:18 +0000218 "Invalid or incomplete context\n",
219 "?? Unknown error ??\n" /* Must be last in the list! */
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000220};
William M. Brackcd65bc92005-01-06 09:39:18 +0000221#define MAXERRNO ((int)(sizeof(xmlXPathErrorMessages) / \
222 sizeof(xmlXPathErrorMessages[0])) - 1)
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000223/**
224 * xmlXPathErrMemory:
225 * @ctxt: an XPath context
226 * @extra: extra informations
227 *
228 * Handle a redefinition of attribute error
229 */
230static void
231xmlXPathErrMemory(xmlXPathContextPtr ctxt, const char *extra)
232{
233 if (ctxt != NULL) {
234 if (extra) {
235 xmlChar buf[200];
236
237 xmlStrPrintf(buf, 200,
238 BAD_CAST "Memory allocation failed : %s\n",
239 extra);
240 ctxt->lastError.message = (char *) xmlStrdup(buf);
241 } else {
242 ctxt->lastError.message = (char *)
243 xmlStrdup(BAD_CAST "Memory allocation failed\n");
244 }
245 ctxt->lastError.domain = XML_FROM_XPATH;
246 ctxt->lastError.code = XML_ERR_NO_MEMORY;
247 if (ctxt->error != NULL)
248 ctxt->error(ctxt->userData, &ctxt->lastError);
249 } else {
250 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000251 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000252 NULL, NULL, XML_FROM_XPATH,
253 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0,
254 extra, NULL, NULL, 0, 0,
255 "Memory allocation failed : %s\n", extra);
256 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000257 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000258 NULL, NULL, XML_FROM_XPATH,
259 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0,
260 NULL, NULL, NULL, 0, 0,
261 "Memory allocation failed\n");
262 }
263}
264
265/**
Daniel Veillard8de5c0b2004-10-07 13:14:19 +0000266 * xmlXPathPErrMemory:
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000267 * @ctxt: an XPath parser context
268 * @extra: extra informations
269 *
270 * Handle a redefinition of attribute error
271 */
272static void
273xmlXPathPErrMemory(xmlXPathParserContextPtr ctxt, const char *extra)
274{
275 ctxt->error = XPATH_MEMORY_ERROR;
276 if (ctxt == NULL)
277 xmlXPathErrMemory(NULL, extra);
278 else
279 xmlXPathErrMemory(ctxt->context, extra);
280}
281
282/**
283 * xmlXPathErr:
284 * @ctxt: a XPath parser context
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000285 * @error: the error code
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000286 *
William M. Brackcd65bc92005-01-06 09:39:18 +0000287 * Handle an XPath error
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000288 */
289void
290xmlXPathErr(xmlXPathParserContextPtr ctxt, int error)
291{
William M. Brackcd65bc92005-01-06 09:39:18 +0000292 if ((error < 0) || (error > MAXERRNO))
293 error = MAXERRNO;
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000294 if (ctxt == NULL) {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000295 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000296 NULL, NULL, XML_FROM_XPATH,
297 error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
298 XML_ERR_ERROR, NULL, 0,
299 NULL, NULL, NULL, 0, 0,
300 xmlXPathErrorMessages[error]);
301 return;
302 }
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000303 ctxt->error = error;
304 if (ctxt->context == NULL) {
305 __xmlRaiseError(NULL, NULL, NULL,
306 NULL, NULL, XML_FROM_XPATH,
307 error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
308 XML_ERR_ERROR, NULL, 0,
309 (const char *) ctxt->base, NULL, NULL,
310 ctxt->cur - ctxt->base, 0,
311 xmlXPathErrorMessages[error]);
312 return;
313 }
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000314 ctxt->context->lastError.domain = XML_FROM_XPATH;
315 ctxt->context->lastError.code = error + XML_XPATH_EXPRESSION_OK -
316 XPATH_EXPRESSION_OK;
Daniel Veillardfcf719c2003-10-10 11:42:17 +0000317 ctxt->context->lastError.level = XML_ERR_ERROR;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000318 ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base);
319 ctxt->context->lastError.int1 = ctxt->cur - ctxt->base;
320 ctxt->context->lastError.node = ctxt->context->debugNode;
321 if (ctxt->context->error != NULL) {
322 ctxt->context->error(ctxt->context->userData,
323 &ctxt->context->lastError);
324 } else {
Daniel Veillard659e71e2003-10-10 14:10:40 +0000325 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000326 NULL, ctxt->context->debugNode, XML_FROM_XPATH,
327 error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK,
328 XML_ERR_ERROR, NULL, 0,
329 (const char *) ctxt->base, NULL, NULL,
330 ctxt->cur - ctxt->base, 0,
331 xmlXPathErrorMessages[error]);
332 }
333
334}
335
336/**
337 * xmlXPatherror:
338 * @ctxt: the XPath Parser context
339 * @file: the file name
340 * @line: the line number
341 * @no: the error number
342 *
343 * Formats an error message.
344 */
345void
346xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file ATTRIBUTE_UNUSED,
347 int line ATTRIBUTE_UNUSED, int no) {
348 xmlXPathErr(ctxt, no);
349}
350
351
352/************************************************************************
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000353 * *
354 * Parser Types *
355 * *
356 ************************************************************************/
357
358/*
359 * Types are private:
360 */
361
362typedef enum {
363 XPATH_OP_END=0,
364 XPATH_OP_AND,
365 XPATH_OP_OR,
366 XPATH_OP_EQUAL,
367 XPATH_OP_CMP,
368 XPATH_OP_PLUS,
369 XPATH_OP_MULT,
370 XPATH_OP_UNION,
371 XPATH_OP_ROOT,
372 XPATH_OP_NODE,
373 XPATH_OP_RESET,
374 XPATH_OP_COLLECT,
375 XPATH_OP_VALUE,
376 XPATH_OP_VARIABLE,
377 XPATH_OP_FUNCTION,
378 XPATH_OP_ARG,
379 XPATH_OP_PREDICATE,
Daniel Veillardd8df6c02001-04-05 16:54:14 +0000380 XPATH_OP_FILTER,
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000381 XPATH_OP_SORT
382#ifdef LIBXML_XPTR_ENABLED
383 ,XPATH_OP_RANGETO
384#endif
385} xmlXPathOp;
386
387typedef enum {
388 AXIS_ANCESTOR = 1,
389 AXIS_ANCESTOR_OR_SELF,
390 AXIS_ATTRIBUTE,
391 AXIS_CHILD,
392 AXIS_DESCENDANT,
393 AXIS_DESCENDANT_OR_SELF,
394 AXIS_FOLLOWING,
395 AXIS_FOLLOWING_SIBLING,
396 AXIS_NAMESPACE,
397 AXIS_PARENT,
398 AXIS_PRECEDING,
399 AXIS_PRECEDING_SIBLING,
400 AXIS_SELF
401} xmlXPathAxisVal;
402
403typedef enum {
404 NODE_TEST_NONE = 0,
405 NODE_TEST_TYPE = 1,
406 NODE_TEST_PI = 2,
407 NODE_TEST_ALL = 3,
408 NODE_TEST_NS = 4,
409 NODE_TEST_NAME = 5
410} xmlXPathTestVal;
411
412typedef enum {
413 NODE_TYPE_NODE = 0,
414 NODE_TYPE_COMMENT = XML_COMMENT_NODE,
415 NODE_TYPE_TEXT = XML_TEXT_NODE,
416 NODE_TYPE_PI = XML_PI_NODE
417} xmlXPathTypeVal;
418
419
420typedef struct _xmlXPathStepOp xmlXPathStepOp;
421typedef xmlXPathStepOp *xmlXPathStepOpPtr;
422struct _xmlXPathStepOp {
William M. Brack08171912003-12-29 02:52:11 +0000423 xmlXPathOp op; /* The identifier of the operation */
424 int ch1; /* First child */
425 int ch2; /* Second child */
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000426 int value;
427 int value2;
428 int value3;
429 void *value4;
430 void *value5;
Daniel Veillarde39a93d2001-04-28 14:35:02 +0000431 void *cache;
Daniel Veillard42596ad2001-05-22 16:57:14 +0000432 void *cacheURI;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000433};
434
435struct _xmlXPathCompExpr {
William M. Brack08171912003-12-29 02:52:11 +0000436 int nbStep; /* Number of steps in this expression */
437 int maxStep; /* Maximum number of steps allocated */
438 xmlXPathStepOp *steps; /* ops for computation of this expression */
439 int last; /* index of last step in expression */
440 xmlChar *expr; /* the expression being computed */
Daniel Veillard4773df22004-01-23 13:15:13 +0000441 xmlDictPtr dict; /* the dictionnary to use if any */
Daniel Veillardf06307e2001-07-03 10:35:50 +0000442#ifdef DEBUG_EVAL_COUNTS
443 int nb;
444 xmlChar *string;
445#endif
Daniel Veillard56de87e2005-02-16 00:22:29 +0000446#ifdef XPATH_STREAMING
447 xmlPatternPtr stream;
448#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000449};
450
451/************************************************************************
452 * *
453 * Parser Type functions *
454 * *
455 ************************************************************************/
456
457/**
458 * xmlXPathNewCompExpr:
459 *
460 * Create a new Xpath component
461 *
462 * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error
463 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000464static xmlXPathCompExprPtr
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000465xmlXPathNewCompExpr(void) {
466 xmlXPathCompExprPtr cur;
467
468 cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr));
469 if (cur == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000470 xmlXPathErrMemory(NULL, "allocating component\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000471 return(NULL);
472 }
473 memset(cur, 0, sizeof(xmlXPathCompExpr));
474 cur->maxStep = 10;
475 cur->nbStep = 0;
476 cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep *
477 sizeof(xmlXPathStepOp));
478 if (cur->steps == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000479 xmlXPathErrMemory(NULL, "allocating steps\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000480 xmlFree(cur);
481 return(NULL);
482 }
483 memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp));
484 cur->last = -1;
Daniel Veillardf06307e2001-07-03 10:35:50 +0000485#ifdef DEBUG_EVAL_COUNTS
486 cur->nb = 0;
487#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000488 return(cur);
489}
490
491/**
492 * xmlXPathFreeCompExpr:
493 * @comp: an XPATH comp
494 *
495 * Free up the memory allocated by @comp
496 */
497void
Daniel Veillardf06307e2001-07-03 10:35:50 +0000498xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp)
499{
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000500 xmlXPathStepOpPtr op;
501 int i;
502
503 if (comp == NULL)
Daniel Veillardf06307e2001-07-03 10:35:50 +0000504 return;
Daniel Veillard4773df22004-01-23 13:15:13 +0000505 if (comp->dict == NULL) {
506 for (i = 0; i < comp->nbStep; i++) {
507 op = &comp->steps[i];
508 if (op->value4 != NULL) {
509 if (op->op == XPATH_OP_VALUE)
510 xmlXPathFreeObject(op->value4);
511 else
512 xmlFree(op->value4);
513 }
514 if (op->value5 != NULL)
515 xmlFree(op->value5);
516 }
517 } else {
518 for (i = 0; i < comp->nbStep; i++) {
519 op = &comp->steps[i];
520 if (op->value4 != NULL) {
521 if (op->op == XPATH_OP_VALUE)
522 xmlXPathFreeObject(op->value4);
523 }
524 }
525 xmlDictFree(comp->dict);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000526 }
527 if (comp->steps != NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +0000528 xmlFree(comp->steps);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000529 }
Daniel Veillardf06307e2001-07-03 10:35:50 +0000530#ifdef DEBUG_EVAL_COUNTS
531 if (comp->string != NULL) {
532 xmlFree(comp->string);
533 }
534#endif
Daniel Veillard56de87e2005-02-16 00:22:29 +0000535#ifdef XPATH_STREAMING
536 if (comp->stream != NULL) {
537 xmlFreePatternList(comp->stream);
538 }
539#endif
Daniel Veillard118aed72002-09-24 14:13:13 +0000540 if (comp->expr != NULL) {
541 xmlFree(comp->expr);
542 }
Daniel Veillardf06307e2001-07-03 10:35:50 +0000543
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000544 xmlFree(comp);
545}
546
547/**
548 * xmlXPathCompExprAdd:
549 * @comp: the compiled expression
550 * @ch1: first child index
551 * @ch2: second child index
552 * @op: an op
553 * @value: the first int value
554 * @value2: the second int value
555 * @value3: the third int value
556 * @value4: the first string value
557 * @value5: the second string value
558 *
William M. Brack08171912003-12-29 02:52:11 +0000559 * Add a step to an XPath Compiled Expression
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000560 *
561 * Returns -1 in case of failure, the index otherwise
562 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000563static int
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000564xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2,
565 xmlXPathOp op, int value,
566 int value2, int value3, void *value4, void *value5) {
567 if (comp->nbStep >= comp->maxStep) {
568 xmlXPathStepOp *real;
569
570 comp->maxStep *= 2;
571 real = (xmlXPathStepOp *) xmlRealloc(comp->steps,
572 comp->maxStep * sizeof(xmlXPathStepOp));
573 if (real == NULL) {
574 comp->maxStep /= 2;
Daniel Veillardd96f6d32003-10-07 21:25:12 +0000575 xmlXPathErrMemory(NULL, "adding step\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000576 return(-1);
577 }
578 comp->steps = real;
579 }
580 comp->last = comp->nbStep;
581 comp->steps[comp->nbStep].ch1 = ch1;
582 comp->steps[comp->nbStep].ch2 = ch2;
583 comp->steps[comp->nbStep].op = op;
584 comp->steps[comp->nbStep].value = value;
585 comp->steps[comp->nbStep].value2 = value2;
586 comp->steps[comp->nbStep].value3 = value3;
Daniel Veillard4773df22004-01-23 13:15:13 +0000587 if ((comp->dict != NULL) &&
588 ((op == XPATH_OP_FUNCTION) || (op == XPATH_OP_VARIABLE) ||
589 (op == XPATH_OP_COLLECT))) {
590 if (value4 != NULL) {
Daniel Veillardb3377952004-02-09 12:48:55 +0000591 comp->steps[comp->nbStep].value4 = (xmlChar *)
William M. Brackc07ed5e2004-01-30 07:52:48 +0000592 (void *)xmlDictLookup(comp->dict, value4, -1);
Daniel Veillard4773df22004-01-23 13:15:13 +0000593 xmlFree(value4);
594 } else
595 comp->steps[comp->nbStep].value4 = NULL;
596 if (value5 != NULL) {
Daniel Veillardb3377952004-02-09 12:48:55 +0000597 comp->steps[comp->nbStep].value5 = (xmlChar *)
William M. Brackc07ed5e2004-01-30 07:52:48 +0000598 (void *)xmlDictLookup(comp->dict, value5, -1);
Daniel Veillard4773df22004-01-23 13:15:13 +0000599 xmlFree(value5);
600 } else
601 comp->steps[comp->nbStep].value5 = NULL;
602 } else {
603 comp->steps[comp->nbStep].value4 = value4;
604 comp->steps[comp->nbStep].value5 = value5;
605 }
Daniel Veillarde39a93d2001-04-28 14:35:02 +0000606 comp->steps[comp->nbStep].cache = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000607 return(comp->nbStep++);
608}
609
Daniel Veillardf06307e2001-07-03 10:35:50 +0000610/**
611 * xmlXPathCompSwap:
612 * @comp: the compiled expression
613 * @op: operation index
614 *
615 * Swaps 2 operations in the compiled expression
Daniel Veillardf06307e2001-07-03 10:35:50 +0000616 */
617static void
618xmlXPathCompSwap(xmlXPathStepOpPtr op) {
619 int tmp;
620
Daniel Veillardbc6f7592002-04-16 07:49:59 +0000621#ifndef LIBXML_THREAD_ENABLED
Daniel Veillard81463942001-10-16 12:34:39 +0000622 /*
623 * Since this manipulates possibly shared variables, this is
William M. Brack08171912003-12-29 02:52:11 +0000624 * disabled if one detects that the library is used in a multithreaded
Daniel Veillard81463942001-10-16 12:34:39 +0000625 * application
626 */
627 if (xmlXPathDisableOptimizer)
628 return;
629#endif
630
Daniel Veillardf06307e2001-07-03 10:35:50 +0000631 tmp = op->ch1;
632 op->ch1 = op->ch2;
633 op->ch2 = tmp;
634}
635
Daniel Veillardd8df6c02001-04-05 16:54:14 +0000636#define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \
637 xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \
638 (op), (val), (val2), (val3), (val4), (val5))
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000639#define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \
640 xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \
641 (op), (val), (val2), (val3), (val4), (val5))
642
643#define PUSH_LEAVE_EXPR(op, val, val2) \
644xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL)
645
646#define PUSH_UNARY_EXPR(op, ch, val, val2) \
647xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL)
648
649#define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \
William M. Brack08171912003-12-29 02:52:11 +0000650xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \
651 (val), (val2), 0 ,NULL ,NULL)
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000652
653/************************************************************************
Owen Taylor3473f882001-02-23 17:55:21 +0000654 * *
655 * Debugging related functions *
656 * *
657 ************************************************************************/
658
Owen Taylor3473f882001-02-23 17:55:21 +0000659#define STRANGE \
660 xmlGenericError(xmlGenericErrorContext, \
661 "Internal error at %s:%d\n", \
662 __FILE__, __LINE__);
663
664#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000665static void
666xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000667 int i;
668 char shift[100];
669
670 for (i = 0;((i < depth) && (i < 25));i++)
671 shift[2 * i] = shift[2 * i + 1] = ' ';
672 shift[2 * i] = shift[2 * i + 1] = 0;
673 if (cur == NULL) {
674 fprintf(output, shift);
675 fprintf(output, "Node is NULL !\n");
676 return;
677
678 }
679
680 if ((cur->type == XML_DOCUMENT_NODE) ||
681 (cur->type == XML_HTML_DOCUMENT_NODE)) {
682 fprintf(output, shift);
683 fprintf(output, " /\n");
684 } else if (cur->type == XML_ATTRIBUTE_NODE)
685 xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth);
686 else
687 xmlDebugDumpOneNode(output, cur, depth);
688}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000689static void
690xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000691 xmlNodePtr tmp;
692 int i;
693 char shift[100];
694
695 for (i = 0;((i < depth) && (i < 25));i++)
696 shift[2 * i] = shift[2 * i + 1] = ' ';
697 shift[2 * i] = shift[2 * i + 1] = 0;
698 if (cur == NULL) {
699 fprintf(output, shift);
700 fprintf(output, "Node is NULL !\n");
701 return;
702
703 }
704
705 while (cur != NULL) {
706 tmp = cur;
707 cur = cur->next;
708 xmlDebugDumpOneNode(output, tmp, depth);
709 }
710}
Owen Taylor3473f882001-02-23 17:55:21 +0000711
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000712static void
713xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000714 int i;
715 char shift[100];
716
717 for (i = 0;((i < depth) && (i < 25));i++)
718 shift[2 * i] = shift[2 * i + 1] = ' ';
719 shift[2 * i] = shift[2 * i + 1] = 0;
720
721 if (cur == NULL) {
722 fprintf(output, shift);
723 fprintf(output, "NodeSet is NULL !\n");
724 return;
725
726 }
727
Daniel Veillard911f49a2001-04-07 15:39:35 +0000728 if (cur != NULL) {
729 fprintf(output, "Set contains %d nodes:\n", cur->nodeNr);
730 for (i = 0;i < cur->nodeNr;i++) {
731 fprintf(output, shift);
732 fprintf(output, "%d", i + 1);
733 xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1);
734 }
Owen Taylor3473f882001-02-23 17:55:21 +0000735 }
736}
737
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000738static void
739xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000740 int i;
741 char shift[100];
742
743 for (i = 0;((i < depth) && (i < 25));i++)
744 shift[2 * i] = shift[2 * i + 1] = ' ';
745 shift[2 * i] = shift[2 * i + 1] = 0;
746
747 if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) {
748 fprintf(output, shift);
749 fprintf(output, "Value Tree is NULL !\n");
750 return;
751
752 }
753
754 fprintf(output, shift);
755 fprintf(output, "%d", i + 1);
756 xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1);
757}
Owen Taylor3473f882001-02-23 17:55:21 +0000758#if defined(LIBXML_XPTR_ENABLED)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000759static void
760xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000761 int i;
762 char shift[100];
763
764 for (i = 0;((i < depth) && (i < 25));i++)
765 shift[2 * i] = shift[2 * i + 1] = ' ';
766 shift[2 * i] = shift[2 * i + 1] = 0;
767
768 if (cur == NULL) {
769 fprintf(output, shift);
770 fprintf(output, "LocationSet is NULL !\n");
771 return;
772
773 }
774
775 for (i = 0;i < cur->locNr;i++) {
776 fprintf(output, shift);
777 fprintf(output, "%d : ", i + 1);
778 xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1);
779 }
780}
Daniel Veillard017b1082001-06-21 11:20:21 +0000781#endif /* LIBXML_XPTR_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000782
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000783/**
784 * xmlXPathDebugDumpObject:
785 * @output: the FILE * to dump the output
786 * @cur: the object to inspect
787 * @depth: indentation level
788 *
789 * Dump the content of the object for debugging purposes
790 */
791void
792xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000793 int i;
794 char shift[100];
795
Daniel Veillarda82b1822004-11-08 16:24:57 +0000796 if (output == NULL) return;
797
Owen Taylor3473f882001-02-23 17:55:21 +0000798 for (i = 0;((i < depth) && (i < 25));i++)
799 shift[2 * i] = shift[2 * i + 1] = ' ';
800 shift[2 * i] = shift[2 * i + 1] = 0;
801
802 fprintf(output, shift);
803
804 if (cur == NULL) {
805 fprintf(output, "Object is empty (NULL)\n");
806 return;
807 }
808 switch(cur->type) {
809 case XPATH_UNDEFINED:
810 fprintf(output, "Object is uninitialized\n");
811 break;
812 case XPATH_NODESET:
813 fprintf(output, "Object is a Node Set :\n");
814 xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth);
815 break;
816 case XPATH_XSLT_TREE:
817 fprintf(output, "Object is an XSLT value tree :\n");
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000818 xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth);
Owen Taylor3473f882001-02-23 17:55:21 +0000819 break;
820 case XPATH_BOOLEAN:
821 fprintf(output, "Object is a Boolean : ");
822 if (cur->boolval) fprintf(output, "true\n");
823 else fprintf(output, "false\n");
824 break;
825 case XPATH_NUMBER:
Daniel Veillardcda96922001-08-21 10:56:31 +0000826 switch (xmlXPathIsInf(cur->floatval)) {
Daniel Veillard357c9602001-05-03 10:49:20 +0000827 case 1:
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000828 fprintf(output, "Object is a number : Infinity\n");
Daniel Veillard357c9602001-05-03 10:49:20 +0000829 break;
830 case -1:
831 fprintf(output, "Object is a number : -Infinity\n");
832 break;
833 default:
Daniel Veillardcda96922001-08-21 10:56:31 +0000834 if (xmlXPathIsNaN(cur->floatval)) {
Daniel Veillard357c9602001-05-03 10:49:20 +0000835 fprintf(output, "Object is a number : NaN\n");
Daniel Veillardd30be4a2002-03-28 18:25:31 +0000836 } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) {
837 fprintf(output, "Object is a number : 0\n");
Daniel Veillard357c9602001-05-03 10:49:20 +0000838 } else {
839 fprintf(output, "Object is a number : %0g\n", cur->floatval);
840 }
841 }
Owen Taylor3473f882001-02-23 17:55:21 +0000842 break;
843 case XPATH_STRING:
844 fprintf(output, "Object is a string : ");
845 xmlDebugDumpString(output, cur->stringval);
846 fprintf(output, "\n");
847 break;
848 case XPATH_POINT:
849 fprintf(output, "Object is a point : index %d in node", cur->index);
850 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1);
851 fprintf(output, "\n");
852 break;
853 case XPATH_RANGE:
854 if ((cur->user2 == NULL) ||
855 ((cur->user2 == cur->user) && (cur->index == cur->index2))) {
856 fprintf(output, "Object is a collapsed range :\n");
857 fprintf(output, shift);
858 if (cur->index >= 0)
859 fprintf(output, "index %d in ", cur->index);
860 fprintf(output, "node\n");
861 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user,
862 depth + 1);
863 } else {
864 fprintf(output, "Object is a range :\n");
865 fprintf(output, shift);
866 fprintf(output, "From ");
867 if (cur->index >= 0)
868 fprintf(output, "index %d in ", cur->index);
869 fprintf(output, "node\n");
870 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user,
871 depth + 1);
872 fprintf(output, shift);
873 fprintf(output, "To ");
874 if (cur->index2 >= 0)
875 fprintf(output, "index %d in ", cur->index2);
876 fprintf(output, "node\n");
877 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2,
878 depth + 1);
879 fprintf(output, "\n");
880 }
881 break;
882 case XPATH_LOCATIONSET:
883#if defined(LIBXML_XPTR_ENABLED)
884 fprintf(output, "Object is a Location Set:\n");
885 xmlXPathDebugDumpLocationSet(output,
886 (xmlLocationSetPtr) cur->user, depth);
887#endif
888 break;
889 case XPATH_USERS:
890 fprintf(output, "Object is user defined\n");
891 break;
892 }
893}
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000894
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000895static void
896xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp,
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000897 xmlXPathStepOpPtr op, int depth) {
898 int i;
899 char shift[100];
900
901 for (i = 0;((i < depth) && (i < 25));i++)
902 shift[2 * i] = shift[2 * i + 1] = ' ';
903 shift[2 * i] = shift[2 * i + 1] = 0;
904
905 fprintf(output, shift);
906 if (op == NULL) {
907 fprintf(output, "Step is NULL\n");
908 return;
909 }
910 switch (op->op) {
911 case XPATH_OP_END:
912 fprintf(output, "END"); break;
913 case XPATH_OP_AND:
914 fprintf(output, "AND"); break;
915 case XPATH_OP_OR:
916 fprintf(output, "OR"); break;
917 case XPATH_OP_EQUAL:
918 if (op->value)
919 fprintf(output, "EQUAL =");
920 else
921 fprintf(output, "EQUAL !=");
922 break;
923 case XPATH_OP_CMP:
924 if (op->value)
925 fprintf(output, "CMP <");
926 else
927 fprintf(output, "CMP >");
928 if (!op->value2)
929 fprintf(output, "=");
930 break;
931 case XPATH_OP_PLUS:
932 if (op->value == 0)
933 fprintf(output, "PLUS -");
934 else if (op->value == 1)
935 fprintf(output, "PLUS +");
936 else if (op->value == 2)
937 fprintf(output, "PLUS unary -");
938 else if (op->value == 3)
939 fprintf(output, "PLUS unary - -");
940 break;
941 case XPATH_OP_MULT:
942 if (op->value == 0)
943 fprintf(output, "MULT *");
944 else if (op->value == 1)
945 fprintf(output, "MULT div");
946 else
947 fprintf(output, "MULT mod");
948 break;
949 case XPATH_OP_UNION:
950 fprintf(output, "UNION"); break;
951 case XPATH_OP_ROOT:
952 fprintf(output, "ROOT"); break;
953 case XPATH_OP_NODE:
954 fprintf(output, "NODE"); break;
955 case XPATH_OP_RESET:
956 fprintf(output, "RESET"); break;
957 case XPATH_OP_SORT:
958 fprintf(output, "SORT"); break;
959 case XPATH_OP_COLLECT: {
William M. Brack78637da2003-07-31 14:47:38 +0000960 xmlXPathAxisVal axis = (xmlXPathAxisVal)op->value;
961 xmlXPathTestVal test = (xmlXPathTestVal)op->value2;
962 xmlXPathTypeVal type = (xmlXPathTypeVal)op->value3;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000963 const xmlChar *prefix = op->value4;
964 const xmlChar *name = op->value5;
965
966 fprintf(output, "COLLECT ");
967 switch (axis) {
968 case AXIS_ANCESTOR:
969 fprintf(output, " 'ancestors' "); break;
970 case AXIS_ANCESTOR_OR_SELF:
971 fprintf(output, " 'ancestors-or-self' "); break;
972 case AXIS_ATTRIBUTE:
973 fprintf(output, " 'attributes' "); break;
974 case AXIS_CHILD:
975 fprintf(output, " 'child' "); break;
976 case AXIS_DESCENDANT:
977 fprintf(output, " 'descendant' "); break;
978 case AXIS_DESCENDANT_OR_SELF:
979 fprintf(output, " 'descendant-or-self' "); break;
980 case AXIS_FOLLOWING:
981 fprintf(output, " 'following' "); break;
982 case AXIS_FOLLOWING_SIBLING:
983 fprintf(output, " 'following-siblings' "); break;
984 case AXIS_NAMESPACE:
985 fprintf(output, " 'namespace' "); break;
986 case AXIS_PARENT:
987 fprintf(output, " 'parent' "); break;
988 case AXIS_PRECEDING:
989 fprintf(output, " 'preceding' "); break;
990 case AXIS_PRECEDING_SIBLING:
991 fprintf(output, " 'preceding-sibling' "); break;
992 case AXIS_SELF:
993 fprintf(output, " 'self' "); break;
994 }
995 switch (test) {
996 case NODE_TEST_NONE:
997 fprintf(output, "'none' "); break;
998 case NODE_TEST_TYPE:
999 fprintf(output, "'type' "); break;
1000 case NODE_TEST_PI:
1001 fprintf(output, "'PI' "); break;
1002 case NODE_TEST_ALL:
1003 fprintf(output, "'all' "); break;
1004 case NODE_TEST_NS:
1005 fprintf(output, "'namespace' "); break;
1006 case NODE_TEST_NAME:
1007 fprintf(output, "'name' "); break;
1008 }
1009 switch (type) {
1010 case NODE_TYPE_NODE:
1011 fprintf(output, "'node' "); break;
1012 case NODE_TYPE_COMMENT:
1013 fprintf(output, "'comment' "); break;
1014 case NODE_TYPE_TEXT:
1015 fprintf(output, "'text' "); break;
1016 case NODE_TYPE_PI:
1017 fprintf(output, "'PI' "); break;
1018 }
1019 if (prefix != NULL)
1020 fprintf(output, "%s:", prefix);
1021 if (name != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00001022 fprintf(output, "%s", (const char *) name);
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001023 break;
1024
1025 }
1026 case XPATH_OP_VALUE: {
1027 xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4;
1028
1029 fprintf(output, "ELEM ");
1030 xmlXPathDebugDumpObject(output, object, 0);
1031 goto finish;
1032 }
1033 case XPATH_OP_VARIABLE: {
1034 const xmlChar *prefix = op->value5;
1035 const xmlChar *name = op->value4;
1036
1037 if (prefix != NULL)
1038 fprintf(output, "VARIABLE %s:%s", prefix, name);
1039 else
1040 fprintf(output, "VARIABLE %s", name);
1041 break;
1042 }
1043 case XPATH_OP_FUNCTION: {
1044 int nbargs = op->value;
1045 const xmlChar *prefix = op->value5;
1046 const xmlChar *name = op->value4;
1047
1048 if (prefix != NULL)
1049 fprintf(output, "FUNCTION %s:%s(%d args)",
1050 prefix, name, nbargs);
1051 else
1052 fprintf(output, "FUNCTION %s(%d args)", name, nbargs);
1053 break;
1054 }
1055 case XPATH_OP_ARG: fprintf(output, "ARG"); break;
1056 case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00001057 case XPATH_OP_FILTER: fprintf(output, "FILTER"); break;
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00001058#ifdef LIBXML_XPTR_ENABLED
1059 case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break;
1060#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001061 default:
1062 fprintf(output, "UNKNOWN %d\n", op->op); return;
1063 }
1064 fprintf(output, "\n");
1065finish:
1066 if (op->ch1 >= 0)
1067 xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1);
1068 if (op->ch2 >= 0)
1069 xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1);
1070}
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001071
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001072/**
1073 * xmlXPathDebugDumpCompExpr:
1074 * @output: the FILE * for the output
1075 * @comp: the precompiled XPath expression
1076 * @depth: the indentation level.
1077 *
1078 * Dumps the tree of the compiled XPath expression.
1079 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001080void
1081xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp,
1082 int depth) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001083 int i;
1084 char shift[100];
1085
Daniel Veillarda82b1822004-11-08 16:24:57 +00001086 if ((output == NULL) || (comp == NULL)) return;
1087
Daniel Veillard9e7160d2001-03-18 23:17:47 +00001088 for (i = 0;((i < depth) && (i < 25));i++)
1089 shift[2 * i] = shift[2 * i + 1] = ' ';
1090 shift[2 * i] = shift[2 * i + 1] = 0;
1091
1092 fprintf(output, shift);
1093
1094 if (comp == NULL) {
1095 fprintf(output, "Compiled Expression is NULL\n");
1096 return;
1097 }
1098 fprintf(output, "Compiled Expression : %d elements\n",
1099 comp->nbStep);
1100 i = comp->last;
1101 xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1);
1102}
Daniel Veillard017b1082001-06-21 11:20:21 +00001103#endif /* LIBXML_DEBUG_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001104
1105/************************************************************************
1106 * *
1107 * Parser stacks related functions and macros *
1108 * *
1109 ************************************************************************/
1110
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001111/**
1112 * valuePop:
1113 * @ctxt: an XPath evaluation context
1114 *
1115 * Pops the top XPath object from the value stack
1116 *
1117 * Returns the XPath object just removed
1118 */
Daniel Veillard1c732d22002-11-30 11:22:59 +00001119extern xmlXPathObjectPtr
1120valuePop(xmlXPathParserContextPtr ctxt)
1121{
1122 xmlXPathObjectPtr ret;
1123
Daniel Veillarda82b1822004-11-08 16:24:57 +00001124 if ((ctxt == NULL) || (ctxt->valueNr <= 0))
Daniel Veillard1c732d22002-11-30 11:22:59 +00001125 return (0);
1126 ctxt->valueNr--;
1127 if (ctxt->valueNr > 0)
1128 ctxt->value = ctxt->valueTab[ctxt->valueNr - 1];
1129 else
1130 ctxt->value = NULL;
1131 ret = ctxt->valueTab[ctxt->valueNr];
1132 ctxt->valueTab[ctxt->valueNr] = 0;
1133 return (ret);
1134}
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001135/**
1136 * valuePush:
1137 * @ctxt: an XPath evaluation context
1138 * @value: the XPath object
1139 *
1140 * Pushes a new XPath object on top of the value stack
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001141 *
1142 * returns the number of items on the value stack
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001143 */
Daniel Veillard1c732d22002-11-30 11:22:59 +00001144extern int
1145valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value)
1146{
Daniel Veillarda82b1822004-11-08 16:24:57 +00001147 if ((ctxt == NULL) || (value == NULL)) return(-1);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001148 if (ctxt->valueNr >= ctxt->valueMax) {
Daniel Veillarda918b5b2004-09-26 14:25:37 +00001149 xmlXPathObjectPtr *tmp;
1150
1151 tmp = (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab,
1152 2 * ctxt->valueMax *
Daniel Veillard1c732d22002-11-30 11:22:59 +00001153 sizeof(ctxt->valueTab[0]));
Daniel Veillarda918b5b2004-09-26 14:25:37 +00001154 if (tmp == NULL) {
Daniel Veillard1c732d22002-11-30 11:22:59 +00001155 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1156 return (0);
1157 }
Daniel Veillarda918b5b2004-09-26 14:25:37 +00001158 ctxt->valueMax *= 2;
1159 ctxt->valueTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001160 }
1161 ctxt->valueTab[ctxt->valueNr] = value;
1162 ctxt->value = value;
1163 return (ctxt->valueNr++);
1164}
Owen Taylor3473f882001-02-23 17:55:21 +00001165
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001166/**
1167 * xmlXPathPopBoolean:
1168 * @ctxt: an XPath parser context
1169 *
1170 * Pops a boolean from the stack, handling conversion if needed.
1171 * Check error with #xmlXPathCheckError.
1172 *
1173 * Returns the boolean
1174 */
1175int
1176xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) {
1177 xmlXPathObjectPtr obj;
1178 int ret;
1179
1180 obj = valuePop(ctxt);
1181 if (obj == NULL) {
1182 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1183 return(0);
1184 }
William M. Brack08171912003-12-29 02:52:11 +00001185 if (obj->type != XPATH_BOOLEAN)
1186 ret = xmlXPathCastToBoolean(obj);
1187 else
1188 ret = obj->boolval;
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001189 xmlXPathFreeObject(obj);
1190 return(ret);
1191}
1192
1193/**
1194 * xmlXPathPopNumber:
1195 * @ctxt: an XPath parser context
1196 *
1197 * Pops a number from the stack, handling conversion if needed.
1198 * Check error with #xmlXPathCheckError.
1199 *
1200 * Returns the number
1201 */
1202double
1203xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) {
1204 xmlXPathObjectPtr obj;
1205 double ret;
1206
1207 obj = valuePop(ctxt);
1208 if (obj == NULL) {
1209 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1210 return(0);
1211 }
William M. Brack08171912003-12-29 02:52:11 +00001212 if (obj->type != XPATH_NUMBER)
1213 ret = xmlXPathCastToNumber(obj);
1214 else
1215 ret = obj->floatval;
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001216 xmlXPathFreeObject(obj);
1217 return(ret);
1218}
1219
1220/**
1221 * xmlXPathPopString:
1222 * @ctxt: an XPath parser context
1223 *
1224 * Pops a string from the stack, handling conversion if needed.
1225 * Check error with #xmlXPathCheckError.
1226 *
1227 * Returns the string
1228 */
1229xmlChar *
1230xmlXPathPopString (xmlXPathParserContextPtr ctxt) {
1231 xmlXPathObjectPtr obj;
1232 xmlChar * ret;
1233
1234 obj = valuePop(ctxt);
1235 if (obj == NULL) {
1236 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1237 return(NULL);
1238 }
William M. Brack08171912003-12-29 02:52:11 +00001239 ret = xmlXPathCastToString(obj); /* this does required strdup */
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001240 /* TODO: needs refactoring somewhere else */
1241 if (obj->stringval == ret)
1242 obj->stringval = NULL;
1243 xmlXPathFreeObject(obj);
1244 return(ret);
1245}
1246
1247/**
1248 * xmlXPathPopNodeSet:
1249 * @ctxt: an XPath parser context
1250 *
1251 * Pops a node-set from the stack, handling conversion if needed.
1252 * Check error with #xmlXPathCheckError.
1253 *
1254 * Returns the node-set
1255 */
1256xmlNodeSetPtr
1257xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) {
1258 xmlXPathObjectPtr obj;
1259 xmlNodeSetPtr ret;
1260
Daniel Veillardf2a36f92004-11-08 17:55:01 +00001261 if (ctxt == NULL) return(NULL);
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001262 if (ctxt->value == NULL) {
1263 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1264 return(NULL);
1265 }
1266 if (!xmlXPathStackIsNodeSet(ctxt)) {
1267 xmlXPathSetTypeError(ctxt);
1268 return(NULL);
1269 }
1270 obj = valuePop(ctxt);
1271 ret = obj->nodesetval;
William M. Bracke9449c52004-07-11 14:41:20 +00001272#if 0
Daniel Veillard9deb2422003-07-28 20:40:59 +00001273 /* to fix memory leak of not clearing obj->user */
1274 if (obj->boolval && obj->user != NULL)
1275 xmlFreeNodeList((xmlNodePtr) obj->user);
William M. Bracke9449c52004-07-11 14:41:20 +00001276#endif
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001277 xmlXPathFreeNodeSetList(obj);
1278 return(ret);
1279}
1280
1281/**
1282 * xmlXPathPopExternal:
1283 * @ctxt: an XPath parser context
1284 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001285 * Pops an external object from the stack, handling conversion if needed.
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001286 * Check error with #xmlXPathCheckError.
1287 *
1288 * Returns the object
1289 */
1290void *
1291xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) {
1292 xmlXPathObjectPtr obj;
1293 void * ret;
1294
Daniel Veillarda82b1822004-11-08 16:24:57 +00001295 if ((ctxt == NULL) || (ctxt->value == NULL)) {
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001296 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1297 return(NULL);
1298 }
1299 if (ctxt->value->type != XPATH_USERS) {
1300 xmlXPathSetTypeError(ctxt);
1301 return(NULL);
1302 }
1303 obj = valuePop(ctxt);
1304 ret = obj->user;
1305 xmlXPathFreeObject(obj);
1306 return(ret);
1307}
1308
Owen Taylor3473f882001-02-23 17:55:21 +00001309/*
1310 * Macros for accessing the content. Those should be used only by the parser,
1311 * and not exported.
1312 *
1313 * Dirty macros, i.e. one need to make assumption on the context to use them
1314 *
1315 * CUR_PTR return the current pointer to the xmlChar to be parsed.
1316 * CUR returns the current xmlChar value, i.e. a 8 bit value
1317 * in ISO-Latin or UTF-8.
1318 * This should be used internally by the parser
1319 * only to compare to ASCII values otherwise it would break when
1320 * running with UTF-8 encoding.
1321 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
1322 * to compare on ASCII based substring.
1323 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
1324 * strings within the parser.
1325 * CURRENT Returns the current char value, with the full decoding of
1326 * UTF-8 if we are using this mode. It returns an int.
1327 * NEXT Skip to the next character, this does the proper decoding
1328 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
1329 * It returns the pointer to the current xmlChar.
1330 */
1331
1332#define CUR (*ctxt->cur)
1333#define SKIP(val) ctxt->cur += (val)
1334#define NXT(val) ctxt->cur[(val)]
1335#define CUR_PTR ctxt->cur
Daniel Veillard61d80a22001-04-27 17:13:01 +00001336#define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l)
1337
1338#define COPY_BUF(l,b,i,v) \
1339 if (l == 1) b[i++] = (xmlChar) v; \
1340 else i += xmlCopyChar(l,&b[i],v)
1341
1342#define NEXTL(l) ctxt->cur += l
Owen Taylor3473f882001-02-23 17:55:21 +00001343
1344#define SKIP_BLANKS \
William M. Brack76e95df2003-10-18 16:20:14 +00001345 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
Owen Taylor3473f882001-02-23 17:55:21 +00001346
1347#define CURRENT (*ctxt->cur)
1348#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1349
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001350
1351#ifndef DBL_DIG
1352#define DBL_DIG 16
1353#endif
1354#ifndef DBL_EPSILON
1355#define DBL_EPSILON 1E-9
1356#endif
1357
1358#define UPPER_DOUBLE 1E9
1359#define LOWER_DOUBLE 1E-5
1360
1361#define INTEGER_DIGITS DBL_DIG
1362#define FRACTION_DIGITS (DBL_DIG + 1)
1363#define EXPONENT_DIGITS (3 + 2)
1364
1365/**
1366 * xmlXPathFormatNumber:
1367 * @number: number to format
1368 * @buffer: output buffer
1369 * @buffersize: size of output buffer
1370 *
1371 * Convert the number into a string representation.
1372 */
1373static void
1374xmlXPathFormatNumber(double number, char buffer[], int buffersize)
1375{
Daniel Veillardcda96922001-08-21 10:56:31 +00001376 switch (xmlXPathIsInf(number)) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001377 case 1:
Daniel Veillard5fc1f082002-03-27 09:05:40 +00001378 if (buffersize > (int)sizeof("Infinity"))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001379 snprintf(buffer, buffersize, "Infinity");
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001380 break;
1381 case -1:
1382 if (buffersize > (int)sizeof("-Infinity"))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001383 snprintf(buffer, buffersize, "-Infinity");
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001384 break;
1385 default:
Daniel Veillardcda96922001-08-21 10:56:31 +00001386 if (xmlXPathIsNaN(number)) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001387 if (buffersize > (int)sizeof("NaN"))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001388 snprintf(buffer, buffersize, "NaN");
Daniel Veillardd30be4a2002-03-28 18:25:31 +00001389 } else if (number == 0 && xmlXPathGetSign(number) != 0) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001390 snprintf(buffer, buffersize, "0");
Daniel Veillard28cac6b2002-03-19 11:25:30 +00001391 } else if (number == ((int) number)) {
1392 char work[30];
1393 char *ptr, *cur;
1394 int res, value = (int) number;
1395
1396 ptr = &buffer[0];
1397 if (value < 0) {
1398 *ptr++ = '-';
1399 value = -value;
1400 }
1401 if (value == 0) {
1402 *ptr++ = '0';
1403 } else {
1404 cur = &work[0];
1405 while (value != 0) {
1406 res = value % 10;
1407 value = value / 10;
1408 *cur++ = '0' + res;
1409 }
1410 cur--;
1411 while ((cur >= &work[0]) && (ptr - buffer < buffersize)) {
1412 *ptr++ = *cur--;
1413 }
1414 }
1415 if (ptr - buffer < buffersize) {
1416 *ptr = 0;
1417 } else if (buffersize > 0) {
1418 ptr--;
1419 *ptr = 0;
1420 }
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001421 } else {
Bjorn Reese70a9da52001-04-21 16:57:29 +00001422 /* 3 is sign, decimal point, and terminating zero */
1423 char work[DBL_DIG + EXPONENT_DIGITS + 3];
1424 int integer_place, fraction_place;
1425 char *ptr;
1426 char *after_fraction;
1427 double absolute_value;
1428 int size;
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001429
Bjorn Reese70a9da52001-04-21 16:57:29 +00001430 absolute_value = fabs(number);
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001431
Bjorn Reese70a9da52001-04-21 16:57:29 +00001432 /*
1433 * First choose format - scientific or regular floating point.
1434 * In either case, result is in work, and after_fraction points
1435 * just past the fractional part.
1436 */
1437 if ( ((absolute_value > UPPER_DOUBLE) ||
1438 (absolute_value < LOWER_DOUBLE)) &&
1439 (absolute_value != 0.0) ) {
1440 /* Use scientific notation */
1441 integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
1442 fraction_place = DBL_DIG - 1;
1443 snprintf(work, sizeof(work),"%*.*e",
1444 integer_place, fraction_place, number);
1445 after_fraction = strchr(work + DBL_DIG, 'e');
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001446 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00001447 else {
1448 /* Use regular notation */
Daniel Veillard56f06462001-06-24 21:34:03 +00001449 if (absolute_value > 0.0)
1450 integer_place = 1 + (int)log10(absolute_value);
1451 else
Daniel Veillarda3067d12001-06-24 21:39:39 +00001452 integer_place = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00001453 fraction_place = (integer_place > 0)
1454 ? DBL_DIG - integer_place
1455 : DBL_DIG;
1456 size = snprintf(work, sizeof(work), "%0.*f",
1457 fraction_place, number);
1458 after_fraction = work + size;
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001459 }
1460
Bjorn Reese70a9da52001-04-21 16:57:29 +00001461 /* Remove fractional trailing zeroes */
1462 ptr = after_fraction;
1463 while (*(--ptr) == '0')
1464 ;
1465 if (*ptr != '.')
1466 ptr++;
Daniel Veillard5dd3c962003-09-12 15:32:16 +00001467 while ((*ptr++ = *after_fraction++) != 0);
Bjorn Reese70a9da52001-04-21 16:57:29 +00001468
1469 /* Finally copy result back to caller */
1470 size = strlen(work) + 1;
1471 if (size > buffersize) {
1472 work[buffersize - 1] = 0;
1473 size = buffersize;
1474 }
Daniel Veillard5dd3c962003-09-12 15:32:16 +00001475 memmove(buffer, work, size);
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001476 }
1477 break;
1478 }
1479}
1480
Owen Taylor3473f882001-02-23 17:55:21 +00001481
1482/************************************************************************
1483 * *
1484 * Routines to handle NodeSets *
1485 * *
1486 ************************************************************************/
1487
1488/**
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001489 * xmlXPathOrderDocElems:
1490 * @doc: an input document
1491 *
1492 * Call this routine to speed up XPath computation on static documents.
1493 * This stamps all the element nodes with the document order
1494 * Like for line information, the order is kept in the element->content
William M. Brack08171912003-12-29 02:52:11 +00001495 * field, the value stored is actually - the node number (starting at -1)
1496 * to be able to differentiate from line numbers.
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001497 *
William M. Brack08171912003-12-29 02:52:11 +00001498 * Returns the number of elements found in the document or -1 in case
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001499 * of error.
1500 */
1501long
1502xmlXPathOrderDocElems(xmlDocPtr doc) {
1503 long count = 0;
1504 xmlNodePtr cur;
1505
1506 if (doc == NULL)
1507 return(-1);
1508 cur = doc->children;
1509 while (cur != NULL) {
1510 if (cur->type == XML_ELEMENT_NODE) {
1511 cur->content = (void *) (-(++count));
1512 if (cur->children != NULL) {
1513 cur = cur->children;
1514 continue;
1515 }
1516 }
1517 if (cur->next != NULL) {
1518 cur = cur->next;
1519 continue;
1520 }
1521 do {
1522 cur = cur->parent;
1523 if (cur == NULL)
1524 break;
1525 if (cur == (xmlNodePtr) doc) {
1526 cur = NULL;
1527 break;
1528 }
1529 if (cur->next != NULL) {
1530 cur = cur->next;
1531 break;
1532 }
1533 } while (cur != NULL);
1534 }
1535 return(count);
1536}
1537
1538/**
Owen Taylor3473f882001-02-23 17:55:21 +00001539 * xmlXPathCmpNodes:
1540 * @node1: the first node
1541 * @node2: the second node
1542 *
1543 * Compare two nodes w.r.t document order
1544 *
1545 * Returns -2 in case of error 1 if first point < second point, 0 if
William M. Brack08171912003-12-29 02:52:11 +00001546 * it's the same node, -1 otherwise
Owen Taylor3473f882001-02-23 17:55:21 +00001547 */
1548int
1549xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) {
1550 int depth1, depth2;
Daniel Veillardedfd5882003-03-07 14:20:40 +00001551 int attr1 = 0, attr2 = 0;
William M. Bracke8d1bd92003-12-23 01:28:58 +00001552 xmlNodePtr attrNode1 = NULL, attrNode2 = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001553 xmlNodePtr cur, root;
1554
1555 if ((node1 == NULL) || (node2 == NULL))
1556 return(-2);
1557 /*
1558 * a couple of optimizations which will avoid computations in most cases
1559 */
Daniel Veillardedfd5882003-03-07 14:20:40 +00001560 if (node1->type == XML_ATTRIBUTE_NODE) {
1561 attr1 = 1;
William M. Bracke8d1bd92003-12-23 01:28:58 +00001562 attrNode1 = node1;
Daniel Veillardedfd5882003-03-07 14:20:40 +00001563 node1 = node1->parent;
1564 }
1565 if (node2->type == XML_ATTRIBUTE_NODE) {
1566 attr2 = 1;
William M. Bracke8d1bd92003-12-23 01:28:58 +00001567 attrNode2 = node2;
Daniel Veillardedfd5882003-03-07 14:20:40 +00001568 node2 = node2->parent;
1569 }
1570 if (node1 == node2) {
William M. Bracke8d1bd92003-12-23 01:28:58 +00001571 if (attr1 == attr2) {
1572 /* not required, but we keep attributes in order */
1573 if (attr1 != 0) {
1574 cur = attrNode2->prev;
1575 while (cur != NULL) {
1576 if (cur == attrNode1)
1577 return (1);
1578 cur = cur->prev;
1579 }
1580 return (-1);
1581 }
Daniel Veillardedfd5882003-03-07 14:20:40 +00001582 return(0);
William M. Bracke8d1bd92003-12-23 01:28:58 +00001583 }
Daniel Veillardedfd5882003-03-07 14:20:40 +00001584 if (attr2 == 1)
1585 return(1);
1586 return(-1);
1587 }
Daniel Veillardb33c2012001-04-25 12:59:04 +00001588 if ((node1->type == XML_NAMESPACE_DECL) ||
1589 (node2->type == XML_NAMESPACE_DECL))
1590 return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00001591 if (node1 == node2->prev)
1592 return(1);
1593 if (node1 == node2->next)
1594 return(-1);
1595
1596 /*
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001597 * Speedup using document order if availble.
Daniel Veillard7216cfd2002-11-08 15:10:00 +00001598 */
1599 if ((node1->type == XML_ELEMENT_NODE) &&
1600 (node2->type == XML_ELEMENT_NODE) &&
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001601 (0 > (long) node1->content) &&
1602 (0 > (long) node2->content) &&
1603 (node1->doc == node2->doc)) {
1604 long l1, l2;
1605
1606 l1 = -((long) node1->content);
1607 l2 = -((long) node2->content);
Daniel Veillard7216cfd2002-11-08 15:10:00 +00001608 if (l1 < l2)
1609 return(1);
1610 if (l1 > l2)
1611 return(-1);
1612 }
Daniel Veillarde4fa2932003-03-26 00:38:10 +00001613
Daniel Veillard7216cfd2002-11-08 15:10:00 +00001614 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001615 * compute depth to root
1616 */
1617 for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) {
1618 if (cur == node1)
1619 return(1);
1620 depth2++;
1621 }
1622 root = cur;
1623 for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) {
1624 if (cur == node2)
1625 return(-1);
1626 depth1++;
1627 }
1628 /*
1629 * Distinct document (or distinct entities :-( ) case.
1630 */
1631 if (root != cur) {
1632 return(-2);
1633 }
1634 /*
1635 * get the nearest common ancestor.
1636 */
1637 while (depth1 > depth2) {
1638 depth1--;
1639 node1 = node1->parent;
1640 }
1641 while (depth2 > depth1) {
1642 depth2--;
1643 node2 = node2->parent;
1644 }
1645 while (node1->parent != node2->parent) {
1646 node1 = node1->parent;
1647 node2 = node2->parent;
1648 /* should not happen but just in case ... */
1649 if ((node1 == NULL) || (node2 == NULL))
1650 return(-2);
1651 }
1652 /*
1653 * Find who's first.
1654 */
Daniel Veillardf49be472004-02-17 11:48:18 +00001655 if (node1 == node2->prev)
1656 return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00001657 if (node1 == node2->next)
1658 return(-1);
Daniel Veillardf49be472004-02-17 11:48:18 +00001659 /*
1660 * Speedup using document order if availble.
1661 */
1662 if ((node1->type == XML_ELEMENT_NODE) &&
1663 (node2->type == XML_ELEMENT_NODE) &&
1664 (0 > (long) node1->content) &&
1665 (0 > (long) node2->content) &&
1666 (node1->doc == node2->doc)) {
1667 long l1, l2;
1668
1669 l1 = -((long) node1->content);
1670 l2 = -((long) node2->content);
1671 if (l1 < l2)
1672 return(1);
1673 if (l1 > l2)
1674 return(-1);
1675 }
1676
Owen Taylor3473f882001-02-23 17:55:21 +00001677 for (cur = node1->next;cur != NULL;cur = cur->next)
1678 if (cur == node2)
1679 return(1);
1680 return(-1); /* assume there is no sibling list corruption */
1681}
1682
1683/**
1684 * xmlXPathNodeSetSort:
1685 * @set: the node set
1686 *
1687 * Sort the node set in document order
1688 */
1689void
1690xmlXPathNodeSetSort(xmlNodeSetPtr set) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001691 int i, j, incr, len;
Owen Taylor3473f882001-02-23 17:55:21 +00001692 xmlNodePtr tmp;
1693
1694 if (set == NULL)
1695 return;
1696
1697 /* Use Shell's sort to sort the node-set */
1698 len = set->nodeNr;
1699 for (incr = len / 2; incr > 0; incr /= 2) {
1700 for (i = incr; i < len; i++) {
1701 j = i - incr;
1702 while (j >= 0) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001703 if (xmlXPathCmpNodes(set->nodeTab[j],
1704 set->nodeTab[j + incr]) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +00001705 tmp = set->nodeTab[j];
1706 set->nodeTab[j] = set->nodeTab[j + incr];
1707 set->nodeTab[j + incr] = tmp;
1708 j -= incr;
1709 } else
1710 break;
1711 }
1712 }
1713 }
1714}
1715
1716#define XML_NODESET_DEFAULT 10
1717/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001718 * xmlXPathNodeSetDupNs:
1719 * @node: the parent node of the namespace XPath node
1720 * @ns: the libxml namespace declaration node.
1721 *
1722 * Namespace node in libxml don't match the XPath semantic. In a node set
1723 * the namespace nodes are duplicated and the next pointer is set to the
1724 * parent node in the XPath semantic.
1725 *
1726 * Returns the newly created object.
1727 */
1728static xmlNodePtr
1729xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) {
1730 xmlNsPtr cur;
1731
1732 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
1733 return(NULL);
1734 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
1735 return((xmlNodePtr) ns);
1736
1737 /*
1738 * Allocate a new Namespace and fill the fields.
1739 */
1740 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
1741 if (cur == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001742 xmlXPathErrMemory(NULL, "duplicating namespace\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001743 return(NULL);
1744 }
1745 memset(cur, 0, sizeof(xmlNs));
1746 cur->type = XML_NAMESPACE_DECL;
1747 if (ns->href != NULL)
1748 cur->href = xmlStrdup(ns->href);
1749 if (ns->prefix != NULL)
1750 cur->prefix = xmlStrdup(ns->prefix);
1751 cur->next = (xmlNsPtr) node;
1752 return((xmlNodePtr) cur);
1753}
1754
1755/**
1756 * xmlXPathNodeSetFreeNs:
1757 * @ns: the XPath namespace node found in a nodeset.
1758 *
William M. Brack08171912003-12-29 02:52:11 +00001759 * Namespace nodes in libxml don't match the XPath semantic. In a node set
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001760 * the namespace nodes are duplicated and the next pointer is set to the
William M. Brack08171912003-12-29 02:52:11 +00001761 * parent node in the XPath semantic. Check if such a node needs to be freed
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001762 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001763void
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001764xmlXPathNodeSetFreeNs(xmlNsPtr ns) {
1765 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
1766 return;
1767
1768 if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) {
1769 if (ns->href != NULL)
1770 xmlFree((xmlChar *)ns->href);
1771 if (ns->prefix != NULL)
1772 xmlFree((xmlChar *)ns->prefix);
1773 xmlFree(ns);
1774 }
1775}
1776
1777/**
Owen Taylor3473f882001-02-23 17:55:21 +00001778 * xmlXPathNodeSetCreate:
1779 * @val: an initial xmlNodePtr, or NULL
1780 *
1781 * Create a new xmlNodeSetPtr of type double and of value @val
1782 *
1783 * Returns the newly created object.
1784 */
1785xmlNodeSetPtr
1786xmlXPathNodeSetCreate(xmlNodePtr val) {
1787 xmlNodeSetPtr ret;
1788
1789 ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
1790 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001791 xmlXPathErrMemory(NULL, "creating nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001792 return(NULL);
1793 }
1794 memset(ret, 0 , (size_t) sizeof(xmlNodeSet));
1795 if (val != NULL) {
1796 ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1797 sizeof(xmlNodePtr));
1798 if (ret->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001799 xmlXPathErrMemory(NULL, "creating nodeset\n");
1800 xmlFree(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001801 return(NULL);
1802 }
1803 memset(ret->nodeTab, 0 ,
1804 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1805 ret->nodeMax = XML_NODESET_DEFAULT;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001806 if (val->type == XML_NAMESPACE_DECL) {
1807 xmlNsPtr ns = (xmlNsPtr) val;
1808
1809 ret->nodeTab[ret->nodeNr++] =
1810 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1811 } else
1812 ret->nodeTab[ret->nodeNr++] = val;
Owen Taylor3473f882001-02-23 17:55:21 +00001813 }
1814 return(ret);
1815}
1816
1817/**
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001818 * xmlXPathNodeSetContains:
1819 * @cur: the node-set
1820 * @val: the node
1821 *
1822 * checks whether @cur contains @val
1823 *
1824 * Returns true (1) if @cur contains @val, false (0) otherwise
1825 */
1826int
1827xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) {
1828 int i;
1829
Daniel Veillarda82b1822004-11-08 16:24:57 +00001830 if ((cur == NULL) || (val == NULL)) return(0);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001831 if (val->type == XML_NAMESPACE_DECL) {
1832 for (i = 0; i < cur->nodeNr; i++) {
1833 if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) {
1834 xmlNsPtr ns1, ns2;
1835
1836 ns1 = (xmlNsPtr) val;
1837 ns2 = (xmlNsPtr) cur->nodeTab[i];
1838 if (ns1 == ns2)
1839 return(1);
1840 if ((ns1->next != NULL) && (ns2->next == ns1->next) &&
1841 (xmlStrEqual(ns1->prefix, ns2->prefix)))
1842 return(1);
1843 }
1844 }
1845 } else {
1846 for (i = 0; i < cur->nodeNr; i++) {
1847 if (cur->nodeTab[i] == val)
1848 return(1);
1849 }
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001850 }
1851 return(0);
1852}
1853
1854/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001855 * xmlXPathNodeSetAddNs:
1856 * @cur: the initial node set
1857 * @node: the hosting node
1858 * @ns: a the namespace node
1859 *
1860 * add a new namespace node to an existing NodeSet
1861 */
Aleksey Sanin79376ba2002-05-14 06:41:32 +00001862void
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001863xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) {
1864 int i;
1865
Daniel Veillarda82b1822004-11-08 16:24:57 +00001866
1867 if ((cur == NULL) || (ns == NULL) || (node == NULL) ||
1868 (ns->type != XML_NAMESPACE_DECL) ||
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001869 (node->type != XML_ELEMENT_NODE))
1870 return;
1871
William M. Brack08171912003-12-29 02:52:11 +00001872 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001873 /*
William M. Brack08171912003-12-29 02:52:11 +00001874 * prevent duplicates
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001875 */
1876 for (i = 0;i < cur->nodeNr;i++) {
1877 if ((cur->nodeTab[i] != NULL) &&
1878 (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) &&
Daniel Veillardc62a1472002-03-19 18:35:12 +00001879 (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) &&
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001880 (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix)))
1881 return;
1882 }
1883
1884 /*
1885 * grow the nodeTab if needed
1886 */
1887 if (cur->nodeMax == 0) {
1888 cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1889 sizeof(xmlNodePtr));
1890 if (cur->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001891 xmlXPathErrMemory(NULL, "growing nodeset\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001892 return;
1893 }
1894 memset(cur->nodeTab, 0 ,
1895 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1896 cur->nodeMax = XML_NODESET_DEFAULT;
1897 } else if (cur->nodeNr == cur->nodeMax) {
1898 xmlNodePtr *temp;
1899
1900 cur->nodeMax *= 2;
1901 temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
1902 sizeof(xmlNodePtr));
1903 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001904 xmlXPathErrMemory(NULL, "growing nodeset\n");
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001905 return;
1906 }
1907 cur->nodeTab = temp;
1908 }
1909 cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns);
1910}
1911
1912/**
Owen Taylor3473f882001-02-23 17:55:21 +00001913 * xmlXPathNodeSetAdd:
1914 * @cur: the initial node set
1915 * @val: a new xmlNodePtr
1916 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001917 * add a new xmlNodePtr to an existing NodeSet
Owen Taylor3473f882001-02-23 17:55:21 +00001918 */
1919void
1920xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) {
1921 int i;
1922
Daniel Veillarda82b1822004-11-08 16:24:57 +00001923 if ((cur == NULL) || (val == NULL)) return;
Owen Taylor3473f882001-02-23 17:55:21 +00001924
Daniel Veillardef0b4502003-03-24 13:57:34 +00001925#if 0
Daniel Veillard652d8a92003-02-04 19:28:49 +00001926 if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' '))
1927 return; /* an XSLT fake node */
Daniel Veillardef0b4502003-03-24 13:57:34 +00001928#endif
Daniel Veillard652d8a92003-02-04 19:28:49 +00001929
William M. Brack08171912003-12-29 02:52:11 +00001930 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00001931 /*
William M. Brack08171912003-12-29 02:52:11 +00001932 * prevent duplcates
Owen Taylor3473f882001-02-23 17:55:21 +00001933 */
1934 for (i = 0;i < cur->nodeNr;i++)
1935 if (cur->nodeTab[i] == val) return;
1936
1937 /*
1938 * grow the nodeTab if needed
1939 */
1940 if (cur->nodeMax == 0) {
1941 cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1942 sizeof(xmlNodePtr));
1943 if (cur->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001944 xmlXPathErrMemory(NULL, "growing nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001945 return;
1946 }
1947 memset(cur->nodeTab, 0 ,
1948 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1949 cur->nodeMax = XML_NODESET_DEFAULT;
1950 } else if (cur->nodeNr == cur->nodeMax) {
1951 xmlNodePtr *temp;
1952
1953 cur->nodeMax *= 2;
1954 temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
1955 sizeof(xmlNodePtr));
1956 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001957 xmlXPathErrMemory(NULL, "growing nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001958 return;
1959 }
1960 cur->nodeTab = temp;
1961 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001962 if (val->type == XML_NAMESPACE_DECL) {
1963 xmlNsPtr ns = (xmlNsPtr) val;
1964
1965 cur->nodeTab[cur->nodeNr++] =
1966 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1967 } else
1968 cur->nodeTab[cur->nodeNr++] = val;
Owen Taylor3473f882001-02-23 17:55:21 +00001969}
1970
1971/**
1972 * xmlXPathNodeSetAddUnique:
1973 * @cur: the initial node set
1974 * @val: a new xmlNodePtr
1975 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001976 * add a new xmlNodePtr to an existing NodeSet, optimized version
Owen Taylor3473f882001-02-23 17:55:21 +00001977 * when we are sure the node is not already in the set.
1978 */
1979void
1980xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00001981 if ((cur == NULL) || (val == NULL)) return;
Owen Taylor3473f882001-02-23 17:55:21 +00001982
Daniel Veillardef0b4502003-03-24 13:57:34 +00001983#if 0
Daniel Veillard652d8a92003-02-04 19:28:49 +00001984 if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' '))
1985 return; /* an XSLT fake node */
Daniel Veillardef0b4502003-03-24 13:57:34 +00001986#endif
Daniel Veillard652d8a92003-02-04 19:28:49 +00001987
William M. Brack08171912003-12-29 02:52:11 +00001988 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00001989 /*
1990 * grow the nodeTab if needed
1991 */
1992 if (cur->nodeMax == 0) {
1993 cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1994 sizeof(xmlNodePtr));
1995 if (cur->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00001996 xmlXPathErrMemory(NULL, "growing nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001997 return;
1998 }
1999 memset(cur->nodeTab, 0 ,
2000 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
2001 cur->nodeMax = XML_NODESET_DEFAULT;
2002 } else if (cur->nodeNr == cur->nodeMax) {
2003 xmlNodePtr *temp;
2004
2005 cur->nodeMax *= 2;
2006 temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
2007 sizeof(xmlNodePtr));
2008 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002009 xmlXPathErrMemory(NULL, "growing nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002010 return;
2011 }
2012 cur->nodeTab = temp;
2013 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002014 if (val->type == XML_NAMESPACE_DECL) {
2015 xmlNsPtr ns = (xmlNsPtr) val;
2016
2017 cur->nodeTab[cur->nodeNr++] =
2018 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
2019 } else
2020 cur->nodeTab[cur->nodeNr++] = val;
Owen Taylor3473f882001-02-23 17:55:21 +00002021}
2022
2023/**
2024 * xmlXPathNodeSetMerge:
2025 * @val1: the first NodeSet or NULL
2026 * @val2: the second NodeSet
2027 *
2028 * Merges two nodesets, all nodes from @val2 are added to @val1
2029 * if @val1 is NULL, a new set is created and copied from @val2
2030 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002031 * Returns @val1 once extended or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00002032 */
2033xmlNodeSetPtr
2034xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00002035 int i, j, initNr, skip;
Owen Taylor3473f882001-02-23 17:55:21 +00002036
2037 if (val2 == NULL) return(val1);
2038 if (val1 == NULL) {
2039 val1 = xmlXPathNodeSetCreate(NULL);
2040 }
2041
William M. Brack08171912003-12-29 02:52:11 +00002042 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00002043 initNr = val1->nodeNr;
2044
2045 for (i = 0;i < val2->nodeNr;i++) {
2046 /*
William M. Brack08171912003-12-29 02:52:11 +00002047 * check against duplicates
Owen Taylor3473f882001-02-23 17:55:21 +00002048 */
Daniel Veillardd8df6c02001-04-05 16:54:14 +00002049 skip = 0;
2050 for (j = 0; j < initNr; j++) {
2051 if (val1->nodeTab[j] == val2->nodeTab[i]) {
2052 skip = 1;
2053 break;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002054 } else if ((val1->nodeTab[j]->type == XML_NAMESPACE_DECL) &&
2055 (val2->nodeTab[i]->type == XML_NAMESPACE_DECL)) {
2056 xmlNsPtr ns1, ns2;
2057 ns1 = (xmlNsPtr) val1->nodeTab[j];
2058 ns2 = (xmlNsPtr) val2->nodeTab[i];
2059 if ((ns1->next == ns2->next) &&
2060 (xmlStrEqual(ns1->prefix, ns2->prefix))) {
2061 skip = 1;
2062 break;
2063 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00002064 }
2065 }
2066 if (skip)
2067 continue;
Owen Taylor3473f882001-02-23 17:55:21 +00002068
2069 /*
2070 * grow the nodeTab if needed
2071 */
2072 if (val1->nodeMax == 0) {
2073 val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
2074 sizeof(xmlNodePtr));
2075 if (val1->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002076 xmlXPathErrMemory(NULL, "merging nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002077 return(NULL);
2078 }
2079 memset(val1->nodeTab, 0 ,
2080 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
2081 val1->nodeMax = XML_NODESET_DEFAULT;
2082 } else if (val1->nodeNr == val1->nodeMax) {
2083 xmlNodePtr *temp;
2084
2085 val1->nodeMax *= 2;
2086 temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax *
2087 sizeof(xmlNodePtr));
2088 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002089 xmlXPathErrMemory(NULL, "merging nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002090 return(NULL);
2091 }
2092 val1->nodeTab = temp;
2093 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002094 if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) {
2095 xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i];
2096
2097 val1->nodeTab[val1->nodeNr++] =
2098 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
2099 } else
2100 val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i];
Owen Taylor3473f882001-02-23 17:55:21 +00002101 }
2102
2103 return(val1);
2104}
2105
2106/**
Daniel Veillard75be0132002-03-13 10:03:35 +00002107 * xmlXPathNodeSetMergeUnique:
2108 * @val1: the first NodeSet or NULL
2109 * @val2: the second NodeSet
2110 *
2111 * Merges two nodesets, all nodes from @val2 are added to @val1
2112 * if @val1 is NULL, a new set is created and copied from @val2
2113 *
2114 * Returns @val1 once extended or NULL in case of error.
2115 */
2116static xmlNodeSetPtr
2117xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
William M. Brack78637da2003-07-31 14:47:38 +00002118 int i;
Daniel Veillard75be0132002-03-13 10:03:35 +00002119
2120 if (val2 == NULL) return(val1);
2121 if (val1 == NULL) {
2122 val1 = xmlXPathNodeSetCreate(NULL);
2123 }
2124
William M. Brack08171912003-12-29 02:52:11 +00002125 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Daniel Veillard75be0132002-03-13 10:03:35 +00002126
2127 for (i = 0;i < val2->nodeNr;i++) {
2128 /*
2129 * grow the nodeTab if needed
2130 */
2131 if (val1->nodeMax == 0) {
2132 val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
2133 sizeof(xmlNodePtr));
2134 if (val1->nodeTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002135 xmlXPathErrMemory(NULL, "merging nodeset\n");
Daniel Veillard75be0132002-03-13 10:03:35 +00002136 return(NULL);
2137 }
2138 memset(val1->nodeTab, 0 ,
2139 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
2140 val1->nodeMax = XML_NODESET_DEFAULT;
2141 } else if (val1->nodeNr == val1->nodeMax) {
2142 xmlNodePtr *temp;
2143
2144 val1->nodeMax *= 2;
2145 temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax *
2146 sizeof(xmlNodePtr));
2147 if (temp == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002148 xmlXPathErrMemory(NULL, "merging nodeset\n");
Daniel Veillard75be0132002-03-13 10:03:35 +00002149 return(NULL);
2150 }
2151 val1->nodeTab = temp;
2152 }
2153 if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) {
2154 xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i];
2155
2156 val1->nodeTab[val1->nodeNr++] =
2157 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
2158 } else
2159 val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i];
2160 }
2161
2162 return(val1);
2163}
2164
2165/**
Owen Taylor3473f882001-02-23 17:55:21 +00002166 * xmlXPathNodeSetDel:
2167 * @cur: the initial node set
2168 * @val: an xmlNodePtr
2169 *
2170 * Removes an xmlNodePtr from an existing NodeSet
2171 */
2172void
2173xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) {
2174 int i;
2175
2176 if (cur == NULL) return;
2177 if (val == NULL) return;
2178
2179 /*
William M. Brack08171912003-12-29 02:52:11 +00002180 * find node in nodeTab
Owen Taylor3473f882001-02-23 17:55:21 +00002181 */
2182 for (i = 0;i < cur->nodeNr;i++)
2183 if (cur->nodeTab[i] == val) break;
2184
William M. Brack08171912003-12-29 02:52:11 +00002185 if (i >= cur->nodeNr) { /* not found */
Owen Taylor3473f882001-02-23 17:55:21 +00002186#ifdef DEBUG
2187 xmlGenericError(xmlGenericErrorContext,
2188 "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n",
2189 val->name);
2190#endif
2191 return;
2192 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002193 if ((cur->nodeTab[i] != NULL) &&
2194 (cur->nodeTab[i]->type == XML_NAMESPACE_DECL))
2195 xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00002196 cur->nodeNr--;
2197 for (;i < cur->nodeNr;i++)
2198 cur->nodeTab[i] = cur->nodeTab[i + 1];
2199 cur->nodeTab[cur->nodeNr] = NULL;
2200}
2201
2202/**
2203 * xmlXPathNodeSetRemove:
2204 * @cur: the initial node set
2205 * @val: the index to remove
2206 *
2207 * Removes an entry from an existing NodeSet list.
2208 */
2209void
2210xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) {
2211 if (cur == NULL) return;
2212 if (val >= cur->nodeNr) return;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002213 if ((cur->nodeTab[val] != NULL) &&
2214 (cur->nodeTab[val]->type == XML_NAMESPACE_DECL))
2215 xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]);
Owen Taylor3473f882001-02-23 17:55:21 +00002216 cur->nodeNr--;
2217 for (;val < cur->nodeNr;val++)
2218 cur->nodeTab[val] = cur->nodeTab[val + 1];
2219 cur->nodeTab[cur->nodeNr] = NULL;
2220}
2221
2222/**
2223 * xmlXPathFreeNodeSet:
2224 * @obj: the xmlNodeSetPtr to free
2225 *
2226 * Free the NodeSet compound (not the actual nodes !).
2227 */
2228void
2229xmlXPathFreeNodeSet(xmlNodeSetPtr obj) {
2230 if (obj == NULL) return;
2231 if (obj->nodeTab != NULL) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002232 int i;
2233
William M. Brack08171912003-12-29 02:52:11 +00002234 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002235 for (i = 0;i < obj->nodeNr;i++)
2236 if ((obj->nodeTab[i] != NULL) &&
2237 (obj->nodeTab[i]->type == XML_NAMESPACE_DECL))
2238 xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]);
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/**
2245 * xmlXPathFreeValueTree:
2246 * @obj: the xmlNodeSetPtr to free
2247 *
2248 * Free the NodeSet compound and the actual tree, this is different
2249 * from xmlXPathFreeNodeSet()
2250 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002251static void
Owen Taylor3473f882001-02-23 17:55:21 +00002252xmlXPathFreeValueTree(xmlNodeSetPtr obj) {
2253 int i;
2254
2255 if (obj == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002256
2257 if (obj->nodeTab != NULL) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002258 for (i = 0;i < obj->nodeNr;i++) {
2259 if (obj->nodeTab[i] != NULL) {
2260 if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) {
2261 xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]);
2262 } else {
2263 xmlFreeNodeList(obj->nodeTab[i]);
2264 }
2265 }
2266 }
Owen Taylor3473f882001-02-23 17:55:21 +00002267 xmlFree(obj->nodeTab);
2268 }
Owen Taylor3473f882001-02-23 17:55:21 +00002269 xmlFree(obj);
2270}
2271
2272#if defined(DEBUG) || defined(DEBUG_STEP)
2273/**
2274 * xmlGenericErrorContextNodeSet:
2275 * @output: a FILE * for the output
William M. Brack08171912003-12-29 02:52:11 +00002276 * @obj: the xmlNodeSetPtr to display
Owen Taylor3473f882001-02-23 17:55:21 +00002277 *
2278 * Quick display of a NodeSet
2279 */
2280void
2281xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) {
2282 int i;
2283
2284 if (output == NULL) output = xmlGenericErrorContext;
2285 if (obj == NULL) {
2286 fprintf(output, "NodeSet == NULL !\n");
2287 return;
2288 }
2289 if (obj->nodeNr == 0) {
2290 fprintf(output, "NodeSet is empty\n");
2291 return;
2292 }
2293 if (obj->nodeTab == NULL) {
2294 fprintf(output, " nodeTab == NULL !\n");
2295 return;
2296 }
2297 for (i = 0; i < obj->nodeNr; i++) {
2298 if (obj->nodeTab[i] == NULL) {
2299 fprintf(output, " NULL !\n");
2300 return;
2301 }
2302 if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) ||
2303 (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE))
2304 fprintf(output, " /");
2305 else if (obj->nodeTab[i]->name == NULL)
2306 fprintf(output, " noname!");
2307 else fprintf(output, " %s", obj->nodeTab[i]->name);
2308 }
2309 fprintf(output, "\n");
2310}
2311#endif
2312
2313/**
2314 * xmlXPathNewNodeSet:
2315 * @val: the NodePtr value
2316 *
2317 * Create a new xmlXPathObjectPtr of type NodeSet and initialize
2318 * it with the single Node @val
2319 *
2320 * Returns the newly created object.
2321 */
2322xmlXPathObjectPtr
2323xmlXPathNewNodeSet(xmlNodePtr val) {
2324 xmlXPathObjectPtr ret;
2325
2326 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2327 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002328 xmlXPathErrMemory(NULL, "creating nodeset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002329 return(NULL);
2330 }
2331 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2332 ret->type = XPATH_NODESET;
Daniel Veillard77851712001-02-27 21:54:07 +00002333 ret->boolval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002334 ret->nodesetval = xmlXPathNodeSetCreate(val);
William M. Brack08171912003-12-29 02:52:11 +00002335 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00002336 return(ret);
2337}
2338
2339/**
2340 * xmlXPathNewValueTree:
2341 * @val: the NodePtr value
2342 *
2343 * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize
2344 * it with the tree root @val
2345 *
2346 * Returns the newly created object.
2347 */
2348xmlXPathObjectPtr
2349xmlXPathNewValueTree(xmlNodePtr val) {
2350 xmlXPathObjectPtr ret;
2351
2352 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2353 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002354 xmlXPathErrMemory(NULL, "creating result value tree\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002355 return(NULL);
2356 }
2357 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2358 ret->type = XPATH_XSLT_TREE;
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00002359 ret->boolval = 1;
2360 ret->user = (void *) val;
Owen Taylor3473f882001-02-23 17:55:21 +00002361 ret->nodesetval = xmlXPathNodeSetCreate(val);
2362 return(ret);
2363}
2364
2365/**
2366 * xmlXPathNewNodeSetList:
2367 * @val: an existing NodeSet
2368 *
2369 * Create a new xmlXPathObjectPtr of type NodeSet and initialize
2370 * it with the Nodeset @val
2371 *
2372 * Returns the newly created object.
2373 */
2374xmlXPathObjectPtr
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002375xmlXPathNewNodeSetList(xmlNodeSetPtr val)
2376{
Owen Taylor3473f882001-02-23 17:55:21 +00002377 xmlXPathObjectPtr ret;
2378 int i;
2379
2380 if (val == NULL)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002381 ret = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002382 else if (val->nodeTab == NULL)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002383 ret = xmlXPathNewNodeSet(NULL);
2384 else {
2385 ret = xmlXPathNewNodeSet(val->nodeTab[0]);
2386 for (i = 1; i < val->nodeNr; ++i)
2387 xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]);
2388 }
Owen Taylor3473f882001-02-23 17:55:21 +00002389
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002390 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002391}
2392
2393/**
2394 * xmlXPathWrapNodeSet:
2395 * @val: the NodePtr value
2396 *
2397 * Wrap the Nodeset @val in a new xmlXPathObjectPtr
2398 *
2399 * Returns the newly created object.
2400 */
2401xmlXPathObjectPtr
2402xmlXPathWrapNodeSet(xmlNodeSetPtr val) {
2403 xmlXPathObjectPtr ret;
2404
2405 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2406 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00002407 xmlXPathErrMemory(NULL, "creating node set object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002408 return(NULL);
2409 }
2410 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2411 ret->type = XPATH_NODESET;
2412 ret->nodesetval = val;
2413 return(ret);
2414}
2415
2416/**
2417 * xmlXPathFreeNodeSetList:
2418 * @obj: an existing NodeSetList object
2419 *
2420 * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in
2421 * the list contrary to xmlXPathFreeObject().
2422 */
2423void
2424xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) {
2425 if (obj == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002426 xmlFree(obj);
2427}
2428
Thomas Broyerf06a3d82001-07-16 04:52:57 +00002429/**
2430 * xmlXPathDifference:
2431 * @nodes1: a node-set
2432 * @nodes2: a node-set
2433 *
2434 * Implements the EXSLT - Sets difference() function:
2435 * node-set set:difference (node-set, node-set)
2436 *
2437 * Returns the difference between the two node sets, or nodes1 if
2438 * nodes2 is empty
2439 */
2440xmlNodeSetPtr
2441xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2442 xmlNodeSetPtr ret;
2443 int i, l1;
2444 xmlNodePtr cur;
2445
2446 if (xmlXPathNodeSetIsEmpty(nodes2))
2447 return(nodes1);
2448
2449 ret = xmlXPathNodeSetCreate(NULL);
2450 if (xmlXPathNodeSetIsEmpty(nodes1))
2451 return(ret);
2452
2453 l1 = xmlXPathNodeSetGetLength(nodes1);
2454
2455 for (i = 0; i < l1; i++) {
2456 cur = xmlXPathNodeSetItem(nodes1, i);
2457 if (!xmlXPathNodeSetContains(nodes2, cur))
2458 xmlXPathNodeSetAddUnique(ret, cur);
2459 }
2460 return(ret);
2461}
2462
2463/**
2464 * xmlXPathIntersection:
2465 * @nodes1: a node-set
2466 * @nodes2: a node-set
2467 *
2468 * Implements the EXSLT - Sets intersection() function:
2469 * node-set set:intersection (node-set, node-set)
2470 *
2471 * Returns a node set comprising the nodes that are within both the
2472 * node sets passed as arguments
2473 */
2474xmlNodeSetPtr
2475xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2476 xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL);
2477 int i, l1;
2478 xmlNodePtr cur;
2479
2480 if (xmlXPathNodeSetIsEmpty(nodes1))
2481 return(ret);
2482 if (xmlXPathNodeSetIsEmpty(nodes2))
2483 return(ret);
2484
2485 l1 = xmlXPathNodeSetGetLength(nodes1);
2486
2487 for (i = 0; i < l1; i++) {
2488 cur = xmlXPathNodeSetItem(nodes1, i);
2489 if (xmlXPathNodeSetContains(nodes2, cur))
2490 xmlXPathNodeSetAddUnique(ret, cur);
2491 }
2492 return(ret);
2493}
2494
2495/**
2496 * xmlXPathDistinctSorted:
2497 * @nodes: a node-set, sorted by document order
2498 *
2499 * Implements the EXSLT - Sets distinct() function:
2500 * node-set set:distinct (node-set)
2501 *
2502 * Returns a subset of the nodes contained in @nodes, or @nodes if
2503 * it is empty
2504 */
2505xmlNodeSetPtr
2506xmlXPathDistinctSorted (xmlNodeSetPtr nodes) {
2507 xmlNodeSetPtr ret;
2508 xmlHashTablePtr hash;
2509 int i, l;
2510 xmlChar * strval;
2511 xmlNodePtr cur;
2512
2513 if (xmlXPathNodeSetIsEmpty(nodes))
2514 return(nodes);
2515
2516 ret = xmlXPathNodeSetCreate(NULL);
2517 l = xmlXPathNodeSetGetLength(nodes);
2518 hash = xmlHashCreate (l);
2519 for (i = 0; i < l; i++) {
2520 cur = xmlXPathNodeSetItem(nodes, i);
2521 strval = xmlXPathCastNodeToString(cur);
2522 if (xmlHashLookup(hash, strval) == NULL) {
2523 xmlHashAddEntry(hash, strval, strval);
2524 xmlXPathNodeSetAddUnique(ret, cur);
2525 } else {
2526 xmlFree(strval);
2527 }
2528 }
2529 xmlHashFree(hash, (xmlHashDeallocator) xmlFree);
2530 return(ret);
2531}
2532
2533/**
2534 * xmlXPathDistinct:
2535 * @nodes: a node-set
2536 *
2537 * Implements the EXSLT - Sets distinct() function:
2538 * node-set set:distinct (node-set)
2539 * @nodes is sorted by document order, then #exslSetsDistinctSorted
2540 * is called with the sorted node-set
2541 *
2542 * Returns a subset of the nodes contained in @nodes, or @nodes if
2543 * it is empty
2544 */
2545xmlNodeSetPtr
2546xmlXPathDistinct (xmlNodeSetPtr nodes) {
2547 if (xmlXPathNodeSetIsEmpty(nodes))
2548 return(nodes);
2549
2550 xmlXPathNodeSetSort(nodes);
2551 return(xmlXPathDistinctSorted(nodes));
2552}
2553
2554/**
2555 * xmlXPathHasSameNodes:
2556 * @nodes1: a node-set
2557 * @nodes2: a node-set
2558 *
2559 * Implements the EXSLT - Sets has-same-nodes function:
2560 * boolean set:has-same-node(node-set, node-set)
2561 *
2562 * Returns true (1) if @nodes1 shares any node with @nodes2, false (0)
2563 * otherwise
2564 */
2565int
2566xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2567 int i, l;
2568 xmlNodePtr cur;
2569
2570 if (xmlXPathNodeSetIsEmpty(nodes1) ||
2571 xmlXPathNodeSetIsEmpty(nodes2))
2572 return(0);
2573
2574 l = xmlXPathNodeSetGetLength(nodes1);
2575 for (i = 0; i < l; i++) {
2576 cur = xmlXPathNodeSetItem(nodes1, i);
2577 if (xmlXPathNodeSetContains(nodes2, cur))
2578 return(1);
2579 }
2580 return(0);
2581}
2582
2583/**
2584 * xmlXPathNodeLeadingSorted:
2585 * @nodes: a node-set, sorted by document order
2586 * @node: a node
2587 *
2588 * Implements the EXSLT - Sets leading() function:
2589 * node-set set:leading (node-set, node-set)
2590 *
2591 * Returns the nodes in @nodes that precede @node in document order,
2592 * @nodes if @node is NULL or an empty node-set if @nodes
2593 * doesn't contain @node
2594 */
2595xmlNodeSetPtr
2596xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) {
2597 int i, l;
2598 xmlNodePtr cur;
2599 xmlNodeSetPtr ret;
2600
2601 if (node == NULL)
2602 return(nodes);
2603
2604 ret = xmlXPathNodeSetCreate(NULL);
2605 if (xmlXPathNodeSetIsEmpty(nodes) ||
2606 (!xmlXPathNodeSetContains(nodes, node)))
2607 return(ret);
2608
2609 l = xmlXPathNodeSetGetLength(nodes);
2610 for (i = 0; i < l; i++) {
2611 cur = xmlXPathNodeSetItem(nodes, i);
2612 if (cur == node)
2613 break;
2614 xmlXPathNodeSetAddUnique(ret, cur);
2615 }
2616 return(ret);
2617}
2618
2619/**
2620 * xmlXPathNodeLeading:
2621 * @nodes: a node-set
2622 * @node: a node
2623 *
2624 * Implements the EXSLT - Sets leading() function:
2625 * node-set set:leading (node-set, node-set)
2626 * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted
2627 * is called.
2628 *
2629 * Returns the nodes in @nodes that precede @node in document order,
2630 * @nodes if @node is NULL or an empty node-set if @nodes
2631 * doesn't contain @node
2632 */
2633xmlNodeSetPtr
2634xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) {
2635 xmlXPathNodeSetSort(nodes);
2636 return(xmlXPathNodeLeadingSorted(nodes, node));
2637}
2638
2639/**
2640 * xmlXPathLeadingSorted:
2641 * @nodes1: a node-set, sorted by document order
2642 * @nodes2: a node-set, sorted by document order
2643 *
2644 * Implements the EXSLT - Sets leading() function:
2645 * node-set set:leading (node-set, node-set)
2646 *
2647 * Returns the nodes in @nodes1 that precede the first node in @nodes2
2648 * in document order, @nodes1 if @nodes2 is NULL or empty or
2649 * an empty node-set if @nodes1 doesn't contain @nodes2
2650 */
2651xmlNodeSetPtr
2652xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2653 if (xmlXPathNodeSetIsEmpty(nodes2))
2654 return(nodes1);
2655 return(xmlXPathNodeLeadingSorted(nodes1,
2656 xmlXPathNodeSetItem(nodes2, 1)));
2657}
2658
2659/**
2660 * xmlXPathLeading:
2661 * @nodes1: a node-set
2662 * @nodes2: a node-set
2663 *
2664 * Implements the EXSLT - Sets leading() function:
2665 * node-set set:leading (node-set, node-set)
2666 * @nodes1 and @nodes2 are sorted by document order, then
2667 * #exslSetsLeadingSorted is called.
2668 *
2669 * Returns the nodes in @nodes1 that precede the first node in @nodes2
2670 * in document order, @nodes1 if @nodes2 is NULL or empty or
2671 * an empty node-set if @nodes1 doesn't contain @nodes2
2672 */
2673xmlNodeSetPtr
2674xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2675 if (xmlXPathNodeSetIsEmpty(nodes2))
2676 return(nodes1);
2677 if (xmlXPathNodeSetIsEmpty(nodes1))
2678 return(xmlXPathNodeSetCreate(NULL));
2679 xmlXPathNodeSetSort(nodes1);
2680 xmlXPathNodeSetSort(nodes2);
2681 return(xmlXPathNodeLeadingSorted(nodes1,
2682 xmlXPathNodeSetItem(nodes2, 1)));
2683}
2684
2685/**
2686 * xmlXPathNodeTrailingSorted:
2687 * @nodes: a node-set, sorted by document order
2688 * @node: a node
2689 *
2690 * Implements the EXSLT - Sets trailing() function:
2691 * node-set set:trailing (node-set, node-set)
2692 *
2693 * Returns the nodes in @nodes that follow @node in document order,
2694 * @nodes if @node is NULL or an empty node-set if @nodes
2695 * doesn't contain @node
2696 */
2697xmlNodeSetPtr
2698xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) {
2699 int i, l;
2700 xmlNodePtr cur;
2701 xmlNodeSetPtr ret;
2702
2703 if (node == NULL)
2704 return(nodes);
2705
2706 ret = xmlXPathNodeSetCreate(NULL);
2707 if (xmlXPathNodeSetIsEmpty(nodes) ||
2708 (!xmlXPathNodeSetContains(nodes, node)))
2709 return(ret);
2710
2711 l = xmlXPathNodeSetGetLength(nodes);
Thomas Broyerf186c822001-07-31 23:30:37 +00002712 for (i = l; i > 0; i--) {
Thomas Broyerf06a3d82001-07-16 04:52:57 +00002713 cur = xmlXPathNodeSetItem(nodes, i);
2714 if (cur == node)
2715 break;
2716 xmlXPathNodeSetAddUnique(ret, cur);
2717 }
2718 return(ret);
2719}
2720
2721/**
2722 * xmlXPathNodeTrailing:
2723 * @nodes: a node-set
2724 * @node: a node
2725 *
2726 * Implements the EXSLT - Sets trailing() function:
2727 * node-set set:trailing (node-set, node-set)
2728 * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted
2729 * is called.
2730 *
2731 * Returns the nodes in @nodes that follow @node in document order,
2732 * @nodes if @node is NULL or an empty node-set if @nodes
2733 * doesn't contain @node
2734 */
2735xmlNodeSetPtr
2736xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) {
2737 xmlXPathNodeSetSort(nodes);
2738 return(xmlXPathNodeTrailingSorted(nodes, node));
2739}
2740
2741/**
2742 * xmlXPathTrailingSorted:
2743 * @nodes1: a node-set, sorted by document order
2744 * @nodes2: a node-set, sorted by document order
2745 *
2746 * Implements the EXSLT - Sets trailing() function:
2747 * node-set set:trailing (node-set, node-set)
2748 *
2749 * Returns the nodes in @nodes1 that follow the first node in @nodes2
2750 * in document order, @nodes1 if @nodes2 is NULL or empty or
2751 * an empty node-set if @nodes1 doesn't contain @nodes2
2752 */
2753xmlNodeSetPtr
2754xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2755 if (xmlXPathNodeSetIsEmpty(nodes2))
2756 return(nodes1);
2757 return(xmlXPathNodeTrailingSorted(nodes1,
2758 xmlXPathNodeSetItem(nodes2, 0)));
2759}
2760
2761/**
2762 * xmlXPathTrailing:
2763 * @nodes1: a node-set
2764 * @nodes2: a node-set
2765 *
2766 * Implements the EXSLT - Sets trailing() function:
2767 * node-set set:trailing (node-set, node-set)
2768 * @nodes1 and @nodes2 are sorted by document order, then
2769 * #xmlXPathTrailingSorted is called.
2770 *
2771 * Returns the nodes in @nodes1 that follow the first node in @nodes2
2772 * in document order, @nodes1 if @nodes2 is NULL or empty or
2773 * an empty node-set if @nodes1 doesn't contain @nodes2
2774 */
2775xmlNodeSetPtr
2776xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2777 if (xmlXPathNodeSetIsEmpty(nodes2))
2778 return(nodes1);
2779 if (xmlXPathNodeSetIsEmpty(nodes1))
2780 return(xmlXPathNodeSetCreate(NULL));
2781 xmlXPathNodeSetSort(nodes1);
2782 xmlXPathNodeSetSort(nodes2);
2783 return(xmlXPathNodeTrailingSorted(nodes1,
2784 xmlXPathNodeSetItem(nodes2, 0)));
2785}
2786
Owen Taylor3473f882001-02-23 17:55:21 +00002787/************************************************************************
2788 * *
2789 * Routines to handle extra functions *
2790 * *
2791 ************************************************************************/
2792
2793/**
2794 * xmlXPathRegisterFunc:
2795 * @ctxt: the XPath context
2796 * @name: the function name
2797 * @f: the function implementation or NULL
2798 *
2799 * Register a new function. If @f is NULL it unregisters the function
2800 *
2801 * Returns 0 in case of success, -1 in case of error
2802 */
2803int
2804xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name,
2805 xmlXPathFunction f) {
2806 return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f));
2807}
2808
2809/**
2810 * xmlXPathRegisterFuncNS:
2811 * @ctxt: the XPath context
2812 * @name: the function name
2813 * @ns_uri: the function namespace URI
2814 * @f: the function implementation or NULL
2815 *
2816 * Register a new function. If @f is NULL it unregisters the function
2817 *
2818 * Returns 0 in case of success, -1 in case of error
2819 */
2820int
2821xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2822 const xmlChar *ns_uri, xmlXPathFunction f) {
2823 if (ctxt == NULL)
2824 return(-1);
2825 if (name == NULL)
2826 return(-1);
2827
2828 if (ctxt->funcHash == NULL)
2829 ctxt->funcHash = xmlHashCreate(0);
2830 if (ctxt->funcHash == NULL)
2831 return(-1);
Daniel Veillard94394cd2003-10-29 17:07:51 +00002832 if (f == NULL)
2833 return(xmlHashRemoveEntry2(ctxt->funcHash, name, ns_uri, NULL));
William M. Brackad0e67c2004-12-01 14:35:10 +00002834 return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, XML_CAST_FPTR(f)));
Owen Taylor3473f882001-02-23 17:55:21 +00002835}
2836
2837/**
Thomas Broyerba4ad322001-07-26 16:55:21 +00002838 * xmlXPathRegisterFuncLookup:
2839 * @ctxt: the XPath context
2840 * @f: the lookup function
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002841 * @funcCtxt: the lookup data
Thomas Broyerba4ad322001-07-26 16:55:21 +00002842 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002843 * Registers an external mechanism to do function lookup.
Thomas Broyerba4ad322001-07-26 16:55:21 +00002844 */
2845void
2846xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt,
2847 xmlXPathFuncLookupFunc f,
2848 void *funcCtxt) {
2849 if (ctxt == NULL)
2850 return;
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00002851 ctxt->funcLookupFunc = f;
Thomas Broyerba4ad322001-07-26 16:55:21 +00002852 ctxt->funcLookupData = funcCtxt;
2853}
2854
2855/**
Owen Taylor3473f882001-02-23 17:55:21 +00002856 * xmlXPathFunctionLookup:
2857 * @ctxt: the XPath context
2858 * @name: the function name
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
2866xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) {
Thomas Broyerba4ad322001-07-26 16:55:21 +00002867 if (ctxt == NULL)
2868 return (NULL);
2869
2870 if (ctxt->funcLookupFunc != NULL) {
2871 xmlXPathFunction ret;
Daniel Veillard99e55eb2002-01-21 08:56:29 +00002872 xmlXPathFuncLookupFunc f;
Thomas Broyerba4ad322001-07-26 16:55:21 +00002873
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00002874 f = ctxt->funcLookupFunc;
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002875 ret = f(ctxt->funcLookupData, name, NULL);
Thomas Broyerba4ad322001-07-26 16:55:21 +00002876 if (ret != NULL)
2877 return(ret);
2878 }
Owen Taylor3473f882001-02-23 17:55:21 +00002879 return(xmlXPathFunctionLookupNS(ctxt, name, NULL));
2880}
2881
2882/**
2883 * xmlXPathFunctionLookupNS:
2884 * @ctxt: the XPath context
2885 * @name: the function name
2886 * @ns_uri: the function namespace URI
2887 *
2888 * Search in the Function array of the context for the given
2889 * function.
2890 *
2891 * Returns the xmlXPathFunction or NULL if not found
2892 */
2893xmlXPathFunction
2894xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2895 const xmlChar *ns_uri) {
William M. Brackad0e67c2004-12-01 14:35:10 +00002896 xmlXPathFunction ret;
2897
Owen Taylor3473f882001-02-23 17:55:21 +00002898 if (ctxt == NULL)
2899 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002900 if (name == NULL)
2901 return(NULL);
2902
Thomas Broyerba4ad322001-07-26 16:55:21 +00002903 if (ctxt->funcLookupFunc != NULL) {
Daniel Veillard99e55eb2002-01-21 08:56:29 +00002904 xmlXPathFuncLookupFunc f;
Thomas Broyerba4ad322001-07-26 16:55:21 +00002905
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00002906 f = ctxt->funcLookupFunc;
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002907 ret = f(ctxt->funcLookupData, name, ns_uri);
Thomas Broyerba4ad322001-07-26 16:55:21 +00002908 if (ret != NULL)
2909 return(ret);
2910 }
2911
2912 if (ctxt->funcHash == NULL)
2913 return(NULL);
2914
William M. Brackad0e67c2004-12-01 14:35:10 +00002915 XML_CAST_FPTR(ret) = xmlHashLookup2(ctxt->funcHash, name, ns_uri);
2916 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002917}
2918
2919/**
2920 * xmlXPathRegisteredFuncsCleanup:
2921 * @ctxt: the XPath context
2922 *
2923 * Cleanup the XPath context data associated to registered functions
2924 */
2925void
2926xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) {
2927 if (ctxt == NULL)
2928 return;
2929
2930 xmlHashFree(ctxt->funcHash, NULL);
2931 ctxt->funcHash = NULL;
2932}
2933
2934/************************************************************************
2935 * *
William M. Brack08171912003-12-29 02:52:11 +00002936 * Routines to handle Variables *
Owen Taylor3473f882001-02-23 17:55:21 +00002937 * *
2938 ************************************************************************/
2939
2940/**
2941 * xmlXPathRegisterVariable:
2942 * @ctxt: the XPath context
2943 * @name: the variable name
2944 * @value: the variable value or NULL
2945 *
2946 * Register a new variable value. If @value is NULL it unregisters
2947 * the variable
2948 *
2949 * Returns 0 in case of success, -1 in case of error
2950 */
2951int
2952xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name,
2953 xmlXPathObjectPtr value) {
2954 return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value));
2955}
2956
2957/**
2958 * xmlXPathRegisterVariableNS:
2959 * @ctxt: the XPath context
2960 * @name: the variable name
2961 * @ns_uri: the variable namespace URI
2962 * @value: the variable value or NULL
2963 *
2964 * Register a new variable value. If @value is NULL it unregisters
2965 * the variable
2966 *
2967 * Returns 0 in case of success, -1 in case of error
2968 */
2969int
2970xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2971 const xmlChar *ns_uri,
2972 xmlXPathObjectPtr value) {
2973 if (ctxt == NULL)
2974 return(-1);
2975 if (name == NULL)
2976 return(-1);
2977
2978 if (ctxt->varHash == NULL)
2979 ctxt->varHash = xmlHashCreate(0);
2980 if (ctxt->varHash == NULL)
2981 return(-1);
Daniel Veillard94394cd2003-10-29 17:07:51 +00002982 if (value == NULL)
2983 return(xmlHashRemoveEntry2(ctxt->varHash, name, ns_uri,
2984 (xmlHashDeallocator)xmlXPathFreeObject));
Owen Taylor3473f882001-02-23 17:55:21 +00002985 return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri,
2986 (void *) value,
2987 (xmlHashDeallocator)xmlXPathFreeObject));
2988}
2989
2990/**
2991 * xmlXPathRegisterVariableLookup:
2992 * @ctxt: the XPath context
2993 * @f: the lookup function
2994 * @data: the lookup data
2995 *
2996 * register an external mechanism to do variable lookup
2997 */
2998void
2999xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt,
3000 xmlXPathVariableLookupFunc f, void *data) {
3001 if (ctxt == NULL)
3002 return;
Daniel Veillard6ebf3c42004-08-22 13:11:39 +00003003 ctxt->varLookupFunc = f;
Owen Taylor3473f882001-02-23 17:55:21 +00003004 ctxt->varLookupData = data;
3005}
3006
3007/**
3008 * xmlXPathVariableLookup:
3009 * @ctxt: the XPath context
3010 * @name: the variable name
3011 *
3012 * Search in the Variable array of the context for the given
3013 * variable value.
3014 *
Daniel Veillard73c9c042001-07-05 20:02:54 +00003015 * Returns a copy of the value or NULL if not found
Owen Taylor3473f882001-02-23 17:55:21 +00003016 */
3017xmlXPathObjectPtr
3018xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) {
3019 if (ctxt == NULL)
3020 return(NULL);
3021
3022 if (ctxt->varLookupFunc != NULL) {
3023 xmlXPathObjectPtr ret;
3024
3025 ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc)
3026 (ctxt->varLookupData, name, NULL);
Daniel Veillard556c6682001-10-06 09:59:51 +00003027 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003028 }
3029 return(xmlXPathVariableLookupNS(ctxt, name, NULL));
3030}
3031
3032/**
3033 * xmlXPathVariableLookupNS:
3034 * @ctxt: the XPath context
3035 * @name: the variable name
3036 * @ns_uri: the variable namespace URI
3037 *
3038 * Search in the Variable array of the context for the given
Daniel Veillard73c9c042001-07-05 20:02:54 +00003039 * variable value.
Owen Taylor3473f882001-02-23 17:55:21 +00003040 *
Daniel Veillard73c9c042001-07-05 20:02:54 +00003041 * Returns the a copy of the value or NULL if not found
Owen Taylor3473f882001-02-23 17:55:21 +00003042 */
3043xmlXPathObjectPtr
3044xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name,
3045 const xmlChar *ns_uri) {
3046 if (ctxt == NULL)
3047 return(NULL);
3048
3049 if (ctxt->varLookupFunc != NULL) {
3050 xmlXPathObjectPtr ret;
3051
3052 ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc)
3053 (ctxt->varLookupData, name, ns_uri);
3054 if (ret != NULL) return(ret);
3055 }
3056
3057 if (ctxt->varHash == NULL)
3058 return(NULL);
3059 if (name == NULL)
3060 return(NULL);
3061
Daniel Veillard8c357d52001-07-03 23:43:33 +00003062 return(xmlXPathObjectCopy((xmlXPathObjectPtr)
3063 xmlHashLookup2(ctxt->varHash, name, ns_uri)));
Owen Taylor3473f882001-02-23 17:55:21 +00003064}
3065
3066/**
3067 * xmlXPathRegisteredVariablesCleanup:
3068 * @ctxt: the XPath context
3069 *
3070 * Cleanup the XPath context data associated to registered variables
3071 */
3072void
3073xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) {
3074 if (ctxt == NULL)
3075 return;
3076
Daniel Veillard76d66f42001-05-16 21:05:17 +00003077 xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject);
Owen Taylor3473f882001-02-23 17:55:21 +00003078 ctxt->varHash = NULL;
3079}
3080
3081/**
3082 * xmlXPathRegisterNs:
3083 * @ctxt: the XPath context
3084 * @prefix: the namespace prefix
3085 * @ns_uri: the namespace name
3086 *
3087 * Register a new namespace. If @ns_uri is NULL it unregisters
3088 * the namespace
3089 *
3090 * Returns 0 in case of success, -1 in case of error
3091 */
3092int
3093xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix,
3094 const xmlChar *ns_uri) {
3095 if (ctxt == NULL)
3096 return(-1);
3097 if (prefix == NULL)
3098 return(-1);
3099
3100 if (ctxt->nsHash == NULL)
3101 ctxt->nsHash = xmlHashCreate(10);
3102 if (ctxt->nsHash == NULL)
3103 return(-1);
Daniel Veillarde991fe92003-10-29 11:18:37 +00003104 if (ns_uri == NULL)
Daniel Veillard94394cd2003-10-29 17:07:51 +00003105 return(xmlHashRemoveEntry(ctxt->nsHash, prefix,
Daniel Veillarde991fe92003-10-29 11:18:37 +00003106 (xmlHashDeallocator)xmlFree));
Daniel Veillard42766c02002-08-22 20:52:17 +00003107 return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri),
Owen Taylor3473f882001-02-23 17:55:21 +00003108 (xmlHashDeallocator)xmlFree));
3109}
3110
3111/**
3112 * xmlXPathNsLookup:
3113 * @ctxt: the XPath context
3114 * @prefix: the namespace prefix value
3115 *
3116 * Search in the namespace declaration array of the context for the given
3117 * namespace name associated to the given prefix
3118 *
3119 * Returns the value or NULL if not found
3120 */
3121const xmlChar *
3122xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) {
3123 if (ctxt == NULL)
3124 return(NULL);
3125 if (prefix == NULL)
3126 return(NULL);
3127
3128#ifdef XML_XML_NAMESPACE
3129 if (xmlStrEqual(prefix, (const xmlChar *) "xml"))
3130 return(XML_XML_NAMESPACE);
3131#endif
3132
Daniel Veillardc8f620b2001-04-30 20:31:33 +00003133 if (ctxt->namespaces != NULL) {
3134 int i;
3135
3136 for (i = 0;i < ctxt->nsNr;i++) {
3137 if ((ctxt->namespaces[i] != NULL) &&
3138 (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix)))
3139 return(ctxt->namespaces[i]->href);
3140 }
3141 }
Owen Taylor3473f882001-02-23 17:55:21 +00003142
3143 return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix));
3144}
3145
3146/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003147 * xmlXPathRegisteredNsCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +00003148 * @ctxt: the XPath context
3149 *
3150 * Cleanup the XPath context data associated to registered variables
3151 */
3152void
3153xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) {
3154 if (ctxt == NULL)
3155 return;
3156
Daniel Veillard42766c02002-08-22 20:52:17 +00003157 xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree);
Owen Taylor3473f882001-02-23 17:55:21 +00003158 ctxt->nsHash = NULL;
3159}
3160
3161/************************************************************************
3162 * *
3163 * Routines to handle Values *
3164 * *
3165 ************************************************************************/
3166
William M. Brack08171912003-12-29 02:52:11 +00003167/* Allocations are terrible, one needs to optimize all this !!! */
Owen Taylor3473f882001-02-23 17:55:21 +00003168
3169/**
3170 * xmlXPathNewFloat:
3171 * @val: the double value
3172 *
3173 * Create a new xmlXPathObjectPtr of type double and of value @val
3174 *
3175 * Returns the newly created object.
3176 */
3177xmlXPathObjectPtr
3178xmlXPathNewFloat(double val) {
3179 xmlXPathObjectPtr ret;
3180
3181 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3182 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003183 xmlXPathErrMemory(NULL, "creating float object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003184 return(NULL);
3185 }
3186 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3187 ret->type = XPATH_NUMBER;
3188 ret->floatval = val;
3189 return(ret);
3190}
3191
3192/**
3193 * xmlXPathNewBoolean:
3194 * @val: the boolean value
3195 *
3196 * Create a new xmlXPathObjectPtr of type boolean and of value @val
3197 *
3198 * Returns the newly created object.
3199 */
3200xmlXPathObjectPtr
3201xmlXPathNewBoolean(int val) {
3202 xmlXPathObjectPtr ret;
3203
3204 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3205 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003206 xmlXPathErrMemory(NULL, "creating boolean object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003207 return(NULL);
3208 }
3209 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3210 ret->type = XPATH_BOOLEAN;
3211 ret->boolval = (val != 0);
3212 return(ret);
3213}
3214
3215/**
3216 * xmlXPathNewString:
3217 * @val: the xmlChar * value
3218 *
3219 * Create a new xmlXPathObjectPtr of type string and of value @val
3220 *
3221 * Returns the newly created object.
3222 */
3223xmlXPathObjectPtr
3224xmlXPathNewString(const xmlChar *val) {
3225 xmlXPathObjectPtr ret;
3226
3227 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3228 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003229 xmlXPathErrMemory(NULL, "creating string object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003230 return(NULL);
3231 }
3232 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3233 ret->type = XPATH_STRING;
3234 if (val != NULL)
3235 ret->stringval = xmlStrdup(val);
3236 else
3237 ret->stringval = xmlStrdup((const xmlChar *)"");
3238 return(ret);
3239}
3240
3241/**
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003242 * xmlXPathWrapString:
3243 * @val: the xmlChar * value
3244 *
3245 * Wraps the @val string into an XPath object.
3246 *
3247 * Returns the newly created object.
3248 */
3249xmlXPathObjectPtr
3250xmlXPathWrapString (xmlChar *val) {
3251 xmlXPathObjectPtr ret;
3252
3253 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3254 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003255 xmlXPathErrMemory(NULL, "creating string object\n");
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003256 return(NULL);
3257 }
3258 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3259 ret->type = XPATH_STRING;
3260 ret->stringval = val;
3261 return(ret);
3262}
3263
3264/**
Owen Taylor3473f882001-02-23 17:55:21 +00003265 * xmlXPathNewCString:
3266 * @val: the char * value
3267 *
3268 * Create a new xmlXPathObjectPtr of type string and of value @val
3269 *
3270 * Returns the newly created object.
3271 */
3272xmlXPathObjectPtr
3273xmlXPathNewCString(const char *val) {
3274 xmlXPathObjectPtr ret;
3275
3276 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3277 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003278 xmlXPathErrMemory(NULL, "creating string object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003279 return(NULL);
3280 }
3281 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3282 ret->type = XPATH_STRING;
3283 ret->stringval = xmlStrdup(BAD_CAST val);
3284 return(ret);
3285}
3286
3287/**
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003288 * xmlXPathWrapCString:
3289 * @val: the char * value
3290 *
3291 * Wraps a string into an XPath object.
3292 *
3293 * Returns the newly created object.
3294 */
3295xmlXPathObjectPtr
3296xmlXPathWrapCString (char * val) {
3297 return(xmlXPathWrapString((xmlChar *)(val)));
3298}
3299
3300/**
Thomas Broyerf06a3d82001-07-16 04:52:57 +00003301 * xmlXPathWrapExternal:
3302 * @val: the user data
3303 *
3304 * Wraps the @val data into an XPath object.
3305 *
3306 * Returns the newly created object.
3307 */
3308xmlXPathObjectPtr
3309xmlXPathWrapExternal (void *val) {
3310 xmlXPathObjectPtr ret;
3311
3312 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3313 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003314 xmlXPathErrMemory(NULL, "creating user object\n");
Thomas Broyerf06a3d82001-07-16 04:52:57 +00003315 return(NULL);
3316 }
3317 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3318 ret->type = XPATH_USERS;
3319 ret->user = val;
3320 return(ret);
3321}
3322
3323/**
Owen Taylor3473f882001-02-23 17:55:21 +00003324 * xmlXPathObjectCopy:
3325 * @val: the original object
3326 *
3327 * allocate a new copy of a given object
3328 *
3329 * Returns the newly created object.
3330 */
3331xmlXPathObjectPtr
3332xmlXPathObjectCopy(xmlXPathObjectPtr val) {
3333 xmlXPathObjectPtr ret;
3334
3335 if (val == NULL)
3336 return(NULL);
3337
3338 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3339 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003340 xmlXPathErrMemory(NULL, "copying object\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003341 return(NULL);
3342 }
3343 memcpy(ret, val , (size_t) sizeof(xmlXPathObject));
3344 switch (val->type) {
3345 case XPATH_BOOLEAN:
3346 case XPATH_NUMBER:
3347 case XPATH_POINT:
3348 case XPATH_RANGE:
3349 break;
3350 case XPATH_STRING:
3351 ret->stringval = xmlStrdup(val->stringval);
3352 break;
3353 case XPATH_XSLT_TREE:
William M. Bracke9449c52004-07-11 14:41:20 +00003354#if 0
3355/*
3356 Removed 11 July 2004 - the current handling of xslt tmpRVT nodes means that
3357 this previous handling is no longer correct, and can cause some serious
3358 problems (ref. bug 145547)
3359*/
Owen Taylor3473f882001-02-23 17:55:21 +00003360 if ((val->nodesetval != NULL) &&
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003361 (val->nodesetval->nodeTab != NULL)) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003362 xmlNodePtr cur, tmp;
3363 xmlDocPtr top;
Daniel Veillardef0b4502003-03-24 13:57:34 +00003364
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003365 ret->boolval = 1;
Daniel Veillard9adc0462003-03-24 18:39:54 +00003366 top = xmlNewDoc(NULL);
3367 top->name = (char *)
3368 xmlStrdup(val->nodesetval->nodeTab[0]->name);
Daniel Veillardef0b4502003-03-24 13:57:34 +00003369 ret->user = top;
3370 if (top != NULL) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003371 top->doc = top;
Daniel Veillardef0b4502003-03-24 13:57:34 +00003372 cur = val->nodesetval->nodeTab[0]->children;
3373 while (cur != NULL) {
Daniel Veillard9adc0462003-03-24 18:39:54 +00003374 tmp = xmlDocCopyNode(cur, top, 1);
3375 xmlAddChild((xmlNodePtr) top, tmp);
Daniel Veillardef0b4502003-03-24 13:57:34 +00003376 cur = cur->next;
3377 }
3378 }
William M. Bracke9449c52004-07-11 14:41:20 +00003379
Daniel Veillard9adc0462003-03-24 18:39:54 +00003380 ret->nodesetval = xmlXPathNodeSetCreate((xmlNodePtr) top);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003381 } else
Owen Taylor3473f882001-02-23 17:55:21 +00003382 ret->nodesetval = xmlXPathNodeSetCreate(NULL);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003383 /* Deallocate the copied tree value */
Owen Taylor3473f882001-02-23 17:55:21 +00003384 break;
William M. Bracke9449c52004-07-11 14:41:20 +00003385#endif
Owen Taylor3473f882001-02-23 17:55:21 +00003386 case XPATH_NODESET:
3387 ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003388 /* Do not deallocate the copied tree value */
3389 ret->boolval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003390 break;
3391 case XPATH_LOCATIONSET:
3392#ifdef LIBXML_XPTR_ENABLED
3393 {
3394 xmlLocationSetPtr loc = val->user;
3395 ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc);
3396 break;
3397 }
3398#endif
Thomas Broyer47334c02001-10-07 16:41:52 +00003399 case XPATH_USERS:
3400 ret->user = val->user;
3401 break;
3402 case XPATH_UNDEFINED:
Owen Taylor3473f882001-02-23 17:55:21 +00003403 xmlGenericError(xmlGenericErrorContext,
3404 "xmlXPathObjectCopy: unsupported type %d\n",
3405 val->type);
3406 break;
3407 }
3408 return(ret);
3409}
3410
3411/**
3412 * xmlXPathFreeObject:
3413 * @obj: the object to free
3414 *
3415 * Free up an xmlXPathObjectPtr object.
3416 */
3417void
3418xmlXPathFreeObject(xmlXPathObjectPtr obj) {
3419 if (obj == NULL) return;
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003420 if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) {
Daniel Veillard77851712001-02-27 21:54:07 +00003421 if (obj->boolval) {
William M. Bracke9449c52004-07-11 14:41:20 +00003422#if 0
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003423 if (obj->user != NULL) {
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003424 xmlXPathFreeNodeSet(obj->nodesetval);
Daniel Veillard38bf6f02002-03-16 22:03:31 +00003425 xmlFreeNodeList((xmlNodePtr) obj->user);
William M. Bracke9449c52004-07-11 14:41:20 +00003426 } else
3427#endif
3428 if (obj->nodesetval != NULL)
Daniel Veillard77851712001-02-27 21:54:07 +00003429 xmlXPathFreeValueTree(obj->nodesetval);
3430 } else {
3431 if (obj->nodesetval != NULL)
3432 xmlXPathFreeNodeSet(obj->nodesetval);
3433 }
Owen Taylor3473f882001-02-23 17:55:21 +00003434#ifdef LIBXML_XPTR_ENABLED
3435 } else if (obj->type == XPATH_LOCATIONSET) {
3436 if (obj->user != NULL)
3437 xmlXPtrFreeLocationSet(obj->user);
3438#endif
3439 } else if (obj->type == XPATH_STRING) {
3440 if (obj->stringval != NULL)
3441 xmlFree(obj->stringval);
Owen Taylor3473f882001-02-23 17:55:21 +00003442 }
3443
Owen Taylor3473f882001-02-23 17:55:21 +00003444 xmlFree(obj);
3445}
3446
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003447
3448/************************************************************************
3449 * *
3450 * Type Casting Routines *
3451 * *
3452 ************************************************************************/
3453
3454/**
3455 * xmlXPathCastBooleanToString:
3456 * @val: a boolean
3457 *
3458 * Converts a boolean to its string value.
3459 *
3460 * Returns a newly allocated string.
3461 */
3462xmlChar *
3463xmlXPathCastBooleanToString (int val) {
3464 xmlChar *ret;
3465 if (val)
3466 ret = xmlStrdup((const xmlChar *) "true");
3467 else
3468 ret = xmlStrdup((const xmlChar *) "false");
3469 return(ret);
3470}
3471
3472/**
3473 * xmlXPathCastNumberToString:
3474 * @val: a number
3475 *
3476 * Converts a number to its string value.
3477 *
3478 * Returns a newly allocated string.
3479 */
3480xmlChar *
3481xmlXPathCastNumberToString (double val) {
3482 xmlChar *ret;
Daniel Veillardcda96922001-08-21 10:56:31 +00003483 switch (xmlXPathIsInf(val)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003484 case 1:
Daniel Veillard5fc1f082002-03-27 09:05:40 +00003485 ret = xmlStrdup((const xmlChar *) "Infinity");
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003486 break;
3487 case -1:
3488 ret = xmlStrdup((const xmlChar *) "-Infinity");
3489 break;
3490 default:
Daniel Veillardcda96922001-08-21 10:56:31 +00003491 if (xmlXPathIsNaN(val)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003492 ret = xmlStrdup((const xmlChar *) "NaN");
Daniel Veillardd30be4a2002-03-28 18:25:31 +00003493 } else if (val == 0 && xmlXPathGetSign(val) != 0) {
3494 ret = xmlStrdup((const xmlChar *) "0");
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003495 } else {
3496 /* could be improved */
3497 char buf[100];
3498 xmlXPathFormatNumber(val, buf, 100);
3499 ret = xmlStrdup((const xmlChar *) buf);
3500 }
3501 }
3502 return(ret);
3503}
3504
3505/**
3506 * xmlXPathCastNodeToString:
3507 * @node: a node
3508 *
3509 * Converts a node to its string value.
3510 *
3511 * Returns a newly allocated string.
3512 */
3513xmlChar *
3514xmlXPathCastNodeToString (xmlNodePtr node) {
3515 return(xmlNodeGetContent(node));
3516}
3517
3518/**
3519 * xmlXPathCastNodeSetToString:
3520 * @ns: a node-set
3521 *
3522 * Converts a node-set to its string value.
3523 *
3524 * Returns a newly allocated string.
3525 */
3526xmlChar *
3527xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) {
3528 if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL))
3529 return(xmlStrdup((const xmlChar *) ""));
3530
3531 xmlXPathNodeSetSort(ns);
3532 return(xmlXPathCastNodeToString(ns->nodeTab[0]));
3533}
3534
3535/**
3536 * xmlXPathCastToString:
3537 * @val: an XPath object
3538 *
3539 * Converts an existing object to its string() equivalent
3540 *
3541 * Returns the string value of the object, NULL in case of error.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003542 * A new string is allocated only if needed (@val isn't a
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003543 * string object).
3544 */
3545xmlChar *
3546xmlXPathCastToString(xmlXPathObjectPtr val) {
3547 xmlChar *ret = NULL;
3548
3549 if (val == NULL)
3550 return(xmlStrdup((const xmlChar *) ""));
3551 switch (val->type) {
3552 case XPATH_UNDEFINED:
3553#ifdef DEBUG_EXPR
3554 xmlGenericError(xmlGenericErrorContext, "String: undefined\n");
3555#endif
3556 ret = xmlStrdup((const xmlChar *) "");
3557 break;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003558 case XPATH_NODESET:
William M. Brack0c022ad2002-07-12 00:56:01 +00003559 case XPATH_XSLT_TREE:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003560 ret = xmlXPathCastNodeSetToString(val->nodesetval);
3561 break;
3562 case XPATH_STRING:
Daniel Veillard4e2df542002-03-22 12:23:14 +00003563 return(xmlStrdup(val->stringval));
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003564 case XPATH_BOOLEAN:
3565 ret = xmlXPathCastBooleanToString(val->boolval);
3566 break;
3567 case XPATH_NUMBER: {
3568 ret = xmlXPathCastNumberToString(val->floatval);
3569 break;
3570 }
3571 case XPATH_USERS:
3572 case XPATH_POINT:
3573 case XPATH_RANGE:
3574 case XPATH_LOCATIONSET:
3575 TODO
3576 ret = xmlStrdup((const xmlChar *) "");
3577 break;
3578 }
3579 return(ret);
3580}
3581
3582/**
3583 * xmlXPathConvertString:
3584 * @val: an XPath object
3585 *
3586 * Converts an existing object to its string() equivalent
3587 *
3588 * Returns the new object, the old one is freed (or the operation
3589 * is done directly on @val)
3590 */
3591xmlXPathObjectPtr
3592xmlXPathConvertString(xmlXPathObjectPtr val) {
3593 xmlChar *res = NULL;
3594
3595 if (val == NULL)
3596 return(xmlXPathNewCString(""));
3597
3598 switch (val->type) {
3599 case XPATH_UNDEFINED:
3600#ifdef DEBUG_EXPR
3601 xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n");
3602#endif
3603 break;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003604 case XPATH_NODESET:
William M. Brack0c022ad2002-07-12 00:56:01 +00003605 case XPATH_XSLT_TREE:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003606 res = xmlXPathCastNodeSetToString(val->nodesetval);
3607 break;
3608 case XPATH_STRING:
3609 return(val);
3610 case XPATH_BOOLEAN:
3611 res = xmlXPathCastBooleanToString(val->boolval);
3612 break;
3613 case XPATH_NUMBER:
3614 res = xmlXPathCastNumberToString(val->floatval);
3615 break;
3616 case XPATH_USERS:
3617 case XPATH_POINT:
3618 case XPATH_RANGE:
3619 case XPATH_LOCATIONSET:
3620 TODO;
3621 break;
3622 }
3623 xmlXPathFreeObject(val);
3624 if (res == NULL)
3625 return(xmlXPathNewCString(""));
3626 return(xmlXPathWrapString(res));
3627}
3628
3629/**
3630 * xmlXPathCastBooleanToNumber:
3631 * @val: a boolean
3632 *
3633 * Converts a boolean to its number value
3634 *
3635 * Returns the number value
3636 */
3637double
3638xmlXPathCastBooleanToNumber(int val) {
3639 if (val)
3640 return(1.0);
3641 return(0.0);
3642}
3643
3644/**
3645 * xmlXPathCastStringToNumber:
3646 * @val: a string
3647 *
3648 * Converts a string to its number value
3649 *
3650 * Returns the number value
3651 */
3652double
3653xmlXPathCastStringToNumber(const xmlChar * val) {
3654 return(xmlXPathStringEvalNumber(val));
3655}
3656
3657/**
3658 * xmlXPathCastNodeToNumber:
3659 * @node: a node
3660 *
3661 * Converts a node to its number value
3662 *
3663 * Returns the number value
3664 */
3665double
3666xmlXPathCastNodeToNumber (xmlNodePtr node) {
3667 xmlChar *strval;
3668 double ret;
3669
3670 if (node == NULL)
3671 return(xmlXPathNAN);
3672 strval = xmlXPathCastNodeToString(node);
3673 if (strval == NULL)
3674 return(xmlXPathNAN);
3675 ret = xmlXPathCastStringToNumber(strval);
3676 xmlFree(strval);
3677
3678 return(ret);
3679}
3680
3681/**
3682 * xmlXPathCastNodeSetToNumber:
3683 * @ns: a node-set
3684 *
3685 * Converts a node-set to its number value
3686 *
3687 * Returns the number value
3688 */
3689double
3690xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) {
3691 xmlChar *str;
3692 double ret;
3693
3694 if (ns == NULL)
3695 return(xmlXPathNAN);
3696 str = xmlXPathCastNodeSetToString(ns);
3697 ret = xmlXPathCastStringToNumber(str);
3698 xmlFree(str);
3699 return(ret);
3700}
3701
3702/**
3703 * xmlXPathCastToNumber:
3704 * @val: an XPath object
3705 *
3706 * Converts an XPath object to its number value
3707 *
3708 * Returns the number value
3709 */
3710double
3711xmlXPathCastToNumber(xmlXPathObjectPtr val) {
3712 double ret = 0.0;
3713
3714 if (val == NULL)
3715 return(xmlXPathNAN);
3716 switch (val->type) {
3717 case XPATH_UNDEFINED:
3718#ifdef DEGUB_EXPR
3719 xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n");
3720#endif
3721 ret = xmlXPathNAN;
3722 break;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003723 case XPATH_NODESET:
William M. Brack0c022ad2002-07-12 00:56:01 +00003724 case XPATH_XSLT_TREE:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003725 ret = xmlXPathCastNodeSetToNumber(val->nodesetval);
3726 break;
3727 case XPATH_STRING:
3728 ret = xmlXPathCastStringToNumber(val->stringval);
3729 break;
3730 case XPATH_NUMBER:
3731 ret = val->floatval;
3732 break;
3733 case XPATH_BOOLEAN:
3734 ret = xmlXPathCastBooleanToNumber(val->boolval);
3735 break;
3736 case XPATH_USERS:
3737 case XPATH_POINT:
3738 case XPATH_RANGE:
3739 case XPATH_LOCATIONSET:
3740 TODO;
3741 ret = xmlXPathNAN;
3742 break;
3743 }
3744 return(ret);
3745}
3746
3747/**
3748 * xmlXPathConvertNumber:
3749 * @val: an XPath object
3750 *
3751 * Converts an existing object to its number() equivalent
3752 *
3753 * Returns the new object, the old one is freed (or the operation
3754 * is done directly on @val)
3755 */
3756xmlXPathObjectPtr
3757xmlXPathConvertNumber(xmlXPathObjectPtr val) {
3758 xmlXPathObjectPtr ret;
3759
3760 if (val == NULL)
3761 return(xmlXPathNewFloat(0.0));
3762 if (val->type == XPATH_NUMBER)
3763 return(val);
3764 ret = xmlXPathNewFloat(xmlXPathCastToNumber(val));
3765 xmlXPathFreeObject(val);
3766 return(ret);
3767}
3768
3769/**
3770 * xmlXPathCastNumberToBoolean:
3771 * @val: a number
3772 *
3773 * Converts a number to its boolean value
3774 *
3775 * Returns the boolean value
3776 */
3777int
3778xmlXPathCastNumberToBoolean (double val) {
Daniel Veillardcda96922001-08-21 10:56:31 +00003779 if (xmlXPathIsNaN(val) || (val == 0.0))
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003780 return(0);
3781 return(1);
3782}
3783
3784/**
3785 * xmlXPathCastStringToBoolean:
3786 * @val: a string
3787 *
3788 * Converts a string to its boolean value
3789 *
3790 * Returns the boolean value
3791 */
3792int
3793xmlXPathCastStringToBoolean (const xmlChar *val) {
3794 if ((val == NULL) || (xmlStrlen(val) == 0))
3795 return(0);
3796 return(1);
3797}
3798
3799/**
3800 * xmlXPathCastNodeSetToBoolean:
3801 * @ns: a node-set
3802 *
3803 * Converts a node-set to its boolean value
3804 *
3805 * Returns the boolean value
3806 */
3807int
3808xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) {
3809 if ((ns == NULL) || (ns->nodeNr == 0))
3810 return(0);
3811 return(1);
3812}
3813
3814/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003815 * xmlXPathCastToBoolean:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003816 * @val: an XPath object
3817 *
3818 * Converts an XPath object to its boolean value
3819 *
3820 * Returns the boolean value
3821 */
3822int
3823xmlXPathCastToBoolean (xmlXPathObjectPtr val) {
3824 int ret = 0;
3825
3826 if (val == NULL)
3827 return(0);
3828 switch (val->type) {
3829 case XPATH_UNDEFINED:
3830#ifdef DEBUG_EXPR
3831 xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n");
3832#endif
3833 ret = 0;
3834 break;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003835 case XPATH_NODESET:
William M. Brack0c022ad2002-07-12 00:56:01 +00003836 case XPATH_XSLT_TREE:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003837 ret = xmlXPathCastNodeSetToBoolean(val->nodesetval);
3838 break;
3839 case XPATH_STRING:
3840 ret = xmlXPathCastStringToBoolean(val->stringval);
3841 break;
3842 case XPATH_NUMBER:
3843 ret = xmlXPathCastNumberToBoolean(val->floatval);
3844 break;
3845 case XPATH_BOOLEAN:
3846 ret = val->boolval;
3847 break;
3848 case XPATH_USERS:
3849 case XPATH_POINT:
3850 case XPATH_RANGE:
3851 case XPATH_LOCATIONSET:
3852 TODO;
3853 ret = 0;
3854 break;
3855 }
3856 return(ret);
3857}
3858
3859
3860/**
3861 * xmlXPathConvertBoolean:
3862 * @val: an XPath object
3863 *
3864 * Converts an existing object to its boolean() equivalent
3865 *
3866 * Returns the new object, the old one is freed (or the operation
3867 * is done directly on @val)
3868 */
3869xmlXPathObjectPtr
3870xmlXPathConvertBoolean(xmlXPathObjectPtr val) {
3871 xmlXPathObjectPtr ret;
3872
3873 if (val == NULL)
3874 return(xmlXPathNewBoolean(0));
3875 if (val->type == XPATH_BOOLEAN)
3876 return(val);
3877 ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val));
3878 xmlXPathFreeObject(val);
3879 return(ret);
3880}
3881
Owen Taylor3473f882001-02-23 17:55:21 +00003882/************************************************************************
3883 * *
3884 * Routines to handle XPath contexts *
3885 * *
3886 ************************************************************************/
3887
3888/**
3889 * xmlXPathNewContext:
3890 * @doc: the XML document
3891 *
3892 * Create a new xmlXPathContext
3893 *
Daniel Veillardaf43f632002-03-08 15:05:20 +00003894 * Returns the xmlXPathContext just allocated. The caller will need to free it.
Owen Taylor3473f882001-02-23 17:55:21 +00003895 */
3896xmlXPathContextPtr
3897xmlXPathNewContext(xmlDocPtr doc) {
3898 xmlXPathContextPtr ret;
3899
3900 ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext));
3901 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003902 xmlXPathErrMemory(NULL, "creating context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003903 return(NULL);
3904 }
3905 memset(ret, 0 , (size_t) sizeof(xmlXPathContext));
3906 ret->doc = doc;
3907 ret->node = NULL;
3908
3909 ret->varHash = NULL;
3910
3911 ret->nb_types = 0;
3912 ret->max_types = 0;
3913 ret->types = NULL;
3914
3915 ret->funcHash = xmlHashCreate(0);
3916
3917 ret->nb_axis = 0;
3918 ret->max_axis = 0;
3919 ret->axis = NULL;
3920
3921 ret->nsHash = NULL;
3922 ret->user = NULL;
3923
3924 ret->contextSize = -1;
3925 ret->proximityPosition = -1;
3926
3927 xmlXPathRegisterAllFunctions(ret);
3928
3929 return(ret);
3930}
3931
3932/**
3933 * xmlXPathFreeContext:
3934 * @ctxt: the context to free
3935 *
3936 * Free up an xmlXPathContext
3937 */
3938void
3939xmlXPathFreeContext(xmlXPathContextPtr ctxt) {
Daniel Veillard7eca35f2004-11-29 13:08:03 +00003940 if (ctxt == NULL) return;
3941
Owen Taylor3473f882001-02-23 17:55:21 +00003942 xmlXPathRegisteredNsCleanup(ctxt);
3943 xmlXPathRegisteredFuncsCleanup(ctxt);
3944 xmlXPathRegisteredVariablesCleanup(ctxt);
Daniel Veillard7eca35f2004-11-29 13:08:03 +00003945 xmlResetError(&ctxt->lastError);
Owen Taylor3473f882001-02-23 17:55:21 +00003946 xmlFree(ctxt);
3947}
3948
3949/************************************************************************
3950 * *
3951 * Routines to handle XPath parser contexts *
3952 * *
3953 ************************************************************************/
3954
3955#define CHECK_CTXT(ctxt) \
3956 if (ctxt == NULL) { \
William M. Brackf13f77f2004-11-12 16:03:48 +00003957 __xmlRaiseError(NULL, NULL, NULL, \
3958 NULL, NULL, XML_FROM_XPATH, \
3959 XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \
3960 __FILE__, __LINE__, \
3961 NULL, NULL, NULL, 0, 0, \
3962 "NULL context pointer\n"); \
3963 return(NULL); \
Owen Taylor3473f882001-02-23 17:55:21 +00003964 } \
3965
3966
3967#define CHECK_CONTEXT(ctxt) \
Daniel Veillard57b25162004-11-06 14:50:18 +00003968 if ((ctxt == NULL) || (ctxt->doc == NULL) || \
3969 (ctxt->doc->children == NULL)) { \
3970 xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_INVALID_CTXT); \
Daniel Veillardce682bc2004-11-05 17:22:25 +00003971 return(NULL); \
Daniel Veillard57b25162004-11-06 14:50:18 +00003972 }
Owen Taylor3473f882001-02-23 17:55:21 +00003973
3974
3975/**
3976 * xmlXPathNewParserContext:
3977 * @str: the XPath expression
3978 * @ctxt: the XPath context
3979 *
3980 * Create a new xmlXPathParserContext
3981 *
3982 * Returns the xmlXPathParserContext just allocated.
3983 */
3984xmlXPathParserContextPtr
3985xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) {
3986 xmlXPathParserContextPtr ret;
3987
3988 ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext));
3989 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00003990 xmlXPathErrMemory(ctxt, "creating parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003991 return(NULL);
3992 }
3993 memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext));
3994 ret->cur = ret->base = str;
3995 ret->context = ctxt;
3996
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003997 ret->comp = xmlXPathNewCompExpr();
3998 if (ret->comp == NULL) {
3999 xmlFree(ret->valueTab);
4000 xmlFree(ret);
4001 return(NULL);
4002 }
Daniel Veillard4773df22004-01-23 13:15:13 +00004003 if ((ctxt != NULL) && (ctxt->dict != NULL)) {
4004 ret->comp->dict = ctxt->dict;
4005 xmlDictReference(ret->comp->dict);
4006 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004007
4008 return(ret);
4009}
4010
4011/**
4012 * xmlXPathCompParserContext:
4013 * @comp: the XPath compiled expression
4014 * @ctxt: the XPath context
4015 *
4016 * Create a new xmlXPathParserContext when processing a compiled expression
4017 *
4018 * Returns the xmlXPathParserContext just allocated.
4019 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004020static xmlXPathParserContextPtr
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004021xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) {
4022 xmlXPathParserContextPtr ret;
4023
4024 ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext));
4025 if (ret == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004026 xmlXPathErrMemory(ctxt, "creating evaluation context\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004027 return(NULL);
4028 }
4029 memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext));
4030
Owen Taylor3473f882001-02-23 17:55:21 +00004031 /* Allocate the value stack */
4032 ret->valueTab = (xmlXPathObjectPtr *)
4033 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004034 if (ret->valueTab == NULL) {
4035 xmlFree(ret);
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004036 xmlXPathErrMemory(ctxt, "creating evaluation context\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004037 return(NULL);
4038 }
Owen Taylor3473f882001-02-23 17:55:21 +00004039 ret->valueNr = 0;
4040 ret->valueMax = 10;
4041 ret->value = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004042
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00004043 ret->context = ctxt;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004044 ret->comp = comp;
4045
Owen Taylor3473f882001-02-23 17:55:21 +00004046 return(ret);
4047}
4048
4049/**
4050 * xmlXPathFreeParserContext:
4051 * @ctxt: the context to free
4052 *
4053 * Free up an xmlXPathParserContext
4054 */
4055void
4056xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) {
4057 if (ctxt->valueTab != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00004058 xmlFree(ctxt->valueTab);
4059 }
Daniel Veillard56de87e2005-02-16 00:22:29 +00004060 if (ctxt->comp != NULL) {
4061#ifdef XPATH_STREAMING
4062 if (ctxt->comp->stream != NULL) {
4063 xmlFreePatternList(ctxt->comp->stream);
4064 ctxt->comp->stream = NULL;
4065 }
4066#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +00004067 xmlXPathFreeCompExpr(ctxt->comp);
Daniel Veillard56de87e2005-02-16 00:22:29 +00004068 }
Owen Taylor3473f882001-02-23 17:55:21 +00004069 xmlFree(ctxt);
4070}
4071
4072/************************************************************************
4073 * *
4074 * The implicit core function library *
4075 * *
4076 ************************************************************************/
4077
Owen Taylor3473f882001-02-23 17:55:21 +00004078/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004079 * xmlXPathNodeValHash:
Daniel Veillardf06307e2001-07-03 10:35:50 +00004080 * @node: a node pointer
4081 *
4082 * Function computing the beginning of the string value of the node,
4083 * used to speed up comparisons
4084 *
4085 * Returns an int usable as a hash
4086 */
4087static unsigned int
4088xmlXPathNodeValHash(xmlNodePtr node) {
4089 int len = 2;
4090 const xmlChar * string = NULL;
4091 xmlNodePtr tmp = NULL;
4092 unsigned int ret = 0;
4093
4094 if (node == NULL)
4095 return(0);
4096
Daniel Veillard9adc0462003-03-24 18:39:54 +00004097 if (node->type == XML_DOCUMENT_NODE) {
4098 tmp = xmlDocGetRootElement((xmlDocPtr) node);
4099 if (tmp == NULL)
4100 node = node->children;
4101 else
4102 node = tmp;
4103
4104 if (node == NULL)
4105 return(0);
4106 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004107
4108 switch (node->type) {
4109 case XML_COMMENT_NODE:
4110 case XML_PI_NODE:
4111 case XML_CDATA_SECTION_NODE:
4112 case XML_TEXT_NODE:
4113 string = node->content;
4114 if (string == NULL)
4115 return(0);
4116 if (string[0] == 0)
4117 return(0);
4118 return(((unsigned int) string[0]) +
4119 (((unsigned int) string[1]) << 8));
4120 case XML_NAMESPACE_DECL:
4121 string = ((xmlNsPtr)node)->href;
4122 if (string == NULL)
4123 return(0);
4124 if (string[0] == 0)
4125 return(0);
4126 return(((unsigned int) string[0]) +
4127 (((unsigned int) string[1]) << 8));
4128 case XML_ATTRIBUTE_NODE:
4129 tmp = ((xmlAttrPtr) node)->children;
4130 break;
4131 case XML_ELEMENT_NODE:
4132 tmp = node->children;
4133 break;
4134 default:
4135 return(0);
4136 }
4137 while (tmp != NULL) {
4138 switch (tmp->type) {
4139 case XML_COMMENT_NODE:
4140 case XML_PI_NODE:
4141 case XML_CDATA_SECTION_NODE:
4142 case XML_TEXT_NODE:
4143 string = tmp->content;
4144 break;
4145 case XML_NAMESPACE_DECL:
4146 string = ((xmlNsPtr)tmp)->href;
4147 break;
4148 default:
4149 break;
4150 }
4151 if ((string != NULL) && (string[0] != 0)) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00004152 if (len == 1) {
4153 return(ret + (((unsigned int) string[0]) << 8));
4154 }
4155 if (string[1] == 0) {
4156 len = 1;
4157 ret = (unsigned int) string[0];
4158 } else {
4159 return(((unsigned int) string[0]) +
4160 (((unsigned int) string[1]) << 8));
4161 }
4162 }
4163 /*
4164 * Skip to next node
4165 */
4166 if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) {
4167 if (tmp->children->type != XML_ENTITY_DECL) {
4168 tmp = tmp->children;
4169 continue;
4170 }
4171 }
4172 if (tmp == node)
4173 break;
4174
4175 if (tmp->next != NULL) {
4176 tmp = tmp->next;
4177 continue;
4178 }
4179
4180 do {
4181 tmp = tmp->parent;
4182 if (tmp == NULL)
4183 break;
4184 if (tmp == node) {
4185 tmp = NULL;
4186 break;
4187 }
4188 if (tmp->next != NULL) {
4189 tmp = tmp->next;
4190 break;
4191 }
4192 } while (tmp != NULL);
4193 }
4194 return(ret);
4195}
4196
4197/**
4198 * xmlXPathStringHash:
4199 * @string: a string
4200 *
4201 * Function computing the beginning of the string value of the node,
4202 * used to speed up comparisons
4203 *
4204 * Returns an int usable as a hash
4205 */
4206static unsigned int
4207xmlXPathStringHash(const xmlChar * string) {
4208 if (string == NULL)
4209 return((unsigned int) 0);
4210 if (string[0] == 0)
4211 return(0);
4212 return(((unsigned int) string[0]) +
4213 (((unsigned int) string[1]) << 8));
4214}
4215
4216/**
Owen Taylor3473f882001-02-23 17:55:21 +00004217 * xmlXPathCompareNodeSetFloat:
4218 * @ctxt: the XPath Parser context
4219 * @inf: less than (1) or greater than (0)
4220 * @strict: is the comparison strict
4221 * @arg: the node set
4222 * @f: the value
4223 *
4224 * Implement the compare operation between a nodeset and a number
4225 * @ns < @val (1, 1, ...
4226 * @ns <= @val (1, 0, ...
4227 * @ns > @val (0, 1, ...
4228 * @ns >= @val (0, 0, ...
4229 *
4230 * If one object to be compared is a node-set and the other is a number,
4231 * then the comparison will be true if and only if there is a node in the
4232 * node-set such that the result of performing the comparison on the number
4233 * to be compared and on the result of converting the string-value of that
4234 * node to a number using the number function is true.
4235 *
4236 * Returns 0 or 1 depending on the results of the test.
4237 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004238static int
Owen Taylor3473f882001-02-23 17:55:21 +00004239xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict,
4240 xmlXPathObjectPtr arg, xmlXPathObjectPtr f) {
4241 int i, ret = 0;
4242 xmlNodeSetPtr ns;
4243 xmlChar *str2;
4244
4245 if ((f == NULL) || (arg == NULL) ||
4246 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) {
4247 xmlXPathFreeObject(arg);
4248 xmlXPathFreeObject(f);
4249 return(0);
4250 }
4251 ns = arg->nodesetval;
Daniel Veillard911f49a2001-04-07 15:39:35 +00004252 if (ns != NULL) {
4253 for (i = 0;i < ns->nodeNr;i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004254 str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
Daniel Veillard911f49a2001-04-07 15:39:35 +00004255 if (str2 != NULL) {
4256 valuePush(ctxt,
4257 xmlXPathNewString(str2));
4258 xmlFree(str2);
4259 xmlXPathNumberFunction(ctxt, 1);
4260 valuePush(ctxt, xmlXPathObjectCopy(f));
4261 ret = xmlXPathCompareValues(ctxt, inf, strict);
4262 if (ret)
4263 break;
4264 }
4265 }
Owen Taylor3473f882001-02-23 17:55:21 +00004266 }
4267 xmlXPathFreeObject(arg);
4268 xmlXPathFreeObject(f);
4269 return(ret);
4270}
4271
4272/**
4273 * xmlXPathCompareNodeSetString:
4274 * @ctxt: the XPath Parser context
4275 * @inf: less than (1) or greater than (0)
4276 * @strict: is the comparison strict
4277 * @arg: the node set
4278 * @s: the value
4279 *
4280 * Implement the compare operation between a nodeset and a string
4281 * @ns < @val (1, 1, ...
4282 * @ns <= @val (1, 0, ...
4283 * @ns > @val (0, 1, ...
4284 * @ns >= @val (0, 0, ...
4285 *
4286 * If one object to be compared is a node-set and the other is a string,
4287 * then the comparison will be true if and only if there is a node in
4288 * the node-set such that the result of performing the comparison on the
4289 * string-value of the node and the other string is true.
4290 *
4291 * Returns 0 or 1 depending on the results of the test.
4292 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004293static int
Owen Taylor3473f882001-02-23 17:55:21 +00004294xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict,
4295 xmlXPathObjectPtr arg, xmlXPathObjectPtr s) {
4296 int i, ret = 0;
4297 xmlNodeSetPtr ns;
4298 xmlChar *str2;
4299
4300 if ((s == NULL) || (arg == NULL) ||
4301 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) {
4302 xmlXPathFreeObject(arg);
4303 xmlXPathFreeObject(s);
4304 return(0);
4305 }
4306 ns = arg->nodesetval;
Daniel Veillard911f49a2001-04-07 15:39:35 +00004307 if (ns != NULL) {
4308 for (i = 0;i < ns->nodeNr;i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004309 str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
Daniel Veillard911f49a2001-04-07 15:39:35 +00004310 if (str2 != NULL) {
4311 valuePush(ctxt,
4312 xmlXPathNewString(str2));
4313 xmlFree(str2);
4314 valuePush(ctxt, xmlXPathObjectCopy(s));
4315 ret = xmlXPathCompareValues(ctxt, inf, strict);
4316 if (ret)
4317 break;
4318 }
4319 }
Owen Taylor3473f882001-02-23 17:55:21 +00004320 }
4321 xmlXPathFreeObject(arg);
4322 xmlXPathFreeObject(s);
4323 return(ret);
4324}
4325
4326/**
4327 * xmlXPathCompareNodeSets:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004328 * @inf: less than (1) or greater than (0)
Owen Taylor3473f882001-02-23 17:55:21 +00004329 * @strict: is the comparison strict
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004330 * @arg1: the first node set object
Owen Taylor3473f882001-02-23 17:55:21 +00004331 * @arg2: the second node set object
4332 *
4333 * Implement the compare operation on nodesets:
4334 *
4335 * If both objects to be compared are node-sets, then the comparison
4336 * will be true if and only if there is a node in the first node-set
4337 * and a node in the second node-set such that the result of performing
4338 * the comparison on the string-values of the two nodes is true.
4339 * ....
4340 * When neither object to be compared is a node-set and the operator
4341 * is <=, <, >= or >, then the objects are compared by converting both
4342 * objects to numbers and comparing the numbers according to IEEE 754.
4343 * ....
4344 * The number function converts its argument to a number as follows:
4345 * - a string that consists of optional whitespace followed by an
4346 * optional minus sign followed by a Number followed by whitespace
4347 * is converted to the IEEE 754 number that is nearest (according
4348 * to the IEEE 754 round-to-nearest rule) to the mathematical value
4349 * represented by the string; any other string is converted to NaN
4350 *
4351 * Conclusion all nodes need to be converted first to their string value
4352 * and then the comparison must be done when possible
4353 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004354static int
4355xmlXPathCompareNodeSets(int inf, int strict,
Owen Taylor3473f882001-02-23 17:55:21 +00004356 xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) {
4357 int i, j, init = 0;
4358 double val1;
4359 double *values2;
4360 int ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004361 xmlNodeSetPtr ns1;
4362 xmlNodeSetPtr ns2;
4363
4364 if ((arg1 == NULL) ||
Daniel Veillard4dd93462001-04-02 15:16:19 +00004365 ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) {
4366 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004367 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004368 }
Owen Taylor3473f882001-02-23 17:55:21 +00004369 if ((arg2 == NULL) ||
Daniel Veillard4dd93462001-04-02 15:16:19 +00004370 ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) {
4371 xmlXPathFreeObject(arg1);
4372 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004373 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004374 }
Owen Taylor3473f882001-02-23 17:55:21 +00004375
4376 ns1 = arg1->nodesetval;
4377 ns2 = arg2->nodesetval;
4378
Daniel Veillardd8df6c02001-04-05 16:54:14 +00004379 if ((ns1 == NULL) || (ns1->nodeNr <= 0)) {
Daniel Veillard4dd93462001-04-02 15:16:19 +00004380 xmlXPathFreeObject(arg1);
4381 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004382 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004383 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00004384 if ((ns2 == NULL) || (ns2->nodeNr <= 0)) {
Daniel Veillard4dd93462001-04-02 15:16:19 +00004385 xmlXPathFreeObject(arg1);
4386 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004387 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004388 }
Owen Taylor3473f882001-02-23 17:55:21 +00004389
4390 values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double));
4391 if (values2 == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004392 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Daniel Veillard4dd93462001-04-02 15:16:19 +00004393 xmlXPathFreeObject(arg1);
4394 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004395 return(0);
4396 }
4397 for (i = 0;i < ns1->nodeNr;i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004398 val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]);
Daniel Veillardcda96922001-08-21 10:56:31 +00004399 if (xmlXPathIsNaN(val1))
Owen Taylor3473f882001-02-23 17:55:21 +00004400 continue;
4401 for (j = 0;j < ns2->nodeNr;j++) {
4402 if (init == 0) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004403 values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]);
Owen Taylor3473f882001-02-23 17:55:21 +00004404 }
Daniel Veillardcda96922001-08-21 10:56:31 +00004405 if (xmlXPathIsNaN(values2[j]))
Owen Taylor3473f882001-02-23 17:55:21 +00004406 continue;
4407 if (inf && strict)
4408 ret = (val1 < values2[j]);
4409 else if (inf && !strict)
4410 ret = (val1 <= values2[j]);
4411 else if (!inf && strict)
4412 ret = (val1 > values2[j]);
4413 else if (!inf && !strict)
4414 ret = (val1 >= values2[j]);
4415 if (ret)
4416 break;
4417 }
4418 if (ret)
4419 break;
4420 init = 1;
4421 }
4422 xmlFree(values2);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004423 xmlXPathFreeObject(arg1);
4424 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004425 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004426}
4427
4428/**
4429 * xmlXPathCompareNodeSetValue:
4430 * @ctxt: the XPath Parser context
4431 * @inf: less than (1) or greater than (0)
4432 * @strict: is the comparison strict
4433 * @arg: the node set
4434 * @val: the value
4435 *
4436 * Implement the compare operation between a nodeset and a value
4437 * @ns < @val (1, 1, ...
4438 * @ns <= @val (1, 0, ...
4439 * @ns > @val (0, 1, ...
4440 * @ns >= @val (0, 0, ...
4441 *
4442 * If one object to be compared is a node-set and the other is a boolean,
4443 * then the comparison will be true if and only if the result of performing
4444 * the comparison on the boolean and on the result of converting
4445 * the node-set to a boolean using the boolean function is true.
4446 *
4447 * Returns 0 or 1 depending on the results of the test.
4448 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004449static int
Owen Taylor3473f882001-02-23 17:55:21 +00004450xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict,
4451 xmlXPathObjectPtr arg, xmlXPathObjectPtr val) {
4452 if ((val == NULL) || (arg == NULL) ||
4453 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
4454 return(0);
4455
4456 switch(val->type) {
4457 case XPATH_NUMBER:
4458 return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val));
4459 case XPATH_NODESET:
4460 case XPATH_XSLT_TREE:
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004461 return(xmlXPathCompareNodeSets(inf, strict, arg, val));
Owen Taylor3473f882001-02-23 17:55:21 +00004462 case XPATH_STRING:
4463 return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val));
4464 case XPATH_BOOLEAN:
4465 valuePush(ctxt, arg);
4466 xmlXPathBooleanFunction(ctxt, 1);
4467 valuePush(ctxt, val);
4468 return(xmlXPathCompareValues(ctxt, inf, strict));
4469 default:
4470 TODO
Owen Taylor3473f882001-02-23 17:55:21 +00004471 }
4472 return(0);
4473}
4474
4475/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004476 * xmlXPathEqualNodeSetString:
Owen Taylor3473f882001-02-23 17:55:21 +00004477 * @arg: the nodeset object argument
4478 * @str: the string to compare to.
William M. Brack0c022ad2002-07-12 00:56:01 +00004479 * @neq: flag to show whether for '=' (0) or '!=' (1)
Owen Taylor3473f882001-02-23 17:55:21 +00004480 *
4481 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4482 * If one object to be compared is a node-set and the other is a string,
4483 * then the comparison will be true if and only if there is a node in
4484 * the node-set such that the result of performing the comparison on the
4485 * string-value of the node and the other string is true.
4486 *
4487 * Returns 0 or 1 depending on the results of the test.
4488 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004489static int
William M. Brack0c022ad2002-07-12 00:56:01 +00004490xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str, int neq)
Daniel Veillardf06307e2001-07-03 10:35:50 +00004491{
Owen Taylor3473f882001-02-23 17:55:21 +00004492 int i;
4493 xmlNodeSetPtr ns;
4494 xmlChar *str2;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004495 unsigned int hash;
Owen Taylor3473f882001-02-23 17:55:21 +00004496
4497 if ((str == NULL) || (arg == NULL) ||
Daniel Veillardf06307e2001-07-03 10:35:50 +00004498 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
4499 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00004500 ns = arg->nodesetval;
William M. Brackc125a722003-11-16 08:06:19 +00004501 /*
4502 * A NULL nodeset compared with a string is always false
4503 * (since there is no node equal, and no node not equal)
4504 */
4505 if ((ns == NULL) || (ns->nodeNr <= 0) )
Daniel Veillardf06307e2001-07-03 10:35:50 +00004506 return (0);
William M. Brackc125a722003-11-16 08:06:19 +00004507 hash = xmlXPathStringHash(str);
Daniel Veillardf06307e2001-07-03 10:35:50 +00004508 for (i = 0; i < ns->nodeNr; i++) {
4509 if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) {
4510 str2 = xmlNodeGetContent(ns->nodeTab[i]);
4511 if ((str2 != NULL) && (xmlStrEqual(str, str2))) {
4512 xmlFree(str2);
William M. Brack0c022ad2002-07-12 00:56:01 +00004513 if (neq)
4514 continue;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004515 return (1);
Daniel Veillard9adc0462003-03-24 18:39:54 +00004516 } else if ((str2 == NULL) && (xmlStrEqual(str, BAD_CAST ""))) {
4517 if (neq)
4518 continue;
4519 return (1);
William M. Brack0c022ad2002-07-12 00:56:01 +00004520 } else if (neq) {
4521 if (str2 != NULL)
4522 xmlFree(str2);
4523 return (1);
4524 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004525 if (str2 != NULL)
4526 xmlFree(str2);
William M. Brack0c022ad2002-07-12 00:56:01 +00004527 } else if (neq)
4528 return (1);
Owen Taylor3473f882001-02-23 17:55:21 +00004529 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004530 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00004531}
4532
4533/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004534 * xmlXPathEqualNodeSetFloat:
Owen Taylor3473f882001-02-23 17:55:21 +00004535 * @arg: the nodeset object argument
4536 * @f: the float to compare to
William M. Brack0c022ad2002-07-12 00:56:01 +00004537 * @neq: flag to show whether to compare '=' (0) or '!=' (1)
Owen Taylor3473f882001-02-23 17:55:21 +00004538 *
4539 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4540 * If one object to be compared is a node-set and the other is a number,
4541 * then the comparison will be true if and only if there is a node in
4542 * the node-set such that the result of performing the comparison on the
4543 * number to be compared and on the result of converting the string-value
4544 * of that node to a number using the number function is true.
4545 *
4546 * Returns 0 or 1 depending on the results of the test.
4547 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004548static int
William M. Brack0c022ad2002-07-12 00:56:01 +00004549xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt,
4550 xmlXPathObjectPtr arg, double f, int neq) {
4551 int i, ret=0;
4552 xmlNodeSetPtr ns;
4553 xmlChar *str2;
4554 xmlXPathObjectPtr val;
4555 double v;
Owen Taylor3473f882001-02-23 17:55:21 +00004556
4557 if ((arg == NULL) ||
4558 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
4559 return(0);
4560
William M. Brack0c022ad2002-07-12 00:56:01 +00004561 ns = arg->nodesetval;
4562 if (ns != NULL) {
4563 for (i=0;i<ns->nodeNr;i++) {
4564 str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
4565 if (str2 != NULL) {
4566 valuePush(ctxt, xmlXPathNewString(str2));
4567 xmlFree(str2);
4568 xmlXPathNumberFunction(ctxt, 1);
4569 val = valuePop(ctxt);
4570 v = val->floatval;
4571 xmlXPathFreeObject(val);
4572 if (!xmlXPathIsNaN(v)) {
4573 if ((!neq) && (v==f)) {
4574 ret = 1;
4575 break;
4576 } else if ((neq) && (v!=f)) {
4577 ret = 1;
4578 break;
4579 }
4580 }
4581 }
4582 }
4583 }
4584
4585 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004586}
4587
4588
4589/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00004590 * xmlXPathEqualNodeSets:
Owen Taylor3473f882001-02-23 17:55:21 +00004591 * @arg1: first nodeset object argument
4592 * @arg2: second nodeset object argument
William M. Brack0c022ad2002-07-12 00:56:01 +00004593 * @neq: flag to show whether to test '=' (0) or '!=' (1)
Owen Taylor3473f882001-02-23 17:55:21 +00004594 *
William M. Brack0c022ad2002-07-12 00:56:01 +00004595 * Implement the equal / not equal operation on XPath nodesets:
4596 * @arg1 == @arg2 or @arg1 != @arg2
Owen Taylor3473f882001-02-23 17:55:21 +00004597 * If both objects to be compared are node-sets, then the comparison
4598 * will be true if and only if there is a node in the first node-set and
4599 * a node in the second node-set such that the result of performing the
4600 * comparison on the string-values of the two nodes is true.
4601 *
4602 * (needless to say, this is a costly operation)
4603 *
4604 * Returns 0 or 1 depending on the results of the test.
4605 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004606static int
William M. Brack0c022ad2002-07-12 00:56:01 +00004607xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2, int neq) {
Owen Taylor3473f882001-02-23 17:55:21 +00004608 int i, j;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004609 unsigned int *hashs1;
4610 unsigned int *hashs2;
Owen Taylor3473f882001-02-23 17:55:21 +00004611 xmlChar **values1;
4612 xmlChar **values2;
4613 int ret = 0;
4614 xmlNodeSetPtr ns1;
4615 xmlNodeSetPtr ns2;
4616
4617 if ((arg1 == NULL) ||
4618 ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)))
4619 return(0);
4620 if ((arg2 == NULL) ||
4621 ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE)))
4622 return(0);
4623
4624 ns1 = arg1->nodesetval;
4625 ns2 = arg2->nodesetval;
4626
Daniel Veillard911f49a2001-04-07 15:39:35 +00004627 if ((ns1 == NULL) || (ns1->nodeNr <= 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004628 return(0);
Daniel Veillard911f49a2001-04-07 15:39:35 +00004629 if ((ns2 == NULL) || (ns2->nodeNr <= 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004630 return(0);
4631
4632 /*
William M. Brack0c022ad2002-07-12 00:56:01 +00004633 * for equal, check if there is a node pertaining to both sets
Owen Taylor3473f882001-02-23 17:55:21 +00004634 */
William M. Brack0c022ad2002-07-12 00:56:01 +00004635 if (neq == 0)
4636 for (i = 0;i < ns1->nodeNr;i++)
4637 for (j = 0;j < ns2->nodeNr;j++)
4638 if (ns1->nodeTab[i] == ns2->nodeTab[j])
4639 return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00004640
4641 values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *));
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004642 if (values1 == NULL) {
4643 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004644 return(0);
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004645 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004646 hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int));
4647 if (hashs1 == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004648 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Daniel Veillardf06307e2001-07-03 10:35:50 +00004649 xmlFree(values1);
4650 return(0);
4651 }
Owen Taylor3473f882001-02-23 17:55:21 +00004652 memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *));
4653 values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *));
4654 if (values2 == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004655 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Daniel Veillardf06307e2001-07-03 10:35:50 +00004656 xmlFree(hashs1);
Owen Taylor3473f882001-02-23 17:55:21 +00004657 xmlFree(values1);
4658 return(0);
4659 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004660 hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int));
4661 if (hashs2 == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +00004662 xmlXPathErrMemory(NULL, "comparing nodesets\n");
Daniel Veillardf06307e2001-07-03 10:35:50 +00004663 xmlFree(hashs1);
4664 xmlFree(values1);
4665 xmlFree(values2);
4666 return(0);
4667 }
Owen Taylor3473f882001-02-23 17:55:21 +00004668 memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *));
4669 for (i = 0;i < ns1->nodeNr;i++) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00004670 hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00004671 for (j = 0;j < ns2->nodeNr;j++) {
4672 if (i == 0)
Daniel Veillardf06307e2001-07-03 10:35:50 +00004673 hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]);
William M. Brack0c022ad2002-07-12 00:56:01 +00004674 if (hashs1[i] != hashs2[j]) {
4675 if (neq) {
4676 ret = 1;
4677 break;
4678 }
4679 }
4680 else {
Daniel Veillardf06307e2001-07-03 10:35:50 +00004681 if (values1[i] == NULL)
4682 values1[i] = xmlNodeGetContent(ns1->nodeTab[i]);
4683 if (values2[j] == NULL)
4684 values2[j] = xmlNodeGetContent(ns2->nodeTab[j]);
William M. Brack0c022ad2002-07-12 00:56:01 +00004685 ret = xmlStrEqual(values1[i], values2[j]) ^ neq;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004686 if (ret)
4687 break;
4688 }
Owen Taylor3473f882001-02-23 17:55:21 +00004689 }
4690 if (ret)
4691 break;
4692 }
4693 for (i = 0;i < ns1->nodeNr;i++)
4694 if (values1[i] != NULL)
4695 xmlFree(values1[i]);
4696 for (j = 0;j < ns2->nodeNr;j++)
4697 if (values2[j] != NULL)
4698 xmlFree(values2[j]);
4699 xmlFree(values1);
4700 xmlFree(values2);
Daniel Veillardf06307e2001-07-03 10:35:50 +00004701 xmlFree(hashs1);
4702 xmlFree(hashs2);
Owen Taylor3473f882001-02-23 17:55:21 +00004703 return(ret);
4704}
4705
William M. Brack0c022ad2002-07-12 00:56:01 +00004706static int
4707xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt,
4708 xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) {
Owen Taylor3473f882001-02-23 17:55:21 +00004709 int ret = 0;
William M. Brack0c022ad2002-07-12 00:56:01 +00004710 /*
4711 *At this point we are assured neither arg1 nor arg2
4712 *is a nodeset, so we can just pick the appropriate routine.
4713 */
Owen Taylor3473f882001-02-23 17:55:21 +00004714 switch (arg1->type) {
4715 case XPATH_UNDEFINED:
4716#ifdef DEBUG_EXPR
4717 xmlGenericError(xmlGenericErrorContext,
4718 "Equal: undefined\n");
4719#endif
4720 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004721 case XPATH_BOOLEAN:
4722 switch (arg2->type) {
4723 case XPATH_UNDEFINED:
4724#ifdef DEBUG_EXPR
4725 xmlGenericError(xmlGenericErrorContext,
4726 "Equal: undefined\n");
4727#endif
4728 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004729 case XPATH_BOOLEAN:
4730#ifdef DEBUG_EXPR
4731 xmlGenericError(xmlGenericErrorContext,
4732 "Equal: %d boolean %d \n",
4733 arg1->boolval, arg2->boolval);
4734#endif
4735 ret = (arg1->boolval == arg2->boolval);
4736 break;
4737 case XPATH_NUMBER:
William M. Brackef61d202002-07-19 08:32:00 +00004738 ret = (arg1->boolval ==
4739 xmlXPathCastNumberToBoolean(arg2->floatval));
Owen Taylor3473f882001-02-23 17:55:21 +00004740 break;
4741 case XPATH_STRING:
4742 if ((arg2->stringval == NULL) ||
4743 (arg2->stringval[0] == 0)) ret = 0;
4744 else
4745 ret = 1;
4746 ret = (arg1->boolval == ret);
4747 break;
4748 case XPATH_USERS:
4749 case XPATH_POINT:
4750 case XPATH_RANGE:
4751 case XPATH_LOCATIONSET:
4752 TODO
4753 break;
William M. Brack0c022ad2002-07-12 00:56:01 +00004754 case XPATH_NODESET:
4755 case XPATH_XSLT_TREE:
4756 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004757 }
4758 break;
4759 case XPATH_NUMBER:
4760 switch (arg2->type) {
4761 case XPATH_UNDEFINED:
4762#ifdef DEBUG_EXPR
4763 xmlGenericError(xmlGenericErrorContext,
4764 "Equal: undefined\n");
4765#endif
4766 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004767 case XPATH_BOOLEAN:
William M. Brackef61d202002-07-19 08:32:00 +00004768 ret = (arg2->boolval==
4769 xmlXPathCastNumberToBoolean(arg1->floatval));
Owen Taylor3473f882001-02-23 17:55:21 +00004770 break;
4771 case XPATH_STRING:
4772 valuePush(ctxt, arg2);
4773 xmlXPathNumberFunction(ctxt, 1);
4774 arg2 = valuePop(ctxt);
4775 /* no break on purpose */
4776 case XPATH_NUMBER:
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004777 /* Hand check NaN and Infinity equalities */
William M. Brack08171912003-12-29 02:52:11 +00004778 if (xmlXPathIsNaN(arg1->floatval) ||
4779 xmlXPathIsNaN(arg2->floatval)) {
Daniel Veillard21458c82002-03-27 16:12:22 +00004780 ret = 0;
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004781 } else if (xmlXPathIsInf(arg1->floatval) == 1) {
4782 if (xmlXPathIsInf(arg2->floatval) == 1)
4783 ret = 1;
4784 else
4785 ret = 0;
4786 } else if (xmlXPathIsInf(arg1->floatval) == -1) {
4787 if (xmlXPathIsInf(arg2->floatval) == -1)
4788 ret = 1;
4789 else
4790 ret = 0;
4791 } else if (xmlXPathIsInf(arg2->floatval) == 1) {
4792 if (xmlXPathIsInf(arg1->floatval) == 1)
4793 ret = 1;
4794 else
4795 ret = 0;
4796 } else if (xmlXPathIsInf(arg2->floatval) == -1) {
4797 if (xmlXPathIsInf(arg1->floatval) == -1)
4798 ret = 1;
4799 else
4800 ret = 0;
Daniel Veillard21458c82002-03-27 16:12:22 +00004801 } else {
4802 ret = (arg1->floatval == arg2->floatval);
4803 }
Owen Taylor3473f882001-02-23 17:55:21 +00004804 break;
4805 case XPATH_USERS:
4806 case XPATH_POINT:
4807 case XPATH_RANGE:
4808 case XPATH_LOCATIONSET:
4809 TODO
4810 break;
William M. Brack0c022ad2002-07-12 00:56:01 +00004811 case XPATH_NODESET:
4812 case XPATH_XSLT_TREE:
4813 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004814 }
4815 break;
4816 case XPATH_STRING:
4817 switch (arg2->type) {
4818 case XPATH_UNDEFINED:
4819#ifdef DEBUG_EXPR
4820 xmlGenericError(xmlGenericErrorContext,
4821 "Equal: undefined\n");
4822#endif
4823 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004824 case XPATH_BOOLEAN:
4825 if ((arg1->stringval == NULL) ||
4826 (arg1->stringval[0] == 0)) ret = 0;
4827 else
4828 ret = 1;
4829 ret = (arg2->boolval == ret);
4830 break;
4831 case XPATH_STRING:
4832 ret = xmlStrEqual(arg1->stringval, arg2->stringval);
4833 break;
4834 case XPATH_NUMBER:
4835 valuePush(ctxt, arg1);
4836 xmlXPathNumberFunction(ctxt, 1);
4837 arg1 = valuePop(ctxt);
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004838 /* Hand check NaN and Infinity equalities */
William M. Brack08171912003-12-29 02:52:11 +00004839 if (xmlXPathIsNaN(arg1->floatval) ||
4840 xmlXPathIsNaN(arg2->floatval)) {
Daniel Veillard21458c82002-03-27 16:12:22 +00004841 ret = 0;
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004842 } else if (xmlXPathIsInf(arg1->floatval) == 1) {
4843 if (xmlXPathIsInf(arg2->floatval) == 1)
4844 ret = 1;
4845 else
4846 ret = 0;
4847 } else if (xmlXPathIsInf(arg1->floatval) == -1) {
4848 if (xmlXPathIsInf(arg2->floatval) == -1)
4849 ret = 1;
4850 else
4851 ret = 0;
4852 } else if (xmlXPathIsInf(arg2->floatval) == 1) {
4853 if (xmlXPathIsInf(arg1->floatval) == 1)
4854 ret = 1;
4855 else
4856 ret = 0;
4857 } else if (xmlXPathIsInf(arg2->floatval) == -1) {
4858 if (xmlXPathIsInf(arg1->floatval) == -1)
4859 ret = 1;
4860 else
4861 ret = 0;
Daniel Veillard21458c82002-03-27 16:12:22 +00004862 } else {
4863 ret = (arg1->floatval == arg2->floatval);
4864 }
Owen Taylor3473f882001-02-23 17:55:21 +00004865 break;
4866 case XPATH_USERS:
4867 case XPATH_POINT:
4868 case XPATH_RANGE:
4869 case XPATH_LOCATIONSET:
4870 TODO
4871 break;
William M. Brack0c022ad2002-07-12 00:56:01 +00004872 case XPATH_NODESET:
4873 case XPATH_XSLT_TREE:
4874 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004875 }
4876 break;
4877 case XPATH_USERS:
4878 case XPATH_POINT:
4879 case XPATH_RANGE:
4880 case XPATH_LOCATIONSET:
4881 TODO
4882 break;
William M. Brack0c022ad2002-07-12 00:56:01 +00004883 case XPATH_NODESET:
4884 case XPATH_XSLT_TREE:
4885 break;
Owen Taylor3473f882001-02-23 17:55:21 +00004886 }
4887 xmlXPathFreeObject(arg1);
4888 xmlXPathFreeObject(arg2);
4889 return(ret);
4890}
4891
William M. Brack0c022ad2002-07-12 00:56:01 +00004892/**
4893 * xmlXPathEqualValues:
4894 * @ctxt: the XPath Parser context
4895 *
4896 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4897 *
4898 * Returns 0 or 1 depending on the results of the test.
4899 */
4900int
4901xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) {
4902 xmlXPathObjectPtr arg1, arg2, argtmp;
4903 int ret = 0;
4904
Daniel Veillard6128c012004-11-08 17:16:15 +00004905 if ((ctxt == NULL) || (ctxt->context == NULL)) return(0);
William M. Brack0c022ad2002-07-12 00:56:01 +00004906 arg2 = valuePop(ctxt);
4907 arg1 = valuePop(ctxt);
4908 if ((arg1 == NULL) || (arg2 == NULL)) {
4909 if (arg1 != NULL)
4910 xmlXPathFreeObject(arg1);
4911 else
4912 xmlXPathFreeObject(arg2);
4913 XP_ERROR0(XPATH_INVALID_OPERAND);
4914 }
4915
4916 if (arg1 == arg2) {
4917#ifdef DEBUG_EXPR
4918 xmlGenericError(xmlGenericErrorContext,
4919 "Equal: by pointer\n");
4920#endif
William M. Brack2c19a7b2005-04-10 01:03:23 +00004921 xmlXPathFreeObject(arg1);
William M. Brack0c022ad2002-07-12 00:56:01 +00004922 return(1);
4923 }
4924
4925 /*
4926 *If either argument is a nodeset, it's a 'special case'
4927 */
4928 if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) ||
4929 (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
4930 /*
4931 *Hack it to assure arg1 is the nodeset
4932 */
4933 if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) {
4934 argtmp = arg2;
4935 arg2 = arg1;
4936 arg1 = argtmp;
4937 }
4938 switch (arg2->type) {
4939 case XPATH_UNDEFINED:
4940#ifdef DEBUG_EXPR
4941 xmlGenericError(xmlGenericErrorContext,
4942 "Equal: undefined\n");
4943#endif
4944 break;
4945 case XPATH_NODESET:
4946 case XPATH_XSLT_TREE:
4947 ret = xmlXPathEqualNodeSets(arg1, arg2, 0);
4948 break;
4949 case XPATH_BOOLEAN:
4950 if ((arg1->nodesetval == NULL) ||
4951 (arg1->nodesetval->nodeNr == 0)) ret = 0;
4952 else
4953 ret = 1;
4954 ret = (ret == arg2->boolval);
4955 break;
4956 case XPATH_NUMBER:
4957 ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 0);
4958 break;
4959 case XPATH_STRING:
4960 ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval, 0);
4961 break;
4962 case XPATH_USERS:
4963 case XPATH_POINT:
4964 case XPATH_RANGE:
4965 case XPATH_LOCATIONSET:
4966 TODO
4967 break;
4968 }
4969 xmlXPathFreeObject(arg1);
4970 xmlXPathFreeObject(arg2);
4971 return(ret);
4972 }
4973
4974 return (xmlXPathEqualValuesCommon(ctxt, arg1, arg2));
4975}
4976
4977/**
4978 * xmlXPathNotEqualValues:
4979 * @ctxt: the XPath Parser context
4980 *
4981 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4982 *
4983 * Returns 0 or 1 depending on the results of the test.
4984 */
4985int
4986xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) {
4987 xmlXPathObjectPtr arg1, arg2, argtmp;
4988 int ret = 0;
4989
Daniel Veillard6128c012004-11-08 17:16:15 +00004990 if ((ctxt == NULL) || (ctxt->context == NULL)) return(0);
William M. Brack0c022ad2002-07-12 00:56:01 +00004991 arg2 = valuePop(ctxt);
4992 arg1 = valuePop(ctxt);
4993 if ((arg1 == NULL) || (arg2 == NULL)) {
4994 if (arg1 != NULL)
4995 xmlXPathFreeObject(arg1);
4996 else
4997 xmlXPathFreeObject(arg2);
4998 XP_ERROR0(XPATH_INVALID_OPERAND);
4999 }
5000
5001 if (arg1 == arg2) {
5002#ifdef DEBUG_EXPR
5003 xmlGenericError(xmlGenericErrorContext,
5004 "NotEqual: by pointer\n");
5005#endif
William M. Brack2c19a7b2005-04-10 01:03:23 +00005006 xmlXPathFreeObject(arg1);
William M. Brack0c022ad2002-07-12 00:56:01 +00005007 return(0);
5008 }
5009
5010 /*
5011 *If either argument is a nodeset, it's a 'special case'
5012 */
5013 if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) ||
5014 (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
5015 /*
5016 *Hack it to assure arg1 is the nodeset
5017 */
5018 if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) {
5019 argtmp = arg2;
5020 arg2 = arg1;
5021 arg1 = argtmp;
5022 }
5023 switch (arg2->type) {
5024 case XPATH_UNDEFINED:
5025#ifdef DEBUG_EXPR
5026 xmlGenericError(xmlGenericErrorContext,
5027 "NotEqual: undefined\n");
5028#endif
5029 break;
5030 case XPATH_NODESET:
5031 case XPATH_XSLT_TREE:
5032 ret = xmlXPathEqualNodeSets(arg1, arg2, 1);
5033 break;
5034 case XPATH_BOOLEAN:
5035 if ((arg1->nodesetval == NULL) ||
5036 (arg1->nodesetval->nodeNr == 0)) ret = 0;
5037 else
5038 ret = 1;
William M. Brackef61d202002-07-19 08:32:00 +00005039 ret = (ret != arg2->boolval);
William M. Brack0c022ad2002-07-12 00:56:01 +00005040 break;
5041 case XPATH_NUMBER:
5042 ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 1);
5043 break;
5044 case XPATH_STRING:
5045 ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval,1);
5046 break;
5047 case XPATH_USERS:
5048 case XPATH_POINT:
5049 case XPATH_RANGE:
5050 case XPATH_LOCATIONSET:
5051 TODO
5052 break;
5053 }
5054 xmlXPathFreeObject(arg1);
5055 xmlXPathFreeObject(arg2);
5056 return(ret);
5057 }
5058
5059 return (!xmlXPathEqualValuesCommon(ctxt, arg1, arg2));
5060}
Owen Taylor3473f882001-02-23 17:55:21 +00005061
5062/**
5063 * xmlXPathCompareValues:
5064 * @ctxt: the XPath Parser context
5065 * @inf: less than (1) or greater than (0)
5066 * @strict: is the comparison strict
5067 *
5068 * Implement the compare operation on XPath objects:
5069 * @arg1 < @arg2 (1, 1, ...
5070 * @arg1 <= @arg2 (1, 0, ...
5071 * @arg1 > @arg2 (0, 1, ...
5072 * @arg1 >= @arg2 (0, 0, ...
5073 *
5074 * When neither object to be compared is a node-set and the operator is
5075 * <=, <, >=, >, then the objects are compared by converted both objects
5076 * to numbers and comparing the numbers according to IEEE 754. The <
5077 * comparison will be true if and only if the first number is less than the
5078 * second number. The <= comparison will be true if and only if the first
5079 * number is less than or equal to the second number. The > comparison
5080 * will be true if and only if the first number is greater than the second
5081 * number. The >= comparison will be true if and only if the first number
5082 * is greater than or equal to the second number.
5083 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005084 * Returns 1 if the comparison succeeded, 0 if it failed
Owen Taylor3473f882001-02-23 17:55:21 +00005085 */
5086int
5087xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) {
Daniel Veillardd30be4a2002-03-28 18:25:31 +00005088 int ret = 0, arg1i = 0, arg2i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005089 xmlXPathObjectPtr arg1, arg2;
5090
Daniel Veillard6128c012004-11-08 17:16:15 +00005091 if ((ctxt == NULL) || (ctxt->context == NULL)) return(0);
William M. Brack0c022ad2002-07-12 00:56:01 +00005092 arg2 = valuePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005093 arg1 = valuePop(ctxt);
William M. Brack0c022ad2002-07-12 00:56:01 +00005094 if ((arg1 == NULL) || (arg2 == NULL)) {
5095 if (arg1 != NULL)
5096 xmlXPathFreeObject(arg1);
5097 else
5098 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00005099 XP_ERROR0(XPATH_INVALID_OPERAND);
5100 }
5101
William M. Brack0c022ad2002-07-12 00:56:01 +00005102 if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) ||
5103 (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
5104 if (((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE)) &&
5105 ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE))){
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005106 ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00005107 } else {
William M. Brack0c022ad2002-07-12 00:56:01 +00005108 if ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) {
Daniel Veillard4af6b6e2001-03-06 08:33:38 +00005109 ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict,
5110 arg1, arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00005111 } else {
Daniel Veillard4af6b6e2001-03-06 08:33:38 +00005112 ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict,
5113 arg2, arg1);
Owen Taylor3473f882001-02-23 17:55:21 +00005114 }
5115 }
William M. Brack2c19a7b2005-04-10 01:03:23 +00005116 /*
5117 * Note from Bill: I don't understand why, but arg1 and arg2 shouldn't be
5118 * freed here!
5119 */
Owen Taylor3473f882001-02-23 17:55:21 +00005120 return(ret);
5121 }
5122
5123 if (arg1->type != XPATH_NUMBER) {
5124 valuePush(ctxt, arg1);
5125 xmlXPathNumberFunction(ctxt, 1);
5126 arg1 = valuePop(ctxt);
5127 }
5128 if (arg1->type != XPATH_NUMBER) {
5129 xmlXPathFreeObject(arg1);
5130 xmlXPathFreeObject(arg2);
5131 XP_ERROR0(XPATH_INVALID_OPERAND);
5132 }
5133 if (arg2->type != XPATH_NUMBER) {
5134 valuePush(ctxt, arg2);
5135 xmlXPathNumberFunction(ctxt, 1);
5136 arg2 = valuePop(ctxt);
5137 }
5138 if (arg2->type != XPATH_NUMBER) {
5139 xmlXPathFreeObject(arg1);
5140 xmlXPathFreeObject(arg2);
5141 XP_ERROR0(XPATH_INVALID_OPERAND);
5142 }
5143 /*
5144 * Add tests for infinity and nan
5145 * => feedback on 3.4 for Inf and NaN
5146 */
Daniel Veillardd30be4a2002-03-28 18:25:31 +00005147 /* Hand check NaN and Infinity comparisons */
Daniel Veillard21458c82002-03-27 16:12:22 +00005148 if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) {
Daniel Veillardd30be4a2002-03-28 18:25:31 +00005149 ret=0;
Daniel Veillard21458c82002-03-27 16:12:22 +00005150 } else {
Daniel Veillardd30be4a2002-03-28 18:25:31 +00005151 arg1i=xmlXPathIsInf(arg1->floatval);
5152 arg2i=xmlXPathIsInf(arg2->floatval);
5153 if (inf && strict) {
5154 if ((arg1i == -1 && arg2i != -1) ||
5155 (arg2i == 1 && arg1i != 1)) {
5156 ret = 1;
5157 } else if (arg1i == 0 && arg2i == 0) {
5158 ret = (arg1->floatval < arg2->floatval);
5159 } else {
5160 ret = 0;
5161 }
5162 }
5163 else if (inf && !strict) {
5164 if (arg1i == -1 || arg2i == 1) {
5165 ret = 1;
5166 } else if (arg1i == 0 && arg2i == 0) {
5167 ret = (arg1->floatval <= arg2->floatval);
5168 } else {
5169 ret = 0;
5170 }
5171 }
5172 else if (!inf && strict) {
5173 if ((arg1i == 1 && arg2i != 1) ||
5174 (arg2i == -1 && arg1i != -1)) {
5175 ret = 1;
5176 } else if (arg1i == 0 && arg2i == 0) {
5177 ret = (arg1->floatval > arg2->floatval);
5178 } else {
5179 ret = 0;
5180 }
5181 }
5182 else if (!inf && !strict) {
5183 if (arg1i == 1 || arg2i == -1) {
5184 ret = 1;
5185 } else if (arg1i == 0 && arg2i == 0) {
5186 ret = (arg1->floatval >= arg2->floatval);
5187 } else {
5188 ret = 0;
5189 }
5190 }
Daniel Veillard21458c82002-03-27 16:12:22 +00005191 }
Owen Taylor3473f882001-02-23 17:55:21 +00005192 xmlXPathFreeObject(arg1);
5193 xmlXPathFreeObject(arg2);
5194 return(ret);
5195}
5196
5197/**
5198 * xmlXPathValueFlipSign:
5199 * @ctxt: the XPath Parser context
5200 *
5201 * Implement the unary - operation on an XPath object
5202 * The numeric operators convert their operands to numbers as if
5203 * by calling the number function.
5204 */
5205void
5206xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00005207 if ((ctxt == NULL) || (ctxt->context == NULL)) return;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005208 CAST_TO_NUMBER;
5209 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillardeca82812002-04-24 11:42:02 +00005210 if (xmlXPathIsNaN(ctxt->value->floatval))
5211 ctxt->value->floatval=xmlXPathNAN;
5212 else if (xmlXPathIsInf(ctxt->value->floatval) == 1)
5213 ctxt->value->floatval=xmlXPathNINF;
5214 else if (xmlXPathIsInf(ctxt->value->floatval) == -1)
5215 ctxt->value->floatval=xmlXPathPINF;
5216 else if (ctxt->value->floatval == 0) {
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005217 if (xmlXPathGetSign(ctxt->value->floatval) == 0)
5218 ctxt->value->floatval = xmlXPathNZERO;
5219 else
5220 ctxt->value->floatval = 0;
5221 }
5222 else
5223 ctxt->value->floatval = - ctxt->value->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00005224}
5225
5226/**
5227 * xmlXPathAddValues:
5228 * @ctxt: the XPath Parser context
5229 *
5230 * Implement the add operation on XPath objects:
5231 * The numeric operators convert their operands to numbers as if
5232 * by calling the number function.
5233 */
5234void
5235xmlXPathAddValues(xmlXPathParserContextPtr ctxt) {
5236 xmlXPathObjectPtr arg;
5237 double val;
5238
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005239 arg = valuePop(ctxt);
5240 if (arg == NULL)
5241 XP_ERROR(XPATH_INVALID_OPERAND);
5242 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005243 xmlXPathFreeObject(arg);
5244
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005245 CAST_TO_NUMBER;
5246 CHECK_TYPE(XPATH_NUMBER);
5247 ctxt->value->floatval += val;
Owen Taylor3473f882001-02-23 17:55:21 +00005248}
5249
5250/**
5251 * xmlXPathSubValues:
5252 * @ctxt: the XPath Parser context
5253 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005254 * Implement the subtraction operation on XPath objects:
Owen Taylor3473f882001-02-23 17:55:21 +00005255 * The numeric operators convert their operands to numbers as if
5256 * by calling the number function.
5257 */
5258void
5259xmlXPathSubValues(xmlXPathParserContextPtr ctxt) {
5260 xmlXPathObjectPtr arg;
5261 double val;
5262
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005263 arg = valuePop(ctxt);
5264 if (arg == NULL)
5265 XP_ERROR(XPATH_INVALID_OPERAND);
5266 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005267 xmlXPathFreeObject(arg);
5268
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005269 CAST_TO_NUMBER;
5270 CHECK_TYPE(XPATH_NUMBER);
5271 ctxt->value->floatval -= val;
Owen Taylor3473f882001-02-23 17:55:21 +00005272}
5273
5274/**
5275 * xmlXPathMultValues:
5276 * @ctxt: the XPath Parser context
5277 *
5278 * Implement the multiply operation on XPath objects:
5279 * The numeric operators convert their operands to numbers as if
5280 * by calling the number function.
5281 */
5282void
5283xmlXPathMultValues(xmlXPathParserContextPtr ctxt) {
5284 xmlXPathObjectPtr arg;
5285 double val;
5286
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005287 arg = valuePop(ctxt);
5288 if (arg == NULL)
5289 XP_ERROR(XPATH_INVALID_OPERAND);
5290 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005291 xmlXPathFreeObject(arg);
5292
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005293 CAST_TO_NUMBER;
5294 CHECK_TYPE(XPATH_NUMBER);
5295 ctxt->value->floatval *= val;
Owen Taylor3473f882001-02-23 17:55:21 +00005296}
5297
5298/**
5299 * xmlXPathDivValues:
5300 * @ctxt: the XPath Parser context
5301 *
5302 * Implement the div operation on XPath objects @arg1 / @arg2:
5303 * The numeric operators convert their operands to numbers as if
5304 * by calling the number function.
5305 */
5306void
5307xmlXPathDivValues(xmlXPathParserContextPtr ctxt) {
5308 xmlXPathObjectPtr arg;
5309 double val;
5310
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005311 arg = valuePop(ctxt);
5312 if (arg == NULL)
5313 XP_ERROR(XPATH_INVALID_OPERAND);
5314 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005315 xmlXPathFreeObject(arg);
5316
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005317 CAST_TO_NUMBER;
5318 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillardeca82812002-04-24 11:42:02 +00005319 if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval))
5320 ctxt->value->floatval = xmlXPathNAN;
5321 else if (val == 0 && xmlXPathGetSign(val) != 0) {
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005322 if (ctxt->value->floatval == 0)
5323 ctxt->value->floatval = xmlXPathNAN;
5324 else if (ctxt->value->floatval > 0)
5325 ctxt->value->floatval = xmlXPathNINF;
5326 else if (ctxt->value->floatval < 0)
5327 ctxt->value->floatval = xmlXPathPINF;
5328 }
5329 else if (val == 0) {
Daniel Veillard5f4b5992002-02-20 10:22:49 +00005330 if (ctxt->value->floatval == 0)
5331 ctxt->value->floatval = xmlXPathNAN;
5332 else if (ctxt->value->floatval > 0)
5333 ctxt->value->floatval = xmlXPathPINF;
5334 else if (ctxt->value->floatval < 0)
5335 ctxt->value->floatval = xmlXPathNINF;
5336 } else
5337 ctxt->value->floatval /= val;
Owen Taylor3473f882001-02-23 17:55:21 +00005338}
5339
5340/**
5341 * xmlXPathModValues:
5342 * @ctxt: the XPath Parser context
5343 *
5344 * Implement the mod operation on XPath objects: @arg1 / @arg2
5345 * The numeric operators convert their operands to numbers as if
5346 * by calling the number function.
5347 */
5348void
5349xmlXPathModValues(xmlXPathParserContextPtr ctxt) {
5350 xmlXPathObjectPtr arg;
Daniel Veillardfdc91562002-07-01 21:52:03 +00005351 double arg1, arg2;
Owen Taylor3473f882001-02-23 17:55:21 +00005352
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005353 arg = valuePop(ctxt);
5354 if (arg == NULL)
5355 XP_ERROR(XPATH_INVALID_OPERAND);
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005356 arg2 = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00005357 xmlXPathFreeObject(arg);
5358
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005359 CAST_TO_NUMBER;
5360 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005361 arg1 = ctxt->value->floatval;
Daniel Veillard268fd1b2001-08-26 18:46:36 +00005362 if (arg2 == 0)
5363 ctxt->value->floatval = xmlXPathNAN;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005364 else {
Daniel Veillardfdc91562002-07-01 21:52:03 +00005365 ctxt->value->floatval = fmod(arg1, arg2);
Daniel Veillard5fc1f082002-03-27 09:05:40 +00005366 }
Owen Taylor3473f882001-02-23 17:55:21 +00005367}
5368
5369/************************************************************************
5370 * *
5371 * The traversal functions *
5372 * *
5373 ************************************************************************/
5374
Owen Taylor3473f882001-02-23 17:55:21 +00005375/*
5376 * A traversal function enumerates nodes along an axis.
5377 * Initially it must be called with NULL, and it indicates
5378 * termination on the axis by returning NULL.
5379 */
5380typedef xmlNodePtr (*xmlXPathTraversalFunction)
5381 (xmlXPathParserContextPtr ctxt, xmlNodePtr cur);
5382
5383/**
5384 * xmlXPathNextSelf:
5385 * @ctxt: the XPath Parser context
5386 * @cur: the current node in the traversal
5387 *
5388 * Traversal function for the "self" direction
5389 * The self axis contains just the context node itself
5390 *
5391 * Returns the next element following that axis
5392 */
5393xmlNodePtr
5394xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00005395 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005396 if (cur == NULL)
5397 return(ctxt->context->node);
5398 return(NULL);
5399}
5400
5401/**
5402 * xmlXPathNextChild:
5403 * @ctxt: the XPath Parser context
5404 * @cur: the current node in the traversal
5405 *
5406 * Traversal function for the "child" direction
5407 * The child axis contains the children of the context node in document order.
5408 *
5409 * Returns the next element following that axis
5410 */
5411xmlNodePtr
5412xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00005413 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005414 if (cur == NULL) {
5415 if (ctxt->context->node == NULL) return(NULL);
5416 switch (ctxt->context->node->type) {
5417 case XML_ELEMENT_NODE:
5418 case XML_TEXT_NODE:
5419 case XML_CDATA_SECTION_NODE:
5420 case XML_ENTITY_REF_NODE:
5421 case XML_ENTITY_NODE:
5422 case XML_PI_NODE:
5423 case XML_COMMENT_NODE:
5424 case XML_NOTATION_NODE:
5425 case XML_DTD_NODE:
5426 return(ctxt->context->node->children);
5427 case XML_DOCUMENT_NODE:
5428 case XML_DOCUMENT_TYPE_NODE:
5429 case XML_DOCUMENT_FRAG_NODE:
5430 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005431#ifdef LIBXML_DOCB_ENABLED
5432 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005433#endif
5434 return(((xmlDocPtr) ctxt->context->node)->children);
5435 case XML_ELEMENT_DECL:
5436 case XML_ATTRIBUTE_DECL:
5437 case XML_ENTITY_DECL:
5438 case XML_ATTRIBUTE_NODE:
5439 case XML_NAMESPACE_DECL:
5440 case XML_XINCLUDE_START:
5441 case XML_XINCLUDE_END:
5442 return(NULL);
5443 }
5444 return(NULL);
5445 }
5446 if ((cur->type == XML_DOCUMENT_NODE) ||
5447 (cur->type == XML_HTML_DOCUMENT_NODE))
5448 return(NULL);
5449 return(cur->next);
5450}
5451
5452/**
5453 * xmlXPathNextDescendant:
5454 * @ctxt: the XPath Parser context
5455 * @cur: the current node in the traversal
5456 *
5457 * Traversal function for the "descendant" direction
5458 * the descendant axis contains the descendants of the context node in document
5459 * order; a descendant is a child or a child of a child and so on.
5460 *
5461 * Returns the next element following that axis
5462 */
5463xmlNodePtr
5464xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00005465 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005466 if (cur == NULL) {
5467 if (ctxt->context->node == NULL)
5468 return(NULL);
5469 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5470 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5471 return(NULL);
5472
5473 if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc)
5474 return(ctxt->context->doc->children);
5475 return(ctxt->context->node->children);
5476 }
5477
Daniel Veillard567e1b42001-08-01 15:53:47 +00005478 if (cur->children != NULL) {
Daniel Veillard68e9e742002-11-16 15:35:11 +00005479 /*
5480 * Do not descend on entities declarations
5481 */
5482 if (cur->children->type != XML_ENTITY_DECL) {
5483 cur = cur->children;
5484 /*
5485 * Skip DTDs
5486 */
5487 if (cur->type != XML_DTD_NODE)
5488 return(cur);
5489 }
Daniel Veillard567e1b42001-08-01 15:53:47 +00005490 }
5491
5492 if (cur == ctxt->context->node) return(NULL);
5493
Daniel Veillard68e9e742002-11-16 15:35:11 +00005494 while (cur->next != NULL) {
5495 cur = cur->next;
5496 if ((cur->type != XML_ENTITY_DECL) &&
5497 (cur->type != XML_DTD_NODE))
5498 return(cur);
5499 }
Owen Taylor3473f882001-02-23 17:55:21 +00005500
5501 do {
5502 cur = cur->parent;
5503 if (cur == NULL) return(NULL);
5504 if (cur == ctxt->context->node) return(NULL);
5505 if (cur->next != NULL) {
5506 cur = cur->next;
5507 return(cur);
5508 }
5509 } while (cur != NULL);
5510 return(cur);
5511}
5512
5513/**
5514 * xmlXPathNextDescendantOrSelf:
5515 * @ctxt: the XPath Parser context
5516 * @cur: the current node in the traversal
5517 *
5518 * Traversal function for the "descendant-or-self" direction
5519 * the descendant-or-self axis contains the context node and the descendants
5520 * of the context node in document order; thus the context node is the first
5521 * node on the axis, and the first child of the context node is the second node
5522 * on the axis
5523 *
5524 * Returns the next element following that axis
5525 */
5526xmlNodePtr
5527xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00005528 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005529 if (cur == NULL) {
5530 if (ctxt->context->node == NULL)
5531 return(NULL);
5532 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5533 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5534 return(NULL);
5535 return(ctxt->context->node);
5536 }
5537
5538 return(xmlXPathNextDescendant(ctxt, cur));
5539}
5540
5541/**
5542 * xmlXPathNextParent:
5543 * @ctxt: the XPath Parser context
5544 * @cur: the current node in the traversal
5545 *
5546 * Traversal function for the "parent" direction
5547 * The parent axis contains the parent of the context node, if there is one.
5548 *
5549 * Returns the next element following that axis
5550 */
5551xmlNodePtr
5552xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00005553 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005554 /*
5555 * the parent of an attribute or namespace node is the element
5556 * to which the attribute or namespace node is attached
5557 * Namespace handling !!!
5558 */
5559 if (cur == NULL) {
5560 if (ctxt->context->node == NULL) return(NULL);
5561 switch (ctxt->context->node->type) {
5562 case XML_ELEMENT_NODE:
5563 case XML_TEXT_NODE:
5564 case XML_CDATA_SECTION_NODE:
5565 case XML_ENTITY_REF_NODE:
5566 case XML_ENTITY_NODE:
5567 case XML_PI_NODE:
5568 case XML_COMMENT_NODE:
5569 case XML_NOTATION_NODE:
5570 case XML_DTD_NODE:
5571 case XML_ELEMENT_DECL:
5572 case XML_ATTRIBUTE_DECL:
5573 case XML_XINCLUDE_START:
5574 case XML_XINCLUDE_END:
5575 case XML_ENTITY_DECL:
5576 if (ctxt->context->node->parent == NULL)
5577 return((xmlNodePtr) ctxt->context->doc);
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005578 if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) &&
Daniel Veillard652d8a92003-02-04 19:28:49 +00005579 ((ctxt->context->node->parent->name[0] == ' ') ||
5580 (xmlStrEqual(ctxt->context->node->parent->name,
5581 BAD_CAST "fake node libxslt"))))
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005582 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005583 return(ctxt->context->node->parent);
5584 case XML_ATTRIBUTE_NODE: {
5585 xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node;
5586
5587 return(att->parent);
5588 }
5589 case XML_DOCUMENT_NODE:
5590 case XML_DOCUMENT_TYPE_NODE:
5591 case XML_DOCUMENT_FRAG_NODE:
5592 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005593#ifdef LIBXML_DOCB_ENABLED
5594 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005595#endif
5596 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005597 case XML_NAMESPACE_DECL: {
5598 xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
5599
5600 if ((ns->next != NULL) &&
5601 (ns->next->type != XML_NAMESPACE_DECL))
5602 return((xmlNodePtr) ns->next);
Owen Taylor3473f882001-02-23 17:55:21 +00005603 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005604 }
Owen Taylor3473f882001-02-23 17:55:21 +00005605 }
5606 }
5607 return(NULL);
5608}
5609
5610/**
5611 * xmlXPathNextAncestor:
5612 * @ctxt: the XPath Parser context
5613 * @cur: the current node in the traversal
5614 *
5615 * Traversal function for the "ancestor" direction
5616 * the ancestor axis contains the ancestors of the context node; the ancestors
5617 * of the context node consist of the parent of context node and the parent's
5618 * parent and so on; the nodes are ordered in reverse document order; thus the
5619 * parent is the first node on the axis, and the parent's parent is the second
5620 * node on the axis
5621 *
5622 * Returns the next element following that axis
5623 */
5624xmlNodePtr
5625xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00005626 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005627 /*
5628 * the parent of an attribute or namespace node is the element
5629 * to which the attribute or namespace node is attached
5630 * !!!!!!!!!!!!!
5631 */
5632 if (cur == NULL) {
5633 if (ctxt->context->node == NULL) return(NULL);
5634 switch (ctxt->context->node->type) {
5635 case XML_ELEMENT_NODE:
5636 case XML_TEXT_NODE:
5637 case XML_CDATA_SECTION_NODE:
5638 case XML_ENTITY_REF_NODE:
5639 case XML_ENTITY_NODE:
5640 case XML_PI_NODE:
5641 case XML_COMMENT_NODE:
5642 case XML_DTD_NODE:
5643 case XML_ELEMENT_DECL:
5644 case XML_ATTRIBUTE_DECL:
5645 case XML_ENTITY_DECL:
5646 case XML_NOTATION_NODE:
5647 case XML_XINCLUDE_START:
5648 case XML_XINCLUDE_END:
5649 if (ctxt->context->node->parent == NULL)
5650 return((xmlNodePtr) ctxt->context->doc);
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005651 if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) &&
Daniel Veillard652d8a92003-02-04 19:28:49 +00005652 ((ctxt->context->node->parent->name[0] == ' ') ||
5653 (xmlStrEqual(ctxt->context->node->parent->name,
5654 BAD_CAST "fake node libxslt"))))
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005655 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005656 return(ctxt->context->node->parent);
5657 case XML_ATTRIBUTE_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005658 xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node;
Owen Taylor3473f882001-02-23 17:55:21 +00005659
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005660 return(tmp->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005661 }
5662 case XML_DOCUMENT_NODE:
5663 case XML_DOCUMENT_TYPE_NODE:
5664 case XML_DOCUMENT_FRAG_NODE:
5665 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005666#ifdef LIBXML_DOCB_ENABLED
5667 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005668#endif
5669 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005670 case XML_NAMESPACE_DECL: {
5671 xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
5672
5673 if ((ns->next != NULL) &&
5674 (ns->next->type != XML_NAMESPACE_DECL))
5675 return((xmlNodePtr) ns->next);
William M. Brack08171912003-12-29 02:52:11 +00005676 /* Bad, how did that namespace end up here ? */
Owen Taylor3473f882001-02-23 17:55:21 +00005677 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005678 }
Owen Taylor3473f882001-02-23 17:55:21 +00005679 }
5680 return(NULL);
5681 }
5682 if (cur == ctxt->context->doc->children)
5683 return((xmlNodePtr) ctxt->context->doc);
5684 if (cur == (xmlNodePtr) ctxt->context->doc)
5685 return(NULL);
5686 switch (cur->type) {
5687 case XML_ELEMENT_NODE:
5688 case XML_TEXT_NODE:
5689 case XML_CDATA_SECTION_NODE:
5690 case XML_ENTITY_REF_NODE:
5691 case XML_ENTITY_NODE:
5692 case XML_PI_NODE:
5693 case XML_COMMENT_NODE:
5694 case XML_NOTATION_NODE:
5695 case XML_DTD_NODE:
5696 case XML_ELEMENT_DECL:
5697 case XML_ATTRIBUTE_DECL:
5698 case XML_ENTITY_DECL:
5699 case XML_XINCLUDE_START:
5700 case XML_XINCLUDE_END:
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005701 if (cur->parent == NULL)
5702 return(NULL);
5703 if ((cur->parent->type == XML_ELEMENT_NODE) &&
Daniel Veillard652d8a92003-02-04 19:28:49 +00005704 ((cur->parent->name[0] == ' ') ||
5705 (xmlStrEqual(cur->parent->name,
5706 BAD_CAST "fake node libxslt"))))
Daniel Veillard8e7e1c02003-01-10 17:06:09 +00005707 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005708 return(cur->parent);
5709 case XML_ATTRIBUTE_NODE: {
5710 xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node;
5711
5712 return(att->parent);
5713 }
Aleksey Sanindffd5c82002-05-31 04:24:13 +00005714 case XML_NAMESPACE_DECL: {
5715 xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
5716
5717 if ((ns->next != NULL) &&
5718 (ns->next->type != XML_NAMESPACE_DECL))
5719 return((xmlNodePtr) ns->next);
William M. Brack08171912003-12-29 02:52:11 +00005720 /* Bad, how did that namespace end up here ? */
Aleksey Sanindffd5c82002-05-31 04:24:13 +00005721 return(NULL);
5722 }
Owen Taylor3473f882001-02-23 17:55:21 +00005723 case XML_DOCUMENT_NODE:
5724 case XML_DOCUMENT_TYPE_NODE:
5725 case XML_DOCUMENT_FRAG_NODE:
5726 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005727#ifdef LIBXML_DOCB_ENABLED
5728 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005729#endif
5730 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005731 }
5732 return(NULL);
5733}
5734
5735/**
5736 * xmlXPathNextAncestorOrSelf:
5737 * @ctxt: the XPath Parser context
5738 * @cur: the current node in the traversal
5739 *
5740 * Traversal function for the "ancestor-or-self" direction
5741 * he ancestor-or-self axis contains the context node and ancestors of
5742 * the context node in reverse document order; thus the context node is
5743 * the first node on the axis, and the context node's parent the second;
5744 * parent here is defined the same as with the parent axis.
5745 *
5746 * Returns the next element following that axis
5747 */
5748xmlNodePtr
5749xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00005750 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005751 if (cur == NULL)
5752 return(ctxt->context->node);
5753 return(xmlXPathNextAncestor(ctxt, cur));
5754}
5755
5756/**
5757 * xmlXPathNextFollowingSibling:
5758 * @ctxt: the XPath Parser context
5759 * @cur: the current node in the traversal
5760 *
5761 * Traversal function for the "following-sibling" direction
5762 * The following-sibling axis contains the following siblings of the context
5763 * node in document order.
5764 *
5765 * Returns the next element following that axis
5766 */
5767xmlNodePtr
5768xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00005769 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005770 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5771 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5772 return(NULL);
5773 if (cur == (xmlNodePtr) ctxt->context->doc)
5774 return(NULL);
5775 if (cur == NULL)
5776 return(ctxt->context->node->next);
5777 return(cur->next);
5778}
5779
5780/**
5781 * xmlXPathNextPrecedingSibling:
5782 * @ctxt: the XPath Parser context
5783 * @cur: the current node in the traversal
5784 *
5785 * Traversal function for the "preceding-sibling" direction
5786 * The preceding-sibling axis contains the preceding siblings of the context
5787 * node in reverse document order; the first preceding sibling is first on the
5788 * axis; the sibling preceding that node is the second on the axis and so on.
5789 *
5790 * Returns the next element following that axis
5791 */
5792xmlNodePtr
5793xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00005794 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005795 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5796 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5797 return(NULL);
5798 if (cur == (xmlNodePtr) ctxt->context->doc)
5799 return(NULL);
5800 if (cur == NULL)
5801 return(ctxt->context->node->prev);
Daniel Veillardf06307e2001-07-03 10:35:50 +00005802 if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) {
5803 cur = cur->prev;
5804 if (cur == NULL)
5805 return(ctxt->context->node->prev);
5806 }
Owen Taylor3473f882001-02-23 17:55:21 +00005807 return(cur->prev);
5808}
5809
5810/**
5811 * xmlXPathNextFollowing:
5812 * @ctxt: the XPath Parser context
5813 * @cur: the current node in the traversal
5814 *
5815 * Traversal function for the "following" direction
5816 * The following axis contains all nodes in the same document as the context
5817 * node that are after the context node in document order, excluding any
5818 * descendants and excluding attribute nodes and namespace nodes; the nodes
5819 * are ordered in document order
5820 *
5821 * Returns the next element following that axis
5822 */
5823xmlNodePtr
5824xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00005825 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005826 if (cur != NULL && cur->children != NULL)
5827 return cur->children ;
5828 if (cur == NULL) cur = ctxt->context->node;
5829 if (cur == NULL) return(NULL) ; /* ERROR */
5830 if (cur->next != NULL) return(cur->next) ;
5831 do {
5832 cur = cur->parent;
5833 if (cur == NULL) return(NULL);
5834 if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL);
5835 if (cur->next != NULL) return(cur->next);
5836 } while (cur != NULL);
5837 return(cur);
5838}
5839
5840/*
5841 * xmlXPathIsAncestor:
5842 * @ancestor: the ancestor node
5843 * @node: the current node
5844 *
5845 * Check that @ancestor is a @node's ancestor
5846 *
5847 * returns 1 if @ancestor is a @node's ancestor, 0 otherwise.
5848 */
5849static int
5850xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) {
5851 if ((ancestor == NULL) || (node == NULL)) return(0);
5852 /* nodes need to be in the same document */
5853 if (ancestor->doc != node->doc) return(0);
5854 /* avoid searching if ancestor or node is the root node */
5855 if (ancestor == (xmlNodePtr) node->doc) return(1);
5856 if (node == (xmlNodePtr) ancestor->doc) return(0);
5857 while (node->parent != NULL) {
5858 if (node->parent == ancestor)
5859 return(1);
5860 node = node->parent;
5861 }
5862 return(0);
5863}
5864
5865/**
5866 * xmlXPathNextPreceding:
5867 * @ctxt: the XPath Parser context
5868 * @cur: the current node in the traversal
5869 *
5870 * Traversal function for the "preceding" direction
5871 * the preceding axis contains all nodes in the same document as the context
5872 * node that are before the context node in document order, excluding any
5873 * ancestors and excluding attribute nodes and namespace nodes; the nodes are
5874 * ordered in reverse document order
5875 *
5876 * Returns the next element following that axis
5877 */
5878xmlNodePtr
Daniel Veillardf06307e2001-07-03 10:35:50 +00005879xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur)
5880{
Daniel Veillarda82b1822004-11-08 16:24:57 +00005881 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005882 if (cur == NULL)
Daniel Veillardf06307e2001-07-03 10:35:50 +00005883 cur = ctxt->context->node;
5884 if (cur == NULL)
5885 return (NULL);
5886 if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
5887 cur = cur->prev;
Owen Taylor3473f882001-02-23 17:55:21 +00005888 do {
5889 if (cur->prev != NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00005890 for (cur = cur->prev; cur->last != NULL; cur = cur->last) ;
5891 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00005892 }
5893
5894 cur = cur->parent;
Daniel Veillardf06307e2001-07-03 10:35:50 +00005895 if (cur == NULL)
5896 return (NULL);
5897 if (cur == ctxt->context->doc->children)
5898 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005899 } while (xmlXPathIsAncestor(cur, ctxt->context->node));
Daniel Veillardf06307e2001-07-03 10:35:50 +00005900 return (cur);
5901}
5902
5903/**
5904 * xmlXPathNextPrecedingInternal:
5905 * @ctxt: the XPath Parser context
5906 * @cur: the current node in the traversal
5907 *
5908 * Traversal function for the "preceding" direction
5909 * the preceding axis contains all nodes in the same document as the context
5910 * node that are before the context node in document order, excluding any
5911 * ancestors and excluding attribute nodes and namespace nodes; the nodes are
5912 * ordered in reverse document order
5913 * This is a faster implementation but internal only since it requires a
5914 * state kept in the parser context: ctxt->ancestor.
5915 *
5916 * Returns the next element following that axis
5917 */
5918static xmlNodePtr
5919xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt,
5920 xmlNodePtr cur)
5921{
Daniel Veillarda82b1822004-11-08 16:24:57 +00005922 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Daniel Veillardf06307e2001-07-03 10:35:50 +00005923 if (cur == NULL) {
5924 cur = ctxt->context->node;
5925 if (cur == NULL)
5926 return (NULL);
William M. Brack40c22b42003-10-10 03:58:39 +00005927 if (cur->type == XML_NAMESPACE_DECL)
5928 cur = (xmlNodePtr)((xmlNsPtr)cur)->next;
Daniel Veillardf06307e2001-07-03 10:35:50 +00005929 ctxt->ancestor = cur->parent;
5930 }
5931 if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
5932 cur = cur->prev;
5933 while (cur->prev == NULL) {
5934 cur = cur->parent;
5935 if (cur == NULL)
5936 return (NULL);
5937 if (cur == ctxt->context->doc->children)
5938 return (NULL);
5939 if (cur != ctxt->ancestor)
5940 return (cur);
5941 ctxt->ancestor = cur->parent;
5942 }
5943 cur = cur->prev;
5944 while (cur->last != NULL)
5945 cur = cur->last;
5946 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00005947}
5948
5949/**
5950 * xmlXPathNextNamespace:
5951 * @ctxt: the XPath Parser context
5952 * @cur: the current attribute in the traversal
5953 *
5954 * Traversal function for the "namespace" direction
5955 * the namespace axis contains the namespace nodes of the context node;
5956 * the order of nodes on this axis is implementation-defined; the axis will
5957 * be empty unless the context node is an element
5958 *
Daniel Veillard20ee8c02001-10-05 09:18:14 +00005959 * We keep the XML namespace node at the end of the list.
5960 *
Owen Taylor3473f882001-02-23 17:55:21 +00005961 * Returns the next element following that axis
5962 */
5963xmlNodePtr
5964xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00005965 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005966 if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL);
Daniel Veillardfdc91562002-07-01 21:52:03 +00005967 if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) {
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005968 if (ctxt->context->tmpNsList != NULL)
5969 xmlFree(ctxt->context->tmpNsList);
5970 ctxt->context->tmpNsList =
Owen Taylor3473f882001-02-23 17:55:21 +00005971 xmlGetNsList(ctxt->context->doc, ctxt->context->node);
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005972 ctxt->context->tmpNsNr = 0;
Daniel Veillardfdc91562002-07-01 21:52:03 +00005973 if (ctxt->context->tmpNsList != NULL) {
5974 while (ctxt->context->tmpNsList[ctxt->context->tmpNsNr] != NULL) {
5975 ctxt->context->tmpNsNr++;
5976 }
5977 }
Daniel Veillard20ee8c02001-10-05 09:18:14 +00005978 return((xmlNodePtr) xmlXPathXMLNamespace);
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005979 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00005980 if (ctxt->context->tmpNsNr > 0) {
5981 return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr];
5982 } else {
5983 if (ctxt->context->tmpNsList != NULL)
5984 xmlFree(ctxt->context->tmpNsList);
5985 ctxt->context->tmpNsList = NULL;
5986 return(NULL);
5987 }
Owen Taylor3473f882001-02-23 17:55:21 +00005988}
5989
5990/**
5991 * xmlXPathNextAttribute:
5992 * @ctxt: the XPath Parser context
5993 * @cur: the current attribute in the traversal
5994 *
5995 * Traversal function for the "attribute" direction
5996 * TODO: support DTD inherited default attributes
5997 *
5998 * Returns the next element following that axis
5999 */
6000xmlNodePtr
6001xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00006002 if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);
Daniel Veillarde470df72001-04-18 21:41:07 +00006003 if (ctxt->context->node == NULL)
6004 return(NULL);
6005 if (ctxt->context->node->type != XML_ELEMENT_NODE)
6006 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006007 if (cur == NULL) {
6008 if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc)
6009 return(NULL);
6010 return((xmlNodePtr)ctxt->context->node->properties);
6011 }
6012 return((xmlNodePtr)cur->next);
6013}
6014
6015/************************************************************************
6016 * *
6017 * NodeTest Functions *
6018 * *
6019 ************************************************************************/
6020
Owen Taylor3473f882001-02-23 17:55:21 +00006021#define IS_FUNCTION 200
6022
Owen Taylor3473f882001-02-23 17:55:21 +00006023
6024/************************************************************************
6025 * *
6026 * Implicit tree core function library *
6027 * *
6028 ************************************************************************/
6029
6030/**
6031 * xmlXPathRoot:
6032 * @ctxt: the XPath Parser context
6033 *
6034 * Initialize the context to the root of the document
6035 */
6036void
6037xmlXPathRoot(xmlXPathParserContextPtr ctxt) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00006038 if ((ctxt == NULL) || (ctxt->context == NULL)) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006039 ctxt->context->node = (xmlNodePtr) ctxt->context->doc;
6040 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
6041}
6042
6043/************************************************************************
6044 * *
6045 * The explicit core function library *
6046 *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib *
6047 * *
6048 ************************************************************************/
6049
6050
6051/**
6052 * xmlXPathLastFunction:
6053 * @ctxt: the XPath Parser context
6054 * @nargs: the number of arguments
6055 *
6056 * Implement the last() XPath function
6057 * number last()
6058 * The last function returns the number of nodes in the context node list.
6059 */
6060void
6061xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6062 CHECK_ARITY(0);
6063 if (ctxt->context->contextSize >= 0) {
6064 valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize));
6065#ifdef DEBUG_EXPR
6066 xmlGenericError(xmlGenericErrorContext,
6067 "last() : %d\n", ctxt->context->contextSize);
6068#endif
6069 } else {
6070 XP_ERROR(XPATH_INVALID_CTXT_SIZE);
6071 }
6072}
6073
6074/**
6075 * xmlXPathPositionFunction:
6076 * @ctxt: the XPath Parser context
6077 * @nargs: the number of arguments
6078 *
6079 * Implement the position() XPath function
6080 * number position()
6081 * The position function returns the position of the context node in the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006082 * context node list. The first position is 1, and so the last position
Owen Taylor3473f882001-02-23 17:55:21 +00006083 * will be equal to last().
6084 */
6085void
6086xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6087 CHECK_ARITY(0);
6088 if (ctxt->context->proximityPosition >= 0) {
6089 valuePush(ctxt,
6090 xmlXPathNewFloat((double) ctxt->context->proximityPosition));
6091#ifdef DEBUG_EXPR
6092 xmlGenericError(xmlGenericErrorContext, "position() : %d\n",
6093 ctxt->context->proximityPosition);
6094#endif
6095 } else {
6096 XP_ERROR(XPATH_INVALID_CTXT_POSITION);
6097 }
6098}
6099
6100/**
6101 * xmlXPathCountFunction:
6102 * @ctxt: the XPath Parser context
6103 * @nargs: the number of arguments
6104 *
6105 * Implement the count() XPath function
6106 * number count(node-set)
6107 */
6108void
6109xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6110 xmlXPathObjectPtr cur;
6111
6112 CHECK_ARITY(1);
6113 if ((ctxt->value == NULL) ||
6114 ((ctxt->value->type != XPATH_NODESET) &&
6115 (ctxt->value->type != XPATH_XSLT_TREE)))
6116 XP_ERROR(XPATH_INVALID_TYPE);
6117 cur = valuePop(ctxt);
6118
Daniel Veillard911f49a2001-04-07 15:39:35 +00006119 if ((cur == NULL) || (cur->nodesetval == NULL))
6120 valuePush(ctxt, xmlXPathNewFloat((double) 0));
William M. Brack0c022ad2002-07-12 00:56:01 +00006121 else if ((cur->type == XPATH_NODESET) || (cur->type == XPATH_XSLT_TREE)) {
Daniel Veillard911f49a2001-04-07 15:39:35 +00006122 valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr));
Daniel Veillardfe703322001-08-14 12:18:09 +00006123 } else {
6124 if ((cur->nodesetval->nodeNr != 1) ||
6125 (cur->nodesetval->nodeTab == NULL)) {
6126 valuePush(ctxt, xmlXPathNewFloat((double) 0));
6127 } else {
6128 xmlNodePtr tmp;
6129 int i = 0;
6130
6131 tmp = cur->nodesetval->nodeTab[0];
6132 if (tmp != NULL) {
6133 tmp = tmp->children;
6134 while (tmp != NULL) {
6135 tmp = tmp->next;
6136 i++;
6137 }
6138 }
6139 valuePush(ctxt, xmlXPathNewFloat((double) i));
6140 }
6141 }
Owen Taylor3473f882001-02-23 17:55:21 +00006142 xmlXPathFreeObject(cur);
6143}
6144
6145/**
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006146 * xmlXPathGetElementsByIds:
6147 * @doc: the document
6148 * @ids: a whitespace separated list of IDs
6149 *
6150 * Selects elements by their unique ID.
6151 *
6152 * Returns a node-set of selected elements.
6153 */
6154static xmlNodeSetPtr
6155xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) {
6156 xmlNodeSetPtr ret;
6157 const xmlChar *cur = ids;
6158 xmlChar *ID;
6159 xmlAttrPtr attr;
6160 xmlNodePtr elem = NULL;
6161
Daniel Veillard7a985a12003-07-06 17:57:42 +00006162 if (ids == NULL) return(NULL);
6163
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006164 ret = xmlXPathNodeSetCreate(NULL);
6165
William M. Brack76e95df2003-10-18 16:20:14 +00006166 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006167 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00006168 while ((!IS_BLANK_CH(*cur)) && (*cur != 0))
Daniel Veillarde209b332003-03-26 21:40:13 +00006169 cur++;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006170
6171 ID = xmlStrndup(ids, cur - ids);
Daniel Veillarde209b332003-03-26 21:40:13 +00006172 if (ID != NULL) {
Daniel Veillard68cb4b22004-04-18 20:55:39 +00006173 /*
6174 * We used to check the fact that the value passed
6175 * was an NCName, but this generated much troubles for
6176 * me and Aleksey Sanin, people blatantly violated that
6177 * constaint, like Visa3D spec.
6178 * if (xmlValidateNCName(ID, 1) == 0)
6179 */
6180 attr = xmlGetID(doc, ID);
6181 if (attr != NULL) {
6182 if (attr->type == XML_ATTRIBUTE_NODE)
6183 elem = attr->parent;
6184 else if (attr->type == XML_ELEMENT_NODE)
6185 elem = (xmlNodePtr) attr;
6186 else
6187 elem = NULL;
6188 if (elem != NULL)
6189 xmlXPathNodeSetAdd(ret, elem);
Daniel Veillarde209b332003-03-26 21:40:13 +00006190 }
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006191 xmlFree(ID);
Daniel Veillarde209b332003-03-26 21:40:13 +00006192 }
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006193
William M. Brack76e95df2003-10-18 16:20:14 +00006194 while (IS_BLANK_CH(*cur)) cur++;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006195 ids = cur;
6196 }
6197 return(ret);
6198}
6199
6200/**
Owen Taylor3473f882001-02-23 17:55:21 +00006201 * xmlXPathIdFunction:
6202 * @ctxt: the XPath Parser context
6203 * @nargs: the number of arguments
6204 *
6205 * Implement the id() XPath function
6206 * node-set id(object)
6207 * The id function selects elements by their unique ID
6208 * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set,
6209 * then the result is the union of the result of applying id to the
6210 * string value of each of the nodes in the argument node-set. When the
6211 * argument to id is of any other type, the argument is converted to a
6212 * string as if by a call to the string function; the string is split
6213 * into a whitespace-separated list of tokens (whitespace is any sequence
6214 * of characters matching the production S); the result is a node-set
6215 * containing the elements in the same document as the context node that
6216 * have a unique ID equal to any of the tokens in the list.
6217 */
6218void
6219xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006220 xmlChar *tokens;
6221 xmlNodeSetPtr ret;
6222 xmlXPathObjectPtr obj;
Owen Taylor3473f882001-02-23 17:55:21 +00006223
6224 CHECK_ARITY(1);
6225 obj = valuePop(ctxt);
6226 if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
William M. Brack0c022ad2002-07-12 00:56:01 +00006227 if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006228 xmlNodeSetPtr ns;
Owen Taylor3473f882001-02-23 17:55:21 +00006229 int i;
6230
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006231 ret = xmlXPathNodeSetCreate(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006232
Daniel Veillard911f49a2001-04-07 15:39:35 +00006233 if (obj->nodesetval != NULL) {
6234 for (i = 0; i < obj->nodesetval->nodeNr; i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006235 tokens =
6236 xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]);
6237 ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens);
6238 ret = xmlXPathNodeSetMerge(ret, ns);
6239 xmlXPathFreeNodeSet(ns);
6240 if (tokens != NULL)
6241 xmlFree(tokens);
Daniel Veillard911f49a2001-04-07 15:39:35 +00006242 }
Owen Taylor3473f882001-02-23 17:55:21 +00006243 }
6244
6245 xmlXPathFreeObject(obj);
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006246 valuePush(ctxt, xmlXPathWrapNodeSet(ret));
Owen Taylor3473f882001-02-23 17:55:21 +00006247 return;
6248 }
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006249 obj = xmlXPathConvertString(obj);
Owen Taylor3473f882001-02-23 17:55:21 +00006250
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006251 ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval);
6252 valuePush(ctxt, xmlXPathWrapNodeSet(ret));
Owen Taylor3473f882001-02-23 17:55:21 +00006253
Owen Taylor3473f882001-02-23 17:55:21 +00006254 xmlXPathFreeObject(obj);
6255 return;
6256}
6257
6258/**
6259 * xmlXPathLocalNameFunction:
6260 * @ctxt: the XPath Parser context
6261 * @nargs: the number of arguments
6262 *
6263 * Implement the local-name() XPath function
6264 * string local-name(node-set?)
6265 * The local-name function returns a string containing the local part
6266 * of the name of the node in the argument node-set that is first in
6267 * document order. If the node-set is empty or the first node has no
6268 * name, an empty string is returned. If the argument is omitted it
6269 * defaults to the context node.
6270 */
6271void
6272xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6273 xmlXPathObjectPtr cur;
6274
Daniel Veillarda82b1822004-11-08 16:24:57 +00006275 if (ctxt == NULL) return;
6276
Owen Taylor3473f882001-02-23 17:55:21 +00006277 if (nargs == 0) {
6278 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
6279 nargs = 1;
6280 }
6281
6282 CHECK_ARITY(1);
6283 if ((ctxt->value == NULL) ||
6284 ((ctxt->value->type != XPATH_NODESET) &&
6285 (ctxt->value->type != XPATH_XSLT_TREE)))
6286 XP_ERROR(XPATH_INVALID_TYPE);
6287 cur = valuePop(ctxt);
6288
Daniel Veillard911f49a2001-04-07 15:39:35 +00006289 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006290 valuePush(ctxt, xmlXPathNewCString(""));
6291 } else {
6292 int i = 0; /* Should be first in document order !!!!! */
6293 switch (cur->nodesetval->nodeTab[i]->type) {
6294 case XML_ELEMENT_NODE:
6295 case XML_ATTRIBUTE_NODE:
6296 case XML_PI_NODE:
Daniel Veillard652d8a92003-02-04 19:28:49 +00006297 if (cur->nodesetval->nodeTab[i]->name[0] == ' ')
6298 valuePush(ctxt, xmlXPathNewCString(""));
6299 else
6300 valuePush(ctxt,
Owen Taylor3473f882001-02-23 17:55:21 +00006301 xmlXPathNewString(cur->nodesetval->nodeTab[i]->name));
6302 break;
6303 case XML_NAMESPACE_DECL:
6304 valuePush(ctxt, xmlXPathNewString(
6305 ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix));
6306 break;
6307 default:
6308 valuePush(ctxt, xmlXPathNewCString(""));
6309 }
6310 }
6311 xmlXPathFreeObject(cur);
6312}
6313
6314/**
6315 * xmlXPathNamespaceURIFunction:
6316 * @ctxt: the XPath Parser context
6317 * @nargs: the number of arguments
6318 *
6319 * Implement the namespace-uri() XPath function
6320 * string namespace-uri(node-set?)
6321 * The namespace-uri function returns a string containing the
6322 * namespace URI of the expanded name of the node in the argument
6323 * node-set that is first in document order. If the node-set is empty,
6324 * the first node has no name, or the expanded name has no namespace
6325 * URI, an empty string is returned. If the argument is omitted it
6326 * defaults to the context node.
6327 */
6328void
6329xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6330 xmlXPathObjectPtr cur;
6331
Daniel Veillarda82b1822004-11-08 16:24:57 +00006332 if (ctxt == NULL) return;
6333
Owen Taylor3473f882001-02-23 17:55:21 +00006334 if (nargs == 0) {
6335 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
6336 nargs = 1;
6337 }
6338 CHECK_ARITY(1);
6339 if ((ctxt->value == NULL) ||
6340 ((ctxt->value->type != XPATH_NODESET) &&
6341 (ctxt->value->type != XPATH_XSLT_TREE)))
6342 XP_ERROR(XPATH_INVALID_TYPE);
6343 cur = valuePop(ctxt);
6344
Daniel Veillard911f49a2001-04-07 15:39:35 +00006345 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006346 valuePush(ctxt, xmlXPathNewCString(""));
6347 } else {
6348 int i = 0; /* Should be first in document order !!!!! */
6349 switch (cur->nodesetval->nodeTab[i]->type) {
6350 case XML_ELEMENT_NODE:
6351 case XML_ATTRIBUTE_NODE:
6352 if (cur->nodesetval->nodeTab[i]->ns == NULL)
6353 valuePush(ctxt, xmlXPathNewCString(""));
6354 else
6355 valuePush(ctxt, xmlXPathNewString(
6356 cur->nodesetval->nodeTab[i]->ns->href));
6357 break;
6358 default:
6359 valuePush(ctxt, xmlXPathNewCString(""));
6360 }
6361 }
6362 xmlXPathFreeObject(cur);
6363}
6364
6365/**
6366 * xmlXPathNameFunction:
6367 * @ctxt: the XPath Parser context
6368 * @nargs: the number of arguments
6369 *
6370 * Implement the name() XPath function
6371 * string name(node-set?)
6372 * The name function returns a string containing a QName representing
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006373 * the name of the node in the argument node-set that is first in document
Owen Taylor3473f882001-02-23 17:55:21 +00006374 * order. The QName must represent the name with respect to the namespace
6375 * declarations in effect on the node whose name is being represented.
6376 * Typically, this will be the form in which the name occurred in the XML
6377 * source. This need not be the case if there are namespace declarations
6378 * in effect on the node that associate multiple prefixes with the same
6379 * namespace. However, an implementation may include information about
6380 * the original prefix in its representation of nodes; in this case, an
6381 * implementation can ensure that the returned string is always the same
6382 * as the QName used in the XML source. If the argument it omitted it
6383 * defaults to the context node.
6384 * Libxml keep the original prefix so the "real qualified name" used is
6385 * returned.
6386 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006387static void
Daniel Veillard04383752001-07-08 14:27:15 +00006388xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs)
6389{
Owen Taylor3473f882001-02-23 17:55:21 +00006390 xmlXPathObjectPtr cur;
6391
6392 if (nargs == 0) {
Daniel Veillard04383752001-07-08 14:27:15 +00006393 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
6394 nargs = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00006395 }
6396
6397 CHECK_ARITY(1);
Daniel Veillard04383752001-07-08 14:27:15 +00006398 if ((ctxt->value == NULL) ||
6399 ((ctxt->value->type != XPATH_NODESET) &&
6400 (ctxt->value->type != XPATH_XSLT_TREE)))
6401 XP_ERROR(XPATH_INVALID_TYPE);
Owen Taylor3473f882001-02-23 17:55:21 +00006402 cur = valuePop(ctxt);
6403
Daniel Veillard911f49a2001-04-07 15:39:35 +00006404 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Daniel Veillard04383752001-07-08 14:27:15 +00006405 valuePush(ctxt, xmlXPathNewCString(""));
Owen Taylor3473f882001-02-23 17:55:21 +00006406 } else {
Daniel Veillard04383752001-07-08 14:27:15 +00006407 int i = 0; /* Should be first in document order !!!!! */
Owen Taylor3473f882001-02-23 17:55:21 +00006408
Daniel Veillard04383752001-07-08 14:27:15 +00006409 switch (cur->nodesetval->nodeTab[i]->type) {
6410 case XML_ELEMENT_NODE:
6411 case XML_ATTRIBUTE_NODE:
Daniel Veillard652d8a92003-02-04 19:28:49 +00006412 if (cur->nodesetval->nodeTab[i]->name[0] == ' ')
6413 valuePush(ctxt, xmlXPathNewCString(""));
6414 else if ((cur->nodesetval->nodeTab[i]->ns == NULL) ||
6415 (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) {
Daniel Veillard04383752001-07-08 14:27:15 +00006416 valuePush(ctxt,
Daniel Veillardc00cda82003-04-07 10:22:39 +00006417 xmlXPathNewString(cur->nodesetval->nodeTab[i]->name));
Daniel Veillard04383752001-07-08 14:27:15 +00006418
Daniel Veillard652d8a92003-02-04 19:28:49 +00006419 } else {
Daniel Veillardc00cda82003-04-07 10:22:39 +00006420 xmlChar *fullname;
6421
6422 fullname = xmlBuildQName(cur->nodesetval->nodeTab[i]->name,
6423 cur->nodesetval->nodeTab[i]->ns->prefix,
6424 NULL, 0);
6425 if (fullname == cur->nodesetval->nodeTab[i]->name)
6426 fullname = xmlStrdup(cur->nodesetval->nodeTab[i]->name);
6427 if (fullname == NULL) {
6428 XP_ERROR(XPATH_MEMORY_ERROR);
6429 }
6430 valuePush(ctxt, xmlXPathWrapString(fullname));
Daniel Veillard04383752001-07-08 14:27:15 +00006431 }
6432 break;
6433 default:
6434 valuePush(ctxt,
6435 xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i]));
6436 xmlXPathLocalNameFunction(ctxt, 1);
6437 }
Owen Taylor3473f882001-02-23 17:55:21 +00006438 }
6439 xmlXPathFreeObject(cur);
6440}
6441
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00006442
6443/**
Owen Taylor3473f882001-02-23 17:55:21 +00006444 * xmlXPathStringFunction:
6445 * @ctxt: the XPath Parser context
6446 * @nargs: the number of arguments
6447 *
6448 * Implement the string() XPath function
6449 * string string(object?)
William M. Brack08171912003-12-29 02:52:11 +00006450 * The string function converts an object to a string as follows:
Owen Taylor3473f882001-02-23 17:55:21 +00006451 * - A node-set is converted to a string by returning the value of
6452 * the node in the node-set that is first in document order.
6453 * If the node-set is empty, an empty string is returned.
6454 * - A number is converted to a string as follows
6455 * + NaN is converted to the string NaN
6456 * + positive zero is converted to the string 0
6457 * + negative zero is converted to the string 0
6458 * + positive infinity is converted to the string Infinity
6459 * + negative infinity is converted to the string -Infinity
6460 * + if the number is an integer, the number is represented in
6461 * decimal form as a Number with no decimal point and no leading
6462 * zeros, preceded by a minus sign (-) if the number is negative
6463 * + otherwise, the number is represented in decimal form as a
6464 * Number including a decimal point with at least one digit
6465 * before the decimal point and at least one digit after the
6466 * decimal point, preceded by a minus sign (-) if the number
6467 * is negative; there must be no leading zeros before the decimal
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006468 * point apart possibly from the one required digit immediately
Owen Taylor3473f882001-02-23 17:55:21 +00006469 * before the decimal point; beyond the one required digit
6470 * after the decimal point there must be as many, but only as
6471 * many, more digits as are needed to uniquely distinguish the
6472 * number from all other IEEE 754 numeric values.
6473 * - The boolean false value is converted to the string false.
6474 * The boolean true value is converted to the string true.
6475 *
6476 * If the argument is omitted, it defaults to a node-set with the
6477 * context node as its only member.
6478 */
6479void
6480xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6481 xmlXPathObjectPtr cur;
6482
Daniel Veillarda82b1822004-11-08 16:24:57 +00006483 if (ctxt == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006484 if (nargs == 0) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006485 valuePush(ctxt,
6486 xmlXPathWrapString(
6487 xmlXPathCastNodeToString(ctxt->context->node)));
6488 return;
Owen Taylor3473f882001-02-23 17:55:21 +00006489 }
6490
6491 CHECK_ARITY(1);
6492 cur = valuePop(ctxt);
6493 if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00006494 cur = xmlXPathConvertString(cur);
6495 valuePush(ctxt, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00006496}
6497
6498/**
6499 * xmlXPathStringLengthFunction:
6500 * @ctxt: the XPath Parser context
6501 * @nargs: the number of arguments
6502 *
6503 * Implement the string-length() XPath function
6504 * number string-length(string?)
6505 * The string-length returns the number of characters in the string
6506 * (see [3.6 Strings]). If the argument is omitted, it defaults to
6507 * the context node converted to a string, in other words the value
6508 * of the context node.
6509 */
6510void
6511xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6512 xmlXPathObjectPtr cur;
6513
6514 if (nargs == 0) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00006515 if ((ctxt == NULL) || (ctxt->context == NULL))
6516 return;
Owen Taylor3473f882001-02-23 17:55:21 +00006517 if (ctxt->context->node == NULL) {
6518 valuePush(ctxt, xmlXPathNewFloat(0));
6519 } else {
6520 xmlChar *content;
6521
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006522 content = xmlXPathCastNodeToString(ctxt->context->node);
Daniel Veillarde043ee12001-04-16 14:08:07 +00006523 valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content)));
Owen Taylor3473f882001-02-23 17:55:21 +00006524 xmlFree(content);
6525 }
6526 return;
6527 }
6528 CHECK_ARITY(1);
6529 CAST_TO_STRING;
6530 CHECK_TYPE(XPATH_STRING);
6531 cur = valuePop(ctxt);
Daniel Veillarde043ee12001-04-16 14:08:07 +00006532 valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(cur->stringval)));
Owen Taylor3473f882001-02-23 17:55:21 +00006533 xmlXPathFreeObject(cur);
6534}
6535
6536/**
6537 * xmlXPathConcatFunction:
6538 * @ctxt: the XPath Parser context
6539 * @nargs: the number of arguments
6540 *
6541 * Implement the concat() XPath function
6542 * string concat(string, string, string*)
6543 * The concat function returns the concatenation of its arguments.
6544 */
6545void
6546xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6547 xmlXPathObjectPtr cur, newobj;
6548 xmlChar *tmp;
6549
Daniel Veillarda82b1822004-11-08 16:24:57 +00006550 if (ctxt == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006551 if (nargs < 2) {
6552 CHECK_ARITY(2);
6553 }
6554
6555 CAST_TO_STRING;
6556 cur = valuePop(ctxt);
6557 if ((cur == NULL) || (cur->type != XPATH_STRING)) {
6558 xmlXPathFreeObject(cur);
6559 return;
6560 }
6561 nargs--;
6562
6563 while (nargs > 0) {
6564 CAST_TO_STRING;
6565 newobj = valuePop(ctxt);
6566 if ((newobj == NULL) || (newobj->type != XPATH_STRING)) {
6567 xmlXPathFreeObject(newobj);
6568 xmlXPathFreeObject(cur);
6569 XP_ERROR(XPATH_INVALID_TYPE);
6570 }
6571 tmp = xmlStrcat(newobj->stringval, cur->stringval);
6572 newobj->stringval = cur->stringval;
6573 cur->stringval = tmp;
6574
6575 xmlXPathFreeObject(newobj);
6576 nargs--;
6577 }
6578 valuePush(ctxt, cur);
6579}
6580
6581/**
6582 * xmlXPathContainsFunction:
6583 * @ctxt: the XPath Parser context
6584 * @nargs: the number of arguments
6585 *
6586 * Implement the contains() XPath function
6587 * boolean contains(string, string)
6588 * The contains function returns true if the first argument string
6589 * contains the second argument string, and otherwise returns false.
6590 */
6591void
6592xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6593 xmlXPathObjectPtr hay, needle;
6594
6595 CHECK_ARITY(2);
6596 CAST_TO_STRING;
6597 CHECK_TYPE(XPATH_STRING);
6598 needle = valuePop(ctxt);
6599 CAST_TO_STRING;
6600 hay = valuePop(ctxt);
6601 if ((hay == NULL) || (hay->type != XPATH_STRING)) {
6602 xmlXPathFreeObject(hay);
6603 xmlXPathFreeObject(needle);
6604 XP_ERROR(XPATH_INVALID_TYPE);
6605 }
6606 if (xmlStrstr(hay->stringval, needle->stringval))
6607 valuePush(ctxt, xmlXPathNewBoolean(1));
6608 else
6609 valuePush(ctxt, xmlXPathNewBoolean(0));
6610 xmlXPathFreeObject(hay);
6611 xmlXPathFreeObject(needle);
6612}
6613
6614/**
6615 * xmlXPathStartsWithFunction:
6616 * @ctxt: the XPath Parser context
6617 * @nargs: the number of arguments
6618 *
6619 * Implement the starts-with() XPath function
6620 * boolean starts-with(string, string)
6621 * The starts-with function returns true if the first argument string
6622 * starts with the second argument string, and otherwise returns false.
6623 */
6624void
6625xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6626 xmlXPathObjectPtr hay, needle;
6627 int n;
6628
6629 CHECK_ARITY(2);
6630 CAST_TO_STRING;
6631 CHECK_TYPE(XPATH_STRING);
6632 needle = valuePop(ctxt);
6633 CAST_TO_STRING;
6634 hay = valuePop(ctxt);
6635 if ((hay == NULL) || (hay->type != XPATH_STRING)) {
6636 xmlXPathFreeObject(hay);
6637 xmlXPathFreeObject(needle);
6638 XP_ERROR(XPATH_INVALID_TYPE);
6639 }
6640 n = xmlStrlen(needle->stringval);
6641 if (xmlStrncmp(hay->stringval, needle->stringval, n))
6642 valuePush(ctxt, xmlXPathNewBoolean(0));
6643 else
6644 valuePush(ctxt, xmlXPathNewBoolean(1));
6645 xmlXPathFreeObject(hay);
6646 xmlXPathFreeObject(needle);
6647}
6648
6649/**
6650 * xmlXPathSubstringFunction:
6651 * @ctxt: the XPath Parser context
6652 * @nargs: the number of arguments
6653 *
6654 * Implement the substring() XPath function
6655 * string substring(string, number, number?)
6656 * The substring function returns the substring of the first argument
6657 * starting at the position specified in the second argument with
6658 * length specified in the third argument. For example,
6659 * substring("12345",2,3) returns "234". If the third argument is not
6660 * specified, it returns the substring starting at the position specified
6661 * in the second argument and continuing to the end of the string. For
6662 * example, substring("12345",2) returns "2345". More precisely, each
6663 * character in the string (see [3.6 Strings]) is considered to have a
6664 * numeric position: the position of the first character is 1, the position
6665 * of the second character is 2 and so on. The returned substring contains
6666 * those characters for which the position of the character is greater than
6667 * or equal to the second argument and, if the third argument is specified,
6668 * less than the sum of the second and third arguments; the comparisons
6669 * and addition used for the above follow the standard IEEE 754 rules. Thus:
6670 * - substring("12345", 1.5, 2.6) returns "234"
6671 * - substring("12345", 0, 3) returns "12"
6672 * - substring("12345", 0 div 0, 3) returns ""
6673 * - substring("12345", 1, 0 div 0) returns ""
6674 * - substring("12345", -42, 1 div 0) returns "12345"
6675 * - substring("12345", -1 div 0, 1 div 0) returns ""
6676 */
6677void
6678xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6679 xmlXPathObjectPtr str, start, len;
Daniel Veillard97ac1312001-05-30 19:14:17 +00006680 double le=0, in;
6681 int i, l, m;
Owen Taylor3473f882001-02-23 17:55:21 +00006682 xmlChar *ret;
6683
Owen Taylor3473f882001-02-23 17:55:21 +00006684 if (nargs < 2) {
6685 CHECK_ARITY(2);
6686 }
6687 if (nargs > 3) {
6688 CHECK_ARITY(3);
6689 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006690 /*
6691 * take care of possible last (position) argument
6692 */
Owen Taylor3473f882001-02-23 17:55:21 +00006693 if (nargs == 3) {
6694 CAST_TO_NUMBER;
6695 CHECK_TYPE(XPATH_NUMBER);
6696 len = valuePop(ctxt);
6697 le = len->floatval;
6698 xmlXPathFreeObject(len);
Owen Taylor3473f882001-02-23 17:55:21 +00006699 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006700
Owen Taylor3473f882001-02-23 17:55:21 +00006701 CAST_TO_NUMBER;
6702 CHECK_TYPE(XPATH_NUMBER);
6703 start = valuePop(ctxt);
6704 in = start->floatval;
6705 xmlXPathFreeObject(start);
6706 CAST_TO_STRING;
6707 CHECK_TYPE(XPATH_STRING);
6708 str = valuePop(ctxt);
Daniel Veillard97ac1312001-05-30 19:14:17 +00006709 m = xmlUTF8Strlen((const unsigned char *)str->stringval);
Owen Taylor3473f882001-02-23 17:55:21 +00006710
Daniel Veillard97ac1312001-05-30 19:14:17 +00006711 /*
6712 * If last pos not present, calculate last position
6713 */
Daniel Veillard9e412302002-06-10 15:59:44 +00006714 if (nargs != 3) {
6715 le = (double)m;
6716 if (in < 1.0)
6717 in = 1.0;
6718 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006719
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006720 /* Need to check for the special cases where either
6721 * the index is NaN, the length is NaN, or both
6722 * arguments are infinity (relying on Inf + -Inf = NaN)
Daniel Veillard97ac1312001-05-30 19:14:17 +00006723 */
Daniel Veillard9e412302002-06-10 15:59:44 +00006724 if (!xmlXPathIsNaN(in + le) && !xmlXPathIsInf(in)) {
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006725 /*
Daniel Veillard9e412302002-06-10 15:59:44 +00006726 * To meet the requirements of the spec, the arguments
6727 * must be converted to integer format before
6728 * initial index calculations are done
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006729 *
Daniel Veillard9e412302002-06-10 15:59:44 +00006730 * First we go to integer form, rounding up
6731 * and checking for special cases
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006732 */
6733 i = (int) in;
Daniel Veillard9e412302002-06-10 15:59:44 +00006734 if (((double)i)+0.5 <= in) i++;
Owen Taylor3473f882001-02-23 17:55:21 +00006735
Daniel Veillard9e412302002-06-10 15:59:44 +00006736 if (xmlXPathIsInf(le) == 1) {
6737 l = m;
6738 if (i < 1)
6739 i = 1;
6740 }
6741 else if (xmlXPathIsInf(le) == -1 || le < 0.0)
6742 l = 0;
6743 else {
6744 l = (int) le;
6745 if (((double)l)+0.5 <= le) l++;
6746 }
6747
6748 /* Now we normalize inidices */
6749 i -= 1;
6750 l += i;
6751 if (i < 0)
6752 i = 0;
6753 if (l > m)
6754 l = m;
Owen Taylor3473f882001-02-23 17:55:21 +00006755
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006756 /* number of chars to copy */
6757 l -= i;
Owen Taylor3473f882001-02-23 17:55:21 +00006758
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006759 ret = xmlUTF8Strsub(str->stringval, i, l);
6760 }
6761 else {
6762 ret = NULL;
6763 }
6764
Owen Taylor3473f882001-02-23 17:55:21 +00006765 if (ret == NULL)
6766 valuePush(ctxt, xmlXPathNewCString(""));
6767 else {
6768 valuePush(ctxt, xmlXPathNewString(ret));
6769 xmlFree(ret);
6770 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006771
Owen Taylor3473f882001-02-23 17:55:21 +00006772 xmlXPathFreeObject(str);
6773}
6774
6775/**
6776 * xmlXPathSubstringBeforeFunction:
6777 * @ctxt: the XPath Parser context
6778 * @nargs: the number of arguments
6779 *
6780 * Implement the substring-before() XPath function
6781 * string substring-before(string, string)
6782 * The substring-before function returns the substring of the first
6783 * argument string that precedes the first occurrence of the second
6784 * argument string in the first argument string, or the empty string
6785 * if the first argument string does not contain the second argument
6786 * string. For example, substring-before("1999/04/01","/") returns 1999.
6787 */
6788void
6789xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6790 xmlXPathObjectPtr str;
6791 xmlXPathObjectPtr find;
6792 xmlBufferPtr target;
6793 const xmlChar *point;
6794 int offset;
6795
6796 CHECK_ARITY(2);
6797 CAST_TO_STRING;
6798 find = valuePop(ctxt);
6799 CAST_TO_STRING;
6800 str = valuePop(ctxt);
6801
6802 target = xmlBufferCreate();
6803 if (target) {
6804 point = xmlStrstr(str->stringval, find->stringval);
6805 if (point) {
6806 offset = (int)(point - str->stringval);
6807 xmlBufferAdd(target, str->stringval, offset);
6808 }
6809 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6810 xmlBufferFree(target);
6811 }
6812
6813 xmlXPathFreeObject(str);
6814 xmlXPathFreeObject(find);
6815}
6816
6817/**
6818 * xmlXPathSubstringAfterFunction:
6819 * @ctxt: the XPath Parser context
6820 * @nargs: the number of arguments
6821 *
6822 * Implement the substring-after() XPath function
6823 * string substring-after(string, string)
6824 * The substring-after function returns the substring of the first
6825 * argument string that follows the first occurrence of the second
6826 * argument string in the first argument string, or the empty stringi
6827 * if the first argument string does not contain the second argument
6828 * string. For example, substring-after("1999/04/01","/") returns 04/01,
6829 * and substring-after("1999/04/01","19") returns 99/04/01.
6830 */
6831void
6832xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6833 xmlXPathObjectPtr str;
6834 xmlXPathObjectPtr find;
6835 xmlBufferPtr target;
6836 const xmlChar *point;
6837 int offset;
6838
6839 CHECK_ARITY(2);
6840 CAST_TO_STRING;
6841 find = valuePop(ctxt);
6842 CAST_TO_STRING;
6843 str = valuePop(ctxt);
6844
6845 target = xmlBufferCreate();
6846 if (target) {
6847 point = xmlStrstr(str->stringval, find->stringval);
6848 if (point) {
6849 offset = (int)(point - str->stringval) + xmlStrlen(find->stringval);
6850 xmlBufferAdd(target, &str->stringval[offset],
6851 xmlStrlen(str->stringval) - offset);
6852 }
6853 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6854 xmlBufferFree(target);
6855 }
6856
6857 xmlXPathFreeObject(str);
6858 xmlXPathFreeObject(find);
6859}
6860
6861/**
6862 * xmlXPathNormalizeFunction:
6863 * @ctxt: the XPath Parser context
6864 * @nargs: the number of arguments
6865 *
6866 * Implement the normalize-space() XPath function
6867 * string normalize-space(string?)
6868 * The normalize-space function returns the argument string with white
6869 * space normalized by stripping leading and trailing whitespace
6870 * and replacing sequences of whitespace characters by a single
6871 * space. Whitespace characters are the same allowed by the S production
6872 * in XML. If the argument is omitted, it defaults to the context
6873 * node converted to a string, in other words the value of the context node.
6874 */
6875void
6876xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6877 xmlXPathObjectPtr obj = NULL;
6878 xmlChar *source = NULL;
6879 xmlBufferPtr target;
6880 xmlChar blank;
6881
Daniel Veillarda82b1822004-11-08 16:24:57 +00006882 if (ctxt == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00006883 if (nargs == 0) {
6884 /* Use current context node */
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006885 valuePush(ctxt,
6886 xmlXPathWrapString(
6887 xmlXPathCastNodeToString(ctxt->context->node)));
Owen Taylor3473f882001-02-23 17:55:21 +00006888 nargs = 1;
6889 }
6890
6891 CHECK_ARITY(1);
6892 CAST_TO_STRING;
6893 CHECK_TYPE(XPATH_STRING);
6894 obj = valuePop(ctxt);
6895 source = obj->stringval;
6896
6897 target = xmlBufferCreate();
6898 if (target && source) {
6899
6900 /* Skip leading whitespaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006901 while (IS_BLANK_CH(*source))
Owen Taylor3473f882001-02-23 17:55:21 +00006902 source++;
6903
6904 /* Collapse intermediate whitespaces, and skip trailing whitespaces */
6905 blank = 0;
6906 while (*source) {
William M. Brack76e95df2003-10-18 16:20:14 +00006907 if (IS_BLANK_CH(*source)) {
Daniel Veillard97ac1312001-05-30 19:14:17 +00006908 blank = 0x20;
Owen Taylor3473f882001-02-23 17:55:21 +00006909 } else {
6910 if (blank) {
6911 xmlBufferAdd(target, &blank, 1);
6912 blank = 0;
6913 }
6914 xmlBufferAdd(target, source, 1);
6915 }
6916 source++;
6917 }
6918
6919 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6920 xmlBufferFree(target);
6921 }
6922 xmlXPathFreeObject(obj);
6923}
6924
6925/**
6926 * xmlXPathTranslateFunction:
6927 * @ctxt: the XPath Parser context
6928 * @nargs: the number of arguments
6929 *
6930 * Implement the translate() XPath function
6931 * string translate(string, string, string)
6932 * The translate function returns the first argument string with
6933 * occurrences of characters in the second argument string replaced
6934 * by the character at the corresponding position in the third argument
6935 * string. For example, translate("bar","abc","ABC") returns the string
6936 * BAr. If there is a character in the second argument string with no
6937 * character at a corresponding position in the third argument string
6938 * (because the second argument string is longer than the third argument
6939 * string), then occurrences of that character in the first argument
6940 * string are removed. For example, translate("--aaa--","abc-","ABC")
6941 * returns "AAA". If a character occurs more than once in second
6942 * argument string, then the first occurrence determines the replacement
6943 * character. If the third argument string is longer than the second
6944 * argument string, then excess characters are ignored.
6945 */
6946void
6947xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillarde043ee12001-04-16 14:08:07 +00006948 xmlXPathObjectPtr str;
6949 xmlXPathObjectPtr from;
6950 xmlXPathObjectPtr to;
6951 xmlBufferPtr target;
Daniel Veillard97ac1312001-05-30 19:14:17 +00006952 int offset, max;
Daniel Veillarde043ee12001-04-16 14:08:07 +00006953 xmlChar ch;
William M. Brackb031cef2004-11-05 16:34:22 +00006954 const xmlChar *point;
Daniel Veillard97ac1312001-05-30 19:14:17 +00006955 xmlChar *cptr;
Owen Taylor3473f882001-02-23 17:55:21 +00006956
Daniel Veillarde043ee12001-04-16 14:08:07 +00006957 CHECK_ARITY(3);
Owen Taylor3473f882001-02-23 17:55:21 +00006958
Daniel Veillarde043ee12001-04-16 14:08:07 +00006959 CAST_TO_STRING;
6960 to = valuePop(ctxt);
6961 CAST_TO_STRING;
6962 from = valuePop(ctxt);
6963 CAST_TO_STRING;
6964 str = valuePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006965
Daniel Veillarde043ee12001-04-16 14:08:07 +00006966 target = xmlBufferCreate();
6967 if (target) {
Daniel Veillard97ac1312001-05-30 19:14:17 +00006968 max = xmlUTF8Strlen(to->stringval);
6969 for (cptr = str->stringval; (ch=*cptr); ) {
6970 offset = xmlUTF8Strloc(from->stringval, cptr);
6971 if (offset >= 0) {
6972 if (offset < max) {
6973 point = xmlUTF8Strpos(to->stringval, offset);
6974 if (point)
6975 xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1));
6976 }
6977 } else
6978 xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1));
6979
6980 /* Step to next character in input */
6981 cptr++;
6982 if ( ch & 0x80 ) {
6983 /* if not simple ascii, verify proper format */
6984 if ( (ch & 0xc0) != 0xc0 ) {
6985 xmlGenericError(xmlGenericErrorContext,
6986 "xmlXPathTranslateFunction: Invalid UTF8 string\n");
6987 break;
6988 }
6989 /* then skip over remaining bytes for this char */
6990 while ( (ch <<= 1) & 0x80 )
6991 if ( (*cptr++ & 0xc0) != 0x80 ) {
6992 xmlGenericError(xmlGenericErrorContext,
6993 "xmlXPathTranslateFunction: Invalid UTF8 string\n");
6994 break;
6995 }
6996 if (ch & 0x80) /* must have had error encountered */
6997 break;
6998 }
Daniel Veillarde043ee12001-04-16 14:08:07 +00006999 }
Owen Taylor3473f882001-02-23 17:55:21 +00007000 }
Daniel Veillarde043ee12001-04-16 14:08:07 +00007001 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
7002 xmlBufferFree(target);
7003 xmlXPathFreeObject(str);
7004 xmlXPathFreeObject(from);
7005 xmlXPathFreeObject(to);
Owen Taylor3473f882001-02-23 17:55:21 +00007006}
7007
7008/**
7009 * xmlXPathBooleanFunction:
7010 * @ctxt: the XPath Parser context
7011 * @nargs: the number of arguments
7012 *
7013 * Implement the boolean() XPath function
7014 * boolean boolean(object)
William M. Brack08171912003-12-29 02:52:11 +00007015 * The boolean function converts its argument to a boolean as follows:
Owen Taylor3473f882001-02-23 17:55:21 +00007016 * - a number is true if and only if it is neither positive or
7017 * negative zero nor NaN
7018 * - a node-set is true if and only if it is non-empty
7019 * - a string is true if and only if its length is non-zero
7020 */
7021void
7022xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7023 xmlXPathObjectPtr cur;
Owen Taylor3473f882001-02-23 17:55:21 +00007024
7025 CHECK_ARITY(1);
7026 cur = valuePop(ctxt);
7027 if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007028 cur = xmlXPathConvertBoolean(cur);
7029 valuePush(ctxt, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00007030}
7031
7032/**
7033 * xmlXPathNotFunction:
7034 * @ctxt: the XPath Parser context
7035 * @nargs: the number of arguments
7036 *
7037 * Implement the not() XPath function
7038 * boolean not(boolean)
7039 * The not function returns true if its argument is false,
7040 * and false otherwise.
7041 */
7042void
7043xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7044 CHECK_ARITY(1);
7045 CAST_TO_BOOLEAN;
7046 CHECK_TYPE(XPATH_BOOLEAN);
7047 ctxt->value->boolval = ! ctxt->value->boolval;
7048}
7049
7050/**
7051 * xmlXPathTrueFunction:
7052 * @ctxt: the XPath Parser context
7053 * @nargs: the number of arguments
7054 *
7055 * Implement the true() XPath function
7056 * boolean true()
7057 */
7058void
7059xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7060 CHECK_ARITY(0);
7061 valuePush(ctxt, xmlXPathNewBoolean(1));
7062}
7063
7064/**
7065 * xmlXPathFalseFunction:
7066 * @ctxt: the XPath Parser context
7067 * @nargs: the number of arguments
7068 *
7069 * Implement the false() XPath function
7070 * boolean false()
7071 */
7072void
7073xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7074 CHECK_ARITY(0);
7075 valuePush(ctxt, xmlXPathNewBoolean(0));
7076}
7077
7078/**
7079 * xmlXPathLangFunction:
7080 * @ctxt: the XPath Parser context
7081 * @nargs: the number of arguments
7082 *
7083 * Implement the lang() XPath function
7084 * boolean lang(string)
7085 * The lang function returns true or false depending on whether the
7086 * language of the context node as specified by xml:lang attributes
7087 * is the same as or is a sublanguage of the language specified by
7088 * the argument string. The language of the context node is determined
7089 * by the value of the xml:lang attribute on the context node, or, if
7090 * the context node has no xml:lang attribute, by the value of the
7091 * xml:lang attribute on the nearest ancestor of the context node that
7092 * has an xml:lang attribute. If there is no such attribute, then lang
7093 * returns false. If there is such an attribute, then lang returns
7094 * true if the attribute value is equal to the argument ignoring case,
7095 * or if there is some suffix starting with - such that the attribute
7096 * value is equal to the argument ignoring that suffix of the attribute
7097 * value and ignoring case.
7098 */
7099void
7100xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard4ddaa562005-04-06 14:09:08 +00007101 xmlXPathObjectPtr val = NULL;
7102 const xmlChar *theLang = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00007103 const xmlChar *lang;
7104 int ret = 0;
7105 int i;
7106
7107 CHECK_ARITY(1);
7108 CAST_TO_STRING;
7109 CHECK_TYPE(XPATH_STRING);
7110 val = valuePop(ctxt);
7111 lang = val->stringval;
7112 theLang = xmlNodeGetLang(ctxt->context->node);
7113 if ((theLang != NULL) && (lang != NULL)) {
7114 for (i = 0;lang[i] != 0;i++)
7115 if (toupper(lang[i]) != toupper(theLang[i]))
7116 goto not_equal;
Daniel Veillard4ddaa562005-04-06 14:09:08 +00007117 if ((theLang[i] == 0) || (theLang[i] == '-'))
7118 ret = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00007119 }
7120not_equal:
Daniel Veillard4ddaa562005-04-06 14:09:08 +00007121 if (theLang != NULL)
7122 xmlFree((void *)theLang);
Owen Taylor3473f882001-02-23 17:55:21 +00007123 xmlXPathFreeObject(val);
7124 valuePush(ctxt, xmlXPathNewBoolean(ret));
7125}
7126
7127/**
7128 * xmlXPathNumberFunction:
7129 * @ctxt: the XPath Parser context
7130 * @nargs: the number of arguments
7131 *
7132 * Implement the number() XPath function
7133 * number number(object?)
7134 */
7135void
7136xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7137 xmlXPathObjectPtr cur;
7138 double res;
7139
Daniel Veillarda82b1822004-11-08 16:24:57 +00007140 if (ctxt == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00007141 if (nargs == 0) {
7142 if (ctxt->context->node == NULL) {
7143 valuePush(ctxt, xmlXPathNewFloat(0.0));
7144 } else {
7145 xmlChar* content = xmlNodeGetContent(ctxt->context->node);
7146
7147 res = xmlXPathStringEvalNumber(content);
7148 valuePush(ctxt, xmlXPathNewFloat(res));
7149 xmlFree(content);
7150 }
7151 return;
7152 }
7153
7154 CHECK_ARITY(1);
7155 cur = valuePop(ctxt);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007156 cur = xmlXPathConvertNumber(cur);
7157 valuePush(ctxt, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00007158}
7159
7160/**
7161 * xmlXPathSumFunction:
7162 * @ctxt: the XPath Parser context
7163 * @nargs: the number of arguments
7164 *
7165 * Implement the sum() XPath function
7166 * number sum(node-set)
7167 * The sum function returns the sum of the values of the nodes in
7168 * the argument node-set.
7169 */
7170void
7171xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7172 xmlXPathObjectPtr cur;
7173 int i;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00007174 double res = 0.0;
Owen Taylor3473f882001-02-23 17:55:21 +00007175
7176 CHECK_ARITY(1);
7177 if ((ctxt->value == NULL) ||
7178 ((ctxt->value->type != XPATH_NODESET) &&
7179 (ctxt->value->type != XPATH_XSLT_TREE)))
7180 XP_ERROR(XPATH_INVALID_TYPE);
7181 cur = valuePop(ctxt);
7182
William M. Brack08171912003-12-29 02:52:11 +00007183 if ((cur->nodesetval != NULL) && (cur->nodesetval->nodeNr != 0)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00007184 for (i = 0; i < cur->nodesetval->nodeNr; i++) {
7185 res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00007186 }
7187 }
William M. Brack08171912003-12-29 02:52:11 +00007188 valuePush(ctxt, xmlXPathNewFloat(res));
Owen Taylor3473f882001-02-23 17:55:21 +00007189 xmlXPathFreeObject(cur);
7190}
7191
7192/**
7193 * xmlXPathFloorFunction:
7194 * @ctxt: the XPath Parser context
7195 * @nargs: the number of arguments
7196 *
7197 * Implement the floor() XPath function
7198 * number floor(number)
7199 * The floor function returns the largest (closest to positive infinity)
7200 * number that is not greater than the argument and that is an integer.
7201 */
7202void
7203xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007204 double f;
7205
Owen Taylor3473f882001-02-23 17:55:21 +00007206 CHECK_ARITY(1);
7207 CAST_TO_NUMBER;
7208 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007209
7210 f = (double)((int) ctxt->value->floatval);
7211 if (f != ctxt->value->floatval) {
7212 if (ctxt->value->floatval > 0)
7213 ctxt->value->floatval = f;
7214 else
7215 ctxt->value->floatval = f - 1;
7216 }
Owen Taylor3473f882001-02-23 17:55:21 +00007217}
7218
7219/**
7220 * xmlXPathCeilingFunction:
7221 * @ctxt: the XPath Parser context
7222 * @nargs: the number of arguments
7223 *
7224 * Implement the ceiling() XPath function
7225 * number ceiling(number)
7226 * The ceiling function returns the smallest (closest to negative infinity)
7227 * number that is not less than the argument and that is an integer.
7228 */
7229void
7230xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7231 double f;
7232
7233 CHECK_ARITY(1);
7234 CAST_TO_NUMBER;
7235 CHECK_TYPE(XPATH_NUMBER);
7236
7237#if 0
7238 ctxt->value->floatval = ceil(ctxt->value->floatval);
7239#else
7240 f = (double)((int) ctxt->value->floatval);
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007241 if (f != ctxt->value->floatval) {
7242 if (ctxt->value->floatval > 0)
7243 ctxt->value->floatval = f + 1;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00007244 else {
7245 if (ctxt->value->floatval < 0 && f == 0)
7246 ctxt->value->floatval = xmlXPathNZERO;
7247 else
7248 ctxt->value->floatval = f;
7249 }
7250
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007251 }
Owen Taylor3473f882001-02-23 17:55:21 +00007252#endif
7253}
7254
7255/**
7256 * xmlXPathRoundFunction:
7257 * @ctxt: the XPath Parser context
7258 * @nargs: the number of arguments
7259 *
7260 * Implement the round() XPath function
7261 * number round(number)
7262 * The round function returns the number that is closest to the
7263 * argument and that is an integer. If there are two such numbers,
7264 * then the one that is even is returned.
7265 */
7266void
7267xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) {
7268 double f;
7269
7270 CHECK_ARITY(1);
7271 CAST_TO_NUMBER;
7272 CHECK_TYPE(XPATH_NUMBER);
7273
Daniel Veillardcda96922001-08-21 10:56:31 +00007274 if ((xmlXPathIsNaN(ctxt->value->floatval)) ||
7275 (xmlXPathIsInf(ctxt->value->floatval) == 1) ||
7276 (xmlXPathIsInf(ctxt->value->floatval) == -1) ||
Owen Taylor3473f882001-02-23 17:55:21 +00007277 (ctxt->value->floatval == 0.0))
7278 return;
7279
Owen Taylor3473f882001-02-23 17:55:21 +00007280 f = (double)((int) ctxt->value->floatval);
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007281 if (ctxt->value->floatval < 0) {
7282 if (ctxt->value->floatval < f - 0.5)
7283 ctxt->value->floatval = f - 1;
7284 else
7285 ctxt->value->floatval = f;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00007286 if (ctxt->value->floatval == 0)
7287 ctxt->value->floatval = xmlXPathNZERO;
Daniel Veillard56cd18b2002-03-22 14:14:43 +00007288 } else {
7289 if (ctxt->value->floatval < f + 0.5)
7290 ctxt->value->floatval = f;
7291 else
7292 ctxt->value->floatval = f + 1;
7293 }
Owen Taylor3473f882001-02-23 17:55:21 +00007294}
7295
7296/************************************************************************
7297 * *
7298 * The Parser *
7299 * *
7300 ************************************************************************/
7301
7302/*
William M. Brack08171912003-12-29 02:52:11 +00007303 * a few forward declarations since we use a recursive call based
Owen Taylor3473f882001-02-23 17:55:21 +00007304 * implementation.
7305 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007306static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00007307static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007308static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007309static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt);
Daniel Veillard2156a562001-04-28 12:24:34 +00007310static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt,
7311 int qualified);
Owen Taylor3473f882001-02-23 17:55:21 +00007312
7313/**
Daniel Veillard61d80a22001-04-27 17:13:01 +00007314 * xmlXPathCurrentChar:
7315 * @ctxt: the XPath parser context
7316 * @cur: pointer to the beginning of the char
7317 * @len: pointer to the length of the char read
7318 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00007319 * The current char value, if using UTF-8 this may actually span multiple
Daniel Veillard61d80a22001-04-27 17:13:01 +00007320 * bytes in the input buffer.
7321 *
Daniel Veillard60087f32001-10-10 09:45:09 +00007322 * Returns the current char value and its length
Daniel Veillard61d80a22001-04-27 17:13:01 +00007323 */
7324
7325static int
7326xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) {
7327 unsigned char c;
7328 unsigned int val;
7329 const xmlChar *cur;
7330
7331 if (ctxt == NULL)
7332 return(0);
7333 cur = ctxt->cur;
7334
7335 /*
7336 * We are supposed to handle UTF8, check it's valid
7337 * From rfc2044: encoding of the Unicode values on UTF-8:
7338 *
7339 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
7340 * 0000 0000-0000 007F 0xxxxxxx
7341 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
7342 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
7343 *
7344 * Check for the 0x110000 limit too
7345 */
7346 c = *cur;
7347 if (c & 0x80) {
7348 if ((cur[1] & 0xc0) != 0x80)
7349 goto encoding_error;
7350 if ((c & 0xe0) == 0xe0) {
7351
7352 if ((cur[2] & 0xc0) != 0x80)
7353 goto encoding_error;
7354 if ((c & 0xf0) == 0xf0) {
7355 if (((c & 0xf8) != 0xf0) ||
7356 ((cur[3] & 0xc0) != 0x80))
7357 goto encoding_error;
7358 /* 4-byte code */
7359 *len = 4;
7360 val = (cur[0] & 0x7) << 18;
7361 val |= (cur[1] & 0x3f) << 12;
7362 val |= (cur[2] & 0x3f) << 6;
7363 val |= cur[3] & 0x3f;
7364 } else {
7365 /* 3-byte code */
7366 *len = 3;
7367 val = (cur[0] & 0xf) << 12;
7368 val |= (cur[1] & 0x3f) << 6;
7369 val |= cur[2] & 0x3f;
7370 }
7371 } else {
7372 /* 2-byte code */
7373 *len = 2;
7374 val = (cur[0] & 0x1f) << 6;
7375 val |= cur[1] & 0x3f;
7376 }
7377 if (!IS_CHAR(val)) {
7378 XP_ERROR0(XPATH_INVALID_CHAR_ERROR);
7379 }
7380 return(val);
7381 } else {
7382 /* 1-byte code */
7383 *len = 1;
7384 return((int) *cur);
7385 }
7386encoding_error:
7387 /*
William M. Brack08171912003-12-29 02:52:11 +00007388 * If we detect an UTF8 error that probably means that the
7389 * input encoding didn't get properly advertised in the
Daniel Veillard61d80a22001-04-27 17:13:01 +00007390 * declaration header. Report the error and switch the encoding
7391 * to ISO-Latin-1 (if you don't like this policy, just declare the
7392 * encoding !)
7393 */
Daniel Veillard42596ad2001-05-22 16:57:14 +00007394 *len = 0;
Daniel Veillard61d80a22001-04-27 17:13:01 +00007395 XP_ERROR0(XPATH_ENCODING_ERROR);
Daniel Veillard61d80a22001-04-27 17:13:01 +00007396}
7397
7398/**
Owen Taylor3473f882001-02-23 17:55:21 +00007399 * xmlXPathParseNCName:
7400 * @ctxt: the XPath Parser context
7401 *
7402 * parse an XML namespace non qualified name.
7403 *
7404 * [NS 3] NCName ::= (Letter | '_') (NCNameChar)*
7405 *
7406 * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
7407 * CombiningChar | Extender
7408 *
7409 * Returns the namespace name or NULL
7410 */
7411
7412xmlChar *
7413xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) {
Daniel Veillard2156a562001-04-28 12:24:34 +00007414 const xmlChar *in;
7415 xmlChar *ret;
7416 int count = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007417
Daniel Veillarda82b1822004-11-08 16:24:57 +00007418 if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL);
Daniel Veillard2156a562001-04-28 12:24:34 +00007419 /*
7420 * Accelerator for simple ASCII names
7421 */
7422 in = ctxt->cur;
7423 if (((*in >= 0x61) && (*in <= 0x7A)) ||
7424 ((*in >= 0x41) && (*in <= 0x5A)) ||
7425 (*in == '_')) {
7426 in++;
7427 while (((*in >= 0x61) && (*in <= 0x7A)) ||
7428 ((*in >= 0x41) && (*in <= 0x5A)) ||
7429 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard9a89a8a2001-06-27 11:13:35 +00007430 (*in == '_') || (*in == '.') ||
7431 (*in == '-'))
Daniel Veillard2156a562001-04-28 12:24:34 +00007432 in++;
7433 if ((*in == ' ') || (*in == '>') || (*in == '/') ||
7434 (*in == '[') || (*in == ']') || (*in == ':') ||
7435 (*in == '@') || (*in == '*')) {
7436 count = in - ctxt->cur;
7437 if (count == 0)
7438 return(NULL);
7439 ret = xmlStrndup(ctxt->cur, count);
7440 ctxt->cur = in;
7441 return(ret);
7442 }
7443 }
7444 return(xmlXPathParseNameComplex(ctxt, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00007445}
7446
Daniel Veillard2156a562001-04-28 12:24:34 +00007447
Owen Taylor3473f882001-02-23 17:55:21 +00007448/**
7449 * xmlXPathParseQName:
7450 * @ctxt: the XPath Parser context
7451 * @prefix: a xmlChar **
7452 *
7453 * parse an XML qualified name
7454 *
7455 * [NS 5] QName ::= (Prefix ':')? LocalPart
7456 *
7457 * [NS 6] Prefix ::= NCName
7458 *
7459 * [NS 7] LocalPart ::= NCName
7460 *
7461 * Returns the function returns the local part, and prefix is updated
7462 * to get the Prefix if any.
7463 */
7464
Daniel Veillard56a4cb82001-03-24 17:00:36 +00007465static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00007466xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) {
7467 xmlChar *ret = NULL;
7468
7469 *prefix = NULL;
7470 ret = xmlXPathParseNCName(ctxt);
7471 if (CUR == ':') {
7472 *prefix = ret;
7473 NEXT;
7474 ret = xmlXPathParseNCName(ctxt);
7475 }
7476 return(ret);
7477}
7478
7479/**
7480 * xmlXPathParseName:
7481 * @ctxt: the XPath Parser context
7482 *
7483 * parse an XML name
7484 *
7485 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
7486 * CombiningChar | Extender
7487 *
7488 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
7489 *
7490 * Returns the namespace name or NULL
7491 */
7492
7493xmlChar *
7494xmlXPathParseName(xmlXPathParserContextPtr ctxt) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00007495 const xmlChar *in;
7496 xmlChar *ret;
7497 int count = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007498
Daniel Veillarda82b1822004-11-08 16:24:57 +00007499 if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL);
Daniel Veillard61d80a22001-04-27 17:13:01 +00007500 /*
7501 * Accelerator for simple ASCII names
7502 */
7503 in = ctxt->cur;
7504 if (((*in >= 0x61) && (*in <= 0x7A)) ||
7505 ((*in >= 0x41) && (*in <= 0x5A)) ||
7506 (*in == '_') || (*in == ':')) {
7507 in++;
7508 while (((*in >= 0x61) && (*in <= 0x7A)) ||
7509 ((*in >= 0x41) && (*in <= 0x5A)) ||
7510 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard76d66f42001-05-16 21:05:17 +00007511 (*in == '_') || (*in == '-') ||
7512 (*in == ':') || (*in == '.'))
Daniel Veillard61d80a22001-04-27 17:13:01 +00007513 in++;
Daniel Veillard76d66f42001-05-16 21:05:17 +00007514 if ((*in > 0) && (*in < 0x80)) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00007515 count = in - ctxt->cur;
7516 ret = xmlStrndup(ctxt->cur, count);
7517 ctxt->cur = in;
7518 return(ret);
7519 }
7520 }
Daniel Veillard2156a562001-04-28 12:24:34 +00007521 return(xmlXPathParseNameComplex(ctxt, 1));
Owen Taylor3473f882001-02-23 17:55:21 +00007522}
7523
Daniel Veillard61d80a22001-04-27 17:13:01 +00007524static xmlChar *
Daniel Veillard2156a562001-04-28 12:24:34 +00007525xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00007526 xmlChar buf[XML_MAX_NAMELEN + 5];
7527 int len = 0, l;
7528 int c;
7529
7530 /*
7531 * Handler for more complex cases
7532 */
7533 c = CUR_CHAR(l);
7534 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
Daniel Veillard2156a562001-04-28 12:24:34 +00007535 (c == '[') || (c == ']') || (c == '@') || /* accelerators */
7536 (c == '*') || /* accelerators */
Daniel Veillard61d80a22001-04-27 17:13:01 +00007537 (!IS_LETTER(c) && (c != '_') &&
Daniel Veillard2156a562001-04-28 12:24:34 +00007538 ((qualified) && (c != ':')))) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00007539 return(NULL);
7540 }
7541
7542 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
7543 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
7544 (c == '.') || (c == '-') ||
Daniel Veillard2156a562001-04-28 12:24:34 +00007545 (c == '_') || ((qualified) && (c == ':')) ||
Daniel Veillard61d80a22001-04-27 17:13:01 +00007546 (IS_COMBINING(c)) ||
7547 (IS_EXTENDER(c)))) {
7548 COPY_BUF(l,buf,len,c);
7549 NEXTL(l);
7550 c = CUR_CHAR(l);
7551 if (len >= XML_MAX_NAMELEN) {
7552 /*
7553 * Okay someone managed to make a huge name, so he's ready to pay
7554 * for the processing speed.
7555 */
7556 xmlChar *buffer;
7557 int max = len * 2;
7558
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007559 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Daniel Veillard61d80a22001-04-27 17:13:01 +00007560 if (buffer == NULL) {
7561 XP_ERROR0(XPATH_MEMORY_ERROR);
7562 }
7563 memcpy(buffer, buf, len);
7564 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */
7565 (c == '.') || (c == '-') ||
Daniel Veillard2156a562001-04-28 12:24:34 +00007566 (c == '_') || ((qualified) && (c == ':')) ||
Daniel Veillard61d80a22001-04-27 17:13:01 +00007567 (IS_COMBINING(c)) ||
7568 (IS_EXTENDER(c))) {
7569 if (len + 10 > max) {
7570 max *= 2;
7571 buffer = (xmlChar *) xmlRealloc(buffer,
7572 max * sizeof(xmlChar));
Daniel Veillard61d80a22001-04-27 17:13:01 +00007573 if (buffer == NULL) {
7574 XP_ERROR0(XPATH_MEMORY_ERROR);
7575 }
7576 }
7577 COPY_BUF(l,buffer,len,c);
7578 NEXTL(l);
7579 c = CUR_CHAR(l);
7580 }
7581 buffer[len] = 0;
7582 return(buffer);
7583 }
7584 }
Daniel Veillard2156a562001-04-28 12:24:34 +00007585 if (len == 0)
7586 return(NULL);
Daniel Veillard61d80a22001-04-27 17:13:01 +00007587 return(xmlStrndup(buf, len));
7588}
Daniel Veillard3cd72402002-05-13 10:33:30 +00007589
7590#define MAX_FRAC 20
7591
William M. Brack372a4452004-02-17 13:09:23 +00007592/*
7593 * These are used as divisors for the fractional part of a number.
7594 * Since the table includes 1.0 (representing '0' fractional digits),
7595 * it must be dimensioned at MAX_FRAC+1 (bug 133921)
7596 */
7597static double my_pow10[MAX_FRAC+1] = {
Daniel Veillard3cd72402002-05-13 10:33:30 +00007598 1.0, 10.0, 100.0, 1000.0, 10000.0,
7599 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0,
7600 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0,
7601 100000000000000.0,
7602 1000000000000000.0, 10000000000000000.0, 100000000000000000.0,
William M. Brack372a4452004-02-17 13:09:23 +00007603 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0
Daniel Veillard3cd72402002-05-13 10:33:30 +00007604};
7605
Owen Taylor3473f882001-02-23 17:55:21 +00007606/**
7607 * xmlXPathStringEvalNumber:
7608 * @str: A string to scan
7609 *
Bjorn Reese70a9da52001-04-21 16:57:29 +00007610 * [30a] Float ::= Number ('e' Digits?)?
7611 *
Owen Taylor3473f882001-02-23 17:55:21 +00007612 * [30] Number ::= Digits ('.' Digits?)?
7613 * | '.' Digits
7614 * [31] Digits ::= [0-9]+
7615 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007616 * Compile a Number in the string
Owen Taylor3473f882001-02-23 17:55:21 +00007617 * In complement of the Number expression, this function also handles
7618 * negative values : '-' Number.
7619 *
7620 * Returns the double value.
7621 */
7622double
7623xmlXPathStringEvalNumber(const xmlChar *str) {
7624 const xmlChar *cur = str;
Daniel Veillard7b416132002-03-07 08:36:03 +00007625 double ret;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007626 int ok = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007627 int isneg = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007628 int exponent = 0;
7629 int is_exponent_negative = 0;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007630#ifdef __GNUC__
7631 unsigned long tmp = 0;
Daniel Veillard7b416132002-03-07 08:36:03 +00007632 double temp;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007633#endif
Daniel Veillardeca82812002-04-24 11:42:02 +00007634 if (cur == NULL) return(0);
William M. Brack76e95df2003-10-18 16:20:14 +00007635 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00007636 if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) {
7637 return(xmlXPathNAN);
7638 }
7639 if (*cur == '-') {
7640 isneg = 1;
7641 cur++;
7642 }
Daniel Veillardb06c6142001-08-27 14:26:30 +00007643
7644#ifdef __GNUC__
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007645 /*
Daniel Veillard7b416132002-03-07 08:36:03 +00007646 * tmp/temp is a workaround against a gcc compiler bug
7647 * http://veillard.com/gcc.bug
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007648 */
Daniel Veillard7b416132002-03-07 08:36:03 +00007649 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007650 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillard7b416132002-03-07 08:36:03 +00007651 ret = ret * 10;
7652 tmp = (*cur - '0');
Owen Taylor3473f882001-02-23 17:55:21 +00007653 ok = 1;
7654 cur++;
Daniel Veillard7b416132002-03-07 08:36:03 +00007655 temp = (double) tmp;
7656 ret = ret + temp;
Owen Taylor3473f882001-02-23 17:55:21 +00007657 }
Daniel Veillardb06c6142001-08-27 14:26:30 +00007658#else
Daniel Veillard7b416132002-03-07 08:36:03 +00007659 ret = 0;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007660 while ((*cur >= '0') && (*cur <= '9')) {
7661 ret = ret * 10 + (*cur - '0');
7662 ok = 1;
7663 cur++;
7664 }
7665#endif
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007666
Owen Taylor3473f882001-02-23 17:55:21 +00007667 if (*cur == '.') {
Daniel Veillard3cd72402002-05-13 10:33:30 +00007668 int v, frac = 0;
7669 double fraction = 0;
7670
Owen Taylor3473f882001-02-23 17:55:21 +00007671 cur++;
7672 if (((*cur < '0') || (*cur > '9')) && (!ok)) {
7673 return(xmlXPathNAN);
7674 }
Daniel Veillard3cd72402002-05-13 10:33:30 +00007675 while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) {
7676 v = (*cur - '0');
7677 fraction = fraction * 10 + v;
7678 frac = frac + 1;
Owen Taylor3473f882001-02-23 17:55:21 +00007679 cur++;
7680 }
Daniel Veillard3cd72402002-05-13 10:33:30 +00007681 fraction /= my_pow10[frac];
7682 ret = ret + fraction;
7683 while ((*cur >= '0') && (*cur <= '9'))
7684 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00007685 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00007686 if ((*cur == 'e') || (*cur == 'E')) {
7687 cur++;
7688 if (*cur == '-') {
7689 is_exponent_negative = 1;
7690 cur++;
William M. Brack99127052004-05-24 02:52:28 +00007691 } else if (*cur == '+') {
7692 cur++;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007693 }
7694 while ((*cur >= '0') && (*cur <= '9')) {
7695 exponent = exponent * 10 + (*cur - '0');
7696 cur++;
7697 }
7698 }
William M. Brack76e95df2003-10-18 16:20:14 +00007699 while (IS_BLANK_CH(*cur)) cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00007700 if (*cur != 0) return(xmlXPathNAN);
7701 if (isneg) ret = -ret;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007702 if (is_exponent_negative) exponent = -exponent;
7703 ret *= pow(10.0, (double)exponent);
Owen Taylor3473f882001-02-23 17:55:21 +00007704 return(ret);
7705}
7706
7707/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007708 * xmlXPathCompNumber:
Owen Taylor3473f882001-02-23 17:55:21 +00007709 * @ctxt: the XPath Parser context
7710 *
7711 * [30] Number ::= Digits ('.' Digits?)?
7712 * | '.' Digits
7713 * [31] Digits ::= [0-9]+
7714 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007715 * Compile a Number, then push it on the stack
Owen Taylor3473f882001-02-23 17:55:21 +00007716 *
7717 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007718static void
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007719xmlXPathCompNumber(xmlXPathParserContextPtr ctxt)
7720{
Owen Taylor3473f882001-02-23 17:55:21 +00007721 double ret = 0.0;
7722 double mult = 1;
Daniel Veillard7b416132002-03-07 08:36:03 +00007723 int ok = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007724 int exponent = 0;
7725 int is_exponent_negative = 0;
Daniel Veillard7b416132002-03-07 08:36:03 +00007726#ifdef __GNUC__
7727 unsigned long tmp = 0;
7728 double temp;
7729#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007730
7731 CHECK_ERROR;
7732 if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) {
7733 XP_ERROR(XPATH_NUMBER_ERROR);
7734 }
Daniel Veillard7b416132002-03-07 08:36:03 +00007735#ifdef __GNUC__
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007736 /*
Daniel Veillard7b416132002-03-07 08:36:03 +00007737 * tmp/temp is a workaround against a gcc compiler bug
7738 * http://veillard.com/gcc.bug
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007739 */
Daniel Veillard7b416132002-03-07 08:36:03 +00007740 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007741 while ((CUR >= '0') && (CUR <= '9')) {
Daniel Veillard7b416132002-03-07 08:36:03 +00007742 ret = ret * 10;
7743 tmp = (CUR - '0');
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007744 ok = 1;
7745 NEXT;
Daniel Veillard7b416132002-03-07 08:36:03 +00007746 temp = (double) tmp;
7747 ret = ret + temp;
Owen Taylor3473f882001-02-23 17:55:21 +00007748 }
Daniel Veillard7b416132002-03-07 08:36:03 +00007749#else
7750 ret = 0;
7751 while ((CUR >= '0') && (CUR <= '9')) {
7752 ret = ret * 10 + (CUR - '0');
7753 ok = 1;
7754 NEXT;
7755 }
7756#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007757 if (CUR == '.') {
7758 NEXT;
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007759 if (((CUR < '0') || (CUR > '9')) && (!ok)) {
7760 XP_ERROR(XPATH_NUMBER_ERROR);
7761 }
7762 while ((CUR >= '0') && (CUR <= '9')) {
7763 mult /= 10;
7764 ret = ret + (CUR - '0') * mult;
7765 NEXT;
7766 }
Owen Taylor3473f882001-02-23 17:55:21 +00007767 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00007768 if ((CUR == 'e') || (CUR == 'E')) {
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007769 NEXT;
7770 if (CUR == '-') {
7771 is_exponent_negative = 1;
7772 NEXT;
William M. Brack99127052004-05-24 02:52:28 +00007773 } else if (CUR == '+') {
7774 NEXT;
7775 }
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007776 while ((CUR >= '0') && (CUR <= '9')) {
7777 exponent = exponent * 10 + (CUR - '0');
7778 NEXT;
7779 }
7780 if (is_exponent_negative)
7781 exponent = -exponent;
7782 ret *= pow(10.0, (double) exponent);
Bjorn Reese70a9da52001-04-21 16:57:29 +00007783 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007784 PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0,
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007785 xmlXPathNewFloat(ret), NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007786}
7787
7788/**
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007789 * xmlXPathParseLiteral:
7790 * @ctxt: the XPath Parser context
7791 *
7792 * Parse a Literal
7793 *
7794 * [29] Literal ::= '"' [^"]* '"'
7795 * | "'" [^']* "'"
7796 *
7797 * Returns the value found or NULL in case of error
7798 */
7799static xmlChar *
7800xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) {
7801 const xmlChar *q;
7802 xmlChar *ret = NULL;
7803
7804 if (CUR == '"') {
7805 NEXT;
7806 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00007807 while ((IS_CHAR_CH(CUR)) && (CUR != '"'))
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007808 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00007809 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007810 XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR);
7811 } else {
7812 ret = xmlStrndup(q, CUR_PTR - q);
7813 NEXT;
7814 }
7815 } else if (CUR == '\'') {
7816 NEXT;
7817 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00007818 while ((IS_CHAR_CH(CUR)) && (CUR != '\''))
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007819 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00007820 if (!IS_CHAR_CH(CUR)) {
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007821 XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR);
7822 } else {
7823 ret = xmlStrndup(q, CUR_PTR - q);
7824 NEXT;
7825 }
7826 } else {
7827 XP_ERROR0(XPATH_START_LITERAL_ERROR);
7828 }
7829 return(ret);
7830}
7831
7832/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007833 * xmlXPathCompLiteral:
Owen Taylor3473f882001-02-23 17:55:21 +00007834 * @ctxt: the XPath Parser context
7835 *
7836 * Parse a Literal and push it on the stack.
7837 *
7838 * [29] Literal ::= '"' [^"]* '"'
7839 * | "'" [^']* "'"
7840 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007841 * TODO: xmlXPathCompLiteral memory allocation could be improved.
Owen Taylor3473f882001-02-23 17:55:21 +00007842 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007843static void
7844xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007845 const xmlChar *q;
7846 xmlChar *ret = NULL;
7847
7848 if (CUR == '"') {
7849 NEXT;
7850 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00007851 while ((IS_CHAR_CH(CUR)) && (CUR != '"'))
Owen Taylor3473f882001-02-23 17:55:21 +00007852 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00007853 if (!IS_CHAR_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007854 XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR);
7855 } else {
7856 ret = xmlStrndup(q, CUR_PTR - q);
7857 NEXT;
7858 }
7859 } else if (CUR == '\'') {
7860 NEXT;
7861 q = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00007862 while ((IS_CHAR_CH(CUR)) && (CUR != '\''))
Owen Taylor3473f882001-02-23 17:55:21 +00007863 NEXT;
William M. Brack76e95df2003-10-18 16:20:14 +00007864 if (!IS_CHAR_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00007865 XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR);
7866 } else {
7867 ret = xmlStrndup(q, CUR_PTR - q);
7868 NEXT;
7869 }
7870 } else {
7871 XP_ERROR(XPATH_START_LITERAL_ERROR);
7872 }
7873 if (ret == NULL) return;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007874 PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0,
7875 xmlXPathNewString(ret), NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007876 xmlFree(ret);
7877}
7878
7879/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007880 * xmlXPathCompVariableReference:
Owen Taylor3473f882001-02-23 17:55:21 +00007881 * @ctxt: the XPath Parser context
7882 *
7883 * Parse a VariableReference, evaluate it and push it on the stack.
7884 *
7885 * The variable bindings consist of a mapping from variable names
William M. Brack08171912003-12-29 02:52:11 +00007886 * to variable values. The value of a variable is an object, which can be
Owen Taylor3473f882001-02-23 17:55:21 +00007887 * of any of the types that are possible for the value of an expression,
7888 * and may also be of additional types not specified here.
7889 *
7890 * Early evaluation is possible since:
7891 * The variable bindings [...] used to evaluate a subexpression are
7892 * always the same as those used to evaluate the containing expression.
7893 *
7894 * [36] VariableReference ::= '$' QName
7895 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007896static void
7897xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007898 xmlChar *name;
7899 xmlChar *prefix;
Owen Taylor3473f882001-02-23 17:55:21 +00007900
7901 SKIP_BLANKS;
7902 if (CUR != '$') {
7903 XP_ERROR(XPATH_VARIABLE_REF_ERROR);
7904 }
7905 NEXT;
7906 name = xmlXPathParseQName(ctxt, &prefix);
7907 if (name == NULL) {
7908 XP_ERROR(XPATH_VARIABLE_REF_ERROR);
7909 }
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007910 ctxt->comp->last = -1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007911 PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0,
7912 name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00007913 SKIP_BLANKS;
7914}
7915
7916/**
7917 * xmlXPathIsNodeType:
Owen Taylor3473f882001-02-23 17:55:21 +00007918 * @name: a name string
7919 *
7920 * Is the name given a NodeType one.
7921 *
7922 * [38] NodeType ::= 'comment'
7923 * | 'text'
7924 * | 'processing-instruction'
7925 * | 'node'
7926 *
7927 * Returns 1 if true 0 otherwise
7928 */
7929int
7930xmlXPathIsNodeType(const xmlChar *name) {
7931 if (name == NULL)
7932 return(0);
7933
Daniel Veillard1971ee22002-01-31 20:29:19 +00007934 if (xmlStrEqual(name, BAD_CAST "node"))
Owen Taylor3473f882001-02-23 17:55:21 +00007935 return(1);
7936 if (xmlStrEqual(name, BAD_CAST "text"))
7937 return(1);
Daniel Veillard1971ee22002-01-31 20:29:19 +00007938 if (xmlStrEqual(name, BAD_CAST "comment"))
Owen Taylor3473f882001-02-23 17:55:21 +00007939 return(1);
Daniel Veillard1971ee22002-01-31 20:29:19 +00007940 if (xmlStrEqual(name, BAD_CAST "processing-instruction"))
Owen Taylor3473f882001-02-23 17:55:21 +00007941 return(1);
7942 return(0);
7943}
7944
7945/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007946 * xmlXPathCompFunctionCall:
Owen Taylor3473f882001-02-23 17:55:21 +00007947 * @ctxt: the XPath Parser context
7948 *
7949 * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')'
7950 * [17] Argument ::= Expr
7951 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007952 * Compile a function call, the evaluation of all arguments are
Owen Taylor3473f882001-02-23 17:55:21 +00007953 * pushed on the stack
7954 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007955static void
7956xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007957 xmlChar *name;
7958 xmlChar *prefix;
Owen Taylor3473f882001-02-23 17:55:21 +00007959 int nbargs = 0;
7960
7961 name = xmlXPathParseQName(ctxt, &prefix);
7962 if (name == NULL) {
7963 XP_ERROR(XPATH_EXPR_ERROR);
7964 }
7965 SKIP_BLANKS;
Owen Taylor3473f882001-02-23 17:55:21 +00007966#ifdef DEBUG_EXPR
7967 if (prefix == NULL)
7968 xmlGenericError(xmlGenericErrorContext, "Calling function %s\n",
7969 name);
7970 else
7971 xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n",
7972 prefix, name);
7973#endif
7974
Owen Taylor3473f882001-02-23 17:55:21 +00007975 if (CUR != '(') {
7976 XP_ERROR(XPATH_EXPR_ERROR);
7977 }
7978 NEXT;
7979 SKIP_BLANKS;
7980
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007981 ctxt->comp->last = -1;
Daniel Veillard71f9d732003-01-14 16:07:16 +00007982 if (CUR != ')') {
7983 while (CUR != 0) {
7984 int op1 = ctxt->comp->last;
7985 ctxt->comp->last = -1;
7986 xmlXPathCompileExpr(ctxt);
7987 CHECK_ERROR;
7988 PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0);
7989 nbargs++;
7990 if (CUR == ')') break;
7991 if (CUR != ',') {
7992 XP_ERROR(XPATH_EXPR_ERROR);
7993 }
7994 NEXT;
7995 SKIP_BLANKS;
Owen Taylor3473f882001-02-23 17:55:21 +00007996 }
Owen Taylor3473f882001-02-23 17:55:21 +00007997 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007998 PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0,
7999 name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00008000 NEXT;
8001 SKIP_BLANKS;
Owen Taylor3473f882001-02-23 17:55:21 +00008002}
8003
8004/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008005 * xmlXPathCompPrimaryExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008006 * @ctxt: the XPath Parser context
8007 *
8008 * [15] PrimaryExpr ::= VariableReference
8009 * | '(' Expr ')'
8010 * | Literal
8011 * | Number
8012 * | FunctionCall
8013 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008014 * Compile a primary expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008015 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008016static void
8017xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00008018 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008019 if (CUR == '$') xmlXPathCompVariableReference(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008020 else if (CUR == '(') {
8021 NEXT;
8022 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008023 xmlXPathCompileExpr(ctxt);
Aleksey Sanin50fe8b12002-05-07 16:21:36 +00008024 CHECK_ERROR;
Owen Taylor3473f882001-02-23 17:55:21 +00008025 if (CUR != ')') {
8026 XP_ERROR(XPATH_EXPR_ERROR);
8027 }
8028 NEXT;
8029 SKIP_BLANKS;
William M. Brackd1757ab2004-10-02 22:07:48 +00008030 } else if (IS_ASCII_DIGIT(CUR) || (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008031 xmlXPathCompNumber(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008032 } else if ((CUR == '\'') || (CUR == '"')) {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008033 xmlXPathCompLiteral(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008034 } else {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008035 xmlXPathCompFunctionCall(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008036 }
8037 SKIP_BLANKS;
8038}
8039
8040/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008041 * xmlXPathCompFilterExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008042 * @ctxt: the XPath Parser context
8043 *
8044 * [20] FilterExpr ::= PrimaryExpr
8045 * | FilterExpr Predicate
8046 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008047 * Compile a filter expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008048 * Square brackets are used to filter expressions in the same way that
8049 * they are used in location paths. It is an error if the expression to
8050 * be filtered does not evaluate to a node-set. The context node list
8051 * used for evaluating the expression in square brackets is the node-set
8052 * to be filtered listed in document order.
8053 */
8054
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008055static void
8056xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) {
8057 xmlXPathCompPrimaryExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008058 CHECK_ERROR;
8059 SKIP_BLANKS;
8060
8061 while (CUR == '[') {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008062 xmlXPathCompPredicate(ctxt, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00008063 SKIP_BLANKS;
8064 }
8065
8066
8067}
8068
8069/**
8070 * xmlXPathScanName:
8071 * @ctxt: the XPath Parser context
8072 *
8073 * Trickery: parse an XML name but without consuming the input flow
8074 * Needed to avoid insanity in the parser state.
8075 *
8076 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
8077 * CombiningChar | Extender
8078 *
8079 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
8080 *
8081 * [6] Names ::= Name (S Name)*
8082 *
8083 * Returns the Name parsed or NULL
8084 */
8085
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008086static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00008087xmlXPathScanName(xmlXPathParserContextPtr ctxt) {
Daniel Veillard03226812004-11-01 14:55:21 +00008088 int len = 0, l;
8089 int c;
Daniel Veillard03226812004-11-01 14:55:21 +00008090 const xmlChar *cur;
8091 xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00008092
Daniel Veillard03226812004-11-01 14:55:21 +00008093 cur = ctxt->cur;
8094
8095 c = CUR_CHAR(l);
8096 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
8097 (!IS_LETTER(c) && (c != '_') &&
8098 (c != ':'))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008099 return(NULL);
8100 }
8101
Daniel Veillard03226812004-11-01 14:55:21 +00008102 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
8103 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
8104 (c == '.') || (c == '-') ||
8105 (c == '_') || (c == ':') ||
8106 (IS_COMBINING(c)) ||
8107 (IS_EXTENDER(c)))) {
8108 len += l;
8109 NEXTL(l);
8110 c = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00008111 }
Daniel Veillard03226812004-11-01 14:55:21 +00008112 ret = xmlStrndup(cur, ctxt->cur - cur);
8113 ctxt->cur = cur;
8114 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00008115}
8116
8117/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008118 * xmlXPathCompPathExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008119 * @ctxt: the XPath Parser context
8120 *
8121 * [19] PathExpr ::= LocationPath
8122 * | FilterExpr
8123 * | FilterExpr '/' RelativeLocationPath
8124 * | FilterExpr '//' RelativeLocationPath
8125 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008126 * Compile a path expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008127 * The / operator and // operators combine an arbitrary expression
8128 * and a relative location path. It is an error if the expression
8129 * does not evaluate to a node-set.
8130 * The / operator does composition in the same way as when / is
8131 * used in a location path. As in location paths, // is short for
8132 * /descendant-or-self::node()/.
8133 */
8134
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008135static void
8136xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00008137 int lc = 1; /* Should we branch to LocationPath ? */
8138 xmlChar *name = NULL; /* we may have to preparse a name to find out */
8139
8140 SKIP_BLANKS;
William M. Brackd1757ab2004-10-02 22:07:48 +00008141 if ((CUR == '$') || (CUR == '(') ||
8142 (IS_ASCII_DIGIT(CUR)) ||
8143 (CUR == '\'') || (CUR == '"') ||
8144 (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008145 lc = 0;
8146 } else if (CUR == '*') {
8147 /* relative or absolute location path */
8148 lc = 1;
8149 } else if (CUR == '/') {
8150 /* relative or absolute location path */
8151 lc = 1;
8152 } else if (CUR == '@') {
8153 /* relative abbreviated attribute location path */
8154 lc = 1;
8155 } else if (CUR == '.') {
8156 /* relative abbreviated attribute location path */
8157 lc = 1;
8158 } else {
8159 /*
8160 * Problem is finding if we have a name here whether it's:
8161 * - a nodetype
8162 * - a function call in which case it's followed by '('
8163 * - an axis in which case it's followed by ':'
8164 * - a element name
8165 * We do an a priori analysis here rather than having to
8166 * maintain parsed token content through the recursive function
William M. Brack08171912003-12-29 02:52:11 +00008167 * calls. This looks uglier but makes the code easier to
Owen Taylor3473f882001-02-23 17:55:21 +00008168 * read/write/debug.
8169 */
8170 SKIP_BLANKS;
8171 name = xmlXPathScanName(ctxt);
8172 if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) {
8173#ifdef DEBUG_STEP
8174 xmlGenericError(xmlGenericErrorContext,
8175 "PathExpr: Axis\n");
8176#endif
8177 lc = 1;
8178 xmlFree(name);
8179 } else if (name != NULL) {
8180 int len =xmlStrlen(name);
Owen Taylor3473f882001-02-23 17:55:21 +00008181
8182
8183 while (NXT(len) != 0) {
8184 if (NXT(len) == '/') {
8185 /* element name */
8186#ifdef DEBUG_STEP
8187 xmlGenericError(xmlGenericErrorContext,
8188 "PathExpr: AbbrRelLocation\n");
8189#endif
8190 lc = 1;
8191 break;
William M. Brack76e95df2003-10-18 16:20:14 +00008192 } else if (IS_BLANK_CH(NXT(len))) {
William M. Brack78637da2003-07-31 14:47:38 +00008193 /* ignore blanks */
8194 ;
Owen Taylor3473f882001-02-23 17:55:21 +00008195 } else if (NXT(len) == ':') {
8196#ifdef DEBUG_STEP
8197 xmlGenericError(xmlGenericErrorContext,
8198 "PathExpr: AbbrRelLocation\n");
8199#endif
8200 lc = 1;
8201 break;
8202 } else if ((NXT(len) == '(')) {
8203 /* Note Type or Function */
8204 if (xmlXPathIsNodeType(name)) {
8205#ifdef DEBUG_STEP
8206 xmlGenericError(xmlGenericErrorContext,
8207 "PathExpr: Type search\n");
8208#endif
8209 lc = 1;
8210 } else {
8211#ifdef DEBUG_STEP
8212 xmlGenericError(xmlGenericErrorContext,
8213 "PathExpr: function call\n");
8214#endif
8215 lc = 0;
8216 }
8217 break;
8218 } else if ((NXT(len) == '[')) {
8219 /* element name */
8220#ifdef DEBUG_STEP
8221 xmlGenericError(xmlGenericErrorContext,
8222 "PathExpr: AbbrRelLocation\n");
8223#endif
8224 lc = 1;
8225 break;
8226 } else if ((NXT(len) == '<') || (NXT(len) == '>') ||
8227 (NXT(len) == '=')) {
8228 lc = 1;
8229 break;
8230 } else {
8231 lc = 1;
8232 break;
8233 }
8234 len++;
8235 }
8236 if (NXT(len) == 0) {
8237#ifdef DEBUG_STEP
8238 xmlGenericError(xmlGenericErrorContext,
8239 "PathExpr: AbbrRelLocation\n");
8240#endif
8241 /* element name */
8242 lc = 1;
8243 }
8244 xmlFree(name);
8245 } else {
William M. Brack08171912003-12-29 02:52:11 +00008246 /* make sure all cases are covered explicitly */
Owen Taylor3473f882001-02-23 17:55:21 +00008247 XP_ERROR(XPATH_EXPR_ERROR);
8248 }
8249 }
8250
8251 if (lc) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008252 if (CUR == '/') {
8253 PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0);
8254 } else {
8255 PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008256 }
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008257 xmlXPathCompLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008258 } else {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008259 xmlXPathCompFilterExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008260 CHECK_ERROR;
8261 if ((CUR == '/') && (NXT(1) == '/')) {
8262 SKIP(2);
8263 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008264
8265 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
8266 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
8267 PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0);
8268
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008269 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008270 } else if (CUR == '/') {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008271 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008272 }
8273 }
8274 SKIP_BLANKS;
8275}
8276
8277/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008278 * xmlXPathCompUnionExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008279 * @ctxt: the XPath Parser context
8280 *
8281 * [18] UnionExpr ::= PathExpr
8282 * | UnionExpr '|' PathExpr
8283 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008284 * Compile an union expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008285 */
8286
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008287static void
8288xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) {
8289 xmlXPathCompPathExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008290 CHECK_ERROR;
8291 SKIP_BLANKS;
8292 while (CUR == '|') {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008293 int op1 = ctxt->comp->last;
8294 PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008295
8296 NEXT;
8297 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008298 xmlXPathCompPathExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008299
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008300 PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0);
8301
Owen Taylor3473f882001-02-23 17:55:21 +00008302 SKIP_BLANKS;
8303 }
Owen Taylor3473f882001-02-23 17:55:21 +00008304}
8305
8306/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008307 * xmlXPathCompUnaryExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008308 * @ctxt: the XPath Parser context
8309 *
8310 * [27] UnaryExpr ::= UnionExpr
8311 * | '-' UnaryExpr
8312 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008313 * Compile an unary expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008314 */
8315
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008316static void
8317xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00008318 int minus = 0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008319 int found = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008320
8321 SKIP_BLANKS;
Daniel Veillard68d7b672001-03-12 18:22:04 +00008322 while (CUR == '-') {
8323 minus = 1 - minus;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008324 found = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00008325 NEXT;
8326 SKIP_BLANKS;
8327 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008328
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008329 xmlXPathCompUnionExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008330 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008331 if (found) {
8332 if (minus)
8333 PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0);
8334 else
8335 PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008336 }
8337}
8338
8339/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008340 * xmlXPathCompMultiplicativeExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008341 * @ctxt: the XPath Parser context
8342 *
8343 * [26] MultiplicativeExpr ::= UnaryExpr
8344 * | MultiplicativeExpr MultiplyOperator UnaryExpr
8345 * | MultiplicativeExpr 'div' UnaryExpr
8346 * | MultiplicativeExpr 'mod' UnaryExpr
8347 * [34] MultiplyOperator ::= '*'
8348 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008349 * Compile an Additive expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008350 */
8351
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008352static void
8353xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) {
8354 xmlXPathCompUnaryExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008355 CHECK_ERROR;
8356 SKIP_BLANKS;
8357 while ((CUR == '*') ||
8358 ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) ||
8359 ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) {
8360 int op = -1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008361 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008362
8363 if (CUR == '*') {
8364 op = 0;
8365 NEXT;
8366 } else if (CUR == 'd') {
8367 op = 1;
8368 SKIP(3);
8369 } else if (CUR == 'm') {
8370 op = 2;
8371 SKIP(3);
8372 }
8373 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008374 xmlXPathCompUnaryExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008375 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008376 PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008377 SKIP_BLANKS;
8378 }
8379}
8380
8381/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008382 * xmlXPathCompAdditiveExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008383 * @ctxt: the XPath Parser context
8384 *
8385 * [25] AdditiveExpr ::= MultiplicativeExpr
8386 * | AdditiveExpr '+' MultiplicativeExpr
8387 * | AdditiveExpr '-' MultiplicativeExpr
8388 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008389 * Compile an Additive expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008390 */
8391
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008392static void
8393xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008394
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008395 xmlXPathCompMultiplicativeExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008396 CHECK_ERROR;
8397 SKIP_BLANKS;
8398 while ((CUR == '+') || (CUR == '-')) {
8399 int plus;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008400 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008401
8402 if (CUR == '+') plus = 1;
8403 else plus = 0;
8404 NEXT;
8405 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008406 xmlXPathCompMultiplicativeExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008407 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008408 PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008409 SKIP_BLANKS;
8410 }
8411}
8412
8413/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008414 * xmlXPathCompRelationalExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008415 * @ctxt: the XPath Parser context
8416 *
8417 * [24] RelationalExpr ::= AdditiveExpr
8418 * | RelationalExpr '<' AdditiveExpr
8419 * | RelationalExpr '>' AdditiveExpr
8420 * | RelationalExpr '<=' AdditiveExpr
8421 * | RelationalExpr '>=' AdditiveExpr
8422 *
8423 * A <= B > C is allowed ? Answer from James, yes with
8424 * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr
8425 * which is basically what got implemented.
8426 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008427 * Compile a Relational expression, then push the result
Owen Taylor3473f882001-02-23 17:55:21 +00008428 * on the stack
8429 */
8430
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008431static void
8432xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) {
8433 xmlXPathCompAdditiveExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008434 CHECK_ERROR;
8435 SKIP_BLANKS;
8436 while ((CUR == '<') ||
8437 (CUR == '>') ||
8438 ((CUR == '<') && (NXT(1) == '=')) ||
8439 ((CUR == '>') && (NXT(1) == '='))) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008440 int inf, strict;
8441 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008442
8443 if (CUR == '<') inf = 1;
8444 else inf = 0;
8445 if (NXT(1) == '=') strict = 0;
8446 else strict = 1;
8447 NEXT;
8448 if (!strict) NEXT;
8449 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008450 xmlXPathCompAdditiveExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008451 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008452 PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict);
Owen Taylor3473f882001-02-23 17:55:21 +00008453 SKIP_BLANKS;
8454 }
8455}
8456
8457/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008458 * xmlXPathCompEqualityExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008459 * @ctxt: the XPath Parser context
8460 *
8461 * [23] EqualityExpr ::= RelationalExpr
8462 * | EqualityExpr '=' RelationalExpr
8463 * | EqualityExpr '!=' RelationalExpr
8464 *
8465 * A != B != C is allowed ? Answer from James, yes with
8466 * (RelationalExpr = RelationalExpr) = RelationalExpr
8467 * (RelationalExpr != RelationalExpr) != RelationalExpr
8468 * which is basically what got implemented.
8469 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008470 * Compile an Equality expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008471 *
8472 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008473static void
8474xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) {
8475 xmlXPathCompRelationalExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008476 CHECK_ERROR;
8477 SKIP_BLANKS;
8478 while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008479 int eq;
8480 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008481
8482 if (CUR == '=') eq = 1;
8483 else eq = 0;
8484 NEXT;
8485 if (!eq) NEXT;
8486 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008487 xmlXPathCompRelationalExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008488 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008489 PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008490 SKIP_BLANKS;
8491 }
8492}
8493
8494/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008495 * xmlXPathCompAndExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008496 * @ctxt: the XPath Parser context
8497 *
8498 * [22] AndExpr ::= EqualityExpr
8499 * | AndExpr 'and' EqualityExpr
8500 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008501 * Compile an AND expression.
Owen Taylor3473f882001-02-23 17:55:21 +00008502 *
8503 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008504static void
8505xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) {
8506 xmlXPathCompEqualityExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008507 CHECK_ERROR;
8508 SKIP_BLANKS;
8509 while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008510 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008511 SKIP(3);
8512 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008513 xmlXPathCompEqualityExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008514 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008515 PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008516 SKIP_BLANKS;
8517 }
8518}
8519
8520/**
Daniel Veillard591b4be2003-02-09 23:33:36 +00008521 * xmlXPathCompileExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00008522 * @ctxt: the XPath Parser context
8523 *
8524 * [14] Expr ::= OrExpr
8525 * [21] OrExpr ::= AndExpr
8526 * | OrExpr 'or' AndExpr
8527 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008528 * Parse and compile an expression
Owen Taylor3473f882001-02-23 17:55:21 +00008529 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008530static void
8531xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt) {
8532 xmlXPathCompAndExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008533 CHECK_ERROR;
8534 SKIP_BLANKS;
8535 while ((CUR == 'o') && (NXT(1) == 'r')) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008536 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008537 SKIP(2);
8538 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008539 xmlXPathCompAndExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008540 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008541 PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0);
8542 op1 = ctxt->comp->nbStep;
Owen Taylor3473f882001-02-23 17:55:21 +00008543 SKIP_BLANKS;
8544 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008545 if (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE) {
8546 /* more ops could be optimized too */
8547 PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0);
8548 }
Owen Taylor3473f882001-02-23 17:55:21 +00008549}
8550
8551/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008552 * xmlXPathCompPredicate:
Owen Taylor3473f882001-02-23 17:55:21 +00008553 * @ctxt: the XPath Parser context
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008554 * @filter: act as a filter
Owen Taylor3473f882001-02-23 17:55:21 +00008555 *
8556 * [8] Predicate ::= '[' PredicateExpr ']'
8557 * [9] PredicateExpr ::= Expr
8558 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008559 * Compile a predicate expression
Owen Taylor3473f882001-02-23 17:55:21 +00008560 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008561static void
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008562xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008563 int op1 = ctxt->comp->last;
8564
8565 SKIP_BLANKS;
8566 if (CUR != '[') {
8567 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
8568 }
8569 NEXT;
8570 SKIP_BLANKS;
8571
8572 ctxt->comp->last = -1;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008573 xmlXPathCompileExpr(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008574 CHECK_ERROR;
8575
8576 if (CUR != ']') {
8577 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
8578 }
8579
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008580 if (filter)
8581 PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0);
8582 else
8583 PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0);
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008584
8585 NEXT;
8586 SKIP_BLANKS;
8587}
8588
8589/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008590 * xmlXPathCompNodeTest:
Owen Taylor3473f882001-02-23 17:55:21 +00008591 * @ctxt: the XPath Parser context
8592 * @test: pointer to a xmlXPathTestVal
8593 * @type: pointer to a xmlXPathTypeVal
8594 * @prefix: placeholder for a possible name prefix
8595 *
8596 * [7] NodeTest ::= NameTest
8597 * | NodeType '(' ')'
8598 * | 'processing-instruction' '(' Literal ')'
8599 *
8600 * [37] NameTest ::= '*'
8601 * | NCName ':' '*'
8602 * | QName
8603 * [38] NodeType ::= 'comment'
8604 * | 'text'
8605 * | 'processing-instruction'
8606 * | 'node'
8607 *
William M. Brack08171912003-12-29 02:52:11 +00008608 * Returns the name found and updates @test, @type and @prefix appropriately
Owen Taylor3473f882001-02-23 17:55:21 +00008609 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008610static xmlChar *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008611xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test,
8612 xmlXPathTypeVal *type, const xmlChar **prefix,
8613 xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00008614 int blanks;
8615
8616 if ((test == NULL) || (type == NULL) || (prefix == NULL)) {
8617 STRANGE;
8618 return(NULL);
8619 }
William M. Brack78637da2003-07-31 14:47:38 +00008620 *type = (xmlXPathTypeVal) 0;
8621 *test = (xmlXPathTestVal) 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008622 *prefix = NULL;
8623 SKIP_BLANKS;
8624
8625 if ((name == NULL) && (CUR == '*')) {
8626 /*
8627 * All elements
8628 */
8629 NEXT;
8630 *test = NODE_TEST_ALL;
8631 return(NULL);
8632 }
8633
8634 if (name == NULL)
8635 name = xmlXPathParseNCName(ctxt);
8636 if (name == NULL) {
8637 XP_ERROR0(XPATH_EXPR_ERROR);
8638 }
8639
William M. Brack76e95df2003-10-18 16:20:14 +00008640 blanks = IS_BLANK_CH(CUR);
Owen Taylor3473f882001-02-23 17:55:21 +00008641 SKIP_BLANKS;
8642 if (CUR == '(') {
8643 NEXT;
8644 /*
8645 * NodeType or PI search
8646 */
8647 if (xmlStrEqual(name, BAD_CAST "comment"))
8648 *type = NODE_TYPE_COMMENT;
8649 else if (xmlStrEqual(name, BAD_CAST "node"))
8650 *type = NODE_TYPE_NODE;
8651 else if (xmlStrEqual(name, BAD_CAST "processing-instruction"))
8652 *type = NODE_TYPE_PI;
8653 else if (xmlStrEqual(name, BAD_CAST "text"))
8654 *type = NODE_TYPE_TEXT;
8655 else {
8656 if (name != NULL)
8657 xmlFree(name);
8658 XP_ERROR0(XPATH_EXPR_ERROR);
8659 }
8660
8661 *test = NODE_TEST_TYPE;
8662
8663 SKIP_BLANKS;
8664 if (*type == NODE_TYPE_PI) {
8665 /*
8666 * Specific case: search a PI by name.
8667 */
Owen Taylor3473f882001-02-23 17:55:21 +00008668 if (name != NULL)
8669 xmlFree(name);
Daniel Veillard82e49712001-04-26 14:38:03 +00008670 name = NULL;
8671 if (CUR != ')') {
8672 name = xmlXPathParseLiteral(ctxt);
8673 CHECK_ERROR 0;
Daniel Veillarded23b7d2002-05-27 12:16:02 +00008674 *test = NODE_TEST_PI;
Daniel Veillard82e49712001-04-26 14:38:03 +00008675 SKIP_BLANKS;
8676 }
Owen Taylor3473f882001-02-23 17:55:21 +00008677 }
8678 if (CUR != ')') {
8679 if (name != NULL)
8680 xmlFree(name);
8681 XP_ERROR0(XPATH_UNCLOSED_ERROR);
8682 }
8683 NEXT;
8684 return(name);
8685 }
8686 *test = NODE_TEST_NAME;
8687 if ((!blanks) && (CUR == ':')) {
8688 NEXT;
8689
8690 /*
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008691 * Since currently the parser context don't have a
8692 * namespace list associated:
8693 * The namespace name for this prefix can be computed
8694 * only at evaluation time. The compilation is done
8695 * outside of any context.
Owen Taylor3473f882001-02-23 17:55:21 +00008696 */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008697#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00008698 *prefix = xmlXPathNsLookup(ctxt->context, name);
8699 if (name != NULL)
8700 xmlFree(name);
8701 if (*prefix == NULL) {
8702 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
8703 }
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008704#else
8705 *prefix = name;
8706#endif
Owen Taylor3473f882001-02-23 17:55:21 +00008707
8708 if (CUR == '*') {
8709 /*
8710 * All elements
8711 */
8712 NEXT;
8713 *test = NODE_TEST_ALL;
8714 return(NULL);
8715 }
8716
8717 name = xmlXPathParseNCName(ctxt);
8718 if (name == NULL) {
8719 XP_ERROR0(XPATH_EXPR_ERROR);
8720 }
8721 }
8722 return(name);
8723}
8724
8725/**
8726 * xmlXPathIsAxisName:
8727 * @name: a preparsed name token
8728 *
8729 * [6] AxisName ::= 'ancestor'
8730 * | 'ancestor-or-self'
8731 * | 'attribute'
8732 * | 'child'
8733 * | 'descendant'
8734 * | 'descendant-or-self'
8735 * | 'following'
8736 * | 'following-sibling'
8737 * | 'namespace'
8738 * | 'parent'
8739 * | 'preceding'
8740 * | 'preceding-sibling'
8741 * | 'self'
8742 *
8743 * Returns the axis or 0
8744 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008745static xmlXPathAxisVal
Owen Taylor3473f882001-02-23 17:55:21 +00008746xmlXPathIsAxisName(const xmlChar *name) {
William M. Brack78637da2003-07-31 14:47:38 +00008747 xmlXPathAxisVal ret = (xmlXPathAxisVal) 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008748 switch (name[0]) {
8749 case 'a':
8750 if (xmlStrEqual(name, BAD_CAST "ancestor"))
8751 ret = AXIS_ANCESTOR;
8752 if (xmlStrEqual(name, BAD_CAST "ancestor-or-self"))
8753 ret = AXIS_ANCESTOR_OR_SELF;
8754 if (xmlStrEqual(name, BAD_CAST "attribute"))
8755 ret = AXIS_ATTRIBUTE;
8756 break;
8757 case 'c':
8758 if (xmlStrEqual(name, BAD_CAST "child"))
8759 ret = AXIS_CHILD;
8760 break;
8761 case 'd':
8762 if (xmlStrEqual(name, BAD_CAST "descendant"))
8763 ret = AXIS_DESCENDANT;
8764 if (xmlStrEqual(name, BAD_CAST "descendant-or-self"))
8765 ret = AXIS_DESCENDANT_OR_SELF;
8766 break;
8767 case 'f':
8768 if (xmlStrEqual(name, BAD_CAST "following"))
8769 ret = AXIS_FOLLOWING;
8770 if (xmlStrEqual(name, BAD_CAST "following-sibling"))
8771 ret = AXIS_FOLLOWING_SIBLING;
8772 break;
8773 case 'n':
8774 if (xmlStrEqual(name, BAD_CAST "namespace"))
8775 ret = AXIS_NAMESPACE;
8776 break;
8777 case 'p':
8778 if (xmlStrEqual(name, BAD_CAST "parent"))
8779 ret = AXIS_PARENT;
8780 if (xmlStrEqual(name, BAD_CAST "preceding"))
8781 ret = AXIS_PRECEDING;
8782 if (xmlStrEqual(name, BAD_CAST "preceding-sibling"))
8783 ret = AXIS_PRECEDING_SIBLING;
8784 break;
8785 case 's':
8786 if (xmlStrEqual(name, BAD_CAST "self"))
8787 ret = AXIS_SELF;
8788 break;
8789 }
8790 return(ret);
8791}
8792
8793/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008794 * xmlXPathCompStep:
Owen Taylor3473f882001-02-23 17:55:21 +00008795 * @ctxt: the XPath Parser context
8796 *
8797 * [4] Step ::= AxisSpecifier NodeTest Predicate*
8798 * | AbbreviatedStep
8799 *
8800 * [12] AbbreviatedStep ::= '.' | '..'
8801 *
8802 * [5] AxisSpecifier ::= AxisName '::'
8803 * | AbbreviatedAxisSpecifier
8804 *
8805 * [13] AbbreviatedAxisSpecifier ::= '@'?
8806 *
8807 * Modified for XPtr range support as:
8808 *
8809 * [4xptr] Step ::= AxisSpecifier NodeTest Predicate*
8810 * | AbbreviatedStep
8811 * | 'range-to' '(' Expr ')' Predicate*
8812 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008813 * Compile one step in a Location Path
Owen Taylor3473f882001-02-23 17:55:21 +00008814 * A location step of . is short for self::node(). This is
8815 * particularly useful in conjunction with //. For example, the
8816 * location path .//para is short for
8817 * self::node()/descendant-or-self::node()/child::para
8818 * and so will select all para descendant elements of the context
8819 * node.
8820 * Similarly, a location step of .. is short for parent::node().
8821 * For example, ../title is short for parent::node()/child::title
8822 * and so will select the title children of the parent of the context
8823 * node.
8824 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008825static void
8826xmlXPathCompStep(xmlXPathParserContextPtr ctxt) {
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008827#ifdef LIBXML_XPTR_ENABLED
8828 int rangeto = 0;
8829 int op2 = -1;
8830#endif
8831
Owen Taylor3473f882001-02-23 17:55:21 +00008832 SKIP_BLANKS;
8833 if ((CUR == '.') && (NXT(1) == '.')) {
8834 SKIP(2);
8835 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008836 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT,
8837 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008838 } else if (CUR == '.') {
8839 NEXT;
8840 SKIP_BLANKS;
8841 } else {
8842 xmlChar *name = NULL;
8843 const xmlChar *prefix = NULL;
8844 xmlXPathTestVal test;
William M. Brack78637da2003-07-31 14:47:38 +00008845 xmlXPathAxisVal axis = (xmlXPathAxisVal) 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008846 xmlXPathTypeVal type;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008847 int op1;
Owen Taylor3473f882001-02-23 17:55:21 +00008848
8849 /*
8850 * The modification needed for XPointer change to the production
8851 */
8852#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008853 if (ctxt->xptr) {
Owen Taylor3473f882001-02-23 17:55:21 +00008854 name = xmlXPathParseNCName(ctxt);
8855 if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) {
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008856 op2 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008857 xmlFree(name);
8858 SKIP_BLANKS;
8859 if (CUR != '(') {
8860 XP_ERROR(XPATH_EXPR_ERROR);
8861 }
8862 NEXT;
8863 SKIP_BLANKS;
8864
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008865 xmlXPathCompileExpr(ctxt);
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008866 /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */
Owen Taylor3473f882001-02-23 17:55:21 +00008867 CHECK_ERROR;
8868
8869 SKIP_BLANKS;
8870 if (CUR != ')') {
8871 XP_ERROR(XPATH_EXPR_ERROR);
8872 }
8873 NEXT;
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008874 rangeto = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00008875 goto eval_predicates;
8876 }
8877 }
8878#endif
Daniel Veillard2156a562001-04-28 12:24:34 +00008879 if (CUR == '*') {
8880 axis = AXIS_CHILD;
8881 } else {
8882 if (name == NULL)
8883 name = xmlXPathParseNCName(ctxt);
8884 if (name != NULL) {
8885 axis = xmlXPathIsAxisName(name);
8886 if (axis != 0) {
8887 SKIP_BLANKS;
8888 if ((CUR == ':') && (NXT(1) == ':')) {
8889 SKIP(2);
8890 xmlFree(name);
8891 name = NULL;
8892 } else {
8893 /* an element name can conflict with an axis one :-\ */
8894 axis = AXIS_CHILD;
8895 }
Owen Taylor3473f882001-02-23 17:55:21 +00008896 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00008897 axis = AXIS_CHILD;
8898 }
Daniel Veillard2156a562001-04-28 12:24:34 +00008899 } else if (CUR == '@') {
8900 NEXT;
8901 axis = AXIS_ATTRIBUTE;
Owen Taylor3473f882001-02-23 17:55:21 +00008902 } else {
Daniel Veillard2156a562001-04-28 12:24:34 +00008903 axis = AXIS_CHILD;
Owen Taylor3473f882001-02-23 17:55:21 +00008904 }
Owen Taylor3473f882001-02-23 17:55:21 +00008905 }
8906
8907 CHECK_ERROR;
8908
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008909 name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name);
Owen Taylor3473f882001-02-23 17:55:21 +00008910 if (test == 0)
8911 return;
8912
8913#ifdef DEBUG_STEP
8914 xmlGenericError(xmlGenericErrorContext,
8915 "Basis : computing new set\n");
8916#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008917
Owen Taylor3473f882001-02-23 17:55:21 +00008918#ifdef DEBUG_STEP
8919 xmlGenericError(xmlGenericErrorContext, "Basis : ");
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008920 if (ctxt->value == NULL)
8921 xmlGenericError(xmlGenericErrorContext, "no value\n");
8922 else if (ctxt->value->nodesetval == NULL)
8923 xmlGenericError(xmlGenericErrorContext, "Empty\n");
8924 else
8925 xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval);
Owen Taylor3473f882001-02-23 17:55:21 +00008926#endif
Owen Taylor3473f882001-02-23 17:55:21 +00008927
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00008928#ifdef LIBXML_XPTR_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00008929eval_predicates:
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00008930#endif
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008931 op1 = ctxt->comp->last;
8932 ctxt->comp->last = -1;
8933
Owen Taylor3473f882001-02-23 17:55:21 +00008934 SKIP_BLANKS;
8935 while (CUR == '[') {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008936 xmlXPathCompPredicate(ctxt, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008937 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008938
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008939#ifdef LIBXML_XPTR_ENABLED
8940 if (rangeto) {
8941 PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0);
8942 } else
8943#endif
8944 PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis,
8945 test, type, (void *)prefix, (void *)name);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008946
Owen Taylor3473f882001-02-23 17:55:21 +00008947 }
8948#ifdef DEBUG_STEP
8949 xmlGenericError(xmlGenericErrorContext, "Step : ");
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008950 if (ctxt->value == NULL)
8951 xmlGenericError(xmlGenericErrorContext, "no value\n");
8952 else if (ctxt->value->nodesetval == NULL)
8953 xmlGenericError(xmlGenericErrorContext, "Empty\n");
8954 else
8955 xmlGenericErrorContextNodeSet(xmlGenericErrorContext,
8956 ctxt->value->nodesetval);
Owen Taylor3473f882001-02-23 17:55:21 +00008957#endif
8958}
8959
8960/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008961 * xmlXPathCompRelativeLocationPath:
Owen Taylor3473f882001-02-23 17:55:21 +00008962 * @ctxt: the XPath Parser context
8963 *
8964 * [3] RelativeLocationPath ::= Step
8965 * | RelativeLocationPath '/' Step
8966 * | AbbreviatedRelativeLocationPath
8967 * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step
8968 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008969 * Compile a relative location path.
Owen Taylor3473f882001-02-23 17:55:21 +00008970 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008971static void
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008972xmlXPathCompRelativeLocationPath
Owen Taylor3473f882001-02-23 17:55:21 +00008973(xmlXPathParserContextPtr ctxt) {
8974 SKIP_BLANKS;
8975 if ((CUR == '/') && (NXT(1) == '/')) {
8976 SKIP(2);
8977 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008978 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
8979 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008980 } else if (CUR == '/') {
8981 NEXT;
8982 SKIP_BLANKS;
8983 }
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008984 xmlXPathCompStep(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008985 SKIP_BLANKS;
8986 while (CUR == '/') {
8987 if ((CUR == '/') && (NXT(1) == '/')) {
8988 SKIP(2);
8989 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008990 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
Owen Taylor3473f882001-02-23 17:55:21 +00008991 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008992 xmlXPathCompStep(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008993 } else if (CUR == '/') {
8994 NEXT;
8995 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008996 xmlXPathCompStep(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008997 }
8998 SKIP_BLANKS;
8999 }
9000}
9001
9002/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00009003 * xmlXPathCompLocationPath:
Owen Taylor3473f882001-02-23 17:55:21 +00009004 * @ctxt: the XPath Parser context
9005 *
9006 * [1] LocationPath ::= RelativeLocationPath
9007 * | AbsoluteLocationPath
9008 * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath?
9009 * | AbbreviatedAbsoluteLocationPath
9010 * [10] AbbreviatedAbsoluteLocationPath ::=
9011 * '//' RelativeLocationPath
9012 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00009013 * Compile a location path
9014 *
Owen Taylor3473f882001-02-23 17:55:21 +00009015 * // is short for /descendant-or-self::node()/. For example,
9016 * //para is short for /descendant-or-self::node()/child::para and
9017 * so will select any para element in the document (even a para element
9018 * that is a document element will be selected by //para since the
9019 * document element node is a child of the root node); div//para is
9020 * short for div/descendant-or-self::node()/child::para and so will
9021 * select all para descendants of div children.
9022 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00009023static void
9024xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00009025 SKIP_BLANKS;
9026 if (CUR != '/') {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00009027 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009028 } else {
9029 while (CUR == '/') {
9030 if ((CUR == '/') && (NXT(1) == '/')) {
9031 SKIP(2);
9032 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009033 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
9034 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00009035 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009036 } else if (CUR == '/') {
9037 NEXT;
Daniel Veillard608ad072001-06-14 08:32:28 +00009038 SKIP_BLANKS;
9039 if ((CUR != 0 ) &&
William M. Brackd1757ab2004-10-02 22:07:48 +00009040 ((IS_ASCII_LETTER(CUR)) || (CUR == '_') || (CUR == '.') ||
Daniel Veillard608ad072001-06-14 08:32:28 +00009041 (CUR == '@') || (CUR == '*')))
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00009042 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009043 }
9044 }
9045 }
9046}
9047
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009048/************************************************************************
9049 * *
9050 * XPath precompiled expression evaluation *
9051 * *
9052 ************************************************************************/
9053
Daniel Veillardf06307e2001-07-03 10:35:50 +00009054static int
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009055xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op);
9056
9057/**
9058 * xmlXPathNodeCollectAndTest:
9059 * @ctxt: the XPath Parser context
9060 * @op: the XPath precompiled step operation
Daniel Veillardf06307e2001-07-03 10:35:50 +00009061 * @first: pointer to the first element in document order
9062 * @last: pointer to the last element in document order
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009063 *
9064 * This is the function implementing a step: based on the current list
9065 * of nodes, it builds up a new list, looking at all nodes under that
William M. Brack08171912003-12-29 02:52:11 +00009066 * axis and selecting them. It also does the predicate filtering
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009067 *
9068 * Pushes the new NodeSet resulting from the search.
Daniel Veillardf06307e2001-07-03 10:35:50 +00009069 *
William M. Brack08171912003-12-29 02:52:11 +00009070 * Returns the number of nodes traversed
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009071 */
Daniel Veillardf06307e2001-07-03 10:35:50 +00009072static int
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009073xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt,
Daniel Veillardf06307e2001-07-03 10:35:50 +00009074 xmlXPathStepOpPtr op,
9075 xmlNodePtr * first, xmlNodePtr * last)
9076{
William M. Brack78637da2003-07-31 14:47:38 +00009077 xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value;
9078 xmlXPathTestVal test = (xmlXPathTestVal) op->value2;
9079 xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009080 const xmlChar *prefix = op->value4;
9081 const xmlChar *name = op->value5;
Daniel Veillarde043ee12001-04-16 14:08:07 +00009082 const xmlChar *URI = NULL;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009083
9084#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009085 int n = 0;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009086#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009087 int i, t = 0;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009088 xmlNodeSetPtr ret, list;
9089 xmlXPathTraversalFunction next = NULL;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009090 void (*addNode) (xmlNodeSetPtr, xmlNodePtr);
Daniel Veillard75be0132002-03-13 10:03:35 +00009091 xmlNodeSetPtr (*mergeNodeSet) (xmlNodeSetPtr, xmlNodeSetPtr);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009092 xmlNodePtr cur = NULL;
9093 xmlXPathObjectPtr obj;
9094 xmlNodeSetPtr nodelist;
9095 xmlNodePtr tmp;
9096
Daniel Veillardf06307e2001-07-03 10:35:50 +00009097 CHECK_TYPE0(XPATH_NODESET);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009098 obj = valuePop(ctxt);
9099 addNode = xmlXPathNodeSetAdd;
Daniel Veillard75be0132002-03-13 10:03:35 +00009100 mergeNodeSet = xmlXPathNodeSetMerge;
Daniel Veillarde043ee12001-04-16 14:08:07 +00009101 if (prefix != NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009102 URI = xmlXPathNsLookup(ctxt->context, prefix);
William M. Brack2c19a7b2005-04-10 01:03:23 +00009103 if (URI == NULL) {
9104 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009105 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
William M. Brack2c19a7b2005-04-10 01:03:23 +00009106 }
Daniel Veillarde043ee12001-04-16 14:08:07 +00009107 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009108#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009109 xmlGenericError(xmlGenericErrorContext, "new step : ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009110#endif
9111 switch (axis) {
9112 case AXIS_ANCESTOR:
9113#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009114 xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009115#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009116 first = NULL;
9117 next = xmlXPathNextAncestor;
9118 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009119 case AXIS_ANCESTOR_OR_SELF:
9120#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009121 xmlGenericError(xmlGenericErrorContext,
9122 "axis 'ancestors-or-self' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009123#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009124 first = NULL;
9125 next = xmlXPathNextAncestorOrSelf;
9126 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009127 case AXIS_ATTRIBUTE:
9128#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009129 xmlGenericError(xmlGenericErrorContext, "axis 'attributes' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009130#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009131 first = NULL;
9132 last = NULL;
9133 next = xmlXPathNextAttribute;
Daniel Veillard75be0132002-03-13 10:03:35 +00009134 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009135 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009136 case AXIS_CHILD:
9137#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009138 xmlGenericError(xmlGenericErrorContext, "axis 'child' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009139#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009140 last = NULL;
9141 next = xmlXPathNextChild;
Daniel Veillard75be0132002-03-13 10:03:35 +00009142 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009143 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009144 case AXIS_DESCENDANT:
9145#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009146 xmlGenericError(xmlGenericErrorContext, "axis 'descendant' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009147#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009148 last = NULL;
9149 next = xmlXPathNextDescendant;
9150 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009151 case AXIS_DESCENDANT_OR_SELF:
9152#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009153 xmlGenericError(xmlGenericErrorContext,
9154 "axis 'descendant-or-self' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009155#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009156 last = NULL;
9157 next = xmlXPathNextDescendantOrSelf;
9158 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009159 case AXIS_FOLLOWING:
9160#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009161 xmlGenericError(xmlGenericErrorContext, "axis 'following' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009162#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009163 last = NULL;
9164 next = xmlXPathNextFollowing;
9165 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009166 case AXIS_FOLLOWING_SIBLING:
9167#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009168 xmlGenericError(xmlGenericErrorContext,
9169 "axis 'following-siblings' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009170#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009171 last = NULL;
9172 next = xmlXPathNextFollowingSibling;
9173 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009174 case AXIS_NAMESPACE:
9175#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009176 xmlGenericError(xmlGenericErrorContext, "axis 'namespace' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009177#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009178 first = NULL;
9179 last = NULL;
9180 next = (xmlXPathTraversalFunction) xmlXPathNextNamespace;
Daniel Veillard75be0132002-03-13 10:03:35 +00009181 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009182 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009183 case AXIS_PARENT:
9184#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009185 xmlGenericError(xmlGenericErrorContext, "axis 'parent' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009186#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009187 first = NULL;
9188 next = xmlXPathNextParent;
9189 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009190 case AXIS_PRECEDING:
9191#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009192 xmlGenericError(xmlGenericErrorContext, "axis 'preceding' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009193#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009194 first = NULL;
9195 next = xmlXPathNextPrecedingInternal;
9196 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009197 case AXIS_PRECEDING_SIBLING:
9198#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009199 xmlGenericError(xmlGenericErrorContext,
9200 "axis 'preceding-sibling' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009201#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009202 first = NULL;
9203 next = xmlXPathNextPrecedingSibling;
9204 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009205 case AXIS_SELF:
9206#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009207 xmlGenericError(xmlGenericErrorContext, "axis 'self' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009208#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009209 first = NULL;
9210 last = NULL;
9211 next = xmlXPathNextSelf;
Daniel Veillard75be0132002-03-13 10:03:35 +00009212 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009213 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009214 }
William M. Brack2c19a7b2005-04-10 01:03:23 +00009215 if (next == NULL) {
9216 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009217 return(0);
William M. Brack2c19a7b2005-04-10 01:03:23 +00009218 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009219
9220 nodelist = obj->nodesetval;
9221 if (nodelist == NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009222 xmlXPathFreeObject(obj);
9223 valuePush(ctxt, xmlXPathWrapNodeSet(NULL));
9224 return(0);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009225 }
9226 addNode = xmlXPathNodeSetAddUnique;
9227 ret = NULL;
9228#ifdef DEBUG_STEP
9229 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf06307e2001-07-03 10:35:50 +00009230 " context contains %d nodes\n", nodelist->nodeNr);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009231 switch (test) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009232 case NODE_TEST_NONE:
9233 xmlGenericError(xmlGenericErrorContext,
9234 " searching for none !!!\n");
9235 break;
9236 case NODE_TEST_TYPE:
9237 xmlGenericError(xmlGenericErrorContext,
9238 " searching for type %d\n", type);
9239 break;
9240 case NODE_TEST_PI:
9241 xmlGenericError(xmlGenericErrorContext,
9242 " searching for PI !!!\n");
9243 break;
9244 case NODE_TEST_ALL:
9245 xmlGenericError(xmlGenericErrorContext,
9246 " searching for *\n");
9247 break;
9248 case NODE_TEST_NS:
9249 xmlGenericError(xmlGenericErrorContext,
9250 " searching for namespace %s\n",
9251 prefix);
9252 break;
9253 case NODE_TEST_NAME:
9254 xmlGenericError(xmlGenericErrorContext,
9255 " searching for name %s\n", name);
9256 if (prefix != NULL)
9257 xmlGenericError(xmlGenericErrorContext,
9258 " with namespace %s\n", prefix);
9259 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009260 }
9261 xmlGenericError(xmlGenericErrorContext, "Testing : ");
9262#endif
9263 /*
9264 * 2.3 Node Tests
9265 * - For the attribute axis, the principal node type is attribute.
9266 * - For the namespace axis, the principal node type is namespace.
9267 * - For other axes, the principal node type is element.
9268 *
9269 * A node test * is true for any node of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009270 * principal node type. For example, child::* will
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009271 * select all element children of the context node
9272 */
9273 tmp = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009274 for (i = 0; i < nodelist->nodeNr; i++) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009275 ctxt->context->node = nodelist->nodeTab[i];
9276
Daniel Veillardf06307e2001-07-03 10:35:50 +00009277 cur = NULL;
9278 list = xmlXPathNodeSetCreate(NULL);
9279 do {
9280 cur = next(ctxt, cur);
9281 if (cur == NULL)
9282 break;
9283 if ((first != NULL) && (*first == cur))
9284 break;
9285 if (((t % 256) == 0) &&
9286 (first != NULL) && (*first != NULL) &&
9287 (xmlXPathCmpNodes(*first, cur) >= 0))
9288 break;
9289 if ((last != NULL) && (*last == cur))
9290 break;
9291 if (((t % 256) == 0) &&
9292 (last != NULL) && (*last != NULL) &&
9293 (xmlXPathCmpNodes(cur, *last) >= 0))
9294 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009295 t++;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009296#ifdef DEBUG_STEP
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009297 xmlGenericError(xmlGenericErrorContext, " %s", cur->name);
9298#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009299 switch (test) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009300 case NODE_TEST_NONE:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009301 ctxt->context->node = tmp;
William M. Brack2c19a7b2005-04-10 01:03:23 +00009302 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009303 STRANGE return(t);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009304 case NODE_TEST_TYPE:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009305 if ((cur->type == type) ||
9306 ((type == NODE_TYPE_NODE) &&
9307 ((cur->type == XML_DOCUMENT_NODE) ||
9308 (cur->type == XML_HTML_DOCUMENT_NODE) ||
9309 (cur->type == XML_ELEMENT_NODE) ||
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00009310 (cur->type == XML_NAMESPACE_DECL) ||
9311 (cur->type == XML_ATTRIBUTE_NODE) ||
Daniel Veillardf06307e2001-07-03 10:35:50 +00009312 (cur->type == XML_PI_NODE) ||
9313 (cur->type == XML_COMMENT_NODE) ||
9314 (cur->type == XML_CDATA_SECTION_NODE) ||
Daniel Veillard7583a592001-07-08 13:15:55 +00009315 (cur->type == XML_TEXT_NODE))) ||
9316 ((type == NODE_TYPE_TEXT) &&
9317 (cur->type == XML_CDATA_SECTION_NODE))) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009318#ifdef DEBUG_STEP
9319 n++;
9320#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009321 addNode(list, cur);
9322 }
9323 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009324 case NODE_TEST_PI:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009325 if (cur->type == XML_PI_NODE) {
9326 if ((name != NULL) &&
9327 (!xmlStrEqual(name, cur->name)))
9328 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009329#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009330 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009331#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009332 addNode(list, cur);
9333 }
9334 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009335 case NODE_TEST_ALL:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009336 if (axis == AXIS_ATTRIBUTE) {
9337 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009338#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009339 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009340#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009341 addNode(list, cur);
9342 }
9343 } else if (axis == AXIS_NAMESPACE) {
9344 if (cur->type == XML_NAMESPACE_DECL) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009345#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009346 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009347#endif
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009348 xmlXPathNodeSetAddNs(list, ctxt->context->node,
9349 (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009350 }
9351 } else {
9352 if (cur->type == XML_ELEMENT_NODE) {
9353 if (prefix == NULL) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009354#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009355 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009356#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009357 addNode(list, cur);
9358 } else if ((cur->ns != NULL) &&
9359 (xmlStrEqual(URI, cur->ns->href))) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009360#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009361 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009362#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009363 addNode(list, cur);
9364 }
9365 }
9366 }
9367 break;
9368 case NODE_TEST_NS:{
9369 TODO;
9370 break;
9371 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009372 case NODE_TEST_NAME:
Daniel Veillardf06307e2001-07-03 10:35:50 +00009373 switch (cur->type) {
9374 case XML_ELEMENT_NODE:
9375 if (xmlStrEqual(name, cur->name)) {
9376 if (prefix == NULL) {
9377 if (cur->ns == NULL) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009378#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009379 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009380#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009381 addNode(list, cur);
9382 }
9383 } else {
9384 if ((cur->ns != NULL) &&
9385 (xmlStrEqual(URI,
9386 cur->ns->href))) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009387#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00009388 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009389#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009390 addNode(list, cur);
9391 }
9392 }
9393 }
9394 break;
9395 case XML_ATTRIBUTE_NODE:{
9396 xmlAttrPtr attr = (xmlAttrPtr) cur;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009397
Daniel Veillardf06307e2001-07-03 10:35:50 +00009398 if (xmlStrEqual(name, attr->name)) {
9399 if (prefix == NULL) {
9400 if ((attr->ns == NULL) ||
9401 (attr->ns->prefix == NULL)) {
9402#ifdef DEBUG_STEP
9403 n++;
9404#endif
9405 addNode(list,
9406 (xmlNodePtr) attr);
9407 }
9408 } else {
9409 if ((attr->ns != NULL) &&
9410 (xmlStrEqual(URI,
9411 attr->ns->
9412 href))) {
9413#ifdef DEBUG_STEP
9414 n++;
9415#endif
9416 addNode(list,
9417 (xmlNodePtr) attr);
9418 }
9419 }
9420 }
9421 break;
9422 }
9423 case XML_NAMESPACE_DECL:
9424 if (cur->type == XML_NAMESPACE_DECL) {
9425 xmlNsPtr ns = (xmlNsPtr) cur;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009426
Daniel Veillardf06307e2001-07-03 10:35:50 +00009427 if ((ns->prefix != NULL) && (name != NULL)
9428 && (xmlStrEqual(ns->prefix, name))) {
9429#ifdef DEBUG_STEP
9430 n++;
9431#endif
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009432 xmlXPathNodeSetAddNs(list,
9433 ctxt->context->node, (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009434 }
9435 }
9436 break;
9437 default:
9438 break;
9439 }
9440 break;
9441 break;
9442 }
9443 } while (cur != NULL);
9444
9445 /*
9446 * If there is some predicate filtering do it now
9447 */
Daniel Veillard6fbcf422002-03-21 12:32:59 +00009448 if ((op->ch2 != -1) && (list != NULL) && (list->nodeNr > 0)) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009449 xmlXPathObjectPtr obj2;
9450
9451 valuePush(ctxt, xmlXPathWrapNodeSet(list));
9452 xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch2]);
9453 CHECK_TYPE0(XPATH_NODESET);
9454 obj2 = valuePop(ctxt);
9455 list = obj2->nodesetval;
9456 obj2->nodesetval = NULL;
9457 xmlXPathFreeObject(obj2);
William M. Brack2c19a7b2005-04-10 01:03:23 +00009458 if (ctxt->error != XPATH_EXPRESSION_OK) {
9459 xmlXPathFreeObject(obj);
9460 xmlXPathFreeNodeSet(list);
9461 return(0);
9462 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00009463 }
9464 if (ret == NULL) {
9465 ret = list;
9466 } else {
Daniel Veillard75be0132002-03-13 10:03:35 +00009467 ret = mergeNodeSet(ret, list);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009468 xmlXPathFreeNodeSet(list);
9469 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009470 }
9471 ctxt->context->node = tmp;
9472#ifdef DEBUG_STEP
9473 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf06307e2001-07-03 10:35:50 +00009474 "\nExamined %d nodes, found %d nodes at that step\n",
9475 t, n);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009476#endif
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009477 valuePush(ctxt, xmlXPathWrapNodeSet(ret));
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00009478 if ((obj->boolval) && (obj->user != NULL)) {
9479 ctxt->value->boolval = 1;
9480 ctxt->value->user = obj->user;
9481 obj->user = NULL;
9482 obj->boolval = 0;
9483 }
9484 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009485 return(t);
9486}
9487
9488/**
9489 * xmlXPathNodeCollectAndTestNth:
9490 * @ctxt: the XPath Parser context
9491 * @op: the XPath precompiled step operation
9492 * @indx: the index to collect
9493 * @first: pointer to the first element in document order
9494 * @last: pointer to the last element in document order
9495 *
9496 * This is the function implementing a step: based on the current list
9497 * of nodes, it builds up a new list, looking at all nodes under that
William M. Brack08171912003-12-29 02:52:11 +00009498 * axis and selecting them. It also does the predicate filtering
Daniel Veillardf06307e2001-07-03 10:35:50 +00009499 *
9500 * Pushes the new NodeSet resulting from the search.
9501 * Returns the number of node traversed
9502 */
9503static int
9504xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt,
9505 xmlXPathStepOpPtr op, int indx,
9506 xmlNodePtr * first, xmlNodePtr * last)
9507{
William M. Brack78637da2003-07-31 14:47:38 +00009508 xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value;
9509 xmlXPathTestVal test = (xmlXPathTestVal) op->value2;
9510 xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009511 const xmlChar *prefix = op->value4;
9512 const xmlChar *name = op->value5;
9513 const xmlChar *URI = NULL;
9514 int n = 0, t = 0;
9515
9516 int i;
9517 xmlNodeSetPtr list;
9518 xmlXPathTraversalFunction next = NULL;
9519 void (*addNode) (xmlNodeSetPtr, xmlNodePtr);
9520 xmlNodePtr cur = NULL;
9521 xmlXPathObjectPtr obj;
9522 xmlNodeSetPtr nodelist;
9523 xmlNodePtr tmp;
9524
9525 CHECK_TYPE0(XPATH_NODESET);
9526 obj = valuePop(ctxt);
9527 addNode = xmlXPathNodeSetAdd;
9528 if (prefix != NULL) {
9529 URI = xmlXPathNsLookup(ctxt->context, prefix);
William M. Brack2c19a7b2005-04-10 01:03:23 +00009530 if (URI == NULL) {
9531 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009532 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
William M. Brack2c19a7b2005-04-10 01:03:23 +00009533 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00009534 }
9535#ifdef DEBUG_STEP_NTH
9536 xmlGenericError(xmlGenericErrorContext, "new step : ");
9537 if (first != NULL) {
9538 if (*first != NULL)
9539 xmlGenericError(xmlGenericErrorContext, "first = %s ",
9540 (*first)->name);
9541 else
9542 xmlGenericError(xmlGenericErrorContext, "first = NULL ");
9543 }
9544 if (last != NULL) {
9545 if (*last != NULL)
9546 xmlGenericError(xmlGenericErrorContext, "last = %s ",
9547 (*last)->name);
9548 else
9549 xmlGenericError(xmlGenericErrorContext, "last = NULL ");
9550 }
9551#endif
9552 switch (axis) {
9553 case AXIS_ANCESTOR:
9554#ifdef DEBUG_STEP_NTH
9555 xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' ");
9556#endif
9557 first = NULL;
9558 next = xmlXPathNextAncestor;
9559 break;
9560 case AXIS_ANCESTOR_OR_SELF:
9561#ifdef DEBUG_STEP_NTH
9562 xmlGenericError(xmlGenericErrorContext,
9563 "axis 'ancestors-or-self' ");
9564#endif
9565 first = NULL;
9566 next = xmlXPathNextAncestorOrSelf;
9567 break;
9568 case AXIS_ATTRIBUTE:
9569#ifdef DEBUG_STEP_NTH
9570 xmlGenericError(xmlGenericErrorContext, "axis 'attributes' ");
9571#endif
9572 first = NULL;
9573 last = NULL;
9574 next = xmlXPathNextAttribute;
9575 break;
9576 case AXIS_CHILD:
9577#ifdef DEBUG_STEP_NTH
9578 xmlGenericError(xmlGenericErrorContext, "axis 'child' ");
9579#endif
9580 last = NULL;
9581 next = xmlXPathNextChild;
9582 break;
9583 case AXIS_DESCENDANT:
9584#ifdef DEBUG_STEP_NTH
9585 xmlGenericError(xmlGenericErrorContext, "axis 'descendant' ");
9586#endif
9587 last = NULL;
9588 next = xmlXPathNextDescendant;
9589 break;
9590 case AXIS_DESCENDANT_OR_SELF:
9591#ifdef DEBUG_STEP_NTH
9592 xmlGenericError(xmlGenericErrorContext,
9593 "axis 'descendant-or-self' ");
9594#endif
9595 last = NULL;
9596 next = xmlXPathNextDescendantOrSelf;
9597 break;
9598 case AXIS_FOLLOWING:
9599#ifdef DEBUG_STEP_NTH
9600 xmlGenericError(xmlGenericErrorContext, "axis 'following' ");
9601#endif
9602 last = NULL;
9603 next = xmlXPathNextFollowing;
9604 break;
9605 case AXIS_FOLLOWING_SIBLING:
9606#ifdef DEBUG_STEP_NTH
9607 xmlGenericError(xmlGenericErrorContext,
9608 "axis 'following-siblings' ");
9609#endif
9610 last = NULL;
9611 next = xmlXPathNextFollowingSibling;
9612 break;
9613 case AXIS_NAMESPACE:
9614#ifdef DEBUG_STEP_NTH
9615 xmlGenericError(xmlGenericErrorContext, "axis 'namespace' ");
9616#endif
9617 last = NULL;
9618 first = NULL;
9619 next = (xmlXPathTraversalFunction) xmlXPathNextNamespace;
9620 break;
9621 case AXIS_PARENT:
9622#ifdef DEBUG_STEP_NTH
9623 xmlGenericError(xmlGenericErrorContext, "axis 'parent' ");
9624#endif
9625 first = NULL;
9626 next = xmlXPathNextParent;
9627 break;
9628 case AXIS_PRECEDING:
9629#ifdef DEBUG_STEP_NTH
9630 xmlGenericError(xmlGenericErrorContext, "axis 'preceding' ");
9631#endif
9632 first = NULL;
9633 next = xmlXPathNextPrecedingInternal;
9634 break;
9635 case AXIS_PRECEDING_SIBLING:
9636#ifdef DEBUG_STEP_NTH
9637 xmlGenericError(xmlGenericErrorContext,
9638 "axis 'preceding-sibling' ");
9639#endif
9640 first = NULL;
9641 next = xmlXPathNextPrecedingSibling;
9642 break;
9643 case AXIS_SELF:
9644#ifdef DEBUG_STEP_NTH
9645 xmlGenericError(xmlGenericErrorContext, "axis 'self' ");
9646#endif
9647 first = NULL;
9648 last = NULL;
9649 next = xmlXPathNextSelf;
9650 break;
9651 }
William M. Brack2c19a7b2005-04-10 01:03:23 +00009652 if (next == NULL) {
9653 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009654 return(0);
William M. Brack2c19a7b2005-04-10 01:03:23 +00009655 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00009656
9657 nodelist = obj->nodesetval;
9658 if (nodelist == NULL) {
9659 xmlXPathFreeObject(obj);
9660 valuePush(ctxt, xmlXPathWrapNodeSet(NULL));
9661 return(0);
9662 }
9663 addNode = xmlXPathNodeSetAddUnique;
9664#ifdef DEBUG_STEP_NTH
9665 xmlGenericError(xmlGenericErrorContext,
9666 " context contains %d nodes\n", nodelist->nodeNr);
9667 switch (test) {
9668 case NODE_TEST_NONE:
9669 xmlGenericError(xmlGenericErrorContext,
9670 " searching for none !!!\n");
9671 break;
9672 case NODE_TEST_TYPE:
9673 xmlGenericError(xmlGenericErrorContext,
9674 " searching for type %d\n", type);
9675 break;
9676 case NODE_TEST_PI:
9677 xmlGenericError(xmlGenericErrorContext,
9678 " searching for PI !!!\n");
9679 break;
9680 case NODE_TEST_ALL:
9681 xmlGenericError(xmlGenericErrorContext,
9682 " searching for *\n");
9683 break;
9684 case NODE_TEST_NS:
9685 xmlGenericError(xmlGenericErrorContext,
9686 " searching for namespace %s\n",
9687 prefix);
9688 break;
9689 case NODE_TEST_NAME:
9690 xmlGenericError(xmlGenericErrorContext,
9691 " searching for name %s\n", name);
9692 if (prefix != NULL)
9693 xmlGenericError(xmlGenericErrorContext,
9694 " with namespace %s\n", prefix);
9695 break;
9696 }
9697 xmlGenericError(xmlGenericErrorContext, "Testing : ");
9698#endif
9699 /*
9700 * 2.3 Node Tests
9701 * - For the attribute axis, the principal node type is attribute.
9702 * - For the namespace axis, the principal node type is namespace.
9703 * - For other axes, the principal node type is element.
9704 *
9705 * A node test * is true for any node of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009706 * principal node type. For example, child::* will
Daniel Veillardf06307e2001-07-03 10:35:50 +00009707 * select all element children of the context node
9708 */
9709 tmp = ctxt->context->node;
9710 list = xmlXPathNodeSetCreate(NULL);
9711 for (i = 0; i < nodelist->nodeNr; i++) {
9712 ctxt->context->node = nodelist->nodeTab[i];
9713
9714 cur = NULL;
9715 n = 0;
9716 do {
9717 cur = next(ctxt, cur);
9718 if (cur == NULL)
9719 break;
9720 if ((first != NULL) && (*first == cur))
9721 break;
9722 if (((t % 256) == 0) &&
9723 (first != NULL) && (*first != NULL) &&
9724 (xmlXPathCmpNodes(*first, cur) >= 0))
9725 break;
9726 if ((last != NULL) && (*last == cur))
9727 break;
9728 if (((t % 256) == 0) &&
9729 (last != NULL) && (*last != NULL) &&
9730 (xmlXPathCmpNodes(cur, *last) >= 0))
9731 break;
9732 t++;
9733 switch (test) {
9734 case NODE_TEST_NONE:
9735 ctxt->context->node = tmp;
9736 STRANGE return(0);
9737 case NODE_TEST_TYPE:
9738 if ((cur->type == type) ||
9739 ((type == NODE_TYPE_NODE) &&
9740 ((cur->type == XML_DOCUMENT_NODE) ||
9741 (cur->type == XML_HTML_DOCUMENT_NODE) ||
9742 (cur->type == XML_ELEMENT_NODE) ||
9743 (cur->type == XML_PI_NODE) ||
9744 (cur->type == XML_COMMENT_NODE) ||
9745 (cur->type == XML_CDATA_SECTION_NODE) ||
Daniel Veillard8606bbb2002-11-12 12:36:52 +00009746 (cur->type == XML_TEXT_NODE))) ||
9747 ((type == NODE_TYPE_TEXT) &&
9748 (cur->type == XML_CDATA_SECTION_NODE))) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009749 n++;
9750 if (n == indx)
9751 addNode(list, cur);
9752 }
9753 break;
9754 case NODE_TEST_PI:
9755 if (cur->type == XML_PI_NODE) {
9756 if ((name != NULL) &&
9757 (!xmlStrEqual(name, cur->name)))
9758 break;
9759 n++;
9760 if (n == indx)
9761 addNode(list, cur);
9762 }
9763 break;
9764 case NODE_TEST_ALL:
9765 if (axis == AXIS_ATTRIBUTE) {
9766 if (cur->type == XML_ATTRIBUTE_NODE) {
9767 n++;
9768 if (n == indx)
9769 addNode(list, cur);
9770 }
9771 } else if (axis == AXIS_NAMESPACE) {
9772 if (cur->type == XML_NAMESPACE_DECL) {
9773 n++;
9774 if (n == indx)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009775 xmlXPathNodeSetAddNs(list, ctxt->context->node,
9776 (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009777 }
9778 } else {
9779 if (cur->type == XML_ELEMENT_NODE) {
9780 if (prefix == NULL) {
9781 n++;
9782 if (n == indx)
9783 addNode(list, cur);
9784 } else if ((cur->ns != NULL) &&
9785 (xmlStrEqual(URI, cur->ns->href))) {
9786 n++;
9787 if (n == indx)
9788 addNode(list, cur);
9789 }
9790 }
9791 }
9792 break;
9793 case NODE_TEST_NS:{
9794 TODO;
9795 break;
9796 }
9797 case NODE_TEST_NAME:
9798 switch (cur->type) {
9799 case XML_ELEMENT_NODE:
9800 if (xmlStrEqual(name, cur->name)) {
9801 if (prefix == NULL) {
9802 if (cur->ns == NULL) {
9803 n++;
9804 if (n == indx)
9805 addNode(list, cur);
9806 }
9807 } else {
9808 if ((cur->ns != NULL) &&
9809 (xmlStrEqual(URI,
9810 cur->ns->href))) {
9811 n++;
9812 if (n == indx)
9813 addNode(list, cur);
9814 }
9815 }
9816 }
9817 break;
9818 case XML_ATTRIBUTE_NODE:{
9819 xmlAttrPtr attr = (xmlAttrPtr) cur;
9820
9821 if (xmlStrEqual(name, attr->name)) {
9822 if (prefix == NULL) {
9823 if ((attr->ns == NULL) ||
9824 (attr->ns->prefix == NULL)) {
9825 n++;
9826 if (n == indx)
9827 addNode(list, cur);
9828 }
9829 } else {
9830 if ((attr->ns != NULL) &&
9831 (xmlStrEqual(URI,
9832 attr->ns->
9833 href))) {
9834 n++;
9835 if (n == indx)
9836 addNode(list, cur);
9837 }
9838 }
9839 }
9840 break;
9841 }
9842 case XML_NAMESPACE_DECL:
9843 if (cur->type == XML_NAMESPACE_DECL) {
9844 xmlNsPtr ns = (xmlNsPtr) cur;
9845
9846 if ((ns->prefix != NULL) && (name != NULL)
9847 && (xmlStrEqual(ns->prefix, name))) {
9848 n++;
9849 if (n == indx)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009850 xmlXPathNodeSetAddNs(list,
9851 ctxt->context->node, (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009852 }
9853 }
9854 break;
9855 default:
9856 break;
9857 }
9858 break;
9859 break;
9860 }
9861 } while (n < indx);
9862 }
9863 ctxt->context->node = tmp;
9864#ifdef DEBUG_STEP_NTH
9865 xmlGenericError(xmlGenericErrorContext,
9866 "\nExamined %d nodes, found %d nodes at that step\n",
9867 t, list->nodeNr);
9868#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009869 valuePush(ctxt, xmlXPathWrapNodeSet(list));
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00009870 if ((obj->boolval) && (obj->user != NULL)) {
9871 ctxt->value->boolval = 1;
9872 ctxt->value->user = obj->user;
9873 obj->user = NULL;
9874 obj->boolval = 0;
9875 }
9876 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009877 return(t);
9878}
9879
9880/**
9881 * xmlXPathCompOpEvalFirst:
9882 * @ctxt: the XPath parser context with the compiled expression
9883 * @op: an XPath compiled operation
9884 * @first: the first elem found so far
9885 *
9886 * Evaluate the Precompiled XPath operation searching only the first
9887 * element in document order
9888 *
9889 * Returns the number of examined objects.
9890 */
9891static int
9892xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt,
9893 xmlXPathStepOpPtr op, xmlNodePtr * first)
9894{
9895 int total = 0, cur;
9896 xmlXPathCompExprPtr comp;
9897 xmlXPathObjectPtr arg1, arg2;
9898
Daniel Veillard556c6682001-10-06 09:59:51 +00009899 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009900 comp = ctxt->comp;
9901 switch (op->op) {
9902 case XPATH_OP_END:
9903 return (0);
9904 case XPATH_OP_UNION:
9905 total =
9906 xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1],
9907 first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009908 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009909 if ((ctxt->value != NULL)
9910 && (ctxt->value->type == XPATH_NODESET)
9911 && (ctxt->value->nodesetval != NULL)
9912 && (ctxt->value->nodesetval->nodeNr >= 1)) {
9913 /*
9914 * limit tree traversing to first node in the result
9915 */
9916 xmlXPathNodeSetSort(ctxt->value->nodesetval);
9917 *first = ctxt->value->nodesetval->nodeTab[0];
9918 }
9919 cur =
9920 xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2],
9921 first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009922 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009923 CHECK_TYPE0(XPATH_NODESET);
9924 arg2 = valuePop(ctxt);
9925
9926 CHECK_TYPE0(XPATH_NODESET);
9927 arg1 = valuePop(ctxt);
9928
9929 arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
9930 arg2->nodesetval);
9931 valuePush(ctxt, arg1);
9932 xmlXPathFreeObject(arg2);
9933 /* optimizer */
9934 if (total > cur)
9935 xmlXPathCompSwap(op);
9936 return (total + cur);
9937 case XPATH_OP_ROOT:
9938 xmlXPathRoot(ctxt);
9939 return (0);
9940 case XPATH_OP_NODE:
9941 if (op->ch1 != -1)
9942 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009943 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009944 if (op->ch2 != -1)
9945 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009946 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009947 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
9948 return (total);
9949 case XPATH_OP_RESET:
9950 if (op->ch1 != -1)
9951 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009952 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009953 if (op->ch2 != -1)
9954 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009955 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009956 ctxt->context->node = NULL;
9957 return (total);
9958 case XPATH_OP_COLLECT:{
9959 if (op->ch1 == -1)
9960 return (total);
9961
9962 total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009963 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009964
9965 /*
9966 * Optimization for [n] selection where n is a number
9967 */
9968 if ((op->ch2 != -1) &&
9969 (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) &&
9970 (comp->steps[op->ch2].ch1 == -1) &&
9971 (comp->steps[op->ch2].ch2 != -1) &&
9972 (comp->steps[comp->steps[op->ch2].ch2].op ==
9973 XPATH_OP_VALUE)) {
9974 xmlXPathObjectPtr val;
9975
9976 val = comp->steps[comp->steps[op->ch2].ch2].value4;
9977 if ((val != NULL) && (val->type == XPATH_NUMBER)) {
9978 int indx = (int) val->floatval;
9979
9980 if (val->floatval == (float) indx) {
9981 xmlXPathNodeCollectAndTestNth(ctxt, op, indx,
9982 first, NULL);
9983 return (total);
9984 }
9985 }
9986 }
9987 total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL);
9988 return (total);
9989 }
9990 case XPATH_OP_VALUE:
9991 valuePush(ctxt,
9992 xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4));
9993 return (0);
9994 case XPATH_OP_SORT:
9995 if (op->ch1 != -1)
9996 total +=
9997 xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1],
9998 first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009999 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010000 if ((ctxt->value != NULL)
10001 && (ctxt->value->type == XPATH_NODESET)
10002 && (ctxt->value->nodesetval != NULL))
10003 xmlXPathNodeSetSort(ctxt->value->nodesetval);
10004 return (total);
10005 default:
10006 return (xmlXPathCompOpEval(ctxt, op));
10007 }
10008}
10009
10010/**
10011 * xmlXPathCompOpEvalLast:
10012 * @ctxt: the XPath parser context with the compiled expression
10013 * @op: an XPath compiled operation
10014 * @last: the last elem found so far
10015 *
10016 * Evaluate the Precompiled XPath operation searching only the last
10017 * element in document order
10018 *
William M. Brack08171912003-12-29 02:52:11 +000010019 * Returns the number of nodes traversed
Daniel Veillardf06307e2001-07-03 10:35:50 +000010020 */
10021static int
10022xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op,
10023 xmlNodePtr * last)
10024{
10025 int total = 0, cur;
10026 xmlXPathCompExprPtr comp;
10027 xmlXPathObjectPtr arg1, arg2;
William M. Brackce4fc562004-01-22 02:47:18 +000010028 xmlNodePtr bak;
10029 xmlDocPtr bakd;
10030 int pp;
10031 int cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010032
Daniel Veillard556c6682001-10-06 09:59:51 +000010033 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010034 comp = ctxt->comp;
10035 switch (op->op) {
10036 case XPATH_OP_END:
10037 return (0);
10038 case XPATH_OP_UNION:
William M. Brackce4fc562004-01-22 02:47:18 +000010039 bakd = ctxt->context->doc;
10040 bak = ctxt->context->node;
10041 pp = ctxt->context->proximityPosition;
10042 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010043 total =
10044 xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last);
Daniel Veillard556c6682001-10-06 09:59:51 +000010045 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010046 if ((ctxt->value != NULL)
10047 && (ctxt->value->type == XPATH_NODESET)
10048 && (ctxt->value->nodesetval != NULL)
10049 && (ctxt->value->nodesetval->nodeNr >= 1)) {
10050 /*
10051 * limit tree traversing to first node in the result
10052 */
10053 xmlXPathNodeSetSort(ctxt->value->nodesetval);
10054 *last =
10055 ctxt->value->nodesetval->nodeTab[ctxt->value->
10056 nodesetval->nodeNr -
10057 1];
10058 }
William M. Brackce4fc562004-01-22 02:47:18 +000010059 ctxt->context->doc = bakd;
10060 ctxt->context->node = bak;
10061 ctxt->context->proximityPosition = pp;
10062 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010063 cur =
10064 xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last);
Daniel Veillard556c6682001-10-06 09:59:51 +000010065 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010066 if ((ctxt->value != NULL)
10067 && (ctxt->value->type == XPATH_NODESET)
10068 && (ctxt->value->nodesetval != NULL)
10069 && (ctxt->value->nodesetval->nodeNr >= 1)) {
10070 }
10071 CHECK_TYPE0(XPATH_NODESET);
10072 arg2 = valuePop(ctxt);
10073
10074 CHECK_TYPE0(XPATH_NODESET);
10075 arg1 = valuePop(ctxt);
10076
10077 arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
10078 arg2->nodesetval);
10079 valuePush(ctxt, arg1);
10080 xmlXPathFreeObject(arg2);
10081 /* optimizer */
10082 if (total > cur)
10083 xmlXPathCompSwap(op);
10084 return (total + cur);
10085 case XPATH_OP_ROOT:
10086 xmlXPathRoot(ctxt);
10087 return (0);
10088 case XPATH_OP_NODE:
10089 if (op->ch1 != -1)
10090 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010091 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010092 if (op->ch2 != -1)
10093 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010094 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010095 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
10096 return (total);
10097 case XPATH_OP_RESET:
10098 if (op->ch1 != -1)
10099 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010100 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010101 if (op->ch2 != -1)
10102 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010103 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010104 ctxt->context->node = NULL;
10105 return (total);
10106 case XPATH_OP_COLLECT:{
10107 if (op->ch1 == -1)
10108 return (0);
10109
10110 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010111 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010112
10113 /*
10114 * Optimization for [n] selection where n is a number
10115 */
10116 if ((op->ch2 != -1) &&
10117 (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) &&
10118 (comp->steps[op->ch2].ch1 == -1) &&
10119 (comp->steps[op->ch2].ch2 != -1) &&
10120 (comp->steps[comp->steps[op->ch2].ch2].op ==
10121 XPATH_OP_VALUE)) {
10122 xmlXPathObjectPtr val;
10123
10124 val = comp->steps[comp->steps[op->ch2].ch2].value4;
10125 if ((val != NULL) && (val->type == XPATH_NUMBER)) {
10126 int indx = (int) val->floatval;
10127
10128 if (val->floatval == (float) indx) {
10129 total +=
10130 xmlXPathNodeCollectAndTestNth(ctxt, op,
10131 indx, NULL,
10132 last);
10133 return (total);
10134 }
10135 }
10136 }
10137 total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last);
10138 return (total);
10139 }
10140 case XPATH_OP_VALUE:
10141 valuePush(ctxt,
10142 xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4));
10143 return (0);
10144 case XPATH_OP_SORT:
10145 if (op->ch1 != -1)
10146 total +=
10147 xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1],
10148 last);
Daniel Veillard556c6682001-10-06 09:59:51 +000010149 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010150 if ((ctxt->value != NULL)
10151 && (ctxt->value->type == XPATH_NODESET)
10152 && (ctxt->value->nodesetval != NULL))
10153 xmlXPathNodeSetSort(ctxt->value->nodesetval);
10154 return (total);
10155 default:
10156 return (xmlXPathCompOpEval(ctxt, op));
10157 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +000010158}
10159
Owen Taylor3473f882001-02-23 17:55:21 +000010160/**
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010161 * xmlXPathCompOpEval:
10162 * @ctxt: the XPath parser context with the compiled expression
10163 * @op: an XPath compiled operation
10164 *
10165 * Evaluate the Precompiled XPath operation
William M. Brack08171912003-12-29 02:52:11 +000010166 * Returns the number of nodes traversed
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010167 */
Daniel Veillardf06307e2001-07-03 10:35:50 +000010168static int
10169xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op)
10170{
10171 int total = 0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010172 int equal, ret;
10173 xmlXPathCompExprPtr comp;
10174 xmlXPathObjectPtr arg1, arg2;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010175 xmlNodePtr bak;
10176 xmlDocPtr bakd;
William M. Brack6000af52002-06-28 11:43:13 +000010177 int pp;
William M. Brack692092b2002-06-28 15:01:24 +000010178 int cs;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010179
Daniel Veillard556c6682001-10-06 09:59:51 +000010180 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010181 comp = ctxt->comp;
10182 switch (op->op) {
Daniel Veillardf06307e2001-07-03 10:35:50 +000010183 case XPATH_OP_END:
10184 return (0);
10185 case XPATH_OP_AND:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010186 bakd = ctxt->context->doc;
10187 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010188 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010189 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010190 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010191 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010192 xmlXPathBooleanFunction(ctxt, 1);
10193 if ((ctxt->value == NULL) || (ctxt->value->boolval == 0))
10194 return (total);
10195 arg2 = valuePop(ctxt);
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010196 ctxt->context->doc = bakd;
10197 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010198 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010199 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010200 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010201 if (ctxt->error) {
10202 xmlXPathFreeObject(arg2);
10203 return(0);
10204 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010205 xmlXPathBooleanFunction(ctxt, 1);
10206 arg1 = valuePop(ctxt);
10207 arg1->boolval &= arg2->boolval;
10208 valuePush(ctxt, arg1);
10209 xmlXPathFreeObject(arg2);
10210 return (total);
10211 case XPATH_OP_OR:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010212 bakd = ctxt->context->doc;
10213 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010214 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010215 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010216 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010217 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010218 xmlXPathBooleanFunction(ctxt, 1);
10219 if ((ctxt->value == NULL) || (ctxt->value->boolval == 1))
10220 return (total);
10221 arg2 = valuePop(ctxt);
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010222 ctxt->context->doc = bakd;
10223 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010224 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010225 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010226 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010227 if (ctxt->error) {
10228 xmlXPathFreeObject(arg2);
10229 return(0);
10230 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010231 xmlXPathBooleanFunction(ctxt, 1);
10232 arg1 = valuePop(ctxt);
10233 arg1->boolval |= arg2->boolval;
10234 valuePush(ctxt, arg1);
10235 xmlXPathFreeObject(arg2);
10236 return (total);
10237 case XPATH_OP_EQUAL:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010238 bakd = ctxt->context->doc;
10239 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010240 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010241 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010242 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010243 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010244 ctxt->context->doc = bakd;
10245 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010246 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010247 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010248 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010249 CHECK_ERROR0;
William M. Brack0c022ad2002-07-12 00:56:01 +000010250 if (op->value)
10251 equal = xmlXPathEqualValues(ctxt);
10252 else
10253 equal = xmlXPathNotEqualValues(ctxt);
10254 valuePush(ctxt, xmlXPathNewBoolean(equal));
Daniel Veillardf06307e2001-07-03 10:35:50 +000010255 return (total);
10256 case XPATH_OP_CMP:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010257 bakd = ctxt->context->doc;
10258 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010259 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010260 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010261 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010262 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010263 ctxt->context->doc = bakd;
10264 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010265 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010266 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010267 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010268 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010269 ret = xmlXPathCompareValues(ctxt, op->value, op->value2);
10270 valuePush(ctxt, xmlXPathNewBoolean(ret));
10271 return (total);
10272 case XPATH_OP_PLUS:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010273 bakd = ctxt->context->doc;
10274 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010275 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010276 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010277 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010278 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010279 if (op->ch2 != -1) {
10280 ctxt->context->doc = bakd;
10281 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010282 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010283 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010284 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010285 }
Daniel Veillard556c6682001-10-06 09:59:51 +000010286 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010287 if (op->value == 0)
10288 xmlXPathSubValues(ctxt);
10289 else if (op->value == 1)
10290 xmlXPathAddValues(ctxt);
10291 else if (op->value == 2)
10292 xmlXPathValueFlipSign(ctxt);
10293 else if (op->value == 3) {
10294 CAST_TO_NUMBER;
10295 CHECK_TYPE0(XPATH_NUMBER);
10296 }
10297 return (total);
10298 case XPATH_OP_MULT:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010299 bakd = ctxt->context->doc;
10300 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010301 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010302 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010303 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010304 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010305 ctxt->context->doc = bakd;
10306 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010307 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010308 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010309 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010310 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010311 if (op->value == 0)
10312 xmlXPathMultValues(ctxt);
10313 else if (op->value == 1)
10314 xmlXPathDivValues(ctxt);
10315 else if (op->value == 2)
10316 xmlXPathModValues(ctxt);
10317 return (total);
10318 case XPATH_OP_UNION:
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010319 bakd = ctxt->context->doc;
10320 bak = ctxt->context->node;
William M. Brack6000af52002-06-28 11:43:13 +000010321 pp = ctxt->context->proximityPosition;
William M. Brack692092b2002-06-28 15:01:24 +000010322 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010323 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010324 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +000010325 ctxt->context->doc = bakd;
10326 ctxt->context->node = bak;
William M. Brack6000af52002-06-28 11:43:13 +000010327 ctxt->context->proximityPosition = pp;
William M. Brack692092b2002-06-28 15:01:24 +000010328 ctxt->context->contextSize = cs;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010329 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010330 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010331 CHECK_TYPE0(XPATH_NODESET);
10332 arg2 = valuePop(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010333
Daniel Veillardf06307e2001-07-03 10:35:50 +000010334 CHECK_TYPE0(XPATH_NODESET);
10335 arg1 = valuePop(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010336
Daniel Veillardf06307e2001-07-03 10:35:50 +000010337 arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
10338 arg2->nodesetval);
10339 valuePush(ctxt, arg1);
10340 xmlXPathFreeObject(arg2);
10341 return (total);
10342 case XPATH_OP_ROOT:
10343 xmlXPathRoot(ctxt);
10344 return (total);
10345 case XPATH_OP_NODE:
10346 if (op->ch1 != -1)
10347 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010348 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010349 if (op->ch2 != -1)
10350 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010351 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010352 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
10353 return (total);
10354 case XPATH_OP_RESET:
10355 if (op->ch1 != -1)
10356 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010357 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010358 if (op->ch2 != -1)
10359 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010360 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010361 ctxt->context->node = NULL;
10362 return (total);
10363 case XPATH_OP_COLLECT:{
10364 if (op->ch1 == -1)
10365 return (total);
Daniel Veillardd8df6c02001-04-05 16:54:14 +000010366
Daniel Veillardf06307e2001-07-03 10:35:50 +000010367 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010368 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010369
Daniel Veillardf06307e2001-07-03 10:35:50 +000010370 /*
10371 * Optimization for [n] selection where n is a number
10372 */
10373 if ((op->ch2 != -1) &&
10374 (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) &&
10375 (comp->steps[op->ch2].ch1 == -1) &&
10376 (comp->steps[op->ch2].ch2 != -1) &&
10377 (comp->steps[comp->steps[op->ch2].ch2].op ==
10378 XPATH_OP_VALUE)) {
10379 xmlXPathObjectPtr val;
Daniel Veillard42596ad2001-05-22 16:57:14 +000010380
Daniel Veillardf06307e2001-07-03 10:35:50 +000010381 val = comp->steps[comp->steps[op->ch2].ch2].value4;
10382 if ((val != NULL) && (val->type == XPATH_NUMBER)) {
10383 int indx = (int) val->floatval;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010384
Daniel Veillardf06307e2001-07-03 10:35:50 +000010385 if (val->floatval == (float) indx) {
10386 total +=
10387 xmlXPathNodeCollectAndTestNth(ctxt, op,
10388 indx, NULL,
10389 NULL);
10390 return (total);
10391 }
10392 }
10393 }
10394 total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL);
10395 return (total);
10396 }
10397 case XPATH_OP_VALUE:
10398 valuePush(ctxt,
10399 xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4));
10400 return (total);
10401 case XPATH_OP_VARIABLE:{
Daniel Veillard556c6682001-10-06 09:59:51 +000010402 xmlXPathObjectPtr val;
10403
Daniel Veillardf06307e2001-07-03 10:35:50 +000010404 if (op->ch1 != -1)
10405 total +=
10406 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010407 if (op->value5 == NULL) {
10408 val = xmlXPathVariableLookup(ctxt->context, op->value4);
10409 if (val == NULL) {
10410 ctxt->error = XPATH_UNDEF_VARIABLE_ERROR;
10411 return(0);
10412 }
10413 valuePush(ctxt, val);
10414 } else {
Daniel Veillardf06307e2001-07-03 10:35:50 +000010415 const xmlChar *URI;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010416
Daniel Veillardf06307e2001-07-03 10:35:50 +000010417 URI = xmlXPathNsLookup(ctxt->context, op->value5);
10418 if (URI == NULL) {
10419 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010420 "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n",
Daniel Veillardf06307e2001-07-03 10:35:50 +000010421 op->value4, op->value5);
10422 return (total);
10423 }
Daniel Veillard556c6682001-10-06 09:59:51 +000010424 val = xmlXPathVariableLookupNS(ctxt->context,
10425 op->value4, URI);
10426 if (val == NULL) {
10427 ctxt->error = XPATH_UNDEF_VARIABLE_ERROR;
10428 return(0);
10429 }
10430 valuePush(ctxt, val);
Daniel Veillardf06307e2001-07-03 10:35:50 +000010431 }
10432 return (total);
10433 }
10434 case XPATH_OP_FUNCTION:{
10435 xmlXPathFunction func;
10436 const xmlChar *oldFunc, *oldFuncURI;
Daniel Veillard556c6682001-10-06 09:59:51 +000010437 int i;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010438
10439 if (op->ch1 != -1)
10440 total +=
10441 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010442 if (ctxt->valueNr < op->value) {
10443 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010444 "xmlXPathCompOpEval: parameter error\n");
Daniel Veillard556c6682001-10-06 09:59:51 +000010445 ctxt->error = XPATH_INVALID_OPERAND;
10446 return (total);
10447 }
10448 for (i = 0; i < op->value; i++)
10449 if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) {
10450 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010451 "xmlXPathCompOpEval: parameter error\n");
Daniel Veillard556c6682001-10-06 09:59:51 +000010452 ctxt->error = XPATH_INVALID_OPERAND;
10453 return (total);
10454 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010455 if (op->cache != NULL)
William M. Brackad0e67c2004-12-01 14:35:10 +000010456 XML_CAST_FPTR(func) = op->cache;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010457 else {
10458 const xmlChar *URI = NULL;
10459
10460 if (op->value5 == NULL)
10461 func =
10462 xmlXPathFunctionLookup(ctxt->context,
10463 op->value4);
10464 else {
10465 URI = xmlXPathNsLookup(ctxt->context, op->value5);
10466 if (URI == NULL) {
10467 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010468 "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n",
Daniel Veillardf06307e2001-07-03 10:35:50 +000010469 op->value4, op->value5);
10470 return (total);
10471 }
10472 func = xmlXPathFunctionLookupNS(ctxt->context,
10473 op->value4, URI);
10474 }
10475 if (func == NULL) {
10476 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010477 "xmlXPathCompOpEval: function %s not found\n",
Daniel Veillardf06307e2001-07-03 10:35:50 +000010478 op->value4);
10479 XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR);
Daniel Veillardf06307e2001-07-03 10:35:50 +000010480 }
William M. Brackad0e67c2004-12-01 14:35:10 +000010481 op->cache = XML_CAST_FPTR(func);
Daniel Veillardf06307e2001-07-03 10:35:50 +000010482 op->cacheURI = (void *) URI;
10483 }
10484 oldFunc = ctxt->context->function;
10485 oldFuncURI = ctxt->context->functionURI;
10486 ctxt->context->function = op->value4;
10487 ctxt->context->functionURI = op->cacheURI;
10488 func(ctxt, op->value);
10489 ctxt->context->function = oldFunc;
10490 ctxt->context->functionURI = oldFuncURI;
10491 return (total);
10492 }
10493 case XPATH_OP_ARG:
Daniel Veillard088bf112002-05-14 11:03:59 +000010494 bakd = ctxt->context->doc;
10495 bak = ctxt->context->node;
William M. Brack645a9242004-11-09 12:20:42 +000010496 pp = ctxt->context->proximityPosition;
10497 cs = ctxt->context->contextSize;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010498 if (op->ch1 != -1)
10499 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
William M. Brack645a9242004-11-09 12:20:42 +000010500 ctxt->context->contextSize = cs;
10501 ctxt->context->proximityPosition = pp;
Daniel Veillard088bf112002-05-14 11:03:59 +000010502 ctxt->context->node = bak;
William M. Brack645a9242004-11-09 12:20:42 +000010503 ctxt->context->doc = bakd;
Daniel Veillard556c6682001-10-06 09:59:51 +000010504 CHECK_ERROR0;
William M. Brack72ee48d2003-12-30 08:30:19 +000010505 if (op->ch2 != -1) {
Daniel Veillardf06307e2001-07-03 10:35:50 +000010506 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
William M. Brack72ee48d2003-12-30 08:30:19 +000010507 ctxt->context->doc = bakd;
10508 ctxt->context->node = bak;
10509 CHECK_ERROR0;
10510 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010511 return (total);
10512 case XPATH_OP_PREDICATE:
10513 case XPATH_OP_FILTER:{
10514 xmlXPathObjectPtr res;
10515 xmlXPathObjectPtr obj, tmp;
10516 xmlNodeSetPtr newset = NULL;
10517 xmlNodeSetPtr oldset;
10518 xmlNodePtr oldnode;
William M. Brack3794b9e2004-07-13 15:06:20 +000010519 xmlDocPtr oldDoc;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010520 int i;
10521
10522 /*
10523 * Optimization for ()[1] selection i.e. the first elem
10524 */
10525 if ((op->ch1 != -1) && (op->ch2 != -1) &&
10526 (comp->steps[op->ch1].op == XPATH_OP_SORT) &&
10527 (comp->steps[op->ch2].op == XPATH_OP_VALUE)) {
10528 xmlXPathObjectPtr val;
10529
10530 val = comp->steps[op->ch2].value4;
10531 if ((val != NULL) && (val->type == XPATH_NUMBER) &&
10532 (val->floatval == 1.0)) {
10533 xmlNodePtr first = NULL;
10534
10535 total +=
10536 xmlXPathCompOpEvalFirst(ctxt,
10537 &comp->steps[op->ch1],
10538 &first);
Daniel Veillard556c6682001-10-06 09:59:51 +000010539 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010540 /*
10541 * The nodeset should be in document order,
10542 * Keep only the first value
10543 */
10544 if ((ctxt->value != NULL) &&
10545 (ctxt->value->type == XPATH_NODESET) &&
10546 (ctxt->value->nodesetval != NULL) &&
10547 (ctxt->value->nodesetval->nodeNr > 1))
10548 ctxt->value->nodesetval->nodeNr = 1;
10549 return (total);
10550 }
10551 }
10552 /*
10553 * Optimization for ()[last()] selection i.e. the last elem
10554 */
10555 if ((op->ch1 != -1) && (op->ch2 != -1) &&
10556 (comp->steps[op->ch1].op == XPATH_OP_SORT) &&
10557 (comp->steps[op->ch2].op == XPATH_OP_SORT)) {
10558 int f = comp->steps[op->ch2].ch1;
10559
10560 if ((f != -1) &&
10561 (comp->steps[f].op == XPATH_OP_FUNCTION) &&
10562 (comp->steps[f].value5 == NULL) &&
10563 (comp->steps[f].value == 0) &&
10564 (comp->steps[f].value4 != NULL) &&
10565 (xmlStrEqual
10566 (comp->steps[f].value4, BAD_CAST "last"))) {
10567 xmlNodePtr last = NULL;
10568
10569 total +=
10570 xmlXPathCompOpEvalLast(ctxt,
10571 &comp->steps[op->ch1],
10572 &last);
Daniel Veillard556c6682001-10-06 09:59:51 +000010573 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010574 /*
10575 * The nodeset should be in document order,
10576 * Keep only the last value
10577 */
10578 if ((ctxt->value != NULL) &&
10579 (ctxt->value->type == XPATH_NODESET) &&
10580 (ctxt->value->nodesetval != NULL) &&
10581 (ctxt->value->nodesetval->nodeTab != NULL) &&
10582 (ctxt->value->nodesetval->nodeNr > 1)) {
10583 ctxt->value->nodesetval->nodeTab[0] =
10584 ctxt->value->nodesetval->nodeTab[ctxt->
10585 value->
10586 nodesetval->
10587 nodeNr -
10588 1];
10589 ctxt->value->nodesetval->nodeNr = 1;
10590 }
10591 return (total);
10592 }
10593 }
10594
10595 if (op->ch1 != -1)
10596 total +=
10597 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010598 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010599 if (op->ch2 == -1)
10600 return (total);
10601 if (ctxt->value == NULL)
10602 return (total);
10603
10604 oldnode = ctxt->context->node;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010605
10606#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardf06307e2001-07-03 10:35:50 +000010607 /*
10608 * Hum are we filtering the result of an XPointer expression
10609 */
10610 if (ctxt->value->type == XPATH_LOCATIONSET) {
10611 xmlLocationSetPtr newlocset = NULL;
10612 xmlLocationSetPtr oldlocset;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010613
Daniel Veillardf06307e2001-07-03 10:35:50 +000010614 /*
10615 * Extract the old locset, and then evaluate the result of the
10616 * expression for all the element in the locset. use it to grow
10617 * up a new locset.
10618 */
10619 CHECK_TYPE0(XPATH_LOCATIONSET);
10620 obj = valuePop(ctxt);
10621 oldlocset = obj->user;
10622 ctxt->context->node = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010623
Daniel Veillardf06307e2001-07-03 10:35:50 +000010624 if ((oldlocset == NULL) || (oldlocset->locNr == 0)) {
10625 ctxt->context->contextSize = 0;
10626 ctxt->context->proximityPosition = 0;
10627 if (op->ch2 != -1)
10628 total +=
10629 xmlXPathCompOpEval(ctxt,
10630 &comp->steps[op->ch2]);
10631 res = valuePop(ctxt);
10632 if (res != NULL)
10633 xmlXPathFreeObject(res);
10634 valuePush(ctxt, obj);
10635 CHECK_ERROR0;
10636 return (total);
10637 }
10638 newlocset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010639
Daniel Veillardf06307e2001-07-03 10:35:50 +000010640 for (i = 0; i < oldlocset->locNr; i++) {
10641 /*
10642 * Run the evaluation with a node list made of a
10643 * single item in the nodelocset.
10644 */
10645 ctxt->context->node = oldlocset->locTab[i]->user;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010646 ctxt->context->contextSize = oldlocset->locNr;
10647 ctxt->context->proximityPosition = i + 1;
William M. Brackf7eb7942003-12-31 07:59:17 +000010648 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10649 valuePush(ctxt, tmp);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010650
Daniel Veillardf06307e2001-07-03 10:35:50 +000010651 if (op->ch2 != -1)
10652 total +=
10653 xmlXPathCompOpEval(ctxt,
10654 &comp->steps[op->ch2]);
William M. Brack2c19a7b2005-04-10 01:03:23 +000010655 if (ctxt->error != XPATH_EXPRESSION_OK) {
10656 xmlXPathFreeObject(obj);
10657 return(0);
10658 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010659
Daniel Veillardf06307e2001-07-03 10:35:50 +000010660 /*
10661 * The result of the evaluation need to be tested to
10662 * decided whether the filter succeeded or not
10663 */
10664 res = valuePop(ctxt);
10665 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
10666 xmlXPtrLocationSetAdd(newlocset,
10667 xmlXPathObjectCopy
10668 (oldlocset->locTab[i]));
10669 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010670
Daniel Veillardf06307e2001-07-03 10:35:50 +000010671 /*
10672 * Cleanup
10673 */
10674 if (res != NULL)
10675 xmlXPathFreeObject(res);
10676 if (ctxt->value == tmp) {
10677 res = valuePop(ctxt);
10678 xmlXPathFreeObject(res);
10679 }
10680
10681 ctxt->context->node = NULL;
10682 }
10683
10684 /*
10685 * The result is used as the new evaluation locset.
10686 */
10687 xmlXPathFreeObject(obj);
10688 ctxt->context->node = NULL;
10689 ctxt->context->contextSize = -1;
10690 ctxt->context->proximityPosition = -1;
10691 valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset));
10692 ctxt->context->node = oldnode;
10693 return (total);
10694 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010695#endif /* LIBXML_XPTR_ENABLED */
10696
Daniel Veillardf06307e2001-07-03 10:35:50 +000010697 /*
10698 * Extract the old set, and then evaluate the result of the
10699 * expression for all the element in the set. use it to grow
10700 * up a new set.
10701 */
10702 CHECK_TYPE0(XPATH_NODESET);
10703 obj = valuePop(ctxt);
10704 oldset = obj->nodesetval;
Daniel Veillard911f49a2001-04-07 15:39:35 +000010705
Daniel Veillardf06307e2001-07-03 10:35:50 +000010706 oldnode = ctxt->context->node;
William M. Brack3794b9e2004-07-13 15:06:20 +000010707 oldDoc = ctxt->context->doc;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010708 ctxt->context->node = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010709
Daniel Veillardf06307e2001-07-03 10:35:50 +000010710 if ((oldset == NULL) || (oldset->nodeNr == 0)) {
10711 ctxt->context->contextSize = 0;
10712 ctxt->context->proximityPosition = 0;
William M. Brack8fad8bf2004-06-02 08:26:25 +000010713/*
Daniel Veillardf06307e2001-07-03 10:35:50 +000010714 if (op->ch2 != -1)
10715 total +=
10716 xmlXPathCompOpEval(ctxt,
10717 &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010718 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010719 res = valuePop(ctxt);
10720 if (res != NULL)
10721 xmlXPathFreeObject(res);
William M. Brack8fad8bf2004-06-02 08:26:25 +000010722*/
Daniel Veillardf06307e2001-07-03 10:35:50 +000010723 valuePush(ctxt, obj);
10724 ctxt->context->node = oldnode;
10725 CHECK_ERROR0;
10726 } else {
10727 /*
10728 * Initialize the new set.
William M. Brack3794b9e2004-07-13 15:06:20 +000010729 * Also set the xpath document in case things like
10730 * key() evaluation are attempted on the predicate
Daniel Veillardf06307e2001-07-03 10:35:50 +000010731 */
10732 newset = xmlXPathNodeSetCreate(NULL);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010733
Daniel Veillardf06307e2001-07-03 10:35:50 +000010734 for (i = 0; i < oldset->nodeNr; i++) {
10735 /*
10736 * Run the evaluation with a node list made of
10737 * a single item in the nodeset.
10738 */
10739 ctxt->context->node = oldset->nodeTab[i];
William M. Brack3794b9e2004-07-13 15:06:20 +000010740 if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) &&
10741 (oldset->nodeTab[i]->doc != NULL))
10742 ctxt->context->doc = oldset->nodeTab[i]->doc;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010743 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10744 valuePush(ctxt, tmp);
10745 ctxt->context->contextSize = oldset->nodeNr;
10746 ctxt->context->proximityPosition = i + 1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010747
Daniel Veillardf06307e2001-07-03 10:35:50 +000010748 if (op->ch2 != -1)
10749 total +=
10750 xmlXPathCompOpEval(ctxt,
10751 &comp->steps[op->ch2]);
William M. Brack2c19a7b2005-04-10 01:03:23 +000010752 if (ctxt->error != XPATH_EXPRESSION_OK) {
10753 xmlXPathFreeNodeSet(newset);
10754 xmlXPathFreeObject(obj);
10755 return(0);
10756 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010757
Daniel Veillardf06307e2001-07-03 10:35:50 +000010758 /*
William M. Brack08171912003-12-29 02:52:11 +000010759 * The result of the evaluation needs to be tested to
10760 * decide whether the filter succeeded or not
Daniel Veillardf06307e2001-07-03 10:35:50 +000010761 */
10762 res = valuePop(ctxt);
10763 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
10764 xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]);
10765 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010766
Daniel Veillardf06307e2001-07-03 10:35:50 +000010767 /*
10768 * Cleanup
10769 */
10770 if (res != NULL)
10771 xmlXPathFreeObject(res);
10772 if (ctxt->value == tmp) {
10773 res = valuePop(ctxt);
10774 xmlXPathFreeObject(res);
10775 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010776
Daniel Veillardf06307e2001-07-03 10:35:50 +000010777 ctxt->context->node = NULL;
10778 }
10779
10780 /*
10781 * The result is used as the new evaluation set.
10782 */
10783 xmlXPathFreeObject(obj);
10784 ctxt->context->node = NULL;
10785 ctxt->context->contextSize = -1;
10786 ctxt->context->proximityPosition = -1;
William M. Brack3794b9e2004-07-13 15:06:20 +000010787 /* may want to move this past the '}' later */
10788 ctxt->context->doc = oldDoc;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010789 valuePush(ctxt, xmlXPathWrapNodeSet(newset));
10790 }
10791 ctxt->context->node = oldnode;
10792 return (total);
10793 }
10794 case XPATH_OP_SORT:
10795 if (op->ch1 != -1)
10796 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010797 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010798 if ((ctxt->value != NULL) &&
10799 (ctxt->value->type == XPATH_NODESET) &&
10800 (ctxt->value->nodesetval != NULL))
10801 xmlXPathNodeSetSort(ctxt->value->nodesetval);
10802 return (total);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010803#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardf06307e2001-07-03 10:35:50 +000010804 case XPATH_OP_RANGETO:{
10805 xmlXPathObjectPtr range;
10806 xmlXPathObjectPtr res, obj;
10807 xmlXPathObjectPtr tmp;
William M. Brack08171912003-12-29 02:52:11 +000010808 xmlLocationSetPtr newlocset = NULL;
10809 xmlLocationSetPtr oldlocset;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010810 xmlNodeSetPtr oldset;
William M. Brack72ee48d2003-12-30 08:30:19 +000010811 int i, j;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010812
Daniel Veillardf06307e2001-07-03 10:35:50 +000010813 if (op->ch1 != -1)
10814 total +=
10815 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
10816 if (op->ch2 == -1)
10817 return (total);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010818
William M. Brack08171912003-12-29 02:52:11 +000010819 if (ctxt->value->type == XPATH_LOCATIONSET) {
10820 /*
10821 * Extract the old locset, and then evaluate the result of the
10822 * expression for all the element in the locset. use it to grow
10823 * up a new locset.
10824 */
10825 CHECK_TYPE0(XPATH_LOCATIONSET);
10826 obj = valuePop(ctxt);
10827 oldlocset = obj->user;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010828
William M. Brack08171912003-12-29 02:52:11 +000010829 if ((oldlocset == NULL) || (oldlocset->locNr == 0)) {
William M. Brack72ee48d2003-12-30 08:30:19 +000010830 ctxt->context->node = NULL;
William M. Brack08171912003-12-29 02:52:11 +000010831 ctxt->context->contextSize = 0;
10832 ctxt->context->proximityPosition = 0;
10833 total += xmlXPathCompOpEval(ctxt,&comp->steps[op->ch2]);
10834 res = valuePop(ctxt);
10835 if (res != NULL)
10836 xmlXPathFreeObject(res);
10837 valuePush(ctxt, obj);
10838 CHECK_ERROR0;
10839 return (total);
10840 }
10841 newlocset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010842
William M. Brack08171912003-12-29 02:52:11 +000010843 for (i = 0; i < oldlocset->locNr; i++) {
Daniel Veillardf06307e2001-07-03 10:35:50 +000010844 /*
William M. Brack08171912003-12-29 02:52:11 +000010845 * Run the evaluation with a node list made of a
10846 * single item in the nodelocset.
Daniel Veillardf06307e2001-07-03 10:35:50 +000010847 */
William M. Brackf7eb7942003-12-31 07:59:17 +000010848 ctxt->context->node = oldlocset->locTab[i]->user;
10849 ctxt->context->contextSize = oldlocset->locNr;
10850 ctxt->context->proximityPosition = i + 1;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010851 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10852 valuePush(ctxt, tmp);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010853
Daniel Veillardf06307e2001-07-03 10:35:50 +000010854 if (op->ch2 != -1)
10855 total +=
10856 xmlXPathCompOpEval(ctxt,
10857 &comp->steps[op->ch2]);
William M. Brack2c19a7b2005-04-10 01:03:23 +000010858 if (ctxt->error != XPATH_EXPRESSION_OK) {
10859 xmlXPathFreeObject(obj);
10860 return(0);
10861 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010862
Daniel Veillardf06307e2001-07-03 10:35:50 +000010863 res = valuePop(ctxt);
William M. Brack72ee48d2003-12-30 08:30:19 +000010864 if (res->type == XPATH_LOCATIONSET) {
10865 xmlLocationSetPtr rloc =
10866 (xmlLocationSetPtr)res->user;
10867 for (j=0; j<rloc->locNr; j++) {
10868 range = xmlXPtrNewRange(
10869 oldlocset->locTab[i]->user,
10870 oldlocset->locTab[i]->index,
10871 rloc->locTab[j]->user2,
10872 rloc->locTab[j]->index2);
10873 if (range != NULL) {
10874 xmlXPtrLocationSetAdd(newlocset, range);
10875 }
10876 }
10877 } else {
10878 range = xmlXPtrNewRangeNodeObject(
10879 (xmlNodePtr)oldlocset->locTab[i]->user, res);
10880 if (range != NULL) {
10881 xmlXPtrLocationSetAdd(newlocset,range);
10882 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010883 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010884
Daniel Veillardf06307e2001-07-03 10:35:50 +000010885 /*
10886 * Cleanup
10887 */
10888 if (res != NULL)
10889 xmlXPathFreeObject(res);
10890 if (ctxt->value == tmp) {
10891 res = valuePop(ctxt);
10892 xmlXPathFreeObject(res);
10893 }
10894
10895 ctxt->context->node = NULL;
10896 }
William M. Brack72ee48d2003-12-30 08:30:19 +000010897 } else { /* Not a location set */
William M. Brack08171912003-12-29 02:52:11 +000010898 CHECK_TYPE0(XPATH_NODESET);
10899 obj = valuePop(ctxt);
10900 oldset = obj->nodesetval;
10901 ctxt->context->node = NULL;
10902
10903 newlocset = xmlXPtrLocationSetCreate(NULL);
10904
10905 if (oldset != NULL) {
10906 for (i = 0; i < oldset->nodeNr; i++) {
10907 /*
10908 * Run the evaluation with a node list made of a single item
10909 * in the nodeset.
10910 */
10911 ctxt->context->node = oldset->nodeTab[i];
10912 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10913 valuePush(ctxt, tmp);
10914
10915 if (op->ch2 != -1)
10916 total +=
10917 xmlXPathCompOpEval(ctxt,
10918 &comp->steps[op->ch2]);
William M. Brack2c19a7b2005-04-10 01:03:23 +000010919 if (ctxt->error != XPATH_EXPRESSION_OK) {
10920 xmlXPathFreeObject(obj);
10921 return(0);
10922 }
William M. Brack08171912003-12-29 02:52:11 +000010923
William M. Brack08171912003-12-29 02:52:11 +000010924 res = valuePop(ctxt);
10925 range =
10926 xmlXPtrNewRangeNodeObject(oldset->nodeTab[i],
10927 res);
10928 if (range != NULL) {
10929 xmlXPtrLocationSetAdd(newlocset, range);
10930 }
10931
10932 /*
10933 * Cleanup
10934 */
10935 if (res != NULL)
10936 xmlXPathFreeObject(res);
10937 if (ctxt->value == tmp) {
10938 res = valuePop(ctxt);
10939 xmlXPathFreeObject(res);
10940 }
10941
10942 ctxt->context->node = NULL;
10943 }
10944 }
Daniel Veillardf06307e2001-07-03 10:35:50 +000010945 }
10946
10947 /*
10948 * The result is used as the new evaluation set.
10949 */
10950 xmlXPathFreeObject(obj);
10951 ctxt->context->node = NULL;
10952 ctxt->context->contextSize = -1;
10953 ctxt->context->proximityPosition = -1;
William M. Brack08171912003-12-29 02:52:11 +000010954 valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset));
Daniel Veillardf06307e2001-07-03 10:35:50 +000010955 return (total);
10956 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010957#endif /* LIBXML_XPTR_ENABLED */
10958 }
10959 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf06307e2001-07-03 10:35:50 +000010960 "XPath: unknown precompiled operation %d\n", op->op);
10961 return (total);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010962}
10963
Daniel Veillard56de87e2005-02-16 00:22:29 +000010964#ifdef XPATH_STREAMING
10965/**
10966 * xmlXPathRunStreamEval:
10967 * @ctxt: the XPath parser context with the compiled expression
10968 *
10969 * Evaluate the Precompiled Streamable XPath expression in the given context.
10970 */
10971static xmlXPathObjectPtr
10972xmlXPathRunStreamEval(xmlXPathContextPtr ctxt, xmlPatternPtr comp) {
10973 int max_depth;
10974 int from_root;
10975 int ret, depth;
William M. Brack12d37ab2005-02-21 13:54:07 +000010976 xmlNodePtr cur = NULL, limit = NULL;
Daniel Veillard56de87e2005-02-16 00:22:29 +000010977 xmlXPathObjectPtr retval;
10978 xmlStreamCtxtPtr patstream;
10979
10980 int nb_nodes = 0;
10981
10982 if ((ctxt == NULL) || (comp == NULL))
10983 return(NULL);
10984 max_depth = xmlPatternMaxDepth(comp);
10985 if (max_depth == -1)
10986 return(NULL);
10987 if (max_depth == -2)
10988 max_depth = 10000;
10989 from_root = xmlPatternFromRoot(comp);
10990 if (from_root < 0)
10991 return(NULL);
Daniel Veillardfa1f77f2005-02-21 10:44:36 +000010992#if 0
10993 printf("stream eval: depth %d from root %d\n", max_depth, from_root);
10994#endif
Daniel Veillard56de87e2005-02-16 00:22:29 +000010995
10996 retval = xmlXPathNewNodeSet(NULL);
10997 if (retval == NULL)
10998 return(NULL);
10999
Daniel Veillard56de87e2005-02-16 00:22:29 +000011000 if ((from_root) && (max_depth == 0)) {
11001 xmlXPathNodeSetAddUnique(retval->nodesetval, (xmlNodePtr) ctxt->doc);
11002 return(retval);
11003 } else if (max_depth == 0) {
11004 xmlXPathNodeSetAddUnique(retval->nodesetval, ctxt->node);
11005 return(retval);
11006 }
11007 if (from_root) {
William M. Brack12d37ab2005-02-21 13:54:07 +000011008 cur = (xmlNodePtr)ctxt->doc;
Daniel Veillard56de87e2005-02-16 00:22:29 +000011009 } else if (ctxt->node != NULL) {
11010 switch (ctxt->node->type) {
11011 case XML_ELEMENT_NODE:
11012 case XML_DOCUMENT_NODE:
11013 case XML_DOCUMENT_FRAG_NODE:
11014 case XML_HTML_DOCUMENT_NODE:
11015#ifdef LIBXML_DOCB_ENABLED
11016 case XML_DOCB_DOCUMENT_NODE:
11017#endif
11018 cur = ctxt->node;
11019 break;
11020 case XML_ATTRIBUTE_NODE:
11021 case XML_TEXT_NODE:
11022 case XML_CDATA_SECTION_NODE:
11023 case XML_ENTITY_REF_NODE:
11024 case XML_ENTITY_NODE:
11025 case XML_PI_NODE:
11026 case XML_COMMENT_NODE:
11027 case XML_NOTATION_NODE:
11028 case XML_DTD_NODE:
11029 case XML_DOCUMENT_TYPE_NODE:
11030 case XML_ELEMENT_DECL:
11031 case XML_ATTRIBUTE_DECL:
11032 case XML_ENTITY_DECL:
11033 case XML_NAMESPACE_DECL:
11034 case XML_XINCLUDE_START:
11035 case XML_XINCLUDE_END:
Daniel Veillard56de87e2005-02-16 00:22:29 +000011036 break;
11037 }
11038 limit = cur;
11039 }
11040 if (cur == NULL)
11041 return(retval);
11042
11043 patstream = xmlPatternGetStreamCtxt(comp);
11044 if (patstream == NULL) {
11045 return(retval);
11046 }
11047
11048 if (from_root) {
11049 ret = xmlStreamPush(patstream, NULL, NULL);
11050 if (ret < 0) {
11051 } else if (ret == 1) {
11052 xmlXPathNodeSetAddUnique(retval->nodesetval, cur);
11053 }
11054 }
11055
11056 depth = 0;
11057 goto scan_children;
11058 do {
11059next_node:
11060 nb_nodes++;
11061 if (cur->type == XML_ELEMENT_NODE) {
11062 ret = xmlStreamPush(patstream, cur->name,
11063 (cur->ns ? cur->ns->href : NULL));
11064 if (ret < 0) {
11065 } else if (ret == 1) {
11066 xmlXPathNodeSetAddUnique(retval->nodesetval, cur);
11067 }
11068 if ((cur->children == NULL) || (depth >= max_depth)) {
11069 ret = xmlStreamPop(patstream);
11070 }
11071 }
11072
11073scan_children:
11074 if ((cur->children != NULL) && (depth < max_depth)) {
11075 /*
11076 * Do not descend on entities declarations
11077 */
11078 if (cur->children->type != XML_ENTITY_DECL) {
11079 cur = cur->children;
11080 depth++;
11081 /*
11082 * Skip DTDs
11083 */
11084 if (cur->type != XML_DTD_NODE)
11085 continue;
11086 }
11087 }
11088
11089 if (cur == limit)
11090 break;
11091
11092 while (cur->next != NULL) {
11093 cur = cur->next;
11094 if ((cur->type != XML_ENTITY_DECL) &&
11095 (cur->type != XML_DTD_NODE))
11096 goto next_node;
11097 }
11098
11099 do {
11100 ret = xmlStreamPop(patstream);
11101 cur = cur->parent;
11102 depth--;
11103 if ((cur == NULL) || (cur == limit))
11104 goto done;
11105 if (cur->next != NULL) {
11106 cur = cur->next;
11107 break;
11108 }
11109 } while (cur != NULL);
11110
11111 } while ((cur != NULL) && (depth >= 0));
11112done:
Daniel Veillardfa1f77f2005-02-21 10:44:36 +000011113#if 0
11114 printf("stream eval: checked %d nodes selected %d\n",
11115 nb_nodes, retval->nodesetval->nodeNr);
11116#endif
Daniel Veillard56de87e2005-02-16 00:22:29 +000011117 xmlFreeStreamCtxt(patstream);
11118 return(retval);
11119}
11120#endif /* XPATH_STREAMING */
11121
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011122/**
11123 * xmlXPathRunEval:
11124 * @ctxt: the XPath parser context with the compiled expression
11125 *
11126 * Evaluate the Precompiled XPath expression in the given context.
11127 */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000011128static void
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011129xmlXPathRunEval(xmlXPathParserContextPtr ctxt) {
11130 xmlXPathCompExprPtr comp;
11131
11132 if ((ctxt == NULL) || (ctxt->comp == NULL))
11133 return;
11134
11135 if (ctxt->valueTab == NULL) {
11136 /* Allocate the value stack */
11137 ctxt->valueTab = (xmlXPathObjectPtr *)
11138 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
11139 if (ctxt->valueTab == NULL) {
Daniel Veillardd96f6d32003-10-07 21:25:12 +000011140 xmlXPathPErrMemory(ctxt, "creating evaluation context\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011141 xmlFree(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011142 }
11143 ctxt->valueNr = 0;
11144 ctxt->valueMax = 10;
11145 ctxt->value = NULL;
11146 }
Daniel Veillard56de87e2005-02-16 00:22:29 +000011147#ifdef XPATH_STREAMING
11148 if (ctxt->comp->stream) {
11149 xmlXPathObjectPtr ret;
11150 ret = xmlXPathRunStreamEval(ctxt->context, ctxt->comp->stream);
11151 if (ret != NULL) {
11152 valuePush(ctxt, ret);
11153 return;
11154 }
11155 }
11156#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011157 comp = ctxt->comp;
Aleksey Sanin29b6f762002-05-05 06:59:57 +000011158 if(comp->last < 0) {
11159 xmlGenericError(xmlGenericErrorContext,
11160 "xmlXPathRunEval: last is less than zero\n");
11161 return;
11162 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011163 xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]);
11164}
11165
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011166/************************************************************************
11167 * *
11168 * Public interfaces *
11169 * *
11170 ************************************************************************/
11171
11172/**
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000011173 * xmlXPathEvalPredicate:
11174 * @ctxt: the XPath context
11175 * @res: the Predicate Expression evaluation result
11176 *
11177 * Evaluate a predicate result for the current node.
11178 * A PredicateExpr is evaluated by evaluating the Expr and converting
11179 * the result to a boolean. If the result is a number, the result will
11180 * be converted to true if the number is equal to the position of the
11181 * context node in the context node list (as returned by the position
11182 * function) and will be converted to false otherwise; if the result
11183 * is not a number, then the result will be converted as if by a call
11184 * to the boolean function.
11185 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011186 * Returns 1 if predicate is true, 0 otherwise
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000011187 */
11188int
11189xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) {
Daniel Veillardce682bc2004-11-05 17:22:25 +000011190 if ((ctxt == NULL) || (res == NULL)) return(0);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000011191 switch (res->type) {
11192 case XPATH_BOOLEAN:
11193 return(res->boolval);
11194 case XPATH_NUMBER:
11195 return(res->floatval == ctxt->proximityPosition);
11196 case XPATH_NODESET:
11197 case XPATH_XSLT_TREE:
Daniel Veillardd8df6c02001-04-05 16:54:14 +000011198 if (res->nodesetval == NULL)
11199 return(0);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000011200 return(res->nodesetval->nodeNr != 0);
11201 case XPATH_STRING:
11202 return((res->stringval != NULL) &&
11203 (xmlStrlen(res->stringval) != 0));
11204 default:
11205 STRANGE
11206 }
11207 return(0);
11208}
11209
11210/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011211 * xmlXPathEvaluatePredicateResult:
11212 * @ctxt: the XPath Parser context
11213 * @res: the Predicate Expression evaluation result
11214 *
11215 * Evaluate a predicate result for the current node.
11216 * A PredicateExpr is evaluated by evaluating the Expr and converting
11217 * the result to a boolean. If the result is a number, the result will
11218 * be converted to true if the number is equal to the position of the
11219 * context node in the context node list (as returned by the position
11220 * function) and will be converted to false otherwise; if the result
11221 * is not a number, then the result will be converted as if by a call
11222 * to the boolean function.
11223 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011224 * Returns 1 if predicate is true, 0 otherwise
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011225 */
11226int
11227xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt,
11228 xmlXPathObjectPtr res) {
Daniel Veillardce682bc2004-11-05 17:22:25 +000011229 if ((ctxt == NULL) || (res == NULL)) return(0);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011230 switch (res->type) {
11231 case XPATH_BOOLEAN:
11232 return(res->boolval);
11233 case XPATH_NUMBER:
Daniel Veillard9ea62312004-04-29 14:04:09 +000011234#if defined(__BORLANDC__) || (defined(_MSC_VER) && (_MSC_VER == 1200))
Daniel Veillard7c4eb632004-04-19 21:29:12 +000011235 return((res->floatval == ctxt->context->proximityPosition) &&
11236 (!xmlXPathIsNaN(res->floatval))); /* MSC pbm Mark Vakoc !*/
Daniel Veillard2582a332004-04-18 19:49:46 +000011237#else
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011238 return(res->floatval == ctxt->context->proximityPosition);
Daniel Veillard2582a332004-04-18 19:49:46 +000011239#endif
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011240 case XPATH_NODESET:
11241 case XPATH_XSLT_TREE:
Daniel Veillard73639a72001-04-10 14:31:39 +000011242 if (res->nodesetval == NULL)
Daniel Veillard911f49a2001-04-07 15:39:35 +000011243 return(0);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011244 return(res->nodesetval->nodeNr != 0);
11245 case XPATH_STRING:
11246 return((res->stringval != NULL) &&
11247 (xmlStrlen(res->stringval) != 0));
William M. Brack08171912003-12-29 02:52:11 +000011248#ifdef LIBXML_XPTR_ENABLED
11249 case XPATH_LOCATIONSET:{
11250 xmlLocationSetPtr ptr = res->user;
11251 if (ptr == NULL)
11252 return(0);
11253 return (ptr->locNr != 0);
11254 }
11255#endif
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011256 default:
11257 STRANGE
11258 }
11259 return(0);
11260}
11261
Daniel Veillard56de87e2005-02-16 00:22:29 +000011262#ifdef XPATH_STREAMING
11263/**
11264 * xmlXPathTryStreamCompile:
11265 * @ctxt: an XPath context
11266 * @str: the XPath expression
11267 *
11268 * Try to compile the XPath expression as a streamable subset.
11269 *
11270 * Returns the compiled expression or NULL if failed to compile.
11271 */
11272static xmlXPathCompExprPtr
11273xmlXPathTryStreamCompile(xmlXPathContextPtr ctxt, const xmlChar *str) {
11274 /*
11275 * Optimization: use streaming patterns when the XPath expression can
11276 * be compiled to a stream lookup
11277 */
11278 xmlPatternPtr stream;
11279 xmlXPathCompExprPtr comp;
11280 xmlDictPtr dict = NULL;
11281 const xmlChar **namespaces = NULL;
11282 xmlNsPtr ns;
11283 int i, j;
11284
11285 if ((!xmlStrchr(str, '[')) && (!xmlStrchr(str, '(')) &&
11286 (!xmlStrchr(str, '@'))) {
11287 if (ctxt != NULL) {
11288 dict = ctxt->dict;
11289 if (ctxt->nsNr > 0) {
11290 namespaces = xmlMalloc(2 * (ctxt->nsNr + 1));
11291 if (namespaces == NULL) {
11292 xmlXPathErrMemory(ctxt, "allocating namespaces array\n");
11293 return(NULL);
11294 }
11295 for (i = 0, j = 0; (j < ctxt->nsNr); j++) {
11296 ns = ctxt->namespaces[j];
11297 namespaces[i++] = ns->href;
11298 namespaces[i++] = ns->prefix;
11299 }
11300 namespaces[i++] = NULL;
11301 namespaces[i++] = NULL;
11302 }
11303 }
11304
11305 stream = xmlPatterncompile(str, dict, 0, &namespaces[0]);
11306 if ((stream != NULL) && (xmlPatternStreamable(stream) == 1)) {
11307 comp = xmlXPathNewCompExpr();
11308 if (comp == NULL) {
11309 xmlXPathErrMemory(ctxt, "allocating streamable expression\n");
11310 return(NULL);
11311 }
11312 comp->stream = stream;
11313 comp->dict = dict;
11314 if (comp->dict)
11315 xmlDictReference(comp->dict);
11316 return(comp);
11317 }
11318 xmlFreePattern(stream);
11319 }
11320 return(NULL);
11321}
11322#endif /* XPATH_STREAMING */
11323
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011324/**
Daniel Veillard4773df22004-01-23 13:15:13 +000011325 * xmlXPathCtxtCompile:
11326 * @ctxt: an XPath context
11327 * @str: the XPath expression
11328 *
11329 * Compile an XPath expression
11330 *
11331 * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL.
11332 * the caller has to free the object.
11333 */
11334xmlXPathCompExprPtr
11335xmlXPathCtxtCompile(xmlXPathContextPtr ctxt, const xmlChar *str) {
11336 xmlXPathParserContextPtr pctxt;
11337 xmlXPathCompExprPtr comp;
11338
Daniel Veillard56de87e2005-02-16 00:22:29 +000011339#ifdef XPATH_STREAMING
11340 comp = xmlXPathTryStreamCompile(ctxt, str);
11341 if (comp != NULL)
11342 return(comp);
11343#endif
11344
Daniel Veillard4773df22004-01-23 13:15:13 +000011345 xmlXPathInit();
11346
11347 pctxt = xmlXPathNewParserContext(str, ctxt);
11348 xmlXPathCompileExpr(pctxt);
11349
11350 if( pctxt->error != XPATH_EXPRESSION_OK )
11351 {
11352 xmlXPathFreeParserContext(pctxt);
11353 return (0);
11354 }
11355
11356 if (*pctxt->cur != 0) {
11357 /*
11358 * aleksey: in some cases this line prints *second* error message
11359 * (see bug #78858) and probably this should be fixed.
11360 * However, we are not sure that all error messages are printed
11361 * out in other places. It's not critical so we leave it as-is for now
11362 */
11363 xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
11364 comp = NULL;
11365 } else {
11366 comp = pctxt->comp;
11367 pctxt->comp = NULL;
11368 }
11369 xmlXPathFreeParserContext(pctxt);
11370 if (comp != NULL) {
11371 comp->expr = xmlStrdup(str);
11372#ifdef DEBUG_EVAL_COUNTS
11373 comp->string = xmlStrdup(str);
11374 comp->nb = 0;
11375#endif
11376 }
11377 return(comp);
11378}
11379
11380/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011381 * xmlXPathCompile:
11382 * @str: the XPath expression
11383 *
11384 * Compile an XPath expression
11385 *
Daniel Veillard591b4be2003-02-09 23:33:36 +000011386 * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL.
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011387 * the caller has to free the object.
11388 */
11389xmlXPathCompExprPtr
11390xmlXPathCompile(const xmlChar *str) {
Daniel Veillard4773df22004-01-23 13:15:13 +000011391 return(xmlXPathCtxtCompile(NULL, str));
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011392}
11393
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011394/**
11395 * xmlXPathCompiledEval:
11396 * @comp: the compiled XPath expression
Owen Taylor3473f882001-02-23 17:55:21 +000011397 * @ctx: the XPath context
11398 *
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011399 * Evaluate the Precompiled XPath expression in the given context.
Owen Taylor3473f882001-02-23 17:55:21 +000011400 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011401 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Owen Taylor3473f882001-02-23 17:55:21 +000011402 * the caller has to free the object.
11403 */
11404xmlXPathObjectPtr
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011405xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) {
Owen Taylor3473f882001-02-23 17:55:21 +000011406 xmlXPathParserContextPtr ctxt;
11407 xmlXPathObjectPtr res, tmp, init = NULL;
11408 int stack = 0;
Daniel Veillard81463942001-10-16 12:34:39 +000011409#ifndef LIBXML_THREAD_ENABLED
11410 static int reentance = 0;
11411#endif
Owen Taylor3473f882001-02-23 17:55:21 +000011412
William M. Brackf13f77f2004-11-12 16:03:48 +000011413 CHECK_CTXT(ctx)
11414
11415 if (comp == NULL)
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011416 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011417 xmlXPathInit();
11418
Daniel Veillard81463942001-10-16 12:34:39 +000011419#ifndef LIBXML_THREAD_ENABLED
11420 reentance++;
11421 if (reentance > 1)
11422 xmlXPathDisableOptimizer = 1;
11423#endif
11424
Daniel Veillardf06307e2001-07-03 10:35:50 +000011425#ifdef DEBUG_EVAL_COUNTS
11426 comp->nb++;
11427 if ((comp->string != NULL) && (comp->nb > 100)) {
11428 fprintf(stderr, "100 x %s\n", comp->string);
11429 comp->nb = 0;
11430 }
11431#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011432 ctxt = xmlXPathCompParserContext(comp, ctx);
11433 xmlXPathRunEval(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011434
11435 if (ctxt->value == NULL) {
11436 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011437 "xmlXPathCompiledEval: evaluation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +000011438 res = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000011439 } else {
11440 res = valuePop(ctxt);
11441 }
11442
Daniel Veillardf06307e2001-07-03 10:35:50 +000011443
Owen Taylor3473f882001-02-23 17:55:21 +000011444 do {
11445 tmp = valuePop(ctxt);
11446 if (tmp != NULL) {
11447 if (tmp != init)
11448 stack++;
11449 xmlXPathFreeObject(tmp);
11450 }
11451 } while (tmp != NULL);
11452 if ((stack != 0) && (res != NULL)) {
11453 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011454 "xmlXPathCompiledEval: %d object left on the stack\n",
Owen Taylor3473f882001-02-23 17:55:21 +000011455 stack);
11456 }
11457 if (ctxt->error != XPATH_EXPRESSION_OK) {
11458 xmlXPathFreeObject(res);
11459 res = NULL;
11460 }
11461
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011462
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011463 ctxt->comp = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011464 xmlXPathFreeParserContext(ctxt);
Daniel Veillard81463942001-10-16 12:34:39 +000011465#ifndef LIBXML_THREAD_ENABLED
11466 reentance--;
11467#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011468 return(res);
11469}
11470
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011471/**
11472 * xmlXPathEvalExpr:
11473 * @ctxt: the XPath Parser context
11474 *
11475 * Parse and evaluate an XPath expression in the given context,
11476 * then push the result on the context stack
11477 */
11478void
11479xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) {
Daniel Veillard56de87e2005-02-16 00:22:29 +000011480#ifdef XPATH_STREAMING
11481 xmlXPathCompExprPtr comp;
11482#endif
11483
Daniel Veillarda82b1822004-11-08 16:24:57 +000011484 if (ctxt == NULL) return;
Daniel Veillard56de87e2005-02-16 00:22:29 +000011485
11486#ifdef XPATH_STREAMING
11487 comp = xmlXPathTryStreamCompile(ctxt->context, ctxt->base);
11488 if (comp != NULL) {
11489 if (ctxt->comp != NULL)
11490 xmlXPathFreeCompExpr(ctxt->comp);
11491 ctxt->comp = comp;
11492 if (ctxt->cur != NULL)
11493 while (*ctxt->cur != 0) ctxt->cur++;
11494 } else
11495#endif
11496 {
11497 xmlXPathCompileExpr(ctxt);
11498 }
Aleksey Sanin50fe8b12002-05-07 16:21:36 +000011499 CHECK_ERROR;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000011500 xmlXPathRunEval(ctxt);
11501}
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011502
11503/**
11504 * xmlXPathEval:
11505 * @str: the XPath expression
11506 * @ctx: the XPath context
11507 *
11508 * Evaluate the XPath Location Path in the given context.
11509 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011510 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011511 * the caller has to free the object.
11512 */
11513xmlXPathObjectPtr
11514xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) {
11515 xmlXPathParserContextPtr ctxt;
11516 xmlXPathObjectPtr res, tmp, init = NULL;
11517 int stack = 0;
11518
William M. Brackf13f77f2004-11-12 16:03:48 +000011519 CHECK_CTXT(ctx)
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011520
William M. Brackf13f77f2004-11-12 16:03:48 +000011521 xmlXPathInit();
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011522
11523 ctxt = xmlXPathNewParserContext(str, ctx);
11524 xmlXPathEvalExpr(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011525
11526 if (ctxt->value == NULL) {
11527 xmlGenericError(xmlGenericErrorContext,
11528 "xmlXPathEval: evaluation failed\n");
11529 res = NULL;
Daniel Veillard56de87e2005-02-16 00:22:29 +000011530 } else if ((*ctxt->cur != 0) && (ctxt->comp != NULL)
11531#ifdef XPATH_STREAMING
11532 && (ctxt->comp->stream == NULL)
11533#endif
11534 ) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +000011535 xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
11536 res = NULL;
11537 } else {
11538 res = valuePop(ctxt);
11539 }
11540
11541 do {
11542 tmp = valuePop(ctxt);
11543 if (tmp != NULL) {
11544 if (tmp != init)
11545 stack++;
11546 xmlXPathFreeObject(tmp);
11547 }
11548 } while (tmp != NULL);
11549 if ((stack != 0) && (res != NULL)) {
11550 xmlGenericError(xmlGenericErrorContext,
11551 "xmlXPathEval: %d object left on the stack\n",
11552 stack);
11553 }
11554 if (ctxt->error != XPATH_EXPRESSION_OK) {
11555 xmlXPathFreeObject(res);
11556 res = NULL;
11557 }
11558
Owen Taylor3473f882001-02-23 17:55:21 +000011559 xmlXPathFreeParserContext(ctxt);
11560 return(res);
11561}
11562
11563/**
11564 * xmlXPathEvalExpression:
11565 * @str: the XPath expression
11566 * @ctxt: the XPath context
11567 *
11568 * Evaluate the XPath expression in the given context.
11569 *
11570 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
11571 * the caller has to free the object.
11572 */
11573xmlXPathObjectPtr
11574xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) {
11575 xmlXPathParserContextPtr pctxt;
11576 xmlXPathObjectPtr res, tmp;
11577 int stack = 0;
11578
William M. Brackf13f77f2004-11-12 16:03:48 +000011579 CHECK_CTXT(ctxt)
Owen Taylor3473f882001-02-23 17:55:21 +000011580
William M. Brackf13f77f2004-11-12 16:03:48 +000011581 xmlXPathInit();
Owen Taylor3473f882001-02-23 17:55:21 +000011582
11583 pctxt = xmlXPathNewParserContext(str, ctxt);
11584 xmlXPathEvalExpr(pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011585
11586 if (*pctxt->cur != 0) {
11587 xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
11588 res = NULL;
11589 } else {
11590 res = valuePop(pctxt);
11591 }
11592 do {
11593 tmp = valuePop(pctxt);
11594 if (tmp != NULL) {
11595 xmlXPathFreeObject(tmp);
11596 stack++;
11597 }
11598 } while (tmp != NULL);
11599 if ((stack != 0) && (res != NULL)) {
11600 xmlGenericError(xmlGenericErrorContext,
11601 "xmlXPathEvalExpression: %d object left on the stack\n",
11602 stack);
11603 }
11604 xmlXPathFreeParserContext(pctxt);
11605 return(res);
11606}
11607
Daniel Veillard42766c02002-08-22 20:52:17 +000011608/************************************************************************
11609 * *
11610 * Extra functions not pertaining to the XPath spec *
11611 * *
11612 ************************************************************************/
11613/**
11614 * xmlXPathEscapeUriFunction:
11615 * @ctxt: the XPath Parser context
11616 * @nargs: the number of arguments
11617 *
11618 * Implement the escape-uri() XPath function
11619 * string escape-uri(string $str, bool $escape-reserved)
11620 *
11621 * This function applies the URI escaping rules defined in section 2 of [RFC
11622 * 2396] to the string supplied as $uri-part, which typically represents all
11623 * or part of a URI. The effect of the function is to replace any special
11624 * character in the string by an escape sequence of the form %xx%yy...,
11625 * where xxyy... is the hexadecimal representation of the octets used to
11626 * represent the character in UTF-8.
11627 *
11628 * The set of characters that are escaped depends on the setting of the
11629 * boolean argument $escape-reserved.
11630 *
11631 * If $escape-reserved is true, all characters are escaped other than lower
11632 * case letters a-z, upper case letters A-Z, digits 0-9, and the characters
11633 * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!"
11634 * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only
11635 * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and
11636 * A-F).
11637 *
11638 * If $escape-reserved is false, the behavior differs in that characters
11639 * referred to in [RFC 2396] as reserved characters are not escaped. These
11640 * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",".
11641 *
11642 * [RFC 2396] does not define whether escaped URIs should use lower case or
11643 * upper case for hexadecimal digits. To ensure that escaped URIs can be
11644 * compared using string comparison functions, this function must always use
11645 * the upper-case letters A-F.
11646 *
11647 * Generally, $escape-reserved should be set to true when escaping a string
11648 * that is to form a single part of a URI, and to false when escaping an
11649 * entire URI or URI reference.
11650 *
11651 * In the case of non-ascii characters, the string is encoded according to
11652 * utf-8 and then converted according to RFC 2396.
11653 *
11654 * Examples
11655 * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true())
11656 * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean"
11657 * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false())
11658 * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean"
11659 *
11660 */
Daniel Veillard118aed72002-09-24 14:13:13 +000011661static void
Daniel Veillard42766c02002-08-22 20:52:17 +000011662xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) {
11663 xmlXPathObjectPtr str;
11664 int escape_reserved;
11665 xmlBufferPtr target;
11666 xmlChar *cptr;
11667 xmlChar escape[4];
11668
11669 CHECK_ARITY(2);
11670
11671 escape_reserved = xmlXPathPopBoolean(ctxt);
11672
11673 CAST_TO_STRING;
11674 str = valuePop(ctxt);
11675
11676 target = xmlBufferCreate();
11677
11678 escape[0] = '%';
11679 escape[3] = 0;
11680
11681 if (target) {
11682 for (cptr = str->stringval; *cptr; cptr++) {
11683 if ((*cptr >= 'A' && *cptr <= 'Z') ||
11684 (*cptr >= 'a' && *cptr <= 'z') ||
11685 (*cptr >= '0' && *cptr <= '9') ||
11686 *cptr == '-' || *cptr == '_' || *cptr == '.' ||
11687 *cptr == '!' || *cptr == '~' || *cptr == '*' ||
11688 *cptr == '\''|| *cptr == '(' || *cptr == ')' ||
11689 (*cptr == '%' &&
11690 ((cptr[1] >= 'A' && cptr[1] <= 'F') ||
11691 (cptr[1] >= 'a' && cptr[1] <= 'f') ||
11692 (cptr[1] >= '0' && cptr[1] <= '9')) &&
11693 ((cptr[2] >= 'A' && cptr[2] <= 'F') ||
11694 (cptr[2] >= 'a' && cptr[2] <= 'f') ||
11695 (cptr[2] >= '0' && cptr[2] <= '9'))) ||
11696 (!escape_reserved &&
11697 (*cptr == ';' || *cptr == '/' || *cptr == '?' ||
11698 *cptr == ':' || *cptr == '@' || *cptr == '&' ||
11699 *cptr == '=' || *cptr == '+' || *cptr == '$' ||
11700 *cptr == ','))) {
11701 xmlBufferAdd(target, cptr, 1);
11702 } else {
11703 if ((*cptr >> 4) < 10)
11704 escape[1] = '0' + (*cptr >> 4);
11705 else
11706 escape[1] = 'A' - 10 + (*cptr >> 4);
11707 if ((*cptr & 0xF) < 10)
11708 escape[2] = '0' + (*cptr & 0xF);
11709 else
11710 escape[2] = 'A' - 10 + (*cptr & 0xF);
11711
11712 xmlBufferAdd(target, &escape[0], 3);
11713 }
11714 }
11715 }
11716 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
11717 xmlBufferFree(target);
11718 xmlXPathFreeObject(str);
11719}
11720
Owen Taylor3473f882001-02-23 17:55:21 +000011721/**
11722 * xmlXPathRegisterAllFunctions:
11723 * @ctxt: the XPath context
11724 *
11725 * Registers all default XPath functions in this context
11726 */
11727void
11728xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt)
11729{
11730 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean",
11731 xmlXPathBooleanFunction);
11732 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling",
11733 xmlXPathCeilingFunction);
11734 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count",
11735 xmlXPathCountFunction);
11736 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat",
11737 xmlXPathConcatFunction);
11738 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains",
11739 xmlXPathContainsFunction);
11740 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id",
11741 xmlXPathIdFunction);
11742 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false",
11743 xmlXPathFalseFunction);
11744 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor",
11745 xmlXPathFloorFunction);
11746 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last",
11747 xmlXPathLastFunction);
11748 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang",
11749 xmlXPathLangFunction);
11750 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name",
11751 xmlXPathLocalNameFunction);
11752 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not",
11753 xmlXPathNotFunction);
11754 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name",
11755 xmlXPathNameFunction);
11756 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri",
11757 xmlXPathNamespaceURIFunction);
11758 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space",
11759 xmlXPathNormalizeFunction);
11760 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number",
11761 xmlXPathNumberFunction);
11762 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position",
11763 xmlXPathPositionFunction);
11764 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round",
11765 xmlXPathRoundFunction);
11766 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string",
11767 xmlXPathStringFunction);
11768 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length",
11769 xmlXPathStringLengthFunction);
11770 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with",
11771 xmlXPathStartsWithFunction);
11772 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring",
11773 xmlXPathSubstringFunction);
11774 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before",
11775 xmlXPathSubstringBeforeFunction);
11776 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after",
11777 xmlXPathSubstringAfterFunction);
11778 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum",
11779 xmlXPathSumFunction);
11780 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true",
11781 xmlXPathTrueFunction);
11782 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate",
11783 xmlXPathTranslateFunction);
Daniel Veillard42766c02002-08-22 20:52:17 +000011784
11785 xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri",
11786 (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions",
11787 xmlXPathEscapeUriFunction);
Owen Taylor3473f882001-02-23 17:55:21 +000011788}
11789
11790#endif /* LIBXML_XPATH_ENABLED */
Daniel Veillard5d4644e2005-04-01 13:11:58 +000011791#define bottom_xpath
11792#include "elfgcchack.h"