Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * xpath.c: XML Path Language implementation |
| 3 | * XPath is a language for addressing parts of an XML document, |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4 | * designed to be used by both XSLT and XPointer |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5 | * |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 6 | * Reference: W3C Recommendation 16 November 1999 |
| 7 | * http://www.w3.org/TR/1999/REC-xpath-19991116 |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 8 | * Public reference: |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 9 | * http://www.w3.org/TR/xpath |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 10 | * |
| 11 | * See COPYRIGHT for the status of this software |
| 12 | * |
| 13 | * Author: Daniel.Veillard@w3.org |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 14 | * |
| 15 | * 14 Nov 2000 ht - truncated declaration of xmlXPathEvalRelativeLocationPath |
| 16 | * for VMS |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 19 | #ifdef WIN32 |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 20 | #include "win32config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 21 | #else |
| 22 | #include "config.h" |
| 23 | #endif |
| 24 | |
Daniel Veillard | b71379b | 2000-10-09 12:30:39 +0000 | [diff] [blame] | 25 | #include <libxml/xmlversion.h> |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 26 | #ifdef LIBXML_XPATH_ENABLED |
| 27 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #ifdef HAVE_SYS_TYPES_H |
| 32 | #include <sys/types.h> |
| 33 | #endif |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 34 | #ifdef HAVE_MATH_H |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 35 | #include <math.h> |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 36 | #endif |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 37 | #ifdef HAVE_FLOAT_H |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 38 | #include <float.h> |
| 39 | #endif |
| 40 | #ifdef HAVE_IEEEFP_H |
| 41 | #include <ieeefp.h> |
| 42 | #endif |
| 43 | #ifdef HAVE_NAN_H |
| 44 | #include <nan.h> |
| 45 | #endif |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 46 | #ifdef HAVE_CTYPE_H |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 47 | #include <ctype.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 50 | #include <libxml/xmlmemory.h> |
| 51 | #include <libxml/tree.h> |
| 52 | #include <libxml/valid.h> |
| 53 | #include <libxml/xpath.h> |
Daniel Veillard | 29a11cc | 2000-10-25 13:32:39 +0000 | [diff] [blame] | 54 | #include <libxml/xpathInternals.h> |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 55 | #include <libxml/parserInternals.h> |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 56 | #include <libxml/hash.h> |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 57 | #ifdef LIBXML_XPTR_ENABLED |
| 58 | #include <libxml/xpointer.h> |
| 59 | #endif |
Daniel Veillard | c2df4cd | 2000-10-12 23:15:24 +0000 | [diff] [blame] | 60 | #ifdef LIBXML_DEBUG_ENABLED |
| 61 | #include <libxml/debugXML.h> |
| 62 | #endif |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 63 | #include <libxml/xmlerror.h> |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 64 | |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 65 | /* #define DEBUG */ |
| 66 | /* #define DEBUG_STEP */ |
| 67 | /* #define DEBUG_EXPR */ |
| 68 | |
Daniel Veillard | c2df4cd | 2000-10-12 23:15:24 +0000 | [diff] [blame] | 69 | void xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs); |
| 70 | double xmlXPathStringEvalNumber(const xmlChar *str); |
| 71 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 72 | /* |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 73 | * Setup stuff for floating point |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 74 | * The lack of portability of this section of the libc is annoying ! |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 75 | */ |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 76 | double xmlXPathNAN = 0; |
| 77 | double xmlXPathPINF = 1; |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 78 | double xmlXPathNINF = -1; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 79 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 80 | #ifndef isinf |
| 81 | #ifndef HAVE_ISINF |
| 82 | |
| 83 | #if HAVE_FPCLASS |
| 84 | |
| 85 | int isinf(double d) { |
| 86 | fpclass_t type = fpclass(d); |
| 87 | switch (type) { |
| 88 | case FP_NINF: |
| 89 | return(-1); |
| 90 | case FP_PINF: |
| 91 | return(1); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 92 | } |
| 93 | return(0); |
| 94 | } |
| 95 | |
| 96 | #elif defined(HAVE_FP_CLASS) || defined(HAVE_FP_CLASS_D) |
| 97 | |
| 98 | #if HAVE_FP_CLASS_H |
| 99 | #include <fp_class.h> |
| 100 | #endif |
| 101 | |
| 102 | int isinf(double d) { |
| 103 | #if HAVE_FP_CLASS |
| 104 | int fpclass = fp_class(d); |
| 105 | #else |
| 106 | int fpclass = fp_class_d(d); |
| 107 | #endif |
| 108 | if (fpclass == FP_POS_INF) |
| 109 | return(1); |
| 110 | if (fpclass == FP_NEG_INF) |
| 111 | return(-1); |
| 112 | return(0); |
| 113 | } |
| 114 | |
| 115 | #elif defined(HAVE_CLASS) |
| 116 | |
| 117 | int isinf(double d) { |
| 118 | int fpclass = class(d); |
| 119 | if (fpclass == FP_PLUS_INF) |
| 120 | return(1); |
| 121 | if (fpclass == FP_MINUS_INF) |
| 122 | return(-1); |
| 123 | return(0); |
| 124 | } |
| 125 | #elif defined(finite) || defined(HAVE_FINITE) |
| 126 | int isinf(double x) { return !finite(x) && x==x; } |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 127 | #elif defined(HUGE_VAL) |
Daniel Veillard | fc708e2 | 2000-04-08 13:17:27 +0000 | [diff] [blame] | 128 | int isinf(double x) |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 129 | { |
| 130 | if (x == HUGE_VAL) |
| 131 | return(1); |
| 132 | if (x == -HUGE_VAL) |
| 133 | return(-1); |
| 134 | return(0); |
| 135 | } |
| 136 | #endif |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 137 | |
| 138 | #endif /* ! HAVE_ISINF */ |
| 139 | #endif /* ! defined(isinf) */ |
| 140 | |
| 141 | #ifndef isnan |
| 142 | #ifndef HAVE_ISNAN |
| 143 | |
| 144 | #ifdef HAVE_ISNAND |
| 145 | #define isnan(f) isnand(f) |
| 146 | #endif /* HAVE_iSNAND */ |
| 147 | |
| 148 | #endif /* ! HAVE_iSNAN */ |
| 149 | #endif /* ! defined(isnan) */ |
| 150 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 151 | /** |
| 152 | * xmlXPathInit: |
| 153 | * |
| 154 | * Initialize the XPath environment |
| 155 | */ |
| 156 | void |
| 157 | xmlXPathInit(void) { |
| 158 | static int initialized = 0; |
| 159 | |
| 160 | if (initialized) return; |
| 161 | |
| 162 | xmlXPathNAN = 0; |
| 163 | xmlXPathNAN /= 0; |
| 164 | |
| 165 | xmlXPathPINF = 1; |
| 166 | xmlXPathPINF /= 0; |
| 167 | |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 168 | xmlXPathNINF = -1; |
| 169 | xmlXPathNINF /= 0; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 170 | |
| 171 | initialized = 1; |
| 172 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 173 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 174 | /************************************************************************ |
| 175 | * * |
Daniel Veillard | c2df4cd | 2000-10-12 23:15:24 +0000 | [diff] [blame] | 176 | * Debugging related functions * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 177 | * * |
| 178 | ************************************************************************/ |
| 179 | |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 180 | #define TODO \ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 181 | xmlGenericError(xmlGenericErrorContext, \ |
| 182 | "Unimplemented block at %s:%d\n", \ |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 183 | __FILE__, __LINE__); |
| 184 | |
| 185 | #define STRANGE \ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 186 | xmlGenericError(xmlGenericErrorContext, \ |
| 187 | "Internal error at %s:%d\n", \ |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 188 | __FILE__, __LINE__); |
| 189 | |
Daniel Veillard | c2df4cd | 2000-10-12 23:15:24 +0000 | [diff] [blame] | 190 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | c2df4cd | 2000-10-12 23:15:24 +0000 | [diff] [blame] | 191 | void xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) { |
| 192 | int i; |
| 193 | char shift[100]; |
| 194 | |
| 195 | for (i = 0;((i < depth) && (i < 25));i++) |
| 196 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 197 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 198 | if (cur == NULL) { |
| 199 | fprintf(output, shift); |
| 200 | fprintf(output, "Node is NULL !\n"); |
| 201 | return; |
| 202 | |
| 203 | } |
| 204 | |
| 205 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 206 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 207 | fprintf(output, shift); |
| 208 | fprintf(output, " /\n"); |
| 209 | } else if (cur->type == XML_ATTRIBUTE_NODE) |
| 210 | xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth); |
| 211 | else |
| 212 | xmlDebugDumpOneNode(output, cur, depth); |
| 213 | } |
| 214 | |
| 215 | void xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) { |
| 216 | int i; |
| 217 | char shift[100]; |
| 218 | |
| 219 | for (i = 0;((i < depth) && (i < 25));i++) |
| 220 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 221 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 222 | |
| 223 | if (cur == NULL) { |
| 224 | fprintf(output, shift); |
| 225 | fprintf(output, "NodeSet is NULL !\n"); |
| 226 | return; |
| 227 | |
| 228 | } |
| 229 | |
| 230 | fprintf(output, "Set contains %d nodes:\n", cur->nodeNr); |
| 231 | for (i = 0;i < cur->nodeNr;i++) { |
| 232 | fprintf(output, shift); |
| 233 | fprintf(output, "%d", i + 1); |
| 234 | xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | #if defined(LIBXML_XPTR_ENABLED) |
| 239 | void xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth); |
| 240 | void xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) { |
| 241 | int i; |
| 242 | char shift[100]; |
| 243 | |
| 244 | for (i = 0;((i < depth) && (i < 25));i++) |
| 245 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 246 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 247 | |
| 248 | if (cur == NULL) { |
| 249 | fprintf(output, shift); |
| 250 | fprintf(output, "LocationSet is NULL !\n"); |
| 251 | return; |
| 252 | |
| 253 | } |
| 254 | |
| 255 | for (i = 0;i < cur->locNr;i++) { |
| 256 | fprintf(output, shift); |
| 257 | fprintf(output, "%d : ", i + 1); |
| 258 | xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1); |
| 259 | } |
| 260 | } |
| 261 | #endif |
| 262 | |
| 263 | void xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) { |
| 264 | int i; |
| 265 | char shift[100]; |
| 266 | |
| 267 | for (i = 0;((i < depth) && (i < 25));i++) |
| 268 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 269 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 270 | |
| 271 | fprintf(output, shift); |
| 272 | |
| 273 | if (cur == NULL) { |
| 274 | fprintf(output, "Object is empty (NULL)\n"); |
| 275 | return; |
| 276 | } |
| 277 | switch(cur->type) { |
| 278 | case XPATH_UNDEFINED: |
| 279 | fprintf(output, "Object is uninitialized\n"); |
| 280 | break; |
| 281 | case XPATH_NODESET: |
| 282 | fprintf(output, "Object is a Node Set :\n"); |
| 283 | xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth); |
| 284 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 285 | case XPATH_XSLT_TREE: |
| 286 | fprintf(output, "Object is an XSLT value tree :\n"); |
| 287 | xmlXPathDebugDumpNode(output, cur->user, depth); |
| 288 | break; |
Daniel Veillard | c2df4cd | 2000-10-12 23:15:24 +0000 | [diff] [blame] | 289 | case XPATH_BOOLEAN: |
| 290 | fprintf(output, "Object is a Boolean : "); |
| 291 | if (cur->boolval) fprintf(output, "true\n"); |
| 292 | else fprintf(output, "false\n"); |
| 293 | break; |
| 294 | case XPATH_NUMBER: |
| 295 | fprintf(output, "Object is a number : %0g\n", cur->floatval); |
| 296 | break; |
| 297 | case XPATH_STRING: |
| 298 | fprintf(output, "Object is a string : "); |
| 299 | xmlDebugDumpString(output, cur->stringval); |
| 300 | fprintf(output, "\n"); |
| 301 | break; |
| 302 | case XPATH_POINT: |
| 303 | fprintf(output, "Object is a point : index %d in node", cur->index); |
| 304 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1); |
| 305 | fprintf(output, "\n"); |
| 306 | break; |
| 307 | case XPATH_RANGE: |
| 308 | if ((cur->user2 == NULL) || |
| 309 | ((cur->user2 == cur->user) && (cur->index == cur->index2))) { |
| 310 | fprintf(output, "Object is a collapsed range :\n"); |
| 311 | fprintf(output, shift); |
| 312 | if (cur->index >= 0) |
| 313 | fprintf(output, "index %d in ", cur->index); |
| 314 | fprintf(output, "node\n"); |
| 315 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 316 | depth + 1); |
| 317 | } else { |
| 318 | fprintf(output, "Object is a range :\n"); |
| 319 | fprintf(output, shift); |
| 320 | fprintf(output, "From "); |
| 321 | if (cur->index >= 0) |
| 322 | fprintf(output, "index %d in ", cur->index); |
| 323 | fprintf(output, "node\n"); |
| 324 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 325 | depth + 1); |
| 326 | fprintf(output, shift); |
| 327 | fprintf(output, "To "); |
| 328 | if (cur->index2 >= 0) |
| 329 | fprintf(output, "index %d in ", cur->index2); |
| 330 | fprintf(output, "node\n"); |
| 331 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2, |
| 332 | depth + 1); |
| 333 | fprintf(output, "\n"); |
| 334 | } |
| 335 | break; |
| 336 | case XPATH_LOCATIONSET: |
| 337 | #if defined(LIBXML_XPTR_ENABLED) |
| 338 | fprintf(output, "Object is a Location Set:\n"); |
| 339 | xmlXPathDebugDumpLocationSet(output, |
| 340 | (xmlLocationSetPtr) cur->user, depth); |
| 341 | #endif |
| 342 | break; |
| 343 | case XPATH_USERS: |
| 344 | fprintf(output, "Object is user defined\n"); |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | #endif |
| 349 | |
| 350 | /************************************************************************ |
| 351 | * * |
| 352 | * Parser stacks related functions and macros * |
| 353 | * * |
| 354 | ************************************************************************/ |
| 355 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 356 | /* |
| 357 | * Generic function for accessing stacks in the Parser Context |
| 358 | */ |
| 359 | |
| 360 | #define PUSH_AND_POP(type, name) \ |
| 361 | extern int name##Push(xmlXPathParserContextPtr ctxt, type value) { \ |
| 362 | if (ctxt->name##Nr >= ctxt->name##Max) { \ |
| 363 | ctxt->name##Max *= 2; \ |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 364 | ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 365 | ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \ |
| 366 | if (ctxt->name##Tab == NULL) { \ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 367 | xmlGenericError(xmlGenericErrorContext, \ |
| 368 | "realloc failed !\n"); \ |
Daniel Veillard | 0142b84 | 2000-01-14 14:45:24 +0000 | [diff] [blame] | 369 | return(0); \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 370 | } \ |
| 371 | } \ |
| 372 | ctxt->name##Tab[ctxt->name##Nr] = value; \ |
| 373 | ctxt->name = value; \ |
| 374 | return(ctxt->name##Nr++); \ |
| 375 | } \ |
| 376 | extern type name##Pop(xmlXPathParserContextPtr ctxt) { \ |
| 377 | type ret; \ |
| 378 | if (ctxt->name##Nr <= 0) return(0); \ |
| 379 | ctxt->name##Nr--; \ |
| 380 | if (ctxt->name##Nr > 0) \ |
| 381 | ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \ |
| 382 | else \ |
| 383 | ctxt->name = NULL; \ |
| 384 | ret = ctxt->name##Tab[ctxt->name##Nr]; \ |
| 385 | ctxt->name##Tab[ctxt->name##Nr] = 0; \ |
| 386 | return(ret); \ |
| 387 | } \ |
| 388 | |
| 389 | PUSH_AND_POP(xmlXPathObjectPtr, value) |
| 390 | |
| 391 | /* |
| 392 | * Macros for accessing the content. Those should be used only by the parser, |
| 393 | * and not exported. |
| 394 | * |
| 395 | * Dirty macros, i.e. one need to make assumption on the context to use them |
| 396 | * |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 397 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 398 | * CUR returns the current xmlChar value, i.e. a 8 bit value |
| 399 | * in ISO-Latin or UTF-8. |
| 400 | * This should be used internally by the parser |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 401 | * only to compare to ASCII values otherwise it would break when |
| 402 | * running with UTF-8 encoding. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 403 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 404 | * to compare on ASCII based substring. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 405 | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 406 | * strings within the parser. |
| 407 | * CURRENT Returns the current char value, with the full decoding of |
| 408 | * UTF-8 if we are using this mode. It returns an int. |
| 409 | * NEXT Skip to the next character, this does the proper decoding |
| 410 | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 411 | * It returns the pointer to the current xmlChar. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 412 | */ |
| 413 | |
| 414 | #define CUR (*ctxt->cur) |
| 415 | #define SKIP(val) ctxt->cur += (val) |
| 416 | #define NXT(val) ctxt->cur[(val)] |
| 417 | #define CUR_PTR ctxt->cur |
| 418 | |
| 419 | #define SKIP_BLANKS \ |
| 420 | while (IS_BLANK(*(ctxt->cur))) NEXT |
| 421 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 422 | #define CURRENT (*ctxt->cur) |
| 423 | #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 424 | |
| 425 | /************************************************************************ |
| 426 | * * |
| 427 | * Error handling routines * |
| 428 | * * |
| 429 | ************************************************************************/ |
| 430 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 431 | |
| 432 | const char *xmlXPathErrorMessages[] = { |
| 433 | "Ok", |
| 434 | "Number encoding", |
| 435 | "Unfinished litteral", |
| 436 | "Start of litteral", |
| 437 | "Expected $ for variable reference", |
| 438 | "Undefined variable", |
| 439 | "Invalid predicate", |
| 440 | "Invalid expression", |
| 441 | "Missing closing curly brace", |
| 442 | "Unregistered function", |
| 443 | "Invalid operand", |
| 444 | "Invalid type", |
| 445 | "Invalid number of arguments", |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 446 | "Invalid context size", |
| 447 | "Invalid context position", |
Daniel Veillard | b71379b | 2000-10-09 12:30:39 +0000 | [diff] [blame] | 448 | "Memory allocation error", |
| 449 | "Syntax error", |
| 450 | "Resource error", |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 451 | "Sub resource error", |
| 452 | "Undefined namespace prefix" |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | /** |
| 456 | * xmlXPathError: |
| 457 | * @ctxt: the XPath Parser context |
| 458 | * @file: the file name |
| 459 | * @line: the line number |
| 460 | * @no: the error number |
| 461 | * |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 462 | * Create a new xmlNodeSetPtr of type double and of value @val |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 463 | * |
| 464 | * Returns the newly created object. |
| 465 | */ |
| 466 | void |
| 467 | xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file, |
| 468 | int line, int no) { |
| 469 | int n; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 470 | const xmlChar *cur; |
| 471 | const xmlChar *base; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 472 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 473 | xmlGenericError(xmlGenericErrorContext, |
| 474 | "Error %s:%d: %s\n", file, line, |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 475 | xmlXPathErrorMessages[no]); |
| 476 | |
| 477 | cur = ctxt->cur; |
| 478 | base = ctxt->base; |
| 479 | while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) { |
| 480 | cur--; |
| 481 | } |
| 482 | n = 0; |
| 483 | while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r')) |
| 484 | cur--; |
| 485 | if ((*cur == '\n') || (*cur == '\r')) cur++; |
| 486 | base = cur; |
| 487 | n = 0; |
| 488 | while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 489 | xmlGenericError(xmlGenericErrorContext, "%c", (unsigned char) *cur++); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 490 | n++; |
| 491 | } |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 492 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 493 | cur = ctxt->cur; |
| 494 | while ((*cur == '\n') || (*cur == '\r')) |
| 495 | cur--; |
| 496 | n = 0; |
| 497 | while ((cur != base) && (n++ < 80)) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 498 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 499 | base++; |
| 500 | } |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 501 | xmlGenericError(xmlGenericErrorContext,"^\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 504 | |
| 505 | /************************************************************************ |
| 506 | * * |
| 507 | * Routines to handle NodeSets * |
| 508 | * * |
| 509 | ************************************************************************/ |
| 510 | |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 511 | /** |
| 512 | * xmlXPathCmpNodes: |
| 513 | * @node1: the first node |
| 514 | * @node2: the second node |
| 515 | * |
| 516 | * Compare two nodes w.r.t document order |
| 517 | * |
| 518 | * Returns -2 in case of error 1 if first point < second point, 0 if |
Daniel Veillard | 2f913b7 | 2001-01-31 13:23:49 +0000 | [diff] [blame] | 519 | * that's the same node, -1 otherwise |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 520 | */ |
| 521 | int |
| 522 | xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) { |
| 523 | int depth1, depth2; |
| 524 | xmlNodePtr cur, root; |
| 525 | |
| 526 | if ((node1 == NULL) || (node2 == NULL)) |
| 527 | return(-2); |
| 528 | /* |
| 529 | * a couple of optimizations which will avoid computations in most cases |
| 530 | */ |
| 531 | if (node1 == node2) |
| 532 | return(0); |
| 533 | if (node1 == node2->prev) |
| 534 | return(1); |
| 535 | if (node1 == node2->next) |
| 536 | return(-1); |
| 537 | |
| 538 | /* |
| 539 | * compute depth to root |
| 540 | */ |
| 541 | for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) { |
| 542 | if (cur == node1) |
| 543 | return(1); |
| 544 | depth2++; |
| 545 | } |
| 546 | root = cur; |
| 547 | for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) { |
| 548 | if (cur == node2) |
| 549 | return(-1); |
| 550 | depth1++; |
| 551 | } |
| 552 | /* |
| 553 | * Distinct document (or distinct entities :-( ) case. |
| 554 | */ |
| 555 | if (root != cur) { |
| 556 | return(-2); |
| 557 | } |
| 558 | /* |
| 559 | * get the nearest common ancestor. |
| 560 | */ |
| 561 | while (depth1 > depth2) { |
| 562 | depth1--; |
| 563 | node1 = node1->parent; |
| 564 | } |
| 565 | while (depth2 > depth1) { |
| 566 | depth2--; |
| 567 | node2 = node2->parent; |
| 568 | } |
| 569 | while (node1->parent != node2->parent) { |
| 570 | node1 = node1->parent; |
| 571 | node2 = node2->parent; |
| 572 | /* should not happen but just in case ... */ |
| 573 | if ((node1 == NULL) || (node2 == NULL)) |
| 574 | return(-2); |
| 575 | } |
| 576 | /* |
| 577 | * Find who's first. |
| 578 | */ |
| 579 | if (node1 == node2->next) |
| 580 | return(-1); |
| 581 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 582 | if (cur == node2) |
| 583 | return(1); |
| 584 | return(-1); /* assume there is no sibling list corruption */ |
| 585 | } |
| 586 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 587 | #define XML_NODESET_DEFAULT 10 |
| 588 | /** |
| 589 | * xmlXPathNodeSetCreate: |
| 590 | * @val: an initial xmlNodePtr, or NULL |
| 591 | * |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 592 | * Create a new xmlNodeSetPtr of type double and of value @val |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 593 | * |
| 594 | * Returns the newly created object. |
| 595 | */ |
| 596 | xmlNodeSetPtr |
| 597 | xmlXPathNodeSetCreate(xmlNodePtr val) { |
| 598 | xmlNodeSetPtr ret; |
| 599 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 600 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 601 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 602 | xmlGenericError(xmlGenericErrorContext, |
| 603 | "xmlXPathNewNodeSet: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 604 | return(NULL); |
| 605 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 606 | memset(ret, 0 , (size_t) sizeof(xmlNodeSet)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 607 | if (val != NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 608 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 609 | sizeof(xmlNodePtr)); |
| 610 | if (ret->nodeTab == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 611 | xmlGenericError(xmlGenericErrorContext, |
| 612 | "xmlXPathNewNodeSet: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 613 | return(NULL); |
| 614 | } |
| 615 | memset(ret->nodeTab, 0 , |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 616 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 617 | ret->nodeMax = XML_NODESET_DEFAULT; |
| 618 | ret->nodeTab[ret->nodeNr++] = val; |
| 619 | } |
| 620 | return(ret); |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * xmlXPathNodeSetAdd: |
| 625 | * @cur: the initial node set |
| 626 | * @val: a new xmlNodePtr |
| 627 | * |
| 628 | * add a new xmlNodePtr ot an existing NodeSet |
| 629 | */ |
| 630 | void |
| 631 | xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 632 | int i; |
| 633 | |
| 634 | if (val == NULL) return; |
| 635 | |
| 636 | /* |
| 637 | * check against doublons |
| 638 | */ |
| 639 | for (i = 0;i < cur->nodeNr;i++) |
| 640 | if (cur->nodeTab[i] == val) return; |
| 641 | |
| 642 | /* |
| 643 | * grow the nodeTab if needed |
| 644 | */ |
| 645 | if (cur->nodeMax == 0) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 646 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 647 | sizeof(xmlNodePtr)); |
| 648 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 649 | xmlGenericError(xmlGenericErrorContext, |
| 650 | "xmlXPathNodeSetAdd: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | memset(cur->nodeTab, 0 , |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 654 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 655 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 656 | } else if (cur->nodeNr == cur->nodeMax) { |
| 657 | xmlNodePtr *temp; |
| 658 | |
| 659 | cur->nodeMax *= 2; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 660 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 661 | sizeof(xmlNodePtr)); |
| 662 | if (temp == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 663 | xmlGenericError(xmlGenericErrorContext, |
| 664 | "xmlXPathNodeSetAdd: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 665 | return; |
| 666 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 667 | cur->nodeTab = temp; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 668 | } |
| 669 | cur->nodeTab[cur->nodeNr++] = val; |
| 670 | } |
| 671 | |
| 672 | /** |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 673 | * xmlXPathNodeSetAddUnique: |
| 674 | * @cur: the initial node set |
| 675 | * @val: a new xmlNodePtr |
| 676 | * |
| 677 | * add a new xmlNodePtr ot an existing NodeSet, optimized version |
| 678 | * when we are sure the node is not already in the set. |
| 679 | */ |
| 680 | void |
| 681 | xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 682 | if (val == NULL) return; |
| 683 | |
| 684 | /* |
| 685 | * grow the nodeTab if needed |
| 686 | */ |
| 687 | if (cur->nodeMax == 0) { |
| 688 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 689 | sizeof(xmlNodePtr)); |
| 690 | if (cur->nodeTab == NULL) { |
| 691 | xmlGenericError(xmlGenericErrorContext, |
| 692 | "xmlXPathNodeSetAddUnique: out of memory\n"); |
| 693 | return; |
| 694 | } |
| 695 | memset(cur->nodeTab, 0 , |
| 696 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 697 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 698 | } else if (cur->nodeNr == cur->nodeMax) { |
| 699 | xmlNodePtr *temp; |
| 700 | |
| 701 | cur->nodeMax *= 2; |
| 702 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 703 | sizeof(xmlNodePtr)); |
| 704 | if (temp == NULL) { |
| 705 | xmlGenericError(xmlGenericErrorContext, |
| 706 | "xmlXPathNodeSetAddUnique: out of memory\n"); |
| 707 | return; |
| 708 | } |
| 709 | cur->nodeTab = temp; |
| 710 | } |
| 711 | cur->nodeTab[cur->nodeNr++] = val; |
| 712 | } |
| 713 | |
| 714 | /** |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 715 | * xmlXPathNodeSetMerge: |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 716 | * @val1: the first NodeSet or NULL |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 717 | * @val2: the second NodeSet |
| 718 | * |
| 719 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 720 | * if @val1 is NULL, a new set is created and copied from @val2 |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 721 | * |
| 722 | * Returns val1 once extended or NULL in case of error. |
| 723 | */ |
| 724 | xmlNodeSetPtr |
| 725 | xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 726 | int i, j, initNr; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 727 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 728 | if (val2 == NULL) return(val1); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 729 | if (val1 == NULL) { |
| 730 | val1 = xmlXPathNodeSetCreate(NULL); |
| 731 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 732 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 733 | initNr = val1->nodeNr; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 734 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 735 | for (i = 0;i < val2->nodeNr;i++) { |
| 736 | /* |
| 737 | * check against doublons |
| 738 | */ |
| 739 | for (j = 0; j < initNr; j++) |
| 740 | if (val1->nodeTab[j] == val2->nodeTab[i]) continue; |
| 741 | |
| 742 | /* |
| 743 | * grow the nodeTab if needed |
| 744 | */ |
| 745 | if (val1->nodeMax == 0) { |
| 746 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 747 | sizeof(xmlNodePtr)); |
| 748 | if (val1->nodeTab == NULL) { |
| 749 | xmlGenericError(xmlGenericErrorContext, |
| 750 | "xmlXPathNodeSetMerge: out of memory\n"); |
| 751 | return(NULL); |
| 752 | } |
| 753 | memset(val1->nodeTab, 0 , |
| 754 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 755 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 756 | } else if (val1->nodeNr == val1->nodeMax) { |
| 757 | xmlNodePtr *temp; |
| 758 | |
| 759 | val1->nodeMax *= 2; |
| 760 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 761 | sizeof(xmlNodePtr)); |
| 762 | if (temp == NULL) { |
| 763 | xmlGenericError(xmlGenericErrorContext, |
| 764 | "xmlXPathNodeSetMerge: out of memory\n"); |
| 765 | return(NULL); |
| 766 | } |
| 767 | val1->nodeTab = temp; |
| 768 | } |
| 769 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
| 770 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 771 | |
| 772 | return(val1); |
| 773 | } |
| 774 | |
| 775 | /** |
| 776 | * xmlXPathNodeSetDel: |
| 777 | * @cur: the initial node set |
| 778 | * @val: an xmlNodePtr |
| 779 | * |
| 780 | * Removes an xmlNodePtr from an existing NodeSet |
| 781 | */ |
| 782 | void |
| 783 | xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 784 | int i; |
| 785 | |
| 786 | if (cur == NULL) return; |
| 787 | if (val == NULL) return; |
| 788 | |
| 789 | /* |
| 790 | * check against doublons |
| 791 | */ |
| 792 | for (i = 0;i < cur->nodeNr;i++) |
| 793 | if (cur->nodeTab[i] == val) break; |
| 794 | |
| 795 | if (i >= cur->nodeNr) { |
| 796 | #ifdef DEBUG |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 797 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 798 | "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n", |
| 799 | val->name); |
| 800 | #endif |
| 801 | return; |
| 802 | } |
| 803 | cur->nodeNr--; |
| 804 | for (;i < cur->nodeNr;i++) |
| 805 | cur->nodeTab[i] = cur->nodeTab[i + 1]; |
| 806 | cur->nodeTab[cur->nodeNr] = NULL; |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * xmlXPathNodeSetRemove: |
| 811 | * @cur: the initial node set |
| 812 | * @val: the index to remove |
| 813 | * |
| 814 | * Removes an entry from an existing NodeSet list. |
| 815 | */ |
| 816 | void |
| 817 | xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) { |
| 818 | if (cur == NULL) return; |
| 819 | if (val >= cur->nodeNr) return; |
| 820 | cur->nodeNr--; |
| 821 | for (;val < cur->nodeNr;val++) |
| 822 | cur->nodeTab[val] = cur->nodeTab[val + 1]; |
| 823 | cur->nodeTab[cur->nodeNr] = NULL; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * xmlXPathFreeNodeSet: |
| 828 | * @obj: the xmlNodeSetPtr to free |
| 829 | * |
| 830 | * Free the NodeSet compound (not the actual nodes !). |
| 831 | */ |
| 832 | void |
| 833 | xmlXPathFreeNodeSet(xmlNodeSetPtr obj) { |
| 834 | if (obj == NULL) return; |
| 835 | if (obj->nodeTab != NULL) { |
| 836 | #ifdef DEBUG |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 837 | memset(obj->nodeTab, 0xB , (size_t) sizeof(xmlNodePtr) * obj->nodeMax); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 838 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 839 | xmlFree(obj->nodeTab); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 840 | } |
| 841 | #ifdef DEBUG |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 842 | memset(obj, 0xB , (size_t) sizeof(xmlNodeSet)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 843 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 844 | xmlFree(obj); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Daniel Veillard | 8a7642f | 2001-01-22 10:45:16 +0000 | [diff] [blame] | 847 | /** |
| 848 | * xmlXPathFreeValueTree: |
| 849 | * @obj: the xmlNodeSetPtr to free |
| 850 | * |
| 851 | * Free the NodeSet compound and the actual tree, this is different |
| 852 | * from xmlXPathFreeNodeSet() |
| 853 | */ |
| 854 | void |
| 855 | xmlXPathFreeValueTree(xmlNodeSetPtr obj) { |
| 856 | int i; |
| 857 | |
| 858 | if (obj == NULL) return; |
| 859 | for (i = 0;i < obj->nodeNr;i++) |
| 860 | if (obj->nodeTab[i] != NULL) |
| 861 | xmlFreeNode(obj->nodeTab[i]); |
| 862 | |
| 863 | if (obj->nodeTab != NULL) { |
| 864 | #ifdef DEBUG |
| 865 | memset(obj->nodeTab, 0xB , (size_t) sizeof(xmlNodePtr) * obj->nodeMax); |
| 866 | #endif |
| 867 | xmlFree(obj->nodeTab); |
| 868 | } |
| 869 | #ifdef DEBUG |
| 870 | memset(obj, 0xB , (size_t) sizeof(xmlNodeSet)); |
| 871 | #endif |
| 872 | xmlFree(obj); |
| 873 | } |
| 874 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 875 | #if defined(DEBUG) || defined(DEBUG_STEP) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 876 | /** |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 877 | * xmlGenericErrorContextNodeSet: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 878 | * @output: a FILE * for the output |
| 879 | * @obj: the xmlNodeSetPtr to free |
| 880 | * |
| 881 | * Quick display of a NodeSet |
| 882 | */ |
| 883 | void |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 884 | xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 885 | int i; |
| 886 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 887 | if (output == NULL) output = xmlGenericErrorContext; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 888 | if (obj == NULL) { |
| 889 | fprintf(output, "NodeSet == NULL !\n"); |
| 890 | return; |
| 891 | } |
| 892 | if (obj->nodeNr == 0) { |
| 893 | fprintf(output, "NodeSet is empty\n"); |
| 894 | return; |
| 895 | } |
| 896 | if (obj->nodeTab == NULL) { |
| 897 | fprintf(output, " nodeTab == NULL !\n"); |
| 898 | return; |
| 899 | } |
| 900 | for (i = 0; i < obj->nodeNr; i++) { |
| 901 | if (obj->nodeTab[i] == NULL) { |
| 902 | fprintf(output, " NULL !\n"); |
| 903 | return; |
| 904 | } |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 905 | if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) || |
| 906 | (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE)) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 907 | fprintf(output, " /"); |
| 908 | else if (obj->nodeTab[i]->name == NULL) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 909 | fprintf(output, " noname!"); |
| 910 | else fprintf(output, " %s", obj->nodeTab[i]->name); |
| 911 | } |
| 912 | fprintf(output, "\n"); |
| 913 | } |
| 914 | #endif |
| 915 | |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 916 | /** |
| 917 | * xmlXPathNewNodeSet: |
| 918 | * @val: the NodePtr value |
| 919 | * |
| 920 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 921 | * it with the single Node @val |
| 922 | * |
| 923 | * Returns the newly created object. |
| 924 | */ |
| 925 | xmlXPathObjectPtr |
| 926 | xmlXPathNewNodeSet(xmlNodePtr val) { |
| 927 | xmlXPathObjectPtr ret; |
| 928 | |
| 929 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 930 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 931 | xmlGenericError(xmlGenericErrorContext, |
| 932 | "xmlXPathNewNodeSet: out of memory\n"); |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 933 | return(NULL); |
| 934 | } |
| 935 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 936 | ret->type = XPATH_NODESET; |
| 937 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
| 938 | return(ret); |
| 939 | } |
| 940 | |
| 941 | /** |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 942 | * xmlXPathNewValueTree: |
| 943 | * @val: the NodePtr value |
| 944 | * |
| 945 | * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize |
| 946 | * it with the tree root @val |
| 947 | * |
| 948 | * Returns the newly created object. |
| 949 | */ |
| 950 | xmlXPathObjectPtr |
| 951 | xmlXPathNewValueTree(xmlNodePtr val) { |
| 952 | xmlXPathObjectPtr ret; |
| 953 | |
| 954 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 955 | if (ret == NULL) { |
| 956 | xmlGenericError(xmlGenericErrorContext, |
| 957 | "xmlXPathNewNodeSet: out of memory\n"); |
| 958 | return(NULL); |
| 959 | } |
| 960 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 961 | ret->type = XPATH_XSLT_TREE; |
| 962 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
| 963 | return(ret); |
| 964 | } |
| 965 | |
| 966 | /** |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 967 | * xmlXPathNewNodeSetList: |
| 968 | * @val: an existing NodeSet |
| 969 | * |
| 970 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 971 | * it with the Nodeset @val |
| 972 | * |
| 973 | * Returns the newly created object. |
| 974 | */ |
| 975 | xmlXPathObjectPtr |
| 976 | xmlXPathNewNodeSetList(xmlNodeSetPtr val) { |
| 977 | xmlXPathObjectPtr ret; |
| 978 | int i; |
| 979 | |
| 980 | if (val == NULL) |
| 981 | ret = NULL; |
| 982 | else if (val->nodeTab == NULL) |
| 983 | ret = xmlXPathNewNodeSet(NULL); |
| 984 | else |
| 985 | { |
| 986 | ret = xmlXPathNewNodeSet(val->nodeTab[0]); |
| 987 | for (i = 1; i < val->nodeNr; ++i) |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 988 | xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]); |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | return(ret); |
| 992 | } |
| 993 | |
| 994 | /** |
| 995 | * xmlXPathWrapNodeSet: |
| 996 | * @val: the NodePtr value |
| 997 | * |
| 998 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 999 | * |
| 1000 | * Returns the newly created object. |
| 1001 | */ |
| 1002 | xmlXPathObjectPtr |
| 1003 | xmlXPathWrapNodeSet(xmlNodeSetPtr val) { |
| 1004 | xmlXPathObjectPtr ret; |
| 1005 | |
| 1006 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 1007 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1008 | xmlGenericError(xmlGenericErrorContext, |
| 1009 | "xmlXPathWrapNodeSet: out of memory\n"); |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 1010 | return(NULL); |
| 1011 | } |
| 1012 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 1013 | ret->type = XPATH_NODESET; |
| 1014 | ret->nodesetval = val; |
| 1015 | return(ret); |
| 1016 | } |
| 1017 | |
| 1018 | /** |
| 1019 | * xmlXPathFreeNodeSetList: |
| 1020 | * @obj: an existing NodeSetList object |
| 1021 | * |
| 1022 | * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in |
| 1023 | * the list contrary to xmlXPathFreeObject(). |
| 1024 | */ |
| 1025 | void |
| 1026 | xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) { |
| 1027 | if (obj == NULL) return; |
| 1028 | #ifdef DEBUG |
| 1029 | memset(obj, 0xB , (size_t) sizeof(xmlXPathObject)); |
| 1030 | #endif |
| 1031 | xmlFree(obj); |
| 1032 | } |
| 1033 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1034 | /************************************************************************ |
| 1035 | * * |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1036 | * Routines to handle extra functions * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1037 | * * |
| 1038 | ************************************************************************/ |
| 1039 | |
| 1040 | /** |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1041 | * xmlXPathRegisterFunc: |
| 1042 | * @ctxt: the XPath context |
| 1043 | * @name: the function name |
| 1044 | * @f: the function implementation or NULL |
| 1045 | * |
| 1046 | * Register a new function. If @f is NULL it unregisters the function |
| 1047 | * |
| 1048 | * Returns 0 in case of success, -1 in case of error |
| 1049 | */ |
| 1050 | int |
| 1051 | xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 1052 | xmlXPathFunction f) { |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1053 | return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f)); |
| 1054 | } |
| 1055 | |
| 1056 | /** |
| 1057 | * xmlXPathRegisterFuncNS: |
| 1058 | * @ctxt: the XPath context |
| 1059 | * @name: the function name |
| 1060 | * @ns_uri: the function namespace URI |
| 1061 | * @f: the function implementation or NULL |
| 1062 | * |
| 1063 | * Register a new function. If @f is NULL it unregisters the function |
| 1064 | * |
| 1065 | * Returns 0 in case of success, -1 in case of error |
| 1066 | */ |
| 1067 | int |
| 1068 | xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 1069 | const xmlChar *ns_uri, xmlXPathFunction f) { |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1070 | if (ctxt == NULL) |
| 1071 | return(-1); |
| 1072 | if (name == NULL) |
| 1073 | return(-1); |
| 1074 | |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 1075 | if (ctxt->funcHash == NULL) |
| 1076 | ctxt->funcHash = xmlHashCreate(0); |
| 1077 | if (ctxt->funcHash == NULL) |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1078 | return(-1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1079 | return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, (void *) f)); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | /** |
| 1083 | * xmlXPathFunctionLookup: |
| 1084 | * @ctxt: the XPath context |
| 1085 | * @name: the function name |
| 1086 | * |
| 1087 | * Search in the Function array of the context for the given |
| 1088 | * function. |
| 1089 | * |
| 1090 | * Returns the xmlXPathFunction or NULL if not found |
| 1091 | */ |
| 1092 | xmlXPathFunction |
| 1093 | xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1094 | return(xmlXPathFunctionLookupNS(ctxt, name, NULL)); |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * xmlXPathFunctionLookupNS: |
| 1099 | * @ctxt: the XPath context |
| 1100 | * @name: the function name |
| 1101 | * @ns_uri: the function namespace URI |
| 1102 | * |
| 1103 | * Search in the Function array of the context for the given |
| 1104 | * function. |
| 1105 | * |
| 1106 | * Returns the xmlXPathFunction or NULL if not found |
| 1107 | */ |
| 1108 | xmlXPathFunction |
| 1109 | xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 1110 | const xmlChar *ns_uri) { |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1111 | if (ctxt == NULL) |
| 1112 | return(NULL); |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 1113 | if (ctxt->funcHash == NULL) |
| 1114 | return(NULL); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1115 | if (name == NULL) |
| 1116 | return(NULL); |
| 1117 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1118 | return((xmlXPathFunction) xmlHashLookup2(ctxt->funcHash, name, ns_uri)); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * xmlXPathRegisteredFuncsCleanup: |
| 1123 | * @ctxt: the XPath context |
| 1124 | * |
| 1125 | * Cleanup the XPath context data associated to registered functions |
| 1126 | */ |
| 1127 | void |
| 1128 | xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) { |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1129 | if (ctxt == NULL) |
| 1130 | return; |
| 1131 | |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 1132 | xmlHashFree(ctxt->funcHash, NULL); |
| 1133 | ctxt->funcHash = NULL; |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | /************************************************************************ |
| 1137 | * * |
| 1138 | * Routines to handle Variable * |
| 1139 | * * |
| 1140 | ************************************************************************/ |
| 1141 | |
| 1142 | /** |
| 1143 | * xmlXPathRegisterVariable: |
| 1144 | * @ctxt: the XPath context |
| 1145 | * @name: the variable name |
| 1146 | * @value: the variable value or NULL |
| 1147 | * |
| 1148 | * Register a new variable value. If @value is NULL it unregisters |
| 1149 | * the variable |
| 1150 | * |
| 1151 | * Returns 0 in case of success, -1 in case of error |
| 1152 | */ |
| 1153 | int |
| 1154 | xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 1155 | xmlXPathObjectPtr value) { |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1156 | return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value)); |
| 1157 | } |
| 1158 | |
| 1159 | /** |
| 1160 | * xmlXPathRegisterVariableNS: |
| 1161 | * @ctxt: the XPath context |
| 1162 | * @name: the variable name |
| 1163 | * @ns_uri: the variable namespace URI |
| 1164 | * @value: the variable value or NULL |
| 1165 | * |
| 1166 | * Register a new variable value. If @value is NULL it unregisters |
| 1167 | * the variable |
| 1168 | * |
| 1169 | * Returns 0 in case of success, -1 in case of error |
| 1170 | */ |
| 1171 | int |
| 1172 | xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 1173 | const xmlChar *ns_uri, |
| 1174 | xmlXPathObjectPtr value) { |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1175 | if (ctxt == NULL) |
| 1176 | return(-1); |
| 1177 | if (name == NULL) |
| 1178 | return(-1); |
| 1179 | |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 1180 | if (ctxt->varHash == NULL) |
| 1181 | ctxt->varHash = xmlHashCreate(0); |
| 1182 | if (ctxt->varHash == NULL) |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1183 | return(-1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1184 | return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri, |
| 1185 | (void *) value, |
| 1186 | (xmlHashDeallocator)xmlXPathFreeObject)); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /** |
Daniel Veillard | 8f4d975 | 2001-01-19 05:32:34 +0000 | [diff] [blame] | 1190 | * xmlXPathRegisterVariableLookup: |
| 1191 | * @ctxt: the XPath context |
| 1192 | * @f: the lookup function |
| 1193 | * @data: the lookup data |
| 1194 | * |
| 1195 | * register an external mechanism to do variable lookup |
| 1196 | */ |
| 1197 | void |
| 1198 | xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt, |
| 1199 | xmlXPathVariableLookupFunc f, void *data) { |
| 1200 | if (ctxt == NULL) |
| 1201 | return; |
| 1202 | ctxt->varLookupFunc = (void *) f; |
| 1203 | ctxt->varLookupData = data; |
| 1204 | } |
| 1205 | |
| 1206 | /** |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1207 | * xmlXPathVariableLookup: |
| 1208 | * @ctxt: the XPath context |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1209 | * @name: the variable name |
| 1210 | * |
| 1211 | * Search in the Variable array of the context for the given |
| 1212 | * variable value. |
| 1213 | * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1214 | * Returns the value or NULL if not found |
| 1215 | */ |
| 1216 | xmlXPathObjectPtr |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1217 | xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Daniel Veillard | 8f4d975 | 2001-01-19 05:32:34 +0000 | [diff] [blame] | 1218 | if (ctxt == NULL) |
| 1219 | return(NULL); |
| 1220 | |
| 1221 | if (ctxt->varLookupFunc != NULL) { |
| 1222 | xmlXPathObjectPtr ret; |
| 1223 | |
| 1224 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 1225 | (ctxt->varLookupData, name, NULL); |
| 1226 | if (ret != NULL) return(ret); |
| 1227 | } |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1228 | return(xmlXPathVariableLookupNS(ctxt, name, NULL)); |
| 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * xmlXPathVariableLookupNS: |
| 1233 | * @ctxt: the XPath context |
| 1234 | * @name: the variable name |
| 1235 | * @ns_uri: the variable namespace URI |
| 1236 | * |
| 1237 | * Search in the Variable array of the context for the given |
| 1238 | * variable value. |
| 1239 | * |
| 1240 | * Returns the value or NULL if not found |
| 1241 | */ |
| 1242 | xmlXPathObjectPtr |
| 1243 | xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 1244 | const xmlChar *ns_uri) { |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1245 | if (ctxt == NULL) |
| 1246 | return(NULL); |
Daniel Veillard | 8f4d975 | 2001-01-19 05:32:34 +0000 | [diff] [blame] | 1247 | |
| 1248 | if (ctxt->varLookupFunc != NULL) { |
| 1249 | xmlXPathObjectPtr ret; |
| 1250 | |
| 1251 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 1252 | (ctxt->varLookupData, name, ns_uri); |
| 1253 | if (ret != NULL) return(ret); |
| 1254 | } |
| 1255 | |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 1256 | if (ctxt->varHash == NULL) |
| 1257 | return(NULL); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1258 | if (name == NULL) |
| 1259 | return(NULL); |
| 1260 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1261 | return((xmlXPathObjectPtr) xmlHashLookup2(ctxt->varHash, name, ns_uri)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1264 | /** |
| 1265 | * xmlXPathRegisteredVariablesCleanup: |
| 1266 | * @ctxt: the XPath context |
| 1267 | * |
| 1268 | * Cleanup the XPath context data associated to registered variables |
| 1269 | */ |
| 1270 | void |
| 1271 | xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) { |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1272 | if (ctxt == NULL) |
| 1273 | return; |
| 1274 | |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 1275 | xmlHashFree(ctxt->varHash, NULL); |
| 1276 | ctxt->varHash = NULL; |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 1279 | /** |
| 1280 | * xmlXPathRegisterNs: |
| 1281 | * @ctxt: the XPath context |
| 1282 | * @prefix: the namespace prefix |
| 1283 | * @ns_uri: the namespace name |
| 1284 | * |
| 1285 | * Register a new namespace. If @ns_uri is NULL it unregisters |
| 1286 | * the namespace |
| 1287 | * |
| 1288 | * Returns 0 in case of success, -1 in case of error |
| 1289 | */ |
| 1290 | int |
| 1291 | xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix, |
| 1292 | const xmlChar *ns_uri) { |
| 1293 | if (ctxt == NULL) |
| 1294 | return(-1); |
| 1295 | if (prefix == NULL) |
| 1296 | return(-1); |
| 1297 | |
| 1298 | if (ctxt->nsHash == NULL) |
| 1299 | ctxt->nsHash = xmlHashCreate(10); |
| 1300 | if (ctxt->nsHash == NULL) |
| 1301 | return(-1); |
| 1302 | return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) ns_uri, |
| 1303 | (xmlHashDeallocator)xmlFree)); |
| 1304 | } |
| 1305 | |
| 1306 | /** |
| 1307 | * xmlXPathNsLookup: |
| 1308 | * @ctxt: the XPath context |
| 1309 | * @prefix: the namespace prefix value |
| 1310 | * |
| 1311 | * Search in the namespace declaration array of the context for the given |
| 1312 | * namespace name associated to the given prefix |
| 1313 | * |
| 1314 | * Returns the value or NULL if not found |
| 1315 | */ |
| 1316 | const xmlChar * |
| 1317 | xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) { |
| 1318 | if (ctxt == NULL) |
| 1319 | return(NULL); |
| 1320 | if (prefix == NULL) |
| 1321 | return(NULL); |
| 1322 | if (ctxt->nsHash == NULL) |
| 1323 | return(NULL); |
| 1324 | |
| 1325 | return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix)); |
| 1326 | } |
| 1327 | |
| 1328 | /** |
| 1329 | * xmlXPathRegisteredVariablesCleanup: |
| 1330 | * @ctxt: the XPath context |
| 1331 | * |
| 1332 | * Cleanup the XPath context data associated to registered variables |
| 1333 | */ |
| 1334 | void |
| 1335 | xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) { |
| 1336 | if (ctxt == NULL) |
| 1337 | return; |
| 1338 | |
| 1339 | xmlHashFree(ctxt->nsHash, NULL); |
| 1340 | ctxt->nsHash = NULL; |
| 1341 | } |
| 1342 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1343 | /************************************************************************ |
| 1344 | * * |
| 1345 | * Routines to handle Values * |
| 1346 | * * |
| 1347 | ************************************************************************/ |
| 1348 | |
| 1349 | /* Allocations are terrible, one need to optimize all this !!! */ |
| 1350 | |
| 1351 | /** |
| 1352 | * xmlXPathNewFloat: |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1353 | * @val: the double value |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1354 | * |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1355 | * Create a new xmlXPathObjectPtr of type double and of value @val |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1356 | * |
| 1357 | * Returns the newly created object. |
| 1358 | */ |
| 1359 | xmlXPathObjectPtr |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1360 | xmlXPathNewFloat(double val) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1361 | xmlXPathObjectPtr ret; |
| 1362 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1363 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1364 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1365 | xmlGenericError(xmlGenericErrorContext, |
| 1366 | "xmlXPathNewFloat: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1367 | return(NULL); |
| 1368 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1369 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1370 | ret->type = XPATH_NUMBER; |
| 1371 | ret->floatval = val; |
| 1372 | return(ret); |
| 1373 | } |
| 1374 | |
| 1375 | /** |
| 1376 | * xmlXPathNewBoolean: |
| 1377 | * @val: the boolean value |
| 1378 | * |
| 1379 | * Create a new xmlXPathObjectPtr of type boolean and of value @val |
| 1380 | * |
| 1381 | * Returns the newly created object. |
| 1382 | */ |
| 1383 | xmlXPathObjectPtr |
| 1384 | xmlXPathNewBoolean(int val) { |
| 1385 | xmlXPathObjectPtr ret; |
| 1386 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1387 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1388 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1389 | xmlGenericError(xmlGenericErrorContext, |
| 1390 | "xmlXPathNewBoolean: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1391 | return(NULL); |
| 1392 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1393 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1394 | ret->type = XPATH_BOOLEAN; |
| 1395 | ret->boolval = (val != 0); |
| 1396 | return(ret); |
| 1397 | } |
| 1398 | |
| 1399 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1400 | * xmlXPathNewString: |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1401 | * @val: the xmlChar * value |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1402 | * |
| 1403 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 1404 | * |
| 1405 | * Returns the newly created object. |
| 1406 | */ |
| 1407 | xmlXPathObjectPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1408 | xmlXPathNewString(const xmlChar *val) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1409 | xmlXPathObjectPtr ret; |
| 1410 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1411 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1412 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1413 | xmlGenericError(xmlGenericErrorContext, |
| 1414 | "xmlXPathNewString: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1415 | return(NULL); |
| 1416 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1417 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1418 | ret->type = XPATH_STRING; |
Daniel Veillard | 5a2b697 | 2001-01-20 21:15:50 +0000 | [diff] [blame] | 1419 | if (val != NULL) |
| 1420 | ret->stringval = xmlStrdup(val); |
| 1421 | else |
| 1422 | ret->stringval = xmlStrdup((const xmlChar *)""); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1423 | return(ret); |
| 1424 | } |
| 1425 | |
| 1426 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1427 | * xmlXPathNewCString: |
| 1428 | * @val: the char * value |
| 1429 | * |
| 1430 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 1431 | * |
| 1432 | * Returns the newly created object. |
| 1433 | */ |
| 1434 | xmlXPathObjectPtr |
| 1435 | xmlXPathNewCString(const char *val) { |
| 1436 | xmlXPathObjectPtr ret; |
| 1437 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1438 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1439 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1440 | xmlGenericError(xmlGenericErrorContext, |
| 1441 | "xmlXPathNewCString: out of memory\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1442 | return(NULL); |
| 1443 | } |
| 1444 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 1445 | ret->type = XPATH_STRING; |
| 1446 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 1447 | return(ret); |
| 1448 | } |
| 1449 | |
| 1450 | /** |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1451 | * xmlXPathObjectCopy: |
| 1452 | * @val: the original object |
| 1453 | * |
| 1454 | * allocate a new copy of a given object |
| 1455 | * |
| 1456 | * Returns the newly created object. |
| 1457 | */ |
| 1458 | xmlXPathObjectPtr |
| 1459 | xmlXPathObjectCopy(xmlXPathObjectPtr val) { |
| 1460 | xmlXPathObjectPtr ret; |
| 1461 | |
| 1462 | if (val == NULL) |
| 1463 | return(NULL); |
| 1464 | |
| 1465 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 1466 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1467 | xmlGenericError(xmlGenericErrorContext, |
| 1468 | "xmlXPathObjectCopy: out of memory\n"); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1469 | return(NULL); |
| 1470 | } |
| 1471 | memcpy(ret, val , (size_t) sizeof(xmlXPathObject)); |
| 1472 | switch (val->type) { |
| 1473 | case XPATH_BOOLEAN: |
| 1474 | case XPATH_NUMBER: |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1475 | case XPATH_POINT: |
| 1476 | case XPATH_RANGE: |
| 1477 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 1478 | case XPATH_STRING: |
| 1479 | ret->stringval = xmlStrdup(val->stringval); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1480 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 1481 | case XPATH_XSLT_TREE: |
Daniel Veillard | 8a7642f | 2001-01-22 10:45:16 +0000 | [diff] [blame] | 1482 | if ((val->nodesetval != NULL) && |
| 1483 | (val->nodesetval->nodeTab != NULL)) |
| 1484 | ret->nodesetval = xmlXPathNodeSetCreate( |
| 1485 | xmlCopyNode(val->nodesetval->nodeTab[0], 1)); |
| 1486 | else |
| 1487 | ret->nodesetval = xmlXPathNodeSetCreate(NULL); |
| 1488 | break; |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1489 | case XPATH_NODESET: |
| 1490 | ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval); |
| 1491 | break; |
| 1492 | case XPATH_LOCATIONSET: |
| 1493 | #ifdef LIBXML_XPTR_ENABLED |
| 1494 | { |
| 1495 | xmlLocationSetPtr loc = val->user; |
| 1496 | ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc); |
| 1497 | break; |
| 1498 | } |
| 1499 | #endif |
| 1500 | case XPATH_UNDEFINED: |
| 1501 | case XPATH_USERS: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1502 | xmlGenericError(xmlGenericErrorContext, |
| 1503 | "xmlXPathObjectCopy: unsupported type %d\n", |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1504 | val->type); |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 1505 | break; |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1506 | } |
| 1507 | return(ret); |
| 1508 | } |
| 1509 | |
| 1510 | /** |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1511 | * xmlXPathFreeObject: |
| 1512 | * @obj: the object to free |
| 1513 | * |
| 1514 | * Free up an xmlXPathObjectPtr object. |
| 1515 | */ |
| 1516 | void |
| 1517 | xmlXPathFreeObject(xmlXPathObjectPtr obj) { |
| 1518 | if (obj == NULL) return; |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 1519 | if (obj->type == XPATH_NODESET) { |
| 1520 | if (obj->nodesetval != NULL) |
| 1521 | xmlXPathFreeNodeSet(obj->nodesetval); |
Daniel Veillard | b71379b | 2000-10-09 12:30:39 +0000 | [diff] [blame] | 1522 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 1523 | } else if (obj->type == XPATH_LOCATIONSET) { |
| 1524 | if (obj->user != NULL) |
Daniel Veillard | c8df0aa | 2000-10-10 23:50:30 +0000 | [diff] [blame] | 1525 | xmlXPtrFreeLocationSet(obj->user); |
Daniel Veillard | b71379b | 2000-10-09 12:30:39 +0000 | [diff] [blame] | 1526 | #endif |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 1527 | } else if (obj->type == XPATH_STRING) { |
| 1528 | if (obj->stringval != NULL) |
| 1529 | xmlFree(obj->stringval); |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 1530 | } else if (obj->type == XPATH_XSLT_TREE) { |
| 1531 | if (obj->nodesetval != NULL) |
Daniel Veillard | 8a7642f | 2001-01-22 10:45:16 +0000 | [diff] [blame] | 1532 | xmlXPathFreeValueTree(obj->nodesetval); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1535 | #ifdef DEBUG |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1536 | memset(obj, 0xB , (size_t) sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1537 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1538 | xmlFree(obj); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | /************************************************************************ |
| 1542 | * * |
| 1543 | * Routines to handle XPath contexts * |
| 1544 | * * |
| 1545 | ************************************************************************/ |
| 1546 | |
| 1547 | /** |
| 1548 | * xmlXPathNewContext: |
| 1549 | * @doc: the XML document |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1550 | * |
| 1551 | * Create a new xmlXPathContext |
| 1552 | * |
| 1553 | * Returns the xmlXPathContext just allocated. |
| 1554 | */ |
| 1555 | xmlXPathContextPtr |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1556 | xmlXPathNewContext(xmlDocPtr doc) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1557 | xmlXPathContextPtr ret; |
| 1558 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1559 | ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1560 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1561 | xmlGenericError(xmlGenericErrorContext, |
| 1562 | "xmlXPathNewContext: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1563 | return(NULL); |
| 1564 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1565 | memset(ret, 0 , (size_t) sizeof(xmlXPathContext)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1566 | ret->doc = doc; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 1567 | ret->node = NULL; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1568 | |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 1569 | ret->varHash = NULL; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1570 | |
| 1571 | ret->nb_types = 0; |
| 1572 | ret->max_types = 0; |
| 1573 | ret->types = NULL; |
| 1574 | |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 1575 | ret->funcHash = xmlHashCreate(0); |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1576 | |
| 1577 | ret->nb_axis = 0; |
| 1578 | ret->max_axis = 0; |
| 1579 | ret->axis = NULL; |
| 1580 | |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 1581 | ret->nsHash = NULL; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1582 | ret->user = NULL; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 1583 | |
| 1584 | ret->contextSize = -1; |
| 1585 | ret->proximityPosition = -1; |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 1586 | |
| 1587 | xmlXPathRegisterAllFunctions(ret); |
| 1588 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1589 | return(ret); |
| 1590 | } |
| 1591 | |
| 1592 | /** |
| 1593 | * xmlXPathFreeContext: |
| 1594 | * @ctxt: the context to free |
| 1595 | * |
| 1596 | * Free up an xmlXPathContext |
| 1597 | */ |
| 1598 | void |
| 1599 | xmlXPathFreeContext(xmlXPathContextPtr ctxt) { |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 1600 | xmlXPathRegisteredNsCleanup(ctxt); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1601 | xmlXPathRegisteredFuncsCleanup(ctxt); |
| 1602 | xmlXPathRegisteredVariablesCleanup(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1603 | #ifdef DEBUG |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1604 | memset(ctxt, 0xB , (size_t) sizeof(xmlXPathContext)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1605 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1606 | xmlFree(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | /************************************************************************ |
| 1610 | * * |
| 1611 | * Routines to handle XPath parser contexts * |
| 1612 | * * |
| 1613 | ************************************************************************/ |
| 1614 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 1615 | #define CHECK_CTXT(ctxt) \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1616 | if (ctxt == NULL) { \ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1617 | xmlGenericError(xmlGenericErrorContext, \ |
| 1618 | "%s:%d Internal error: ctxt == NULL\n", \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1619 | __FILE__, __LINE__); \ |
| 1620 | } \ |
| 1621 | |
| 1622 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 1623 | #define CHECK_CONTEXT(ctxt) \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1624 | if (ctxt == NULL) { \ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1625 | xmlGenericError(xmlGenericErrorContext, \ |
| 1626 | "%s:%d Internal error: no context\n", \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1627 | __FILE__, __LINE__); \ |
| 1628 | } \ |
Daniel Veillard | df7ef2a | 2000-10-25 10:11:55 +0000 | [diff] [blame] | 1629 | else if (ctxt->doc == NULL) { \ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1630 | xmlGenericError(xmlGenericErrorContext, \ |
| 1631 | "%s:%d Internal error: no document\n", \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1632 | __FILE__, __LINE__); \ |
| 1633 | } \ |
Daniel Veillard | df7ef2a | 2000-10-25 10:11:55 +0000 | [diff] [blame] | 1634 | else if (ctxt->doc->children == NULL) { \ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1635 | xmlGenericError(xmlGenericErrorContext, \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1636 | "%s:%d Internal error: document without root\n", \ |
| 1637 | __FILE__, __LINE__); \ |
| 1638 | } \ |
| 1639 | |
| 1640 | |
| 1641 | /** |
| 1642 | * xmlXPathNewParserContext: |
| 1643 | * @str: the XPath expression |
| 1644 | * @ctxt: the XPath context |
| 1645 | * |
| 1646 | * Create a new xmlXPathParserContext |
| 1647 | * |
| 1648 | * Returns the xmlXPathParserContext just allocated. |
| 1649 | */ |
| 1650 | xmlXPathParserContextPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1651 | xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1652 | xmlXPathParserContextPtr ret; |
| 1653 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1654 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1655 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1656 | xmlGenericError(xmlGenericErrorContext, |
| 1657 | "xmlXPathNewParserContext: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1658 | return(NULL); |
| 1659 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1660 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1661 | ret->cur = ret->base = str; |
| 1662 | ret->context = ctxt; |
| 1663 | |
| 1664 | /* Allocate the value stack */ |
| 1665 | ret->valueTab = (xmlXPathObjectPtr *) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1666 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1667 | ret->valueNr = 0; |
| 1668 | ret->valueMax = 10; |
| 1669 | ret->value = NULL; |
| 1670 | return(ret); |
| 1671 | } |
| 1672 | |
| 1673 | /** |
| 1674 | * xmlXPathFreeParserContext: |
| 1675 | * @ctxt: the context to free |
| 1676 | * |
| 1677 | * Free up an xmlXPathParserContext |
| 1678 | */ |
| 1679 | void |
| 1680 | xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) { |
| 1681 | if (ctxt->valueTab != NULL) { |
| 1682 | #ifdef DEBUG |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1683 | memset(ctxt->valueTab, 0xB , 10 * (size_t) sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1684 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1685 | xmlFree(ctxt->valueTab); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1686 | } |
| 1687 | #ifdef DEBUG |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1688 | memset(ctxt, 0xB , (size_t) sizeof(xmlXPathParserContext)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1689 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1690 | xmlFree(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | /************************************************************************ |
| 1694 | * * |
| 1695 | * The implicit core function library * |
| 1696 | * * |
| 1697 | ************************************************************************/ |
| 1698 | |
| 1699 | /* |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1700 | * Auto-pop and cast to a number |
| 1701 | */ |
| 1702 | void xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs); |
| 1703 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1704 | |
| 1705 | #define POP_FLOAT \ |
| 1706 | arg = valuePop(ctxt); \ |
| 1707 | if (arg == NULL) { \ |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 1708 | XP_ERROR(XPATH_INVALID_OPERAND); \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1709 | } \ |
| 1710 | if (arg->type != XPATH_NUMBER) { \ |
| 1711 | valuePush(ctxt, arg); \ |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1712 | xmlXPathNumberFunction(ctxt, 1); \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1713 | arg = valuePop(ctxt); \ |
| 1714 | } |
| 1715 | |
| 1716 | /** |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 1717 | * xmlXPathCompareNodeSetFloat: |
| 1718 | * @ctxt: the XPath Parser context |
| 1719 | * @inf: less than (1) or greater than (0) |
| 1720 | * @strict: is the comparison strict |
| 1721 | * @arg: the node set |
| 1722 | * @f: the value |
| 1723 | * |
| 1724 | * Implement the compare operation between a nodeset and a number |
| 1725 | * @ns < @val (1, 1, ... |
| 1726 | * @ns <= @val (1, 0, ... |
| 1727 | * @ns > @val (0, 1, ... |
| 1728 | * @ns >= @val (0, 0, ... |
| 1729 | * |
| 1730 | * If one object to be compared is a node-set and the other is a number, |
| 1731 | * then the comparison will be true if and only if there is a node in the |
| 1732 | * node-set such that the result of performing the comparison on the number |
| 1733 | * to be compared and on the result of converting the string-value of that |
| 1734 | * node to a number using the number function is true. |
| 1735 | * |
| 1736 | * Returns 0 or 1 depending on the results of the test. |
| 1737 | */ |
| 1738 | int |
| 1739 | xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 1740 | xmlXPathObjectPtr arg, xmlXPathObjectPtr f) { |
| 1741 | int i, ret = 0; |
| 1742 | xmlNodeSetPtr ns; |
| 1743 | xmlChar *str2; |
| 1744 | |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1745 | if ((f == NULL) || (arg == NULL) || |
| 1746 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 1747 | xmlXPathFreeObject(arg); |
| 1748 | xmlXPathFreeObject(f); |
| 1749 | return(0); |
| 1750 | } |
| 1751 | ns = arg->nodesetval; |
| 1752 | for (i = 0;i < ns->nodeNr;i++) { |
| 1753 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 1754 | if (str2 != NULL) { |
| 1755 | valuePush(ctxt, |
| 1756 | xmlXPathNewString(str2)); |
| 1757 | xmlFree(str2); |
| 1758 | xmlXPathNumberFunction(ctxt, 1); |
| 1759 | valuePush(ctxt, xmlXPathObjectCopy(f)); |
| 1760 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 1761 | if (ret) |
| 1762 | break; |
| 1763 | } |
| 1764 | } |
| 1765 | xmlXPathFreeObject(arg); |
| 1766 | xmlXPathFreeObject(f); |
| 1767 | return(ret); |
| 1768 | } |
| 1769 | |
| 1770 | /** |
| 1771 | * xmlXPathCompareNodeSetString: |
| 1772 | * @ctxt: the XPath Parser context |
| 1773 | * @inf: less than (1) or greater than (0) |
| 1774 | * @strict: is the comparison strict |
| 1775 | * @arg: the node set |
| 1776 | * @s: the value |
| 1777 | * |
| 1778 | * Implement the compare operation between a nodeset and a string |
| 1779 | * @ns < @val (1, 1, ... |
| 1780 | * @ns <= @val (1, 0, ... |
| 1781 | * @ns > @val (0, 1, ... |
| 1782 | * @ns >= @val (0, 0, ... |
| 1783 | * |
| 1784 | * If one object to be compared is a node-set and the other is a string, |
| 1785 | * then the comparison will be true if and only if there is a node in |
| 1786 | * the node-set such that the result of performing the comparison on the |
| 1787 | * string-value of the node and the other string is true. |
| 1788 | * |
| 1789 | * Returns 0 or 1 depending on the results of the test. |
| 1790 | */ |
| 1791 | int |
| 1792 | xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 1793 | xmlXPathObjectPtr arg, xmlXPathObjectPtr s) { |
| 1794 | int i, ret = 0; |
| 1795 | xmlNodeSetPtr ns; |
| 1796 | xmlChar *str2; |
| 1797 | |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1798 | if ((s == NULL) || (arg == NULL) || |
| 1799 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 1800 | xmlXPathFreeObject(arg); |
| 1801 | xmlXPathFreeObject(s); |
| 1802 | return(0); |
| 1803 | } |
| 1804 | ns = arg->nodesetval; |
| 1805 | for (i = 0;i < ns->nodeNr;i++) { |
| 1806 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 1807 | if (str2 != NULL) { |
| 1808 | valuePush(ctxt, |
| 1809 | xmlXPathNewString(str2)); |
| 1810 | xmlFree(str2); |
| 1811 | valuePush(ctxt, xmlXPathObjectCopy(s)); |
| 1812 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 1813 | if (ret) |
| 1814 | break; |
| 1815 | } |
| 1816 | } |
| 1817 | xmlXPathFreeObject(arg); |
| 1818 | xmlXPathFreeObject(s); |
| 1819 | return(ret); |
| 1820 | } |
| 1821 | |
| 1822 | /** |
| 1823 | * xmlXPathCompareNodeSets: |
| 1824 | * @ctxt: the XPath Parser context |
| 1825 | * @op: less than (-1), equal (0) or greater than (1) |
| 1826 | * @strict: is the comparison strict |
| 1827 | * @ns1: the fist node set |
| 1828 | * @ns2: the second node set |
| 1829 | * |
| 1830 | * Implement the compare operation on nodesets: |
| 1831 | * |
| 1832 | * If both objects to be compared are node-sets, then the comparison will be true if |
| 1833 | * and only if there is a node in the first node-set and a node in the second node-set |
| 1834 | * such that the result of performing the comparison on the string-values of the two |
| 1835 | * nodes is true. |
| 1836 | */ |
| 1837 | int |
| 1838 | xmlXPathCompareNodeSets(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 1839 | xmlXPathObjectPtr ns1, xmlXPathObjectPtr ns2) { |
| 1840 | TODO |
| 1841 | return(0); |
| 1842 | } |
| 1843 | |
| 1844 | /** |
| 1845 | * xmlXPathCompareNodeSetValue: |
| 1846 | * @ctxt: the XPath Parser context |
| 1847 | * @inf: less than (1) or greater than (0) |
| 1848 | * @strict: is the comparison strict |
| 1849 | * @arg: the node set |
| 1850 | * @val: the value |
| 1851 | * |
| 1852 | * Implement the compare operation between a nodeset and a value |
| 1853 | * @ns < @val (1, 1, ... |
| 1854 | * @ns <= @val (1, 0, ... |
| 1855 | * @ns > @val (0, 1, ... |
| 1856 | * @ns >= @val (0, 0, ... |
| 1857 | * |
| 1858 | * If one object to be compared is a node-set and the other is a boolean, then the |
| 1859 | * comparison will be true if and only if the result of performing the comparison |
| 1860 | * on the boolean and on the result of converting the node-set to a boolean using |
| 1861 | * the boolean function is true. |
| 1862 | * |
| 1863 | * Returns 0 or 1 depending on the results of the test. |
| 1864 | */ |
| 1865 | int |
| 1866 | xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 1867 | xmlXPathObjectPtr arg, xmlXPathObjectPtr val) { |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1868 | if ((val == NULL) || (arg == NULL) || |
| 1869 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 1870 | return(0); |
| 1871 | |
| 1872 | switch(val->type) { |
| 1873 | case XPATH_NUMBER: |
| 1874 | return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val)); |
| 1875 | case XPATH_NODESET: |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1876 | case XPATH_XSLT_TREE: |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 1877 | return(xmlXPathCompareNodeSets(ctxt, inf, strict, arg, val)); |
| 1878 | case XPATH_STRING: |
| 1879 | return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val)); |
| 1880 | case XPATH_BOOLEAN: |
| 1881 | valuePush(ctxt, arg); |
| 1882 | xmlXPathBooleanFunction(ctxt, 1); |
| 1883 | valuePush(ctxt, val); |
| 1884 | return(xmlXPathCompareValues(ctxt, inf, strict)); |
| 1885 | default: |
| 1886 | TODO |
| 1887 | return(0); |
| 1888 | } |
| 1889 | return(0); |
| 1890 | } |
| 1891 | |
| 1892 | /** |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1893 | * xmlXPathEqualNodeSetString |
| 1894 | * @arg: the nodeset object argument |
| 1895 | * @str: the string to compare to. |
| 1896 | * |
| 1897 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 1898 | * If one object to be compared is a node-set and the other is a string, |
| 1899 | * then the comparison will be true if and only if there is a node in |
| 1900 | * the node-set such that the result of performing the comparison on the |
| 1901 | * string-value of the node and the other string is true. |
| 1902 | * |
| 1903 | * Returns 0 or 1 depending on the results of the test. |
| 1904 | */ |
| 1905 | int |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1906 | xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar *str) { |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1907 | int i; |
| 1908 | xmlNodeSetPtr ns; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1909 | xmlChar *str2; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1910 | |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1911 | if ((str == NULL) || (arg == NULL) || |
| 1912 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1913 | return(0); |
| 1914 | ns = arg->nodesetval; |
| 1915 | for (i = 0;i < ns->nodeNr;i++) { |
| 1916 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 1917 | if ((str2 != NULL) && (xmlStrEqual(str, str2))) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1918 | xmlFree(str2); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1919 | return(1); |
| 1920 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1921 | xmlFree(str2); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1922 | } |
| 1923 | return(0); |
| 1924 | } |
| 1925 | |
| 1926 | /** |
| 1927 | * xmlXPathEqualNodeSetFloat |
| 1928 | * @arg: the nodeset object argument |
| 1929 | * @f: the float to compare to |
| 1930 | * |
| 1931 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 1932 | * If one object to be compared is a node-set and the other is a number, |
| 1933 | * then the comparison will be true if and only if there is a node in |
| 1934 | * the node-set such that the result of performing the comparison on the |
| 1935 | * number to be compared and on the result of converting the string-value |
| 1936 | * of that node to a number using the number function is true. |
| 1937 | * |
| 1938 | * Returns 0 or 1 depending on the results of the test. |
| 1939 | */ |
| 1940 | int |
| 1941 | xmlXPathEqualNodeSetFloat(xmlXPathObjectPtr arg, float f) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1942 | char buf[100] = ""; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1943 | |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1944 | if ((arg == NULL) || |
| 1945 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1946 | return(0); |
| 1947 | |
| 1948 | if (isnan(f)) |
| 1949 | sprintf(buf, "NaN"); |
| 1950 | else if (isinf(f) > 0) |
| 1951 | sprintf(buf, "+Infinity"); |
| 1952 | else if (isinf(f) < 0) |
| 1953 | sprintf(buf, "-Infinity"); |
| 1954 | else |
| 1955 | sprintf(buf, "%0g", f); |
| 1956 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1957 | return(xmlXPathEqualNodeSetString(arg, BAD_CAST buf)); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | |
| 1961 | /** |
| 1962 | * xmlXPathEqualNodeSets |
| 1963 | * @arg1: first nodeset object argument |
| 1964 | * @arg2: second nodeset object argument |
| 1965 | * |
| 1966 | * Implement the equal operation on XPath nodesets: @arg1 == @arg2 |
| 1967 | * If both objects to be compared are node-sets, then the comparison |
| 1968 | * will be true if and only if there is a node in the first node-set and |
| 1969 | * a node in the second node-set such that the result of performing the |
| 1970 | * comparison on the string-values of the two nodes is true. |
| 1971 | * |
| 1972 | * (needless to say, this is a costly operation) |
| 1973 | * |
| 1974 | * Returns 0 or 1 depending on the results of the test. |
| 1975 | */ |
| 1976 | int |
| 1977 | xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
| 1978 | int i; |
| 1979 | xmlNodeSetPtr ns; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1980 | xmlChar *str; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1981 | |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1982 | if ((arg1 == NULL) || |
| 1983 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1984 | return(0); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1985 | if ((arg2 == NULL) || |
| 1986 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1987 | return(0); |
| 1988 | |
| 1989 | ns = arg1->nodesetval; |
| 1990 | for (i = 0;i < ns->nodeNr;i++) { |
| 1991 | str = xmlNodeGetContent(ns->nodeTab[i]); |
| 1992 | if ((str != NULL) && (xmlXPathEqualNodeSetString(arg2, str))) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1993 | xmlFree(str); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1994 | return(1); |
| 1995 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1996 | xmlFree(str); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1997 | } |
| 1998 | return(0); |
| 1999 | } |
| 2000 | |
| 2001 | /** |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2002 | * xmlXPathEqualValues: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2003 | * @ctxt: the XPath Parser context |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2004 | * |
| 2005 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 2006 | * |
| 2007 | * Returns 0 or 1 depending on the results of the test. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2008 | */ |
| 2009 | int |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2010 | xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) { |
| 2011 | xmlXPathObjectPtr arg1, arg2; |
| 2012 | int ret = 0; |
| 2013 | |
| 2014 | arg1 = valuePop(ctxt); |
| 2015 | if (arg1 == NULL) |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 2016 | XP_ERROR0(XPATH_INVALID_OPERAND); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2017 | |
| 2018 | arg2 = valuePop(ctxt); |
| 2019 | if (arg2 == NULL) { |
| 2020 | xmlXPathFreeObject(arg1); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 2021 | XP_ERROR0(XPATH_INVALID_OPERAND); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2024 | if (arg1 == arg2) { |
| 2025 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2026 | xmlGenericError(xmlGenericErrorContext, |
| 2027 | "Equal: by pointer\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2028 | #endif |
| 2029 | return(1); |
| 2030 | } |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2031 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2032 | switch (arg1->type) { |
| 2033 | case XPATH_UNDEFINED: |
| 2034 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2035 | xmlGenericError(xmlGenericErrorContext, |
| 2036 | "Equal: undefined\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2037 | #endif |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2038 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 2039 | case XPATH_XSLT_TREE: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2040 | case XPATH_NODESET: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2041 | switch (arg2->type) { |
| 2042 | case XPATH_UNDEFINED: |
| 2043 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2044 | xmlGenericError(xmlGenericErrorContext, |
| 2045 | "Equal: undefined\n"); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2046 | #endif |
| 2047 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 2048 | case XPATH_XSLT_TREE: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2049 | case XPATH_NODESET: |
| 2050 | ret = xmlXPathEqualNodeSets(arg1, arg2); |
| 2051 | break; |
| 2052 | case XPATH_BOOLEAN: |
| 2053 | if ((arg1->nodesetval == NULL) || |
| 2054 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 2055 | else |
| 2056 | ret = 1; |
| 2057 | ret = (ret == arg2->boolval); |
| 2058 | break; |
| 2059 | case XPATH_NUMBER: |
| 2060 | ret = xmlXPathEqualNodeSetFloat(arg1, arg2->floatval); |
| 2061 | break; |
| 2062 | case XPATH_STRING: |
| 2063 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval); |
| 2064 | break; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2065 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2066 | case XPATH_POINT: |
| 2067 | case XPATH_RANGE: |
| 2068 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2069 | TODO |
| 2070 | break; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2071 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2072 | break; |
| 2073 | case XPATH_BOOLEAN: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2074 | switch (arg2->type) { |
| 2075 | case XPATH_UNDEFINED: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2076 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2077 | xmlGenericError(xmlGenericErrorContext, |
| 2078 | "Equal: undefined\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2079 | #endif |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2080 | break; |
| 2081 | case XPATH_NODESET: |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 2082 | case XPATH_XSLT_TREE: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2083 | if ((arg2->nodesetval == NULL) || |
| 2084 | (arg2->nodesetval->nodeNr == 0)) ret = 0; |
| 2085 | else |
| 2086 | ret = 1; |
| 2087 | break; |
| 2088 | case XPATH_BOOLEAN: |
| 2089 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2090 | xmlGenericError(xmlGenericErrorContext, |
| 2091 | "Equal: %d boolean %d \n", |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2092 | arg1->boolval, arg2->boolval); |
| 2093 | #endif |
| 2094 | ret = (arg1->boolval == arg2->boolval); |
| 2095 | break; |
| 2096 | case XPATH_NUMBER: |
| 2097 | if (arg2->floatval) ret = 1; |
| 2098 | else ret = 0; |
| 2099 | ret = (arg1->boolval == ret); |
| 2100 | break; |
| 2101 | case XPATH_STRING: |
| 2102 | if ((arg2->stringval == NULL) || |
| 2103 | (arg2->stringval[0] == 0)) ret = 0; |
| 2104 | else |
| 2105 | ret = 1; |
| 2106 | ret = (arg1->boolval == ret); |
| 2107 | break; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2108 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2109 | case XPATH_POINT: |
| 2110 | case XPATH_RANGE: |
| 2111 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2112 | TODO |
| 2113 | break; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2114 | } |
| 2115 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2116 | case XPATH_NUMBER: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2117 | switch (arg2->type) { |
| 2118 | case XPATH_UNDEFINED: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2119 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2120 | xmlGenericError(xmlGenericErrorContext, |
| 2121 | "Equal: undefined\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2122 | #endif |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2123 | break; |
| 2124 | case XPATH_NODESET: |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 2125 | case XPATH_XSLT_TREE: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2126 | ret = xmlXPathEqualNodeSetFloat(arg2, arg1->floatval); |
| 2127 | break; |
| 2128 | case XPATH_BOOLEAN: |
| 2129 | if (arg1->floatval) ret = 1; |
| 2130 | else ret = 0; |
| 2131 | ret = (arg2->boolval == ret); |
| 2132 | break; |
| 2133 | case XPATH_STRING: |
| 2134 | valuePush(ctxt, arg2); |
| 2135 | xmlXPathNumberFunction(ctxt, 1); |
| 2136 | arg2 = valuePop(ctxt); |
| 2137 | /* no break on purpose */ |
| 2138 | case XPATH_NUMBER: |
| 2139 | ret = (arg1->floatval == arg2->floatval); |
| 2140 | break; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2141 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2142 | case XPATH_POINT: |
| 2143 | case XPATH_RANGE: |
| 2144 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2145 | TODO |
| 2146 | break; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2147 | } |
| 2148 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2149 | case XPATH_STRING: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2150 | switch (arg2->type) { |
| 2151 | case XPATH_UNDEFINED: |
| 2152 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2153 | xmlGenericError(xmlGenericErrorContext, |
| 2154 | "Equal: undefined\n"); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2155 | #endif |
| 2156 | break; |
| 2157 | case XPATH_NODESET: |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 2158 | case XPATH_XSLT_TREE: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2159 | ret = xmlXPathEqualNodeSetString(arg2, arg1->stringval); |
| 2160 | break; |
| 2161 | case XPATH_BOOLEAN: |
| 2162 | if ((arg1->stringval == NULL) || |
| 2163 | (arg1->stringval[0] == 0)) ret = 0; |
| 2164 | else |
| 2165 | ret = 1; |
| 2166 | ret = (arg2->boolval == ret); |
| 2167 | break; |
| 2168 | case XPATH_STRING: |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 2169 | ret = xmlStrEqual(arg1->stringval, arg2->stringval); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2170 | break; |
| 2171 | case XPATH_NUMBER: |
| 2172 | valuePush(ctxt, arg1); |
| 2173 | xmlXPathNumberFunction(ctxt, 1); |
| 2174 | arg1 = valuePop(ctxt); |
| 2175 | ret = (arg1->floatval == arg2->floatval); |
| 2176 | break; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2177 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2178 | case XPATH_POINT: |
| 2179 | case XPATH_RANGE: |
| 2180 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2181 | TODO |
| 2182 | break; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2183 | } |
| 2184 | break; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2185 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2186 | case XPATH_POINT: |
| 2187 | case XPATH_RANGE: |
| 2188 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2189 | TODO |
| 2190 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2191 | } |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2192 | xmlXPathFreeObject(arg1); |
| 2193 | xmlXPathFreeObject(arg2); |
| 2194 | return(ret); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2197 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2198 | /** |
| 2199 | * xmlXPathCompareValues: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2200 | * @ctxt: the XPath Parser context |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2201 | * @inf: less than (1) or greater than (0) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2202 | * @strict: is the comparison strict |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2203 | * |
| 2204 | * Implement the compare operation on XPath objects: |
| 2205 | * @arg1 < @arg2 (1, 1, ... |
| 2206 | * @arg1 <= @arg2 (1, 0, ... |
| 2207 | * @arg1 > @arg2 (0, 1, ... |
| 2208 | * @arg1 >= @arg2 (0, 0, ... |
| 2209 | * |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2210 | * When neither object to be compared is a node-set and the operator is |
| 2211 | * <=, <, >=, >, then the objects are compared by converted both objects |
| 2212 | * to numbers and comparing the numbers according to IEEE 754. The < |
| 2213 | * comparison will be true if and only if the first number is less than the |
| 2214 | * second number. The <= comparison will be true if and only if the first |
| 2215 | * number is less than or equal to the second number. The > comparison |
| 2216 | * will be true if and only if the first number is greater than the second |
| 2217 | * number. The >= comparison will be true if and only if the first number |
| 2218 | * is greater than or equal to the second number. |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2219 | * |
| 2220 | * Returns 1 if the comparaison succeeded, 0 if it failed |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2221 | */ |
| 2222 | int |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2223 | xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) { |
| 2224 | int ret = 0; |
| 2225 | xmlXPathObjectPtr arg1, arg2; |
| 2226 | |
| 2227 | arg2 = valuePop(ctxt); |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2228 | if (arg2 == NULL) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 2229 | XP_ERROR0(XPATH_INVALID_OPERAND); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | arg1 = valuePop(ctxt); |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2233 | if (arg1 == NULL) { |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2234 | xmlXPathFreeObject(arg2); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 2235 | XP_ERROR0(XPATH_INVALID_OPERAND); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2238 | if ((arg2->type == XPATH_NODESET) || (arg1->type == XPATH_NODESET)) { |
| 2239 | if ((arg2->type == XPATH_NODESET) && (arg1->type == XPATH_NODESET)) { |
| 2240 | ret = xmlXPathCompareNodeSets(ctxt, inf, strict, arg1, arg2); |
| 2241 | } else { |
| 2242 | if (arg1->type == XPATH_NODESET) { |
| 2243 | ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict, arg1, arg2); |
| 2244 | } else { |
| 2245 | ret = xmlXPathCompareNodeSetValue(ctxt, !inf, !strict, arg2, arg2); |
| 2246 | } |
| 2247 | } |
| 2248 | return(ret); |
| 2249 | } |
| 2250 | |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2251 | if (arg1->type != XPATH_NUMBER) { |
| 2252 | valuePush(ctxt, arg1); |
| 2253 | xmlXPathNumberFunction(ctxt, 1); |
| 2254 | arg1 = valuePop(ctxt); |
| 2255 | } |
| 2256 | if (arg1->type != XPATH_NUMBER) { |
| 2257 | xmlXPathFreeObject(arg1); |
| 2258 | xmlXPathFreeObject(arg2); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 2259 | XP_ERROR0(XPATH_INVALID_OPERAND); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2260 | } |
| 2261 | if (arg2->type != XPATH_NUMBER) { |
| 2262 | valuePush(ctxt, arg2); |
| 2263 | xmlXPathNumberFunction(ctxt, 1); |
| 2264 | arg2 = valuePop(ctxt); |
| 2265 | } |
| 2266 | if (arg2->type != XPATH_NUMBER) { |
| 2267 | xmlXPathFreeObject(arg1); |
| 2268 | xmlXPathFreeObject(arg2); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 2269 | XP_ERROR0(XPATH_INVALID_OPERAND); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2270 | } |
| 2271 | /* |
| 2272 | * Add tests for infinity and nan |
| 2273 | * => feedback on 3.4 for Inf and NaN |
| 2274 | */ |
| 2275 | if (inf && strict) |
| 2276 | ret = (arg1->floatval < arg2->floatval); |
| 2277 | else if (inf && !strict) |
| 2278 | ret = (arg1->floatval <= arg2->floatval); |
| 2279 | else if (!inf && strict) |
| 2280 | ret = (arg1->floatval > arg2->floatval); |
| 2281 | else if (!inf && !strict) |
| 2282 | ret = (arg1->floatval >= arg2->floatval); |
| 2283 | xmlXPathFreeObject(arg1); |
| 2284 | xmlXPathFreeObject(arg2); |
| 2285 | return(ret); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | /** |
| 2289 | * xmlXPathValueFlipSign: |
| 2290 | * @ctxt: the XPath Parser context |
| 2291 | * |
| 2292 | * Implement the unary - operation on an XPath object |
| 2293 | * The numeric operators convert their operands to numbers as if |
| 2294 | * by calling the number function. |
| 2295 | */ |
| 2296 | void |
| 2297 | xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) { |
| 2298 | xmlXPathObjectPtr arg; |
| 2299 | |
| 2300 | POP_FLOAT |
| 2301 | arg->floatval = -arg->floatval; |
| 2302 | valuePush(ctxt, arg); |
| 2303 | } |
| 2304 | |
| 2305 | /** |
| 2306 | * xmlXPathAddValues: |
| 2307 | * @ctxt: the XPath Parser context |
| 2308 | * |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2309 | * Implement the add operation on XPath objects: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2310 | * The numeric operators convert their operands to numbers as if |
| 2311 | * by calling the number function. |
| 2312 | */ |
| 2313 | void |
| 2314 | xmlXPathAddValues(xmlXPathParserContextPtr ctxt) { |
| 2315 | xmlXPathObjectPtr arg; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2316 | double val; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2317 | |
| 2318 | POP_FLOAT |
| 2319 | val = arg->floatval; |
| 2320 | xmlXPathFreeObject(arg); |
| 2321 | |
| 2322 | POP_FLOAT |
| 2323 | arg->floatval += val; |
| 2324 | valuePush(ctxt, arg); |
| 2325 | } |
| 2326 | |
| 2327 | /** |
| 2328 | * xmlXPathSubValues: |
| 2329 | * @ctxt: the XPath Parser context |
| 2330 | * |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2331 | * Implement the substraction operation on XPath objects: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2332 | * The numeric operators convert their operands to numbers as if |
| 2333 | * by calling the number function. |
| 2334 | */ |
| 2335 | void |
| 2336 | xmlXPathSubValues(xmlXPathParserContextPtr ctxt) { |
| 2337 | xmlXPathObjectPtr arg; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2338 | double val; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2339 | |
| 2340 | POP_FLOAT |
| 2341 | val = arg->floatval; |
| 2342 | xmlXPathFreeObject(arg); |
| 2343 | |
| 2344 | POP_FLOAT |
| 2345 | arg->floatval -= val; |
| 2346 | valuePush(ctxt, arg); |
| 2347 | } |
| 2348 | |
| 2349 | /** |
| 2350 | * xmlXPathMultValues: |
| 2351 | * @ctxt: the XPath Parser context |
| 2352 | * |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2353 | * Implement the multiply operation on XPath objects: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2354 | * The numeric operators convert their operands to numbers as if |
| 2355 | * by calling the number function. |
| 2356 | */ |
| 2357 | void |
| 2358 | xmlXPathMultValues(xmlXPathParserContextPtr ctxt) { |
| 2359 | xmlXPathObjectPtr arg; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2360 | double val; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2361 | |
| 2362 | POP_FLOAT |
| 2363 | val = arg->floatval; |
| 2364 | xmlXPathFreeObject(arg); |
| 2365 | |
| 2366 | POP_FLOAT |
| 2367 | arg->floatval *= val; |
| 2368 | valuePush(ctxt, arg); |
| 2369 | } |
| 2370 | |
| 2371 | /** |
| 2372 | * xmlXPathDivValues: |
| 2373 | * @ctxt: the XPath Parser context |
| 2374 | * |
Daniel Veillard | 2b325a0 | 2001-01-31 20:46:31 +0000 | [diff] [blame] | 2375 | * Implement the div operation on XPath objects @arg1 / @arg2: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2376 | * The numeric operators convert their operands to numbers as if |
| 2377 | * by calling the number function. |
| 2378 | */ |
| 2379 | void |
| 2380 | xmlXPathDivValues(xmlXPathParserContextPtr ctxt) { |
| 2381 | xmlXPathObjectPtr arg; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2382 | double val; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2383 | |
| 2384 | POP_FLOAT |
| 2385 | val = arg->floatval; |
| 2386 | xmlXPathFreeObject(arg); |
| 2387 | |
| 2388 | POP_FLOAT |
| 2389 | arg->floatval /= val; |
| 2390 | valuePush(ctxt, arg); |
| 2391 | } |
| 2392 | |
| 2393 | /** |
| 2394 | * xmlXPathModValues: |
| 2395 | * @ctxt: the XPath Parser context |
| 2396 | * |
Daniel Veillard | 2b325a0 | 2001-01-31 20:46:31 +0000 | [diff] [blame] | 2397 | * Implement the mod operation on XPath objects: @arg1 / @arg2 |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2398 | * The numeric operators convert their operands to numbers as if |
| 2399 | * by calling the number function. |
| 2400 | */ |
| 2401 | void |
| 2402 | xmlXPathModValues(xmlXPathParserContextPtr ctxt) { |
| 2403 | xmlXPathObjectPtr arg; |
Daniel Veillard | 2b325a0 | 2001-01-31 20:46:31 +0000 | [diff] [blame] | 2404 | int arg1, arg2; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2405 | |
| 2406 | POP_FLOAT |
Daniel Veillard | 2b325a0 | 2001-01-31 20:46:31 +0000 | [diff] [blame] | 2407 | arg2 = (int) arg->floatval; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2408 | xmlXPathFreeObject(arg); |
| 2409 | |
| 2410 | POP_FLOAT |
Daniel Veillard | 2b325a0 | 2001-01-31 20:46:31 +0000 | [diff] [blame] | 2411 | arg1 = (int) arg->floatval; |
| 2412 | arg->floatval = arg1 % arg2; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2413 | valuePush(ctxt, arg); |
| 2414 | } |
| 2415 | |
| 2416 | /************************************************************************ |
| 2417 | * * |
| 2418 | * The traversal functions * |
| 2419 | * * |
| 2420 | ************************************************************************/ |
| 2421 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2422 | typedef enum { |
| 2423 | AXIS_ANCESTOR = 1, |
| 2424 | AXIS_ANCESTOR_OR_SELF, |
| 2425 | AXIS_ATTRIBUTE, |
| 2426 | AXIS_CHILD, |
| 2427 | AXIS_DESCENDANT, |
| 2428 | AXIS_DESCENDANT_OR_SELF, |
| 2429 | AXIS_FOLLOWING, |
| 2430 | AXIS_FOLLOWING_SIBLING, |
| 2431 | AXIS_NAMESPACE, |
| 2432 | AXIS_PARENT, |
| 2433 | AXIS_PRECEDING, |
| 2434 | AXIS_PRECEDING_SIBLING, |
| 2435 | AXIS_SELF |
| 2436 | } xmlXPathAxisVal; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2437 | |
| 2438 | /* |
| 2439 | * A traversal function enumerates nodes along an axis. |
| 2440 | * Initially it must be called with NULL, and it indicates |
| 2441 | * termination on the axis by returning NULL. |
| 2442 | */ |
| 2443 | typedef xmlNodePtr (*xmlXPathTraversalFunction) |
| 2444 | (xmlXPathParserContextPtr ctxt, xmlNodePtr cur); |
| 2445 | |
| 2446 | /** |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2447 | * xmlXPathNextSelf: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2448 | * @ctxt: the XPath Parser context |
| 2449 | * @cur: the current node in the traversal |
| 2450 | * |
| 2451 | * Traversal function for the "self" direction |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2452 | * The self axis contains just the context node itself |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2453 | * |
| 2454 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2455 | */ |
| 2456 | xmlNodePtr |
| 2457 | xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 2458 | if (cur == NULL) |
| 2459 | return(ctxt->context->node); |
| 2460 | return(NULL); |
| 2461 | } |
| 2462 | |
| 2463 | /** |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2464 | * xmlXPathNextChild: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2465 | * @ctxt: the XPath Parser context |
| 2466 | * @cur: the current node in the traversal |
| 2467 | * |
| 2468 | * Traversal function for the "child" direction |
| 2469 | * The child axis contains the children of the context node in document order. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2470 | * |
| 2471 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2472 | */ |
| 2473 | xmlNodePtr |
| 2474 | xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2475 | if (cur == NULL) { |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2476 | if (ctxt->context->node == NULL) return(NULL); |
| 2477 | switch (ctxt->context->node->type) { |
| 2478 | case XML_ELEMENT_NODE: |
| 2479 | case XML_TEXT_NODE: |
| 2480 | case XML_CDATA_SECTION_NODE: |
| 2481 | case XML_ENTITY_REF_NODE: |
| 2482 | case XML_ENTITY_NODE: |
| 2483 | case XML_PI_NODE: |
| 2484 | case XML_COMMENT_NODE: |
| 2485 | case XML_NOTATION_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2486 | case XML_DTD_NODE: |
| 2487 | return(ctxt->context->node->children); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2488 | case XML_DOCUMENT_NODE: |
| 2489 | case XML_DOCUMENT_TYPE_NODE: |
| 2490 | case XML_DOCUMENT_FRAG_NODE: |
| 2491 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2492 | #ifdef LIBXML_SGML_ENABLED |
| 2493 | case XML_SGML_DOCUMENT_NODE: |
| 2494 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2495 | return(((xmlDocPtr) ctxt->context->node)->children); |
| 2496 | case XML_ELEMENT_DECL: |
| 2497 | case XML_ATTRIBUTE_DECL: |
| 2498 | case XML_ENTITY_DECL: |
| 2499 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2500 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2501 | case XML_XINCLUDE_START: |
| 2502 | case XML_XINCLUDE_END: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2503 | return(NULL); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2504 | } |
| 2505 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2506 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 2507 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 2508 | (cur->type == XML_HTML_DOCUMENT_NODE)) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2509 | return(NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2510 | return(cur->next); |
| 2511 | } |
| 2512 | |
| 2513 | /** |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2514 | * xmlXPathNextDescendant: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2515 | * @ctxt: the XPath Parser context |
| 2516 | * @cur: the current node in the traversal |
| 2517 | * |
| 2518 | * Traversal function for the "descendant" direction |
| 2519 | * the descendant axis contains the descendants of the context node in document |
| 2520 | * order; a descendant is a child or a child of a child and so on. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2521 | * |
| 2522 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2523 | */ |
| 2524 | xmlNodePtr |
| 2525 | xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2526 | if (cur == NULL) { |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2527 | if (ctxt->context->node == NULL) |
| 2528 | return(NULL); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2529 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 2530 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2531 | return(NULL); |
| 2532 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2533 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2534 | return(ctxt->context->doc->children); |
| 2535 | return(ctxt->context->node->children); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2536 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2537 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 2538 | if (cur->children != NULL) |
| 2539 | { |
| 2540 | if (cur->children->type != XML_ENTITY_DECL) |
| 2541 | return(cur->children); |
| 2542 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2543 | if (cur->next != NULL) return(cur->next); |
| 2544 | |
| 2545 | do { |
| 2546 | cur = cur->parent; |
| 2547 | if (cur == NULL) return(NULL); |
| 2548 | if (cur == ctxt->context->node) return(NULL); |
| 2549 | if (cur->next != NULL) { |
| 2550 | cur = cur->next; |
| 2551 | return(cur); |
| 2552 | } |
| 2553 | } while (cur != NULL); |
| 2554 | return(cur); |
| 2555 | } |
| 2556 | |
| 2557 | /** |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2558 | * xmlXPathNextDescendantOrSelf: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2559 | * @ctxt: the XPath Parser context |
| 2560 | * @cur: the current node in the traversal |
| 2561 | * |
| 2562 | * Traversal function for the "descendant-or-self" direction |
| 2563 | * the descendant-or-self axis contains the context node and the descendants |
| 2564 | * of the context node in document order; thus the context node is the first |
| 2565 | * node on the axis, and the first child of the context node is the second node |
| 2566 | * on the axis |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2567 | * |
| 2568 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2569 | */ |
| 2570 | xmlNodePtr |
| 2571 | xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2572 | if (cur == NULL) { |
| 2573 | if (ctxt->context->node == NULL) |
| 2574 | return(NULL); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2575 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 2576 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2577 | return(NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2578 | return(ctxt->context->node); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2579 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2580 | |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2581 | return(xmlXPathNextDescendant(ctxt, cur)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | /** |
| 2585 | * xmlXPathNextParent: |
| 2586 | * @ctxt: the XPath Parser context |
| 2587 | * @cur: the current node in the traversal |
| 2588 | * |
| 2589 | * Traversal function for the "parent" direction |
| 2590 | * The parent axis contains the parent of the context node, if there is one. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2591 | * |
| 2592 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2593 | */ |
| 2594 | xmlNodePtr |
| 2595 | xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 2596 | /* |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2597 | * the parent of an attribute or namespace node is the element |
| 2598 | * to which the attribute or namespace node is attached |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2599 | * Namespace handling !!! |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2600 | */ |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2601 | if (cur == NULL) { |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2602 | if (ctxt->context->node == NULL) return(NULL); |
| 2603 | switch (ctxt->context->node->type) { |
| 2604 | case XML_ELEMENT_NODE: |
| 2605 | case XML_TEXT_NODE: |
| 2606 | case XML_CDATA_SECTION_NODE: |
| 2607 | case XML_ENTITY_REF_NODE: |
| 2608 | case XML_ENTITY_NODE: |
| 2609 | case XML_PI_NODE: |
| 2610 | case XML_COMMENT_NODE: |
| 2611 | case XML_NOTATION_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2612 | case XML_DTD_NODE: |
| 2613 | case XML_ELEMENT_DECL: |
| 2614 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2615 | case XML_XINCLUDE_START: |
| 2616 | case XML_XINCLUDE_END: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2617 | case XML_ENTITY_DECL: |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2618 | if (ctxt->context->node->parent == NULL) |
| 2619 | return((xmlNodePtr) ctxt->context->doc); |
| 2620 | return(ctxt->context->node->parent); |
| 2621 | case XML_ATTRIBUTE_NODE: { |
| 2622 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 2623 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2624 | return(att->parent); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2625 | } |
| 2626 | case XML_DOCUMENT_NODE: |
| 2627 | case XML_DOCUMENT_TYPE_NODE: |
| 2628 | case XML_DOCUMENT_FRAG_NODE: |
| 2629 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2630 | #ifdef LIBXML_SGML_ENABLED |
| 2631 | case XML_SGML_DOCUMENT_NODE: |
| 2632 | #endif |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2633 | return(NULL); |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2634 | case XML_NAMESPACE_DECL: |
| 2635 | /* |
| 2636 | * TODO !!! may require extending struct _xmlNs with |
| 2637 | * parent field |
| 2638 | * C.f. Infoset case... |
| 2639 | */ |
| 2640 | return(NULL); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2641 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2642 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2643 | return(NULL); |
| 2644 | } |
| 2645 | |
| 2646 | /** |
| 2647 | * xmlXPathNextAncestor: |
| 2648 | * @ctxt: the XPath Parser context |
| 2649 | * @cur: the current node in the traversal |
| 2650 | * |
| 2651 | * Traversal function for the "ancestor" direction |
| 2652 | * the ancestor axis contains the ancestors of the context node; the ancestors |
| 2653 | * of the context node consist of the parent of context node and the parent's |
| 2654 | * parent and so on; the nodes are ordered in reverse document order; thus the |
| 2655 | * parent is the first node on the axis, and the parent's parent is the second |
| 2656 | * node on the axis |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2657 | * |
| 2658 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2659 | */ |
| 2660 | xmlNodePtr |
| 2661 | xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 2662 | /* |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2663 | * the parent of an attribute or namespace node is the element |
| 2664 | * to which the attribute or namespace node is attached |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2665 | * !!!!!!!!!!!!! |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2666 | */ |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2667 | if (cur == NULL) { |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2668 | if (ctxt->context->node == NULL) return(NULL); |
| 2669 | switch (ctxt->context->node->type) { |
| 2670 | case XML_ELEMENT_NODE: |
| 2671 | case XML_TEXT_NODE: |
| 2672 | case XML_CDATA_SECTION_NODE: |
| 2673 | case XML_ENTITY_REF_NODE: |
| 2674 | case XML_ENTITY_NODE: |
| 2675 | case XML_PI_NODE: |
| 2676 | case XML_COMMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2677 | case XML_DTD_NODE: |
| 2678 | case XML_ELEMENT_DECL: |
| 2679 | case XML_ATTRIBUTE_DECL: |
| 2680 | case XML_ENTITY_DECL: |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2681 | case XML_NOTATION_NODE: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2682 | case XML_XINCLUDE_START: |
| 2683 | case XML_XINCLUDE_END: |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2684 | if (ctxt->context->node->parent == NULL) |
| 2685 | return((xmlNodePtr) ctxt->context->doc); |
| 2686 | return(ctxt->context->node->parent); |
| 2687 | case XML_ATTRIBUTE_NODE: { |
| 2688 | xmlAttrPtr cur = (xmlAttrPtr) ctxt->context->node; |
| 2689 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2690 | return(cur->parent); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2691 | } |
| 2692 | case XML_DOCUMENT_NODE: |
| 2693 | case XML_DOCUMENT_TYPE_NODE: |
| 2694 | case XML_DOCUMENT_FRAG_NODE: |
| 2695 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2696 | #ifdef LIBXML_SGML_ENABLED |
| 2697 | case XML_SGML_DOCUMENT_NODE: |
| 2698 | #endif |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2699 | return(NULL); |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2700 | case XML_NAMESPACE_DECL: |
| 2701 | /* |
| 2702 | * TODO !!! may require extending struct _xmlNs with |
| 2703 | * parent field |
| 2704 | * C.f. Infoset case... |
| 2705 | */ |
| 2706 | return(NULL); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2707 | } |
| 2708 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2709 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2710 | if (cur == ctxt->context->doc->children) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2711 | return((xmlNodePtr) ctxt->context->doc); |
| 2712 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 2713 | return(NULL); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2714 | switch (cur->type) { |
| 2715 | case XML_ELEMENT_NODE: |
| 2716 | case XML_TEXT_NODE: |
| 2717 | case XML_CDATA_SECTION_NODE: |
| 2718 | case XML_ENTITY_REF_NODE: |
| 2719 | case XML_ENTITY_NODE: |
| 2720 | case XML_PI_NODE: |
| 2721 | case XML_COMMENT_NODE: |
| 2722 | case XML_NOTATION_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2723 | case XML_DTD_NODE: |
| 2724 | case XML_ELEMENT_DECL: |
| 2725 | case XML_ATTRIBUTE_DECL: |
| 2726 | case XML_ENTITY_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2727 | case XML_XINCLUDE_START: |
| 2728 | case XML_XINCLUDE_END: |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2729 | return(cur->parent); |
| 2730 | case XML_ATTRIBUTE_NODE: { |
| 2731 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 2732 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2733 | return(att->parent); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2734 | } |
| 2735 | case XML_DOCUMENT_NODE: |
| 2736 | case XML_DOCUMENT_TYPE_NODE: |
| 2737 | case XML_DOCUMENT_FRAG_NODE: |
| 2738 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2739 | #ifdef LIBXML_SGML_ENABLED |
| 2740 | case XML_SGML_DOCUMENT_NODE: |
| 2741 | #endif |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2742 | return(NULL); |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2743 | case XML_NAMESPACE_DECL: |
| 2744 | /* |
| 2745 | * TODO !!! may require extending struct _xmlNs with |
| 2746 | * parent field |
| 2747 | * C.f. Infoset case... |
| 2748 | */ |
| 2749 | return(NULL); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2750 | } |
| 2751 | return(NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | /** |
| 2755 | * xmlXPathNextAncestorOrSelf: |
| 2756 | * @ctxt: the XPath Parser context |
| 2757 | * @cur: the current node in the traversal |
| 2758 | * |
| 2759 | * Traversal function for the "ancestor-or-self" direction |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2760 | * he ancestor-or-self axis contains the context node and ancestors of |
| 2761 | * the context node in reverse document order; thus the context node is |
| 2762 | * the first node on the axis, and the context node's parent the second; |
| 2763 | * parent here is defined the same as with the parent axis. |
| 2764 | * |
| 2765 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2766 | */ |
| 2767 | xmlNodePtr |
| 2768 | xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2769 | if (cur == NULL) |
| 2770 | return(ctxt->context->node); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2771 | return(xmlXPathNextAncestor(ctxt, cur)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | /** |
| 2775 | * xmlXPathNextFollowingSibling: |
| 2776 | * @ctxt: the XPath Parser context |
| 2777 | * @cur: the current node in the traversal |
| 2778 | * |
| 2779 | * Traversal function for the "following-sibling" direction |
| 2780 | * The following-sibling axis contains the following siblings of the context |
| 2781 | * node in document order. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2782 | * |
| 2783 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2784 | */ |
| 2785 | xmlNodePtr |
| 2786 | xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2787 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 2788 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 2789 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2790 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 2791 | return(NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2792 | if (cur == NULL) |
| 2793 | return(ctxt->context->node->next); |
| 2794 | return(cur->next); |
| 2795 | } |
| 2796 | |
| 2797 | /** |
| 2798 | * xmlXPathNextPrecedingSibling: |
| 2799 | * @ctxt: the XPath Parser context |
| 2800 | * @cur: the current node in the traversal |
| 2801 | * |
| 2802 | * Traversal function for the "preceding-sibling" direction |
| 2803 | * The preceding-sibling axis contains the preceding siblings of the context |
| 2804 | * node in reverse document order; the first preceding sibling is first on the |
| 2805 | * axis; the sibling preceding that node is the second on the axis and so on. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2806 | * |
| 2807 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2808 | */ |
| 2809 | xmlNodePtr |
| 2810 | xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2811 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 2812 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 2813 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2814 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 2815 | return(NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2816 | if (cur == NULL) |
| 2817 | return(ctxt->context->node->prev); |
| 2818 | return(cur->prev); |
| 2819 | } |
| 2820 | |
| 2821 | /** |
| 2822 | * xmlXPathNextFollowing: |
| 2823 | * @ctxt: the XPath Parser context |
| 2824 | * @cur: the current node in the traversal |
| 2825 | * |
| 2826 | * Traversal function for the "following" direction |
| 2827 | * The following axis contains all nodes in the same document as the context |
| 2828 | * node that are after the context node in document order, excluding any |
| 2829 | * descendants and excluding attribute nodes and namespace nodes; the nodes |
| 2830 | * are ordered in document order |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2831 | * |
| 2832 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2833 | */ |
| 2834 | xmlNodePtr |
| 2835 | xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2836 | if (cur != NULL && cur->children != NULL) |
| 2837 | return cur->children ; |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2838 | if (cur == NULL) cur = ctxt->context->node; |
| 2839 | if (cur == NULL) return(NULL) ; /* ERROR */ |
| 2840 | if (cur->next != NULL) return(cur->next) ; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2841 | do { |
| 2842 | cur = cur->parent; |
| 2843 | if (cur == NULL) return(NULL); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2844 | if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2845 | if (cur->next != NULL) return(cur->next); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2846 | } while (cur != NULL); |
| 2847 | return(cur); |
| 2848 | } |
| 2849 | |
| 2850 | /* |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2851 | * xmlXPathIsAncestor: |
| 2852 | * @ancestor: the ancestor node |
| 2853 | * @node: the current node |
| 2854 | * |
| 2855 | * Check that @ancestor is a @node's ancestor |
| 2856 | * |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2857 | * returns 1 if @ancestor is a @node's ancestor, 0 otherwise. |
| 2858 | */ |
| 2859 | static int |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2860 | xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2861 | if ((ancestor == NULL) || (node == NULL)) return(0); |
| 2862 | /* nodes need to be in the same document */ |
| 2863 | if (ancestor->doc != node->doc) return(0); |
| 2864 | /* avoid searching if ancestor or node is the root node */ |
| 2865 | if (ancestor == (xmlNodePtr) node->doc) return(1); |
| 2866 | if (node == (xmlNodePtr) ancestor->doc) return(0); |
| 2867 | while (node->parent != NULL) { |
| 2868 | if (node->parent == ancestor) |
| 2869 | return(1); |
| 2870 | node = node->parent; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2871 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2872 | return(0); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | /** |
| 2876 | * xmlXPathNextPreceding: |
| 2877 | * @ctxt: the XPath Parser context |
| 2878 | * @cur: the current node in the traversal |
| 2879 | * |
| 2880 | * Traversal function for the "preceding" direction |
| 2881 | * the preceding axis contains all nodes in the same document as the context |
| 2882 | * node that are before the context node in document order, excluding any |
| 2883 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 2884 | * ordered in reverse document order |
| 2885 | * |
| 2886 | * Returns the next element following that axis |
| 2887 | */ |
| 2888 | xmlNodePtr |
| 2889 | xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 2890 | if (cur == NULL) |
| 2891 | cur = ctxt->context->node ; |
| 2892 | do { |
| 2893 | if (cur->prev != NULL) { |
| 2894 | for (cur = cur->prev ; cur->last != NULL ; cur = cur->last) |
| 2895 | ; |
| 2896 | return(cur) ; |
| 2897 | } |
| 2898 | |
| 2899 | cur = cur->parent; |
| 2900 | if (cur == NULL) return(NULL); |
| 2901 | if (cur == ctxt->context->doc->children) return(NULL); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2902 | } while (xmlXPathIsAncestor(cur, ctxt->context->node)); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2903 | return(cur); |
| 2904 | } |
| 2905 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2906 | /** |
| 2907 | * xmlXPathNextNamespace: |
| 2908 | * @ctxt: the XPath Parser context |
| 2909 | * @cur: the current attribute in the traversal |
| 2910 | * |
| 2911 | * Traversal function for the "namespace" direction |
| 2912 | * the namespace axis contains the namespace nodes of the context node; |
| 2913 | * the order of nodes on this axis is implementation-defined; the axis will |
| 2914 | * be empty unless the context node is an element |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2915 | * |
| 2916 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2917 | */ |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2918 | xmlNodePtr |
| 2919 | xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 2920 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2921 | if ((cur == NULL) || (ctxt->context->namespaces == NULL)) { |
| 2922 | if (ctxt->context->namespaces != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2923 | xmlFree(ctxt->context->namespaces); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2924 | ctxt->context->namespaces = |
| 2925 | xmlGetNsList(ctxt->context->doc, ctxt->context->node); |
| 2926 | if (ctxt->context->namespaces == NULL) return(NULL); |
| 2927 | ctxt->context->nsNr = 0; |
| 2928 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2929 | return((xmlNodePtr)ctxt->context->namespaces[ctxt->context->nsNr++]); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | /** |
| 2933 | * xmlXPathNextAttribute: |
| 2934 | * @ctxt: the XPath Parser context |
| 2935 | * @cur: the current attribute in the traversal |
| 2936 | * |
| 2937 | * Traversal function for the "attribute" direction |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 2938 | * TODO: support DTD inherited default attributes |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2939 | * |
| 2940 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2941 | */ |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2942 | xmlNodePtr |
| 2943 | xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 2944 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2945 | if (cur == NULL) { |
| 2946 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 2947 | return(NULL); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2948 | return((xmlNodePtr)ctxt->context->node->properties); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2949 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2950 | return((xmlNodePtr)cur->next); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | /************************************************************************ |
| 2954 | * * |
| 2955 | * NodeTest Functions * |
| 2956 | * * |
| 2957 | ************************************************************************/ |
| 2958 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2959 | typedef enum { |
| 2960 | NODE_TEST_NONE = 0, |
| 2961 | NODE_TEST_TYPE = 1, |
| 2962 | NODE_TEST_PI = 2, |
| 2963 | NODE_TEST_ALL = 3, |
| 2964 | NODE_TEST_NS = 4, |
| 2965 | NODE_TEST_NAME = 5 |
| 2966 | } xmlXPathTestVal; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2967 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2968 | typedef enum { |
Daniel Veillard | 2f913b7 | 2001-01-31 13:23:49 +0000 | [diff] [blame] | 2969 | NODE_TYPE_NODE = 0, |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 2970 | NODE_TYPE_COMMENT = XML_COMMENT_NODE, |
| 2971 | NODE_TYPE_TEXT = XML_TEXT_NODE, |
Daniel Veillard | 2f913b7 | 2001-01-31 13:23:49 +0000 | [diff] [blame] | 2972 | NODE_TYPE_PI = XML_PI_NODE |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2973 | } xmlXPathTypeVal; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2974 | |
| 2975 | #define IS_FUNCTION 200 |
| 2976 | |
| 2977 | /** |
| 2978 | * xmlXPathNodeCollectAndTest: |
| 2979 | * @ctxt: the XPath Parser context |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2980 | * @axis: the XPath axis |
| 2981 | * @test: the XPath test |
| 2982 | * @type: the XPath type |
| 2983 | * @prefix: the namesapce prefix if any |
| 2984 | * @name: the name used in the search if any |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2985 | * |
| 2986 | * This is the function implementing a step: based on the current list |
| 2987 | * of nodes, it builds up a new list, looking at all nodes under that |
| 2988 | * axis and selecting them. |
| 2989 | * |
| 2990 | * Returns the new NodeSet resulting from the search. |
| 2991 | */ |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2992 | void |
| 2993 | xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, xmlXPathAxisVal axis, |
| 2994 | xmlXPathTestVal test, xmlXPathTypeVal type, |
| 2995 | const xmlChar *prefix, const xmlChar *name) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2996 | #ifdef DEBUG_STEP |
| 2997 | int n = 0, t = 0; |
| 2998 | #endif |
| 2999 | int i; |
| 3000 | xmlNodeSetPtr ret; |
| 3001 | xmlXPathTraversalFunction next = NULL; |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 3002 | void (*addNode)(xmlNodeSetPtr, xmlNodePtr); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3003 | xmlNodePtr cur = NULL; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3004 | xmlXPathObjectPtr obj; |
| 3005 | xmlNodeSetPtr nodelist; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3006 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3007 | CHECK_TYPE(XPATH_NODESET); |
| 3008 | obj = valuePop(ctxt); |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 3009 | addNode = xmlXPathNodeSetAdd; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3010 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3011 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3012 | xmlGenericError(xmlGenericErrorContext, |
| 3013 | "new step : "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3014 | #endif |
| 3015 | switch (axis) { |
| 3016 | case AXIS_ANCESTOR: |
| 3017 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3018 | xmlGenericError(xmlGenericErrorContext, |
| 3019 | "axis 'ancestors' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3020 | #endif |
| 3021 | next = xmlXPathNextAncestor; break; |
| 3022 | case AXIS_ANCESTOR_OR_SELF: |
| 3023 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3024 | xmlGenericError(xmlGenericErrorContext, |
| 3025 | "axis 'ancestors-or-self' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3026 | #endif |
| 3027 | next = xmlXPathNextAncestorOrSelf; break; |
| 3028 | case AXIS_ATTRIBUTE: |
| 3029 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3030 | xmlGenericError(xmlGenericErrorContext, |
| 3031 | "axis 'attributes' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3032 | #endif |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3033 | next = xmlXPathNextAttribute; break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3034 | break; |
| 3035 | case AXIS_CHILD: |
| 3036 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3037 | xmlGenericError(xmlGenericErrorContext, |
| 3038 | "axis 'child' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3039 | #endif |
| 3040 | next = xmlXPathNextChild; break; |
| 3041 | case AXIS_DESCENDANT: |
| 3042 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3043 | xmlGenericError(xmlGenericErrorContext, |
| 3044 | "axis 'descendant' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3045 | #endif |
| 3046 | next = xmlXPathNextDescendant; break; |
| 3047 | case AXIS_DESCENDANT_OR_SELF: |
| 3048 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3049 | xmlGenericError(xmlGenericErrorContext, |
| 3050 | "axis 'descendant-or-self' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3051 | #endif |
| 3052 | next = xmlXPathNextDescendantOrSelf; break; |
| 3053 | case AXIS_FOLLOWING: |
| 3054 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3055 | xmlGenericError(xmlGenericErrorContext, |
| 3056 | "axis 'following' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3057 | #endif |
| 3058 | next = xmlXPathNextFollowing; break; |
| 3059 | case AXIS_FOLLOWING_SIBLING: |
| 3060 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3061 | xmlGenericError(xmlGenericErrorContext, |
| 3062 | "axis 'following-siblings' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3063 | #endif |
| 3064 | next = xmlXPathNextFollowingSibling; break; |
| 3065 | case AXIS_NAMESPACE: |
| 3066 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3067 | xmlGenericError(xmlGenericErrorContext, |
| 3068 | "axis 'namespace' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3069 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3070 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3071 | break; |
| 3072 | case AXIS_PARENT: |
| 3073 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3074 | xmlGenericError(xmlGenericErrorContext, |
| 3075 | "axis 'parent' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3076 | #endif |
| 3077 | next = xmlXPathNextParent; break; |
| 3078 | case AXIS_PRECEDING: |
| 3079 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3080 | xmlGenericError(xmlGenericErrorContext, |
| 3081 | "axis 'preceding' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3082 | #endif |
| 3083 | next = xmlXPathNextPreceding; break; |
| 3084 | case AXIS_PRECEDING_SIBLING: |
| 3085 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3086 | xmlGenericError(xmlGenericErrorContext, |
| 3087 | "axis 'preceding-sibling' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3088 | #endif |
| 3089 | next = xmlXPathNextPrecedingSibling; break; |
| 3090 | case AXIS_SELF: |
| 3091 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3092 | xmlGenericError(xmlGenericErrorContext, |
| 3093 | "axis 'self' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3094 | #endif |
| 3095 | next = xmlXPathNextSelf; break; |
| 3096 | } |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3097 | if (next == NULL) |
| 3098 | return; |
| 3099 | |
| 3100 | nodelist = obj->nodesetval; |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 3101 | if ((nodelist != NULL) && |
| 3102 | (nodelist->nodeNr <= 1)) |
| 3103 | addNode = xmlXPathNodeSetAddUnique; |
| 3104 | else |
| 3105 | addNode = xmlXPathNodeSetAdd; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3106 | ret = xmlXPathNodeSetCreate(NULL); |
| 3107 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3108 | xmlGenericError(xmlGenericErrorContext, |
| 3109 | " context contains %d nodes\n", |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3110 | nodelist->nodeNr); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3111 | switch (test) { |
Daniel Veillard | 2f913b7 | 2001-01-31 13:23:49 +0000 | [diff] [blame] | 3112 | case NODE_TEST_NODE: |
| 3113 | xmlGenericError(xmlGenericErrorContext, |
| 3114 | " searching all nodes\n"); |
| 3115 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3116 | case NODE_TEST_NONE: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3117 | xmlGenericError(xmlGenericErrorContext, |
| 3118 | " searching for none !!!\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3119 | break; |
| 3120 | case NODE_TEST_TYPE: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3121 | xmlGenericError(xmlGenericErrorContext, |
| 3122 | " searching for type %d\n", type); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3123 | break; |
| 3124 | case NODE_TEST_PI: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3125 | xmlGenericError(xmlGenericErrorContext, |
| 3126 | " searching for PI !!!\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3127 | break; |
| 3128 | case NODE_TEST_ALL: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3129 | xmlGenericError(xmlGenericErrorContext, |
| 3130 | " searching for *\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3131 | break; |
| 3132 | case NODE_TEST_NS: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3133 | xmlGenericError(xmlGenericErrorContext, |
| 3134 | " searching for namespace %s\n", |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3135 | prefix); |
| 3136 | break; |
| 3137 | case NODE_TEST_NAME: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3138 | xmlGenericError(xmlGenericErrorContext, |
| 3139 | " searching for name %s\n", name); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3140 | if (prefix != NULL) |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3141 | xmlGenericError(xmlGenericErrorContext, |
| 3142 | " with namespace %s\n", |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3143 | prefix); |
| 3144 | break; |
| 3145 | } |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3146 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3147 | #endif |
Daniel Veillard | c8df0aa | 2000-10-10 23:50:30 +0000 | [diff] [blame] | 3148 | /* |
| 3149 | * 2.3 Node Tests |
| 3150 | * - For the attribute axis, the principal node type is attribute. |
| 3151 | * - For the namespace axis, the principal node type is namespace. |
| 3152 | * - For other axes, the principal node type is element. |
| 3153 | * |
| 3154 | * A node test * is true for any node of the |
| 3155 | * principal node type. For example, child::* willi |
| 3156 | * select all element children of the context node |
| 3157 | */ |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3158 | for (i = 0;i < nodelist->nodeNr; i++) { |
| 3159 | ctxt->context->node = nodelist->nodeTab[i]; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3160 | |
| 3161 | cur = NULL; |
| 3162 | do { |
| 3163 | cur = next(ctxt, cur); |
| 3164 | if (cur == NULL) break; |
| 3165 | #ifdef DEBUG_STEP |
| 3166 | t++; |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3167 | xmlGenericError(xmlGenericErrorContext, " %s", cur->name); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3168 | #endif |
| 3169 | switch (test) { |
| 3170 | case NODE_TEST_NONE: |
| 3171 | STRANGE |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3172 | return; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3173 | case NODE_TEST_TYPE: |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3174 | if ((cur->type == type) || |
Daniel Veillard | 2f913b7 | 2001-01-31 13:23:49 +0000 | [diff] [blame] | 3175 | ((type == NODE_TYPE_NODE) && |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3176 | ((cur->type == XML_DOCUMENT_NODE) || |
Daniel Veillard | 2f913b7 | 2001-01-31 13:23:49 +0000 | [diff] [blame] | 3177 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 3178 | (cur->type == XML_ELEMENT_NODE) || |
| 3179 | (cur->type == XML_PI_NODE) || |
| 3180 | (cur->type == XML_COMMENT_NODE) || |
| 3181 | (cur->type == XML_CDATA_SECTION_NODE) || |
| 3182 | (cur->type == XML_TEXT_NODE)))) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3183 | #ifdef DEBUG_STEP |
| 3184 | n++; |
| 3185 | #endif |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 3186 | addNode(ret, cur); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3187 | } |
| 3188 | break; |
| 3189 | case NODE_TEST_PI: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3190 | if (cur->type == XML_PI_NODE) { |
| 3191 | if ((name != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3192 | (!xmlStrEqual(name, cur->name))) |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3193 | break; |
| 3194 | #ifdef DEBUG_STEP |
| 3195 | n++; |
| 3196 | #endif |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 3197 | addNode(ret, cur); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3198 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3199 | break; |
| 3200 | case NODE_TEST_ALL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3201 | if (axis == AXIS_ATTRIBUTE) { |
| 3202 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3203 | #ifdef DEBUG_STEP |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3204 | n++; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3205 | #endif |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 3206 | addNode(ret, cur); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3207 | } |
| 3208 | } else if (axis == AXIS_NAMESPACE) { |
| 3209 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3210 | #ifdef DEBUG_STEP |
| 3211 | n++; |
| 3212 | #endif |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 3213 | addNode(ret, cur); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3214 | } |
| 3215 | } else { |
| 3216 | if ((cur->type == XML_ELEMENT_NODE) || |
| 3217 | (cur->type == XML_DOCUMENT_NODE) || |
| 3218 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3219 | if (prefix == NULL) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3220 | #ifdef DEBUG_STEP |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3221 | n++; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3222 | #endif |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 3223 | addNode(ret, cur); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3224 | } else if ((cur->ns != NULL) && |
| 3225 | (xmlStrEqual(prefix, |
| 3226 | cur->ns->href))) { |
| 3227 | #ifdef DEBUG_STEP |
| 3228 | n++; |
| 3229 | #endif |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 3230 | addNode(ret, cur); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3231 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3232 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3233 | } |
| 3234 | break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3235 | case NODE_TEST_NS: { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3236 | TODO; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3237 | break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3238 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3239 | case NODE_TEST_NAME: |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3240 | switch (cur->type) { |
| 3241 | case XML_ELEMENT_NODE: |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3242 | if (xmlStrEqual(name, cur->name)) { |
| 3243 | if (prefix == NULL) { |
| 3244 | if ((cur->ns == NULL) || |
| 3245 | (cur->ns->prefix == NULL)) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3246 | #ifdef DEBUG_STEP |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3247 | n++; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3248 | #endif |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 3249 | addNode(ret, cur); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3250 | } |
| 3251 | } else { |
| 3252 | if ((cur->ns != NULL) && |
| 3253 | (xmlStrEqual(prefix, |
| 3254 | cur->ns->href))) { |
| 3255 | #ifdef DEBUG_STEP |
| 3256 | n++; |
| 3257 | #endif |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 3258 | addNode(ret, cur); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3259 | } |
| 3260 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3261 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3262 | break; |
| 3263 | case XML_ATTRIBUTE_NODE: { |
| 3264 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3265 | if (xmlStrEqual(name, attr->name)) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3266 | #ifdef DEBUG_STEP |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3267 | n++; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3268 | #endif |
Daniel Veillard | 2c257ec | 2001-02-06 13:29:07 +0000 | [diff] [blame] | 3269 | addNode(ret, cur); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3270 | } |
| 3271 | break; |
| 3272 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3273 | case XML_NAMESPACE_DECL: { |
| 3274 | TODO; |
| 3275 | break; |
| 3276 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3277 | default: |
| 3278 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3279 | } |
| 3280 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3281 | } |
| 3282 | } while (cur != NULL); |
| 3283 | } |
| 3284 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3285 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3286 | "\nExamined %d nodes, found %d nodes at that step\n", t, n); |
| 3287 | #endif |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3288 | xmlXPathFreeObject(obj); |
| 3289 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | |
| 3293 | /************************************************************************ |
| 3294 | * * |
| 3295 | * Implicit tree core function library * |
| 3296 | * * |
| 3297 | ************************************************************************/ |
| 3298 | |
| 3299 | /** |
| 3300 | * xmlXPathRoot: |
| 3301 | * @ctxt: the XPath Parser context |
| 3302 | * |
| 3303 | * Initialize the context to the root of the document |
| 3304 | */ |
| 3305 | void |
| 3306 | xmlXPathRoot(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3307 | ctxt->context->node = (xmlNodePtr) ctxt->context->doc; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3308 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3311 | /************************************************************************ |
| 3312 | * * |
| 3313 | * The explicit core function library * |
| 3314 | *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib * |
| 3315 | * * |
| 3316 | ************************************************************************/ |
| 3317 | |
| 3318 | |
| 3319 | /** |
| 3320 | * xmlXPathLastFunction: |
| 3321 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3322 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3323 | * |
| 3324 | * Implement the last() XPath function |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3325 | * number last() |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3326 | * The last function returns the number of nodes in the context node list. |
| 3327 | */ |
| 3328 | void |
| 3329 | xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3330 | CHECK_ARITY(0); |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 3331 | if (ctxt->context->contextSize > 0) { |
| 3332 | valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize)); |
| 3333 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3334 | xmlGenericError(xmlGenericErrorContext, |
| 3335 | "last() : %d\n", ctxt->context->contextSize); |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 3336 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3337 | } else { |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 3338 | XP_ERROR(XPATH_INVALID_CTXT_SIZE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3339 | } |
| 3340 | } |
| 3341 | |
| 3342 | /** |
| 3343 | * xmlXPathPositionFunction: |
| 3344 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3345 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3346 | * |
| 3347 | * Implement the position() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3348 | * number position() |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3349 | * The position function returns the position of the context node in the |
| 3350 | * context node list. The first position is 1, and so the last positionr |
| 3351 | * will be equal to last(). |
| 3352 | */ |
| 3353 | void |
| 3354 | xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3355 | CHECK_ARITY(0); |
Daniel Veillard | ff9c330 | 2000-10-13 16:38:25 +0000 | [diff] [blame] | 3356 | if (ctxt->context->proximityPosition >= 0) { |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 3357 | valuePush(ctxt, |
Daniel Veillard | ff9c330 | 2000-10-13 16:38:25 +0000 | [diff] [blame] | 3358 | xmlXPathNewFloat((double) ctxt->context->proximityPosition)); |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 3359 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3360 | xmlGenericError(xmlGenericErrorContext, "position() : %d\n", |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 3361 | ctxt->context->proximityPosition); |
| 3362 | #endif |
| 3363 | } else { |
| 3364 | XP_ERROR(XPATH_INVALID_CTXT_POSITION); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3365 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | /** |
| 3369 | * xmlXPathCountFunction: |
| 3370 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3371 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3372 | * |
| 3373 | * Implement the count() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3374 | * number count(node-set) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3375 | */ |
| 3376 | void |
| 3377 | xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3378 | xmlXPathObjectPtr cur; |
| 3379 | |
| 3380 | CHECK_ARITY(1); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 3381 | if ((ctxt->value == NULL) || |
| 3382 | ((ctxt->value->type != XPATH_NODESET) && |
| 3383 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 3384 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3385 | cur = valuePop(ctxt); |
| 3386 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3387 | valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3388 | xmlXPathFreeObject(cur); |
| 3389 | } |
| 3390 | |
| 3391 | /** |
| 3392 | * xmlXPathIdFunction: |
| 3393 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3394 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3395 | * |
| 3396 | * Implement the id() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3397 | * node-set id(object) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3398 | * The id function selects elements by their unique ID |
| 3399 | * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set, |
| 3400 | * then the result is the union of the result of applying id to the |
| 3401 | * string value of each of the nodes in the argument node-set. When the |
| 3402 | * argument to id is of any other type, the argument is converted to a |
| 3403 | * string as if by a call to the string function; the string is split |
| 3404 | * into a whitespace-separated list of tokens (whitespace is any sequence |
| 3405 | * of characters matching the production S); the result is a node-set |
| 3406 | * containing the elements in the same document as the context node that |
| 3407 | * have a unique ID equal to any of the tokens in the list. |
| 3408 | */ |
| 3409 | void |
| 3410 | xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3411 | const xmlChar *tokens; |
| 3412 | const xmlChar *cur; |
| 3413 | xmlChar *ID; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3414 | xmlAttrPtr attr; |
| 3415 | xmlNodePtr elem = NULL; |
| 3416 | xmlXPathObjectPtr ret, obj; |
| 3417 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3418 | CHECK_ARITY(1); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3419 | obj = valuePop(ctxt); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3420 | if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3421 | if (obj->type == XPATH_NODESET) { |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3422 | xmlXPathObjectPtr newobj; |
| 3423 | int i; |
| 3424 | |
| 3425 | ret = xmlXPathNewNodeSet(NULL); |
| 3426 | |
| 3427 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
| 3428 | valuePush(ctxt, |
| 3429 | xmlXPathNewNodeSet(obj->nodesetval->nodeTab[i])); |
| 3430 | xmlXPathStringFunction(ctxt, 1); |
| 3431 | xmlXPathIdFunction(ctxt, 1); |
| 3432 | newobj = valuePop(ctxt); |
| 3433 | ret->nodesetval = xmlXPathNodeSetMerge(ret->nodesetval, |
| 3434 | newobj->nodesetval); |
| 3435 | xmlXPathFreeObject(newobj); |
| 3436 | } |
| 3437 | |
| 3438 | xmlXPathFreeObject(obj); |
| 3439 | valuePush(ctxt, ret); |
| 3440 | return; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3441 | } |
| 3442 | if (obj->type != XPATH_STRING) { |
| 3443 | valuePush(ctxt, obj); |
| 3444 | xmlXPathStringFunction(ctxt, 1); |
| 3445 | obj = valuePop(ctxt); |
| 3446 | if (obj->type != XPATH_STRING) { |
| 3447 | xmlXPathFreeObject(obj); |
| 3448 | return; |
| 3449 | } |
| 3450 | } |
| 3451 | tokens = obj->stringval; |
| 3452 | |
| 3453 | ret = xmlXPathNewNodeSet(NULL); |
| 3454 | valuePush(ctxt, ret); |
| 3455 | if (tokens == NULL) { |
| 3456 | xmlXPathFreeObject(obj); |
| 3457 | return; |
| 3458 | } |
| 3459 | |
| 3460 | cur = tokens; |
| 3461 | |
| 3462 | while (IS_BLANK(*cur)) cur++; |
| 3463 | while (*cur != 0) { |
| 3464 | while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) || |
| 3465 | (*cur == '.') || (*cur == '-') || |
| 3466 | (*cur == '_') || (*cur == ':') || |
| 3467 | (IS_COMBINING(*cur)) || |
| 3468 | (IS_EXTENDER(*cur))) |
| 3469 | cur++; |
| 3470 | |
| 3471 | if ((!IS_BLANK(*cur)) && (*cur != 0)) break; |
| 3472 | |
| 3473 | ID = xmlStrndup(tokens, cur - tokens); |
| 3474 | attr = xmlGetID(ctxt->context->doc, ID); |
| 3475 | if (attr != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3476 | elem = attr->parent; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3477 | xmlXPathNodeSetAdd(ret->nodesetval, elem); |
| 3478 | } |
| 3479 | if (ID != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3480 | xmlFree(ID); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3481 | |
| 3482 | while (IS_BLANK(*cur)) cur++; |
| 3483 | tokens = cur; |
| 3484 | } |
| 3485 | xmlXPathFreeObject(obj); |
| 3486 | return; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3487 | } |
| 3488 | |
| 3489 | /** |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3490 | * xmlXPathLocalNameFunction: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3491 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3492 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3493 | * |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3494 | * Implement the local-name() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3495 | * string local-name(node-set?) |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3496 | * The local-name function returns a string containing the local part |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3497 | * of the name of the node in the argument node-set that is first in |
| 3498 | * document order. If the node-set is empty or the first node has no |
| 3499 | * name, an empty string is returned. If the argument is omitted it |
| 3500 | * defaults to the context node. |
| 3501 | */ |
| 3502 | void |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3503 | xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3504 | xmlXPathObjectPtr cur; |
| 3505 | |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3506 | if (nargs == 0) { |
| 3507 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 3508 | nargs = 1; |
| 3509 | } |
| 3510 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3511 | CHECK_ARITY(1); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 3512 | if ((ctxt->value == NULL) || |
| 3513 | ((ctxt->value->type != XPATH_NODESET) && |
| 3514 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 3515 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3516 | cur = valuePop(ctxt); |
| 3517 | |
| 3518 | if (cur->nodesetval->nodeNr == 0) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3519 | valuePush(ctxt, xmlXPathNewCString("")); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3520 | } else { |
| 3521 | int i = 0; /* Should be first in document order !!!!! */ |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3522 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 3523 | case XML_ELEMENT_NODE: |
| 3524 | case XML_ATTRIBUTE_NODE: |
| 3525 | case XML_PI_NODE: |
| 3526 | valuePush(ctxt, |
| 3527 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
| 3528 | break; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3529 | case XML_NAMESPACE_DECL: |
| 3530 | valuePush(ctxt, xmlXPathNewString( |
| 3531 | ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix)); |
| 3532 | break; |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3533 | default: |
| 3534 | valuePush(ctxt, xmlXPathNewCString("")); |
| 3535 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3536 | } |
| 3537 | xmlXPathFreeObject(cur); |
| 3538 | } |
| 3539 | |
| 3540 | /** |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3541 | * xmlXPathNamespaceURIFunction: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3542 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3543 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3544 | * |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3545 | * Implement the namespace-uri() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3546 | * string namespace-uri(node-set?) |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3547 | * The namespace-uri function returns a string containing the |
| 3548 | * namespace URI of the expanded name of the node in the argument |
| 3549 | * node-set that is first in document order. If the node-set is empty, |
| 3550 | * the first node has no name, or the expanded name has no namespace |
| 3551 | * URI, an empty string is returned. If the argument is omitted it |
| 3552 | * defaults to the context node. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3553 | */ |
| 3554 | void |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3555 | xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3556 | xmlXPathObjectPtr cur; |
| 3557 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3558 | if (nargs == 0) { |
| 3559 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 3560 | nargs = 1; |
| 3561 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3562 | CHECK_ARITY(1); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 3563 | if ((ctxt->value == NULL) || |
| 3564 | ((ctxt->value->type != XPATH_NODESET) && |
| 3565 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 3566 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3567 | cur = valuePop(ctxt); |
| 3568 | |
| 3569 | if (cur->nodesetval->nodeNr == 0) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3570 | valuePush(ctxt, xmlXPathNewCString("")); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3571 | } else { |
| 3572 | int i = 0; /* Should be first in document order !!!!! */ |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3573 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 3574 | case XML_ELEMENT_NODE: |
| 3575 | case XML_ATTRIBUTE_NODE: |
| 3576 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
| 3577 | valuePush(ctxt, xmlXPathNewCString("")); |
| 3578 | else |
| 3579 | valuePush(ctxt, xmlXPathNewString( |
| 3580 | cur->nodesetval->nodeTab[i]->ns->href)); |
| 3581 | break; |
| 3582 | default: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3583 | valuePush(ctxt, xmlXPathNewCString("")); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3584 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3585 | } |
| 3586 | xmlXPathFreeObject(cur); |
| 3587 | } |
| 3588 | |
| 3589 | /** |
| 3590 | * xmlXPathNameFunction: |
| 3591 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3592 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3593 | * |
| 3594 | * Implement the name() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3595 | * string name(node-set?) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3596 | * The name function returns a string containing a QName representing |
| 3597 | * the name of the node in the argument node-set that is first in documenti |
| 3598 | * order. The QName must represent the name with respect to the namespace |
| 3599 | * declarations in effect on the node whose name is being represented. |
| 3600 | * Typically, this will be the form in which the name occurred in the XML |
| 3601 | * source. This need not be the case if there are namespace declarations |
| 3602 | * in effect on the node that associate multiple prefixes with the same |
| 3603 | * namespace. However, an implementation may include information about |
| 3604 | * the original prefix in its representation of nodes; in this case, an |
| 3605 | * implementation can ensure that the returned string is always the same |
| 3606 | * as the QName used in the XML source. If the argument it omitted it |
| 3607 | * defaults to the context node. |
| 3608 | * Libxml keep the original prefix so the "real qualified name" used is |
| 3609 | * returned. |
| 3610 | */ |
| 3611 | void |
| 3612 | xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3613 | xmlXPathObjectPtr cur; |
| 3614 | |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3615 | if (nargs == 0) { |
| 3616 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 3617 | nargs = 1; |
| 3618 | } |
| 3619 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3620 | CHECK_ARITY(1); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 3621 | if ((ctxt->value == NULL) || |
| 3622 | ((ctxt->value->type != XPATH_NODESET) && |
| 3623 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 3624 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3625 | cur = valuePop(ctxt); |
| 3626 | |
| 3627 | if (cur->nodesetval->nodeNr == 0) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3628 | valuePush(ctxt, xmlXPathNewCString("")); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3629 | } else { |
| 3630 | int i = 0; /* Should be first in document order !!!!! */ |
| 3631 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3632 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 3633 | case XML_ELEMENT_NODE: |
| 3634 | case XML_ATTRIBUTE_NODE: |
| 3635 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
| 3636 | valuePush(ctxt, xmlXPathNewString( |
| 3637 | cur->nodesetval->nodeTab[i]->name)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3638 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3639 | else { |
| 3640 | char name[2000]; |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 3641 | #ifdef HAVE_SNPRINTF |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3642 | snprintf(name, sizeof(name), "%s:%s", |
| 3643 | (char *) cur->nodesetval->nodeTab[i]->ns->prefix, |
| 3644 | (char *) cur->nodesetval->nodeTab[i]->name); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 3645 | #else |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3646 | sprintf(name, "%s:%s", |
| 3647 | (char *) cur->nodesetval->nodeTab[i]->ns->prefix, |
| 3648 | (char *) cur->nodesetval->nodeTab[i]->name); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 3649 | #endif |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3650 | name[sizeof(name) - 1] = 0; |
| 3651 | valuePush(ctxt, xmlXPathNewCString(name)); |
| 3652 | } |
| 3653 | break; |
| 3654 | default: |
| 3655 | valuePush(ctxt, |
| 3656 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i])); |
| 3657 | xmlXPathLocalNameFunction(ctxt, 1); |
| 3658 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3659 | } |
| 3660 | xmlXPathFreeObject(cur); |
| 3661 | } |
| 3662 | |
| 3663 | /** |
| 3664 | * xmlXPathStringFunction: |
| 3665 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3666 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3667 | * |
| 3668 | * Implement the string() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3669 | * string string(object?) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3670 | * he string function converts an object to a string as follows: |
| 3671 | * - A node-set is converted to a string by returning the value of |
| 3672 | * the node in the node-set that is first in document order. |
| 3673 | * If the node-set is empty, an empty string is returned. |
| 3674 | * - A number is converted to a string as follows |
| 3675 | * + NaN is converted to the string NaN |
| 3676 | * + positive zero is converted to the string 0 |
| 3677 | * + negative zero is converted to the string 0 |
| 3678 | * + positive infinity is converted to the string Infinity |
| 3679 | * + negative infinity is converted to the string -Infinity |
| 3680 | * + if the number is an integer, the number is represented in |
| 3681 | * decimal form as a Number with no decimal point and no leading |
| 3682 | * zeros, preceded by a minus sign (-) if the number is negative |
| 3683 | * + otherwise, the number is represented in decimal form as a |
| 3684 | * Number including a decimal point with at least one digit |
| 3685 | * before the decimal point and at least one digit after the |
| 3686 | * decimal point, preceded by a minus sign (-) if the number |
| 3687 | * is negative; there must be no leading zeros before the decimal |
| 3688 | * point apart possibly from the one required digit immediatelyi |
| 3689 | * before the decimal point; beyond the one required digit |
| 3690 | * after the decimal point there must be as many, but only as |
| 3691 | * many, more digits as are needed to uniquely distinguish the |
| 3692 | * number from all other IEEE 754 numeric values. |
| 3693 | * - The boolean false value is converted to the string false. |
| 3694 | * The boolean true value is converted to the string true. |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3695 | * |
| 3696 | * If the argument is omitted, it defaults to a node-set with the |
| 3697 | * context node as its only member. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3698 | */ |
| 3699 | void |
| 3700 | xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3701 | xmlXPathObjectPtr cur; |
| 3702 | |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3703 | if (nargs == 0) { |
| 3704 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 3705 | nargs = 1; |
| 3706 | } |
| 3707 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3708 | CHECK_ARITY(1); |
| 3709 | cur = valuePop(ctxt); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3710 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3711 | switch (cur->type) { |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3712 | case XPATH_UNDEFINED: |
| 3713 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3714 | xmlGenericError(xmlGenericErrorContext, "String: undefined\n"); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3715 | #endif |
| 3716 | valuePush(ctxt, xmlXPathNewCString("")); |
| 3717 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 3718 | case XPATH_XSLT_TREE: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3719 | case XPATH_NODESET: |
| 3720 | if (cur->nodesetval->nodeNr == 0) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3721 | valuePush(ctxt, xmlXPathNewCString("")); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3722 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3723 | xmlChar *res; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3724 | int i = 0; /* Should be first in document order !!!!! */ |
| 3725 | res = xmlNodeGetContent(cur->nodesetval->nodeTab[i]); |
| 3726 | valuePush(ctxt, xmlXPathNewString(res)); |
Daniel Veillard | 5a2b697 | 2001-01-20 21:15:50 +0000 | [diff] [blame] | 3727 | if (res != NULL) |
| 3728 | xmlFree(res); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3729 | } |
| 3730 | xmlXPathFreeObject(cur); |
| 3731 | return; |
| 3732 | case XPATH_STRING: |
| 3733 | valuePush(ctxt, cur); |
| 3734 | return; |
| 3735 | case XPATH_BOOLEAN: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3736 | if (cur->boolval) valuePush(ctxt, xmlXPathNewCString("true")); |
| 3737 | else valuePush(ctxt, xmlXPathNewCString("false")); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3738 | xmlXPathFreeObject(cur); |
| 3739 | return; |
| 3740 | case XPATH_NUMBER: { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3741 | char buf[100]; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3742 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3743 | if (isnan(cur->floatval)) |
| 3744 | sprintf(buf, "NaN"); |
| 3745 | else if (isinf(cur->floatval) > 0) |
| 3746 | sprintf(buf, "+Infinity"); |
| 3747 | else if (isinf(cur->floatval) < 0) |
| 3748 | sprintf(buf, "-Infinity"); |
| 3749 | else |
| 3750 | sprintf(buf, "%0g", cur->floatval); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3751 | valuePush(ctxt, xmlXPathNewCString(buf)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3752 | xmlXPathFreeObject(cur); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3753 | return; |
| 3754 | } |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3755 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 3756 | case XPATH_POINT: |
| 3757 | case XPATH_RANGE: |
| 3758 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3759 | TODO |
| 3760 | valuePush(ctxt, xmlXPathNewCString("")); |
| 3761 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3762 | } |
| 3763 | STRANGE |
| 3764 | } |
| 3765 | |
| 3766 | /** |
| 3767 | * xmlXPathStringLengthFunction: |
| 3768 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3769 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3770 | * |
| 3771 | * Implement the string-length() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3772 | * number string-length(string?) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3773 | * The string-length returns the number of characters in the string |
| 3774 | * (see [3.6 Strings]). If the argument is omitted, it defaults to |
| 3775 | * the context node converted to a string, in other words the value |
| 3776 | * of the context node. |
| 3777 | */ |
| 3778 | void |
| 3779 | xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3780 | xmlXPathObjectPtr cur; |
| 3781 | |
| 3782 | if (nargs == 0) { |
| 3783 | if (ctxt->context->node == NULL) { |
| 3784 | valuePush(ctxt, xmlXPathNewFloat(0)); |
| 3785 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3786 | xmlChar *content; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3787 | |
| 3788 | content = xmlNodeGetContent(ctxt->context->node); |
| 3789 | valuePush(ctxt, xmlXPathNewFloat(xmlStrlen(content))); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3790 | xmlFree(content); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3791 | } |
| 3792 | return; |
| 3793 | } |
| 3794 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3795 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3796 | CHECK_TYPE(XPATH_STRING); |
| 3797 | cur = valuePop(ctxt); |
| 3798 | valuePush(ctxt, xmlXPathNewFloat(xmlStrlen(cur->stringval))); |
| 3799 | xmlXPathFreeObject(cur); |
| 3800 | } |
| 3801 | |
| 3802 | /** |
| 3803 | * xmlXPathConcatFunction: |
| 3804 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3805 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3806 | * |
| 3807 | * Implement the concat() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3808 | * string concat(string, string, string*) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3809 | * The concat function returns the concatenation of its arguments. |
| 3810 | */ |
| 3811 | void |
| 3812 | xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3813 | xmlXPathObjectPtr cur, newobj; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3814 | xmlChar *tmp; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3815 | |
| 3816 | if (nargs < 2) { |
| 3817 | CHECK_ARITY(2); |
| 3818 | } |
| 3819 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3820 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3821 | cur = valuePop(ctxt); |
| 3822 | if ((cur == NULL) || (cur->type != XPATH_STRING)) { |
| 3823 | xmlXPathFreeObject(cur); |
| 3824 | return; |
| 3825 | } |
| 3826 | nargs--; |
| 3827 | |
| 3828 | while (nargs > 0) { |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3829 | CAST_TO_STRING; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3830 | newobj = valuePop(ctxt); |
| 3831 | if ((newobj == NULL) || (newobj->type != XPATH_STRING)) { |
| 3832 | xmlXPathFreeObject(newobj); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3833 | xmlXPathFreeObject(cur); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3834 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3835 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3836 | tmp = xmlStrcat(newobj->stringval, cur->stringval); |
| 3837 | newobj->stringval = cur->stringval; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3838 | cur->stringval = tmp; |
| 3839 | |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3840 | xmlXPathFreeObject(newobj); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3841 | nargs--; |
| 3842 | } |
| 3843 | valuePush(ctxt, cur); |
| 3844 | } |
| 3845 | |
| 3846 | /** |
| 3847 | * xmlXPathContainsFunction: |
| 3848 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3849 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3850 | * |
| 3851 | * Implement the contains() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3852 | * boolean contains(string, string) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3853 | * The contains function returns true if the first argument string |
| 3854 | * contains the second argument string, and otherwise returns false. |
| 3855 | */ |
| 3856 | void |
| 3857 | xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3858 | xmlXPathObjectPtr hay, needle; |
| 3859 | |
| 3860 | CHECK_ARITY(2); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3861 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3862 | CHECK_TYPE(XPATH_STRING); |
| 3863 | needle = valuePop(ctxt); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3864 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3865 | hay = valuePop(ctxt); |
| 3866 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 3867 | xmlXPathFreeObject(hay); |
| 3868 | xmlXPathFreeObject(needle); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3869 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3870 | } |
| 3871 | if (xmlStrstr(hay->stringval, needle->stringval)) |
| 3872 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 3873 | else |
| 3874 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 3875 | xmlXPathFreeObject(hay); |
| 3876 | xmlXPathFreeObject(needle); |
| 3877 | } |
| 3878 | |
| 3879 | /** |
| 3880 | * xmlXPathStartsWithFunction: |
| 3881 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3882 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3883 | * |
| 3884 | * Implement the starts-with() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3885 | * boolean starts-with(string, string) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3886 | * The starts-with function returns true if the first argument string |
| 3887 | * starts with the second argument string, and otherwise returns false. |
| 3888 | */ |
| 3889 | void |
| 3890 | xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3891 | xmlXPathObjectPtr hay, needle; |
| 3892 | int n; |
| 3893 | |
| 3894 | CHECK_ARITY(2); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3895 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3896 | CHECK_TYPE(XPATH_STRING); |
| 3897 | needle = valuePop(ctxt); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3898 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3899 | hay = valuePop(ctxt); |
| 3900 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 3901 | xmlXPathFreeObject(hay); |
| 3902 | xmlXPathFreeObject(needle); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3903 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3904 | } |
| 3905 | n = xmlStrlen(needle->stringval); |
| 3906 | if (xmlStrncmp(hay->stringval, needle->stringval, n)) |
| 3907 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 3908 | else |
| 3909 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 3910 | xmlXPathFreeObject(hay); |
| 3911 | xmlXPathFreeObject(needle); |
| 3912 | } |
| 3913 | |
| 3914 | /** |
| 3915 | * xmlXPathSubstringFunction: |
| 3916 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3917 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3918 | * |
| 3919 | * Implement the substring() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3920 | * string substring(string, number, number?) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3921 | * The substring function returns the substring of the first argument |
| 3922 | * starting at the position specified in the second argument with |
| 3923 | * length specified in the third argument. For example, |
| 3924 | * substring("12345",2,3) returns "234". If the third argument is not |
| 3925 | * specified, it returns the substring starting at the position specified |
| 3926 | * in the second argument and continuing to the end of the string. For |
| 3927 | * example, substring("12345",2) returns "2345". More precisely, each |
| 3928 | * character in the string (see [3.6 Strings]) is considered to have a |
| 3929 | * numeric position: the position of the first character is 1, the position |
| 3930 | * of the second character is 2 and so on. The returned substring contains |
| 3931 | * those characters for which the position of the character is greater than |
| 3932 | * or equal to the second argument and, if the third argument is specified, |
| 3933 | * less than the sum of the second and third arguments; the comparisons |
| 3934 | * and addition used for the above follow the standard IEEE 754 rules. Thus: |
| 3935 | * - substring("12345", 1.5, 2.6) returns "234" |
| 3936 | * - substring("12345", 0, 3) returns "12" |
| 3937 | * - substring("12345", 0 div 0, 3) returns "" |
| 3938 | * - substring("12345", 1, 0 div 0) returns "" |
| 3939 | * - substring("12345", -42, 1 div 0) returns "12345" |
| 3940 | * - substring("12345", -1 div 0, 1 div 0) returns "" |
| 3941 | */ |
| 3942 | void |
| 3943 | xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3944 | xmlXPathObjectPtr str, start, len; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3945 | double le, in; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3946 | int i, l; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3947 | xmlChar *ret; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3948 | |
| 3949 | /* |
| 3950 | * Conformance needs to be checked !!!!! |
| 3951 | */ |
| 3952 | if (nargs < 2) { |
| 3953 | CHECK_ARITY(2); |
| 3954 | } |
| 3955 | if (nargs > 3) { |
| 3956 | CHECK_ARITY(3); |
| 3957 | } |
| 3958 | if (nargs == 3) { |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3959 | CAST_TO_NUMBER; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3960 | CHECK_TYPE(XPATH_NUMBER); |
| 3961 | len = valuePop(ctxt); |
| 3962 | le = len->floatval; |
| 3963 | xmlXPathFreeObject(len); |
| 3964 | } else { |
| 3965 | le = 2000000000; |
| 3966 | } |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3967 | CAST_TO_NUMBER; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3968 | CHECK_TYPE(XPATH_NUMBER); |
| 3969 | start = valuePop(ctxt); |
| 3970 | in = start->floatval; |
| 3971 | xmlXPathFreeObject(start); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3972 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3973 | CHECK_TYPE(XPATH_STRING); |
| 3974 | str = valuePop(ctxt); |
| 3975 | le += in; |
| 3976 | |
| 3977 | /* integer index of the first char */ |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3978 | i = (int) in; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3979 | if (((double)i) != in) i++; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3980 | |
| 3981 | /* integer index of the last char */ |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3982 | l = (int) le; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3983 | if (((double)l) != le) l++; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3984 | |
| 3985 | /* back to a zero based len */ |
| 3986 | i--; |
| 3987 | l--; |
| 3988 | |
| 3989 | /* check against the string len */ |
| 3990 | if (l > 1024) { |
| 3991 | l = xmlStrlen(str->stringval); |
| 3992 | } |
| 3993 | if (i < 0) { |
| 3994 | i = 0; |
| 3995 | } |
| 3996 | |
| 3997 | /* number of chars to copy */ |
| 3998 | l -= i; |
| 3999 | |
| 4000 | ret = xmlStrsub(str->stringval, i, l); |
| 4001 | if (ret == NULL) |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4002 | valuePush(ctxt, xmlXPathNewCString("")); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4003 | else { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4004 | valuePush(ctxt, xmlXPathNewString(ret)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4005 | xmlFree(ret); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4006 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4007 | xmlXPathFreeObject(str); |
| 4008 | } |
| 4009 | |
| 4010 | /** |
| 4011 | * xmlXPathSubstringBeforeFunction: |
| 4012 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4013 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4014 | * |
| 4015 | * Implement the substring-before() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4016 | * string substring-before(string, string) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4017 | * The substring-before function returns the substring of the first |
| 4018 | * argument string that precedes the first occurrence of the second |
| 4019 | * argument string in the first argument string, or the empty string |
| 4020 | * if the first argument string does not contain the second argument |
| 4021 | * string. For example, substring-before("1999/04/01","/") returns 1999. |
| 4022 | */ |
| 4023 | void |
| 4024 | xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4025 | xmlXPathObjectPtr str; |
| 4026 | xmlXPathObjectPtr find; |
| 4027 | xmlBufferPtr target; |
| 4028 | const xmlChar *point; |
| 4029 | int offset; |
| 4030 | |
| 4031 | CHECK_ARITY(2); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4032 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4033 | find = valuePop(ctxt); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4034 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4035 | str = valuePop(ctxt); |
| 4036 | |
| 4037 | target = xmlBufferCreate(); |
| 4038 | if (target) { |
| 4039 | point = xmlStrstr(str->stringval, find->stringval); |
| 4040 | if (point) { |
| 4041 | offset = (int)(point - str->stringval); |
| 4042 | xmlBufferAdd(target, str->stringval, offset); |
| 4043 | } |
| 4044 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 4045 | xmlBufferFree(target); |
| 4046 | } |
| 4047 | |
| 4048 | xmlXPathFreeObject(str); |
| 4049 | xmlXPathFreeObject(find); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4050 | } |
| 4051 | |
| 4052 | /** |
| 4053 | * xmlXPathSubstringAfterFunction: |
| 4054 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4055 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4056 | * |
| 4057 | * Implement the substring-after() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4058 | * string substring-after(string, string) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4059 | * The substring-after function returns the substring of the first |
| 4060 | * argument string that follows the first occurrence of the second |
| 4061 | * argument string in the first argument string, or the empty stringi |
| 4062 | * if the first argument string does not contain the second argument |
| 4063 | * string. For example, substring-after("1999/04/01","/") returns 04/01, |
| 4064 | * and substring-after("1999/04/01","19") returns 99/04/01. |
| 4065 | */ |
| 4066 | void |
| 4067 | xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4068 | xmlXPathObjectPtr str; |
| 4069 | xmlXPathObjectPtr find; |
| 4070 | xmlBufferPtr target; |
| 4071 | const xmlChar *point; |
| 4072 | int offset; |
| 4073 | |
| 4074 | CHECK_ARITY(2); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4075 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4076 | find = valuePop(ctxt); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4077 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4078 | str = valuePop(ctxt); |
| 4079 | |
| 4080 | target = xmlBufferCreate(); |
| 4081 | if (target) { |
| 4082 | point = xmlStrstr(str->stringval, find->stringval); |
| 4083 | if (point) { |
| 4084 | offset = (int)(point - str->stringval) + xmlStrlen(find->stringval); |
| 4085 | xmlBufferAdd(target, &str->stringval[offset], |
| 4086 | xmlStrlen(str->stringval) - offset); |
| 4087 | } |
| 4088 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 4089 | xmlBufferFree(target); |
| 4090 | } |
| 4091 | |
| 4092 | xmlXPathFreeObject(str); |
| 4093 | xmlXPathFreeObject(find); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | /** |
| 4097 | * xmlXPathNormalizeFunction: |
| 4098 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4099 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4100 | * |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4101 | * Implement the normalize-space() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4102 | * string normalize-space(string?) |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4103 | * The normalize-space function returns the argument string with white |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4104 | * space normalized by stripping leading and trailing whitespace |
| 4105 | * and replacing sequences of whitespace characters by a single |
| 4106 | * space. Whitespace characters are the same allowed by the S production |
| 4107 | * in XML. If the argument is omitted, it defaults to the context |
| 4108 | * node converted to a string, in other words the value of the context node. |
| 4109 | */ |
| 4110 | void |
| 4111 | xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4112 | xmlXPathObjectPtr obj = NULL; |
| 4113 | xmlChar *source = NULL; |
| 4114 | xmlBufferPtr target; |
| 4115 | xmlChar blank; |
| 4116 | |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4117 | if (nargs == 0) { |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4118 | /* Use current context node */ |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4119 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 4120 | xmlXPathStringFunction(ctxt, 1); |
| 4121 | nargs = 1; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4122 | } |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4123 | |
| 4124 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4125 | CAST_TO_STRING; |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4126 | CHECK_TYPE(XPATH_STRING); |
| 4127 | obj = valuePop(ctxt); |
| 4128 | source = obj->stringval; |
| 4129 | |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4130 | target = xmlBufferCreate(); |
| 4131 | if (target && source) { |
| 4132 | |
| 4133 | /* Skip leading whitespaces */ |
| 4134 | while (IS_BLANK(*source)) |
| 4135 | source++; |
| 4136 | |
| 4137 | /* Collapse intermediate whitespaces, and skip trailing whitespaces */ |
| 4138 | blank = 0; |
| 4139 | while (*source) { |
| 4140 | if (IS_BLANK(*source)) { |
| 4141 | blank = *source; |
| 4142 | } else { |
| 4143 | if (blank) { |
| 4144 | xmlBufferAdd(target, &blank, 1); |
| 4145 | blank = 0; |
| 4146 | } |
| 4147 | xmlBufferAdd(target, source, 1); |
| 4148 | } |
| 4149 | source++; |
| 4150 | } |
| 4151 | |
| 4152 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 4153 | xmlBufferFree(target); |
| 4154 | } |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4155 | xmlXPathFreeObject(obj); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | /** |
| 4159 | * xmlXPathTranslateFunction: |
| 4160 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4161 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4162 | * |
| 4163 | * Implement the translate() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4164 | * string translate(string, string, string) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4165 | * The translate function returns the first argument string with |
| 4166 | * occurrences of characters in the second argument string replaced |
| 4167 | * by the character at the corresponding position in the third argument |
| 4168 | * string. For example, translate("bar","abc","ABC") returns the string |
| 4169 | * BAr. If there is a character in the second argument string with no |
| 4170 | * character at a corresponding position in the third argument string |
| 4171 | * (because the second argument string is longer than the third argument |
| 4172 | * string), then occurrences of that character in the first argument |
| 4173 | * string are removed. For example, translate("--aaa--","abc-","ABC") |
| 4174 | * returns "AAA". If a character occurs more than once in second |
| 4175 | * argument string, then the first occurrence determines the replacement |
| 4176 | * character. If the third argument string is longer than the second |
| 4177 | * argument string, then excess characters are ignored. |
| 4178 | */ |
| 4179 | void |
| 4180 | xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4181 | xmlXPathObjectPtr str; |
| 4182 | xmlXPathObjectPtr from; |
| 4183 | xmlXPathObjectPtr to; |
| 4184 | xmlBufferPtr target; |
| 4185 | int i, offset, max; |
| 4186 | xmlChar ch; |
| 4187 | const xmlChar *point; |
| 4188 | |
| 4189 | CHECK_ARITY(3); |
| 4190 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4191 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4192 | to = valuePop(ctxt); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4193 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4194 | from = valuePop(ctxt); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4195 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4196 | str = valuePop(ctxt); |
| 4197 | |
| 4198 | target = xmlBufferCreate(); |
| 4199 | if (target) { |
| 4200 | max = xmlStrlen(to->stringval); |
| 4201 | for (i = 0; (ch = str->stringval[i]); i++) { |
| 4202 | point = xmlStrchr(from->stringval, ch); |
| 4203 | if (point) { |
| 4204 | /* Warning: This may not work with UTF-8 */ |
| 4205 | offset = (int)(point - from->stringval); |
| 4206 | if (offset < max) |
| 4207 | xmlBufferAdd(target, &to->stringval[offset], 1); |
| 4208 | } else |
| 4209 | xmlBufferAdd(target, &ch, 1); |
| 4210 | } |
| 4211 | } |
| 4212 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 4213 | xmlBufferFree(target); |
| 4214 | xmlXPathFreeObject(str); |
| 4215 | xmlXPathFreeObject(from); |
| 4216 | xmlXPathFreeObject(to); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4217 | } |
| 4218 | |
| 4219 | /** |
| 4220 | * xmlXPathBooleanFunction: |
| 4221 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4222 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4223 | * |
| 4224 | * Implement the boolean() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4225 | * boolean boolean(object) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4226 | * he boolean function converts its argument to a boolean as follows: |
| 4227 | * - a number is true if and only if it is neither positive or |
| 4228 | * negative zero nor NaN |
| 4229 | * - a node-set is true if and only if it is non-empty |
| 4230 | * - a string is true if and only if its length is non-zero |
| 4231 | */ |
| 4232 | void |
| 4233 | xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 4234 | xmlXPathObjectPtr cur; |
| 4235 | int res = 0; |
| 4236 | |
| 4237 | CHECK_ARITY(1); |
| 4238 | cur = valuePop(ctxt); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4239 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4240 | switch (cur->type) { |
| 4241 | case XPATH_NODESET: |
Daniel Veillard | 0f2a53c | 2001-02-05 17:57:33 +0000 | [diff] [blame] | 4242 | case XPATH_XSLT_TREE: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4243 | if ((cur->nodesetval == NULL) || |
| 4244 | (cur->nodesetval->nodeNr == 0)) res = 0; |
| 4245 | else |
| 4246 | res = 1; |
| 4247 | break; |
| 4248 | case XPATH_STRING: |
| 4249 | if ((cur->stringval == NULL) || |
| 4250 | (cur->stringval[0] == 0)) res = 0; |
| 4251 | else |
| 4252 | res = 1; |
| 4253 | break; |
| 4254 | case XPATH_BOOLEAN: |
| 4255 | valuePush(ctxt, cur); |
| 4256 | return; |
| 4257 | case XPATH_NUMBER: |
| 4258 | if (cur->floatval) res = 1; |
| 4259 | break; |
| 4260 | default: |
| 4261 | STRANGE |
| 4262 | } |
| 4263 | xmlXPathFreeObject(cur); |
| 4264 | valuePush(ctxt, xmlXPathNewBoolean(res)); |
| 4265 | } |
| 4266 | |
| 4267 | /** |
| 4268 | * xmlXPathNotFunction: |
| 4269 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4270 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4271 | * |
| 4272 | * Implement the not() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4273 | * boolean not(boolean) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4274 | * The not function returns true if its argument is false, |
| 4275 | * and false otherwise. |
| 4276 | */ |
| 4277 | void |
| 4278 | xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 4279 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4280 | CAST_TO_BOOLEAN; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4281 | CHECK_TYPE(XPATH_BOOLEAN); |
| 4282 | ctxt->value->boolval = ! ctxt->value->boolval; |
| 4283 | } |
| 4284 | |
| 4285 | /** |
| 4286 | * xmlXPathTrueFunction: |
| 4287 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4288 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4289 | * |
| 4290 | * Implement the true() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4291 | * boolean true() |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4292 | */ |
| 4293 | void |
| 4294 | xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 4295 | CHECK_ARITY(0); |
| 4296 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 4297 | } |
| 4298 | |
| 4299 | /** |
| 4300 | * xmlXPathFalseFunction: |
| 4301 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4302 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4303 | * |
| 4304 | * Implement the false() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4305 | * boolean false() |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4306 | */ |
| 4307 | void |
| 4308 | xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 4309 | CHECK_ARITY(0); |
| 4310 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 4311 | } |
| 4312 | |
| 4313 | /** |
| 4314 | * xmlXPathLangFunction: |
| 4315 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4316 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4317 | * |
| 4318 | * Implement the lang() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4319 | * boolean lang(string) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4320 | * The lang function returns true or false depending on whether the |
| 4321 | * language of the context node as specified by xml:lang attributes |
| 4322 | * is the same as or is a sublanguage of the language specified by |
| 4323 | * the argument string. The language of the context node is determined |
| 4324 | * by the value of the xml:lang attribute on the context node, or, if |
| 4325 | * the context node has no xml:lang attribute, by the value of the |
| 4326 | * xml:lang attribute on the nearest ancestor of the context node that |
| 4327 | * has an xml:lang attribute. If there is no such attribute, then lang |
| 4328 | * returns false. If there is such an attribute, then lang returns |
| 4329 | * true if the attribute value is equal to the argument ignoring case, |
| 4330 | * or if there is some suffix starting with - such that the attribute |
| 4331 | * value is equal to the argument ignoring that suffix of the attribute |
| 4332 | * value and ignoring case. |
| 4333 | */ |
| 4334 | void |
| 4335 | xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4336 | xmlXPathObjectPtr val; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4337 | const xmlChar *theLang; |
| 4338 | const xmlChar *lang; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4339 | int ret = 0; |
| 4340 | int i; |
| 4341 | |
| 4342 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4343 | CAST_TO_STRING; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4344 | CHECK_TYPE(XPATH_STRING); |
| 4345 | val = valuePop(ctxt); |
| 4346 | lang = val->stringval; |
| 4347 | theLang = xmlNodeGetLang(ctxt->context->node); |
| 4348 | if ((theLang != NULL) && (lang != NULL)) { |
| 4349 | for (i = 0;lang[i] != 0;i++) |
| 4350 | if (toupper(lang[i]) != toupper(theLang[i])) |
| 4351 | goto not_equal; |
| 4352 | ret = 1; |
| 4353 | } |
| 4354 | not_equal: |
| 4355 | xmlXPathFreeObject(val); |
| 4356 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
| 4359 | /** |
| 4360 | * xmlXPathNumberFunction: |
| 4361 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4362 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4363 | * |
| 4364 | * Implement the number() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4365 | * number number(object?) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4366 | */ |
| 4367 | void |
| 4368 | xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 4369 | xmlXPathObjectPtr cur; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4370 | double res; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4371 | |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4372 | if (nargs == 0) { |
| 4373 | if (ctxt->context->node == NULL) { |
| 4374 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 4375 | } else { |
| 4376 | xmlChar* content = xmlNodeGetContent(ctxt->context->node); |
| 4377 | |
| 4378 | res = xmlXPathStringEvalNumber(content); |
| 4379 | valuePush(ctxt, xmlXPathNewFloat(res)); |
| 4380 | xmlFree(content); |
| 4381 | } |
| 4382 | return; |
| 4383 | } |
| 4384 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4385 | CHECK_ARITY(1); |
| 4386 | cur = valuePop(ctxt); |
| 4387 | switch (cur->type) { |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 4388 | case XPATH_UNDEFINED: |
| 4389 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4390 | xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n"); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 4391 | #endif |
| 4392 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 4393 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 4394 | case XPATH_XSLT_TREE: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4395 | case XPATH_NODESET: |
| 4396 | valuePush(ctxt, cur); |
| 4397 | xmlXPathStringFunction(ctxt, 1); |
| 4398 | cur = valuePop(ctxt); |
| 4399 | case XPATH_STRING: |
| 4400 | res = xmlXPathStringEvalNumber(cur->stringval); |
| 4401 | valuePush(ctxt, xmlXPathNewFloat(res)); |
| 4402 | xmlXPathFreeObject(cur); |
| 4403 | return; |
| 4404 | case XPATH_BOOLEAN: |
| 4405 | if (cur->boolval) valuePush(ctxt, xmlXPathNewFloat(1.0)); |
| 4406 | else valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 4407 | xmlXPathFreeObject(cur); |
| 4408 | return; |
| 4409 | case XPATH_NUMBER: |
| 4410 | valuePush(ctxt, cur); |
| 4411 | return; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 4412 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 4413 | case XPATH_POINT: |
| 4414 | case XPATH_RANGE: |
| 4415 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 4416 | TODO |
| 4417 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 4418 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4419 | } |
| 4420 | STRANGE |
| 4421 | } |
| 4422 | |
| 4423 | /** |
| 4424 | * xmlXPathSumFunction: |
| 4425 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4426 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4427 | * |
| 4428 | * Implement the sum() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4429 | * number sum(node-set) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4430 | * The sum function returns the sum of the values of the nodes in |
| 4431 | * the argument node-set. |
| 4432 | */ |
| 4433 | void |
| 4434 | xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4435 | xmlXPathObjectPtr cur; |
| 4436 | int i; |
| 4437 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4438 | CHECK_ARITY(1); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 4439 | if ((ctxt->value == NULL) || |
| 4440 | ((ctxt->value->type != XPATH_NODESET) && |
| 4441 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 4442 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4443 | cur = valuePop(ctxt); |
| 4444 | |
| 4445 | if (cur->nodesetval->nodeNr == 0) { |
| 4446 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 4447 | } else { |
| 4448 | valuePush(ctxt, |
| 4449 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[0])); |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4450 | xmlXPathNumberFunction(ctxt, 1); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4451 | for (i = 1; i < cur->nodesetval->nodeNr; i++) { |
| 4452 | valuePush(ctxt, |
| 4453 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i])); |
| 4454 | xmlXPathAddValues(ctxt); |
| 4455 | } |
| 4456 | } |
| 4457 | xmlXPathFreeObject(cur); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | /** |
| 4461 | * xmlXPathFloorFunction: |
| 4462 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4463 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4464 | * |
| 4465 | * Implement the floor() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4466 | * number floor(number) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4467 | * The floor function returns the largest (closest to positive infinity) |
| 4468 | * number that is not greater than the argument and that is an integer. |
| 4469 | */ |
| 4470 | void |
| 4471 | xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 4472 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4473 | CAST_TO_NUMBER; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4474 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4475 | #if 0 |
| 4476 | ctxt->value->floatval = floor(ctxt->value->floatval); |
| 4477 | #else |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4478 | /* floor(0.999999999999) => 1.0 !!!!!!!!!!! */ |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4479 | ctxt->value->floatval = (double)((int) ctxt->value->floatval); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4480 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
| 4483 | /** |
| 4484 | * xmlXPathCeilingFunction: |
| 4485 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4486 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4487 | * |
| 4488 | * Implement the ceiling() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4489 | * number ceiling(number) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4490 | * The ceiling function returns the smallest (closest to negative infinity) |
| 4491 | * number that is not less than the argument and that is an integer. |
| 4492 | */ |
| 4493 | void |
| 4494 | xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4495 | double f; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4496 | |
| 4497 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4498 | CAST_TO_NUMBER; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4499 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4500 | |
| 4501 | #if 0 |
| 4502 | ctxt->value->floatval = ceil(ctxt->value->floatval); |
| 4503 | #else |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4504 | f = (double)((int) ctxt->value->floatval); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4505 | if (f != ctxt->value->floatval) |
| 4506 | ctxt->value->floatval = f + 1; |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4507 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | /** |
| 4511 | * xmlXPathRoundFunction: |
| 4512 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4513 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4514 | * |
| 4515 | * Implement the round() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4516 | * number round(number) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4517 | * The round function returns the number that is closest to the |
| 4518 | * argument and that is an integer. If there are two such numbers, |
| 4519 | * then the one that is even is returned. |
| 4520 | */ |
| 4521 | void |
| 4522 | xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4523 | double f; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4524 | |
| 4525 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4526 | CAST_TO_NUMBER; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4527 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4528 | |
| 4529 | if ((ctxt->value->floatval == xmlXPathNAN) || |
| 4530 | (ctxt->value->floatval == xmlXPathPINF) || |
| 4531 | (ctxt->value->floatval == xmlXPathNINF) || |
| 4532 | (ctxt->value->floatval == 0.0)) |
| 4533 | return; |
| 4534 | |
| 4535 | #if 0 |
| 4536 | f = floor(ctxt->value->floatval); |
| 4537 | #else |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4538 | f = (double)((int) ctxt->value->floatval); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4539 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4540 | if (ctxt->value->floatval < f + 0.5) |
| 4541 | ctxt->value->floatval = f; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4542 | else |
| 4543 | ctxt->value->floatval = f + 1; |
| 4544 | } |
| 4545 | |
| 4546 | /************************************************************************ |
| 4547 | * * |
| 4548 | * The Parser * |
| 4549 | * * |
| 4550 | ************************************************************************/ |
| 4551 | |
| 4552 | /* |
| 4553 | * a couple of forward declarations since we use a recursive call based |
| 4554 | * implementation. |
| 4555 | */ |
| 4556 | void xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt); |
| 4557 | void xmlXPathEvalPredicate(xmlXPathParserContextPtr ctxt); |
| 4558 | void xmlXPathEvalLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 4559 | #ifdef VMS |
| 4560 | void xmlXPathEvalRelLocationPath(xmlXPathParserContextPtr ctxt); |
| 4561 | #define xmlXPathEvalRelativeLocationPath xmlXPathEvalRelLocationPath |
| 4562 | #else |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4563 | void xmlXPathEvalRelativeLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 4564 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4565 | |
| 4566 | /** |
| 4567 | * xmlXPathParseNCName: |
| 4568 | * @ctxt: the XPath Parser context |
| 4569 | * |
| 4570 | * parse an XML namespace non qualified name. |
| 4571 | * |
| 4572 | * [NS 3] NCName ::= (Letter | '_') (NCNameChar)* |
| 4573 | * |
| 4574 | * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 4575 | * CombiningChar | Extender |
| 4576 | * |
| 4577 | * Returns the namespace name or NULL |
| 4578 | */ |
| 4579 | |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4580 | xmlChar * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4581 | xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4582 | const xmlChar *q; |
| 4583 | xmlChar *ret = NULL; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4584 | |
| 4585 | if (!IS_LETTER(CUR) && (CUR != '_')) return(NULL); |
| 4586 | q = NEXT; |
| 4587 | |
| 4588 | while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) || |
| 4589 | (CUR == '.') || (CUR == '-') || |
| 4590 | (CUR == '_') || |
| 4591 | (IS_COMBINING(CUR)) || |
| 4592 | (IS_EXTENDER(CUR))) |
| 4593 | NEXT; |
| 4594 | |
| 4595 | ret = xmlStrndup(q, CUR_PTR - q); |
| 4596 | |
| 4597 | return(ret); |
| 4598 | } |
| 4599 | |
| 4600 | /** |
| 4601 | * xmlXPathParseQName: |
| 4602 | * @ctxt: the XPath Parser context |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4603 | * @prefix: a xmlChar ** |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4604 | * |
| 4605 | * parse an XML qualified name |
| 4606 | * |
| 4607 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 4608 | * |
| 4609 | * [NS 6] Prefix ::= NCName |
| 4610 | * |
| 4611 | * [NS 7] LocalPart ::= NCName |
| 4612 | * |
| 4613 | * Returns the function returns the local part, and prefix is updated |
| 4614 | * to get the Prefix if any. |
| 4615 | */ |
| 4616 | |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4617 | xmlChar * |
| 4618 | xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) { |
| 4619 | xmlChar *ret = NULL; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4620 | |
| 4621 | *prefix = NULL; |
| 4622 | ret = xmlXPathParseNCName(ctxt); |
| 4623 | if (CUR == ':') { |
| 4624 | *prefix = ret; |
| 4625 | NEXT; |
| 4626 | ret = xmlXPathParseNCName(ctxt); |
| 4627 | } |
| 4628 | return(ret); |
| 4629 | } |
| 4630 | |
| 4631 | /** |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 4632 | * xmlXPathParseName: |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 4633 | * @ctxt: the XPath Parser context |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 4634 | * |
| 4635 | * parse an XML name |
| 4636 | * |
| 4637 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 4638 | * CombiningChar | Extender |
| 4639 | * |
| 4640 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 4641 | * |
| 4642 | * Returns the namespace name or NULL |
| 4643 | */ |
| 4644 | |
| 4645 | xmlChar * |
| 4646 | xmlXPathParseName(xmlXPathParserContextPtr ctxt) { |
| 4647 | const xmlChar *q; |
| 4648 | xmlChar *ret = NULL; |
| 4649 | |
| 4650 | if (!IS_LETTER(CUR) && (CUR != '_')) return(NULL); |
| 4651 | q = NEXT; |
| 4652 | |
| 4653 | /* TODO Make this UTF8 compliant !!! */ |
| 4654 | while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) || |
| 4655 | (CUR == '.') || (CUR == '-') || |
| 4656 | (CUR == '_') || (CUR == ':') || |
| 4657 | (IS_COMBINING(CUR)) || |
| 4658 | (IS_EXTENDER(CUR))) |
| 4659 | NEXT; |
| 4660 | |
| 4661 | ret = xmlStrndup(q, CUR_PTR - q); |
| 4662 | |
| 4663 | return(ret); |
| 4664 | } |
| 4665 | |
| 4666 | /** |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4667 | * xmlXPathStringEvalNumber: |
| 4668 | * @str: A string to scan |
| 4669 | * |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4670 | * [30] Number ::= Digits ('.' Digits?)? |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4671 | * | '.' Digits |
| 4672 | * [31] Digits ::= [0-9]+ |
| 4673 | * |
| 4674 | * Parse and evaluate a Number in the string |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4675 | * In complement of the Number expression, this function also handles |
| 4676 | * negative values : '-' Number. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4677 | * |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4678 | * Returns the double value. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4679 | */ |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4680 | double |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4681 | xmlXPathStringEvalNumber(const xmlChar *str) { |
| 4682 | const xmlChar *cur = str; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4683 | double ret = 0.0; |
| 4684 | double mult = 1; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4685 | int ok = 0; |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4686 | int isneg = 0; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4687 | |
| 4688 | while (*cur == ' ') cur++; |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4689 | if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4690 | return(xmlXPathNAN); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4691 | } |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4692 | if (*cur == '-') { |
| 4693 | isneg = 1; |
| 4694 | cur++; |
| 4695 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4696 | while ((*cur >= '0') && (*cur <= '9')) { |
| 4697 | ret = ret * 10 + (*cur - '0'); |
| 4698 | ok = 1; |
| 4699 | cur++; |
| 4700 | } |
| 4701 | if (*cur == '.') { |
| 4702 | cur++; |
| 4703 | if (((*cur < '0') || (*cur > '9')) && (!ok)) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4704 | return(xmlXPathNAN); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4705 | } |
| 4706 | while ((*cur >= '0') && (*cur <= '9')) { |
| 4707 | mult /= 10; |
| 4708 | ret = ret + (*cur - '0') * mult; |
| 4709 | cur++; |
| 4710 | } |
| 4711 | } |
| 4712 | while (*cur == ' ') cur++; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4713 | if (*cur != 0) return(xmlXPathNAN); |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4714 | if (isneg) ret = -ret; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4715 | return(ret); |
| 4716 | } |
| 4717 | |
| 4718 | /** |
| 4719 | * xmlXPathEvalNumber: |
| 4720 | * @ctxt: the XPath Parser context |
| 4721 | * |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4722 | * [30] Number ::= Digits ('.' Digits?)? |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4723 | * | '.' Digits |
| 4724 | * [31] Digits ::= [0-9]+ |
| 4725 | * |
| 4726 | * Parse and evaluate a Number, then push it on the stack |
| 4727 | * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4728 | */ |
| 4729 | void |
| 4730 | xmlXPathEvalNumber(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4731 | double ret = 0.0; |
| 4732 | double mult = 1; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4733 | int ok = 0; |
| 4734 | |
| 4735 | CHECK_ERROR; |
| 4736 | if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4737 | XP_ERROR(XPATH_NUMBER_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4738 | } |
| 4739 | while ((CUR >= '0') && (CUR <= '9')) { |
| 4740 | ret = ret * 10 + (CUR - '0'); |
| 4741 | ok = 1; |
| 4742 | NEXT; |
| 4743 | } |
| 4744 | if (CUR == '.') { |
| 4745 | NEXT; |
| 4746 | if (((CUR < '0') || (CUR > '9')) && (!ok)) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4747 | XP_ERROR(XPATH_NUMBER_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4748 | } |
| 4749 | while ((CUR >= '0') && (CUR <= '9')) { |
| 4750 | mult /= 10; |
| 4751 | ret = ret + (CUR - '0') * mult; |
| 4752 | NEXT; |
| 4753 | } |
| 4754 | } |
| 4755 | valuePush(ctxt, xmlXPathNewFloat(ret)); |
| 4756 | } |
| 4757 | |
| 4758 | /** |
| 4759 | * xmlXPathEvalLiteral: |
| 4760 | * @ctxt: the XPath Parser context |
| 4761 | * |
| 4762 | * Parse a Literal and push it on the stack. |
| 4763 | * |
| 4764 | * [29] Literal ::= '"' [^"]* '"' |
| 4765 | * | "'" [^']* "'" |
| 4766 | * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4767 | * TODO: xmlXPathEvalLiteral memory allocation could be improved. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4768 | */ |
| 4769 | void |
| 4770 | xmlXPathEvalLiteral(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4771 | const xmlChar *q; |
| 4772 | xmlChar *ret = NULL; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4773 | |
| 4774 | if (CUR == '"') { |
| 4775 | NEXT; |
| 4776 | q = CUR_PTR; |
| 4777 | while ((IS_CHAR(CUR)) && (CUR != '"')) |
| 4778 | NEXT; |
| 4779 | if (!IS_CHAR(CUR)) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4780 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4781 | } else { |
| 4782 | ret = xmlStrndup(q, CUR_PTR - q); |
| 4783 | NEXT; |
| 4784 | } |
| 4785 | } else if (CUR == '\'') { |
| 4786 | NEXT; |
| 4787 | q = CUR_PTR; |
| 4788 | while ((IS_CHAR(CUR)) && (CUR != '\'')) |
| 4789 | NEXT; |
| 4790 | if (!IS_CHAR(CUR)) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4791 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4792 | } else { |
| 4793 | ret = xmlStrndup(q, CUR_PTR - q); |
| 4794 | NEXT; |
| 4795 | } |
| 4796 | } else { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4797 | XP_ERROR(XPATH_START_LITERAL_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4798 | } |
| 4799 | if (ret == NULL) return; |
| 4800 | valuePush(ctxt, xmlXPathNewString(ret)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4801 | xmlFree(ret); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4802 | } |
| 4803 | |
| 4804 | /** |
| 4805 | * xmlXPathEvalVariableReference: |
| 4806 | * @ctxt: the XPath Parser context |
| 4807 | * |
| 4808 | * Parse a VariableReference, evaluate it and push it on the stack. |
| 4809 | * |
| 4810 | * The variable bindings consist of a mapping from variable names |
| 4811 | * to variable values. The value of a variable is an object, which |
| 4812 | * of any of the types that are possible for the value of an expression, |
| 4813 | * and may also be of additional types not specified here. |
| 4814 | * |
| 4815 | * Early evaluation is possible since: |
| 4816 | * The variable bindings [...] used to evaluate a subexpression are |
| 4817 | * always the same as those used to evaluate the containing expression. |
| 4818 | * |
| 4819 | * [36] VariableReference ::= '$' QName |
| 4820 | */ |
| 4821 | void |
| 4822 | xmlXPathEvalVariableReference(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4823 | xmlChar *name; |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4824 | xmlChar *prefix; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4825 | xmlXPathObjectPtr value; |
| 4826 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4827 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4828 | if (CUR != '$') { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4829 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4830 | } |
Daniel Veillard | 5a2b697 | 2001-01-20 21:15:50 +0000 | [diff] [blame] | 4831 | NEXT; |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4832 | name = xmlXPathParseQName(ctxt, &prefix); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4833 | if (name == NULL) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4834 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4835 | } |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4836 | if (prefix == NULL) { |
| 4837 | value = xmlXPathVariableLookup(ctxt->context, name); |
| 4838 | } else { |
| 4839 | TODO; |
| 4840 | value = NULL; |
| 4841 | } |
| 4842 | xmlFree(name); |
| 4843 | if (prefix != NULL) xmlFree(prefix); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4844 | if (value == NULL) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4845 | XP_ERROR(XPATH_UNDEF_VARIABLE_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4846 | } |
| 4847 | valuePush(ctxt, value); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4848 | SKIP_BLANKS; |
| 4849 | } |
| 4850 | |
| 4851 | /** |
| 4852 | * xmlXPathIsNodeType: |
| 4853 | * @ctxt: the XPath Parser context |
| 4854 | * @name: a name string |
| 4855 | * |
| 4856 | * Is the name given a NodeType one. |
| 4857 | * |
| 4858 | * [38] NodeType ::= 'comment' |
| 4859 | * | 'text' |
| 4860 | * | 'processing-instruction' |
| 4861 | * | 'node' |
| 4862 | * |
| 4863 | * Returns 1 if true 0 otherwise |
| 4864 | */ |
| 4865 | int |
| 4866 | xmlXPathIsNodeType(const xmlChar *name) { |
| 4867 | if (name == NULL) |
| 4868 | return(0); |
| 4869 | |
| 4870 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 4871 | return(1); |
| 4872 | if (xmlStrEqual(name, BAD_CAST "text")) |
| 4873 | return(1); |
| 4874 | if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 4875 | return(1); |
| 4876 | if (xmlStrEqual(name, BAD_CAST "node")) |
| 4877 | return(1); |
| 4878 | return(0); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4879 | } |
| 4880 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4881 | /** |
| 4882 | * xmlXPathEvalFunctionCall: |
| 4883 | * @ctxt: the XPath Parser context |
| 4884 | * |
| 4885 | * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')' |
| 4886 | * [17] Argument ::= Expr |
| 4887 | * |
| 4888 | * Parse and evaluate a function call, the evaluation of all arguments are |
| 4889 | * pushed on the stack |
| 4890 | */ |
| 4891 | void |
| 4892 | xmlXPathEvalFunctionCall(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4893 | xmlChar *name; |
| 4894 | xmlChar *prefix; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4895 | xmlXPathFunction func; |
| 4896 | int nbargs = 0; |
| 4897 | |
| 4898 | name = xmlXPathParseQName(ctxt, &prefix); |
| 4899 | if (name == NULL) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4900 | XP_ERROR(XPATH_EXPR_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4901 | } |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4902 | SKIP_BLANKS; |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4903 | if (prefix == NULL) { |
| 4904 | func = xmlXPathFunctionLookup(ctxt->context, name); |
| 4905 | } else { |
| 4906 | TODO; |
| 4907 | func = NULL; |
| 4908 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4909 | if (func == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4910 | xmlFree(name); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4911 | if (prefix != NULL) xmlFree(prefix); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4912 | XP_ERROR(XPATH_UNKNOWN_FUNC_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4913 | } |
| 4914 | #ifdef DEBUG_EXPR |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4915 | if (prefix == NULL) |
| 4916 | xmlGenericError(xmlGenericErrorContext, "Calling function %s\n", |
| 4917 | name); |
| 4918 | else |
| 4919 | xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n", |
| 4920 | prefix, name); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4921 | #endif |
| 4922 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4923 | xmlFree(name); |
| 4924 | if (prefix != NULL) xmlFree(prefix); |
| 4925 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4926 | if (CUR != '(') { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4927 | XP_ERROR(XPATH_EXPR_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4928 | } |
| 4929 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4930 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4931 | |
| 4932 | while (CUR != ')') { |
| 4933 | xmlXPathEvalExpr(ctxt); |
| 4934 | nbargs++; |
| 4935 | if (CUR == ')') break; |
| 4936 | if (CUR != ',') { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4937 | XP_ERROR(XPATH_EXPR_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4938 | } |
| 4939 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4940 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4941 | } |
| 4942 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4943 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4944 | func(ctxt, nbargs); |
| 4945 | } |
| 4946 | |
| 4947 | /** |
| 4948 | * xmlXPathEvalPrimaryExpr: |
| 4949 | * @ctxt: the XPath Parser context |
| 4950 | * |
| 4951 | * [15] PrimaryExpr ::= VariableReference |
| 4952 | * | '(' Expr ')' |
| 4953 | * | Literal |
| 4954 | * | Number |
| 4955 | * | FunctionCall |
| 4956 | * |
| 4957 | * Parse and evaluate a primary expression, then push the result on the stack |
| 4958 | */ |
| 4959 | void |
| 4960 | xmlXPathEvalPrimaryExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4961 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4962 | if (CUR == '$') xmlXPathEvalVariableReference(ctxt); |
| 4963 | else if (CUR == '(') { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4964 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4965 | SKIP_BLANKS; |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4966 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4967 | if (CUR != ')') { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4968 | XP_ERROR(XPATH_EXPR_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4969 | } |
| 4970 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4971 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4972 | } else if (IS_DIGIT(CUR)) { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4973 | xmlXPathEvalNumber(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4974 | } else if ((CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4975 | xmlXPathEvalLiteral(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4976 | } else { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4977 | xmlXPathEvalFunctionCall(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4978 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4979 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | /** |
| 4983 | * xmlXPathEvalFilterExpr: |
| 4984 | * @ctxt: the XPath Parser context |
| 4985 | * |
| 4986 | * [20] FilterExpr ::= PrimaryExpr |
| 4987 | * | FilterExpr Predicate |
| 4988 | * |
| 4989 | * Parse and evaluate a filter expression, then push the result on the stack |
| 4990 | * Square brackets are used to filter expressions in the same way that |
| 4991 | * they are used in location paths. It is an error if the expression to |
| 4992 | * be filtered does not evaluate to a node-set. The context node list |
| 4993 | * used for evaluating the expression in square brackets is the node-set |
| 4994 | * to be filtered listed in document order. |
| 4995 | */ |
| 4996 | |
| 4997 | void |
| 4998 | xmlXPathEvalFilterExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4999 | xmlXPathEvalPrimaryExpr(ctxt); |
| 5000 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5001 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5002 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5003 | while (CUR == '[') { |
Daniel Veillard | f62ceff | 2000-11-24 23:36:01 +0000 | [diff] [blame] | 5004 | if ((ctxt->value == NULL) || |
| 5005 | ((ctxt->value->type != XPATH_NODESET) && |
| 5006 | (ctxt->value->type != XPATH_LOCATIONSET))) |
| 5007 | XP_ERROR(XPATH_INVALID_TYPE) |
| 5008 | |
| 5009 | if (ctxt->value->type == XPATH_NODESET) |
| 5010 | xmlXPathEvalPredicate(ctxt); |
| 5011 | else |
| 5012 | xmlXPtrEvalRangePredicate(ctxt); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5013 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5014 | } |
| 5015 | |
| 5016 | |
| 5017 | } |
| 5018 | |
| 5019 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5020 | * xmlXPathScanName: |
| 5021 | * @ctxt: the XPath Parser context |
| 5022 | * |
| 5023 | * Trickery: parse an XML name but without consuming the input flow |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5024 | * Needed to avoid insanity in the parser state. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5025 | * |
| 5026 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 5027 | * CombiningChar | Extender |
| 5028 | * |
| 5029 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 5030 | * |
| 5031 | * [6] Names ::= Name (S Name)* |
| 5032 | * |
| 5033 | * Returns the Name parsed or NULL |
| 5034 | */ |
| 5035 | |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5036 | xmlChar * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5037 | xmlXPathScanName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5038 | xmlChar buf[XML_MAX_NAMELEN]; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5039 | int len = 0; |
| 5040 | |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5041 | SKIP_BLANKS; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5042 | if (!IS_LETTER(CUR) && (CUR != '_') && |
| 5043 | (CUR != ':')) { |
| 5044 | return(NULL); |
| 5045 | } |
| 5046 | |
| 5047 | while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) || |
| 5048 | (NXT(len) == '.') || (NXT(len) == '-') || |
| 5049 | (NXT(len) == '_') || (NXT(len) == ':') || |
| 5050 | (IS_COMBINING(NXT(len))) || |
| 5051 | (IS_EXTENDER(NXT(len)))) { |
| 5052 | buf[len] = NXT(len); |
| 5053 | len++; |
| 5054 | if (len >= XML_MAX_NAMELEN) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5055 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5056 | "xmlScanName: reached XML_MAX_NAMELEN limit\n"); |
| 5057 | while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) || |
| 5058 | (NXT(len) == '.') || (NXT(len) == '-') || |
| 5059 | (NXT(len) == '_') || (NXT(len) == ':') || |
| 5060 | (IS_COMBINING(NXT(len))) || |
| 5061 | (IS_EXTENDER(NXT(len)))) |
| 5062 | len++; |
| 5063 | break; |
| 5064 | } |
| 5065 | } |
| 5066 | return(xmlStrndup(buf, len)); |
| 5067 | } |
| 5068 | |
| 5069 | /** |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5070 | * xmlXPathEvalPathExpr: |
| 5071 | * @ctxt: the XPath Parser context |
| 5072 | * |
| 5073 | * [19] PathExpr ::= LocationPath |
| 5074 | * | FilterExpr |
| 5075 | * | FilterExpr '/' RelativeLocationPath |
| 5076 | * | FilterExpr '//' RelativeLocationPath |
| 5077 | * |
| 5078 | * Parse and evaluate a path expression, then push the result on the stack |
| 5079 | * The / operator and // operators combine an arbitrary expression |
| 5080 | * and a relative location path. It is an error if the expression |
| 5081 | * does not evaluate to a node-set. |
| 5082 | * The / operator does composition in the same way as when / is |
| 5083 | * used in a location path. As in location paths, // is short for |
| 5084 | * /descendant-or-self::node()/. |
| 5085 | */ |
| 5086 | |
| 5087 | void |
| 5088 | xmlXPathEvalPathExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5089 | int lc = 1; /* Should we branch to LocationPath ? */ |
| 5090 | xmlChar *name = NULL; /* we may have to preparse a name to find out */ |
| 5091 | |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5092 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5093 | if ((CUR == '$') || (CUR == '(') || (IS_DIGIT(CUR)) || |
| 5094 | (CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5095 | lc = 0; |
| 5096 | } else if (CUR == '/') { |
| 5097 | /* relative or absolute location path */ |
| 5098 | lc = 1; |
| 5099 | } else if (CUR == '@') { |
| 5100 | /* relative abbreviated attribute location path */ |
| 5101 | lc = 1; |
| 5102 | } else if (CUR == '.') { |
| 5103 | /* relative abbreviated attribute location path */ |
| 5104 | lc = 1; |
| 5105 | } else { |
| 5106 | /* |
| 5107 | * Problem is finding if we have a name here whether it's: |
| 5108 | * - a nodetype |
| 5109 | * - a function call in which case it's followed by '(' |
| 5110 | * - an axis in which case it's followed by ':' |
| 5111 | * - a element name |
| 5112 | * We do an a priori analysis here rather than having to |
| 5113 | * maintain parsed token content through the recursive function |
| 5114 | * calls. This looks uglier but makes the code quite easier to |
| 5115 | * read/write/debug. |
| 5116 | */ |
| 5117 | SKIP_BLANKS; |
| 5118 | name = xmlXPathScanName(ctxt); |
| 5119 | if (name != NULL) { |
| 5120 | int len =xmlStrlen(name); |
| 5121 | int blank = 0; |
| 5122 | |
| 5123 | while (NXT(len) != 0) { |
| 5124 | if (NXT(len) == '/') { |
| 5125 | /* element name */ |
| 5126 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5127 | xmlGenericError(xmlGenericErrorContext, |
| 5128 | "PathExpr: AbbrRelLocation\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5129 | #endif |
| 5130 | lc = 1; |
| 5131 | break; |
| 5132 | } else if (IS_BLANK(NXT(len))) { |
| 5133 | /* skip to next */ |
| 5134 | blank = 1; |
| 5135 | } else if (NXT(len) == ':') { |
| 5136 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5137 | xmlGenericError(xmlGenericErrorContext, |
| 5138 | "PathExpr: AbbrRelLocation\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5139 | #endif |
| 5140 | lc = 1; |
| 5141 | break; |
| 5142 | } else if ((NXT(len) == '(')) { |
| 5143 | /* Note Type or Function */ |
| 5144 | if (xmlXPathIsNodeType(name)) { |
| 5145 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5146 | xmlGenericError(xmlGenericErrorContext, |
| 5147 | "PathExpr: Type search\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5148 | #endif |
| 5149 | lc = 1; |
| 5150 | } else { |
| 5151 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5152 | xmlGenericError(xmlGenericErrorContext, |
| 5153 | "PathExpr: function call\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5154 | #endif |
| 5155 | lc = 0; |
| 5156 | } |
| 5157 | break; |
| 5158 | } else if ((NXT(len) == '[')) { |
| 5159 | /* element name */ |
| 5160 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5161 | xmlGenericError(xmlGenericErrorContext, |
| 5162 | "PathExpr: AbbrRelLocation\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5163 | #endif |
| 5164 | lc = 1; |
| 5165 | break; |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 5166 | } else if ((NXT(len) == '<') || (NXT(len) == '>') || |
| 5167 | (NXT(len) == '=')) { |
| 5168 | lc = 1; |
| 5169 | break; |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5170 | } else { |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 5171 | lc = 1; |
| 5172 | break; |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5173 | } |
| 5174 | len++; |
| 5175 | } |
| 5176 | if (NXT(len) == 0) { |
| 5177 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5178 | xmlGenericError(xmlGenericErrorContext, |
| 5179 | "PathExpr: AbbrRelLocation\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5180 | #endif |
| 5181 | /* element name */ |
| 5182 | lc = 1; |
| 5183 | } |
| 5184 | xmlFree(name); |
| 5185 | } else { |
| 5186 | /* make sure all cases are covered explicitely */ |
| 5187 | XP_ERROR(XPATH_EXPR_ERROR); |
| 5188 | } |
| 5189 | } |
| 5190 | |
| 5191 | if (lc) { |
Daniel Veillard | c4f4f0b | 2000-10-29 17:46:30 +0000 | [diff] [blame] | 5192 | if (CUR == '/') |
| 5193 | xmlXPathRoot(ctxt); |
Daniel Veillard | 5a2b697 | 2001-01-20 21:15:50 +0000 | [diff] [blame] | 5194 | else { |
| 5195 | /* TAG:9999 */ |
| 5196 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 5197 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5198 | xmlXPathEvalLocationPath(ctxt); |
| 5199 | } else { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5200 | xmlXPathEvalFilterExpr(ctxt); |
| 5201 | CHECK_ERROR; |
| 5202 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 5203 | SKIP(2); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5204 | SKIP_BLANKS; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5205 | xmlXPathNodeCollectAndTest(ctxt, AXIS_DESCENDANT_OR_SELF, |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 5206 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5207 | ctxt->context->node = NULL; |
| 5208 | xmlXPathEvalRelativeLocationPath(ctxt); |
| 5209 | } else if (CUR == '/') { |
| 5210 | xmlXPathEvalRelativeLocationPath(ctxt); |
| 5211 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5212 | } |
| 5213 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | /** |
| 5217 | * xmlXPathEvalUnionExpr: |
| 5218 | * @ctxt: the XPath Parser context |
| 5219 | * |
| 5220 | * [18] UnionExpr ::= PathExpr |
| 5221 | * | UnionExpr '|' PathExpr |
| 5222 | * |
| 5223 | * Parse and evaluate an union expression, then push the result on the stack |
| 5224 | */ |
| 5225 | |
| 5226 | void |
| 5227 | xmlXPathEvalUnionExpr(xmlXPathParserContextPtr ctxt) { |
| 5228 | xmlXPathEvalPathExpr(ctxt); |
| 5229 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5230 | SKIP_BLANKS; |
Daniel Veillard | 0810898 | 2001-01-03 15:24:58 +0000 | [diff] [blame] | 5231 | while (CUR == '|') { |
| 5232 | xmlXPathObjectPtr obj1,obj2, tmp; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5233 | |
| 5234 | CHECK_TYPE(XPATH_NODESET); |
| 5235 | obj1 = valuePop(ctxt); |
Daniel Veillard | 0810898 | 2001-01-03 15:24:58 +0000 | [diff] [blame] | 5236 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 5237 | valuePush(ctxt, tmp); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5238 | |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5239 | NEXT; |
| 5240 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5241 | xmlXPathEvalPathExpr(ctxt); |
| 5242 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5243 | CHECK_TYPE(XPATH_NODESET); |
| 5244 | obj2 = valuePop(ctxt); |
| 5245 | obj1->nodesetval = xmlXPathNodeSetMerge(obj1->nodesetval, |
| 5246 | obj2->nodesetval); |
Daniel Veillard | 0810898 | 2001-01-03 15:24:58 +0000 | [diff] [blame] | 5247 | if (ctxt->value == tmp) { |
| 5248 | tmp = valuePop(ctxt); |
| 5249 | xmlXPathFreeObject(tmp); |
| 5250 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 5251 | valuePush(ctxt, obj1); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5252 | xmlXPathFreeObject(obj2); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5253 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5254 | } |
| 5255 | } |
| 5256 | |
| 5257 | /** |
| 5258 | * xmlXPathEvalUnaryExpr: |
| 5259 | * @ctxt: the XPath Parser context |
| 5260 | * |
| 5261 | * [27] UnaryExpr ::= UnionExpr |
| 5262 | * | '-' UnaryExpr |
| 5263 | * |
| 5264 | * Parse and evaluate an unary expression, then push the result on the stack |
| 5265 | */ |
| 5266 | |
| 5267 | void |
| 5268 | xmlXPathEvalUnaryExpr(xmlXPathParserContextPtr ctxt) { |
| 5269 | int minus = 0; |
| 5270 | |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5271 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5272 | if (CUR == '-') { |
| 5273 | minus = 1; |
| 5274 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5275 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5276 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5277 | xmlXPathEvalUnionExpr(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5278 | CHECK_ERROR; |
| 5279 | if (minus) { |
| 5280 | xmlXPathValueFlipSign(ctxt); |
| 5281 | } |
| 5282 | } |
| 5283 | |
| 5284 | /** |
| 5285 | * xmlXPathEvalMultiplicativeExpr: |
| 5286 | * @ctxt: the XPath Parser context |
| 5287 | * |
| 5288 | * [26] MultiplicativeExpr ::= UnaryExpr |
| 5289 | * | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 5290 | * | MultiplicativeExpr 'div' UnaryExpr |
| 5291 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 5292 | * [34] MultiplyOperator ::= '*' |
| 5293 | * |
| 5294 | * Parse and evaluate an Additive expression, then push the result on the stack |
| 5295 | */ |
| 5296 | |
| 5297 | void |
| 5298 | xmlXPathEvalMultiplicativeExpr(xmlXPathParserContextPtr ctxt) { |
| 5299 | xmlXPathEvalUnaryExpr(ctxt); |
| 5300 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5301 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5302 | while ((CUR == '*') || |
| 5303 | ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) || |
| 5304 | ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) { |
| 5305 | int op = -1; |
| 5306 | |
| 5307 | if (CUR == '*') { |
| 5308 | op = 0; |
| 5309 | NEXT; |
| 5310 | } else if (CUR == 'd') { |
| 5311 | op = 1; |
| 5312 | SKIP(3); |
| 5313 | } else if (CUR == 'm') { |
| 5314 | op = 2; |
| 5315 | SKIP(3); |
| 5316 | } |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5317 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5318 | xmlXPathEvalUnaryExpr(ctxt); |
| 5319 | CHECK_ERROR; |
| 5320 | switch (op) { |
| 5321 | case 0: |
| 5322 | xmlXPathMultValues(ctxt); |
| 5323 | break; |
| 5324 | case 1: |
| 5325 | xmlXPathDivValues(ctxt); |
| 5326 | break; |
| 5327 | case 2: |
| 5328 | xmlXPathModValues(ctxt); |
| 5329 | break; |
| 5330 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5331 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5332 | } |
| 5333 | } |
| 5334 | |
| 5335 | /** |
| 5336 | * xmlXPathEvalAdditiveExpr: |
| 5337 | * @ctxt: the XPath Parser context |
| 5338 | * |
| 5339 | * [25] AdditiveExpr ::= MultiplicativeExpr |
| 5340 | * | AdditiveExpr '+' MultiplicativeExpr |
| 5341 | * | AdditiveExpr '-' MultiplicativeExpr |
| 5342 | * |
| 5343 | * Parse and evaluate an Additive expression, then push the result on the stack |
| 5344 | */ |
| 5345 | |
| 5346 | void |
| 5347 | xmlXPathEvalAdditiveExpr(xmlXPathParserContextPtr ctxt) { |
| 5348 | xmlXPathEvalMultiplicativeExpr(ctxt); |
| 5349 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5350 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5351 | while ((CUR == '+') || (CUR == '-')) { |
| 5352 | int plus; |
| 5353 | |
| 5354 | if (CUR == '+') plus = 1; |
| 5355 | else plus = 0; |
| 5356 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5357 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5358 | xmlXPathEvalMultiplicativeExpr(ctxt); |
| 5359 | CHECK_ERROR; |
| 5360 | if (plus) xmlXPathAddValues(ctxt); |
| 5361 | else xmlXPathSubValues(ctxt); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5362 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5363 | } |
| 5364 | } |
| 5365 | |
| 5366 | /** |
| 5367 | * xmlXPathEvalRelationalExpr: |
| 5368 | * @ctxt: the XPath Parser context |
| 5369 | * |
| 5370 | * [24] RelationalExpr ::= AdditiveExpr |
| 5371 | * | RelationalExpr '<' AdditiveExpr |
| 5372 | * | RelationalExpr '>' AdditiveExpr |
| 5373 | * | RelationalExpr '<=' AdditiveExpr |
| 5374 | * | RelationalExpr '>=' AdditiveExpr |
| 5375 | * |
| 5376 | * A <= B > C is allowed ? Answer from James, yes with |
| 5377 | * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr |
| 5378 | * which is basically what got implemented. |
| 5379 | * |
| 5380 | * Parse and evaluate a Relational expression, then push the result |
| 5381 | * on the stack |
| 5382 | */ |
| 5383 | |
| 5384 | void |
| 5385 | xmlXPathEvalRelationalExpr(xmlXPathParserContextPtr ctxt) { |
| 5386 | xmlXPathEvalAdditiveExpr(ctxt); |
| 5387 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5388 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5389 | while ((CUR == '<') || |
| 5390 | (CUR == '>') || |
| 5391 | ((CUR == '<') && (NXT(1) == '=')) || |
| 5392 | ((CUR == '>') && (NXT(1) == '='))) { |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 5393 | int inf, strict, ret; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5394 | |
| 5395 | if (CUR == '<') inf = 1; |
| 5396 | else inf = 0; |
| 5397 | if (NXT(1) == '=') strict = 0; |
| 5398 | else strict = 1; |
| 5399 | NEXT; |
| 5400 | if (!strict) NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5401 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5402 | xmlXPathEvalAdditiveExpr(ctxt); |
| 5403 | CHECK_ERROR; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 5404 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 5405 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5406 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5407 | } |
| 5408 | } |
| 5409 | |
| 5410 | /** |
| 5411 | * xmlXPathEvalEqualityExpr: |
| 5412 | * @ctxt: the XPath Parser context |
| 5413 | * |
| 5414 | * [23] EqualityExpr ::= RelationalExpr |
| 5415 | * | EqualityExpr '=' RelationalExpr |
| 5416 | * | EqualityExpr '!=' RelationalExpr |
| 5417 | * |
| 5418 | * A != B != C is allowed ? Answer from James, yes with |
| 5419 | * (RelationalExpr = RelationalExpr) = RelationalExpr |
| 5420 | * (RelationalExpr != RelationalExpr) != RelationalExpr |
| 5421 | * which is basically what got implemented. |
| 5422 | * |
| 5423 | * Parse and evaluate an Equality expression, then push the result on the stack |
| 5424 | * |
| 5425 | */ |
| 5426 | void |
| 5427 | xmlXPathEvalEqualityExpr(xmlXPathParserContextPtr ctxt) { |
| 5428 | xmlXPathEvalRelationalExpr(ctxt); |
| 5429 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5430 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5431 | while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) { |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 5432 | xmlXPathObjectPtr res; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5433 | int eq, equal; |
| 5434 | |
| 5435 | if (CUR == '=') eq = 1; |
| 5436 | else eq = 0; |
| 5437 | NEXT; |
| 5438 | if (!eq) NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5439 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5440 | xmlXPathEvalRelationalExpr(ctxt); |
| 5441 | CHECK_ERROR; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 5442 | equal = xmlXPathEqualValues(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5443 | if (eq) res = xmlXPathNewBoolean(equal); |
| 5444 | else res = xmlXPathNewBoolean(!equal); |
| 5445 | valuePush(ctxt, res); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5446 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5447 | } |
| 5448 | } |
| 5449 | |
| 5450 | /** |
| 5451 | * xmlXPathEvalAndExpr: |
| 5452 | * @ctxt: the XPath Parser context |
| 5453 | * |
| 5454 | * [22] AndExpr ::= EqualityExpr |
| 5455 | * | AndExpr 'and' EqualityExpr |
| 5456 | * |
| 5457 | * Parse and evaluate an AND expression, then push the result on the stack |
| 5458 | * |
| 5459 | */ |
| 5460 | void |
| 5461 | xmlXPathEvalAndExpr(xmlXPathParserContextPtr ctxt) { |
| 5462 | xmlXPathEvalEqualityExpr(ctxt); |
| 5463 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5464 | SKIP_BLANKS; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5465 | while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5466 | xmlXPathObjectPtr arg1, arg2; |
| 5467 | |
| 5468 | SKIP(3); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5469 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5470 | xmlXPathEvalEqualityExpr(ctxt); |
| 5471 | CHECK_ERROR; |
Daniel Veillard | 0f2a53c | 2001-02-05 17:57:33 +0000 | [diff] [blame] | 5472 | xmlXPathBooleanFunction(ctxt, 1); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5473 | arg2 = valuePop(ctxt); |
Daniel Veillard | 0f2a53c | 2001-02-05 17:57:33 +0000 | [diff] [blame] | 5474 | xmlXPathBooleanFunction(ctxt, 1); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5475 | arg1 = valuePop(ctxt); |
| 5476 | arg1->boolval &= arg2->boolval; |
| 5477 | valuePush(ctxt, arg1); |
| 5478 | xmlXPathFreeObject(arg2); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5479 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5480 | } |
| 5481 | } |
| 5482 | |
| 5483 | /** |
| 5484 | * xmlXPathEvalExpr: |
| 5485 | * @ctxt: the XPath Parser context |
| 5486 | * |
| 5487 | * [14] Expr ::= OrExpr |
| 5488 | * [21] OrExpr ::= AndExpr |
| 5489 | * | OrExpr 'or' AndExpr |
| 5490 | * |
| 5491 | * Parse and evaluate an expression, then push the result on the stack |
| 5492 | * |
| 5493 | */ |
| 5494 | void |
| 5495 | xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) { |
| 5496 | xmlXPathEvalAndExpr(ctxt); |
| 5497 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5498 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5499 | while ((CUR == 'o') && (NXT(1) == 'r')) { |
| 5500 | xmlXPathObjectPtr arg1, arg2; |
| 5501 | |
| 5502 | SKIP(2); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5503 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5504 | xmlXPathEvalAndExpr(ctxt); |
| 5505 | CHECK_ERROR; |
Daniel Veillard | 0f2a53c | 2001-02-05 17:57:33 +0000 | [diff] [blame] | 5506 | xmlXPathBooleanFunction(ctxt, 1); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5507 | arg2 = valuePop(ctxt); |
Daniel Veillard | 0f2a53c | 2001-02-05 17:57:33 +0000 | [diff] [blame] | 5508 | xmlXPathBooleanFunction(ctxt, 1); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5509 | arg1 = valuePop(ctxt); |
| 5510 | arg1->boolval |= arg2->boolval; |
| 5511 | valuePush(ctxt, arg1); |
| 5512 | xmlXPathFreeObject(arg2); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5513 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5514 | } |
| 5515 | } |
| 5516 | |
| 5517 | /** |
| 5518 | * xmlXPathEvaluatePredicateResult: |
| 5519 | * @ctxt: the XPath Parser context |
| 5520 | * @res: the Predicate Expression evaluation result |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5521 | * |
| 5522 | * Evaluate a predicate result for the current node. |
| 5523 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 5524 | * the result to a boolean. If the result is a number, the result will |
| 5525 | * be converted to true if the number is equal to the position of the |
| 5526 | * context node in the context node list (as returned by the position |
| 5527 | * function) and will be converted to false otherwise; if the result |
| 5528 | * is not a number, then the result will be converted as if by a call |
| 5529 | * to the boolean function. |
Daniel Veillard | 2c833b6 | 2001-02-03 08:52:06 +0000 | [diff] [blame] | 5530 | * |
| 5531 | * Return 1 if predicate is true, 0 otherwise |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5532 | */ |
| 5533 | int |
| 5534 | xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5535 | xmlXPathObjectPtr res) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5536 | if (res == NULL) return(0); |
| 5537 | switch (res->type) { |
| 5538 | case XPATH_BOOLEAN: |
| 5539 | return(res->boolval); |
| 5540 | case XPATH_NUMBER: |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5541 | return(res->floatval == ctxt->context->proximityPosition); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5542 | case XPATH_NODESET: |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 5543 | case XPATH_XSLT_TREE: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5544 | return(res->nodesetval->nodeNr != 0); |
| 5545 | case XPATH_STRING: |
| 5546 | return((res->stringval != NULL) && |
| 5547 | (xmlStrlen(res->stringval) != 0)); |
| 5548 | default: |
| 5549 | STRANGE |
| 5550 | } |
| 5551 | return(0); |
| 5552 | } |
| 5553 | |
| 5554 | /** |
| 5555 | * xmlXPathEvalPredicate: |
| 5556 | * @ctxt: the XPath Parser context |
| 5557 | * |
| 5558 | * [8] Predicate ::= '[' PredicateExpr ']' |
| 5559 | * [9] PredicateExpr ::= Expr |
| 5560 | * |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5561 | * --------------------- |
| 5562 | * For each node in the node-set to be filtered, the PredicateExpr is |
| 5563 | * evaluated with that node as the context node, with the number of nodes |
| 5564 | * in the node-set as the context size, and with the proximity position |
| 5565 | * of the node in the node-set with respect to the axis as the context |
| 5566 | * position; if PredicateExpr evaluates to true for that node, the node |
| 5567 | * is included in the new node-set; otherwise, it is not included. |
| 5568 | * --------------------- |
| 5569 | * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5570 | * Parse and evaluate a predicate for all the elements of the |
| 5571 | * current node list. Then refine the list by removing all |
| 5572 | * nodes where the predicate is false. |
| 5573 | */ |
| 5574 | void |
| 5575 | xmlXPathEvalPredicate(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5576 | const xmlChar *cur; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5577 | xmlXPathObjectPtr res; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5578 | xmlXPathObjectPtr obj, tmp; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5579 | xmlNodeSetPtr newset = NULL; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5580 | xmlNodeSetPtr oldset; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5581 | int i; |
| 5582 | |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5583 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5584 | if (CUR != '[') { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 5585 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5586 | } |
| 5587 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5588 | SKIP_BLANKS; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5589 | |
| 5590 | /* |
| 5591 | * Extract the old set, and then evaluate the result of the |
| 5592 | * expression for all the element in the set. use it to grow |
| 5593 | * up a new set. |
| 5594 | */ |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5595 | CHECK_TYPE(XPATH_NODESET); |
| 5596 | obj = valuePop(ctxt); |
| 5597 | oldset = obj->nodesetval; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5598 | ctxt->context->node = NULL; |
| 5599 | |
| 5600 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5601 | ctxt->context->contextSize = 0; |
| 5602 | ctxt->context->proximityPosition = 0; |
Daniel Veillard | ff9c330 | 2000-10-13 16:38:25 +0000 | [diff] [blame] | 5603 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5604 | res = valuePop(ctxt); |
| 5605 | if (res != NULL) |
| 5606 | xmlXPathFreeObject(res); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5607 | valuePush(ctxt, obj); |
Daniel Veillard | ff9c330 | 2000-10-13 16:38:25 +0000 | [diff] [blame] | 5608 | CHECK_ERROR; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5609 | } else { |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5610 | /* |
| 5611 | * Save the expression pointer since we will have to evaluate |
| 5612 | * it multiple times. Initialize the new set. |
| 5613 | */ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5614 | cur = ctxt->cur; |
| 5615 | newset = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5616 | |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5617 | for (i = 0; i < oldset->nodeNr; i++) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5618 | ctxt->cur = cur; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5619 | |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5620 | /* |
| 5621 | * Run the evaluation with a node list made of a single item |
| 5622 | * in the nodeset. |
| 5623 | */ |
| 5624 | ctxt->context->node = oldset->nodeTab[i]; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5625 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 5626 | valuePush(ctxt, tmp); |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5627 | ctxt->context->contextSize = oldset->nodeNr; |
| 5628 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5629 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5630 | xmlXPathEvalExpr(ctxt); |
| 5631 | CHECK_ERROR; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5632 | |
| 5633 | /* |
| 5634 | * The result of the evaluation need to be tested to |
| 5635 | * decided whether the filter succeeded or not |
| 5636 | */ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5637 | res = valuePop(ctxt); |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5638 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 5639 | xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); |
| 5640 | } |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5641 | |
| 5642 | /* |
| 5643 | * Cleanup |
| 5644 | */ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5645 | if (res != NULL) |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5646 | xmlXPathFreeObject(res); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5647 | if (ctxt->value == tmp) { |
| 5648 | res = valuePop(ctxt); |
| 5649 | xmlXPathFreeObject(res); |
| 5650 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5651 | |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5652 | ctxt->context->node = NULL; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5653 | } |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5654 | |
| 5655 | /* |
| 5656 | * The result is used as the new evaluation set. |
| 5657 | */ |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5658 | xmlXPathFreeObject(obj); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5659 | ctxt->context->node = NULL; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5660 | ctxt->context->contextSize = -1; |
| 5661 | ctxt->context->proximityPosition = -1; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5662 | valuePush(ctxt, xmlXPathWrapNodeSet(newset)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5663 | } |
| 5664 | if (CUR != ']') { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 5665 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5666 | } |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5667 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5668 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5669 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5670 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5671 | xmlGenericError(xmlGenericErrorContext, "After predicate : "); |
| 5672 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 5673 | ctxt->value->nodesetval); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5674 | #endif |
| 5675 | } |
| 5676 | |
| 5677 | /** |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5678 | * xmlXPathEvalNodeTest: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5679 | * @ctxt: the XPath Parser context |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5680 | * @test: pointer to a xmlXPathTestVal |
| 5681 | * @type: pointer to a xmlXPathTypeVal |
| 5682 | * @prefix: placeholder for a possible name prefix |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5683 | * |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5684 | * [7] NodeTest ::= NameTest |
| 5685 | * | NodeType '(' ')' |
| 5686 | * | 'processing-instruction' '(' Literal ')' |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5687 | * |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5688 | * [37] NameTest ::= '*' |
| 5689 | * | NCName ':' '*' |
| 5690 | * | QName |
| 5691 | * [38] NodeType ::= 'comment' |
| 5692 | * | 'text' |
| 5693 | * | 'processing-instruction' |
| 5694 | * | 'node' |
| 5695 | * |
| 5696 | * Returns the name found and update @test, @type and @prefix appropriately |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5697 | */ |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5698 | xmlChar * |
| 5699 | xmlXPathEvalNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test, |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5700 | xmlXPathTypeVal *type, const xmlChar **prefix, xmlChar *name) { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5701 | int blanks; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5702 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5703 | if ((test == NULL) || (type == NULL) || (prefix == NULL)) { |
| 5704 | STRANGE; |
| 5705 | return(NULL); |
| 5706 | } |
| 5707 | *type = 0; |
| 5708 | *test = 0; |
| 5709 | *prefix = NULL; |
| 5710 | SKIP_BLANKS; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5711 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5712 | if ((name == NULL) && (CUR == '*')) { |
| 5713 | /* |
| 5714 | * All elements |
| 5715 | */ |
| 5716 | NEXT; |
| 5717 | *test = NODE_TEST_ALL; |
| 5718 | return(NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5719 | } |
| 5720 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5721 | if (name == NULL) |
| 5722 | name = xmlXPathParseNCName(ctxt); |
| 5723 | if (name == NULL) { |
| 5724 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 5725 | } |
| 5726 | |
| 5727 | blanks = IS_BLANK(CUR); |
| 5728 | SKIP_BLANKS; |
| 5729 | if (CUR == '(') { |
| 5730 | NEXT; |
| 5731 | /* |
| 5732 | * NodeType or PI search |
| 5733 | */ |
| 5734 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 5735 | *type = NODE_TYPE_COMMENT; |
| 5736 | else if (xmlStrEqual(name, BAD_CAST "node")) |
| 5737 | *type = NODE_TYPE_NODE; |
| 5738 | else if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 5739 | *type = NODE_TYPE_PI; |
| 5740 | else if (xmlStrEqual(name, BAD_CAST "text")) |
| 5741 | *type = NODE_TYPE_TEXT; |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5742 | else { |
| 5743 | if (name != NULL) |
| 5744 | xmlFree(name); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5745 | XP_ERROR0(XPATH_EXPR_ERROR); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5746 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5747 | |
| 5748 | *test = NODE_TEST_TYPE; |
| 5749 | |
| 5750 | SKIP_BLANKS; |
| 5751 | if (*type == NODE_TYPE_PI) { |
| 5752 | /* |
| 5753 | * Specific case: search a PI by name. |
| 5754 | */ |
| 5755 | xmlXPathObjectPtr cur; |
| 5756 | |
| 5757 | if (name != NULL) |
| 5758 | xmlFree(name); |
| 5759 | |
| 5760 | xmlXPathEvalLiteral(ctxt); |
| 5761 | CHECK_ERROR 0; |
| 5762 | xmlXPathStringFunction(ctxt, 1); |
| 5763 | CHECK_ERROR0; |
| 5764 | cur = valuePop(ctxt); |
| 5765 | name = xmlStrdup(cur->stringval); |
| 5766 | xmlXPathFreeObject(cur); |
| 5767 | SKIP_BLANKS; |
| 5768 | } |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5769 | if (CUR != ')') { |
| 5770 | if (name != NULL) |
| 5771 | xmlFree(name); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5772 | XP_ERROR0(XPATH_UNCLOSED_ERROR); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5773 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5774 | NEXT; |
| 5775 | return(name); |
| 5776 | } |
| 5777 | *test = NODE_TEST_NAME; |
| 5778 | if ((!blanks) && (CUR == ':')) { |
| 5779 | NEXT; |
| 5780 | |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5781 | /* |
| 5782 | * get the namespace name for this prefix |
| 5783 | */ |
| 5784 | *prefix = xmlXPathNsLookup(ctxt->context, name); |
| 5785 | if (name != NULL) |
| 5786 | xmlFree(name); |
| 5787 | if (*prefix == NULL) { |
| 5788 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 5789 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5790 | |
| 5791 | if (CUR == '*') { |
| 5792 | /* |
| 5793 | * All elements |
| 5794 | */ |
| 5795 | NEXT; |
| 5796 | *test = NODE_TEST_ALL; |
| 5797 | return(NULL); |
| 5798 | } |
| 5799 | |
| 5800 | name = xmlXPathParseNCName(ctxt); |
| 5801 | if (name == NULL) { |
| 5802 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 5803 | } |
| 5804 | } |
| 5805 | return(name); |
| 5806 | } |
| 5807 | |
| 5808 | /** |
| 5809 | * xmlXPathIsAxisName: |
| 5810 | * @name: a preparsed name token |
| 5811 | * |
| 5812 | * [6] AxisName ::= 'ancestor' |
| 5813 | * | 'ancestor-or-self' |
| 5814 | * | 'attribute' |
| 5815 | * | 'child' |
| 5816 | * | 'descendant' |
| 5817 | * | 'descendant-or-self' |
| 5818 | * | 'following' |
| 5819 | * | 'following-sibling' |
| 5820 | * | 'namespace' |
| 5821 | * | 'parent' |
| 5822 | * | 'preceding' |
| 5823 | * | 'preceding-sibling' |
| 5824 | * | 'self' |
| 5825 | * |
| 5826 | * Returns the axis or 0 |
| 5827 | */ |
| 5828 | xmlXPathAxisVal |
| 5829 | xmlXPathIsAxisName(const xmlChar *name) { |
| 5830 | xmlXPathAxisVal ret = 0; |
| 5831 | switch (name[0]) { |
| 5832 | case 'a': |
| 5833 | if (xmlStrEqual(name, BAD_CAST "ancestor")) |
| 5834 | ret = AXIS_ANCESTOR; |
| 5835 | if (xmlStrEqual(name, BAD_CAST "ancestor-or-self")) |
| 5836 | ret = AXIS_ANCESTOR_OR_SELF; |
| 5837 | if (xmlStrEqual(name, BAD_CAST "attribute")) |
| 5838 | ret = AXIS_ATTRIBUTE; |
| 5839 | break; |
| 5840 | case 'c': |
| 5841 | if (xmlStrEqual(name, BAD_CAST "child")) |
| 5842 | ret = AXIS_CHILD; |
| 5843 | break; |
| 5844 | case 'd': |
| 5845 | if (xmlStrEqual(name, BAD_CAST "descendant")) |
| 5846 | ret = AXIS_DESCENDANT; |
| 5847 | if (xmlStrEqual(name, BAD_CAST "descendant-or-self")) |
| 5848 | ret = AXIS_DESCENDANT_OR_SELF; |
| 5849 | break; |
| 5850 | case 'f': |
| 5851 | if (xmlStrEqual(name, BAD_CAST "following")) |
| 5852 | ret = AXIS_FOLLOWING; |
| 5853 | if (xmlStrEqual(name, BAD_CAST "following-sibling")) |
| 5854 | ret = AXIS_FOLLOWING_SIBLING; |
| 5855 | break; |
| 5856 | case 'n': |
| 5857 | if (xmlStrEqual(name, BAD_CAST "namespace")) |
| 5858 | ret = AXIS_NAMESPACE; |
| 5859 | break; |
| 5860 | case 'p': |
| 5861 | if (xmlStrEqual(name, BAD_CAST "parent")) |
| 5862 | ret = AXIS_PARENT; |
| 5863 | if (xmlStrEqual(name, BAD_CAST "preceding")) |
| 5864 | ret = AXIS_PRECEDING; |
| 5865 | if (xmlStrEqual(name, BAD_CAST "preceding-sibling")) |
| 5866 | ret = AXIS_PRECEDING_SIBLING; |
| 5867 | break; |
| 5868 | case 's': |
| 5869 | if (xmlStrEqual(name, BAD_CAST "self")) |
| 5870 | ret = AXIS_SELF; |
| 5871 | break; |
| 5872 | } |
| 5873 | return(ret); |
| 5874 | } |
| 5875 | |
| 5876 | /** |
| 5877 | * xmlXPathEvalAxisSpecifier: |
| 5878 | * @ctxt: the XPath Parser context |
| 5879 | * |
| 5880 | * |
| 5881 | * Returns the axis found |
| 5882 | */ |
| 5883 | xmlXPathAxisVal |
| 5884 | xmlXPathEvalAxisSpecifier(xmlXPathParserContextPtr ctxt) { |
| 5885 | xmlXPathAxisVal ret = AXIS_CHILD; |
| 5886 | int blank = 0; |
| 5887 | xmlChar *name; |
| 5888 | |
| 5889 | if (CUR == '@') { |
| 5890 | NEXT; |
| 5891 | return(AXIS_ATTRIBUTE); |
| 5892 | } else { |
| 5893 | name = xmlXPathParseNCName(ctxt); |
| 5894 | if (name == NULL) { |
| 5895 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 5896 | } |
| 5897 | if (IS_BLANK(CUR)) |
| 5898 | blank = 1; |
| 5899 | SKIP_BLANKS; |
| 5900 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 5901 | ret = xmlXPathIsAxisName(name); |
| 5902 | } else if ((blank) && (CUR == ':')) |
| 5903 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 5904 | |
| 5905 | xmlFree(name); |
| 5906 | } |
| 5907 | return(ret); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5908 | } |
| 5909 | |
| 5910 | /** |
| 5911 | * xmlXPathEvalStep: |
| 5912 | * @ctxt: the XPath Parser context |
| 5913 | * |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5914 | * [4] Step ::= AxisSpecifier NodeTest Predicate* |
| 5915 | * | AbbreviatedStep |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5916 | * |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5917 | * [12] AbbreviatedStep ::= '.' | '..' |
| 5918 | * |
| 5919 | * [5] AxisSpecifier ::= AxisName '::' |
| 5920 | * | AbbreviatedAxisSpecifier |
| 5921 | * |
| 5922 | * [13] AbbreviatedAxisSpecifier ::= '@'? |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5923 | * |
| 5924 | * Modified for XPtr range support as: |
| 5925 | * |
| 5926 | * [4xptr] Step ::= AxisSpecifier NodeTest Predicate* |
| 5927 | * | AbbreviatedStep |
| 5928 | * | 'range-to' '(' Expr ')' Predicate* |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5929 | * |
| 5930 | * Evaluate one step in a Location Path |
| 5931 | * A location step of . is short for self::node(). This is |
| 5932 | * particularly useful in conjunction with //. For example, the |
| 5933 | * location path .//para is short for |
| 5934 | * self::node()/descendant-or-self::node()/child::para |
| 5935 | * and so will select all para descendant elements of the context |
| 5936 | * node. |
| 5937 | * Similarly, a location step of .. is short for parent::node(). |
| 5938 | * For example, ../title is short for parent::node()/child::title |
| 5939 | * and so will select the title children of the parent of the context |
| 5940 | * node. |
| 5941 | */ |
| 5942 | void |
| 5943 | xmlXPathEvalStep(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5944 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5945 | if ((CUR == '.') && (NXT(1) == '.')) { |
| 5946 | SKIP(2); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5947 | SKIP_BLANKS; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5948 | xmlXPathNodeCollectAndTest(ctxt, AXIS_PARENT, |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 5949 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5950 | } else if (CUR == '.') { |
| 5951 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5952 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5953 | } else { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5954 | xmlChar *name = NULL; |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5955 | const xmlChar *prefix = NULL; |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5956 | xmlXPathTestVal test; |
| 5957 | xmlXPathAxisVal axis; |
| 5958 | xmlXPathTypeVal type; |
| 5959 | |
| 5960 | /* |
| 5961 | * The modification needed for XPointer change to the production |
| 5962 | */ |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5963 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5964 | if (ctxt->context->xptr) { |
| 5965 | name = xmlXPathParseNCName(ctxt); |
| 5966 | if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) { |
| 5967 | xmlFree(name); |
| 5968 | SKIP_BLANKS; |
| 5969 | if (CUR != '(') { |
| 5970 | XP_ERROR(XPATH_EXPR_ERROR); |
| 5971 | } |
| 5972 | NEXT; |
| 5973 | SKIP_BLANKS; |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5974 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5975 | xmlXPtrRangeToFunction(ctxt, 1); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5976 | CHECK_ERROR; |
| 5977 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5978 | SKIP_BLANKS; |
| 5979 | if (CUR != ')') { |
| 5980 | XP_ERROR(XPATH_EXPR_ERROR); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5981 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5982 | NEXT; |
| 5983 | goto eval_predicates; |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5984 | } |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5985 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5986 | #endif |
| 5987 | if (name == NULL) |
| 5988 | name = xmlXPathParseNCName(ctxt); |
| 5989 | if (name != NULL) { |
| 5990 | axis = xmlXPathIsAxisName(name); |
| 5991 | if (axis != 0) { |
| 5992 | SKIP_BLANKS; |
| 5993 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 5994 | SKIP(2); |
| 5995 | xmlFree(name); |
| 5996 | name = NULL; |
| 5997 | } else { |
| 5998 | /* an element name can conflict with an axis one :-\ */ |
| 5999 | axis = AXIS_CHILD; |
| 6000 | } |
| 6001 | } else { |
| 6002 | axis = AXIS_CHILD; |
| 6003 | } |
| 6004 | } else if (CUR == '@') { |
| 6005 | NEXT; |
| 6006 | axis = AXIS_ATTRIBUTE; |
| 6007 | } else { |
| 6008 | axis = AXIS_CHILD; |
| 6009 | } |
| 6010 | |
| 6011 | CHECK_ERROR; |
| 6012 | |
| 6013 | name = xmlXPathEvalNodeTest(ctxt, &test, &type, &prefix, name); |
| 6014 | if (test == 0) |
| 6015 | return; |
| 6016 | |
| 6017 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 6018 | xmlGenericError(xmlGenericErrorContext, |
| 6019 | "Basis : computing new set\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 6020 | #endif |
| 6021 | xmlXPathNodeCollectAndTest(ctxt, axis, test, type, prefix, name); |
| 6022 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 6023 | xmlGenericError(xmlGenericErrorContext, "Basis : "); |
| 6024 | xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 6025 | #endif |
| 6026 | if (name != NULL) |
| 6027 | xmlFree(name); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 6028 | |
| 6029 | eval_predicates: |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6030 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6031 | while (CUR == '[') { |
| 6032 | xmlXPathEvalPredicate(ctxt); |
| 6033 | } |
| 6034 | } |
| 6035 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 6036 | xmlGenericError(xmlGenericErrorContext, "Step : "); |
| 6037 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 6038 | ctxt->value->nodesetval); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6039 | #endif |
| 6040 | } |
| 6041 | |
| 6042 | /** |
| 6043 | * xmlXPathEvalRelativeLocationPath: |
| 6044 | * @ctxt: the XPath Parser context |
| 6045 | * |
| 6046 | * [3] RelativeLocationPath ::= Step |
| 6047 | * | RelativeLocationPath '/' Step |
| 6048 | * | AbbreviatedRelativeLocationPath |
| 6049 | * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
| 6050 | * |
| 6051 | */ |
| 6052 | void |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 6053 | #ifdef VMS |
| 6054 | xmlXPathEvalRelLocationPath |
| 6055 | #else |
| 6056 | xmlXPathEvalRelativeLocationPath |
| 6057 | #endif |
| 6058 | (xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6059 | SKIP_BLANKS; |
Daniel Veillard | c8df0aa | 2000-10-10 23:50:30 +0000 | [diff] [blame] | 6060 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 6061 | SKIP(2); |
| 6062 | SKIP_BLANKS; |
| 6063 | xmlXPathNodeCollectAndTest(ctxt, AXIS_DESCENDANT_OR_SELF, |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 6064 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | c8df0aa | 2000-10-10 23:50:30 +0000 | [diff] [blame] | 6065 | } else if (CUR == '/') { |
| 6066 | NEXT; |
| 6067 | SKIP_BLANKS; |
| 6068 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6069 | xmlXPathEvalStep(ctxt); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6070 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6071 | while (CUR == '/') { |
| 6072 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 6073 | SKIP(2); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6074 | SKIP_BLANKS; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6075 | xmlXPathNodeCollectAndTest(ctxt, AXIS_DESCENDANT_OR_SELF, |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 6076 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6077 | xmlXPathEvalStep(ctxt); |
| 6078 | } else if (CUR == '/') { |
| 6079 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6080 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6081 | xmlXPathEvalStep(ctxt); |
| 6082 | } |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6083 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6084 | } |
| 6085 | } |
| 6086 | |
| 6087 | /** |
| 6088 | * xmlXPathEvalLocationPath: |
| 6089 | * @ctxt: the XPath Parser context |
| 6090 | * |
| 6091 | * [1] LocationPath ::= RelativeLocationPath |
| 6092 | * | AbsoluteLocationPath |
| 6093 | * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
| 6094 | * | AbbreviatedAbsoluteLocationPath |
| 6095 | * [10] AbbreviatedAbsoluteLocationPath ::= |
| 6096 | * '//' RelativeLocationPath |
| 6097 | * |
| 6098 | * // is short for /descendant-or-self::node()/. For example, |
| 6099 | * //para is short for /descendant-or-self::node()/child::para and |
| 6100 | * so will select any para element in the document (even a para element |
| 6101 | * that is a document element will be selected by //para since the |
| 6102 | * document element node is a child of the root node); div//para is |
| 6103 | * short for div/descendant-or-self::node()/child::para and so will |
| 6104 | * select all para descendants of div children. |
| 6105 | */ |
| 6106 | void |
| 6107 | xmlXPathEvalLocationPath(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6108 | SKIP_BLANKS; |
| 6109 | if (CUR != '/') { |
| 6110 | xmlXPathEvalRelativeLocationPath(ctxt); |
| 6111 | } else { |
| 6112 | while (CUR == '/') { |
| 6113 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 6114 | SKIP(2); |
| 6115 | SKIP_BLANKS; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6116 | xmlXPathNodeCollectAndTest(ctxt, |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6117 | AXIS_DESCENDANT_OR_SELF, NODE_TEST_TYPE, |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 6118 | NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 6119 | xmlXPathEvalRelativeLocationPath(ctxt); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6120 | } else if (CUR == '/') { |
| 6121 | NEXT; |
| 6122 | SKIP_BLANKS; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6123 | if (CUR != 0) |
| 6124 | xmlXPathEvalRelativeLocationPath(ctxt); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6125 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6126 | } |
| 6127 | } |
| 6128 | } |
| 6129 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6130 | /** |
| 6131 | * xmlXPathEval: |
| 6132 | * @str: the XPath expression |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6133 | * @ctx: the XPath context |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6134 | * |
| 6135 | * Evaluate the XPath Location Path in the given context. |
| 6136 | * |
| 6137 | * Returns the xmlXPathObjectPtr resulting from the eveluation or NULL. |
| 6138 | * the caller has to free the object. |
| 6139 | */ |
| 6140 | xmlXPathObjectPtr |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6141 | xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) { |
| 6142 | xmlXPathParserContextPtr ctxt; |
Daniel Veillard | 41e0651 | 2000-11-13 11:47:47 +0000 | [diff] [blame] | 6143 | xmlXPathObjectPtr res = NULL, tmp, init = NULL; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6144 | int stack = 0; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6145 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 6146 | xmlXPathInit(); |
| 6147 | |
Daniel Veillard | df7ef2a | 2000-10-25 10:11:55 +0000 | [diff] [blame] | 6148 | CHECK_CONTEXT(ctx) |
| 6149 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6150 | ctxt = xmlXPathNewParserContext(str, ctx); |
Daniel Veillard | 5a2b697 | 2001-01-20 21:15:50 +0000 | [diff] [blame] | 6151 | /**** TAG:9999 |
Daniel Veillard | 41e0651 | 2000-11-13 11:47:47 +0000 | [diff] [blame] | 6152 | if (ctx->node != NULL) { |
| 6153 | init = xmlXPathNewNodeSet(ctx->node); |
| 6154 | valuePush(ctxt, init); |
| 6155 | } |
Daniel Veillard | 5a2b697 | 2001-01-20 21:15:50 +0000 | [diff] [blame] | 6156 | ****/ |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 6157 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6158 | |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 6159 | if (ctxt->value == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 6160 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 6161 | "xmlXPathEval: evaluation failed\n"); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6162 | } else { |
| 6163 | res = valuePop(ctxt); |
| 6164 | } |
| 6165 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6166 | do { |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6167 | tmp = valuePop(ctxt); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6168 | if (tmp != NULL) { |
Daniel Veillard | 41e0651 | 2000-11-13 11:47:47 +0000 | [diff] [blame] | 6169 | if (tmp != init) |
| 6170 | stack++; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 6171 | xmlXPathFreeObject(tmp); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6172 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 6173 | } while (tmp != NULL); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6174 | if (stack != 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 6175 | xmlGenericError(xmlGenericErrorContext, |
| 6176 | "xmlXPathEval: %d object left on the stack\n", |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6177 | stack); |
| 6178 | } |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6179 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 6180 | xmlXPathFreeObject(res); |
| 6181 | res = NULL; |
| 6182 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 6183 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6184 | xmlXPathFreeParserContext(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6185 | return(res); |
| 6186 | } |
| 6187 | |
| 6188 | /** |
| 6189 | * xmlXPathEvalExpression: |
| 6190 | * @str: the XPath expression |
| 6191 | * @ctxt: the XPath context |
| 6192 | * |
| 6193 | * Evaluate the XPath expression in the given context. |
| 6194 | * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 6195 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6196 | * the caller has to free the object. |
| 6197 | */ |
| 6198 | xmlXPathObjectPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 6199 | xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6200 | xmlXPathParserContextPtr pctxt; |
| 6201 | xmlXPathObjectPtr res, tmp; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6202 | int stack = 0; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6203 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 6204 | xmlXPathInit(); |
| 6205 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6206 | CHECK_CONTEXT(ctxt) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6207 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6208 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 6209 | xmlXPathEvalExpr(pctxt); |
| 6210 | |
| 6211 | res = valuePop(pctxt); |
| 6212 | do { |
| 6213 | tmp = valuePop(pctxt); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6214 | if (tmp != NULL) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 6215 | xmlXPathFreeObject(tmp); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6216 | stack++; |
| 6217 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6218 | } while (tmp != NULL); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6219 | if (stack != 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 6220 | xmlGenericError(xmlGenericErrorContext, |
| 6221 | "xmlXPathEvalExpression: %d object left on the stack\n", |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6222 | stack); |
| 6223 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6224 | xmlXPathFreeParserContext(pctxt); |
| 6225 | return(res); |
| 6226 | } |
| 6227 | |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 6228 | /** |
| 6229 | * xmlXPathRegisterAllFunctions: |
| 6230 | * @ctxt: the XPath context |
| 6231 | * |
| 6232 | * Registers all default XPath functions in this context |
| 6233 | */ |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 6234 | void |
| 6235 | xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt) |
| 6236 | { |
| 6237 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean", |
| 6238 | xmlXPathBooleanFunction); |
| 6239 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling", |
| 6240 | xmlXPathCeilingFunction); |
| 6241 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count", |
| 6242 | xmlXPathCountFunction); |
| 6243 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat", |
| 6244 | xmlXPathConcatFunction); |
| 6245 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains", |
| 6246 | xmlXPathContainsFunction); |
| 6247 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id", |
| 6248 | xmlXPathIdFunction); |
| 6249 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false", |
| 6250 | xmlXPathFalseFunction); |
| 6251 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor", |
| 6252 | xmlXPathFloorFunction); |
| 6253 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last", |
| 6254 | xmlXPathLastFunction); |
| 6255 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang", |
| 6256 | xmlXPathLangFunction); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 6257 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name", |
| 6258 | xmlXPathLocalNameFunction); |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 6259 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not", |
| 6260 | xmlXPathNotFunction); |
| 6261 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name", |
| 6262 | xmlXPathNameFunction); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 6263 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri", |
| 6264 | xmlXPathNamespaceURIFunction); |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 6265 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space", |
| 6266 | xmlXPathNormalizeFunction); |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 6267 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number", |
| 6268 | xmlXPathNumberFunction); |
| 6269 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position", |
| 6270 | xmlXPathPositionFunction); |
| 6271 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round", |
| 6272 | xmlXPathRoundFunction); |
| 6273 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string", |
| 6274 | xmlXPathStringFunction); |
| 6275 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length", |
| 6276 | xmlXPathStringLengthFunction); |
| 6277 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with", |
| 6278 | xmlXPathStartsWithFunction); |
| 6279 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring", |
| 6280 | xmlXPathSubstringFunction); |
| 6281 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before", |
| 6282 | xmlXPathSubstringBeforeFunction); |
| 6283 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after", |
| 6284 | xmlXPathSubstringAfterFunction); |
| 6285 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum", |
| 6286 | xmlXPathSumFunction); |
| 6287 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true", |
| 6288 | xmlXPathTrueFunction); |
| 6289 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate", |
| 6290 | xmlXPathTranslateFunction); |
| 6291 | } |
| 6292 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 6293 | #endif /* LIBXML_XPATH_ENABLED */ |