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 | /** |
| 673 | * xmlXPathNodeSetMerge: |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 674 | * @val1: the first NodeSet or NULL |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 675 | * @val2: the second NodeSet |
| 676 | * |
| 677 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 678 | * 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] | 679 | * |
| 680 | * Returns val1 once extended or NULL in case of error. |
| 681 | */ |
| 682 | xmlNodeSetPtr |
| 683 | xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 684 | int i, j, initNr; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 685 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 686 | if (val2 == NULL) return(val1); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 687 | if (val1 == NULL) { |
| 688 | val1 = xmlXPathNodeSetCreate(NULL); |
| 689 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 690 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 691 | initNr = val1->nodeNr; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 692 | |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 693 | for (i = 0;i < val2->nodeNr;i++) { |
| 694 | /* |
| 695 | * check against doublons |
| 696 | */ |
| 697 | for (j = 0; j < initNr; j++) |
| 698 | if (val1->nodeTab[j] == val2->nodeTab[i]) continue; |
| 699 | |
| 700 | /* |
| 701 | * grow the nodeTab if needed |
| 702 | */ |
| 703 | if (val1->nodeMax == 0) { |
| 704 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 705 | sizeof(xmlNodePtr)); |
| 706 | if (val1->nodeTab == NULL) { |
| 707 | xmlGenericError(xmlGenericErrorContext, |
| 708 | "xmlXPathNodeSetMerge: out of memory\n"); |
| 709 | return(NULL); |
| 710 | } |
| 711 | memset(val1->nodeTab, 0 , |
| 712 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 713 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 714 | } else if (val1->nodeNr == val1->nodeMax) { |
| 715 | xmlNodePtr *temp; |
| 716 | |
| 717 | val1->nodeMax *= 2; |
| 718 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 719 | sizeof(xmlNodePtr)); |
| 720 | if (temp == NULL) { |
| 721 | xmlGenericError(xmlGenericErrorContext, |
| 722 | "xmlXPathNodeSetMerge: out of memory\n"); |
| 723 | return(NULL); |
| 724 | } |
| 725 | val1->nodeTab = temp; |
| 726 | } |
| 727 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
| 728 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 729 | |
| 730 | return(val1); |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * xmlXPathNodeSetDel: |
| 735 | * @cur: the initial node set |
| 736 | * @val: an xmlNodePtr |
| 737 | * |
| 738 | * Removes an xmlNodePtr from an existing NodeSet |
| 739 | */ |
| 740 | void |
| 741 | xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 742 | int i; |
| 743 | |
| 744 | if (cur == NULL) return; |
| 745 | if (val == NULL) return; |
| 746 | |
| 747 | /* |
| 748 | * check against doublons |
| 749 | */ |
| 750 | for (i = 0;i < cur->nodeNr;i++) |
| 751 | if (cur->nodeTab[i] == val) break; |
| 752 | |
| 753 | if (i >= cur->nodeNr) { |
| 754 | #ifdef DEBUG |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 755 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 756 | "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n", |
| 757 | val->name); |
| 758 | #endif |
| 759 | return; |
| 760 | } |
| 761 | cur->nodeNr--; |
| 762 | for (;i < cur->nodeNr;i++) |
| 763 | cur->nodeTab[i] = cur->nodeTab[i + 1]; |
| 764 | cur->nodeTab[cur->nodeNr] = NULL; |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * xmlXPathNodeSetRemove: |
| 769 | * @cur: the initial node set |
| 770 | * @val: the index to remove |
| 771 | * |
| 772 | * Removes an entry from an existing NodeSet list. |
| 773 | */ |
| 774 | void |
| 775 | xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) { |
| 776 | if (cur == NULL) return; |
| 777 | if (val >= cur->nodeNr) return; |
| 778 | cur->nodeNr--; |
| 779 | for (;val < cur->nodeNr;val++) |
| 780 | cur->nodeTab[val] = cur->nodeTab[val + 1]; |
| 781 | cur->nodeTab[cur->nodeNr] = NULL; |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * xmlXPathFreeNodeSet: |
| 786 | * @obj: the xmlNodeSetPtr to free |
| 787 | * |
| 788 | * Free the NodeSet compound (not the actual nodes !). |
| 789 | */ |
| 790 | void |
| 791 | xmlXPathFreeNodeSet(xmlNodeSetPtr obj) { |
| 792 | if (obj == NULL) return; |
| 793 | if (obj->nodeTab != NULL) { |
| 794 | #ifdef DEBUG |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 795 | memset(obj->nodeTab, 0xB , (size_t) sizeof(xmlNodePtr) * obj->nodeMax); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 796 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 797 | xmlFree(obj->nodeTab); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 798 | } |
| 799 | #ifdef DEBUG |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 800 | memset(obj, 0xB , (size_t) sizeof(xmlNodeSet)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 801 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 802 | xmlFree(obj); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Daniel Veillard | 8a7642f | 2001-01-22 10:45:16 +0000 | [diff] [blame] | 805 | /** |
| 806 | * xmlXPathFreeValueTree: |
| 807 | * @obj: the xmlNodeSetPtr to free |
| 808 | * |
| 809 | * Free the NodeSet compound and the actual tree, this is different |
| 810 | * from xmlXPathFreeNodeSet() |
| 811 | */ |
| 812 | void |
| 813 | xmlXPathFreeValueTree(xmlNodeSetPtr obj) { |
| 814 | int i; |
| 815 | |
| 816 | if (obj == NULL) return; |
| 817 | for (i = 0;i < obj->nodeNr;i++) |
| 818 | if (obj->nodeTab[i] != NULL) |
| 819 | xmlFreeNode(obj->nodeTab[i]); |
| 820 | |
| 821 | if (obj->nodeTab != NULL) { |
| 822 | #ifdef DEBUG |
| 823 | memset(obj->nodeTab, 0xB , (size_t) sizeof(xmlNodePtr) * obj->nodeMax); |
| 824 | #endif |
| 825 | xmlFree(obj->nodeTab); |
| 826 | } |
| 827 | #ifdef DEBUG |
| 828 | memset(obj, 0xB , (size_t) sizeof(xmlNodeSet)); |
| 829 | #endif |
| 830 | xmlFree(obj); |
| 831 | } |
| 832 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 833 | #if defined(DEBUG) || defined(DEBUG_STEP) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 834 | /** |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 835 | * xmlGenericErrorContextNodeSet: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 836 | * @output: a FILE * for the output |
| 837 | * @obj: the xmlNodeSetPtr to free |
| 838 | * |
| 839 | * Quick display of a NodeSet |
| 840 | */ |
| 841 | void |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 842 | xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 843 | int i; |
| 844 | |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 845 | if (output == NULL) output = xmlGenericErrorContext; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 846 | if (obj == NULL) { |
| 847 | fprintf(output, "NodeSet == NULL !\n"); |
| 848 | return; |
| 849 | } |
| 850 | if (obj->nodeNr == 0) { |
| 851 | fprintf(output, "NodeSet is empty\n"); |
| 852 | return; |
| 853 | } |
| 854 | if (obj->nodeTab == NULL) { |
| 855 | fprintf(output, " nodeTab == NULL !\n"); |
| 856 | return; |
| 857 | } |
| 858 | for (i = 0; i < obj->nodeNr; i++) { |
| 859 | if (obj->nodeTab[i] == NULL) { |
| 860 | fprintf(output, " NULL !\n"); |
| 861 | return; |
| 862 | } |
Daniel Veillard | 7c1206f | 1999-10-14 09:10:25 +0000 | [diff] [blame] | 863 | if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) || |
| 864 | (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE)) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 865 | fprintf(output, " /"); |
| 866 | else if (obj->nodeTab[i]->name == NULL) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 867 | fprintf(output, " noname!"); |
| 868 | else fprintf(output, " %s", obj->nodeTab[i]->name); |
| 869 | } |
| 870 | fprintf(output, "\n"); |
| 871 | } |
| 872 | #endif |
| 873 | |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 874 | /** |
| 875 | * xmlXPathNewNodeSet: |
| 876 | * @val: the NodePtr value |
| 877 | * |
| 878 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 879 | * it with the single Node @val |
| 880 | * |
| 881 | * Returns the newly created object. |
| 882 | */ |
| 883 | xmlXPathObjectPtr |
| 884 | xmlXPathNewNodeSet(xmlNodePtr val) { |
| 885 | xmlXPathObjectPtr ret; |
| 886 | |
| 887 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 888 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 889 | xmlGenericError(xmlGenericErrorContext, |
| 890 | "xmlXPathNewNodeSet: out of memory\n"); |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 891 | return(NULL); |
| 892 | } |
| 893 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 894 | ret->type = XPATH_NODESET; |
| 895 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
| 896 | return(ret); |
| 897 | } |
| 898 | |
| 899 | /** |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 900 | * xmlXPathNewValueTree: |
| 901 | * @val: the NodePtr value |
| 902 | * |
| 903 | * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize |
| 904 | * it with the tree root @val |
| 905 | * |
| 906 | * Returns the newly created object. |
| 907 | */ |
| 908 | xmlXPathObjectPtr |
| 909 | xmlXPathNewValueTree(xmlNodePtr val) { |
| 910 | xmlXPathObjectPtr ret; |
| 911 | |
| 912 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 913 | if (ret == NULL) { |
| 914 | xmlGenericError(xmlGenericErrorContext, |
| 915 | "xmlXPathNewNodeSet: out of memory\n"); |
| 916 | return(NULL); |
| 917 | } |
| 918 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 919 | ret->type = XPATH_XSLT_TREE; |
| 920 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
| 921 | return(ret); |
| 922 | } |
| 923 | |
| 924 | /** |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 925 | * xmlXPathNewNodeSetList: |
| 926 | * @val: an existing NodeSet |
| 927 | * |
| 928 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 929 | * it with the Nodeset @val |
| 930 | * |
| 931 | * Returns the newly created object. |
| 932 | */ |
| 933 | xmlXPathObjectPtr |
| 934 | xmlXPathNewNodeSetList(xmlNodeSetPtr val) { |
| 935 | xmlXPathObjectPtr ret; |
| 936 | int i; |
| 937 | |
| 938 | if (val == NULL) |
| 939 | ret = NULL; |
| 940 | else if (val->nodeTab == NULL) |
| 941 | ret = xmlXPathNewNodeSet(NULL); |
| 942 | else |
| 943 | { |
| 944 | ret = xmlXPathNewNodeSet(val->nodeTab[0]); |
| 945 | for (i = 1; i < val->nodeNr; ++i) |
| 946 | xmlXPathNodeSetAdd(ret->nodesetval, val->nodeTab[i]); |
| 947 | } |
| 948 | |
| 949 | return(ret); |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * xmlXPathWrapNodeSet: |
| 954 | * @val: the NodePtr value |
| 955 | * |
| 956 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 957 | * |
| 958 | * Returns the newly created object. |
| 959 | */ |
| 960 | xmlXPathObjectPtr |
| 961 | xmlXPathWrapNodeSet(xmlNodeSetPtr val) { |
| 962 | xmlXPathObjectPtr ret; |
| 963 | |
| 964 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 965 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 966 | xmlGenericError(xmlGenericErrorContext, |
| 967 | "xmlXPathWrapNodeSet: out of memory\n"); |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 968 | return(NULL); |
| 969 | } |
| 970 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 971 | ret->type = XPATH_NODESET; |
| 972 | ret->nodesetval = val; |
| 973 | return(ret); |
| 974 | } |
| 975 | |
| 976 | /** |
| 977 | * xmlXPathFreeNodeSetList: |
| 978 | * @obj: an existing NodeSetList object |
| 979 | * |
| 980 | * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in |
| 981 | * the list contrary to xmlXPathFreeObject(). |
| 982 | */ |
| 983 | void |
| 984 | xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) { |
| 985 | if (obj == NULL) return; |
| 986 | #ifdef DEBUG |
| 987 | memset(obj, 0xB , (size_t) sizeof(xmlXPathObject)); |
| 988 | #endif |
| 989 | xmlFree(obj); |
| 990 | } |
| 991 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 992 | /************************************************************************ |
| 993 | * * |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 994 | * Routines to handle extra functions * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 995 | * * |
| 996 | ************************************************************************/ |
| 997 | |
| 998 | /** |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 999 | * xmlXPathRegisterFunc: |
| 1000 | * @ctxt: the XPath context |
| 1001 | * @name: the function name |
| 1002 | * @f: the function implementation or NULL |
| 1003 | * |
| 1004 | * Register a new function. If @f is NULL it unregisters the function |
| 1005 | * |
| 1006 | * Returns 0 in case of success, -1 in case of error |
| 1007 | */ |
| 1008 | int |
| 1009 | xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 1010 | xmlXPathFunction f) { |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1011 | return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f)); |
| 1012 | } |
| 1013 | |
| 1014 | /** |
| 1015 | * xmlXPathRegisterFuncNS: |
| 1016 | * @ctxt: the XPath context |
| 1017 | * @name: the function name |
| 1018 | * @ns_uri: the function namespace URI |
| 1019 | * @f: the function implementation or NULL |
| 1020 | * |
| 1021 | * Register a new function. If @f is NULL it unregisters the function |
| 1022 | * |
| 1023 | * Returns 0 in case of success, -1 in case of error |
| 1024 | */ |
| 1025 | int |
| 1026 | xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 1027 | const xmlChar *ns_uri, xmlXPathFunction f) { |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1028 | if (ctxt == NULL) |
| 1029 | return(-1); |
| 1030 | if (name == NULL) |
| 1031 | return(-1); |
| 1032 | |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 1033 | if (ctxt->funcHash == NULL) |
| 1034 | ctxt->funcHash = xmlHashCreate(0); |
| 1035 | if (ctxt->funcHash == NULL) |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1036 | return(-1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1037 | return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, (void *) f)); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * xmlXPathFunctionLookup: |
| 1042 | * @ctxt: the XPath context |
| 1043 | * @name: the function name |
| 1044 | * |
| 1045 | * Search in the Function array of the context for the given |
| 1046 | * function. |
| 1047 | * |
| 1048 | * Returns the xmlXPathFunction or NULL if not found |
| 1049 | */ |
| 1050 | xmlXPathFunction |
| 1051 | xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1052 | return(xmlXPathFunctionLookupNS(ctxt, name, NULL)); |
| 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * xmlXPathFunctionLookupNS: |
| 1057 | * @ctxt: the XPath context |
| 1058 | * @name: the function name |
| 1059 | * @ns_uri: the function namespace URI |
| 1060 | * |
| 1061 | * Search in the Function array of the context for the given |
| 1062 | * function. |
| 1063 | * |
| 1064 | * Returns the xmlXPathFunction or NULL if not found |
| 1065 | */ |
| 1066 | xmlXPathFunction |
| 1067 | xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 1068 | const xmlChar *ns_uri) { |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1069 | if (ctxt == NULL) |
| 1070 | return(NULL); |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 1071 | if (ctxt->funcHash == NULL) |
| 1072 | return(NULL); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1073 | if (name == NULL) |
| 1074 | return(NULL); |
| 1075 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1076 | return((xmlXPathFunction) xmlHashLookup2(ctxt->funcHash, name, ns_uri)); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /** |
| 1080 | * xmlXPathRegisteredFuncsCleanup: |
| 1081 | * @ctxt: the XPath context |
| 1082 | * |
| 1083 | * Cleanup the XPath context data associated to registered functions |
| 1084 | */ |
| 1085 | void |
| 1086 | xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) { |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1087 | if (ctxt == NULL) |
| 1088 | return; |
| 1089 | |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 1090 | xmlHashFree(ctxt->funcHash, NULL); |
| 1091 | ctxt->funcHash = NULL; |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /************************************************************************ |
| 1095 | * * |
| 1096 | * Routines to handle Variable * |
| 1097 | * * |
| 1098 | ************************************************************************/ |
| 1099 | |
| 1100 | /** |
| 1101 | * xmlXPathRegisterVariable: |
| 1102 | * @ctxt: the XPath context |
| 1103 | * @name: the variable name |
| 1104 | * @value: the variable value or NULL |
| 1105 | * |
| 1106 | * Register a new variable value. If @value is NULL it unregisters |
| 1107 | * the variable |
| 1108 | * |
| 1109 | * Returns 0 in case of success, -1 in case of error |
| 1110 | */ |
| 1111 | int |
| 1112 | xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 1113 | xmlXPathObjectPtr value) { |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1114 | return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value)); |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * xmlXPathRegisterVariableNS: |
| 1119 | * @ctxt: the XPath context |
| 1120 | * @name: the variable name |
| 1121 | * @ns_uri: the variable namespace URI |
| 1122 | * @value: the variable value or NULL |
| 1123 | * |
| 1124 | * Register a new variable value. If @value is NULL it unregisters |
| 1125 | * the variable |
| 1126 | * |
| 1127 | * Returns 0 in case of success, -1 in case of error |
| 1128 | */ |
| 1129 | int |
| 1130 | xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 1131 | const xmlChar *ns_uri, |
| 1132 | xmlXPathObjectPtr value) { |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1133 | if (ctxt == NULL) |
| 1134 | return(-1); |
| 1135 | if (name == NULL) |
| 1136 | return(-1); |
| 1137 | |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 1138 | if (ctxt->varHash == NULL) |
| 1139 | ctxt->varHash = xmlHashCreate(0); |
| 1140 | if (ctxt->varHash == NULL) |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1141 | return(-1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1142 | return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri, |
| 1143 | (void *) value, |
| 1144 | (xmlHashDeallocator)xmlXPathFreeObject)); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | /** |
Daniel Veillard | 8f4d975 | 2001-01-19 05:32:34 +0000 | [diff] [blame] | 1148 | * xmlXPathRegisterVariableLookup: |
| 1149 | * @ctxt: the XPath context |
| 1150 | * @f: the lookup function |
| 1151 | * @data: the lookup data |
| 1152 | * |
| 1153 | * register an external mechanism to do variable lookup |
| 1154 | */ |
| 1155 | void |
| 1156 | xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt, |
| 1157 | xmlXPathVariableLookupFunc f, void *data) { |
| 1158 | if (ctxt == NULL) |
| 1159 | return; |
| 1160 | ctxt->varLookupFunc = (void *) f; |
| 1161 | ctxt->varLookupData = data; |
| 1162 | } |
| 1163 | |
| 1164 | /** |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1165 | * xmlXPathVariableLookup: |
| 1166 | * @ctxt: the XPath context |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1167 | * @name: the variable name |
| 1168 | * |
| 1169 | * Search in the Variable array of the context for the given |
| 1170 | * variable value. |
| 1171 | * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1172 | * Returns the value or NULL if not found |
| 1173 | */ |
| 1174 | xmlXPathObjectPtr |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1175 | xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Daniel Veillard | 8f4d975 | 2001-01-19 05:32:34 +0000 | [diff] [blame] | 1176 | if (ctxt == NULL) |
| 1177 | return(NULL); |
| 1178 | |
| 1179 | if (ctxt->varLookupFunc != NULL) { |
| 1180 | xmlXPathObjectPtr ret; |
| 1181 | |
| 1182 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 1183 | (ctxt->varLookupData, name, NULL); |
| 1184 | if (ret != NULL) return(ret); |
| 1185 | } |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1186 | return(xmlXPathVariableLookupNS(ctxt, name, NULL)); |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * xmlXPathVariableLookupNS: |
| 1191 | * @ctxt: the XPath context |
| 1192 | * @name: the variable name |
| 1193 | * @ns_uri: the variable namespace URI |
| 1194 | * |
| 1195 | * Search in the Variable array of the context for the given |
| 1196 | * variable value. |
| 1197 | * |
| 1198 | * Returns the value or NULL if not found |
| 1199 | */ |
| 1200 | xmlXPathObjectPtr |
| 1201 | xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 1202 | const xmlChar *ns_uri) { |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1203 | if (ctxt == NULL) |
| 1204 | return(NULL); |
Daniel Veillard | 8f4d975 | 2001-01-19 05:32:34 +0000 | [diff] [blame] | 1205 | |
| 1206 | if (ctxt->varLookupFunc != NULL) { |
| 1207 | xmlXPathObjectPtr ret; |
| 1208 | |
| 1209 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 1210 | (ctxt->varLookupData, name, ns_uri); |
| 1211 | if (ret != NULL) return(ret); |
| 1212 | } |
| 1213 | |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 1214 | if (ctxt->varHash == NULL) |
| 1215 | return(NULL); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1216 | if (name == NULL) |
| 1217 | return(NULL); |
| 1218 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 1219 | return((xmlXPathObjectPtr) xmlHashLookup2(ctxt->varHash, name, ns_uri)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1222 | /** |
| 1223 | * xmlXPathRegisteredVariablesCleanup: |
| 1224 | * @ctxt: the XPath context |
| 1225 | * |
| 1226 | * Cleanup the XPath context data associated to registered variables |
| 1227 | */ |
| 1228 | void |
| 1229 | xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) { |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1230 | if (ctxt == NULL) |
| 1231 | return; |
| 1232 | |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 1233 | xmlHashFree(ctxt->varHash, NULL); |
| 1234 | ctxt->varHash = NULL; |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 1237 | /** |
| 1238 | * xmlXPathRegisterNs: |
| 1239 | * @ctxt: the XPath context |
| 1240 | * @prefix: the namespace prefix |
| 1241 | * @ns_uri: the namespace name |
| 1242 | * |
| 1243 | * Register a new namespace. If @ns_uri is NULL it unregisters |
| 1244 | * the namespace |
| 1245 | * |
| 1246 | * Returns 0 in case of success, -1 in case of error |
| 1247 | */ |
| 1248 | int |
| 1249 | xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix, |
| 1250 | const xmlChar *ns_uri) { |
| 1251 | if (ctxt == NULL) |
| 1252 | return(-1); |
| 1253 | if (prefix == NULL) |
| 1254 | return(-1); |
| 1255 | |
| 1256 | if (ctxt->nsHash == NULL) |
| 1257 | ctxt->nsHash = xmlHashCreate(10); |
| 1258 | if (ctxt->nsHash == NULL) |
| 1259 | return(-1); |
| 1260 | return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) ns_uri, |
| 1261 | (xmlHashDeallocator)xmlFree)); |
| 1262 | } |
| 1263 | |
| 1264 | /** |
| 1265 | * xmlXPathNsLookup: |
| 1266 | * @ctxt: the XPath context |
| 1267 | * @prefix: the namespace prefix value |
| 1268 | * |
| 1269 | * Search in the namespace declaration array of the context for the given |
| 1270 | * namespace name associated to the given prefix |
| 1271 | * |
| 1272 | * Returns the value or NULL if not found |
| 1273 | */ |
| 1274 | const xmlChar * |
| 1275 | xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) { |
| 1276 | if (ctxt == NULL) |
| 1277 | return(NULL); |
| 1278 | if (prefix == NULL) |
| 1279 | return(NULL); |
| 1280 | if (ctxt->nsHash == NULL) |
| 1281 | return(NULL); |
| 1282 | |
| 1283 | return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix)); |
| 1284 | } |
| 1285 | |
| 1286 | /** |
| 1287 | * xmlXPathRegisteredVariablesCleanup: |
| 1288 | * @ctxt: the XPath context |
| 1289 | * |
| 1290 | * Cleanup the XPath context data associated to registered variables |
| 1291 | */ |
| 1292 | void |
| 1293 | xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) { |
| 1294 | if (ctxt == NULL) |
| 1295 | return; |
| 1296 | |
| 1297 | xmlHashFree(ctxt->nsHash, NULL); |
| 1298 | ctxt->nsHash = NULL; |
| 1299 | } |
| 1300 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1301 | /************************************************************************ |
| 1302 | * * |
| 1303 | * Routines to handle Values * |
| 1304 | * * |
| 1305 | ************************************************************************/ |
| 1306 | |
| 1307 | /* Allocations are terrible, one need to optimize all this !!! */ |
| 1308 | |
| 1309 | /** |
| 1310 | * xmlXPathNewFloat: |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1311 | * @val: the double value |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1312 | * |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1313 | * Create a new xmlXPathObjectPtr of type double and of value @val |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1314 | * |
| 1315 | * Returns the newly created object. |
| 1316 | */ |
| 1317 | xmlXPathObjectPtr |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1318 | xmlXPathNewFloat(double val) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1319 | xmlXPathObjectPtr ret; |
| 1320 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1321 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1322 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1323 | xmlGenericError(xmlGenericErrorContext, |
| 1324 | "xmlXPathNewFloat: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1325 | return(NULL); |
| 1326 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1327 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1328 | ret->type = XPATH_NUMBER; |
| 1329 | ret->floatval = val; |
| 1330 | return(ret); |
| 1331 | } |
| 1332 | |
| 1333 | /** |
| 1334 | * xmlXPathNewBoolean: |
| 1335 | * @val: the boolean value |
| 1336 | * |
| 1337 | * Create a new xmlXPathObjectPtr of type boolean and of value @val |
| 1338 | * |
| 1339 | * Returns the newly created object. |
| 1340 | */ |
| 1341 | xmlXPathObjectPtr |
| 1342 | xmlXPathNewBoolean(int val) { |
| 1343 | xmlXPathObjectPtr ret; |
| 1344 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1345 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1346 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1347 | xmlGenericError(xmlGenericErrorContext, |
| 1348 | "xmlXPathNewBoolean: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1349 | return(NULL); |
| 1350 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1351 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1352 | ret->type = XPATH_BOOLEAN; |
| 1353 | ret->boolval = (val != 0); |
| 1354 | return(ret); |
| 1355 | } |
| 1356 | |
| 1357 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1358 | * xmlXPathNewString: |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1359 | * @val: the xmlChar * value |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1360 | * |
| 1361 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 1362 | * |
| 1363 | * Returns the newly created object. |
| 1364 | */ |
| 1365 | xmlXPathObjectPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1366 | xmlXPathNewString(const xmlChar *val) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1367 | xmlXPathObjectPtr ret; |
| 1368 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1369 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1370 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1371 | xmlGenericError(xmlGenericErrorContext, |
| 1372 | "xmlXPathNewString: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1373 | return(NULL); |
| 1374 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1375 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1376 | ret->type = XPATH_STRING; |
Daniel Veillard | 5a2b697 | 2001-01-20 21:15:50 +0000 | [diff] [blame] | 1377 | if (val != NULL) |
| 1378 | ret->stringval = xmlStrdup(val); |
| 1379 | else |
| 1380 | ret->stringval = xmlStrdup((const xmlChar *)""); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1381 | return(ret); |
| 1382 | } |
| 1383 | |
| 1384 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1385 | * xmlXPathNewCString: |
| 1386 | * @val: the char * value |
| 1387 | * |
| 1388 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 1389 | * |
| 1390 | * Returns the newly created object. |
| 1391 | */ |
| 1392 | xmlXPathObjectPtr |
| 1393 | xmlXPathNewCString(const char *val) { |
| 1394 | xmlXPathObjectPtr ret; |
| 1395 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1396 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1397 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1398 | xmlGenericError(xmlGenericErrorContext, |
| 1399 | "xmlXPathNewCString: out of memory\n"); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1400 | return(NULL); |
| 1401 | } |
| 1402 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 1403 | ret->type = XPATH_STRING; |
| 1404 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 1405 | return(ret); |
| 1406 | } |
| 1407 | |
| 1408 | /** |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1409 | * xmlXPathObjectCopy: |
| 1410 | * @val: the original object |
| 1411 | * |
| 1412 | * allocate a new copy of a given object |
| 1413 | * |
| 1414 | * Returns the newly created object. |
| 1415 | */ |
| 1416 | xmlXPathObjectPtr |
| 1417 | xmlXPathObjectCopy(xmlXPathObjectPtr val) { |
| 1418 | xmlXPathObjectPtr ret; |
| 1419 | |
| 1420 | if (val == NULL) |
| 1421 | return(NULL); |
| 1422 | |
| 1423 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 1424 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1425 | xmlGenericError(xmlGenericErrorContext, |
| 1426 | "xmlXPathObjectCopy: out of memory\n"); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1427 | return(NULL); |
| 1428 | } |
| 1429 | memcpy(ret, val , (size_t) sizeof(xmlXPathObject)); |
| 1430 | switch (val->type) { |
| 1431 | case XPATH_BOOLEAN: |
| 1432 | case XPATH_NUMBER: |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1433 | case XPATH_POINT: |
| 1434 | case XPATH_RANGE: |
| 1435 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 1436 | case XPATH_STRING: |
| 1437 | ret->stringval = xmlStrdup(val->stringval); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1438 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 1439 | case XPATH_XSLT_TREE: |
Daniel Veillard | 8a7642f | 2001-01-22 10:45:16 +0000 | [diff] [blame] | 1440 | if ((val->nodesetval != NULL) && |
| 1441 | (val->nodesetval->nodeTab != NULL)) |
| 1442 | ret->nodesetval = xmlXPathNodeSetCreate( |
| 1443 | xmlCopyNode(val->nodesetval->nodeTab[0], 1)); |
| 1444 | else |
| 1445 | ret->nodesetval = xmlXPathNodeSetCreate(NULL); |
| 1446 | break; |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1447 | case XPATH_NODESET: |
| 1448 | ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval); |
| 1449 | break; |
| 1450 | case XPATH_LOCATIONSET: |
| 1451 | #ifdef LIBXML_XPTR_ENABLED |
| 1452 | { |
| 1453 | xmlLocationSetPtr loc = val->user; |
| 1454 | ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc); |
| 1455 | break; |
| 1456 | } |
| 1457 | #endif |
| 1458 | case XPATH_UNDEFINED: |
| 1459 | case XPATH_USERS: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1460 | xmlGenericError(xmlGenericErrorContext, |
| 1461 | "xmlXPathObjectCopy: unsupported type %d\n", |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1462 | val->type); |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 1463 | break; |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1464 | } |
| 1465 | return(ret); |
| 1466 | } |
| 1467 | |
| 1468 | /** |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1469 | * xmlXPathFreeObject: |
| 1470 | * @obj: the object to free |
| 1471 | * |
| 1472 | * Free up an xmlXPathObjectPtr object. |
| 1473 | */ |
| 1474 | void |
| 1475 | xmlXPathFreeObject(xmlXPathObjectPtr obj) { |
| 1476 | if (obj == NULL) return; |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 1477 | if (obj->type == XPATH_NODESET) { |
| 1478 | if (obj->nodesetval != NULL) |
| 1479 | xmlXPathFreeNodeSet(obj->nodesetval); |
Daniel Veillard | b71379b | 2000-10-09 12:30:39 +0000 | [diff] [blame] | 1480 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | 7e99c63 | 2000-10-06 12:59:53 +0000 | [diff] [blame] | 1481 | } else if (obj->type == XPATH_LOCATIONSET) { |
| 1482 | if (obj->user != NULL) |
Daniel Veillard | c8df0aa | 2000-10-10 23:50:30 +0000 | [diff] [blame] | 1483 | xmlXPtrFreeLocationSet(obj->user); |
Daniel Veillard | b71379b | 2000-10-09 12:30:39 +0000 | [diff] [blame] | 1484 | #endif |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 1485 | } else if (obj->type == XPATH_STRING) { |
| 1486 | if (obj->stringval != NULL) |
| 1487 | xmlFree(obj->stringval); |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 1488 | } else if (obj->type == XPATH_XSLT_TREE) { |
| 1489 | if (obj->nodesetval != NULL) |
Daniel Veillard | 8a7642f | 2001-01-22 10:45:16 +0000 | [diff] [blame] | 1490 | xmlXPathFreeValueTree(obj->nodesetval); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1493 | #ifdef DEBUG |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1494 | memset(obj, 0xB , (size_t) sizeof(xmlXPathObject)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1495 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1496 | xmlFree(obj); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | /************************************************************************ |
| 1500 | * * |
| 1501 | * Routines to handle XPath contexts * |
| 1502 | * * |
| 1503 | ************************************************************************/ |
| 1504 | |
| 1505 | /** |
| 1506 | * xmlXPathNewContext: |
| 1507 | * @doc: the XML document |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1508 | * |
| 1509 | * Create a new xmlXPathContext |
| 1510 | * |
| 1511 | * Returns the xmlXPathContext just allocated. |
| 1512 | */ |
| 1513 | xmlXPathContextPtr |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1514 | xmlXPathNewContext(xmlDocPtr doc) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1515 | xmlXPathContextPtr ret; |
| 1516 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1517 | ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1518 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1519 | xmlGenericError(xmlGenericErrorContext, |
| 1520 | "xmlXPathNewContext: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1521 | return(NULL); |
| 1522 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1523 | memset(ret, 0 , (size_t) sizeof(xmlXPathContext)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1524 | ret->doc = doc; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 1525 | ret->node = NULL; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1526 | |
Daniel Veillard | 126f279 | 2000-10-24 17:10:12 +0000 | [diff] [blame] | 1527 | ret->varHash = NULL; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1528 | |
| 1529 | ret->nb_types = 0; |
| 1530 | ret->max_types = 0; |
| 1531 | ret->types = NULL; |
| 1532 | |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 1533 | ret->funcHash = xmlHashCreate(0); |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1534 | |
| 1535 | ret->nb_axis = 0; |
| 1536 | ret->max_axis = 0; |
| 1537 | ret->axis = NULL; |
| 1538 | |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 1539 | ret->nsHash = NULL; |
Daniel Veillard | c08a2c6 | 1999-09-08 21:35:25 +0000 | [diff] [blame] | 1540 | ret->user = NULL; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 1541 | |
| 1542 | ret->contextSize = -1; |
| 1543 | ret->proximityPosition = -1; |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 1544 | |
| 1545 | xmlXPathRegisterAllFunctions(ret); |
| 1546 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1547 | return(ret); |
| 1548 | } |
| 1549 | |
| 1550 | /** |
| 1551 | * xmlXPathFreeContext: |
| 1552 | * @ctxt: the context to free |
| 1553 | * |
| 1554 | * Free up an xmlXPathContext |
| 1555 | */ |
| 1556 | void |
| 1557 | xmlXPathFreeContext(xmlXPathContextPtr ctxt) { |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 1558 | xmlXPathRegisteredNsCleanup(ctxt); |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 1559 | xmlXPathRegisteredFuncsCleanup(ctxt); |
| 1560 | xmlXPathRegisteredVariablesCleanup(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1561 | #ifdef DEBUG |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1562 | memset(ctxt, 0xB , (size_t) sizeof(xmlXPathContext)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1563 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1564 | xmlFree(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | /************************************************************************ |
| 1568 | * * |
| 1569 | * Routines to handle XPath parser contexts * |
| 1570 | * * |
| 1571 | ************************************************************************/ |
| 1572 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 1573 | #define CHECK_CTXT(ctxt) \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1574 | if (ctxt == NULL) { \ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1575 | xmlGenericError(xmlGenericErrorContext, \ |
| 1576 | "%s:%d Internal error: ctxt == NULL\n", \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1577 | __FILE__, __LINE__); \ |
| 1578 | } \ |
| 1579 | |
| 1580 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 1581 | #define CHECK_CONTEXT(ctxt) \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1582 | if (ctxt == NULL) { \ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1583 | xmlGenericError(xmlGenericErrorContext, \ |
| 1584 | "%s:%d Internal error: no context\n", \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1585 | __FILE__, __LINE__); \ |
| 1586 | } \ |
Daniel Veillard | df7ef2a | 2000-10-25 10:11:55 +0000 | [diff] [blame] | 1587 | else if (ctxt->doc == NULL) { \ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1588 | xmlGenericError(xmlGenericErrorContext, \ |
| 1589 | "%s:%d Internal error: no document\n", \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1590 | __FILE__, __LINE__); \ |
| 1591 | } \ |
Daniel Veillard | df7ef2a | 2000-10-25 10:11:55 +0000 | [diff] [blame] | 1592 | else if (ctxt->doc->children == NULL) { \ |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1593 | xmlGenericError(xmlGenericErrorContext, \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1594 | "%s:%d Internal error: document without root\n", \ |
| 1595 | __FILE__, __LINE__); \ |
| 1596 | } \ |
| 1597 | |
| 1598 | |
| 1599 | /** |
| 1600 | * xmlXPathNewParserContext: |
| 1601 | * @str: the XPath expression |
| 1602 | * @ctxt: the XPath context |
| 1603 | * |
| 1604 | * Create a new xmlXPathParserContext |
| 1605 | * |
| 1606 | * Returns the xmlXPathParserContext just allocated. |
| 1607 | */ |
| 1608 | xmlXPathParserContextPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1609 | xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1610 | xmlXPathParserContextPtr ret; |
| 1611 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1612 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1613 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1614 | xmlGenericError(xmlGenericErrorContext, |
| 1615 | "xmlXPathNewParserContext: out of memory\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1616 | return(NULL); |
| 1617 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1618 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1619 | ret->cur = ret->base = str; |
| 1620 | ret->context = ctxt; |
| 1621 | |
| 1622 | /* Allocate the value stack */ |
| 1623 | ret->valueTab = (xmlXPathObjectPtr *) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1624 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1625 | ret->valueNr = 0; |
| 1626 | ret->valueMax = 10; |
| 1627 | ret->value = NULL; |
| 1628 | return(ret); |
| 1629 | } |
| 1630 | |
| 1631 | /** |
| 1632 | * xmlXPathFreeParserContext: |
| 1633 | * @ctxt: the context to free |
| 1634 | * |
| 1635 | * Free up an xmlXPathParserContext |
| 1636 | */ |
| 1637 | void |
| 1638 | xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) { |
| 1639 | if (ctxt->valueTab != NULL) { |
| 1640 | #ifdef DEBUG |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1641 | memset(ctxt->valueTab, 0xB , 10 * (size_t) sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1642 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1643 | xmlFree(ctxt->valueTab); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1644 | } |
| 1645 | #ifdef DEBUG |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1646 | memset(ctxt, 0xB , (size_t) sizeof(xmlXPathParserContext)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1647 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1648 | xmlFree(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | /************************************************************************ |
| 1652 | * * |
| 1653 | * The implicit core function library * |
| 1654 | * * |
| 1655 | ************************************************************************/ |
| 1656 | |
| 1657 | /* |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1658 | * Auto-pop and cast to a number |
| 1659 | */ |
| 1660 | void xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs); |
| 1661 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1662 | |
| 1663 | #define POP_FLOAT \ |
| 1664 | arg = valuePop(ctxt); \ |
| 1665 | if (arg == NULL) { \ |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 1666 | XP_ERROR(XPATH_INVALID_OPERAND); \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1667 | } \ |
| 1668 | if (arg->type != XPATH_NUMBER) { \ |
| 1669 | valuePush(ctxt, arg); \ |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1670 | xmlXPathNumberFunction(ctxt, 1); \ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1671 | arg = valuePop(ctxt); \ |
| 1672 | } |
| 1673 | |
| 1674 | /** |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 1675 | * xmlXPathCompareNodeSetFloat: |
| 1676 | * @ctxt: the XPath Parser context |
| 1677 | * @inf: less than (1) or greater than (0) |
| 1678 | * @strict: is the comparison strict |
| 1679 | * @arg: the node set |
| 1680 | * @f: the value |
| 1681 | * |
| 1682 | * Implement the compare operation between a nodeset and a number |
| 1683 | * @ns < @val (1, 1, ... |
| 1684 | * @ns <= @val (1, 0, ... |
| 1685 | * @ns > @val (0, 1, ... |
| 1686 | * @ns >= @val (0, 0, ... |
| 1687 | * |
| 1688 | * If one object to be compared is a node-set and the other is a number, |
| 1689 | * then the comparison will be true if and only if there is a node in the |
| 1690 | * node-set such that the result of performing the comparison on the number |
| 1691 | * to be compared and on the result of converting the string-value of that |
| 1692 | * node to a number using the number function is true. |
| 1693 | * |
| 1694 | * Returns 0 or 1 depending on the results of the test. |
| 1695 | */ |
| 1696 | int |
| 1697 | xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 1698 | xmlXPathObjectPtr arg, xmlXPathObjectPtr f) { |
| 1699 | int i, ret = 0; |
| 1700 | xmlNodeSetPtr ns; |
| 1701 | xmlChar *str2; |
| 1702 | |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1703 | if ((f == NULL) || (arg == NULL) || |
| 1704 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 1705 | xmlXPathFreeObject(arg); |
| 1706 | xmlXPathFreeObject(f); |
| 1707 | return(0); |
| 1708 | } |
| 1709 | ns = arg->nodesetval; |
| 1710 | for (i = 0;i < ns->nodeNr;i++) { |
| 1711 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 1712 | if (str2 != NULL) { |
| 1713 | valuePush(ctxt, |
| 1714 | xmlXPathNewString(str2)); |
| 1715 | xmlFree(str2); |
| 1716 | xmlXPathNumberFunction(ctxt, 1); |
| 1717 | valuePush(ctxt, xmlXPathObjectCopy(f)); |
| 1718 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 1719 | if (ret) |
| 1720 | break; |
| 1721 | } |
| 1722 | } |
| 1723 | xmlXPathFreeObject(arg); |
| 1724 | xmlXPathFreeObject(f); |
| 1725 | return(ret); |
| 1726 | } |
| 1727 | |
| 1728 | /** |
| 1729 | * xmlXPathCompareNodeSetString: |
| 1730 | * @ctxt: the XPath Parser context |
| 1731 | * @inf: less than (1) or greater than (0) |
| 1732 | * @strict: is the comparison strict |
| 1733 | * @arg: the node set |
| 1734 | * @s: the value |
| 1735 | * |
| 1736 | * Implement the compare operation between a nodeset and a string |
| 1737 | * @ns < @val (1, 1, ... |
| 1738 | * @ns <= @val (1, 0, ... |
| 1739 | * @ns > @val (0, 1, ... |
| 1740 | * @ns >= @val (0, 0, ... |
| 1741 | * |
| 1742 | * If one object to be compared is a node-set and the other is a string, |
| 1743 | * then the comparison will be true if and only if there is a node in |
| 1744 | * the node-set such that the result of performing the comparison on the |
| 1745 | * string-value of the node and the other string is true. |
| 1746 | * |
| 1747 | * Returns 0 or 1 depending on the results of the test. |
| 1748 | */ |
| 1749 | int |
| 1750 | xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 1751 | xmlXPathObjectPtr arg, xmlXPathObjectPtr s) { |
| 1752 | int i, ret = 0; |
| 1753 | xmlNodeSetPtr ns; |
| 1754 | xmlChar *str2; |
| 1755 | |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1756 | if ((s == NULL) || (arg == NULL) || |
| 1757 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 1758 | xmlXPathFreeObject(arg); |
| 1759 | xmlXPathFreeObject(s); |
| 1760 | return(0); |
| 1761 | } |
| 1762 | ns = arg->nodesetval; |
| 1763 | for (i = 0;i < ns->nodeNr;i++) { |
| 1764 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 1765 | if (str2 != NULL) { |
| 1766 | valuePush(ctxt, |
| 1767 | xmlXPathNewString(str2)); |
| 1768 | xmlFree(str2); |
| 1769 | valuePush(ctxt, xmlXPathObjectCopy(s)); |
| 1770 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 1771 | if (ret) |
| 1772 | break; |
| 1773 | } |
| 1774 | } |
| 1775 | xmlXPathFreeObject(arg); |
| 1776 | xmlXPathFreeObject(s); |
| 1777 | return(ret); |
| 1778 | } |
| 1779 | |
| 1780 | /** |
| 1781 | * xmlXPathCompareNodeSets: |
| 1782 | * @ctxt: the XPath Parser context |
| 1783 | * @op: less than (-1), equal (0) or greater than (1) |
| 1784 | * @strict: is the comparison strict |
| 1785 | * @ns1: the fist node set |
| 1786 | * @ns2: the second node set |
| 1787 | * |
| 1788 | * Implement the compare operation on nodesets: |
| 1789 | * |
| 1790 | * If both objects to be compared are node-sets, then the comparison will be true if |
| 1791 | * and only if there is a node in the first node-set and a node in the second node-set |
| 1792 | * such that the result of performing the comparison on the string-values of the two |
| 1793 | * nodes is true. |
| 1794 | */ |
| 1795 | int |
| 1796 | xmlXPathCompareNodeSets(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 1797 | xmlXPathObjectPtr ns1, xmlXPathObjectPtr ns2) { |
| 1798 | TODO |
| 1799 | return(0); |
| 1800 | } |
| 1801 | |
| 1802 | /** |
| 1803 | * xmlXPathCompareNodeSetValue: |
| 1804 | * @ctxt: the XPath Parser context |
| 1805 | * @inf: less than (1) or greater than (0) |
| 1806 | * @strict: is the comparison strict |
| 1807 | * @arg: the node set |
| 1808 | * @val: the value |
| 1809 | * |
| 1810 | * Implement the compare operation between a nodeset and a value |
| 1811 | * @ns < @val (1, 1, ... |
| 1812 | * @ns <= @val (1, 0, ... |
| 1813 | * @ns > @val (0, 1, ... |
| 1814 | * @ns >= @val (0, 0, ... |
| 1815 | * |
| 1816 | * If one object to be compared is a node-set and the other is a boolean, then the |
| 1817 | * comparison will be true if and only if the result of performing the comparison |
| 1818 | * on the boolean and on the result of converting the node-set to a boolean using |
| 1819 | * the boolean function is true. |
| 1820 | * |
| 1821 | * Returns 0 or 1 depending on the results of the test. |
| 1822 | */ |
| 1823 | int |
| 1824 | xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 1825 | xmlXPathObjectPtr arg, xmlXPathObjectPtr val) { |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1826 | if ((val == NULL) || (arg == NULL) || |
| 1827 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 1828 | return(0); |
| 1829 | |
| 1830 | switch(val->type) { |
| 1831 | case XPATH_NUMBER: |
| 1832 | return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val)); |
| 1833 | case XPATH_NODESET: |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1834 | case XPATH_XSLT_TREE: |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 1835 | return(xmlXPathCompareNodeSets(ctxt, inf, strict, arg, val)); |
| 1836 | case XPATH_STRING: |
| 1837 | return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val)); |
| 1838 | case XPATH_BOOLEAN: |
| 1839 | valuePush(ctxt, arg); |
| 1840 | xmlXPathBooleanFunction(ctxt, 1); |
| 1841 | valuePush(ctxt, val); |
| 1842 | return(xmlXPathCompareValues(ctxt, inf, strict)); |
| 1843 | default: |
| 1844 | TODO |
| 1845 | return(0); |
| 1846 | } |
| 1847 | return(0); |
| 1848 | } |
| 1849 | |
| 1850 | /** |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1851 | * xmlXPathEqualNodeSetString |
| 1852 | * @arg: the nodeset object argument |
| 1853 | * @str: the string to compare to. |
| 1854 | * |
| 1855 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 1856 | * If one object to be compared is a node-set and the other is a string, |
| 1857 | * then the comparison will be true if and only if there is a node in |
| 1858 | * the node-set such that the result of performing the comparison on the |
| 1859 | * string-value of the node and the other string is true. |
| 1860 | * |
| 1861 | * Returns 0 or 1 depending on the results of the test. |
| 1862 | */ |
| 1863 | int |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1864 | xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar *str) { |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1865 | int i; |
| 1866 | xmlNodeSetPtr ns; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1867 | xmlChar *str2; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1868 | |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1869 | if ((str == NULL) || (arg == NULL) || |
| 1870 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1871 | return(0); |
| 1872 | ns = arg->nodesetval; |
| 1873 | for (i = 0;i < ns->nodeNr;i++) { |
| 1874 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 1875 | if ((str2 != NULL) && (xmlStrEqual(str, str2))) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1876 | xmlFree(str2); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1877 | return(1); |
| 1878 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1879 | xmlFree(str2); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1880 | } |
| 1881 | return(0); |
| 1882 | } |
| 1883 | |
| 1884 | /** |
| 1885 | * xmlXPathEqualNodeSetFloat |
| 1886 | * @arg: the nodeset object argument |
| 1887 | * @f: the float to compare to |
| 1888 | * |
| 1889 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 1890 | * If one object to be compared is a node-set and the other is a number, |
| 1891 | * then the comparison will be true if and only if there is a node in |
| 1892 | * the node-set such that the result of performing the comparison on the |
| 1893 | * number to be compared and on the result of converting the string-value |
| 1894 | * of that node to a number using the number function is true. |
| 1895 | * |
| 1896 | * Returns 0 or 1 depending on the results of the test. |
| 1897 | */ |
| 1898 | int |
| 1899 | xmlXPathEqualNodeSetFloat(xmlXPathObjectPtr arg, float f) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1900 | char buf[100] = ""; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1901 | |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1902 | if ((arg == NULL) || |
| 1903 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1904 | return(0); |
| 1905 | |
| 1906 | if (isnan(f)) |
| 1907 | sprintf(buf, "NaN"); |
| 1908 | else if (isinf(f) > 0) |
| 1909 | sprintf(buf, "+Infinity"); |
| 1910 | else if (isinf(f) < 0) |
| 1911 | sprintf(buf, "-Infinity"); |
| 1912 | else |
| 1913 | sprintf(buf, "%0g", f); |
| 1914 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1915 | return(xmlXPathEqualNodeSetString(arg, BAD_CAST buf)); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | |
| 1919 | /** |
| 1920 | * xmlXPathEqualNodeSets |
| 1921 | * @arg1: first nodeset object argument |
| 1922 | * @arg2: second nodeset object argument |
| 1923 | * |
| 1924 | * Implement the equal operation on XPath nodesets: @arg1 == @arg2 |
| 1925 | * If both objects to be compared are node-sets, then the comparison |
| 1926 | * will be true if and only if there is a node in the first node-set and |
| 1927 | * a node in the second node-set such that the result of performing the |
| 1928 | * comparison on the string-values of the two nodes is true. |
| 1929 | * |
| 1930 | * (needless to say, this is a costly operation) |
| 1931 | * |
| 1932 | * Returns 0 or 1 depending on the results of the test. |
| 1933 | */ |
| 1934 | int |
| 1935 | xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
| 1936 | int i; |
| 1937 | xmlNodeSetPtr ns; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1938 | xmlChar *str; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1939 | |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1940 | if ((arg1 == NULL) || |
| 1941 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1942 | return(0); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 1943 | if ((arg2 == NULL) || |
| 1944 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1945 | return(0); |
| 1946 | |
| 1947 | ns = arg1->nodesetval; |
| 1948 | for (i = 0;i < ns->nodeNr;i++) { |
| 1949 | str = xmlNodeGetContent(ns->nodeTab[i]); |
| 1950 | if ((str != NULL) && (xmlXPathEqualNodeSetString(arg2, str))) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1951 | xmlFree(str); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1952 | return(1); |
| 1953 | } |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1954 | xmlFree(str); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1955 | } |
| 1956 | return(0); |
| 1957 | } |
| 1958 | |
| 1959 | /** |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1960 | * xmlXPathEqualValues: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1961 | * @ctxt: the XPath Parser context |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1962 | * |
| 1963 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 1964 | * |
| 1965 | * Returns 0 or 1 depending on the results of the test. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1966 | */ |
| 1967 | int |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1968 | xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) { |
| 1969 | xmlXPathObjectPtr arg1, arg2; |
| 1970 | int ret = 0; |
| 1971 | |
| 1972 | arg1 = valuePop(ctxt); |
| 1973 | if (arg1 == NULL) |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 1974 | XP_ERROR0(XPATH_INVALID_OPERAND); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1975 | |
| 1976 | arg2 = valuePop(ctxt); |
| 1977 | if (arg2 == NULL) { |
| 1978 | xmlXPathFreeObject(arg1); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 1979 | XP_ERROR0(XPATH_INVALID_OPERAND); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1982 | if (arg1 == arg2) { |
| 1983 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1984 | xmlGenericError(xmlGenericErrorContext, |
| 1985 | "Equal: by pointer\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1986 | #endif |
| 1987 | return(1); |
| 1988 | } |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1989 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1990 | switch (arg1->type) { |
| 1991 | case XPATH_UNDEFINED: |
| 1992 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1993 | xmlGenericError(xmlGenericErrorContext, |
| 1994 | "Equal: undefined\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1995 | #endif |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1996 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 1997 | case XPATH_XSLT_TREE: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 1998 | case XPATH_NODESET: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 1999 | switch (arg2->type) { |
| 2000 | case XPATH_UNDEFINED: |
| 2001 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2002 | xmlGenericError(xmlGenericErrorContext, |
| 2003 | "Equal: undefined\n"); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2004 | #endif |
| 2005 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 2006 | case XPATH_XSLT_TREE: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2007 | case XPATH_NODESET: |
| 2008 | ret = xmlXPathEqualNodeSets(arg1, arg2); |
| 2009 | break; |
| 2010 | case XPATH_BOOLEAN: |
| 2011 | if ((arg1->nodesetval == NULL) || |
| 2012 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 2013 | else |
| 2014 | ret = 1; |
| 2015 | ret = (ret == arg2->boolval); |
| 2016 | break; |
| 2017 | case XPATH_NUMBER: |
| 2018 | ret = xmlXPathEqualNodeSetFloat(arg1, arg2->floatval); |
| 2019 | break; |
| 2020 | case XPATH_STRING: |
| 2021 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval); |
| 2022 | break; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2023 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2024 | case XPATH_POINT: |
| 2025 | case XPATH_RANGE: |
| 2026 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2027 | TODO |
| 2028 | break; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2029 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2030 | break; |
| 2031 | case XPATH_BOOLEAN: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2032 | switch (arg2->type) { |
| 2033 | case XPATH_UNDEFINED: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 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; |
| 2039 | case XPATH_NODESET: |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 2040 | case XPATH_XSLT_TREE: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2041 | if ((arg2->nodesetval == NULL) || |
| 2042 | (arg2->nodesetval->nodeNr == 0)) ret = 0; |
| 2043 | else |
| 2044 | ret = 1; |
| 2045 | break; |
| 2046 | case XPATH_BOOLEAN: |
| 2047 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2048 | xmlGenericError(xmlGenericErrorContext, |
| 2049 | "Equal: %d boolean %d \n", |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2050 | arg1->boolval, arg2->boolval); |
| 2051 | #endif |
| 2052 | ret = (arg1->boolval == arg2->boolval); |
| 2053 | break; |
| 2054 | case XPATH_NUMBER: |
| 2055 | if (arg2->floatval) ret = 1; |
| 2056 | else ret = 0; |
| 2057 | ret = (arg1->boolval == ret); |
| 2058 | break; |
| 2059 | case XPATH_STRING: |
| 2060 | if ((arg2->stringval == NULL) || |
| 2061 | (arg2->stringval[0] == 0)) ret = 0; |
| 2062 | else |
| 2063 | ret = 1; |
| 2064 | ret = (arg1->boolval == ret); |
| 2065 | break; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2066 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2067 | case XPATH_POINT: |
| 2068 | case XPATH_RANGE: |
| 2069 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2070 | TODO |
| 2071 | break; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2072 | } |
| 2073 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2074 | case XPATH_NUMBER: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2075 | switch (arg2->type) { |
| 2076 | case XPATH_UNDEFINED: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2077 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2078 | xmlGenericError(xmlGenericErrorContext, |
| 2079 | "Equal: undefined\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2080 | #endif |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2081 | break; |
| 2082 | case XPATH_NODESET: |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 2083 | case XPATH_XSLT_TREE: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2084 | ret = xmlXPathEqualNodeSetFloat(arg2, arg1->floatval); |
| 2085 | break; |
| 2086 | case XPATH_BOOLEAN: |
| 2087 | if (arg1->floatval) ret = 1; |
| 2088 | else ret = 0; |
| 2089 | ret = (arg2->boolval == ret); |
| 2090 | break; |
| 2091 | case XPATH_STRING: |
| 2092 | valuePush(ctxt, arg2); |
| 2093 | xmlXPathNumberFunction(ctxt, 1); |
| 2094 | arg2 = valuePop(ctxt); |
| 2095 | /* no break on purpose */ |
| 2096 | case XPATH_NUMBER: |
| 2097 | ret = (arg1->floatval == arg2->floatval); |
| 2098 | break; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2099 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2100 | case XPATH_POINT: |
| 2101 | case XPATH_RANGE: |
| 2102 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2103 | TODO |
| 2104 | break; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2105 | } |
| 2106 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2107 | case XPATH_STRING: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2108 | switch (arg2->type) { |
| 2109 | case XPATH_UNDEFINED: |
| 2110 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2111 | xmlGenericError(xmlGenericErrorContext, |
| 2112 | "Equal: undefined\n"); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2113 | #endif |
| 2114 | break; |
| 2115 | case XPATH_NODESET: |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 2116 | case XPATH_XSLT_TREE: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2117 | ret = xmlXPathEqualNodeSetString(arg2, arg1->stringval); |
| 2118 | break; |
| 2119 | case XPATH_BOOLEAN: |
| 2120 | if ((arg1->stringval == NULL) || |
| 2121 | (arg1->stringval[0] == 0)) ret = 0; |
| 2122 | else |
| 2123 | ret = 1; |
| 2124 | ret = (arg2->boolval == ret); |
| 2125 | break; |
| 2126 | case XPATH_STRING: |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 2127 | ret = xmlStrEqual(arg1->stringval, arg2->stringval); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2128 | break; |
| 2129 | case XPATH_NUMBER: |
| 2130 | valuePush(ctxt, arg1); |
| 2131 | xmlXPathNumberFunction(ctxt, 1); |
| 2132 | arg1 = valuePop(ctxt); |
| 2133 | ret = (arg1->floatval == arg2->floatval); |
| 2134 | break; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2135 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2136 | case XPATH_POINT: |
| 2137 | case XPATH_RANGE: |
| 2138 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2139 | TODO |
| 2140 | break; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2141 | } |
| 2142 | break; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2143 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2144 | case XPATH_POINT: |
| 2145 | case XPATH_RANGE: |
| 2146 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2147 | TODO |
| 2148 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2149 | } |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2150 | xmlXPathFreeObject(arg1); |
| 2151 | xmlXPathFreeObject(arg2); |
| 2152 | return(ret); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2155 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2156 | /** |
| 2157 | * xmlXPathCompareValues: |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2158 | * @ctxt: the XPath Parser context |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2159 | * @inf: less than (1) or greater than (0) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2160 | * @strict: is the comparison strict |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2161 | * |
| 2162 | * Implement the compare operation on XPath objects: |
| 2163 | * @arg1 < @arg2 (1, 1, ... |
| 2164 | * @arg1 <= @arg2 (1, 0, ... |
| 2165 | * @arg1 > @arg2 (0, 1, ... |
| 2166 | * @arg1 >= @arg2 (0, 0, ... |
| 2167 | * |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2168 | * When neither object to be compared is a node-set and the operator is |
| 2169 | * <=, <, >=, >, then the objects are compared by converted both objects |
| 2170 | * to numbers and comparing the numbers according to IEEE 754. The < |
| 2171 | * comparison will be true if and only if the first number is less than the |
| 2172 | * second number. The <= comparison will be true if and only if the first |
| 2173 | * number is less than or equal to the second number. The > comparison |
| 2174 | * will be true if and only if the first number is greater than the second |
| 2175 | * number. The >= comparison will be true if and only if the first number |
| 2176 | * is greater than or equal to the second number. |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2177 | * |
| 2178 | * Returns 1 if the comparaison succeeded, 0 if it failed |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2179 | */ |
| 2180 | int |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2181 | xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) { |
| 2182 | int ret = 0; |
| 2183 | xmlXPathObjectPtr arg1, arg2; |
| 2184 | |
| 2185 | arg2 = valuePop(ctxt); |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2186 | if (arg2 == NULL) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 2187 | XP_ERROR0(XPATH_INVALID_OPERAND); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | arg1 = valuePop(ctxt); |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2191 | if (arg1 == NULL) { |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2192 | xmlXPathFreeObject(arg2); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 2193 | XP_ERROR0(XPATH_INVALID_OPERAND); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 2196 | if ((arg2->type == XPATH_NODESET) || (arg1->type == XPATH_NODESET)) { |
| 2197 | if ((arg2->type == XPATH_NODESET) && (arg1->type == XPATH_NODESET)) { |
| 2198 | ret = xmlXPathCompareNodeSets(ctxt, inf, strict, arg1, arg2); |
| 2199 | } else { |
| 2200 | if (arg1->type == XPATH_NODESET) { |
| 2201 | ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict, arg1, arg2); |
| 2202 | } else { |
| 2203 | ret = xmlXPathCompareNodeSetValue(ctxt, !inf, !strict, arg2, arg2); |
| 2204 | } |
| 2205 | } |
| 2206 | return(ret); |
| 2207 | } |
| 2208 | |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2209 | if (arg1->type != XPATH_NUMBER) { |
| 2210 | valuePush(ctxt, arg1); |
| 2211 | xmlXPathNumberFunction(ctxt, 1); |
| 2212 | arg1 = valuePop(ctxt); |
| 2213 | } |
| 2214 | if (arg1->type != XPATH_NUMBER) { |
| 2215 | xmlXPathFreeObject(arg1); |
| 2216 | xmlXPathFreeObject(arg2); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 2217 | XP_ERROR0(XPATH_INVALID_OPERAND); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2218 | } |
| 2219 | if (arg2->type != XPATH_NUMBER) { |
| 2220 | valuePush(ctxt, arg2); |
| 2221 | xmlXPathNumberFunction(ctxt, 1); |
| 2222 | arg2 = valuePop(ctxt); |
| 2223 | } |
| 2224 | if (arg2->type != XPATH_NUMBER) { |
| 2225 | xmlXPathFreeObject(arg1); |
| 2226 | xmlXPathFreeObject(arg2); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 2227 | XP_ERROR0(XPATH_INVALID_OPERAND); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2228 | } |
| 2229 | /* |
| 2230 | * Add tests for infinity and nan |
| 2231 | * => feedback on 3.4 for Inf and NaN |
| 2232 | */ |
| 2233 | if (inf && strict) |
| 2234 | ret = (arg1->floatval < arg2->floatval); |
| 2235 | else if (inf && !strict) |
| 2236 | ret = (arg1->floatval <= arg2->floatval); |
| 2237 | else if (!inf && strict) |
| 2238 | ret = (arg1->floatval > arg2->floatval); |
| 2239 | else if (!inf && !strict) |
| 2240 | ret = (arg1->floatval >= arg2->floatval); |
| 2241 | xmlXPathFreeObject(arg1); |
| 2242 | xmlXPathFreeObject(arg2); |
| 2243 | return(ret); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | /** |
| 2247 | * xmlXPathValueFlipSign: |
| 2248 | * @ctxt: the XPath Parser context |
| 2249 | * |
| 2250 | * Implement the unary - operation on an XPath object |
| 2251 | * The numeric operators convert their operands to numbers as if |
| 2252 | * by calling the number function. |
| 2253 | */ |
| 2254 | void |
| 2255 | xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) { |
| 2256 | xmlXPathObjectPtr arg; |
| 2257 | |
| 2258 | POP_FLOAT |
| 2259 | arg->floatval = -arg->floatval; |
| 2260 | valuePush(ctxt, arg); |
| 2261 | } |
| 2262 | |
| 2263 | /** |
| 2264 | * xmlXPathAddValues: |
| 2265 | * @ctxt: the XPath Parser context |
| 2266 | * |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2267 | * Implement the add operation on XPath objects: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2268 | * The numeric operators convert their operands to numbers as if |
| 2269 | * by calling the number function. |
| 2270 | */ |
| 2271 | void |
| 2272 | xmlXPathAddValues(xmlXPathParserContextPtr ctxt) { |
| 2273 | xmlXPathObjectPtr arg; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2274 | double val; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2275 | |
| 2276 | POP_FLOAT |
| 2277 | val = arg->floatval; |
| 2278 | xmlXPathFreeObject(arg); |
| 2279 | |
| 2280 | POP_FLOAT |
| 2281 | arg->floatval += val; |
| 2282 | valuePush(ctxt, arg); |
| 2283 | } |
| 2284 | |
| 2285 | /** |
| 2286 | * xmlXPathSubValues: |
| 2287 | * @ctxt: the XPath Parser context |
| 2288 | * |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2289 | * Implement the substraction operation on XPath objects: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2290 | * The numeric operators convert their operands to numbers as if |
| 2291 | * by calling the number function. |
| 2292 | */ |
| 2293 | void |
| 2294 | xmlXPathSubValues(xmlXPathParserContextPtr ctxt) { |
| 2295 | xmlXPathObjectPtr arg; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2296 | double val; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2297 | |
| 2298 | POP_FLOAT |
| 2299 | val = arg->floatval; |
| 2300 | xmlXPathFreeObject(arg); |
| 2301 | |
| 2302 | POP_FLOAT |
| 2303 | arg->floatval -= val; |
| 2304 | valuePush(ctxt, arg); |
| 2305 | } |
| 2306 | |
| 2307 | /** |
| 2308 | * xmlXPathMultValues: |
| 2309 | * @ctxt: the XPath Parser context |
| 2310 | * |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2311 | * Implement the multiply operation on XPath objects: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2312 | * The numeric operators convert their operands to numbers as if |
| 2313 | * by calling the number function. |
| 2314 | */ |
| 2315 | void |
| 2316 | xmlXPathMultValues(xmlXPathParserContextPtr ctxt) { |
| 2317 | xmlXPathObjectPtr arg; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2318 | double val; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2319 | |
| 2320 | POP_FLOAT |
| 2321 | val = arg->floatval; |
| 2322 | xmlXPathFreeObject(arg); |
| 2323 | |
| 2324 | POP_FLOAT |
| 2325 | arg->floatval *= val; |
| 2326 | valuePush(ctxt, arg); |
| 2327 | } |
| 2328 | |
| 2329 | /** |
| 2330 | * xmlXPathDivValues: |
| 2331 | * @ctxt: the XPath Parser context |
| 2332 | * |
Daniel Veillard | 2b325a0 | 2001-01-31 20:46:31 +0000 | [diff] [blame] | 2333 | * Implement the div operation on XPath objects @arg1 / @arg2: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2334 | * The numeric operators convert their operands to numbers as if |
| 2335 | * by calling the number function. |
| 2336 | */ |
| 2337 | void |
| 2338 | xmlXPathDivValues(xmlXPathParserContextPtr ctxt) { |
| 2339 | xmlXPathObjectPtr arg; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 2340 | double val; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2341 | |
| 2342 | POP_FLOAT |
| 2343 | val = arg->floatval; |
| 2344 | xmlXPathFreeObject(arg); |
| 2345 | |
| 2346 | POP_FLOAT |
| 2347 | arg->floatval /= val; |
| 2348 | valuePush(ctxt, arg); |
| 2349 | } |
| 2350 | |
| 2351 | /** |
| 2352 | * xmlXPathModValues: |
| 2353 | * @ctxt: the XPath Parser context |
| 2354 | * |
Daniel Veillard | 2b325a0 | 2001-01-31 20:46:31 +0000 | [diff] [blame] | 2355 | * Implement the mod operation on XPath objects: @arg1 / @arg2 |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2356 | * The numeric operators convert their operands to numbers as if |
| 2357 | * by calling the number function. |
| 2358 | */ |
| 2359 | void |
| 2360 | xmlXPathModValues(xmlXPathParserContextPtr ctxt) { |
| 2361 | xmlXPathObjectPtr arg; |
Daniel Veillard | 2b325a0 | 2001-01-31 20:46:31 +0000 | [diff] [blame] | 2362 | int arg1, arg2; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2363 | |
| 2364 | POP_FLOAT |
Daniel Veillard | 2b325a0 | 2001-01-31 20:46:31 +0000 | [diff] [blame] | 2365 | arg2 = (int) arg->floatval; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2366 | xmlXPathFreeObject(arg); |
| 2367 | |
| 2368 | POP_FLOAT |
Daniel Veillard | 2b325a0 | 2001-01-31 20:46:31 +0000 | [diff] [blame] | 2369 | arg1 = (int) arg->floatval; |
| 2370 | arg->floatval = arg1 % arg2; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2371 | valuePush(ctxt, arg); |
| 2372 | } |
| 2373 | |
| 2374 | /************************************************************************ |
| 2375 | * * |
| 2376 | * The traversal functions * |
| 2377 | * * |
| 2378 | ************************************************************************/ |
| 2379 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2380 | typedef enum { |
| 2381 | AXIS_ANCESTOR = 1, |
| 2382 | AXIS_ANCESTOR_OR_SELF, |
| 2383 | AXIS_ATTRIBUTE, |
| 2384 | AXIS_CHILD, |
| 2385 | AXIS_DESCENDANT, |
| 2386 | AXIS_DESCENDANT_OR_SELF, |
| 2387 | AXIS_FOLLOWING, |
| 2388 | AXIS_FOLLOWING_SIBLING, |
| 2389 | AXIS_NAMESPACE, |
| 2390 | AXIS_PARENT, |
| 2391 | AXIS_PRECEDING, |
| 2392 | AXIS_PRECEDING_SIBLING, |
| 2393 | AXIS_SELF |
| 2394 | } xmlXPathAxisVal; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2395 | |
| 2396 | /* |
| 2397 | * A traversal function enumerates nodes along an axis. |
| 2398 | * Initially it must be called with NULL, and it indicates |
| 2399 | * termination on the axis by returning NULL. |
| 2400 | */ |
| 2401 | typedef xmlNodePtr (*xmlXPathTraversalFunction) |
| 2402 | (xmlXPathParserContextPtr ctxt, xmlNodePtr cur); |
| 2403 | |
| 2404 | /** |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2405 | * xmlXPathNextSelf: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2406 | * @ctxt: the XPath Parser context |
| 2407 | * @cur: the current node in the traversal |
| 2408 | * |
| 2409 | * Traversal function for the "self" direction |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2410 | * The self axis contains just the context node itself |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2411 | * |
| 2412 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2413 | */ |
| 2414 | xmlNodePtr |
| 2415 | xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 2416 | if (cur == NULL) |
| 2417 | return(ctxt->context->node); |
| 2418 | return(NULL); |
| 2419 | } |
| 2420 | |
| 2421 | /** |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2422 | * xmlXPathNextChild: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2423 | * @ctxt: the XPath Parser context |
| 2424 | * @cur: the current node in the traversal |
| 2425 | * |
| 2426 | * Traversal function for the "child" direction |
| 2427 | * 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] | 2428 | * |
| 2429 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2430 | */ |
| 2431 | xmlNodePtr |
| 2432 | xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2433 | if (cur == NULL) { |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2434 | if (ctxt->context->node == NULL) return(NULL); |
| 2435 | switch (ctxt->context->node->type) { |
| 2436 | case XML_ELEMENT_NODE: |
| 2437 | case XML_TEXT_NODE: |
| 2438 | case XML_CDATA_SECTION_NODE: |
| 2439 | case XML_ENTITY_REF_NODE: |
| 2440 | case XML_ENTITY_NODE: |
| 2441 | case XML_PI_NODE: |
| 2442 | case XML_COMMENT_NODE: |
| 2443 | case XML_NOTATION_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2444 | case XML_DTD_NODE: |
| 2445 | return(ctxt->context->node->children); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2446 | case XML_DOCUMENT_NODE: |
| 2447 | case XML_DOCUMENT_TYPE_NODE: |
| 2448 | case XML_DOCUMENT_FRAG_NODE: |
| 2449 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2450 | #ifdef LIBXML_SGML_ENABLED |
| 2451 | case XML_SGML_DOCUMENT_NODE: |
| 2452 | #endif |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2453 | return(((xmlDocPtr) ctxt->context->node)->children); |
| 2454 | case XML_ELEMENT_DECL: |
| 2455 | case XML_ATTRIBUTE_DECL: |
| 2456 | case XML_ENTITY_DECL: |
| 2457 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2458 | case XML_NAMESPACE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2459 | case XML_XINCLUDE_START: |
| 2460 | case XML_XINCLUDE_END: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2461 | return(NULL); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2462 | } |
| 2463 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2464 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 2465 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 2466 | (cur->type == XML_HTML_DOCUMENT_NODE)) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2467 | return(NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2468 | return(cur->next); |
| 2469 | } |
| 2470 | |
| 2471 | /** |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2472 | * xmlXPathNextDescendant: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2473 | * @ctxt: the XPath Parser context |
| 2474 | * @cur: the current node in the traversal |
| 2475 | * |
| 2476 | * Traversal function for the "descendant" direction |
| 2477 | * the descendant axis contains the descendants of the context node in document |
| 2478 | * 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] | 2479 | * |
| 2480 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2481 | */ |
| 2482 | xmlNodePtr |
| 2483 | xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2484 | if (cur == NULL) { |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2485 | if (ctxt->context->node == NULL) |
| 2486 | return(NULL); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2487 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 2488 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2489 | return(NULL); |
| 2490 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2491 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2492 | return(ctxt->context->doc->children); |
| 2493 | return(ctxt->context->node->children); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2494 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2495 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 2496 | if (cur->children != NULL) |
| 2497 | { |
| 2498 | if (cur->children->type != XML_ENTITY_DECL) |
| 2499 | return(cur->children); |
| 2500 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2501 | if (cur->next != NULL) return(cur->next); |
| 2502 | |
| 2503 | do { |
| 2504 | cur = cur->parent; |
| 2505 | if (cur == NULL) return(NULL); |
| 2506 | if (cur == ctxt->context->node) return(NULL); |
| 2507 | if (cur->next != NULL) { |
| 2508 | cur = cur->next; |
| 2509 | return(cur); |
| 2510 | } |
| 2511 | } while (cur != NULL); |
| 2512 | return(cur); |
| 2513 | } |
| 2514 | |
| 2515 | /** |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2516 | * xmlXPathNextDescendantOrSelf: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2517 | * @ctxt: the XPath Parser context |
| 2518 | * @cur: the current node in the traversal |
| 2519 | * |
| 2520 | * Traversal function for the "descendant-or-self" direction |
| 2521 | * the descendant-or-self axis contains the context node and the descendants |
| 2522 | * of the context node in document order; thus the context node is the first |
| 2523 | * node on the axis, and the first child of the context node is the second node |
| 2524 | * on the axis |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2525 | * |
| 2526 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2527 | */ |
| 2528 | xmlNodePtr |
| 2529 | xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2530 | if (cur == NULL) { |
| 2531 | if (ctxt->context->node == NULL) |
| 2532 | return(NULL); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2533 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 2534 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2535 | return(NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2536 | return(ctxt->context->node); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2537 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2538 | |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2539 | return(xmlXPathNextDescendant(ctxt, cur)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | /** |
| 2543 | * xmlXPathNextParent: |
| 2544 | * @ctxt: the XPath Parser context |
| 2545 | * @cur: the current node in the traversal |
| 2546 | * |
| 2547 | * Traversal function for the "parent" direction |
| 2548 | * 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] | 2549 | * |
| 2550 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2551 | */ |
| 2552 | xmlNodePtr |
| 2553 | xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 2554 | /* |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2555 | * the parent of an attribute or namespace node is the element |
| 2556 | * to which the attribute or namespace node is attached |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2557 | * Namespace handling !!! |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2558 | */ |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2559 | if (cur == NULL) { |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2560 | if (ctxt->context->node == NULL) return(NULL); |
| 2561 | switch (ctxt->context->node->type) { |
| 2562 | case XML_ELEMENT_NODE: |
| 2563 | case XML_TEXT_NODE: |
| 2564 | case XML_CDATA_SECTION_NODE: |
| 2565 | case XML_ENTITY_REF_NODE: |
| 2566 | case XML_ENTITY_NODE: |
| 2567 | case XML_PI_NODE: |
| 2568 | case XML_COMMENT_NODE: |
| 2569 | case XML_NOTATION_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2570 | case XML_DTD_NODE: |
| 2571 | case XML_ELEMENT_DECL: |
| 2572 | case XML_ATTRIBUTE_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2573 | case XML_XINCLUDE_START: |
| 2574 | case XML_XINCLUDE_END: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2575 | case XML_ENTITY_DECL: |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2576 | if (ctxt->context->node->parent == NULL) |
| 2577 | return((xmlNodePtr) ctxt->context->doc); |
| 2578 | return(ctxt->context->node->parent); |
| 2579 | case XML_ATTRIBUTE_NODE: { |
| 2580 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 2581 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2582 | return(att->parent); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2583 | } |
| 2584 | case XML_DOCUMENT_NODE: |
| 2585 | case XML_DOCUMENT_TYPE_NODE: |
| 2586 | case XML_DOCUMENT_FRAG_NODE: |
| 2587 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2588 | #ifdef LIBXML_SGML_ENABLED |
| 2589 | case XML_SGML_DOCUMENT_NODE: |
| 2590 | #endif |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2591 | return(NULL); |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2592 | case XML_NAMESPACE_DECL: |
| 2593 | /* |
| 2594 | * TODO !!! may require extending struct _xmlNs with |
| 2595 | * parent field |
| 2596 | * C.f. Infoset case... |
| 2597 | */ |
| 2598 | return(NULL); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2599 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2600 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2601 | return(NULL); |
| 2602 | } |
| 2603 | |
| 2604 | /** |
| 2605 | * xmlXPathNextAncestor: |
| 2606 | * @ctxt: the XPath Parser context |
| 2607 | * @cur: the current node in the traversal |
| 2608 | * |
| 2609 | * Traversal function for the "ancestor" direction |
| 2610 | * the ancestor axis contains the ancestors of the context node; the ancestors |
| 2611 | * of the context node consist of the parent of context node and the parent's |
| 2612 | * parent and so on; the nodes are ordered in reverse document order; thus the |
| 2613 | * parent is the first node on the axis, and the parent's parent is the second |
| 2614 | * node on the axis |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2615 | * |
| 2616 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2617 | */ |
| 2618 | xmlNodePtr |
| 2619 | xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 2620 | /* |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2621 | * the parent of an attribute or namespace node is the element |
| 2622 | * to which the attribute or namespace node is attached |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2623 | * !!!!!!!!!!!!! |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2624 | */ |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2625 | if (cur == NULL) { |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2626 | if (ctxt->context->node == NULL) return(NULL); |
| 2627 | switch (ctxt->context->node->type) { |
| 2628 | case XML_ELEMENT_NODE: |
| 2629 | case XML_TEXT_NODE: |
| 2630 | case XML_CDATA_SECTION_NODE: |
| 2631 | case XML_ENTITY_REF_NODE: |
| 2632 | case XML_ENTITY_NODE: |
| 2633 | case XML_PI_NODE: |
| 2634 | case XML_COMMENT_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2635 | case XML_DTD_NODE: |
| 2636 | case XML_ELEMENT_DECL: |
| 2637 | case XML_ATTRIBUTE_DECL: |
| 2638 | case XML_ENTITY_DECL: |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2639 | case XML_NOTATION_NODE: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2640 | case XML_XINCLUDE_START: |
| 2641 | case XML_XINCLUDE_END: |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2642 | if (ctxt->context->node->parent == NULL) |
| 2643 | return((xmlNodePtr) ctxt->context->doc); |
| 2644 | return(ctxt->context->node->parent); |
| 2645 | case XML_ATTRIBUTE_NODE: { |
| 2646 | xmlAttrPtr cur = (xmlAttrPtr) ctxt->context->node; |
| 2647 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2648 | return(cur->parent); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2649 | } |
| 2650 | case XML_DOCUMENT_NODE: |
| 2651 | case XML_DOCUMENT_TYPE_NODE: |
| 2652 | case XML_DOCUMENT_FRAG_NODE: |
| 2653 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2654 | #ifdef LIBXML_SGML_ENABLED |
| 2655 | case XML_SGML_DOCUMENT_NODE: |
| 2656 | #endif |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2657 | return(NULL); |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2658 | case XML_NAMESPACE_DECL: |
| 2659 | /* |
| 2660 | * TODO !!! may require extending struct _xmlNs with |
| 2661 | * parent field |
| 2662 | * C.f. Infoset case... |
| 2663 | */ |
| 2664 | return(NULL); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2665 | } |
| 2666 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2667 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2668 | if (cur == ctxt->context->doc->children) |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2669 | return((xmlNodePtr) ctxt->context->doc); |
| 2670 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 2671 | return(NULL); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2672 | switch (cur->type) { |
| 2673 | case XML_ELEMENT_NODE: |
| 2674 | case XML_TEXT_NODE: |
| 2675 | case XML_CDATA_SECTION_NODE: |
| 2676 | case XML_ENTITY_REF_NODE: |
| 2677 | case XML_ENTITY_NODE: |
| 2678 | case XML_PI_NODE: |
| 2679 | case XML_COMMENT_NODE: |
| 2680 | case XML_NOTATION_NODE: |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2681 | case XML_DTD_NODE: |
| 2682 | case XML_ELEMENT_DECL: |
| 2683 | case XML_ATTRIBUTE_DECL: |
| 2684 | case XML_ENTITY_DECL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2685 | case XML_XINCLUDE_START: |
| 2686 | case XML_XINCLUDE_END: |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2687 | return(cur->parent); |
| 2688 | case XML_ATTRIBUTE_NODE: { |
| 2689 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 2690 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 2691 | return(att->parent); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2692 | } |
| 2693 | case XML_DOCUMENT_NODE: |
| 2694 | case XML_DOCUMENT_TYPE_NODE: |
| 2695 | case XML_DOCUMENT_FRAG_NODE: |
| 2696 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | 04698d9 | 2000-09-17 16:00:22 +0000 | [diff] [blame] | 2697 | #ifdef LIBXML_SGML_ENABLED |
| 2698 | case XML_SGML_DOCUMENT_NODE: |
| 2699 | #endif |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2700 | return(NULL); |
Daniel Veillard | a4964b7 | 2000-10-31 18:23:44 +0000 | [diff] [blame] | 2701 | case XML_NAMESPACE_DECL: |
| 2702 | /* |
| 2703 | * TODO !!! may require extending struct _xmlNs with |
| 2704 | * parent field |
| 2705 | * C.f. Infoset case... |
| 2706 | */ |
| 2707 | return(NULL); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2708 | } |
| 2709 | return(NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | /** |
| 2713 | * xmlXPathNextAncestorOrSelf: |
| 2714 | * @ctxt: the XPath Parser context |
| 2715 | * @cur: the current node in the traversal |
| 2716 | * |
| 2717 | * Traversal function for the "ancestor-or-self" direction |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2718 | * he ancestor-or-self axis contains the context node and ancestors of |
| 2719 | * the context node in reverse document order; thus the context node is |
| 2720 | * the first node on the axis, and the context node's parent the second; |
| 2721 | * parent here is defined the same as with the parent axis. |
| 2722 | * |
| 2723 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2724 | */ |
| 2725 | xmlNodePtr |
| 2726 | xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2727 | if (cur == NULL) |
| 2728 | return(ctxt->context->node); |
Daniel Veillard | e3d88ef | 2000-01-24 13:55:06 +0000 | [diff] [blame] | 2729 | return(xmlXPathNextAncestor(ctxt, cur)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | /** |
| 2733 | * xmlXPathNextFollowingSibling: |
| 2734 | * @ctxt: the XPath Parser context |
| 2735 | * @cur: the current node in the traversal |
| 2736 | * |
| 2737 | * Traversal function for the "following-sibling" direction |
| 2738 | * The following-sibling axis contains the following siblings of the context |
| 2739 | * node in document order. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2740 | * |
| 2741 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2742 | */ |
| 2743 | xmlNodePtr |
| 2744 | xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2745 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 2746 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 2747 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2748 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 2749 | return(NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2750 | if (cur == NULL) |
| 2751 | return(ctxt->context->node->next); |
| 2752 | return(cur->next); |
| 2753 | } |
| 2754 | |
| 2755 | /** |
| 2756 | * xmlXPathNextPrecedingSibling: |
| 2757 | * @ctxt: the XPath Parser context |
| 2758 | * @cur: the current node in the traversal |
| 2759 | * |
| 2760 | * Traversal function for the "preceding-sibling" direction |
| 2761 | * The preceding-sibling axis contains the preceding siblings of the context |
| 2762 | * node in reverse document order; the first preceding sibling is first on the |
| 2763 | * 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] | 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 | xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2769 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 2770 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 2771 | return(NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 2772 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 2773 | return(NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2774 | if (cur == NULL) |
| 2775 | return(ctxt->context->node->prev); |
| 2776 | return(cur->prev); |
| 2777 | } |
| 2778 | |
| 2779 | /** |
| 2780 | * xmlXPathNextFollowing: |
| 2781 | * @ctxt: the XPath Parser context |
| 2782 | * @cur: the current node in the traversal |
| 2783 | * |
| 2784 | * Traversal function for the "following" direction |
| 2785 | * The following axis contains all nodes in the same document as the context |
| 2786 | * node that are after the context node in document order, excluding any |
| 2787 | * descendants and excluding attribute nodes and namespace nodes; the nodes |
| 2788 | * are ordered in document order |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2789 | * |
| 2790 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2791 | */ |
| 2792 | xmlNodePtr |
| 2793 | xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2794 | if (cur != NULL && cur->children != NULL) |
| 2795 | return cur->children ; |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2796 | if (cur == NULL) cur = ctxt->context->node; |
| 2797 | if (cur == NULL) return(NULL) ; /* ERROR */ |
| 2798 | if (cur->next != NULL) return(cur->next) ; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2799 | do { |
| 2800 | cur = cur->parent; |
| 2801 | if (cur == NULL) return(NULL); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2802 | if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2803 | if (cur->next != NULL) return(cur->next); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2804 | } while (cur != NULL); |
| 2805 | return(cur); |
| 2806 | } |
| 2807 | |
| 2808 | /* |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2809 | * xmlXPathIsAncestor: |
| 2810 | * @ancestor: the ancestor node |
| 2811 | * @node: the current node |
| 2812 | * |
| 2813 | * Check that @ancestor is a @node's ancestor |
| 2814 | * |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2815 | * returns 1 if @ancestor is a @node's ancestor, 0 otherwise. |
| 2816 | */ |
| 2817 | static int |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2818 | xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2819 | if ((ancestor == NULL) || (node == NULL)) return(0); |
| 2820 | /* nodes need to be in the same document */ |
| 2821 | if (ancestor->doc != node->doc) return(0); |
| 2822 | /* avoid searching if ancestor or node is the root node */ |
| 2823 | if (ancestor == (xmlNodePtr) node->doc) return(1); |
| 2824 | if (node == (xmlNodePtr) ancestor->doc) return(0); |
| 2825 | while (node->parent != NULL) { |
| 2826 | if (node->parent == ancestor) |
| 2827 | return(1); |
| 2828 | node = node->parent; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2829 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2830 | return(0); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
| 2833 | /** |
| 2834 | * xmlXPathNextPreceding: |
| 2835 | * @ctxt: the XPath Parser context |
| 2836 | * @cur: the current node in the traversal |
| 2837 | * |
| 2838 | * Traversal function for the "preceding" direction |
| 2839 | * the preceding axis contains all nodes in the same document as the context |
| 2840 | * node that are before the context node in document order, excluding any |
| 2841 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 2842 | * ordered in reverse document order |
| 2843 | * |
| 2844 | * Returns the next element following that axis |
| 2845 | */ |
| 2846 | xmlNodePtr |
| 2847 | xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 2848 | if (cur == NULL) |
| 2849 | cur = ctxt->context->node ; |
| 2850 | do { |
| 2851 | if (cur->prev != NULL) { |
| 2852 | for (cur = cur->prev ; cur->last != NULL ; cur = cur->last) |
| 2853 | ; |
| 2854 | return(cur) ; |
| 2855 | } |
| 2856 | |
| 2857 | cur = cur->parent; |
| 2858 | if (cur == NULL) return(NULL); |
| 2859 | if (cur == ctxt->context->doc->children) return(NULL); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 2860 | } while (xmlXPathIsAncestor(cur, ctxt->context->node)); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2861 | return(cur); |
| 2862 | } |
| 2863 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2864 | /** |
| 2865 | * xmlXPathNextNamespace: |
| 2866 | * @ctxt: the XPath Parser context |
| 2867 | * @cur: the current attribute in the traversal |
| 2868 | * |
| 2869 | * Traversal function for the "namespace" direction |
| 2870 | * the namespace axis contains the namespace nodes of the context node; |
| 2871 | * the order of nodes on this axis is implementation-defined; the axis will |
| 2872 | * be empty unless the context node is an element |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2873 | * |
| 2874 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2875 | */ |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2876 | xmlNodePtr |
| 2877 | xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 2878 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2879 | if ((cur == NULL) || (ctxt->context->namespaces == NULL)) { |
| 2880 | if (ctxt->context->namespaces != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 2881 | xmlFree(ctxt->context->namespaces); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2882 | ctxt->context->namespaces = |
| 2883 | xmlGetNsList(ctxt->context->doc, ctxt->context->node); |
| 2884 | if (ctxt->context->namespaces == NULL) return(NULL); |
| 2885 | ctxt->context->nsNr = 0; |
| 2886 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2887 | return((xmlNodePtr)ctxt->context->namespaces[ctxt->context->nsNr++]); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
| 2890 | /** |
| 2891 | * xmlXPathNextAttribute: |
| 2892 | * @ctxt: the XPath Parser context |
| 2893 | * @cur: the current attribute in the traversal |
| 2894 | * |
| 2895 | * Traversal function for the "attribute" direction |
Daniel Veillard | 10a2c65 | 1999-12-12 13:03:50 +0000 | [diff] [blame] | 2896 | * TODO: support DTD inherited default attributes |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 2897 | * |
| 2898 | * Returns the next element following that axis |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2899 | */ |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2900 | xmlNodePtr |
| 2901 | xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
| 2902 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2903 | if (cur == NULL) { |
| 2904 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 2905 | return(NULL); |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2906 | return((xmlNodePtr)ctxt->context->node->properties); |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 2907 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2908 | return((xmlNodePtr)cur->next); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | /************************************************************************ |
| 2912 | * * |
| 2913 | * NodeTest Functions * |
| 2914 | * * |
| 2915 | ************************************************************************/ |
| 2916 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2917 | typedef enum { |
| 2918 | NODE_TEST_NONE = 0, |
| 2919 | NODE_TEST_TYPE = 1, |
| 2920 | NODE_TEST_PI = 2, |
| 2921 | NODE_TEST_ALL = 3, |
| 2922 | NODE_TEST_NS = 4, |
| 2923 | NODE_TEST_NAME = 5 |
| 2924 | } xmlXPathTestVal; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2925 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2926 | typedef enum { |
Daniel Veillard | 2f913b7 | 2001-01-31 13:23:49 +0000 | [diff] [blame] | 2927 | NODE_TYPE_NODE = 0, |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 2928 | NODE_TYPE_COMMENT = XML_COMMENT_NODE, |
| 2929 | NODE_TYPE_TEXT = XML_TEXT_NODE, |
Daniel Veillard | 2f913b7 | 2001-01-31 13:23:49 +0000 | [diff] [blame] | 2930 | NODE_TYPE_PI = XML_PI_NODE |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2931 | } xmlXPathTypeVal; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2932 | |
| 2933 | #define IS_FUNCTION 200 |
| 2934 | |
| 2935 | /** |
| 2936 | * xmlXPathNodeCollectAndTest: |
| 2937 | * @ctxt: the XPath Parser context |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2938 | * @axis: the XPath axis |
| 2939 | * @test: the XPath test |
| 2940 | * @type: the XPath type |
| 2941 | * @prefix: the namesapce prefix if any |
| 2942 | * @name: the name used in the search if any |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2943 | * |
| 2944 | * This is the function implementing a step: based on the current list |
| 2945 | * of nodes, it builds up a new list, looking at all nodes under that |
| 2946 | * axis and selecting them. |
| 2947 | * |
| 2948 | * Returns the new NodeSet resulting from the search. |
| 2949 | */ |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2950 | void |
| 2951 | xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, xmlXPathAxisVal axis, |
| 2952 | xmlXPathTestVal test, xmlXPathTypeVal type, |
| 2953 | const xmlChar *prefix, const xmlChar *name) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2954 | #ifdef DEBUG_STEP |
| 2955 | int n = 0, t = 0; |
| 2956 | #endif |
| 2957 | int i; |
| 2958 | xmlNodeSetPtr ret; |
| 2959 | xmlXPathTraversalFunction next = NULL; |
| 2960 | xmlNodePtr cur = NULL; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2961 | xmlXPathObjectPtr obj; |
| 2962 | xmlNodeSetPtr nodelist; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2963 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 2964 | CHECK_TYPE(XPATH_NODESET); |
| 2965 | obj = valuePop(ctxt); |
| 2966 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2967 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2968 | xmlGenericError(xmlGenericErrorContext, |
| 2969 | "new step : "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2970 | #endif |
| 2971 | switch (axis) { |
| 2972 | case AXIS_ANCESTOR: |
| 2973 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2974 | xmlGenericError(xmlGenericErrorContext, |
| 2975 | "axis 'ancestors' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2976 | #endif |
| 2977 | next = xmlXPathNextAncestor; break; |
| 2978 | case AXIS_ANCESTOR_OR_SELF: |
| 2979 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2980 | xmlGenericError(xmlGenericErrorContext, |
| 2981 | "axis 'ancestors-or-self' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2982 | #endif |
| 2983 | next = xmlXPathNextAncestorOrSelf; break; |
| 2984 | case AXIS_ATTRIBUTE: |
| 2985 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2986 | xmlGenericError(xmlGenericErrorContext, |
| 2987 | "axis 'attributes' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2988 | #endif |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 2989 | next = xmlXPathNextAttribute; break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2990 | break; |
| 2991 | case AXIS_CHILD: |
| 2992 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2993 | xmlGenericError(xmlGenericErrorContext, |
| 2994 | "axis 'child' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 2995 | #endif |
| 2996 | next = xmlXPathNextChild; break; |
| 2997 | case AXIS_DESCENDANT: |
| 2998 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2999 | xmlGenericError(xmlGenericErrorContext, |
| 3000 | "axis 'descendant' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3001 | #endif |
| 3002 | next = xmlXPathNextDescendant; break; |
| 3003 | case AXIS_DESCENDANT_OR_SELF: |
| 3004 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3005 | xmlGenericError(xmlGenericErrorContext, |
| 3006 | "axis 'descendant-or-self' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3007 | #endif |
| 3008 | next = xmlXPathNextDescendantOrSelf; break; |
| 3009 | case AXIS_FOLLOWING: |
| 3010 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3011 | xmlGenericError(xmlGenericErrorContext, |
| 3012 | "axis 'following' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3013 | #endif |
| 3014 | next = xmlXPathNextFollowing; break; |
| 3015 | case AXIS_FOLLOWING_SIBLING: |
| 3016 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3017 | xmlGenericError(xmlGenericErrorContext, |
| 3018 | "axis 'following-siblings' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3019 | #endif |
| 3020 | next = xmlXPathNextFollowingSibling; break; |
| 3021 | case AXIS_NAMESPACE: |
| 3022 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3023 | xmlGenericError(xmlGenericErrorContext, |
| 3024 | "axis 'namespace' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3025 | #endif |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3026 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3027 | break; |
| 3028 | case AXIS_PARENT: |
| 3029 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3030 | xmlGenericError(xmlGenericErrorContext, |
| 3031 | "axis 'parent' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3032 | #endif |
| 3033 | next = xmlXPathNextParent; break; |
| 3034 | case AXIS_PRECEDING: |
| 3035 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3036 | xmlGenericError(xmlGenericErrorContext, |
| 3037 | "axis 'preceding' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3038 | #endif |
| 3039 | next = xmlXPathNextPreceding; break; |
| 3040 | case AXIS_PRECEDING_SIBLING: |
| 3041 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3042 | xmlGenericError(xmlGenericErrorContext, |
| 3043 | "axis 'preceding-sibling' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3044 | #endif |
| 3045 | next = xmlXPathNextPrecedingSibling; break; |
| 3046 | case AXIS_SELF: |
| 3047 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3048 | xmlGenericError(xmlGenericErrorContext, |
| 3049 | "axis 'self' "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3050 | #endif |
| 3051 | next = xmlXPathNextSelf; break; |
| 3052 | } |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3053 | if (next == NULL) |
| 3054 | return; |
| 3055 | |
| 3056 | nodelist = obj->nodesetval; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3057 | ret = xmlXPathNodeSetCreate(NULL); |
| 3058 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3059 | xmlGenericError(xmlGenericErrorContext, |
| 3060 | " context contains %d nodes\n", |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3061 | nodelist->nodeNr); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3062 | switch (test) { |
Daniel Veillard | 2f913b7 | 2001-01-31 13:23:49 +0000 | [diff] [blame] | 3063 | case NODE_TEST_NODE: |
| 3064 | xmlGenericError(xmlGenericErrorContext, |
| 3065 | " searching all nodes\n"); |
| 3066 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3067 | case NODE_TEST_NONE: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3068 | xmlGenericError(xmlGenericErrorContext, |
| 3069 | " searching for none !!!\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3070 | break; |
| 3071 | case NODE_TEST_TYPE: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3072 | xmlGenericError(xmlGenericErrorContext, |
| 3073 | " searching for type %d\n", type); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3074 | break; |
| 3075 | case NODE_TEST_PI: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3076 | xmlGenericError(xmlGenericErrorContext, |
| 3077 | " searching for PI !!!\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3078 | break; |
| 3079 | case NODE_TEST_ALL: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3080 | xmlGenericError(xmlGenericErrorContext, |
| 3081 | " searching for *\n"); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3082 | break; |
| 3083 | case NODE_TEST_NS: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3084 | xmlGenericError(xmlGenericErrorContext, |
| 3085 | " searching for namespace %s\n", |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3086 | prefix); |
| 3087 | break; |
| 3088 | case NODE_TEST_NAME: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3089 | xmlGenericError(xmlGenericErrorContext, |
| 3090 | " searching for name %s\n", name); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3091 | if (prefix != NULL) |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3092 | xmlGenericError(xmlGenericErrorContext, |
| 3093 | " with namespace %s\n", |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3094 | prefix); |
| 3095 | break; |
| 3096 | } |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3097 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3098 | #endif |
Daniel Veillard | c8df0aa | 2000-10-10 23:50:30 +0000 | [diff] [blame] | 3099 | /* |
| 3100 | * 2.3 Node Tests |
| 3101 | * - For the attribute axis, the principal node type is attribute. |
| 3102 | * - For the namespace axis, the principal node type is namespace. |
| 3103 | * - For other axes, the principal node type is element. |
| 3104 | * |
| 3105 | * A node test * is true for any node of the |
| 3106 | * principal node type. For example, child::* willi |
| 3107 | * select all element children of the context node |
| 3108 | */ |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3109 | for (i = 0;i < nodelist->nodeNr; i++) { |
| 3110 | ctxt->context->node = nodelist->nodeTab[i]; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3111 | |
| 3112 | cur = NULL; |
| 3113 | do { |
| 3114 | cur = next(ctxt, cur); |
| 3115 | if (cur == NULL) break; |
| 3116 | #ifdef DEBUG_STEP |
| 3117 | t++; |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3118 | xmlGenericError(xmlGenericErrorContext, " %s", cur->name); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3119 | #endif |
| 3120 | switch (test) { |
| 3121 | case NODE_TEST_NONE: |
| 3122 | STRANGE |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3123 | return; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3124 | case NODE_TEST_TYPE: |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3125 | if ((cur->type == type) || |
Daniel Veillard | 2f913b7 | 2001-01-31 13:23:49 +0000 | [diff] [blame] | 3126 | ((type == NODE_TYPE_NODE) && |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 3127 | ((cur->type == XML_DOCUMENT_NODE) || |
Daniel Veillard | 2f913b7 | 2001-01-31 13:23:49 +0000 | [diff] [blame] | 3128 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 3129 | (cur->type == XML_ELEMENT_NODE) || |
| 3130 | (cur->type == XML_PI_NODE) || |
| 3131 | (cur->type == XML_COMMENT_NODE) || |
| 3132 | (cur->type == XML_CDATA_SECTION_NODE) || |
| 3133 | (cur->type == XML_TEXT_NODE)))) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3134 | #ifdef DEBUG_STEP |
| 3135 | n++; |
| 3136 | #endif |
| 3137 | xmlXPathNodeSetAdd(ret, cur); |
| 3138 | } |
| 3139 | break; |
| 3140 | case NODE_TEST_PI: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3141 | if (cur->type == XML_PI_NODE) { |
| 3142 | if ((name != NULL) && |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3143 | (!xmlStrEqual(name, cur->name))) |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3144 | break; |
| 3145 | #ifdef DEBUG_STEP |
| 3146 | n++; |
| 3147 | #endif |
| 3148 | xmlXPathNodeSetAdd(ret, cur); |
| 3149 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3150 | break; |
| 3151 | case NODE_TEST_ALL: |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3152 | if (axis == AXIS_ATTRIBUTE) { |
| 3153 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3154 | #ifdef DEBUG_STEP |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3155 | n++; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3156 | #endif |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3157 | xmlXPathNodeSetAdd(ret, cur); |
| 3158 | } |
| 3159 | } else if (axis == AXIS_NAMESPACE) { |
| 3160 | if (cur->type == XML_NAMESPACE_DECL) { |
| 3161 | #ifdef DEBUG_STEP |
| 3162 | n++; |
| 3163 | #endif |
| 3164 | xmlXPathNodeSetAdd(ret, cur); |
| 3165 | } |
| 3166 | } else { |
| 3167 | if ((cur->type == XML_ELEMENT_NODE) || |
| 3168 | (cur->type == XML_DOCUMENT_NODE) || |
| 3169 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3170 | if (prefix == NULL) { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3171 | #ifdef DEBUG_STEP |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3172 | n++; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3173 | #endif |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3174 | xmlXPathNodeSetAdd(ret, cur); |
| 3175 | } else if ((cur->ns != NULL) && |
| 3176 | (xmlStrEqual(prefix, |
| 3177 | cur->ns->href))) { |
| 3178 | #ifdef DEBUG_STEP |
| 3179 | n++; |
| 3180 | #endif |
| 3181 | xmlXPathNodeSetAdd(ret, cur); |
| 3182 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3183 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3184 | } |
| 3185 | break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3186 | case NODE_TEST_NS: { |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3187 | TODO; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3188 | break; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3189 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3190 | case NODE_TEST_NAME: |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3191 | switch (cur->type) { |
| 3192 | case XML_ELEMENT_NODE: |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3193 | if (xmlStrEqual(name, cur->name)) { |
| 3194 | if (prefix == NULL) { |
| 3195 | if ((cur->ns == NULL) || |
| 3196 | (cur->ns->prefix == NULL)) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3197 | #ifdef DEBUG_STEP |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3198 | n++; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3199 | #endif |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 3200 | xmlXPathNodeSetAdd(ret, cur); |
| 3201 | } |
| 3202 | } else { |
| 3203 | if ((cur->ns != NULL) && |
| 3204 | (xmlStrEqual(prefix, |
| 3205 | cur->ns->href))) { |
| 3206 | #ifdef DEBUG_STEP |
| 3207 | n++; |
| 3208 | #endif |
| 3209 | xmlXPathNodeSetAdd(ret, cur); |
| 3210 | } |
| 3211 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3212 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3213 | break; |
| 3214 | case XML_ATTRIBUTE_NODE: { |
| 3215 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | 8b5dd83 | 2000-10-01 20:28:44 +0000 | [diff] [blame] | 3216 | if (xmlStrEqual(name, attr->name)) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3217 | #ifdef DEBUG_STEP |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3218 | n++; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3219 | #endif |
| 3220 | xmlXPathNodeSetAdd(ret, cur); |
| 3221 | } |
| 3222 | break; |
| 3223 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3224 | case XML_NAMESPACE_DECL: { |
| 3225 | TODO; |
| 3226 | break; |
| 3227 | } |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3228 | default: |
| 3229 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3230 | } |
| 3231 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3232 | } |
| 3233 | } while (cur != NULL); |
| 3234 | } |
| 3235 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3236 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3237 | "\nExamined %d nodes, found %d nodes at that step\n", t, n); |
| 3238 | #endif |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3239 | xmlXPathFreeObject(obj); |
| 3240 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
| 3243 | |
| 3244 | /************************************************************************ |
| 3245 | * * |
| 3246 | * Implicit tree core function library * |
| 3247 | * * |
| 3248 | ************************************************************************/ |
| 3249 | |
| 3250 | /** |
| 3251 | * xmlXPathRoot: |
| 3252 | * @ctxt: the XPath Parser context |
| 3253 | * |
| 3254 | * Initialize the context to the root of the document |
| 3255 | */ |
| 3256 | void |
| 3257 | xmlXPathRoot(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 3258 | ctxt->context->node = (xmlNodePtr) ctxt->context->doc; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3259 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3262 | /************************************************************************ |
| 3263 | * * |
| 3264 | * The explicit core function library * |
| 3265 | *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib * |
| 3266 | * * |
| 3267 | ************************************************************************/ |
| 3268 | |
| 3269 | |
| 3270 | /** |
| 3271 | * xmlXPathLastFunction: |
| 3272 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3273 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3274 | * |
| 3275 | * Implement the last() XPath function |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3276 | * number last() |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3277 | * The last function returns the number of nodes in the context node list. |
| 3278 | */ |
| 3279 | void |
| 3280 | xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3281 | CHECK_ARITY(0); |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 3282 | if (ctxt->context->contextSize > 0) { |
| 3283 | valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize)); |
| 3284 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3285 | xmlGenericError(xmlGenericErrorContext, |
| 3286 | "last() : %d\n", ctxt->context->contextSize); |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 3287 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3288 | } else { |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 3289 | XP_ERROR(XPATH_INVALID_CTXT_SIZE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3290 | } |
| 3291 | } |
| 3292 | |
| 3293 | /** |
| 3294 | * xmlXPathPositionFunction: |
| 3295 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3296 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3297 | * |
| 3298 | * Implement the position() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3299 | * number position() |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3300 | * The position function returns the position of the context node in the |
| 3301 | * context node list. The first position is 1, and so the last positionr |
| 3302 | * will be equal to last(). |
| 3303 | */ |
| 3304 | void |
| 3305 | xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3306 | CHECK_ARITY(0); |
Daniel Veillard | ff9c330 | 2000-10-13 16:38:25 +0000 | [diff] [blame] | 3307 | if (ctxt->context->proximityPosition >= 0) { |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 3308 | valuePush(ctxt, |
Daniel Veillard | ff9c330 | 2000-10-13 16:38:25 +0000 | [diff] [blame] | 3309 | xmlXPathNewFloat((double) ctxt->context->proximityPosition)); |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 3310 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3311 | xmlGenericError(xmlGenericErrorContext, "position() : %d\n", |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 3312 | ctxt->context->proximityPosition); |
| 3313 | #endif |
| 3314 | } else { |
| 3315 | XP_ERROR(XPATH_INVALID_CTXT_POSITION); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3316 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | /** |
| 3320 | * xmlXPathCountFunction: |
| 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 count() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3325 | * number count(node-set) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3326 | */ |
| 3327 | void |
| 3328 | xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3329 | xmlXPathObjectPtr cur; |
| 3330 | |
| 3331 | CHECK_ARITY(1); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 3332 | if ((ctxt->value == NULL) || |
| 3333 | ((ctxt->value->type != XPATH_NODESET) && |
| 3334 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 3335 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3336 | cur = valuePop(ctxt); |
| 3337 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3338 | valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3339 | xmlXPathFreeObject(cur); |
| 3340 | } |
| 3341 | |
| 3342 | /** |
| 3343 | * xmlXPathIdFunction: |
| 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 id() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3348 | * node-set id(object) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3349 | * The id function selects elements by their unique ID |
| 3350 | * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set, |
| 3351 | * then the result is the union of the result of applying id to the |
| 3352 | * string value of each of the nodes in the argument node-set. When the |
| 3353 | * argument to id is of any other type, the argument is converted to a |
| 3354 | * string as if by a call to the string function; the string is split |
| 3355 | * into a whitespace-separated list of tokens (whitespace is any sequence |
| 3356 | * of characters matching the production S); the result is a node-set |
| 3357 | * containing the elements in the same document as the context node that |
| 3358 | * have a unique ID equal to any of the tokens in the list. |
| 3359 | */ |
| 3360 | void |
| 3361 | xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3362 | const xmlChar *tokens; |
| 3363 | const xmlChar *cur; |
| 3364 | xmlChar *ID; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3365 | xmlAttrPtr attr; |
| 3366 | xmlNodePtr elem = NULL; |
| 3367 | xmlXPathObjectPtr ret, obj; |
| 3368 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3369 | CHECK_ARITY(1); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3370 | obj = valuePop(ctxt); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3371 | if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3372 | if (obj->type == XPATH_NODESET) { |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3373 | xmlXPathObjectPtr newobj; |
| 3374 | int i; |
| 3375 | |
| 3376 | ret = xmlXPathNewNodeSet(NULL); |
| 3377 | |
| 3378 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
| 3379 | valuePush(ctxt, |
| 3380 | xmlXPathNewNodeSet(obj->nodesetval->nodeTab[i])); |
| 3381 | xmlXPathStringFunction(ctxt, 1); |
| 3382 | xmlXPathIdFunction(ctxt, 1); |
| 3383 | newobj = valuePop(ctxt); |
| 3384 | ret->nodesetval = xmlXPathNodeSetMerge(ret->nodesetval, |
| 3385 | newobj->nodesetval); |
| 3386 | xmlXPathFreeObject(newobj); |
| 3387 | } |
| 3388 | |
| 3389 | xmlXPathFreeObject(obj); |
| 3390 | valuePush(ctxt, ret); |
| 3391 | return; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3392 | } |
| 3393 | if (obj->type != XPATH_STRING) { |
| 3394 | valuePush(ctxt, obj); |
| 3395 | xmlXPathStringFunction(ctxt, 1); |
| 3396 | obj = valuePop(ctxt); |
| 3397 | if (obj->type != XPATH_STRING) { |
| 3398 | xmlXPathFreeObject(obj); |
| 3399 | return; |
| 3400 | } |
| 3401 | } |
| 3402 | tokens = obj->stringval; |
| 3403 | |
| 3404 | ret = xmlXPathNewNodeSet(NULL); |
| 3405 | valuePush(ctxt, ret); |
| 3406 | if (tokens == NULL) { |
| 3407 | xmlXPathFreeObject(obj); |
| 3408 | return; |
| 3409 | } |
| 3410 | |
| 3411 | cur = tokens; |
| 3412 | |
| 3413 | while (IS_BLANK(*cur)) cur++; |
| 3414 | while (*cur != 0) { |
| 3415 | while ((IS_LETTER(*cur)) || (IS_DIGIT(*cur)) || |
| 3416 | (*cur == '.') || (*cur == '-') || |
| 3417 | (*cur == '_') || (*cur == ':') || |
| 3418 | (IS_COMBINING(*cur)) || |
| 3419 | (IS_EXTENDER(*cur))) |
| 3420 | cur++; |
| 3421 | |
| 3422 | if ((!IS_BLANK(*cur)) && (*cur != 0)) break; |
| 3423 | |
| 3424 | ID = xmlStrndup(tokens, cur - tokens); |
| 3425 | attr = xmlGetID(ctxt->context->doc, ID); |
| 3426 | if (attr != NULL) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 3427 | elem = attr->parent; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3428 | xmlXPathNodeSetAdd(ret->nodesetval, elem); |
| 3429 | } |
| 3430 | if (ID != NULL) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3431 | xmlFree(ID); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3432 | |
| 3433 | while (IS_BLANK(*cur)) cur++; |
| 3434 | tokens = cur; |
| 3435 | } |
| 3436 | xmlXPathFreeObject(obj); |
| 3437 | return; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | /** |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3441 | * xmlXPathLocalNameFunction: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3442 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3443 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3444 | * |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3445 | * Implement the local-name() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3446 | * string local-name(node-set?) |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3447 | * The local-name function returns a string containing the local part |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3448 | * of the name of the node in the argument node-set that is first in |
| 3449 | * document order. If the node-set is empty or the first node has no |
| 3450 | * name, an empty string is returned. If the argument is omitted it |
| 3451 | * defaults to the context node. |
| 3452 | */ |
| 3453 | void |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3454 | xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3455 | xmlXPathObjectPtr cur; |
| 3456 | |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3457 | if (nargs == 0) { |
| 3458 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 3459 | nargs = 1; |
| 3460 | } |
| 3461 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3462 | CHECK_ARITY(1); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 3463 | if ((ctxt->value == NULL) || |
| 3464 | ((ctxt->value->type != XPATH_NODESET) && |
| 3465 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 3466 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3467 | cur = valuePop(ctxt); |
| 3468 | |
| 3469 | if (cur->nodesetval->nodeNr == 0) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3470 | valuePush(ctxt, xmlXPathNewCString("")); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3471 | } else { |
| 3472 | int i = 0; /* Should be first in document order !!!!! */ |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3473 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 3474 | case XML_ELEMENT_NODE: |
| 3475 | case XML_ATTRIBUTE_NODE: |
| 3476 | case XML_PI_NODE: |
| 3477 | valuePush(ctxt, |
| 3478 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
| 3479 | break; |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 3480 | case XML_NAMESPACE_DECL: |
| 3481 | valuePush(ctxt, xmlXPathNewString( |
| 3482 | ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix)); |
| 3483 | break; |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3484 | default: |
| 3485 | valuePush(ctxt, xmlXPathNewCString("")); |
| 3486 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3487 | } |
| 3488 | xmlXPathFreeObject(cur); |
| 3489 | } |
| 3490 | |
| 3491 | /** |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3492 | * xmlXPathNamespaceURIFunction: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3493 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3494 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3495 | * |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3496 | * Implement the namespace-uri() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3497 | * string namespace-uri(node-set?) |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3498 | * The namespace-uri function returns a string containing the |
| 3499 | * namespace URI of the expanded name of the node in the argument |
| 3500 | * node-set that is first in document order. If the node-set is empty, |
| 3501 | * the first node has no name, or the expanded name has no namespace |
| 3502 | * URI, an empty string is returned. If the argument is omitted it |
| 3503 | * defaults to the context node. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3504 | */ |
| 3505 | void |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3506 | xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3507 | xmlXPathObjectPtr cur; |
| 3508 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3509 | if (nargs == 0) { |
| 3510 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 3511 | nargs = 1; |
| 3512 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3513 | CHECK_ARITY(1); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 3514 | if ((ctxt->value == NULL) || |
| 3515 | ((ctxt->value->type != XPATH_NODESET) && |
| 3516 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 3517 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3518 | cur = valuePop(ctxt); |
| 3519 | |
| 3520 | if (cur->nodesetval->nodeNr == 0) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3521 | valuePush(ctxt, xmlXPathNewCString("")); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3522 | } else { |
| 3523 | int i = 0; /* Should be first in document order !!!!! */ |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3524 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 3525 | case XML_ELEMENT_NODE: |
| 3526 | case XML_ATTRIBUTE_NODE: |
| 3527 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
| 3528 | valuePush(ctxt, xmlXPathNewCString("")); |
| 3529 | else |
| 3530 | valuePush(ctxt, xmlXPathNewString( |
| 3531 | cur->nodesetval->nodeTab[i]->ns->href)); |
| 3532 | break; |
| 3533 | default: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3534 | valuePush(ctxt, xmlXPathNewCString("")); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3535 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3536 | } |
| 3537 | xmlXPathFreeObject(cur); |
| 3538 | } |
| 3539 | |
| 3540 | /** |
| 3541 | * xmlXPathNameFunction: |
| 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 | * |
| 3545 | * Implement the name() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3546 | * string name(node-set?) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3547 | * The name function returns a string containing a QName representing |
| 3548 | * the name of the node in the argument node-set that is first in documenti |
| 3549 | * order. The QName must represent the name with respect to the namespace |
| 3550 | * declarations in effect on the node whose name is being represented. |
| 3551 | * Typically, this will be the form in which the name occurred in the XML |
| 3552 | * source. This need not be the case if there are namespace declarations |
| 3553 | * in effect on the node that associate multiple prefixes with the same |
| 3554 | * namespace. However, an implementation may include information about |
| 3555 | * the original prefix in its representation of nodes; in this case, an |
| 3556 | * implementation can ensure that the returned string is always the same |
| 3557 | * as the QName used in the XML source. If the argument it omitted it |
| 3558 | * defaults to the context node. |
| 3559 | * Libxml keep the original prefix so the "real qualified name" used is |
| 3560 | * returned. |
| 3561 | */ |
| 3562 | void |
| 3563 | xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3564 | xmlXPathObjectPtr cur; |
| 3565 | |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3566 | if (nargs == 0) { |
| 3567 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 3568 | nargs = 1; |
| 3569 | } |
| 3570 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3571 | CHECK_ARITY(1); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 3572 | if ((ctxt->value == NULL) || |
| 3573 | ((ctxt->value->type != XPATH_NODESET) && |
| 3574 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 3575 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3576 | cur = valuePop(ctxt); |
| 3577 | |
| 3578 | if (cur->nodesetval->nodeNr == 0) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3579 | valuePush(ctxt, xmlXPathNewCString("")); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3580 | } else { |
| 3581 | int i = 0; /* Should be first in document order !!!!! */ |
| 3582 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3583 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 3584 | case XML_ELEMENT_NODE: |
| 3585 | case XML_ATTRIBUTE_NODE: |
| 3586 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
| 3587 | valuePush(ctxt, xmlXPathNewString( |
| 3588 | cur->nodesetval->nodeTab[i]->name)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3589 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3590 | else { |
| 3591 | char name[2000]; |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 3592 | #ifdef HAVE_SNPRINTF |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3593 | snprintf(name, sizeof(name), "%s:%s", |
| 3594 | (char *) cur->nodesetval->nodeTab[i]->ns->prefix, |
| 3595 | (char *) cur->nodesetval->nodeTab[i]->name); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 3596 | #else |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3597 | sprintf(name, "%s:%s", |
| 3598 | (char *) cur->nodesetval->nodeTab[i]->ns->prefix, |
| 3599 | (char *) cur->nodesetval->nodeTab[i]->name); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 3600 | #endif |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3601 | name[sizeof(name) - 1] = 0; |
| 3602 | valuePush(ctxt, xmlXPathNewCString(name)); |
| 3603 | } |
| 3604 | break; |
| 3605 | default: |
| 3606 | valuePush(ctxt, |
| 3607 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i])); |
| 3608 | xmlXPathLocalNameFunction(ctxt, 1); |
| 3609 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3610 | } |
| 3611 | xmlXPathFreeObject(cur); |
| 3612 | } |
| 3613 | |
| 3614 | /** |
| 3615 | * xmlXPathStringFunction: |
| 3616 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3617 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3618 | * |
| 3619 | * Implement the string() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3620 | * string string(object?) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3621 | * he string function converts an object to a string as follows: |
| 3622 | * - A node-set is converted to a string by returning the value of |
| 3623 | * the node in the node-set that is first in document order. |
| 3624 | * If the node-set is empty, an empty string is returned. |
| 3625 | * - A number is converted to a string as follows |
| 3626 | * + NaN is converted to the string NaN |
| 3627 | * + positive zero is converted to the string 0 |
| 3628 | * + negative zero is converted to the string 0 |
| 3629 | * + positive infinity is converted to the string Infinity |
| 3630 | * + negative infinity is converted to the string -Infinity |
| 3631 | * + if the number is an integer, the number is represented in |
| 3632 | * decimal form as a Number with no decimal point and no leading |
| 3633 | * zeros, preceded by a minus sign (-) if the number is negative |
| 3634 | * + otherwise, the number is represented in decimal form as a |
| 3635 | * Number including a decimal point with at least one digit |
| 3636 | * before the decimal point and at least one digit after the |
| 3637 | * decimal point, preceded by a minus sign (-) if the number |
| 3638 | * is negative; there must be no leading zeros before the decimal |
| 3639 | * point apart possibly from the one required digit immediatelyi |
| 3640 | * before the decimal point; beyond the one required digit |
| 3641 | * after the decimal point there must be as many, but only as |
| 3642 | * many, more digits as are needed to uniquely distinguish the |
| 3643 | * number from all other IEEE 754 numeric values. |
| 3644 | * - The boolean false value is converted to the string false. |
| 3645 | * The boolean true value is converted to the string true. |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3646 | * |
| 3647 | * If the argument is omitted, it defaults to a node-set with the |
| 3648 | * context node as its only member. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3649 | */ |
| 3650 | void |
| 3651 | xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3652 | xmlXPathObjectPtr cur; |
| 3653 | |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 3654 | if (nargs == 0) { |
| 3655 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 3656 | nargs = 1; |
| 3657 | } |
| 3658 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3659 | CHECK_ARITY(1); |
| 3660 | cur = valuePop(ctxt); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3661 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3662 | switch (cur->type) { |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3663 | case XPATH_UNDEFINED: |
| 3664 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 3665 | xmlGenericError(xmlGenericErrorContext, "String: undefined\n"); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3666 | #endif |
| 3667 | valuePush(ctxt, xmlXPathNewCString("")); |
| 3668 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 3669 | case XPATH_XSLT_TREE: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3670 | case XPATH_NODESET: |
| 3671 | if (cur->nodesetval->nodeNr == 0) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3672 | valuePush(ctxt, xmlXPathNewCString("")); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3673 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3674 | xmlChar *res; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3675 | int i = 0; /* Should be first in document order !!!!! */ |
| 3676 | res = xmlNodeGetContent(cur->nodesetval->nodeTab[i]); |
| 3677 | valuePush(ctxt, xmlXPathNewString(res)); |
Daniel Veillard | 5a2b697 | 2001-01-20 21:15:50 +0000 | [diff] [blame] | 3678 | if (res != NULL) |
| 3679 | xmlFree(res); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3680 | } |
| 3681 | xmlXPathFreeObject(cur); |
| 3682 | return; |
| 3683 | case XPATH_STRING: |
| 3684 | valuePush(ctxt, cur); |
| 3685 | return; |
| 3686 | case XPATH_BOOLEAN: |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3687 | if (cur->boolval) valuePush(ctxt, xmlXPathNewCString("true")); |
| 3688 | else valuePush(ctxt, xmlXPathNewCString("false")); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3689 | xmlXPathFreeObject(cur); |
| 3690 | return; |
| 3691 | case XPATH_NUMBER: { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3692 | char buf[100]; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3693 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3694 | if (isnan(cur->floatval)) |
| 3695 | sprintf(buf, "NaN"); |
| 3696 | else if (isinf(cur->floatval) > 0) |
| 3697 | sprintf(buf, "+Infinity"); |
| 3698 | else if (isinf(cur->floatval) < 0) |
| 3699 | sprintf(buf, "-Infinity"); |
| 3700 | else |
| 3701 | sprintf(buf, "%0g", cur->floatval); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3702 | valuePush(ctxt, xmlXPathNewCString(buf)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3703 | xmlXPathFreeObject(cur); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3704 | return; |
| 3705 | } |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3706 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 3707 | case XPATH_POINT: |
| 3708 | case XPATH_RANGE: |
| 3709 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 3710 | TODO |
| 3711 | valuePush(ctxt, xmlXPathNewCString("")); |
| 3712 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3713 | } |
| 3714 | STRANGE |
| 3715 | } |
| 3716 | |
| 3717 | /** |
| 3718 | * xmlXPathStringLengthFunction: |
| 3719 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3720 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3721 | * |
| 3722 | * Implement the string-length() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3723 | * number string-length(string?) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3724 | * The string-length returns the number of characters in the string |
| 3725 | * (see [3.6 Strings]). If the argument is omitted, it defaults to |
| 3726 | * the context node converted to a string, in other words the value |
| 3727 | * of the context node. |
| 3728 | */ |
| 3729 | void |
| 3730 | xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3731 | xmlXPathObjectPtr cur; |
| 3732 | |
| 3733 | if (nargs == 0) { |
| 3734 | if (ctxt->context->node == NULL) { |
| 3735 | valuePush(ctxt, xmlXPathNewFloat(0)); |
| 3736 | } else { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3737 | xmlChar *content; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3738 | |
| 3739 | content = xmlNodeGetContent(ctxt->context->node); |
| 3740 | valuePush(ctxt, xmlXPathNewFloat(xmlStrlen(content))); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3741 | xmlFree(content); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3742 | } |
| 3743 | return; |
| 3744 | } |
| 3745 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3746 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3747 | CHECK_TYPE(XPATH_STRING); |
| 3748 | cur = valuePop(ctxt); |
| 3749 | valuePush(ctxt, xmlXPathNewFloat(xmlStrlen(cur->stringval))); |
| 3750 | xmlXPathFreeObject(cur); |
| 3751 | } |
| 3752 | |
| 3753 | /** |
| 3754 | * xmlXPathConcatFunction: |
| 3755 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3756 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3757 | * |
| 3758 | * Implement the concat() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3759 | * string concat(string, string, string*) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3760 | * The concat function returns the concatenation of its arguments. |
| 3761 | */ |
| 3762 | void |
| 3763 | xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3764 | xmlXPathObjectPtr cur, newobj; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3765 | xmlChar *tmp; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3766 | |
| 3767 | if (nargs < 2) { |
| 3768 | CHECK_ARITY(2); |
| 3769 | } |
| 3770 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3771 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3772 | cur = valuePop(ctxt); |
| 3773 | if ((cur == NULL) || (cur->type != XPATH_STRING)) { |
| 3774 | xmlXPathFreeObject(cur); |
| 3775 | return; |
| 3776 | } |
| 3777 | nargs--; |
| 3778 | |
| 3779 | while (nargs > 0) { |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3780 | CAST_TO_STRING; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3781 | newobj = valuePop(ctxt); |
| 3782 | if ((newobj == NULL) || (newobj->type != XPATH_STRING)) { |
| 3783 | xmlXPathFreeObject(newobj); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3784 | xmlXPathFreeObject(cur); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3785 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3786 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3787 | tmp = xmlStrcat(newobj->stringval, cur->stringval); |
| 3788 | newobj->stringval = cur->stringval; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3789 | cur->stringval = tmp; |
| 3790 | |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 3791 | xmlXPathFreeObject(newobj); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3792 | nargs--; |
| 3793 | } |
| 3794 | valuePush(ctxt, cur); |
| 3795 | } |
| 3796 | |
| 3797 | /** |
| 3798 | * xmlXPathContainsFunction: |
| 3799 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3800 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3801 | * |
| 3802 | * Implement the contains() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3803 | * boolean contains(string, string) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3804 | * The contains function returns true if the first argument string |
| 3805 | * contains the second argument string, and otherwise returns false. |
| 3806 | */ |
| 3807 | void |
| 3808 | xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3809 | xmlXPathObjectPtr hay, needle; |
| 3810 | |
| 3811 | CHECK_ARITY(2); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3812 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3813 | CHECK_TYPE(XPATH_STRING); |
| 3814 | needle = valuePop(ctxt); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3815 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3816 | hay = valuePop(ctxt); |
| 3817 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 3818 | xmlXPathFreeObject(hay); |
| 3819 | xmlXPathFreeObject(needle); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3820 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3821 | } |
| 3822 | if (xmlStrstr(hay->stringval, needle->stringval)) |
| 3823 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 3824 | else |
| 3825 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 3826 | xmlXPathFreeObject(hay); |
| 3827 | xmlXPathFreeObject(needle); |
| 3828 | } |
| 3829 | |
| 3830 | /** |
| 3831 | * xmlXPathStartsWithFunction: |
| 3832 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3833 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3834 | * |
| 3835 | * Implement the starts-with() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3836 | * boolean starts-with(string, string) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3837 | * The starts-with function returns true if the first argument string |
| 3838 | * starts with the second argument string, and otherwise returns false. |
| 3839 | */ |
| 3840 | void |
| 3841 | xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3842 | xmlXPathObjectPtr hay, needle; |
| 3843 | int n; |
| 3844 | |
| 3845 | CHECK_ARITY(2); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3846 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3847 | CHECK_TYPE(XPATH_STRING); |
| 3848 | needle = valuePop(ctxt); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3849 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3850 | hay = valuePop(ctxt); |
| 3851 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 3852 | xmlXPathFreeObject(hay); |
| 3853 | xmlXPathFreeObject(needle); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3854 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3855 | } |
| 3856 | n = xmlStrlen(needle->stringval); |
| 3857 | if (xmlStrncmp(hay->stringval, needle->stringval, n)) |
| 3858 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 3859 | else |
| 3860 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 3861 | xmlXPathFreeObject(hay); |
| 3862 | xmlXPathFreeObject(needle); |
| 3863 | } |
| 3864 | |
| 3865 | /** |
| 3866 | * xmlXPathSubstringFunction: |
| 3867 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3868 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3869 | * |
| 3870 | * Implement the substring() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3871 | * string substring(string, number, number?) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3872 | * The substring function returns the substring of the first argument |
| 3873 | * starting at the position specified in the second argument with |
| 3874 | * length specified in the third argument. For example, |
| 3875 | * substring("12345",2,3) returns "234". If the third argument is not |
| 3876 | * specified, it returns the substring starting at the position specified |
| 3877 | * in the second argument and continuing to the end of the string. For |
| 3878 | * example, substring("12345",2) returns "2345". More precisely, each |
| 3879 | * character in the string (see [3.6 Strings]) is considered to have a |
| 3880 | * numeric position: the position of the first character is 1, the position |
| 3881 | * of the second character is 2 and so on. The returned substring contains |
| 3882 | * those characters for which the position of the character is greater than |
| 3883 | * or equal to the second argument and, if the third argument is specified, |
| 3884 | * less than the sum of the second and third arguments; the comparisons |
| 3885 | * and addition used for the above follow the standard IEEE 754 rules. Thus: |
| 3886 | * - substring("12345", 1.5, 2.6) returns "234" |
| 3887 | * - substring("12345", 0, 3) returns "12" |
| 3888 | * - substring("12345", 0 div 0, 3) returns "" |
| 3889 | * - substring("12345", 1, 0 div 0) returns "" |
| 3890 | * - substring("12345", -42, 1 div 0) returns "12345" |
| 3891 | * - substring("12345", -1 div 0, 1 div 0) returns "" |
| 3892 | */ |
| 3893 | void |
| 3894 | xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 3895 | xmlXPathObjectPtr str, start, len; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3896 | double le, in; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3897 | int i, l; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 3898 | xmlChar *ret; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3899 | |
| 3900 | /* |
| 3901 | * Conformance needs to be checked !!!!! |
| 3902 | */ |
| 3903 | if (nargs < 2) { |
| 3904 | CHECK_ARITY(2); |
| 3905 | } |
| 3906 | if (nargs > 3) { |
| 3907 | CHECK_ARITY(3); |
| 3908 | } |
| 3909 | if (nargs == 3) { |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3910 | CAST_TO_NUMBER; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3911 | CHECK_TYPE(XPATH_NUMBER); |
| 3912 | len = valuePop(ctxt); |
| 3913 | le = len->floatval; |
| 3914 | xmlXPathFreeObject(len); |
| 3915 | } else { |
| 3916 | le = 2000000000; |
| 3917 | } |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3918 | CAST_TO_NUMBER; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3919 | CHECK_TYPE(XPATH_NUMBER); |
| 3920 | start = valuePop(ctxt); |
| 3921 | in = start->floatval; |
| 3922 | xmlXPathFreeObject(start); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3923 | CAST_TO_STRING; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3924 | CHECK_TYPE(XPATH_STRING); |
| 3925 | str = valuePop(ctxt); |
| 3926 | le += in; |
| 3927 | |
| 3928 | /* integer index of the first char */ |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3929 | i = (int) in; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3930 | if (((double)i) != in) i++; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3931 | |
| 3932 | /* integer index of the last char */ |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 3933 | l = (int) le; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3934 | if (((double)l) != le) l++; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3935 | |
| 3936 | /* back to a zero based len */ |
| 3937 | i--; |
| 3938 | l--; |
| 3939 | |
| 3940 | /* check against the string len */ |
| 3941 | if (l > 1024) { |
| 3942 | l = xmlStrlen(str->stringval); |
| 3943 | } |
| 3944 | if (i < 0) { |
| 3945 | i = 0; |
| 3946 | } |
| 3947 | |
| 3948 | /* number of chars to copy */ |
| 3949 | l -= i; |
| 3950 | |
| 3951 | ret = xmlStrsub(str->stringval, i, l); |
| 3952 | if (ret == NULL) |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 3953 | valuePush(ctxt, xmlXPathNewCString("")); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3954 | else { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3955 | valuePush(ctxt, xmlXPathNewString(ret)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 3956 | xmlFree(ret); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 3957 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3958 | xmlXPathFreeObject(str); |
| 3959 | } |
| 3960 | |
| 3961 | /** |
| 3962 | * xmlXPathSubstringBeforeFunction: |
| 3963 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 3964 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3965 | * |
| 3966 | * Implement the substring-before() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3967 | * string substring-before(string, string) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 3968 | * The substring-before function returns the substring of the first |
| 3969 | * argument string that precedes the first occurrence of the second |
| 3970 | * argument string in the first argument string, or the empty string |
| 3971 | * if the first argument string does not contain the second argument |
| 3972 | * string. For example, substring-before("1999/04/01","/") returns 1999. |
| 3973 | */ |
| 3974 | void |
| 3975 | xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 3976 | xmlXPathObjectPtr str; |
| 3977 | xmlXPathObjectPtr find; |
| 3978 | xmlBufferPtr target; |
| 3979 | const xmlChar *point; |
| 3980 | int offset; |
| 3981 | |
| 3982 | CHECK_ARITY(2); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3983 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 3984 | find = valuePop(ctxt); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 3985 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 3986 | str = valuePop(ctxt); |
| 3987 | |
| 3988 | target = xmlBufferCreate(); |
| 3989 | if (target) { |
| 3990 | point = xmlStrstr(str->stringval, find->stringval); |
| 3991 | if (point) { |
| 3992 | offset = (int)(point - str->stringval); |
| 3993 | xmlBufferAdd(target, str->stringval, offset); |
| 3994 | } |
| 3995 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 3996 | xmlBufferFree(target); |
| 3997 | } |
| 3998 | |
| 3999 | xmlXPathFreeObject(str); |
| 4000 | xmlXPathFreeObject(find); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | /** |
| 4004 | * xmlXPathSubstringAfterFunction: |
| 4005 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4006 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4007 | * |
| 4008 | * Implement the substring-after() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4009 | * string substring-after(string, string) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4010 | * The substring-after function returns the substring of the first |
| 4011 | * argument string that follows the first occurrence of the second |
| 4012 | * argument string in the first argument string, or the empty stringi |
| 4013 | * if the first argument string does not contain the second argument |
| 4014 | * string. For example, substring-after("1999/04/01","/") returns 04/01, |
| 4015 | * and substring-after("1999/04/01","19") returns 99/04/01. |
| 4016 | */ |
| 4017 | void |
| 4018 | xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4019 | xmlXPathObjectPtr str; |
| 4020 | xmlXPathObjectPtr find; |
| 4021 | xmlBufferPtr target; |
| 4022 | const xmlChar *point; |
| 4023 | int offset; |
| 4024 | |
| 4025 | CHECK_ARITY(2); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4026 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4027 | find = valuePop(ctxt); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4028 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4029 | str = valuePop(ctxt); |
| 4030 | |
| 4031 | target = xmlBufferCreate(); |
| 4032 | if (target) { |
| 4033 | point = xmlStrstr(str->stringval, find->stringval); |
| 4034 | if (point) { |
| 4035 | offset = (int)(point - str->stringval) + xmlStrlen(find->stringval); |
| 4036 | xmlBufferAdd(target, &str->stringval[offset], |
| 4037 | xmlStrlen(str->stringval) - offset); |
| 4038 | } |
| 4039 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 4040 | xmlBufferFree(target); |
| 4041 | } |
| 4042 | |
| 4043 | xmlXPathFreeObject(str); |
| 4044 | xmlXPathFreeObject(find); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | /** |
| 4048 | * xmlXPathNormalizeFunction: |
| 4049 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4050 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4051 | * |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4052 | * Implement the normalize-space() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4053 | * string normalize-space(string?) |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4054 | * The normalize-space function returns the argument string with white |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4055 | * space normalized by stripping leading and trailing whitespace |
| 4056 | * and replacing sequences of whitespace characters by a single |
| 4057 | * space. Whitespace characters are the same allowed by the S production |
| 4058 | * in XML. If the argument is omitted, it defaults to the context |
| 4059 | * node converted to a string, in other words the value of the context node. |
| 4060 | */ |
| 4061 | void |
| 4062 | xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4063 | xmlXPathObjectPtr obj = NULL; |
| 4064 | xmlChar *source = NULL; |
| 4065 | xmlBufferPtr target; |
| 4066 | xmlChar blank; |
| 4067 | |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4068 | if (nargs == 0) { |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4069 | /* Use current context node */ |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4070 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 4071 | xmlXPathStringFunction(ctxt, 1); |
| 4072 | nargs = 1; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4073 | } |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4074 | |
| 4075 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4076 | CAST_TO_STRING; |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4077 | CHECK_TYPE(XPATH_STRING); |
| 4078 | obj = valuePop(ctxt); |
| 4079 | source = obj->stringval; |
| 4080 | |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4081 | target = xmlBufferCreate(); |
| 4082 | if (target && source) { |
| 4083 | |
| 4084 | /* Skip leading whitespaces */ |
| 4085 | while (IS_BLANK(*source)) |
| 4086 | source++; |
| 4087 | |
| 4088 | /* Collapse intermediate whitespaces, and skip trailing whitespaces */ |
| 4089 | blank = 0; |
| 4090 | while (*source) { |
| 4091 | if (IS_BLANK(*source)) { |
| 4092 | blank = *source; |
| 4093 | } else { |
| 4094 | if (blank) { |
| 4095 | xmlBufferAdd(target, &blank, 1); |
| 4096 | blank = 0; |
| 4097 | } |
| 4098 | xmlBufferAdd(target, source, 1); |
| 4099 | } |
| 4100 | source++; |
| 4101 | } |
| 4102 | |
| 4103 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 4104 | xmlBufferFree(target); |
| 4105 | } |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4106 | xmlXPathFreeObject(obj); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | /** |
| 4110 | * xmlXPathTranslateFunction: |
| 4111 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4112 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4113 | * |
| 4114 | * Implement the translate() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4115 | * string translate(string, string, string) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4116 | * The translate function returns the first argument string with |
| 4117 | * occurrences of characters in the second argument string replaced |
| 4118 | * by the character at the corresponding position in the third argument |
| 4119 | * string. For example, translate("bar","abc","ABC") returns the string |
| 4120 | * BAr. If there is a character in the second argument string with no |
| 4121 | * character at a corresponding position in the third argument string |
| 4122 | * (because the second argument string is longer than the third argument |
| 4123 | * string), then occurrences of that character in the first argument |
| 4124 | * string are removed. For example, translate("--aaa--","abc-","ABC") |
| 4125 | * returns "AAA". If a character occurs more than once in second |
| 4126 | * argument string, then the first occurrence determines the replacement |
| 4127 | * character. If the third argument string is longer than the second |
| 4128 | * argument string, then excess characters are ignored. |
| 4129 | */ |
| 4130 | void |
| 4131 | xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4132 | xmlXPathObjectPtr str; |
| 4133 | xmlXPathObjectPtr from; |
| 4134 | xmlXPathObjectPtr to; |
| 4135 | xmlBufferPtr target; |
| 4136 | int i, offset, max; |
| 4137 | xmlChar ch; |
| 4138 | const xmlChar *point; |
| 4139 | |
| 4140 | CHECK_ARITY(3); |
| 4141 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4142 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4143 | to = valuePop(ctxt); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4144 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4145 | from = valuePop(ctxt); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4146 | CAST_TO_STRING; |
Daniel Veillard | 46057e1 | 2000-09-24 18:49:59 +0000 | [diff] [blame] | 4147 | str = valuePop(ctxt); |
| 4148 | |
| 4149 | target = xmlBufferCreate(); |
| 4150 | if (target) { |
| 4151 | max = xmlStrlen(to->stringval); |
| 4152 | for (i = 0; (ch = str->stringval[i]); i++) { |
| 4153 | point = xmlStrchr(from->stringval, ch); |
| 4154 | if (point) { |
| 4155 | /* Warning: This may not work with UTF-8 */ |
| 4156 | offset = (int)(point - from->stringval); |
| 4157 | if (offset < max) |
| 4158 | xmlBufferAdd(target, &to->stringval[offset], 1); |
| 4159 | } else |
| 4160 | xmlBufferAdd(target, &ch, 1); |
| 4161 | } |
| 4162 | } |
| 4163 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 4164 | xmlBufferFree(target); |
| 4165 | xmlXPathFreeObject(str); |
| 4166 | xmlXPathFreeObject(from); |
| 4167 | xmlXPathFreeObject(to); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | /** |
| 4171 | * xmlXPathBooleanFunction: |
| 4172 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4173 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4174 | * |
| 4175 | * Implement the boolean() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4176 | * boolean boolean(object) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4177 | * he boolean function converts its argument to a boolean as follows: |
| 4178 | * - a number is true if and only if it is neither positive or |
| 4179 | * negative zero nor NaN |
| 4180 | * - a node-set is true if and only if it is non-empty |
| 4181 | * - a string is true if and only if its length is non-zero |
| 4182 | */ |
| 4183 | void |
| 4184 | xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 4185 | xmlXPathObjectPtr cur; |
| 4186 | int res = 0; |
| 4187 | |
| 4188 | CHECK_ARITY(1); |
| 4189 | cur = valuePop(ctxt); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4190 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4191 | switch (cur->type) { |
| 4192 | case XPATH_NODESET: |
Daniel Veillard | 0f2a53c | 2001-02-05 17:57:33 +0000 | [diff] [blame^] | 4193 | case XPATH_XSLT_TREE: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4194 | if ((cur->nodesetval == NULL) || |
| 4195 | (cur->nodesetval->nodeNr == 0)) res = 0; |
| 4196 | else |
| 4197 | res = 1; |
| 4198 | break; |
| 4199 | case XPATH_STRING: |
| 4200 | if ((cur->stringval == NULL) || |
| 4201 | (cur->stringval[0] == 0)) res = 0; |
| 4202 | else |
| 4203 | res = 1; |
| 4204 | break; |
| 4205 | case XPATH_BOOLEAN: |
| 4206 | valuePush(ctxt, cur); |
| 4207 | return; |
| 4208 | case XPATH_NUMBER: |
| 4209 | if (cur->floatval) res = 1; |
| 4210 | break; |
| 4211 | default: |
| 4212 | STRANGE |
| 4213 | } |
| 4214 | xmlXPathFreeObject(cur); |
| 4215 | valuePush(ctxt, xmlXPathNewBoolean(res)); |
| 4216 | } |
| 4217 | |
| 4218 | /** |
| 4219 | * xmlXPathNotFunction: |
| 4220 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4221 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4222 | * |
| 4223 | * Implement the not() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4224 | * boolean not(boolean) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4225 | * The not function returns true if its argument is false, |
| 4226 | * and false otherwise. |
| 4227 | */ |
| 4228 | void |
| 4229 | xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 4230 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4231 | CAST_TO_BOOLEAN; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4232 | CHECK_TYPE(XPATH_BOOLEAN); |
| 4233 | ctxt->value->boolval = ! ctxt->value->boolval; |
| 4234 | } |
| 4235 | |
| 4236 | /** |
| 4237 | * xmlXPathTrueFunction: |
| 4238 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4239 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4240 | * |
| 4241 | * Implement the true() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4242 | * boolean true() |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4243 | */ |
| 4244 | void |
| 4245 | xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 4246 | CHECK_ARITY(0); |
| 4247 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 4248 | } |
| 4249 | |
| 4250 | /** |
| 4251 | * xmlXPathFalseFunction: |
| 4252 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4253 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4254 | * |
| 4255 | * Implement the false() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4256 | * boolean false() |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4257 | */ |
| 4258 | void |
| 4259 | xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 4260 | CHECK_ARITY(0); |
| 4261 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 4262 | } |
| 4263 | |
| 4264 | /** |
| 4265 | * xmlXPathLangFunction: |
| 4266 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4267 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4268 | * |
| 4269 | * Implement the lang() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4270 | * boolean lang(string) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4271 | * The lang function returns true or false depending on whether the |
| 4272 | * language of the context node as specified by xml:lang attributes |
| 4273 | * is the same as or is a sublanguage of the language specified by |
| 4274 | * the argument string. The language of the context node is determined |
| 4275 | * by the value of the xml:lang attribute on the context node, or, if |
| 4276 | * the context node has no xml:lang attribute, by the value of the |
| 4277 | * xml:lang attribute on the nearest ancestor of the context node that |
| 4278 | * has an xml:lang attribute. If there is no such attribute, then lang |
| 4279 | * returns false. If there is such an attribute, then lang returns |
| 4280 | * true if the attribute value is equal to the argument ignoring case, |
| 4281 | * or if there is some suffix starting with - such that the attribute |
| 4282 | * value is equal to the argument ignoring that suffix of the attribute |
| 4283 | * value and ignoring case. |
| 4284 | */ |
| 4285 | void |
| 4286 | xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4287 | xmlXPathObjectPtr val; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4288 | const xmlChar *theLang; |
| 4289 | const xmlChar *lang; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4290 | int ret = 0; |
| 4291 | int i; |
| 4292 | |
| 4293 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4294 | CAST_TO_STRING; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4295 | CHECK_TYPE(XPATH_STRING); |
| 4296 | val = valuePop(ctxt); |
| 4297 | lang = val->stringval; |
| 4298 | theLang = xmlNodeGetLang(ctxt->context->node); |
| 4299 | if ((theLang != NULL) && (lang != NULL)) { |
| 4300 | for (i = 0;lang[i] != 0;i++) |
| 4301 | if (toupper(lang[i]) != toupper(theLang[i])) |
| 4302 | goto not_equal; |
| 4303 | ret = 1; |
| 4304 | } |
| 4305 | not_equal: |
| 4306 | xmlXPathFreeObject(val); |
| 4307 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | /** |
| 4311 | * xmlXPathNumberFunction: |
| 4312 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4313 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4314 | * |
| 4315 | * Implement the number() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4316 | * number number(object?) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4317 | */ |
| 4318 | void |
| 4319 | xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 4320 | xmlXPathObjectPtr cur; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4321 | double res; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4322 | |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4323 | if (nargs == 0) { |
| 4324 | if (ctxt->context->node == NULL) { |
| 4325 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 4326 | } else { |
| 4327 | xmlChar* content = xmlNodeGetContent(ctxt->context->node); |
| 4328 | |
| 4329 | res = xmlXPathStringEvalNumber(content); |
| 4330 | valuePush(ctxt, xmlXPathNewFloat(res)); |
| 4331 | xmlFree(content); |
| 4332 | } |
| 4333 | return; |
| 4334 | } |
| 4335 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4336 | CHECK_ARITY(1); |
| 4337 | cur = valuePop(ctxt); |
| 4338 | switch (cur->type) { |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 4339 | case XPATH_UNDEFINED: |
| 4340 | #ifdef DEBUG_EXPR |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 4341 | xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n"); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 4342 | #endif |
| 4343 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 4344 | break; |
Daniel Veillard | e456646 | 2001-01-22 09:58:39 +0000 | [diff] [blame] | 4345 | case XPATH_XSLT_TREE: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4346 | case XPATH_NODESET: |
| 4347 | valuePush(ctxt, cur); |
| 4348 | xmlXPathStringFunction(ctxt, 1); |
| 4349 | cur = valuePop(ctxt); |
| 4350 | case XPATH_STRING: |
| 4351 | res = xmlXPathStringEvalNumber(cur->stringval); |
| 4352 | valuePush(ctxt, xmlXPathNewFloat(res)); |
| 4353 | xmlXPathFreeObject(cur); |
| 4354 | return; |
| 4355 | case XPATH_BOOLEAN: |
| 4356 | if (cur->boolval) valuePush(ctxt, xmlXPathNewFloat(1.0)); |
| 4357 | else valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 4358 | xmlXPathFreeObject(cur); |
| 4359 | return; |
| 4360 | case XPATH_NUMBER: |
| 4361 | valuePush(ctxt, cur); |
| 4362 | return; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 4363 | case XPATH_USERS: |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 4364 | case XPATH_POINT: |
| 4365 | case XPATH_RANGE: |
| 4366 | case XPATH_LOCATIONSET: |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 4367 | TODO |
| 4368 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 4369 | break; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4370 | } |
| 4371 | STRANGE |
| 4372 | } |
| 4373 | |
| 4374 | /** |
| 4375 | * xmlXPathSumFunction: |
| 4376 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4377 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4378 | * |
| 4379 | * Implement the sum() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4380 | * number sum(node-set) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4381 | * The sum function returns the sum of the values of the nodes in |
| 4382 | * the argument node-set. |
| 4383 | */ |
| 4384 | void |
| 4385 | xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4386 | xmlXPathObjectPtr cur; |
| 4387 | int i; |
| 4388 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4389 | CHECK_ARITY(1); |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 4390 | if ((ctxt->value == NULL) || |
| 4391 | ((ctxt->value->type != XPATH_NODESET) && |
| 4392 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 4393 | XP_ERROR(XPATH_INVALID_TYPE); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4394 | cur = valuePop(ctxt); |
| 4395 | |
| 4396 | if (cur->nodesetval->nodeNr == 0) { |
| 4397 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 4398 | } else { |
| 4399 | valuePush(ctxt, |
| 4400 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[0])); |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4401 | xmlXPathNumberFunction(ctxt, 1); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4402 | for (i = 1; i < cur->nodesetval->nodeNr; i++) { |
| 4403 | valuePush(ctxt, |
| 4404 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i])); |
| 4405 | xmlXPathAddValues(ctxt); |
| 4406 | } |
| 4407 | } |
| 4408 | xmlXPathFreeObject(cur); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | /** |
| 4412 | * xmlXPathFloorFunction: |
| 4413 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4414 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4415 | * |
| 4416 | * Implement the floor() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4417 | * number floor(number) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4418 | * The floor function returns the largest (closest to positive infinity) |
| 4419 | * number that is not greater than the argument and that is an integer. |
| 4420 | */ |
| 4421 | void |
| 4422 | xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 4423 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4424 | CAST_TO_NUMBER; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4425 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4426 | #if 0 |
| 4427 | ctxt->value->floatval = floor(ctxt->value->floatval); |
| 4428 | #else |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4429 | /* floor(0.999999999999) => 1.0 !!!!!!!!!!! */ |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4430 | ctxt->value->floatval = (double)((int) ctxt->value->floatval); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4431 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | /** |
| 4435 | * xmlXPathCeilingFunction: |
| 4436 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4437 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4438 | * |
| 4439 | * Implement the ceiling() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4440 | * number ceiling(number) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4441 | * The ceiling function returns the smallest (closest to negative infinity) |
| 4442 | * number that is not less than the argument and that is an integer. |
| 4443 | */ |
| 4444 | void |
| 4445 | xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4446 | double f; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4447 | |
| 4448 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4449 | CAST_TO_NUMBER; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4450 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4451 | |
| 4452 | #if 0 |
| 4453 | ctxt->value->floatval = ceil(ctxt->value->floatval); |
| 4454 | #else |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4455 | f = (double)((int) ctxt->value->floatval); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4456 | if (f != ctxt->value->floatval) |
| 4457 | ctxt->value->floatval = f + 1; |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4458 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
| 4461 | /** |
| 4462 | * xmlXPathRoundFunction: |
| 4463 | * @ctxt: the XPath Parser context |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 4464 | * @nargs: the number of arguments |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4465 | * |
| 4466 | * Implement the round() XPath function |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4467 | * number round(number) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4468 | * The round function returns the number that is closest to the |
| 4469 | * argument and that is an integer. If there are two such numbers, |
| 4470 | * then the one that is even is returned. |
| 4471 | */ |
| 4472 | void |
| 4473 | xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4474 | double f; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4475 | |
| 4476 | CHECK_ARITY(1); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4477 | CAST_TO_NUMBER; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4478 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4479 | |
| 4480 | if ((ctxt->value->floatval == xmlXPathNAN) || |
| 4481 | (ctxt->value->floatval == xmlXPathPINF) || |
| 4482 | (ctxt->value->floatval == xmlXPathNINF) || |
| 4483 | (ctxt->value->floatval == 0.0)) |
| 4484 | return; |
| 4485 | |
| 4486 | #if 0 |
| 4487 | f = floor(ctxt->value->floatval); |
| 4488 | #else |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4489 | f = (double)((int) ctxt->value->floatval); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4490 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4491 | if (ctxt->value->floatval < f + 0.5) |
| 4492 | ctxt->value->floatval = f; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4493 | else |
| 4494 | ctxt->value->floatval = f + 1; |
| 4495 | } |
| 4496 | |
| 4497 | /************************************************************************ |
| 4498 | * * |
| 4499 | * The Parser * |
| 4500 | * * |
| 4501 | ************************************************************************/ |
| 4502 | |
| 4503 | /* |
| 4504 | * a couple of forward declarations since we use a recursive call based |
| 4505 | * implementation. |
| 4506 | */ |
| 4507 | void xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt); |
| 4508 | void xmlXPathEvalPredicate(xmlXPathParserContextPtr ctxt); |
| 4509 | void xmlXPathEvalLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 4510 | #ifdef VMS |
| 4511 | void xmlXPathEvalRelLocationPath(xmlXPathParserContextPtr ctxt); |
| 4512 | #define xmlXPathEvalRelativeLocationPath xmlXPathEvalRelLocationPath |
| 4513 | #else |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4514 | void xmlXPathEvalRelativeLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 4515 | #endif |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4516 | |
| 4517 | /** |
| 4518 | * xmlXPathParseNCName: |
| 4519 | * @ctxt: the XPath Parser context |
| 4520 | * |
| 4521 | * parse an XML namespace non qualified name. |
| 4522 | * |
| 4523 | * [NS 3] NCName ::= (Letter | '_') (NCNameChar)* |
| 4524 | * |
| 4525 | * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 4526 | * CombiningChar | Extender |
| 4527 | * |
| 4528 | * Returns the namespace name or NULL |
| 4529 | */ |
| 4530 | |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4531 | xmlChar * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4532 | xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4533 | const xmlChar *q; |
| 4534 | xmlChar *ret = NULL; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4535 | |
| 4536 | if (!IS_LETTER(CUR) && (CUR != '_')) return(NULL); |
| 4537 | q = NEXT; |
| 4538 | |
| 4539 | while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) || |
| 4540 | (CUR == '.') || (CUR == '-') || |
| 4541 | (CUR == '_') || |
| 4542 | (IS_COMBINING(CUR)) || |
| 4543 | (IS_EXTENDER(CUR))) |
| 4544 | NEXT; |
| 4545 | |
| 4546 | ret = xmlStrndup(q, CUR_PTR - q); |
| 4547 | |
| 4548 | return(ret); |
| 4549 | } |
| 4550 | |
| 4551 | /** |
| 4552 | * xmlXPathParseQName: |
| 4553 | * @ctxt: the XPath Parser context |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4554 | * @prefix: a xmlChar ** |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4555 | * |
| 4556 | * parse an XML qualified name |
| 4557 | * |
| 4558 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 4559 | * |
| 4560 | * [NS 6] Prefix ::= NCName |
| 4561 | * |
| 4562 | * [NS 7] LocalPart ::= NCName |
| 4563 | * |
| 4564 | * Returns the function returns the local part, and prefix is updated |
| 4565 | * to get the Prefix if any. |
| 4566 | */ |
| 4567 | |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4568 | xmlChar * |
| 4569 | xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) { |
| 4570 | xmlChar *ret = NULL; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4571 | |
| 4572 | *prefix = NULL; |
| 4573 | ret = xmlXPathParseNCName(ctxt); |
| 4574 | if (CUR == ':') { |
| 4575 | *prefix = ret; |
| 4576 | NEXT; |
| 4577 | ret = xmlXPathParseNCName(ctxt); |
| 4578 | } |
| 4579 | return(ret); |
| 4580 | } |
| 4581 | |
| 4582 | /** |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 4583 | * xmlXPathParseName: |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 4584 | * @ctxt: the XPath Parser context |
Daniel Veillard | 2d38f04 | 2000-10-11 10:54:10 +0000 | [diff] [blame] | 4585 | * |
| 4586 | * parse an XML name |
| 4587 | * |
| 4588 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 4589 | * CombiningChar | Extender |
| 4590 | * |
| 4591 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 4592 | * |
| 4593 | * Returns the namespace name or NULL |
| 4594 | */ |
| 4595 | |
| 4596 | xmlChar * |
| 4597 | xmlXPathParseName(xmlXPathParserContextPtr ctxt) { |
| 4598 | const xmlChar *q; |
| 4599 | xmlChar *ret = NULL; |
| 4600 | |
| 4601 | if (!IS_LETTER(CUR) && (CUR != '_')) return(NULL); |
| 4602 | q = NEXT; |
| 4603 | |
| 4604 | /* TODO Make this UTF8 compliant !!! */ |
| 4605 | while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) || |
| 4606 | (CUR == '.') || (CUR == '-') || |
| 4607 | (CUR == '_') || (CUR == ':') || |
| 4608 | (IS_COMBINING(CUR)) || |
| 4609 | (IS_EXTENDER(CUR))) |
| 4610 | NEXT; |
| 4611 | |
| 4612 | ret = xmlStrndup(q, CUR_PTR - q); |
| 4613 | |
| 4614 | return(ret); |
| 4615 | } |
| 4616 | |
| 4617 | /** |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4618 | * xmlXPathStringEvalNumber: |
| 4619 | * @str: A string to scan |
| 4620 | * |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4621 | * [30] Number ::= Digits ('.' Digits?)? |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4622 | * | '.' Digits |
| 4623 | * [31] Digits ::= [0-9]+ |
| 4624 | * |
| 4625 | * Parse and evaluate a Number in the string |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4626 | * In complement of the Number expression, this function also handles |
| 4627 | * negative values : '-' Number. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4628 | * |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4629 | * Returns the double value. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4630 | */ |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4631 | double |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4632 | xmlXPathStringEvalNumber(const xmlChar *str) { |
| 4633 | const xmlChar *cur = str; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4634 | double ret = 0.0; |
| 4635 | double mult = 1; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4636 | int ok = 0; |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4637 | int isneg = 0; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4638 | |
| 4639 | while (*cur == ' ') cur++; |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4640 | if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4641 | return(xmlXPathNAN); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4642 | } |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4643 | if (*cur == '-') { |
| 4644 | isneg = 1; |
| 4645 | cur++; |
| 4646 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4647 | while ((*cur >= '0') && (*cur <= '9')) { |
| 4648 | ret = ret * 10 + (*cur - '0'); |
| 4649 | ok = 1; |
| 4650 | cur++; |
| 4651 | } |
| 4652 | if (*cur == '.') { |
| 4653 | cur++; |
| 4654 | if (((*cur < '0') || (*cur > '9')) && (!ok)) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4655 | return(xmlXPathNAN); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4656 | } |
| 4657 | while ((*cur >= '0') && (*cur <= '9')) { |
| 4658 | mult /= 10; |
| 4659 | ret = ret + (*cur - '0') * mult; |
| 4660 | cur++; |
| 4661 | } |
| 4662 | } |
| 4663 | while (*cur == ' ') cur++; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4664 | if (*cur != 0) return(xmlXPathNAN); |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 4665 | if (isneg) ret = -ret; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4666 | return(ret); |
| 4667 | } |
| 4668 | |
| 4669 | /** |
| 4670 | * xmlXPathEvalNumber: |
| 4671 | * @ctxt: the XPath Parser context |
| 4672 | * |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 4673 | * [30] Number ::= Digits ('.' Digits?)? |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4674 | * | '.' Digits |
| 4675 | * [31] Digits ::= [0-9]+ |
| 4676 | * |
| 4677 | * Parse and evaluate a Number, then push it on the stack |
| 4678 | * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4679 | */ |
| 4680 | void |
| 4681 | xmlXPathEvalNumber(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 4682 | double ret = 0.0; |
| 4683 | double mult = 1; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4684 | int ok = 0; |
| 4685 | |
| 4686 | CHECK_ERROR; |
| 4687 | if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4688 | XP_ERROR(XPATH_NUMBER_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4689 | } |
| 4690 | while ((CUR >= '0') && (CUR <= '9')) { |
| 4691 | ret = ret * 10 + (CUR - '0'); |
| 4692 | ok = 1; |
| 4693 | NEXT; |
| 4694 | } |
| 4695 | if (CUR == '.') { |
| 4696 | NEXT; |
| 4697 | if (((CUR < '0') || (CUR > '9')) && (!ok)) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4698 | XP_ERROR(XPATH_NUMBER_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4699 | } |
| 4700 | while ((CUR >= '0') && (CUR <= '9')) { |
| 4701 | mult /= 10; |
| 4702 | ret = ret + (CUR - '0') * mult; |
| 4703 | NEXT; |
| 4704 | } |
| 4705 | } |
| 4706 | valuePush(ctxt, xmlXPathNewFloat(ret)); |
| 4707 | } |
| 4708 | |
| 4709 | /** |
| 4710 | * xmlXPathEvalLiteral: |
| 4711 | * @ctxt: the XPath Parser context |
| 4712 | * |
| 4713 | * Parse a Literal and push it on the stack. |
| 4714 | * |
| 4715 | * [29] Literal ::= '"' [^"]* '"' |
| 4716 | * | "'" [^']* "'" |
| 4717 | * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4718 | * TODO: xmlXPathEvalLiteral memory allocation could be improved. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4719 | */ |
| 4720 | void |
| 4721 | xmlXPathEvalLiteral(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4722 | const xmlChar *q; |
| 4723 | xmlChar *ret = NULL; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4724 | |
| 4725 | if (CUR == '"') { |
| 4726 | NEXT; |
| 4727 | q = CUR_PTR; |
| 4728 | while ((IS_CHAR(CUR)) && (CUR != '"')) |
| 4729 | NEXT; |
| 4730 | if (!IS_CHAR(CUR)) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4731 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4732 | } else { |
| 4733 | ret = xmlStrndup(q, CUR_PTR - q); |
| 4734 | NEXT; |
| 4735 | } |
| 4736 | } else if (CUR == '\'') { |
| 4737 | NEXT; |
| 4738 | q = CUR_PTR; |
| 4739 | while ((IS_CHAR(CUR)) && (CUR != '\'')) |
| 4740 | NEXT; |
| 4741 | if (!IS_CHAR(CUR)) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4742 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4743 | } else { |
| 4744 | ret = xmlStrndup(q, CUR_PTR - q); |
| 4745 | NEXT; |
| 4746 | } |
| 4747 | } else { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4748 | XP_ERROR(XPATH_START_LITERAL_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4749 | } |
| 4750 | if (ret == NULL) return; |
| 4751 | valuePush(ctxt, xmlXPathNewString(ret)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4752 | xmlFree(ret); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4753 | } |
| 4754 | |
| 4755 | /** |
| 4756 | * xmlXPathEvalVariableReference: |
| 4757 | * @ctxt: the XPath Parser context |
| 4758 | * |
| 4759 | * Parse a VariableReference, evaluate it and push it on the stack. |
| 4760 | * |
| 4761 | * The variable bindings consist of a mapping from variable names |
| 4762 | * to variable values. The value of a variable is an object, which |
| 4763 | * of any of the types that are possible for the value of an expression, |
| 4764 | * and may also be of additional types not specified here. |
| 4765 | * |
| 4766 | * Early evaluation is possible since: |
| 4767 | * The variable bindings [...] used to evaluate a subexpression are |
| 4768 | * always the same as those used to evaluate the containing expression. |
| 4769 | * |
| 4770 | * [36] VariableReference ::= '$' QName |
| 4771 | */ |
| 4772 | void |
| 4773 | xmlXPathEvalVariableReference(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4774 | xmlChar *name; |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4775 | xmlChar *prefix; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4776 | xmlXPathObjectPtr value; |
| 4777 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4778 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4779 | if (CUR != '$') { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4780 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4781 | } |
Daniel Veillard | 5a2b697 | 2001-01-20 21:15:50 +0000 | [diff] [blame] | 4782 | NEXT; |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4783 | name = xmlXPathParseQName(ctxt, &prefix); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4784 | if (name == NULL) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4785 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4786 | } |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4787 | if (prefix == NULL) { |
| 4788 | value = xmlXPathVariableLookup(ctxt->context, name); |
| 4789 | } else { |
| 4790 | TODO; |
| 4791 | value = NULL; |
| 4792 | } |
| 4793 | xmlFree(name); |
| 4794 | if (prefix != NULL) xmlFree(prefix); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4795 | if (value == NULL) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4796 | XP_ERROR(XPATH_UNDEF_VARIABLE_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4797 | } |
| 4798 | valuePush(ctxt, value); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4799 | SKIP_BLANKS; |
| 4800 | } |
| 4801 | |
| 4802 | /** |
| 4803 | * xmlXPathIsNodeType: |
| 4804 | * @ctxt: the XPath Parser context |
| 4805 | * @name: a name string |
| 4806 | * |
| 4807 | * Is the name given a NodeType one. |
| 4808 | * |
| 4809 | * [38] NodeType ::= 'comment' |
| 4810 | * | 'text' |
| 4811 | * | 'processing-instruction' |
| 4812 | * | 'node' |
| 4813 | * |
| 4814 | * Returns 1 if true 0 otherwise |
| 4815 | */ |
| 4816 | int |
| 4817 | xmlXPathIsNodeType(const xmlChar *name) { |
| 4818 | if (name == NULL) |
| 4819 | return(0); |
| 4820 | |
| 4821 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 4822 | return(1); |
| 4823 | if (xmlStrEqual(name, BAD_CAST "text")) |
| 4824 | return(1); |
| 4825 | if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 4826 | return(1); |
| 4827 | if (xmlStrEqual(name, BAD_CAST "node")) |
| 4828 | return(1); |
| 4829 | return(0); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4830 | } |
| 4831 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4832 | /** |
| 4833 | * xmlXPathEvalFunctionCall: |
| 4834 | * @ctxt: the XPath Parser context |
| 4835 | * |
| 4836 | * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')' |
| 4837 | * [17] Argument ::= Expr |
| 4838 | * |
| 4839 | * Parse and evaluate a function call, the evaluation of all arguments are |
| 4840 | * pushed on the stack |
| 4841 | */ |
| 4842 | void |
| 4843 | xmlXPathEvalFunctionCall(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4844 | xmlChar *name; |
| 4845 | xmlChar *prefix; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4846 | xmlXPathFunction func; |
| 4847 | int nbargs = 0; |
| 4848 | |
| 4849 | name = xmlXPathParseQName(ctxt, &prefix); |
| 4850 | if (name == NULL) { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4851 | XP_ERROR(XPATH_EXPR_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4852 | } |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4853 | SKIP_BLANKS; |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4854 | if (prefix == NULL) { |
| 4855 | func = xmlXPathFunctionLookup(ctxt->context, name); |
| 4856 | } else { |
| 4857 | TODO; |
| 4858 | func = NULL; |
| 4859 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4860 | if (func == NULL) { |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 4861 | xmlFree(name); |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4862 | if (prefix != NULL) xmlFree(prefix); |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4863 | XP_ERROR(XPATH_UNKNOWN_FUNC_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4864 | } |
| 4865 | #ifdef DEBUG_EXPR |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4866 | if (prefix == NULL) |
| 4867 | xmlGenericError(xmlGenericErrorContext, "Calling function %s\n", |
| 4868 | name); |
| 4869 | else |
| 4870 | xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n", |
| 4871 | prefix, name); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4872 | #endif |
| 4873 | |
Daniel Veillard | a5db68a | 2000-10-29 18:06:06 +0000 | [diff] [blame] | 4874 | xmlFree(name); |
| 4875 | if (prefix != NULL) xmlFree(prefix); |
| 4876 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4877 | if (CUR != '(') { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4878 | XP_ERROR(XPATH_EXPR_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4879 | } |
| 4880 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4881 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4882 | |
| 4883 | while (CUR != ')') { |
| 4884 | xmlXPathEvalExpr(ctxt); |
| 4885 | nbargs++; |
| 4886 | if (CUR == ')') break; |
| 4887 | if (CUR != ',') { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4888 | XP_ERROR(XPATH_EXPR_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4889 | } |
| 4890 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4891 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4892 | } |
| 4893 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4894 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4895 | func(ctxt, nbargs); |
| 4896 | } |
| 4897 | |
| 4898 | /** |
| 4899 | * xmlXPathEvalPrimaryExpr: |
| 4900 | * @ctxt: the XPath Parser context |
| 4901 | * |
| 4902 | * [15] PrimaryExpr ::= VariableReference |
| 4903 | * | '(' Expr ')' |
| 4904 | * | Literal |
| 4905 | * | Number |
| 4906 | * | FunctionCall |
| 4907 | * |
| 4908 | * Parse and evaluate a primary expression, then push the result on the stack |
| 4909 | */ |
| 4910 | void |
| 4911 | xmlXPathEvalPrimaryExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4912 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4913 | if (CUR == '$') xmlXPathEvalVariableReference(ctxt); |
| 4914 | else if (CUR == '(') { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4915 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4916 | SKIP_BLANKS; |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4917 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4918 | if (CUR != ')') { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 4919 | XP_ERROR(XPATH_EXPR_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4920 | } |
| 4921 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4922 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4923 | } else if (IS_DIGIT(CUR)) { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4924 | xmlXPathEvalNumber(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4925 | } else if ((CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4926 | xmlXPathEvalLiteral(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4927 | } else { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4928 | xmlXPathEvalFunctionCall(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4929 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4930 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4931 | } |
| 4932 | |
| 4933 | /** |
| 4934 | * xmlXPathEvalFilterExpr: |
| 4935 | * @ctxt: the XPath Parser context |
| 4936 | * |
| 4937 | * [20] FilterExpr ::= PrimaryExpr |
| 4938 | * | FilterExpr Predicate |
| 4939 | * |
| 4940 | * Parse and evaluate a filter expression, then push the result on the stack |
| 4941 | * Square brackets are used to filter expressions in the same way that |
| 4942 | * they are used in location paths. It is an error if the expression to |
| 4943 | * be filtered does not evaluate to a node-set. The context node list |
| 4944 | * used for evaluating the expression in square brackets is the node-set |
| 4945 | * to be filtered listed in document order. |
| 4946 | */ |
| 4947 | |
| 4948 | void |
| 4949 | xmlXPathEvalFilterExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4950 | xmlXPathEvalPrimaryExpr(ctxt); |
| 4951 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4952 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4953 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4954 | while (CUR == '[') { |
Daniel Veillard | f62ceff | 2000-11-24 23:36:01 +0000 | [diff] [blame] | 4955 | if ((ctxt->value == NULL) || |
| 4956 | ((ctxt->value->type != XPATH_NODESET) && |
| 4957 | (ctxt->value->type != XPATH_LOCATIONSET))) |
| 4958 | XP_ERROR(XPATH_INVALID_TYPE) |
| 4959 | |
| 4960 | if (ctxt->value->type == XPATH_NODESET) |
| 4961 | xmlXPathEvalPredicate(ctxt); |
| 4962 | else |
| 4963 | xmlXPtrEvalRangePredicate(ctxt); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4964 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | |
| 4968 | } |
| 4969 | |
| 4970 | /** |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4971 | * xmlXPathScanName: |
| 4972 | * @ctxt: the XPath Parser context |
| 4973 | * |
| 4974 | * Trickery: parse an XML name but without consuming the input flow |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 4975 | * Needed to avoid insanity in the parser state. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4976 | * |
| 4977 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 4978 | * CombiningChar | Extender |
| 4979 | * |
| 4980 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 4981 | * |
| 4982 | * [6] Names ::= Name (S Name)* |
| 4983 | * |
| 4984 | * Returns the Name parsed or NULL |
| 4985 | */ |
| 4986 | |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4987 | xmlChar * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4988 | xmlXPathScanName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 4989 | xmlChar buf[XML_MAX_NAMELEN]; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4990 | int len = 0; |
| 4991 | |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 4992 | SKIP_BLANKS; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 4993 | if (!IS_LETTER(CUR) && (CUR != '_') && |
| 4994 | (CUR != ':')) { |
| 4995 | return(NULL); |
| 4996 | } |
| 4997 | |
| 4998 | while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) || |
| 4999 | (NXT(len) == '.') || (NXT(len) == '-') || |
| 5000 | (NXT(len) == '_') || (NXT(len) == ':') || |
| 5001 | (IS_COMBINING(NXT(len))) || |
| 5002 | (IS_EXTENDER(NXT(len)))) { |
| 5003 | buf[len] = NXT(len); |
| 5004 | len++; |
| 5005 | if (len >= XML_MAX_NAMELEN) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5006 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5007 | "xmlScanName: reached XML_MAX_NAMELEN limit\n"); |
| 5008 | while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) || |
| 5009 | (NXT(len) == '.') || (NXT(len) == '-') || |
| 5010 | (NXT(len) == '_') || (NXT(len) == ':') || |
| 5011 | (IS_COMBINING(NXT(len))) || |
| 5012 | (IS_EXTENDER(NXT(len)))) |
| 5013 | len++; |
| 5014 | break; |
| 5015 | } |
| 5016 | } |
| 5017 | return(xmlStrndup(buf, len)); |
| 5018 | } |
| 5019 | |
| 5020 | /** |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5021 | * xmlXPathEvalPathExpr: |
| 5022 | * @ctxt: the XPath Parser context |
| 5023 | * |
| 5024 | * [19] PathExpr ::= LocationPath |
| 5025 | * | FilterExpr |
| 5026 | * | FilterExpr '/' RelativeLocationPath |
| 5027 | * | FilterExpr '//' RelativeLocationPath |
| 5028 | * |
| 5029 | * Parse and evaluate a path expression, then push the result on the stack |
| 5030 | * The / operator and // operators combine an arbitrary expression |
| 5031 | * and a relative location path. It is an error if the expression |
| 5032 | * does not evaluate to a node-set. |
| 5033 | * The / operator does composition in the same way as when / is |
| 5034 | * used in a location path. As in location paths, // is short for |
| 5035 | * /descendant-or-self::node()/. |
| 5036 | */ |
| 5037 | |
| 5038 | void |
| 5039 | xmlXPathEvalPathExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5040 | int lc = 1; /* Should we branch to LocationPath ? */ |
| 5041 | xmlChar *name = NULL; /* we may have to preparse a name to find out */ |
| 5042 | |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5043 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5044 | if ((CUR == '$') || (CUR == '(') || (IS_DIGIT(CUR)) || |
| 5045 | (CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5046 | lc = 0; |
| 5047 | } else if (CUR == '/') { |
| 5048 | /* relative or absolute location path */ |
| 5049 | lc = 1; |
| 5050 | } else if (CUR == '@') { |
| 5051 | /* relative abbreviated attribute location path */ |
| 5052 | lc = 1; |
| 5053 | } else if (CUR == '.') { |
| 5054 | /* relative abbreviated attribute location path */ |
| 5055 | lc = 1; |
| 5056 | } else { |
| 5057 | /* |
| 5058 | * Problem is finding if we have a name here whether it's: |
| 5059 | * - a nodetype |
| 5060 | * - a function call in which case it's followed by '(' |
| 5061 | * - an axis in which case it's followed by ':' |
| 5062 | * - a element name |
| 5063 | * We do an a priori analysis here rather than having to |
| 5064 | * maintain parsed token content through the recursive function |
| 5065 | * calls. This looks uglier but makes the code quite easier to |
| 5066 | * read/write/debug. |
| 5067 | */ |
| 5068 | SKIP_BLANKS; |
| 5069 | name = xmlXPathScanName(ctxt); |
| 5070 | if (name != NULL) { |
| 5071 | int len =xmlStrlen(name); |
| 5072 | int blank = 0; |
| 5073 | |
| 5074 | while (NXT(len) != 0) { |
| 5075 | if (NXT(len) == '/') { |
| 5076 | /* element name */ |
| 5077 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5078 | xmlGenericError(xmlGenericErrorContext, |
| 5079 | "PathExpr: AbbrRelLocation\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5080 | #endif |
| 5081 | lc = 1; |
| 5082 | break; |
| 5083 | } else if (IS_BLANK(NXT(len))) { |
| 5084 | /* skip to next */ |
| 5085 | blank = 1; |
| 5086 | } else if (NXT(len) == ':') { |
| 5087 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5088 | xmlGenericError(xmlGenericErrorContext, |
| 5089 | "PathExpr: AbbrRelLocation\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5090 | #endif |
| 5091 | lc = 1; |
| 5092 | break; |
| 5093 | } else if ((NXT(len) == '(')) { |
| 5094 | /* Note Type or Function */ |
| 5095 | if (xmlXPathIsNodeType(name)) { |
| 5096 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5097 | xmlGenericError(xmlGenericErrorContext, |
| 5098 | "PathExpr: Type search\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5099 | #endif |
| 5100 | lc = 1; |
| 5101 | } else { |
| 5102 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5103 | xmlGenericError(xmlGenericErrorContext, |
| 5104 | "PathExpr: function call\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5105 | #endif |
| 5106 | lc = 0; |
| 5107 | } |
| 5108 | break; |
| 5109 | } else if ((NXT(len) == '[')) { |
| 5110 | /* element name */ |
| 5111 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5112 | xmlGenericError(xmlGenericErrorContext, |
| 5113 | "PathExpr: AbbrRelLocation\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5114 | #endif |
| 5115 | lc = 1; |
| 5116 | break; |
Daniel Veillard | 389e6b7 | 2001-01-15 19:41:13 +0000 | [diff] [blame] | 5117 | } else if ((NXT(len) == '<') || (NXT(len) == '>') || |
| 5118 | (NXT(len) == '=')) { |
| 5119 | lc = 1; |
| 5120 | break; |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5121 | } else { |
Daniel Veillard | f6eea27 | 2001-01-18 12:17:12 +0000 | [diff] [blame] | 5122 | lc = 1; |
| 5123 | break; |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5124 | } |
| 5125 | len++; |
| 5126 | } |
| 5127 | if (NXT(len) == 0) { |
| 5128 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5129 | xmlGenericError(xmlGenericErrorContext, |
| 5130 | "PathExpr: AbbrRelLocation\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5131 | #endif |
| 5132 | /* element name */ |
| 5133 | lc = 1; |
| 5134 | } |
| 5135 | xmlFree(name); |
| 5136 | } else { |
| 5137 | /* make sure all cases are covered explicitely */ |
| 5138 | XP_ERROR(XPATH_EXPR_ERROR); |
| 5139 | } |
| 5140 | } |
| 5141 | |
| 5142 | if (lc) { |
Daniel Veillard | c4f4f0b | 2000-10-29 17:46:30 +0000 | [diff] [blame] | 5143 | if (CUR == '/') |
| 5144 | xmlXPathRoot(ctxt); |
Daniel Veillard | 5a2b697 | 2001-01-20 21:15:50 +0000 | [diff] [blame] | 5145 | else { |
| 5146 | /* TAG:9999 */ |
| 5147 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 5148 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5149 | xmlXPathEvalLocationPath(ctxt); |
| 5150 | } else { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5151 | xmlXPathEvalFilterExpr(ctxt); |
| 5152 | CHECK_ERROR; |
| 5153 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 5154 | SKIP(2); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5155 | SKIP_BLANKS; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5156 | xmlXPathNodeCollectAndTest(ctxt, AXIS_DESCENDANT_OR_SELF, |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 5157 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5158 | ctxt->context->node = NULL; |
| 5159 | xmlXPathEvalRelativeLocationPath(ctxt); |
| 5160 | } else if (CUR == '/') { |
| 5161 | xmlXPathEvalRelativeLocationPath(ctxt); |
| 5162 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5163 | } |
| 5164 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
| 5167 | /** |
| 5168 | * xmlXPathEvalUnionExpr: |
| 5169 | * @ctxt: the XPath Parser context |
| 5170 | * |
| 5171 | * [18] UnionExpr ::= PathExpr |
| 5172 | * | UnionExpr '|' PathExpr |
| 5173 | * |
| 5174 | * Parse and evaluate an union expression, then push the result on the stack |
| 5175 | */ |
| 5176 | |
| 5177 | void |
| 5178 | xmlXPathEvalUnionExpr(xmlXPathParserContextPtr ctxt) { |
| 5179 | xmlXPathEvalPathExpr(ctxt); |
| 5180 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5181 | SKIP_BLANKS; |
Daniel Veillard | 0810898 | 2001-01-03 15:24:58 +0000 | [diff] [blame] | 5182 | while (CUR == '|') { |
| 5183 | xmlXPathObjectPtr obj1,obj2, tmp; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5184 | |
| 5185 | CHECK_TYPE(XPATH_NODESET); |
| 5186 | obj1 = valuePop(ctxt); |
Daniel Veillard | 0810898 | 2001-01-03 15:24:58 +0000 | [diff] [blame] | 5187 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 5188 | valuePush(ctxt, tmp); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5189 | |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5190 | NEXT; |
| 5191 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5192 | xmlXPathEvalPathExpr(ctxt); |
| 5193 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5194 | CHECK_TYPE(XPATH_NODESET); |
| 5195 | obj2 = valuePop(ctxt); |
| 5196 | obj1->nodesetval = xmlXPathNodeSetMerge(obj1->nodesetval, |
| 5197 | obj2->nodesetval); |
Daniel Veillard | 0810898 | 2001-01-03 15:24:58 +0000 | [diff] [blame] | 5198 | if (ctxt->value == tmp) { |
| 5199 | tmp = valuePop(ctxt); |
| 5200 | xmlXPathFreeObject(tmp); |
| 5201 | } |
Daniel Veillard | 9e8bfae | 2000-11-06 16:43:11 +0000 | [diff] [blame] | 5202 | valuePush(ctxt, obj1); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5203 | xmlXPathFreeObject(obj2); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5204 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | /** |
| 5209 | * xmlXPathEvalUnaryExpr: |
| 5210 | * @ctxt: the XPath Parser context |
| 5211 | * |
| 5212 | * [27] UnaryExpr ::= UnionExpr |
| 5213 | * | '-' UnaryExpr |
| 5214 | * |
| 5215 | * Parse and evaluate an unary expression, then push the result on the stack |
| 5216 | */ |
| 5217 | |
| 5218 | void |
| 5219 | xmlXPathEvalUnaryExpr(xmlXPathParserContextPtr ctxt) { |
| 5220 | int minus = 0; |
| 5221 | |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5222 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5223 | if (CUR == '-') { |
| 5224 | minus = 1; |
| 5225 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5226 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5227 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5228 | xmlXPathEvalUnionExpr(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5229 | CHECK_ERROR; |
| 5230 | if (minus) { |
| 5231 | xmlXPathValueFlipSign(ctxt); |
| 5232 | } |
| 5233 | } |
| 5234 | |
| 5235 | /** |
| 5236 | * xmlXPathEvalMultiplicativeExpr: |
| 5237 | * @ctxt: the XPath Parser context |
| 5238 | * |
| 5239 | * [26] MultiplicativeExpr ::= UnaryExpr |
| 5240 | * | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 5241 | * | MultiplicativeExpr 'div' UnaryExpr |
| 5242 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 5243 | * [34] MultiplyOperator ::= '*' |
| 5244 | * |
| 5245 | * Parse and evaluate an Additive expression, then push the result on the stack |
| 5246 | */ |
| 5247 | |
| 5248 | void |
| 5249 | xmlXPathEvalMultiplicativeExpr(xmlXPathParserContextPtr ctxt) { |
| 5250 | xmlXPathEvalUnaryExpr(ctxt); |
| 5251 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5252 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5253 | while ((CUR == '*') || |
| 5254 | ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) || |
| 5255 | ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) { |
| 5256 | int op = -1; |
| 5257 | |
| 5258 | if (CUR == '*') { |
| 5259 | op = 0; |
| 5260 | NEXT; |
| 5261 | } else if (CUR == 'd') { |
| 5262 | op = 1; |
| 5263 | SKIP(3); |
| 5264 | } else if (CUR == 'm') { |
| 5265 | op = 2; |
| 5266 | SKIP(3); |
| 5267 | } |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5268 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5269 | xmlXPathEvalUnaryExpr(ctxt); |
| 5270 | CHECK_ERROR; |
| 5271 | switch (op) { |
| 5272 | case 0: |
| 5273 | xmlXPathMultValues(ctxt); |
| 5274 | break; |
| 5275 | case 1: |
| 5276 | xmlXPathDivValues(ctxt); |
| 5277 | break; |
| 5278 | case 2: |
| 5279 | xmlXPathModValues(ctxt); |
| 5280 | break; |
| 5281 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5282 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5283 | } |
| 5284 | } |
| 5285 | |
| 5286 | /** |
| 5287 | * xmlXPathEvalAdditiveExpr: |
| 5288 | * @ctxt: the XPath Parser context |
| 5289 | * |
| 5290 | * [25] AdditiveExpr ::= MultiplicativeExpr |
| 5291 | * | AdditiveExpr '+' MultiplicativeExpr |
| 5292 | * | AdditiveExpr '-' MultiplicativeExpr |
| 5293 | * |
| 5294 | * Parse and evaluate an Additive expression, then push the result on the stack |
| 5295 | */ |
| 5296 | |
| 5297 | void |
| 5298 | xmlXPathEvalAdditiveExpr(xmlXPathParserContextPtr ctxt) { |
| 5299 | xmlXPathEvalMultiplicativeExpr(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 == '+') || (CUR == '-')) { |
| 5303 | int plus; |
| 5304 | |
| 5305 | if (CUR == '+') plus = 1; |
| 5306 | else plus = 0; |
| 5307 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5308 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5309 | xmlXPathEvalMultiplicativeExpr(ctxt); |
| 5310 | CHECK_ERROR; |
| 5311 | if (plus) xmlXPathAddValues(ctxt); |
| 5312 | else xmlXPathSubValues(ctxt); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5313 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5314 | } |
| 5315 | } |
| 5316 | |
| 5317 | /** |
| 5318 | * xmlXPathEvalRelationalExpr: |
| 5319 | * @ctxt: the XPath Parser context |
| 5320 | * |
| 5321 | * [24] RelationalExpr ::= AdditiveExpr |
| 5322 | * | RelationalExpr '<' AdditiveExpr |
| 5323 | * | RelationalExpr '>' AdditiveExpr |
| 5324 | * | RelationalExpr '<=' AdditiveExpr |
| 5325 | * | RelationalExpr '>=' AdditiveExpr |
| 5326 | * |
| 5327 | * A <= B > C is allowed ? Answer from James, yes with |
| 5328 | * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr |
| 5329 | * which is basically what got implemented. |
| 5330 | * |
| 5331 | * Parse and evaluate a Relational expression, then push the result |
| 5332 | * on the stack |
| 5333 | */ |
| 5334 | |
| 5335 | void |
| 5336 | xmlXPathEvalRelationalExpr(xmlXPathParserContextPtr ctxt) { |
| 5337 | xmlXPathEvalAdditiveExpr(ctxt); |
| 5338 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5339 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5340 | while ((CUR == '<') || |
| 5341 | (CUR == '>') || |
| 5342 | ((CUR == '<') && (NXT(1) == '=')) || |
| 5343 | ((CUR == '>') && (NXT(1) == '='))) { |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 5344 | int inf, strict, ret; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5345 | |
| 5346 | if (CUR == '<') inf = 1; |
| 5347 | else inf = 0; |
| 5348 | if (NXT(1) == '=') strict = 0; |
| 5349 | else strict = 1; |
| 5350 | NEXT; |
| 5351 | if (!strict) NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5352 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5353 | xmlXPathEvalAdditiveExpr(ctxt); |
| 5354 | CHECK_ERROR; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 5355 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 5356 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5357 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5358 | } |
| 5359 | } |
| 5360 | |
| 5361 | /** |
| 5362 | * xmlXPathEvalEqualityExpr: |
| 5363 | * @ctxt: the XPath Parser context |
| 5364 | * |
| 5365 | * [23] EqualityExpr ::= RelationalExpr |
| 5366 | * | EqualityExpr '=' RelationalExpr |
| 5367 | * | EqualityExpr '!=' RelationalExpr |
| 5368 | * |
| 5369 | * A != B != C is allowed ? Answer from James, yes with |
| 5370 | * (RelationalExpr = RelationalExpr) = RelationalExpr |
| 5371 | * (RelationalExpr != RelationalExpr) != RelationalExpr |
| 5372 | * which is basically what got implemented. |
| 5373 | * |
| 5374 | * Parse and evaluate an Equality expression, then push the result on the stack |
| 5375 | * |
| 5376 | */ |
| 5377 | void |
| 5378 | xmlXPathEvalEqualityExpr(xmlXPathParserContextPtr ctxt) { |
| 5379 | xmlXPathEvalRelationalExpr(ctxt); |
| 5380 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5381 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5382 | while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) { |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 5383 | xmlXPathObjectPtr res; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5384 | int eq, equal; |
| 5385 | |
| 5386 | if (CUR == '=') eq = 1; |
| 5387 | else eq = 0; |
| 5388 | NEXT; |
| 5389 | if (!eq) NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5390 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5391 | xmlXPathEvalRelationalExpr(ctxt); |
| 5392 | CHECK_ERROR; |
Daniel Veillard | 991e63d | 1999-08-15 23:32:28 +0000 | [diff] [blame] | 5393 | equal = xmlXPathEqualValues(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5394 | if (eq) res = xmlXPathNewBoolean(equal); |
| 5395 | else res = xmlXPathNewBoolean(!equal); |
| 5396 | valuePush(ctxt, res); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5397 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5398 | } |
| 5399 | } |
| 5400 | |
| 5401 | /** |
| 5402 | * xmlXPathEvalAndExpr: |
| 5403 | * @ctxt: the XPath Parser context |
| 5404 | * |
| 5405 | * [22] AndExpr ::= EqualityExpr |
| 5406 | * | AndExpr 'and' EqualityExpr |
| 5407 | * |
| 5408 | * Parse and evaluate an AND expression, then push the result on the stack |
| 5409 | * |
| 5410 | */ |
| 5411 | void |
| 5412 | xmlXPathEvalAndExpr(xmlXPathParserContextPtr ctxt) { |
| 5413 | xmlXPathEvalEqualityExpr(ctxt); |
| 5414 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5415 | SKIP_BLANKS; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5416 | while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5417 | xmlXPathObjectPtr arg1, arg2; |
| 5418 | |
| 5419 | SKIP(3); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5420 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5421 | xmlXPathEvalEqualityExpr(ctxt); |
| 5422 | CHECK_ERROR; |
Daniel Veillard | 0f2a53c | 2001-02-05 17:57:33 +0000 | [diff] [blame^] | 5423 | xmlXPathBooleanFunction(ctxt, 1); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5424 | arg2 = valuePop(ctxt); |
Daniel Veillard | 0f2a53c | 2001-02-05 17:57:33 +0000 | [diff] [blame^] | 5425 | xmlXPathBooleanFunction(ctxt, 1); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5426 | arg1 = valuePop(ctxt); |
| 5427 | arg1->boolval &= arg2->boolval; |
| 5428 | valuePush(ctxt, arg1); |
| 5429 | xmlXPathFreeObject(arg2); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5430 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5431 | } |
| 5432 | } |
| 5433 | |
| 5434 | /** |
| 5435 | * xmlXPathEvalExpr: |
| 5436 | * @ctxt: the XPath Parser context |
| 5437 | * |
| 5438 | * [14] Expr ::= OrExpr |
| 5439 | * [21] OrExpr ::= AndExpr |
| 5440 | * | OrExpr 'or' AndExpr |
| 5441 | * |
| 5442 | * Parse and evaluate an expression, then push the result on the stack |
| 5443 | * |
| 5444 | */ |
| 5445 | void |
| 5446 | xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) { |
| 5447 | xmlXPathEvalAndExpr(ctxt); |
| 5448 | CHECK_ERROR; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5449 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5450 | while ((CUR == 'o') && (NXT(1) == 'r')) { |
| 5451 | xmlXPathObjectPtr arg1, arg2; |
| 5452 | |
| 5453 | SKIP(2); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5454 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5455 | xmlXPathEvalAndExpr(ctxt); |
| 5456 | CHECK_ERROR; |
Daniel Veillard | 0f2a53c | 2001-02-05 17:57:33 +0000 | [diff] [blame^] | 5457 | xmlXPathBooleanFunction(ctxt, 1); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5458 | arg2 = valuePop(ctxt); |
Daniel Veillard | 0f2a53c | 2001-02-05 17:57:33 +0000 | [diff] [blame^] | 5459 | xmlXPathBooleanFunction(ctxt, 1); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5460 | arg1 = valuePop(ctxt); |
| 5461 | arg1->boolval |= arg2->boolval; |
| 5462 | valuePush(ctxt, arg1); |
| 5463 | xmlXPathFreeObject(arg2); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5464 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5465 | } |
| 5466 | } |
| 5467 | |
| 5468 | /** |
| 5469 | * xmlXPathEvaluatePredicateResult: |
| 5470 | * @ctxt: the XPath Parser context |
| 5471 | * @res: the Predicate Expression evaluation result |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5472 | * |
| 5473 | * Evaluate a predicate result for the current node. |
| 5474 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 5475 | * the result to a boolean. If the result is a number, the result will |
| 5476 | * be converted to true if the number is equal to the position of the |
| 5477 | * context node in the context node list (as returned by the position |
| 5478 | * function) and will be converted to false otherwise; if the result |
| 5479 | * is not a number, then the result will be converted as if by a call |
| 5480 | * to the boolean function. |
Daniel Veillard | 2c833b6 | 2001-02-03 08:52:06 +0000 | [diff] [blame] | 5481 | * |
| 5482 | * Return 1 if predicate is true, 0 otherwise |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5483 | */ |
| 5484 | int |
| 5485 | xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5486 | xmlXPathObjectPtr res) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5487 | if (res == NULL) return(0); |
| 5488 | switch (res->type) { |
| 5489 | case XPATH_BOOLEAN: |
| 5490 | return(res->boolval); |
| 5491 | case XPATH_NUMBER: |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5492 | return(res->floatval == ctxt->context->proximityPosition); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5493 | case XPATH_NODESET: |
Daniel Veillard | e99a476 | 2001-02-01 04:34:35 +0000 | [diff] [blame] | 5494 | case XPATH_XSLT_TREE: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5495 | return(res->nodesetval->nodeNr != 0); |
| 5496 | case XPATH_STRING: |
| 5497 | return((res->stringval != NULL) && |
| 5498 | (xmlStrlen(res->stringval) != 0)); |
| 5499 | default: |
| 5500 | STRANGE |
| 5501 | } |
| 5502 | return(0); |
| 5503 | } |
| 5504 | |
| 5505 | /** |
| 5506 | * xmlXPathEvalPredicate: |
| 5507 | * @ctxt: the XPath Parser context |
| 5508 | * |
| 5509 | * [8] Predicate ::= '[' PredicateExpr ']' |
| 5510 | * [9] PredicateExpr ::= Expr |
| 5511 | * |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5512 | * --------------------- |
| 5513 | * For each node in the node-set to be filtered, the PredicateExpr is |
| 5514 | * evaluated with that node as the context node, with the number of nodes |
| 5515 | * in the node-set as the context size, and with the proximity position |
| 5516 | * of the node in the node-set with respect to the axis as the context |
| 5517 | * position; if PredicateExpr evaluates to true for that node, the node |
| 5518 | * is included in the new node-set; otherwise, it is not included. |
| 5519 | * --------------------- |
| 5520 | * |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5521 | * Parse and evaluate a predicate for all the elements of the |
| 5522 | * current node list. Then refine the list by removing all |
| 5523 | * nodes where the predicate is false. |
| 5524 | */ |
| 5525 | void |
| 5526 | xmlXPathEvalPredicate(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 5527 | const xmlChar *cur; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5528 | xmlXPathObjectPtr res; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5529 | xmlXPathObjectPtr obj, tmp; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5530 | xmlNodeSetPtr newset = NULL; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5531 | xmlNodeSetPtr oldset; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5532 | int i; |
| 5533 | |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5534 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5535 | if (CUR != '[') { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 5536 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5537 | } |
| 5538 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5539 | SKIP_BLANKS; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5540 | |
| 5541 | /* |
| 5542 | * Extract the old set, and then evaluate the result of the |
| 5543 | * expression for all the element in the set. use it to grow |
| 5544 | * up a new set. |
| 5545 | */ |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5546 | CHECK_TYPE(XPATH_NODESET); |
| 5547 | obj = valuePop(ctxt); |
| 5548 | oldset = obj->nodesetval; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5549 | ctxt->context->node = NULL; |
| 5550 | |
| 5551 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5552 | ctxt->context->contextSize = 0; |
| 5553 | ctxt->context->proximityPosition = 0; |
Daniel Veillard | ff9c330 | 2000-10-13 16:38:25 +0000 | [diff] [blame] | 5554 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5555 | res = valuePop(ctxt); |
| 5556 | if (res != NULL) |
| 5557 | xmlXPathFreeObject(res); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5558 | valuePush(ctxt, obj); |
Daniel Veillard | ff9c330 | 2000-10-13 16:38:25 +0000 | [diff] [blame] | 5559 | CHECK_ERROR; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5560 | } else { |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5561 | /* |
| 5562 | * Save the expression pointer since we will have to evaluate |
| 5563 | * it multiple times. Initialize the new set. |
| 5564 | */ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5565 | cur = ctxt->cur; |
| 5566 | newset = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5567 | |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5568 | for (i = 0; i < oldset->nodeNr; i++) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5569 | ctxt->cur = cur; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5570 | |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5571 | /* |
| 5572 | * Run the evaluation with a node list made of a single item |
| 5573 | * in the nodeset. |
| 5574 | */ |
| 5575 | ctxt->context->node = oldset->nodeTab[i]; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5576 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 5577 | valuePush(ctxt, tmp); |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5578 | ctxt->context->contextSize = oldset->nodeNr; |
| 5579 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5580 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5581 | xmlXPathEvalExpr(ctxt); |
| 5582 | CHECK_ERROR; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5583 | |
| 5584 | /* |
| 5585 | * The result of the evaluation need to be tested to |
| 5586 | * decided whether the filter succeeded or not |
| 5587 | */ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5588 | res = valuePop(ctxt); |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5589 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 5590 | xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); |
| 5591 | } |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5592 | |
| 5593 | /* |
| 5594 | * Cleanup |
| 5595 | */ |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5596 | if (res != NULL) |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5597 | xmlXPathFreeObject(res); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5598 | if (ctxt->value == tmp) { |
| 5599 | res = valuePop(ctxt); |
| 5600 | xmlXPathFreeObject(res); |
| 5601 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 5602 | |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5603 | ctxt->context->node = NULL; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5604 | } |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5605 | |
| 5606 | /* |
| 5607 | * The result is used as the new evaluation set. |
| 5608 | */ |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5609 | xmlXPathFreeObject(obj); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5610 | ctxt->context->node = NULL; |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5611 | ctxt->context->contextSize = -1; |
| 5612 | ctxt->context->proximityPosition = -1; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5613 | valuePush(ctxt, xmlXPathWrapNodeSet(newset)); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5614 | } |
| 5615 | if (CUR != ']') { |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 5616 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5617 | } |
Daniel Veillard | f09e7e3 | 2000-10-01 15:53:30 +0000 | [diff] [blame] | 5618 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5619 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5620 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5621 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5622 | xmlGenericError(xmlGenericErrorContext, "After predicate : "); |
| 5623 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 5624 | ctxt->value->nodesetval); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5625 | #endif |
| 5626 | } |
| 5627 | |
| 5628 | /** |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5629 | * xmlXPathEvalNodeTest: |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5630 | * @ctxt: the XPath Parser context |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5631 | * @test: pointer to a xmlXPathTestVal |
| 5632 | * @type: pointer to a xmlXPathTypeVal |
| 5633 | * @prefix: placeholder for a possible name prefix |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5634 | * |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5635 | * [7] NodeTest ::= NameTest |
| 5636 | * | NodeType '(' ')' |
| 5637 | * | 'processing-instruction' '(' Literal ')' |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5638 | * |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5639 | * [37] NameTest ::= '*' |
| 5640 | * | NCName ':' '*' |
| 5641 | * | QName |
| 5642 | * [38] NodeType ::= 'comment' |
| 5643 | * | 'text' |
| 5644 | * | 'processing-instruction' |
| 5645 | * | 'node' |
| 5646 | * |
| 5647 | * Returns the name found and update @test, @type and @prefix appropriately |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5648 | */ |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5649 | xmlChar * |
| 5650 | xmlXPathEvalNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test, |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5651 | xmlXPathTypeVal *type, const xmlChar **prefix, xmlChar *name) { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5652 | int blanks; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5653 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5654 | if ((test == NULL) || (type == NULL) || (prefix == NULL)) { |
| 5655 | STRANGE; |
| 5656 | return(NULL); |
| 5657 | } |
| 5658 | *type = 0; |
| 5659 | *test = 0; |
| 5660 | *prefix = NULL; |
| 5661 | SKIP_BLANKS; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 5662 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5663 | if ((name == NULL) && (CUR == '*')) { |
| 5664 | /* |
| 5665 | * All elements |
| 5666 | */ |
| 5667 | NEXT; |
| 5668 | *test = NODE_TEST_ALL; |
| 5669 | return(NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5670 | } |
| 5671 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5672 | if (name == NULL) |
| 5673 | name = xmlXPathParseNCName(ctxt); |
| 5674 | if (name == NULL) { |
| 5675 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 5676 | } |
| 5677 | |
| 5678 | blanks = IS_BLANK(CUR); |
| 5679 | SKIP_BLANKS; |
| 5680 | if (CUR == '(') { |
| 5681 | NEXT; |
| 5682 | /* |
| 5683 | * NodeType or PI search |
| 5684 | */ |
| 5685 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 5686 | *type = NODE_TYPE_COMMENT; |
| 5687 | else if (xmlStrEqual(name, BAD_CAST "node")) |
| 5688 | *type = NODE_TYPE_NODE; |
| 5689 | else if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 5690 | *type = NODE_TYPE_PI; |
| 5691 | else if (xmlStrEqual(name, BAD_CAST "text")) |
| 5692 | *type = NODE_TYPE_TEXT; |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5693 | else { |
| 5694 | if (name != NULL) |
| 5695 | xmlFree(name); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5696 | XP_ERROR0(XPATH_EXPR_ERROR); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5697 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5698 | |
| 5699 | *test = NODE_TEST_TYPE; |
| 5700 | |
| 5701 | SKIP_BLANKS; |
| 5702 | if (*type == NODE_TYPE_PI) { |
| 5703 | /* |
| 5704 | * Specific case: search a PI by name. |
| 5705 | */ |
| 5706 | xmlXPathObjectPtr cur; |
| 5707 | |
| 5708 | if (name != NULL) |
| 5709 | xmlFree(name); |
| 5710 | |
| 5711 | xmlXPathEvalLiteral(ctxt); |
| 5712 | CHECK_ERROR 0; |
| 5713 | xmlXPathStringFunction(ctxt, 1); |
| 5714 | CHECK_ERROR0; |
| 5715 | cur = valuePop(ctxt); |
| 5716 | name = xmlStrdup(cur->stringval); |
| 5717 | xmlXPathFreeObject(cur); |
| 5718 | SKIP_BLANKS; |
| 5719 | } |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5720 | if (CUR != ')') { |
| 5721 | if (name != NULL) |
| 5722 | xmlFree(name); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5723 | XP_ERROR0(XPATH_UNCLOSED_ERROR); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5724 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5725 | NEXT; |
| 5726 | return(name); |
| 5727 | } |
| 5728 | *test = NODE_TEST_NAME; |
| 5729 | if ((!blanks) && (CUR == ':')) { |
| 5730 | NEXT; |
| 5731 | |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5732 | /* |
| 5733 | * get the namespace name for this prefix |
| 5734 | */ |
| 5735 | *prefix = xmlXPathNsLookup(ctxt->context, name); |
| 5736 | if (name != NULL) |
| 5737 | xmlFree(name); |
| 5738 | if (*prefix == NULL) { |
| 5739 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 5740 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5741 | |
| 5742 | if (CUR == '*') { |
| 5743 | /* |
| 5744 | * All elements |
| 5745 | */ |
| 5746 | NEXT; |
| 5747 | *test = NODE_TEST_ALL; |
| 5748 | return(NULL); |
| 5749 | } |
| 5750 | |
| 5751 | name = xmlXPathParseNCName(ctxt); |
| 5752 | if (name == NULL) { |
| 5753 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 5754 | } |
| 5755 | } |
| 5756 | return(name); |
| 5757 | } |
| 5758 | |
| 5759 | /** |
| 5760 | * xmlXPathIsAxisName: |
| 5761 | * @name: a preparsed name token |
| 5762 | * |
| 5763 | * [6] AxisName ::= 'ancestor' |
| 5764 | * | 'ancestor-or-self' |
| 5765 | * | 'attribute' |
| 5766 | * | 'child' |
| 5767 | * | 'descendant' |
| 5768 | * | 'descendant-or-self' |
| 5769 | * | 'following' |
| 5770 | * | 'following-sibling' |
| 5771 | * | 'namespace' |
| 5772 | * | 'parent' |
| 5773 | * | 'preceding' |
| 5774 | * | 'preceding-sibling' |
| 5775 | * | 'self' |
| 5776 | * |
| 5777 | * Returns the axis or 0 |
| 5778 | */ |
| 5779 | xmlXPathAxisVal |
| 5780 | xmlXPathIsAxisName(const xmlChar *name) { |
| 5781 | xmlXPathAxisVal ret = 0; |
| 5782 | switch (name[0]) { |
| 5783 | case 'a': |
| 5784 | if (xmlStrEqual(name, BAD_CAST "ancestor")) |
| 5785 | ret = AXIS_ANCESTOR; |
| 5786 | if (xmlStrEqual(name, BAD_CAST "ancestor-or-self")) |
| 5787 | ret = AXIS_ANCESTOR_OR_SELF; |
| 5788 | if (xmlStrEqual(name, BAD_CAST "attribute")) |
| 5789 | ret = AXIS_ATTRIBUTE; |
| 5790 | break; |
| 5791 | case 'c': |
| 5792 | if (xmlStrEqual(name, BAD_CAST "child")) |
| 5793 | ret = AXIS_CHILD; |
| 5794 | break; |
| 5795 | case 'd': |
| 5796 | if (xmlStrEqual(name, BAD_CAST "descendant")) |
| 5797 | ret = AXIS_DESCENDANT; |
| 5798 | if (xmlStrEqual(name, BAD_CAST "descendant-or-self")) |
| 5799 | ret = AXIS_DESCENDANT_OR_SELF; |
| 5800 | break; |
| 5801 | case 'f': |
| 5802 | if (xmlStrEqual(name, BAD_CAST "following")) |
| 5803 | ret = AXIS_FOLLOWING; |
| 5804 | if (xmlStrEqual(name, BAD_CAST "following-sibling")) |
| 5805 | ret = AXIS_FOLLOWING_SIBLING; |
| 5806 | break; |
| 5807 | case 'n': |
| 5808 | if (xmlStrEqual(name, BAD_CAST "namespace")) |
| 5809 | ret = AXIS_NAMESPACE; |
| 5810 | break; |
| 5811 | case 'p': |
| 5812 | if (xmlStrEqual(name, BAD_CAST "parent")) |
| 5813 | ret = AXIS_PARENT; |
| 5814 | if (xmlStrEqual(name, BAD_CAST "preceding")) |
| 5815 | ret = AXIS_PRECEDING; |
| 5816 | if (xmlStrEqual(name, BAD_CAST "preceding-sibling")) |
| 5817 | ret = AXIS_PRECEDING_SIBLING; |
| 5818 | break; |
| 5819 | case 's': |
| 5820 | if (xmlStrEqual(name, BAD_CAST "self")) |
| 5821 | ret = AXIS_SELF; |
| 5822 | break; |
| 5823 | } |
| 5824 | return(ret); |
| 5825 | } |
| 5826 | |
| 5827 | /** |
| 5828 | * xmlXPathEvalAxisSpecifier: |
| 5829 | * @ctxt: the XPath Parser context |
| 5830 | * |
| 5831 | * |
| 5832 | * Returns the axis found |
| 5833 | */ |
| 5834 | xmlXPathAxisVal |
| 5835 | xmlXPathEvalAxisSpecifier(xmlXPathParserContextPtr ctxt) { |
| 5836 | xmlXPathAxisVal ret = AXIS_CHILD; |
| 5837 | int blank = 0; |
| 5838 | xmlChar *name; |
| 5839 | |
| 5840 | if (CUR == '@') { |
| 5841 | NEXT; |
| 5842 | return(AXIS_ATTRIBUTE); |
| 5843 | } else { |
| 5844 | name = xmlXPathParseNCName(ctxt); |
| 5845 | if (name == NULL) { |
| 5846 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 5847 | } |
| 5848 | if (IS_BLANK(CUR)) |
| 5849 | blank = 1; |
| 5850 | SKIP_BLANKS; |
| 5851 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 5852 | ret = xmlXPathIsAxisName(name); |
| 5853 | } else if ((blank) && (CUR == ':')) |
| 5854 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 5855 | |
| 5856 | xmlFree(name); |
| 5857 | } |
| 5858 | return(ret); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5859 | } |
| 5860 | |
| 5861 | /** |
| 5862 | * xmlXPathEvalStep: |
| 5863 | * @ctxt: the XPath Parser context |
| 5864 | * |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5865 | * [4] Step ::= AxisSpecifier NodeTest Predicate* |
| 5866 | * | AbbreviatedStep |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5867 | * |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5868 | * [12] AbbreviatedStep ::= '.' | '..' |
| 5869 | * |
| 5870 | * [5] AxisSpecifier ::= AxisName '::' |
| 5871 | * | AbbreviatedAxisSpecifier |
| 5872 | * |
| 5873 | * [13] AbbreviatedAxisSpecifier ::= '@'? |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5874 | * |
| 5875 | * Modified for XPtr range support as: |
| 5876 | * |
| 5877 | * [4xptr] Step ::= AxisSpecifier NodeTest Predicate* |
| 5878 | * | AbbreviatedStep |
| 5879 | * | 'range-to' '(' Expr ')' Predicate* |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5880 | * |
| 5881 | * Evaluate one step in a Location Path |
| 5882 | * A location step of . is short for self::node(). This is |
| 5883 | * particularly useful in conjunction with //. For example, the |
| 5884 | * location path .//para is short for |
| 5885 | * self::node()/descendant-or-self::node()/child::para |
| 5886 | * and so will select all para descendant elements of the context |
| 5887 | * node. |
| 5888 | * Similarly, a location step of .. is short for parent::node(). |
| 5889 | * For example, ../title is short for parent::node()/child::title |
| 5890 | * and so will select the title children of the parent of the context |
| 5891 | * node. |
| 5892 | */ |
| 5893 | void |
| 5894 | xmlXPathEvalStep(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5895 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5896 | if ((CUR == '.') && (NXT(1) == '.')) { |
| 5897 | SKIP(2); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5898 | SKIP_BLANKS; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 5899 | xmlXPathNodeCollectAndTest(ctxt, AXIS_PARENT, |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 5900 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5901 | } else if (CUR == '.') { |
| 5902 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5903 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5904 | } else { |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5905 | xmlChar *name = NULL; |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 5906 | const xmlChar *prefix = NULL; |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5907 | xmlXPathTestVal test; |
| 5908 | xmlXPathAxisVal axis; |
| 5909 | xmlXPathTypeVal type; |
| 5910 | |
| 5911 | /* |
| 5912 | * The modification needed for XPointer change to the production |
| 5913 | */ |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5914 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5915 | if (ctxt->context->xptr) { |
| 5916 | name = xmlXPathParseNCName(ctxt); |
| 5917 | if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) { |
| 5918 | xmlFree(name); |
| 5919 | SKIP_BLANKS; |
| 5920 | if (CUR != '(') { |
| 5921 | XP_ERROR(XPATH_EXPR_ERROR); |
| 5922 | } |
| 5923 | NEXT; |
| 5924 | SKIP_BLANKS; |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5925 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5926 | xmlXPtrRangeToFunction(ctxt, 1); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5927 | CHECK_ERROR; |
| 5928 | |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5929 | SKIP_BLANKS; |
| 5930 | if (CUR != ')') { |
| 5931 | XP_ERROR(XPATH_EXPR_ERROR); |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5932 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5933 | NEXT; |
| 5934 | goto eval_predicates; |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5935 | } |
Daniel Veillard | ac26030 | 2000-10-04 13:33:43 +0000 | [diff] [blame] | 5936 | } |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5937 | #endif |
| 5938 | if (name == NULL) |
| 5939 | name = xmlXPathParseNCName(ctxt); |
| 5940 | if (name != NULL) { |
| 5941 | axis = xmlXPathIsAxisName(name); |
| 5942 | if (axis != 0) { |
| 5943 | SKIP_BLANKS; |
| 5944 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 5945 | SKIP(2); |
| 5946 | xmlFree(name); |
| 5947 | name = NULL; |
| 5948 | } else { |
| 5949 | /* an element name can conflict with an axis one :-\ */ |
| 5950 | axis = AXIS_CHILD; |
| 5951 | } |
| 5952 | } else { |
| 5953 | axis = AXIS_CHILD; |
| 5954 | } |
| 5955 | } else if (CUR == '@') { |
| 5956 | NEXT; |
| 5957 | axis = AXIS_ATTRIBUTE; |
| 5958 | } else { |
| 5959 | axis = AXIS_CHILD; |
| 5960 | } |
| 5961 | |
| 5962 | CHECK_ERROR; |
| 5963 | |
| 5964 | name = xmlXPathEvalNodeTest(ctxt, &test, &type, &prefix, name); |
| 5965 | if (test == 0) |
| 5966 | return; |
| 5967 | |
| 5968 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5969 | xmlGenericError(xmlGenericErrorContext, |
| 5970 | "Basis : computing new set\n"); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5971 | #endif |
| 5972 | xmlXPathNodeCollectAndTest(ctxt, axis, test, type, prefix, name); |
| 5973 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5974 | xmlGenericError(xmlGenericErrorContext, "Basis : "); |
| 5975 | xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5976 | #endif |
| 5977 | if (name != NULL) |
| 5978 | xmlFree(name); |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 5979 | |
| 5980 | eval_predicates: |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 5981 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5982 | while (CUR == '[') { |
| 5983 | xmlXPathEvalPredicate(ctxt); |
| 5984 | } |
| 5985 | } |
| 5986 | #ifdef DEBUG_STEP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 5987 | xmlGenericError(xmlGenericErrorContext, "Step : "); |
| 5988 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 5989 | ctxt->value->nodesetval); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 5990 | #endif |
| 5991 | } |
| 5992 | |
| 5993 | /** |
| 5994 | * xmlXPathEvalRelativeLocationPath: |
| 5995 | * @ctxt: the XPath Parser context |
| 5996 | * |
| 5997 | * [3] RelativeLocationPath ::= Step |
| 5998 | * | RelativeLocationPath '/' Step |
| 5999 | * | AbbreviatedRelativeLocationPath |
| 6000 | * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
| 6001 | * |
| 6002 | */ |
| 6003 | void |
Daniel Veillard | ce6e98d | 2000-11-25 09:54:49 +0000 | [diff] [blame] | 6004 | #ifdef VMS |
| 6005 | xmlXPathEvalRelLocationPath |
| 6006 | #else |
| 6007 | xmlXPathEvalRelativeLocationPath |
| 6008 | #endif |
| 6009 | (xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6010 | SKIP_BLANKS; |
Daniel Veillard | c8df0aa | 2000-10-10 23:50:30 +0000 | [diff] [blame] | 6011 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 6012 | SKIP(2); |
| 6013 | SKIP_BLANKS; |
| 6014 | xmlXPathNodeCollectAndTest(ctxt, AXIS_DESCENDANT_OR_SELF, |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 6015 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | c8df0aa | 2000-10-10 23:50:30 +0000 | [diff] [blame] | 6016 | } else if (CUR == '/') { |
| 6017 | NEXT; |
| 6018 | SKIP_BLANKS; |
| 6019 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6020 | xmlXPathEvalStep(ctxt); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6021 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6022 | while (CUR == '/') { |
| 6023 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 6024 | SKIP(2); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6025 | SKIP_BLANKS; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6026 | xmlXPathNodeCollectAndTest(ctxt, AXIS_DESCENDANT_OR_SELF, |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 6027 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6028 | xmlXPathEvalStep(ctxt); |
| 6029 | } else if (CUR == '/') { |
| 6030 | NEXT; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6031 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6032 | xmlXPathEvalStep(ctxt); |
| 6033 | } |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6034 | SKIP_BLANKS; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6035 | } |
| 6036 | } |
| 6037 | |
| 6038 | /** |
| 6039 | * xmlXPathEvalLocationPath: |
| 6040 | * @ctxt: the XPath Parser context |
| 6041 | * |
| 6042 | * [1] LocationPath ::= RelativeLocationPath |
| 6043 | * | AbsoluteLocationPath |
| 6044 | * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
| 6045 | * | AbbreviatedAbsoluteLocationPath |
| 6046 | * [10] AbbreviatedAbsoluteLocationPath ::= |
| 6047 | * '//' RelativeLocationPath |
| 6048 | * |
| 6049 | * // is short for /descendant-or-self::node()/. For example, |
| 6050 | * //para is short for /descendant-or-self::node()/child::para and |
| 6051 | * so will select any para element in the document (even a para element |
| 6052 | * that is a document element will be selected by //para since the |
| 6053 | * document element node is a child of the root node); div//para is |
| 6054 | * short for div/descendant-or-self::node()/child::para and so will |
| 6055 | * select all para descendants of div children. |
| 6056 | */ |
| 6057 | void |
| 6058 | xmlXPathEvalLocationPath(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6059 | SKIP_BLANKS; |
| 6060 | if (CUR != '/') { |
| 6061 | xmlXPathEvalRelativeLocationPath(ctxt); |
| 6062 | } else { |
| 6063 | while (CUR == '/') { |
| 6064 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 6065 | SKIP(2); |
| 6066 | SKIP_BLANKS; |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6067 | xmlXPathNodeCollectAndTest(ctxt, |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6068 | AXIS_DESCENDANT_OR_SELF, NODE_TEST_TYPE, |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 6069 | NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 6070 | xmlXPathEvalRelativeLocationPath(ctxt); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6071 | } else if (CUR == '/') { |
| 6072 | NEXT; |
| 6073 | SKIP_BLANKS; |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6074 | if (CUR != 0) |
| 6075 | xmlXPathEvalRelativeLocationPath(ctxt); |
Daniel Veillard | 00fdf37 | 1999-10-08 09:40:39 +0000 | [diff] [blame] | 6076 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6077 | } |
| 6078 | } |
| 6079 | } |
| 6080 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6081 | /** |
| 6082 | * xmlXPathEval: |
| 6083 | * @str: the XPath expression |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6084 | * @ctx: the XPath context |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6085 | * |
| 6086 | * Evaluate the XPath Location Path in the given context. |
| 6087 | * |
| 6088 | * Returns the xmlXPathObjectPtr resulting from the eveluation or NULL. |
| 6089 | * the caller has to free the object. |
| 6090 | */ |
| 6091 | xmlXPathObjectPtr |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6092 | xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) { |
| 6093 | xmlXPathParserContextPtr ctxt; |
Daniel Veillard | 41e0651 | 2000-11-13 11:47:47 +0000 | [diff] [blame] | 6094 | xmlXPathObjectPtr res = NULL, tmp, init = NULL; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6095 | int stack = 0; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6096 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 6097 | xmlXPathInit(); |
| 6098 | |
Daniel Veillard | df7ef2a | 2000-10-25 10:11:55 +0000 | [diff] [blame] | 6099 | CHECK_CONTEXT(ctx) |
| 6100 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6101 | ctxt = xmlXPathNewParserContext(str, ctx); |
Daniel Veillard | 5a2b697 | 2001-01-20 21:15:50 +0000 | [diff] [blame] | 6102 | /**** TAG:9999 |
Daniel Veillard | 41e0651 | 2000-11-13 11:47:47 +0000 | [diff] [blame] | 6103 | if (ctx->node != NULL) { |
| 6104 | init = xmlXPathNewNodeSet(ctx->node); |
| 6105 | valuePush(ctxt, init); |
| 6106 | } |
Daniel Veillard | 5a2b697 | 2001-01-20 21:15:50 +0000 | [diff] [blame] | 6107 | ****/ |
Daniel Veillard | 55b91f2 | 2000-10-05 16:30:11 +0000 | [diff] [blame] | 6108 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6109 | |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 6110 | if (ctxt->value == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 6111 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 767662d | 2000-10-27 17:04:52 +0000 | [diff] [blame] | 6112 | "xmlXPathEval: evaluation failed\n"); |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6113 | } else { |
| 6114 | res = valuePop(ctxt); |
| 6115 | } |
| 6116 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6117 | do { |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6118 | tmp = valuePop(ctxt); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6119 | if (tmp != NULL) { |
Daniel Veillard | 41e0651 | 2000-11-13 11:47:47 +0000 | [diff] [blame] | 6120 | if (tmp != init) |
| 6121 | stack++; |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 6122 | xmlXPathFreeObject(tmp); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6123 | } |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 6124 | } while (tmp != NULL); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6125 | if (stack != 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 6126 | xmlGenericError(xmlGenericErrorContext, |
| 6127 | "xmlXPathEval: %d object left on the stack\n", |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6128 | stack); |
| 6129 | } |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6130 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 6131 | xmlXPathFreeObject(res); |
| 6132 | res = NULL; |
| 6133 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 6134 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6135 | xmlXPathFreeParserContext(ctxt); |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6136 | return(res); |
| 6137 | } |
| 6138 | |
| 6139 | /** |
| 6140 | * xmlXPathEvalExpression: |
| 6141 | * @str: the XPath expression |
| 6142 | * @ctxt: the XPath context |
| 6143 | * |
| 6144 | * Evaluate the XPath expression in the given context. |
| 6145 | * |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 6146 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6147 | * the caller has to free the object. |
| 6148 | */ |
| 6149 | xmlXPathObjectPtr |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 6150 | xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) { |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6151 | xmlXPathParserContextPtr pctxt; |
| 6152 | xmlXPathObjectPtr res, tmp; |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6153 | int stack = 0; |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6154 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 6155 | xmlXPathInit(); |
| 6156 | |
Daniel Veillard | 740abf5 | 2000-10-02 23:04:54 +0000 | [diff] [blame] | 6157 | CHECK_CONTEXT(ctxt) |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6158 | |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6159 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 6160 | xmlXPathEvalExpr(pctxt); |
| 6161 | |
| 6162 | res = valuePop(pctxt); |
| 6163 | do { |
| 6164 | tmp = valuePop(pctxt); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6165 | if (tmp != NULL) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 6166 | xmlXPathFreeObject(tmp); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6167 | stack++; |
| 6168 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6169 | } while (tmp != NULL); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6170 | if (stack != 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 6171 | xmlGenericError(xmlGenericErrorContext, |
| 6172 | "xmlXPathEvalExpression: %d object left on the stack\n", |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 6173 | stack); |
| 6174 | } |
Daniel Veillard | 1566d3a | 1999-07-15 14:24:29 +0000 | [diff] [blame] | 6175 | xmlXPathFreeParserContext(pctxt); |
| 6176 | return(res); |
| 6177 | } |
| 6178 | |
Daniel Veillard | f17e09b | 2001-01-25 13:55:35 +0000 | [diff] [blame] | 6179 | /** |
| 6180 | * xmlXPathRegisterAllFunctions: |
| 6181 | * @ctxt: the XPath context |
| 6182 | * |
| 6183 | * Registers all default XPath functions in this context |
| 6184 | */ |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 6185 | void |
| 6186 | xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt) |
| 6187 | { |
| 6188 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean", |
| 6189 | xmlXPathBooleanFunction); |
| 6190 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling", |
| 6191 | xmlXPathCeilingFunction); |
| 6192 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count", |
| 6193 | xmlXPathCountFunction); |
| 6194 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat", |
| 6195 | xmlXPathConcatFunction); |
| 6196 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains", |
| 6197 | xmlXPathContainsFunction); |
| 6198 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id", |
| 6199 | xmlXPathIdFunction); |
| 6200 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false", |
| 6201 | xmlXPathFalseFunction); |
| 6202 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor", |
| 6203 | xmlXPathFloorFunction); |
| 6204 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last", |
| 6205 | xmlXPathLastFunction); |
| 6206 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang", |
| 6207 | xmlXPathLangFunction); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 6208 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name", |
| 6209 | xmlXPathLocalNameFunction); |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 6210 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not", |
| 6211 | xmlXPathNotFunction); |
| 6212 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name", |
| 6213 | xmlXPathNameFunction); |
Daniel Veillard | f6bf921 | 2000-10-26 14:07:44 +0000 | [diff] [blame] | 6214 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri", |
| 6215 | xmlXPathNamespaceURIFunction); |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 6216 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space", |
| 6217 | xmlXPathNormalizeFunction); |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 6218 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number", |
| 6219 | xmlXPathNumberFunction); |
| 6220 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position", |
| 6221 | xmlXPathPositionFunction); |
| 6222 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round", |
| 6223 | xmlXPathRoundFunction); |
| 6224 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string", |
| 6225 | xmlXPathStringFunction); |
| 6226 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length", |
| 6227 | xmlXPathStringLengthFunction); |
| 6228 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with", |
| 6229 | xmlXPathStartsWithFunction); |
| 6230 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring", |
| 6231 | xmlXPathSubstringFunction); |
| 6232 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before", |
| 6233 | xmlXPathSubstringBeforeFunction); |
| 6234 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after", |
| 6235 | xmlXPathSubstringAfterFunction); |
| 6236 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum", |
| 6237 | xmlXPathSumFunction); |
| 6238 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true", |
| 6239 | xmlXPathTrueFunction); |
| 6240 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate", |
| 6241 | xmlXPathTranslateFunction); |
| 6242 | } |
| 6243 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 6244 | #endif /* LIBXML_XPATH_ENABLED */ |