blob: f4f79b38e0b8796f1e38bbfbf7a5b9e387be547c [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 *
15 * 14 Nov 2000 ht - truncated declaration of xmlXPathEvalRelativeLocationPath
16 * for VMS
17 */
18
Daniel Veillard34ce8be2002-03-18 19:37:11 +000019#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000020#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000021#ifdef LIBXML_XPATH_ENABLED
22
Owen Taylor3473f882001-02-23 17:55:21 +000023#include <string.h>
24
25#ifdef HAVE_SYS_TYPES_H
26#include <sys/types.h>
27#endif
28#ifdef HAVE_MATH_H
29#include <math.h>
30#endif
31#ifdef HAVE_FLOAT_H
32#include <float.h>
33#endif
Owen Taylor3473f882001-02-23 17:55:21 +000034#ifdef HAVE_CTYPE_H
35#include <ctype.h>
36#endif
Daniel Veillard5792e162001-04-30 17:44:45 +000037#ifdef HAVE_SIGNAL_H
Daniel Veillardb45c43b2001-04-28 17:02:11 +000038#include <signal.h>
Daniel Veillardb45c43b2001-04-28 17:02:11 +000039#endif
Owen Taylor3473f882001-02-23 17:55:21 +000040
41#include <libxml/xmlmemory.h>
42#include <libxml/tree.h>
43#include <libxml/valid.h>
44#include <libxml/xpath.h>
45#include <libxml/xpathInternals.h>
46#include <libxml/parserInternals.h>
47#include <libxml/hash.h>
48#ifdef LIBXML_XPTR_ENABLED
49#include <libxml/xpointer.h>
50#endif
51#ifdef LIBXML_DEBUG_ENABLED
52#include <libxml/debugXML.h>
53#endif
54#include <libxml/xmlerror.h>
Daniel Veillard81463942001-10-16 12:34:39 +000055#include <libxml/threads.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000056#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000057
58/* #define DEBUG */
59/* #define DEBUG_STEP */
Daniel Veillardf06307e2001-07-03 10:35:50 +000060/* #define DEBUG_STEP_NTH */
Owen Taylor3473f882001-02-23 17:55:21 +000061/* #define DEBUG_EXPR */
Daniel Veillardf06307e2001-07-03 10:35:50 +000062/* #define DEBUG_EVAL_COUNTS */
Owen Taylor3473f882001-02-23 17:55:21 +000063
Daniel Veillard5792e162001-04-30 17:44:45 +000064double xmlXPathDivideBy(double f, double fzero);
Owen Taylor3473f882001-02-23 17:55:21 +000065
Daniel Veillard20ee8c02001-10-05 09:18:14 +000066static xmlNs xmlXPathXMLNamespaceStruct = {
67 NULL,
68 XML_NAMESPACE_DECL,
69 XML_XML_NAMESPACE,
70 BAD_CAST "xml"
71};
72static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct;
Daniel Veillardda423da2002-04-10 19:25:38 +000073#ifndef LIBXML_THREAD_ENABLED
Daniel Veillard81463942001-10-16 12:34:39 +000074/*
75 * Optimizer is disabled only when threaded apps are detected while
76 * the library ain't compiled for thread safety.
77 */
78static int xmlXPathDisableOptimizer = 0;
79#endif
Daniel Veillard20ee8c02001-10-05 09:18:14 +000080
Daniel Veillard9e7160d2001-03-18 23:17:47 +000081/************************************************************************
82 * *
83 * Floating point stuff *
84 * *
85 ************************************************************************/
86
Daniel Veillardc0631a62001-09-20 13:56:06 +000087#ifndef TRIO_REPLACE_STDIO
Daniel Veillardcda96922001-08-21 10:56:31 +000088#define TRIO_PUBLIC static
Daniel Veillardc0631a62001-09-20 13:56:06 +000089#endif
Daniel Veillardcda96922001-08-21 10:56:31 +000090#include "trionan.c"
91
Owen Taylor3473f882001-02-23 17:55:21 +000092/*
Owen Taylor3473f882001-02-23 17:55:21 +000093 * The lack of portability of this section of the libc is annoying !
94 */
95double xmlXPathNAN = 0;
96double xmlXPathPINF = 1;
97double xmlXPathNINF = -1;
Daniel Veillard5fc1f082002-03-27 09:05:40 +000098double xmlXPathNZERO = 0;
Daniel Veillard20ee8c02001-10-05 09:18:14 +000099static int xmlXPathInitialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000100
Owen Taylor3473f882001-02-23 17:55:21 +0000101/**
102 * xmlXPathInit:
103 *
104 * Initialize the XPath environment
105 */
106void
107xmlXPathInit(void) {
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000108 if (xmlXPathInitialized) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000109
Bjorn Reese45029602001-08-21 09:23:53 +0000110 xmlXPathPINF = trio_pinf();
111 xmlXPathNINF = trio_ninf();
112 xmlXPathNAN = trio_nan();
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000113 xmlXPathNZERO = trio_nzero();
Owen Taylor3473f882001-02-23 17:55:21 +0000114
Daniel Veillard20ee8c02001-10-05 09:18:14 +0000115 xmlXPathInitialized = 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000116}
117
Daniel Veillardcda96922001-08-21 10:56:31 +0000118/**
119 * xmlXPathIsNaN:
120 * @val: a double value
121 *
122 * Provides a portable isnan() function to detect whether a double
123 * is a NotaNumber. Based on trio code
124 * http://sourceforge.net/projects/ctrio/
125 *
126 * Returns 1 if the value is a NaN, 0 otherwise
127 */
128int
129xmlXPathIsNaN(double val) {
130 return(trio_isnan(val));
131}
132
133/**
134 * xmlXPathIsInf:
135 * @val: a double value
136 *
137 * Provides a portable isinf() function to detect whether a double
138 * is a +Infinite or -Infinite. Based on trio code
139 * http://sourceforge.net/projects/ctrio/
140 *
141 * Returns 1 vi the value is +Infinite, -1 if -Infinite, 0 otherwise
142 */
143int
144xmlXPathIsInf(double val) {
145 return(trio_isinf(val));
146}
147
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000148/**
149 * xmlXPathGetSign:
150 * @val: a double value
151 *
152 * Provides a portable function to detect the sign of a double
153 * Modified from trio code
154 * http://sourceforge.net/projects/ctrio/
155 *
156 * Returns 1 if the value is Negative, 0 if positive
157 */
Daniel Veillard21458c82002-03-27 16:12:22 +0000158static int
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000159xmlXPathGetSign(double val) {
Daniel Veillard21458c82002-03-27 16:12:22 +0000160 return(trio_signbit(val));
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000161}
162
163
Owen Taylor3473f882001-02-23 17:55:21 +0000164/************************************************************************
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000165 * *
166 * Parser Types *
167 * *
168 ************************************************************************/
169
170/*
171 * Types are private:
172 */
173
174typedef enum {
175 XPATH_OP_END=0,
176 XPATH_OP_AND,
177 XPATH_OP_OR,
178 XPATH_OP_EQUAL,
179 XPATH_OP_CMP,
180 XPATH_OP_PLUS,
181 XPATH_OP_MULT,
182 XPATH_OP_UNION,
183 XPATH_OP_ROOT,
184 XPATH_OP_NODE,
185 XPATH_OP_RESET,
186 XPATH_OP_COLLECT,
187 XPATH_OP_VALUE,
188 XPATH_OP_VARIABLE,
189 XPATH_OP_FUNCTION,
190 XPATH_OP_ARG,
191 XPATH_OP_PREDICATE,
Daniel Veillardd8df6c02001-04-05 16:54:14 +0000192 XPATH_OP_FILTER,
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000193 XPATH_OP_SORT
194#ifdef LIBXML_XPTR_ENABLED
195 ,XPATH_OP_RANGETO
196#endif
197} xmlXPathOp;
198
199typedef enum {
200 AXIS_ANCESTOR = 1,
201 AXIS_ANCESTOR_OR_SELF,
202 AXIS_ATTRIBUTE,
203 AXIS_CHILD,
204 AXIS_DESCENDANT,
205 AXIS_DESCENDANT_OR_SELF,
206 AXIS_FOLLOWING,
207 AXIS_FOLLOWING_SIBLING,
208 AXIS_NAMESPACE,
209 AXIS_PARENT,
210 AXIS_PRECEDING,
211 AXIS_PRECEDING_SIBLING,
212 AXIS_SELF
213} xmlXPathAxisVal;
214
215typedef enum {
216 NODE_TEST_NONE = 0,
217 NODE_TEST_TYPE = 1,
218 NODE_TEST_PI = 2,
219 NODE_TEST_ALL = 3,
220 NODE_TEST_NS = 4,
221 NODE_TEST_NAME = 5
222} xmlXPathTestVal;
223
224typedef enum {
225 NODE_TYPE_NODE = 0,
226 NODE_TYPE_COMMENT = XML_COMMENT_NODE,
227 NODE_TYPE_TEXT = XML_TEXT_NODE,
228 NODE_TYPE_PI = XML_PI_NODE
229} xmlXPathTypeVal;
230
231
232typedef struct _xmlXPathStepOp xmlXPathStepOp;
233typedef xmlXPathStepOp *xmlXPathStepOpPtr;
234struct _xmlXPathStepOp {
235 xmlXPathOp op;
236 int ch1;
237 int ch2;
238 int value;
239 int value2;
240 int value3;
241 void *value4;
242 void *value5;
Daniel Veillarde39a93d2001-04-28 14:35:02 +0000243 void *cache;
Daniel Veillard42596ad2001-05-22 16:57:14 +0000244 void *cacheURI;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000245};
246
247struct _xmlXPathCompExpr {
248 int nbStep;
249 int maxStep;
250 xmlXPathStepOp *steps; /* ops for computation */
251 int last;
Daniel Veillardf06307e2001-07-03 10:35:50 +0000252#ifdef DEBUG_EVAL_COUNTS
253 int nb;
254 xmlChar *string;
255#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000256};
257
258/************************************************************************
259 * *
260 * Parser Type functions *
261 * *
262 ************************************************************************/
263
264/**
265 * xmlXPathNewCompExpr:
266 *
267 * Create a new Xpath component
268 *
269 * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error
270 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000271static xmlXPathCompExprPtr
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000272xmlXPathNewCompExpr(void) {
273 xmlXPathCompExprPtr cur;
274
275 cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr));
276 if (cur == NULL) {
277 xmlGenericError(xmlGenericErrorContext,
278 "xmlXPathNewCompExpr : malloc failed\n");
279 return(NULL);
280 }
281 memset(cur, 0, sizeof(xmlXPathCompExpr));
282 cur->maxStep = 10;
283 cur->nbStep = 0;
284 cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep *
285 sizeof(xmlXPathStepOp));
286 if (cur->steps == NULL) {
287 xmlGenericError(xmlGenericErrorContext,
288 "xmlXPathNewCompExpr : malloc failed\n");
289 xmlFree(cur);
290 return(NULL);
291 }
292 memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp));
293 cur->last = -1;
Daniel Veillardf06307e2001-07-03 10:35:50 +0000294#ifdef DEBUG_EVAL_COUNTS
295 cur->nb = 0;
296#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000297 return(cur);
298}
299
300/**
301 * xmlXPathFreeCompExpr:
302 * @comp: an XPATH comp
303 *
304 * Free up the memory allocated by @comp
305 */
306void
Daniel Veillardf06307e2001-07-03 10:35:50 +0000307xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp)
308{
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000309 xmlXPathStepOpPtr op;
310 int i;
311
312 if (comp == NULL)
Daniel Veillardf06307e2001-07-03 10:35:50 +0000313 return;
314 for (i = 0; i < comp->nbStep; i++) {
315 op = &comp->steps[i];
316 if (op->value4 != NULL) {
317 if (op->op == XPATH_OP_VALUE)
318 xmlXPathFreeObject(op->value4);
319 else
320 xmlFree(op->value4);
321 }
322 if (op->value5 != NULL)
323 xmlFree(op->value5);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000324 }
325 if (comp->steps != NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +0000326 xmlFree(comp->steps);
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000327 }
Daniel Veillardf06307e2001-07-03 10:35:50 +0000328#ifdef DEBUG_EVAL_COUNTS
329 if (comp->string != NULL) {
330 xmlFree(comp->string);
331 }
332#endif
333
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000334 xmlFree(comp);
335}
336
337/**
338 * xmlXPathCompExprAdd:
339 * @comp: the compiled expression
340 * @ch1: first child index
341 * @ch2: second child index
342 * @op: an op
343 * @value: the first int value
344 * @value2: the second int value
345 * @value3: the third int value
346 * @value4: the first string value
347 * @value5: the second string value
348 *
349 * Add an step to an XPath Compiled Expression
350 *
351 * Returns -1 in case of failure, the index otherwise
352 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000353static int
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000354xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2,
355 xmlXPathOp op, int value,
356 int value2, int value3, void *value4, void *value5) {
357 if (comp->nbStep >= comp->maxStep) {
358 xmlXPathStepOp *real;
359
360 comp->maxStep *= 2;
361 real = (xmlXPathStepOp *) xmlRealloc(comp->steps,
362 comp->maxStep * sizeof(xmlXPathStepOp));
363 if (real == NULL) {
364 comp->maxStep /= 2;
365 xmlGenericError(xmlGenericErrorContext,
366 "xmlXPathCompExprAdd : realloc failed\n");
367 return(-1);
368 }
369 comp->steps = real;
370 }
371 comp->last = comp->nbStep;
372 comp->steps[comp->nbStep].ch1 = ch1;
373 comp->steps[comp->nbStep].ch2 = ch2;
374 comp->steps[comp->nbStep].op = op;
375 comp->steps[comp->nbStep].value = value;
376 comp->steps[comp->nbStep].value2 = value2;
377 comp->steps[comp->nbStep].value3 = value3;
378 comp->steps[comp->nbStep].value4 = value4;
379 comp->steps[comp->nbStep].value5 = value5;
Daniel Veillarde39a93d2001-04-28 14:35:02 +0000380 comp->steps[comp->nbStep].cache = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000381 return(comp->nbStep++);
382}
383
Daniel Veillardf06307e2001-07-03 10:35:50 +0000384/**
385 * xmlXPathCompSwap:
386 * @comp: the compiled expression
387 * @op: operation index
388 *
389 * Swaps 2 operations in the compiled expression
Daniel Veillardf06307e2001-07-03 10:35:50 +0000390 */
391static void
392xmlXPathCompSwap(xmlXPathStepOpPtr op) {
393 int tmp;
394
Daniel Veillardbc6f7592002-04-16 07:49:59 +0000395#ifndef LIBXML_THREAD_ENABLED
Daniel Veillard81463942001-10-16 12:34:39 +0000396 /*
397 * Since this manipulates possibly shared variables, this is
398 * disable if one detects that the library is used in a multithreaded
399 * application
400 */
401 if (xmlXPathDisableOptimizer)
402 return;
403#endif
404
Daniel Veillardf06307e2001-07-03 10:35:50 +0000405 tmp = op->ch1;
406 op->ch1 = op->ch2;
407 op->ch2 = tmp;
408}
409
Daniel Veillardd8df6c02001-04-05 16:54:14 +0000410#define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \
411 xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \
412 (op), (val), (val2), (val3), (val4), (val5))
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000413#define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \
414 xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \
415 (op), (val), (val2), (val3), (val4), (val5))
416
417#define PUSH_LEAVE_EXPR(op, val, val2) \
418xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL)
419
420#define PUSH_UNARY_EXPR(op, ch, val, val2) \
421xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL)
422
423#define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \
424xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), (val), (val2), 0 ,NULL ,NULL)
425
426/************************************************************************
Owen Taylor3473f882001-02-23 17:55:21 +0000427 * *
428 * Debugging related functions *
429 * *
430 ************************************************************************/
431
432#define TODO \
433 xmlGenericError(xmlGenericErrorContext, \
434 "Unimplemented block at %s:%d\n", \
435 __FILE__, __LINE__);
436
437#define STRANGE \
438 xmlGenericError(xmlGenericErrorContext, \
439 "Internal error at %s:%d\n", \
440 __FILE__, __LINE__);
441
442#ifdef LIBXML_DEBUG_ENABLED
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000443static void
444xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000445 int i;
446 char shift[100];
447
448 for (i = 0;((i < depth) && (i < 25));i++)
449 shift[2 * i] = shift[2 * i + 1] = ' ';
450 shift[2 * i] = shift[2 * i + 1] = 0;
451 if (cur == NULL) {
452 fprintf(output, shift);
453 fprintf(output, "Node is NULL !\n");
454 return;
455
456 }
457
458 if ((cur->type == XML_DOCUMENT_NODE) ||
459 (cur->type == XML_HTML_DOCUMENT_NODE)) {
460 fprintf(output, shift);
461 fprintf(output, " /\n");
462 } else if (cur->type == XML_ATTRIBUTE_NODE)
463 xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth);
464 else
465 xmlDebugDumpOneNode(output, cur, depth);
466}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000467static void
468xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000469 xmlNodePtr tmp;
470 int i;
471 char shift[100];
472
473 for (i = 0;((i < depth) && (i < 25));i++)
474 shift[2 * i] = shift[2 * i + 1] = ' ';
475 shift[2 * i] = shift[2 * i + 1] = 0;
476 if (cur == NULL) {
477 fprintf(output, shift);
478 fprintf(output, "Node is NULL !\n");
479 return;
480
481 }
482
483 while (cur != NULL) {
484 tmp = cur;
485 cur = cur->next;
486 xmlDebugDumpOneNode(output, tmp, depth);
487 }
488}
Owen Taylor3473f882001-02-23 17:55:21 +0000489
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000490static void
491xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000492 int i;
493 char shift[100];
494
495 for (i = 0;((i < depth) && (i < 25));i++)
496 shift[2 * i] = shift[2 * i + 1] = ' ';
497 shift[2 * i] = shift[2 * i + 1] = 0;
498
499 if (cur == NULL) {
500 fprintf(output, shift);
501 fprintf(output, "NodeSet is NULL !\n");
502 return;
503
504 }
505
Daniel Veillard911f49a2001-04-07 15:39:35 +0000506 if (cur != NULL) {
507 fprintf(output, "Set contains %d nodes:\n", cur->nodeNr);
508 for (i = 0;i < cur->nodeNr;i++) {
509 fprintf(output, shift);
510 fprintf(output, "%d", i + 1);
511 xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1);
512 }
Owen Taylor3473f882001-02-23 17:55:21 +0000513 }
514}
515
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000516static void
517xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) {
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000518 int i;
519 char shift[100];
520
521 for (i = 0;((i < depth) && (i < 25));i++)
522 shift[2 * i] = shift[2 * i + 1] = ' ';
523 shift[2 * i] = shift[2 * i + 1] = 0;
524
525 if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) {
526 fprintf(output, shift);
527 fprintf(output, "Value Tree is NULL !\n");
528 return;
529
530 }
531
532 fprintf(output, shift);
533 fprintf(output, "%d", i + 1);
534 xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1);
535}
Owen Taylor3473f882001-02-23 17:55:21 +0000536#if defined(LIBXML_XPTR_ENABLED)
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000537static void
538xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000539 int i;
540 char shift[100];
541
542 for (i = 0;((i < depth) && (i < 25));i++)
543 shift[2 * i] = shift[2 * i + 1] = ' ';
544 shift[2 * i] = shift[2 * i + 1] = 0;
545
546 if (cur == NULL) {
547 fprintf(output, shift);
548 fprintf(output, "LocationSet is NULL !\n");
549 return;
550
551 }
552
553 for (i = 0;i < cur->locNr;i++) {
554 fprintf(output, shift);
555 fprintf(output, "%d : ", i + 1);
556 xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1);
557 }
558}
Daniel Veillard017b1082001-06-21 11:20:21 +0000559#endif /* LIBXML_XPTR_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000560
Daniel Veillardafcbe1c2001-03-19 10:57:13 +0000561/**
562 * xmlXPathDebugDumpObject:
563 * @output: the FILE * to dump the output
564 * @cur: the object to inspect
565 * @depth: indentation level
566 *
567 * Dump the content of the object for debugging purposes
568 */
569void
570xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) {
Owen Taylor3473f882001-02-23 17:55:21 +0000571 int i;
572 char shift[100];
573
574 for (i = 0;((i < depth) && (i < 25));i++)
575 shift[2 * i] = shift[2 * i + 1] = ' ';
576 shift[2 * i] = shift[2 * i + 1] = 0;
577
578 fprintf(output, shift);
579
580 if (cur == NULL) {
581 fprintf(output, "Object is empty (NULL)\n");
582 return;
583 }
584 switch(cur->type) {
585 case XPATH_UNDEFINED:
586 fprintf(output, "Object is uninitialized\n");
587 break;
588 case XPATH_NODESET:
589 fprintf(output, "Object is a Node Set :\n");
590 xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth);
591 break;
592 case XPATH_XSLT_TREE:
593 fprintf(output, "Object is an XSLT value tree :\n");
Daniel Veillardf7cd4812001-02-23 18:44:52 +0000594 xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth);
Owen Taylor3473f882001-02-23 17:55:21 +0000595 break;
596 case XPATH_BOOLEAN:
597 fprintf(output, "Object is a Boolean : ");
598 if (cur->boolval) fprintf(output, "true\n");
599 else fprintf(output, "false\n");
600 break;
601 case XPATH_NUMBER:
Daniel Veillardcda96922001-08-21 10:56:31 +0000602 switch (xmlXPathIsInf(cur->floatval)) {
Daniel Veillard357c9602001-05-03 10:49:20 +0000603 case 1:
Daniel Veillard5fc1f082002-03-27 09:05:40 +0000604 fprintf(output, "Object is a number : Infinity\n");
Daniel Veillard357c9602001-05-03 10:49:20 +0000605 break;
606 case -1:
607 fprintf(output, "Object is a number : -Infinity\n");
608 break;
609 default:
Daniel Veillardcda96922001-08-21 10:56:31 +0000610 if (xmlXPathIsNaN(cur->floatval)) {
Daniel Veillard357c9602001-05-03 10:49:20 +0000611 fprintf(output, "Object is a number : NaN\n");
Daniel Veillardd30be4a2002-03-28 18:25:31 +0000612 } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) {
613 fprintf(output, "Object is a number : 0\n");
Daniel Veillard357c9602001-05-03 10:49:20 +0000614 } else {
615 fprintf(output, "Object is a number : %0g\n", cur->floatval);
616 }
617 }
Owen Taylor3473f882001-02-23 17:55:21 +0000618 break;
619 case XPATH_STRING:
620 fprintf(output, "Object is a string : ");
621 xmlDebugDumpString(output, cur->stringval);
622 fprintf(output, "\n");
623 break;
624 case XPATH_POINT:
625 fprintf(output, "Object is a point : index %d in node", cur->index);
626 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1);
627 fprintf(output, "\n");
628 break;
629 case XPATH_RANGE:
630 if ((cur->user2 == NULL) ||
631 ((cur->user2 == cur->user) && (cur->index == cur->index2))) {
632 fprintf(output, "Object is a collapsed range :\n");
633 fprintf(output, shift);
634 if (cur->index >= 0)
635 fprintf(output, "index %d in ", cur->index);
636 fprintf(output, "node\n");
637 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user,
638 depth + 1);
639 } else {
640 fprintf(output, "Object is a range :\n");
641 fprintf(output, shift);
642 fprintf(output, "From ");
643 if (cur->index >= 0)
644 fprintf(output, "index %d in ", cur->index);
645 fprintf(output, "node\n");
646 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user,
647 depth + 1);
648 fprintf(output, shift);
649 fprintf(output, "To ");
650 if (cur->index2 >= 0)
651 fprintf(output, "index %d in ", cur->index2);
652 fprintf(output, "node\n");
653 xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2,
654 depth + 1);
655 fprintf(output, "\n");
656 }
657 break;
658 case XPATH_LOCATIONSET:
659#if defined(LIBXML_XPTR_ENABLED)
660 fprintf(output, "Object is a Location Set:\n");
661 xmlXPathDebugDumpLocationSet(output,
662 (xmlLocationSetPtr) cur->user, depth);
663#endif
664 break;
665 case XPATH_USERS:
666 fprintf(output, "Object is user defined\n");
667 break;
668 }
669}
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000670
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000671static void
672xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp,
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000673 xmlXPathStepOpPtr op, int depth) {
674 int i;
675 char shift[100];
676
677 for (i = 0;((i < depth) && (i < 25));i++)
678 shift[2 * i] = shift[2 * i + 1] = ' ';
679 shift[2 * i] = shift[2 * i + 1] = 0;
680
681 fprintf(output, shift);
682 if (op == NULL) {
683 fprintf(output, "Step is NULL\n");
684 return;
685 }
686 switch (op->op) {
687 case XPATH_OP_END:
688 fprintf(output, "END"); break;
689 case XPATH_OP_AND:
690 fprintf(output, "AND"); break;
691 case XPATH_OP_OR:
692 fprintf(output, "OR"); break;
693 case XPATH_OP_EQUAL:
694 if (op->value)
695 fprintf(output, "EQUAL =");
696 else
697 fprintf(output, "EQUAL !=");
698 break;
699 case XPATH_OP_CMP:
700 if (op->value)
701 fprintf(output, "CMP <");
702 else
703 fprintf(output, "CMP >");
704 if (!op->value2)
705 fprintf(output, "=");
706 break;
707 case XPATH_OP_PLUS:
708 if (op->value == 0)
709 fprintf(output, "PLUS -");
710 else if (op->value == 1)
711 fprintf(output, "PLUS +");
712 else if (op->value == 2)
713 fprintf(output, "PLUS unary -");
714 else if (op->value == 3)
715 fprintf(output, "PLUS unary - -");
716 break;
717 case XPATH_OP_MULT:
718 if (op->value == 0)
719 fprintf(output, "MULT *");
720 else if (op->value == 1)
721 fprintf(output, "MULT div");
722 else
723 fprintf(output, "MULT mod");
724 break;
725 case XPATH_OP_UNION:
726 fprintf(output, "UNION"); break;
727 case XPATH_OP_ROOT:
728 fprintf(output, "ROOT"); break;
729 case XPATH_OP_NODE:
730 fprintf(output, "NODE"); break;
731 case XPATH_OP_RESET:
732 fprintf(output, "RESET"); break;
733 case XPATH_OP_SORT:
734 fprintf(output, "SORT"); break;
735 case XPATH_OP_COLLECT: {
736 xmlXPathAxisVal axis = op->value;
737 xmlXPathTestVal test = op->value2;
738 xmlXPathTypeVal type = op->value3;
739 const xmlChar *prefix = op->value4;
740 const xmlChar *name = op->value5;
741
742 fprintf(output, "COLLECT ");
743 switch (axis) {
744 case AXIS_ANCESTOR:
745 fprintf(output, " 'ancestors' "); break;
746 case AXIS_ANCESTOR_OR_SELF:
747 fprintf(output, " 'ancestors-or-self' "); break;
748 case AXIS_ATTRIBUTE:
749 fprintf(output, " 'attributes' "); break;
750 case AXIS_CHILD:
751 fprintf(output, " 'child' "); break;
752 case AXIS_DESCENDANT:
753 fprintf(output, " 'descendant' "); break;
754 case AXIS_DESCENDANT_OR_SELF:
755 fprintf(output, " 'descendant-or-self' "); break;
756 case AXIS_FOLLOWING:
757 fprintf(output, " 'following' "); break;
758 case AXIS_FOLLOWING_SIBLING:
759 fprintf(output, " 'following-siblings' "); break;
760 case AXIS_NAMESPACE:
761 fprintf(output, " 'namespace' "); break;
762 case AXIS_PARENT:
763 fprintf(output, " 'parent' "); break;
764 case AXIS_PRECEDING:
765 fprintf(output, " 'preceding' "); break;
766 case AXIS_PRECEDING_SIBLING:
767 fprintf(output, " 'preceding-sibling' "); break;
768 case AXIS_SELF:
769 fprintf(output, " 'self' "); break;
770 }
771 switch (test) {
772 case NODE_TEST_NONE:
773 fprintf(output, "'none' "); break;
774 case NODE_TEST_TYPE:
775 fprintf(output, "'type' "); break;
776 case NODE_TEST_PI:
777 fprintf(output, "'PI' "); break;
778 case NODE_TEST_ALL:
779 fprintf(output, "'all' "); break;
780 case NODE_TEST_NS:
781 fprintf(output, "'namespace' "); break;
782 case NODE_TEST_NAME:
783 fprintf(output, "'name' "); break;
784 }
785 switch (type) {
786 case NODE_TYPE_NODE:
787 fprintf(output, "'node' "); break;
788 case NODE_TYPE_COMMENT:
789 fprintf(output, "'comment' "); break;
790 case NODE_TYPE_TEXT:
791 fprintf(output, "'text' "); break;
792 case NODE_TYPE_PI:
793 fprintf(output, "'PI' "); break;
794 }
795 if (prefix != NULL)
796 fprintf(output, "%s:", prefix);
797 if (name != NULL)
798 fprintf(output, "%s", name);
799 break;
800
801 }
802 case XPATH_OP_VALUE: {
803 xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4;
804
805 fprintf(output, "ELEM ");
806 xmlXPathDebugDumpObject(output, object, 0);
807 goto finish;
808 }
809 case XPATH_OP_VARIABLE: {
810 const xmlChar *prefix = op->value5;
811 const xmlChar *name = op->value4;
812
813 if (prefix != NULL)
814 fprintf(output, "VARIABLE %s:%s", prefix, name);
815 else
816 fprintf(output, "VARIABLE %s", name);
817 break;
818 }
819 case XPATH_OP_FUNCTION: {
820 int nbargs = op->value;
821 const xmlChar *prefix = op->value5;
822 const xmlChar *name = op->value4;
823
824 if (prefix != NULL)
825 fprintf(output, "FUNCTION %s:%s(%d args)",
826 prefix, name, nbargs);
827 else
828 fprintf(output, "FUNCTION %s(%d args)", name, nbargs);
829 break;
830 }
831 case XPATH_OP_ARG: fprintf(output, "ARG"); break;
832 case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +0000833 case XPATH_OP_FILTER: fprintf(output, "FILTER"); break;
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +0000834#ifdef LIBXML_XPTR_ENABLED
835 case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break;
836#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000837 default:
838 fprintf(output, "UNKNOWN %d\n", op->op); return;
839 }
840 fprintf(output, "\n");
841finish:
842 if (op->ch1 >= 0)
843 xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1);
844 if (op->ch2 >= 0)
845 xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1);
846}
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000847
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000848/**
849 * xmlXPathDebugDumpCompExpr:
850 * @output: the FILE * for the output
851 * @comp: the precompiled XPath expression
852 * @depth: the indentation level.
853 *
854 * Dumps the tree of the compiled XPath expression.
855 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000856void
857xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp,
858 int depth) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +0000859 int i;
860 char shift[100];
861
862 for (i = 0;((i < depth) && (i < 25));i++)
863 shift[2 * i] = shift[2 * i + 1] = ' ';
864 shift[2 * i] = shift[2 * i + 1] = 0;
865
866 fprintf(output, shift);
867
868 if (comp == NULL) {
869 fprintf(output, "Compiled Expression is NULL\n");
870 return;
871 }
872 fprintf(output, "Compiled Expression : %d elements\n",
873 comp->nbStep);
874 i = comp->last;
875 xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1);
876}
Daniel Veillard017b1082001-06-21 11:20:21 +0000877#endif /* LIBXML_DEBUG_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000878
879/************************************************************************
880 * *
881 * Parser stacks related functions and macros *
882 * *
883 ************************************************************************/
884
885/*
886 * Generic function for accessing stacks in the Parser Context
887 */
888
889#define PUSH_AND_POP(type, name) \
890extern int name##Push(xmlXPathParserContextPtr ctxt, type value) { \
891 if (ctxt->name##Nr >= ctxt->name##Max) { \
892 ctxt->name##Max *= 2; \
893 ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \
894 ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \
895 if (ctxt->name##Tab == NULL) { \
896 xmlGenericError(xmlGenericErrorContext, \
897 "realloc failed !\n"); \
898 return(0); \
899 } \
900 } \
901 ctxt->name##Tab[ctxt->name##Nr] = value; \
902 ctxt->name = value; \
903 return(ctxt->name##Nr++); \
904} \
905extern type name##Pop(xmlXPathParserContextPtr ctxt) { \
906 type ret; \
907 if (ctxt->name##Nr <= 0) return(0); \
908 ctxt->name##Nr--; \
909 if (ctxt->name##Nr > 0) \
910 ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \
911 else \
912 ctxt->name = NULL; \
913 ret = ctxt->name##Tab[ctxt->name##Nr]; \
914 ctxt->name##Tab[ctxt->name##Nr] = 0; \
915 return(ret); \
916} \
917
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000918/**
919 * valuePop:
920 * @ctxt: an XPath evaluation context
921 *
922 * Pops the top XPath object from the value stack
923 *
924 * Returns the XPath object just removed
925 */
926/**
927 * valuePush:
928 * @ctxt: an XPath evaluation context
929 * @value: the XPath object
930 *
931 * Pushes a new XPath object on top of the value stack
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000932 *
933 * returns the number of items on the value stack
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000934 */
Owen Taylor3473f882001-02-23 17:55:21 +0000935PUSH_AND_POP(xmlXPathObjectPtr, value)
936
Thomas Broyerf06a3d82001-07-16 04:52:57 +0000937/**
938 * xmlXPathPopBoolean:
939 * @ctxt: an XPath parser context
940 *
941 * Pops a boolean from the stack, handling conversion if needed.
942 * Check error with #xmlXPathCheckError.
943 *
944 * Returns the boolean
945 */
946int
947xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) {
948 xmlXPathObjectPtr obj;
949 int ret;
950
951 obj = valuePop(ctxt);
952 if (obj == NULL) {
953 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
954 return(0);
955 }
956 ret = xmlXPathCastToBoolean(obj);
957 xmlXPathFreeObject(obj);
958 return(ret);
959}
960
961/**
962 * xmlXPathPopNumber:
963 * @ctxt: an XPath parser context
964 *
965 * Pops a number from the stack, handling conversion if needed.
966 * Check error with #xmlXPathCheckError.
967 *
968 * Returns the number
969 */
970double
971xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) {
972 xmlXPathObjectPtr obj;
973 double ret;
974
975 obj = valuePop(ctxt);
976 if (obj == NULL) {
977 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
978 return(0);
979 }
980 ret = xmlXPathCastToNumber(obj);
981 xmlXPathFreeObject(obj);
982 return(ret);
983}
984
985/**
986 * xmlXPathPopString:
987 * @ctxt: an XPath parser context
988 *
989 * Pops a string from the stack, handling conversion if needed.
990 * Check error with #xmlXPathCheckError.
991 *
992 * Returns the string
993 */
994xmlChar *
995xmlXPathPopString (xmlXPathParserContextPtr ctxt) {
996 xmlXPathObjectPtr obj;
997 xmlChar * ret;
998
999 obj = valuePop(ctxt);
1000 if (obj == NULL) {
1001 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1002 return(NULL);
1003 }
1004 ret = xmlXPathCastToString(obj);
1005 /* TODO: needs refactoring somewhere else */
1006 if (obj->stringval == ret)
1007 obj->stringval = NULL;
1008 xmlXPathFreeObject(obj);
1009 return(ret);
1010}
1011
1012/**
1013 * xmlXPathPopNodeSet:
1014 * @ctxt: an XPath parser context
1015 *
1016 * Pops a node-set from the stack, handling conversion if needed.
1017 * Check error with #xmlXPathCheckError.
1018 *
1019 * Returns the node-set
1020 */
1021xmlNodeSetPtr
1022xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) {
1023 xmlXPathObjectPtr obj;
1024 xmlNodeSetPtr ret;
1025
1026 if (ctxt->value == NULL) {
1027 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1028 return(NULL);
1029 }
1030 if (!xmlXPathStackIsNodeSet(ctxt)) {
1031 xmlXPathSetTypeError(ctxt);
1032 return(NULL);
1033 }
1034 obj = valuePop(ctxt);
1035 ret = obj->nodesetval;
1036 xmlXPathFreeNodeSetList(obj);
1037 return(ret);
1038}
1039
1040/**
1041 * xmlXPathPopExternal:
1042 * @ctxt: an XPath parser context
1043 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001044 * Pops an external object from the stack, handling conversion if needed.
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001045 * Check error with #xmlXPathCheckError.
1046 *
1047 * Returns the object
1048 */
1049void *
1050xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) {
1051 xmlXPathObjectPtr obj;
1052 void * ret;
1053
1054 if (ctxt->value == NULL) {
1055 xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND);
1056 return(NULL);
1057 }
1058 if (ctxt->value->type != XPATH_USERS) {
1059 xmlXPathSetTypeError(ctxt);
1060 return(NULL);
1061 }
1062 obj = valuePop(ctxt);
1063 ret = obj->user;
1064 xmlXPathFreeObject(obj);
1065 return(ret);
1066}
1067
Owen Taylor3473f882001-02-23 17:55:21 +00001068/*
1069 * Macros for accessing the content. Those should be used only by the parser,
1070 * and not exported.
1071 *
1072 * Dirty macros, i.e. one need to make assumption on the context to use them
1073 *
1074 * CUR_PTR return the current pointer to the xmlChar to be parsed.
1075 * CUR returns the current xmlChar value, i.e. a 8 bit value
1076 * in ISO-Latin or UTF-8.
1077 * This should be used internally by the parser
1078 * only to compare to ASCII values otherwise it would break when
1079 * running with UTF-8 encoding.
1080 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
1081 * to compare on ASCII based substring.
1082 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
1083 * strings within the parser.
1084 * CURRENT Returns the current char value, with the full decoding of
1085 * UTF-8 if we are using this mode. It returns an int.
1086 * NEXT Skip to the next character, this does the proper decoding
1087 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
1088 * It returns the pointer to the current xmlChar.
1089 */
1090
1091#define CUR (*ctxt->cur)
1092#define SKIP(val) ctxt->cur += (val)
1093#define NXT(val) ctxt->cur[(val)]
1094#define CUR_PTR ctxt->cur
Daniel Veillard61d80a22001-04-27 17:13:01 +00001095#define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l)
1096
1097#define COPY_BUF(l,b,i,v) \
1098 if (l == 1) b[i++] = (xmlChar) v; \
1099 else i += xmlCopyChar(l,&b[i],v)
1100
1101#define NEXTL(l) ctxt->cur += l
Owen Taylor3473f882001-02-23 17:55:21 +00001102
1103#define SKIP_BLANKS \
1104 while (IS_BLANK(*(ctxt->cur))) NEXT
1105
1106#define CURRENT (*ctxt->cur)
1107#define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1108
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001109
1110#ifndef DBL_DIG
1111#define DBL_DIG 16
1112#endif
1113#ifndef DBL_EPSILON
1114#define DBL_EPSILON 1E-9
1115#endif
1116
1117#define UPPER_DOUBLE 1E9
1118#define LOWER_DOUBLE 1E-5
1119
1120#define INTEGER_DIGITS DBL_DIG
1121#define FRACTION_DIGITS (DBL_DIG + 1)
1122#define EXPONENT_DIGITS (3 + 2)
1123
1124/**
1125 * xmlXPathFormatNumber:
1126 * @number: number to format
1127 * @buffer: output buffer
1128 * @buffersize: size of output buffer
1129 *
1130 * Convert the number into a string representation.
1131 */
1132static void
1133xmlXPathFormatNumber(double number, char buffer[], int buffersize)
1134{
Daniel Veillardcda96922001-08-21 10:56:31 +00001135 switch (xmlXPathIsInf(number)) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001136 case 1:
Daniel Veillard5fc1f082002-03-27 09:05:40 +00001137 if (buffersize > (int)sizeof("Infinity"))
1138 sprintf(buffer, "Infinity");
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001139 break;
1140 case -1:
1141 if (buffersize > (int)sizeof("-Infinity"))
1142 sprintf(buffer, "-Infinity");
1143 break;
1144 default:
Daniel Veillardcda96922001-08-21 10:56:31 +00001145 if (xmlXPathIsNaN(number)) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001146 if (buffersize > (int)sizeof("NaN"))
1147 sprintf(buffer, "NaN");
Daniel Veillardd30be4a2002-03-28 18:25:31 +00001148 } else if (number == 0 && xmlXPathGetSign(number) != 0) {
1149 sprintf(buffer, "0");
Daniel Veillard28cac6b2002-03-19 11:25:30 +00001150 } else if (number == ((int) number)) {
1151 char work[30];
1152 char *ptr, *cur;
1153 int res, value = (int) number;
1154
1155 ptr = &buffer[0];
1156 if (value < 0) {
1157 *ptr++ = '-';
1158 value = -value;
1159 }
1160 if (value == 0) {
1161 *ptr++ = '0';
1162 } else {
1163 cur = &work[0];
1164 while (value != 0) {
1165 res = value % 10;
1166 value = value / 10;
1167 *cur++ = '0' + res;
1168 }
1169 cur--;
1170 while ((cur >= &work[0]) && (ptr - buffer < buffersize)) {
1171 *ptr++ = *cur--;
1172 }
1173 }
1174 if (ptr - buffer < buffersize) {
1175 *ptr = 0;
1176 } else if (buffersize > 0) {
1177 ptr--;
1178 *ptr = 0;
1179 }
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001180 } else {
Bjorn Reese70a9da52001-04-21 16:57:29 +00001181 /* 3 is sign, decimal point, and terminating zero */
1182 char work[DBL_DIG + EXPONENT_DIGITS + 3];
1183 int integer_place, fraction_place;
1184 char *ptr;
1185 char *after_fraction;
1186 double absolute_value;
1187 int size;
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001188
Bjorn Reese70a9da52001-04-21 16:57:29 +00001189 absolute_value = fabs(number);
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001190
Bjorn Reese70a9da52001-04-21 16:57:29 +00001191 /*
1192 * First choose format - scientific or regular floating point.
1193 * In either case, result is in work, and after_fraction points
1194 * just past the fractional part.
1195 */
1196 if ( ((absolute_value > UPPER_DOUBLE) ||
1197 (absolute_value < LOWER_DOUBLE)) &&
1198 (absolute_value != 0.0) ) {
1199 /* Use scientific notation */
1200 integer_place = DBL_DIG + EXPONENT_DIGITS + 1;
1201 fraction_place = DBL_DIG - 1;
1202 snprintf(work, sizeof(work),"%*.*e",
1203 integer_place, fraction_place, number);
1204 after_fraction = strchr(work + DBL_DIG, 'e');
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001205 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00001206 else {
1207 /* Use regular notation */
Daniel Veillard56f06462001-06-24 21:34:03 +00001208 if (absolute_value > 0.0)
1209 integer_place = 1 + (int)log10(absolute_value);
1210 else
Daniel Veillarda3067d12001-06-24 21:39:39 +00001211 integer_place = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00001212 fraction_place = (integer_place > 0)
1213 ? DBL_DIG - integer_place
1214 : DBL_DIG;
1215 size = snprintf(work, sizeof(work), "%0.*f",
1216 fraction_place, number);
1217 after_fraction = work + size;
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001218 }
1219
Bjorn Reese70a9da52001-04-21 16:57:29 +00001220 /* Remove fractional trailing zeroes */
1221 ptr = after_fraction;
1222 while (*(--ptr) == '0')
1223 ;
1224 if (*ptr != '.')
1225 ptr++;
1226 strcpy(ptr, after_fraction);
1227
1228 /* Finally copy result back to caller */
1229 size = strlen(work) + 1;
1230 if (size > buffersize) {
1231 work[buffersize - 1] = 0;
1232 size = buffersize;
1233 }
1234 memcpy(buffer, work, size);
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001235 }
1236 break;
1237 }
1238}
1239
Owen Taylor3473f882001-02-23 17:55:21 +00001240/************************************************************************
1241 * *
1242 * Error handling routines *
1243 * *
1244 ************************************************************************/
1245
1246
Daniel Veillardb44025c2001-10-11 22:55:55 +00001247static const char *xmlXPathErrorMessages[] = {
Owen Taylor3473f882001-02-23 17:55:21 +00001248 "Ok",
1249 "Number encoding",
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001250 "Unfinished literal",
1251 "Start of literal",
Owen Taylor3473f882001-02-23 17:55:21 +00001252 "Expected $ for variable reference",
1253 "Undefined variable",
1254 "Invalid predicate",
1255 "Invalid expression",
1256 "Missing closing curly brace",
1257 "Unregistered function",
1258 "Invalid operand",
1259 "Invalid type",
1260 "Invalid number of arguments",
1261 "Invalid context size",
1262 "Invalid context position",
1263 "Memory allocation error",
1264 "Syntax error",
1265 "Resource error",
1266 "Sub resource error",
Daniel Veillard61d80a22001-04-27 17:13:01 +00001267 "Undefined namespace prefix",
1268 "Encoding error",
1269 "Char out of XML range"
Owen Taylor3473f882001-02-23 17:55:21 +00001270};
1271
1272/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001273 * xmlXPatherror:
Owen Taylor3473f882001-02-23 17:55:21 +00001274 * @ctxt: the XPath Parser context
1275 * @file: the file name
1276 * @line: the line number
1277 * @no: the error number
1278 *
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001279 * Formats an error message.
Owen Taylor3473f882001-02-23 17:55:21 +00001280 */
1281void
Daniel Veillard21458c82002-03-27 16:12:22 +00001282xmlXPatherror(xmlXPathParserContextPtr ctxt, ATTRIBUTE_UNUSED const char *file,
1283 ATTRIBUTE_UNUSED int line, int no) {
Owen Taylor3473f882001-02-23 17:55:21 +00001284 int n;
1285 const xmlChar *cur;
1286 const xmlChar *base;
1287
Daniel Veillard5fc1f082002-03-27 09:05:40 +00001288/* xmlGenericError(xmlGenericErrorContext,
Owen Taylor3473f882001-02-23 17:55:21 +00001289 "Error %s:%d: %s\n", file, line,
1290 xmlXPathErrorMessages[no]);
Daniel Veillard5fc1f082002-03-27 09:05:40 +00001291*/
1292 xmlGenericError(xmlGenericErrorContext,
1293 "Error %s\n", xmlXPathErrorMessages[no]);
Owen Taylor3473f882001-02-23 17:55:21 +00001294
1295 cur = ctxt->cur;
1296 base = ctxt->base;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00001297 if ((cur == NULL) || (base == NULL))
1298 return;
1299
Owen Taylor3473f882001-02-23 17:55:21 +00001300 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
1301 cur--;
1302 }
1303 n = 0;
1304 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
1305 cur--;
1306 if ((*cur == '\n') || (*cur == '\r')) cur++;
1307 base = cur;
1308 n = 0;
1309 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
1310 xmlGenericError(xmlGenericErrorContext, "%c", (unsigned char) *cur++);
1311 n++;
1312 }
1313 xmlGenericError(xmlGenericErrorContext, "\n");
1314 cur = ctxt->cur;
1315 while ((*cur == '\n') || (*cur == '\r'))
1316 cur--;
1317 n = 0;
1318 while ((cur != base) && (n++ < 80)) {
1319 xmlGenericError(xmlGenericErrorContext, " ");
1320 base++;
1321 }
1322 xmlGenericError(xmlGenericErrorContext,"^\n");
1323}
1324
1325
1326/************************************************************************
1327 * *
1328 * Routines to handle NodeSets *
1329 * *
1330 ************************************************************************/
1331
1332/**
1333 * xmlXPathCmpNodes:
1334 * @node1: the first node
1335 * @node2: the second node
1336 *
1337 * Compare two nodes w.r.t document order
1338 *
1339 * Returns -2 in case of error 1 if first point < second point, 0 if
1340 * that's the same node, -1 otherwise
1341 */
1342int
1343xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) {
1344 int depth1, depth2;
1345 xmlNodePtr cur, root;
1346
1347 if ((node1 == NULL) || (node2 == NULL))
1348 return(-2);
1349 /*
1350 * a couple of optimizations which will avoid computations in most cases
1351 */
1352 if (node1 == node2)
1353 return(0);
Daniel Veillardb33c2012001-04-25 12:59:04 +00001354 if ((node1->type == XML_NAMESPACE_DECL) ||
1355 (node2->type == XML_NAMESPACE_DECL))
1356 return(1);
Owen Taylor3473f882001-02-23 17:55:21 +00001357 if (node1 == node2->prev)
1358 return(1);
1359 if (node1 == node2->next)
1360 return(-1);
1361
1362 /*
1363 * compute depth to root
1364 */
1365 for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) {
1366 if (cur == node1)
1367 return(1);
1368 depth2++;
1369 }
1370 root = cur;
1371 for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) {
1372 if (cur == node2)
1373 return(-1);
1374 depth1++;
1375 }
1376 /*
1377 * Distinct document (or distinct entities :-( ) case.
1378 */
1379 if (root != cur) {
1380 return(-2);
1381 }
1382 /*
1383 * get the nearest common ancestor.
1384 */
1385 while (depth1 > depth2) {
1386 depth1--;
1387 node1 = node1->parent;
1388 }
1389 while (depth2 > depth1) {
1390 depth2--;
1391 node2 = node2->parent;
1392 }
1393 while (node1->parent != node2->parent) {
1394 node1 = node1->parent;
1395 node2 = node2->parent;
1396 /* should not happen but just in case ... */
1397 if ((node1 == NULL) || (node2 == NULL))
1398 return(-2);
1399 }
1400 /*
1401 * Find who's first.
1402 */
1403 if (node1 == node2->next)
1404 return(-1);
1405 for (cur = node1->next;cur != NULL;cur = cur->next)
1406 if (cur == node2)
1407 return(1);
1408 return(-1); /* assume there is no sibling list corruption */
1409}
1410
1411/**
1412 * xmlXPathNodeSetSort:
1413 * @set: the node set
1414 *
1415 * Sort the node set in document order
1416 */
1417void
1418xmlXPathNodeSetSort(xmlNodeSetPtr set) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001419 int i, j, incr, len;
Owen Taylor3473f882001-02-23 17:55:21 +00001420 xmlNodePtr tmp;
1421
1422 if (set == NULL)
1423 return;
1424
1425 /* Use Shell's sort to sort the node-set */
1426 len = set->nodeNr;
1427 for (incr = len / 2; incr > 0; incr /= 2) {
1428 for (i = incr; i < len; i++) {
1429 j = i - incr;
1430 while (j >= 0) {
Bjorn Reesee1dc0112001-03-03 12:09:03 +00001431 if (xmlXPathCmpNodes(set->nodeTab[j],
1432 set->nodeTab[j + incr]) == -1) {
Owen Taylor3473f882001-02-23 17:55:21 +00001433 tmp = set->nodeTab[j];
1434 set->nodeTab[j] = set->nodeTab[j + incr];
1435 set->nodeTab[j + incr] = tmp;
1436 j -= incr;
1437 } else
1438 break;
1439 }
1440 }
1441 }
1442}
1443
1444#define XML_NODESET_DEFAULT 10
1445/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001446 * xmlXPathNodeSetDupNs:
1447 * @node: the parent node of the namespace XPath node
1448 * @ns: the libxml namespace declaration node.
1449 *
1450 * Namespace node in libxml don't match the XPath semantic. In a node set
1451 * the namespace nodes are duplicated and the next pointer is set to the
1452 * parent node in the XPath semantic.
1453 *
1454 * Returns the newly created object.
1455 */
1456static xmlNodePtr
1457xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) {
1458 xmlNsPtr cur;
1459
1460 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
1461 return(NULL);
1462 if ((node == NULL) || (node->type == XML_NAMESPACE_DECL))
1463 return((xmlNodePtr) ns);
1464
1465 /*
1466 * Allocate a new Namespace and fill the fields.
1467 */
1468 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
1469 if (cur == NULL) {
1470 xmlGenericError(xmlGenericErrorContext,
1471 "xmlXPathNodeSetDupNs : malloc failed\n");
1472 return(NULL);
1473 }
1474 memset(cur, 0, sizeof(xmlNs));
1475 cur->type = XML_NAMESPACE_DECL;
1476 if (ns->href != NULL)
1477 cur->href = xmlStrdup(ns->href);
1478 if (ns->prefix != NULL)
1479 cur->prefix = xmlStrdup(ns->prefix);
1480 cur->next = (xmlNsPtr) node;
1481 return((xmlNodePtr) cur);
1482}
1483
1484/**
1485 * xmlXPathNodeSetFreeNs:
1486 * @ns: the XPath namespace node found in a nodeset.
1487 *
1488 * Namespace node in libxml don't match the XPath semantic. In a node set
1489 * the namespace nodes are duplicated and the next pointer is set to the
1490 * parent node in the XPath semantic. Check if such a node need to be freed
1491 */
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00001492void
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001493xmlXPathNodeSetFreeNs(xmlNsPtr ns) {
1494 if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
1495 return;
1496
1497 if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) {
1498 if (ns->href != NULL)
1499 xmlFree((xmlChar *)ns->href);
1500 if (ns->prefix != NULL)
1501 xmlFree((xmlChar *)ns->prefix);
1502 xmlFree(ns);
1503 }
1504}
1505
1506/**
Owen Taylor3473f882001-02-23 17:55:21 +00001507 * xmlXPathNodeSetCreate:
1508 * @val: an initial xmlNodePtr, or NULL
1509 *
1510 * Create a new xmlNodeSetPtr of type double and of value @val
1511 *
1512 * Returns the newly created object.
1513 */
1514xmlNodeSetPtr
1515xmlXPathNodeSetCreate(xmlNodePtr val) {
1516 xmlNodeSetPtr ret;
1517
1518 ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet));
1519 if (ret == NULL) {
1520 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001521 "xmlXPathNodeSetCreate: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001522 return(NULL);
1523 }
1524 memset(ret, 0 , (size_t) sizeof(xmlNodeSet));
1525 if (val != NULL) {
1526 ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1527 sizeof(xmlNodePtr));
1528 if (ret->nodeTab == NULL) {
1529 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001530 "xmlXPathNodeSetCreate: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001531 return(NULL);
1532 }
1533 memset(ret->nodeTab, 0 ,
1534 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1535 ret->nodeMax = XML_NODESET_DEFAULT;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001536 if (val->type == XML_NAMESPACE_DECL) {
1537 xmlNsPtr ns = (xmlNsPtr) val;
1538
1539 ret->nodeTab[ret->nodeNr++] =
1540 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1541 } else
1542 ret->nodeTab[ret->nodeNr++] = val;
Owen Taylor3473f882001-02-23 17:55:21 +00001543 }
1544 return(ret);
1545}
1546
1547/**
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001548 * xmlXPathNodeSetContains:
1549 * @cur: the node-set
1550 * @val: the node
1551 *
1552 * checks whether @cur contains @val
1553 *
1554 * Returns true (1) if @cur contains @val, false (0) otherwise
1555 */
1556int
1557xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) {
1558 int i;
1559
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001560 if (val->type == XML_NAMESPACE_DECL) {
1561 for (i = 0; i < cur->nodeNr; i++) {
1562 if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) {
1563 xmlNsPtr ns1, ns2;
1564
1565 ns1 = (xmlNsPtr) val;
1566 ns2 = (xmlNsPtr) cur->nodeTab[i];
1567 if (ns1 == ns2)
1568 return(1);
1569 if ((ns1->next != NULL) && (ns2->next == ns1->next) &&
1570 (xmlStrEqual(ns1->prefix, ns2->prefix)))
1571 return(1);
1572 }
1573 }
1574 } else {
1575 for (i = 0; i < cur->nodeNr; i++) {
1576 if (cur->nodeTab[i] == val)
1577 return(1);
1578 }
Thomas Broyerf06a3d82001-07-16 04:52:57 +00001579 }
1580 return(0);
1581}
1582
1583/**
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001584 * xmlXPathNodeSetAddNs:
1585 * @cur: the initial node set
1586 * @node: the hosting node
1587 * @ns: a the namespace node
1588 *
1589 * add a new namespace node to an existing NodeSet
1590 */
Aleksey Sanin79376ba2002-05-14 06:41:32 +00001591void
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001592xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) {
1593 int i;
1594
1595 if ((ns == NULL) || (node == NULL) || (ns->type != XML_NAMESPACE_DECL) ||
1596 (node->type != XML_ELEMENT_NODE))
1597 return;
1598
1599 /* @@ with_ns to check wether namespace nodes should be looked at @@ */
1600 /*
1601 * check against doublons
1602 */
1603 for (i = 0;i < cur->nodeNr;i++) {
1604 if ((cur->nodeTab[i] != NULL) &&
1605 (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) &&
Daniel Veillardc62a1472002-03-19 18:35:12 +00001606 (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) &&
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001607 (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix)))
1608 return;
1609 }
1610
1611 /*
1612 * grow the nodeTab if needed
1613 */
1614 if (cur->nodeMax == 0) {
1615 cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1616 sizeof(xmlNodePtr));
1617 if (cur->nodeTab == NULL) {
1618 xmlGenericError(xmlGenericErrorContext,
1619 "xmlXPathNodeSetAdd: out of memory\n");
1620 return;
1621 }
1622 memset(cur->nodeTab, 0 ,
1623 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1624 cur->nodeMax = XML_NODESET_DEFAULT;
1625 } else if (cur->nodeNr == cur->nodeMax) {
1626 xmlNodePtr *temp;
1627
1628 cur->nodeMax *= 2;
1629 temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
1630 sizeof(xmlNodePtr));
1631 if (temp == NULL) {
1632 xmlGenericError(xmlGenericErrorContext,
1633 "xmlXPathNodeSetAdd: out of memory\n");
1634 return;
1635 }
1636 cur->nodeTab = temp;
1637 }
1638 cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns);
1639}
1640
1641/**
Owen Taylor3473f882001-02-23 17:55:21 +00001642 * xmlXPathNodeSetAdd:
1643 * @cur: the initial node set
1644 * @val: a new xmlNodePtr
1645 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001646 * add a new xmlNodePtr to an existing NodeSet
Owen Taylor3473f882001-02-23 17:55:21 +00001647 */
1648void
1649xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) {
1650 int i;
1651
1652 if (val == NULL) return;
1653
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001654 /* @@ with_ns to check wether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00001655 /*
1656 * check against doublons
1657 */
1658 for (i = 0;i < cur->nodeNr;i++)
1659 if (cur->nodeTab[i] == val) return;
1660
1661 /*
1662 * grow the nodeTab if needed
1663 */
1664 if (cur->nodeMax == 0) {
1665 cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1666 sizeof(xmlNodePtr));
1667 if (cur->nodeTab == NULL) {
1668 xmlGenericError(xmlGenericErrorContext,
1669 "xmlXPathNodeSetAdd: out of memory\n");
1670 return;
1671 }
1672 memset(cur->nodeTab, 0 ,
1673 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1674 cur->nodeMax = XML_NODESET_DEFAULT;
1675 } else if (cur->nodeNr == cur->nodeMax) {
1676 xmlNodePtr *temp;
1677
1678 cur->nodeMax *= 2;
1679 temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
1680 sizeof(xmlNodePtr));
1681 if (temp == NULL) {
1682 xmlGenericError(xmlGenericErrorContext,
1683 "xmlXPathNodeSetAdd: out of memory\n");
1684 return;
1685 }
1686 cur->nodeTab = temp;
1687 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001688 if (val->type == XML_NAMESPACE_DECL) {
1689 xmlNsPtr ns = (xmlNsPtr) val;
1690
1691 cur->nodeTab[cur->nodeNr++] =
1692 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1693 } else
1694 cur->nodeTab[cur->nodeNr++] = val;
Owen Taylor3473f882001-02-23 17:55:21 +00001695}
1696
1697/**
1698 * xmlXPathNodeSetAddUnique:
1699 * @cur: the initial node set
1700 * @val: a new xmlNodePtr
1701 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001702 * add a new xmlNodePtr to an existing NodeSet, optimized version
Owen Taylor3473f882001-02-23 17:55:21 +00001703 * when we are sure the node is not already in the set.
1704 */
1705void
1706xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) {
1707 if (val == NULL) return;
1708
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001709 /* @@ with_ns to check wether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00001710 /*
1711 * grow the nodeTab if needed
1712 */
1713 if (cur->nodeMax == 0) {
1714 cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1715 sizeof(xmlNodePtr));
1716 if (cur->nodeTab == NULL) {
1717 xmlGenericError(xmlGenericErrorContext,
1718 "xmlXPathNodeSetAddUnique: out of memory\n");
1719 return;
1720 }
1721 memset(cur->nodeTab, 0 ,
1722 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1723 cur->nodeMax = XML_NODESET_DEFAULT;
1724 } else if (cur->nodeNr == cur->nodeMax) {
1725 xmlNodePtr *temp;
1726
1727 cur->nodeMax *= 2;
1728 temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax *
1729 sizeof(xmlNodePtr));
1730 if (temp == NULL) {
1731 xmlGenericError(xmlGenericErrorContext,
1732 "xmlXPathNodeSetAddUnique: out of memory\n");
1733 return;
1734 }
1735 cur->nodeTab = temp;
1736 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001737 if (val->type == XML_NAMESPACE_DECL) {
1738 xmlNsPtr ns = (xmlNsPtr) val;
1739
1740 cur->nodeTab[cur->nodeNr++] =
1741 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1742 } else
1743 cur->nodeTab[cur->nodeNr++] = val;
Owen Taylor3473f882001-02-23 17:55:21 +00001744}
1745
1746/**
1747 * xmlXPathNodeSetMerge:
1748 * @val1: the first NodeSet or NULL
1749 * @val2: the second NodeSet
1750 *
1751 * Merges two nodesets, all nodes from @val2 are added to @val1
1752 * if @val1 is NULL, a new set is created and copied from @val2
1753 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001754 * Returns @val1 once extended or NULL in case of error.
Owen Taylor3473f882001-02-23 17:55:21 +00001755 */
1756xmlNodeSetPtr
1757xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00001758 int i, j, initNr, skip;
Owen Taylor3473f882001-02-23 17:55:21 +00001759
1760 if (val2 == NULL) return(val1);
1761 if (val1 == NULL) {
1762 val1 = xmlXPathNodeSetCreate(NULL);
1763 }
1764
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001765 /* @@ with_ns to check wether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00001766 initNr = val1->nodeNr;
1767
1768 for (i = 0;i < val2->nodeNr;i++) {
1769 /*
1770 * check against doublons
1771 */
Daniel Veillardd8df6c02001-04-05 16:54:14 +00001772 skip = 0;
1773 for (j = 0; j < initNr; j++) {
1774 if (val1->nodeTab[j] == val2->nodeTab[i]) {
1775 skip = 1;
1776 break;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001777 } else if ((val1->nodeTab[j]->type == XML_NAMESPACE_DECL) &&
1778 (val2->nodeTab[i]->type == XML_NAMESPACE_DECL)) {
1779 xmlNsPtr ns1, ns2;
1780 ns1 = (xmlNsPtr) val1->nodeTab[j];
1781 ns2 = (xmlNsPtr) val2->nodeTab[i];
1782 if ((ns1->next == ns2->next) &&
1783 (xmlStrEqual(ns1->prefix, ns2->prefix))) {
1784 skip = 1;
1785 break;
1786 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00001787 }
1788 }
1789 if (skip)
1790 continue;
Owen Taylor3473f882001-02-23 17:55:21 +00001791
1792 /*
1793 * grow the nodeTab if needed
1794 */
1795 if (val1->nodeMax == 0) {
1796 val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1797 sizeof(xmlNodePtr));
1798 if (val1->nodeTab == NULL) {
1799 xmlGenericError(xmlGenericErrorContext,
1800 "xmlXPathNodeSetMerge: out of memory\n");
1801 return(NULL);
1802 }
1803 memset(val1->nodeTab, 0 ,
1804 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1805 val1->nodeMax = XML_NODESET_DEFAULT;
1806 } else if (val1->nodeNr == val1->nodeMax) {
1807 xmlNodePtr *temp;
1808
1809 val1->nodeMax *= 2;
1810 temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax *
1811 sizeof(xmlNodePtr));
1812 if (temp == NULL) {
1813 xmlGenericError(xmlGenericErrorContext,
1814 "xmlXPathNodeSetMerge: out of memory\n");
1815 return(NULL);
1816 }
1817 val1->nodeTab = temp;
1818 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001819 if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) {
1820 xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i];
1821
1822 val1->nodeTab[val1->nodeNr++] =
1823 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1824 } else
1825 val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i];
Owen Taylor3473f882001-02-23 17:55:21 +00001826 }
1827
1828 return(val1);
1829}
1830
1831/**
Daniel Veillard75be0132002-03-13 10:03:35 +00001832 * xmlXPathNodeSetMergeUnique:
1833 * @val1: the first NodeSet or NULL
1834 * @val2: the second NodeSet
1835 *
1836 * Merges two nodesets, all nodes from @val2 are added to @val1
1837 * if @val1 is NULL, a new set is created and copied from @val2
1838 *
1839 * Returns @val1 once extended or NULL in case of error.
1840 */
1841static xmlNodeSetPtr
1842xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) {
1843 int i, initNr;
1844
1845 if (val2 == NULL) return(val1);
1846 if (val1 == NULL) {
1847 val1 = xmlXPathNodeSetCreate(NULL);
1848 }
1849
1850 /* @@ with_ns to check wether namespace nodes should be looked at @@ */
1851 initNr = val1->nodeNr;
1852
1853 for (i = 0;i < val2->nodeNr;i++) {
1854 /*
1855 * grow the nodeTab if needed
1856 */
1857 if (val1->nodeMax == 0) {
1858 val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT *
1859 sizeof(xmlNodePtr));
1860 if (val1->nodeTab == NULL) {
1861 xmlGenericError(xmlGenericErrorContext,
1862 "xmlXPathNodeSetMerge: out of memory\n");
1863 return(NULL);
1864 }
1865 memset(val1->nodeTab, 0 ,
1866 XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr));
1867 val1->nodeMax = XML_NODESET_DEFAULT;
1868 } else if (val1->nodeNr == val1->nodeMax) {
1869 xmlNodePtr *temp;
1870
1871 val1->nodeMax *= 2;
1872 temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax *
1873 sizeof(xmlNodePtr));
1874 if (temp == NULL) {
1875 xmlGenericError(xmlGenericErrorContext,
1876 "xmlXPathNodeSetMerge: out of memory\n");
1877 return(NULL);
1878 }
1879 val1->nodeTab = temp;
1880 }
1881 if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) {
1882 xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i];
1883
1884 val1->nodeTab[val1->nodeNr++] =
1885 xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns);
1886 } else
1887 val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i];
1888 }
1889
1890 return(val1);
1891}
1892
1893/**
Owen Taylor3473f882001-02-23 17:55:21 +00001894 * xmlXPathNodeSetDel:
1895 * @cur: the initial node set
1896 * @val: an xmlNodePtr
1897 *
1898 * Removes an xmlNodePtr from an existing NodeSet
1899 */
1900void
1901xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) {
1902 int i;
1903
1904 if (cur == NULL) return;
1905 if (val == NULL) return;
1906
1907 /*
1908 * check against doublons
1909 */
1910 for (i = 0;i < cur->nodeNr;i++)
1911 if (cur->nodeTab[i] == val) break;
1912
1913 if (i >= cur->nodeNr) {
1914#ifdef DEBUG
1915 xmlGenericError(xmlGenericErrorContext,
1916 "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n",
1917 val->name);
1918#endif
1919 return;
1920 }
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001921 if ((cur->nodeTab[i] != NULL) &&
1922 (cur->nodeTab[i]->type == XML_NAMESPACE_DECL))
1923 xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001924 cur->nodeNr--;
1925 for (;i < cur->nodeNr;i++)
1926 cur->nodeTab[i] = cur->nodeTab[i + 1];
1927 cur->nodeTab[cur->nodeNr] = NULL;
1928}
1929
1930/**
1931 * xmlXPathNodeSetRemove:
1932 * @cur: the initial node set
1933 * @val: the index to remove
1934 *
1935 * Removes an entry from an existing NodeSet list.
1936 */
1937void
1938xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) {
1939 if (cur == NULL) return;
1940 if (val >= cur->nodeNr) return;
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001941 if ((cur->nodeTab[val] != NULL) &&
1942 (cur->nodeTab[val]->type == XML_NAMESPACE_DECL))
1943 xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]);
Owen Taylor3473f882001-02-23 17:55:21 +00001944 cur->nodeNr--;
1945 for (;val < cur->nodeNr;val++)
1946 cur->nodeTab[val] = cur->nodeTab[val + 1];
1947 cur->nodeTab[cur->nodeNr] = NULL;
1948}
1949
1950/**
1951 * xmlXPathFreeNodeSet:
1952 * @obj: the xmlNodeSetPtr to free
1953 *
1954 * Free the NodeSet compound (not the actual nodes !).
1955 */
1956void
1957xmlXPathFreeNodeSet(xmlNodeSetPtr obj) {
1958 if (obj == NULL) return;
1959 if (obj->nodeTab != NULL) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001960 int i;
1961
1962 /* @@ with_ns to check wether namespace nodes should be looked at @@ */
1963 for (i = 0;i < obj->nodeNr;i++)
1964 if ((obj->nodeTab[i] != NULL) &&
1965 (obj->nodeTab[i]->type == XML_NAMESPACE_DECL))
1966 xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00001967 xmlFree(obj->nodeTab);
1968 }
Owen Taylor3473f882001-02-23 17:55:21 +00001969 xmlFree(obj);
1970}
1971
1972/**
1973 * xmlXPathFreeValueTree:
1974 * @obj: the xmlNodeSetPtr to free
1975 *
1976 * Free the NodeSet compound and the actual tree, this is different
1977 * from xmlXPathFreeNodeSet()
1978 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001979static void
Owen Taylor3473f882001-02-23 17:55:21 +00001980xmlXPathFreeValueTree(xmlNodeSetPtr obj) {
1981 int i;
1982
1983 if (obj == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00001984
1985 if (obj->nodeTab != NULL) {
Daniel Veillard044fc6b2002-03-04 17:09:44 +00001986 for (i = 0;i < obj->nodeNr;i++) {
1987 if (obj->nodeTab[i] != NULL) {
1988 if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) {
1989 xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]);
1990 } else {
1991 xmlFreeNodeList(obj->nodeTab[i]);
1992 }
1993 }
1994 }
Owen Taylor3473f882001-02-23 17:55:21 +00001995 xmlFree(obj->nodeTab);
1996 }
Owen Taylor3473f882001-02-23 17:55:21 +00001997 xmlFree(obj);
1998}
1999
2000#if defined(DEBUG) || defined(DEBUG_STEP)
2001/**
2002 * xmlGenericErrorContextNodeSet:
2003 * @output: a FILE * for the output
2004 * @obj: the xmlNodeSetPtr to free
2005 *
2006 * Quick display of a NodeSet
2007 */
2008void
2009xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) {
2010 int i;
2011
2012 if (output == NULL) output = xmlGenericErrorContext;
2013 if (obj == NULL) {
2014 fprintf(output, "NodeSet == NULL !\n");
2015 return;
2016 }
2017 if (obj->nodeNr == 0) {
2018 fprintf(output, "NodeSet is empty\n");
2019 return;
2020 }
2021 if (obj->nodeTab == NULL) {
2022 fprintf(output, " nodeTab == NULL !\n");
2023 return;
2024 }
2025 for (i = 0; i < obj->nodeNr; i++) {
2026 if (obj->nodeTab[i] == NULL) {
2027 fprintf(output, " NULL !\n");
2028 return;
2029 }
2030 if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) ||
2031 (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE))
2032 fprintf(output, " /");
2033 else if (obj->nodeTab[i]->name == NULL)
2034 fprintf(output, " noname!");
2035 else fprintf(output, " %s", obj->nodeTab[i]->name);
2036 }
2037 fprintf(output, "\n");
2038}
2039#endif
2040
2041/**
2042 * xmlXPathNewNodeSet:
2043 * @val: the NodePtr value
2044 *
2045 * Create a new xmlXPathObjectPtr of type NodeSet and initialize
2046 * it with the single Node @val
2047 *
2048 * Returns the newly created object.
2049 */
2050xmlXPathObjectPtr
2051xmlXPathNewNodeSet(xmlNodePtr val) {
2052 xmlXPathObjectPtr ret;
2053
2054 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2055 if (ret == NULL) {
2056 xmlGenericError(xmlGenericErrorContext,
2057 "xmlXPathNewNodeSet: out of memory\n");
2058 return(NULL);
2059 }
2060 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2061 ret->type = XPATH_NODESET;
Daniel Veillard77851712001-02-27 21:54:07 +00002062 ret->boolval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002063 ret->nodesetval = xmlXPathNodeSetCreate(val);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002064 /* @@ with_ns to check wether namespace nodes should be looked at @@ */
Owen Taylor3473f882001-02-23 17:55:21 +00002065 return(ret);
2066}
2067
2068/**
2069 * xmlXPathNewValueTree:
2070 * @val: the NodePtr value
2071 *
2072 * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize
2073 * it with the tree root @val
2074 *
2075 * Returns the newly created object.
2076 */
2077xmlXPathObjectPtr
2078xmlXPathNewValueTree(xmlNodePtr val) {
2079 xmlXPathObjectPtr ret;
2080
2081 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2082 if (ret == NULL) {
2083 xmlGenericError(xmlGenericErrorContext,
2084 "xmlXPathNewNodeSet: out of memory\n");
2085 return(NULL);
2086 }
2087 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2088 ret->type = XPATH_XSLT_TREE;
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00002089 ret->boolval = 1;
2090 ret->user = (void *) val;
Owen Taylor3473f882001-02-23 17:55:21 +00002091 ret->nodesetval = xmlXPathNodeSetCreate(val);
2092 return(ret);
2093}
2094
2095/**
2096 * xmlXPathNewNodeSetList:
2097 * @val: an existing NodeSet
2098 *
2099 * Create a new xmlXPathObjectPtr of type NodeSet and initialize
2100 * it with the Nodeset @val
2101 *
2102 * Returns the newly created object.
2103 */
2104xmlXPathObjectPtr
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002105xmlXPathNewNodeSetList(xmlNodeSetPtr val)
2106{
Owen Taylor3473f882001-02-23 17:55:21 +00002107 xmlXPathObjectPtr ret;
2108 int i;
2109
2110 if (val == NULL)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002111 ret = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002112 else if (val->nodeTab == NULL)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002113 ret = xmlXPathNewNodeSet(NULL);
2114 else {
2115 ret = xmlXPathNewNodeSet(val->nodeTab[0]);
2116 for (i = 1; i < val->nodeNr; ++i)
2117 xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]);
2118 }
Owen Taylor3473f882001-02-23 17:55:21 +00002119
Daniel Veillard044fc6b2002-03-04 17:09:44 +00002120 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002121}
2122
2123/**
2124 * xmlXPathWrapNodeSet:
2125 * @val: the NodePtr value
2126 *
2127 * Wrap the Nodeset @val in a new xmlXPathObjectPtr
2128 *
2129 * Returns the newly created object.
2130 */
2131xmlXPathObjectPtr
2132xmlXPathWrapNodeSet(xmlNodeSetPtr val) {
2133 xmlXPathObjectPtr ret;
2134
2135 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2136 if (ret == NULL) {
2137 xmlGenericError(xmlGenericErrorContext,
2138 "xmlXPathWrapNodeSet: out of memory\n");
2139 return(NULL);
2140 }
2141 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2142 ret->type = XPATH_NODESET;
2143 ret->nodesetval = val;
2144 return(ret);
2145}
2146
2147/**
2148 * xmlXPathFreeNodeSetList:
2149 * @obj: an existing NodeSetList object
2150 *
2151 * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in
2152 * the list contrary to xmlXPathFreeObject().
2153 */
2154void
2155xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) {
2156 if (obj == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +00002157 xmlFree(obj);
2158}
2159
Thomas Broyerf06a3d82001-07-16 04:52:57 +00002160/**
2161 * xmlXPathDifference:
2162 * @nodes1: a node-set
2163 * @nodes2: a node-set
2164 *
2165 * Implements the EXSLT - Sets difference() function:
2166 * node-set set:difference (node-set, node-set)
2167 *
2168 * Returns the difference between the two node sets, or nodes1 if
2169 * nodes2 is empty
2170 */
2171xmlNodeSetPtr
2172xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2173 xmlNodeSetPtr ret;
2174 int i, l1;
2175 xmlNodePtr cur;
2176
2177 if (xmlXPathNodeSetIsEmpty(nodes2))
2178 return(nodes1);
2179
2180 ret = xmlXPathNodeSetCreate(NULL);
2181 if (xmlXPathNodeSetIsEmpty(nodes1))
2182 return(ret);
2183
2184 l1 = xmlXPathNodeSetGetLength(nodes1);
2185
2186 for (i = 0; i < l1; i++) {
2187 cur = xmlXPathNodeSetItem(nodes1, i);
2188 if (!xmlXPathNodeSetContains(nodes2, cur))
2189 xmlXPathNodeSetAddUnique(ret, cur);
2190 }
2191 return(ret);
2192}
2193
2194/**
2195 * xmlXPathIntersection:
2196 * @nodes1: a node-set
2197 * @nodes2: a node-set
2198 *
2199 * Implements the EXSLT - Sets intersection() function:
2200 * node-set set:intersection (node-set, node-set)
2201 *
2202 * Returns a node set comprising the nodes that are within both the
2203 * node sets passed as arguments
2204 */
2205xmlNodeSetPtr
2206xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2207 xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL);
2208 int i, l1;
2209 xmlNodePtr cur;
2210
2211 if (xmlXPathNodeSetIsEmpty(nodes1))
2212 return(ret);
2213 if (xmlXPathNodeSetIsEmpty(nodes2))
2214 return(ret);
2215
2216 l1 = xmlXPathNodeSetGetLength(nodes1);
2217
2218 for (i = 0; i < l1; i++) {
2219 cur = xmlXPathNodeSetItem(nodes1, i);
2220 if (xmlXPathNodeSetContains(nodes2, cur))
2221 xmlXPathNodeSetAddUnique(ret, cur);
2222 }
2223 return(ret);
2224}
2225
2226/**
2227 * xmlXPathDistinctSorted:
2228 * @nodes: a node-set, sorted by document order
2229 *
2230 * Implements the EXSLT - Sets distinct() function:
2231 * node-set set:distinct (node-set)
2232 *
2233 * Returns a subset of the nodes contained in @nodes, or @nodes if
2234 * it is empty
2235 */
2236xmlNodeSetPtr
2237xmlXPathDistinctSorted (xmlNodeSetPtr nodes) {
2238 xmlNodeSetPtr ret;
2239 xmlHashTablePtr hash;
2240 int i, l;
2241 xmlChar * strval;
2242 xmlNodePtr cur;
2243
2244 if (xmlXPathNodeSetIsEmpty(nodes))
2245 return(nodes);
2246
2247 ret = xmlXPathNodeSetCreate(NULL);
2248 l = xmlXPathNodeSetGetLength(nodes);
2249 hash = xmlHashCreate (l);
2250 for (i = 0; i < l; i++) {
2251 cur = xmlXPathNodeSetItem(nodes, i);
2252 strval = xmlXPathCastNodeToString(cur);
2253 if (xmlHashLookup(hash, strval) == NULL) {
2254 xmlHashAddEntry(hash, strval, strval);
2255 xmlXPathNodeSetAddUnique(ret, cur);
2256 } else {
2257 xmlFree(strval);
2258 }
2259 }
2260 xmlHashFree(hash, (xmlHashDeallocator) xmlFree);
2261 return(ret);
2262}
2263
2264/**
2265 * xmlXPathDistinct:
2266 * @nodes: a node-set
2267 *
2268 * Implements the EXSLT - Sets distinct() function:
2269 * node-set set:distinct (node-set)
2270 * @nodes is sorted by document order, then #exslSetsDistinctSorted
2271 * is called with the sorted node-set
2272 *
2273 * Returns a subset of the nodes contained in @nodes, or @nodes if
2274 * it is empty
2275 */
2276xmlNodeSetPtr
2277xmlXPathDistinct (xmlNodeSetPtr nodes) {
2278 if (xmlXPathNodeSetIsEmpty(nodes))
2279 return(nodes);
2280
2281 xmlXPathNodeSetSort(nodes);
2282 return(xmlXPathDistinctSorted(nodes));
2283}
2284
2285/**
2286 * xmlXPathHasSameNodes:
2287 * @nodes1: a node-set
2288 * @nodes2: a node-set
2289 *
2290 * Implements the EXSLT - Sets has-same-nodes function:
2291 * boolean set:has-same-node(node-set, node-set)
2292 *
2293 * Returns true (1) if @nodes1 shares any node with @nodes2, false (0)
2294 * otherwise
2295 */
2296int
2297xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2298 int i, l;
2299 xmlNodePtr cur;
2300
2301 if (xmlXPathNodeSetIsEmpty(nodes1) ||
2302 xmlXPathNodeSetIsEmpty(nodes2))
2303 return(0);
2304
2305 l = xmlXPathNodeSetGetLength(nodes1);
2306 for (i = 0; i < l; i++) {
2307 cur = xmlXPathNodeSetItem(nodes1, i);
2308 if (xmlXPathNodeSetContains(nodes2, cur))
2309 return(1);
2310 }
2311 return(0);
2312}
2313
2314/**
2315 * xmlXPathNodeLeadingSorted:
2316 * @nodes: a node-set, sorted by document order
2317 * @node: a node
2318 *
2319 * Implements the EXSLT - Sets leading() function:
2320 * node-set set:leading (node-set, node-set)
2321 *
2322 * Returns the nodes in @nodes that precede @node in document order,
2323 * @nodes if @node is NULL or an empty node-set if @nodes
2324 * doesn't contain @node
2325 */
2326xmlNodeSetPtr
2327xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) {
2328 int i, l;
2329 xmlNodePtr cur;
2330 xmlNodeSetPtr ret;
2331
2332 if (node == NULL)
2333 return(nodes);
2334
2335 ret = xmlXPathNodeSetCreate(NULL);
2336 if (xmlXPathNodeSetIsEmpty(nodes) ||
2337 (!xmlXPathNodeSetContains(nodes, node)))
2338 return(ret);
2339
2340 l = xmlXPathNodeSetGetLength(nodes);
2341 for (i = 0; i < l; i++) {
2342 cur = xmlXPathNodeSetItem(nodes, i);
2343 if (cur == node)
2344 break;
2345 xmlXPathNodeSetAddUnique(ret, cur);
2346 }
2347 return(ret);
2348}
2349
2350/**
2351 * xmlXPathNodeLeading:
2352 * @nodes: a node-set
2353 * @node: a node
2354 *
2355 * Implements the EXSLT - Sets leading() function:
2356 * node-set set:leading (node-set, node-set)
2357 * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted
2358 * is called.
2359 *
2360 * Returns the nodes in @nodes that precede @node in document order,
2361 * @nodes if @node is NULL or an empty node-set if @nodes
2362 * doesn't contain @node
2363 */
2364xmlNodeSetPtr
2365xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) {
2366 xmlXPathNodeSetSort(nodes);
2367 return(xmlXPathNodeLeadingSorted(nodes, node));
2368}
2369
2370/**
2371 * xmlXPathLeadingSorted:
2372 * @nodes1: a node-set, sorted by document order
2373 * @nodes2: a node-set, sorted by document order
2374 *
2375 * Implements the EXSLT - Sets leading() function:
2376 * node-set set:leading (node-set, node-set)
2377 *
2378 * Returns the nodes in @nodes1 that precede the first node in @nodes2
2379 * in document order, @nodes1 if @nodes2 is NULL or empty or
2380 * an empty node-set if @nodes1 doesn't contain @nodes2
2381 */
2382xmlNodeSetPtr
2383xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2384 if (xmlXPathNodeSetIsEmpty(nodes2))
2385 return(nodes1);
2386 return(xmlXPathNodeLeadingSorted(nodes1,
2387 xmlXPathNodeSetItem(nodes2, 1)));
2388}
2389
2390/**
2391 * xmlXPathLeading:
2392 * @nodes1: a node-set
2393 * @nodes2: a node-set
2394 *
2395 * Implements the EXSLT - Sets leading() function:
2396 * node-set set:leading (node-set, node-set)
2397 * @nodes1 and @nodes2 are sorted by document order, then
2398 * #exslSetsLeadingSorted is called.
2399 *
2400 * Returns the nodes in @nodes1 that precede the first node in @nodes2
2401 * in document order, @nodes1 if @nodes2 is NULL or empty or
2402 * an empty node-set if @nodes1 doesn't contain @nodes2
2403 */
2404xmlNodeSetPtr
2405xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2406 if (xmlXPathNodeSetIsEmpty(nodes2))
2407 return(nodes1);
2408 if (xmlXPathNodeSetIsEmpty(nodes1))
2409 return(xmlXPathNodeSetCreate(NULL));
2410 xmlXPathNodeSetSort(nodes1);
2411 xmlXPathNodeSetSort(nodes2);
2412 return(xmlXPathNodeLeadingSorted(nodes1,
2413 xmlXPathNodeSetItem(nodes2, 1)));
2414}
2415
2416/**
2417 * xmlXPathNodeTrailingSorted:
2418 * @nodes: a node-set, sorted by document order
2419 * @node: a node
2420 *
2421 * Implements the EXSLT - Sets trailing() function:
2422 * node-set set:trailing (node-set, node-set)
2423 *
2424 * Returns the nodes in @nodes that follow @node in document order,
2425 * @nodes if @node is NULL or an empty node-set if @nodes
2426 * doesn't contain @node
2427 */
2428xmlNodeSetPtr
2429xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) {
2430 int i, l;
2431 xmlNodePtr cur;
2432 xmlNodeSetPtr ret;
2433
2434 if (node == NULL)
2435 return(nodes);
2436
2437 ret = xmlXPathNodeSetCreate(NULL);
2438 if (xmlXPathNodeSetIsEmpty(nodes) ||
2439 (!xmlXPathNodeSetContains(nodes, node)))
2440 return(ret);
2441
2442 l = xmlXPathNodeSetGetLength(nodes);
Thomas Broyerf186c822001-07-31 23:30:37 +00002443 for (i = l; i > 0; i--) {
Thomas Broyerf06a3d82001-07-16 04:52:57 +00002444 cur = xmlXPathNodeSetItem(nodes, i);
2445 if (cur == node)
2446 break;
2447 xmlXPathNodeSetAddUnique(ret, cur);
2448 }
2449 return(ret);
2450}
2451
2452/**
2453 * xmlXPathNodeTrailing:
2454 * @nodes: a node-set
2455 * @node: a node
2456 *
2457 * Implements the EXSLT - Sets trailing() function:
2458 * node-set set:trailing (node-set, node-set)
2459 * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted
2460 * is called.
2461 *
2462 * Returns the nodes in @nodes that follow @node in document order,
2463 * @nodes if @node is NULL or an empty node-set if @nodes
2464 * doesn't contain @node
2465 */
2466xmlNodeSetPtr
2467xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) {
2468 xmlXPathNodeSetSort(nodes);
2469 return(xmlXPathNodeTrailingSorted(nodes, node));
2470}
2471
2472/**
2473 * xmlXPathTrailingSorted:
2474 * @nodes1: a node-set, sorted by document order
2475 * @nodes2: a node-set, sorted by document order
2476 *
2477 * Implements the EXSLT - Sets trailing() function:
2478 * node-set set:trailing (node-set, node-set)
2479 *
2480 * Returns the nodes in @nodes1 that follow the first node in @nodes2
2481 * in document order, @nodes1 if @nodes2 is NULL or empty or
2482 * an empty node-set if @nodes1 doesn't contain @nodes2
2483 */
2484xmlNodeSetPtr
2485xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2486 if (xmlXPathNodeSetIsEmpty(nodes2))
2487 return(nodes1);
2488 return(xmlXPathNodeTrailingSorted(nodes1,
2489 xmlXPathNodeSetItem(nodes2, 0)));
2490}
2491
2492/**
2493 * xmlXPathTrailing:
2494 * @nodes1: a node-set
2495 * @nodes2: a node-set
2496 *
2497 * Implements the EXSLT - Sets trailing() function:
2498 * node-set set:trailing (node-set, node-set)
2499 * @nodes1 and @nodes2 are sorted by document order, then
2500 * #xmlXPathTrailingSorted is called.
2501 *
2502 * Returns the nodes in @nodes1 that follow the first node in @nodes2
2503 * in document order, @nodes1 if @nodes2 is NULL or empty or
2504 * an empty node-set if @nodes1 doesn't contain @nodes2
2505 */
2506xmlNodeSetPtr
2507xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) {
2508 if (xmlXPathNodeSetIsEmpty(nodes2))
2509 return(nodes1);
2510 if (xmlXPathNodeSetIsEmpty(nodes1))
2511 return(xmlXPathNodeSetCreate(NULL));
2512 xmlXPathNodeSetSort(nodes1);
2513 xmlXPathNodeSetSort(nodes2);
2514 return(xmlXPathNodeTrailingSorted(nodes1,
2515 xmlXPathNodeSetItem(nodes2, 0)));
2516}
2517
Owen Taylor3473f882001-02-23 17:55:21 +00002518/************************************************************************
2519 * *
2520 * Routines to handle extra functions *
2521 * *
2522 ************************************************************************/
2523
2524/**
2525 * xmlXPathRegisterFunc:
2526 * @ctxt: the XPath context
2527 * @name: the function name
2528 * @f: the function implementation or NULL
2529 *
2530 * Register a new function. If @f is NULL it unregisters the function
2531 *
2532 * Returns 0 in case of success, -1 in case of error
2533 */
2534int
2535xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name,
2536 xmlXPathFunction f) {
2537 return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f));
2538}
2539
2540/**
2541 * xmlXPathRegisterFuncNS:
2542 * @ctxt: the XPath context
2543 * @name: the function name
2544 * @ns_uri: the function namespace URI
2545 * @f: the function implementation or NULL
2546 *
2547 * Register a new function. If @f is NULL it unregisters the function
2548 *
2549 * Returns 0 in case of success, -1 in case of error
2550 */
2551int
2552xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2553 const xmlChar *ns_uri, xmlXPathFunction f) {
2554 if (ctxt == NULL)
2555 return(-1);
2556 if (name == NULL)
2557 return(-1);
2558
2559 if (ctxt->funcHash == NULL)
2560 ctxt->funcHash = xmlHashCreate(0);
2561 if (ctxt->funcHash == NULL)
2562 return(-1);
2563 return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, (void *) f));
2564}
2565
2566/**
Thomas Broyerba4ad322001-07-26 16:55:21 +00002567 * xmlXPathRegisterFuncLookup:
2568 * @ctxt: the XPath context
2569 * @f: the lookup function
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002570 * @funcCtxt: the lookup data
Thomas Broyerba4ad322001-07-26 16:55:21 +00002571 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002572 * Registers an external mechanism to do function lookup.
Thomas Broyerba4ad322001-07-26 16:55:21 +00002573 */
2574void
2575xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt,
2576 xmlXPathFuncLookupFunc f,
2577 void *funcCtxt) {
2578 if (ctxt == NULL)
2579 return;
2580 ctxt->funcLookupFunc = (void *) f;
2581 ctxt->funcLookupData = funcCtxt;
2582}
2583
2584/**
Owen Taylor3473f882001-02-23 17:55:21 +00002585 * xmlXPathFunctionLookup:
2586 * @ctxt: the XPath context
2587 * @name: the function name
2588 *
2589 * Search in the Function array of the context for the given
2590 * function.
2591 *
2592 * Returns the xmlXPathFunction or NULL if not found
2593 */
2594xmlXPathFunction
2595xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) {
Thomas Broyerba4ad322001-07-26 16:55:21 +00002596 if (ctxt == NULL)
2597 return (NULL);
2598
2599 if (ctxt->funcLookupFunc != NULL) {
2600 xmlXPathFunction ret;
Daniel Veillard99e55eb2002-01-21 08:56:29 +00002601 xmlXPathFuncLookupFunc f;
Thomas Broyerba4ad322001-07-26 16:55:21 +00002602
Daniel Veillard99e55eb2002-01-21 08:56:29 +00002603 f = (xmlXPathFuncLookupFunc) ctxt->funcLookupFunc;
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002604 ret = f(ctxt->funcLookupData, name, NULL);
Thomas Broyerba4ad322001-07-26 16:55:21 +00002605 if (ret != NULL)
2606 return(ret);
2607 }
Owen Taylor3473f882001-02-23 17:55:21 +00002608 return(xmlXPathFunctionLookupNS(ctxt, name, NULL));
2609}
2610
2611/**
2612 * xmlXPathFunctionLookupNS:
2613 * @ctxt: the XPath context
2614 * @name: the function name
2615 * @ns_uri: the function namespace URI
2616 *
2617 * Search in the Function array of the context for the given
2618 * function.
2619 *
2620 * Returns the xmlXPathFunction or NULL if not found
2621 */
2622xmlXPathFunction
2623xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2624 const xmlChar *ns_uri) {
2625 if (ctxt == NULL)
2626 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002627 if (name == NULL)
2628 return(NULL);
2629
Thomas Broyerba4ad322001-07-26 16:55:21 +00002630 if (ctxt->funcLookupFunc != NULL) {
2631 xmlXPathFunction ret;
Daniel Veillard99e55eb2002-01-21 08:56:29 +00002632 xmlXPathFuncLookupFunc f;
Thomas Broyerba4ad322001-07-26 16:55:21 +00002633
Daniel Veillard99e55eb2002-01-21 08:56:29 +00002634 f = (xmlXPathFuncLookupFunc) ctxt->funcLookupFunc;
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002635 ret = f(ctxt->funcLookupData, name, ns_uri);
Thomas Broyerba4ad322001-07-26 16:55:21 +00002636 if (ret != NULL)
2637 return(ret);
2638 }
2639
2640 if (ctxt->funcHash == NULL)
2641 return(NULL);
2642
Owen Taylor3473f882001-02-23 17:55:21 +00002643 return((xmlXPathFunction) xmlHashLookup2(ctxt->funcHash, name, ns_uri));
2644}
2645
2646/**
2647 * xmlXPathRegisteredFuncsCleanup:
2648 * @ctxt: the XPath context
2649 *
2650 * Cleanup the XPath context data associated to registered functions
2651 */
2652void
2653xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) {
2654 if (ctxt == NULL)
2655 return;
2656
2657 xmlHashFree(ctxt->funcHash, NULL);
2658 ctxt->funcHash = NULL;
2659}
2660
2661/************************************************************************
2662 * *
2663 * Routines to handle Variable *
2664 * *
2665 ************************************************************************/
2666
2667/**
2668 * xmlXPathRegisterVariable:
2669 * @ctxt: the XPath context
2670 * @name: the variable name
2671 * @value: the variable value or NULL
2672 *
2673 * Register a new variable value. If @value is NULL it unregisters
2674 * the variable
2675 *
2676 * Returns 0 in case of success, -1 in case of error
2677 */
2678int
2679xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name,
2680 xmlXPathObjectPtr value) {
2681 return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value));
2682}
2683
2684/**
2685 * xmlXPathRegisterVariableNS:
2686 * @ctxt: the XPath context
2687 * @name: the variable name
2688 * @ns_uri: the variable namespace URI
2689 * @value: the variable value or NULL
2690 *
2691 * Register a new variable value. If @value is NULL it unregisters
2692 * the variable
2693 *
2694 * Returns 0 in case of success, -1 in case of error
2695 */
2696int
2697xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2698 const xmlChar *ns_uri,
2699 xmlXPathObjectPtr value) {
2700 if (ctxt == NULL)
2701 return(-1);
2702 if (name == NULL)
2703 return(-1);
2704
2705 if (ctxt->varHash == NULL)
2706 ctxt->varHash = xmlHashCreate(0);
2707 if (ctxt->varHash == NULL)
2708 return(-1);
2709 return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri,
2710 (void *) value,
2711 (xmlHashDeallocator)xmlXPathFreeObject));
2712}
2713
2714/**
2715 * xmlXPathRegisterVariableLookup:
2716 * @ctxt: the XPath context
2717 * @f: the lookup function
2718 * @data: the lookup data
2719 *
2720 * register an external mechanism to do variable lookup
2721 */
2722void
2723xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt,
2724 xmlXPathVariableLookupFunc f, void *data) {
2725 if (ctxt == NULL)
2726 return;
2727 ctxt->varLookupFunc = (void *) f;
2728 ctxt->varLookupData = data;
2729}
2730
2731/**
2732 * xmlXPathVariableLookup:
2733 * @ctxt: the XPath context
2734 * @name: the variable name
2735 *
2736 * Search in the Variable array of the context for the given
2737 * variable value.
2738 *
Daniel Veillard73c9c042001-07-05 20:02:54 +00002739 * Returns a copy of the value or NULL if not found
Owen Taylor3473f882001-02-23 17:55:21 +00002740 */
2741xmlXPathObjectPtr
2742xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) {
2743 if (ctxt == NULL)
2744 return(NULL);
2745
2746 if (ctxt->varLookupFunc != NULL) {
2747 xmlXPathObjectPtr ret;
2748
2749 ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc)
2750 (ctxt->varLookupData, name, NULL);
Daniel Veillard556c6682001-10-06 09:59:51 +00002751 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002752 }
2753 return(xmlXPathVariableLookupNS(ctxt, name, NULL));
2754}
2755
2756/**
2757 * xmlXPathVariableLookupNS:
2758 * @ctxt: the XPath context
2759 * @name: the variable name
2760 * @ns_uri: the variable namespace URI
2761 *
2762 * Search in the Variable array of the context for the given
Daniel Veillard73c9c042001-07-05 20:02:54 +00002763 * variable value.
Owen Taylor3473f882001-02-23 17:55:21 +00002764 *
Daniel Veillard73c9c042001-07-05 20:02:54 +00002765 * Returns the a copy of the value or NULL if not found
Owen Taylor3473f882001-02-23 17:55:21 +00002766 */
2767xmlXPathObjectPtr
2768xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name,
2769 const xmlChar *ns_uri) {
2770 if (ctxt == NULL)
2771 return(NULL);
2772
2773 if (ctxt->varLookupFunc != NULL) {
2774 xmlXPathObjectPtr ret;
2775
2776 ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc)
2777 (ctxt->varLookupData, name, ns_uri);
2778 if (ret != NULL) return(ret);
2779 }
2780
2781 if (ctxt->varHash == NULL)
2782 return(NULL);
2783 if (name == NULL)
2784 return(NULL);
2785
Daniel Veillard8c357d52001-07-03 23:43:33 +00002786 return(xmlXPathObjectCopy((xmlXPathObjectPtr)
2787 xmlHashLookup2(ctxt->varHash, name, ns_uri)));
Owen Taylor3473f882001-02-23 17:55:21 +00002788}
2789
2790/**
2791 * xmlXPathRegisteredVariablesCleanup:
2792 * @ctxt: the XPath context
2793 *
2794 * Cleanup the XPath context data associated to registered variables
2795 */
2796void
2797xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) {
2798 if (ctxt == NULL)
2799 return;
2800
Daniel Veillard76d66f42001-05-16 21:05:17 +00002801 xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject);
Owen Taylor3473f882001-02-23 17:55:21 +00002802 ctxt->varHash = NULL;
2803}
2804
2805/**
2806 * xmlXPathRegisterNs:
2807 * @ctxt: the XPath context
2808 * @prefix: the namespace prefix
2809 * @ns_uri: the namespace name
2810 *
2811 * Register a new namespace. If @ns_uri is NULL it unregisters
2812 * the namespace
2813 *
2814 * Returns 0 in case of success, -1 in case of error
2815 */
2816int
2817xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix,
2818 const xmlChar *ns_uri) {
2819 if (ctxt == NULL)
2820 return(-1);
2821 if (prefix == NULL)
2822 return(-1);
2823
2824 if (ctxt->nsHash == NULL)
2825 ctxt->nsHash = xmlHashCreate(10);
2826 if (ctxt->nsHash == NULL)
2827 return(-1);
2828 return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) ns_uri,
2829 (xmlHashDeallocator)xmlFree));
2830}
2831
2832/**
2833 * xmlXPathNsLookup:
2834 * @ctxt: the XPath context
2835 * @prefix: the namespace prefix value
2836 *
2837 * Search in the namespace declaration array of the context for the given
2838 * namespace name associated to the given prefix
2839 *
2840 * Returns the value or NULL if not found
2841 */
2842const xmlChar *
2843xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) {
2844 if (ctxt == NULL)
2845 return(NULL);
2846 if (prefix == NULL)
2847 return(NULL);
2848
2849#ifdef XML_XML_NAMESPACE
2850 if (xmlStrEqual(prefix, (const xmlChar *) "xml"))
2851 return(XML_XML_NAMESPACE);
2852#endif
2853
Daniel Veillardc8f620b2001-04-30 20:31:33 +00002854 if (ctxt->namespaces != NULL) {
2855 int i;
2856
2857 for (i = 0;i < ctxt->nsNr;i++) {
2858 if ((ctxt->namespaces[i] != NULL) &&
2859 (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix)))
2860 return(ctxt->namespaces[i]->href);
2861 }
2862 }
Owen Taylor3473f882001-02-23 17:55:21 +00002863
2864 return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix));
2865}
2866
2867/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002868 * xmlXPathRegisteredNsCleanup:
Owen Taylor3473f882001-02-23 17:55:21 +00002869 * @ctxt: the XPath context
2870 *
2871 * Cleanup the XPath context data associated to registered variables
2872 */
2873void
2874xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) {
2875 if (ctxt == NULL)
2876 return;
2877
2878 xmlHashFree(ctxt->nsHash, NULL);
2879 ctxt->nsHash = NULL;
2880}
2881
2882/************************************************************************
2883 * *
2884 * Routines to handle Values *
2885 * *
2886 ************************************************************************/
2887
2888/* Allocations are terrible, one need to optimize all this !!! */
2889
2890/**
2891 * xmlXPathNewFloat:
2892 * @val: the double value
2893 *
2894 * Create a new xmlXPathObjectPtr of type double and of value @val
2895 *
2896 * Returns the newly created object.
2897 */
2898xmlXPathObjectPtr
2899xmlXPathNewFloat(double val) {
2900 xmlXPathObjectPtr ret;
2901
2902 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2903 if (ret == NULL) {
2904 xmlGenericError(xmlGenericErrorContext,
2905 "xmlXPathNewFloat: out of memory\n");
2906 return(NULL);
2907 }
2908 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2909 ret->type = XPATH_NUMBER;
2910 ret->floatval = val;
2911 return(ret);
2912}
2913
2914/**
2915 * xmlXPathNewBoolean:
2916 * @val: the boolean value
2917 *
2918 * Create a new xmlXPathObjectPtr of type boolean and of value @val
2919 *
2920 * Returns the newly created object.
2921 */
2922xmlXPathObjectPtr
2923xmlXPathNewBoolean(int val) {
2924 xmlXPathObjectPtr ret;
2925
2926 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2927 if (ret == NULL) {
2928 xmlGenericError(xmlGenericErrorContext,
2929 "xmlXPathNewBoolean: out of memory\n");
2930 return(NULL);
2931 }
2932 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2933 ret->type = XPATH_BOOLEAN;
2934 ret->boolval = (val != 0);
2935 return(ret);
2936}
2937
2938/**
2939 * xmlXPathNewString:
2940 * @val: the xmlChar * value
2941 *
2942 * Create a new xmlXPathObjectPtr of type string and of value @val
2943 *
2944 * Returns the newly created object.
2945 */
2946xmlXPathObjectPtr
2947xmlXPathNewString(const xmlChar *val) {
2948 xmlXPathObjectPtr ret;
2949
2950 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2951 if (ret == NULL) {
2952 xmlGenericError(xmlGenericErrorContext,
2953 "xmlXPathNewString: out of memory\n");
2954 return(NULL);
2955 }
2956 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2957 ret->type = XPATH_STRING;
2958 if (val != NULL)
2959 ret->stringval = xmlStrdup(val);
2960 else
2961 ret->stringval = xmlStrdup((const xmlChar *)"");
2962 return(ret);
2963}
2964
2965/**
Daniel Veillardba0b8c92001-05-15 09:43:47 +00002966 * xmlXPathWrapString:
2967 * @val: the xmlChar * value
2968 *
2969 * Wraps the @val string into an XPath object.
2970 *
2971 * Returns the newly created object.
2972 */
2973xmlXPathObjectPtr
2974xmlXPathWrapString (xmlChar *val) {
2975 xmlXPathObjectPtr ret;
2976
2977 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
2978 if (ret == NULL) {
2979 xmlGenericError(xmlGenericErrorContext,
2980 "xmlXPathWrapString: out of memory\n");
2981 return(NULL);
2982 }
2983 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
2984 ret->type = XPATH_STRING;
2985 ret->stringval = val;
2986 return(ret);
2987}
2988
2989/**
Owen Taylor3473f882001-02-23 17:55:21 +00002990 * xmlXPathNewCString:
2991 * @val: the char * value
2992 *
2993 * Create a new xmlXPathObjectPtr of type string and of value @val
2994 *
2995 * Returns the newly created object.
2996 */
2997xmlXPathObjectPtr
2998xmlXPathNewCString(const char *val) {
2999 xmlXPathObjectPtr ret;
3000
3001 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3002 if (ret == NULL) {
3003 xmlGenericError(xmlGenericErrorContext,
3004 "xmlXPathNewCString: out of memory\n");
3005 return(NULL);
3006 }
3007 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3008 ret->type = XPATH_STRING;
3009 ret->stringval = xmlStrdup(BAD_CAST val);
3010 return(ret);
3011}
3012
3013/**
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003014 * xmlXPathWrapCString:
3015 * @val: the char * value
3016 *
3017 * Wraps a string into an XPath object.
3018 *
3019 * Returns the newly created object.
3020 */
3021xmlXPathObjectPtr
3022xmlXPathWrapCString (char * val) {
3023 return(xmlXPathWrapString((xmlChar *)(val)));
3024}
3025
3026/**
Thomas Broyerf06a3d82001-07-16 04:52:57 +00003027 * xmlXPathWrapExternal:
3028 * @val: the user data
3029 *
3030 * Wraps the @val data into an XPath object.
3031 *
3032 * Returns the newly created object.
3033 */
3034xmlXPathObjectPtr
3035xmlXPathWrapExternal (void *val) {
3036 xmlXPathObjectPtr ret;
3037
3038 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3039 if (ret == NULL) {
3040 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003041 "xmlXPathWrapExternal: out of memory\n");
Thomas Broyerf06a3d82001-07-16 04:52:57 +00003042 return(NULL);
3043 }
3044 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
3045 ret->type = XPATH_USERS;
3046 ret->user = val;
3047 return(ret);
3048}
3049
3050/**
Owen Taylor3473f882001-02-23 17:55:21 +00003051 * xmlXPathObjectCopy:
3052 * @val: the original object
3053 *
3054 * allocate a new copy of a given object
3055 *
3056 * Returns the newly created object.
3057 */
3058xmlXPathObjectPtr
3059xmlXPathObjectCopy(xmlXPathObjectPtr val) {
3060 xmlXPathObjectPtr ret;
3061
3062 if (val == NULL)
3063 return(NULL);
3064
3065 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
3066 if (ret == NULL) {
3067 xmlGenericError(xmlGenericErrorContext,
3068 "xmlXPathObjectCopy: out of memory\n");
3069 return(NULL);
3070 }
3071 memcpy(ret, val , (size_t) sizeof(xmlXPathObject));
3072 switch (val->type) {
3073 case XPATH_BOOLEAN:
3074 case XPATH_NUMBER:
3075 case XPATH_POINT:
3076 case XPATH_RANGE:
3077 break;
3078 case XPATH_STRING:
3079 ret->stringval = xmlStrdup(val->stringval);
3080 break;
3081 case XPATH_XSLT_TREE:
3082 if ((val->nodesetval != NULL) &&
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003083 (val->nodesetval->nodeTab != NULL)) {
3084 ret->boolval = 1;
Daniel Veillard6ab38382001-10-06 13:08:27 +00003085 ret->user = xmlDocCopyNode(val->nodesetval->nodeTab[0],
3086 val->nodesetval->nodeTab[0]->doc, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00003087 ret->nodesetval = xmlXPathNodeSetCreate(
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003088 (xmlNodePtr) ret->user);
3089 } else
Owen Taylor3473f882001-02-23 17:55:21 +00003090 ret->nodesetval = xmlXPathNodeSetCreate(NULL);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003091 /* Deallocate the copied tree value */
Owen Taylor3473f882001-02-23 17:55:21 +00003092 break;
3093 case XPATH_NODESET:
3094 ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003095 /* Do not deallocate the copied tree value */
3096 ret->boolval = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003097 break;
3098 case XPATH_LOCATIONSET:
3099#ifdef LIBXML_XPTR_ENABLED
3100 {
3101 xmlLocationSetPtr loc = val->user;
3102 ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc);
3103 break;
3104 }
3105#endif
Thomas Broyer47334c02001-10-07 16:41:52 +00003106 case XPATH_USERS:
3107 ret->user = val->user;
3108 break;
3109 case XPATH_UNDEFINED:
Owen Taylor3473f882001-02-23 17:55:21 +00003110 xmlGenericError(xmlGenericErrorContext,
3111 "xmlXPathObjectCopy: unsupported type %d\n",
3112 val->type);
3113 break;
3114 }
3115 return(ret);
3116}
3117
3118/**
3119 * xmlXPathFreeObject:
3120 * @obj: the object to free
3121 *
3122 * Free up an xmlXPathObjectPtr object.
3123 */
3124void
3125xmlXPathFreeObject(xmlXPathObjectPtr obj) {
3126 if (obj == NULL) return;
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003127 if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) {
Daniel Veillard77851712001-02-27 21:54:07 +00003128 if (obj->boolval) {
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003129 if (obj->user != NULL) {
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003130 xmlXPathFreeNodeSet(obj->nodesetval);
Daniel Veillard38bf6f02002-03-16 22:03:31 +00003131 xmlFreeNodeList((xmlNodePtr) obj->user);
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00003132 } else if (obj->nodesetval != NULL)
Daniel Veillard77851712001-02-27 21:54:07 +00003133 xmlXPathFreeValueTree(obj->nodesetval);
3134 } else {
3135 if (obj->nodesetval != NULL)
3136 xmlXPathFreeNodeSet(obj->nodesetval);
3137 }
Owen Taylor3473f882001-02-23 17:55:21 +00003138#ifdef LIBXML_XPTR_ENABLED
3139 } else if (obj->type == XPATH_LOCATIONSET) {
3140 if (obj->user != NULL)
3141 xmlXPtrFreeLocationSet(obj->user);
3142#endif
3143 } else if (obj->type == XPATH_STRING) {
3144 if (obj->stringval != NULL)
3145 xmlFree(obj->stringval);
Owen Taylor3473f882001-02-23 17:55:21 +00003146 }
3147
Owen Taylor3473f882001-02-23 17:55:21 +00003148 xmlFree(obj);
3149}
3150
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003151
3152/************************************************************************
3153 * *
3154 * Type Casting Routines *
3155 * *
3156 ************************************************************************/
3157
3158/**
3159 * xmlXPathCastBooleanToString:
3160 * @val: a boolean
3161 *
3162 * Converts a boolean to its string value.
3163 *
3164 * Returns a newly allocated string.
3165 */
3166xmlChar *
3167xmlXPathCastBooleanToString (int val) {
3168 xmlChar *ret;
3169 if (val)
3170 ret = xmlStrdup((const xmlChar *) "true");
3171 else
3172 ret = xmlStrdup((const xmlChar *) "false");
3173 return(ret);
3174}
3175
3176/**
3177 * xmlXPathCastNumberToString:
3178 * @val: a number
3179 *
3180 * Converts a number to its string value.
3181 *
3182 * Returns a newly allocated string.
3183 */
3184xmlChar *
3185xmlXPathCastNumberToString (double val) {
3186 xmlChar *ret;
Daniel Veillardcda96922001-08-21 10:56:31 +00003187 switch (xmlXPathIsInf(val)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003188 case 1:
Daniel Veillard5fc1f082002-03-27 09:05:40 +00003189 ret = xmlStrdup((const xmlChar *) "Infinity");
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003190 break;
3191 case -1:
3192 ret = xmlStrdup((const xmlChar *) "-Infinity");
3193 break;
3194 default:
Daniel Veillardcda96922001-08-21 10:56:31 +00003195 if (xmlXPathIsNaN(val)) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003196 ret = xmlStrdup((const xmlChar *) "NaN");
Daniel Veillardd30be4a2002-03-28 18:25:31 +00003197 } else if (val == 0 && xmlXPathGetSign(val) != 0) {
3198 ret = xmlStrdup((const xmlChar *) "0");
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003199 } else {
3200 /* could be improved */
3201 char buf[100];
3202 xmlXPathFormatNumber(val, buf, 100);
3203 ret = xmlStrdup((const xmlChar *) buf);
3204 }
3205 }
3206 return(ret);
3207}
3208
3209/**
3210 * xmlXPathCastNodeToString:
3211 * @node: a node
3212 *
3213 * Converts a node to its string value.
3214 *
3215 * Returns a newly allocated string.
3216 */
3217xmlChar *
3218xmlXPathCastNodeToString (xmlNodePtr node) {
Daniel Veillard23b1f372002-04-18 15:50:05 +00003219 if ((node != NULL) && (node->type == XML_DOCUMENT_NODE))
3220 node = xmlDocGetRootElement((xmlDocPtr) node);
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003221 return(xmlNodeGetContent(node));
3222}
3223
3224/**
3225 * xmlXPathCastNodeSetToString:
3226 * @ns: a node-set
3227 *
3228 * Converts a node-set to its string value.
3229 *
3230 * Returns a newly allocated string.
3231 */
3232xmlChar *
3233xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) {
3234 if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL))
3235 return(xmlStrdup((const xmlChar *) ""));
3236
3237 xmlXPathNodeSetSort(ns);
3238 return(xmlXPathCastNodeToString(ns->nodeTab[0]));
3239}
3240
3241/**
3242 * xmlXPathCastToString:
3243 * @val: an XPath object
3244 *
3245 * Converts an existing object to its string() equivalent
3246 *
3247 * Returns the string value of the object, NULL in case of error.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003248 * A new string is allocated only if needed (@val isn't a
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003249 * string object).
3250 */
3251xmlChar *
3252xmlXPathCastToString(xmlXPathObjectPtr val) {
3253 xmlChar *ret = NULL;
3254
3255 if (val == NULL)
3256 return(xmlStrdup((const xmlChar *) ""));
3257 switch (val->type) {
3258 case XPATH_UNDEFINED:
3259#ifdef DEBUG_EXPR
3260 xmlGenericError(xmlGenericErrorContext, "String: undefined\n");
3261#endif
3262 ret = xmlStrdup((const xmlChar *) "");
3263 break;
3264 case XPATH_XSLT_TREE:
3265 case XPATH_NODESET:
3266 ret = xmlXPathCastNodeSetToString(val->nodesetval);
3267 break;
3268 case XPATH_STRING:
Daniel Veillard4e2df542002-03-22 12:23:14 +00003269 return(xmlStrdup(val->stringval));
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003270 case XPATH_BOOLEAN:
3271 ret = xmlXPathCastBooleanToString(val->boolval);
3272 break;
3273 case XPATH_NUMBER: {
3274 ret = xmlXPathCastNumberToString(val->floatval);
3275 break;
3276 }
3277 case XPATH_USERS:
3278 case XPATH_POINT:
3279 case XPATH_RANGE:
3280 case XPATH_LOCATIONSET:
3281 TODO
3282 ret = xmlStrdup((const xmlChar *) "");
3283 break;
3284 }
3285 return(ret);
3286}
3287
3288/**
3289 * xmlXPathConvertString:
3290 * @val: an XPath object
3291 *
3292 * Converts an existing object to its string() equivalent
3293 *
3294 * Returns the new object, the old one is freed (or the operation
3295 * is done directly on @val)
3296 */
3297xmlXPathObjectPtr
3298xmlXPathConvertString(xmlXPathObjectPtr val) {
3299 xmlChar *res = NULL;
3300
3301 if (val == NULL)
3302 return(xmlXPathNewCString(""));
3303
3304 switch (val->type) {
3305 case XPATH_UNDEFINED:
3306#ifdef DEBUG_EXPR
3307 xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n");
3308#endif
3309 break;
3310 case XPATH_XSLT_TREE:
3311 case XPATH_NODESET:
3312 res = xmlXPathCastNodeSetToString(val->nodesetval);
3313 break;
3314 case XPATH_STRING:
3315 return(val);
3316 case XPATH_BOOLEAN:
3317 res = xmlXPathCastBooleanToString(val->boolval);
3318 break;
3319 case XPATH_NUMBER:
3320 res = xmlXPathCastNumberToString(val->floatval);
3321 break;
3322 case XPATH_USERS:
3323 case XPATH_POINT:
3324 case XPATH_RANGE:
3325 case XPATH_LOCATIONSET:
3326 TODO;
3327 break;
3328 }
3329 xmlXPathFreeObject(val);
3330 if (res == NULL)
3331 return(xmlXPathNewCString(""));
3332 return(xmlXPathWrapString(res));
3333}
3334
3335/**
3336 * xmlXPathCastBooleanToNumber:
3337 * @val: a boolean
3338 *
3339 * Converts a boolean to its number value
3340 *
3341 * Returns the number value
3342 */
3343double
3344xmlXPathCastBooleanToNumber(int val) {
3345 if (val)
3346 return(1.0);
3347 return(0.0);
3348}
3349
3350/**
3351 * xmlXPathCastStringToNumber:
3352 * @val: a string
3353 *
3354 * Converts a string to its number value
3355 *
3356 * Returns the number value
3357 */
3358double
3359xmlXPathCastStringToNumber(const xmlChar * val) {
3360 return(xmlXPathStringEvalNumber(val));
3361}
3362
3363/**
3364 * xmlXPathCastNodeToNumber:
3365 * @node: a node
3366 *
3367 * Converts a node to its number value
3368 *
3369 * Returns the number value
3370 */
3371double
3372xmlXPathCastNodeToNumber (xmlNodePtr node) {
3373 xmlChar *strval;
3374 double ret;
3375
3376 if (node == NULL)
3377 return(xmlXPathNAN);
3378 strval = xmlXPathCastNodeToString(node);
3379 if (strval == NULL)
3380 return(xmlXPathNAN);
3381 ret = xmlXPathCastStringToNumber(strval);
3382 xmlFree(strval);
3383
3384 return(ret);
3385}
3386
3387/**
3388 * xmlXPathCastNodeSetToNumber:
3389 * @ns: a node-set
3390 *
3391 * Converts a node-set to its number value
3392 *
3393 * Returns the number value
3394 */
3395double
3396xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) {
3397 xmlChar *str;
3398 double ret;
3399
3400 if (ns == NULL)
3401 return(xmlXPathNAN);
3402 str = xmlXPathCastNodeSetToString(ns);
3403 ret = xmlXPathCastStringToNumber(str);
3404 xmlFree(str);
3405 return(ret);
3406}
3407
3408/**
3409 * xmlXPathCastToNumber:
3410 * @val: an XPath object
3411 *
3412 * Converts an XPath object to its number value
3413 *
3414 * Returns the number value
3415 */
3416double
3417xmlXPathCastToNumber(xmlXPathObjectPtr val) {
3418 double ret = 0.0;
3419
3420 if (val == NULL)
3421 return(xmlXPathNAN);
3422 switch (val->type) {
3423 case XPATH_UNDEFINED:
3424#ifdef DEGUB_EXPR
3425 xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n");
3426#endif
3427 ret = xmlXPathNAN;
3428 break;
3429 case XPATH_XSLT_TREE:
3430 case XPATH_NODESET:
3431 ret = xmlXPathCastNodeSetToNumber(val->nodesetval);
3432 break;
3433 case XPATH_STRING:
3434 ret = xmlXPathCastStringToNumber(val->stringval);
3435 break;
3436 case XPATH_NUMBER:
3437 ret = val->floatval;
3438 break;
3439 case XPATH_BOOLEAN:
3440 ret = xmlXPathCastBooleanToNumber(val->boolval);
3441 break;
3442 case XPATH_USERS:
3443 case XPATH_POINT:
3444 case XPATH_RANGE:
3445 case XPATH_LOCATIONSET:
3446 TODO;
3447 ret = xmlXPathNAN;
3448 break;
3449 }
3450 return(ret);
3451}
3452
3453/**
3454 * xmlXPathConvertNumber:
3455 * @val: an XPath object
3456 *
3457 * Converts an existing object to its number() equivalent
3458 *
3459 * Returns the new object, the old one is freed (or the operation
3460 * is done directly on @val)
3461 */
3462xmlXPathObjectPtr
3463xmlXPathConvertNumber(xmlXPathObjectPtr val) {
3464 xmlXPathObjectPtr ret;
3465
3466 if (val == NULL)
3467 return(xmlXPathNewFloat(0.0));
3468 if (val->type == XPATH_NUMBER)
3469 return(val);
3470 ret = xmlXPathNewFloat(xmlXPathCastToNumber(val));
3471 xmlXPathFreeObject(val);
3472 return(ret);
3473}
3474
3475/**
3476 * xmlXPathCastNumberToBoolean:
3477 * @val: a number
3478 *
3479 * Converts a number to its boolean value
3480 *
3481 * Returns the boolean value
3482 */
3483int
3484xmlXPathCastNumberToBoolean (double val) {
Daniel Veillardcda96922001-08-21 10:56:31 +00003485 if (xmlXPathIsNaN(val) || (val == 0.0))
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003486 return(0);
3487 return(1);
3488}
3489
3490/**
3491 * xmlXPathCastStringToBoolean:
3492 * @val: a string
3493 *
3494 * Converts a string to its boolean value
3495 *
3496 * Returns the boolean value
3497 */
3498int
3499xmlXPathCastStringToBoolean (const xmlChar *val) {
3500 if ((val == NULL) || (xmlStrlen(val) == 0))
3501 return(0);
3502 return(1);
3503}
3504
3505/**
3506 * xmlXPathCastNodeSetToBoolean:
3507 * @ns: a node-set
3508 *
3509 * Converts a node-set to its boolean value
3510 *
3511 * Returns the boolean value
3512 */
3513int
3514xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) {
3515 if ((ns == NULL) || (ns->nodeNr == 0))
3516 return(0);
3517 return(1);
3518}
3519
3520/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003521 * xmlXPathCastToBoolean:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003522 * @val: an XPath object
3523 *
3524 * Converts an XPath object to its boolean value
3525 *
3526 * Returns the boolean value
3527 */
3528int
3529xmlXPathCastToBoolean (xmlXPathObjectPtr val) {
3530 int ret = 0;
3531
3532 if (val == NULL)
3533 return(0);
3534 switch (val->type) {
3535 case XPATH_UNDEFINED:
3536#ifdef DEBUG_EXPR
3537 xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n");
3538#endif
3539 ret = 0;
3540 break;
3541 case XPATH_XSLT_TREE:
3542 case XPATH_NODESET:
3543 ret = xmlXPathCastNodeSetToBoolean(val->nodesetval);
3544 break;
3545 case XPATH_STRING:
3546 ret = xmlXPathCastStringToBoolean(val->stringval);
3547 break;
3548 case XPATH_NUMBER:
3549 ret = xmlXPathCastNumberToBoolean(val->floatval);
3550 break;
3551 case XPATH_BOOLEAN:
3552 ret = val->boolval;
3553 break;
3554 case XPATH_USERS:
3555 case XPATH_POINT:
3556 case XPATH_RANGE:
3557 case XPATH_LOCATIONSET:
3558 TODO;
3559 ret = 0;
3560 break;
3561 }
3562 return(ret);
3563}
3564
3565
3566/**
3567 * xmlXPathConvertBoolean:
3568 * @val: an XPath object
3569 *
3570 * Converts an existing object to its boolean() equivalent
3571 *
3572 * Returns the new object, the old one is freed (or the operation
3573 * is done directly on @val)
3574 */
3575xmlXPathObjectPtr
3576xmlXPathConvertBoolean(xmlXPathObjectPtr val) {
3577 xmlXPathObjectPtr ret;
3578
3579 if (val == NULL)
3580 return(xmlXPathNewBoolean(0));
3581 if (val->type == XPATH_BOOLEAN)
3582 return(val);
3583 ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val));
3584 xmlXPathFreeObject(val);
3585 return(ret);
3586}
3587
Owen Taylor3473f882001-02-23 17:55:21 +00003588/************************************************************************
3589 * *
3590 * Routines to handle XPath contexts *
3591 * *
3592 ************************************************************************/
3593
3594/**
3595 * xmlXPathNewContext:
3596 * @doc: the XML document
3597 *
3598 * Create a new xmlXPathContext
3599 *
Daniel Veillardaf43f632002-03-08 15:05:20 +00003600 * Returns the xmlXPathContext just allocated. The caller will need to free it.
Owen Taylor3473f882001-02-23 17:55:21 +00003601 */
3602xmlXPathContextPtr
3603xmlXPathNewContext(xmlDocPtr doc) {
3604 xmlXPathContextPtr ret;
3605
3606 ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext));
3607 if (ret == NULL) {
3608 xmlGenericError(xmlGenericErrorContext,
3609 "xmlXPathNewContext: out of memory\n");
3610 return(NULL);
3611 }
3612 memset(ret, 0 , (size_t) sizeof(xmlXPathContext));
3613 ret->doc = doc;
3614 ret->node = NULL;
3615
3616 ret->varHash = NULL;
3617
3618 ret->nb_types = 0;
3619 ret->max_types = 0;
3620 ret->types = NULL;
3621
3622 ret->funcHash = xmlHashCreate(0);
3623
3624 ret->nb_axis = 0;
3625 ret->max_axis = 0;
3626 ret->axis = NULL;
3627
3628 ret->nsHash = NULL;
3629 ret->user = NULL;
3630
3631 ret->contextSize = -1;
3632 ret->proximityPosition = -1;
3633
3634 xmlXPathRegisterAllFunctions(ret);
3635
3636 return(ret);
3637}
3638
3639/**
3640 * xmlXPathFreeContext:
3641 * @ctxt: the context to free
3642 *
3643 * Free up an xmlXPathContext
3644 */
3645void
3646xmlXPathFreeContext(xmlXPathContextPtr ctxt) {
3647 xmlXPathRegisteredNsCleanup(ctxt);
3648 xmlXPathRegisteredFuncsCleanup(ctxt);
3649 xmlXPathRegisteredVariablesCleanup(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003650 xmlFree(ctxt);
3651}
3652
3653/************************************************************************
3654 * *
3655 * Routines to handle XPath parser contexts *
3656 * *
3657 ************************************************************************/
3658
3659#define CHECK_CTXT(ctxt) \
3660 if (ctxt == NULL) { \
3661 xmlGenericError(xmlGenericErrorContext, \
3662 "%s:%d Internal error: ctxt == NULL\n", \
3663 __FILE__, __LINE__); \
3664 } \
3665
3666
3667#define CHECK_CONTEXT(ctxt) \
3668 if (ctxt == NULL) { \
3669 xmlGenericError(xmlGenericErrorContext, \
3670 "%s:%d Internal error: no context\n", \
3671 __FILE__, __LINE__); \
3672 } \
3673 else if (ctxt->doc == NULL) { \
3674 xmlGenericError(xmlGenericErrorContext, \
3675 "%s:%d Internal error: no document\n", \
3676 __FILE__, __LINE__); \
3677 } \
3678 else if (ctxt->doc->children == NULL) { \
3679 xmlGenericError(xmlGenericErrorContext, \
3680 "%s:%d Internal error: document without root\n", \
3681 __FILE__, __LINE__); \
3682 } \
3683
3684
3685/**
3686 * xmlXPathNewParserContext:
3687 * @str: the XPath expression
3688 * @ctxt: the XPath context
3689 *
3690 * Create a new xmlXPathParserContext
3691 *
3692 * Returns the xmlXPathParserContext just allocated.
3693 */
3694xmlXPathParserContextPtr
3695xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) {
3696 xmlXPathParserContextPtr ret;
3697
3698 ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext));
3699 if (ret == NULL) {
3700 xmlGenericError(xmlGenericErrorContext,
3701 "xmlXPathNewParserContext: out of memory\n");
3702 return(NULL);
3703 }
3704 memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext));
3705 ret->cur = ret->base = str;
3706 ret->context = ctxt;
3707
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003708 ret->comp = xmlXPathNewCompExpr();
3709 if (ret->comp == NULL) {
3710 xmlFree(ret->valueTab);
3711 xmlFree(ret);
3712 return(NULL);
3713 }
3714
3715 return(ret);
3716}
3717
3718/**
3719 * xmlXPathCompParserContext:
3720 * @comp: the XPath compiled expression
3721 * @ctxt: the XPath context
3722 *
3723 * Create a new xmlXPathParserContext when processing a compiled expression
3724 *
3725 * Returns the xmlXPathParserContext just allocated.
3726 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003727static xmlXPathParserContextPtr
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003728xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) {
3729 xmlXPathParserContextPtr ret;
3730
3731 ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext));
3732 if (ret == NULL) {
3733 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003734 "xmlXPathCompParserContext: out of memory\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003735 return(NULL);
3736 }
3737 memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext));
3738
Owen Taylor3473f882001-02-23 17:55:21 +00003739 /* Allocate the value stack */
3740 ret->valueTab = (xmlXPathObjectPtr *)
3741 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003742 if (ret->valueTab == NULL) {
3743 xmlFree(ret);
3744 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003745 "xmlXPathCompParserContext: out of memory\n");
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003746 return(NULL);
3747 }
Owen Taylor3473f882001-02-23 17:55:21 +00003748 ret->valueNr = 0;
3749 ret->valueMax = 10;
3750 ret->value = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003751
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00003752 ret->context = ctxt;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003753 ret->comp = comp;
3754
Owen Taylor3473f882001-02-23 17:55:21 +00003755 return(ret);
3756}
3757
3758/**
3759 * xmlXPathFreeParserContext:
3760 * @ctxt: the context to free
3761 *
3762 * Free up an xmlXPathParserContext
3763 */
3764void
3765xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) {
3766 if (ctxt->valueTab != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00003767 xmlFree(ctxt->valueTab);
3768 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00003769 if (ctxt->comp)
3770 xmlXPathFreeCompExpr(ctxt->comp);
Owen Taylor3473f882001-02-23 17:55:21 +00003771 xmlFree(ctxt);
3772}
3773
3774/************************************************************************
3775 * *
3776 * The implicit core function library *
3777 * *
3778 ************************************************************************/
3779
Owen Taylor3473f882001-02-23 17:55:21 +00003780/**
Daniel Veillardf06307e2001-07-03 10:35:50 +00003781 * xmlXPathNodeStringHash:
3782 * @node: a node pointer
3783 *
3784 * Function computing the beginning of the string value of the node,
3785 * used to speed up comparisons
3786 *
3787 * Returns an int usable as a hash
3788 */
3789static unsigned int
3790xmlXPathNodeValHash(xmlNodePtr node) {
3791 int len = 2;
3792 const xmlChar * string = NULL;
3793 xmlNodePtr tmp = NULL;
3794 unsigned int ret = 0;
3795
3796 if (node == NULL)
3797 return(0);
3798
3799
3800 switch (node->type) {
3801 case XML_COMMENT_NODE:
3802 case XML_PI_NODE:
3803 case XML_CDATA_SECTION_NODE:
3804 case XML_TEXT_NODE:
3805 string = node->content;
3806 if (string == NULL)
3807 return(0);
3808 if (string[0] == 0)
3809 return(0);
3810 return(((unsigned int) string[0]) +
3811 (((unsigned int) string[1]) << 8));
3812 case XML_NAMESPACE_DECL:
3813 string = ((xmlNsPtr)node)->href;
3814 if (string == NULL)
3815 return(0);
3816 if (string[0] == 0)
3817 return(0);
3818 return(((unsigned int) string[0]) +
3819 (((unsigned int) string[1]) << 8));
3820 case XML_ATTRIBUTE_NODE:
3821 tmp = ((xmlAttrPtr) node)->children;
3822 break;
3823 case XML_ELEMENT_NODE:
3824 tmp = node->children;
3825 break;
3826 default:
3827 return(0);
3828 }
3829 while (tmp != NULL) {
3830 switch (tmp->type) {
3831 case XML_COMMENT_NODE:
3832 case XML_PI_NODE:
3833 case XML_CDATA_SECTION_NODE:
3834 case XML_TEXT_NODE:
3835 string = tmp->content;
3836 break;
3837 case XML_NAMESPACE_DECL:
3838 string = ((xmlNsPtr)tmp)->href;
3839 break;
3840 default:
3841 break;
3842 }
3843 if ((string != NULL) && (string[0] != 0)) {
3844 if (string[0] == 0)
3845 return(0);
3846 if (len == 1) {
3847 return(ret + (((unsigned int) string[0]) << 8));
3848 }
3849 if (string[1] == 0) {
3850 len = 1;
3851 ret = (unsigned int) string[0];
3852 } else {
3853 return(((unsigned int) string[0]) +
3854 (((unsigned int) string[1]) << 8));
3855 }
3856 }
3857 /*
3858 * Skip to next node
3859 */
3860 if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) {
3861 if (tmp->children->type != XML_ENTITY_DECL) {
3862 tmp = tmp->children;
3863 continue;
3864 }
3865 }
3866 if (tmp == node)
3867 break;
3868
3869 if (tmp->next != NULL) {
3870 tmp = tmp->next;
3871 continue;
3872 }
3873
3874 do {
3875 tmp = tmp->parent;
3876 if (tmp == NULL)
3877 break;
3878 if (tmp == node) {
3879 tmp = NULL;
3880 break;
3881 }
3882 if (tmp->next != NULL) {
3883 tmp = tmp->next;
3884 break;
3885 }
3886 } while (tmp != NULL);
3887 }
3888 return(ret);
3889}
3890
3891/**
3892 * xmlXPathStringHash:
3893 * @string: a string
3894 *
3895 * Function computing the beginning of the string value of the node,
3896 * used to speed up comparisons
3897 *
3898 * Returns an int usable as a hash
3899 */
3900static unsigned int
3901xmlXPathStringHash(const xmlChar * string) {
3902 if (string == NULL)
3903 return((unsigned int) 0);
3904 if (string[0] == 0)
3905 return(0);
3906 return(((unsigned int) string[0]) +
3907 (((unsigned int) string[1]) << 8));
3908}
3909
3910/**
Owen Taylor3473f882001-02-23 17:55:21 +00003911 * xmlXPathCompareNodeSetFloat:
3912 * @ctxt: the XPath Parser context
3913 * @inf: less than (1) or greater than (0)
3914 * @strict: is the comparison strict
3915 * @arg: the node set
3916 * @f: the value
3917 *
3918 * Implement the compare operation between a nodeset and a number
3919 * @ns < @val (1, 1, ...
3920 * @ns <= @val (1, 0, ...
3921 * @ns > @val (0, 1, ...
3922 * @ns >= @val (0, 0, ...
3923 *
3924 * If one object to be compared is a node-set and the other is a number,
3925 * then the comparison will be true if and only if there is a node in the
3926 * node-set such that the result of performing the comparison on the number
3927 * to be compared and on the result of converting the string-value of that
3928 * node to a number using the number function is true.
3929 *
3930 * Returns 0 or 1 depending on the results of the test.
3931 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003932static int
Owen Taylor3473f882001-02-23 17:55:21 +00003933xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict,
3934 xmlXPathObjectPtr arg, xmlXPathObjectPtr f) {
3935 int i, ret = 0;
3936 xmlNodeSetPtr ns;
3937 xmlChar *str2;
3938
3939 if ((f == NULL) || (arg == NULL) ||
3940 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) {
3941 xmlXPathFreeObject(arg);
3942 xmlXPathFreeObject(f);
3943 return(0);
3944 }
3945 ns = arg->nodesetval;
Daniel Veillard911f49a2001-04-07 15:39:35 +00003946 if (ns != NULL) {
3947 for (i = 0;i < ns->nodeNr;i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00003948 str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
Daniel Veillard911f49a2001-04-07 15:39:35 +00003949 if (str2 != NULL) {
3950 valuePush(ctxt,
3951 xmlXPathNewString(str2));
3952 xmlFree(str2);
3953 xmlXPathNumberFunction(ctxt, 1);
3954 valuePush(ctxt, xmlXPathObjectCopy(f));
3955 ret = xmlXPathCompareValues(ctxt, inf, strict);
3956 if (ret)
3957 break;
3958 }
3959 }
Owen Taylor3473f882001-02-23 17:55:21 +00003960 }
3961 xmlXPathFreeObject(arg);
3962 xmlXPathFreeObject(f);
3963 return(ret);
3964}
3965
3966/**
3967 * xmlXPathCompareNodeSetString:
3968 * @ctxt: the XPath Parser context
3969 * @inf: less than (1) or greater than (0)
3970 * @strict: is the comparison strict
3971 * @arg: the node set
3972 * @s: the value
3973 *
3974 * Implement the compare operation between a nodeset and a string
3975 * @ns < @val (1, 1, ...
3976 * @ns <= @val (1, 0, ...
3977 * @ns > @val (0, 1, ...
3978 * @ns >= @val (0, 0, ...
3979 *
3980 * If one object to be compared is a node-set and the other is a string,
3981 * then the comparison will be true if and only if there is a node in
3982 * the node-set such that the result of performing the comparison on the
3983 * string-value of the node and the other string is true.
3984 *
3985 * Returns 0 or 1 depending on the results of the test.
3986 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00003987static int
Owen Taylor3473f882001-02-23 17:55:21 +00003988xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict,
3989 xmlXPathObjectPtr arg, xmlXPathObjectPtr s) {
3990 int i, ret = 0;
3991 xmlNodeSetPtr ns;
3992 xmlChar *str2;
3993
3994 if ((s == NULL) || (arg == NULL) ||
3995 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) {
3996 xmlXPathFreeObject(arg);
3997 xmlXPathFreeObject(s);
3998 return(0);
3999 }
4000 ns = arg->nodesetval;
Daniel Veillard911f49a2001-04-07 15:39:35 +00004001 if (ns != NULL) {
4002 for (i = 0;i < ns->nodeNr;i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004003 str2 = xmlXPathCastNodeToString(ns->nodeTab[i]);
Daniel Veillard911f49a2001-04-07 15:39:35 +00004004 if (str2 != NULL) {
4005 valuePush(ctxt,
4006 xmlXPathNewString(str2));
4007 xmlFree(str2);
4008 valuePush(ctxt, xmlXPathObjectCopy(s));
4009 ret = xmlXPathCompareValues(ctxt, inf, strict);
4010 if (ret)
4011 break;
4012 }
4013 }
Owen Taylor3473f882001-02-23 17:55:21 +00004014 }
4015 xmlXPathFreeObject(arg);
4016 xmlXPathFreeObject(s);
4017 return(ret);
4018}
4019
4020/**
4021 * xmlXPathCompareNodeSets:
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004022 * @inf: less than (1) or greater than (0)
Owen Taylor3473f882001-02-23 17:55:21 +00004023 * @strict: is the comparison strict
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004024 * @arg1: the first node set object
Owen Taylor3473f882001-02-23 17:55:21 +00004025 * @arg2: the second node set object
4026 *
4027 * Implement the compare operation on nodesets:
4028 *
4029 * If both objects to be compared are node-sets, then the comparison
4030 * will be true if and only if there is a node in the first node-set
4031 * and a node in the second node-set such that the result of performing
4032 * the comparison on the string-values of the two nodes is true.
4033 * ....
4034 * When neither object to be compared is a node-set and the operator
4035 * is <=, <, >= or >, then the objects are compared by converting both
4036 * objects to numbers and comparing the numbers according to IEEE 754.
4037 * ....
4038 * The number function converts its argument to a number as follows:
4039 * - a string that consists of optional whitespace followed by an
4040 * optional minus sign followed by a Number followed by whitespace
4041 * is converted to the IEEE 754 number that is nearest (according
4042 * to the IEEE 754 round-to-nearest rule) to the mathematical value
4043 * represented by the string; any other string is converted to NaN
4044 *
4045 * Conclusion all nodes need to be converted first to their string value
4046 * and then the comparison must be done when possible
4047 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004048static int
4049xmlXPathCompareNodeSets(int inf, int strict,
Owen Taylor3473f882001-02-23 17:55:21 +00004050 xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) {
4051 int i, j, init = 0;
4052 double val1;
4053 double *values2;
4054 int ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004055 xmlNodeSetPtr ns1;
4056 xmlNodeSetPtr ns2;
4057
4058 if ((arg1 == NULL) ||
Daniel Veillard4dd93462001-04-02 15:16:19 +00004059 ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) {
4060 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004061 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004062 }
Owen Taylor3473f882001-02-23 17:55:21 +00004063 if ((arg2 == NULL) ||
Daniel Veillard4dd93462001-04-02 15:16:19 +00004064 ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) {
4065 xmlXPathFreeObject(arg1);
4066 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004067 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004068 }
Owen Taylor3473f882001-02-23 17:55:21 +00004069
4070 ns1 = arg1->nodesetval;
4071 ns2 = arg2->nodesetval;
4072
Daniel Veillardd8df6c02001-04-05 16:54:14 +00004073 if ((ns1 == NULL) || (ns1->nodeNr <= 0)) {
Daniel Veillard4dd93462001-04-02 15:16:19 +00004074 xmlXPathFreeObject(arg1);
4075 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004076 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004077 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00004078 if ((ns2 == NULL) || (ns2->nodeNr <= 0)) {
Daniel Veillard4dd93462001-04-02 15:16:19 +00004079 xmlXPathFreeObject(arg1);
4080 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004081 return(0);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004082 }
Owen Taylor3473f882001-02-23 17:55:21 +00004083
4084 values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double));
4085 if (values2 == NULL) {
Daniel Veillard4dd93462001-04-02 15:16:19 +00004086 xmlXPathFreeObject(arg1);
4087 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004088 return(0);
4089 }
4090 for (i = 0;i < ns1->nodeNr;i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004091 val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]);
Daniel Veillardcda96922001-08-21 10:56:31 +00004092 if (xmlXPathIsNaN(val1))
Owen Taylor3473f882001-02-23 17:55:21 +00004093 continue;
4094 for (j = 0;j < ns2->nodeNr;j++) {
4095 if (init == 0) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004096 values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]);
Owen Taylor3473f882001-02-23 17:55:21 +00004097 }
Daniel Veillardcda96922001-08-21 10:56:31 +00004098 if (xmlXPathIsNaN(values2[j]))
Owen Taylor3473f882001-02-23 17:55:21 +00004099 continue;
4100 if (inf && strict)
4101 ret = (val1 < values2[j]);
4102 else if (inf && !strict)
4103 ret = (val1 <= values2[j]);
4104 else if (!inf && strict)
4105 ret = (val1 > values2[j]);
4106 else if (!inf && !strict)
4107 ret = (val1 >= values2[j]);
4108 if (ret)
4109 break;
4110 }
4111 if (ret)
4112 break;
4113 init = 1;
4114 }
4115 xmlFree(values2);
Daniel Veillard4dd93462001-04-02 15:16:19 +00004116 xmlXPathFreeObject(arg1);
4117 xmlXPathFreeObject(arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004118 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004119}
4120
4121/**
4122 * xmlXPathCompareNodeSetValue:
4123 * @ctxt: the XPath Parser context
4124 * @inf: less than (1) or greater than (0)
4125 * @strict: is the comparison strict
4126 * @arg: the node set
4127 * @val: the value
4128 *
4129 * Implement the compare operation between a nodeset and a value
4130 * @ns < @val (1, 1, ...
4131 * @ns <= @val (1, 0, ...
4132 * @ns > @val (0, 1, ...
4133 * @ns >= @val (0, 0, ...
4134 *
4135 * If one object to be compared is a node-set and the other is a boolean,
4136 * then the comparison will be true if and only if the result of performing
4137 * the comparison on the boolean and on the result of converting
4138 * the node-set to a boolean using the boolean function is true.
4139 *
4140 * Returns 0 or 1 depending on the results of the test.
4141 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004142static int
Owen Taylor3473f882001-02-23 17:55:21 +00004143xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict,
4144 xmlXPathObjectPtr arg, xmlXPathObjectPtr val) {
4145 if ((val == NULL) || (arg == NULL) ||
4146 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
4147 return(0);
4148
4149 switch(val->type) {
4150 case XPATH_NUMBER:
4151 return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val));
4152 case XPATH_NODESET:
4153 case XPATH_XSLT_TREE:
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004154 return(xmlXPathCompareNodeSets(inf, strict, arg, val));
Owen Taylor3473f882001-02-23 17:55:21 +00004155 case XPATH_STRING:
4156 return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val));
4157 case XPATH_BOOLEAN:
4158 valuePush(ctxt, arg);
4159 xmlXPathBooleanFunction(ctxt, 1);
4160 valuePush(ctxt, val);
4161 return(xmlXPathCompareValues(ctxt, inf, strict));
4162 default:
4163 TODO
Owen Taylor3473f882001-02-23 17:55:21 +00004164 }
4165 return(0);
4166}
4167
4168/**
4169 * xmlXPathEqualNodeSetString
4170 * @arg: the nodeset object argument
4171 * @str: the string to compare to.
4172 *
4173 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4174 * If one object to be compared is a node-set and the other is a string,
4175 * then the comparison will be true if and only if there is a node in
4176 * the node-set such that the result of performing the comparison on the
4177 * string-value of the node and the other string is true.
4178 *
4179 * Returns 0 or 1 depending on the results of the test.
4180 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004181static int
Daniel Veillardf06307e2001-07-03 10:35:50 +00004182xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str)
4183{
Owen Taylor3473f882001-02-23 17:55:21 +00004184 int i;
4185 xmlNodeSetPtr ns;
4186 xmlChar *str2;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004187 unsigned int hash;
Owen Taylor3473f882001-02-23 17:55:21 +00004188
4189 if ((str == NULL) || (arg == NULL) ||
Daniel Veillardf06307e2001-07-03 10:35:50 +00004190 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
4191 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00004192 ns = arg->nodesetval;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004193 hash = xmlXPathStringHash(str);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00004194 if (ns == NULL)
Daniel Veillardf06307e2001-07-03 10:35:50 +00004195 return (0);
Daniel Veillard73c9c042001-07-05 20:02:54 +00004196 if (ns->nodeNr <= 0) {
4197 if (hash == 0)
4198 return(1);
4199 return(0);
4200 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004201 for (i = 0; i < ns->nodeNr; i++) {
4202 if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) {
4203 str2 = xmlNodeGetContent(ns->nodeTab[i]);
4204 if ((str2 != NULL) && (xmlStrEqual(str, str2))) {
4205 xmlFree(str2);
4206 return (1);
4207 }
4208 if (str2 != NULL)
4209 xmlFree(str2);
4210 }
Owen Taylor3473f882001-02-23 17:55:21 +00004211 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004212 return (0);
Owen Taylor3473f882001-02-23 17:55:21 +00004213}
4214
4215/**
4216 * xmlXPathEqualNodeSetFloat
4217 * @arg: the nodeset object argument
4218 * @f: the float to compare to
4219 *
4220 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4221 * If one object to be compared is a node-set and the other is a number,
4222 * then the comparison will be true if and only if there is a node in
4223 * the node-set such that the result of performing the comparison on the
4224 * number to be compared and on the result of converting the string-value
4225 * of that node to a number using the number function is true.
4226 *
4227 * Returns 0 or 1 depending on the results of the test.
4228 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004229static int
Owen Taylor3473f882001-02-23 17:55:21 +00004230xmlXPathEqualNodeSetFloat(xmlXPathObjectPtr arg, double f) {
4231 char buf[100] = "";
4232
4233 if ((arg == NULL) ||
4234 ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE)))
4235 return(0);
4236
Bjorn Reesee1dc0112001-03-03 12:09:03 +00004237 xmlXPathFormatNumber(f, buf, sizeof(buf));
Owen Taylor3473f882001-02-23 17:55:21 +00004238 return(xmlXPathEqualNodeSetString(arg, BAD_CAST buf));
4239}
4240
4241
4242/**
4243 * xmlXPathEqualNodeSets
4244 * @arg1: first nodeset object argument
4245 * @arg2: second nodeset object argument
4246 *
4247 * Implement the equal operation on XPath nodesets: @arg1 == @arg2
4248 * If both objects to be compared are node-sets, then the comparison
4249 * will be true if and only if there is a node in the first node-set and
4250 * a node in the second node-set such that the result of performing the
4251 * comparison on the string-values of the two nodes is true.
4252 *
4253 * (needless to say, this is a costly operation)
4254 *
4255 * Returns 0 or 1 depending on the results of the test.
4256 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004257static int
Owen Taylor3473f882001-02-23 17:55:21 +00004258xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) {
4259 int i, j;
Daniel Veillardf06307e2001-07-03 10:35:50 +00004260 unsigned int *hashs1;
4261 unsigned int *hashs2;
Owen Taylor3473f882001-02-23 17:55:21 +00004262 xmlChar **values1;
4263 xmlChar **values2;
4264 int ret = 0;
4265 xmlNodeSetPtr ns1;
4266 xmlNodeSetPtr ns2;
4267
4268 if ((arg1 == NULL) ||
4269 ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)))
4270 return(0);
4271 if ((arg2 == NULL) ||
4272 ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE)))
4273 return(0);
4274
4275 ns1 = arg1->nodesetval;
4276 ns2 = arg2->nodesetval;
4277
Daniel Veillard911f49a2001-04-07 15:39:35 +00004278 if ((ns1 == NULL) || (ns1->nodeNr <= 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004279 return(0);
Daniel Veillard911f49a2001-04-07 15:39:35 +00004280 if ((ns2 == NULL) || (ns2->nodeNr <= 0))
Owen Taylor3473f882001-02-23 17:55:21 +00004281 return(0);
4282
4283 /*
4284 * check if there is a node pertaining to both sets
4285 */
4286 for (i = 0;i < ns1->nodeNr;i++)
4287 for (j = 0;j < ns2->nodeNr;j++)
4288 if (ns1->nodeTab[i] == ns2->nodeTab[j])
4289 return(1);
4290
4291 values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *));
4292 if (values1 == NULL)
4293 return(0);
Daniel Veillardf06307e2001-07-03 10:35:50 +00004294 hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int));
4295 if (hashs1 == NULL) {
4296 xmlFree(values1);
4297 return(0);
4298 }
Owen Taylor3473f882001-02-23 17:55:21 +00004299 memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *));
4300 values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *));
4301 if (values2 == NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00004302 xmlFree(hashs1);
Owen Taylor3473f882001-02-23 17:55:21 +00004303 xmlFree(values1);
4304 return(0);
4305 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00004306 hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int));
4307 if (hashs2 == NULL) {
4308 xmlFree(hashs1);
4309 xmlFree(values1);
4310 xmlFree(values2);
4311 return(0);
4312 }
Owen Taylor3473f882001-02-23 17:55:21 +00004313 memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *));
4314 for (i = 0;i < ns1->nodeNr;i++) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00004315 hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00004316 for (j = 0;j < ns2->nodeNr;j++) {
4317 if (i == 0)
Daniel Veillardf06307e2001-07-03 10:35:50 +00004318 hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]);
4319 if (hashs1[i] == hashs2[j]) {
4320 if (values1[i] == NULL)
4321 values1[i] = xmlNodeGetContent(ns1->nodeTab[i]);
4322 if (values2[j] == NULL)
4323 values2[j] = xmlNodeGetContent(ns2->nodeTab[j]);
4324 ret = xmlStrEqual(values1[i], values2[j]);
4325 if (ret)
4326 break;
4327 }
Owen Taylor3473f882001-02-23 17:55:21 +00004328 }
4329 if (ret)
4330 break;
4331 }
4332 for (i = 0;i < ns1->nodeNr;i++)
4333 if (values1[i] != NULL)
4334 xmlFree(values1[i]);
4335 for (j = 0;j < ns2->nodeNr;j++)
4336 if (values2[j] != NULL)
4337 xmlFree(values2[j]);
4338 xmlFree(values1);
4339 xmlFree(values2);
Daniel Veillardf06307e2001-07-03 10:35:50 +00004340 xmlFree(hashs1);
4341 xmlFree(hashs2);
Owen Taylor3473f882001-02-23 17:55:21 +00004342 return(ret);
4343}
4344
4345/**
4346 * xmlXPathEqualValues:
4347 * @ctxt: the XPath Parser context
4348 *
4349 * Implement the equal operation on XPath objects content: @arg1 == @arg2
4350 *
4351 * Returns 0 or 1 depending on the results of the test.
4352 */
4353int
4354xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) {
4355 xmlXPathObjectPtr arg1, arg2;
4356 int ret = 0;
4357
4358 arg1 = valuePop(ctxt);
4359 if (arg1 == NULL)
4360 XP_ERROR0(XPATH_INVALID_OPERAND);
4361
4362 arg2 = valuePop(ctxt);
4363 if (arg2 == NULL) {
4364 xmlXPathFreeObject(arg1);
4365 XP_ERROR0(XPATH_INVALID_OPERAND);
4366 }
4367
4368 if (arg1 == arg2) {
4369#ifdef DEBUG_EXPR
4370 xmlGenericError(xmlGenericErrorContext,
4371 "Equal: by pointer\n");
4372#endif
4373 return(1);
4374 }
4375
4376 switch (arg1->type) {
4377 case XPATH_UNDEFINED:
4378#ifdef DEBUG_EXPR
4379 xmlGenericError(xmlGenericErrorContext,
4380 "Equal: undefined\n");
4381#endif
4382 break;
4383 case XPATH_XSLT_TREE:
4384 case XPATH_NODESET:
4385 switch (arg2->type) {
4386 case XPATH_UNDEFINED:
4387#ifdef DEBUG_EXPR
4388 xmlGenericError(xmlGenericErrorContext,
4389 "Equal: undefined\n");
4390#endif
4391 break;
4392 case XPATH_XSLT_TREE:
4393 case XPATH_NODESET:
4394 ret = xmlXPathEqualNodeSets(arg1, arg2);
4395 break;
4396 case XPATH_BOOLEAN:
4397 if ((arg1->nodesetval == NULL) ||
4398 (arg1->nodesetval->nodeNr == 0)) ret = 0;
4399 else
4400 ret = 1;
4401 ret = (ret == arg2->boolval);
4402 break;
4403 case XPATH_NUMBER:
4404 ret = xmlXPathEqualNodeSetFloat(arg1, arg2->floatval);
4405 break;
4406 case XPATH_STRING:
4407 ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval);
4408 break;
4409 case XPATH_USERS:
4410 case XPATH_POINT:
4411 case XPATH_RANGE:
4412 case XPATH_LOCATIONSET:
4413 TODO
4414 break;
4415 }
4416 break;
4417 case XPATH_BOOLEAN:
4418 switch (arg2->type) {
4419 case XPATH_UNDEFINED:
4420#ifdef DEBUG_EXPR
4421 xmlGenericError(xmlGenericErrorContext,
4422 "Equal: undefined\n");
4423#endif
4424 break;
4425 case XPATH_NODESET:
4426 case XPATH_XSLT_TREE:
4427 if ((arg2->nodesetval == NULL) ||
Daniel Veillardbd6e6312002-04-01 08:04:14 +00004428 (arg2->nodesetval->nodeNr == 0))
4429 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004430 else
4431 ret = 1;
Daniel Veillardbd6e6312002-04-01 08:04:14 +00004432 ret = (ret == arg1->boolval);
Owen Taylor3473f882001-02-23 17:55:21 +00004433 break;
4434 case XPATH_BOOLEAN:
4435#ifdef DEBUG_EXPR
4436 xmlGenericError(xmlGenericErrorContext,
4437 "Equal: %d boolean %d \n",
4438 arg1->boolval, arg2->boolval);
4439#endif
4440 ret = (arg1->boolval == arg2->boolval);
4441 break;
4442 case XPATH_NUMBER:
4443 if (arg2->floatval) ret = 1;
4444 else ret = 0;
4445 ret = (arg1->boolval == ret);
4446 break;
4447 case XPATH_STRING:
4448 if ((arg2->stringval == NULL) ||
4449 (arg2->stringval[0] == 0)) ret = 0;
4450 else
4451 ret = 1;
4452 ret = (arg1->boolval == ret);
4453 break;
4454 case XPATH_USERS:
4455 case XPATH_POINT:
4456 case XPATH_RANGE:
4457 case XPATH_LOCATIONSET:
4458 TODO
4459 break;
4460 }
4461 break;
4462 case XPATH_NUMBER:
4463 switch (arg2->type) {
4464 case XPATH_UNDEFINED:
4465#ifdef DEBUG_EXPR
4466 xmlGenericError(xmlGenericErrorContext,
4467 "Equal: undefined\n");
4468#endif
4469 break;
4470 case XPATH_NODESET:
4471 case XPATH_XSLT_TREE:
4472 ret = xmlXPathEqualNodeSetFloat(arg2, arg1->floatval);
4473 break;
4474 case XPATH_BOOLEAN:
4475 if (arg1->floatval) ret = 1;
4476 else ret = 0;
4477 ret = (arg2->boolval == ret);
4478 break;
4479 case XPATH_STRING:
4480 valuePush(ctxt, arg2);
4481 xmlXPathNumberFunction(ctxt, 1);
4482 arg2 = valuePop(ctxt);
4483 /* no break on purpose */
4484 case XPATH_NUMBER:
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004485 /* Hand check NaN and Infinity equalities */
Daniel Veillard21458c82002-03-27 16:12:22 +00004486 if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) {
4487 ret = 0;
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004488 } else if (xmlXPathIsInf(arg1->floatval) == 1) {
4489 if (xmlXPathIsInf(arg2->floatval) == 1)
4490 ret = 1;
4491 else
4492 ret = 0;
4493 } else if (xmlXPathIsInf(arg1->floatval) == -1) {
4494 if (xmlXPathIsInf(arg2->floatval) == -1)
4495 ret = 1;
4496 else
4497 ret = 0;
4498 } else if (xmlXPathIsInf(arg2->floatval) == 1) {
4499 if (xmlXPathIsInf(arg1->floatval) == 1)
4500 ret = 1;
4501 else
4502 ret = 0;
4503 } else if (xmlXPathIsInf(arg2->floatval) == -1) {
4504 if (xmlXPathIsInf(arg1->floatval) == -1)
4505 ret = 1;
4506 else
4507 ret = 0;
Daniel Veillard21458c82002-03-27 16:12:22 +00004508 } else {
4509 ret = (arg1->floatval == arg2->floatval);
4510 }
Owen Taylor3473f882001-02-23 17:55:21 +00004511 break;
4512 case XPATH_USERS:
4513 case XPATH_POINT:
4514 case XPATH_RANGE:
4515 case XPATH_LOCATIONSET:
4516 TODO
4517 break;
4518 }
4519 break;
4520 case XPATH_STRING:
4521 switch (arg2->type) {
4522 case XPATH_UNDEFINED:
4523#ifdef DEBUG_EXPR
4524 xmlGenericError(xmlGenericErrorContext,
4525 "Equal: undefined\n");
4526#endif
4527 break;
4528 case XPATH_NODESET:
4529 case XPATH_XSLT_TREE:
4530 ret = xmlXPathEqualNodeSetString(arg2, arg1->stringval);
4531 break;
4532 case XPATH_BOOLEAN:
4533 if ((arg1->stringval == NULL) ||
4534 (arg1->stringval[0] == 0)) ret = 0;
4535 else
4536 ret = 1;
4537 ret = (arg2->boolval == ret);
4538 break;
4539 case XPATH_STRING:
4540 ret = xmlStrEqual(arg1->stringval, arg2->stringval);
4541 break;
4542 case XPATH_NUMBER:
4543 valuePush(ctxt, arg1);
4544 xmlXPathNumberFunction(ctxt, 1);
4545 arg1 = valuePop(ctxt);
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004546 /* Hand check NaN and Infinity equalities */
Daniel Veillard21458c82002-03-27 16:12:22 +00004547 if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) {
4548 ret = 0;
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004549 } else if (xmlXPathIsInf(arg1->floatval) == 1) {
4550 if (xmlXPathIsInf(arg2->floatval) == 1)
4551 ret = 1;
4552 else
4553 ret = 0;
4554 } else if (xmlXPathIsInf(arg1->floatval) == -1) {
4555 if (xmlXPathIsInf(arg2->floatval) == -1)
4556 ret = 1;
4557 else
4558 ret = 0;
4559 } else if (xmlXPathIsInf(arg2->floatval) == 1) {
4560 if (xmlXPathIsInf(arg1->floatval) == 1)
4561 ret = 1;
4562 else
4563 ret = 0;
4564 } else if (xmlXPathIsInf(arg2->floatval) == -1) {
4565 if (xmlXPathIsInf(arg1->floatval) == -1)
4566 ret = 1;
4567 else
4568 ret = 0;
Daniel Veillard21458c82002-03-27 16:12:22 +00004569 } else {
4570 ret = (arg1->floatval == arg2->floatval);
4571 }
Owen Taylor3473f882001-02-23 17:55:21 +00004572 break;
4573 case XPATH_USERS:
4574 case XPATH_POINT:
4575 case XPATH_RANGE:
4576 case XPATH_LOCATIONSET:
4577 TODO
4578 break;
4579 }
4580 break;
4581 case XPATH_USERS:
4582 case XPATH_POINT:
4583 case XPATH_RANGE:
4584 case XPATH_LOCATIONSET:
4585 TODO
4586 break;
4587 }
4588 xmlXPathFreeObject(arg1);
4589 xmlXPathFreeObject(arg2);
4590 return(ret);
4591}
4592
4593
4594/**
4595 * xmlXPathCompareValues:
4596 * @ctxt: the XPath Parser context
4597 * @inf: less than (1) or greater than (0)
4598 * @strict: is the comparison strict
4599 *
4600 * Implement the compare operation on XPath objects:
4601 * @arg1 < @arg2 (1, 1, ...
4602 * @arg1 <= @arg2 (1, 0, ...
4603 * @arg1 > @arg2 (0, 1, ...
4604 * @arg1 >= @arg2 (0, 0, ...
4605 *
4606 * When neither object to be compared is a node-set and the operator is
4607 * <=, <, >=, >, then the objects are compared by converted both objects
4608 * to numbers and comparing the numbers according to IEEE 754. The <
4609 * comparison will be true if and only if the first number is less than the
4610 * second number. The <= comparison will be true if and only if the first
4611 * number is less than or equal to the second number. The > comparison
4612 * will be true if and only if the first number is greater than the second
4613 * number. The >= comparison will be true if and only if the first number
4614 * is greater than or equal to the second number.
4615 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004616 * Returns 1 if the comparison succeeded, 0 if it failed
Owen Taylor3473f882001-02-23 17:55:21 +00004617 */
4618int
4619xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) {
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004620 int ret = 0, arg1i = 0, arg2i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00004621 xmlXPathObjectPtr arg1, arg2;
4622
4623 arg2 = valuePop(ctxt);
4624 if (arg2 == NULL) {
4625 XP_ERROR0(XPATH_INVALID_OPERAND);
4626 }
4627
4628 arg1 = valuePop(ctxt);
4629 if (arg1 == NULL) {
4630 xmlXPathFreeObject(arg2);
4631 XP_ERROR0(XPATH_INVALID_OPERAND);
4632 }
4633
4634 if ((arg2->type == XPATH_NODESET) || (arg1->type == XPATH_NODESET)) {
4635 if ((arg2->type == XPATH_NODESET) && (arg1->type == XPATH_NODESET)) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004636 ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004637 } else {
4638 if (arg1->type == XPATH_NODESET) {
Daniel Veillard4af6b6e2001-03-06 08:33:38 +00004639 ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict,
4640 arg1, arg2);
Owen Taylor3473f882001-02-23 17:55:21 +00004641 } else {
Daniel Veillard4af6b6e2001-03-06 08:33:38 +00004642 ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict,
4643 arg2, arg1);
Owen Taylor3473f882001-02-23 17:55:21 +00004644 }
4645 }
4646 return(ret);
4647 }
4648
4649 if (arg1->type != XPATH_NUMBER) {
4650 valuePush(ctxt, arg1);
4651 xmlXPathNumberFunction(ctxt, 1);
4652 arg1 = valuePop(ctxt);
4653 }
4654 if (arg1->type != XPATH_NUMBER) {
4655 xmlXPathFreeObject(arg1);
4656 xmlXPathFreeObject(arg2);
4657 XP_ERROR0(XPATH_INVALID_OPERAND);
4658 }
4659 if (arg2->type != XPATH_NUMBER) {
4660 valuePush(ctxt, arg2);
4661 xmlXPathNumberFunction(ctxt, 1);
4662 arg2 = valuePop(ctxt);
4663 }
4664 if (arg2->type != XPATH_NUMBER) {
4665 xmlXPathFreeObject(arg1);
4666 xmlXPathFreeObject(arg2);
4667 XP_ERROR0(XPATH_INVALID_OPERAND);
4668 }
4669 /*
4670 * Add tests for infinity and nan
4671 * => feedback on 3.4 for Inf and NaN
4672 */
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004673 /* Hand check NaN and Infinity comparisons */
Daniel Veillard21458c82002-03-27 16:12:22 +00004674 if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) {
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004675 ret=0;
Daniel Veillard21458c82002-03-27 16:12:22 +00004676 } else {
Daniel Veillardd30be4a2002-03-28 18:25:31 +00004677 arg1i=xmlXPathIsInf(arg1->floatval);
4678 arg2i=xmlXPathIsInf(arg2->floatval);
4679 if (inf && strict) {
4680 if ((arg1i == -1 && arg2i != -1) ||
4681 (arg2i == 1 && arg1i != 1)) {
4682 ret = 1;
4683 } else if (arg1i == 0 && arg2i == 0) {
4684 ret = (arg1->floatval < arg2->floatval);
4685 } else {
4686 ret = 0;
4687 }
4688 }
4689 else if (inf && !strict) {
4690 if (arg1i == -1 || arg2i == 1) {
4691 ret = 1;
4692 } else if (arg1i == 0 && arg2i == 0) {
4693 ret = (arg1->floatval <= arg2->floatval);
4694 } else {
4695 ret = 0;
4696 }
4697 }
4698 else if (!inf && strict) {
4699 if ((arg1i == 1 && arg2i != 1) ||
4700 (arg2i == -1 && arg1i != -1)) {
4701 ret = 1;
4702 } else if (arg1i == 0 && arg2i == 0) {
4703 ret = (arg1->floatval > arg2->floatval);
4704 } else {
4705 ret = 0;
4706 }
4707 }
4708 else if (!inf && !strict) {
4709 if (arg1i == 1 || arg2i == -1) {
4710 ret = 1;
4711 } else if (arg1i == 0 && arg2i == 0) {
4712 ret = (arg1->floatval >= arg2->floatval);
4713 } else {
4714 ret = 0;
4715 }
4716 }
Daniel Veillard21458c82002-03-27 16:12:22 +00004717 }
Owen Taylor3473f882001-02-23 17:55:21 +00004718 xmlXPathFreeObject(arg1);
4719 xmlXPathFreeObject(arg2);
4720 return(ret);
4721}
4722
4723/**
4724 * xmlXPathValueFlipSign:
4725 * @ctxt: the XPath Parser context
4726 *
4727 * Implement the unary - operation on an XPath object
4728 * The numeric operators convert their operands to numbers as if
4729 * by calling the number function.
4730 */
4731void
4732xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004733 CAST_TO_NUMBER;
4734 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillardeca82812002-04-24 11:42:02 +00004735 if (xmlXPathIsNaN(ctxt->value->floatval))
4736 ctxt->value->floatval=xmlXPathNAN;
4737 else if (xmlXPathIsInf(ctxt->value->floatval) == 1)
4738 ctxt->value->floatval=xmlXPathNINF;
4739 else if (xmlXPathIsInf(ctxt->value->floatval) == -1)
4740 ctxt->value->floatval=xmlXPathPINF;
4741 else if (ctxt->value->floatval == 0) {
Daniel Veillard5fc1f082002-03-27 09:05:40 +00004742 if (xmlXPathGetSign(ctxt->value->floatval) == 0)
4743 ctxt->value->floatval = xmlXPathNZERO;
4744 else
4745 ctxt->value->floatval = 0;
4746 }
4747 else
4748 ctxt->value->floatval = - ctxt->value->floatval;
Owen Taylor3473f882001-02-23 17:55:21 +00004749}
4750
4751/**
4752 * xmlXPathAddValues:
4753 * @ctxt: the XPath Parser context
4754 *
4755 * Implement the add operation on XPath objects:
4756 * The numeric operators convert their operands to numbers as if
4757 * by calling the number function.
4758 */
4759void
4760xmlXPathAddValues(xmlXPathParserContextPtr ctxt) {
4761 xmlXPathObjectPtr arg;
4762 double val;
4763
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004764 arg = valuePop(ctxt);
4765 if (arg == NULL)
4766 XP_ERROR(XPATH_INVALID_OPERAND);
4767 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00004768 xmlXPathFreeObject(arg);
4769
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004770 CAST_TO_NUMBER;
4771 CHECK_TYPE(XPATH_NUMBER);
4772 ctxt->value->floatval += val;
Owen Taylor3473f882001-02-23 17:55:21 +00004773}
4774
4775/**
4776 * xmlXPathSubValues:
4777 * @ctxt: the XPath Parser context
4778 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004779 * Implement the subtraction operation on XPath objects:
Owen Taylor3473f882001-02-23 17:55:21 +00004780 * The numeric operators convert their operands to numbers as if
4781 * by calling the number function.
4782 */
4783void
4784xmlXPathSubValues(xmlXPathParserContextPtr ctxt) {
4785 xmlXPathObjectPtr arg;
4786 double val;
4787
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004788 arg = valuePop(ctxt);
4789 if (arg == NULL)
4790 XP_ERROR(XPATH_INVALID_OPERAND);
4791 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00004792 xmlXPathFreeObject(arg);
4793
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004794 CAST_TO_NUMBER;
4795 CHECK_TYPE(XPATH_NUMBER);
4796 ctxt->value->floatval -= val;
Owen Taylor3473f882001-02-23 17:55:21 +00004797}
4798
4799/**
4800 * xmlXPathMultValues:
4801 * @ctxt: the XPath Parser context
4802 *
4803 * Implement the multiply operation on XPath objects:
4804 * The numeric operators convert their operands to numbers as if
4805 * by calling the number function.
4806 */
4807void
4808xmlXPathMultValues(xmlXPathParserContextPtr ctxt) {
4809 xmlXPathObjectPtr arg;
4810 double val;
4811
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004812 arg = valuePop(ctxt);
4813 if (arg == NULL)
4814 XP_ERROR(XPATH_INVALID_OPERAND);
4815 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00004816 xmlXPathFreeObject(arg);
4817
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004818 CAST_TO_NUMBER;
4819 CHECK_TYPE(XPATH_NUMBER);
4820 ctxt->value->floatval *= val;
Owen Taylor3473f882001-02-23 17:55:21 +00004821}
4822
4823/**
4824 * xmlXPathDivValues:
4825 * @ctxt: the XPath Parser context
4826 *
4827 * Implement the div operation on XPath objects @arg1 / @arg2:
4828 * The numeric operators convert their operands to numbers as if
4829 * by calling the number function.
4830 */
4831void
4832xmlXPathDivValues(xmlXPathParserContextPtr ctxt) {
4833 xmlXPathObjectPtr arg;
4834 double val;
4835
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004836 arg = valuePop(ctxt);
4837 if (arg == NULL)
4838 XP_ERROR(XPATH_INVALID_OPERAND);
4839 val = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00004840 xmlXPathFreeObject(arg);
4841
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004842 CAST_TO_NUMBER;
4843 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillardeca82812002-04-24 11:42:02 +00004844 if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval))
4845 ctxt->value->floatval = xmlXPathNAN;
4846 else if (val == 0 && xmlXPathGetSign(val) != 0) {
Daniel Veillard5fc1f082002-03-27 09:05:40 +00004847 if (ctxt->value->floatval == 0)
4848 ctxt->value->floatval = xmlXPathNAN;
4849 else if (ctxt->value->floatval > 0)
4850 ctxt->value->floatval = xmlXPathNINF;
4851 else if (ctxt->value->floatval < 0)
4852 ctxt->value->floatval = xmlXPathPINF;
4853 }
4854 else if (val == 0) {
Daniel Veillard5f4b5992002-02-20 10:22:49 +00004855 if (ctxt->value->floatval == 0)
4856 ctxt->value->floatval = xmlXPathNAN;
4857 else if (ctxt->value->floatval > 0)
4858 ctxt->value->floatval = xmlXPathPINF;
4859 else if (ctxt->value->floatval < 0)
4860 ctxt->value->floatval = xmlXPathNINF;
4861 } else
4862 ctxt->value->floatval /= val;
Owen Taylor3473f882001-02-23 17:55:21 +00004863}
4864
4865/**
4866 * xmlXPathModValues:
4867 * @ctxt: the XPath Parser context
4868 *
4869 * Implement the mod operation on XPath objects: @arg1 / @arg2
4870 * The numeric operators convert their operands to numbers as if
4871 * by calling the number function.
4872 */
4873void
4874xmlXPathModValues(xmlXPathParserContextPtr ctxt) {
4875 xmlXPathObjectPtr arg;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00004876 double arg1, arg2, tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00004877
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004878 arg = valuePop(ctxt);
4879 if (arg == NULL)
4880 XP_ERROR(XPATH_INVALID_OPERAND);
Daniel Veillard5fc1f082002-03-27 09:05:40 +00004881 arg2 = xmlXPathCastToNumber(arg);
Owen Taylor3473f882001-02-23 17:55:21 +00004882 xmlXPathFreeObject(arg);
4883
Daniel Veillardba0b8c92001-05-15 09:43:47 +00004884 CAST_TO_NUMBER;
4885 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillard5fc1f082002-03-27 09:05:40 +00004886 arg1 = ctxt->value->floatval;
Daniel Veillard268fd1b2001-08-26 18:46:36 +00004887 if (arg2 == 0)
4888 ctxt->value->floatval = xmlXPathNAN;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00004889 else {
4890 tmp=arg1/arg2;
4891 ctxt->value->floatval = arg2 * (tmp - (double)((int)tmp));
4892 }
Owen Taylor3473f882001-02-23 17:55:21 +00004893}
4894
4895/************************************************************************
4896 * *
4897 * The traversal functions *
4898 * *
4899 ************************************************************************/
4900
Owen Taylor3473f882001-02-23 17:55:21 +00004901/*
4902 * A traversal function enumerates nodes along an axis.
4903 * Initially it must be called with NULL, and it indicates
4904 * termination on the axis by returning NULL.
4905 */
4906typedef xmlNodePtr (*xmlXPathTraversalFunction)
4907 (xmlXPathParserContextPtr ctxt, xmlNodePtr cur);
4908
4909/**
4910 * xmlXPathNextSelf:
4911 * @ctxt: the XPath Parser context
4912 * @cur: the current node in the traversal
4913 *
4914 * Traversal function for the "self" direction
4915 * The self axis contains just the context node itself
4916 *
4917 * Returns the next element following that axis
4918 */
4919xmlNodePtr
4920xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
4921 if (cur == NULL)
4922 return(ctxt->context->node);
4923 return(NULL);
4924}
4925
4926/**
4927 * xmlXPathNextChild:
4928 * @ctxt: the XPath Parser context
4929 * @cur: the current node in the traversal
4930 *
4931 * Traversal function for the "child" direction
4932 * The child axis contains the children of the context node in document order.
4933 *
4934 * Returns the next element following that axis
4935 */
4936xmlNodePtr
4937xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
4938 if (cur == NULL) {
4939 if (ctxt->context->node == NULL) return(NULL);
4940 switch (ctxt->context->node->type) {
4941 case XML_ELEMENT_NODE:
4942 case XML_TEXT_NODE:
4943 case XML_CDATA_SECTION_NODE:
4944 case XML_ENTITY_REF_NODE:
4945 case XML_ENTITY_NODE:
4946 case XML_PI_NODE:
4947 case XML_COMMENT_NODE:
4948 case XML_NOTATION_NODE:
4949 case XML_DTD_NODE:
4950 return(ctxt->context->node->children);
4951 case XML_DOCUMENT_NODE:
4952 case XML_DOCUMENT_TYPE_NODE:
4953 case XML_DOCUMENT_FRAG_NODE:
4954 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00004955#ifdef LIBXML_DOCB_ENABLED
4956 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00004957#endif
4958 return(((xmlDocPtr) ctxt->context->node)->children);
4959 case XML_ELEMENT_DECL:
4960 case XML_ATTRIBUTE_DECL:
4961 case XML_ENTITY_DECL:
4962 case XML_ATTRIBUTE_NODE:
4963 case XML_NAMESPACE_DECL:
4964 case XML_XINCLUDE_START:
4965 case XML_XINCLUDE_END:
4966 return(NULL);
4967 }
4968 return(NULL);
4969 }
4970 if ((cur->type == XML_DOCUMENT_NODE) ||
4971 (cur->type == XML_HTML_DOCUMENT_NODE))
4972 return(NULL);
4973 return(cur->next);
4974}
4975
4976/**
4977 * xmlXPathNextDescendant:
4978 * @ctxt: the XPath Parser context
4979 * @cur: the current node in the traversal
4980 *
4981 * Traversal function for the "descendant" direction
4982 * the descendant axis contains the descendants of the context node in document
4983 * order; a descendant is a child or a child of a child and so on.
4984 *
4985 * Returns the next element following that axis
4986 */
4987xmlNodePtr
4988xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
4989 if (cur == NULL) {
4990 if (ctxt->context->node == NULL)
4991 return(NULL);
4992 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
4993 (ctxt->context->node->type == XML_NAMESPACE_DECL))
4994 return(NULL);
4995
4996 if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc)
4997 return(ctxt->context->doc->children);
4998 return(ctxt->context->node->children);
4999 }
5000
Daniel Veillard567e1b42001-08-01 15:53:47 +00005001 if (cur->children != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00005002 if (cur->children->type != XML_ENTITY_DECL)
Daniel Veillard567e1b42001-08-01 15:53:47 +00005003 return(cur->children);
5004 }
5005
5006 if (cur == ctxt->context->node) return(NULL);
5007
Owen Taylor3473f882001-02-23 17:55:21 +00005008 if (cur->next != NULL) return(cur->next);
5009
5010 do {
5011 cur = cur->parent;
5012 if (cur == NULL) return(NULL);
5013 if (cur == ctxt->context->node) return(NULL);
5014 if (cur->next != NULL) {
5015 cur = cur->next;
5016 return(cur);
5017 }
5018 } while (cur != NULL);
5019 return(cur);
5020}
5021
5022/**
5023 * xmlXPathNextDescendantOrSelf:
5024 * @ctxt: the XPath Parser context
5025 * @cur: the current node in the traversal
5026 *
5027 * Traversal function for the "descendant-or-self" direction
5028 * the descendant-or-self axis contains the context node and the descendants
5029 * of the context node in document order; thus the context node is the first
5030 * node on the axis, and the first child of the context node is the second node
5031 * on the axis
5032 *
5033 * Returns the next element following that axis
5034 */
5035xmlNodePtr
5036xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5037 if (cur == NULL) {
5038 if (ctxt->context->node == NULL)
5039 return(NULL);
5040 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5041 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5042 return(NULL);
5043 return(ctxt->context->node);
5044 }
5045
5046 return(xmlXPathNextDescendant(ctxt, cur));
5047}
5048
5049/**
5050 * xmlXPathNextParent:
5051 * @ctxt: the XPath Parser context
5052 * @cur: the current node in the traversal
5053 *
5054 * Traversal function for the "parent" direction
5055 * The parent axis contains the parent of the context node, if there is one.
5056 *
5057 * Returns the next element following that axis
5058 */
5059xmlNodePtr
5060xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5061 /*
5062 * the parent of an attribute or namespace node is the element
5063 * to which the attribute or namespace node is attached
5064 * Namespace handling !!!
5065 */
5066 if (cur == NULL) {
5067 if (ctxt->context->node == NULL) return(NULL);
5068 switch (ctxt->context->node->type) {
5069 case XML_ELEMENT_NODE:
5070 case XML_TEXT_NODE:
5071 case XML_CDATA_SECTION_NODE:
5072 case XML_ENTITY_REF_NODE:
5073 case XML_ENTITY_NODE:
5074 case XML_PI_NODE:
5075 case XML_COMMENT_NODE:
5076 case XML_NOTATION_NODE:
5077 case XML_DTD_NODE:
5078 case XML_ELEMENT_DECL:
5079 case XML_ATTRIBUTE_DECL:
5080 case XML_XINCLUDE_START:
5081 case XML_XINCLUDE_END:
5082 case XML_ENTITY_DECL:
5083 if (ctxt->context->node->parent == NULL)
5084 return((xmlNodePtr) ctxt->context->doc);
5085 return(ctxt->context->node->parent);
5086 case XML_ATTRIBUTE_NODE: {
5087 xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node;
5088
5089 return(att->parent);
5090 }
5091 case XML_DOCUMENT_NODE:
5092 case XML_DOCUMENT_TYPE_NODE:
5093 case XML_DOCUMENT_FRAG_NODE:
5094 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005095#ifdef LIBXML_DOCB_ENABLED
5096 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005097#endif
5098 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005099 case XML_NAMESPACE_DECL: {
5100 xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
5101
5102 if ((ns->next != NULL) &&
5103 (ns->next->type != XML_NAMESPACE_DECL))
5104 return((xmlNodePtr) ns->next);
Owen Taylor3473f882001-02-23 17:55:21 +00005105 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005106 }
Owen Taylor3473f882001-02-23 17:55:21 +00005107 }
5108 }
5109 return(NULL);
5110}
5111
5112/**
5113 * xmlXPathNextAncestor:
5114 * @ctxt: the XPath Parser context
5115 * @cur: the current node in the traversal
5116 *
5117 * Traversal function for the "ancestor" direction
5118 * the ancestor axis contains the ancestors of the context node; the ancestors
5119 * of the context node consist of the parent of context node and the parent's
5120 * parent and so on; the nodes are ordered in reverse document order; thus the
5121 * parent is the first node on the axis, and the parent's parent is the second
5122 * node on the axis
5123 *
5124 * Returns the next element following that axis
5125 */
5126xmlNodePtr
5127xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5128 /*
5129 * the parent of an attribute or namespace node is the element
5130 * to which the attribute or namespace node is attached
5131 * !!!!!!!!!!!!!
5132 */
5133 if (cur == NULL) {
5134 if (ctxt->context->node == NULL) return(NULL);
5135 switch (ctxt->context->node->type) {
5136 case XML_ELEMENT_NODE:
5137 case XML_TEXT_NODE:
5138 case XML_CDATA_SECTION_NODE:
5139 case XML_ENTITY_REF_NODE:
5140 case XML_ENTITY_NODE:
5141 case XML_PI_NODE:
5142 case XML_COMMENT_NODE:
5143 case XML_DTD_NODE:
5144 case XML_ELEMENT_DECL:
5145 case XML_ATTRIBUTE_DECL:
5146 case XML_ENTITY_DECL:
5147 case XML_NOTATION_NODE:
5148 case XML_XINCLUDE_START:
5149 case XML_XINCLUDE_END:
5150 if (ctxt->context->node->parent == NULL)
5151 return((xmlNodePtr) ctxt->context->doc);
5152 return(ctxt->context->node->parent);
5153 case XML_ATTRIBUTE_NODE: {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005154 xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node;
Owen Taylor3473f882001-02-23 17:55:21 +00005155
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005156 return(tmp->parent);
Owen Taylor3473f882001-02-23 17:55:21 +00005157 }
5158 case XML_DOCUMENT_NODE:
5159 case XML_DOCUMENT_TYPE_NODE:
5160 case XML_DOCUMENT_FRAG_NODE:
5161 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005162#ifdef LIBXML_DOCB_ENABLED
5163 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005164#endif
5165 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005166 case XML_NAMESPACE_DECL: {
5167 xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
5168
5169 if ((ns->next != NULL) &&
5170 (ns->next->type != XML_NAMESPACE_DECL))
5171 return((xmlNodePtr) ns->next);
5172 /* Bad, how did that namespace ended-up there ? */
Owen Taylor3473f882001-02-23 17:55:21 +00005173 return(NULL);
Daniel Veillard044fc6b2002-03-04 17:09:44 +00005174 }
Owen Taylor3473f882001-02-23 17:55:21 +00005175 }
5176 return(NULL);
5177 }
5178 if (cur == ctxt->context->doc->children)
5179 return((xmlNodePtr) ctxt->context->doc);
5180 if (cur == (xmlNodePtr) ctxt->context->doc)
5181 return(NULL);
5182 switch (cur->type) {
5183 case XML_ELEMENT_NODE:
5184 case XML_TEXT_NODE:
5185 case XML_CDATA_SECTION_NODE:
5186 case XML_ENTITY_REF_NODE:
5187 case XML_ENTITY_NODE:
5188 case XML_PI_NODE:
5189 case XML_COMMENT_NODE:
5190 case XML_NOTATION_NODE:
5191 case XML_DTD_NODE:
5192 case XML_ELEMENT_DECL:
5193 case XML_ATTRIBUTE_DECL:
5194 case XML_ENTITY_DECL:
5195 case XML_XINCLUDE_START:
5196 case XML_XINCLUDE_END:
5197 return(cur->parent);
5198 case XML_ATTRIBUTE_NODE: {
5199 xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node;
5200
5201 return(att->parent);
5202 }
Aleksey Sanindffd5c82002-05-31 04:24:13 +00005203 case XML_NAMESPACE_DECL: {
5204 xmlNsPtr ns = (xmlNsPtr) ctxt->context->node;
5205
5206 if ((ns->next != NULL) &&
5207 (ns->next->type != XML_NAMESPACE_DECL))
5208 return((xmlNodePtr) ns->next);
5209 /* Bad, how did that namespace ended-up there ? */
5210 return(NULL);
5211 }
Owen Taylor3473f882001-02-23 17:55:21 +00005212 case XML_DOCUMENT_NODE:
5213 case XML_DOCUMENT_TYPE_NODE:
5214 case XML_DOCUMENT_FRAG_NODE:
5215 case XML_HTML_DOCUMENT_NODE:
Daniel Veillardeae522a2001-04-23 13:41:34 +00005216#ifdef LIBXML_DOCB_ENABLED
5217 case XML_DOCB_DOCUMENT_NODE:
Owen Taylor3473f882001-02-23 17:55:21 +00005218#endif
5219 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005220 }
5221 return(NULL);
5222}
5223
5224/**
5225 * xmlXPathNextAncestorOrSelf:
5226 * @ctxt: the XPath Parser context
5227 * @cur: the current node in the traversal
5228 *
5229 * Traversal function for the "ancestor-or-self" direction
5230 * he ancestor-or-self axis contains the context node and ancestors of
5231 * the context node in reverse document order; thus the context node is
5232 * the first node on the axis, and the context node's parent the second;
5233 * parent here is defined the same as with the parent axis.
5234 *
5235 * Returns the next element following that axis
5236 */
5237xmlNodePtr
5238xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5239 if (cur == NULL)
5240 return(ctxt->context->node);
5241 return(xmlXPathNextAncestor(ctxt, cur));
5242}
5243
5244/**
5245 * xmlXPathNextFollowingSibling:
5246 * @ctxt: the XPath Parser context
5247 * @cur: the current node in the traversal
5248 *
5249 * Traversal function for the "following-sibling" direction
5250 * The following-sibling axis contains the following siblings of the context
5251 * node in document order.
5252 *
5253 * Returns the next element following that axis
5254 */
5255xmlNodePtr
5256xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5257 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5258 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5259 return(NULL);
5260 if (cur == (xmlNodePtr) ctxt->context->doc)
5261 return(NULL);
5262 if (cur == NULL)
5263 return(ctxt->context->node->next);
5264 return(cur->next);
5265}
5266
5267/**
5268 * xmlXPathNextPrecedingSibling:
5269 * @ctxt: the XPath Parser context
5270 * @cur: the current node in the traversal
5271 *
5272 * Traversal function for the "preceding-sibling" direction
5273 * The preceding-sibling axis contains the preceding siblings of the context
5274 * node in reverse document order; the first preceding sibling is first on the
5275 * axis; the sibling preceding that node is the second on the axis and so on.
5276 *
5277 * Returns the next element following that axis
5278 */
5279xmlNodePtr
5280xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5281 if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) ||
5282 (ctxt->context->node->type == XML_NAMESPACE_DECL))
5283 return(NULL);
5284 if (cur == (xmlNodePtr) ctxt->context->doc)
5285 return(NULL);
5286 if (cur == NULL)
5287 return(ctxt->context->node->prev);
Daniel Veillardf06307e2001-07-03 10:35:50 +00005288 if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) {
5289 cur = cur->prev;
5290 if (cur == NULL)
5291 return(ctxt->context->node->prev);
5292 }
Owen Taylor3473f882001-02-23 17:55:21 +00005293 return(cur->prev);
5294}
5295
5296/**
5297 * xmlXPathNextFollowing:
5298 * @ctxt: the XPath Parser context
5299 * @cur: the current node in the traversal
5300 *
5301 * Traversal function for the "following" direction
5302 * The following axis contains all nodes in the same document as the context
5303 * node that are after the context node in document order, excluding any
5304 * descendants and excluding attribute nodes and namespace nodes; the nodes
5305 * are ordered in document order
5306 *
5307 * Returns the next element following that axis
5308 */
5309xmlNodePtr
5310xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
5311 if (cur != NULL && cur->children != NULL)
5312 return cur->children ;
5313 if (cur == NULL) cur = ctxt->context->node;
5314 if (cur == NULL) return(NULL) ; /* ERROR */
5315 if (cur->next != NULL) return(cur->next) ;
5316 do {
5317 cur = cur->parent;
5318 if (cur == NULL) return(NULL);
5319 if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL);
5320 if (cur->next != NULL) return(cur->next);
5321 } while (cur != NULL);
5322 return(cur);
5323}
5324
5325/*
5326 * xmlXPathIsAncestor:
5327 * @ancestor: the ancestor node
5328 * @node: the current node
5329 *
5330 * Check that @ancestor is a @node's ancestor
5331 *
5332 * returns 1 if @ancestor is a @node's ancestor, 0 otherwise.
5333 */
5334static int
5335xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) {
5336 if ((ancestor == NULL) || (node == NULL)) return(0);
5337 /* nodes need to be in the same document */
5338 if (ancestor->doc != node->doc) return(0);
5339 /* avoid searching if ancestor or node is the root node */
5340 if (ancestor == (xmlNodePtr) node->doc) return(1);
5341 if (node == (xmlNodePtr) ancestor->doc) return(0);
5342 while (node->parent != NULL) {
5343 if (node->parent == ancestor)
5344 return(1);
5345 node = node->parent;
5346 }
5347 return(0);
5348}
5349
5350/**
5351 * xmlXPathNextPreceding:
5352 * @ctxt: the XPath Parser context
5353 * @cur: the current node in the traversal
5354 *
5355 * Traversal function for the "preceding" direction
5356 * the preceding axis contains all nodes in the same document as the context
5357 * node that are before the context node in document order, excluding any
5358 * ancestors and excluding attribute nodes and namespace nodes; the nodes are
5359 * ordered in reverse document order
5360 *
5361 * Returns the next element following that axis
5362 */
5363xmlNodePtr
Daniel Veillardf06307e2001-07-03 10:35:50 +00005364xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur)
5365{
Owen Taylor3473f882001-02-23 17:55:21 +00005366 if (cur == NULL)
Daniel Veillardf06307e2001-07-03 10:35:50 +00005367 cur = ctxt->context->node;
5368 if (cur == NULL)
5369 return (NULL);
5370 if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
5371 cur = cur->prev;
Owen Taylor3473f882001-02-23 17:55:21 +00005372 do {
5373 if (cur->prev != NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00005374 for (cur = cur->prev; cur->last != NULL; cur = cur->last) ;
5375 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00005376 }
5377
5378 cur = cur->parent;
Daniel Veillardf06307e2001-07-03 10:35:50 +00005379 if (cur == NULL)
5380 return (NULL);
5381 if (cur == ctxt->context->doc->children)
5382 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005383 } while (xmlXPathIsAncestor(cur, ctxt->context->node));
Daniel Veillardf06307e2001-07-03 10:35:50 +00005384 return (cur);
5385}
5386
5387/**
5388 * xmlXPathNextPrecedingInternal:
5389 * @ctxt: the XPath Parser context
5390 * @cur: the current node in the traversal
5391 *
5392 * Traversal function for the "preceding" direction
5393 * the preceding axis contains all nodes in the same document as the context
5394 * node that are before the context node in document order, excluding any
5395 * ancestors and excluding attribute nodes and namespace nodes; the nodes are
5396 * ordered in reverse document order
5397 * This is a faster implementation but internal only since it requires a
5398 * state kept in the parser context: ctxt->ancestor.
5399 *
5400 * Returns the next element following that axis
5401 */
5402static xmlNodePtr
5403xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt,
5404 xmlNodePtr cur)
5405{
5406 if (cur == NULL) {
5407 cur = ctxt->context->node;
5408 if (cur == NULL)
5409 return (NULL);
5410 ctxt->ancestor = cur->parent;
5411 }
5412 if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
5413 cur = cur->prev;
5414 while (cur->prev == NULL) {
5415 cur = cur->parent;
5416 if (cur == NULL)
5417 return (NULL);
5418 if (cur == ctxt->context->doc->children)
5419 return (NULL);
5420 if (cur != ctxt->ancestor)
5421 return (cur);
5422 ctxt->ancestor = cur->parent;
5423 }
5424 cur = cur->prev;
5425 while (cur->last != NULL)
5426 cur = cur->last;
5427 return (cur);
Owen Taylor3473f882001-02-23 17:55:21 +00005428}
5429
5430/**
5431 * xmlXPathNextNamespace:
5432 * @ctxt: the XPath Parser context
5433 * @cur: the current attribute in the traversal
5434 *
5435 * Traversal function for the "namespace" direction
5436 * the namespace axis contains the namespace nodes of the context node;
5437 * the order of nodes on this axis is implementation-defined; the axis will
5438 * be empty unless the context node is an element
5439 *
Daniel Veillard20ee8c02001-10-05 09:18:14 +00005440 * We keep the XML namespace node at the end of the list.
5441 *
Owen Taylor3473f882001-02-23 17:55:21 +00005442 * Returns the next element following that axis
5443 */
5444xmlNodePtr
5445xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005446 xmlNodePtr ret;
5447
Owen Taylor3473f882001-02-23 17:55:21 +00005448 if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL);
Daniel Veillard20ee8c02001-10-05 09:18:14 +00005449 if (cur == (xmlNodePtr) xmlXPathXMLNamespace)
5450 return(NULL);
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005451 if ((cur == NULL) || (ctxt->context->tmpNsList == NULL)) {
5452 if (ctxt->context->tmpNsList != NULL)
5453 xmlFree(ctxt->context->tmpNsList);
5454 ctxt->context->tmpNsList =
Owen Taylor3473f882001-02-23 17:55:21 +00005455 xmlGetNsList(ctxt->context->doc, ctxt->context->node);
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005456 if (ctxt->context->tmpNsList == NULL) return(NULL);
5457 ctxt->context->tmpNsNr = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005458 }
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005459 ret = (xmlNodePtr)ctxt->context->tmpNsList[ctxt->context->tmpNsNr++];
5460 if (ret == NULL) {
5461 xmlFree(ctxt->context->tmpNsList);
5462 ctxt->context->tmpNsList = NULL;
Daniel Veillard20ee8c02001-10-05 09:18:14 +00005463 return((xmlNodePtr) xmlXPathXMLNamespace);
Daniel Veillard7d7e3792001-07-30 13:42:13 +00005464 }
5465 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00005466}
5467
5468/**
5469 * xmlXPathNextAttribute:
5470 * @ctxt: the XPath Parser context
5471 * @cur: the current attribute in the traversal
5472 *
5473 * Traversal function for the "attribute" direction
5474 * TODO: support DTD inherited default attributes
5475 *
5476 * Returns the next element following that axis
5477 */
5478xmlNodePtr
5479xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) {
Daniel Veillarde470df72001-04-18 21:41:07 +00005480 if (ctxt->context->node == NULL)
5481 return(NULL);
5482 if (ctxt->context->node->type != XML_ELEMENT_NODE)
5483 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005484 if (cur == NULL) {
5485 if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc)
5486 return(NULL);
5487 return((xmlNodePtr)ctxt->context->node->properties);
5488 }
5489 return((xmlNodePtr)cur->next);
5490}
5491
5492/************************************************************************
5493 * *
5494 * NodeTest Functions *
5495 * *
5496 ************************************************************************/
5497
Owen Taylor3473f882001-02-23 17:55:21 +00005498#define IS_FUNCTION 200
5499
Owen Taylor3473f882001-02-23 17:55:21 +00005500
5501/************************************************************************
5502 * *
5503 * Implicit tree core function library *
5504 * *
5505 ************************************************************************/
5506
5507/**
5508 * xmlXPathRoot:
5509 * @ctxt: the XPath Parser context
5510 *
5511 * Initialize the context to the root of the document
5512 */
5513void
5514xmlXPathRoot(xmlXPathParserContextPtr ctxt) {
5515 ctxt->context->node = (xmlNodePtr) ctxt->context->doc;
5516 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
5517}
5518
5519/************************************************************************
5520 * *
5521 * The explicit core function library *
5522 *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib *
5523 * *
5524 ************************************************************************/
5525
5526
5527/**
5528 * xmlXPathLastFunction:
5529 * @ctxt: the XPath Parser context
5530 * @nargs: the number of arguments
5531 *
5532 * Implement the last() XPath function
5533 * number last()
5534 * The last function returns the number of nodes in the context node list.
5535 */
5536void
5537xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) {
5538 CHECK_ARITY(0);
5539 if (ctxt->context->contextSize >= 0) {
5540 valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize));
5541#ifdef DEBUG_EXPR
5542 xmlGenericError(xmlGenericErrorContext,
5543 "last() : %d\n", ctxt->context->contextSize);
5544#endif
5545 } else {
5546 XP_ERROR(XPATH_INVALID_CTXT_SIZE);
5547 }
5548}
5549
5550/**
5551 * xmlXPathPositionFunction:
5552 * @ctxt: the XPath Parser context
5553 * @nargs: the number of arguments
5554 *
5555 * Implement the position() XPath function
5556 * number position()
5557 * The position function returns the position of the context node in the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005558 * context node list. The first position is 1, and so the last position
Owen Taylor3473f882001-02-23 17:55:21 +00005559 * will be equal to last().
5560 */
5561void
5562xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) {
5563 CHECK_ARITY(0);
5564 if (ctxt->context->proximityPosition >= 0) {
5565 valuePush(ctxt,
5566 xmlXPathNewFloat((double) ctxt->context->proximityPosition));
5567#ifdef DEBUG_EXPR
5568 xmlGenericError(xmlGenericErrorContext, "position() : %d\n",
5569 ctxt->context->proximityPosition);
5570#endif
5571 } else {
5572 XP_ERROR(XPATH_INVALID_CTXT_POSITION);
5573 }
5574}
5575
5576/**
5577 * xmlXPathCountFunction:
5578 * @ctxt: the XPath Parser context
5579 * @nargs: the number of arguments
5580 *
5581 * Implement the count() XPath function
5582 * number count(node-set)
5583 */
5584void
5585xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) {
5586 xmlXPathObjectPtr cur;
5587
5588 CHECK_ARITY(1);
5589 if ((ctxt->value == NULL) ||
5590 ((ctxt->value->type != XPATH_NODESET) &&
5591 (ctxt->value->type != XPATH_XSLT_TREE)))
5592 XP_ERROR(XPATH_INVALID_TYPE);
5593 cur = valuePop(ctxt);
5594
Daniel Veillard911f49a2001-04-07 15:39:35 +00005595 if ((cur == NULL) || (cur->nodesetval == NULL))
5596 valuePush(ctxt, xmlXPathNewFloat((double) 0));
Daniel Veillardfe703322001-08-14 12:18:09 +00005597 else if (cur->type == XPATH_NODESET) {
Daniel Veillard911f49a2001-04-07 15:39:35 +00005598 valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr));
Daniel Veillardfe703322001-08-14 12:18:09 +00005599 } else {
5600 if ((cur->nodesetval->nodeNr != 1) ||
5601 (cur->nodesetval->nodeTab == NULL)) {
5602 valuePush(ctxt, xmlXPathNewFloat((double) 0));
5603 } else {
5604 xmlNodePtr tmp;
5605 int i = 0;
5606
5607 tmp = cur->nodesetval->nodeTab[0];
5608 if (tmp != NULL) {
5609 tmp = tmp->children;
5610 while (tmp != NULL) {
5611 tmp = tmp->next;
5612 i++;
5613 }
5614 }
5615 valuePush(ctxt, xmlXPathNewFloat((double) i));
5616 }
5617 }
Owen Taylor3473f882001-02-23 17:55:21 +00005618 xmlXPathFreeObject(cur);
5619}
5620
5621/**
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005622 * xmlXPathGetElementsByIds:
5623 * @doc: the document
5624 * @ids: a whitespace separated list of IDs
5625 *
5626 * Selects elements by their unique ID.
5627 *
5628 * Returns a node-set of selected elements.
5629 */
5630static xmlNodeSetPtr
5631xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) {
5632 xmlNodeSetPtr ret;
5633 const xmlChar *cur = ids;
5634 xmlChar *ID;
5635 xmlAttrPtr attr;
5636 xmlNodePtr elem = NULL;
5637
5638 ret = xmlXPathNodeSetCreate(NULL);
5639
5640 while (IS_BLANK(*cur)) cur++;
5641 while (*cur != 0) {
5642 while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) ||
5643 (*cur == '.') || (*cur == '-') ||
5644 (*cur == '_') || (*cur == ':') ||
5645 (IS_COMBINING(*cur)) ||
5646 (IS_EXTENDER(*cur)))
5647 cur++;
5648
5649 if ((!IS_BLANK(*cur)) && (*cur != 0)) break;
5650
5651 ID = xmlStrndup(ids, cur - ids);
5652 attr = xmlGetID(doc, ID);
5653 if (attr != NULL) {
5654 elem = attr->parent;
5655 xmlXPathNodeSetAdd(ret, elem);
5656 }
5657 if (ID != NULL)
5658 xmlFree(ID);
5659
5660 while (IS_BLANK(*cur)) cur++;
5661 ids = cur;
5662 }
5663 return(ret);
5664}
5665
5666/**
Owen Taylor3473f882001-02-23 17:55:21 +00005667 * xmlXPathIdFunction:
5668 * @ctxt: the XPath Parser context
5669 * @nargs: the number of arguments
5670 *
5671 * Implement the id() XPath function
5672 * node-set id(object)
5673 * The id function selects elements by their unique ID
5674 * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set,
5675 * then the result is the union of the result of applying id to the
5676 * string value of each of the nodes in the argument node-set. When the
5677 * argument to id is of any other type, the argument is converted to a
5678 * string as if by a call to the string function; the string is split
5679 * into a whitespace-separated list of tokens (whitespace is any sequence
5680 * of characters matching the production S); the result is a node-set
5681 * containing the elements in the same document as the context node that
5682 * have a unique ID equal to any of the tokens in the list.
5683 */
5684void
5685xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005686 xmlChar *tokens;
5687 xmlNodeSetPtr ret;
5688 xmlXPathObjectPtr obj;
Owen Taylor3473f882001-02-23 17:55:21 +00005689
5690 CHECK_ARITY(1);
5691 obj = valuePop(ctxt);
5692 if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
5693 if (obj->type == XPATH_NODESET) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005694 xmlNodeSetPtr ns;
Owen Taylor3473f882001-02-23 17:55:21 +00005695 int i;
5696
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005697 ret = xmlXPathNodeSetCreate(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005698
Daniel Veillard911f49a2001-04-07 15:39:35 +00005699 if (obj->nodesetval != NULL) {
5700 for (i = 0; i < obj->nodesetval->nodeNr; i++) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005701 tokens =
5702 xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]);
5703 ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens);
5704 ret = xmlXPathNodeSetMerge(ret, ns);
5705 xmlXPathFreeNodeSet(ns);
5706 if (tokens != NULL)
5707 xmlFree(tokens);
Daniel Veillard911f49a2001-04-07 15:39:35 +00005708 }
Owen Taylor3473f882001-02-23 17:55:21 +00005709 }
5710
5711 xmlXPathFreeObject(obj);
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005712 valuePush(ctxt, xmlXPathWrapNodeSet(ret));
Owen Taylor3473f882001-02-23 17:55:21 +00005713 return;
5714 }
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005715 obj = xmlXPathConvertString(obj);
Owen Taylor3473f882001-02-23 17:55:21 +00005716
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005717 ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval);
5718 valuePush(ctxt, xmlXPathWrapNodeSet(ret));
Owen Taylor3473f882001-02-23 17:55:21 +00005719
Owen Taylor3473f882001-02-23 17:55:21 +00005720 xmlXPathFreeObject(obj);
5721 return;
5722}
5723
5724/**
5725 * xmlXPathLocalNameFunction:
5726 * @ctxt: the XPath Parser context
5727 * @nargs: the number of arguments
5728 *
5729 * Implement the local-name() XPath function
5730 * string local-name(node-set?)
5731 * The local-name function returns a string containing the local part
5732 * of the name of the node in the argument node-set that is first in
5733 * document order. If the node-set is empty or the first node has no
5734 * name, an empty string is returned. If the argument is omitted it
5735 * defaults to the context node.
5736 */
5737void
5738xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) {
5739 xmlXPathObjectPtr cur;
5740
5741 if (nargs == 0) {
5742 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
5743 nargs = 1;
5744 }
5745
5746 CHECK_ARITY(1);
5747 if ((ctxt->value == NULL) ||
5748 ((ctxt->value->type != XPATH_NODESET) &&
5749 (ctxt->value->type != XPATH_XSLT_TREE)))
5750 XP_ERROR(XPATH_INVALID_TYPE);
5751 cur = valuePop(ctxt);
5752
Daniel Veillard911f49a2001-04-07 15:39:35 +00005753 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005754 valuePush(ctxt, xmlXPathNewCString(""));
5755 } else {
5756 int i = 0; /* Should be first in document order !!!!! */
5757 switch (cur->nodesetval->nodeTab[i]->type) {
5758 case XML_ELEMENT_NODE:
5759 case XML_ATTRIBUTE_NODE:
5760 case XML_PI_NODE:
5761 valuePush(ctxt,
5762 xmlXPathNewString(cur->nodesetval->nodeTab[i]->name));
5763 break;
5764 case XML_NAMESPACE_DECL:
5765 valuePush(ctxt, xmlXPathNewString(
5766 ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix));
5767 break;
5768 default:
5769 valuePush(ctxt, xmlXPathNewCString(""));
5770 }
5771 }
5772 xmlXPathFreeObject(cur);
5773}
5774
5775/**
5776 * xmlXPathNamespaceURIFunction:
5777 * @ctxt: the XPath Parser context
5778 * @nargs: the number of arguments
5779 *
5780 * Implement the namespace-uri() XPath function
5781 * string namespace-uri(node-set?)
5782 * The namespace-uri function returns a string containing the
5783 * namespace URI of the expanded name of the node in the argument
5784 * node-set that is first in document order. If the node-set is empty,
5785 * the first node has no name, or the expanded name has no namespace
5786 * URI, an empty string is returned. If the argument is omitted it
5787 * defaults to the context node.
5788 */
5789void
5790xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) {
5791 xmlXPathObjectPtr cur;
5792
5793 if (nargs == 0) {
5794 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
5795 nargs = 1;
5796 }
5797 CHECK_ARITY(1);
5798 if ((ctxt->value == NULL) ||
5799 ((ctxt->value->type != XPATH_NODESET) &&
5800 (ctxt->value->type != XPATH_XSLT_TREE)))
5801 XP_ERROR(XPATH_INVALID_TYPE);
5802 cur = valuePop(ctxt);
5803
Daniel Veillard911f49a2001-04-07 15:39:35 +00005804 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005805 valuePush(ctxt, xmlXPathNewCString(""));
5806 } else {
5807 int i = 0; /* Should be first in document order !!!!! */
5808 switch (cur->nodesetval->nodeTab[i]->type) {
5809 case XML_ELEMENT_NODE:
5810 case XML_ATTRIBUTE_NODE:
5811 if (cur->nodesetval->nodeTab[i]->ns == NULL)
5812 valuePush(ctxt, xmlXPathNewCString(""));
5813 else
5814 valuePush(ctxt, xmlXPathNewString(
5815 cur->nodesetval->nodeTab[i]->ns->href));
5816 break;
5817 default:
5818 valuePush(ctxt, xmlXPathNewCString(""));
5819 }
5820 }
5821 xmlXPathFreeObject(cur);
5822}
5823
5824/**
5825 * xmlXPathNameFunction:
5826 * @ctxt: the XPath Parser context
5827 * @nargs: the number of arguments
5828 *
5829 * Implement the name() XPath function
5830 * string name(node-set?)
5831 * The name function returns a string containing a QName representing
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005832 * the name of the node in the argument node-set that is first in document
Owen Taylor3473f882001-02-23 17:55:21 +00005833 * order. The QName must represent the name with respect to the namespace
5834 * declarations in effect on the node whose name is being represented.
5835 * Typically, this will be the form in which the name occurred in the XML
5836 * source. This need not be the case if there are namespace declarations
5837 * in effect on the node that associate multiple prefixes with the same
5838 * namespace. However, an implementation may include information about
5839 * the original prefix in its representation of nodes; in this case, an
5840 * implementation can ensure that the returned string is always the same
5841 * as the QName used in the XML source. If the argument it omitted it
5842 * defaults to the context node.
5843 * Libxml keep the original prefix so the "real qualified name" used is
5844 * returned.
5845 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005846static void
Daniel Veillard04383752001-07-08 14:27:15 +00005847xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs)
5848{
Owen Taylor3473f882001-02-23 17:55:21 +00005849 xmlXPathObjectPtr cur;
5850
5851 if (nargs == 0) {
Daniel Veillard04383752001-07-08 14:27:15 +00005852 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
5853 nargs = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005854 }
5855
5856 CHECK_ARITY(1);
Daniel Veillard04383752001-07-08 14:27:15 +00005857 if ((ctxt->value == NULL) ||
5858 ((ctxt->value->type != XPATH_NODESET) &&
5859 (ctxt->value->type != XPATH_XSLT_TREE)))
5860 XP_ERROR(XPATH_INVALID_TYPE);
Owen Taylor3473f882001-02-23 17:55:21 +00005861 cur = valuePop(ctxt);
5862
Daniel Veillard911f49a2001-04-07 15:39:35 +00005863 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Daniel Veillard04383752001-07-08 14:27:15 +00005864 valuePush(ctxt, xmlXPathNewCString(""));
Owen Taylor3473f882001-02-23 17:55:21 +00005865 } else {
Daniel Veillard04383752001-07-08 14:27:15 +00005866 int i = 0; /* Should be first in document order !!!!! */
Owen Taylor3473f882001-02-23 17:55:21 +00005867
Daniel Veillard04383752001-07-08 14:27:15 +00005868 switch (cur->nodesetval->nodeTab[i]->type) {
5869 case XML_ELEMENT_NODE:
5870 case XML_ATTRIBUTE_NODE:
5871 if ((cur->nodesetval->nodeTab[i]->ns == NULL) ||
5872 (cur->nodesetval->nodeTab[i]->ns->prefix == NULL))
5873 valuePush(ctxt,
5874 xmlXPathNewString(cur->nodesetval->
5875 nodeTab[i]->name));
5876
5877 else {
5878 char name[2000];
5879
5880 snprintf(name, sizeof(name), "%s:%s",
5881 (char *) cur->nodesetval->nodeTab[i]->ns->
5882 prefix,
5883 (char *) cur->nodesetval->nodeTab[i]->name);
5884 name[sizeof(name) - 1] = 0;
5885 valuePush(ctxt, xmlXPathNewCString(name));
5886 }
5887 break;
5888 default:
5889 valuePush(ctxt,
5890 xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i]));
5891 xmlXPathLocalNameFunction(ctxt, 1);
5892 }
Owen Taylor3473f882001-02-23 17:55:21 +00005893 }
5894 xmlXPathFreeObject(cur);
5895}
5896
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00005897
5898/**
Owen Taylor3473f882001-02-23 17:55:21 +00005899 * xmlXPathStringFunction:
5900 * @ctxt: the XPath Parser context
5901 * @nargs: the number of arguments
5902 *
5903 * Implement the string() XPath function
5904 * string string(object?)
5905 * he string function converts an object to a string as follows:
5906 * - A node-set is converted to a string by returning the value of
5907 * the node in the node-set that is first in document order.
5908 * If the node-set is empty, an empty string is returned.
5909 * - A number is converted to a string as follows
5910 * + NaN is converted to the string NaN
5911 * + positive zero is converted to the string 0
5912 * + negative zero is converted to the string 0
5913 * + positive infinity is converted to the string Infinity
5914 * + negative infinity is converted to the string -Infinity
5915 * + if the number is an integer, the number is represented in
5916 * decimal form as a Number with no decimal point and no leading
5917 * zeros, preceded by a minus sign (-) if the number is negative
5918 * + otherwise, the number is represented in decimal form as a
5919 * Number including a decimal point with at least one digit
5920 * before the decimal point and at least one digit after the
5921 * decimal point, preceded by a minus sign (-) if the number
5922 * is negative; there must be no leading zeros before the decimal
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005923 * point apart possibly from the one required digit immediately
Owen Taylor3473f882001-02-23 17:55:21 +00005924 * before the decimal point; beyond the one required digit
5925 * after the decimal point there must be as many, but only as
5926 * many, more digits as are needed to uniquely distinguish the
5927 * number from all other IEEE 754 numeric values.
5928 * - The boolean false value is converted to the string false.
5929 * The boolean true value is converted to the string true.
5930 *
5931 * If the argument is omitted, it defaults to a node-set with the
5932 * context node as its only member.
5933 */
5934void
5935xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) {
5936 xmlXPathObjectPtr cur;
5937
5938 if (nargs == 0) {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005939 valuePush(ctxt,
5940 xmlXPathWrapString(
5941 xmlXPathCastNodeToString(ctxt->context->node)));
5942 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005943 }
5944
5945 CHECK_ARITY(1);
5946 cur = valuePop(ctxt);
5947 if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00005948 cur = xmlXPathConvertString(cur);
5949 valuePush(ctxt, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00005950}
5951
5952/**
5953 * xmlXPathStringLengthFunction:
5954 * @ctxt: the XPath Parser context
5955 * @nargs: the number of arguments
5956 *
5957 * Implement the string-length() XPath function
5958 * number string-length(string?)
5959 * The string-length returns the number of characters in the string
5960 * (see [3.6 Strings]). If the argument is omitted, it defaults to
5961 * the context node converted to a string, in other words the value
5962 * of the context node.
5963 */
5964void
5965xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) {
5966 xmlXPathObjectPtr cur;
5967
5968 if (nargs == 0) {
5969 if (ctxt->context->node == NULL) {
5970 valuePush(ctxt, xmlXPathNewFloat(0));
5971 } else {
5972 xmlChar *content;
5973
Daniel Veillardba0b8c92001-05-15 09:43:47 +00005974 content = xmlXPathCastNodeToString(ctxt->context->node);
Daniel Veillarde043ee12001-04-16 14:08:07 +00005975 valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content)));
Owen Taylor3473f882001-02-23 17:55:21 +00005976 xmlFree(content);
5977 }
5978 return;
5979 }
5980 CHECK_ARITY(1);
5981 CAST_TO_STRING;
5982 CHECK_TYPE(XPATH_STRING);
5983 cur = valuePop(ctxt);
Daniel Veillarde043ee12001-04-16 14:08:07 +00005984 valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(cur->stringval)));
Owen Taylor3473f882001-02-23 17:55:21 +00005985 xmlXPathFreeObject(cur);
5986}
5987
5988/**
5989 * xmlXPathConcatFunction:
5990 * @ctxt: the XPath Parser context
5991 * @nargs: the number of arguments
5992 *
5993 * Implement the concat() XPath function
5994 * string concat(string, string, string*)
5995 * The concat function returns the concatenation of its arguments.
5996 */
5997void
5998xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) {
5999 xmlXPathObjectPtr cur, newobj;
6000 xmlChar *tmp;
6001
6002 if (nargs < 2) {
6003 CHECK_ARITY(2);
6004 }
6005
6006 CAST_TO_STRING;
6007 cur = valuePop(ctxt);
6008 if ((cur == NULL) || (cur->type != XPATH_STRING)) {
6009 xmlXPathFreeObject(cur);
6010 return;
6011 }
6012 nargs--;
6013
6014 while (nargs > 0) {
6015 CAST_TO_STRING;
6016 newobj = valuePop(ctxt);
6017 if ((newobj == NULL) || (newobj->type != XPATH_STRING)) {
6018 xmlXPathFreeObject(newobj);
6019 xmlXPathFreeObject(cur);
6020 XP_ERROR(XPATH_INVALID_TYPE);
6021 }
6022 tmp = xmlStrcat(newobj->stringval, cur->stringval);
6023 newobj->stringval = cur->stringval;
6024 cur->stringval = tmp;
6025
6026 xmlXPathFreeObject(newobj);
6027 nargs--;
6028 }
6029 valuePush(ctxt, cur);
6030}
6031
6032/**
6033 * xmlXPathContainsFunction:
6034 * @ctxt: the XPath Parser context
6035 * @nargs: the number of arguments
6036 *
6037 * Implement the contains() XPath function
6038 * boolean contains(string, string)
6039 * The contains function returns true if the first argument string
6040 * contains the second argument string, and otherwise returns false.
6041 */
6042void
6043xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6044 xmlXPathObjectPtr hay, needle;
6045
6046 CHECK_ARITY(2);
6047 CAST_TO_STRING;
6048 CHECK_TYPE(XPATH_STRING);
6049 needle = valuePop(ctxt);
6050 CAST_TO_STRING;
6051 hay = valuePop(ctxt);
6052 if ((hay == NULL) || (hay->type != XPATH_STRING)) {
6053 xmlXPathFreeObject(hay);
6054 xmlXPathFreeObject(needle);
6055 XP_ERROR(XPATH_INVALID_TYPE);
6056 }
6057 if (xmlStrstr(hay->stringval, needle->stringval))
6058 valuePush(ctxt, xmlXPathNewBoolean(1));
6059 else
6060 valuePush(ctxt, xmlXPathNewBoolean(0));
6061 xmlXPathFreeObject(hay);
6062 xmlXPathFreeObject(needle);
6063}
6064
6065/**
6066 * xmlXPathStartsWithFunction:
6067 * @ctxt: the XPath Parser context
6068 * @nargs: the number of arguments
6069 *
6070 * Implement the starts-with() XPath function
6071 * boolean starts-with(string, string)
6072 * The starts-with function returns true if the first argument string
6073 * starts with the second argument string, and otherwise returns false.
6074 */
6075void
6076xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6077 xmlXPathObjectPtr hay, needle;
6078 int n;
6079
6080 CHECK_ARITY(2);
6081 CAST_TO_STRING;
6082 CHECK_TYPE(XPATH_STRING);
6083 needle = valuePop(ctxt);
6084 CAST_TO_STRING;
6085 hay = valuePop(ctxt);
6086 if ((hay == NULL) || (hay->type != XPATH_STRING)) {
6087 xmlXPathFreeObject(hay);
6088 xmlXPathFreeObject(needle);
6089 XP_ERROR(XPATH_INVALID_TYPE);
6090 }
6091 n = xmlStrlen(needle->stringval);
6092 if (xmlStrncmp(hay->stringval, needle->stringval, n))
6093 valuePush(ctxt, xmlXPathNewBoolean(0));
6094 else
6095 valuePush(ctxt, xmlXPathNewBoolean(1));
6096 xmlXPathFreeObject(hay);
6097 xmlXPathFreeObject(needle);
6098}
6099
6100/**
6101 * xmlXPathSubstringFunction:
6102 * @ctxt: the XPath Parser context
6103 * @nargs: the number of arguments
6104 *
6105 * Implement the substring() XPath function
6106 * string substring(string, number, number?)
6107 * The substring function returns the substring of the first argument
6108 * starting at the position specified in the second argument with
6109 * length specified in the third argument. For example,
6110 * substring("12345",2,3) returns "234". If the third argument is not
6111 * specified, it returns the substring starting at the position specified
6112 * in the second argument and continuing to the end of the string. For
6113 * example, substring("12345",2) returns "2345". More precisely, each
6114 * character in the string (see [3.6 Strings]) is considered to have a
6115 * numeric position: the position of the first character is 1, the position
6116 * of the second character is 2 and so on. The returned substring contains
6117 * those characters for which the position of the character is greater than
6118 * or equal to the second argument and, if the third argument is specified,
6119 * less than the sum of the second and third arguments; the comparisons
6120 * and addition used for the above follow the standard IEEE 754 rules. Thus:
6121 * - substring("12345", 1.5, 2.6) returns "234"
6122 * - substring("12345", 0, 3) returns "12"
6123 * - substring("12345", 0 div 0, 3) returns ""
6124 * - substring("12345", 1, 0 div 0) returns ""
6125 * - substring("12345", -42, 1 div 0) returns "12345"
6126 * - substring("12345", -1 div 0, 1 div 0) returns ""
6127 */
6128void
6129xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6130 xmlXPathObjectPtr str, start, len;
Daniel Veillard97ac1312001-05-30 19:14:17 +00006131 double le=0, in;
6132 int i, l, m;
Owen Taylor3473f882001-02-23 17:55:21 +00006133 xmlChar *ret;
6134
Owen Taylor3473f882001-02-23 17:55:21 +00006135 if (nargs < 2) {
6136 CHECK_ARITY(2);
6137 }
6138 if (nargs > 3) {
6139 CHECK_ARITY(3);
6140 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006141 /*
6142 * take care of possible last (position) argument
6143 */
Owen Taylor3473f882001-02-23 17:55:21 +00006144 if (nargs == 3) {
6145 CAST_TO_NUMBER;
6146 CHECK_TYPE(XPATH_NUMBER);
6147 len = valuePop(ctxt);
6148 le = len->floatval;
6149 xmlXPathFreeObject(len);
Owen Taylor3473f882001-02-23 17:55:21 +00006150 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006151
Owen Taylor3473f882001-02-23 17:55:21 +00006152 CAST_TO_NUMBER;
6153 CHECK_TYPE(XPATH_NUMBER);
6154 start = valuePop(ctxt);
6155 in = start->floatval;
6156 xmlXPathFreeObject(start);
6157 CAST_TO_STRING;
6158 CHECK_TYPE(XPATH_STRING);
6159 str = valuePop(ctxt);
Daniel Veillard97ac1312001-05-30 19:14:17 +00006160 m = xmlUTF8Strlen((const unsigned char *)str->stringval);
Owen Taylor3473f882001-02-23 17:55:21 +00006161
Daniel Veillard97ac1312001-05-30 19:14:17 +00006162 /*
6163 * If last pos not present, calculate last position
6164 */
Daniel Veillard9e412302002-06-10 15:59:44 +00006165 if (nargs != 3) {
6166 le = (double)m;
6167 if (in < 1.0)
6168 in = 1.0;
6169 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006170
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006171 /* Need to check for the special cases where either
6172 * the index is NaN, the length is NaN, or both
6173 * arguments are infinity (relying on Inf + -Inf = NaN)
Daniel Veillard97ac1312001-05-30 19:14:17 +00006174 */
Daniel Veillard9e412302002-06-10 15:59:44 +00006175 if (!xmlXPathIsNaN(in + le) && !xmlXPathIsInf(in)) {
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006176 /*
Daniel Veillard9e412302002-06-10 15:59:44 +00006177 * To meet the requirements of the spec, the arguments
6178 * must be converted to integer format before
6179 * initial index calculations are done
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006180 *
Daniel Veillard9e412302002-06-10 15:59:44 +00006181 * First we go to integer form, rounding up
6182 * and checking for special cases
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006183 */
6184 i = (int) in;
Daniel Veillard9e412302002-06-10 15:59:44 +00006185 if (((double)i)+0.5 <= in) i++;
Owen Taylor3473f882001-02-23 17:55:21 +00006186
Daniel Veillard9e412302002-06-10 15:59:44 +00006187 if (xmlXPathIsInf(le) == 1) {
6188 l = m;
6189 if (i < 1)
6190 i = 1;
6191 }
6192 else if (xmlXPathIsInf(le) == -1 || le < 0.0)
6193 l = 0;
6194 else {
6195 l = (int) le;
6196 if (((double)l)+0.5 <= le) l++;
6197 }
6198
6199 /* Now we normalize inidices */
6200 i -= 1;
6201 l += i;
6202 if (i < 0)
6203 i = 0;
6204 if (l > m)
6205 l = m;
Owen Taylor3473f882001-02-23 17:55:21 +00006206
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006207 /* number of chars to copy */
6208 l -= i;
Owen Taylor3473f882001-02-23 17:55:21 +00006209
Daniel Veillard0eafdef2002-04-10 16:14:34 +00006210 ret = xmlUTF8Strsub(str->stringval, i, l);
6211 }
6212 else {
6213 ret = NULL;
6214 }
6215
Owen Taylor3473f882001-02-23 17:55:21 +00006216 if (ret == NULL)
6217 valuePush(ctxt, xmlXPathNewCString(""));
6218 else {
6219 valuePush(ctxt, xmlXPathNewString(ret));
6220 xmlFree(ret);
6221 }
Daniel Veillard97ac1312001-05-30 19:14:17 +00006222
Owen Taylor3473f882001-02-23 17:55:21 +00006223 xmlXPathFreeObject(str);
6224}
6225
6226/**
6227 * xmlXPathSubstringBeforeFunction:
6228 * @ctxt: the XPath Parser context
6229 * @nargs: the number of arguments
6230 *
6231 * Implement the substring-before() XPath function
6232 * string substring-before(string, string)
6233 * The substring-before function returns the substring of the first
6234 * argument string that precedes the first occurrence of the second
6235 * argument string in the first argument string, or the empty string
6236 * if the first argument string does not contain the second argument
6237 * string. For example, substring-before("1999/04/01","/") returns 1999.
6238 */
6239void
6240xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6241 xmlXPathObjectPtr str;
6242 xmlXPathObjectPtr find;
6243 xmlBufferPtr target;
6244 const xmlChar *point;
6245 int offset;
6246
6247 CHECK_ARITY(2);
6248 CAST_TO_STRING;
6249 find = valuePop(ctxt);
6250 CAST_TO_STRING;
6251 str = valuePop(ctxt);
6252
6253 target = xmlBufferCreate();
6254 if (target) {
6255 point = xmlStrstr(str->stringval, find->stringval);
6256 if (point) {
6257 offset = (int)(point - str->stringval);
6258 xmlBufferAdd(target, str->stringval, offset);
6259 }
6260 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6261 xmlBufferFree(target);
6262 }
6263
6264 xmlXPathFreeObject(str);
6265 xmlXPathFreeObject(find);
6266}
6267
6268/**
6269 * xmlXPathSubstringAfterFunction:
6270 * @ctxt: the XPath Parser context
6271 * @nargs: the number of arguments
6272 *
6273 * Implement the substring-after() XPath function
6274 * string substring-after(string, string)
6275 * The substring-after function returns the substring of the first
6276 * argument string that follows the first occurrence of the second
6277 * argument string in the first argument string, or the empty stringi
6278 * if the first argument string does not contain the second argument
6279 * string. For example, substring-after("1999/04/01","/") returns 04/01,
6280 * and substring-after("1999/04/01","19") returns 99/04/01.
6281 */
6282void
6283xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6284 xmlXPathObjectPtr str;
6285 xmlXPathObjectPtr find;
6286 xmlBufferPtr target;
6287 const xmlChar *point;
6288 int offset;
6289
6290 CHECK_ARITY(2);
6291 CAST_TO_STRING;
6292 find = valuePop(ctxt);
6293 CAST_TO_STRING;
6294 str = valuePop(ctxt);
6295
6296 target = xmlBufferCreate();
6297 if (target) {
6298 point = xmlStrstr(str->stringval, find->stringval);
6299 if (point) {
6300 offset = (int)(point - str->stringval) + xmlStrlen(find->stringval);
6301 xmlBufferAdd(target, &str->stringval[offset],
6302 xmlStrlen(str->stringval) - offset);
6303 }
6304 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6305 xmlBufferFree(target);
6306 }
6307
6308 xmlXPathFreeObject(str);
6309 xmlXPathFreeObject(find);
6310}
6311
6312/**
6313 * xmlXPathNormalizeFunction:
6314 * @ctxt: the XPath Parser context
6315 * @nargs: the number of arguments
6316 *
6317 * Implement the normalize-space() XPath function
6318 * string normalize-space(string?)
6319 * The normalize-space function returns the argument string with white
6320 * space normalized by stripping leading and trailing whitespace
6321 * and replacing sequences of whitespace characters by a single
6322 * space. Whitespace characters are the same allowed by the S production
6323 * in XML. If the argument is omitted, it defaults to the context
6324 * node converted to a string, in other words the value of the context node.
6325 */
6326void
6327xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6328 xmlXPathObjectPtr obj = NULL;
6329 xmlChar *source = NULL;
6330 xmlBufferPtr target;
6331 xmlChar blank;
6332
6333 if (nargs == 0) {
6334 /* Use current context node */
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006335 valuePush(ctxt,
6336 xmlXPathWrapString(
6337 xmlXPathCastNodeToString(ctxt->context->node)));
Owen Taylor3473f882001-02-23 17:55:21 +00006338 nargs = 1;
6339 }
6340
6341 CHECK_ARITY(1);
6342 CAST_TO_STRING;
6343 CHECK_TYPE(XPATH_STRING);
6344 obj = valuePop(ctxt);
6345 source = obj->stringval;
6346
6347 target = xmlBufferCreate();
6348 if (target && source) {
6349
6350 /* Skip leading whitespaces */
6351 while (IS_BLANK(*source))
6352 source++;
6353
6354 /* Collapse intermediate whitespaces, and skip trailing whitespaces */
6355 blank = 0;
6356 while (*source) {
6357 if (IS_BLANK(*source)) {
Daniel Veillard97ac1312001-05-30 19:14:17 +00006358 blank = 0x20;
Owen Taylor3473f882001-02-23 17:55:21 +00006359 } else {
6360 if (blank) {
6361 xmlBufferAdd(target, &blank, 1);
6362 blank = 0;
6363 }
6364 xmlBufferAdd(target, source, 1);
6365 }
6366 source++;
6367 }
6368
6369 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6370 xmlBufferFree(target);
6371 }
6372 xmlXPathFreeObject(obj);
6373}
6374
6375/**
6376 * xmlXPathTranslateFunction:
6377 * @ctxt: the XPath Parser context
6378 * @nargs: the number of arguments
6379 *
6380 * Implement the translate() XPath function
6381 * string translate(string, string, string)
6382 * The translate function returns the first argument string with
6383 * occurrences of characters in the second argument string replaced
6384 * by the character at the corresponding position in the third argument
6385 * string. For example, translate("bar","abc","ABC") returns the string
6386 * BAr. If there is a character in the second argument string with no
6387 * character at a corresponding position in the third argument string
6388 * (because the second argument string is longer than the third argument
6389 * string), then occurrences of that character in the first argument
6390 * string are removed. For example, translate("--aaa--","abc-","ABC")
6391 * returns "AAA". If a character occurs more than once in second
6392 * argument string, then the first occurrence determines the replacement
6393 * character. If the third argument string is longer than the second
6394 * argument string, then excess characters are ignored.
6395 */
6396void
6397xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillarde043ee12001-04-16 14:08:07 +00006398 xmlXPathObjectPtr str;
6399 xmlXPathObjectPtr from;
6400 xmlXPathObjectPtr to;
6401 xmlBufferPtr target;
Daniel Veillard97ac1312001-05-30 19:14:17 +00006402 int offset, max;
Daniel Veillarde043ee12001-04-16 14:08:07 +00006403 xmlChar ch;
Daniel Veillard97ac1312001-05-30 19:14:17 +00006404 xmlChar *point;
6405 xmlChar *cptr;
Owen Taylor3473f882001-02-23 17:55:21 +00006406
Daniel Veillarde043ee12001-04-16 14:08:07 +00006407 CHECK_ARITY(3);
Owen Taylor3473f882001-02-23 17:55:21 +00006408
Daniel Veillarde043ee12001-04-16 14:08:07 +00006409 CAST_TO_STRING;
6410 to = valuePop(ctxt);
6411 CAST_TO_STRING;
6412 from = valuePop(ctxt);
6413 CAST_TO_STRING;
6414 str = valuePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006415
Daniel Veillarde043ee12001-04-16 14:08:07 +00006416 target = xmlBufferCreate();
6417 if (target) {
Daniel Veillard97ac1312001-05-30 19:14:17 +00006418 max = xmlUTF8Strlen(to->stringval);
6419 for (cptr = str->stringval; (ch=*cptr); ) {
6420 offset = xmlUTF8Strloc(from->stringval, cptr);
6421 if (offset >= 0) {
6422 if (offset < max) {
6423 point = xmlUTF8Strpos(to->stringval, offset);
6424 if (point)
6425 xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1));
6426 }
6427 } else
6428 xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1));
6429
6430 /* Step to next character in input */
6431 cptr++;
6432 if ( ch & 0x80 ) {
6433 /* if not simple ascii, verify proper format */
6434 if ( (ch & 0xc0) != 0xc0 ) {
6435 xmlGenericError(xmlGenericErrorContext,
6436 "xmlXPathTranslateFunction: Invalid UTF8 string\n");
6437 break;
6438 }
6439 /* then skip over remaining bytes for this char */
6440 while ( (ch <<= 1) & 0x80 )
6441 if ( (*cptr++ & 0xc0) != 0x80 ) {
6442 xmlGenericError(xmlGenericErrorContext,
6443 "xmlXPathTranslateFunction: Invalid UTF8 string\n");
6444 break;
6445 }
6446 if (ch & 0x80) /* must have had error encountered */
6447 break;
6448 }
Daniel Veillarde043ee12001-04-16 14:08:07 +00006449 }
Owen Taylor3473f882001-02-23 17:55:21 +00006450 }
Daniel Veillarde043ee12001-04-16 14:08:07 +00006451 valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target)));
6452 xmlBufferFree(target);
6453 xmlXPathFreeObject(str);
6454 xmlXPathFreeObject(from);
6455 xmlXPathFreeObject(to);
Owen Taylor3473f882001-02-23 17:55:21 +00006456}
6457
6458/**
6459 * xmlXPathBooleanFunction:
6460 * @ctxt: the XPath Parser context
6461 * @nargs: the number of arguments
6462 *
6463 * Implement the boolean() XPath function
6464 * boolean boolean(object)
6465 * he boolean function converts its argument to a boolean as follows:
6466 * - a number is true if and only if it is neither positive or
6467 * negative zero nor NaN
6468 * - a node-set is true if and only if it is non-empty
6469 * - a string is true if and only if its length is non-zero
6470 */
6471void
6472xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6473 xmlXPathObjectPtr cur;
Owen Taylor3473f882001-02-23 17:55:21 +00006474
6475 CHECK_ARITY(1);
6476 cur = valuePop(ctxt);
6477 if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00006478 cur = xmlXPathConvertBoolean(cur);
6479 valuePush(ctxt, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00006480}
6481
6482/**
6483 * xmlXPathNotFunction:
6484 * @ctxt: the XPath Parser context
6485 * @nargs: the number of arguments
6486 *
6487 * Implement the not() XPath function
6488 * boolean not(boolean)
6489 * The not function returns true if its argument is false,
6490 * and false otherwise.
6491 */
6492void
6493xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6494 CHECK_ARITY(1);
6495 CAST_TO_BOOLEAN;
6496 CHECK_TYPE(XPATH_BOOLEAN);
6497 ctxt->value->boolval = ! ctxt->value->boolval;
6498}
6499
6500/**
6501 * xmlXPathTrueFunction:
6502 * @ctxt: the XPath Parser context
6503 * @nargs: the number of arguments
6504 *
6505 * Implement the true() XPath function
6506 * boolean true()
6507 */
6508void
6509xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6510 CHECK_ARITY(0);
6511 valuePush(ctxt, xmlXPathNewBoolean(1));
6512}
6513
6514/**
6515 * xmlXPathFalseFunction:
6516 * @ctxt: the XPath Parser context
6517 * @nargs: the number of arguments
6518 *
6519 * Implement the false() XPath function
6520 * boolean false()
6521 */
6522void
6523xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6524 CHECK_ARITY(0);
6525 valuePush(ctxt, xmlXPathNewBoolean(0));
6526}
6527
6528/**
6529 * xmlXPathLangFunction:
6530 * @ctxt: the XPath Parser context
6531 * @nargs: the number of arguments
6532 *
6533 * Implement the lang() XPath function
6534 * boolean lang(string)
6535 * The lang function returns true or false depending on whether the
6536 * language of the context node as specified by xml:lang attributes
6537 * is the same as or is a sublanguage of the language specified by
6538 * the argument string. The language of the context node is determined
6539 * by the value of the xml:lang attribute on the context node, or, if
6540 * the context node has no xml:lang attribute, by the value of the
6541 * xml:lang attribute on the nearest ancestor of the context node that
6542 * has an xml:lang attribute. If there is no such attribute, then lang
6543 * returns false. If there is such an attribute, then lang returns
6544 * true if the attribute value is equal to the argument ignoring case,
6545 * or if there is some suffix starting with - such that the attribute
6546 * value is equal to the argument ignoring that suffix of the attribute
6547 * value and ignoring case.
6548 */
6549void
6550xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6551 xmlXPathObjectPtr val;
6552 const xmlChar *theLang;
6553 const xmlChar *lang;
6554 int ret = 0;
6555 int i;
6556
6557 CHECK_ARITY(1);
6558 CAST_TO_STRING;
6559 CHECK_TYPE(XPATH_STRING);
6560 val = valuePop(ctxt);
6561 lang = val->stringval;
6562 theLang = xmlNodeGetLang(ctxt->context->node);
6563 if ((theLang != NULL) && (lang != NULL)) {
6564 for (i = 0;lang[i] != 0;i++)
6565 if (toupper(lang[i]) != toupper(theLang[i]))
6566 goto not_equal;
6567 ret = 1;
6568 }
6569not_equal:
6570 xmlXPathFreeObject(val);
6571 valuePush(ctxt, xmlXPathNewBoolean(ret));
6572}
6573
6574/**
6575 * xmlXPathNumberFunction:
6576 * @ctxt: the XPath Parser context
6577 * @nargs: the number of arguments
6578 *
6579 * Implement the number() XPath function
6580 * number number(object?)
6581 */
6582void
6583xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6584 xmlXPathObjectPtr cur;
6585 double res;
6586
6587 if (nargs == 0) {
6588 if (ctxt->context->node == NULL) {
6589 valuePush(ctxt, xmlXPathNewFloat(0.0));
6590 } else {
6591 xmlChar* content = xmlNodeGetContent(ctxt->context->node);
6592
6593 res = xmlXPathStringEvalNumber(content);
6594 valuePush(ctxt, xmlXPathNewFloat(res));
6595 xmlFree(content);
6596 }
6597 return;
6598 }
6599
6600 CHECK_ARITY(1);
6601 cur = valuePop(ctxt);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00006602 cur = xmlXPathConvertNumber(cur);
6603 valuePush(ctxt, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00006604}
6605
6606/**
6607 * xmlXPathSumFunction:
6608 * @ctxt: the XPath Parser context
6609 * @nargs: the number of arguments
6610 *
6611 * Implement the sum() XPath function
6612 * number sum(node-set)
6613 * The sum function returns the sum of the values of the nodes in
6614 * the argument node-set.
6615 */
6616void
6617xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6618 xmlXPathObjectPtr cur;
6619 int i;
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006620 double res = 0.0;
Owen Taylor3473f882001-02-23 17:55:21 +00006621
6622 CHECK_ARITY(1);
6623 if ((ctxt->value == NULL) ||
6624 ((ctxt->value->type != XPATH_NODESET) &&
6625 (ctxt->value->type != XPATH_XSLT_TREE)))
6626 XP_ERROR(XPATH_INVALID_TYPE);
6627 cur = valuePop(ctxt);
6628
Daniel Veillardd8df6c02001-04-05 16:54:14 +00006629 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00006630 valuePush(ctxt, xmlXPathNewFloat(0.0));
6631 } else {
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006632 for (i = 0; i < cur->nodesetval->nodeNr; i++) {
6633 res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00006634 }
Daniel Veillardba0b8c92001-05-15 09:43:47 +00006635 valuePush(ctxt, xmlXPathNewFloat(res));
Owen Taylor3473f882001-02-23 17:55:21 +00006636 }
6637 xmlXPathFreeObject(cur);
6638}
6639
6640/**
6641 * xmlXPathFloorFunction:
6642 * @ctxt: the XPath Parser context
6643 * @nargs: the number of arguments
6644 *
6645 * Implement the floor() XPath function
6646 * number floor(number)
6647 * The floor function returns the largest (closest to positive infinity)
6648 * number that is not greater than the argument and that is an integer.
6649 */
6650void
6651xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) {
Daniel Veillard56cd18b2002-03-22 14:14:43 +00006652 double f;
6653
Owen Taylor3473f882001-02-23 17:55:21 +00006654 CHECK_ARITY(1);
6655 CAST_TO_NUMBER;
6656 CHECK_TYPE(XPATH_NUMBER);
Daniel Veillard56cd18b2002-03-22 14:14:43 +00006657
6658 f = (double)((int) ctxt->value->floatval);
6659 if (f != ctxt->value->floatval) {
6660 if (ctxt->value->floatval > 0)
6661 ctxt->value->floatval = f;
6662 else
6663 ctxt->value->floatval = f - 1;
6664 }
Owen Taylor3473f882001-02-23 17:55:21 +00006665}
6666
6667/**
6668 * xmlXPathCeilingFunction:
6669 * @ctxt: the XPath Parser context
6670 * @nargs: the number of arguments
6671 *
6672 * Implement the ceiling() XPath function
6673 * number ceiling(number)
6674 * The ceiling function returns the smallest (closest to negative infinity)
6675 * number that is not less than the argument and that is an integer.
6676 */
6677void
6678xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6679 double f;
6680
6681 CHECK_ARITY(1);
6682 CAST_TO_NUMBER;
6683 CHECK_TYPE(XPATH_NUMBER);
6684
6685#if 0
6686 ctxt->value->floatval = ceil(ctxt->value->floatval);
6687#else
6688 f = (double)((int) ctxt->value->floatval);
Daniel Veillard56cd18b2002-03-22 14:14:43 +00006689 if (f != ctxt->value->floatval) {
6690 if (ctxt->value->floatval > 0)
6691 ctxt->value->floatval = f + 1;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00006692 else {
6693 if (ctxt->value->floatval < 0 && f == 0)
6694 ctxt->value->floatval = xmlXPathNZERO;
6695 else
6696 ctxt->value->floatval = f;
6697 }
6698
Daniel Veillard56cd18b2002-03-22 14:14:43 +00006699 }
Owen Taylor3473f882001-02-23 17:55:21 +00006700#endif
6701}
6702
6703/**
6704 * xmlXPathRoundFunction:
6705 * @ctxt: the XPath Parser context
6706 * @nargs: the number of arguments
6707 *
6708 * Implement the round() XPath function
6709 * number round(number)
6710 * The round function returns the number that is closest to the
6711 * argument and that is an integer. If there are two such numbers,
6712 * then the one that is even is returned.
6713 */
6714void
6715xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) {
6716 double f;
6717
6718 CHECK_ARITY(1);
6719 CAST_TO_NUMBER;
6720 CHECK_TYPE(XPATH_NUMBER);
6721
Daniel Veillardcda96922001-08-21 10:56:31 +00006722 if ((xmlXPathIsNaN(ctxt->value->floatval)) ||
6723 (xmlXPathIsInf(ctxt->value->floatval) == 1) ||
6724 (xmlXPathIsInf(ctxt->value->floatval) == -1) ||
Owen Taylor3473f882001-02-23 17:55:21 +00006725 (ctxt->value->floatval == 0.0))
6726 return;
6727
Owen Taylor3473f882001-02-23 17:55:21 +00006728 f = (double)((int) ctxt->value->floatval);
Daniel Veillard56cd18b2002-03-22 14:14:43 +00006729 if (ctxt->value->floatval < 0) {
6730 if (ctxt->value->floatval < f - 0.5)
6731 ctxt->value->floatval = f - 1;
6732 else
6733 ctxt->value->floatval = f;
Daniel Veillard5fc1f082002-03-27 09:05:40 +00006734 if (ctxt->value->floatval == 0)
6735 ctxt->value->floatval = xmlXPathNZERO;
Daniel Veillard56cd18b2002-03-22 14:14:43 +00006736 } else {
6737 if (ctxt->value->floatval < f + 0.5)
6738 ctxt->value->floatval = f;
6739 else
6740 ctxt->value->floatval = f + 1;
6741 }
Owen Taylor3473f882001-02-23 17:55:21 +00006742}
6743
6744/************************************************************************
6745 * *
6746 * The Parser *
6747 * *
6748 ************************************************************************/
6749
6750/*
6751 * a couple of forward declarations since we use a recursive call based
6752 * implementation.
6753 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00006754static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00006755static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00006756static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006757#ifdef VMS
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00006758static void xmlXPathCompRelLocationPath(xmlXPathParserContextPtr ctxt);
6759#define xmlXPathCompRelativeLocationPath xmlXPathCompRelLocationPath
Owen Taylor3473f882001-02-23 17:55:21 +00006760#else
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00006761static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006762#endif
Daniel Veillard2156a562001-04-28 12:24:34 +00006763static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt,
6764 int qualified);
Owen Taylor3473f882001-02-23 17:55:21 +00006765
6766/**
Daniel Veillard61d80a22001-04-27 17:13:01 +00006767 * xmlXPathCurrentChar:
6768 * @ctxt: the XPath parser context
6769 * @cur: pointer to the beginning of the char
6770 * @len: pointer to the length of the char read
6771 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006772 * The current char value, if using UTF-8 this may actually span multiple
Daniel Veillard61d80a22001-04-27 17:13:01 +00006773 * bytes in the input buffer.
6774 *
Daniel Veillard60087f32001-10-10 09:45:09 +00006775 * Returns the current char value and its length
Daniel Veillard61d80a22001-04-27 17:13:01 +00006776 */
6777
6778static int
6779xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) {
6780 unsigned char c;
6781 unsigned int val;
6782 const xmlChar *cur;
6783
6784 if (ctxt == NULL)
6785 return(0);
6786 cur = ctxt->cur;
6787
6788 /*
6789 * We are supposed to handle UTF8, check it's valid
6790 * From rfc2044: encoding of the Unicode values on UTF-8:
6791 *
6792 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
6793 * 0000 0000-0000 007F 0xxxxxxx
6794 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
6795 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
6796 *
6797 * Check for the 0x110000 limit too
6798 */
6799 c = *cur;
6800 if (c & 0x80) {
6801 if ((cur[1] & 0xc0) != 0x80)
6802 goto encoding_error;
6803 if ((c & 0xe0) == 0xe0) {
6804
6805 if ((cur[2] & 0xc0) != 0x80)
6806 goto encoding_error;
6807 if ((c & 0xf0) == 0xf0) {
6808 if (((c & 0xf8) != 0xf0) ||
6809 ((cur[3] & 0xc0) != 0x80))
6810 goto encoding_error;
6811 /* 4-byte code */
6812 *len = 4;
6813 val = (cur[0] & 0x7) << 18;
6814 val |= (cur[1] & 0x3f) << 12;
6815 val |= (cur[2] & 0x3f) << 6;
6816 val |= cur[3] & 0x3f;
6817 } else {
6818 /* 3-byte code */
6819 *len = 3;
6820 val = (cur[0] & 0xf) << 12;
6821 val |= (cur[1] & 0x3f) << 6;
6822 val |= cur[2] & 0x3f;
6823 }
6824 } else {
6825 /* 2-byte code */
6826 *len = 2;
6827 val = (cur[0] & 0x1f) << 6;
6828 val |= cur[1] & 0x3f;
6829 }
6830 if (!IS_CHAR(val)) {
6831 XP_ERROR0(XPATH_INVALID_CHAR_ERROR);
6832 }
6833 return(val);
6834 } else {
6835 /* 1-byte code */
6836 *len = 1;
6837 return((int) *cur);
6838 }
6839encoding_error:
6840 /*
6841 * If we detect an UTF8 error that probably mean that the
6842 * input encoding didn't get properly advertized in the
6843 * declaration header. Report the error and switch the encoding
6844 * to ISO-Latin-1 (if you don't like this policy, just declare the
6845 * encoding !)
6846 */
Daniel Veillard42596ad2001-05-22 16:57:14 +00006847 *len = 0;
Daniel Veillard61d80a22001-04-27 17:13:01 +00006848 XP_ERROR0(XPATH_ENCODING_ERROR);
Daniel Veillard61d80a22001-04-27 17:13:01 +00006849}
6850
6851/**
Owen Taylor3473f882001-02-23 17:55:21 +00006852 * xmlXPathParseNCName:
6853 * @ctxt: the XPath Parser context
6854 *
6855 * parse an XML namespace non qualified name.
6856 *
6857 * [NS 3] NCName ::= (Letter | '_') (NCNameChar)*
6858 *
6859 * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
6860 * CombiningChar | Extender
6861 *
6862 * Returns the namespace name or NULL
6863 */
6864
6865xmlChar *
6866xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) {
Daniel Veillard2156a562001-04-28 12:24:34 +00006867 const xmlChar *in;
6868 xmlChar *ret;
6869 int count = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006870
Daniel Veillard2156a562001-04-28 12:24:34 +00006871 /*
6872 * Accelerator for simple ASCII names
6873 */
6874 in = ctxt->cur;
6875 if (((*in >= 0x61) && (*in <= 0x7A)) ||
6876 ((*in >= 0x41) && (*in <= 0x5A)) ||
6877 (*in == '_')) {
6878 in++;
6879 while (((*in >= 0x61) && (*in <= 0x7A)) ||
6880 ((*in >= 0x41) && (*in <= 0x5A)) ||
6881 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard9a89a8a2001-06-27 11:13:35 +00006882 (*in == '_') || (*in == '.') ||
6883 (*in == '-'))
Daniel Veillard2156a562001-04-28 12:24:34 +00006884 in++;
6885 if ((*in == ' ') || (*in == '>') || (*in == '/') ||
6886 (*in == '[') || (*in == ']') || (*in == ':') ||
6887 (*in == '@') || (*in == '*')) {
6888 count = in - ctxt->cur;
6889 if (count == 0)
6890 return(NULL);
6891 ret = xmlStrndup(ctxt->cur, count);
6892 ctxt->cur = in;
6893 return(ret);
6894 }
6895 }
6896 return(xmlXPathParseNameComplex(ctxt, 0));
Owen Taylor3473f882001-02-23 17:55:21 +00006897}
6898
Daniel Veillard2156a562001-04-28 12:24:34 +00006899
Owen Taylor3473f882001-02-23 17:55:21 +00006900/**
6901 * xmlXPathParseQName:
6902 * @ctxt: the XPath Parser context
6903 * @prefix: a xmlChar **
6904 *
6905 * parse an XML qualified name
6906 *
6907 * [NS 5] QName ::= (Prefix ':')? LocalPart
6908 *
6909 * [NS 6] Prefix ::= NCName
6910 *
6911 * [NS 7] LocalPart ::= NCName
6912 *
6913 * Returns the function returns the local part, and prefix is updated
6914 * to get the Prefix if any.
6915 */
6916
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006917static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006918xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) {
6919 xmlChar *ret = NULL;
6920
6921 *prefix = NULL;
6922 ret = xmlXPathParseNCName(ctxt);
6923 if (CUR == ':') {
6924 *prefix = ret;
6925 NEXT;
6926 ret = xmlXPathParseNCName(ctxt);
6927 }
6928 return(ret);
6929}
6930
6931/**
6932 * xmlXPathParseName:
6933 * @ctxt: the XPath Parser context
6934 *
6935 * parse an XML name
6936 *
6937 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
6938 * CombiningChar | Extender
6939 *
6940 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
6941 *
6942 * Returns the namespace name or NULL
6943 */
6944
6945xmlChar *
6946xmlXPathParseName(xmlXPathParserContextPtr ctxt) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00006947 const xmlChar *in;
6948 xmlChar *ret;
6949 int count = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006950
Daniel Veillard61d80a22001-04-27 17:13:01 +00006951 /*
6952 * Accelerator for simple ASCII names
6953 */
6954 in = ctxt->cur;
6955 if (((*in >= 0x61) && (*in <= 0x7A)) ||
6956 ((*in >= 0x41) && (*in <= 0x5A)) ||
6957 (*in == '_') || (*in == ':')) {
6958 in++;
6959 while (((*in >= 0x61) && (*in <= 0x7A)) ||
6960 ((*in >= 0x41) && (*in <= 0x5A)) ||
6961 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard76d66f42001-05-16 21:05:17 +00006962 (*in == '_') || (*in == '-') ||
6963 (*in == ':') || (*in == '.'))
Daniel Veillard61d80a22001-04-27 17:13:01 +00006964 in++;
Daniel Veillard76d66f42001-05-16 21:05:17 +00006965 if ((*in > 0) && (*in < 0x80)) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00006966 count = in - ctxt->cur;
6967 ret = xmlStrndup(ctxt->cur, count);
6968 ctxt->cur = in;
6969 return(ret);
6970 }
6971 }
Daniel Veillard2156a562001-04-28 12:24:34 +00006972 return(xmlXPathParseNameComplex(ctxt, 1));
Owen Taylor3473f882001-02-23 17:55:21 +00006973}
6974
Daniel Veillard61d80a22001-04-27 17:13:01 +00006975static xmlChar *
Daniel Veillard2156a562001-04-28 12:24:34 +00006976xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00006977 xmlChar buf[XML_MAX_NAMELEN + 5];
6978 int len = 0, l;
6979 int c;
6980
6981 /*
6982 * Handler for more complex cases
6983 */
6984 c = CUR_CHAR(l);
6985 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
Daniel Veillard2156a562001-04-28 12:24:34 +00006986 (c == '[') || (c == ']') || (c == '@') || /* accelerators */
6987 (c == '*') || /* accelerators */
Daniel Veillard61d80a22001-04-27 17:13:01 +00006988 (!IS_LETTER(c) && (c != '_') &&
Daniel Veillard2156a562001-04-28 12:24:34 +00006989 ((qualified) && (c != ':')))) {
Daniel Veillard61d80a22001-04-27 17:13:01 +00006990 return(NULL);
6991 }
6992
6993 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
6994 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
6995 (c == '.') || (c == '-') ||
Daniel Veillard2156a562001-04-28 12:24:34 +00006996 (c == '_') || ((qualified) && (c == ':')) ||
Daniel Veillard61d80a22001-04-27 17:13:01 +00006997 (IS_COMBINING(c)) ||
6998 (IS_EXTENDER(c)))) {
6999 COPY_BUF(l,buf,len,c);
7000 NEXTL(l);
7001 c = CUR_CHAR(l);
7002 if (len >= XML_MAX_NAMELEN) {
7003 /*
7004 * Okay someone managed to make a huge name, so he's ready to pay
7005 * for the processing speed.
7006 */
7007 xmlChar *buffer;
7008 int max = len * 2;
7009
7010 buffer = (xmlChar *) xmlMalloc(max * sizeof(xmlChar));
7011 if (buffer == NULL) {
7012 XP_ERROR0(XPATH_MEMORY_ERROR);
7013 }
7014 memcpy(buffer, buf, len);
7015 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */
7016 (c == '.') || (c == '-') ||
Daniel Veillard2156a562001-04-28 12:24:34 +00007017 (c == '_') || ((qualified) && (c == ':')) ||
Daniel Veillard61d80a22001-04-27 17:13:01 +00007018 (IS_COMBINING(c)) ||
7019 (IS_EXTENDER(c))) {
7020 if (len + 10 > max) {
7021 max *= 2;
7022 buffer = (xmlChar *) xmlRealloc(buffer,
7023 max * sizeof(xmlChar));
Daniel Veillard61d80a22001-04-27 17:13:01 +00007024 if (buffer == NULL) {
7025 XP_ERROR0(XPATH_MEMORY_ERROR);
7026 }
7027 }
7028 COPY_BUF(l,buffer,len,c);
7029 NEXTL(l);
7030 c = CUR_CHAR(l);
7031 }
7032 buffer[len] = 0;
7033 return(buffer);
7034 }
7035 }
Daniel Veillard2156a562001-04-28 12:24:34 +00007036 if (len == 0)
7037 return(NULL);
Daniel Veillard61d80a22001-04-27 17:13:01 +00007038 return(xmlStrndup(buf, len));
7039}
Daniel Veillard3cd72402002-05-13 10:33:30 +00007040
7041#define MAX_FRAC 20
7042
7043static double my_pow10[MAX_FRAC] = {
7044 1.0, 10.0, 100.0, 1000.0, 10000.0,
7045 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0,
7046 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0,
7047 100000000000000.0,
7048 1000000000000000.0, 10000000000000000.0, 100000000000000000.0,
7049 1000000000000000000.0, 10000000000000000000.0
7050};
7051
Owen Taylor3473f882001-02-23 17:55:21 +00007052/**
7053 * xmlXPathStringEvalNumber:
7054 * @str: A string to scan
7055 *
Bjorn Reese70a9da52001-04-21 16:57:29 +00007056 * [30a] Float ::= Number ('e' Digits?)?
7057 *
Owen Taylor3473f882001-02-23 17:55:21 +00007058 * [30] Number ::= Digits ('.' Digits?)?
7059 * | '.' Digits
7060 * [31] Digits ::= [0-9]+
7061 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007062 * Compile a Number in the string
Owen Taylor3473f882001-02-23 17:55:21 +00007063 * In complement of the Number expression, this function also handles
7064 * negative values : '-' Number.
7065 *
7066 * Returns the double value.
7067 */
7068double
7069xmlXPathStringEvalNumber(const xmlChar *str) {
7070 const xmlChar *cur = str;
Daniel Veillard7b416132002-03-07 08:36:03 +00007071 double ret;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007072 int ok = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007073 int isneg = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007074 int exponent = 0;
7075 int is_exponent_negative = 0;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007076#ifdef __GNUC__
7077 unsigned long tmp = 0;
Daniel Veillard7b416132002-03-07 08:36:03 +00007078 double temp;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007079#endif
Daniel Veillardeca82812002-04-24 11:42:02 +00007080 if (cur == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00007081 while (IS_BLANK(*cur)) cur++;
7082 if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) {
7083 return(xmlXPathNAN);
7084 }
7085 if (*cur == '-') {
7086 isneg = 1;
7087 cur++;
7088 }
Daniel Veillardb06c6142001-08-27 14:26:30 +00007089
7090#ifdef __GNUC__
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007091 /*
Daniel Veillard7b416132002-03-07 08:36:03 +00007092 * tmp/temp is a workaround against a gcc compiler bug
7093 * http://veillard.com/gcc.bug
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007094 */
Daniel Veillard7b416132002-03-07 08:36:03 +00007095 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007096 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillard7b416132002-03-07 08:36:03 +00007097 ret = ret * 10;
7098 tmp = (*cur - '0');
Owen Taylor3473f882001-02-23 17:55:21 +00007099 ok = 1;
7100 cur++;
Daniel Veillard7b416132002-03-07 08:36:03 +00007101 temp = (double) tmp;
7102 ret = ret + temp;
Owen Taylor3473f882001-02-23 17:55:21 +00007103 }
Daniel Veillardb06c6142001-08-27 14:26:30 +00007104#else
Daniel Veillard7b416132002-03-07 08:36:03 +00007105 ret = 0;
Daniel Veillardb06c6142001-08-27 14:26:30 +00007106 while ((*cur >= '0') && (*cur <= '9')) {
7107 ret = ret * 10 + (*cur - '0');
7108 ok = 1;
7109 cur++;
7110 }
7111#endif
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007112
Owen Taylor3473f882001-02-23 17:55:21 +00007113 if (*cur == '.') {
Daniel Veillard3cd72402002-05-13 10:33:30 +00007114 int v, frac = 0;
7115 double fraction = 0;
7116
Owen Taylor3473f882001-02-23 17:55:21 +00007117 cur++;
7118 if (((*cur < '0') || (*cur > '9')) && (!ok)) {
7119 return(xmlXPathNAN);
7120 }
Daniel Veillard3cd72402002-05-13 10:33:30 +00007121 while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) {
7122 v = (*cur - '0');
7123 fraction = fraction * 10 + v;
7124 frac = frac + 1;
Owen Taylor3473f882001-02-23 17:55:21 +00007125 cur++;
7126 }
Daniel Veillard3cd72402002-05-13 10:33:30 +00007127 fraction /= my_pow10[frac];
7128 ret = ret + fraction;
7129 while ((*cur >= '0') && (*cur <= '9'))
7130 cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00007131 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00007132 if ((*cur == 'e') || (*cur == 'E')) {
7133 cur++;
7134 if (*cur == '-') {
7135 is_exponent_negative = 1;
7136 cur++;
7137 }
7138 while ((*cur >= '0') && (*cur <= '9')) {
7139 exponent = exponent * 10 + (*cur - '0');
7140 cur++;
7141 }
7142 }
Owen Taylor3473f882001-02-23 17:55:21 +00007143 while (IS_BLANK(*cur)) cur++;
7144 if (*cur != 0) return(xmlXPathNAN);
7145 if (isneg) ret = -ret;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007146 if (is_exponent_negative) exponent = -exponent;
7147 ret *= pow(10.0, (double)exponent);
Owen Taylor3473f882001-02-23 17:55:21 +00007148 return(ret);
7149}
7150
7151/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007152 * xmlXPathCompNumber:
Owen Taylor3473f882001-02-23 17:55:21 +00007153 * @ctxt: the XPath Parser context
7154 *
7155 * [30] Number ::= Digits ('.' Digits?)?
7156 * | '.' Digits
7157 * [31] Digits ::= [0-9]+
7158 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007159 * Compile a Number, then push it on the stack
Owen Taylor3473f882001-02-23 17:55:21 +00007160 *
7161 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007162static void
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007163xmlXPathCompNumber(xmlXPathParserContextPtr ctxt)
7164{
Owen Taylor3473f882001-02-23 17:55:21 +00007165 double ret = 0.0;
7166 double mult = 1;
Daniel Veillard7b416132002-03-07 08:36:03 +00007167 int ok = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00007168 int exponent = 0;
7169 int is_exponent_negative = 0;
Daniel Veillard7b416132002-03-07 08:36:03 +00007170#ifdef __GNUC__
7171 unsigned long tmp = 0;
7172 double temp;
7173#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007174
7175 CHECK_ERROR;
7176 if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) {
7177 XP_ERROR(XPATH_NUMBER_ERROR);
7178 }
Daniel Veillard7b416132002-03-07 08:36:03 +00007179#ifdef __GNUC__
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007180 /*
Daniel Veillard7b416132002-03-07 08:36:03 +00007181 * tmp/temp is a workaround against a gcc compiler bug
7182 * http://veillard.com/gcc.bug
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007183 */
Daniel Veillard7b416132002-03-07 08:36:03 +00007184 ret = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007185 while ((CUR >= '0') && (CUR <= '9')) {
Daniel Veillard7b416132002-03-07 08:36:03 +00007186 ret = ret * 10;
7187 tmp = (CUR - '0');
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007188 ok = 1;
7189 NEXT;
Daniel Veillard7b416132002-03-07 08:36:03 +00007190 temp = (double) tmp;
7191 ret = ret + temp;
Owen Taylor3473f882001-02-23 17:55:21 +00007192 }
Daniel Veillard7b416132002-03-07 08:36:03 +00007193#else
7194 ret = 0;
7195 while ((CUR >= '0') && (CUR <= '9')) {
7196 ret = ret * 10 + (CUR - '0');
7197 ok = 1;
7198 NEXT;
7199 }
7200#endif
Owen Taylor3473f882001-02-23 17:55:21 +00007201 if (CUR == '.') {
7202 NEXT;
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007203 if (((CUR < '0') || (CUR > '9')) && (!ok)) {
7204 XP_ERROR(XPATH_NUMBER_ERROR);
7205 }
7206 while ((CUR >= '0') && (CUR <= '9')) {
7207 mult /= 10;
7208 ret = ret + (CUR - '0') * mult;
7209 NEXT;
7210 }
Owen Taylor3473f882001-02-23 17:55:21 +00007211 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00007212 if ((CUR == 'e') || (CUR == 'E')) {
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007213 NEXT;
7214 if (CUR == '-') {
7215 is_exponent_negative = 1;
7216 NEXT;
7217 }
7218 while ((CUR >= '0') && (CUR <= '9')) {
7219 exponent = exponent * 10 + (CUR - '0');
7220 NEXT;
7221 }
7222 if (is_exponent_negative)
7223 exponent = -exponent;
7224 ret *= pow(10.0, (double) exponent);
Bjorn Reese70a9da52001-04-21 16:57:29 +00007225 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007226 PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0,
Daniel Veillardd79bcd12001-06-21 22:07:42 +00007227 xmlXPathNewFloat(ret), NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007228}
7229
7230/**
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007231 * xmlXPathParseLiteral:
7232 * @ctxt: the XPath Parser context
7233 *
7234 * Parse a Literal
7235 *
7236 * [29] Literal ::= '"' [^"]* '"'
7237 * | "'" [^']* "'"
7238 *
7239 * Returns the value found or NULL in case of error
7240 */
7241static xmlChar *
7242xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) {
7243 const xmlChar *q;
7244 xmlChar *ret = NULL;
7245
7246 if (CUR == '"') {
7247 NEXT;
7248 q = CUR_PTR;
7249 while ((IS_CHAR(CUR)) && (CUR != '"'))
7250 NEXT;
7251 if (!IS_CHAR(CUR)) {
7252 XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR);
7253 } else {
7254 ret = xmlStrndup(q, CUR_PTR - q);
7255 NEXT;
7256 }
7257 } else if (CUR == '\'') {
7258 NEXT;
7259 q = CUR_PTR;
7260 while ((IS_CHAR(CUR)) && (CUR != '\''))
7261 NEXT;
7262 if (!IS_CHAR(CUR)) {
7263 XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR);
7264 } else {
7265 ret = xmlStrndup(q, CUR_PTR - q);
7266 NEXT;
7267 }
7268 } else {
7269 XP_ERROR0(XPATH_START_LITERAL_ERROR);
7270 }
7271 return(ret);
7272}
7273
7274/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007275 * xmlXPathCompLiteral:
Owen Taylor3473f882001-02-23 17:55:21 +00007276 * @ctxt: the XPath Parser context
7277 *
7278 * Parse a Literal and push it on the stack.
7279 *
7280 * [29] Literal ::= '"' [^"]* '"'
7281 * | "'" [^']* "'"
7282 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007283 * TODO: xmlXPathCompLiteral memory allocation could be improved.
Owen Taylor3473f882001-02-23 17:55:21 +00007284 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007285static void
7286xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007287 const xmlChar *q;
7288 xmlChar *ret = NULL;
7289
7290 if (CUR == '"') {
7291 NEXT;
7292 q = CUR_PTR;
7293 while ((IS_CHAR(CUR)) && (CUR != '"'))
7294 NEXT;
7295 if (!IS_CHAR(CUR)) {
7296 XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR);
7297 } else {
7298 ret = xmlStrndup(q, CUR_PTR - q);
7299 NEXT;
7300 }
7301 } else if (CUR == '\'') {
7302 NEXT;
7303 q = CUR_PTR;
7304 while ((IS_CHAR(CUR)) && (CUR != '\''))
7305 NEXT;
7306 if (!IS_CHAR(CUR)) {
7307 XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR);
7308 } else {
7309 ret = xmlStrndup(q, CUR_PTR - q);
7310 NEXT;
7311 }
7312 } else {
7313 XP_ERROR(XPATH_START_LITERAL_ERROR);
7314 }
7315 if (ret == NULL) return;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007316 PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0,
7317 xmlXPathNewString(ret), NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007318 xmlFree(ret);
7319}
7320
7321/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007322 * xmlXPathCompVariableReference:
Owen Taylor3473f882001-02-23 17:55:21 +00007323 * @ctxt: the XPath Parser context
7324 *
7325 * Parse a VariableReference, evaluate it and push it on the stack.
7326 *
7327 * The variable bindings consist of a mapping from variable names
7328 * to variable values. The value of a variable is an object, which
7329 * of any of the types that are possible for the value of an expression,
7330 * and may also be of additional types not specified here.
7331 *
7332 * Early evaluation is possible since:
7333 * The variable bindings [...] used to evaluate a subexpression are
7334 * always the same as those used to evaluate the containing expression.
7335 *
7336 * [36] VariableReference ::= '$' QName
7337 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007338static void
7339xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007340 xmlChar *name;
7341 xmlChar *prefix;
Owen Taylor3473f882001-02-23 17:55:21 +00007342
7343 SKIP_BLANKS;
7344 if (CUR != '$') {
7345 XP_ERROR(XPATH_VARIABLE_REF_ERROR);
7346 }
7347 NEXT;
7348 name = xmlXPathParseQName(ctxt, &prefix);
7349 if (name == NULL) {
7350 XP_ERROR(XPATH_VARIABLE_REF_ERROR);
7351 }
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00007352 ctxt->comp->last = -1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007353 PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0,
7354 name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00007355 SKIP_BLANKS;
7356}
7357
7358/**
7359 * xmlXPathIsNodeType:
7360 * @ctxt: the XPath Parser context
7361 * @name: a name string
7362 *
7363 * Is the name given a NodeType one.
7364 *
7365 * [38] NodeType ::= 'comment'
7366 * | 'text'
7367 * | 'processing-instruction'
7368 * | 'node'
7369 *
7370 * Returns 1 if true 0 otherwise
7371 */
7372int
7373xmlXPathIsNodeType(const xmlChar *name) {
7374 if (name == NULL)
7375 return(0);
7376
Daniel Veillard1971ee22002-01-31 20:29:19 +00007377 if (xmlStrEqual(name, BAD_CAST "node"))
Owen Taylor3473f882001-02-23 17:55:21 +00007378 return(1);
7379 if (xmlStrEqual(name, BAD_CAST "text"))
7380 return(1);
Daniel Veillard1971ee22002-01-31 20:29:19 +00007381 if (xmlStrEqual(name, BAD_CAST "comment"))
Owen Taylor3473f882001-02-23 17:55:21 +00007382 return(1);
Daniel Veillard1971ee22002-01-31 20:29:19 +00007383 if (xmlStrEqual(name, BAD_CAST "processing-instruction"))
Owen Taylor3473f882001-02-23 17:55:21 +00007384 return(1);
7385 return(0);
7386}
7387
7388/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007389 * xmlXPathCompFunctionCall:
Owen Taylor3473f882001-02-23 17:55:21 +00007390 * @ctxt: the XPath Parser context
7391 *
7392 * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')'
7393 * [17] Argument ::= Expr
7394 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007395 * Compile a function call, the evaluation of all arguments are
Owen Taylor3473f882001-02-23 17:55:21 +00007396 * pushed on the stack
7397 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007398static void
7399xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007400 xmlChar *name;
7401 xmlChar *prefix;
Owen Taylor3473f882001-02-23 17:55:21 +00007402 int nbargs = 0;
7403
7404 name = xmlXPathParseQName(ctxt, &prefix);
7405 if (name == NULL) {
7406 XP_ERROR(XPATH_EXPR_ERROR);
7407 }
7408 SKIP_BLANKS;
Owen Taylor3473f882001-02-23 17:55:21 +00007409#ifdef DEBUG_EXPR
7410 if (prefix == NULL)
7411 xmlGenericError(xmlGenericErrorContext, "Calling function %s\n",
7412 name);
7413 else
7414 xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n",
7415 prefix, name);
7416#endif
7417
Owen Taylor3473f882001-02-23 17:55:21 +00007418 if (CUR != '(') {
7419 XP_ERROR(XPATH_EXPR_ERROR);
7420 }
7421 NEXT;
7422 SKIP_BLANKS;
7423
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007424 ctxt->comp->last = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00007425 while (CUR != ')') {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007426 int op1 = ctxt->comp->last;
7427 ctxt->comp->last = -1;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007428 xmlXPathCompileExpr(ctxt);
Aleksey Sanin50fe8b12002-05-07 16:21:36 +00007429 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007430 PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00007431 nbargs++;
7432 if (CUR == ')') break;
7433 if (CUR != ',') {
7434 XP_ERROR(XPATH_EXPR_ERROR);
7435 }
7436 NEXT;
7437 SKIP_BLANKS;
7438 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007439 PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0,
7440 name, prefix);
Owen Taylor3473f882001-02-23 17:55:21 +00007441 NEXT;
7442 SKIP_BLANKS;
Owen Taylor3473f882001-02-23 17:55:21 +00007443}
7444
7445/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007446 * xmlXPathCompPrimaryExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007447 * @ctxt: the XPath Parser context
7448 *
7449 * [15] PrimaryExpr ::= VariableReference
7450 * | '(' Expr ')'
7451 * | Literal
7452 * | Number
7453 * | FunctionCall
7454 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007455 * Compile a primary expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007456 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007457static void
7458xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007459 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007460 if (CUR == '$') xmlXPathCompVariableReference(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007461 else if (CUR == '(') {
7462 NEXT;
7463 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007464 xmlXPathCompileExpr(ctxt);
Aleksey Sanin50fe8b12002-05-07 16:21:36 +00007465 CHECK_ERROR;
Owen Taylor3473f882001-02-23 17:55:21 +00007466 if (CUR != ')') {
7467 XP_ERROR(XPATH_EXPR_ERROR);
7468 }
7469 NEXT;
7470 SKIP_BLANKS;
Daniel Veillard01917aa2002-04-10 11:30:41 +00007471 } else if (IS_DIGIT(CUR) || (CUR == '.' && IS_DIGIT(NXT(1)))) {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007472 xmlXPathCompNumber(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007473 } else if ((CUR == '\'') || (CUR == '"')) {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007474 xmlXPathCompLiteral(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007475 } else {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007476 xmlXPathCompFunctionCall(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007477 }
7478 SKIP_BLANKS;
7479}
7480
7481/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007482 * xmlXPathCompFilterExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007483 * @ctxt: the XPath Parser context
7484 *
7485 * [20] FilterExpr ::= PrimaryExpr
7486 * | FilterExpr Predicate
7487 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007488 * Compile a filter expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007489 * Square brackets are used to filter expressions in the same way that
7490 * they are used in location paths. It is an error if the expression to
7491 * be filtered does not evaluate to a node-set. The context node list
7492 * used for evaluating the expression in square brackets is the node-set
7493 * to be filtered listed in document order.
7494 */
7495
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007496static void
7497xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) {
7498 xmlXPathCompPrimaryExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007499 CHECK_ERROR;
7500 SKIP_BLANKS;
7501
7502 while (CUR == '[') {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00007503 xmlXPathCompPredicate(ctxt, 1);
Owen Taylor3473f882001-02-23 17:55:21 +00007504 SKIP_BLANKS;
7505 }
7506
7507
7508}
7509
7510/**
7511 * xmlXPathScanName:
7512 * @ctxt: the XPath Parser context
7513 *
7514 * Trickery: parse an XML name but without consuming the input flow
7515 * Needed to avoid insanity in the parser state.
7516 *
7517 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
7518 * CombiningChar | Extender
7519 *
7520 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
7521 *
7522 * [6] Names ::= Name (S Name)*
7523 *
7524 * Returns the Name parsed or NULL
7525 */
7526
Daniel Veillard56a4cb82001-03-24 17:00:36 +00007527static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00007528xmlXPathScanName(xmlXPathParserContextPtr ctxt) {
7529 xmlChar buf[XML_MAX_NAMELEN];
7530 int len = 0;
7531
7532 SKIP_BLANKS;
7533 if (!IS_LETTER(CUR) && (CUR != '_') &&
7534 (CUR != ':')) {
7535 return(NULL);
7536 }
7537
7538 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
7539 (NXT(len) == '.') || (NXT(len) == '-') ||
7540 (NXT(len) == '_') || (NXT(len) == ':') ||
7541 (IS_COMBINING(NXT(len))) ||
7542 (IS_EXTENDER(NXT(len)))) {
7543 buf[len] = NXT(len);
7544 len++;
7545 if (len >= XML_MAX_NAMELEN) {
7546 xmlGenericError(xmlGenericErrorContext,
7547 "xmlScanName: reached XML_MAX_NAMELEN limit\n");
7548 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
7549 (NXT(len) == '.') || (NXT(len) == '-') ||
7550 (NXT(len) == '_') || (NXT(len) == ':') ||
7551 (IS_COMBINING(NXT(len))) ||
7552 (IS_EXTENDER(NXT(len))))
7553 len++;
7554 break;
7555 }
7556 }
7557 return(xmlStrndup(buf, len));
7558}
7559
7560/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007561 * xmlXPathCompPathExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007562 * @ctxt: the XPath Parser context
7563 *
7564 * [19] PathExpr ::= LocationPath
7565 * | FilterExpr
7566 * | FilterExpr '/' RelativeLocationPath
7567 * | FilterExpr '//' RelativeLocationPath
7568 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007569 * Compile a path expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007570 * The / operator and // operators combine an arbitrary expression
7571 * and a relative location path. It is an error if the expression
7572 * does not evaluate to a node-set.
7573 * The / operator does composition in the same way as when / is
7574 * used in a location path. As in location paths, // is short for
7575 * /descendant-or-self::node()/.
7576 */
7577
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007578static void
7579xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007580 int lc = 1; /* Should we branch to LocationPath ? */
7581 xmlChar *name = NULL; /* we may have to preparse a name to find out */
7582
7583 SKIP_BLANKS;
7584 if ((CUR == '$') || (CUR == '(') || (IS_DIGIT(CUR)) ||
Daniel Veillard01917aa2002-04-10 11:30:41 +00007585 (CUR == '\'') || (CUR == '"') || (CUR == '.' && IS_DIGIT(NXT(1)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00007586 lc = 0;
7587 } else if (CUR == '*') {
7588 /* relative or absolute location path */
7589 lc = 1;
7590 } else if (CUR == '/') {
7591 /* relative or absolute location path */
7592 lc = 1;
7593 } else if (CUR == '@') {
7594 /* relative abbreviated attribute location path */
7595 lc = 1;
7596 } else if (CUR == '.') {
7597 /* relative abbreviated attribute location path */
7598 lc = 1;
7599 } else {
7600 /*
7601 * Problem is finding if we have a name here whether it's:
7602 * - a nodetype
7603 * - a function call in which case it's followed by '('
7604 * - an axis in which case it's followed by ':'
7605 * - a element name
7606 * We do an a priori analysis here rather than having to
7607 * maintain parsed token content through the recursive function
7608 * calls. This looks uglier but makes the code quite easier to
7609 * read/write/debug.
7610 */
7611 SKIP_BLANKS;
7612 name = xmlXPathScanName(ctxt);
7613 if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) {
7614#ifdef DEBUG_STEP
7615 xmlGenericError(xmlGenericErrorContext,
7616 "PathExpr: Axis\n");
7617#endif
7618 lc = 1;
7619 xmlFree(name);
7620 } else if (name != NULL) {
7621 int len =xmlStrlen(name);
7622 int blank = 0;
7623
7624
7625 while (NXT(len) != 0) {
7626 if (NXT(len) == '/') {
7627 /* element name */
7628#ifdef DEBUG_STEP
7629 xmlGenericError(xmlGenericErrorContext,
7630 "PathExpr: AbbrRelLocation\n");
7631#endif
7632 lc = 1;
7633 break;
7634 } else if (IS_BLANK(NXT(len))) {
7635 /* skip to next */
7636 blank = 1;
7637 } else if (NXT(len) == ':') {
7638#ifdef DEBUG_STEP
7639 xmlGenericError(xmlGenericErrorContext,
7640 "PathExpr: AbbrRelLocation\n");
7641#endif
7642 lc = 1;
7643 break;
7644 } else if ((NXT(len) == '(')) {
7645 /* Note Type or Function */
7646 if (xmlXPathIsNodeType(name)) {
7647#ifdef DEBUG_STEP
7648 xmlGenericError(xmlGenericErrorContext,
7649 "PathExpr: Type search\n");
7650#endif
7651 lc = 1;
7652 } else {
7653#ifdef DEBUG_STEP
7654 xmlGenericError(xmlGenericErrorContext,
7655 "PathExpr: function call\n");
7656#endif
7657 lc = 0;
7658 }
7659 break;
7660 } else if ((NXT(len) == '[')) {
7661 /* element name */
7662#ifdef DEBUG_STEP
7663 xmlGenericError(xmlGenericErrorContext,
7664 "PathExpr: AbbrRelLocation\n");
7665#endif
7666 lc = 1;
7667 break;
7668 } else if ((NXT(len) == '<') || (NXT(len) == '>') ||
7669 (NXT(len) == '=')) {
7670 lc = 1;
7671 break;
7672 } else {
7673 lc = 1;
7674 break;
7675 }
7676 len++;
7677 }
7678 if (NXT(len) == 0) {
7679#ifdef DEBUG_STEP
7680 xmlGenericError(xmlGenericErrorContext,
7681 "PathExpr: AbbrRelLocation\n");
7682#endif
7683 /* element name */
7684 lc = 1;
7685 }
7686 xmlFree(name);
7687 } else {
7688 /* make sure all cases are covered explicitely */
7689 XP_ERROR(XPATH_EXPR_ERROR);
7690 }
7691 }
7692
7693 if (lc) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007694 if (CUR == '/') {
7695 PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0);
7696 } else {
7697 PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00007698 }
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007699 xmlXPathCompLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007700 } else {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007701 xmlXPathCompFilterExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007702 CHECK_ERROR;
7703 if ((CUR == '/') && (NXT(1) == '/')) {
7704 SKIP(2);
7705 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007706
7707 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
7708 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
7709 PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0);
7710
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007711 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007712 } else if (CUR == '/') {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007713 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007714 }
7715 }
7716 SKIP_BLANKS;
7717}
7718
7719/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007720 * xmlXPathCompUnionExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007721 * @ctxt: the XPath Parser context
7722 *
7723 * [18] UnionExpr ::= PathExpr
7724 * | UnionExpr '|' PathExpr
7725 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007726 * Compile an union expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007727 */
7728
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007729static void
7730xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) {
7731 xmlXPathCompPathExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007732 CHECK_ERROR;
7733 SKIP_BLANKS;
7734 while (CUR == '|') {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007735 int op1 = ctxt->comp->last;
7736 PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00007737
7738 NEXT;
7739 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007740 xmlXPathCompPathExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007741
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007742 PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0);
7743
Owen Taylor3473f882001-02-23 17:55:21 +00007744 SKIP_BLANKS;
7745 }
Owen Taylor3473f882001-02-23 17:55:21 +00007746}
7747
7748/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007749 * xmlXPathCompUnaryExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007750 * @ctxt: the XPath Parser context
7751 *
7752 * [27] UnaryExpr ::= UnionExpr
7753 * | '-' UnaryExpr
7754 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007755 * Compile an unary expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007756 */
7757
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007758static void
7759xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00007760 int minus = 0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007761 int found = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00007762
7763 SKIP_BLANKS;
Daniel Veillard68d7b672001-03-12 18:22:04 +00007764 while (CUR == '-') {
7765 minus = 1 - minus;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007766 found = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00007767 NEXT;
7768 SKIP_BLANKS;
7769 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007770
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007771 xmlXPathCompUnionExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007772 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007773 if (found) {
7774 if (minus)
7775 PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0);
7776 else
7777 PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00007778 }
7779}
7780
7781/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007782 * xmlXPathCompMultiplicativeExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007783 * @ctxt: the XPath Parser context
7784 *
7785 * [26] MultiplicativeExpr ::= UnaryExpr
7786 * | MultiplicativeExpr MultiplyOperator UnaryExpr
7787 * | MultiplicativeExpr 'div' UnaryExpr
7788 * | MultiplicativeExpr 'mod' UnaryExpr
7789 * [34] MultiplyOperator ::= '*'
7790 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007791 * Compile an Additive expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007792 */
7793
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007794static void
7795xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) {
7796 xmlXPathCompUnaryExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007797 CHECK_ERROR;
7798 SKIP_BLANKS;
7799 while ((CUR == '*') ||
7800 ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) ||
7801 ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) {
7802 int op = -1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007803 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00007804
7805 if (CUR == '*') {
7806 op = 0;
7807 NEXT;
7808 } else if (CUR == 'd') {
7809 op = 1;
7810 SKIP(3);
7811 } else if (CUR == 'm') {
7812 op = 2;
7813 SKIP(3);
7814 }
7815 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007816 xmlXPathCompUnaryExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007817 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007818 PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00007819 SKIP_BLANKS;
7820 }
7821}
7822
7823/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007824 * xmlXPathCompAdditiveExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007825 * @ctxt: the XPath Parser context
7826 *
7827 * [25] AdditiveExpr ::= MultiplicativeExpr
7828 * | AdditiveExpr '+' MultiplicativeExpr
7829 * | AdditiveExpr '-' MultiplicativeExpr
7830 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007831 * Compile an Additive expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007832 */
7833
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007834static void
7835xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007836
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007837 xmlXPathCompMultiplicativeExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007838 CHECK_ERROR;
7839 SKIP_BLANKS;
7840 while ((CUR == '+') || (CUR == '-')) {
7841 int plus;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007842 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00007843
7844 if (CUR == '+') plus = 1;
7845 else plus = 0;
7846 NEXT;
7847 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007848 xmlXPathCompMultiplicativeExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007849 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007850 PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00007851 SKIP_BLANKS;
7852 }
7853}
7854
7855/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007856 * xmlXPathCompRelationalExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007857 * @ctxt: the XPath Parser context
7858 *
7859 * [24] RelationalExpr ::= AdditiveExpr
7860 * | RelationalExpr '<' AdditiveExpr
7861 * | RelationalExpr '>' AdditiveExpr
7862 * | RelationalExpr '<=' AdditiveExpr
7863 * | RelationalExpr '>=' AdditiveExpr
7864 *
7865 * A <= B > C is allowed ? Answer from James, yes with
7866 * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr
7867 * which is basically what got implemented.
7868 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007869 * Compile a Relational expression, then push the result
Owen Taylor3473f882001-02-23 17:55:21 +00007870 * on the stack
7871 */
7872
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007873static void
7874xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) {
7875 xmlXPathCompAdditiveExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007876 CHECK_ERROR;
7877 SKIP_BLANKS;
7878 while ((CUR == '<') ||
7879 (CUR == '>') ||
7880 ((CUR == '<') && (NXT(1) == '=')) ||
7881 ((CUR == '>') && (NXT(1) == '='))) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007882 int inf, strict;
7883 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00007884
7885 if (CUR == '<') inf = 1;
7886 else inf = 0;
7887 if (NXT(1) == '=') strict = 0;
7888 else strict = 1;
7889 NEXT;
7890 if (!strict) NEXT;
7891 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007892 xmlXPathCompAdditiveExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007893 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007894 PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict);
Owen Taylor3473f882001-02-23 17:55:21 +00007895 SKIP_BLANKS;
7896 }
7897}
7898
7899/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007900 * xmlXPathCompEqualityExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007901 * @ctxt: the XPath Parser context
7902 *
7903 * [23] EqualityExpr ::= RelationalExpr
7904 * | EqualityExpr '=' RelationalExpr
7905 * | EqualityExpr '!=' RelationalExpr
7906 *
7907 * A != B != C is allowed ? Answer from James, yes with
7908 * (RelationalExpr = RelationalExpr) = RelationalExpr
7909 * (RelationalExpr != RelationalExpr) != RelationalExpr
7910 * which is basically what got implemented.
7911 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007912 * Compile an Equality expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007913 *
7914 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007915static void
7916xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) {
7917 xmlXPathCompRelationalExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007918 CHECK_ERROR;
7919 SKIP_BLANKS;
7920 while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007921 int eq;
7922 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00007923
7924 if (CUR == '=') eq = 1;
7925 else eq = 0;
7926 NEXT;
7927 if (!eq) NEXT;
7928 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007929 xmlXPathCompRelationalExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007930 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007931 PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00007932 SKIP_BLANKS;
7933 }
7934}
7935
7936/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007937 * xmlXPathCompAndExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007938 * @ctxt: the XPath Parser context
7939 *
7940 * [22] AndExpr ::= EqualityExpr
7941 * | AndExpr 'and' EqualityExpr
7942 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007943 * Compile an AND expression.
Owen Taylor3473f882001-02-23 17:55:21 +00007944 *
7945 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007946static void
7947xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) {
7948 xmlXPathCompEqualityExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007949 CHECK_ERROR;
7950 SKIP_BLANKS;
7951 while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007952 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00007953 SKIP(3);
7954 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007955 xmlXPathCompEqualityExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007956 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007957 PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00007958 SKIP_BLANKS;
7959 }
7960}
7961
7962/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007963 * xmlXPathCompExpr:
Owen Taylor3473f882001-02-23 17:55:21 +00007964 * @ctxt: the XPath Parser context
7965 *
7966 * [14] Expr ::= OrExpr
7967 * [21] OrExpr ::= AndExpr
7968 * | OrExpr 'or' AndExpr
7969 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007970 * Parse and compile an expression
Owen Taylor3473f882001-02-23 17:55:21 +00007971 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007972static void
7973xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt) {
7974 xmlXPathCompAndExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007975 CHECK_ERROR;
7976 SKIP_BLANKS;
7977 while ((CUR == 'o') && (NXT(1) == 'r')) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007978 int op1 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00007979 SKIP(2);
7980 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007981 xmlXPathCompAndExpr(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007982 CHECK_ERROR;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007983 PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0);
7984 op1 = ctxt->comp->nbStep;
Owen Taylor3473f882001-02-23 17:55:21 +00007985 SKIP_BLANKS;
7986 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +00007987 if (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE) {
7988 /* more ops could be optimized too */
7989 PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0);
7990 }
Owen Taylor3473f882001-02-23 17:55:21 +00007991}
7992
7993/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00007994 * xmlXPathCompPredicate:
Owen Taylor3473f882001-02-23 17:55:21 +00007995 * @ctxt: the XPath Parser context
Daniel Veillardd8df6c02001-04-05 16:54:14 +00007996 * @filter: act as a filter
Owen Taylor3473f882001-02-23 17:55:21 +00007997 *
7998 * [8] Predicate ::= '[' PredicateExpr ']'
7999 * [9] PredicateExpr ::= Expr
8000 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008001 * Compile a predicate expression
Owen Taylor3473f882001-02-23 17:55:21 +00008002 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008003static void
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008004xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) {
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008005 int op1 = ctxt->comp->last;
8006
8007 SKIP_BLANKS;
8008 if (CUR != '[') {
8009 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
8010 }
8011 NEXT;
8012 SKIP_BLANKS;
8013
8014 ctxt->comp->last = -1;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008015 xmlXPathCompileExpr(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008016 CHECK_ERROR;
8017
8018 if (CUR != ']') {
8019 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
8020 }
8021
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008022 if (filter)
8023 PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0);
8024 else
8025 PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0);
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008026
8027 NEXT;
8028 SKIP_BLANKS;
8029}
8030
8031/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008032 * xmlXPathCompNodeTest:
Owen Taylor3473f882001-02-23 17:55:21 +00008033 * @ctxt: the XPath Parser context
8034 * @test: pointer to a xmlXPathTestVal
8035 * @type: pointer to a xmlXPathTypeVal
8036 * @prefix: placeholder for a possible name prefix
8037 *
8038 * [7] NodeTest ::= NameTest
8039 * | NodeType '(' ')'
8040 * | 'processing-instruction' '(' Literal ')'
8041 *
8042 * [37] NameTest ::= '*'
8043 * | NCName ':' '*'
8044 * | QName
8045 * [38] NodeType ::= 'comment'
8046 * | 'text'
8047 * | 'processing-instruction'
8048 * | 'node'
8049 *
8050 * Returns the name found and update @test, @type and @prefix appropriately
8051 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008052static xmlChar *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008053xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test,
8054 xmlXPathTypeVal *type, const xmlChar **prefix,
8055 xmlChar *name) {
Owen Taylor3473f882001-02-23 17:55:21 +00008056 int blanks;
8057
8058 if ((test == NULL) || (type == NULL) || (prefix == NULL)) {
8059 STRANGE;
8060 return(NULL);
8061 }
8062 *type = 0;
8063 *test = 0;
8064 *prefix = NULL;
8065 SKIP_BLANKS;
8066
8067 if ((name == NULL) && (CUR == '*')) {
8068 /*
8069 * All elements
8070 */
8071 NEXT;
8072 *test = NODE_TEST_ALL;
8073 return(NULL);
8074 }
8075
8076 if (name == NULL)
8077 name = xmlXPathParseNCName(ctxt);
8078 if (name == NULL) {
8079 XP_ERROR0(XPATH_EXPR_ERROR);
8080 }
8081
8082 blanks = IS_BLANK(CUR);
8083 SKIP_BLANKS;
8084 if (CUR == '(') {
8085 NEXT;
8086 /*
8087 * NodeType or PI search
8088 */
8089 if (xmlStrEqual(name, BAD_CAST "comment"))
8090 *type = NODE_TYPE_COMMENT;
8091 else if (xmlStrEqual(name, BAD_CAST "node"))
8092 *type = NODE_TYPE_NODE;
8093 else if (xmlStrEqual(name, BAD_CAST "processing-instruction"))
8094 *type = NODE_TYPE_PI;
8095 else if (xmlStrEqual(name, BAD_CAST "text"))
8096 *type = NODE_TYPE_TEXT;
8097 else {
8098 if (name != NULL)
8099 xmlFree(name);
8100 XP_ERROR0(XPATH_EXPR_ERROR);
8101 }
8102
8103 *test = NODE_TEST_TYPE;
8104
8105 SKIP_BLANKS;
8106 if (*type == NODE_TYPE_PI) {
8107 /*
8108 * Specific case: search a PI by name.
8109 */
Owen Taylor3473f882001-02-23 17:55:21 +00008110 if (name != NULL)
8111 xmlFree(name);
Daniel Veillard82e49712001-04-26 14:38:03 +00008112 name = NULL;
8113 if (CUR != ')') {
8114 name = xmlXPathParseLiteral(ctxt);
8115 CHECK_ERROR 0;
Daniel Veillarded23b7d2002-05-27 12:16:02 +00008116 *test = NODE_TEST_PI;
Daniel Veillard82e49712001-04-26 14:38:03 +00008117 SKIP_BLANKS;
8118 }
Owen Taylor3473f882001-02-23 17:55:21 +00008119 }
8120 if (CUR != ')') {
8121 if (name != NULL)
8122 xmlFree(name);
8123 XP_ERROR0(XPATH_UNCLOSED_ERROR);
8124 }
8125 NEXT;
8126 return(name);
8127 }
8128 *test = NODE_TEST_NAME;
8129 if ((!blanks) && (CUR == ':')) {
8130 NEXT;
8131
8132 /*
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008133 * Since currently the parser context don't have a
8134 * namespace list associated:
8135 * The namespace name for this prefix can be computed
8136 * only at evaluation time. The compilation is done
8137 * outside of any context.
Owen Taylor3473f882001-02-23 17:55:21 +00008138 */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008139#if 0
Owen Taylor3473f882001-02-23 17:55:21 +00008140 *prefix = xmlXPathNsLookup(ctxt->context, name);
8141 if (name != NULL)
8142 xmlFree(name);
8143 if (*prefix == NULL) {
8144 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
8145 }
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008146#else
8147 *prefix = name;
8148#endif
Owen Taylor3473f882001-02-23 17:55:21 +00008149
8150 if (CUR == '*') {
8151 /*
8152 * All elements
8153 */
8154 NEXT;
8155 *test = NODE_TEST_ALL;
8156 return(NULL);
8157 }
8158
8159 name = xmlXPathParseNCName(ctxt);
8160 if (name == NULL) {
8161 XP_ERROR0(XPATH_EXPR_ERROR);
8162 }
8163 }
8164 return(name);
8165}
8166
8167/**
8168 * xmlXPathIsAxisName:
8169 * @name: a preparsed name token
8170 *
8171 * [6] AxisName ::= 'ancestor'
8172 * | 'ancestor-or-self'
8173 * | 'attribute'
8174 * | 'child'
8175 * | 'descendant'
8176 * | 'descendant-or-self'
8177 * | 'following'
8178 * | 'following-sibling'
8179 * | 'namespace'
8180 * | 'parent'
8181 * | 'preceding'
8182 * | 'preceding-sibling'
8183 * | 'self'
8184 *
8185 * Returns the axis or 0
8186 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008187static xmlXPathAxisVal
Owen Taylor3473f882001-02-23 17:55:21 +00008188xmlXPathIsAxisName(const xmlChar *name) {
8189 xmlXPathAxisVal ret = 0;
8190 switch (name[0]) {
8191 case 'a':
8192 if (xmlStrEqual(name, BAD_CAST "ancestor"))
8193 ret = AXIS_ANCESTOR;
8194 if (xmlStrEqual(name, BAD_CAST "ancestor-or-self"))
8195 ret = AXIS_ANCESTOR_OR_SELF;
8196 if (xmlStrEqual(name, BAD_CAST "attribute"))
8197 ret = AXIS_ATTRIBUTE;
8198 break;
8199 case 'c':
8200 if (xmlStrEqual(name, BAD_CAST "child"))
8201 ret = AXIS_CHILD;
8202 break;
8203 case 'd':
8204 if (xmlStrEqual(name, BAD_CAST "descendant"))
8205 ret = AXIS_DESCENDANT;
8206 if (xmlStrEqual(name, BAD_CAST "descendant-or-self"))
8207 ret = AXIS_DESCENDANT_OR_SELF;
8208 break;
8209 case 'f':
8210 if (xmlStrEqual(name, BAD_CAST "following"))
8211 ret = AXIS_FOLLOWING;
8212 if (xmlStrEqual(name, BAD_CAST "following-sibling"))
8213 ret = AXIS_FOLLOWING_SIBLING;
8214 break;
8215 case 'n':
8216 if (xmlStrEqual(name, BAD_CAST "namespace"))
8217 ret = AXIS_NAMESPACE;
8218 break;
8219 case 'p':
8220 if (xmlStrEqual(name, BAD_CAST "parent"))
8221 ret = AXIS_PARENT;
8222 if (xmlStrEqual(name, BAD_CAST "preceding"))
8223 ret = AXIS_PRECEDING;
8224 if (xmlStrEqual(name, BAD_CAST "preceding-sibling"))
8225 ret = AXIS_PRECEDING_SIBLING;
8226 break;
8227 case 's':
8228 if (xmlStrEqual(name, BAD_CAST "self"))
8229 ret = AXIS_SELF;
8230 break;
8231 }
8232 return(ret);
8233}
8234
8235/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008236 * xmlXPathCompStep:
Owen Taylor3473f882001-02-23 17:55:21 +00008237 * @ctxt: the XPath Parser context
8238 *
8239 * [4] Step ::= AxisSpecifier NodeTest Predicate*
8240 * | AbbreviatedStep
8241 *
8242 * [12] AbbreviatedStep ::= '.' | '..'
8243 *
8244 * [5] AxisSpecifier ::= AxisName '::'
8245 * | AbbreviatedAxisSpecifier
8246 *
8247 * [13] AbbreviatedAxisSpecifier ::= '@'?
8248 *
8249 * Modified for XPtr range support as:
8250 *
8251 * [4xptr] Step ::= AxisSpecifier NodeTest Predicate*
8252 * | AbbreviatedStep
8253 * | 'range-to' '(' Expr ')' Predicate*
8254 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008255 * Compile one step in a Location Path
Owen Taylor3473f882001-02-23 17:55:21 +00008256 * A location step of . is short for self::node(). This is
8257 * particularly useful in conjunction with //. For example, the
8258 * location path .//para is short for
8259 * self::node()/descendant-or-self::node()/child::para
8260 * and so will select all para descendant elements of the context
8261 * node.
8262 * Similarly, a location step of .. is short for parent::node().
8263 * For example, ../title is short for parent::node()/child::title
8264 * and so will select the title children of the parent of the context
8265 * node.
8266 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008267static void
8268xmlXPathCompStep(xmlXPathParserContextPtr ctxt) {
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008269#ifdef LIBXML_XPTR_ENABLED
8270 int rangeto = 0;
8271 int op2 = -1;
8272#endif
8273
Owen Taylor3473f882001-02-23 17:55:21 +00008274 SKIP_BLANKS;
8275 if ((CUR == '.') && (NXT(1) == '.')) {
8276 SKIP(2);
8277 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008278 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT,
8279 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008280 } else if (CUR == '.') {
8281 NEXT;
8282 SKIP_BLANKS;
8283 } else {
8284 xmlChar *name = NULL;
8285 const xmlChar *prefix = NULL;
8286 xmlXPathTestVal test;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008287 xmlXPathAxisVal axis = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00008288 xmlXPathTypeVal type;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008289 int op1;
Owen Taylor3473f882001-02-23 17:55:21 +00008290
8291 /*
8292 * The modification needed for XPointer change to the production
8293 */
8294#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +00008295 if (ctxt->xptr) {
Owen Taylor3473f882001-02-23 17:55:21 +00008296 name = xmlXPathParseNCName(ctxt);
8297 if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) {
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008298 op2 = ctxt->comp->last;
Owen Taylor3473f882001-02-23 17:55:21 +00008299 xmlFree(name);
8300 SKIP_BLANKS;
8301 if (CUR != '(') {
8302 XP_ERROR(XPATH_EXPR_ERROR);
8303 }
8304 NEXT;
8305 SKIP_BLANKS;
8306
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008307 xmlXPathCompileExpr(ctxt);
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008308 /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */
Owen Taylor3473f882001-02-23 17:55:21 +00008309 CHECK_ERROR;
8310
8311 SKIP_BLANKS;
8312 if (CUR != ')') {
8313 XP_ERROR(XPATH_EXPR_ERROR);
8314 }
8315 NEXT;
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008316 rangeto = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00008317 goto eval_predicates;
8318 }
8319 }
8320#endif
Daniel Veillard2156a562001-04-28 12:24:34 +00008321 if (CUR == '*') {
8322 axis = AXIS_CHILD;
8323 } else {
8324 if (name == NULL)
8325 name = xmlXPathParseNCName(ctxt);
8326 if (name != NULL) {
8327 axis = xmlXPathIsAxisName(name);
8328 if (axis != 0) {
8329 SKIP_BLANKS;
8330 if ((CUR == ':') && (NXT(1) == ':')) {
8331 SKIP(2);
8332 xmlFree(name);
8333 name = NULL;
8334 } else {
8335 /* an element name can conflict with an axis one :-\ */
8336 axis = AXIS_CHILD;
8337 }
Owen Taylor3473f882001-02-23 17:55:21 +00008338 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00008339 axis = AXIS_CHILD;
8340 }
Daniel Veillard2156a562001-04-28 12:24:34 +00008341 } else if (CUR == '@') {
8342 NEXT;
8343 axis = AXIS_ATTRIBUTE;
Owen Taylor3473f882001-02-23 17:55:21 +00008344 } else {
Daniel Veillard2156a562001-04-28 12:24:34 +00008345 axis = AXIS_CHILD;
Owen Taylor3473f882001-02-23 17:55:21 +00008346 }
Owen Taylor3473f882001-02-23 17:55:21 +00008347 }
8348
8349 CHECK_ERROR;
8350
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008351 name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name);
Owen Taylor3473f882001-02-23 17:55:21 +00008352 if (test == 0)
8353 return;
8354
8355#ifdef DEBUG_STEP
8356 xmlGenericError(xmlGenericErrorContext,
8357 "Basis : computing new set\n");
8358#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008359
Owen Taylor3473f882001-02-23 17:55:21 +00008360#ifdef DEBUG_STEP
8361 xmlGenericError(xmlGenericErrorContext, "Basis : ");
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008362 if (ctxt->value == NULL)
8363 xmlGenericError(xmlGenericErrorContext, "no value\n");
8364 else if (ctxt->value->nodesetval == NULL)
8365 xmlGenericError(xmlGenericErrorContext, "Empty\n");
8366 else
8367 xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval);
Owen Taylor3473f882001-02-23 17:55:21 +00008368#endif
Owen Taylor3473f882001-02-23 17:55:21 +00008369
8370eval_predicates:
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008371 op1 = ctxt->comp->last;
8372 ctxt->comp->last = -1;
8373
Owen Taylor3473f882001-02-23 17:55:21 +00008374 SKIP_BLANKS;
8375 while (CUR == '[') {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008376 xmlXPathCompPredicate(ctxt, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00008377 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008378
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008379#ifdef LIBXML_XPTR_ENABLED
8380 if (rangeto) {
8381 PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0);
8382 } else
8383#endif
8384 PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis,
8385 test, type, (void *)prefix, (void *)name);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008386
Owen Taylor3473f882001-02-23 17:55:21 +00008387 }
8388#ifdef DEBUG_STEP
8389 xmlGenericError(xmlGenericErrorContext, "Step : ");
Daniel Veillardfd0c3eb2001-04-22 19:13:10 +00008390 if (ctxt->value == NULL)
8391 xmlGenericError(xmlGenericErrorContext, "no value\n");
8392 else if (ctxt->value->nodesetval == NULL)
8393 xmlGenericError(xmlGenericErrorContext, "Empty\n");
8394 else
8395 xmlGenericErrorContextNodeSet(xmlGenericErrorContext,
8396 ctxt->value->nodesetval);
Owen Taylor3473f882001-02-23 17:55:21 +00008397#endif
8398}
8399
8400/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008401 * xmlXPathCompRelativeLocationPath:
Owen Taylor3473f882001-02-23 17:55:21 +00008402 * @ctxt: the XPath Parser context
8403 *
8404 * [3] RelativeLocationPath ::= Step
8405 * | RelativeLocationPath '/' Step
8406 * | AbbreviatedRelativeLocationPath
8407 * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step
8408 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008409 * Compile a relative location path.
Owen Taylor3473f882001-02-23 17:55:21 +00008410 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008411static void
Owen Taylor3473f882001-02-23 17:55:21 +00008412#ifdef VMS
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008413xmlXPathCompRelLocationPath
Owen Taylor3473f882001-02-23 17:55:21 +00008414#else
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008415xmlXPathCompRelativeLocationPath
Owen Taylor3473f882001-02-23 17:55:21 +00008416#endif
8417(xmlXPathParserContextPtr ctxt) {
8418 SKIP_BLANKS;
8419 if ((CUR == '/') && (NXT(1) == '/')) {
8420 SKIP(2);
8421 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008422 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
8423 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008424 } else if (CUR == '/') {
8425 NEXT;
8426 SKIP_BLANKS;
8427 }
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008428 xmlXPathCompStep(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008429 SKIP_BLANKS;
8430 while (CUR == '/') {
8431 if ((CUR == '/') && (NXT(1) == '/')) {
8432 SKIP(2);
8433 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008434 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
Owen Taylor3473f882001-02-23 17:55:21 +00008435 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008436 xmlXPathCompStep(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008437 } else if (CUR == '/') {
8438 NEXT;
8439 SKIP_BLANKS;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008440 xmlXPathCompStep(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008441 }
8442 SKIP_BLANKS;
8443 }
8444}
8445
8446/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008447 * xmlXPathCompLocationPath:
Owen Taylor3473f882001-02-23 17:55:21 +00008448 * @ctxt: the XPath Parser context
8449 *
8450 * [1] LocationPath ::= RelativeLocationPath
8451 * | AbsoluteLocationPath
8452 * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath?
8453 * | AbbreviatedAbsoluteLocationPath
8454 * [10] AbbreviatedAbsoluteLocationPath ::=
8455 * '//' RelativeLocationPath
8456 *
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008457 * Compile a location path
8458 *
Owen Taylor3473f882001-02-23 17:55:21 +00008459 * // is short for /descendant-or-self::node()/. For example,
8460 * //para is short for /descendant-or-self::node()/child::para and
8461 * so will select any para element in the document (even a para element
8462 * that is a document element will be selected by //para since the
8463 * document element node is a child of the root node); div//para is
8464 * short for div/descendant-or-self::node()/child::para and so will
8465 * select all para descendants of div children.
8466 */
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008467static void
8468xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00008469 SKIP_BLANKS;
8470 if (CUR != '/') {
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008471 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008472 } else {
8473 while (CUR == '/') {
8474 if ((CUR == '/') && (NXT(1) == '/')) {
8475 SKIP(2);
8476 SKIP_BLANKS;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008477 PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF,
8478 NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008479 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008480 } else if (CUR == '/') {
8481 NEXT;
Daniel Veillard608ad072001-06-14 08:32:28 +00008482 SKIP_BLANKS;
8483 if ((CUR != 0 ) &&
8484 ((IS_LETTER(CUR)) || (CUR == '_') || (CUR == '.') ||
8485 (CUR == '@') || (CUR == '*')))
Daniel Veillardafcbe1c2001-03-19 10:57:13 +00008486 xmlXPathCompRelativeLocationPath(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008487 }
8488 }
8489 }
8490}
8491
Daniel Veillard9e7160d2001-03-18 23:17:47 +00008492/************************************************************************
8493 * *
8494 * XPath precompiled expression evaluation *
8495 * *
8496 ************************************************************************/
8497
Daniel Veillardf06307e2001-07-03 10:35:50 +00008498static int
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008499xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op);
8500
8501/**
8502 * xmlXPathNodeCollectAndTest:
8503 * @ctxt: the XPath Parser context
8504 * @op: the XPath precompiled step operation
Daniel Veillardf06307e2001-07-03 10:35:50 +00008505 * @first: pointer to the first element in document order
8506 * @last: pointer to the last element in document order
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008507 *
8508 * This is the function implementing a step: based on the current list
8509 * of nodes, it builds up a new list, looking at all nodes under that
8510 * axis and selecting them it also do the predicate filtering
8511 *
8512 * Pushes the new NodeSet resulting from the search.
Daniel Veillardf06307e2001-07-03 10:35:50 +00008513 *
8514 * Returns the number of node traversed
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008515 */
Daniel Veillardf06307e2001-07-03 10:35:50 +00008516static int
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008517xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt,
Daniel Veillardf06307e2001-07-03 10:35:50 +00008518 xmlXPathStepOpPtr op,
8519 xmlNodePtr * first, xmlNodePtr * last)
8520{
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008521 xmlXPathAxisVal axis = op->value;
8522 xmlXPathTestVal test = op->value2;
8523 xmlXPathTypeVal type = op->value3;
8524 const xmlChar *prefix = op->value4;
8525 const xmlChar *name = op->value5;
Daniel Veillarde043ee12001-04-16 14:08:07 +00008526 const xmlChar *URI = NULL;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008527
8528#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008529 int n = 0;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008530#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008531 int i, t = 0;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008532 xmlNodeSetPtr ret, list;
8533 xmlXPathTraversalFunction next = NULL;
Daniel Veillardf06307e2001-07-03 10:35:50 +00008534 void (*addNode) (xmlNodeSetPtr, xmlNodePtr);
Daniel Veillard75be0132002-03-13 10:03:35 +00008535 xmlNodeSetPtr (*mergeNodeSet) (xmlNodeSetPtr, xmlNodeSetPtr);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008536 xmlNodePtr cur = NULL;
8537 xmlXPathObjectPtr obj;
8538 xmlNodeSetPtr nodelist;
8539 xmlNodePtr tmp;
8540
Daniel Veillardf06307e2001-07-03 10:35:50 +00008541 CHECK_TYPE0(XPATH_NODESET);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008542 obj = valuePop(ctxt);
8543 addNode = xmlXPathNodeSetAdd;
Daniel Veillard75be0132002-03-13 10:03:35 +00008544 mergeNodeSet = xmlXPathNodeSetMerge;
Daniel Veillarde043ee12001-04-16 14:08:07 +00008545 if (prefix != NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00008546 URI = xmlXPathNsLookup(ctxt->context, prefix);
8547 if (URI == NULL)
8548 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
Daniel Veillarde043ee12001-04-16 14:08:07 +00008549 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008550#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008551 xmlGenericError(xmlGenericErrorContext, "new step : ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008552#endif
8553 switch (axis) {
8554 case AXIS_ANCESTOR:
8555#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008556 xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008557#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008558 first = NULL;
8559 next = xmlXPathNextAncestor;
8560 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008561 case AXIS_ANCESTOR_OR_SELF:
8562#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008563 xmlGenericError(xmlGenericErrorContext,
8564 "axis 'ancestors-or-self' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008565#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008566 first = NULL;
8567 next = xmlXPathNextAncestorOrSelf;
8568 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008569 case AXIS_ATTRIBUTE:
8570#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008571 xmlGenericError(xmlGenericErrorContext, "axis 'attributes' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008572#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008573 first = NULL;
8574 last = NULL;
8575 next = xmlXPathNextAttribute;
Daniel Veillard75be0132002-03-13 10:03:35 +00008576 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00008577 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008578 case AXIS_CHILD:
8579#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008580 xmlGenericError(xmlGenericErrorContext, "axis 'child' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008581#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008582 last = NULL;
8583 next = xmlXPathNextChild;
Daniel Veillard75be0132002-03-13 10:03:35 +00008584 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00008585 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008586 case AXIS_DESCENDANT:
8587#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008588 xmlGenericError(xmlGenericErrorContext, "axis 'descendant' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008589#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008590 last = NULL;
8591 next = xmlXPathNextDescendant;
8592 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008593 case AXIS_DESCENDANT_OR_SELF:
8594#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008595 xmlGenericError(xmlGenericErrorContext,
8596 "axis 'descendant-or-self' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008597#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008598 last = NULL;
8599 next = xmlXPathNextDescendantOrSelf;
8600 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008601 case AXIS_FOLLOWING:
8602#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008603 xmlGenericError(xmlGenericErrorContext, "axis 'following' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008604#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008605 last = NULL;
8606 next = xmlXPathNextFollowing;
8607 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008608 case AXIS_FOLLOWING_SIBLING:
8609#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008610 xmlGenericError(xmlGenericErrorContext,
8611 "axis 'following-siblings' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008612#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008613 last = NULL;
8614 next = xmlXPathNextFollowingSibling;
8615 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008616 case AXIS_NAMESPACE:
8617#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008618 xmlGenericError(xmlGenericErrorContext, "axis 'namespace' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008619#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008620 first = NULL;
8621 last = NULL;
8622 next = (xmlXPathTraversalFunction) xmlXPathNextNamespace;
Daniel Veillard75be0132002-03-13 10:03:35 +00008623 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00008624 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008625 case AXIS_PARENT:
8626#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008627 xmlGenericError(xmlGenericErrorContext, "axis 'parent' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008628#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008629 first = NULL;
8630 next = xmlXPathNextParent;
8631 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008632 case AXIS_PRECEDING:
8633#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008634 xmlGenericError(xmlGenericErrorContext, "axis 'preceding' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008635#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008636 first = NULL;
8637 next = xmlXPathNextPrecedingInternal;
8638 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008639 case AXIS_PRECEDING_SIBLING:
8640#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008641 xmlGenericError(xmlGenericErrorContext,
8642 "axis 'preceding-sibling' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008643#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008644 first = NULL;
8645 next = xmlXPathNextPrecedingSibling;
8646 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008647 case AXIS_SELF:
8648#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008649 xmlGenericError(xmlGenericErrorContext, "axis 'self' ");
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008650#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008651 first = NULL;
8652 last = NULL;
8653 next = xmlXPathNextSelf;
Daniel Veillard75be0132002-03-13 10:03:35 +00008654 mergeNodeSet = xmlXPathNodeSetMergeUnique;
Daniel Veillardf06307e2001-07-03 10:35:50 +00008655 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008656 }
8657 if (next == NULL)
Daniel Veillardf06307e2001-07-03 10:35:50 +00008658 return(0);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008659
8660 nodelist = obj->nodesetval;
8661 if (nodelist == NULL) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00008662 xmlXPathFreeObject(obj);
8663 valuePush(ctxt, xmlXPathWrapNodeSet(NULL));
8664 return(0);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008665 }
8666 addNode = xmlXPathNodeSetAddUnique;
8667 ret = NULL;
8668#ifdef DEBUG_STEP
8669 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf06307e2001-07-03 10:35:50 +00008670 " context contains %d nodes\n", nodelist->nodeNr);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008671 switch (test) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00008672 case NODE_TEST_NONE:
8673 xmlGenericError(xmlGenericErrorContext,
8674 " searching for none !!!\n");
8675 break;
8676 case NODE_TEST_TYPE:
8677 xmlGenericError(xmlGenericErrorContext,
8678 " searching for type %d\n", type);
8679 break;
8680 case NODE_TEST_PI:
8681 xmlGenericError(xmlGenericErrorContext,
8682 " searching for PI !!!\n");
8683 break;
8684 case NODE_TEST_ALL:
8685 xmlGenericError(xmlGenericErrorContext,
8686 " searching for *\n");
8687 break;
8688 case NODE_TEST_NS:
8689 xmlGenericError(xmlGenericErrorContext,
8690 " searching for namespace %s\n",
8691 prefix);
8692 break;
8693 case NODE_TEST_NAME:
8694 xmlGenericError(xmlGenericErrorContext,
8695 " searching for name %s\n", name);
8696 if (prefix != NULL)
8697 xmlGenericError(xmlGenericErrorContext,
8698 " with namespace %s\n", prefix);
8699 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008700 }
8701 xmlGenericError(xmlGenericErrorContext, "Testing : ");
8702#endif
8703 /*
8704 * 2.3 Node Tests
8705 * - For the attribute axis, the principal node type is attribute.
8706 * - For the namespace axis, the principal node type is namespace.
8707 * - For other axes, the principal node type is element.
8708 *
8709 * A node test * is true for any node of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00008710 * principal node type. For example, child::* will
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008711 * select all element children of the context node
8712 */
8713 tmp = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +00008714 for (i = 0; i < nodelist->nodeNr; i++) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008715 ctxt->context->node = nodelist->nodeTab[i];
8716
Daniel Veillardf06307e2001-07-03 10:35:50 +00008717 cur = NULL;
8718 list = xmlXPathNodeSetCreate(NULL);
8719 do {
8720 cur = next(ctxt, cur);
8721 if (cur == NULL)
8722 break;
8723 if ((first != NULL) && (*first == cur))
8724 break;
8725 if (((t % 256) == 0) &&
8726 (first != NULL) && (*first != NULL) &&
8727 (xmlXPathCmpNodes(*first, cur) >= 0))
8728 break;
8729 if ((last != NULL) && (*last == cur))
8730 break;
8731 if (((t % 256) == 0) &&
8732 (last != NULL) && (*last != NULL) &&
8733 (xmlXPathCmpNodes(cur, *last) >= 0))
8734 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008735 t++;
Daniel Veillardf06307e2001-07-03 10:35:50 +00008736#ifdef DEBUG_STEP
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008737 xmlGenericError(xmlGenericErrorContext, " %s", cur->name);
8738#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008739 switch (test) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008740 case NODE_TEST_NONE:
Daniel Veillardf06307e2001-07-03 10:35:50 +00008741 ctxt->context->node = tmp;
8742 STRANGE return(t);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008743 case NODE_TEST_TYPE:
Daniel Veillardf06307e2001-07-03 10:35:50 +00008744 if ((cur->type == type) ||
8745 ((type == NODE_TYPE_NODE) &&
8746 ((cur->type == XML_DOCUMENT_NODE) ||
8747 (cur->type == XML_HTML_DOCUMENT_NODE) ||
8748 (cur->type == XML_ELEMENT_NODE) ||
Aleksey Saninf8cb6dd2002-06-04 04:27:06 +00008749 (cur->type == XML_NAMESPACE_DECL) ||
8750 (cur->type == XML_ATTRIBUTE_NODE) ||
Daniel Veillardf06307e2001-07-03 10:35:50 +00008751 (cur->type == XML_PI_NODE) ||
8752 (cur->type == XML_COMMENT_NODE) ||
8753 (cur->type == XML_CDATA_SECTION_NODE) ||
Daniel Veillard7583a592001-07-08 13:15:55 +00008754 (cur->type == XML_TEXT_NODE))) ||
8755 ((type == NODE_TYPE_TEXT) &&
8756 (cur->type == XML_CDATA_SECTION_NODE))) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008757#ifdef DEBUG_STEP
8758 n++;
8759#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008760 addNode(list, cur);
8761 }
8762 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008763 case NODE_TEST_PI:
Daniel Veillardf06307e2001-07-03 10:35:50 +00008764 if (cur->type == XML_PI_NODE) {
8765 if ((name != NULL) &&
8766 (!xmlStrEqual(name, cur->name)))
8767 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008768#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008769 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008770#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008771 addNode(list, cur);
8772 }
8773 break;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008774 case NODE_TEST_ALL:
Daniel Veillardf06307e2001-07-03 10:35:50 +00008775 if (axis == AXIS_ATTRIBUTE) {
8776 if (cur->type == XML_ATTRIBUTE_NODE) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008777#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008778 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008779#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008780 addNode(list, cur);
8781 }
8782 } else if (axis == AXIS_NAMESPACE) {
8783 if (cur->type == XML_NAMESPACE_DECL) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008784#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008785 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008786#endif
Daniel Veillard044fc6b2002-03-04 17:09:44 +00008787 xmlXPathNodeSetAddNs(list, ctxt->context->node,
8788 (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00008789 }
8790 } else {
8791 if (cur->type == XML_ELEMENT_NODE) {
8792 if (prefix == NULL) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008793#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008794 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008795#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008796 addNode(list, cur);
8797 } else if ((cur->ns != NULL) &&
8798 (xmlStrEqual(URI, cur->ns->href))) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008799#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008800 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008801#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008802 addNode(list, cur);
8803 }
8804 }
8805 }
8806 break;
8807 case NODE_TEST_NS:{
8808 TODO;
8809 break;
8810 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008811 case NODE_TEST_NAME:
Daniel Veillardf06307e2001-07-03 10:35:50 +00008812 switch (cur->type) {
8813 case XML_ELEMENT_NODE:
8814 if (xmlStrEqual(name, cur->name)) {
8815 if (prefix == NULL) {
8816 if (cur->ns == NULL) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008817#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008818 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008819#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008820 addNode(list, cur);
8821 }
8822 } else {
8823 if ((cur->ns != NULL) &&
8824 (xmlStrEqual(URI,
8825 cur->ns->href))) {
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008826#ifdef DEBUG_STEP
Daniel Veillardf06307e2001-07-03 10:35:50 +00008827 n++;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008828#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00008829 addNode(list, cur);
8830 }
8831 }
8832 }
8833 break;
8834 case XML_ATTRIBUTE_NODE:{
8835 xmlAttrPtr attr = (xmlAttrPtr) cur;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008836
Daniel Veillardf06307e2001-07-03 10:35:50 +00008837 if (xmlStrEqual(name, attr->name)) {
8838 if (prefix == NULL) {
8839 if ((attr->ns == NULL) ||
8840 (attr->ns->prefix == NULL)) {
8841#ifdef DEBUG_STEP
8842 n++;
8843#endif
8844 addNode(list,
8845 (xmlNodePtr) attr);
8846 }
8847 } else {
8848 if ((attr->ns != NULL) &&
8849 (xmlStrEqual(URI,
8850 attr->ns->
8851 href))) {
8852#ifdef DEBUG_STEP
8853 n++;
8854#endif
8855 addNode(list,
8856 (xmlNodePtr) attr);
8857 }
8858 }
8859 }
8860 break;
8861 }
8862 case XML_NAMESPACE_DECL:
8863 if (cur->type == XML_NAMESPACE_DECL) {
8864 xmlNsPtr ns = (xmlNsPtr) cur;
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008865
Daniel Veillardf06307e2001-07-03 10:35:50 +00008866 if ((ns->prefix != NULL) && (name != NULL)
8867 && (xmlStrEqual(ns->prefix, name))) {
8868#ifdef DEBUG_STEP
8869 n++;
8870#endif
Daniel Veillard044fc6b2002-03-04 17:09:44 +00008871 xmlXPathNodeSetAddNs(list,
8872 ctxt->context->node, (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00008873 }
8874 }
8875 break;
8876 default:
8877 break;
8878 }
8879 break;
8880 break;
8881 }
8882 } while (cur != NULL);
8883
8884 /*
8885 * If there is some predicate filtering do it now
8886 */
Daniel Veillard6fbcf422002-03-21 12:32:59 +00008887 if ((op->ch2 != -1) && (list != NULL) && (list->nodeNr > 0)) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00008888 xmlXPathObjectPtr obj2;
8889
8890 valuePush(ctxt, xmlXPathWrapNodeSet(list));
8891 xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch2]);
8892 CHECK_TYPE0(XPATH_NODESET);
8893 obj2 = valuePop(ctxt);
8894 list = obj2->nodesetval;
8895 obj2->nodesetval = NULL;
8896 xmlXPathFreeObject(obj2);
8897 }
8898 if (ret == NULL) {
8899 ret = list;
8900 } else {
Daniel Veillard75be0132002-03-13 10:03:35 +00008901 ret = mergeNodeSet(ret, list);
Daniel Veillardf06307e2001-07-03 10:35:50 +00008902 xmlXPathFreeNodeSet(list);
8903 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008904 }
8905 ctxt->context->node = tmp;
8906#ifdef DEBUG_STEP
8907 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf06307e2001-07-03 10:35:50 +00008908 "\nExamined %d nodes, found %d nodes at that step\n",
8909 t, n);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008910#endif
Daniel Veillardd8df6c02001-04-05 16:54:14 +00008911 valuePush(ctxt, xmlXPathWrapNodeSet(ret));
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00008912 if ((obj->boolval) && (obj->user != NULL)) {
8913 ctxt->value->boolval = 1;
8914 ctxt->value->user = obj->user;
8915 obj->user = NULL;
8916 obj->boolval = 0;
8917 }
8918 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00008919 return(t);
8920}
8921
8922/**
8923 * xmlXPathNodeCollectAndTestNth:
8924 * @ctxt: the XPath Parser context
8925 * @op: the XPath precompiled step operation
8926 * @indx: the index to collect
8927 * @first: pointer to the first element in document order
8928 * @last: pointer to the last element in document order
8929 *
8930 * This is the function implementing a step: based on the current list
8931 * of nodes, it builds up a new list, looking at all nodes under that
8932 * axis and selecting them it also do the predicate filtering
8933 *
8934 * Pushes the new NodeSet resulting from the search.
8935 * Returns the number of node traversed
8936 */
8937static int
8938xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt,
8939 xmlXPathStepOpPtr op, int indx,
8940 xmlNodePtr * first, xmlNodePtr * last)
8941{
8942 xmlXPathAxisVal axis = op->value;
8943 xmlXPathTestVal test = op->value2;
8944 xmlXPathTypeVal type = op->value3;
8945 const xmlChar *prefix = op->value4;
8946 const xmlChar *name = op->value5;
8947 const xmlChar *URI = NULL;
8948 int n = 0, t = 0;
8949
8950 int i;
8951 xmlNodeSetPtr list;
8952 xmlXPathTraversalFunction next = NULL;
8953 void (*addNode) (xmlNodeSetPtr, xmlNodePtr);
8954 xmlNodePtr cur = NULL;
8955 xmlXPathObjectPtr obj;
8956 xmlNodeSetPtr nodelist;
8957 xmlNodePtr tmp;
8958
8959 CHECK_TYPE0(XPATH_NODESET);
8960 obj = valuePop(ctxt);
8961 addNode = xmlXPathNodeSetAdd;
8962 if (prefix != NULL) {
8963 URI = xmlXPathNsLookup(ctxt->context, prefix);
8964 if (URI == NULL)
8965 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR);
8966 }
8967#ifdef DEBUG_STEP_NTH
8968 xmlGenericError(xmlGenericErrorContext, "new step : ");
8969 if (first != NULL) {
8970 if (*first != NULL)
8971 xmlGenericError(xmlGenericErrorContext, "first = %s ",
8972 (*first)->name);
8973 else
8974 xmlGenericError(xmlGenericErrorContext, "first = NULL ");
8975 }
8976 if (last != NULL) {
8977 if (*last != NULL)
8978 xmlGenericError(xmlGenericErrorContext, "last = %s ",
8979 (*last)->name);
8980 else
8981 xmlGenericError(xmlGenericErrorContext, "last = NULL ");
8982 }
8983#endif
8984 switch (axis) {
8985 case AXIS_ANCESTOR:
8986#ifdef DEBUG_STEP_NTH
8987 xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' ");
8988#endif
8989 first = NULL;
8990 next = xmlXPathNextAncestor;
8991 break;
8992 case AXIS_ANCESTOR_OR_SELF:
8993#ifdef DEBUG_STEP_NTH
8994 xmlGenericError(xmlGenericErrorContext,
8995 "axis 'ancestors-or-self' ");
8996#endif
8997 first = NULL;
8998 next = xmlXPathNextAncestorOrSelf;
8999 break;
9000 case AXIS_ATTRIBUTE:
9001#ifdef DEBUG_STEP_NTH
9002 xmlGenericError(xmlGenericErrorContext, "axis 'attributes' ");
9003#endif
9004 first = NULL;
9005 last = NULL;
9006 next = xmlXPathNextAttribute;
9007 break;
9008 case AXIS_CHILD:
9009#ifdef DEBUG_STEP_NTH
9010 xmlGenericError(xmlGenericErrorContext, "axis 'child' ");
9011#endif
9012 last = NULL;
9013 next = xmlXPathNextChild;
9014 break;
9015 case AXIS_DESCENDANT:
9016#ifdef DEBUG_STEP_NTH
9017 xmlGenericError(xmlGenericErrorContext, "axis 'descendant' ");
9018#endif
9019 last = NULL;
9020 next = xmlXPathNextDescendant;
9021 break;
9022 case AXIS_DESCENDANT_OR_SELF:
9023#ifdef DEBUG_STEP_NTH
9024 xmlGenericError(xmlGenericErrorContext,
9025 "axis 'descendant-or-self' ");
9026#endif
9027 last = NULL;
9028 next = xmlXPathNextDescendantOrSelf;
9029 break;
9030 case AXIS_FOLLOWING:
9031#ifdef DEBUG_STEP_NTH
9032 xmlGenericError(xmlGenericErrorContext, "axis 'following' ");
9033#endif
9034 last = NULL;
9035 next = xmlXPathNextFollowing;
9036 break;
9037 case AXIS_FOLLOWING_SIBLING:
9038#ifdef DEBUG_STEP_NTH
9039 xmlGenericError(xmlGenericErrorContext,
9040 "axis 'following-siblings' ");
9041#endif
9042 last = NULL;
9043 next = xmlXPathNextFollowingSibling;
9044 break;
9045 case AXIS_NAMESPACE:
9046#ifdef DEBUG_STEP_NTH
9047 xmlGenericError(xmlGenericErrorContext, "axis 'namespace' ");
9048#endif
9049 last = NULL;
9050 first = NULL;
9051 next = (xmlXPathTraversalFunction) xmlXPathNextNamespace;
9052 break;
9053 case AXIS_PARENT:
9054#ifdef DEBUG_STEP_NTH
9055 xmlGenericError(xmlGenericErrorContext, "axis 'parent' ");
9056#endif
9057 first = NULL;
9058 next = xmlXPathNextParent;
9059 break;
9060 case AXIS_PRECEDING:
9061#ifdef DEBUG_STEP_NTH
9062 xmlGenericError(xmlGenericErrorContext, "axis 'preceding' ");
9063#endif
9064 first = NULL;
9065 next = xmlXPathNextPrecedingInternal;
9066 break;
9067 case AXIS_PRECEDING_SIBLING:
9068#ifdef DEBUG_STEP_NTH
9069 xmlGenericError(xmlGenericErrorContext,
9070 "axis 'preceding-sibling' ");
9071#endif
9072 first = NULL;
9073 next = xmlXPathNextPrecedingSibling;
9074 break;
9075 case AXIS_SELF:
9076#ifdef DEBUG_STEP_NTH
9077 xmlGenericError(xmlGenericErrorContext, "axis 'self' ");
9078#endif
9079 first = NULL;
9080 last = NULL;
9081 next = xmlXPathNextSelf;
9082 break;
9083 }
9084 if (next == NULL)
9085 return(0);
9086
9087 nodelist = obj->nodesetval;
9088 if (nodelist == NULL) {
9089 xmlXPathFreeObject(obj);
9090 valuePush(ctxt, xmlXPathWrapNodeSet(NULL));
9091 return(0);
9092 }
9093 addNode = xmlXPathNodeSetAddUnique;
9094#ifdef DEBUG_STEP_NTH
9095 xmlGenericError(xmlGenericErrorContext,
9096 " context contains %d nodes\n", nodelist->nodeNr);
9097 switch (test) {
9098 case NODE_TEST_NONE:
9099 xmlGenericError(xmlGenericErrorContext,
9100 " searching for none !!!\n");
9101 break;
9102 case NODE_TEST_TYPE:
9103 xmlGenericError(xmlGenericErrorContext,
9104 " searching for type %d\n", type);
9105 break;
9106 case NODE_TEST_PI:
9107 xmlGenericError(xmlGenericErrorContext,
9108 " searching for PI !!!\n");
9109 break;
9110 case NODE_TEST_ALL:
9111 xmlGenericError(xmlGenericErrorContext,
9112 " searching for *\n");
9113 break;
9114 case NODE_TEST_NS:
9115 xmlGenericError(xmlGenericErrorContext,
9116 " searching for namespace %s\n",
9117 prefix);
9118 break;
9119 case NODE_TEST_NAME:
9120 xmlGenericError(xmlGenericErrorContext,
9121 " searching for name %s\n", name);
9122 if (prefix != NULL)
9123 xmlGenericError(xmlGenericErrorContext,
9124 " with namespace %s\n", prefix);
9125 break;
9126 }
9127 xmlGenericError(xmlGenericErrorContext, "Testing : ");
9128#endif
9129 /*
9130 * 2.3 Node Tests
9131 * - For the attribute axis, the principal node type is attribute.
9132 * - For the namespace axis, the principal node type is namespace.
9133 * - For other axes, the principal node type is element.
9134 *
9135 * A node test * is true for any node of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009136 * principal node type. For example, child::* will
Daniel Veillardf06307e2001-07-03 10:35:50 +00009137 * select all element children of the context node
9138 */
9139 tmp = ctxt->context->node;
9140 list = xmlXPathNodeSetCreate(NULL);
9141 for (i = 0; i < nodelist->nodeNr; i++) {
9142 ctxt->context->node = nodelist->nodeTab[i];
9143
9144 cur = NULL;
9145 n = 0;
9146 do {
9147 cur = next(ctxt, cur);
9148 if (cur == NULL)
9149 break;
9150 if ((first != NULL) && (*first == cur))
9151 break;
9152 if (((t % 256) == 0) &&
9153 (first != NULL) && (*first != NULL) &&
9154 (xmlXPathCmpNodes(*first, cur) >= 0))
9155 break;
9156 if ((last != NULL) && (*last == cur))
9157 break;
9158 if (((t % 256) == 0) &&
9159 (last != NULL) && (*last != NULL) &&
9160 (xmlXPathCmpNodes(cur, *last) >= 0))
9161 break;
9162 t++;
9163 switch (test) {
9164 case NODE_TEST_NONE:
9165 ctxt->context->node = tmp;
9166 STRANGE return(0);
9167 case NODE_TEST_TYPE:
9168 if ((cur->type == type) ||
9169 ((type == NODE_TYPE_NODE) &&
9170 ((cur->type == XML_DOCUMENT_NODE) ||
9171 (cur->type == XML_HTML_DOCUMENT_NODE) ||
9172 (cur->type == XML_ELEMENT_NODE) ||
9173 (cur->type == XML_PI_NODE) ||
9174 (cur->type == XML_COMMENT_NODE) ||
9175 (cur->type == XML_CDATA_SECTION_NODE) ||
9176 (cur->type == XML_TEXT_NODE)))) {
9177 n++;
9178 if (n == indx)
9179 addNode(list, cur);
9180 }
9181 break;
9182 case NODE_TEST_PI:
9183 if (cur->type == XML_PI_NODE) {
9184 if ((name != NULL) &&
9185 (!xmlStrEqual(name, cur->name)))
9186 break;
9187 n++;
9188 if (n == indx)
9189 addNode(list, cur);
9190 }
9191 break;
9192 case NODE_TEST_ALL:
9193 if (axis == AXIS_ATTRIBUTE) {
9194 if (cur->type == XML_ATTRIBUTE_NODE) {
9195 n++;
9196 if (n == indx)
9197 addNode(list, cur);
9198 }
9199 } else if (axis == AXIS_NAMESPACE) {
9200 if (cur->type == XML_NAMESPACE_DECL) {
9201 n++;
9202 if (n == indx)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009203 xmlXPathNodeSetAddNs(list, ctxt->context->node,
9204 (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009205 }
9206 } else {
9207 if (cur->type == XML_ELEMENT_NODE) {
9208 if (prefix == NULL) {
9209 n++;
9210 if (n == indx)
9211 addNode(list, cur);
9212 } else if ((cur->ns != NULL) &&
9213 (xmlStrEqual(URI, cur->ns->href))) {
9214 n++;
9215 if (n == indx)
9216 addNode(list, cur);
9217 }
9218 }
9219 }
9220 break;
9221 case NODE_TEST_NS:{
9222 TODO;
9223 break;
9224 }
9225 case NODE_TEST_NAME:
9226 switch (cur->type) {
9227 case XML_ELEMENT_NODE:
9228 if (xmlStrEqual(name, cur->name)) {
9229 if (prefix == NULL) {
9230 if (cur->ns == NULL) {
9231 n++;
9232 if (n == indx)
9233 addNode(list, cur);
9234 }
9235 } else {
9236 if ((cur->ns != NULL) &&
9237 (xmlStrEqual(URI,
9238 cur->ns->href))) {
9239 n++;
9240 if (n == indx)
9241 addNode(list, cur);
9242 }
9243 }
9244 }
9245 break;
9246 case XML_ATTRIBUTE_NODE:{
9247 xmlAttrPtr attr = (xmlAttrPtr) cur;
9248
9249 if (xmlStrEqual(name, attr->name)) {
9250 if (prefix == NULL) {
9251 if ((attr->ns == NULL) ||
9252 (attr->ns->prefix == NULL)) {
9253 n++;
9254 if (n == indx)
9255 addNode(list, cur);
9256 }
9257 } else {
9258 if ((attr->ns != NULL) &&
9259 (xmlStrEqual(URI,
9260 attr->ns->
9261 href))) {
9262 n++;
9263 if (n == indx)
9264 addNode(list, cur);
9265 }
9266 }
9267 }
9268 break;
9269 }
9270 case XML_NAMESPACE_DECL:
9271 if (cur->type == XML_NAMESPACE_DECL) {
9272 xmlNsPtr ns = (xmlNsPtr) cur;
9273
9274 if ((ns->prefix != NULL) && (name != NULL)
9275 && (xmlStrEqual(ns->prefix, name))) {
9276 n++;
9277 if (n == indx)
Daniel Veillard044fc6b2002-03-04 17:09:44 +00009278 xmlXPathNodeSetAddNs(list,
9279 ctxt->context->node, (xmlNsPtr) cur);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009280 }
9281 }
9282 break;
9283 default:
9284 break;
9285 }
9286 break;
9287 break;
9288 }
9289 } while (n < indx);
9290 }
9291 ctxt->context->node = tmp;
9292#ifdef DEBUG_STEP_NTH
9293 xmlGenericError(xmlGenericErrorContext,
9294 "\nExamined %d nodes, found %d nodes at that step\n",
9295 t, list->nodeNr);
9296#endif
Daniel Veillardf06307e2001-07-03 10:35:50 +00009297 valuePush(ctxt, xmlXPathWrapNodeSet(list));
Daniel Veillard0ab5cab2001-08-14 16:43:10 +00009298 if ((obj->boolval) && (obj->user != NULL)) {
9299 ctxt->value->boolval = 1;
9300 ctxt->value->user = obj->user;
9301 obj->user = NULL;
9302 obj->boolval = 0;
9303 }
9304 xmlXPathFreeObject(obj);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009305 return(t);
9306}
9307
9308/**
9309 * xmlXPathCompOpEvalFirst:
9310 * @ctxt: the XPath parser context with the compiled expression
9311 * @op: an XPath compiled operation
9312 * @first: the first elem found so far
9313 *
9314 * Evaluate the Precompiled XPath operation searching only the first
9315 * element in document order
9316 *
9317 * Returns the number of examined objects.
9318 */
9319static int
9320xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt,
9321 xmlXPathStepOpPtr op, xmlNodePtr * first)
9322{
9323 int total = 0, cur;
9324 xmlXPathCompExprPtr comp;
9325 xmlXPathObjectPtr arg1, arg2;
9326
Daniel Veillard556c6682001-10-06 09:59:51 +00009327 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009328 comp = ctxt->comp;
9329 switch (op->op) {
9330 case XPATH_OP_END:
9331 return (0);
9332 case XPATH_OP_UNION:
9333 total =
9334 xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1],
9335 first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009336 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009337 if ((ctxt->value != NULL)
9338 && (ctxt->value->type == XPATH_NODESET)
9339 && (ctxt->value->nodesetval != NULL)
9340 && (ctxt->value->nodesetval->nodeNr >= 1)) {
9341 /*
9342 * limit tree traversing to first node in the result
9343 */
9344 xmlXPathNodeSetSort(ctxt->value->nodesetval);
9345 *first = ctxt->value->nodesetval->nodeTab[0];
9346 }
9347 cur =
9348 xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2],
9349 first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009350 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009351 CHECK_TYPE0(XPATH_NODESET);
9352 arg2 = valuePop(ctxt);
9353
9354 CHECK_TYPE0(XPATH_NODESET);
9355 arg1 = valuePop(ctxt);
9356
9357 arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
9358 arg2->nodesetval);
9359 valuePush(ctxt, arg1);
9360 xmlXPathFreeObject(arg2);
9361 /* optimizer */
9362 if (total > cur)
9363 xmlXPathCompSwap(op);
9364 return (total + cur);
9365 case XPATH_OP_ROOT:
9366 xmlXPathRoot(ctxt);
9367 return (0);
9368 case XPATH_OP_NODE:
9369 if (op->ch1 != -1)
9370 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009371 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009372 if (op->ch2 != -1)
9373 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009374 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009375 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
9376 return (total);
9377 case XPATH_OP_RESET:
9378 if (op->ch1 != -1)
9379 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009380 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009381 if (op->ch2 != -1)
9382 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009383 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009384 ctxt->context->node = NULL;
9385 return (total);
9386 case XPATH_OP_COLLECT:{
9387 if (op->ch1 == -1)
9388 return (total);
9389
9390 total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009391 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009392
9393 /*
9394 * Optimization for [n] selection where n is a number
9395 */
9396 if ((op->ch2 != -1) &&
9397 (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) &&
9398 (comp->steps[op->ch2].ch1 == -1) &&
9399 (comp->steps[op->ch2].ch2 != -1) &&
9400 (comp->steps[comp->steps[op->ch2].ch2].op ==
9401 XPATH_OP_VALUE)) {
9402 xmlXPathObjectPtr val;
9403
9404 val = comp->steps[comp->steps[op->ch2].ch2].value4;
9405 if ((val != NULL) && (val->type == XPATH_NUMBER)) {
9406 int indx = (int) val->floatval;
9407
9408 if (val->floatval == (float) indx) {
9409 xmlXPathNodeCollectAndTestNth(ctxt, op, indx,
9410 first, NULL);
9411 return (total);
9412 }
9413 }
9414 }
9415 total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL);
9416 return (total);
9417 }
9418 case XPATH_OP_VALUE:
9419 valuePush(ctxt,
9420 xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4));
9421 return (0);
9422 case XPATH_OP_SORT:
9423 if (op->ch1 != -1)
9424 total +=
9425 xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1],
9426 first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009427 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009428 if ((ctxt->value != NULL)
9429 && (ctxt->value->type == XPATH_NODESET)
9430 && (ctxt->value->nodesetval != NULL))
9431 xmlXPathNodeSetSort(ctxt->value->nodesetval);
9432 return (total);
9433 default:
9434 return (xmlXPathCompOpEval(ctxt, op));
9435 }
9436}
9437
9438/**
9439 * xmlXPathCompOpEvalLast:
9440 * @ctxt: the XPath parser context with the compiled expression
9441 * @op: an XPath compiled operation
9442 * @last: the last elem found so far
9443 *
9444 * Evaluate the Precompiled XPath operation searching only the last
9445 * element in document order
9446 *
9447 * Returns the number of node traversed
9448 */
9449static int
9450xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op,
9451 xmlNodePtr * last)
9452{
9453 int total = 0, cur;
9454 xmlXPathCompExprPtr comp;
9455 xmlXPathObjectPtr arg1, arg2;
9456
Daniel Veillard556c6682001-10-06 09:59:51 +00009457 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009458 comp = ctxt->comp;
9459 switch (op->op) {
9460 case XPATH_OP_END:
9461 return (0);
9462 case XPATH_OP_UNION:
9463 total =
9464 xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last);
Daniel Veillard556c6682001-10-06 09:59:51 +00009465 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009466 if ((ctxt->value != NULL)
9467 && (ctxt->value->type == XPATH_NODESET)
9468 && (ctxt->value->nodesetval != NULL)
9469 && (ctxt->value->nodesetval->nodeNr >= 1)) {
9470 /*
9471 * limit tree traversing to first node in the result
9472 */
9473 xmlXPathNodeSetSort(ctxt->value->nodesetval);
9474 *last =
9475 ctxt->value->nodesetval->nodeTab[ctxt->value->
9476 nodesetval->nodeNr -
9477 1];
9478 }
9479 cur =
9480 xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last);
Daniel Veillard556c6682001-10-06 09:59:51 +00009481 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009482 if ((ctxt->value != NULL)
9483 && (ctxt->value->type == XPATH_NODESET)
9484 && (ctxt->value->nodesetval != NULL)
9485 && (ctxt->value->nodesetval->nodeNr >= 1)) {
9486 }
9487 CHECK_TYPE0(XPATH_NODESET);
9488 arg2 = valuePop(ctxt);
9489
9490 CHECK_TYPE0(XPATH_NODESET);
9491 arg1 = valuePop(ctxt);
9492
9493 arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
9494 arg2->nodesetval);
9495 valuePush(ctxt, arg1);
9496 xmlXPathFreeObject(arg2);
9497 /* optimizer */
9498 if (total > cur)
9499 xmlXPathCompSwap(op);
9500 return (total + cur);
9501 case XPATH_OP_ROOT:
9502 xmlXPathRoot(ctxt);
9503 return (0);
9504 case XPATH_OP_NODE:
9505 if (op->ch1 != -1)
9506 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009507 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009508 if (op->ch2 != -1)
9509 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009510 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009511 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
9512 return (total);
9513 case XPATH_OP_RESET:
9514 if (op->ch1 != -1)
9515 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009516 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009517 if (op->ch2 != -1)
9518 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009519 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009520 ctxt->context->node = NULL;
9521 return (total);
9522 case XPATH_OP_COLLECT:{
9523 if (op->ch1 == -1)
9524 return (0);
9525
9526 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009527 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009528
9529 /*
9530 * Optimization for [n] selection where n is a number
9531 */
9532 if ((op->ch2 != -1) &&
9533 (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) &&
9534 (comp->steps[op->ch2].ch1 == -1) &&
9535 (comp->steps[op->ch2].ch2 != -1) &&
9536 (comp->steps[comp->steps[op->ch2].ch2].op ==
9537 XPATH_OP_VALUE)) {
9538 xmlXPathObjectPtr val;
9539
9540 val = comp->steps[comp->steps[op->ch2].ch2].value4;
9541 if ((val != NULL) && (val->type == XPATH_NUMBER)) {
9542 int indx = (int) val->floatval;
9543
9544 if (val->floatval == (float) indx) {
9545 total +=
9546 xmlXPathNodeCollectAndTestNth(ctxt, op,
9547 indx, NULL,
9548 last);
9549 return (total);
9550 }
9551 }
9552 }
9553 total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last);
9554 return (total);
9555 }
9556 case XPATH_OP_VALUE:
9557 valuePush(ctxt,
9558 xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4));
9559 return (0);
9560 case XPATH_OP_SORT:
9561 if (op->ch1 != -1)
9562 total +=
9563 xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1],
9564 last);
Daniel Veillard556c6682001-10-06 09:59:51 +00009565 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009566 if ((ctxt->value != NULL)
9567 && (ctxt->value->type == XPATH_NODESET)
9568 && (ctxt->value->nodesetval != NULL))
9569 xmlXPathNodeSetSort(ctxt->value->nodesetval);
9570 return (total);
9571 default:
9572 return (xmlXPathCompOpEval(ctxt, op));
9573 }
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009574}
9575
Owen Taylor3473f882001-02-23 17:55:21 +00009576/**
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009577 * xmlXPathCompOpEval:
9578 * @ctxt: the XPath parser context with the compiled expression
9579 * @op: an XPath compiled operation
9580 *
9581 * Evaluate the Precompiled XPath operation
Daniel Veillardf06307e2001-07-03 10:35:50 +00009582 * Returns the number of node traversed
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009583 */
Daniel Veillardf06307e2001-07-03 10:35:50 +00009584static int
9585xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op)
9586{
9587 int total = 0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009588 int equal, ret;
9589 xmlXPathCompExprPtr comp;
9590 xmlXPathObjectPtr arg1, arg2;
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009591 xmlNodePtr bak;
9592 xmlDocPtr bakd;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009593
Daniel Veillard556c6682001-10-06 09:59:51 +00009594 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009595 comp = ctxt->comp;
9596 switch (op->op) {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009597 case XPATH_OP_END:
9598 return (0);
9599 case XPATH_OP_AND:
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009600 bakd = ctxt->context->doc;
9601 bak = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009602 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009603 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009604 xmlXPathBooleanFunction(ctxt, 1);
9605 if ((ctxt->value == NULL) || (ctxt->value->boolval == 0))
9606 return (total);
9607 arg2 = valuePop(ctxt);
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009608 ctxt->context->doc = bakd;
9609 ctxt->context->node = bak;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009610 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009611 if (ctxt->error) {
9612 xmlXPathFreeObject(arg2);
9613 return(0);
9614 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00009615 xmlXPathBooleanFunction(ctxt, 1);
9616 arg1 = valuePop(ctxt);
9617 arg1->boolval &= arg2->boolval;
9618 valuePush(ctxt, arg1);
9619 xmlXPathFreeObject(arg2);
9620 return (total);
9621 case XPATH_OP_OR:
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009622 bakd = ctxt->context->doc;
9623 bak = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009624 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009625 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009626 xmlXPathBooleanFunction(ctxt, 1);
9627 if ((ctxt->value == NULL) || (ctxt->value->boolval == 1))
9628 return (total);
9629 arg2 = valuePop(ctxt);
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009630 ctxt->context->doc = bakd;
9631 ctxt->context->node = bak;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009632 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009633 if (ctxt->error) {
9634 xmlXPathFreeObject(arg2);
9635 return(0);
9636 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00009637 xmlXPathBooleanFunction(ctxt, 1);
9638 arg1 = valuePop(ctxt);
9639 arg1->boolval |= arg2->boolval;
9640 valuePush(ctxt, arg1);
9641 xmlXPathFreeObject(arg2);
9642 return (total);
9643 case XPATH_OP_EQUAL:
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009644 bakd = ctxt->context->doc;
9645 bak = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009646 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009647 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009648 ctxt->context->doc = bakd;
9649 ctxt->context->node = bak;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009650 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009651 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009652 equal = xmlXPathEqualValues(ctxt);
9653 if (op->value)
9654 valuePush(ctxt, xmlXPathNewBoolean(equal));
9655 else
9656 valuePush(ctxt, xmlXPathNewBoolean(!equal));
9657 return (total);
9658 case XPATH_OP_CMP:
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009659 bakd = ctxt->context->doc;
9660 bak = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009661 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009662 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009663 ctxt->context->doc = bakd;
9664 ctxt->context->node = bak;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009665 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009666 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009667 ret = xmlXPathCompareValues(ctxt, op->value, op->value2);
9668 valuePush(ctxt, xmlXPathNewBoolean(ret));
9669 return (total);
9670 case XPATH_OP_PLUS:
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009671 bakd = ctxt->context->doc;
9672 bak = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009673 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009674 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009675 if (op->ch2 != -1) {
9676 ctxt->context->doc = bakd;
9677 ctxt->context->node = bak;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009678 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009679 }
Daniel Veillard556c6682001-10-06 09:59:51 +00009680 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009681 if (op->value == 0)
9682 xmlXPathSubValues(ctxt);
9683 else if (op->value == 1)
9684 xmlXPathAddValues(ctxt);
9685 else if (op->value == 2)
9686 xmlXPathValueFlipSign(ctxt);
9687 else if (op->value == 3) {
9688 CAST_TO_NUMBER;
9689 CHECK_TYPE0(XPATH_NUMBER);
9690 }
9691 return (total);
9692 case XPATH_OP_MULT:
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009693 bakd = ctxt->context->doc;
9694 bak = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009695 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009696 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009697 ctxt->context->doc = bakd;
9698 ctxt->context->node = bak;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009699 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009700 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009701 if (op->value == 0)
9702 xmlXPathMultValues(ctxt);
9703 else if (op->value == 1)
9704 xmlXPathDivValues(ctxt);
9705 else if (op->value == 2)
9706 xmlXPathModValues(ctxt);
9707 return (total);
9708 case XPATH_OP_UNION:
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009709 bakd = ctxt->context->doc;
9710 bak = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009711 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009712 CHECK_ERROR0;
Daniel Veillard7089d6b2002-03-29 17:28:10 +00009713 ctxt->context->doc = bakd;
9714 ctxt->context->node = bak;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009715 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009716 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009717 CHECK_TYPE0(XPATH_NODESET);
9718 arg2 = valuePop(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009719
Daniel Veillardf06307e2001-07-03 10:35:50 +00009720 CHECK_TYPE0(XPATH_NODESET);
9721 arg1 = valuePop(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009722
Daniel Veillardf06307e2001-07-03 10:35:50 +00009723 arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval,
9724 arg2->nodesetval);
9725 valuePush(ctxt, arg1);
9726 xmlXPathFreeObject(arg2);
9727 return (total);
9728 case XPATH_OP_ROOT:
9729 xmlXPathRoot(ctxt);
9730 return (total);
9731 case XPATH_OP_NODE:
9732 if (op->ch1 != -1)
9733 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009734 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009735 if (op->ch2 != -1)
9736 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009737 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009738 valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node));
9739 return (total);
9740 case XPATH_OP_RESET:
9741 if (op->ch1 != -1)
9742 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009743 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009744 if (op->ch2 != -1)
9745 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009746 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009747 ctxt->context->node = NULL;
9748 return (total);
9749 case XPATH_OP_COLLECT:{
9750 if (op->ch1 == -1)
9751 return (total);
Daniel Veillardd8df6c02001-04-05 16:54:14 +00009752
Daniel Veillardf06307e2001-07-03 10:35:50 +00009753 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009754 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009755
Daniel Veillardf06307e2001-07-03 10:35:50 +00009756 /*
9757 * Optimization for [n] selection where n is a number
9758 */
9759 if ((op->ch2 != -1) &&
9760 (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) &&
9761 (comp->steps[op->ch2].ch1 == -1) &&
9762 (comp->steps[op->ch2].ch2 != -1) &&
9763 (comp->steps[comp->steps[op->ch2].ch2].op ==
9764 XPATH_OP_VALUE)) {
9765 xmlXPathObjectPtr val;
Daniel Veillard42596ad2001-05-22 16:57:14 +00009766
Daniel Veillardf06307e2001-07-03 10:35:50 +00009767 val = comp->steps[comp->steps[op->ch2].ch2].value4;
9768 if ((val != NULL) && (val->type == XPATH_NUMBER)) {
9769 int indx = (int) val->floatval;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009770
Daniel Veillardf06307e2001-07-03 10:35:50 +00009771 if (val->floatval == (float) indx) {
9772 total +=
9773 xmlXPathNodeCollectAndTestNth(ctxt, op,
9774 indx, NULL,
9775 NULL);
9776 return (total);
9777 }
9778 }
9779 }
9780 total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL);
9781 return (total);
9782 }
9783 case XPATH_OP_VALUE:
9784 valuePush(ctxt,
9785 xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4));
9786 return (total);
9787 case XPATH_OP_VARIABLE:{
Daniel Veillard556c6682001-10-06 09:59:51 +00009788 xmlXPathObjectPtr val;
9789
Daniel Veillardf06307e2001-07-03 10:35:50 +00009790 if (op->ch1 != -1)
9791 total +=
9792 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009793 if (op->value5 == NULL) {
9794 val = xmlXPathVariableLookup(ctxt->context, op->value4);
9795 if (val == NULL) {
9796 ctxt->error = XPATH_UNDEF_VARIABLE_ERROR;
9797 return(0);
9798 }
9799 valuePush(ctxt, val);
9800 } else {
Daniel Veillardf06307e2001-07-03 10:35:50 +00009801 const xmlChar *URI;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009802
Daniel Veillardf06307e2001-07-03 10:35:50 +00009803 URI = xmlXPathNsLookup(ctxt->context, op->value5);
9804 if (URI == NULL) {
9805 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009806 "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n",
Daniel Veillardf06307e2001-07-03 10:35:50 +00009807 op->value4, op->value5);
9808 return (total);
9809 }
Daniel Veillard556c6682001-10-06 09:59:51 +00009810 val = xmlXPathVariableLookupNS(ctxt->context,
9811 op->value4, URI);
9812 if (val == NULL) {
9813 ctxt->error = XPATH_UNDEF_VARIABLE_ERROR;
9814 return(0);
9815 }
9816 valuePush(ctxt, val);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009817 }
9818 return (total);
9819 }
9820 case XPATH_OP_FUNCTION:{
9821 xmlXPathFunction func;
9822 const xmlChar *oldFunc, *oldFuncURI;
Daniel Veillard556c6682001-10-06 09:59:51 +00009823 int i;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009824
9825 if (op->ch1 != -1)
9826 total +=
9827 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009828 if (ctxt->valueNr < op->value) {
9829 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009830 "xmlXPathCompOpEval: parameter error\n");
Daniel Veillard556c6682001-10-06 09:59:51 +00009831 ctxt->error = XPATH_INVALID_OPERAND;
9832 return (total);
9833 }
9834 for (i = 0; i < op->value; i++)
9835 if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) {
9836 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009837 "xmlXPathCompOpEval: parameter error\n");
Daniel Veillard556c6682001-10-06 09:59:51 +00009838 ctxt->error = XPATH_INVALID_OPERAND;
9839 return (total);
9840 }
Daniel Veillardf06307e2001-07-03 10:35:50 +00009841 if (op->cache != NULL)
9842 func = (xmlXPathFunction) op->cache;
9843 else {
9844 const xmlChar *URI = NULL;
9845
9846 if (op->value5 == NULL)
9847 func =
9848 xmlXPathFunctionLookup(ctxt->context,
9849 op->value4);
9850 else {
9851 URI = xmlXPathNsLookup(ctxt->context, op->value5);
9852 if (URI == NULL) {
9853 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009854 "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n",
Daniel Veillardf06307e2001-07-03 10:35:50 +00009855 op->value4, op->value5);
9856 return (total);
9857 }
9858 func = xmlXPathFunctionLookupNS(ctxt->context,
9859 op->value4, URI);
9860 }
9861 if (func == NULL) {
9862 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009863 "xmlXPathCompOpEval: function %s not found\n",
Daniel Veillardf06307e2001-07-03 10:35:50 +00009864 op->value4);
9865 XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR);
Daniel Veillardf06307e2001-07-03 10:35:50 +00009866 }
9867 op->cache = (void *) func;
9868 op->cacheURI = (void *) URI;
9869 }
9870 oldFunc = ctxt->context->function;
9871 oldFuncURI = ctxt->context->functionURI;
9872 ctxt->context->function = op->value4;
9873 ctxt->context->functionURI = op->cacheURI;
9874 func(ctxt, op->value);
9875 ctxt->context->function = oldFunc;
9876 ctxt->context->functionURI = oldFuncURI;
9877 return (total);
9878 }
9879 case XPATH_OP_ARG:
Daniel Veillard088bf112002-05-14 11:03:59 +00009880 bakd = ctxt->context->doc;
9881 bak = ctxt->context->node;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009882 if (op->ch1 != -1)
9883 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard088bf112002-05-14 11:03:59 +00009884 ctxt->context->doc = bakd;
9885 ctxt->context->node = bak;
Daniel Veillard556c6682001-10-06 09:59:51 +00009886 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009887 if (op->ch2 != -1)
9888 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]);
Daniel Veillard088bf112002-05-14 11:03:59 +00009889 ctxt->context->doc = bakd;
9890 ctxt->context->node = bak;
Daniel Veillard556c6682001-10-06 09:59:51 +00009891 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009892 return (total);
9893 case XPATH_OP_PREDICATE:
9894 case XPATH_OP_FILTER:{
9895 xmlXPathObjectPtr res;
9896 xmlXPathObjectPtr obj, tmp;
9897 xmlNodeSetPtr newset = NULL;
9898 xmlNodeSetPtr oldset;
9899 xmlNodePtr oldnode;
9900 int i;
9901
9902 /*
9903 * Optimization for ()[1] selection i.e. the first elem
9904 */
9905 if ((op->ch1 != -1) && (op->ch2 != -1) &&
9906 (comp->steps[op->ch1].op == XPATH_OP_SORT) &&
9907 (comp->steps[op->ch2].op == XPATH_OP_VALUE)) {
9908 xmlXPathObjectPtr val;
9909
9910 val = comp->steps[op->ch2].value4;
9911 if ((val != NULL) && (val->type == XPATH_NUMBER) &&
9912 (val->floatval == 1.0)) {
9913 xmlNodePtr first = NULL;
9914
9915 total +=
9916 xmlXPathCompOpEvalFirst(ctxt,
9917 &comp->steps[op->ch1],
9918 &first);
Daniel Veillard556c6682001-10-06 09:59:51 +00009919 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009920 /*
9921 * The nodeset should be in document order,
9922 * Keep only the first value
9923 */
9924 if ((ctxt->value != NULL) &&
9925 (ctxt->value->type == XPATH_NODESET) &&
9926 (ctxt->value->nodesetval != NULL) &&
9927 (ctxt->value->nodesetval->nodeNr > 1))
9928 ctxt->value->nodesetval->nodeNr = 1;
9929 return (total);
9930 }
9931 }
9932 /*
9933 * Optimization for ()[last()] selection i.e. the last elem
9934 */
9935 if ((op->ch1 != -1) && (op->ch2 != -1) &&
9936 (comp->steps[op->ch1].op == XPATH_OP_SORT) &&
9937 (comp->steps[op->ch2].op == XPATH_OP_SORT)) {
9938 int f = comp->steps[op->ch2].ch1;
9939
9940 if ((f != -1) &&
9941 (comp->steps[f].op == XPATH_OP_FUNCTION) &&
9942 (comp->steps[f].value5 == NULL) &&
9943 (comp->steps[f].value == 0) &&
9944 (comp->steps[f].value4 != NULL) &&
9945 (xmlStrEqual
9946 (comp->steps[f].value4, BAD_CAST "last"))) {
9947 xmlNodePtr last = NULL;
9948
9949 total +=
9950 xmlXPathCompOpEvalLast(ctxt,
9951 &comp->steps[op->ch1],
9952 &last);
Daniel Veillard556c6682001-10-06 09:59:51 +00009953 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009954 /*
9955 * The nodeset should be in document order,
9956 * Keep only the last value
9957 */
9958 if ((ctxt->value != NULL) &&
9959 (ctxt->value->type == XPATH_NODESET) &&
9960 (ctxt->value->nodesetval != NULL) &&
9961 (ctxt->value->nodesetval->nodeTab != NULL) &&
9962 (ctxt->value->nodesetval->nodeNr > 1)) {
9963 ctxt->value->nodesetval->nodeTab[0] =
9964 ctxt->value->nodesetval->nodeTab[ctxt->
9965 value->
9966 nodesetval->
9967 nodeNr -
9968 1];
9969 ctxt->value->nodesetval->nodeNr = 1;
9970 }
9971 return (total);
9972 }
9973 }
9974
9975 if (op->ch1 != -1)
9976 total +=
9977 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +00009978 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +00009979 if (op->ch2 == -1)
9980 return (total);
9981 if (ctxt->value == NULL)
9982 return (total);
9983
9984 oldnode = ctxt->context->node;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009985
9986#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardf06307e2001-07-03 10:35:50 +00009987 /*
9988 * Hum are we filtering the result of an XPointer expression
9989 */
9990 if (ctxt->value->type == XPATH_LOCATIONSET) {
9991 xmlLocationSetPtr newlocset = NULL;
9992 xmlLocationSetPtr oldlocset;
Daniel Veillard9e7160d2001-03-18 23:17:47 +00009993
Daniel Veillardf06307e2001-07-03 10:35:50 +00009994 /*
9995 * Extract the old locset, and then evaluate the result of the
9996 * expression for all the element in the locset. use it to grow
9997 * up a new locset.
9998 */
9999 CHECK_TYPE0(XPATH_LOCATIONSET);
10000 obj = valuePop(ctxt);
10001 oldlocset = obj->user;
10002 ctxt->context->node = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010003
Daniel Veillardf06307e2001-07-03 10:35:50 +000010004 if ((oldlocset == NULL) || (oldlocset->locNr == 0)) {
10005 ctxt->context->contextSize = 0;
10006 ctxt->context->proximityPosition = 0;
10007 if (op->ch2 != -1)
10008 total +=
10009 xmlXPathCompOpEval(ctxt,
10010 &comp->steps[op->ch2]);
10011 res = valuePop(ctxt);
10012 if (res != NULL)
10013 xmlXPathFreeObject(res);
10014 valuePush(ctxt, obj);
10015 CHECK_ERROR0;
10016 return (total);
10017 }
10018 newlocset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010019
Daniel Veillardf06307e2001-07-03 10:35:50 +000010020 for (i = 0; i < oldlocset->locNr; i++) {
10021 /*
10022 * Run the evaluation with a node list made of a
10023 * single item in the nodelocset.
10024 */
10025 ctxt->context->node = oldlocset->locTab[i]->user;
10026 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10027 valuePush(ctxt, tmp);
10028 ctxt->context->contextSize = oldlocset->locNr;
10029 ctxt->context->proximityPosition = i + 1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010030
Daniel Veillardf06307e2001-07-03 10:35:50 +000010031 if (op->ch2 != -1)
10032 total +=
10033 xmlXPathCompOpEval(ctxt,
10034 &comp->steps[op->ch2]);
10035 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010036
Daniel Veillardf06307e2001-07-03 10:35:50 +000010037 /*
10038 * The result of the evaluation need to be tested to
10039 * decided whether the filter succeeded or not
10040 */
10041 res = valuePop(ctxt);
10042 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
10043 xmlXPtrLocationSetAdd(newlocset,
10044 xmlXPathObjectCopy
10045 (oldlocset->locTab[i]));
10046 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010047
Daniel Veillardf06307e2001-07-03 10:35:50 +000010048 /*
10049 * Cleanup
10050 */
10051 if (res != NULL)
10052 xmlXPathFreeObject(res);
10053 if (ctxt->value == tmp) {
10054 res = valuePop(ctxt);
10055 xmlXPathFreeObject(res);
10056 }
10057
10058 ctxt->context->node = NULL;
10059 }
10060
10061 /*
10062 * The result is used as the new evaluation locset.
10063 */
10064 xmlXPathFreeObject(obj);
10065 ctxt->context->node = NULL;
10066 ctxt->context->contextSize = -1;
10067 ctxt->context->proximityPosition = -1;
10068 valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset));
10069 ctxt->context->node = oldnode;
10070 return (total);
10071 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010072#endif /* LIBXML_XPTR_ENABLED */
10073
Daniel Veillardf06307e2001-07-03 10:35:50 +000010074 /*
10075 * Extract the old set, and then evaluate the result of the
10076 * expression for all the element in the set. use it to grow
10077 * up a new set.
10078 */
10079 CHECK_TYPE0(XPATH_NODESET);
10080 obj = valuePop(ctxt);
10081 oldset = obj->nodesetval;
Daniel Veillard911f49a2001-04-07 15:39:35 +000010082
Daniel Veillardf06307e2001-07-03 10:35:50 +000010083 oldnode = ctxt->context->node;
10084 ctxt->context->node = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010085
Daniel Veillardf06307e2001-07-03 10:35:50 +000010086 if ((oldset == NULL) || (oldset->nodeNr == 0)) {
10087 ctxt->context->contextSize = 0;
10088 ctxt->context->proximityPosition = 0;
10089 if (op->ch2 != -1)
10090 total +=
10091 xmlXPathCompOpEval(ctxt,
10092 &comp->steps[op->ch2]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010093 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010094 res = valuePop(ctxt);
10095 if (res != NULL)
10096 xmlXPathFreeObject(res);
10097 valuePush(ctxt, obj);
10098 ctxt->context->node = oldnode;
10099 CHECK_ERROR0;
10100 } else {
10101 /*
10102 * Initialize the new set.
10103 */
10104 newset = xmlXPathNodeSetCreate(NULL);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010105
Daniel Veillardf06307e2001-07-03 10:35:50 +000010106 for (i = 0; i < oldset->nodeNr; i++) {
10107 /*
10108 * Run the evaluation with a node list made of
10109 * a single item in the nodeset.
10110 */
10111 ctxt->context->node = oldset->nodeTab[i];
10112 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10113 valuePush(ctxt, tmp);
10114 ctxt->context->contextSize = oldset->nodeNr;
10115 ctxt->context->proximityPosition = i + 1;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010116
Daniel Veillardf06307e2001-07-03 10:35:50 +000010117 if (op->ch2 != -1)
10118 total +=
10119 xmlXPathCompOpEval(ctxt,
10120 &comp->steps[op->ch2]);
10121 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010122
Daniel Veillardf06307e2001-07-03 10:35:50 +000010123 /*
10124 * The result of the evaluation need to be tested to
10125 * decided whether the filter succeeded or not
10126 */
10127 res = valuePop(ctxt);
10128 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
10129 xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]);
10130 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010131
Daniel Veillardf06307e2001-07-03 10:35:50 +000010132 /*
10133 * Cleanup
10134 */
10135 if (res != NULL)
10136 xmlXPathFreeObject(res);
10137 if (ctxt->value == tmp) {
10138 res = valuePop(ctxt);
10139 xmlXPathFreeObject(res);
10140 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010141
Daniel Veillardf06307e2001-07-03 10:35:50 +000010142 ctxt->context->node = NULL;
10143 }
10144
10145 /*
10146 * The result is used as the new evaluation set.
10147 */
10148 xmlXPathFreeObject(obj);
10149 ctxt->context->node = NULL;
10150 ctxt->context->contextSize = -1;
10151 ctxt->context->proximityPosition = -1;
10152 valuePush(ctxt, xmlXPathWrapNodeSet(newset));
10153 }
10154 ctxt->context->node = oldnode;
10155 return (total);
10156 }
10157 case XPATH_OP_SORT:
10158 if (op->ch1 != -1)
10159 total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
Daniel Veillard556c6682001-10-06 09:59:51 +000010160 CHECK_ERROR0;
Daniel Veillardf06307e2001-07-03 10:35:50 +000010161 if ((ctxt->value != NULL) &&
10162 (ctxt->value->type == XPATH_NODESET) &&
10163 (ctxt->value->nodesetval != NULL))
10164 xmlXPathNodeSetSort(ctxt->value->nodesetval);
10165 return (total);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010166#ifdef LIBXML_XPTR_ENABLED
Daniel Veillardf06307e2001-07-03 10:35:50 +000010167 case XPATH_OP_RANGETO:{
10168 xmlXPathObjectPtr range;
10169 xmlXPathObjectPtr res, obj;
10170 xmlXPathObjectPtr tmp;
10171 xmlLocationSetPtr newset = NULL;
10172 xmlNodeSetPtr oldset;
10173 int i;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010174
Daniel Veillardf06307e2001-07-03 10:35:50 +000010175 if (op->ch1 != -1)
10176 total +=
10177 xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
10178 if (op->ch2 == -1)
10179 return (total);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010180
Daniel Veillardf06307e2001-07-03 10:35:50 +000010181 CHECK_TYPE0(XPATH_NODESET);
10182 obj = valuePop(ctxt);
10183 oldset = obj->nodesetval;
10184 ctxt->context->node = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010185
Daniel Veillardf06307e2001-07-03 10:35:50 +000010186 newset = xmlXPtrLocationSetCreate(NULL);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010187
Daniel Veillardf06307e2001-07-03 10:35:50 +000010188 if (oldset != NULL) {
10189 for (i = 0; i < oldset->nodeNr; i++) {
10190 /*
10191 * Run the evaluation with a node list made of a single item
10192 * in the nodeset.
10193 */
10194 ctxt->context->node = oldset->nodeTab[i];
10195 tmp = xmlXPathNewNodeSet(ctxt->context->node);
10196 valuePush(ctxt, tmp);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010197
Daniel Veillardf06307e2001-07-03 10:35:50 +000010198 if (op->ch2 != -1)
10199 total +=
10200 xmlXPathCompOpEval(ctxt,
10201 &comp->steps[op->ch2]);
10202 CHECK_ERROR0;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010203
Daniel Veillardf06307e2001-07-03 10:35:50 +000010204 /*
10205 * The result of the evaluation need to be tested to
10206 * decided whether the filter succeeded or not
10207 */
10208 res = valuePop(ctxt);
10209 range =
10210 xmlXPtrNewRangeNodeObject(oldset->nodeTab[i],
10211 res);
10212 if (range != NULL) {
10213 xmlXPtrLocationSetAdd(newset, range);
10214 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010215
Daniel Veillardf06307e2001-07-03 10:35:50 +000010216 /*
10217 * Cleanup
10218 */
10219 if (res != NULL)
10220 xmlXPathFreeObject(res);
10221 if (ctxt->value == tmp) {
10222 res = valuePop(ctxt);
10223 xmlXPathFreeObject(res);
10224 }
10225
10226 ctxt->context->node = NULL;
10227 }
10228 }
10229
10230 /*
10231 * The result is used as the new evaluation set.
10232 */
10233 xmlXPathFreeObject(obj);
10234 ctxt->context->node = NULL;
10235 ctxt->context->contextSize = -1;
10236 ctxt->context->proximityPosition = -1;
10237 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
10238 return (total);
10239 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010240#endif /* LIBXML_XPTR_ENABLED */
10241 }
10242 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf06307e2001-07-03 10:35:50 +000010243 "XPath: unknown precompiled operation %d\n", op->op);
10244 return (total);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010245}
10246
10247/**
10248 * xmlXPathRunEval:
10249 * @ctxt: the XPath parser context with the compiled expression
10250 *
10251 * Evaluate the Precompiled XPath expression in the given context.
10252 */
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000010253static void
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010254xmlXPathRunEval(xmlXPathParserContextPtr ctxt) {
10255 xmlXPathCompExprPtr comp;
10256
10257 if ((ctxt == NULL) || (ctxt->comp == NULL))
10258 return;
10259
10260 if (ctxt->valueTab == NULL) {
10261 /* Allocate the value stack */
10262 ctxt->valueTab = (xmlXPathObjectPtr *)
10263 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
10264 if (ctxt->valueTab == NULL) {
10265 xmlFree(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010266 }
10267 ctxt->valueNr = 0;
10268 ctxt->valueMax = 10;
10269 ctxt->value = NULL;
10270 }
10271 comp = ctxt->comp;
Aleksey Sanin29b6f762002-05-05 06:59:57 +000010272 if(comp->last < 0) {
10273 xmlGenericError(xmlGenericErrorContext,
10274 "xmlXPathRunEval: last is less than zero\n");
10275 return;
10276 }
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010277 xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]);
10278}
10279
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010280/************************************************************************
10281 * *
10282 * Public interfaces *
10283 * *
10284 ************************************************************************/
10285
10286/**
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000010287 * xmlXPathEvalPredicate:
10288 * @ctxt: the XPath context
10289 * @res: the Predicate Expression evaluation result
10290 *
10291 * Evaluate a predicate result for the current node.
10292 * A PredicateExpr is evaluated by evaluating the Expr and converting
10293 * the result to a boolean. If the result is a number, the result will
10294 * be converted to true if the number is equal to the position of the
10295 * context node in the context node list (as returned by the position
10296 * function) and will be converted to false otherwise; if the result
10297 * is not a number, then the result will be converted as if by a call
10298 * to the boolean function.
10299 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010300 * Returns 1 if predicate is true, 0 otherwise
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000010301 */
10302int
10303xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) {
10304 if (res == NULL) return(0);
10305 switch (res->type) {
10306 case XPATH_BOOLEAN:
10307 return(res->boolval);
10308 case XPATH_NUMBER:
10309 return(res->floatval == ctxt->proximityPosition);
10310 case XPATH_NODESET:
10311 case XPATH_XSLT_TREE:
Daniel Veillardd8df6c02001-04-05 16:54:14 +000010312 if (res->nodesetval == NULL)
10313 return(0);
Daniel Veillardfbf8a2d2001-03-19 15:58:54 +000010314 return(res->nodesetval->nodeNr != 0);
10315 case XPATH_STRING:
10316 return((res->stringval != NULL) &&
10317 (xmlStrlen(res->stringval) != 0));
10318 default:
10319 STRANGE
10320 }
10321 return(0);
10322}
10323
10324/**
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010325 * xmlXPathEvaluatePredicateResult:
10326 * @ctxt: the XPath Parser context
10327 * @res: the Predicate Expression evaluation result
10328 *
10329 * Evaluate a predicate result for the current node.
10330 * A PredicateExpr is evaluated by evaluating the Expr and converting
10331 * the result to a boolean. If the result is a number, the result will
10332 * be converted to true if the number is equal to the position of the
10333 * context node in the context node list (as returned by the position
10334 * function) and will be converted to false otherwise; if the result
10335 * is not a number, then the result will be converted as if by a call
10336 * to the boolean function.
10337 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010338 * Returns 1 if predicate is true, 0 otherwise
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010339 */
10340int
10341xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt,
10342 xmlXPathObjectPtr res) {
10343 if (res == NULL) return(0);
10344 switch (res->type) {
10345 case XPATH_BOOLEAN:
10346 return(res->boolval);
10347 case XPATH_NUMBER:
10348 return(res->floatval == ctxt->context->proximityPosition);
10349 case XPATH_NODESET:
10350 case XPATH_XSLT_TREE:
Daniel Veillard73639a72001-04-10 14:31:39 +000010351 if (res->nodesetval == NULL)
Daniel Veillard911f49a2001-04-07 15:39:35 +000010352 return(0);
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010353 return(res->nodesetval->nodeNr != 0);
10354 case XPATH_STRING:
10355 return((res->stringval != NULL) &&
10356 (xmlStrlen(res->stringval) != 0));
10357 default:
10358 STRANGE
10359 }
10360 return(0);
10361}
10362
10363/**
10364 * xmlXPathCompile:
10365 * @str: the XPath expression
10366 *
10367 * Compile an XPath expression
10368 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010369 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010370 * the caller has to free the object.
10371 */
10372xmlXPathCompExprPtr
10373xmlXPathCompile(const xmlChar *str) {
10374 xmlXPathParserContextPtr ctxt;
10375 xmlXPathCompExprPtr comp;
10376
10377 xmlXPathInit();
10378
10379 ctxt = xmlXPathNewParserContext(str, NULL);
10380 xmlXPathCompileExpr(ctxt);
Aleksey Sanin50fe8b12002-05-07 16:21:36 +000010381
Daniel Veillard40af6492001-04-22 08:50:55 +000010382 if (*ctxt->cur != 0) {
Aleksey Sanin50fe8b12002-05-07 16:21:36 +000010383 /*
10384 * aleksey: in some cases this line prints *second* error message
10385 * (see bug #78858) and probably this should be fixed.
10386 * However, we are not sure that all error messages are printed
10387 * out in other places. It's not critical so we leave it as-is for now
10388 */
Daniel Veillard40af6492001-04-22 08:50:55 +000010389 xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
10390 comp = NULL;
10391 } else {
10392 comp = ctxt->comp;
10393 ctxt->comp = NULL;
10394 }
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010395 xmlXPathFreeParserContext(ctxt);
Daniel Veillardf06307e2001-07-03 10:35:50 +000010396#ifdef DEBUG_EVAL_COUNTS
10397 if (comp != NULL) {
10398 comp->string = xmlStrdup(str);
10399 comp->nb = 0;
10400 }
10401#endif
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010402 return(comp);
10403}
10404
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010405/**
10406 * xmlXPathCompiledEval:
10407 * @comp: the compiled XPath expression
Owen Taylor3473f882001-02-23 17:55:21 +000010408 * @ctx: the XPath context
10409 *
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010410 * Evaluate the Precompiled XPath expression in the given context.
Owen Taylor3473f882001-02-23 17:55:21 +000010411 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010412 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Owen Taylor3473f882001-02-23 17:55:21 +000010413 * the caller has to free the object.
10414 */
10415xmlXPathObjectPtr
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010416xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) {
Owen Taylor3473f882001-02-23 17:55:21 +000010417 xmlXPathParserContextPtr ctxt;
10418 xmlXPathObjectPtr res, tmp, init = NULL;
10419 int stack = 0;
Daniel Veillard81463942001-10-16 12:34:39 +000010420#ifndef LIBXML_THREAD_ENABLED
10421 static int reentance = 0;
10422#endif
Owen Taylor3473f882001-02-23 17:55:21 +000010423
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010424 if ((comp == NULL) || (ctx == NULL))
10425 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010426 xmlXPathInit();
10427
10428 CHECK_CONTEXT(ctx)
10429
Daniel Veillard81463942001-10-16 12:34:39 +000010430#ifndef LIBXML_THREAD_ENABLED
10431 reentance++;
10432 if (reentance > 1)
10433 xmlXPathDisableOptimizer = 1;
10434#endif
10435
Daniel Veillardf06307e2001-07-03 10:35:50 +000010436#ifdef DEBUG_EVAL_COUNTS
10437 comp->nb++;
10438 if ((comp->string != NULL) && (comp->nb > 100)) {
10439 fprintf(stderr, "100 x %s\n", comp->string);
10440 comp->nb = 0;
10441 }
10442#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010443 ctxt = xmlXPathCompParserContext(comp, ctx);
10444 xmlXPathRunEval(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010445
10446 if (ctxt->value == NULL) {
10447 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010448 "xmlXPathCompiledEval: evaluation failed\n");
Owen Taylor3473f882001-02-23 17:55:21 +000010449 res = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010450 } else {
10451 res = valuePop(ctxt);
10452 }
10453
Daniel Veillardf06307e2001-07-03 10:35:50 +000010454
Owen Taylor3473f882001-02-23 17:55:21 +000010455 do {
10456 tmp = valuePop(ctxt);
10457 if (tmp != NULL) {
10458 if (tmp != init)
10459 stack++;
10460 xmlXPathFreeObject(tmp);
10461 }
10462 } while (tmp != NULL);
10463 if ((stack != 0) && (res != NULL)) {
10464 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010465 "xmlXPathCompiledEval: %d object left on the stack\n",
Owen Taylor3473f882001-02-23 17:55:21 +000010466 stack);
10467 }
10468 if (ctxt->error != XPATH_EXPRESSION_OK) {
10469 xmlXPathFreeObject(res);
10470 res = NULL;
10471 }
10472
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010473
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010474 ctxt->comp = NULL;
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010475 xmlXPathFreeParserContext(ctxt);
Daniel Veillard81463942001-10-16 12:34:39 +000010476#ifndef LIBXML_THREAD_ENABLED
10477 reentance--;
10478#endif
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010479 return(res);
10480}
10481
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010482/**
10483 * xmlXPathEvalExpr:
10484 * @ctxt: the XPath Parser context
10485 *
10486 * Parse and evaluate an XPath expression in the given context,
10487 * then push the result on the context stack
10488 */
10489void
10490xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) {
10491 xmlXPathCompileExpr(ctxt);
Aleksey Sanin50fe8b12002-05-07 16:21:36 +000010492 CHECK_ERROR;
Daniel Veillardafcbe1c2001-03-19 10:57:13 +000010493 xmlXPathRunEval(ctxt);
10494}
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010495
10496/**
10497 * xmlXPathEval:
10498 * @str: the XPath expression
10499 * @ctx: the XPath context
10500 *
10501 * Evaluate the XPath Location Path in the given context.
10502 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010503 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010504 * the caller has to free the object.
10505 */
10506xmlXPathObjectPtr
10507xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) {
10508 xmlXPathParserContextPtr ctxt;
10509 xmlXPathObjectPtr res, tmp, init = NULL;
10510 int stack = 0;
10511
10512 xmlXPathInit();
10513
10514 CHECK_CONTEXT(ctx)
10515
10516 ctxt = xmlXPathNewParserContext(str, ctx);
10517 xmlXPathEvalExpr(ctxt);
Daniel Veillard9e7160d2001-03-18 23:17:47 +000010518
10519 if (ctxt->value == NULL) {
10520 xmlGenericError(xmlGenericErrorContext,
10521 "xmlXPathEval: evaluation failed\n");
10522 res = NULL;
10523 } else if (*ctxt->cur != 0) {
10524 xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
10525 res = NULL;
10526 } else {
10527 res = valuePop(ctxt);
10528 }
10529
10530 do {
10531 tmp = valuePop(ctxt);
10532 if (tmp != NULL) {
10533 if (tmp != init)
10534 stack++;
10535 xmlXPathFreeObject(tmp);
10536 }
10537 } while (tmp != NULL);
10538 if ((stack != 0) && (res != NULL)) {
10539 xmlGenericError(xmlGenericErrorContext,
10540 "xmlXPathEval: %d object left on the stack\n",
10541 stack);
10542 }
10543 if (ctxt->error != XPATH_EXPRESSION_OK) {
10544 xmlXPathFreeObject(res);
10545 res = NULL;
10546 }
10547
Owen Taylor3473f882001-02-23 17:55:21 +000010548 xmlXPathFreeParserContext(ctxt);
10549 return(res);
10550}
10551
10552/**
10553 * xmlXPathEvalExpression:
10554 * @str: the XPath expression
10555 * @ctxt: the XPath context
10556 *
10557 * Evaluate the XPath expression in the given context.
10558 *
10559 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
10560 * the caller has to free the object.
10561 */
10562xmlXPathObjectPtr
10563xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) {
10564 xmlXPathParserContextPtr pctxt;
10565 xmlXPathObjectPtr res, tmp;
10566 int stack = 0;
10567
10568 xmlXPathInit();
10569
10570 CHECK_CONTEXT(ctxt)
10571
10572 pctxt = xmlXPathNewParserContext(str, ctxt);
10573 xmlXPathEvalExpr(pctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010574
10575 if (*pctxt->cur != 0) {
10576 xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR);
10577 res = NULL;
10578 } else {
10579 res = valuePop(pctxt);
10580 }
10581 do {
10582 tmp = valuePop(pctxt);
10583 if (tmp != NULL) {
10584 xmlXPathFreeObject(tmp);
10585 stack++;
10586 }
10587 } while (tmp != NULL);
10588 if ((stack != 0) && (res != NULL)) {
10589 xmlGenericError(xmlGenericErrorContext,
10590 "xmlXPathEvalExpression: %d object left on the stack\n",
10591 stack);
10592 }
10593 xmlXPathFreeParserContext(pctxt);
10594 return(res);
10595}
10596
10597/**
10598 * xmlXPathRegisterAllFunctions:
10599 * @ctxt: the XPath context
10600 *
10601 * Registers all default XPath functions in this context
10602 */
10603void
10604xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt)
10605{
10606 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean",
10607 xmlXPathBooleanFunction);
10608 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling",
10609 xmlXPathCeilingFunction);
10610 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count",
10611 xmlXPathCountFunction);
10612 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat",
10613 xmlXPathConcatFunction);
10614 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains",
10615 xmlXPathContainsFunction);
10616 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id",
10617 xmlXPathIdFunction);
10618 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false",
10619 xmlXPathFalseFunction);
10620 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor",
10621 xmlXPathFloorFunction);
10622 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last",
10623 xmlXPathLastFunction);
10624 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang",
10625 xmlXPathLangFunction);
10626 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name",
10627 xmlXPathLocalNameFunction);
10628 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not",
10629 xmlXPathNotFunction);
10630 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name",
10631 xmlXPathNameFunction);
10632 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri",
10633 xmlXPathNamespaceURIFunction);
10634 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space",
10635 xmlXPathNormalizeFunction);
10636 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number",
10637 xmlXPathNumberFunction);
10638 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position",
10639 xmlXPathPositionFunction);
10640 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round",
10641 xmlXPathRoundFunction);
10642 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string",
10643 xmlXPathStringFunction);
10644 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length",
10645 xmlXPathStringLengthFunction);
10646 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with",
10647 xmlXPathStartsWithFunction);
10648 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring",
10649 xmlXPathSubstringFunction);
10650 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before",
10651 xmlXPathSubstringBeforeFunction);
10652 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after",
10653 xmlXPathSubstringAfterFunction);
10654 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum",
10655 xmlXPathSumFunction);
10656 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true",
10657 xmlXPathTrueFunction);
10658 xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate",
10659 xmlXPathTranslateFunction);
10660}
10661
10662#endif /* LIBXML_XPATH_ENABLED */