Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * xpath.c: XML Path Language implementation |
| 3 | * XPath is a language for addressing parts of an XML document, |
| 4 | * designed to be used by both XSLT and XPointer |
| 5 | * |
| 6 | * Reference: W3C Recommendation 16 November 1999 |
| 7 | * http://www.w3.org/TR/1999/REC-xpath-19991116 |
| 8 | * Public reference: |
| 9 | * http://www.w3.org/TR/xpath |
| 10 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11 | * See Copyright for the status of this software |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 13 | * Author: daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 17 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 18 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 19 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 20 | #include <string.h> |
| 21 | |
| 22 | #ifdef HAVE_SYS_TYPES_H |
| 23 | #include <sys/types.h> |
| 24 | #endif |
| 25 | #ifdef HAVE_MATH_H |
| 26 | #include <math.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_FLOAT_H |
| 29 | #include <float.h> |
| 30 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 31 | #ifdef HAVE_CTYPE_H |
| 32 | #include <ctype.h> |
| 33 | #endif |
Daniel Veillard | 5792e16 | 2001-04-30 17:44:45 +0000 | [diff] [blame] | 34 | #ifdef HAVE_SIGNAL_H |
Daniel Veillard | b45c43b | 2001-04-28 17:02:11 +0000 | [diff] [blame] | 35 | #include <signal.h> |
Daniel Veillard | b45c43b | 2001-04-28 17:02:11 +0000 | [diff] [blame] | 36 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 37 | |
| 38 | #include <libxml/xmlmemory.h> |
| 39 | #include <libxml/tree.h> |
| 40 | #include <libxml/valid.h> |
| 41 | #include <libxml/xpath.h> |
| 42 | #include <libxml/xpathInternals.h> |
| 43 | #include <libxml/parserInternals.h> |
| 44 | #include <libxml/hash.h> |
| 45 | #ifdef LIBXML_XPTR_ENABLED |
| 46 | #include <libxml/xpointer.h> |
| 47 | #endif |
| 48 | #ifdef LIBXML_DEBUG_ENABLED |
| 49 | #include <libxml/debugXML.h> |
| 50 | #endif |
| 51 | #include <libxml/xmlerror.h> |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 52 | #include <libxml/threads.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 53 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 54 | |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 55 | #define TODO \ |
| 56 | xmlGenericError(xmlGenericErrorContext, \ |
| 57 | "Unimplemented block at %s:%d\n", \ |
| 58 | __FILE__, __LINE__); |
| 59 | |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 60 | /* |
| 61 | * TODO: |
| 62 | * There are a few spots where some tests are done which depend upon ascii |
| 63 | * data. These should be enhanced for full UTF8 support (see particularly |
| 64 | * any use of the macros IS_ASCII_CHARACTER and IS_ASCII_DIGIT) |
| 65 | */ |
| 66 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 67 | #if defined(LIBXML_SCHEMAS_ENABLED) || defined(LIBXML_XPATH_ENABLED) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 68 | /************************************************************************ |
| 69 | * * |
| 70 | * Floating point stuff * |
| 71 | * * |
| 72 | ************************************************************************/ |
| 73 | |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 74 | #ifndef TRIO_REPLACE_STDIO |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 75 | #define TRIO_PUBLIC static |
Daniel Veillard | c0631a6 | 2001-09-20 13:56:06 +0000 | [diff] [blame] | 76 | #endif |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 77 | #include "trionan.c" |
| 78 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 79 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 80 | * The lack of portability of this section of the libc is annoying ! |
| 81 | */ |
| 82 | double xmlXPathNAN = 0; |
| 83 | double xmlXPathPINF = 1; |
| 84 | double xmlXPathNINF = -1; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 85 | double xmlXPathNZERO = 0; |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 86 | static int xmlXPathInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 87 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 88 | /** |
| 89 | * xmlXPathInit: |
| 90 | * |
| 91 | * Initialize the XPath environment |
| 92 | */ |
| 93 | void |
| 94 | xmlXPathInit(void) { |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 95 | if (xmlXPathInitialized) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 96 | |
Bjorn Reese | 4502960 | 2001-08-21 09:23:53 +0000 | [diff] [blame] | 97 | xmlXPathPINF = trio_pinf(); |
| 98 | xmlXPathNINF = trio_ninf(); |
| 99 | xmlXPathNAN = trio_nan(); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 100 | xmlXPathNZERO = trio_nzero(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 101 | |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 102 | xmlXPathInitialized = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 105 | /** |
| 106 | * xmlXPathIsNaN: |
| 107 | * @val: a double value |
| 108 | * |
| 109 | * Provides a portable isnan() function to detect whether a double |
| 110 | * is a NotaNumber. Based on trio code |
| 111 | * http://sourceforge.net/projects/ctrio/ |
| 112 | * |
| 113 | * Returns 1 if the value is a NaN, 0 otherwise |
| 114 | */ |
| 115 | int |
| 116 | xmlXPathIsNaN(double val) { |
| 117 | return(trio_isnan(val)); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * xmlXPathIsInf: |
| 122 | * @val: a double value |
| 123 | * |
| 124 | * Provides a portable isinf() function to detect whether a double |
| 125 | * is a +Infinite or -Infinite. Based on trio code |
| 126 | * http://sourceforge.net/projects/ctrio/ |
| 127 | * |
| 128 | * Returns 1 vi the value is +Infinite, -1 if -Infinite, 0 otherwise |
| 129 | */ |
| 130 | int |
| 131 | xmlXPathIsInf(double val) { |
| 132 | return(trio_isinf(val)); |
| 133 | } |
| 134 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 135 | #endif /* SCHEMAS or XPATH */ |
| 136 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 137 | /** |
| 138 | * xmlXPathGetSign: |
| 139 | * @val: a double value |
| 140 | * |
| 141 | * Provides a portable function to detect the sign of a double |
| 142 | * Modified from trio code |
| 143 | * http://sourceforge.net/projects/ctrio/ |
| 144 | * |
| 145 | * Returns 1 if the value is Negative, 0 if positive |
| 146 | */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 147 | static int |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 148 | xmlXPathGetSign(double val) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 149 | return(trio_signbit(val)); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 153 | /* |
| 154 | * TODO: when compatibility allows remove all "fake node libxslt" strings |
| 155 | * the test should just be name[0] = ' ' |
| 156 | */ |
| 157 | /* #define DEBUG */ |
| 158 | /* #define DEBUG_STEP */ |
| 159 | /* #define DEBUG_STEP_NTH */ |
| 160 | /* #define DEBUG_EXPR */ |
| 161 | /* #define DEBUG_EVAL_COUNTS */ |
| 162 | |
| 163 | static xmlNs xmlXPathXMLNamespaceStruct = { |
| 164 | NULL, |
| 165 | XML_NAMESPACE_DECL, |
| 166 | XML_XML_NAMESPACE, |
| 167 | BAD_CAST "xml", |
| 168 | NULL |
| 169 | }; |
| 170 | static xmlNsPtr xmlXPathXMLNamespace = &xmlXPathXMLNamespaceStruct; |
| 171 | #ifndef LIBXML_THREAD_ENABLED |
| 172 | /* |
| 173 | * Optimizer is disabled only when threaded apps are detected while |
| 174 | * the library ain't compiled for thread safety. |
| 175 | */ |
| 176 | static int xmlXPathDisableOptimizer = 0; |
| 177 | #endif |
| 178 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 179 | /************************************************************************ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 180 | * * |
| 181 | * Error handling routines * |
| 182 | * * |
| 183 | ************************************************************************/ |
| 184 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 185 | /* |
| 186 | * The array xmlXPathErrorMessages corresponds to the enum xmlXPathError |
| 187 | */ |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 188 | static const char *xmlXPathErrorMessages[] = { |
| 189 | "Ok\n", |
| 190 | "Number encoding\n", |
| 191 | "Unfinished literal\n", |
| 192 | "Start of literal\n", |
| 193 | "Expected $ for variable reference\n", |
| 194 | "Undefined variable\n", |
| 195 | "Invalid predicate\n", |
| 196 | "Invalid expression\n", |
| 197 | "Missing closing curly brace\n", |
| 198 | "Unregistered function\n", |
| 199 | "Invalid operand\n", |
| 200 | "Invalid type\n", |
| 201 | "Invalid number of arguments\n", |
| 202 | "Invalid context size\n", |
| 203 | "Invalid context position\n", |
| 204 | "Memory allocation error\n", |
| 205 | "Syntax error\n", |
| 206 | "Resource error\n", |
| 207 | "Sub resource error\n", |
| 208 | "Undefined namespace prefix\n", |
| 209 | "Encoding error\n", |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 210 | "Char out of XML range\n", |
| 211 | "Invalid or inclomplete context\n" |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | |
| 215 | /** |
| 216 | * xmlXPathErrMemory: |
| 217 | * @ctxt: an XPath context |
| 218 | * @extra: extra informations |
| 219 | * |
| 220 | * Handle a redefinition of attribute error |
| 221 | */ |
| 222 | static void |
| 223 | xmlXPathErrMemory(xmlXPathContextPtr ctxt, const char *extra) |
| 224 | { |
| 225 | if (ctxt != NULL) { |
| 226 | if (extra) { |
| 227 | xmlChar buf[200]; |
| 228 | |
| 229 | xmlStrPrintf(buf, 200, |
| 230 | BAD_CAST "Memory allocation failed : %s\n", |
| 231 | extra); |
| 232 | ctxt->lastError.message = (char *) xmlStrdup(buf); |
| 233 | } else { |
| 234 | ctxt->lastError.message = (char *) |
| 235 | xmlStrdup(BAD_CAST "Memory allocation failed\n"); |
| 236 | } |
| 237 | ctxt->lastError.domain = XML_FROM_XPATH; |
| 238 | ctxt->lastError.code = XML_ERR_NO_MEMORY; |
| 239 | if (ctxt->error != NULL) |
| 240 | ctxt->error(ctxt->userData, &ctxt->lastError); |
| 241 | } else { |
| 242 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 243 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 244 | NULL, NULL, XML_FROM_XPATH, |
| 245 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, |
| 246 | extra, NULL, NULL, 0, 0, |
| 247 | "Memory allocation failed : %s\n", extra); |
| 248 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 249 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 250 | NULL, NULL, XML_FROM_XPATH, |
| 251 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, |
| 252 | NULL, NULL, NULL, 0, 0, |
| 253 | "Memory allocation failed\n"); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 258 | * xmlXPathPErrMemory: |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 259 | * @ctxt: an XPath parser context |
| 260 | * @extra: extra informations |
| 261 | * |
| 262 | * Handle a redefinition of attribute error |
| 263 | */ |
| 264 | static void |
| 265 | xmlXPathPErrMemory(xmlXPathParserContextPtr ctxt, const char *extra) |
| 266 | { |
| 267 | ctxt->error = XPATH_MEMORY_ERROR; |
| 268 | if (ctxt == NULL) |
| 269 | xmlXPathErrMemory(NULL, extra); |
| 270 | else |
| 271 | xmlXPathErrMemory(ctxt->context, extra); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * xmlXPathErr: |
| 276 | * @ctxt: a XPath parser context |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 277 | * @error: the error code |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 278 | * |
| 279 | * Handle a Relax NG Parsing error |
| 280 | */ |
| 281 | void |
| 282 | xmlXPathErr(xmlXPathParserContextPtr ctxt, int error) |
| 283 | { |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 284 | if (ctxt == NULL) { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 285 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 286 | NULL, NULL, XML_FROM_XPATH, |
| 287 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 288 | XML_ERR_ERROR, NULL, 0, |
| 289 | NULL, NULL, NULL, 0, 0, |
| 290 | xmlXPathErrorMessages[error]); |
| 291 | return; |
| 292 | } |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 293 | ctxt->error = error; |
| 294 | if (ctxt->context == NULL) { |
| 295 | __xmlRaiseError(NULL, NULL, NULL, |
| 296 | NULL, NULL, XML_FROM_XPATH, |
| 297 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 298 | XML_ERR_ERROR, NULL, 0, |
| 299 | (const char *) ctxt->base, NULL, NULL, |
| 300 | ctxt->cur - ctxt->base, 0, |
| 301 | xmlXPathErrorMessages[error]); |
| 302 | return; |
| 303 | } |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 304 | ctxt->context->lastError.domain = XML_FROM_XPATH; |
| 305 | ctxt->context->lastError.code = error + XML_XPATH_EXPRESSION_OK - |
| 306 | XPATH_EXPRESSION_OK; |
Daniel Veillard | fcf719c | 2003-10-10 11:42:17 +0000 | [diff] [blame] | 307 | ctxt->context->lastError.level = XML_ERR_ERROR; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 308 | ctxt->context->lastError.str1 = (char *) xmlStrdup(ctxt->base); |
| 309 | ctxt->context->lastError.int1 = ctxt->cur - ctxt->base; |
| 310 | ctxt->context->lastError.node = ctxt->context->debugNode; |
| 311 | if (ctxt->context->error != NULL) { |
| 312 | ctxt->context->error(ctxt->context->userData, |
| 313 | &ctxt->context->lastError); |
| 314 | } else { |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 315 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 316 | NULL, ctxt->context->debugNode, XML_FROM_XPATH, |
| 317 | error + XML_XPATH_EXPRESSION_OK - XPATH_EXPRESSION_OK, |
| 318 | XML_ERR_ERROR, NULL, 0, |
| 319 | (const char *) ctxt->base, NULL, NULL, |
| 320 | ctxt->cur - ctxt->base, 0, |
| 321 | xmlXPathErrorMessages[error]); |
| 322 | } |
| 323 | |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * xmlXPatherror: |
| 328 | * @ctxt: the XPath Parser context |
| 329 | * @file: the file name |
| 330 | * @line: the line number |
| 331 | * @no: the error number |
| 332 | * |
| 333 | * Formats an error message. |
| 334 | */ |
| 335 | void |
| 336 | xmlXPatherror(xmlXPathParserContextPtr ctxt, const char *file ATTRIBUTE_UNUSED, |
| 337 | int line ATTRIBUTE_UNUSED, int no) { |
| 338 | xmlXPathErr(ctxt, no); |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /************************************************************************ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 343 | * * |
| 344 | * Parser Types * |
| 345 | * * |
| 346 | ************************************************************************/ |
| 347 | |
| 348 | /* |
| 349 | * Types are private: |
| 350 | */ |
| 351 | |
| 352 | typedef enum { |
| 353 | XPATH_OP_END=0, |
| 354 | XPATH_OP_AND, |
| 355 | XPATH_OP_OR, |
| 356 | XPATH_OP_EQUAL, |
| 357 | XPATH_OP_CMP, |
| 358 | XPATH_OP_PLUS, |
| 359 | XPATH_OP_MULT, |
| 360 | XPATH_OP_UNION, |
| 361 | XPATH_OP_ROOT, |
| 362 | XPATH_OP_NODE, |
| 363 | XPATH_OP_RESET, |
| 364 | XPATH_OP_COLLECT, |
| 365 | XPATH_OP_VALUE, |
| 366 | XPATH_OP_VARIABLE, |
| 367 | XPATH_OP_FUNCTION, |
| 368 | XPATH_OP_ARG, |
| 369 | XPATH_OP_PREDICATE, |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 370 | XPATH_OP_FILTER, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 371 | XPATH_OP_SORT |
| 372 | #ifdef LIBXML_XPTR_ENABLED |
| 373 | ,XPATH_OP_RANGETO |
| 374 | #endif |
| 375 | } xmlXPathOp; |
| 376 | |
| 377 | typedef enum { |
| 378 | AXIS_ANCESTOR = 1, |
| 379 | AXIS_ANCESTOR_OR_SELF, |
| 380 | AXIS_ATTRIBUTE, |
| 381 | AXIS_CHILD, |
| 382 | AXIS_DESCENDANT, |
| 383 | AXIS_DESCENDANT_OR_SELF, |
| 384 | AXIS_FOLLOWING, |
| 385 | AXIS_FOLLOWING_SIBLING, |
| 386 | AXIS_NAMESPACE, |
| 387 | AXIS_PARENT, |
| 388 | AXIS_PRECEDING, |
| 389 | AXIS_PRECEDING_SIBLING, |
| 390 | AXIS_SELF |
| 391 | } xmlXPathAxisVal; |
| 392 | |
| 393 | typedef enum { |
| 394 | NODE_TEST_NONE = 0, |
| 395 | NODE_TEST_TYPE = 1, |
| 396 | NODE_TEST_PI = 2, |
| 397 | NODE_TEST_ALL = 3, |
| 398 | NODE_TEST_NS = 4, |
| 399 | NODE_TEST_NAME = 5 |
| 400 | } xmlXPathTestVal; |
| 401 | |
| 402 | typedef enum { |
| 403 | NODE_TYPE_NODE = 0, |
| 404 | NODE_TYPE_COMMENT = XML_COMMENT_NODE, |
| 405 | NODE_TYPE_TEXT = XML_TEXT_NODE, |
| 406 | NODE_TYPE_PI = XML_PI_NODE |
| 407 | } xmlXPathTypeVal; |
| 408 | |
| 409 | |
| 410 | typedef struct _xmlXPathStepOp xmlXPathStepOp; |
| 411 | typedef xmlXPathStepOp *xmlXPathStepOpPtr; |
| 412 | struct _xmlXPathStepOp { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 413 | xmlXPathOp op; /* The identifier of the operation */ |
| 414 | int ch1; /* First child */ |
| 415 | int ch2; /* Second child */ |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 416 | int value; |
| 417 | int value2; |
| 418 | int value3; |
| 419 | void *value4; |
| 420 | void *value5; |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 421 | void *cache; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 422 | void *cacheURI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 423 | }; |
| 424 | |
| 425 | struct _xmlXPathCompExpr { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 426 | int nbStep; /* Number of steps in this expression */ |
| 427 | int maxStep; /* Maximum number of steps allocated */ |
| 428 | xmlXPathStepOp *steps; /* ops for computation of this expression */ |
| 429 | int last; /* index of last step in expression */ |
| 430 | xmlChar *expr; /* the expression being computed */ |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 431 | xmlDictPtr dict; /* the dictionnary to use if any */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 432 | #ifdef DEBUG_EVAL_COUNTS |
| 433 | int nb; |
| 434 | xmlChar *string; |
| 435 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 436 | }; |
| 437 | |
| 438 | /************************************************************************ |
| 439 | * * |
| 440 | * Parser Type functions * |
| 441 | * * |
| 442 | ************************************************************************/ |
| 443 | |
| 444 | /** |
| 445 | * xmlXPathNewCompExpr: |
| 446 | * |
| 447 | * Create a new Xpath component |
| 448 | * |
| 449 | * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error |
| 450 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 451 | static xmlXPathCompExprPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 452 | xmlXPathNewCompExpr(void) { |
| 453 | xmlXPathCompExprPtr cur; |
| 454 | |
| 455 | cur = (xmlXPathCompExprPtr) xmlMalloc(sizeof(xmlXPathCompExpr)); |
| 456 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 457 | xmlXPathErrMemory(NULL, "allocating component\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 458 | return(NULL); |
| 459 | } |
| 460 | memset(cur, 0, sizeof(xmlXPathCompExpr)); |
| 461 | cur->maxStep = 10; |
| 462 | cur->nbStep = 0; |
| 463 | cur->steps = (xmlXPathStepOp *) xmlMalloc(cur->maxStep * |
| 464 | sizeof(xmlXPathStepOp)); |
| 465 | if (cur->steps == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 466 | xmlXPathErrMemory(NULL, "allocating steps\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 467 | xmlFree(cur); |
| 468 | return(NULL); |
| 469 | } |
| 470 | memset(cur->steps, 0, cur->maxStep * sizeof(xmlXPathStepOp)); |
| 471 | cur->last = -1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 472 | #ifdef DEBUG_EVAL_COUNTS |
| 473 | cur->nb = 0; |
| 474 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 475 | return(cur); |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * xmlXPathFreeCompExpr: |
| 480 | * @comp: an XPATH comp |
| 481 | * |
| 482 | * Free up the memory allocated by @comp |
| 483 | */ |
| 484 | void |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 485 | xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp) |
| 486 | { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 487 | xmlXPathStepOpPtr op; |
| 488 | int i; |
| 489 | |
| 490 | if (comp == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 491 | return; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 492 | if (comp->dict == NULL) { |
| 493 | for (i = 0; i < comp->nbStep; i++) { |
| 494 | op = &comp->steps[i]; |
| 495 | if (op->value4 != NULL) { |
| 496 | if (op->op == XPATH_OP_VALUE) |
| 497 | xmlXPathFreeObject(op->value4); |
| 498 | else |
| 499 | xmlFree(op->value4); |
| 500 | } |
| 501 | if (op->value5 != NULL) |
| 502 | xmlFree(op->value5); |
| 503 | } |
| 504 | } else { |
| 505 | for (i = 0; i < comp->nbStep; i++) { |
| 506 | op = &comp->steps[i]; |
| 507 | if (op->value4 != NULL) { |
| 508 | if (op->op == XPATH_OP_VALUE) |
| 509 | xmlXPathFreeObject(op->value4); |
| 510 | } |
| 511 | } |
| 512 | xmlDictFree(comp->dict); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 513 | } |
| 514 | if (comp->steps != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 515 | xmlFree(comp->steps); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 516 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 517 | #ifdef DEBUG_EVAL_COUNTS |
| 518 | if (comp->string != NULL) { |
| 519 | xmlFree(comp->string); |
| 520 | } |
| 521 | #endif |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 522 | if (comp->expr != NULL) { |
| 523 | xmlFree(comp->expr); |
| 524 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 525 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 526 | xmlFree(comp); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * xmlXPathCompExprAdd: |
| 531 | * @comp: the compiled expression |
| 532 | * @ch1: first child index |
| 533 | * @ch2: second child index |
| 534 | * @op: an op |
| 535 | * @value: the first int value |
| 536 | * @value2: the second int value |
| 537 | * @value3: the third int value |
| 538 | * @value4: the first string value |
| 539 | * @value5: the second string value |
| 540 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 541 | * Add a step to an XPath Compiled Expression |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 542 | * |
| 543 | * Returns -1 in case of failure, the index otherwise |
| 544 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 545 | static int |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 546 | xmlXPathCompExprAdd(xmlXPathCompExprPtr comp, int ch1, int ch2, |
| 547 | xmlXPathOp op, int value, |
| 548 | int value2, int value3, void *value4, void *value5) { |
| 549 | if (comp->nbStep >= comp->maxStep) { |
| 550 | xmlXPathStepOp *real; |
| 551 | |
| 552 | comp->maxStep *= 2; |
| 553 | real = (xmlXPathStepOp *) xmlRealloc(comp->steps, |
| 554 | comp->maxStep * sizeof(xmlXPathStepOp)); |
| 555 | if (real == NULL) { |
| 556 | comp->maxStep /= 2; |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 557 | xmlXPathErrMemory(NULL, "adding step\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 558 | return(-1); |
| 559 | } |
| 560 | comp->steps = real; |
| 561 | } |
| 562 | comp->last = comp->nbStep; |
| 563 | comp->steps[comp->nbStep].ch1 = ch1; |
| 564 | comp->steps[comp->nbStep].ch2 = ch2; |
| 565 | comp->steps[comp->nbStep].op = op; |
| 566 | comp->steps[comp->nbStep].value = value; |
| 567 | comp->steps[comp->nbStep].value2 = value2; |
| 568 | comp->steps[comp->nbStep].value3 = value3; |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 569 | if ((comp->dict != NULL) && |
| 570 | ((op == XPATH_OP_FUNCTION) || (op == XPATH_OP_VARIABLE) || |
| 571 | (op == XPATH_OP_COLLECT))) { |
| 572 | if (value4 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 573 | comp->steps[comp->nbStep].value4 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 574 | (void *)xmlDictLookup(comp->dict, value4, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 575 | xmlFree(value4); |
| 576 | } else |
| 577 | comp->steps[comp->nbStep].value4 = NULL; |
| 578 | if (value5 != NULL) { |
Daniel Veillard | b337795 | 2004-02-09 12:48:55 +0000 | [diff] [blame] | 579 | comp->steps[comp->nbStep].value5 = (xmlChar *) |
William M. Brack | c07ed5e | 2004-01-30 07:52:48 +0000 | [diff] [blame] | 580 | (void *)xmlDictLookup(comp->dict, value5, -1); |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 581 | xmlFree(value5); |
| 582 | } else |
| 583 | comp->steps[comp->nbStep].value5 = NULL; |
| 584 | } else { |
| 585 | comp->steps[comp->nbStep].value4 = value4; |
| 586 | comp->steps[comp->nbStep].value5 = value5; |
| 587 | } |
Daniel Veillard | e39a93d | 2001-04-28 14:35:02 +0000 | [diff] [blame] | 588 | comp->steps[comp->nbStep].cache = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 589 | return(comp->nbStep++); |
| 590 | } |
| 591 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 592 | /** |
| 593 | * xmlXPathCompSwap: |
| 594 | * @comp: the compiled expression |
| 595 | * @op: operation index |
| 596 | * |
| 597 | * Swaps 2 operations in the compiled expression |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 598 | */ |
| 599 | static void |
| 600 | xmlXPathCompSwap(xmlXPathStepOpPtr op) { |
| 601 | int tmp; |
| 602 | |
Daniel Veillard | bc6f759 | 2002-04-16 07:49:59 +0000 | [diff] [blame] | 603 | #ifndef LIBXML_THREAD_ENABLED |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 604 | /* |
| 605 | * Since this manipulates possibly shared variables, this is |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 606 | * disabled if one detects that the library is used in a multithreaded |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 607 | * application |
| 608 | */ |
| 609 | if (xmlXPathDisableOptimizer) |
| 610 | return; |
| 611 | #endif |
| 612 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 613 | tmp = op->ch1; |
| 614 | op->ch1 = op->ch2; |
| 615 | op->ch2 = tmp; |
| 616 | } |
| 617 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 618 | #define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \ |
| 619 | xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \ |
| 620 | (op), (val), (val2), (val3), (val4), (val5)) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 621 | #define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \ |
| 622 | xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \ |
| 623 | (op), (val), (val2), (val3), (val4), (val5)) |
| 624 | |
| 625 | #define PUSH_LEAVE_EXPR(op, val, val2) \ |
| 626 | xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 627 | |
| 628 | #define PUSH_UNARY_EXPR(op, ch, val, val2) \ |
| 629 | xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL) |
| 630 | |
| 631 | #define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 632 | xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \ |
| 633 | (val), (val2), 0 ,NULL ,NULL) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 634 | |
| 635 | /************************************************************************ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 636 | * * |
| 637 | * Debugging related functions * |
| 638 | * * |
| 639 | ************************************************************************/ |
| 640 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 641 | #define STRANGE \ |
| 642 | xmlGenericError(xmlGenericErrorContext, \ |
| 643 | "Internal error at %s:%d\n", \ |
| 644 | __FILE__, __LINE__); |
| 645 | |
| 646 | #ifdef LIBXML_DEBUG_ENABLED |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 647 | static void |
| 648 | xmlXPathDebugDumpNode(FILE *output, xmlNodePtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 649 | int i; |
| 650 | char shift[100]; |
| 651 | |
| 652 | for (i = 0;((i < depth) && (i < 25));i++) |
| 653 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 654 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 655 | if (cur == NULL) { |
| 656 | fprintf(output, shift); |
| 657 | fprintf(output, "Node is NULL !\n"); |
| 658 | return; |
| 659 | |
| 660 | } |
| 661 | |
| 662 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 663 | (cur->type == XML_HTML_DOCUMENT_NODE)) { |
| 664 | fprintf(output, shift); |
| 665 | fprintf(output, " /\n"); |
| 666 | } else if (cur->type == XML_ATTRIBUTE_NODE) |
| 667 | xmlDebugDumpAttr(output, (xmlAttrPtr)cur, depth); |
| 668 | else |
| 669 | xmlDebugDumpOneNode(output, cur, depth); |
| 670 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 671 | static void |
| 672 | xmlXPathDebugDumpNodeList(FILE *output, xmlNodePtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 673 | xmlNodePtr tmp; |
| 674 | int i; |
| 675 | char shift[100]; |
| 676 | |
| 677 | for (i = 0;((i < depth) && (i < 25));i++) |
| 678 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 679 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 680 | if (cur == NULL) { |
| 681 | fprintf(output, shift); |
| 682 | fprintf(output, "Node is NULL !\n"); |
| 683 | return; |
| 684 | |
| 685 | } |
| 686 | |
| 687 | while (cur != NULL) { |
| 688 | tmp = cur; |
| 689 | cur = cur->next; |
| 690 | xmlDebugDumpOneNode(output, tmp, depth); |
| 691 | } |
| 692 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 693 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 694 | static void |
| 695 | xmlXPathDebugDumpNodeSet(FILE *output, xmlNodeSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 696 | int i; |
| 697 | char shift[100]; |
| 698 | |
| 699 | for (i = 0;((i < depth) && (i < 25));i++) |
| 700 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 701 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 702 | |
| 703 | if (cur == NULL) { |
| 704 | fprintf(output, shift); |
| 705 | fprintf(output, "NodeSet is NULL !\n"); |
| 706 | return; |
| 707 | |
| 708 | } |
| 709 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 710 | if (cur != NULL) { |
| 711 | fprintf(output, "Set contains %d nodes:\n", cur->nodeNr); |
| 712 | for (i = 0;i < cur->nodeNr;i++) { |
| 713 | fprintf(output, shift); |
| 714 | fprintf(output, "%d", i + 1); |
| 715 | xmlXPathDebugDumpNode(output, cur->nodeTab[i], depth + 1); |
| 716 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 720 | static void |
| 721 | xmlXPathDebugDumpValueTree(FILE *output, xmlNodeSetPtr cur, int depth) { |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 722 | int i; |
| 723 | char shift[100]; |
| 724 | |
| 725 | for (i = 0;((i < depth) && (i < 25));i++) |
| 726 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 727 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 728 | |
| 729 | if ((cur == NULL) || (cur->nodeNr == 0) || (cur->nodeTab[0] == NULL)) { |
| 730 | fprintf(output, shift); |
| 731 | fprintf(output, "Value Tree is NULL !\n"); |
| 732 | return; |
| 733 | |
| 734 | } |
| 735 | |
| 736 | fprintf(output, shift); |
| 737 | fprintf(output, "%d", i + 1); |
| 738 | xmlXPathDebugDumpNodeList(output, cur->nodeTab[0]->children, depth + 1); |
| 739 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 740 | #if defined(LIBXML_XPTR_ENABLED) |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 741 | static void |
| 742 | xmlXPathDebugDumpLocationSet(FILE *output, xmlLocationSetPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 743 | int i; |
| 744 | char shift[100]; |
| 745 | |
| 746 | for (i = 0;((i < depth) && (i < 25));i++) |
| 747 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 748 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 749 | |
| 750 | if (cur == NULL) { |
| 751 | fprintf(output, shift); |
| 752 | fprintf(output, "LocationSet is NULL !\n"); |
| 753 | return; |
| 754 | |
| 755 | } |
| 756 | |
| 757 | for (i = 0;i < cur->locNr;i++) { |
| 758 | fprintf(output, shift); |
| 759 | fprintf(output, "%d : ", i + 1); |
| 760 | xmlXPathDebugDumpObject(output, cur->locTab[i], depth + 1); |
| 761 | } |
| 762 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 763 | #endif /* LIBXML_XPTR_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 764 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 765 | /** |
| 766 | * xmlXPathDebugDumpObject: |
| 767 | * @output: the FILE * to dump the output |
| 768 | * @cur: the object to inspect |
| 769 | * @depth: indentation level |
| 770 | * |
| 771 | * Dump the content of the object for debugging purposes |
| 772 | */ |
| 773 | void |
| 774 | xmlXPathDebugDumpObject(FILE *output, xmlXPathObjectPtr cur, int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 775 | int i; |
| 776 | char shift[100]; |
| 777 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 778 | if (output == NULL) return; |
| 779 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 780 | for (i = 0;((i < depth) && (i < 25));i++) |
| 781 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 782 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 783 | |
| 784 | fprintf(output, shift); |
| 785 | |
| 786 | if (cur == NULL) { |
| 787 | fprintf(output, "Object is empty (NULL)\n"); |
| 788 | return; |
| 789 | } |
| 790 | switch(cur->type) { |
| 791 | case XPATH_UNDEFINED: |
| 792 | fprintf(output, "Object is uninitialized\n"); |
| 793 | break; |
| 794 | case XPATH_NODESET: |
| 795 | fprintf(output, "Object is a Node Set :\n"); |
| 796 | xmlXPathDebugDumpNodeSet(output, cur->nodesetval, depth); |
| 797 | break; |
| 798 | case XPATH_XSLT_TREE: |
| 799 | fprintf(output, "Object is an XSLT value tree :\n"); |
Daniel Veillard | f7cd481 | 2001-02-23 18:44:52 +0000 | [diff] [blame] | 800 | xmlXPathDebugDumpValueTree(output, cur->nodesetval, depth); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 801 | break; |
| 802 | case XPATH_BOOLEAN: |
| 803 | fprintf(output, "Object is a Boolean : "); |
| 804 | if (cur->boolval) fprintf(output, "true\n"); |
| 805 | else fprintf(output, "false\n"); |
| 806 | break; |
| 807 | case XPATH_NUMBER: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 808 | switch (xmlXPathIsInf(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 809 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 810 | fprintf(output, "Object is a number : Infinity\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 811 | break; |
| 812 | case -1: |
| 813 | fprintf(output, "Object is a number : -Infinity\n"); |
| 814 | break; |
| 815 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 816 | if (xmlXPathIsNaN(cur->floatval)) { |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 817 | fprintf(output, "Object is a number : NaN\n"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 818 | } else if (cur->floatval == 0 && xmlXPathGetSign(cur->floatval) != 0) { |
| 819 | fprintf(output, "Object is a number : 0\n"); |
Daniel Veillard | 357c960 | 2001-05-03 10:49:20 +0000 | [diff] [blame] | 820 | } else { |
| 821 | fprintf(output, "Object is a number : %0g\n", cur->floatval); |
| 822 | } |
| 823 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 824 | break; |
| 825 | case XPATH_STRING: |
| 826 | fprintf(output, "Object is a string : "); |
| 827 | xmlDebugDumpString(output, cur->stringval); |
| 828 | fprintf(output, "\n"); |
| 829 | break; |
| 830 | case XPATH_POINT: |
| 831 | fprintf(output, "Object is a point : index %d in node", cur->index); |
| 832 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, depth + 1); |
| 833 | fprintf(output, "\n"); |
| 834 | break; |
| 835 | case XPATH_RANGE: |
| 836 | if ((cur->user2 == NULL) || |
| 837 | ((cur->user2 == cur->user) && (cur->index == cur->index2))) { |
| 838 | fprintf(output, "Object is a collapsed range :\n"); |
| 839 | fprintf(output, shift); |
| 840 | if (cur->index >= 0) |
| 841 | fprintf(output, "index %d in ", cur->index); |
| 842 | fprintf(output, "node\n"); |
| 843 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 844 | depth + 1); |
| 845 | } else { |
| 846 | fprintf(output, "Object is a range :\n"); |
| 847 | fprintf(output, shift); |
| 848 | fprintf(output, "From "); |
| 849 | if (cur->index >= 0) |
| 850 | fprintf(output, "index %d in ", cur->index); |
| 851 | fprintf(output, "node\n"); |
| 852 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user, |
| 853 | depth + 1); |
| 854 | fprintf(output, shift); |
| 855 | fprintf(output, "To "); |
| 856 | if (cur->index2 >= 0) |
| 857 | fprintf(output, "index %d in ", cur->index2); |
| 858 | fprintf(output, "node\n"); |
| 859 | xmlXPathDebugDumpNode(output, (xmlNodePtr) cur->user2, |
| 860 | depth + 1); |
| 861 | fprintf(output, "\n"); |
| 862 | } |
| 863 | break; |
| 864 | case XPATH_LOCATIONSET: |
| 865 | #if defined(LIBXML_XPTR_ENABLED) |
| 866 | fprintf(output, "Object is a Location Set:\n"); |
| 867 | xmlXPathDebugDumpLocationSet(output, |
| 868 | (xmlLocationSetPtr) cur->user, depth); |
| 869 | #endif |
| 870 | break; |
| 871 | case XPATH_USERS: |
| 872 | fprintf(output, "Object is user defined\n"); |
| 873 | break; |
| 874 | } |
| 875 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 876 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 877 | static void |
| 878 | xmlXPathDebugDumpStepOp(FILE *output, xmlXPathCompExprPtr comp, |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 879 | xmlXPathStepOpPtr op, int depth) { |
| 880 | int i; |
| 881 | char shift[100]; |
| 882 | |
| 883 | for (i = 0;((i < depth) && (i < 25));i++) |
| 884 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 885 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 886 | |
| 887 | fprintf(output, shift); |
| 888 | if (op == NULL) { |
| 889 | fprintf(output, "Step is NULL\n"); |
| 890 | return; |
| 891 | } |
| 892 | switch (op->op) { |
| 893 | case XPATH_OP_END: |
| 894 | fprintf(output, "END"); break; |
| 895 | case XPATH_OP_AND: |
| 896 | fprintf(output, "AND"); break; |
| 897 | case XPATH_OP_OR: |
| 898 | fprintf(output, "OR"); break; |
| 899 | case XPATH_OP_EQUAL: |
| 900 | if (op->value) |
| 901 | fprintf(output, "EQUAL ="); |
| 902 | else |
| 903 | fprintf(output, "EQUAL !="); |
| 904 | break; |
| 905 | case XPATH_OP_CMP: |
| 906 | if (op->value) |
| 907 | fprintf(output, "CMP <"); |
| 908 | else |
| 909 | fprintf(output, "CMP >"); |
| 910 | if (!op->value2) |
| 911 | fprintf(output, "="); |
| 912 | break; |
| 913 | case XPATH_OP_PLUS: |
| 914 | if (op->value == 0) |
| 915 | fprintf(output, "PLUS -"); |
| 916 | else if (op->value == 1) |
| 917 | fprintf(output, "PLUS +"); |
| 918 | else if (op->value == 2) |
| 919 | fprintf(output, "PLUS unary -"); |
| 920 | else if (op->value == 3) |
| 921 | fprintf(output, "PLUS unary - -"); |
| 922 | break; |
| 923 | case XPATH_OP_MULT: |
| 924 | if (op->value == 0) |
| 925 | fprintf(output, "MULT *"); |
| 926 | else if (op->value == 1) |
| 927 | fprintf(output, "MULT div"); |
| 928 | else |
| 929 | fprintf(output, "MULT mod"); |
| 930 | break; |
| 931 | case XPATH_OP_UNION: |
| 932 | fprintf(output, "UNION"); break; |
| 933 | case XPATH_OP_ROOT: |
| 934 | fprintf(output, "ROOT"); break; |
| 935 | case XPATH_OP_NODE: |
| 936 | fprintf(output, "NODE"); break; |
| 937 | case XPATH_OP_RESET: |
| 938 | fprintf(output, "RESET"); break; |
| 939 | case XPATH_OP_SORT: |
| 940 | fprintf(output, "SORT"); break; |
| 941 | case XPATH_OP_COLLECT: { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 942 | xmlXPathAxisVal axis = (xmlXPathAxisVal)op->value; |
| 943 | xmlXPathTestVal test = (xmlXPathTestVal)op->value2; |
| 944 | xmlXPathTypeVal type = (xmlXPathTypeVal)op->value3; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 945 | const xmlChar *prefix = op->value4; |
| 946 | const xmlChar *name = op->value5; |
| 947 | |
| 948 | fprintf(output, "COLLECT "); |
| 949 | switch (axis) { |
| 950 | case AXIS_ANCESTOR: |
| 951 | fprintf(output, " 'ancestors' "); break; |
| 952 | case AXIS_ANCESTOR_OR_SELF: |
| 953 | fprintf(output, " 'ancestors-or-self' "); break; |
| 954 | case AXIS_ATTRIBUTE: |
| 955 | fprintf(output, " 'attributes' "); break; |
| 956 | case AXIS_CHILD: |
| 957 | fprintf(output, " 'child' "); break; |
| 958 | case AXIS_DESCENDANT: |
| 959 | fprintf(output, " 'descendant' "); break; |
| 960 | case AXIS_DESCENDANT_OR_SELF: |
| 961 | fprintf(output, " 'descendant-or-self' "); break; |
| 962 | case AXIS_FOLLOWING: |
| 963 | fprintf(output, " 'following' "); break; |
| 964 | case AXIS_FOLLOWING_SIBLING: |
| 965 | fprintf(output, " 'following-siblings' "); break; |
| 966 | case AXIS_NAMESPACE: |
| 967 | fprintf(output, " 'namespace' "); break; |
| 968 | case AXIS_PARENT: |
| 969 | fprintf(output, " 'parent' "); break; |
| 970 | case AXIS_PRECEDING: |
| 971 | fprintf(output, " 'preceding' "); break; |
| 972 | case AXIS_PRECEDING_SIBLING: |
| 973 | fprintf(output, " 'preceding-sibling' "); break; |
| 974 | case AXIS_SELF: |
| 975 | fprintf(output, " 'self' "); break; |
| 976 | } |
| 977 | switch (test) { |
| 978 | case NODE_TEST_NONE: |
| 979 | fprintf(output, "'none' "); break; |
| 980 | case NODE_TEST_TYPE: |
| 981 | fprintf(output, "'type' "); break; |
| 982 | case NODE_TEST_PI: |
| 983 | fprintf(output, "'PI' "); break; |
| 984 | case NODE_TEST_ALL: |
| 985 | fprintf(output, "'all' "); break; |
| 986 | case NODE_TEST_NS: |
| 987 | fprintf(output, "'namespace' "); break; |
| 988 | case NODE_TEST_NAME: |
| 989 | fprintf(output, "'name' "); break; |
| 990 | } |
| 991 | switch (type) { |
| 992 | case NODE_TYPE_NODE: |
| 993 | fprintf(output, "'node' "); break; |
| 994 | case NODE_TYPE_COMMENT: |
| 995 | fprintf(output, "'comment' "); break; |
| 996 | case NODE_TYPE_TEXT: |
| 997 | fprintf(output, "'text' "); break; |
| 998 | case NODE_TYPE_PI: |
| 999 | fprintf(output, "'PI' "); break; |
| 1000 | } |
| 1001 | if (prefix != NULL) |
| 1002 | fprintf(output, "%s:", prefix); |
| 1003 | if (name != NULL) |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1004 | fprintf(output, "%s", (const char *) name); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1005 | break; |
| 1006 | |
| 1007 | } |
| 1008 | case XPATH_OP_VALUE: { |
| 1009 | xmlXPathObjectPtr object = (xmlXPathObjectPtr) op->value4; |
| 1010 | |
| 1011 | fprintf(output, "ELEM "); |
| 1012 | xmlXPathDebugDumpObject(output, object, 0); |
| 1013 | goto finish; |
| 1014 | } |
| 1015 | case XPATH_OP_VARIABLE: { |
| 1016 | const xmlChar *prefix = op->value5; |
| 1017 | const xmlChar *name = op->value4; |
| 1018 | |
| 1019 | if (prefix != NULL) |
| 1020 | fprintf(output, "VARIABLE %s:%s", prefix, name); |
| 1021 | else |
| 1022 | fprintf(output, "VARIABLE %s", name); |
| 1023 | break; |
| 1024 | } |
| 1025 | case XPATH_OP_FUNCTION: { |
| 1026 | int nbargs = op->value; |
| 1027 | const xmlChar *prefix = op->value5; |
| 1028 | const xmlChar *name = op->value4; |
| 1029 | |
| 1030 | if (prefix != NULL) |
| 1031 | fprintf(output, "FUNCTION %s:%s(%d args)", |
| 1032 | prefix, name, nbargs); |
| 1033 | else |
| 1034 | fprintf(output, "FUNCTION %s(%d args)", name, nbargs); |
| 1035 | break; |
| 1036 | } |
| 1037 | case XPATH_OP_ARG: fprintf(output, "ARG"); break; |
| 1038 | case XPATH_OP_PREDICATE: fprintf(output, "PREDICATE"); break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 1039 | case XPATH_OP_FILTER: fprintf(output, "FILTER"); break; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 1040 | #ifdef LIBXML_XPTR_ENABLED |
| 1041 | case XPATH_OP_RANGETO: fprintf(output, "RANGETO"); break; |
| 1042 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1043 | default: |
| 1044 | fprintf(output, "UNKNOWN %d\n", op->op); return; |
| 1045 | } |
| 1046 | fprintf(output, "\n"); |
| 1047 | finish: |
| 1048 | if (op->ch1 >= 0) |
| 1049 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch1], depth + 1); |
| 1050 | if (op->ch2 >= 0) |
| 1051 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[op->ch2], depth + 1); |
| 1052 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1053 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1054 | /** |
| 1055 | * xmlXPathDebugDumpCompExpr: |
| 1056 | * @output: the FILE * for the output |
| 1057 | * @comp: the precompiled XPath expression |
| 1058 | * @depth: the indentation level. |
| 1059 | * |
| 1060 | * Dumps the tree of the compiled XPath expression. |
| 1061 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1062 | void |
| 1063 | xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp, |
| 1064 | int depth) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1065 | int i; |
| 1066 | char shift[100]; |
| 1067 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1068 | if ((output == NULL) || (comp == NULL)) return; |
| 1069 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 1070 | for (i = 0;((i < depth) && (i < 25));i++) |
| 1071 | shift[2 * i] = shift[2 * i + 1] = ' '; |
| 1072 | shift[2 * i] = shift[2 * i + 1] = 0; |
| 1073 | |
| 1074 | fprintf(output, shift); |
| 1075 | |
| 1076 | if (comp == NULL) { |
| 1077 | fprintf(output, "Compiled Expression is NULL\n"); |
| 1078 | return; |
| 1079 | } |
| 1080 | fprintf(output, "Compiled Expression : %d elements\n", |
| 1081 | comp->nbStep); |
| 1082 | i = comp->last; |
| 1083 | xmlXPathDebugDumpStepOp(output, comp, &comp->steps[i], depth + 1); |
| 1084 | } |
Daniel Veillard | 017b108 | 2001-06-21 11:20:21 +0000 | [diff] [blame] | 1085 | #endif /* LIBXML_DEBUG_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1086 | |
| 1087 | /************************************************************************ |
| 1088 | * * |
| 1089 | * Parser stacks related functions and macros * |
| 1090 | * * |
| 1091 | ************************************************************************/ |
| 1092 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1093 | /** |
| 1094 | * valuePop: |
| 1095 | * @ctxt: an XPath evaluation context |
| 1096 | * |
| 1097 | * Pops the top XPath object from the value stack |
| 1098 | * |
| 1099 | * Returns the XPath object just removed |
| 1100 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1101 | extern xmlXPathObjectPtr |
| 1102 | valuePop(xmlXPathParserContextPtr ctxt) |
| 1103 | { |
| 1104 | xmlXPathObjectPtr ret; |
| 1105 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1106 | if ((ctxt == NULL) || (ctxt->valueNr <= 0)) |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1107 | return (0); |
| 1108 | ctxt->valueNr--; |
| 1109 | if (ctxt->valueNr > 0) |
| 1110 | ctxt->value = ctxt->valueTab[ctxt->valueNr - 1]; |
| 1111 | else |
| 1112 | ctxt->value = NULL; |
| 1113 | ret = ctxt->valueTab[ctxt->valueNr]; |
| 1114 | ctxt->valueTab[ctxt->valueNr] = 0; |
| 1115 | return (ret); |
| 1116 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1117 | /** |
| 1118 | * valuePush: |
| 1119 | * @ctxt: an XPath evaluation context |
| 1120 | * @value: the XPath object |
| 1121 | * |
| 1122 | * Pushes a new XPath object on top of the value stack |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1123 | * |
| 1124 | * returns the number of items on the value stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1125 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1126 | extern int |
| 1127 | valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value) |
| 1128 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1129 | if ((ctxt == NULL) || (value == NULL)) return(-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1130 | if (ctxt->valueNr >= ctxt->valueMax) { |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 1131 | xmlXPathObjectPtr *tmp; |
| 1132 | |
| 1133 | tmp = (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab, |
| 1134 | 2 * ctxt->valueMax * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1135 | sizeof(ctxt->valueTab[0])); |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 1136 | if (tmp == NULL) { |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1137 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1138 | return (0); |
| 1139 | } |
Daniel Veillard | a918b5b | 2004-09-26 14:25:37 +0000 | [diff] [blame] | 1140 | ctxt->valueMax *= 2; |
| 1141 | ctxt->valueTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1142 | } |
| 1143 | ctxt->valueTab[ctxt->valueNr] = value; |
| 1144 | ctxt->value = value; |
| 1145 | return (ctxt->valueNr++); |
| 1146 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1147 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1148 | /** |
| 1149 | * xmlXPathPopBoolean: |
| 1150 | * @ctxt: an XPath parser context |
| 1151 | * |
| 1152 | * Pops a boolean from the stack, handling conversion if needed. |
| 1153 | * Check error with #xmlXPathCheckError. |
| 1154 | * |
| 1155 | * Returns the boolean |
| 1156 | */ |
| 1157 | int |
| 1158 | xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt) { |
| 1159 | xmlXPathObjectPtr obj; |
| 1160 | int ret; |
| 1161 | |
| 1162 | obj = valuePop(ctxt); |
| 1163 | if (obj == NULL) { |
| 1164 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1165 | return(0); |
| 1166 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1167 | if (obj->type != XPATH_BOOLEAN) |
| 1168 | ret = xmlXPathCastToBoolean(obj); |
| 1169 | else |
| 1170 | ret = obj->boolval; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1171 | xmlXPathFreeObject(obj); |
| 1172 | return(ret); |
| 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * xmlXPathPopNumber: |
| 1177 | * @ctxt: an XPath parser context |
| 1178 | * |
| 1179 | * Pops a number from the stack, handling conversion if needed. |
| 1180 | * Check error with #xmlXPathCheckError. |
| 1181 | * |
| 1182 | * Returns the number |
| 1183 | */ |
| 1184 | double |
| 1185 | xmlXPathPopNumber (xmlXPathParserContextPtr ctxt) { |
| 1186 | xmlXPathObjectPtr obj; |
| 1187 | double ret; |
| 1188 | |
| 1189 | obj = valuePop(ctxt); |
| 1190 | if (obj == NULL) { |
| 1191 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1192 | return(0); |
| 1193 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1194 | if (obj->type != XPATH_NUMBER) |
| 1195 | ret = xmlXPathCastToNumber(obj); |
| 1196 | else |
| 1197 | ret = obj->floatval; |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1198 | xmlXPathFreeObject(obj); |
| 1199 | return(ret); |
| 1200 | } |
| 1201 | |
| 1202 | /** |
| 1203 | * xmlXPathPopString: |
| 1204 | * @ctxt: an XPath parser context |
| 1205 | * |
| 1206 | * Pops a string from the stack, handling conversion if needed. |
| 1207 | * Check error with #xmlXPathCheckError. |
| 1208 | * |
| 1209 | * Returns the string |
| 1210 | */ |
| 1211 | xmlChar * |
| 1212 | xmlXPathPopString (xmlXPathParserContextPtr ctxt) { |
| 1213 | xmlXPathObjectPtr obj; |
| 1214 | xmlChar * ret; |
| 1215 | |
| 1216 | obj = valuePop(ctxt); |
| 1217 | if (obj == NULL) { |
| 1218 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1219 | return(NULL); |
| 1220 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1221 | ret = xmlXPathCastToString(obj); /* this does required strdup */ |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1222 | /* TODO: needs refactoring somewhere else */ |
| 1223 | if (obj->stringval == ret) |
| 1224 | obj->stringval = NULL; |
| 1225 | xmlXPathFreeObject(obj); |
| 1226 | return(ret); |
| 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * xmlXPathPopNodeSet: |
| 1231 | * @ctxt: an XPath parser context |
| 1232 | * |
| 1233 | * Pops a node-set from the stack, handling conversion if needed. |
| 1234 | * Check error with #xmlXPathCheckError. |
| 1235 | * |
| 1236 | * Returns the node-set |
| 1237 | */ |
| 1238 | xmlNodeSetPtr |
| 1239 | xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt) { |
| 1240 | xmlXPathObjectPtr obj; |
| 1241 | xmlNodeSetPtr ret; |
| 1242 | |
Daniel Veillard | f2a36f9 | 2004-11-08 17:55:01 +0000 | [diff] [blame] | 1243 | if (ctxt == NULL) return(NULL); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1244 | if (ctxt->value == NULL) { |
| 1245 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1246 | return(NULL); |
| 1247 | } |
| 1248 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
| 1249 | xmlXPathSetTypeError(ctxt); |
| 1250 | return(NULL); |
| 1251 | } |
| 1252 | obj = valuePop(ctxt); |
| 1253 | ret = obj->nodesetval; |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 1254 | #if 0 |
Daniel Veillard | 9deb242 | 2003-07-28 20:40:59 +0000 | [diff] [blame] | 1255 | /* to fix memory leak of not clearing obj->user */ |
| 1256 | if (obj->boolval && obj->user != NULL) |
| 1257 | xmlFreeNodeList((xmlNodePtr) obj->user); |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 1258 | #endif |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1259 | xmlXPathFreeNodeSetList(obj); |
| 1260 | return(ret); |
| 1261 | } |
| 1262 | |
| 1263 | /** |
| 1264 | * xmlXPathPopExternal: |
| 1265 | * @ctxt: an XPath parser context |
| 1266 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1267 | * Pops an external object from the stack, handling conversion if needed. |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1268 | * Check error with #xmlXPathCheckError. |
| 1269 | * |
| 1270 | * Returns the object |
| 1271 | */ |
| 1272 | void * |
| 1273 | xmlXPathPopExternal (xmlXPathParserContextPtr ctxt) { |
| 1274 | xmlXPathObjectPtr obj; |
| 1275 | void * ret; |
| 1276 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1277 | if ((ctxt == NULL) || (ctxt->value == NULL)) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1278 | xmlXPathSetError(ctxt, XPATH_INVALID_OPERAND); |
| 1279 | return(NULL); |
| 1280 | } |
| 1281 | if (ctxt->value->type != XPATH_USERS) { |
| 1282 | xmlXPathSetTypeError(ctxt); |
| 1283 | return(NULL); |
| 1284 | } |
| 1285 | obj = valuePop(ctxt); |
| 1286 | ret = obj->user; |
| 1287 | xmlXPathFreeObject(obj); |
| 1288 | return(ret); |
| 1289 | } |
| 1290 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1291 | /* |
| 1292 | * Macros for accessing the content. Those should be used only by the parser, |
| 1293 | * and not exported. |
| 1294 | * |
| 1295 | * Dirty macros, i.e. one need to make assumption on the context to use them |
| 1296 | * |
| 1297 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1298 | * CUR returns the current xmlChar value, i.e. a 8 bit value |
| 1299 | * in ISO-Latin or UTF-8. |
| 1300 | * This should be used internally by the parser |
| 1301 | * only to compare to ASCII values otherwise it would break when |
| 1302 | * running with UTF-8 encoding. |
| 1303 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1304 | * to compare on ASCII based substring. |
| 1305 | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
| 1306 | * strings within the parser. |
| 1307 | * CURRENT Returns the current char value, with the full decoding of |
| 1308 | * UTF-8 if we are using this mode. It returns an int. |
| 1309 | * NEXT Skip to the next character, this does the proper decoding |
| 1310 | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
| 1311 | * It returns the pointer to the current xmlChar. |
| 1312 | */ |
| 1313 | |
| 1314 | #define CUR (*ctxt->cur) |
| 1315 | #define SKIP(val) ctxt->cur += (val) |
| 1316 | #define NXT(val) ctxt->cur[(val)] |
| 1317 | #define CUR_PTR ctxt->cur |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 1318 | #define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l) |
| 1319 | |
| 1320 | #define COPY_BUF(l,b,i,v) \ |
| 1321 | if (l == 1) b[i++] = (xmlChar) v; \ |
| 1322 | else i += xmlCopyChar(l,&b[i],v) |
| 1323 | |
| 1324 | #define NEXTL(l) ctxt->cur += l |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1325 | |
| 1326 | #define SKIP_BLANKS \ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1327 | while (IS_BLANK_CH(*(ctxt->cur))) NEXT |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1328 | |
| 1329 | #define CURRENT (*ctxt->cur) |
| 1330 | #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur) |
| 1331 | |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1332 | |
| 1333 | #ifndef DBL_DIG |
| 1334 | #define DBL_DIG 16 |
| 1335 | #endif |
| 1336 | #ifndef DBL_EPSILON |
| 1337 | #define DBL_EPSILON 1E-9 |
| 1338 | #endif |
| 1339 | |
| 1340 | #define UPPER_DOUBLE 1E9 |
| 1341 | #define LOWER_DOUBLE 1E-5 |
| 1342 | |
| 1343 | #define INTEGER_DIGITS DBL_DIG |
| 1344 | #define FRACTION_DIGITS (DBL_DIG + 1) |
| 1345 | #define EXPONENT_DIGITS (3 + 2) |
| 1346 | |
| 1347 | /** |
| 1348 | * xmlXPathFormatNumber: |
| 1349 | * @number: number to format |
| 1350 | * @buffer: output buffer |
| 1351 | * @buffersize: size of output buffer |
| 1352 | * |
| 1353 | * Convert the number into a string representation. |
| 1354 | */ |
| 1355 | static void |
| 1356 | xmlXPathFormatNumber(double number, char buffer[], int buffersize) |
| 1357 | { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1358 | switch (xmlXPathIsInf(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1359 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 1360 | if (buffersize > (int)sizeof("Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1361 | snprintf(buffer, buffersize, "Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1362 | break; |
| 1363 | case -1: |
| 1364 | if (buffersize > (int)sizeof("-Infinity")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1365 | snprintf(buffer, buffersize, "-Infinity"); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1366 | break; |
| 1367 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 1368 | if (xmlXPathIsNaN(number)) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1369 | if (buffersize > (int)sizeof("NaN")) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1370 | snprintf(buffer, buffersize, "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 1371 | } else if (number == 0 && xmlXPathGetSign(number) != 0) { |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1372 | snprintf(buffer, buffersize, "0"); |
Daniel Veillard | 28cac6b | 2002-03-19 11:25:30 +0000 | [diff] [blame] | 1373 | } else if (number == ((int) number)) { |
| 1374 | char work[30]; |
| 1375 | char *ptr, *cur; |
| 1376 | int res, value = (int) number; |
| 1377 | |
| 1378 | ptr = &buffer[0]; |
| 1379 | if (value < 0) { |
| 1380 | *ptr++ = '-'; |
| 1381 | value = -value; |
| 1382 | } |
| 1383 | if (value == 0) { |
| 1384 | *ptr++ = '0'; |
| 1385 | } else { |
| 1386 | cur = &work[0]; |
| 1387 | while (value != 0) { |
| 1388 | res = value % 10; |
| 1389 | value = value / 10; |
| 1390 | *cur++ = '0' + res; |
| 1391 | } |
| 1392 | cur--; |
| 1393 | while ((cur >= &work[0]) && (ptr - buffer < buffersize)) { |
| 1394 | *ptr++ = *cur--; |
| 1395 | } |
| 1396 | } |
| 1397 | if (ptr - buffer < buffersize) { |
| 1398 | *ptr = 0; |
| 1399 | } else if (buffersize > 0) { |
| 1400 | ptr--; |
| 1401 | *ptr = 0; |
| 1402 | } |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1403 | } else { |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1404 | /* 3 is sign, decimal point, and terminating zero */ |
| 1405 | char work[DBL_DIG + EXPONENT_DIGITS + 3]; |
| 1406 | int integer_place, fraction_place; |
| 1407 | char *ptr; |
| 1408 | char *after_fraction; |
| 1409 | double absolute_value; |
| 1410 | int size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1411 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1412 | absolute_value = fabs(number); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1413 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1414 | /* |
| 1415 | * First choose format - scientific or regular floating point. |
| 1416 | * In either case, result is in work, and after_fraction points |
| 1417 | * just past the fractional part. |
| 1418 | */ |
| 1419 | if ( ((absolute_value > UPPER_DOUBLE) || |
| 1420 | (absolute_value < LOWER_DOUBLE)) && |
| 1421 | (absolute_value != 0.0) ) { |
| 1422 | /* Use scientific notation */ |
| 1423 | integer_place = DBL_DIG + EXPONENT_DIGITS + 1; |
| 1424 | fraction_place = DBL_DIG - 1; |
| 1425 | snprintf(work, sizeof(work),"%*.*e", |
| 1426 | integer_place, fraction_place, number); |
| 1427 | after_fraction = strchr(work + DBL_DIG, 'e'); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1428 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1429 | else { |
| 1430 | /* Use regular notation */ |
Daniel Veillard | 56f0646 | 2001-06-24 21:34:03 +0000 | [diff] [blame] | 1431 | if (absolute_value > 0.0) |
| 1432 | integer_place = 1 + (int)log10(absolute_value); |
| 1433 | else |
Daniel Veillard | a3067d1 | 2001-06-24 21:39:39 +0000 | [diff] [blame] | 1434 | integer_place = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1435 | fraction_place = (integer_place > 0) |
| 1436 | ? DBL_DIG - integer_place |
| 1437 | : DBL_DIG; |
| 1438 | size = snprintf(work, sizeof(work), "%0.*f", |
| 1439 | fraction_place, number); |
| 1440 | after_fraction = work + size; |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1443 | /* Remove fractional trailing zeroes */ |
| 1444 | ptr = after_fraction; |
| 1445 | while (*(--ptr) == '0') |
| 1446 | ; |
| 1447 | if (*ptr != '.') |
| 1448 | ptr++; |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 1449 | while ((*ptr++ = *after_fraction++) != 0); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 1450 | |
| 1451 | /* Finally copy result back to caller */ |
| 1452 | size = strlen(work) + 1; |
| 1453 | if (size > buffersize) { |
| 1454 | work[buffersize - 1] = 0; |
| 1455 | size = buffersize; |
| 1456 | } |
Daniel Veillard | 5dd3c96 | 2003-09-12 15:32:16 +0000 | [diff] [blame] | 1457 | memmove(buffer, work, size); |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1458 | } |
| 1459 | break; |
| 1460 | } |
| 1461 | } |
| 1462 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1463 | |
| 1464 | /************************************************************************ |
| 1465 | * * |
| 1466 | * Routines to handle NodeSets * |
| 1467 | * * |
| 1468 | ************************************************************************/ |
| 1469 | |
| 1470 | /** |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1471 | * xmlXPathOrderDocElems: |
| 1472 | * @doc: an input document |
| 1473 | * |
| 1474 | * Call this routine to speed up XPath computation on static documents. |
| 1475 | * This stamps all the element nodes with the document order |
| 1476 | * Like for line information, the order is kept in the element->content |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1477 | * field, the value stored is actually - the node number (starting at -1) |
| 1478 | * to be able to differentiate from line numbers. |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1479 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1480 | * Returns the number of elements found in the document or -1 in case |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1481 | * of error. |
| 1482 | */ |
| 1483 | long |
| 1484 | xmlXPathOrderDocElems(xmlDocPtr doc) { |
| 1485 | long count = 0; |
| 1486 | xmlNodePtr cur; |
| 1487 | |
| 1488 | if (doc == NULL) |
| 1489 | return(-1); |
| 1490 | cur = doc->children; |
| 1491 | while (cur != NULL) { |
| 1492 | if (cur->type == XML_ELEMENT_NODE) { |
| 1493 | cur->content = (void *) (-(++count)); |
| 1494 | if (cur->children != NULL) { |
| 1495 | cur = cur->children; |
| 1496 | continue; |
| 1497 | } |
| 1498 | } |
| 1499 | if (cur->next != NULL) { |
| 1500 | cur = cur->next; |
| 1501 | continue; |
| 1502 | } |
| 1503 | do { |
| 1504 | cur = cur->parent; |
| 1505 | if (cur == NULL) |
| 1506 | break; |
| 1507 | if (cur == (xmlNodePtr) doc) { |
| 1508 | cur = NULL; |
| 1509 | break; |
| 1510 | } |
| 1511 | if (cur->next != NULL) { |
| 1512 | cur = cur->next; |
| 1513 | break; |
| 1514 | } |
| 1515 | } while (cur != NULL); |
| 1516 | } |
| 1517 | return(count); |
| 1518 | } |
| 1519 | |
| 1520 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1521 | * xmlXPathCmpNodes: |
| 1522 | * @node1: the first node |
| 1523 | * @node2: the second node |
| 1524 | * |
| 1525 | * Compare two nodes w.r.t document order |
| 1526 | * |
| 1527 | * Returns -2 in case of error 1 if first point < second point, 0 if |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1528 | * it's the same node, -1 otherwise |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1529 | */ |
| 1530 | int |
| 1531 | xmlXPathCmpNodes(xmlNodePtr node1, xmlNodePtr node2) { |
| 1532 | int depth1, depth2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1533 | int attr1 = 0, attr2 = 0; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1534 | xmlNodePtr attrNode1 = NULL, attrNode2 = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1535 | xmlNodePtr cur, root; |
| 1536 | |
| 1537 | if ((node1 == NULL) || (node2 == NULL)) |
| 1538 | return(-2); |
| 1539 | /* |
| 1540 | * a couple of optimizations which will avoid computations in most cases |
| 1541 | */ |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1542 | if (node1->type == XML_ATTRIBUTE_NODE) { |
| 1543 | attr1 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1544 | attrNode1 = node1; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1545 | node1 = node1->parent; |
| 1546 | } |
| 1547 | if (node2->type == XML_ATTRIBUTE_NODE) { |
| 1548 | attr2 = 1; |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1549 | attrNode2 = node2; |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1550 | node2 = node2->parent; |
| 1551 | } |
| 1552 | if (node1 == node2) { |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1553 | if (attr1 == attr2) { |
| 1554 | /* not required, but we keep attributes in order */ |
| 1555 | if (attr1 != 0) { |
| 1556 | cur = attrNode2->prev; |
| 1557 | while (cur != NULL) { |
| 1558 | if (cur == attrNode1) |
| 1559 | return (1); |
| 1560 | cur = cur->prev; |
| 1561 | } |
| 1562 | return (-1); |
| 1563 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1564 | return(0); |
William M. Brack | e8d1bd9 | 2003-12-23 01:28:58 +0000 | [diff] [blame] | 1565 | } |
Daniel Veillard | edfd588 | 2003-03-07 14:20:40 +0000 | [diff] [blame] | 1566 | if (attr2 == 1) |
| 1567 | return(1); |
| 1568 | return(-1); |
| 1569 | } |
Daniel Veillard | b33c201 | 2001-04-25 12:59:04 +0000 | [diff] [blame] | 1570 | if ((node1->type == XML_NAMESPACE_DECL) || |
| 1571 | (node2->type == XML_NAMESPACE_DECL)) |
| 1572 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1573 | if (node1 == node2->prev) |
| 1574 | return(1); |
| 1575 | if (node1 == node2->next) |
| 1576 | return(-1); |
| 1577 | |
| 1578 | /* |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1579 | * Speedup using document order if availble. |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1580 | */ |
| 1581 | if ((node1->type == XML_ELEMENT_NODE) && |
| 1582 | (node2->type == XML_ELEMENT_NODE) && |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1583 | (0 > (long) node1->content) && |
| 1584 | (0 > (long) node2->content) && |
| 1585 | (node1->doc == node2->doc)) { |
| 1586 | long l1, l2; |
| 1587 | |
| 1588 | l1 = -((long) node1->content); |
| 1589 | l2 = -((long) node2->content); |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1590 | if (l1 < l2) |
| 1591 | return(1); |
| 1592 | if (l1 > l2) |
| 1593 | return(-1); |
| 1594 | } |
Daniel Veillard | e4fa293 | 2003-03-26 00:38:10 +0000 | [diff] [blame] | 1595 | |
Daniel Veillard | 7216cfd | 2002-11-08 15:10:00 +0000 | [diff] [blame] | 1596 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1597 | * compute depth to root |
| 1598 | */ |
| 1599 | for (depth2 = 0, cur = node2;cur->parent != NULL;cur = cur->parent) { |
| 1600 | if (cur == node1) |
| 1601 | return(1); |
| 1602 | depth2++; |
| 1603 | } |
| 1604 | root = cur; |
| 1605 | for (depth1 = 0, cur = node1;cur->parent != NULL;cur = cur->parent) { |
| 1606 | if (cur == node2) |
| 1607 | return(-1); |
| 1608 | depth1++; |
| 1609 | } |
| 1610 | /* |
| 1611 | * Distinct document (or distinct entities :-( ) case. |
| 1612 | */ |
| 1613 | if (root != cur) { |
| 1614 | return(-2); |
| 1615 | } |
| 1616 | /* |
| 1617 | * get the nearest common ancestor. |
| 1618 | */ |
| 1619 | while (depth1 > depth2) { |
| 1620 | depth1--; |
| 1621 | node1 = node1->parent; |
| 1622 | } |
| 1623 | while (depth2 > depth1) { |
| 1624 | depth2--; |
| 1625 | node2 = node2->parent; |
| 1626 | } |
| 1627 | while (node1->parent != node2->parent) { |
| 1628 | node1 = node1->parent; |
| 1629 | node2 = node2->parent; |
| 1630 | /* should not happen but just in case ... */ |
| 1631 | if ((node1 == NULL) || (node2 == NULL)) |
| 1632 | return(-2); |
| 1633 | } |
| 1634 | /* |
| 1635 | * Find who's first. |
| 1636 | */ |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 1637 | if (node1 == node2->prev) |
| 1638 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1639 | if (node1 == node2->next) |
| 1640 | return(-1); |
Daniel Veillard | f49be47 | 2004-02-17 11:48:18 +0000 | [diff] [blame] | 1641 | /* |
| 1642 | * Speedup using document order if availble. |
| 1643 | */ |
| 1644 | if ((node1->type == XML_ELEMENT_NODE) && |
| 1645 | (node2->type == XML_ELEMENT_NODE) && |
| 1646 | (0 > (long) node1->content) && |
| 1647 | (0 > (long) node2->content) && |
| 1648 | (node1->doc == node2->doc)) { |
| 1649 | long l1, l2; |
| 1650 | |
| 1651 | l1 = -((long) node1->content); |
| 1652 | l2 = -((long) node2->content); |
| 1653 | if (l1 < l2) |
| 1654 | return(1); |
| 1655 | if (l1 > l2) |
| 1656 | return(-1); |
| 1657 | } |
| 1658 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1659 | for (cur = node1->next;cur != NULL;cur = cur->next) |
| 1660 | if (cur == node2) |
| 1661 | return(1); |
| 1662 | return(-1); /* assume there is no sibling list corruption */ |
| 1663 | } |
| 1664 | |
| 1665 | /** |
| 1666 | * xmlXPathNodeSetSort: |
| 1667 | * @set: the node set |
| 1668 | * |
| 1669 | * Sort the node set in document order |
| 1670 | */ |
| 1671 | void |
| 1672 | xmlXPathNodeSetSort(xmlNodeSetPtr set) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1673 | int i, j, incr, len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1674 | xmlNodePtr tmp; |
| 1675 | |
| 1676 | if (set == NULL) |
| 1677 | return; |
| 1678 | |
| 1679 | /* Use Shell's sort to sort the node-set */ |
| 1680 | len = set->nodeNr; |
| 1681 | for (incr = len / 2; incr > 0; incr /= 2) { |
| 1682 | for (i = incr; i < len; i++) { |
| 1683 | j = i - incr; |
| 1684 | while (j >= 0) { |
Bjorn Reese | e1dc011 | 2001-03-03 12:09:03 +0000 | [diff] [blame] | 1685 | if (xmlXPathCmpNodes(set->nodeTab[j], |
| 1686 | set->nodeTab[j + incr]) == -1) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1687 | tmp = set->nodeTab[j]; |
| 1688 | set->nodeTab[j] = set->nodeTab[j + incr]; |
| 1689 | set->nodeTab[j + incr] = tmp; |
| 1690 | j -= incr; |
| 1691 | } else |
| 1692 | break; |
| 1693 | } |
| 1694 | } |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | #define XML_NODESET_DEFAULT 10 |
| 1699 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1700 | * xmlXPathNodeSetDupNs: |
| 1701 | * @node: the parent node of the namespace XPath node |
| 1702 | * @ns: the libxml namespace declaration node. |
| 1703 | * |
| 1704 | * Namespace node in libxml don't match the XPath semantic. In a node set |
| 1705 | * the namespace nodes are duplicated and the next pointer is set to the |
| 1706 | * parent node in the XPath semantic. |
| 1707 | * |
| 1708 | * Returns the newly created object. |
| 1709 | */ |
| 1710 | static xmlNodePtr |
| 1711 | xmlXPathNodeSetDupNs(xmlNodePtr node, xmlNsPtr ns) { |
| 1712 | xmlNsPtr cur; |
| 1713 | |
| 1714 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 1715 | return(NULL); |
| 1716 | if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) |
| 1717 | return((xmlNodePtr) ns); |
| 1718 | |
| 1719 | /* |
| 1720 | * Allocate a new Namespace and fill the fields. |
| 1721 | */ |
| 1722 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 1723 | if (cur == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1724 | xmlXPathErrMemory(NULL, "duplicating namespace\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1725 | return(NULL); |
| 1726 | } |
| 1727 | memset(cur, 0, sizeof(xmlNs)); |
| 1728 | cur->type = XML_NAMESPACE_DECL; |
| 1729 | if (ns->href != NULL) |
| 1730 | cur->href = xmlStrdup(ns->href); |
| 1731 | if (ns->prefix != NULL) |
| 1732 | cur->prefix = xmlStrdup(ns->prefix); |
| 1733 | cur->next = (xmlNsPtr) node; |
| 1734 | return((xmlNodePtr) cur); |
| 1735 | } |
| 1736 | |
| 1737 | /** |
| 1738 | * xmlXPathNodeSetFreeNs: |
| 1739 | * @ns: the XPath namespace node found in a nodeset. |
| 1740 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1741 | * Namespace nodes in libxml don't match the XPath semantic. In a node set |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1742 | * the namespace nodes are duplicated and the next pointer is set to the |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1743 | * parent node in the XPath semantic. Check if such a node needs to be freed |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1744 | */ |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 1745 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1746 | xmlXPathNodeSetFreeNs(xmlNsPtr ns) { |
| 1747 | if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) |
| 1748 | return; |
| 1749 | |
| 1750 | if ((ns->next != NULL) && (ns->next->type != XML_NAMESPACE_DECL)) { |
| 1751 | if (ns->href != NULL) |
| 1752 | xmlFree((xmlChar *)ns->href); |
| 1753 | if (ns->prefix != NULL) |
| 1754 | xmlFree((xmlChar *)ns->prefix); |
| 1755 | xmlFree(ns); |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1760 | * xmlXPathNodeSetCreate: |
| 1761 | * @val: an initial xmlNodePtr, or NULL |
| 1762 | * |
| 1763 | * Create a new xmlNodeSetPtr of type double and of value @val |
| 1764 | * |
| 1765 | * Returns the newly created object. |
| 1766 | */ |
| 1767 | xmlNodeSetPtr |
| 1768 | xmlXPathNodeSetCreate(xmlNodePtr val) { |
| 1769 | xmlNodeSetPtr ret; |
| 1770 | |
| 1771 | ret = (xmlNodeSetPtr) xmlMalloc(sizeof(xmlNodeSet)); |
| 1772 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1773 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1774 | return(NULL); |
| 1775 | } |
| 1776 | memset(ret, 0 , (size_t) sizeof(xmlNodeSet)); |
| 1777 | if (val != NULL) { |
| 1778 | ret->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1779 | sizeof(xmlNodePtr)); |
| 1780 | if (ret->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1781 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
| 1782 | xmlFree(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1783 | return(NULL); |
| 1784 | } |
| 1785 | memset(ret->nodeTab, 0 , |
| 1786 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1787 | ret->nodeMax = XML_NODESET_DEFAULT; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1788 | if (val->type == XML_NAMESPACE_DECL) { |
| 1789 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1790 | |
| 1791 | ret->nodeTab[ret->nodeNr++] = |
| 1792 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1793 | } else |
| 1794 | ret->nodeTab[ret->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1795 | } |
| 1796 | return(ret); |
| 1797 | } |
| 1798 | |
| 1799 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1800 | * xmlXPathNodeSetContains: |
| 1801 | * @cur: the node-set |
| 1802 | * @val: the node |
| 1803 | * |
| 1804 | * checks whether @cur contains @val |
| 1805 | * |
| 1806 | * Returns true (1) if @cur contains @val, false (0) otherwise |
| 1807 | */ |
| 1808 | int |
| 1809 | xmlXPathNodeSetContains (xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1810 | int i; |
| 1811 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1812 | if ((cur == NULL) || (val == NULL)) return(0); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1813 | if (val->type == XML_NAMESPACE_DECL) { |
| 1814 | for (i = 0; i < cur->nodeNr; i++) { |
| 1815 | if (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 1816 | xmlNsPtr ns1, ns2; |
| 1817 | |
| 1818 | ns1 = (xmlNsPtr) val; |
| 1819 | ns2 = (xmlNsPtr) cur->nodeTab[i]; |
| 1820 | if (ns1 == ns2) |
| 1821 | return(1); |
| 1822 | if ((ns1->next != NULL) && (ns2->next == ns1->next) && |
| 1823 | (xmlStrEqual(ns1->prefix, ns2->prefix))) |
| 1824 | return(1); |
| 1825 | } |
| 1826 | } |
| 1827 | } else { |
| 1828 | for (i = 0; i < cur->nodeNr; i++) { |
| 1829 | if (cur->nodeTab[i] == val) |
| 1830 | return(1); |
| 1831 | } |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 1832 | } |
| 1833 | return(0); |
| 1834 | } |
| 1835 | |
| 1836 | /** |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1837 | * xmlXPathNodeSetAddNs: |
| 1838 | * @cur: the initial node set |
| 1839 | * @node: the hosting node |
| 1840 | * @ns: a the namespace node |
| 1841 | * |
| 1842 | * add a new namespace node to an existing NodeSet |
| 1843 | */ |
Aleksey Sanin | 79376ba | 2002-05-14 06:41:32 +0000 | [diff] [blame] | 1844 | void |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1845 | xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) { |
| 1846 | int i; |
| 1847 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1848 | |
| 1849 | if ((cur == NULL) || (ns == NULL) || (node == NULL) || |
| 1850 | (ns->type != XML_NAMESPACE_DECL) || |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1851 | (node->type != XML_ELEMENT_NODE)) |
| 1852 | return; |
| 1853 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1854 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1855 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1856 | * prevent duplicates |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1857 | */ |
| 1858 | for (i = 0;i < cur->nodeNr;i++) { |
| 1859 | if ((cur->nodeTab[i] != NULL) && |
| 1860 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) && |
Daniel Veillard | c62a147 | 2002-03-19 18:35:12 +0000 | [diff] [blame] | 1861 | (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) && |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1862 | (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix))) |
| 1863 | return; |
| 1864 | } |
| 1865 | |
| 1866 | /* |
| 1867 | * grow the nodeTab if needed |
| 1868 | */ |
| 1869 | if (cur->nodeMax == 0) { |
| 1870 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1871 | sizeof(xmlNodePtr)); |
| 1872 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1873 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1874 | return; |
| 1875 | } |
| 1876 | memset(cur->nodeTab, 0 , |
| 1877 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1878 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1879 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1880 | xmlNodePtr *temp; |
| 1881 | |
| 1882 | cur->nodeMax *= 2; |
| 1883 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1884 | sizeof(xmlNodePtr)); |
| 1885 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1886 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1887 | return; |
| 1888 | } |
| 1889 | cur->nodeTab = temp; |
| 1890 | } |
| 1891 | cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns); |
| 1892 | } |
| 1893 | |
| 1894 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1895 | * xmlXPathNodeSetAdd: |
| 1896 | * @cur: the initial node set |
| 1897 | * @val: a new xmlNodePtr |
| 1898 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1899 | * add a new xmlNodePtr to an existing NodeSet |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1900 | */ |
| 1901 | void |
| 1902 | xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 1903 | int i; |
| 1904 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1905 | if ((cur == NULL) || (val == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1906 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1907 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1908 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 1909 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1910 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1911 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1912 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1913 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1914 | * prevent duplcates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1915 | */ |
| 1916 | for (i = 0;i < cur->nodeNr;i++) |
| 1917 | if (cur->nodeTab[i] == val) return; |
| 1918 | |
| 1919 | /* |
| 1920 | * grow the nodeTab if needed |
| 1921 | */ |
| 1922 | if (cur->nodeMax == 0) { |
| 1923 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1924 | sizeof(xmlNodePtr)); |
| 1925 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1926 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1927 | return; |
| 1928 | } |
| 1929 | memset(cur->nodeTab, 0 , |
| 1930 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1931 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1932 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1933 | xmlNodePtr *temp; |
| 1934 | |
| 1935 | cur->nodeMax *= 2; |
| 1936 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1937 | sizeof(xmlNodePtr)); |
| 1938 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1939 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1940 | return; |
| 1941 | } |
| 1942 | cur->nodeTab = temp; |
| 1943 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1944 | if (val->type == XML_NAMESPACE_DECL) { |
| 1945 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1946 | |
| 1947 | cur->nodeTab[cur->nodeNr++] = |
| 1948 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 1949 | } else |
| 1950 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | /** |
| 1954 | * xmlXPathNodeSetAddUnique: |
| 1955 | * @cur: the initial node set |
| 1956 | * @val: a new xmlNodePtr |
| 1957 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1958 | * add a new xmlNodePtr to an existing NodeSet, optimized version |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1959 | * when we are sure the node is not already in the set. |
| 1960 | */ |
| 1961 | void |
| 1962 | xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1963 | if ((cur == NULL) || (val == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1964 | |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1965 | #if 0 |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1966 | if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) |
| 1967 | return; /* an XSLT fake node */ |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 1968 | #endif |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 1969 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 1970 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1971 | /* |
| 1972 | * grow the nodeTab if needed |
| 1973 | */ |
| 1974 | if (cur->nodeMax == 0) { |
| 1975 | cur->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 1976 | sizeof(xmlNodePtr)); |
| 1977 | if (cur->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1978 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1979 | return; |
| 1980 | } |
| 1981 | memset(cur->nodeTab, 0 , |
| 1982 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 1983 | cur->nodeMax = XML_NODESET_DEFAULT; |
| 1984 | } else if (cur->nodeNr == cur->nodeMax) { |
| 1985 | xmlNodePtr *temp; |
| 1986 | |
| 1987 | cur->nodeMax *= 2; |
| 1988 | temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * |
| 1989 | sizeof(xmlNodePtr)); |
| 1990 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 1991 | xmlXPathErrMemory(NULL, "growing nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1992 | return; |
| 1993 | } |
| 1994 | cur->nodeTab = temp; |
| 1995 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 1996 | if (val->type == XML_NAMESPACE_DECL) { |
| 1997 | xmlNsPtr ns = (xmlNsPtr) val; |
| 1998 | |
| 1999 | cur->nodeTab[cur->nodeNr++] = |
| 2000 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2001 | } else |
| 2002 | cur->nodeTab[cur->nodeNr++] = val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | /** |
| 2006 | * xmlXPathNodeSetMerge: |
| 2007 | * @val1: the first NodeSet or NULL |
| 2008 | * @val2: the second NodeSet |
| 2009 | * |
| 2010 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 2011 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 2012 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2013 | * Returns @val1 once extended or NULL in case of error. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2014 | */ |
| 2015 | xmlNodeSetPtr |
| 2016 | xmlXPathNodeSetMerge(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2017 | int i, j, initNr, skip; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2018 | |
| 2019 | if (val2 == NULL) return(val1); |
| 2020 | if (val1 == NULL) { |
| 2021 | val1 = xmlXPathNodeSetCreate(NULL); |
| 2022 | } |
| 2023 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2024 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2025 | initNr = val1->nodeNr; |
| 2026 | |
| 2027 | for (i = 0;i < val2->nodeNr;i++) { |
| 2028 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2029 | * check against duplicates |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2030 | */ |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2031 | skip = 0; |
| 2032 | for (j = 0; j < initNr; j++) { |
| 2033 | if (val1->nodeTab[j] == val2->nodeTab[i]) { |
| 2034 | skip = 1; |
| 2035 | break; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2036 | } else if ((val1->nodeTab[j]->type == XML_NAMESPACE_DECL) && |
| 2037 | (val2->nodeTab[i]->type == XML_NAMESPACE_DECL)) { |
| 2038 | xmlNsPtr ns1, ns2; |
| 2039 | ns1 = (xmlNsPtr) val1->nodeTab[j]; |
| 2040 | ns2 = (xmlNsPtr) val2->nodeTab[i]; |
| 2041 | if ((ns1->next == ns2->next) && |
| 2042 | (xmlStrEqual(ns1->prefix, ns2->prefix))) { |
| 2043 | skip = 1; |
| 2044 | break; |
| 2045 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 2046 | } |
| 2047 | } |
| 2048 | if (skip) |
| 2049 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2050 | |
| 2051 | /* |
| 2052 | * grow the nodeTab if needed |
| 2053 | */ |
| 2054 | if (val1->nodeMax == 0) { |
| 2055 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2056 | sizeof(xmlNodePtr)); |
| 2057 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2058 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2059 | return(NULL); |
| 2060 | } |
| 2061 | memset(val1->nodeTab, 0 , |
| 2062 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2063 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 2064 | } else if (val1->nodeNr == val1->nodeMax) { |
| 2065 | xmlNodePtr *temp; |
| 2066 | |
| 2067 | val1->nodeMax *= 2; |
| 2068 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 2069 | sizeof(xmlNodePtr)); |
| 2070 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2071 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2072 | return(NULL); |
| 2073 | } |
| 2074 | val1->nodeTab = temp; |
| 2075 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2076 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2077 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 2078 | |
| 2079 | val1->nodeTab[val1->nodeNr++] = |
| 2080 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2081 | } else |
| 2082 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | return(val1); |
| 2086 | } |
| 2087 | |
| 2088 | /** |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2089 | * xmlXPathNodeSetMergeUnique: |
| 2090 | * @val1: the first NodeSet or NULL |
| 2091 | * @val2: the second NodeSet |
| 2092 | * |
| 2093 | * Merges two nodesets, all nodes from @val2 are added to @val1 |
| 2094 | * if @val1 is NULL, a new set is created and copied from @val2 |
| 2095 | * |
| 2096 | * Returns @val1 once extended or NULL in case of error. |
| 2097 | */ |
| 2098 | static xmlNodeSetPtr |
| 2099 | xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 2100 | int i; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2101 | |
| 2102 | if (val2 == NULL) return(val1); |
| 2103 | if (val1 == NULL) { |
| 2104 | val1 = xmlXPathNodeSetCreate(NULL); |
| 2105 | } |
| 2106 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2107 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2108 | |
| 2109 | for (i = 0;i < val2->nodeNr;i++) { |
| 2110 | /* |
| 2111 | * grow the nodeTab if needed |
| 2112 | */ |
| 2113 | if (val1->nodeMax == 0) { |
| 2114 | val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * |
| 2115 | sizeof(xmlNodePtr)); |
| 2116 | if (val1->nodeTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2117 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2118 | return(NULL); |
| 2119 | } |
| 2120 | memset(val1->nodeTab, 0 , |
| 2121 | XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); |
| 2122 | val1->nodeMax = XML_NODESET_DEFAULT; |
| 2123 | } else if (val1->nodeNr == val1->nodeMax) { |
| 2124 | xmlNodePtr *temp; |
| 2125 | |
| 2126 | val1->nodeMax *= 2; |
| 2127 | temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * |
| 2128 | sizeof(xmlNodePtr)); |
| 2129 | if (temp == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2130 | xmlXPathErrMemory(NULL, "merging nodeset\n"); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 2131 | return(NULL); |
| 2132 | } |
| 2133 | val1->nodeTab = temp; |
| 2134 | } |
| 2135 | if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2136 | xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; |
| 2137 | |
| 2138 | val1->nodeTab[val1->nodeNr++] = |
| 2139 | xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); |
| 2140 | } else |
| 2141 | val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; |
| 2142 | } |
| 2143 | |
| 2144 | return(val1); |
| 2145 | } |
| 2146 | |
| 2147 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2148 | * xmlXPathNodeSetDel: |
| 2149 | * @cur: the initial node set |
| 2150 | * @val: an xmlNodePtr |
| 2151 | * |
| 2152 | * Removes an xmlNodePtr from an existing NodeSet |
| 2153 | */ |
| 2154 | void |
| 2155 | xmlXPathNodeSetDel(xmlNodeSetPtr cur, xmlNodePtr val) { |
| 2156 | int i; |
| 2157 | |
| 2158 | if (cur == NULL) return; |
| 2159 | if (val == NULL) return; |
| 2160 | |
| 2161 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2162 | * find node in nodeTab |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2163 | */ |
| 2164 | for (i = 0;i < cur->nodeNr;i++) |
| 2165 | if (cur->nodeTab[i] == val) break; |
| 2166 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2167 | if (i >= cur->nodeNr) { /* not found */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2168 | #ifdef DEBUG |
| 2169 | xmlGenericError(xmlGenericErrorContext, |
| 2170 | "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n", |
| 2171 | val->name); |
| 2172 | #endif |
| 2173 | return; |
| 2174 | } |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2175 | if ((cur->nodeTab[i] != NULL) && |
| 2176 | (cur->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 2177 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2178 | cur->nodeNr--; |
| 2179 | for (;i < cur->nodeNr;i++) |
| 2180 | cur->nodeTab[i] = cur->nodeTab[i + 1]; |
| 2181 | cur->nodeTab[cur->nodeNr] = NULL; |
| 2182 | } |
| 2183 | |
| 2184 | /** |
| 2185 | * xmlXPathNodeSetRemove: |
| 2186 | * @cur: the initial node set |
| 2187 | * @val: the index to remove |
| 2188 | * |
| 2189 | * Removes an entry from an existing NodeSet list. |
| 2190 | */ |
| 2191 | void |
| 2192 | xmlXPathNodeSetRemove(xmlNodeSetPtr cur, int val) { |
| 2193 | if (cur == NULL) return; |
| 2194 | if (val >= cur->nodeNr) return; |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2195 | if ((cur->nodeTab[val] != NULL) && |
| 2196 | (cur->nodeTab[val]->type == XML_NAMESPACE_DECL)) |
| 2197 | xmlXPathNodeSetFreeNs((xmlNsPtr) cur->nodeTab[val]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2198 | cur->nodeNr--; |
| 2199 | for (;val < cur->nodeNr;val++) |
| 2200 | cur->nodeTab[val] = cur->nodeTab[val + 1]; |
| 2201 | cur->nodeTab[cur->nodeNr] = NULL; |
| 2202 | } |
| 2203 | |
| 2204 | /** |
| 2205 | * xmlXPathFreeNodeSet: |
| 2206 | * @obj: the xmlNodeSetPtr to free |
| 2207 | * |
| 2208 | * Free the NodeSet compound (not the actual nodes !). |
| 2209 | */ |
| 2210 | void |
| 2211 | xmlXPathFreeNodeSet(xmlNodeSetPtr obj) { |
| 2212 | if (obj == NULL) return; |
| 2213 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2214 | int i; |
| 2215 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2216 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2217 | for (i = 0;i < obj->nodeNr;i++) |
| 2218 | if ((obj->nodeTab[i] != NULL) && |
| 2219 | (obj->nodeTab[i]->type == XML_NAMESPACE_DECL)) |
| 2220 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2221 | xmlFree(obj->nodeTab); |
| 2222 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2223 | xmlFree(obj); |
| 2224 | } |
| 2225 | |
| 2226 | /** |
| 2227 | * xmlXPathFreeValueTree: |
| 2228 | * @obj: the xmlNodeSetPtr to free |
| 2229 | * |
| 2230 | * Free the NodeSet compound and the actual tree, this is different |
| 2231 | * from xmlXPathFreeNodeSet() |
| 2232 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2233 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2234 | xmlXPathFreeValueTree(xmlNodeSetPtr obj) { |
| 2235 | int i; |
| 2236 | |
| 2237 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2238 | |
| 2239 | if (obj->nodeTab != NULL) { |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2240 | for (i = 0;i < obj->nodeNr;i++) { |
| 2241 | if (obj->nodeTab[i] != NULL) { |
| 2242 | if (obj->nodeTab[i]->type == XML_NAMESPACE_DECL) { |
| 2243 | xmlXPathNodeSetFreeNs((xmlNsPtr) obj->nodeTab[i]); |
| 2244 | } else { |
| 2245 | xmlFreeNodeList(obj->nodeTab[i]); |
| 2246 | } |
| 2247 | } |
| 2248 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2249 | xmlFree(obj->nodeTab); |
| 2250 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2251 | xmlFree(obj); |
| 2252 | } |
| 2253 | |
| 2254 | #if defined(DEBUG) || defined(DEBUG_STEP) |
| 2255 | /** |
| 2256 | * xmlGenericErrorContextNodeSet: |
| 2257 | * @output: a FILE * for the output |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2258 | * @obj: the xmlNodeSetPtr to display |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2259 | * |
| 2260 | * Quick display of a NodeSet |
| 2261 | */ |
| 2262 | void |
| 2263 | xmlGenericErrorContextNodeSet(FILE *output, xmlNodeSetPtr obj) { |
| 2264 | int i; |
| 2265 | |
| 2266 | if (output == NULL) output = xmlGenericErrorContext; |
| 2267 | if (obj == NULL) { |
| 2268 | fprintf(output, "NodeSet == NULL !\n"); |
| 2269 | return; |
| 2270 | } |
| 2271 | if (obj->nodeNr == 0) { |
| 2272 | fprintf(output, "NodeSet is empty\n"); |
| 2273 | return; |
| 2274 | } |
| 2275 | if (obj->nodeTab == NULL) { |
| 2276 | fprintf(output, " nodeTab == NULL !\n"); |
| 2277 | return; |
| 2278 | } |
| 2279 | for (i = 0; i < obj->nodeNr; i++) { |
| 2280 | if (obj->nodeTab[i] == NULL) { |
| 2281 | fprintf(output, " NULL !\n"); |
| 2282 | return; |
| 2283 | } |
| 2284 | if ((obj->nodeTab[i]->type == XML_DOCUMENT_NODE) || |
| 2285 | (obj->nodeTab[i]->type == XML_HTML_DOCUMENT_NODE)) |
| 2286 | fprintf(output, " /"); |
| 2287 | else if (obj->nodeTab[i]->name == NULL) |
| 2288 | fprintf(output, " noname!"); |
| 2289 | else fprintf(output, " %s", obj->nodeTab[i]->name); |
| 2290 | } |
| 2291 | fprintf(output, "\n"); |
| 2292 | } |
| 2293 | #endif |
| 2294 | |
| 2295 | /** |
| 2296 | * xmlXPathNewNodeSet: |
| 2297 | * @val: the NodePtr value |
| 2298 | * |
| 2299 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2300 | * it with the single Node @val |
| 2301 | * |
| 2302 | * Returns the newly created object. |
| 2303 | */ |
| 2304 | xmlXPathObjectPtr |
| 2305 | xmlXPathNewNodeSet(xmlNodePtr val) { |
| 2306 | xmlXPathObjectPtr ret; |
| 2307 | |
| 2308 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2309 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2310 | xmlXPathErrMemory(NULL, "creating nodeset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2311 | return(NULL); |
| 2312 | } |
| 2313 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2314 | ret->type = XPATH_NODESET; |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 2315 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2316 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2317 | /* @@ with_ns to check whether namespace nodes should be looked at @@ */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2318 | return(ret); |
| 2319 | } |
| 2320 | |
| 2321 | /** |
| 2322 | * xmlXPathNewValueTree: |
| 2323 | * @val: the NodePtr value |
| 2324 | * |
| 2325 | * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize |
| 2326 | * it with the tree root @val |
| 2327 | * |
| 2328 | * Returns the newly created object. |
| 2329 | */ |
| 2330 | xmlXPathObjectPtr |
| 2331 | xmlXPathNewValueTree(xmlNodePtr val) { |
| 2332 | xmlXPathObjectPtr ret; |
| 2333 | |
| 2334 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2335 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2336 | xmlXPathErrMemory(NULL, "creating result value tree\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2337 | return(NULL); |
| 2338 | } |
| 2339 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2340 | ret->type = XPATH_XSLT_TREE; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 2341 | ret->boolval = 1; |
| 2342 | ret->user = (void *) val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2343 | ret->nodesetval = xmlXPathNodeSetCreate(val); |
| 2344 | return(ret); |
| 2345 | } |
| 2346 | |
| 2347 | /** |
| 2348 | * xmlXPathNewNodeSetList: |
| 2349 | * @val: an existing NodeSet |
| 2350 | * |
| 2351 | * Create a new xmlXPathObjectPtr of type NodeSet and initialize |
| 2352 | * it with the Nodeset @val |
| 2353 | * |
| 2354 | * Returns the newly created object. |
| 2355 | */ |
| 2356 | xmlXPathObjectPtr |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2357 | xmlXPathNewNodeSetList(xmlNodeSetPtr val) |
| 2358 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2359 | xmlXPathObjectPtr ret; |
| 2360 | int i; |
| 2361 | |
| 2362 | if (val == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2363 | ret = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2364 | else if (val->nodeTab == NULL) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2365 | ret = xmlXPathNewNodeSet(NULL); |
| 2366 | else { |
| 2367 | ret = xmlXPathNewNodeSet(val->nodeTab[0]); |
| 2368 | for (i = 1; i < val->nodeNr; ++i) |
| 2369 | xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]); |
| 2370 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2371 | |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 2372 | return (ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | /** |
| 2376 | * xmlXPathWrapNodeSet: |
| 2377 | * @val: the NodePtr value |
| 2378 | * |
| 2379 | * Wrap the Nodeset @val in a new xmlXPathObjectPtr |
| 2380 | * |
| 2381 | * Returns the newly created object. |
| 2382 | */ |
| 2383 | xmlXPathObjectPtr |
| 2384 | xmlXPathWrapNodeSet(xmlNodeSetPtr val) { |
| 2385 | xmlXPathObjectPtr ret; |
| 2386 | |
| 2387 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 2388 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 2389 | xmlXPathErrMemory(NULL, "creating node set object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2390 | return(NULL); |
| 2391 | } |
| 2392 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 2393 | ret->type = XPATH_NODESET; |
| 2394 | ret->nodesetval = val; |
| 2395 | return(ret); |
| 2396 | } |
| 2397 | |
| 2398 | /** |
| 2399 | * xmlXPathFreeNodeSetList: |
| 2400 | * @obj: an existing NodeSetList object |
| 2401 | * |
| 2402 | * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in |
| 2403 | * the list contrary to xmlXPathFreeObject(). |
| 2404 | */ |
| 2405 | void |
| 2406 | xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj) { |
| 2407 | if (obj == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2408 | xmlFree(obj); |
| 2409 | } |
| 2410 | |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2411 | /** |
| 2412 | * xmlXPathDifference: |
| 2413 | * @nodes1: a node-set |
| 2414 | * @nodes2: a node-set |
| 2415 | * |
| 2416 | * Implements the EXSLT - Sets difference() function: |
| 2417 | * node-set set:difference (node-set, node-set) |
| 2418 | * |
| 2419 | * Returns the difference between the two node sets, or nodes1 if |
| 2420 | * nodes2 is empty |
| 2421 | */ |
| 2422 | xmlNodeSetPtr |
| 2423 | xmlXPathDifference (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2424 | xmlNodeSetPtr ret; |
| 2425 | int i, l1; |
| 2426 | xmlNodePtr cur; |
| 2427 | |
| 2428 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2429 | return(nodes1); |
| 2430 | |
| 2431 | ret = xmlXPathNodeSetCreate(NULL); |
| 2432 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2433 | return(ret); |
| 2434 | |
| 2435 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2436 | |
| 2437 | for (i = 0; i < l1; i++) { |
| 2438 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2439 | if (!xmlXPathNodeSetContains(nodes2, cur)) |
| 2440 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2441 | } |
| 2442 | return(ret); |
| 2443 | } |
| 2444 | |
| 2445 | /** |
| 2446 | * xmlXPathIntersection: |
| 2447 | * @nodes1: a node-set |
| 2448 | * @nodes2: a node-set |
| 2449 | * |
| 2450 | * Implements the EXSLT - Sets intersection() function: |
| 2451 | * node-set set:intersection (node-set, node-set) |
| 2452 | * |
| 2453 | * Returns a node set comprising the nodes that are within both the |
| 2454 | * node sets passed as arguments |
| 2455 | */ |
| 2456 | xmlNodeSetPtr |
| 2457 | xmlXPathIntersection (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2458 | xmlNodeSetPtr ret = xmlXPathNodeSetCreate(NULL); |
| 2459 | int i, l1; |
| 2460 | xmlNodePtr cur; |
| 2461 | |
| 2462 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2463 | return(ret); |
| 2464 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2465 | return(ret); |
| 2466 | |
| 2467 | l1 = xmlXPathNodeSetGetLength(nodes1); |
| 2468 | |
| 2469 | for (i = 0; i < l1; i++) { |
| 2470 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2471 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2472 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2473 | } |
| 2474 | return(ret); |
| 2475 | } |
| 2476 | |
| 2477 | /** |
| 2478 | * xmlXPathDistinctSorted: |
| 2479 | * @nodes: a node-set, sorted by document order |
| 2480 | * |
| 2481 | * Implements the EXSLT - Sets distinct() function: |
| 2482 | * node-set set:distinct (node-set) |
| 2483 | * |
| 2484 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2485 | * it is empty |
| 2486 | */ |
| 2487 | xmlNodeSetPtr |
| 2488 | xmlXPathDistinctSorted (xmlNodeSetPtr nodes) { |
| 2489 | xmlNodeSetPtr ret; |
| 2490 | xmlHashTablePtr hash; |
| 2491 | int i, l; |
| 2492 | xmlChar * strval; |
| 2493 | xmlNodePtr cur; |
| 2494 | |
| 2495 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2496 | return(nodes); |
| 2497 | |
| 2498 | ret = xmlXPathNodeSetCreate(NULL); |
| 2499 | l = xmlXPathNodeSetGetLength(nodes); |
| 2500 | hash = xmlHashCreate (l); |
| 2501 | for (i = 0; i < l; i++) { |
| 2502 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2503 | strval = xmlXPathCastNodeToString(cur); |
| 2504 | if (xmlHashLookup(hash, strval) == NULL) { |
| 2505 | xmlHashAddEntry(hash, strval, strval); |
| 2506 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2507 | } else { |
| 2508 | xmlFree(strval); |
| 2509 | } |
| 2510 | } |
| 2511 | xmlHashFree(hash, (xmlHashDeallocator) xmlFree); |
| 2512 | return(ret); |
| 2513 | } |
| 2514 | |
| 2515 | /** |
| 2516 | * xmlXPathDistinct: |
| 2517 | * @nodes: a node-set |
| 2518 | * |
| 2519 | * Implements the EXSLT - Sets distinct() function: |
| 2520 | * node-set set:distinct (node-set) |
| 2521 | * @nodes is sorted by document order, then #exslSetsDistinctSorted |
| 2522 | * is called with the sorted node-set |
| 2523 | * |
| 2524 | * Returns a subset of the nodes contained in @nodes, or @nodes if |
| 2525 | * it is empty |
| 2526 | */ |
| 2527 | xmlNodeSetPtr |
| 2528 | xmlXPathDistinct (xmlNodeSetPtr nodes) { |
| 2529 | if (xmlXPathNodeSetIsEmpty(nodes)) |
| 2530 | return(nodes); |
| 2531 | |
| 2532 | xmlXPathNodeSetSort(nodes); |
| 2533 | return(xmlXPathDistinctSorted(nodes)); |
| 2534 | } |
| 2535 | |
| 2536 | /** |
| 2537 | * xmlXPathHasSameNodes: |
| 2538 | * @nodes1: a node-set |
| 2539 | * @nodes2: a node-set |
| 2540 | * |
| 2541 | * Implements the EXSLT - Sets has-same-nodes function: |
| 2542 | * boolean set:has-same-node(node-set, node-set) |
| 2543 | * |
| 2544 | * Returns true (1) if @nodes1 shares any node with @nodes2, false (0) |
| 2545 | * otherwise |
| 2546 | */ |
| 2547 | int |
| 2548 | xmlXPathHasSameNodes (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2549 | int i, l; |
| 2550 | xmlNodePtr cur; |
| 2551 | |
| 2552 | if (xmlXPathNodeSetIsEmpty(nodes1) || |
| 2553 | xmlXPathNodeSetIsEmpty(nodes2)) |
| 2554 | return(0); |
| 2555 | |
| 2556 | l = xmlXPathNodeSetGetLength(nodes1); |
| 2557 | for (i = 0; i < l; i++) { |
| 2558 | cur = xmlXPathNodeSetItem(nodes1, i); |
| 2559 | if (xmlXPathNodeSetContains(nodes2, cur)) |
| 2560 | return(1); |
| 2561 | } |
| 2562 | return(0); |
| 2563 | } |
| 2564 | |
| 2565 | /** |
| 2566 | * xmlXPathNodeLeadingSorted: |
| 2567 | * @nodes: a node-set, sorted by document order |
| 2568 | * @node: a node |
| 2569 | * |
| 2570 | * Implements the EXSLT - Sets leading() function: |
| 2571 | * node-set set:leading (node-set, node-set) |
| 2572 | * |
| 2573 | * Returns the nodes in @nodes that precede @node in document order, |
| 2574 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2575 | * doesn't contain @node |
| 2576 | */ |
| 2577 | xmlNodeSetPtr |
| 2578 | xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2579 | int i, l; |
| 2580 | xmlNodePtr cur; |
| 2581 | xmlNodeSetPtr ret; |
| 2582 | |
| 2583 | if (node == NULL) |
| 2584 | return(nodes); |
| 2585 | |
| 2586 | ret = xmlXPathNodeSetCreate(NULL); |
| 2587 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2588 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2589 | return(ret); |
| 2590 | |
| 2591 | l = xmlXPathNodeSetGetLength(nodes); |
| 2592 | for (i = 0; i < l; i++) { |
| 2593 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2594 | if (cur == node) |
| 2595 | break; |
| 2596 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2597 | } |
| 2598 | return(ret); |
| 2599 | } |
| 2600 | |
| 2601 | /** |
| 2602 | * xmlXPathNodeLeading: |
| 2603 | * @nodes: a node-set |
| 2604 | * @node: a node |
| 2605 | * |
| 2606 | * Implements the EXSLT - Sets leading() function: |
| 2607 | * node-set set:leading (node-set, node-set) |
| 2608 | * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted |
| 2609 | * is called. |
| 2610 | * |
| 2611 | * Returns the nodes in @nodes that precede @node in document order, |
| 2612 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2613 | * doesn't contain @node |
| 2614 | */ |
| 2615 | xmlNodeSetPtr |
| 2616 | xmlXPathNodeLeading (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2617 | xmlXPathNodeSetSort(nodes); |
| 2618 | return(xmlXPathNodeLeadingSorted(nodes, node)); |
| 2619 | } |
| 2620 | |
| 2621 | /** |
| 2622 | * xmlXPathLeadingSorted: |
| 2623 | * @nodes1: a node-set, sorted by document order |
| 2624 | * @nodes2: a node-set, sorted by document order |
| 2625 | * |
| 2626 | * Implements the EXSLT - Sets leading() function: |
| 2627 | * node-set set:leading (node-set, node-set) |
| 2628 | * |
| 2629 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 2630 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2631 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2632 | */ |
| 2633 | xmlNodeSetPtr |
| 2634 | xmlXPathLeadingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2635 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2636 | return(nodes1); |
| 2637 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 2638 | xmlXPathNodeSetItem(nodes2, 1))); |
| 2639 | } |
| 2640 | |
| 2641 | /** |
| 2642 | * xmlXPathLeading: |
| 2643 | * @nodes1: a node-set |
| 2644 | * @nodes2: a node-set |
| 2645 | * |
| 2646 | * Implements the EXSLT - Sets leading() function: |
| 2647 | * node-set set:leading (node-set, node-set) |
| 2648 | * @nodes1 and @nodes2 are sorted by document order, then |
| 2649 | * #exslSetsLeadingSorted is called. |
| 2650 | * |
| 2651 | * Returns the nodes in @nodes1 that precede the first node in @nodes2 |
| 2652 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2653 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2654 | */ |
| 2655 | xmlNodeSetPtr |
| 2656 | xmlXPathLeading (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2657 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2658 | return(nodes1); |
| 2659 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2660 | return(xmlXPathNodeSetCreate(NULL)); |
| 2661 | xmlXPathNodeSetSort(nodes1); |
| 2662 | xmlXPathNodeSetSort(nodes2); |
| 2663 | return(xmlXPathNodeLeadingSorted(nodes1, |
| 2664 | xmlXPathNodeSetItem(nodes2, 1))); |
| 2665 | } |
| 2666 | |
| 2667 | /** |
| 2668 | * xmlXPathNodeTrailingSorted: |
| 2669 | * @nodes: a node-set, sorted by document order |
| 2670 | * @node: a node |
| 2671 | * |
| 2672 | * Implements the EXSLT - Sets trailing() function: |
| 2673 | * node-set set:trailing (node-set, node-set) |
| 2674 | * |
| 2675 | * Returns the nodes in @nodes that follow @node in document order, |
| 2676 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2677 | * doesn't contain @node |
| 2678 | */ |
| 2679 | xmlNodeSetPtr |
| 2680 | xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2681 | int i, l; |
| 2682 | xmlNodePtr cur; |
| 2683 | xmlNodeSetPtr ret; |
| 2684 | |
| 2685 | if (node == NULL) |
| 2686 | return(nodes); |
| 2687 | |
| 2688 | ret = xmlXPathNodeSetCreate(NULL); |
| 2689 | if (xmlXPathNodeSetIsEmpty(nodes) || |
| 2690 | (!xmlXPathNodeSetContains(nodes, node))) |
| 2691 | return(ret); |
| 2692 | |
| 2693 | l = xmlXPathNodeSetGetLength(nodes); |
Thomas Broyer | f186c82 | 2001-07-31 23:30:37 +0000 | [diff] [blame] | 2694 | for (i = l; i > 0; i--) { |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 2695 | cur = xmlXPathNodeSetItem(nodes, i); |
| 2696 | if (cur == node) |
| 2697 | break; |
| 2698 | xmlXPathNodeSetAddUnique(ret, cur); |
| 2699 | } |
| 2700 | return(ret); |
| 2701 | } |
| 2702 | |
| 2703 | /** |
| 2704 | * xmlXPathNodeTrailing: |
| 2705 | * @nodes: a node-set |
| 2706 | * @node: a node |
| 2707 | * |
| 2708 | * Implements the EXSLT - Sets trailing() function: |
| 2709 | * node-set set:trailing (node-set, node-set) |
| 2710 | * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted |
| 2711 | * is called. |
| 2712 | * |
| 2713 | * Returns the nodes in @nodes that follow @node in document order, |
| 2714 | * @nodes if @node is NULL or an empty node-set if @nodes |
| 2715 | * doesn't contain @node |
| 2716 | */ |
| 2717 | xmlNodeSetPtr |
| 2718 | xmlXPathNodeTrailing (xmlNodeSetPtr nodes, xmlNodePtr node) { |
| 2719 | xmlXPathNodeSetSort(nodes); |
| 2720 | return(xmlXPathNodeTrailingSorted(nodes, node)); |
| 2721 | } |
| 2722 | |
| 2723 | /** |
| 2724 | * xmlXPathTrailingSorted: |
| 2725 | * @nodes1: a node-set, sorted by document order |
| 2726 | * @nodes2: a node-set, sorted by document order |
| 2727 | * |
| 2728 | * Implements the EXSLT - Sets trailing() function: |
| 2729 | * node-set set:trailing (node-set, node-set) |
| 2730 | * |
| 2731 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 2732 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2733 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2734 | */ |
| 2735 | xmlNodeSetPtr |
| 2736 | xmlXPathTrailingSorted (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2737 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2738 | return(nodes1); |
| 2739 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 2740 | xmlXPathNodeSetItem(nodes2, 0))); |
| 2741 | } |
| 2742 | |
| 2743 | /** |
| 2744 | * xmlXPathTrailing: |
| 2745 | * @nodes1: a node-set |
| 2746 | * @nodes2: a node-set |
| 2747 | * |
| 2748 | * Implements the EXSLT - Sets trailing() function: |
| 2749 | * node-set set:trailing (node-set, node-set) |
| 2750 | * @nodes1 and @nodes2 are sorted by document order, then |
| 2751 | * #xmlXPathTrailingSorted is called. |
| 2752 | * |
| 2753 | * Returns the nodes in @nodes1 that follow the first node in @nodes2 |
| 2754 | * in document order, @nodes1 if @nodes2 is NULL or empty or |
| 2755 | * an empty node-set if @nodes1 doesn't contain @nodes2 |
| 2756 | */ |
| 2757 | xmlNodeSetPtr |
| 2758 | xmlXPathTrailing (xmlNodeSetPtr nodes1, xmlNodeSetPtr nodes2) { |
| 2759 | if (xmlXPathNodeSetIsEmpty(nodes2)) |
| 2760 | return(nodes1); |
| 2761 | if (xmlXPathNodeSetIsEmpty(nodes1)) |
| 2762 | return(xmlXPathNodeSetCreate(NULL)); |
| 2763 | xmlXPathNodeSetSort(nodes1); |
| 2764 | xmlXPathNodeSetSort(nodes2); |
| 2765 | return(xmlXPathNodeTrailingSorted(nodes1, |
| 2766 | xmlXPathNodeSetItem(nodes2, 0))); |
| 2767 | } |
| 2768 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2769 | /************************************************************************ |
| 2770 | * * |
| 2771 | * Routines to handle extra functions * |
| 2772 | * * |
| 2773 | ************************************************************************/ |
| 2774 | |
| 2775 | /** |
| 2776 | * xmlXPathRegisterFunc: |
| 2777 | * @ctxt: the XPath context |
| 2778 | * @name: the function name |
| 2779 | * @f: the function implementation or NULL |
| 2780 | * |
| 2781 | * Register a new function. If @f is NULL it unregisters the function |
| 2782 | * |
| 2783 | * Returns 0 in case of success, -1 in case of error |
| 2784 | */ |
| 2785 | int |
| 2786 | xmlXPathRegisterFunc(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2787 | xmlXPathFunction f) { |
| 2788 | return(xmlXPathRegisterFuncNS(ctxt, name, NULL, f)); |
| 2789 | } |
| 2790 | |
| 2791 | /** |
| 2792 | * xmlXPathRegisterFuncNS: |
| 2793 | * @ctxt: the XPath context |
| 2794 | * @name: the function name |
| 2795 | * @ns_uri: the function namespace URI |
| 2796 | * @f: the function implementation or NULL |
| 2797 | * |
| 2798 | * Register a new function. If @f is NULL it unregisters the function |
| 2799 | * |
| 2800 | * Returns 0 in case of success, -1 in case of error |
| 2801 | */ |
| 2802 | int |
| 2803 | xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2804 | const xmlChar *ns_uri, xmlXPathFunction f) { |
| 2805 | if (ctxt == NULL) |
| 2806 | return(-1); |
| 2807 | if (name == NULL) |
| 2808 | return(-1); |
| 2809 | |
| 2810 | if (ctxt->funcHash == NULL) |
| 2811 | ctxt->funcHash = xmlHashCreate(0); |
| 2812 | if (ctxt->funcHash == NULL) |
| 2813 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 2814 | if (f == NULL) |
| 2815 | return(xmlHashRemoveEntry2(ctxt->funcHash, name, ns_uri, NULL)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2816 | return(xmlHashAddEntry2(ctxt->funcHash, name, ns_uri, (void *) f)); |
| 2817 | } |
| 2818 | |
| 2819 | /** |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2820 | * xmlXPathRegisterFuncLookup: |
| 2821 | * @ctxt: the XPath context |
| 2822 | * @f: the lookup function |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2823 | * @funcCtxt: the lookup data |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2824 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2825 | * Registers an external mechanism to do function lookup. |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2826 | */ |
| 2827 | void |
| 2828 | xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt, |
| 2829 | xmlXPathFuncLookupFunc f, |
| 2830 | void *funcCtxt) { |
| 2831 | if (ctxt == NULL) |
| 2832 | return; |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 2833 | ctxt->funcLookupFunc = f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2834 | ctxt->funcLookupData = funcCtxt; |
| 2835 | } |
| 2836 | |
| 2837 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2838 | * xmlXPathFunctionLookup: |
| 2839 | * @ctxt: the XPath context |
| 2840 | * @name: the function name |
| 2841 | * |
| 2842 | * Search in the Function array of the context for the given |
| 2843 | * function. |
| 2844 | * |
| 2845 | * Returns the xmlXPathFunction or NULL if not found |
| 2846 | */ |
| 2847 | xmlXPathFunction |
| 2848 | xmlXPathFunctionLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2849 | if (ctxt == NULL) |
| 2850 | return (NULL); |
| 2851 | |
| 2852 | if (ctxt->funcLookupFunc != NULL) { |
| 2853 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2854 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2855 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 2856 | f = ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2857 | ret = f(ctxt->funcLookupData, name, NULL); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2858 | if (ret != NULL) |
| 2859 | return(ret); |
| 2860 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2861 | return(xmlXPathFunctionLookupNS(ctxt, name, NULL)); |
| 2862 | } |
| 2863 | |
| 2864 | /** |
| 2865 | * xmlXPathFunctionLookupNS: |
| 2866 | * @ctxt: the XPath context |
| 2867 | * @name: the function name |
| 2868 | * @ns_uri: the function namespace URI |
| 2869 | * |
| 2870 | * Search in the Function array of the context for the given |
| 2871 | * function. |
| 2872 | * |
| 2873 | * Returns the xmlXPathFunction or NULL if not found |
| 2874 | */ |
| 2875 | xmlXPathFunction |
| 2876 | xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2877 | const xmlChar *ns_uri) { |
| 2878 | if (ctxt == NULL) |
| 2879 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2880 | if (name == NULL) |
| 2881 | return(NULL); |
| 2882 | |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2883 | if (ctxt->funcLookupFunc != NULL) { |
| 2884 | xmlXPathFunction ret; |
Daniel Veillard | 99e55eb | 2002-01-21 08:56:29 +0000 | [diff] [blame] | 2885 | xmlXPathFuncLookupFunc f; |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2886 | |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 2887 | f = ctxt->funcLookupFunc; |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2888 | ret = f(ctxt->funcLookupData, name, ns_uri); |
Thomas Broyer | ba4ad32 | 2001-07-26 16:55:21 +0000 | [diff] [blame] | 2889 | if (ret != NULL) |
| 2890 | return(ret); |
| 2891 | } |
| 2892 | |
| 2893 | if (ctxt->funcHash == NULL) |
| 2894 | return(NULL); |
| 2895 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2896 | return((xmlXPathFunction) xmlHashLookup2(ctxt->funcHash, name, ns_uri)); |
| 2897 | } |
| 2898 | |
| 2899 | /** |
| 2900 | * xmlXPathRegisteredFuncsCleanup: |
| 2901 | * @ctxt: the XPath context |
| 2902 | * |
| 2903 | * Cleanup the XPath context data associated to registered functions |
| 2904 | */ |
| 2905 | void |
| 2906 | xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt) { |
| 2907 | if (ctxt == NULL) |
| 2908 | return; |
| 2909 | |
| 2910 | xmlHashFree(ctxt->funcHash, NULL); |
| 2911 | ctxt->funcHash = NULL; |
| 2912 | } |
| 2913 | |
| 2914 | /************************************************************************ |
| 2915 | * * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 2916 | * Routines to handle Variables * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2917 | * * |
| 2918 | ************************************************************************/ |
| 2919 | |
| 2920 | /** |
| 2921 | * xmlXPathRegisterVariable: |
| 2922 | * @ctxt: the XPath context |
| 2923 | * @name: the variable name |
| 2924 | * @value: the variable value or NULL |
| 2925 | * |
| 2926 | * Register a new variable value. If @value is NULL it unregisters |
| 2927 | * the variable |
| 2928 | * |
| 2929 | * Returns 0 in case of success, -1 in case of error |
| 2930 | */ |
| 2931 | int |
| 2932 | xmlXPathRegisterVariable(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2933 | xmlXPathObjectPtr value) { |
| 2934 | return(xmlXPathRegisterVariableNS(ctxt, name, NULL, value)); |
| 2935 | } |
| 2936 | |
| 2937 | /** |
| 2938 | * xmlXPathRegisterVariableNS: |
| 2939 | * @ctxt: the XPath context |
| 2940 | * @name: the variable name |
| 2941 | * @ns_uri: the variable namespace URI |
| 2942 | * @value: the variable value or NULL |
| 2943 | * |
| 2944 | * Register a new variable value. If @value is NULL it unregisters |
| 2945 | * the variable |
| 2946 | * |
| 2947 | * Returns 0 in case of success, -1 in case of error |
| 2948 | */ |
| 2949 | int |
| 2950 | xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 2951 | const xmlChar *ns_uri, |
| 2952 | xmlXPathObjectPtr value) { |
| 2953 | if (ctxt == NULL) |
| 2954 | return(-1); |
| 2955 | if (name == NULL) |
| 2956 | return(-1); |
| 2957 | |
| 2958 | if (ctxt->varHash == NULL) |
| 2959 | ctxt->varHash = xmlHashCreate(0); |
| 2960 | if (ctxt->varHash == NULL) |
| 2961 | return(-1); |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 2962 | if (value == NULL) |
| 2963 | return(xmlHashRemoveEntry2(ctxt->varHash, name, ns_uri, |
| 2964 | (xmlHashDeallocator)xmlXPathFreeObject)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2965 | return(xmlHashUpdateEntry2(ctxt->varHash, name, ns_uri, |
| 2966 | (void *) value, |
| 2967 | (xmlHashDeallocator)xmlXPathFreeObject)); |
| 2968 | } |
| 2969 | |
| 2970 | /** |
| 2971 | * xmlXPathRegisterVariableLookup: |
| 2972 | * @ctxt: the XPath context |
| 2973 | * @f: the lookup function |
| 2974 | * @data: the lookup data |
| 2975 | * |
| 2976 | * register an external mechanism to do variable lookup |
| 2977 | */ |
| 2978 | void |
| 2979 | xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt, |
| 2980 | xmlXPathVariableLookupFunc f, void *data) { |
| 2981 | if (ctxt == NULL) |
| 2982 | return; |
Daniel Veillard | 6ebf3c4 | 2004-08-22 13:11:39 +0000 | [diff] [blame] | 2983 | ctxt->varLookupFunc = f; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2984 | ctxt->varLookupData = data; |
| 2985 | } |
| 2986 | |
| 2987 | /** |
| 2988 | * xmlXPathVariableLookup: |
| 2989 | * @ctxt: the XPath context |
| 2990 | * @name: the variable name |
| 2991 | * |
| 2992 | * Search in the Variable array of the context for the given |
| 2993 | * variable value. |
| 2994 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 2995 | * Returns a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2996 | */ |
| 2997 | xmlXPathObjectPtr |
| 2998 | xmlXPathVariableLookup(xmlXPathContextPtr ctxt, const xmlChar *name) { |
| 2999 | if (ctxt == NULL) |
| 3000 | return(NULL); |
| 3001 | |
| 3002 | if (ctxt->varLookupFunc != NULL) { |
| 3003 | xmlXPathObjectPtr ret; |
| 3004 | |
| 3005 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 3006 | (ctxt->varLookupData, name, NULL); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 3007 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3008 | } |
| 3009 | return(xmlXPathVariableLookupNS(ctxt, name, NULL)); |
| 3010 | } |
| 3011 | |
| 3012 | /** |
| 3013 | * xmlXPathVariableLookupNS: |
| 3014 | * @ctxt: the XPath context |
| 3015 | * @name: the variable name |
| 3016 | * @ns_uri: the variable namespace URI |
| 3017 | * |
| 3018 | * Search in the Variable array of the context for the given |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 3019 | * variable value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3020 | * |
Daniel Veillard | 73c9c04 | 2001-07-05 20:02:54 +0000 | [diff] [blame] | 3021 | * Returns the a copy of the value or NULL if not found |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3022 | */ |
| 3023 | xmlXPathObjectPtr |
| 3024 | xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt, const xmlChar *name, |
| 3025 | const xmlChar *ns_uri) { |
| 3026 | if (ctxt == NULL) |
| 3027 | return(NULL); |
| 3028 | |
| 3029 | if (ctxt->varLookupFunc != NULL) { |
| 3030 | xmlXPathObjectPtr ret; |
| 3031 | |
| 3032 | ret = ((xmlXPathVariableLookupFunc)ctxt->varLookupFunc) |
| 3033 | (ctxt->varLookupData, name, ns_uri); |
| 3034 | if (ret != NULL) return(ret); |
| 3035 | } |
| 3036 | |
| 3037 | if (ctxt->varHash == NULL) |
| 3038 | return(NULL); |
| 3039 | if (name == NULL) |
| 3040 | return(NULL); |
| 3041 | |
Daniel Veillard | 8c357d5 | 2001-07-03 23:43:33 +0000 | [diff] [blame] | 3042 | return(xmlXPathObjectCopy((xmlXPathObjectPtr) |
| 3043 | xmlHashLookup2(ctxt->varHash, name, ns_uri))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | /** |
| 3047 | * xmlXPathRegisteredVariablesCleanup: |
| 3048 | * @ctxt: the XPath context |
| 3049 | * |
| 3050 | * Cleanup the XPath context data associated to registered variables |
| 3051 | */ |
| 3052 | void |
| 3053 | xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt) { |
| 3054 | if (ctxt == NULL) |
| 3055 | return; |
| 3056 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3057 | xmlHashFree(ctxt->varHash, (xmlHashDeallocator)xmlXPathFreeObject); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3058 | ctxt->varHash = NULL; |
| 3059 | } |
| 3060 | |
| 3061 | /** |
| 3062 | * xmlXPathRegisterNs: |
| 3063 | * @ctxt: the XPath context |
| 3064 | * @prefix: the namespace prefix |
| 3065 | * @ns_uri: the namespace name |
| 3066 | * |
| 3067 | * Register a new namespace. If @ns_uri is NULL it unregisters |
| 3068 | * the namespace |
| 3069 | * |
| 3070 | * Returns 0 in case of success, -1 in case of error |
| 3071 | */ |
| 3072 | int |
| 3073 | xmlXPathRegisterNs(xmlXPathContextPtr ctxt, const xmlChar *prefix, |
| 3074 | const xmlChar *ns_uri) { |
| 3075 | if (ctxt == NULL) |
| 3076 | return(-1); |
| 3077 | if (prefix == NULL) |
| 3078 | return(-1); |
| 3079 | |
| 3080 | if (ctxt->nsHash == NULL) |
| 3081 | ctxt->nsHash = xmlHashCreate(10); |
| 3082 | if (ctxt->nsHash == NULL) |
| 3083 | return(-1); |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 3084 | if (ns_uri == NULL) |
Daniel Veillard | 94394cd | 2003-10-29 17:07:51 +0000 | [diff] [blame] | 3085 | return(xmlHashRemoveEntry(ctxt->nsHash, prefix, |
Daniel Veillard | e991fe9 | 2003-10-29 11:18:37 +0000 | [diff] [blame] | 3086 | (xmlHashDeallocator)xmlFree)); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 3087 | return(xmlHashUpdateEntry(ctxt->nsHash, prefix, (void *) xmlStrdup(ns_uri), |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3088 | (xmlHashDeallocator)xmlFree)); |
| 3089 | } |
| 3090 | |
| 3091 | /** |
| 3092 | * xmlXPathNsLookup: |
| 3093 | * @ctxt: the XPath context |
| 3094 | * @prefix: the namespace prefix value |
| 3095 | * |
| 3096 | * Search in the namespace declaration array of the context for the given |
| 3097 | * namespace name associated to the given prefix |
| 3098 | * |
| 3099 | * Returns the value or NULL if not found |
| 3100 | */ |
| 3101 | const xmlChar * |
| 3102 | xmlXPathNsLookup(xmlXPathContextPtr ctxt, const xmlChar *prefix) { |
| 3103 | if (ctxt == NULL) |
| 3104 | return(NULL); |
| 3105 | if (prefix == NULL) |
| 3106 | return(NULL); |
| 3107 | |
| 3108 | #ifdef XML_XML_NAMESPACE |
| 3109 | if (xmlStrEqual(prefix, (const xmlChar *) "xml")) |
| 3110 | return(XML_XML_NAMESPACE); |
| 3111 | #endif |
| 3112 | |
Daniel Veillard | c8f620b | 2001-04-30 20:31:33 +0000 | [diff] [blame] | 3113 | if (ctxt->namespaces != NULL) { |
| 3114 | int i; |
| 3115 | |
| 3116 | for (i = 0;i < ctxt->nsNr;i++) { |
| 3117 | if ((ctxt->namespaces[i] != NULL) && |
| 3118 | (xmlStrEqual(ctxt->namespaces[i]->prefix, prefix))) |
| 3119 | return(ctxt->namespaces[i]->href); |
| 3120 | } |
| 3121 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3122 | |
| 3123 | return((const xmlChar *) xmlHashLookup(ctxt->nsHash, prefix)); |
| 3124 | } |
| 3125 | |
| 3126 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3127 | * xmlXPathRegisteredNsCleanup: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3128 | * @ctxt: the XPath context |
| 3129 | * |
| 3130 | * Cleanup the XPath context data associated to registered variables |
| 3131 | */ |
| 3132 | void |
| 3133 | xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt) { |
| 3134 | if (ctxt == NULL) |
| 3135 | return; |
| 3136 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 3137 | xmlHashFree(ctxt->nsHash, (xmlHashDeallocator)xmlFree); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3138 | ctxt->nsHash = NULL; |
| 3139 | } |
| 3140 | |
| 3141 | /************************************************************************ |
| 3142 | * * |
| 3143 | * Routines to handle Values * |
| 3144 | * * |
| 3145 | ************************************************************************/ |
| 3146 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 3147 | /* Allocations are terrible, one needs to optimize all this !!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3148 | |
| 3149 | /** |
| 3150 | * xmlXPathNewFloat: |
| 3151 | * @val: the double value |
| 3152 | * |
| 3153 | * Create a new xmlXPathObjectPtr of type double and of value @val |
| 3154 | * |
| 3155 | * Returns the newly created object. |
| 3156 | */ |
| 3157 | xmlXPathObjectPtr |
| 3158 | xmlXPathNewFloat(double val) { |
| 3159 | xmlXPathObjectPtr ret; |
| 3160 | |
| 3161 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3162 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3163 | xmlXPathErrMemory(NULL, "creating float object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3164 | return(NULL); |
| 3165 | } |
| 3166 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3167 | ret->type = XPATH_NUMBER; |
| 3168 | ret->floatval = val; |
| 3169 | return(ret); |
| 3170 | } |
| 3171 | |
| 3172 | /** |
| 3173 | * xmlXPathNewBoolean: |
| 3174 | * @val: the boolean value |
| 3175 | * |
| 3176 | * Create a new xmlXPathObjectPtr of type boolean and of value @val |
| 3177 | * |
| 3178 | * Returns the newly created object. |
| 3179 | */ |
| 3180 | xmlXPathObjectPtr |
| 3181 | xmlXPathNewBoolean(int val) { |
| 3182 | xmlXPathObjectPtr ret; |
| 3183 | |
| 3184 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3185 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3186 | xmlXPathErrMemory(NULL, "creating boolean object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3187 | return(NULL); |
| 3188 | } |
| 3189 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3190 | ret->type = XPATH_BOOLEAN; |
| 3191 | ret->boolval = (val != 0); |
| 3192 | return(ret); |
| 3193 | } |
| 3194 | |
| 3195 | /** |
| 3196 | * xmlXPathNewString: |
| 3197 | * @val: the xmlChar * value |
| 3198 | * |
| 3199 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 3200 | * |
| 3201 | * Returns the newly created object. |
| 3202 | */ |
| 3203 | xmlXPathObjectPtr |
| 3204 | xmlXPathNewString(const xmlChar *val) { |
| 3205 | xmlXPathObjectPtr ret; |
| 3206 | |
| 3207 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3208 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3209 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3210 | return(NULL); |
| 3211 | } |
| 3212 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3213 | ret->type = XPATH_STRING; |
| 3214 | if (val != NULL) |
| 3215 | ret->stringval = xmlStrdup(val); |
| 3216 | else |
| 3217 | ret->stringval = xmlStrdup((const xmlChar *)""); |
| 3218 | return(ret); |
| 3219 | } |
| 3220 | |
| 3221 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3222 | * xmlXPathWrapString: |
| 3223 | * @val: the xmlChar * value |
| 3224 | * |
| 3225 | * Wraps the @val string into an XPath object. |
| 3226 | * |
| 3227 | * Returns the newly created object. |
| 3228 | */ |
| 3229 | xmlXPathObjectPtr |
| 3230 | xmlXPathWrapString (xmlChar *val) { |
| 3231 | xmlXPathObjectPtr ret; |
| 3232 | |
| 3233 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3234 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3235 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3236 | return(NULL); |
| 3237 | } |
| 3238 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3239 | ret->type = XPATH_STRING; |
| 3240 | ret->stringval = val; |
| 3241 | return(ret); |
| 3242 | } |
| 3243 | |
| 3244 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3245 | * xmlXPathNewCString: |
| 3246 | * @val: the char * value |
| 3247 | * |
| 3248 | * Create a new xmlXPathObjectPtr of type string and of value @val |
| 3249 | * |
| 3250 | * Returns the newly created object. |
| 3251 | */ |
| 3252 | xmlXPathObjectPtr |
| 3253 | xmlXPathNewCString(const char *val) { |
| 3254 | xmlXPathObjectPtr ret; |
| 3255 | |
| 3256 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3257 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3258 | xmlXPathErrMemory(NULL, "creating string object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3259 | return(NULL); |
| 3260 | } |
| 3261 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3262 | ret->type = XPATH_STRING; |
| 3263 | ret->stringval = xmlStrdup(BAD_CAST val); |
| 3264 | return(ret); |
| 3265 | } |
| 3266 | |
| 3267 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3268 | * xmlXPathWrapCString: |
| 3269 | * @val: the char * value |
| 3270 | * |
| 3271 | * Wraps a string into an XPath object. |
| 3272 | * |
| 3273 | * Returns the newly created object. |
| 3274 | */ |
| 3275 | xmlXPathObjectPtr |
| 3276 | xmlXPathWrapCString (char * val) { |
| 3277 | return(xmlXPathWrapString((xmlChar *)(val))); |
| 3278 | } |
| 3279 | |
| 3280 | /** |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3281 | * xmlXPathWrapExternal: |
| 3282 | * @val: the user data |
| 3283 | * |
| 3284 | * Wraps the @val data into an XPath object. |
| 3285 | * |
| 3286 | * Returns the newly created object. |
| 3287 | */ |
| 3288 | xmlXPathObjectPtr |
| 3289 | xmlXPathWrapExternal (void *val) { |
| 3290 | xmlXPathObjectPtr ret; |
| 3291 | |
| 3292 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3293 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3294 | xmlXPathErrMemory(NULL, "creating user object\n"); |
Thomas Broyer | f06a3d8 | 2001-07-16 04:52:57 +0000 | [diff] [blame] | 3295 | return(NULL); |
| 3296 | } |
| 3297 | memset(ret, 0 , (size_t) sizeof(xmlXPathObject)); |
| 3298 | ret->type = XPATH_USERS; |
| 3299 | ret->user = val; |
| 3300 | return(ret); |
| 3301 | } |
| 3302 | |
| 3303 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3304 | * xmlXPathObjectCopy: |
| 3305 | * @val: the original object |
| 3306 | * |
| 3307 | * allocate a new copy of a given object |
| 3308 | * |
| 3309 | * Returns the newly created object. |
| 3310 | */ |
| 3311 | xmlXPathObjectPtr |
| 3312 | xmlXPathObjectCopy(xmlXPathObjectPtr val) { |
| 3313 | xmlXPathObjectPtr ret; |
| 3314 | |
| 3315 | if (val == NULL) |
| 3316 | return(NULL); |
| 3317 | |
| 3318 | ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject)); |
| 3319 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3320 | xmlXPathErrMemory(NULL, "copying object\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3321 | return(NULL); |
| 3322 | } |
| 3323 | memcpy(ret, val , (size_t) sizeof(xmlXPathObject)); |
| 3324 | switch (val->type) { |
| 3325 | case XPATH_BOOLEAN: |
| 3326 | case XPATH_NUMBER: |
| 3327 | case XPATH_POINT: |
| 3328 | case XPATH_RANGE: |
| 3329 | break; |
| 3330 | case XPATH_STRING: |
| 3331 | ret->stringval = xmlStrdup(val->stringval); |
| 3332 | break; |
| 3333 | case XPATH_XSLT_TREE: |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3334 | #if 0 |
| 3335 | /* |
| 3336 | Removed 11 July 2004 - the current handling of xslt tmpRVT nodes means that |
| 3337 | this previous handling is no longer correct, and can cause some serious |
| 3338 | problems (ref. bug 145547) |
| 3339 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3340 | if ((val->nodesetval != NULL) && |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3341 | (val->nodesetval->nodeTab != NULL)) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3342 | xmlNodePtr cur, tmp; |
| 3343 | xmlDocPtr top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3344 | |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3345 | ret->boolval = 1; |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3346 | top = xmlNewDoc(NULL); |
| 3347 | top->name = (char *) |
| 3348 | xmlStrdup(val->nodesetval->nodeTab[0]->name); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3349 | ret->user = top; |
| 3350 | if (top != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3351 | top->doc = top; |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3352 | cur = val->nodesetval->nodeTab[0]->children; |
| 3353 | while (cur != NULL) { |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3354 | tmp = xmlDocCopyNode(cur, top, 1); |
| 3355 | xmlAddChild((xmlNodePtr) top, tmp); |
Daniel Veillard | ef0b450 | 2003-03-24 13:57:34 +0000 | [diff] [blame] | 3356 | cur = cur->next; |
| 3357 | } |
| 3358 | } |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3359 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 3360 | ret->nodesetval = xmlXPathNodeSetCreate((xmlNodePtr) top); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3361 | } else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3362 | ret->nodesetval = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3363 | /* Deallocate the copied tree value */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3364 | break; |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3365 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3366 | case XPATH_NODESET: |
| 3367 | ret->nodesetval = xmlXPathNodeSetMerge(NULL, val->nodesetval); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3368 | /* Do not deallocate the copied tree value */ |
| 3369 | ret->boolval = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3370 | break; |
| 3371 | case XPATH_LOCATIONSET: |
| 3372 | #ifdef LIBXML_XPTR_ENABLED |
| 3373 | { |
| 3374 | xmlLocationSetPtr loc = val->user; |
| 3375 | ret->user = (void *) xmlXPtrLocationSetMerge(NULL, loc); |
| 3376 | break; |
| 3377 | } |
| 3378 | #endif |
Thomas Broyer | 47334c0 | 2001-10-07 16:41:52 +0000 | [diff] [blame] | 3379 | case XPATH_USERS: |
| 3380 | ret->user = val->user; |
| 3381 | break; |
| 3382 | case XPATH_UNDEFINED: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3383 | xmlGenericError(xmlGenericErrorContext, |
| 3384 | "xmlXPathObjectCopy: unsupported type %d\n", |
| 3385 | val->type); |
| 3386 | break; |
| 3387 | } |
| 3388 | return(ret); |
| 3389 | } |
| 3390 | |
| 3391 | /** |
| 3392 | * xmlXPathFreeObject: |
| 3393 | * @obj: the object to free |
| 3394 | * |
| 3395 | * Free up an xmlXPathObjectPtr object. |
| 3396 | */ |
| 3397 | void |
| 3398 | xmlXPathFreeObject(xmlXPathObjectPtr obj) { |
| 3399 | if (obj == NULL) return; |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3400 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3401 | if (obj->boolval) { |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3402 | #if 0 |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3403 | if (obj->user != NULL) { |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 3404 | xmlXPathFreeNodeSet(obj->nodesetval); |
Daniel Veillard | 38bf6f0 | 2002-03-16 22:03:31 +0000 | [diff] [blame] | 3405 | xmlFreeNodeList((xmlNodePtr) obj->user); |
William M. Brack | e9449c5 | 2004-07-11 14:41:20 +0000 | [diff] [blame] | 3406 | } else |
| 3407 | #endif |
| 3408 | if (obj->nodesetval != NULL) |
Daniel Veillard | 7785171 | 2001-02-27 21:54:07 +0000 | [diff] [blame] | 3409 | xmlXPathFreeValueTree(obj->nodesetval); |
| 3410 | } else { |
| 3411 | if (obj->nodesetval != NULL) |
| 3412 | xmlXPathFreeNodeSet(obj->nodesetval); |
| 3413 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3414 | #ifdef LIBXML_XPTR_ENABLED |
| 3415 | } else if (obj->type == XPATH_LOCATIONSET) { |
| 3416 | if (obj->user != NULL) |
| 3417 | xmlXPtrFreeLocationSet(obj->user); |
| 3418 | #endif |
| 3419 | } else if (obj->type == XPATH_STRING) { |
| 3420 | if (obj->stringval != NULL) |
| 3421 | xmlFree(obj->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3422 | } |
| 3423 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3424 | xmlFree(obj); |
| 3425 | } |
| 3426 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3427 | |
| 3428 | /************************************************************************ |
| 3429 | * * |
| 3430 | * Type Casting Routines * |
| 3431 | * * |
| 3432 | ************************************************************************/ |
| 3433 | |
| 3434 | /** |
| 3435 | * xmlXPathCastBooleanToString: |
| 3436 | * @val: a boolean |
| 3437 | * |
| 3438 | * Converts a boolean to its string value. |
| 3439 | * |
| 3440 | * Returns a newly allocated string. |
| 3441 | */ |
| 3442 | xmlChar * |
| 3443 | xmlXPathCastBooleanToString (int val) { |
| 3444 | xmlChar *ret; |
| 3445 | if (val) |
| 3446 | ret = xmlStrdup((const xmlChar *) "true"); |
| 3447 | else |
| 3448 | ret = xmlStrdup((const xmlChar *) "false"); |
| 3449 | return(ret); |
| 3450 | } |
| 3451 | |
| 3452 | /** |
| 3453 | * xmlXPathCastNumberToString: |
| 3454 | * @val: a number |
| 3455 | * |
| 3456 | * Converts a number to its string value. |
| 3457 | * |
| 3458 | * Returns a newly allocated string. |
| 3459 | */ |
| 3460 | xmlChar * |
| 3461 | xmlXPathCastNumberToString (double val) { |
| 3462 | xmlChar *ret; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3463 | switch (xmlXPathIsInf(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3464 | case 1: |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 3465 | ret = xmlStrdup((const xmlChar *) "Infinity"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3466 | break; |
| 3467 | case -1: |
| 3468 | ret = xmlStrdup((const xmlChar *) "-Infinity"); |
| 3469 | break; |
| 3470 | default: |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3471 | if (xmlXPathIsNaN(val)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3472 | ret = xmlStrdup((const xmlChar *) "NaN"); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 3473 | } else if (val == 0 && xmlXPathGetSign(val) != 0) { |
| 3474 | ret = xmlStrdup((const xmlChar *) "0"); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3475 | } else { |
| 3476 | /* could be improved */ |
| 3477 | char buf[100]; |
| 3478 | xmlXPathFormatNumber(val, buf, 100); |
| 3479 | ret = xmlStrdup((const xmlChar *) buf); |
| 3480 | } |
| 3481 | } |
| 3482 | return(ret); |
| 3483 | } |
| 3484 | |
| 3485 | /** |
| 3486 | * xmlXPathCastNodeToString: |
| 3487 | * @node: a node |
| 3488 | * |
| 3489 | * Converts a node to its string value. |
| 3490 | * |
| 3491 | * Returns a newly allocated string. |
| 3492 | */ |
| 3493 | xmlChar * |
| 3494 | xmlXPathCastNodeToString (xmlNodePtr node) { |
| 3495 | return(xmlNodeGetContent(node)); |
| 3496 | } |
| 3497 | |
| 3498 | /** |
| 3499 | * xmlXPathCastNodeSetToString: |
| 3500 | * @ns: a node-set |
| 3501 | * |
| 3502 | * Converts a node-set to its string value. |
| 3503 | * |
| 3504 | * Returns a newly allocated string. |
| 3505 | */ |
| 3506 | xmlChar * |
| 3507 | xmlXPathCastNodeSetToString (xmlNodeSetPtr ns) { |
| 3508 | if ((ns == NULL) || (ns->nodeNr == 0) || (ns->nodeTab == NULL)) |
| 3509 | return(xmlStrdup((const xmlChar *) "")); |
| 3510 | |
| 3511 | xmlXPathNodeSetSort(ns); |
| 3512 | return(xmlXPathCastNodeToString(ns->nodeTab[0])); |
| 3513 | } |
| 3514 | |
| 3515 | /** |
| 3516 | * xmlXPathCastToString: |
| 3517 | * @val: an XPath object |
| 3518 | * |
| 3519 | * Converts an existing object to its string() equivalent |
| 3520 | * |
| 3521 | * Returns the string value of the object, NULL in case of error. |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3522 | * A new string is allocated only if needed (@val isn't a |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3523 | * string object). |
| 3524 | */ |
| 3525 | xmlChar * |
| 3526 | xmlXPathCastToString(xmlXPathObjectPtr val) { |
| 3527 | xmlChar *ret = NULL; |
| 3528 | |
| 3529 | if (val == NULL) |
| 3530 | return(xmlStrdup((const xmlChar *) "")); |
| 3531 | switch (val->type) { |
| 3532 | case XPATH_UNDEFINED: |
| 3533 | #ifdef DEBUG_EXPR |
| 3534 | xmlGenericError(xmlGenericErrorContext, "String: undefined\n"); |
| 3535 | #endif |
| 3536 | ret = xmlStrdup((const xmlChar *) ""); |
| 3537 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3538 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3539 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3540 | ret = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3541 | break; |
| 3542 | case XPATH_STRING: |
Daniel Veillard | 4e2df54 | 2002-03-22 12:23:14 +0000 | [diff] [blame] | 3543 | return(xmlStrdup(val->stringval)); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3544 | case XPATH_BOOLEAN: |
| 3545 | ret = xmlXPathCastBooleanToString(val->boolval); |
| 3546 | break; |
| 3547 | case XPATH_NUMBER: { |
| 3548 | ret = xmlXPathCastNumberToString(val->floatval); |
| 3549 | break; |
| 3550 | } |
| 3551 | case XPATH_USERS: |
| 3552 | case XPATH_POINT: |
| 3553 | case XPATH_RANGE: |
| 3554 | case XPATH_LOCATIONSET: |
| 3555 | TODO |
| 3556 | ret = xmlStrdup((const xmlChar *) ""); |
| 3557 | break; |
| 3558 | } |
| 3559 | return(ret); |
| 3560 | } |
| 3561 | |
| 3562 | /** |
| 3563 | * xmlXPathConvertString: |
| 3564 | * @val: an XPath object |
| 3565 | * |
| 3566 | * Converts an existing object to its string() equivalent |
| 3567 | * |
| 3568 | * Returns the new object, the old one is freed (or the operation |
| 3569 | * is done directly on @val) |
| 3570 | */ |
| 3571 | xmlXPathObjectPtr |
| 3572 | xmlXPathConvertString(xmlXPathObjectPtr val) { |
| 3573 | xmlChar *res = NULL; |
| 3574 | |
| 3575 | if (val == NULL) |
| 3576 | return(xmlXPathNewCString("")); |
| 3577 | |
| 3578 | switch (val->type) { |
| 3579 | case XPATH_UNDEFINED: |
| 3580 | #ifdef DEBUG_EXPR |
| 3581 | xmlGenericError(xmlGenericErrorContext, "STRING: undefined\n"); |
| 3582 | #endif |
| 3583 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3584 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3585 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3586 | res = xmlXPathCastNodeSetToString(val->nodesetval); |
| 3587 | break; |
| 3588 | case XPATH_STRING: |
| 3589 | return(val); |
| 3590 | case XPATH_BOOLEAN: |
| 3591 | res = xmlXPathCastBooleanToString(val->boolval); |
| 3592 | break; |
| 3593 | case XPATH_NUMBER: |
| 3594 | res = xmlXPathCastNumberToString(val->floatval); |
| 3595 | break; |
| 3596 | case XPATH_USERS: |
| 3597 | case XPATH_POINT: |
| 3598 | case XPATH_RANGE: |
| 3599 | case XPATH_LOCATIONSET: |
| 3600 | TODO; |
| 3601 | break; |
| 3602 | } |
| 3603 | xmlXPathFreeObject(val); |
| 3604 | if (res == NULL) |
| 3605 | return(xmlXPathNewCString("")); |
| 3606 | return(xmlXPathWrapString(res)); |
| 3607 | } |
| 3608 | |
| 3609 | /** |
| 3610 | * xmlXPathCastBooleanToNumber: |
| 3611 | * @val: a boolean |
| 3612 | * |
| 3613 | * Converts a boolean to its number value |
| 3614 | * |
| 3615 | * Returns the number value |
| 3616 | */ |
| 3617 | double |
| 3618 | xmlXPathCastBooleanToNumber(int val) { |
| 3619 | if (val) |
| 3620 | return(1.0); |
| 3621 | return(0.0); |
| 3622 | } |
| 3623 | |
| 3624 | /** |
| 3625 | * xmlXPathCastStringToNumber: |
| 3626 | * @val: a string |
| 3627 | * |
| 3628 | * Converts a string to its number value |
| 3629 | * |
| 3630 | * Returns the number value |
| 3631 | */ |
| 3632 | double |
| 3633 | xmlXPathCastStringToNumber(const xmlChar * val) { |
| 3634 | return(xmlXPathStringEvalNumber(val)); |
| 3635 | } |
| 3636 | |
| 3637 | /** |
| 3638 | * xmlXPathCastNodeToNumber: |
| 3639 | * @node: a node |
| 3640 | * |
| 3641 | * Converts a node to its number value |
| 3642 | * |
| 3643 | * Returns the number value |
| 3644 | */ |
| 3645 | double |
| 3646 | xmlXPathCastNodeToNumber (xmlNodePtr node) { |
| 3647 | xmlChar *strval; |
| 3648 | double ret; |
| 3649 | |
| 3650 | if (node == NULL) |
| 3651 | return(xmlXPathNAN); |
| 3652 | strval = xmlXPathCastNodeToString(node); |
| 3653 | if (strval == NULL) |
| 3654 | return(xmlXPathNAN); |
| 3655 | ret = xmlXPathCastStringToNumber(strval); |
| 3656 | xmlFree(strval); |
| 3657 | |
| 3658 | return(ret); |
| 3659 | } |
| 3660 | |
| 3661 | /** |
| 3662 | * xmlXPathCastNodeSetToNumber: |
| 3663 | * @ns: a node-set |
| 3664 | * |
| 3665 | * Converts a node-set to its number value |
| 3666 | * |
| 3667 | * Returns the number value |
| 3668 | */ |
| 3669 | double |
| 3670 | xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns) { |
| 3671 | xmlChar *str; |
| 3672 | double ret; |
| 3673 | |
| 3674 | if (ns == NULL) |
| 3675 | return(xmlXPathNAN); |
| 3676 | str = xmlXPathCastNodeSetToString(ns); |
| 3677 | ret = xmlXPathCastStringToNumber(str); |
| 3678 | xmlFree(str); |
| 3679 | return(ret); |
| 3680 | } |
| 3681 | |
| 3682 | /** |
| 3683 | * xmlXPathCastToNumber: |
| 3684 | * @val: an XPath object |
| 3685 | * |
| 3686 | * Converts an XPath object to its number value |
| 3687 | * |
| 3688 | * Returns the number value |
| 3689 | */ |
| 3690 | double |
| 3691 | xmlXPathCastToNumber(xmlXPathObjectPtr val) { |
| 3692 | double ret = 0.0; |
| 3693 | |
| 3694 | if (val == NULL) |
| 3695 | return(xmlXPathNAN); |
| 3696 | switch (val->type) { |
| 3697 | case XPATH_UNDEFINED: |
| 3698 | #ifdef DEGUB_EXPR |
| 3699 | xmlGenericError(xmlGenericErrorContext, "NUMBER: undefined\n"); |
| 3700 | #endif |
| 3701 | ret = xmlXPathNAN; |
| 3702 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3703 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3704 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3705 | ret = xmlXPathCastNodeSetToNumber(val->nodesetval); |
| 3706 | break; |
| 3707 | case XPATH_STRING: |
| 3708 | ret = xmlXPathCastStringToNumber(val->stringval); |
| 3709 | break; |
| 3710 | case XPATH_NUMBER: |
| 3711 | ret = val->floatval; |
| 3712 | break; |
| 3713 | case XPATH_BOOLEAN: |
| 3714 | ret = xmlXPathCastBooleanToNumber(val->boolval); |
| 3715 | break; |
| 3716 | case XPATH_USERS: |
| 3717 | case XPATH_POINT: |
| 3718 | case XPATH_RANGE: |
| 3719 | case XPATH_LOCATIONSET: |
| 3720 | TODO; |
| 3721 | ret = xmlXPathNAN; |
| 3722 | break; |
| 3723 | } |
| 3724 | return(ret); |
| 3725 | } |
| 3726 | |
| 3727 | /** |
| 3728 | * xmlXPathConvertNumber: |
| 3729 | * @val: an XPath object |
| 3730 | * |
| 3731 | * Converts an existing object to its number() equivalent |
| 3732 | * |
| 3733 | * Returns the new object, the old one is freed (or the operation |
| 3734 | * is done directly on @val) |
| 3735 | */ |
| 3736 | xmlXPathObjectPtr |
| 3737 | xmlXPathConvertNumber(xmlXPathObjectPtr val) { |
| 3738 | xmlXPathObjectPtr ret; |
| 3739 | |
| 3740 | if (val == NULL) |
| 3741 | return(xmlXPathNewFloat(0.0)); |
| 3742 | if (val->type == XPATH_NUMBER) |
| 3743 | return(val); |
| 3744 | ret = xmlXPathNewFloat(xmlXPathCastToNumber(val)); |
| 3745 | xmlXPathFreeObject(val); |
| 3746 | return(ret); |
| 3747 | } |
| 3748 | |
| 3749 | /** |
| 3750 | * xmlXPathCastNumberToBoolean: |
| 3751 | * @val: a number |
| 3752 | * |
| 3753 | * Converts a number to its boolean value |
| 3754 | * |
| 3755 | * Returns the boolean value |
| 3756 | */ |
| 3757 | int |
| 3758 | xmlXPathCastNumberToBoolean (double val) { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 3759 | if (xmlXPathIsNaN(val) || (val == 0.0)) |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3760 | return(0); |
| 3761 | return(1); |
| 3762 | } |
| 3763 | |
| 3764 | /** |
| 3765 | * xmlXPathCastStringToBoolean: |
| 3766 | * @val: a string |
| 3767 | * |
| 3768 | * Converts a string to its boolean value |
| 3769 | * |
| 3770 | * Returns the boolean value |
| 3771 | */ |
| 3772 | int |
| 3773 | xmlXPathCastStringToBoolean (const xmlChar *val) { |
| 3774 | if ((val == NULL) || (xmlStrlen(val) == 0)) |
| 3775 | return(0); |
| 3776 | return(1); |
| 3777 | } |
| 3778 | |
| 3779 | /** |
| 3780 | * xmlXPathCastNodeSetToBoolean: |
| 3781 | * @ns: a node-set |
| 3782 | * |
| 3783 | * Converts a node-set to its boolean value |
| 3784 | * |
| 3785 | * Returns the boolean value |
| 3786 | */ |
| 3787 | int |
| 3788 | xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns) { |
| 3789 | if ((ns == NULL) || (ns->nodeNr == 0)) |
| 3790 | return(0); |
| 3791 | return(1); |
| 3792 | } |
| 3793 | |
| 3794 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3795 | * xmlXPathCastToBoolean: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3796 | * @val: an XPath object |
| 3797 | * |
| 3798 | * Converts an XPath object to its boolean value |
| 3799 | * |
| 3800 | * Returns the boolean value |
| 3801 | */ |
| 3802 | int |
| 3803 | xmlXPathCastToBoolean (xmlXPathObjectPtr val) { |
| 3804 | int ret = 0; |
| 3805 | |
| 3806 | if (val == NULL) |
| 3807 | return(0); |
| 3808 | switch (val->type) { |
| 3809 | case XPATH_UNDEFINED: |
| 3810 | #ifdef DEBUG_EXPR |
| 3811 | xmlGenericError(xmlGenericErrorContext, "BOOLEAN: undefined\n"); |
| 3812 | #endif |
| 3813 | ret = 0; |
| 3814 | break; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3815 | case XPATH_NODESET: |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 3816 | case XPATH_XSLT_TREE: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 3817 | ret = xmlXPathCastNodeSetToBoolean(val->nodesetval); |
| 3818 | break; |
| 3819 | case XPATH_STRING: |
| 3820 | ret = xmlXPathCastStringToBoolean(val->stringval); |
| 3821 | break; |
| 3822 | case XPATH_NUMBER: |
| 3823 | ret = xmlXPathCastNumberToBoolean(val->floatval); |
| 3824 | break; |
| 3825 | case XPATH_BOOLEAN: |
| 3826 | ret = val->boolval; |
| 3827 | break; |
| 3828 | case XPATH_USERS: |
| 3829 | case XPATH_POINT: |
| 3830 | case XPATH_RANGE: |
| 3831 | case XPATH_LOCATIONSET: |
| 3832 | TODO; |
| 3833 | ret = 0; |
| 3834 | break; |
| 3835 | } |
| 3836 | return(ret); |
| 3837 | } |
| 3838 | |
| 3839 | |
| 3840 | /** |
| 3841 | * xmlXPathConvertBoolean: |
| 3842 | * @val: an XPath object |
| 3843 | * |
| 3844 | * Converts an existing object to its boolean() equivalent |
| 3845 | * |
| 3846 | * Returns the new object, the old one is freed (or the operation |
| 3847 | * is done directly on @val) |
| 3848 | */ |
| 3849 | xmlXPathObjectPtr |
| 3850 | xmlXPathConvertBoolean(xmlXPathObjectPtr val) { |
| 3851 | xmlXPathObjectPtr ret; |
| 3852 | |
| 3853 | if (val == NULL) |
| 3854 | return(xmlXPathNewBoolean(0)); |
| 3855 | if (val->type == XPATH_BOOLEAN) |
| 3856 | return(val); |
| 3857 | ret = xmlXPathNewBoolean(xmlXPathCastToBoolean(val)); |
| 3858 | xmlXPathFreeObject(val); |
| 3859 | return(ret); |
| 3860 | } |
| 3861 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3862 | /************************************************************************ |
| 3863 | * * |
| 3864 | * Routines to handle XPath contexts * |
| 3865 | * * |
| 3866 | ************************************************************************/ |
| 3867 | |
| 3868 | /** |
| 3869 | * xmlXPathNewContext: |
| 3870 | * @doc: the XML document |
| 3871 | * |
| 3872 | * Create a new xmlXPathContext |
| 3873 | * |
Daniel Veillard | af43f63 | 2002-03-08 15:05:20 +0000 | [diff] [blame] | 3874 | * Returns the xmlXPathContext just allocated. The caller will need to free it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3875 | */ |
| 3876 | xmlXPathContextPtr |
| 3877 | xmlXPathNewContext(xmlDocPtr doc) { |
| 3878 | xmlXPathContextPtr ret; |
| 3879 | |
| 3880 | ret = (xmlXPathContextPtr) xmlMalloc(sizeof(xmlXPathContext)); |
| 3881 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3882 | xmlXPathErrMemory(NULL, "creating context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3883 | return(NULL); |
| 3884 | } |
| 3885 | memset(ret, 0 , (size_t) sizeof(xmlXPathContext)); |
| 3886 | ret->doc = doc; |
| 3887 | ret->node = NULL; |
| 3888 | |
| 3889 | ret->varHash = NULL; |
| 3890 | |
| 3891 | ret->nb_types = 0; |
| 3892 | ret->max_types = 0; |
| 3893 | ret->types = NULL; |
| 3894 | |
| 3895 | ret->funcHash = xmlHashCreate(0); |
| 3896 | |
| 3897 | ret->nb_axis = 0; |
| 3898 | ret->max_axis = 0; |
| 3899 | ret->axis = NULL; |
| 3900 | |
| 3901 | ret->nsHash = NULL; |
| 3902 | ret->user = NULL; |
| 3903 | |
| 3904 | ret->contextSize = -1; |
| 3905 | ret->proximityPosition = -1; |
| 3906 | |
| 3907 | xmlXPathRegisterAllFunctions(ret); |
| 3908 | |
| 3909 | return(ret); |
| 3910 | } |
| 3911 | |
| 3912 | /** |
| 3913 | * xmlXPathFreeContext: |
| 3914 | * @ctxt: the context to free |
| 3915 | * |
| 3916 | * Free up an xmlXPathContext |
| 3917 | */ |
| 3918 | void |
| 3919 | xmlXPathFreeContext(xmlXPathContextPtr ctxt) { |
| 3920 | xmlXPathRegisteredNsCleanup(ctxt); |
| 3921 | xmlXPathRegisteredFuncsCleanup(ctxt); |
| 3922 | xmlXPathRegisteredVariablesCleanup(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3923 | xmlFree(ctxt); |
| 3924 | } |
| 3925 | |
| 3926 | /************************************************************************ |
| 3927 | * * |
| 3928 | * Routines to handle XPath parser contexts * |
| 3929 | * * |
| 3930 | ************************************************************************/ |
| 3931 | |
| 3932 | #define CHECK_CTXT(ctxt) \ |
| 3933 | if (ctxt == NULL) { \ |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 3934 | __xmlRaiseError(NULL, NULL, NULL, \ |
| 3935 | NULL, NULL, XML_FROM_XPATH, \ |
| 3936 | XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \ |
| 3937 | __FILE__, __LINE__, \ |
| 3938 | NULL, NULL, NULL, 0, 0, \ |
| 3939 | "NULL context pointer\n"); \ |
| 3940 | return(NULL); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3941 | } \ |
| 3942 | |
| 3943 | |
| 3944 | #define CHECK_CONTEXT(ctxt) \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 3945 | if ((ctxt == NULL) || (ctxt->doc == NULL) || \ |
| 3946 | (ctxt->doc->children == NULL)) { \ |
| 3947 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_INVALID_CTXT); \ |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 3948 | return(NULL); \ |
Daniel Veillard | 57b2516 | 2004-11-06 14:50:18 +0000 | [diff] [blame] | 3949 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3950 | |
| 3951 | |
| 3952 | /** |
| 3953 | * xmlXPathNewParserContext: |
| 3954 | * @str: the XPath expression |
| 3955 | * @ctxt: the XPath context |
| 3956 | * |
| 3957 | * Create a new xmlXPathParserContext |
| 3958 | * |
| 3959 | * Returns the xmlXPathParserContext just allocated. |
| 3960 | */ |
| 3961 | xmlXPathParserContextPtr |
| 3962 | xmlXPathNewParserContext(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 3963 | xmlXPathParserContextPtr ret; |
| 3964 | |
| 3965 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 3966 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 3967 | xmlXPathErrMemory(ctxt, "creating parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3968 | return(NULL); |
| 3969 | } |
| 3970 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 3971 | ret->cur = ret->base = str; |
| 3972 | ret->context = ctxt; |
| 3973 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3974 | ret->comp = xmlXPathNewCompExpr(); |
| 3975 | if (ret->comp == NULL) { |
| 3976 | xmlFree(ret->valueTab); |
| 3977 | xmlFree(ret); |
| 3978 | return(NULL); |
| 3979 | } |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 3980 | if ((ctxt != NULL) && (ctxt->dict != NULL)) { |
| 3981 | ret->comp->dict = ctxt->dict; |
| 3982 | xmlDictReference(ret->comp->dict); |
| 3983 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3984 | |
| 3985 | return(ret); |
| 3986 | } |
| 3987 | |
| 3988 | /** |
| 3989 | * xmlXPathCompParserContext: |
| 3990 | * @comp: the XPath compiled expression |
| 3991 | * @ctxt: the XPath context |
| 3992 | * |
| 3993 | * Create a new xmlXPathParserContext when processing a compiled expression |
| 3994 | * |
| 3995 | * Returns the xmlXPathParserContext just allocated. |
| 3996 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3997 | static xmlXPathParserContextPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 3998 | xmlXPathCompParserContext(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctxt) { |
| 3999 | xmlXPathParserContextPtr ret; |
| 4000 | |
| 4001 | ret = (xmlXPathParserContextPtr) xmlMalloc(sizeof(xmlXPathParserContext)); |
| 4002 | if (ret == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4003 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4004 | return(NULL); |
| 4005 | } |
| 4006 | memset(ret, 0 , (size_t) sizeof(xmlXPathParserContext)); |
| 4007 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4008 | /* Allocate the value stack */ |
| 4009 | ret->valueTab = (xmlXPathObjectPtr *) |
| 4010 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4011 | if (ret->valueTab == NULL) { |
| 4012 | xmlFree(ret); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4013 | xmlXPathErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4014 | return(NULL); |
| 4015 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4016 | ret->valueNr = 0; |
| 4017 | ret->valueMax = 10; |
| 4018 | ret->value = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4019 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 4020 | ret->context = ctxt; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4021 | ret->comp = comp; |
| 4022 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4023 | return(ret); |
| 4024 | } |
| 4025 | |
| 4026 | /** |
| 4027 | * xmlXPathFreeParserContext: |
| 4028 | * @ctxt: the context to free |
| 4029 | * |
| 4030 | * Free up an xmlXPathParserContext |
| 4031 | */ |
| 4032 | void |
| 4033 | xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt) { |
| 4034 | if (ctxt->valueTab != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4035 | xmlFree(ctxt->valueTab); |
| 4036 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 4037 | if (ctxt->comp) |
| 4038 | xmlXPathFreeCompExpr(ctxt->comp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4039 | xmlFree(ctxt); |
| 4040 | } |
| 4041 | |
| 4042 | /************************************************************************ |
| 4043 | * * |
| 4044 | * The implicit core function library * |
| 4045 | * * |
| 4046 | ************************************************************************/ |
| 4047 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4048 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4049 | * xmlXPathNodeValHash: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4050 | * @node: a node pointer |
| 4051 | * |
| 4052 | * Function computing the beginning of the string value of the node, |
| 4053 | * used to speed up comparisons |
| 4054 | * |
| 4055 | * Returns an int usable as a hash |
| 4056 | */ |
| 4057 | static unsigned int |
| 4058 | xmlXPathNodeValHash(xmlNodePtr node) { |
| 4059 | int len = 2; |
| 4060 | const xmlChar * string = NULL; |
| 4061 | xmlNodePtr tmp = NULL; |
| 4062 | unsigned int ret = 0; |
| 4063 | |
| 4064 | if (node == NULL) |
| 4065 | return(0); |
| 4066 | |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4067 | if (node->type == XML_DOCUMENT_NODE) { |
| 4068 | tmp = xmlDocGetRootElement((xmlDocPtr) node); |
| 4069 | if (tmp == NULL) |
| 4070 | node = node->children; |
| 4071 | else |
| 4072 | node = tmp; |
| 4073 | |
| 4074 | if (node == NULL) |
| 4075 | return(0); |
| 4076 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4077 | |
| 4078 | switch (node->type) { |
| 4079 | case XML_COMMENT_NODE: |
| 4080 | case XML_PI_NODE: |
| 4081 | case XML_CDATA_SECTION_NODE: |
| 4082 | case XML_TEXT_NODE: |
| 4083 | string = node->content; |
| 4084 | if (string == NULL) |
| 4085 | return(0); |
| 4086 | if (string[0] == 0) |
| 4087 | return(0); |
| 4088 | return(((unsigned int) string[0]) + |
| 4089 | (((unsigned int) string[1]) << 8)); |
| 4090 | case XML_NAMESPACE_DECL: |
| 4091 | string = ((xmlNsPtr)node)->href; |
| 4092 | if (string == NULL) |
| 4093 | return(0); |
| 4094 | if (string[0] == 0) |
| 4095 | return(0); |
| 4096 | return(((unsigned int) string[0]) + |
| 4097 | (((unsigned int) string[1]) << 8)); |
| 4098 | case XML_ATTRIBUTE_NODE: |
| 4099 | tmp = ((xmlAttrPtr) node)->children; |
| 4100 | break; |
| 4101 | case XML_ELEMENT_NODE: |
| 4102 | tmp = node->children; |
| 4103 | break; |
| 4104 | default: |
| 4105 | return(0); |
| 4106 | } |
| 4107 | while (tmp != NULL) { |
| 4108 | switch (tmp->type) { |
| 4109 | case XML_COMMENT_NODE: |
| 4110 | case XML_PI_NODE: |
| 4111 | case XML_CDATA_SECTION_NODE: |
| 4112 | case XML_TEXT_NODE: |
| 4113 | string = tmp->content; |
| 4114 | break; |
| 4115 | case XML_NAMESPACE_DECL: |
| 4116 | string = ((xmlNsPtr)tmp)->href; |
| 4117 | break; |
| 4118 | default: |
| 4119 | break; |
| 4120 | } |
| 4121 | if ((string != NULL) && (string[0] != 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4122 | if (len == 1) { |
| 4123 | return(ret + (((unsigned int) string[0]) << 8)); |
| 4124 | } |
| 4125 | if (string[1] == 0) { |
| 4126 | len = 1; |
| 4127 | ret = (unsigned int) string[0]; |
| 4128 | } else { |
| 4129 | return(((unsigned int) string[0]) + |
| 4130 | (((unsigned int) string[1]) << 8)); |
| 4131 | } |
| 4132 | } |
| 4133 | /* |
| 4134 | * Skip to next node |
| 4135 | */ |
| 4136 | if ((tmp->children != NULL) && (tmp->type != XML_DTD_NODE)) { |
| 4137 | if (tmp->children->type != XML_ENTITY_DECL) { |
| 4138 | tmp = tmp->children; |
| 4139 | continue; |
| 4140 | } |
| 4141 | } |
| 4142 | if (tmp == node) |
| 4143 | break; |
| 4144 | |
| 4145 | if (tmp->next != NULL) { |
| 4146 | tmp = tmp->next; |
| 4147 | continue; |
| 4148 | } |
| 4149 | |
| 4150 | do { |
| 4151 | tmp = tmp->parent; |
| 4152 | if (tmp == NULL) |
| 4153 | break; |
| 4154 | if (tmp == node) { |
| 4155 | tmp = NULL; |
| 4156 | break; |
| 4157 | } |
| 4158 | if (tmp->next != NULL) { |
| 4159 | tmp = tmp->next; |
| 4160 | break; |
| 4161 | } |
| 4162 | } while (tmp != NULL); |
| 4163 | } |
| 4164 | return(ret); |
| 4165 | } |
| 4166 | |
| 4167 | /** |
| 4168 | * xmlXPathStringHash: |
| 4169 | * @string: a string |
| 4170 | * |
| 4171 | * Function computing the beginning of the string value of the node, |
| 4172 | * used to speed up comparisons |
| 4173 | * |
| 4174 | * Returns an int usable as a hash |
| 4175 | */ |
| 4176 | static unsigned int |
| 4177 | xmlXPathStringHash(const xmlChar * string) { |
| 4178 | if (string == NULL) |
| 4179 | return((unsigned int) 0); |
| 4180 | if (string[0] == 0) |
| 4181 | return(0); |
| 4182 | return(((unsigned int) string[0]) + |
| 4183 | (((unsigned int) string[1]) << 8)); |
| 4184 | } |
| 4185 | |
| 4186 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4187 | * xmlXPathCompareNodeSetFloat: |
| 4188 | * @ctxt: the XPath Parser context |
| 4189 | * @inf: less than (1) or greater than (0) |
| 4190 | * @strict: is the comparison strict |
| 4191 | * @arg: the node set |
| 4192 | * @f: the value |
| 4193 | * |
| 4194 | * Implement the compare operation between a nodeset and a number |
| 4195 | * @ns < @val (1, 1, ... |
| 4196 | * @ns <= @val (1, 0, ... |
| 4197 | * @ns > @val (0, 1, ... |
| 4198 | * @ns >= @val (0, 0, ... |
| 4199 | * |
| 4200 | * If one object to be compared is a node-set and the other is a number, |
| 4201 | * then the comparison will be true if and only if there is a node in the |
| 4202 | * node-set such that the result of performing the comparison on the number |
| 4203 | * to be compared and on the result of converting the string-value of that |
| 4204 | * node to a number using the number function is true. |
| 4205 | * |
| 4206 | * Returns 0 or 1 depending on the results of the test. |
| 4207 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4208 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4209 | xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4210 | xmlXPathObjectPtr arg, xmlXPathObjectPtr f) { |
| 4211 | int i, ret = 0; |
| 4212 | xmlNodeSetPtr ns; |
| 4213 | xmlChar *str2; |
| 4214 | |
| 4215 | if ((f == NULL) || (arg == NULL) || |
| 4216 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 4217 | xmlXPathFreeObject(arg); |
| 4218 | xmlXPathFreeObject(f); |
| 4219 | return(0); |
| 4220 | } |
| 4221 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4222 | if (ns != NULL) { |
| 4223 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4224 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4225 | if (str2 != NULL) { |
| 4226 | valuePush(ctxt, |
| 4227 | xmlXPathNewString(str2)); |
| 4228 | xmlFree(str2); |
| 4229 | xmlXPathNumberFunction(ctxt, 1); |
| 4230 | valuePush(ctxt, xmlXPathObjectCopy(f)); |
| 4231 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4232 | if (ret) |
| 4233 | break; |
| 4234 | } |
| 4235 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4236 | } |
| 4237 | xmlXPathFreeObject(arg); |
| 4238 | xmlXPathFreeObject(f); |
| 4239 | return(ret); |
| 4240 | } |
| 4241 | |
| 4242 | /** |
| 4243 | * xmlXPathCompareNodeSetString: |
| 4244 | * @ctxt: the XPath Parser context |
| 4245 | * @inf: less than (1) or greater than (0) |
| 4246 | * @strict: is the comparison strict |
| 4247 | * @arg: the node set |
| 4248 | * @s: the value |
| 4249 | * |
| 4250 | * Implement the compare operation between a nodeset and a string |
| 4251 | * @ns < @val (1, 1, ... |
| 4252 | * @ns <= @val (1, 0, ... |
| 4253 | * @ns > @val (0, 1, ... |
| 4254 | * @ns >= @val (0, 0, ... |
| 4255 | * |
| 4256 | * If one object to be compared is a node-set and the other is a string, |
| 4257 | * then the comparison will be true if and only if there is a node in |
| 4258 | * the node-set such that the result of performing the comparison on the |
| 4259 | * string-value of the node and the other string is true. |
| 4260 | * |
| 4261 | * Returns 0 or 1 depending on the results of the test. |
| 4262 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4263 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4264 | xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4265 | xmlXPathObjectPtr arg, xmlXPathObjectPtr s) { |
| 4266 | int i, ret = 0; |
| 4267 | xmlNodeSetPtr ns; |
| 4268 | xmlChar *str2; |
| 4269 | |
| 4270 | if ((s == NULL) || (arg == NULL) || |
| 4271 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) { |
| 4272 | xmlXPathFreeObject(arg); |
| 4273 | xmlXPathFreeObject(s); |
| 4274 | return(0); |
| 4275 | } |
| 4276 | ns = arg->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4277 | if (ns != NULL) { |
| 4278 | for (i = 0;i < ns->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4279 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4280 | if (str2 != NULL) { |
| 4281 | valuePush(ctxt, |
| 4282 | xmlXPathNewString(str2)); |
| 4283 | xmlFree(str2); |
| 4284 | valuePush(ctxt, xmlXPathObjectCopy(s)); |
| 4285 | ret = xmlXPathCompareValues(ctxt, inf, strict); |
| 4286 | if (ret) |
| 4287 | break; |
| 4288 | } |
| 4289 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4290 | } |
| 4291 | xmlXPathFreeObject(arg); |
| 4292 | xmlXPathFreeObject(s); |
| 4293 | return(ret); |
| 4294 | } |
| 4295 | |
| 4296 | /** |
| 4297 | * xmlXPathCompareNodeSets: |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4298 | * @inf: less than (1) or greater than (0) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4299 | * @strict: is the comparison strict |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4300 | * @arg1: the first node set object |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4301 | * @arg2: the second node set object |
| 4302 | * |
| 4303 | * Implement the compare operation on nodesets: |
| 4304 | * |
| 4305 | * If both objects to be compared are node-sets, then the comparison |
| 4306 | * will be true if and only if there is a node in the first node-set |
| 4307 | * and a node in the second node-set such that the result of performing |
| 4308 | * the comparison on the string-values of the two nodes is true. |
| 4309 | * .... |
| 4310 | * When neither object to be compared is a node-set and the operator |
| 4311 | * is <=, <, >= or >, then the objects are compared by converting both |
| 4312 | * objects to numbers and comparing the numbers according to IEEE 754. |
| 4313 | * .... |
| 4314 | * The number function converts its argument to a number as follows: |
| 4315 | * - a string that consists of optional whitespace followed by an |
| 4316 | * optional minus sign followed by a Number followed by whitespace |
| 4317 | * is converted to the IEEE 754 number that is nearest (according |
| 4318 | * to the IEEE 754 round-to-nearest rule) to the mathematical value |
| 4319 | * represented by the string; any other string is converted to NaN |
| 4320 | * |
| 4321 | * Conclusion all nodes need to be converted first to their string value |
| 4322 | * and then the comparison must be done when possible |
| 4323 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4324 | static int |
| 4325 | xmlXPathCompareNodeSets(int inf, int strict, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4326 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
| 4327 | int i, j, init = 0; |
| 4328 | double val1; |
| 4329 | double *values2; |
| 4330 | int ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4331 | xmlNodeSetPtr ns1; |
| 4332 | xmlNodeSetPtr ns2; |
| 4333 | |
| 4334 | if ((arg1 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4335 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) { |
| 4336 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4337 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4338 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4339 | if ((arg2 == NULL) || |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4340 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) { |
| 4341 | xmlXPathFreeObject(arg1); |
| 4342 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4343 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4344 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4345 | |
| 4346 | ns1 = arg1->nodesetval; |
| 4347 | ns2 = arg2->nodesetval; |
| 4348 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4349 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4350 | xmlXPathFreeObject(arg1); |
| 4351 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4352 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4353 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 4354 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) { |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4355 | xmlXPathFreeObject(arg1); |
| 4356 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4357 | return(0); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4358 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4359 | |
| 4360 | values2 = (double *) xmlMalloc(ns2->nodeNr * sizeof(double)); |
| 4361 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4362 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4363 | xmlXPathFreeObject(arg1); |
| 4364 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4365 | return(0); |
| 4366 | } |
| 4367 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4368 | val1 = xmlXPathCastNodeToNumber(ns1->nodeTab[i]); |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4369 | if (xmlXPathIsNaN(val1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4370 | continue; |
| 4371 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4372 | if (init == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 4373 | values2[j] = xmlXPathCastNodeToNumber(ns2->nodeTab[j]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4374 | } |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 4375 | if (xmlXPathIsNaN(values2[j])) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4376 | continue; |
| 4377 | if (inf && strict) |
| 4378 | ret = (val1 < values2[j]); |
| 4379 | else if (inf && !strict) |
| 4380 | ret = (val1 <= values2[j]); |
| 4381 | else if (!inf && strict) |
| 4382 | ret = (val1 > values2[j]); |
| 4383 | else if (!inf && !strict) |
| 4384 | ret = (val1 >= values2[j]); |
| 4385 | if (ret) |
| 4386 | break; |
| 4387 | } |
| 4388 | if (ret) |
| 4389 | break; |
| 4390 | init = 1; |
| 4391 | } |
| 4392 | xmlFree(values2); |
Daniel Veillard | 4dd9346 | 2001-04-02 15:16:19 +0000 | [diff] [blame] | 4393 | xmlXPathFreeObject(arg1); |
| 4394 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4395 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | /** |
| 4399 | * xmlXPathCompareNodeSetValue: |
| 4400 | * @ctxt: the XPath Parser context |
| 4401 | * @inf: less than (1) or greater than (0) |
| 4402 | * @strict: is the comparison strict |
| 4403 | * @arg: the node set |
| 4404 | * @val: the value |
| 4405 | * |
| 4406 | * Implement the compare operation between a nodeset and a value |
| 4407 | * @ns < @val (1, 1, ... |
| 4408 | * @ns <= @val (1, 0, ... |
| 4409 | * @ns > @val (0, 1, ... |
| 4410 | * @ns >= @val (0, 0, ... |
| 4411 | * |
| 4412 | * If one object to be compared is a node-set and the other is a boolean, |
| 4413 | * then the comparison will be true if and only if the result of performing |
| 4414 | * the comparison on the boolean and on the result of converting |
| 4415 | * the node-set to a boolean using the boolean function is true. |
| 4416 | * |
| 4417 | * Returns 0 or 1 depending on the results of the test. |
| 4418 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4419 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4420 | xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt, int inf, int strict, |
| 4421 | xmlXPathObjectPtr arg, xmlXPathObjectPtr val) { |
| 4422 | if ((val == NULL) || (arg == NULL) || |
| 4423 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4424 | return(0); |
| 4425 | |
| 4426 | switch(val->type) { |
| 4427 | case XPATH_NUMBER: |
| 4428 | return(xmlXPathCompareNodeSetFloat(ctxt, inf, strict, arg, val)); |
| 4429 | case XPATH_NODESET: |
| 4430 | case XPATH_XSLT_TREE: |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4431 | return(xmlXPathCompareNodeSets(inf, strict, arg, val)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4432 | case XPATH_STRING: |
| 4433 | return(xmlXPathCompareNodeSetString(ctxt, inf, strict, arg, val)); |
| 4434 | case XPATH_BOOLEAN: |
| 4435 | valuePush(ctxt, arg); |
| 4436 | xmlXPathBooleanFunction(ctxt, 1); |
| 4437 | valuePush(ctxt, val); |
| 4438 | return(xmlXPathCompareValues(ctxt, inf, strict)); |
| 4439 | default: |
| 4440 | TODO |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4441 | } |
| 4442 | return(0); |
| 4443 | } |
| 4444 | |
| 4445 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4446 | * xmlXPathEqualNodeSetString: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4447 | * @arg: the nodeset object argument |
| 4448 | * @str: the string to compare to. |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4449 | * @neq: flag to show whether for '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4450 | * |
| 4451 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4452 | * If one object to be compared is a node-set and the other is a string, |
| 4453 | * then the comparison will be true if and only if there is a node in |
| 4454 | * the node-set such that the result of performing the comparison on the |
| 4455 | * string-value of the node and the other string is true. |
| 4456 | * |
| 4457 | * Returns 0 or 1 depending on the results of the test. |
| 4458 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4459 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4460 | xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg, const xmlChar * str, int neq) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4461 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4462 | int i; |
| 4463 | xmlNodeSetPtr ns; |
| 4464 | xmlChar *str2; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4465 | unsigned int hash; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4466 | |
| 4467 | if ((str == NULL) || (arg == NULL) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4468 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4469 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4470 | ns = arg->nodesetval; |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 4471 | /* |
| 4472 | * A NULL nodeset compared with a string is always false |
| 4473 | * (since there is no node equal, and no node not equal) |
| 4474 | */ |
| 4475 | if ((ns == NULL) || (ns->nodeNr <= 0) ) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4476 | return (0); |
William M. Brack | c125a72 | 2003-11-16 08:06:19 +0000 | [diff] [blame] | 4477 | hash = xmlXPathStringHash(str); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4478 | for (i = 0; i < ns->nodeNr; i++) { |
| 4479 | if (xmlXPathNodeValHash(ns->nodeTab[i]) == hash) { |
| 4480 | str2 = xmlNodeGetContent(ns->nodeTab[i]); |
| 4481 | if ((str2 != NULL) && (xmlStrEqual(str, str2))) { |
| 4482 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4483 | if (neq) |
| 4484 | continue; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4485 | return (1); |
Daniel Veillard | 9adc046 | 2003-03-24 18:39:54 +0000 | [diff] [blame] | 4486 | } else if ((str2 == NULL) && (xmlStrEqual(str, BAD_CAST ""))) { |
| 4487 | if (neq) |
| 4488 | continue; |
| 4489 | return (1); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4490 | } else if (neq) { |
| 4491 | if (str2 != NULL) |
| 4492 | xmlFree(str2); |
| 4493 | return (1); |
| 4494 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4495 | if (str2 != NULL) |
| 4496 | xmlFree(str2); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4497 | } else if (neq) |
| 4498 | return (1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4499 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4500 | return (0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4504 | * xmlXPathEqualNodeSetFloat: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4505 | * @arg: the nodeset object argument |
| 4506 | * @f: the float to compare to |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4507 | * @neq: flag to show whether to compare '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4508 | * |
| 4509 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4510 | * If one object to be compared is a node-set and the other is a number, |
| 4511 | * then the comparison will be true if and only if there is a node in |
| 4512 | * the node-set such that the result of performing the comparison on the |
| 4513 | * number to be compared and on the result of converting the string-value |
| 4514 | * of that node to a number using the number function is true. |
| 4515 | * |
| 4516 | * Returns 0 or 1 depending on the results of the test. |
| 4517 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4518 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4519 | xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt, |
| 4520 | xmlXPathObjectPtr arg, double f, int neq) { |
| 4521 | int i, ret=0; |
| 4522 | xmlNodeSetPtr ns; |
| 4523 | xmlChar *str2; |
| 4524 | xmlXPathObjectPtr val; |
| 4525 | double v; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4526 | |
| 4527 | if ((arg == NULL) || |
| 4528 | ((arg->type != XPATH_NODESET) && (arg->type != XPATH_XSLT_TREE))) |
| 4529 | return(0); |
| 4530 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4531 | ns = arg->nodesetval; |
| 4532 | if (ns != NULL) { |
| 4533 | for (i=0;i<ns->nodeNr;i++) { |
| 4534 | str2 = xmlXPathCastNodeToString(ns->nodeTab[i]); |
| 4535 | if (str2 != NULL) { |
| 4536 | valuePush(ctxt, xmlXPathNewString(str2)); |
| 4537 | xmlFree(str2); |
| 4538 | xmlXPathNumberFunction(ctxt, 1); |
| 4539 | val = valuePop(ctxt); |
| 4540 | v = val->floatval; |
| 4541 | xmlXPathFreeObject(val); |
| 4542 | if (!xmlXPathIsNaN(v)) { |
| 4543 | if ((!neq) && (v==f)) { |
| 4544 | ret = 1; |
| 4545 | break; |
| 4546 | } else if ((neq) && (v!=f)) { |
| 4547 | ret = 1; |
| 4548 | break; |
| 4549 | } |
| 4550 | } |
| 4551 | } |
| 4552 | } |
| 4553 | } |
| 4554 | |
| 4555 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | |
| 4559 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4560 | * xmlXPathEqualNodeSets: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4561 | * @arg1: first nodeset object argument |
| 4562 | * @arg2: second nodeset object argument |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4563 | * @neq: flag to show whether to test '=' (0) or '!=' (1) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4564 | * |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4565 | * Implement the equal / not equal operation on XPath nodesets: |
| 4566 | * @arg1 == @arg2 or @arg1 != @arg2 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4567 | * If both objects to be compared are node-sets, then the comparison |
| 4568 | * will be true if and only if there is a node in the first node-set and |
| 4569 | * a node in the second node-set such that the result of performing the |
| 4570 | * comparison on the string-values of the two nodes is true. |
| 4571 | * |
| 4572 | * (needless to say, this is a costly operation) |
| 4573 | * |
| 4574 | * Returns 0 or 1 depending on the results of the test. |
| 4575 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4576 | static int |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4577 | xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2, int neq) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4578 | int i, j; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4579 | unsigned int *hashs1; |
| 4580 | unsigned int *hashs2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4581 | xmlChar **values1; |
| 4582 | xmlChar **values2; |
| 4583 | int ret = 0; |
| 4584 | xmlNodeSetPtr ns1; |
| 4585 | xmlNodeSetPtr ns2; |
| 4586 | |
| 4587 | if ((arg1 == NULL) || |
| 4588 | ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE))) |
| 4589 | return(0); |
| 4590 | if ((arg2 == NULL) || |
| 4591 | ((arg2->type != XPATH_NODESET) && (arg2->type != XPATH_XSLT_TREE))) |
| 4592 | return(0); |
| 4593 | |
| 4594 | ns1 = arg1->nodesetval; |
| 4595 | ns2 = arg2->nodesetval; |
| 4596 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4597 | if ((ns1 == NULL) || (ns1->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4598 | return(0); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 4599 | if ((ns2 == NULL) || (ns2->nodeNr <= 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4600 | return(0); |
| 4601 | |
| 4602 | /* |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4603 | * for equal, check if there is a node pertaining to both sets |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4604 | */ |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4605 | if (neq == 0) |
| 4606 | for (i = 0;i < ns1->nodeNr;i++) |
| 4607 | for (j = 0;j < ns2->nodeNr;j++) |
| 4608 | if (ns1->nodeTab[i] == ns2->nodeTab[j]) |
| 4609 | return(1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4610 | |
| 4611 | values1 = (xmlChar **) xmlMalloc(ns1->nodeNr * sizeof(xmlChar *)); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4612 | if (values1 == NULL) { |
| 4613 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4614 | return(0); |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4615 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4616 | hashs1 = (unsigned int *) xmlMalloc(ns1->nodeNr * sizeof(unsigned int)); |
| 4617 | if (hashs1 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4618 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4619 | xmlFree(values1); |
| 4620 | return(0); |
| 4621 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4622 | memset(values1, 0, ns1->nodeNr * sizeof(xmlChar *)); |
| 4623 | values2 = (xmlChar **) xmlMalloc(ns2->nodeNr * sizeof(xmlChar *)); |
| 4624 | if (values2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4625 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4626 | xmlFree(hashs1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4627 | xmlFree(values1); |
| 4628 | return(0); |
| 4629 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4630 | hashs2 = (unsigned int *) xmlMalloc(ns2->nodeNr * sizeof(unsigned int)); |
| 4631 | if (hashs2 == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 4632 | xmlXPathErrMemory(NULL, "comparing nodesets\n"); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4633 | xmlFree(hashs1); |
| 4634 | xmlFree(values1); |
| 4635 | xmlFree(values2); |
| 4636 | return(0); |
| 4637 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4638 | memset(values2, 0, ns2->nodeNr * sizeof(xmlChar *)); |
| 4639 | for (i = 0;i < ns1->nodeNr;i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4640 | hashs1[i] = xmlXPathNodeValHash(ns1->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4641 | for (j = 0;j < ns2->nodeNr;j++) { |
| 4642 | if (i == 0) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4643 | hashs2[j] = xmlXPathNodeValHash(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4644 | if (hashs1[i] != hashs2[j]) { |
| 4645 | if (neq) { |
| 4646 | ret = 1; |
| 4647 | break; |
| 4648 | } |
| 4649 | } |
| 4650 | else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4651 | if (values1[i] == NULL) |
| 4652 | values1[i] = xmlNodeGetContent(ns1->nodeTab[i]); |
| 4653 | if (values2[j] == NULL) |
| 4654 | values2[j] = xmlNodeGetContent(ns2->nodeTab[j]); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4655 | ret = xmlStrEqual(values1[i], values2[j]) ^ neq; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4656 | if (ret) |
| 4657 | break; |
| 4658 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4659 | } |
| 4660 | if (ret) |
| 4661 | break; |
| 4662 | } |
| 4663 | for (i = 0;i < ns1->nodeNr;i++) |
| 4664 | if (values1[i] != NULL) |
| 4665 | xmlFree(values1[i]); |
| 4666 | for (j = 0;j < ns2->nodeNr;j++) |
| 4667 | if (values2[j] != NULL) |
| 4668 | xmlFree(values2[j]); |
| 4669 | xmlFree(values1); |
| 4670 | xmlFree(values2); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 4671 | xmlFree(hashs1); |
| 4672 | xmlFree(hashs2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4673 | return(ret); |
| 4674 | } |
| 4675 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4676 | static int |
| 4677 | xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt, |
| 4678 | xmlXPathObjectPtr arg1, xmlXPathObjectPtr arg2) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4679 | int ret = 0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4680 | /* |
| 4681 | *At this point we are assured neither arg1 nor arg2 |
| 4682 | *is a nodeset, so we can just pick the appropriate routine. |
| 4683 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4684 | switch (arg1->type) { |
| 4685 | case XPATH_UNDEFINED: |
| 4686 | #ifdef DEBUG_EXPR |
| 4687 | xmlGenericError(xmlGenericErrorContext, |
| 4688 | "Equal: undefined\n"); |
| 4689 | #endif |
| 4690 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4691 | case XPATH_BOOLEAN: |
| 4692 | switch (arg2->type) { |
| 4693 | case XPATH_UNDEFINED: |
| 4694 | #ifdef DEBUG_EXPR |
| 4695 | xmlGenericError(xmlGenericErrorContext, |
| 4696 | "Equal: undefined\n"); |
| 4697 | #endif |
| 4698 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4699 | case XPATH_BOOLEAN: |
| 4700 | #ifdef DEBUG_EXPR |
| 4701 | xmlGenericError(xmlGenericErrorContext, |
| 4702 | "Equal: %d boolean %d \n", |
| 4703 | arg1->boolval, arg2->boolval); |
| 4704 | #endif |
| 4705 | ret = (arg1->boolval == arg2->boolval); |
| 4706 | break; |
| 4707 | case XPATH_NUMBER: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 4708 | ret = (arg1->boolval == |
| 4709 | xmlXPathCastNumberToBoolean(arg2->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4710 | break; |
| 4711 | case XPATH_STRING: |
| 4712 | if ((arg2->stringval == NULL) || |
| 4713 | (arg2->stringval[0] == 0)) ret = 0; |
| 4714 | else |
| 4715 | ret = 1; |
| 4716 | ret = (arg1->boolval == ret); |
| 4717 | break; |
| 4718 | case XPATH_USERS: |
| 4719 | case XPATH_POINT: |
| 4720 | case XPATH_RANGE: |
| 4721 | case XPATH_LOCATIONSET: |
| 4722 | TODO |
| 4723 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4724 | case XPATH_NODESET: |
| 4725 | case XPATH_XSLT_TREE: |
| 4726 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4727 | } |
| 4728 | break; |
| 4729 | case XPATH_NUMBER: |
| 4730 | switch (arg2->type) { |
| 4731 | case XPATH_UNDEFINED: |
| 4732 | #ifdef DEBUG_EXPR |
| 4733 | xmlGenericError(xmlGenericErrorContext, |
| 4734 | "Equal: undefined\n"); |
| 4735 | #endif |
| 4736 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4737 | case XPATH_BOOLEAN: |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 4738 | ret = (arg2->boolval== |
| 4739 | xmlXPathCastNumberToBoolean(arg1->floatval)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4740 | break; |
| 4741 | case XPATH_STRING: |
| 4742 | valuePush(ctxt, arg2); |
| 4743 | xmlXPathNumberFunction(ctxt, 1); |
| 4744 | arg2 = valuePop(ctxt); |
| 4745 | /* no break on purpose */ |
| 4746 | case XPATH_NUMBER: |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4747 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4748 | if (xmlXPathIsNaN(arg1->floatval) || |
| 4749 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4750 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4751 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 4752 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 4753 | ret = 1; |
| 4754 | else |
| 4755 | ret = 0; |
| 4756 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 4757 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 4758 | ret = 1; |
| 4759 | else |
| 4760 | ret = 0; |
| 4761 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 4762 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 4763 | ret = 1; |
| 4764 | else |
| 4765 | ret = 0; |
| 4766 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 4767 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 4768 | ret = 1; |
| 4769 | else |
| 4770 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4771 | } else { |
| 4772 | ret = (arg1->floatval == arg2->floatval); |
| 4773 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4774 | break; |
| 4775 | case XPATH_USERS: |
| 4776 | case XPATH_POINT: |
| 4777 | case XPATH_RANGE: |
| 4778 | case XPATH_LOCATIONSET: |
| 4779 | TODO |
| 4780 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4781 | case XPATH_NODESET: |
| 4782 | case XPATH_XSLT_TREE: |
| 4783 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4784 | } |
| 4785 | break; |
| 4786 | case XPATH_STRING: |
| 4787 | switch (arg2->type) { |
| 4788 | case XPATH_UNDEFINED: |
| 4789 | #ifdef DEBUG_EXPR |
| 4790 | xmlGenericError(xmlGenericErrorContext, |
| 4791 | "Equal: undefined\n"); |
| 4792 | #endif |
| 4793 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4794 | case XPATH_BOOLEAN: |
| 4795 | if ((arg1->stringval == NULL) || |
| 4796 | (arg1->stringval[0] == 0)) ret = 0; |
| 4797 | else |
| 4798 | ret = 1; |
| 4799 | ret = (arg2->boolval == ret); |
| 4800 | break; |
| 4801 | case XPATH_STRING: |
| 4802 | ret = xmlStrEqual(arg1->stringval, arg2->stringval); |
| 4803 | break; |
| 4804 | case XPATH_NUMBER: |
| 4805 | valuePush(ctxt, arg1); |
| 4806 | xmlXPathNumberFunction(ctxt, 1); |
| 4807 | arg1 = valuePop(ctxt); |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4808 | /* Hand check NaN and Infinity equalities */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 4809 | if (xmlXPathIsNaN(arg1->floatval) || |
| 4810 | xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4811 | ret = 0; |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 4812 | } else if (xmlXPathIsInf(arg1->floatval) == 1) { |
| 4813 | if (xmlXPathIsInf(arg2->floatval) == 1) |
| 4814 | ret = 1; |
| 4815 | else |
| 4816 | ret = 0; |
| 4817 | } else if (xmlXPathIsInf(arg1->floatval) == -1) { |
| 4818 | if (xmlXPathIsInf(arg2->floatval) == -1) |
| 4819 | ret = 1; |
| 4820 | else |
| 4821 | ret = 0; |
| 4822 | } else if (xmlXPathIsInf(arg2->floatval) == 1) { |
| 4823 | if (xmlXPathIsInf(arg1->floatval) == 1) |
| 4824 | ret = 1; |
| 4825 | else |
| 4826 | ret = 0; |
| 4827 | } else if (xmlXPathIsInf(arg2->floatval) == -1) { |
| 4828 | if (xmlXPathIsInf(arg1->floatval) == -1) |
| 4829 | ret = 1; |
| 4830 | else |
| 4831 | ret = 0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 4832 | } else { |
| 4833 | ret = (arg1->floatval == arg2->floatval); |
| 4834 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4835 | break; |
| 4836 | case XPATH_USERS: |
| 4837 | case XPATH_POINT: |
| 4838 | case XPATH_RANGE: |
| 4839 | case XPATH_LOCATIONSET: |
| 4840 | TODO |
| 4841 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4842 | case XPATH_NODESET: |
| 4843 | case XPATH_XSLT_TREE: |
| 4844 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4845 | } |
| 4846 | break; |
| 4847 | case XPATH_USERS: |
| 4848 | case XPATH_POINT: |
| 4849 | case XPATH_RANGE: |
| 4850 | case XPATH_LOCATIONSET: |
| 4851 | TODO |
| 4852 | break; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4853 | case XPATH_NODESET: |
| 4854 | case XPATH_XSLT_TREE: |
| 4855 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4856 | } |
| 4857 | xmlXPathFreeObject(arg1); |
| 4858 | xmlXPathFreeObject(arg2); |
| 4859 | return(ret); |
| 4860 | } |
| 4861 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4862 | /** |
| 4863 | * xmlXPathEqualValues: |
| 4864 | * @ctxt: the XPath Parser context |
| 4865 | * |
| 4866 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4867 | * |
| 4868 | * Returns 0 or 1 depending on the results of the test. |
| 4869 | */ |
| 4870 | int |
| 4871 | xmlXPathEqualValues(xmlXPathParserContextPtr ctxt) { |
| 4872 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 4873 | int ret = 0; |
| 4874 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 4875 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4876 | arg2 = valuePop(ctxt); |
| 4877 | arg1 = valuePop(ctxt); |
| 4878 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 4879 | if (arg1 != NULL) |
| 4880 | xmlXPathFreeObject(arg1); |
| 4881 | else |
| 4882 | xmlXPathFreeObject(arg2); |
| 4883 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4884 | } |
| 4885 | |
| 4886 | if (arg1 == arg2) { |
| 4887 | #ifdef DEBUG_EXPR |
| 4888 | xmlGenericError(xmlGenericErrorContext, |
| 4889 | "Equal: by pointer\n"); |
| 4890 | #endif |
| 4891 | return(1); |
| 4892 | } |
| 4893 | |
| 4894 | /* |
| 4895 | *If either argument is a nodeset, it's a 'special case' |
| 4896 | */ |
| 4897 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 4898 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 4899 | /* |
| 4900 | *Hack it to assure arg1 is the nodeset |
| 4901 | */ |
| 4902 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 4903 | argtmp = arg2; |
| 4904 | arg2 = arg1; |
| 4905 | arg1 = argtmp; |
| 4906 | } |
| 4907 | switch (arg2->type) { |
| 4908 | case XPATH_UNDEFINED: |
| 4909 | #ifdef DEBUG_EXPR |
| 4910 | xmlGenericError(xmlGenericErrorContext, |
| 4911 | "Equal: undefined\n"); |
| 4912 | #endif |
| 4913 | break; |
| 4914 | case XPATH_NODESET: |
| 4915 | case XPATH_XSLT_TREE: |
| 4916 | ret = xmlXPathEqualNodeSets(arg1, arg2, 0); |
| 4917 | break; |
| 4918 | case XPATH_BOOLEAN: |
| 4919 | if ((arg1->nodesetval == NULL) || |
| 4920 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 4921 | else |
| 4922 | ret = 1; |
| 4923 | ret = (ret == arg2->boolval); |
| 4924 | break; |
| 4925 | case XPATH_NUMBER: |
| 4926 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 0); |
| 4927 | break; |
| 4928 | case XPATH_STRING: |
| 4929 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval, 0); |
| 4930 | break; |
| 4931 | case XPATH_USERS: |
| 4932 | case XPATH_POINT: |
| 4933 | case XPATH_RANGE: |
| 4934 | case XPATH_LOCATIONSET: |
| 4935 | TODO |
| 4936 | break; |
| 4937 | } |
| 4938 | xmlXPathFreeObject(arg1); |
| 4939 | xmlXPathFreeObject(arg2); |
| 4940 | return(ret); |
| 4941 | } |
| 4942 | |
| 4943 | return (xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 4944 | } |
| 4945 | |
| 4946 | /** |
| 4947 | * xmlXPathNotEqualValues: |
| 4948 | * @ctxt: the XPath Parser context |
| 4949 | * |
| 4950 | * Implement the equal operation on XPath objects content: @arg1 == @arg2 |
| 4951 | * |
| 4952 | * Returns 0 or 1 depending on the results of the test. |
| 4953 | */ |
| 4954 | int |
| 4955 | xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt) { |
| 4956 | xmlXPathObjectPtr arg1, arg2, argtmp; |
| 4957 | int ret = 0; |
| 4958 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 4959 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 4960 | arg2 = valuePop(ctxt); |
| 4961 | arg1 = valuePop(ctxt); |
| 4962 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 4963 | if (arg1 != NULL) |
| 4964 | xmlXPathFreeObject(arg1); |
| 4965 | else |
| 4966 | xmlXPathFreeObject(arg2); |
| 4967 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 4968 | } |
| 4969 | |
| 4970 | if (arg1 == arg2) { |
| 4971 | #ifdef DEBUG_EXPR |
| 4972 | xmlGenericError(xmlGenericErrorContext, |
| 4973 | "NotEqual: by pointer\n"); |
| 4974 | #endif |
| 4975 | return(0); |
| 4976 | } |
| 4977 | |
| 4978 | /* |
| 4979 | *If either argument is a nodeset, it's a 'special case' |
| 4980 | */ |
| 4981 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 4982 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 4983 | /* |
| 4984 | *Hack it to assure arg1 is the nodeset |
| 4985 | */ |
| 4986 | if ((arg1->type != XPATH_NODESET) && (arg1->type != XPATH_XSLT_TREE)) { |
| 4987 | argtmp = arg2; |
| 4988 | arg2 = arg1; |
| 4989 | arg1 = argtmp; |
| 4990 | } |
| 4991 | switch (arg2->type) { |
| 4992 | case XPATH_UNDEFINED: |
| 4993 | #ifdef DEBUG_EXPR |
| 4994 | xmlGenericError(xmlGenericErrorContext, |
| 4995 | "NotEqual: undefined\n"); |
| 4996 | #endif |
| 4997 | break; |
| 4998 | case XPATH_NODESET: |
| 4999 | case XPATH_XSLT_TREE: |
| 5000 | ret = xmlXPathEqualNodeSets(arg1, arg2, 1); |
| 5001 | break; |
| 5002 | case XPATH_BOOLEAN: |
| 5003 | if ((arg1->nodesetval == NULL) || |
| 5004 | (arg1->nodesetval->nodeNr == 0)) ret = 0; |
| 5005 | else |
| 5006 | ret = 1; |
William M. Brack | ef61d20 | 2002-07-19 08:32:00 +0000 | [diff] [blame] | 5007 | ret = (ret != arg2->boolval); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5008 | break; |
| 5009 | case XPATH_NUMBER: |
| 5010 | ret = xmlXPathEqualNodeSetFloat(ctxt, arg1, arg2->floatval, 1); |
| 5011 | break; |
| 5012 | case XPATH_STRING: |
| 5013 | ret = xmlXPathEqualNodeSetString(arg1, arg2->stringval,1); |
| 5014 | break; |
| 5015 | case XPATH_USERS: |
| 5016 | case XPATH_POINT: |
| 5017 | case XPATH_RANGE: |
| 5018 | case XPATH_LOCATIONSET: |
| 5019 | TODO |
| 5020 | break; |
| 5021 | } |
| 5022 | xmlXPathFreeObject(arg1); |
| 5023 | xmlXPathFreeObject(arg2); |
| 5024 | return(ret); |
| 5025 | } |
| 5026 | |
| 5027 | return (!xmlXPathEqualValuesCommon(ctxt, arg1, arg2)); |
| 5028 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5029 | |
| 5030 | /** |
| 5031 | * xmlXPathCompareValues: |
| 5032 | * @ctxt: the XPath Parser context |
| 5033 | * @inf: less than (1) or greater than (0) |
| 5034 | * @strict: is the comparison strict |
| 5035 | * |
| 5036 | * Implement the compare operation on XPath objects: |
| 5037 | * @arg1 < @arg2 (1, 1, ... |
| 5038 | * @arg1 <= @arg2 (1, 0, ... |
| 5039 | * @arg1 > @arg2 (0, 1, ... |
| 5040 | * @arg1 >= @arg2 (0, 0, ... |
| 5041 | * |
| 5042 | * When neither object to be compared is a node-set and the operator is |
| 5043 | * <=, <, >=, >, then the objects are compared by converted both objects |
| 5044 | * to numbers and comparing the numbers according to IEEE 754. The < |
| 5045 | * comparison will be true if and only if the first number is less than the |
| 5046 | * second number. The <= comparison will be true if and only if the first |
| 5047 | * number is less than or equal to the second number. The > comparison |
| 5048 | * will be true if and only if the first number is greater than the second |
| 5049 | * number. The >= comparison will be true if and only if the first number |
| 5050 | * is greater than or equal to the second number. |
| 5051 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5052 | * Returns 1 if the comparison succeeded, 0 if it failed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5053 | */ |
| 5054 | int |
| 5055 | xmlXPathCompareValues(xmlXPathParserContextPtr ctxt, int inf, int strict) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5056 | int ret = 0, arg1i = 0, arg2i = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5057 | xmlXPathObjectPtr arg1, arg2; |
| 5058 | |
Daniel Veillard | 6128c01 | 2004-11-08 17:16:15 +0000 | [diff] [blame] | 5059 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(0); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5060 | arg2 = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5061 | arg1 = valuePop(ctxt); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5062 | if ((arg1 == NULL) || (arg2 == NULL)) { |
| 5063 | if (arg1 != NULL) |
| 5064 | xmlXPathFreeObject(arg1); |
| 5065 | else |
| 5066 | xmlXPathFreeObject(arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5067 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5068 | } |
| 5069 | |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5070 | if ((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE) || |
| 5071 | (arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
| 5072 | if (((arg2->type == XPATH_NODESET) || (arg2->type == XPATH_XSLT_TREE)) && |
| 5073 | ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE))){ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5074 | ret = xmlXPathCompareNodeSets(inf, strict, arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5075 | } else { |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 5076 | if ((arg1->type == XPATH_NODESET) || (arg1->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 5077 | ret = xmlXPathCompareNodeSetValue(ctxt, inf, strict, |
| 5078 | arg1, arg2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5079 | } else { |
Daniel Veillard | 4af6b6e | 2001-03-06 08:33:38 +0000 | [diff] [blame] | 5080 | ret = xmlXPathCompareNodeSetValue(ctxt, !inf, strict, |
| 5081 | arg2, arg1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5082 | } |
| 5083 | } |
| 5084 | return(ret); |
| 5085 | } |
| 5086 | |
| 5087 | if (arg1->type != XPATH_NUMBER) { |
| 5088 | valuePush(ctxt, arg1); |
| 5089 | xmlXPathNumberFunction(ctxt, 1); |
| 5090 | arg1 = valuePop(ctxt); |
| 5091 | } |
| 5092 | if (arg1->type != XPATH_NUMBER) { |
| 5093 | xmlXPathFreeObject(arg1); |
| 5094 | xmlXPathFreeObject(arg2); |
| 5095 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5096 | } |
| 5097 | if (arg2->type != XPATH_NUMBER) { |
| 5098 | valuePush(ctxt, arg2); |
| 5099 | xmlXPathNumberFunction(ctxt, 1); |
| 5100 | arg2 = valuePop(ctxt); |
| 5101 | } |
| 5102 | if (arg2->type != XPATH_NUMBER) { |
| 5103 | xmlXPathFreeObject(arg1); |
| 5104 | xmlXPathFreeObject(arg2); |
| 5105 | XP_ERROR0(XPATH_INVALID_OPERAND); |
| 5106 | } |
| 5107 | /* |
| 5108 | * Add tests for infinity and nan |
| 5109 | * => feedback on 3.4 for Inf and NaN |
| 5110 | */ |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5111 | /* Hand check NaN and Infinity comparisons */ |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5112 | if (xmlXPathIsNaN(arg1->floatval) || xmlXPathIsNaN(arg2->floatval)) { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5113 | ret=0; |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5114 | } else { |
Daniel Veillard | d30be4a | 2002-03-28 18:25:31 +0000 | [diff] [blame] | 5115 | arg1i=xmlXPathIsInf(arg1->floatval); |
| 5116 | arg2i=xmlXPathIsInf(arg2->floatval); |
| 5117 | if (inf && strict) { |
| 5118 | if ((arg1i == -1 && arg2i != -1) || |
| 5119 | (arg2i == 1 && arg1i != 1)) { |
| 5120 | ret = 1; |
| 5121 | } else if (arg1i == 0 && arg2i == 0) { |
| 5122 | ret = (arg1->floatval < arg2->floatval); |
| 5123 | } else { |
| 5124 | ret = 0; |
| 5125 | } |
| 5126 | } |
| 5127 | else if (inf && !strict) { |
| 5128 | if (arg1i == -1 || arg2i == 1) { |
| 5129 | ret = 1; |
| 5130 | } else if (arg1i == 0 && arg2i == 0) { |
| 5131 | ret = (arg1->floatval <= arg2->floatval); |
| 5132 | } else { |
| 5133 | ret = 0; |
| 5134 | } |
| 5135 | } |
| 5136 | else if (!inf && strict) { |
| 5137 | if ((arg1i == 1 && arg2i != 1) || |
| 5138 | (arg2i == -1 && arg1i != -1)) { |
| 5139 | ret = 1; |
| 5140 | } else if (arg1i == 0 && arg2i == 0) { |
| 5141 | ret = (arg1->floatval > arg2->floatval); |
| 5142 | } else { |
| 5143 | ret = 0; |
| 5144 | } |
| 5145 | } |
| 5146 | else if (!inf && !strict) { |
| 5147 | if (arg1i == 1 || arg2i == -1) { |
| 5148 | ret = 1; |
| 5149 | } else if (arg1i == 0 && arg2i == 0) { |
| 5150 | ret = (arg1->floatval >= arg2->floatval); |
| 5151 | } else { |
| 5152 | ret = 0; |
| 5153 | } |
| 5154 | } |
Daniel Veillard | 21458c8 | 2002-03-27 16:12:22 +0000 | [diff] [blame] | 5155 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5156 | xmlXPathFreeObject(arg1); |
| 5157 | xmlXPathFreeObject(arg2); |
| 5158 | return(ret); |
| 5159 | } |
| 5160 | |
| 5161 | /** |
| 5162 | * xmlXPathValueFlipSign: |
| 5163 | * @ctxt: the XPath Parser context |
| 5164 | * |
| 5165 | * Implement the unary - operation on an XPath object |
| 5166 | * The numeric operators convert their operands to numbers as if |
| 5167 | * by calling the number function. |
| 5168 | */ |
| 5169 | void |
| 5170 | xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5171 | if ((ctxt == NULL) || (ctxt->context == NULL)) return; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5172 | CAST_TO_NUMBER; |
| 5173 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 5174 | if (xmlXPathIsNaN(ctxt->value->floatval)) |
| 5175 | ctxt->value->floatval=xmlXPathNAN; |
| 5176 | else if (xmlXPathIsInf(ctxt->value->floatval) == 1) |
| 5177 | ctxt->value->floatval=xmlXPathNINF; |
| 5178 | else if (xmlXPathIsInf(ctxt->value->floatval) == -1) |
| 5179 | ctxt->value->floatval=xmlXPathPINF; |
| 5180 | else if (ctxt->value->floatval == 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5181 | if (xmlXPathGetSign(ctxt->value->floatval) == 0) |
| 5182 | ctxt->value->floatval = xmlXPathNZERO; |
| 5183 | else |
| 5184 | ctxt->value->floatval = 0; |
| 5185 | } |
| 5186 | else |
| 5187 | ctxt->value->floatval = - ctxt->value->floatval; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5188 | } |
| 5189 | |
| 5190 | /** |
| 5191 | * xmlXPathAddValues: |
| 5192 | * @ctxt: the XPath Parser context |
| 5193 | * |
| 5194 | * Implement the add operation on XPath objects: |
| 5195 | * The numeric operators convert their operands to numbers as if |
| 5196 | * by calling the number function. |
| 5197 | */ |
| 5198 | void |
| 5199 | xmlXPathAddValues(xmlXPathParserContextPtr ctxt) { |
| 5200 | xmlXPathObjectPtr arg; |
| 5201 | double val; |
| 5202 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5203 | arg = valuePop(ctxt); |
| 5204 | if (arg == NULL) |
| 5205 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5206 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5207 | xmlXPathFreeObject(arg); |
| 5208 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5209 | CAST_TO_NUMBER; |
| 5210 | CHECK_TYPE(XPATH_NUMBER); |
| 5211 | ctxt->value->floatval += val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5212 | } |
| 5213 | |
| 5214 | /** |
| 5215 | * xmlXPathSubValues: |
| 5216 | * @ctxt: the XPath Parser context |
| 5217 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5218 | * Implement the subtraction operation on XPath objects: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5219 | * The numeric operators convert their operands to numbers as if |
| 5220 | * by calling the number function. |
| 5221 | */ |
| 5222 | void |
| 5223 | xmlXPathSubValues(xmlXPathParserContextPtr ctxt) { |
| 5224 | xmlXPathObjectPtr arg; |
| 5225 | double val; |
| 5226 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5227 | arg = valuePop(ctxt); |
| 5228 | if (arg == NULL) |
| 5229 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5230 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5231 | xmlXPathFreeObject(arg); |
| 5232 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5233 | CAST_TO_NUMBER; |
| 5234 | CHECK_TYPE(XPATH_NUMBER); |
| 5235 | ctxt->value->floatval -= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
| 5238 | /** |
| 5239 | * xmlXPathMultValues: |
| 5240 | * @ctxt: the XPath Parser context |
| 5241 | * |
| 5242 | * Implement the multiply operation on XPath objects: |
| 5243 | * The numeric operators convert their operands to numbers as if |
| 5244 | * by calling the number function. |
| 5245 | */ |
| 5246 | void |
| 5247 | xmlXPathMultValues(xmlXPathParserContextPtr ctxt) { |
| 5248 | xmlXPathObjectPtr arg; |
| 5249 | double val; |
| 5250 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5251 | arg = valuePop(ctxt); |
| 5252 | if (arg == NULL) |
| 5253 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5254 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5255 | xmlXPathFreeObject(arg); |
| 5256 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5257 | CAST_TO_NUMBER; |
| 5258 | CHECK_TYPE(XPATH_NUMBER); |
| 5259 | ctxt->value->floatval *= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5260 | } |
| 5261 | |
| 5262 | /** |
| 5263 | * xmlXPathDivValues: |
| 5264 | * @ctxt: the XPath Parser context |
| 5265 | * |
| 5266 | * Implement the div operation on XPath objects @arg1 / @arg2: |
| 5267 | * The numeric operators convert their operands to numbers as if |
| 5268 | * by calling the number function. |
| 5269 | */ |
| 5270 | void |
| 5271 | xmlXPathDivValues(xmlXPathParserContextPtr ctxt) { |
| 5272 | xmlXPathObjectPtr arg; |
| 5273 | double val; |
| 5274 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5275 | arg = valuePop(ctxt); |
| 5276 | if (arg == NULL) |
| 5277 | XP_ERROR(XPATH_INVALID_OPERAND); |
| 5278 | val = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5279 | xmlXPathFreeObject(arg); |
| 5280 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5281 | CAST_TO_NUMBER; |
| 5282 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 5283 | if (xmlXPathIsNaN(val) || xmlXPathIsNaN(ctxt->value->floatval)) |
| 5284 | ctxt->value->floatval = xmlXPathNAN; |
| 5285 | else if (val == 0 && xmlXPathGetSign(val) != 0) { |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5286 | if (ctxt->value->floatval == 0) |
| 5287 | ctxt->value->floatval = xmlXPathNAN; |
| 5288 | else if (ctxt->value->floatval > 0) |
| 5289 | ctxt->value->floatval = xmlXPathNINF; |
| 5290 | else if (ctxt->value->floatval < 0) |
| 5291 | ctxt->value->floatval = xmlXPathPINF; |
| 5292 | } |
| 5293 | else if (val == 0) { |
Daniel Veillard | 5f4b599 | 2002-02-20 10:22:49 +0000 | [diff] [blame] | 5294 | if (ctxt->value->floatval == 0) |
| 5295 | ctxt->value->floatval = xmlXPathNAN; |
| 5296 | else if (ctxt->value->floatval > 0) |
| 5297 | ctxt->value->floatval = xmlXPathPINF; |
| 5298 | else if (ctxt->value->floatval < 0) |
| 5299 | ctxt->value->floatval = xmlXPathNINF; |
| 5300 | } else |
| 5301 | ctxt->value->floatval /= val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5302 | } |
| 5303 | |
| 5304 | /** |
| 5305 | * xmlXPathModValues: |
| 5306 | * @ctxt: the XPath Parser context |
| 5307 | * |
| 5308 | * Implement the mod operation on XPath objects: @arg1 / @arg2 |
| 5309 | * The numeric operators convert their operands to numbers as if |
| 5310 | * by calling the number function. |
| 5311 | */ |
| 5312 | void |
| 5313 | xmlXPathModValues(xmlXPathParserContextPtr ctxt) { |
| 5314 | xmlXPathObjectPtr arg; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5315 | double arg1, arg2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5316 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5317 | arg = valuePop(ctxt); |
| 5318 | if (arg == NULL) |
| 5319 | XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5320 | arg2 = xmlXPathCastToNumber(arg); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5321 | xmlXPathFreeObject(arg); |
| 5322 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 5323 | CAST_TO_NUMBER; |
| 5324 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5325 | arg1 = ctxt->value->floatval; |
Daniel Veillard | 268fd1b | 2001-08-26 18:46:36 +0000 | [diff] [blame] | 5326 | if (arg2 == 0) |
| 5327 | ctxt->value->floatval = xmlXPathNAN; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5328 | else { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5329 | ctxt->value->floatval = fmod(arg1, arg2); |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 5330 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
| 5333 | /************************************************************************ |
| 5334 | * * |
| 5335 | * The traversal functions * |
| 5336 | * * |
| 5337 | ************************************************************************/ |
| 5338 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5339 | /* |
| 5340 | * A traversal function enumerates nodes along an axis. |
| 5341 | * Initially it must be called with NULL, and it indicates |
| 5342 | * termination on the axis by returning NULL. |
| 5343 | */ |
| 5344 | typedef xmlNodePtr (*xmlXPathTraversalFunction) |
| 5345 | (xmlXPathParserContextPtr ctxt, xmlNodePtr cur); |
| 5346 | |
| 5347 | /** |
| 5348 | * xmlXPathNextSelf: |
| 5349 | * @ctxt: the XPath Parser context |
| 5350 | * @cur: the current node in the traversal |
| 5351 | * |
| 5352 | * Traversal function for the "self" direction |
| 5353 | * The self axis contains just the context node itself |
| 5354 | * |
| 5355 | * Returns the next element following that axis |
| 5356 | */ |
| 5357 | xmlNodePtr |
| 5358 | xmlXPathNextSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5359 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5360 | if (cur == NULL) |
| 5361 | return(ctxt->context->node); |
| 5362 | return(NULL); |
| 5363 | } |
| 5364 | |
| 5365 | /** |
| 5366 | * xmlXPathNextChild: |
| 5367 | * @ctxt: the XPath Parser context |
| 5368 | * @cur: the current node in the traversal |
| 5369 | * |
| 5370 | * Traversal function for the "child" direction |
| 5371 | * The child axis contains the children of the context node in document order. |
| 5372 | * |
| 5373 | * Returns the next element following that axis |
| 5374 | */ |
| 5375 | xmlNodePtr |
| 5376 | xmlXPathNextChild(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5377 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5378 | if (cur == NULL) { |
| 5379 | if (ctxt->context->node == NULL) return(NULL); |
| 5380 | switch (ctxt->context->node->type) { |
| 5381 | case XML_ELEMENT_NODE: |
| 5382 | case XML_TEXT_NODE: |
| 5383 | case XML_CDATA_SECTION_NODE: |
| 5384 | case XML_ENTITY_REF_NODE: |
| 5385 | case XML_ENTITY_NODE: |
| 5386 | case XML_PI_NODE: |
| 5387 | case XML_COMMENT_NODE: |
| 5388 | case XML_NOTATION_NODE: |
| 5389 | case XML_DTD_NODE: |
| 5390 | return(ctxt->context->node->children); |
| 5391 | case XML_DOCUMENT_NODE: |
| 5392 | case XML_DOCUMENT_TYPE_NODE: |
| 5393 | case XML_DOCUMENT_FRAG_NODE: |
| 5394 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5395 | #ifdef LIBXML_DOCB_ENABLED |
| 5396 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5397 | #endif |
| 5398 | return(((xmlDocPtr) ctxt->context->node)->children); |
| 5399 | case XML_ELEMENT_DECL: |
| 5400 | case XML_ATTRIBUTE_DECL: |
| 5401 | case XML_ENTITY_DECL: |
| 5402 | case XML_ATTRIBUTE_NODE: |
| 5403 | case XML_NAMESPACE_DECL: |
| 5404 | case XML_XINCLUDE_START: |
| 5405 | case XML_XINCLUDE_END: |
| 5406 | return(NULL); |
| 5407 | } |
| 5408 | return(NULL); |
| 5409 | } |
| 5410 | if ((cur->type == XML_DOCUMENT_NODE) || |
| 5411 | (cur->type == XML_HTML_DOCUMENT_NODE)) |
| 5412 | return(NULL); |
| 5413 | return(cur->next); |
| 5414 | } |
| 5415 | |
| 5416 | /** |
| 5417 | * xmlXPathNextDescendant: |
| 5418 | * @ctxt: the XPath Parser context |
| 5419 | * @cur: the current node in the traversal |
| 5420 | * |
| 5421 | * Traversal function for the "descendant" direction |
| 5422 | * the descendant axis contains the descendants of the context node in document |
| 5423 | * order; a descendant is a child or a child of a child and so on. |
| 5424 | * |
| 5425 | * Returns the next element following that axis |
| 5426 | */ |
| 5427 | xmlNodePtr |
| 5428 | xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5429 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5430 | if (cur == NULL) { |
| 5431 | if (ctxt->context->node == NULL) |
| 5432 | return(NULL); |
| 5433 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5434 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5435 | return(NULL); |
| 5436 | |
| 5437 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 5438 | return(ctxt->context->doc->children); |
| 5439 | return(ctxt->context->node->children); |
| 5440 | } |
| 5441 | |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5442 | if (cur->children != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5443 | /* |
| 5444 | * Do not descend on entities declarations |
| 5445 | */ |
| 5446 | if (cur->children->type != XML_ENTITY_DECL) { |
| 5447 | cur = cur->children; |
| 5448 | /* |
| 5449 | * Skip DTDs |
| 5450 | */ |
| 5451 | if (cur->type != XML_DTD_NODE) |
| 5452 | return(cur); |
| 5453 | } |
Daniel Veillard | 567e1b4 | 2001-08-01 15:53:47 +0000 | [diff] [blame] | 5454 | } |
| 5455 | |
| 5456 | if (cur == ctxt->context->node) return(NULL); |
| 5457 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5458 | while (cur->next != NULL) { |
| 5459 | cur = cur->next; |
| 5460 | if ((cur->type != XML_ENTITY_DECL) && |
| 5461 | (cur->type != XML_DTD_NODE)) |
| 5462 | return(cur); |
| 5463 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5464 | |
| 5465 | do { |
| 5466 | cur = cur->parent; |
| 5467 | if (cur == NULL) return(NULL); |
| 5468 | if (cur == ctxt->context->node) return(NULL); |
| 5469 | if (cur->next != NULL) { |
| 5470 | cur = cur->next; |
| 5471 | return(cur); |
| 5472 | } |
| 5473 | } while (cur != NULL); |
| 5474 | return(cur); |
| 5475 | } |
| 5476 | |
| 5477 | /** |
| 5478 | * xmlXPathNextDescendantOrSelf: |
| 5479 | * @ctxt: the XPath Parser context |
| 5480 | * @cur: the current node in the traversal |
| 5481 | * |
| 5482 | * Traversal function for the "descendant-or-self" direction |
| 5483 | * the descendant-or-self axis contains the context node and the descendants |
| 5484 | * of the context node in document order; thus the context node is the first |
| 5485 | * node on the axis, and the first child of the context node is the second node |
| 5486 | * on the axis |
| 5487 | * |
| 5488 | * Returns the next element following that axis |
| 5489 | */ |
| 5490 | xmlNodePtr |
| 5491 | xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5492 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5493 | if (cur == NULL) { |
| 5494 | if (ctxt->context->node == NULL) |
| 5495 | return(NULL); |
| 5496 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5497 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5498 | return(NULL); |
| 5499 | return(ctxt->context->node); |
| 5500 | } |
| 5501 | |
| 5502 | return(xmlXPathNextDescendant(ctxt, cur)); |
| 5503 | } |
| 5504 | |
| 5505 | /** |
| 5506 | * xmlXPathNextParent: |
| 5507 | * @ctxt: the XPath Parser context |
| 5508 | * @cur: the current node in the traversal |
| 5509 | * |
| 5510 | * Traversal function for the "parent" direction |
| 5511 | * The parent axis contains the parent of the context node, if there is one. |
| 5512 | * |
| 5513 | * Returns the next element following that axis |
| 5514 | */ |
| 5515 | xmlNodePtr |
| 5516 | xmlXPathNextParent(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5517 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5518 | /* |
| 5519 | * the parent of an attribute or namespace node is the element |
| 5520 | * to which the attribute or namespace node is attached |
| 5521 | * Namespace handling !!! |
| 5522 | */ |
| 5523 | if (cur == NULL) { |
| 5524 | if (ctxt->context->node == NULL) return(NULL); |
| 5525 | switch (ctxt->context->node->type) { |
| 5526 | case XML_ELEMENT_NODE: |
| 5527 | case XML_TEXT_NODE: |
| 5528 | case XML_CDATA_SECTION_NODE: |
| 5529 | case XML_ENTITY_REF_NODE: |
| 5530 | case XML_ENTITY_NODE: |
| 5531 | case XML_PI_NODE: |
| 5532 | case XML_COMMENT_NODE: |
| 5533 | case XML_NOTATION_NODE: |
| 5534 | case XML_DTD_NODE: |
| 5535 | case XML_ELEMENT_DECL: |
| 5536 | case XML_ATTRIBUTE_DECL: |
| 5537 | case XML_XINCLUDE_START: |
| 5538 | case XML_XINCLUDE_END: |
| 5539 | case XML_ENTITY_DECL: |
| 5540 | if (ctxt->context->node->parent == NULL) |
| 5541 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5542 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5543 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 5544 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 5545 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5546 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5547 | return(ctxt->context->node->parent); |
| 5548 | case XML_ATTRIBUTE_NODE: { |
| 5549 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5550 | |
| 5551 | return(att->parent); |
| 5552 | } |
| 5553 | case XML_DOCUMENT_NODE: |
| 5554 | case XML_DOCUMENT_TYPE_NODE: |
| 5555 | case XML_DOCUMENT_FRAG_NODE: |
| 5556 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5557 | #ifdef LIBXML_DOCB_ENABLED |
| 5558 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5559 | #endif |
| 5560 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5561 | case XML_NAMESPACE_DECL: { |
| 5562 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5563 | |
| 5564 | if ((ns->next != NULL) && |
| 5565 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5566 | return((xmlNodePtr) ns->next); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5567 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5568 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5569 | } |
| 5570 | } |
| 5571 | return(NULL); |
| 5572 | } |
| 5573 | |
| 5574 | /** |
| 5575 | * xmlXPathNextAncestor: |
| 5576 | * @ctxt: the XPath Parser context |
| 5577 | * @cur: the current node in the traversal |
| 5578 | * |
| 5579 | * Traversal function for the "ancestor" direction |
| 5580 | * the ancestor axis contains the ancestors of the context node; the ancestors |
| 5581 | * of the context node consist of the parent of context node and the parent's |
| 5582 | * parent and so on; the nodes are ordered in reverse document order; thus the |
| 5583 | * parent is the first node on the axis, and the parent's parent is the second |
| 5584 | * node on the axis |
| 5585 | * |
| 5586 | * Returns the next element following that axis |
| 5587 | */ |
| 5588 | xmlNodePtr |
| 5589 | xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5590 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5591 | /* |
| 5592 | * the parent of an attribute or namespace node is the element |
| 5593 | * to which the attribute or namespace node is attached |
| 5594 | * !!!!!!!!!!!!! |
| 5595 | */ |
| 5596 | if (cur == NULL) { |
| 5597 | if (ctxt->context->node == NULL) return(NULL); |
| 5598 | switch (ctxt->context->node->type) { |
| 5599 | case XML_ELEMENT_NODE: |
| 5600 | case XML_TEXT_NODE: |
| 5601 | case XML_CDATA_SECTION_NODE: |
| 5602 | case XML_ENTITY_REF_NODE: |
| 5603 | case XML_ENTITY_NODE: |
| 5604 | case XML_PI_NODE: |
| 5605 | case XML_COMMENT_NODE: |
| 5606 | case XML_DTD_NODE: |
| 5607 | case XML_ELEMENT_DECL: |
| 5608 | case XML_ATTRIBUTE_DECL: |
| 5609 | case XML_ENTITY_DECL: |
| 5610 | case XML_NOTATION_NODE: |
| 5611 | case XML_XINCLUDE_START: |
| 5612 | case XML_XINCLUDE_END: |
| 5613 | if (ctxt->context->node->parent == NULL) |
| 5614 | return((xmlNodePtr) ctxt->context->doc); |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5615 | if ((ctxt->context->node->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5616 | ((ctxt->context->node->parent->name[0] == ' ') || |
| 5617 | (xmlStrEqual(ctxt->context->node->parent->name, |
| 5618 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5619 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5620 | return(ctxt->context->node->parent); |
| 5621 | case XML_ATTRIBUTE_NODE: { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5622 | xmlAttrPtr tmp = (xmlAttrPtr) ctxt->context->node; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5623 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5624 | return(tmp->parent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5625 | } |
| 5626 | case XML_DOCUMENT_NODE: |
| 5627 | case XML_DOCUMENT_TYPE_NODE: |
| 5628 | case XML_DOCUMENT_FRAG_NODE: |
| 5629 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5630 | #ifdef LIBXML_DOCB_ENABLED |
| 5631 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5632 | #endif |
| 5633 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5634 | case XML_NAMESPACE_DECL: { |
| 5635 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5636 | |
| 5637 | if ((ns->next != NULL) && |
| 5638 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5639 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5640 | /* Bad, how did that namespace end up here ? */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5641 | return(NULL); |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 5642 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5643 | } |
| 5644 | return(NULL); |
| 5645 | } |
| 5646 | if (cur == ctxt->context->doc->children) |
| 5647 | return((xmlNodePtr) ctxt->context->doc); |
| 5648 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5649 | return(NULL); |
| 5650 | switch (cur->type) { |
| 5651 | case XML_ELEMENT_NODE: |
| 5652 | case XML_TEXT_NODE: |
| 5653 | case XML_CDATA_SECTION_NODE: |
| 5654 | case XML_ENTITY_REF_NODE: |
| 5655 | case XML_ENTITY_NODE: |
| 5656 | case XML_PI_NODE: |
| 5657 | case XML_COMMENT_NODE: |
| 5658 | case XML_NOTATION_NODE: |
| 5659 | case XML_DTD_NODE: |
| 5660 | case XML_ELEMENT_DECL: |
| 5661 | case XML_ATTRIBUTE_DECL: |
| 5662 | case XML_ENTITY_DECL: |
| 5663 | case XML_XINCLUDE_START: |
| 5664 | case XML_XINCLUDE_END: |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5665 | if (cur->parent == NULL) |
| 5666 | return(NULL); |
| 5667 | if ((cur->parent->type == XML_ELEMENT_NODE) && |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 5668 | ((cur->parent->name[0] == ' ') || |
| 5669 | (xmlStrEqual(cur->parent->name, |
| 5670 | BAD_CAST "fake node libxslt")))) |
Daniel Veillard | 8e7e1c0 | 2003-01-10 17:06:09 +0000 | [diff] [blame] | 5671 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5672 | return(cur->parent); |
| 5673 | case XML_ATTRIBUTE_NODE: { |
| 5674 | xmlAttrPtr att = (xmlAttrPtr) ctxt->context->node; |
| 5675 | |
| 5676 | return(att->parent); |
| 5677 | } |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 5678 | case XML_NAMESPACE_DECL: { |
| 5679 | xmlNsPtr ns = (xmlNsPtr) ctxt->context->node; |
| 5680 | |
| 5681 | if ((ns->next != NULL) && |
| 5682 | (ns->next->type != XML_NAMESPACE_DECL)) |
| 5683 | return((xmlNodePtr) ns->next); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 5684 | /* Bad, how did that namespace end up here ? */ |
Aleksey Sanin | dffd5c8 | 2002-05-31 04:24:13 +0000 | [diff] [blame] | 5685 | return(NULL); |
| 5686 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5687 | case XML_DOCUMENT_NODE: |
| 5688 | case XML_DOCUMENT_TYPE_NODE: |
| 5689 | case XML_DOCUMENT_FRAG_NODE: |
| 5690 | case XML_HTML_DOCUMENT_NODE: |
Daniel Veillard | eae522a | 2001-04-23 13:41:34 +0000 | [diff] [blame] | 5691 | #ifdef LIBXML_DOCB_ENABLED |
| 5692 | case XML_DOCB_DOCUMENT_NODE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5693 | #endif |
| 5694 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5695 | } |
| 5696 | return(NULL); |
| 5697 | } |
| 5698 | |
| 5699 | /** |
| 5700 | * xmlXPathNextAncestorOrSelf: |
| 5701 | * @ctxt: the XPath Parser context |
| 5702 | * @cur: the current node in the traversal |
| 5703 | * |
| 5704 | * Traversal function for the "ancestor-or-self" direction |
| 5705 | * he ancestor-or-self axis contains the context node and ancestors of |
| 5706 | * the context node in reverse document order; thus the context node is |
| 5707 | * the first node on the axis, and the context node's parent the second; |
| 5708 | * parent here is defined the same as with the parent axis. |
| 5709 | * |
| 5710 | * Returns the next element following that axis |
| 5711 | */ |
| 5712 | xmlNodePtr |
| 5713 | xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5714 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5715 | if (cur == NULL) |
| 5716 | return(ctxt->context->node); |
| 5717 | return(xmlXPathNextAncestor(ctxt, cur)); |
| 5718 | } |
| 5719 | |
| 5720 | /** |
| 5721 | * xmlXPathNextFollowingSibling: |
| 5722 | * @ctxt: the XPath Parser context |
| 5723 | * @cur: the current node in the traversal |
| 5724 | * |
| 5725 | * Traversal function for the "following-sibling" direction |
| 5726 | * The following-sibling axis contains the following siblings of the context |
| 5727 | * node in document order. |
| 5728 | * |
| 5729 | * Returns the next element following that axis |
| 5730 | */ |
| 5731 | xmlNodePtr |
| 5732 | xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5733 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5734 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5735 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5736 | return(NULL); |
| 5737 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5738 | return(NULL); |
| 5739 | if (cur == NULL) |
| 5740 | return(ctxt->context->node->next); |
| 5741 | return(cur->next); |
| 5742 | } |
| 5743 | |
| 5744 | /** |
| 5745 | * xmlXPathNextPrecedingSibling: |
| 5746 | * @ctxt: the XPath Parser context |
| 5747 | * @cur: the current node in the traversal |
| 5748 | * |
| 5749 | * Traversal function for the "preceding-sibling" direction |
| 5750 | * The preceding-sibling axis contains the preceding siblings of the context |
| 5751 | * node in reverse document order; the first preceding sibling is first on the |
| 5752 | * axis; the sibling preceding that node is the second on the axis and so on. |
| 5753 | * |
| 5754 | * Returns the next element following that axis |
| 5755 | */ |
| 5756 | xmlNodePtr |
| 5757 | xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5758 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5759 | if ((ctxt->context->node->type == XML_ATTRIBUTE_NODE) || |
| 5760 | (ctxt->context->node->type == XML_NAMESPACE_DECL)) |
| 5761 | return(NULL); |
| 5762 | if (cur == (xmlNodePtr) ctxt->context->doc) |
| 5763 | return(NULL); |
| 5764 | if (cur == NULL) |
| 5765 | return(ctxt->context->node->prev); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5766 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) { |
| 5767 | cur = cur->prev; |
| 5768 | if (cur == NULL) |
| 5769 | return(ctxt->context->node->prev); |
| 5770 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5771 | return(cur->prev); |
| 5772 | } |
| 5773 | |
| 5774 | /** |
| 5775 | * xmlXPathNextFollowing: |
| 5776 | * @ctxt: the XPath Parser context |
| 5777 | * @cur: the current node in the traversal |
| 5778 | * |
| 5779 | * Traversal function for the "following" direction |
| 5780 | * The following axis contains all nodes in the same document as the context |
| 5781 | * node that are after the context node in document order, excluding any |
| 5782 | * descendants and excluding attribute nodes and namespace nodes; the nodes |
| 5783 | * are ordered in document order |
| 5784 | * |
| 5785 | * Returns the next element following that axis |
| 5786 | */ |
| 5787 | xmlNodePtr |
| 5788 | xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5789 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5790 | if (cur != NULL && cur->children != NULL) |
| 5791 | return cur->children ; |
| 5792 | if (cur == NULL) cur = ctxt->context->node; |
| 5793 | if (cur == NULL) return(NULL) ; /* ERROR */ |
| 5794 | if (cur->next != NULL) return(cur->next) ; |
| 5795 | do { |
| 5796 | cur = cur->parent; |
| 5797 | if (cur == NULL) return(NULL); |
| 5798 | if (cur == (xmlNodePtr) ctxt->context->doc) return(NULL); |
| 5799 | if (cur->next != NULL) return(cur->next); |
| 5800 | } while (cur != NULL); |
| 5801 | return(cur); |
| 5802 | } |
| 5803 | |
| 5804 | /* |
| 5805 | * xmlXPathIsAncestor: |
| 5806 | * @ancestor: the ancestor node |
| 5807 | * @node: the current node |
| 5808 | * |
| 5809 | * Check that @ancestor is a @node's ancestor |
| 5810 | * |
| 5811 | * returns 1 if @ancestor is a @node's ancestor, 0 otherwise. |
| 5812 | */ |
| 5813 | static int |
| 5814 | xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) { |
| 5815 | if ((ancestor == NULL) || (node == NULL)) return(0); |
| 5816 | /* nodes need to be in the same document */ |
| 5817 | if (ancestor->doc != node->doc) return(0); |
| 5818 | /* avoid searching if ancestor or node is the root node */ |
| 5819 | if (ancestor == (xmlNodePtr) node->doc) return(1); |
| 5820 | if (node == (xmlNodePtr) ancestor->doc) return(0); |
| 5821 | while (node->parent != NULL) { |
| 5822 | if (node->parent == ancestor) |
| 5823 | return(1); |
| 5824 | node = node->parent; |
| 5825 | } |
| 5826 | return(0); |
| 5827 | } |
| 5828 | |
| 5829 | /** |
| 5830 | * xmlXPathNextPreceding: |
| 5831 | * @ctxt: the XPath Parser context |
| 5832 | * @cur: the current node in the traversal |
| 5833 | * |
| 5834 | * Traversal function for the "preceding" direction |
| 5835 | * the preceding axis contains all nodes in the same document as the context |
| 5836 | * node that are before the context node in document order, excluding any |
| 5837 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 5838 | * ordered in reverse document order |
| 5839 | * |
| 5840 | * Returns the next element following that axis |
| 5841 | */ |
| 5842 | xmlNodePtr |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5843 | xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) |
| 5844 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5845 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5846 | if (cur == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5847 | cur = ctxt->context->node; |
| 5848 | if (cur == NULL) |
| 5849 | return (NULL); |
| 5850 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 5851 | cur = cur->prev; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5852 | do { |
| 5853 | if (cur->prev != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5854 | for (cur = cur->prev; cur->last != NULL; cur = cur->last) ; |
| 5855 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5856 | } |
| 5857 | |
| 5858 | cur = cur->parent; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5859 | if (cur == NULL) |
| 5860 | return (NULL); |
| 5861 | if (cur == ctxt->context->doc->children) |
| 5862 | return (NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5863 | } while (xmlXPathIsAncestor(cur, ctxt->context->node)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5864 | return (cur); |
| 5865 | } |
| 5866 | |
| 5867 | /** |
| 5868 | * xmlXPathNextPrecedingInternal: |
| 5869 | * @ctxt: the XPath Parser context |
| 5870 | * @cur: the current node in the traversal |
| 5871 | * |
| 5872 | * Traversal function for the "preceding" direction |
| 5873 | * the preceding axis contains all nodes in the same document as the context |
| 5874 | * node that are before the context node in document order, excluding any |
| 5875 | * ancestors and excluding attribute nodes and namespace nodes; the nodes are |
| 5876 | * ordered in reverse document order |
| 5877 | * This is a faster implementation but internal only since it requires a |
| 5878 | * state kept in the parser context: ctxt->ancestor. |
| 5879 | * |
| 5880 | * Returns the next element following that axis |
| 5881 | */ |
| 5882 | static xmlNodePtr |
| 5883 | xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt, |
| 5884 | xmlNodePtr cur) |
| 5885 | { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5886 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5887 | if (cur == NULL) { |
| 5888 | cur = ctxt->context->node; |
| 5889 | if (cur == NULL) |
| 5890 | return (NULL); |
William M. Brack | 40c22b4 | 2003-10-10 03:58:39 +0000 | [diff] [blame] | 5891 | if (cur->type == XML_NAMESPACE_DECL) |
| 5892 | cur = (xmlNodePtr)((xmlNsPtr)cur)->next; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 5893 | ctxt->ancestor = cur->parent; |
| 5894 | } |
| 5895 | if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) |
| 5896 | cur = cur->prev; |
| 5897 | while (cur->prev == NULL) { |
| 5898 | cur = cur->parent; |
| 5899 | if (cur == NULL) |
| 5900 | return (NULL); |
| 5901 | if (cur == ctxt->context->doc->children) |
| 5902 | return (NULL); |
| 5903 | if (cur != ctxt->ancestor) |
| 5904 | return (cur); |
| 5905 | ctxt->ancestor = cur->parent; |
| 5906 | } |
| 5907 | cur = cur->prev; |
| 5908 | while (cur->last != NULL) |
| 5909 | cur = cur->last; |
| 5910 | return (cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5911 | } |
| 5912 | |
| 5913 | /** |
| 5914 | * xmlXPathNextNamespace: |
| 5915 | * @ctxt: the XPath Parser context |
| 5916 | * @cur: the current attribute in the traversal |
| 5917 | * |
| 5918 | * Traversal function for the "namespace" direction |
| 5919 | * the namespace axis contains the namespace nodes of the context node; |
| 5920 | * the order of nodes on this axis is implementation-defined; the axis will |
| 5921 | * be empty unless the context node is an element |
| 5922 | * |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5923 | * We keep the XML namespace node at the end of the list. |
| 5924 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5925 | * Returns the next element following that axis |
| 5926 | */ |
| 5927 | xmlNodePtr |
| 5928 | xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5929 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5930 | if (ctxt->context->node->type != XML_ELEMENT_NODE) return(NULL); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5931 | if (ctxt->context->tmpNsList == NULL && cur != (xmlNodePtr) xmlXPathXMLNamespace) { |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5932 | if (ctxt->context->tmpNsList != NULL) |
| 5933 | xmlFree(ctxt->context->tmpNsList); |
| 5934 | ctxt->context->tmpNsList = |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5935 | xmlGetNsList(ctxt->context->doc, ctxt->context->node); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5936 | ctxt->context->tmpNsNr = 0; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5937 | if (ctxt->context->tmpNsList != NULL) { |
| 5938 | while (ctxt->context->tmpNsList[ctxt->context->tmpNsNr] != NULL) { |
| 5939 | ctxt->context->tmpNsNr++; |
| 5940 | } |
| 5941 | } |
Daniel Veillard | 20ee8c0 | 2001-10-05 09:18:14 +0000 | [diff] [blame] | 5942 | return((xmlNodePtr) xmlXPathXMLNamespace); |
Daniel Veillard | 7d7e379 | 2001-07-30 13:42:13 +0000 | [diff] [blame] | 5943 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5944 | if (ctxt->context->tmpNsNr > 0) { |
| 5945 | return (xmlNodePtr)ctxt->context->tmpNsList[--ctxt->context->tmpNsNr]; |
| 5946 | } else { |
| 5947 | if (ctxt->context->tmpNsList != NULL) |
| 5948 | xmlFree(ctxt->context->tmpNsList); |
| 5949 | ctxt->context->tmpNsList = NULL; |
| 5950 | return(NULL); |
| 5951 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5952 | } |
| 5953 | |
| 5954 | /** |
| 5955 | * xmlXPathNextAttribute: |
| 5956 | * @ctxt: the XPath Parser context |
| 5957 | * @cur: the current attribute in the traversal |
| 5958 | * |
| 5959 | * Traversal function for the "attribute" direction |
| 5960 | * TODO: support DTD inherited default attributes |
| 5961 | * |
| 5962 | * Returns the next element following that axis |
| 5963 | */ |
| 5964 | xmlNodePtr |
| 5965 | xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt, xmlNodePtr cur) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5966 | if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL); |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 5967 | if (ctxt->context->node == NULL) |
| 5968 | return(NULL); |
| 5969 | if (ctxt->context->node->type != XML_ELEMENT_NODE) |
| 5970 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5971 | if (cur == NULL) { |
| 5972 | if (ctxt->context->node == (xmlNodePtr) ctxt->context->doc) |
| 5973 | return(NULL); |
| 5974 | return((xmlNodePtr)ctxt->context->node->properties); |
| 5975 | } |
| 5976 | return((xmlNodePtr)cur->next); |
| 5977 | } |
| 5978 | |
| 5979 | /************************************************************************ |
| 5980 | * * |
| 5981 | * NodeTest Functions * |
| 5982 | * * |
| 5983 | ************************************************************************/ |
| 5984 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5985 | #define IS_FUNCTION 200 |
| 5986 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5987 | |
| 5988 | /************************************************************************ |
| 5989 | * * |
| 5990 | * Implicit tree core function library * |
| 5991 | * * |
| 5992 | ************************************************************************/ |
| 5993 | |
| 5994 | /** |
| 5995 | * xmlXPathRoot: |
| 5996 | * @ctxt: the XPath Parser context |
| 5997 | * |
| 5998 | * Initialize the context to the root of the document |
| 5999 | */ |
| 6000 | void |
| 6001 | xmlXPathRoot(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6002 | if ((ctxt == NULL) || (ctxt->context == NULL)) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6003 | ctxt->context->node = (xmlNodePtr) ctxt->context->doc; |
| 6004 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6005 | } |
| 6006 | |
| 6007 | /************************************************************************ |
| 6008 | * * |
| 6009 | * The explicit core function library * |
| 6010 | *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib * |
| 6011 | * * |
| 6012 | ************************************************************************/ |
| 6013 | |
| 6014 | |
| 6015 | /** |
| 6016 | * xmlXPathLastFunction: |
| 6017 | * @ctxt: the XPath Parser context |
| 6018 | * @nargs: the number of arguments |
| 6019 | * |
| 6020 | * Implement the last() XPath function |
| 6021 | * number last() |
| 6022 | * The last function returns the number of nodes in the context node list. |
| 6023 | */ |
| 6024 | void |
| 6025 | xmlXPathLastFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6026 | CHECK_ARITY(0); |
| 6027 | if (ctxt->context->contextSize >= 0) { |
| 6028 | valuePush(ctxt, xmlXPathNewFloat((double) ctxt->context->contextSize)); |
| 6029 | #ifdef DEBUG_EXPR |
| 6030 | xmlGenericError(xmlGenericErrorContext, |
| 6031 | "last() : %d\n", ctxt->context->contextSize); |
| 6032 | #endif |
| 6033 | } else { |
| 6034 | XP_ERROR(XPATH_INVALID_CTXT_SIZE); |
| 6035 | } |
| 6036 | } |
| 6037 | |
| 6038 | /** |
| 6039 | * xmlXPathPositionFunction: |
| 6040 | * @ctxt: the XPath Parser context |
| 6041 | * @nargs: the number of arguments |
| 6042 | * |
| 6043 | * Implement the position() XPath function |
| 6044 | * number position() |
| 6045 | * The position function returns the position of the context node in the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6046 | * context node list. The first position is 1, and so the last position |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6047 | * will be equal to last(). |
| 6048 | */ |
| 6049 | void |
| 6050 | xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6051 | CHECK_ARITY(0); |
| 6052 | if (ctxt->context->proximityPosition >= 0) { |
| 6053 | valuePush(ctxt, |
| 6054 | xmlXPathNewFloat((double) ctxt->context->proximityPosition)); |
| 6055 | #ifdef DEBUG_EXPR |
| 6056 | xmlGenericError(xmlGenericErrorContext, "position() : %d\n", |
| 6057 | ctxt->context->proximityPosition); |
| 6058 | #endif |
| 6059 | } else { |
| 6060 | XP_ERROR(XPATH_INVALID_CTXT_POSITION); |
| 6061 | } |
| 6062 | } |
| 6063 | |
| 6064 | /** |
| 6065 | * xmlXPathCountFunction: |
| 6066 | * @ctxt: the XPath Parser context |
| 6067 | * @nargs: the number of arguments |
| 6068 | * |
| 6069 | * Implement the count() XPath function |
| 6070 | * number count(node-set) |
| 6071 | */ |
| 6072 | void |
| 6073 | xmlXPathCountFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6074 | xmlXPathObjectPtr cur; |
| 6075 | |
| 6076 | CHECK_ARITY(1); |
| 6077 | if ((ctxt->value == NULL) || |
| 6078 | ((ctxt->value->type != XPATH_NODESET) && |
| 6079 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6080 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6081 | cur = valuePop(ctxt); |
| 6082 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6083 | if ((cur == NULL) || (cur->nodesetval == NULL)) |
| 6084 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6085 | else if ((cur->type == XPATH_NODESET) || (cur->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6086 | valuePush(ctxt, xmlXPathNewFloat((double) cur->nodesetval->nodeNr)); |
Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 6087 | } else { |
| 6088 | if ((cur->nodesetval->nodeNr != 1) || |
| 6089 | (cur->nodesetval->nodeTab == NULL)) { |
| 6090 | valuePush(ctxt, xmlXPathNewFloat((double) 0)); |
| 6091 | } else { |
| 6092 | xmlNodePtr tmp; |
| 6093 | int i = 0; |
| 6094 | |
| 6095 | tmp = cur->nodesetval->nodeTab[0]; |
| 6096 | if (tmp != NULL) { |
| 6097 | tmp = tmp->children; |
| 6098 | while (tmp != NULL) { |
| 6099 | tmp = tmp->next; |
| 6100 | i++; |
| 6101 | } |
| 6102 | } |
| 6103 | valuePush(ctxt, xmlXPathNewFloat((double) i)); |
| 6104 | } |
| 6105 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6106 | xmlXPathFreeObject(cur); |
| 6107 | } |
| 6108 | |
| 6109 | /** |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6110 | * xmlXPathGetElementsByIds: |
| 6111 | * @doc: the document |
| 6112 | * @ids: a whitespace separated list of IDs |
| 6113 | * |
| 6114 | * Selects elements by their unique ID. |
| 6115 | * |
| 6116 | * Returns a node-set of selected elements. |
| 6117 | */ |
| 6118 | static xmlNodeSetPtr |
| 6119 | xmlXPathGetElementsByIds (xmlDocPtr doc, const xmlChar *ids) { |
| 6120 | xmlNodeSetPtr ret; |
| 6121 | const xmlChar *cur = ids; |
| 6122 | xmlChar *ID; |
| 6123 | xmlAttrPtr attr; |
| 6124 | xmlNodePtr elem = NULL; |
| 6125 | |
Daniel Veillard | 7a985a1 | 2003-07-06 17:57:42 +0000 | [diff] [blame] | 6126 | if (ids == NULL) return(NULL); |
| 6127 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6128 | ret = xmlXPathNodeSetCreate(NULL); |
| 6129 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6130 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6131 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6132 | while ((!IS_BLANK_CH(*cur)) && (*cur != 0)) |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6133 | cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6134 | |
| 6135 | ID = xmlStrndup(ids, cur - ids); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6136 | if (ID != NULL) { |
Daniel Veillard | 68cb4b2 | 2004-04-18 20:55:39 +0000 | [diff] [blame] | 6137 | /* |
| 6138 | * We used to check the fact that the value passed |
| 6139 | * was an NCName, but this generated much troubles for |
| 6140 | * me and Aleksey Sanin, people blatantly violated that |
| 6141 | * constaint, like Visa3D spec. |
| 6142 | * if (xmlValidateNCName(ID, 1) == 0) |
| 6143 | */ |
| 6144 | attr = xmlGetID(doc, ID); |
| 6145 | if (attr != NULL) { |
| 6146 | if (attr->type == XML_ATTRIBUTE_NODE) |
| 6147 | elem = attr->parent; |
| 6148 | else if (attr->type == XML_ELEMENT_NODE) |
| 6149 | elem = (xmlNodePtr) attr; |
| 6150 | else |
| 6151 | elem = NULL; |
| 6152 | if (elem != NULL) |
| 6153 | xmlXPathNodeSetAdd(ret, elem); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6154 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6155 | xmlFree(ID); |
Daniel Veillard | e209b33 | 2003-03-26 21:40:13 +0000 | [diff] [blame] | 6156 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6157 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6158 | while (IS_BLANK_CH(*cur)) cur++; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6159 | ids = cur; |
| 6160 | } |
| 6161 | return(ret); |
| 6162 | } |
| 6163 | |
| 6164 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6165 | * xmlXPathIdFunction: |
| 6166 | * @ctxt: the XPath Parser context |
| 6167 | * @nargs: the number of arguments |
| 6168 | * |
| 6169 | * Implement the id() XPath function |
| 6170 | * node-set id(object) |
| 6171 | * The id function selects elements by their unique ID |
| 6172 | * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set, |
| 6173 | * then the result is the union of the result of applying id to the |
| 6174 | * string value of each of the nodes in the argument node-set. When the |
| 6175 | * argument to id is of any other type, the argument is converted to a |
| 6176 | * string as if by a call to the string function; the string is split |
| 6177 | * into a whitespace-separated list of tokens (whitespace is any sequence |
| 6178 | * of characters matching the production S); the result is a node-set |
| 6179 | * containing the elements in the same document as the context node that |
| 6180 | * have a unique ID equal to any of the tokens in the list. |
| 6181 | */ |
| 6182 | void |
| 6183 | xmlXPathIdFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6184 | xmlChar *tokens; |
| 6185 | xmlNodeSetPtr ret; |
| 6186 | xmlXPathObjectPtr obj; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6187 | |
| 6188 | CHECK_ARITY(1); |
| 6189 | obj = valuePop(ctxt); |
| 6190 | if (obj == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 6191 | if ((obj->type == XPATH_NODESET) || (obj->type == XPATH_XSLT_TREE)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6192 | xmlNodeSetPtr ns; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6193 | int i; |
| 6194 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6195 | ret = xmlXPathNodeSetCreate(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6196 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6197 | if (obj->nodesetval != NULL) { |
| 6198 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6199 | tokens = |
| 6200 | xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]); |
| 6201 | ns = xmlXPathGetElementsByIds(ctxt->context->doc, tokens); |
| 6202 | ret = xmlXPathNodeSetMerge(ret, ns); |
| 6203 | xmlXPathFreeNodeSet(ns); |
| 6204 | if (tokens != NULL) |
| 6205 | xmlFree(tokens); |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6206 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6207 | } |
| 6208 | |
| 6209 | xmlXPathFreeObject(obj); |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6210 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6211 | return; |
| 6212 | } |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6213 | obj = xmlXPathConvertString(obj); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6214 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6215 | ret = xmlXPathGetElementsByIds(ctxt->context->doc, obj->stringval); |
| 6216 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6217 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6218 | xmlXPathFreeObject(obj); |
| 6219 | return; |
| 6220 | } |
| 6221 | |
| 6222 | /** |
| 6223 | * xmlXPathLocalNameFunction: |
| 6224 | * @ctxt: the XPath Parser context |
| 6225 | * @nargs: the number of arguments |
| 6226 | * |
| 6227 | * Implement the local-name() XPath function |
| 6228 | * string local-name(node-set?) |
| 6229 | * The local-name function returns a string containing the local part |
| 6230 | * of the name of the node in the argument node-set that is first in |
| 6231 | * document order. If the node-set is empty or the first node has no |
| 6232 | * name, an empty string is returned. If the argument is omitted it |
| 6233 | * defaults to the context node. |
| 6234 | */ |
| 6235 | void |
| 6236 | xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6237 | xmlXPathObjectPtr cur; |
| 6238 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6239 | if (ctxt == NULL) return; |
| 6240 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6241 | if (nargs == 0) { |
| 6242 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6243 | nargs = 1; |
| 6244 | } |
| 6245 | |
| 6246 | CHECK_ARITY(1); |
| 6247 | if ((ctxt->value == NULL) || |
| 6248 | ((ctxt->value->type != XPATH_NODESET) && |
| 6249 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6250 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6251 | cur = valuePop(ctxt); |
| 6252 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6253 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6254 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6255 | } else { |
| 6256 | int i = 0; /* Should be first in document order !!!!! */ |
| 6257 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6258 | case XML_ELEMENT_NODE: |
| 6259 | case XML_ATTRIBUTE_NODE: |
| 6260 | case XML_PI_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6261 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
| 6262 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6263 | else |
| 6264 | valuePush(ctxt, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6265 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
| 6266 | break; |
| 6267 | case XML_NAMESPACE_DECL: |
| 6268 | valuePush(ctxt, xmlXPathNewString( |
| 6269 | ((xmlNsPtr)cur->nodesetval->nodeTab[i])->prefix)); |
| 6270 | break; |
| 6271 | default: |
| 6272 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6273 | } |
| 6274 | } |
| 6275 | xmlXPathFreeObject(cur); |
| 6276 | } |
| 6277 | |
| 6278 | /** |
| 6279 | * xmlXPathNamespaceURIFunction: |
| 6280 | * @ctxt: the XPath Parser context |
| 6281 | * @nargs: the number of arguments |
| 6282 | * |
| 6283 | * Implement the namespace-uri() XPath function |
| 6284 | * string namespace-uri(node-set?) |
| 6285 | * The namespace-uri function returns a string containing the |
| 6286 | * namespace URI of the expanded name of the node in the argument |
| 6287 | * node-set that is first in document order. If the node-set is empty, |
| 6288 | * the first node has no name, or the expanded name has no namespace |
| 6289 | * URI, an empty string is returned. If the argument is omitted it |
| 6290 | * defaults to the context node. |
| 6291 | */ |
| 6292 | void |
| 6293 | xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6294 | xmlXPathObjectPtr cur; |
| 6295 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6296 | if (ctxt == NULL) return; |
| 6297 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6298 | if (nargs == 0) { |
| 6299 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6300 | nargs = 1; |
| 6301 | } |
| 6302 | CHECK_ARITY(1); |
| 6303 | if ((ctxt->value == NULL) || |
| 6304 | ((ctxt->value->type != XPATH_NODESET) && |
| 6305 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6306 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6307 | cur = valuePop(ctxt); |
| 6308 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6309 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6310 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6311 | } else { |
| 6312 | int i = 0; /* Should be first in document order !!!!! */ |
| 6313 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6314 | case XML_ELEMENT_NODE: |
| 6315 | case XML_ATTRIBUTE_NODE: |
| 6316 | if (cur->nodesetval->nodeTab[i]->ns == NULL) |
| 6317 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6318 | else |
| 6319 | valuePush(ctxt, xmlXPathNewString( |
| 6320 | cur->nodesetval->nodeTab[i]->ns->href)); |
| 6321 | break; |
| 6322 | default: |
| 6323 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6324 | } |
| 6325 | } |
| 6326 | xmlXPathFreeObject(cur); |
| 6327 | } |
| 6328 | |
| 6329 | /** |
| 6330 | * xmlXPathNameFunction: |
| 6331 | * @ctxt: the XPath Parser context |
| 6332 | * @nargs: the number of arguments |
| 6333 | * |
| 6334 | * Implement the name() XPath function |
| 6335 | * string name(node-set?) |
| 6336 | * The name function returns a string containing a QName representing |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6337 | * the name of the node in the argument node-set that is first in document |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6338 | * order. The QName must represent the name with respect to the namespace |
| 6339 | * declarations in effect on the node whose name is being represented. |
| 6340 | * Typically, this will be the form in which the name occurred in the XML |
| 6341 | * source. This need not be the case if there are namespace declarations |
| 6342 | * in effect on the node that associate multiple prefixes with the same |
| 6343 | * namespace. However, an implementation may include information about |
| 6344 | * the original prefix in its representation of nodes; in this case, an |
| 6345 | * implementation can ensure that the returned string is always the same |
| 6346 | * as the QName used in the XML source. If the argument it omitted it |
| 6347 | * defaults to the context node. |
| 6348 | * Libxml keep the original prefix so the "real qualified name" used is |
| 6349 | * returned. |
| 6350 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6351 | static void |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6352 | xmlXPathNameFunction(xmlXPathParserContextPtr ctxt, int nargs) |
| 6353 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6354 | xmlXPathObjectPtr cur; |
| 6355 | |
| 6356 | if (nargs == 0) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6357 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 6358 | nargs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6359 | } |
| 6360 | |
| 6361 | CHECK_ARITY(1); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6362 | if ((ctxt->value == NULL) || |
| 6363 | ((ctxt->value->type != XPATH_NODESET) && |
| 6364 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 6365 | XP_ERROR(XPATH_INVALID_TYPE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6366 | cur = valuePop(ctxt); |
| 6367 | |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 6368 | if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr == 0)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6369 | valuePush(ctxt, xmlXPathNewCString("")); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6370 | } else { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6371 | int i = 0; /* Should be first in document order !!!!! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6372 | |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6373 | switch (cur->nodesetval->nodeTab[i]->type) { |
| 6374 | case XML_ELEMENT_NODE: |
| 6375 | case XML_ATTRIBUTE_NODE: |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6376 | if (cur->nodesetval->nodeTab[i]->name[0] == ' ') |
| 6377 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6378 | else if ((cur->nodesetval->nodeTab[i]->ns == NULL) || |
| 6379 | (cur->nodesetval->nodeTab[i]->ns->prefix == NULL)) { |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6380 | valuePush(ctxt, |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6381 | xmlXPathNewString(cur->nodesetval->nodeTab[i]->name)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6382 | |
Daniel Veillard | 652d8a9 | 2003-02-04 19:28:49 +0000 | [diff] [blame] | 6383 | } else { |
Daniel Veillard | c00cda8 | 2003-04-07 10:22:39 +0000 | [diff] [blame] | 6384 | xmlChar *fullname; |
| 6385 | |
| 6386 | fullname = xmlBuildQName(cur->nodesetval->nodeTab[i]->name, |
| 6387 | cur->nodesetval->nodeTab[i]->ns->prefix, |
| 6388 | NULL, 0); |
| 6389 | if (fullname == cur->nodesetval->nodeTab[i]->name) |
| 6390 | fullname = xmlStrdup(cur->nodesetval->nodeTab[i]->name); |
| 6391 | if (fullname == NULL) { |
| 6392 | XP_ERROR(XPATH_MEMORY_ERROR); |
| 6393 | } |
| 6394 | valuePush(ctxt, xmlXPathWrapString(fullname)); |
Daniel Veillard | 0438375 | 2001-07-08 14:27:15 +0000 | [diff] [blame] | 6395 | } |
| 6396 | break; |
| 6397 | default: |
| 6398 | valuePush(ctxt, |
| 6399 | xmlXPathNewNodeSet(cur->nodesetval->nodeTab[i])); |
| 6400 | xmlXPathLocalNameFunction(ctxt, 1); |
| 6401 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6402 | } |
| 6403 | xmlXPathFreeObject(cur); |
| 6404 | } |
| 6405 | |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6406 | |
| 6407 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6408 | * xmlXPathStringFunction: |
| 6409 | * @ctxt: the XPath Parser context |
| 6410 | * @nargs: the number of arguments |
| 6411 | * |
| 6412 | * Implement the string() XPath function |
| 6413 | * string string(object?) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 6414 | * The string function converts an object to a string as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6415 | * - A node-set is converted to a string by returning the value of |
| 6416 | * the node in the node-set that is first in document order. |
| 6417 | * If the node-set is empty, an empty string is returned. |
| 6418 | * - A number is converted to a string as follows |
| 6419 | * + NaN is converted to the string NaN |
| 6420 | * + positive zero is converted to the string 0 |
| 6421 | * + negative zero is converted to the string 0 |
| 6422 | * + positive infinity is converted to the string Infinity |
| 6423 | * + negative infinity is converted to the string -Infinity |
| 6424 | * + if the number is an integer, the number is represented in |
| 6425 | * decimal form as a Number with no decimal point and no leading |
| 6426 | * zeros, preceded by a minus sign (-) if the number is negative |
| 6427 | * + otherwise, the number is represented in decimal form as a |
| 6428 | * Number including a decimal point with at least one digit |
| 6429 | * before the decimal point and at least one digit after the |
| 6430 | * decimal point, preceded by a minus sign (-) if the number |
| 6431 | * is negative; there must be no leading zeros before the decimal |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6432 | * point apart possibly from the one required digit immediately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6433 | * before the decimal point; beyond the one required digit |
| 6434 | * after the decimal point there must be as many, but only as |
| 6435 | * many, more digits as are needed to uniquely distinguish the |
| 6436 | * number from all other IEEE 754 numeric values. |
| 6437 | * - The boolean false value is converted to the string false. |
| 6438 | * The boolean true value is converted to the string true. |
| 6439 | * |
| 6440 | * If the argument is omitted, it defaults to a node-set with the |
| 6441 | * context node as its only member. |
| 6442 | */ |
| 6443 | void |
| 6444 | xmlXPathStringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6445 | xmlXPathObjectPtr cur; |
| 6446 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6447 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6448 | if (nargs == 0) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6449 | valuePush(ctxt, |
| 6450 | xmlXPathWrapString( |
| 6451 | xmlXPathCastNodeToString(ctxt->context->node))); |
| 6452 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6453 | } |
| 6454 | |
| 6455 | CHECK_ARITY(1); |
| 6456 | cur = valuePop(ctxt); |
| 6457 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6458 | cur = xmlXPathConvertString(cur); |
| 6459 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6460 | } |
| 6461 | |
| 6462 | /** |
| 6463 | * xmlXPathStringLengthFunction: |
| 6464 | * @ctxt: the XPath Parser context |
| 6465 | * @nargs: the number of arguments |
| 6466 | * |
| 6467 | * Implement the string-length() XPath function |
| 6468 | * number string-length(string?) |
| 6469 | * The string-length returns the number of characters in the string |
| 6470 | * (see [3.6 Strings]). If the argument is omitted, it defaults to |
| 6471 | * the context node converted to a string, in other words the value |
| 6472 | * of the context node. |
| 6473 | */ |
| 6474 | void |
| 6475 | xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6476 | xmlXPathObjectPtr cur; |
| 6477 | |
| 6478 | if (nargs == 0) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6479 | if ((ctxt == NULL) || (ctxt->context == NULL)) |
| 6480 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6481 | if (ctxt->context->node == NULL) { |
| 6482 | valuePush(ctxt, xmlXPathNewFloat(0)); |
| 6483 | } else { |
| 6484 | xmlChar *content; |
| 6485 | |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6486 | content = xmlXPathCastNodeToString(ctxt->context->node); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6487 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(content))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6488 | xmlFree(content); |
| 6489 | } |
| 6490 | return; |
| 6491 | } |
| 6492 | CHECK_ARITY(1); |
| 6493 | CAST_TO_STRING; |
| 6494 | CHECK_TYPE(XPATH_STRING); |
| 6495 | cur = valuePop(ctxt); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6496 | valuePush(ctxt, xmlXPathNewFloat(xmlUTF8Strlen(cur->stringval))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6497 | xmlXPathFreeObject(cur); |
| 6498 | } |
| 6499 | |
| 6500 | /** |
| 6501 | * xmlXPathConcatFunction: |
| 6502 | * @ctxt: the XPath Parser context |
| 6503 | * @nargs: the number of arguments |
| 6504 | * |
| 6505 | * Implement the concat() XPath function |
| 6506 | * string concat(string, string, string*) |
| 6507 | * The concat function returns the concatenation of its arguments. |
| 6508 | */ |
| 6509 | void |
| 6510 | xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6511 | xmlXPathObjectPtr cur, newobj; |
| 6512 | xmlChar *tmp; |
| 6513 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6514 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6515 | if (nargs < 2) { |
| 6516 | CHECK_ARITY(2); |
| 6517 | } |
| 6518 | |
| 6519 | CAST_TO_STRING; |
| 6520 | cur = valuePop(ctxt); |
| 6521 | if ((cur == NULL) || (cur->type != XPATH_STRING)) { |
| 6522 | xmlXPathFreeObject(cur); |
| 6523 | return; |
| 6524 | } |
| 6525 | nargs--; |
| 6526 | |
| 6527 | while (nargs > 0) { |
| 6528 | CAST_TO_STRING; |
| 6529 | newobj = valuePop(ctxt); |
| 6530 | if ((newobj == NULL) || (newobj->type != XPATH_STRING)) { |
| 6531 | xmlXPathFreeObject(newobj); |
| 6532 | xmlXPathFreeObject(cur); |
| 6533 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6534 | } |
| 6535 | tmp = xmlStrcat(newobj->stringval, cur->stringval); |
| 6536 | newobj->stringval = cur->stringval; |
| 6537 | cur->stringval = tmp; |
| 6538 | |
| 6539 | xmlXPathFreeObject(newobj); |
| 6540 | nargs--; |
| 6541 | } |
| 6542 | valuePush(ctxt, cur); |
| 6543 | } |
| 6544 | |
| 6545 | /** |
| 6546 | * xmlXPathContainsFunction: |
| 6547 | * @ctxt: the XPath Parser context |
| 6548 | * @nargs: the number of arguments |
| 6549 | * |
| 6550 | * Implement the contains() XPath function |
| 6551 | * boolean contains(string, string) |
| 6552 | * The contains function returns true if the first argument string |
| 6553 | * contains the second argument string, and otherwise returns false. |
| 6554 | */ |
| 6555 | void |
| 6556 | xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6557 | xmlXPathObjectPtr hay, needle; |
| 6558 | |
| 6559 | CHECK_ARITY(2); |
| 6560 | CAST_TO_STRING; |
| 6561 | CHECK_TYPE(XPATH_STRING); |
| 6562 | needle = valuePop(ctxt); |
| 6563 | CAST_TO_STRING; |
| 6564 | hay = valuePop(ctxt); |
| 6565 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6566 | xmlXPathFreeObject(hay); |
| 6567 | xmlXPathFreeObject(needle); |
| 6568 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6569 | } |
| 6570 | if (xmlStrstr(hay->stringval, needle->stringval)) |
| 6571 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6572 | else |
| 6573 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6574 | xmlXPathFreeObject(hay); |
| 6575 | xmlXPathFreeObject(needle); |
| 6576 | } |
| 6577 | |
| 6578 | /** |
| 6579 | * xmlXPathStartsWithFunction: |
| 6580 | * @ctxt: the XPath Parser context |
| 6581 | * @nargs: the number of arguments |
| 6582 | * |
| 6583 | * Implement the starts-with() XPath function |
| 6584 | * boolean starts-with(string, string) |
| 6585 | * The starts-with function returns true if the first argument string |
| 6586 | * starts with the second argument string, and otherwise returns false. |
| 6587 | */ |
| 6588 | void |
| 6589 | xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6590 | xmlXPathObjectPtr hay, needle; |
| 6591 | int n; |
| 6592 | |
| 6593 | CHECK_ARITY(2); |
| 6594 | CAST_TO_STRING; |
| 6595 | CHECK_TYPE(XPATH_STRING); |
| 6596 | needle = valuePop(ctxt); |
| 6597 | CAST_TO_STRING; |
| 6598 | hay = valuePop(ctxt); |
| 6599 | if ((hay == NULL) || (hay->type != XPATH_STRING)) { |
| 6600 | xmlXPathFreeObject(hay); |
| 6601 | xmlXPathFreeObject(needle); |
| 6602 | XP_ERROR(XPATH_INVALID_TYPE); |
| 6603 | } |
| 6604 | n = xmlStrlen(needle->stringval); |
| 6605 | if (xmlStrncmp(hay->stringval, needle->stringval, n)) |
| 6606 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 6607 | else |
| 6608 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 6609 | xmlXPathFreeObject(hay); |
| 6610 | xmlXPathFreeObject(needle); |
| 6611 | } |
| 6612 | |
| 6613 | /** |
| 6614 | * xmlXPathSubstringFunction: |
| 6615 | * @ctxt: the XPath Parser context |
| 6616 | * @nargs: the number of arguments |
| 6617 | * |
| 6618 | * Implement the substring() XPath function |
| 6619 | * string substring(string, number, number?) |
| 6620 | * The substring function returns the substring of the first argument |
| 6621 | * starting at the position specified in the second argument with |
| 6622 | * length specified in the third argument. For example, |
| 6623 | * substring("12345",2,3) returns "234". If the third argument is not |
| 6624 | * specified, it returns the substring starting at the position specified |
| 6625 | * in the second argument and continuing to the end of the string. For |
| 6626 | * example, substring("12345",2) returns "2345". More precisely, each |
| 6627 | * character in the string (see [3.6 Strings]) is considered to have a |
| 6628 | * numeric position: the position of the first character is 1, the position |
| 6629 | * of the second character is 2 and so on. The returned substring contains |
| 6630 | * those characters for which the position of the character is greater than |
| 6631 | * or equal to the second argument and, if the third argument is specified, |
| 6632 | * less than the sum of the second and third arguments; the comparisons |
| 6633 | * and addition used for the above follow the standard IEEE 754 rules. Thus: |
| 6634 | * - substring("12345", 1.5, 2.6) returns "234" |
| 6635 | * - substring("12345", 0, 3) returns "12" |
| 6636 | * - substring("12345", 0 div 0, 3) returns "" |
| 6637 | * - substring("12345", 1, 0 div 0) returns "" |
| 6638 | * - substring("12345", -42, 1 div 0) returns "12345" |
| 6639 | * - substring("12345", -1 div 0, 1 div 0) returns "" |
| 6640 | */ |
| 6641 | void |
| 6642 | xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6643 | xmlXPathObjectPtr str, start, len; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6644 | double le=0, in; |
| 6645 | int i, l, m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6646 | xmlChar *ret; |
| 6647 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6648 | if (nargs < 2) { |
| 6649 | CHECK_ARITY(2); |
| 6650 | } |
| 6651 | if (nargs > 3) { |
| 6652 | CHECK_ARITY(3); |
| 6653 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6654 | /* |
| 6655 | * take care of possible last (position) argument |
| 6656 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6657 | if (nargs == 3) { |
| 6658 | CAST_TO_NUMBER; |
| 6659 | CHECK_TYPE(XPATH_NUMBER); |
| 6660 | len = valuePop(ctxt); |
| 6661 | le = len->floatval; |
| 6662 | xmlXPathFreeObject(len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6663 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6664 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6665 | CAST_TO_NUMBER; |
| 6666 | CHECK_TYPE(XPATH_NUMBER); |
| 6667 | start = valuePop(ctxt); |
| 6668 | in = start->floatval; |
| 6669 | xmlXPathFreeObject(start); |
| 6670 | CAST_TO_STRING; |
| 6671 | CHECK_TYPE(XPATH_STRING); |
| 6672 | str = valuePop(ctxt); |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6673 | m = xmlUTF8Strlen((const unsigned char *)str->stringval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6674 | |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6675 | /* |
| 6676 | * If last pos not present, calculate last position |
| 6677 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6678 | if (nargs != 3) { |
| 6679 | le = (double)m; |
| 6680 | if (in < 1.0) |
| 6681 | in = 1.0; |
| 6682 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6683 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6684 | /* Need to check for the special cases where either |
| 6685 | * the index is NaN, the length is NaN, or both |
| 6686 | * arguments are infinity (relying on Inf + -Inf = NaN) |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6687 | */ |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6688 | if (!xmlXPathIsNaN(in + le) && !xmlXPathIsInf(in)) { |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6689 | /* |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6690 | * To meet the requirements of the spec, the arguments |
| 6691 | * must be converted to integer format before |
| 6692 | * initial index calculations are done |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6693 | * |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6694 | * First we go to integer form, rounding up |
| 6695 | * and checking for special cases |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6696 | */ |
| 6697 | i = (int) in; |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6698 | if (((double)i)+0.5 <= in) i++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6699 | |
Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 6700 | if (xmlXPathIsInf(le) == 1) { |
| 6701 | l = m; |
| 6702 | if (i < 1) |
| 6703 | i = 1; |
| 6704 | } |
| 6705 | else if (xmlXPathIsInf(le) == -1 || le < 0.0) |
| 6706 | l = 0; |
| 6707 | else { |
| 6708 | l = (int) le; |
| 6709 | if (((double)l)+0.5 <= le) l++; |
| 6710 | } |
| 6711 | |
| 6712 | /* Now we normalize inidices */ |
| 6713 | i -= 1; |
| 6714 | l += i; |
| 6715 | if (i < 0) |
| 6716 | i = 0; |
| 6717 | if (l > m) |
| 6718 | l = m; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6719 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6720 | /* number of chars to copy */ |
| 6721 | l -= i; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6722 | |
Daniel Veillard | 0eafdef | 2002-04-10 16:14:34 +0000 | [diff] [blame] | 6723 | ret = xmlUTF8Strsub(str->stringval, i, l); |
| 6724 | } |
| 6725 | else { |
| 6726 | ret = NULL; |
| 6727 | } |
| 6728 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6729 | if (ret == NULL) |
| 6730 | valuePush(ctxt, xmlXPathNewCString("")); |
| 6731 | else { |
| 6732 | valuePush(ctxt, xmlXPathNewString(ret)); |
| 6733 | xmlFree(ret); |
| 6734 | } |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6735 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6736 | xmlXPathFreeObject(str); |
| 6737 | } |
| 6738 | |
| 6739 | /** |
| 6740 | * xmlXPathSubstringBeforeFunction: |
| 6741 | * @ctxt: the XPath Parser context |
| 6742 | * @nargs: the number of arguments |
| 6743 | * |
| 6744 | * Implement the substring-before() XPath function |
| 6745 | * string substring-before(string, string) |
| 6746 | * The substring-before function returns the substring of the first |
| 6747 | * argument string that precedes the first occurrence of the second |
| 6748 | * argument string in the first argument string, or the empty string |
| 6749 | * if the first argument string does not contain the second argument |
| 6750 | * string. For example, substring-before("1999/04/01","/") returns 1999. |
| 6751 | */ |
| 6752 | void |
| 6753 | xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6754 | xmlXPathObjectPtr str; |
| 6755 | xmlXPathObjectPtr find; |
| 6756 | xmlBufferPtr target; |
| 6757 | const xmlChar *point; |
| 6758 | int offset; |
| 6759 | |
| 6760 | CHECK_ARITY(2); |
| 6761 | CAST_TO_STRING; |
| 6762 | find = valuePop(ctxt); |
| 6763 | CAST_TO_STRING; |
| 6764 | str = valuePop(ctxt); |
| 6765 | |
| 6766 | target = xmlBufferCreate(); |
| 6767 | if (target) { |
| 6768 | point = xmlStrstr(str->stringval, find->stringval); |
| 6769 | if (point) { |
| 6770 | offset = (int)(point - str->stringval); |
| 6771 | xmlBufferAdd(target, str->stringval, offset); |
| 6772 | } |
| 6773 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6774 | xmlBufferFree(target); |
| 6775 | } |
| 6776 | |
| 6777 | xmlXPathFreeObject(str); |
| 6778 | xmlXPathFreeObject(find); |
| 6779 | } |
| 6780 | |
| 6781 | /** |
| 6782 | * xmlXPathSubstringAfterFunction: |
| 6783 | * @ctxt: the XPath Parser context |
| 6784 | * @nargs: the number of arguments |
| 6785 | * |
| 6786 | * Implement the substring-after() XPath function |
| 6787 | * string substring-after(string, string) |
| 6788 | * The substring-after function returns the substring of the first |
| 6789 | * argument string that follows the first occurrence of the second |
| 6790 | * argument string in the first argument string, or the empty stringi |
| 6791 | * if the first argument string does not contain the second argument |
| 6792 | * string. For example, substring-after("1999/04/01","/") returns 04/01, |
| 6793 | * and substring-after("1999/04/01","19") returns 99/04/01. |
| 6794 | */ |
| 6795 | void |
| 6796 | xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6797 | xmlXPathObjectPtr str; |
| 6798 | xmlXPathObjectPtr find; |
| 6799 | xmlBufferPtr target; |
| 6800 | const xmlChar *point; |
| 6801 | int offset; |
| 6802 | |
| 6803 | CHECK_ARITY(2); |
| 6804 | CAST_TO_STRING; |
| 6805 | find = valuePop(ctxt); |
| 6806 | CAST_TO_STRING; |
| 6807 | str = valuePop(ctxt); |
| 6808 | |
| 6809 | target = xmlBufferCreate(); |
| 6810 | if (target) { |
| 6811 | point = xmlStrstr(str->stringval, find->stringval); |
| 6812 | if (point) { |
| 6813 | offset = (int)(point - str->stringval) + xmlStrlen(find->stringval); |
| 6814 | xmlBufferAdd(target, &str->stringval[offset], |
| 6815 | xmlStrlen(str->stringval) - offset); |
| 6816 | } |
| 6817 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6818 | xmlBufferFree(target); |
| 6819 | } |
| 6820 | |
| 6821 | xmlXPathFreeObject(str); |
| 6822 | xmlXPathFreeObject(find); |
| 6823 | } |
| 6824 | |
| 6825 | /** |
| 6826 | * xmlXPathNormalizeFunction: |
| 6827 | * @ctxt: the XPath Parser context |
| 6828 | * @nargs: the number of arguments |
| 6829 | * |
| 6830 | * Implement the normalize-space() XPath function |
| 6831 | * string normalize-space(string?) |
| 6832 | * The normalize-space function returns the argument string with white |
| 6833 | * space normalized by stripping leading and trailing whitespace |
| 6834 | * and replacing sequences of whitespace characters by a single |
| 6835 | * space. Whitespace characters are the same allowed by the S production |
| 6836 | * in XML. If the argument is omitted, it defaults to the context |
| 6837 | * node converted to a string, in other words the value of the context node. |
| 6838 | */ |
| 6839 | void |
| 6840 | xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6841 | xmlXPathObjectPtr obj = NULL; |
| 6842 | xmlChar *source = NULL; |
| 6843 | xmlBufferPtr target; |
| 6844 | xmlChar blank; |
| 6845 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 6846 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6847 | if (nargs == 0) { |
| 6848 | /* Use current context node */ |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 6849 | valuePush(ctxt, |
| 6850 | xmlXPathWrapString( |
| 6851 | xmlXPathCastNodeToString(ctxt->context->node))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6852 | nargs = 1; |
| 6853 | } |
| 6854 | |
| 6855 | CHECK_ARITY(1); |
| 6856 | CAST_TO_STRING; |
| 6857 | CHECK_TYPE(XPATH_STRING); |
| 6858 | obj = valuePop(ctxt); |
| 6859 | source = obj->stringval; |
| 6860 | |
| 6861 | target = xmlBufferCreate(); |
| 6862 | if (target && source) { |
| 6863 | |
| 6864 | /* Skip leading whitespaces */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6865 | while (IS_BLANK_CH(*source)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6866 | source++; |
| 6867 | |
| 6868 | /* Collapse intermediate whitespaces, and skip trailing whitespaces */ |
| 6869 | blank = 0; |
| 6870 | while (*source) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6871 | if (IS_BLANK_CH(*source)) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6872 | blank = 0x20; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6873 | } else { |
| 6874 | if (blank) { |
| 6875 | xmlBufferAdd(target, &blank, 1); |
| 6876 | blank = 0; |
| 6877 | } |
| 6878 | xmlBufferAdd(target, source, 1); |
| 6879 | } |
| 6880 | source++; |
| 6881 | } |
| 6882 | |
| 6883 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6884 | xmlBufferFree(target); |
| 6885 | } |
| 6886 | xmlXPathFreeObject(obj); |
| 6887 | } |
| 6888 | |
| 6889 | /** |
| 6890 | * xmlXPathTranslateFunction: |
| 6891 | * @ctxt: the XPath Parser context |
| 6892 | * @nargs: the number of arguments |
| 6893 | * |
| 6894 | * Implement the translate() XPath function |
| 6895 | * string translate(string, string, string) |
| 6896 | * The translate function returns the first argument string with |
| 6897 | * occurrences of characters in the second argument string replaced |
| 6898 | * by the character at the corresponding position in the third argument |
| 6899 | * string. For example, translate("bar","abc","ABC") returns the string |
| 6900 | * BAr. If there is a character in the second argument string with no |
| 6901 | * character at a corresponding position in the third argument string |
| 6902 | * (because the second argument string is longer than the third argument |
| 6903 | * string), then occurrences of that character in the first argument |
| 6904 | * string are removed. For example, translate("--aaa--","abc-","ABC") |
| 6905 | * returns "AAA". If a character occurs more than once in second |
| 6906 | * argument string, then the first occurrence determines the replacement |
| 6907 | * character. If the third argument string is longer than the second |
| 6908 | * argument string, then excess characters are ignored. |
| 6909 | */ |
| 6910 | void |
| 6911 | xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6912 | xmlXPathObjectPtr str; |
| 6913 | xmlXPathObjectPtr from; |
| 6914 | xmlXPathObjectPtr to; |
| 6915 | xmlBufferPtr target; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6916 | int offset, max; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6917 | xmlChar ch; |
William M. Brack | b031cef | 2004-11-05 16:34:22 +0000 | [diff] [blame] | 6918 | const xmlChar *point; |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6919 | xmlChar *cptr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6920 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6921 | CHECK_ARITY(3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6922 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6923 | CAST_TO_STRING; |
| 6924 | to = valuePop(ctxt); |
| 6925 | CAST_TO_STRING; |
| 6926 | from = valuePop(ctxt); |
| 6927 | CAST_TO_STRING; |
| 6928 | str = valuePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6929 | |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6930 | target = xmlBufferCreate(); |
| 6931 | if (target) { |
Daniel Veillard | 97ac131 | 2001-05-30 19:14:17 +0000 | [diff] [blame] | 6932 | max = xmlUTF8Strlen(to->stringval); |
| 6933 | for (cptr = str->stringval; (ch=*cptr); ) { |
| 6934 | offset = xmlUTF8Strloc(from->stringval, cptr); |
| 6935 | if (offset >= 0) { |
| 6936 | if (offset < max) { |
| 6937 | point = xmlUTF8Strpos(to->stringval, offset); |
| 6938 | if (point) |
| 6939 | xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1)); |
| 6940 | } |
| 6941 | } else |
| 6942 | xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1)); |
| 6943 | |
| 6944 | /* Step to next character in input */ |
| 6945 | cptr++; |
| 6946 | if ( ch & 0x80 ) { |
| 6947 | /* if not simple ascii, verify proper format */ |
| 6948 | if ( (ch & 0xc0) != 0xc0 ) { |
| 6949 | xmlGenericError(xmlGenericErrorContext, |
| 6950 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 6951 | break; |
| 6952 | } |
| 6953 | /* then skip over remaining bytes for this char */ |
| 6954 | while ( (ch <<= 1) & 0x80 ) |
| 6955 | if ( (*cptr++ & 0xc0) != 0x80 ) { |
| 6956 | xmlGenericError(xmlGenericErrorContext, |
| 6957 | "xmlXPathTranslateFunction: Invalid UTF8 string\n"); |
| 6958 | break; |
| 6959 | } |
| 6960 | if (ch & 0x80) /* must have had error encountered */ |
| 6961 | break; |
| 6962 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6963 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6964 | } |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 6965 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 6966 | xmlBufferFree(target); |
| 6967 | xmlXPathFreeObject(str); |
| 6968 | xmlXPathFreeObject(from); |
| 6969 | xmlXPathFreeObject(to); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6970 | } |
| 6971 | |
| 6972 | /** |
| 6973 | * xmlXPathBooleanFunction: |
| 6974 | * @ctxt: the XPath Parser context |
| 6975 | * @nargs: the number of arguments |
| 6976 | * |
| 6977 | * Implement the boolean() XPath function |
| 6978 | * boolean boolean(object) |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 6979 | * The boolean function converts its argument to a boolean as follows: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6980 | * - a number is true if and only if it is neither positive or |
| 6981 | * negative zero nor NaN |
| 6982 | * - a node-set is true if and only if it is non-empty |
| 6983 | * - a string is true if and only if its length is non-zero |
| 6984 | */ |
| 6985 | void |
| 6986 | xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 6987 | xmlXPathObjectPtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6988 | |
| 6989 | CHECK_ARITY(1); |
| 6990 | cur = valuePop(ctxt); |
| 6991 | if (cur == NULL) XP_ERROR(XPATH_INVALID_OPERAND); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 6992 | cur = xmlXPathConvertBoolean(cur); |
| 6993 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6994 | } |
| 6995 | |
| 6996 | /** |
| 6997 | * xmlXPathNotFunction: |
| 6998 | * @ctxt: the XPath Parser context |
| 6999 | * @nargs: the number of arguments |
| 7000 | * |
| 7001 | * Implement the not() XPath function |
| 7002 | * boolean not(boolean) |
| 7003 | * The not function returns true if its argument is false, |
| 7004 | * and false otherwise. |
| 7005 | */ |
| 7006 | void |
| 7007 | xmlXPathNotFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7008 | CHECK_ARITY(1); |
| 7009 | CAST_TO_BOOLEAN; |
| 7010 | CHECK_TYPE(XPATH_BOOLEAN); |
| 7011 | ctxt->value->boolval = ! ctxt->value->boolval; |
| 7012 | } |
| 7013 | |
| 7014 | /** |
| 7015 | * xmlXPathTrueFunction: |
| 7016 | * @ctxt: the XPath Parser context |
| 7017 | * @nargs: the number of arguments |
| 7018 | * |
| 7019 | * Implement the true() XPath function |
| 7020 | * boolean true() |
| 7021 | */ |
| 7022 | void |
| 7023 | xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7024 | CHECK_ARITY(0); |
| 7025 | valuePush(ctxt, xmlXPathNewBoolean(1)); |
| 7026 | } |
| 7027 | |
| 7028 | /** |
| 7029 | * xmlXPathFalseFunction: |
| 7030 | * @ctxt: the XPath Parser context |
| 7031 | * @nargs: the number of arguments |
| 7032 | * |
| 7033 | * Implement the false() XPath function |
| 7034 | * boolean false() |
| 7035 | */ |
| 7036 | void |
| 7037 | xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7038 | CHECK_ARITY(0); |
| 7039 | valuePush(ctxt, xmlXPathNewBoolean(0)); |
| 7040 | } |
| 7041 | |
| 7042 | /** |
| 7043 | * xmlXPathLangFunction: |
| 7044 | * @ctxt: the XPath Parser context |
| 7045 | * @nargs: the number of arguments |
| 7046 | * |
| 7047 | * Implement the lang() XPath function |
| 7048 | * boolean lang(string) |
| 7049 | * The lang function returns true or false depending on whether the |
| 7050 | * language of the context node as specified by xml:lang attributes |
| 7051 | * is the same as or is a sublanguage of the language specified by |
| 7052 | * the argument string. The language of the context node is determined |
| 7053 | * by the value of the xml:lang attribute on the context node, or, if |
| 7054 | * the context node has no xml:lang attribute, by the value of the |
| 7055 | * xml:lang attribute on the nearest ancestor of the context node that |
| 7056 | * has an xml:lang attribute. If there is no such attribute, then lang |
| 7057 | * returns false. If there is such an attribute, then lang returns |
| 7058 | * true if the attribute value is equal to the argument ignoring case, |
| 7059 | * or if there is some suffix starting with - such that the attribute |
| 7060 | * value is equal to the argument ignoring that suffix of the attribute |
| 7061 | * value and ignoring case. |
| 7062 | */ |
| 7063 | void |
| 7064 | xmlXPathLangFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7065 | xmlXPathObjectPtr val; |
| 7066 | const xmlChar *theLang; |
| 7067 | const xmlChar *lang; |
| 7068 | int ret = 0; |
| 7069 | int i; |
| 7070 | |
| 7071 | CHECK_ARITY(1); |
| 7072 | CAST_TO_STRING; |
| 7073 | CHECK_TYPE(XPATH_STRING); |
| 7074 | val = valuePop(ctxt); |
| 7075 | lang = val->stringval; |
| 7076 | theLang = xmlNodeGetLang(ctxt->context->node); |
| 7077 | if ((theLang != NULL) && (lang != NULL)) { |
| 7078 | for (i = 0;lang[i] != 0;i++) |
| 7079 | if (toupper(lang[i]) != toupper(theLang[i])) |
| 7080 | goto not_equal; |
| 7081 | ret = 1; |
| 7082 | } |
| 7083 | not_equal: |
William M. Brack | a59ddb5 | 2004-02-25 08:12:32 +0000 | [diff] [blame] | 7084 | xmlFree((void *)theLang); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7085 | xmlXPathFreeObject(val); |
| 7086 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 7087 | } |
| 7088 | |
| 7089 | /** |
| 7090 | * xmlXPathNumberFunction: |
| 7091 | * @ctxt: the XPath Parser context |
| 7092 | * @nargs: the number of arguments |
| 7093 | * |
| 7094 | * Implement the number() XPath function |
| 7095 | * number number(object?) |
| 7096 | */ |
| 7097 | void |
| 7098 | xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7099 | xmlXPathObjectPtr cur; |
| 7100 | double res; |
| 7101 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7102 | if (ctxt == NULL) return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7103 | if (nargs == 0) { |
| 7104 | if (ctxt->context->node == NULL) { |
| 7105 | valuePush(ctxt, xmlXPathNewFloat(0.0)); |
| 7106 | } else { |
| 7107 | xmlChar* content = xmlNodeGetContent(ctxt->context->node); |
| 7108 | |
| 7109 | res = xmlXPathStringEvalNumber(content); |
| 7110 | valuePush(ctxt, xmlXPathNewFloat(res)); |
| 7111 | xmlFree(content); |
| 7112 | } |
| 7113 | return; |
| 7114 | } |
| 7115 | |
| 7116 | CHECK_ARITY(1); |
| 7117 | cur = valuePop(ctxt); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7118 | cur = xmlXPathConvertNumber(cur); |
| 7119 | valuePush(ctxt, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7120 | } |
| 7121 | |
| 7122 | /** |
| 7123 | * xmlXPathSumFunction: |
| 7124 | * @ctxt: the XPath Parser context |
| 7125 | * @nargs: the number of arguments |
| 7126 | * |
| 7127 | * Implement the sum() XPath function |
| 7128 | * number sum(node-set) |
| 7129 | * The sum function returns the sum of the values of the nodes in |
| 7130 | * the argument node-set. |
| 7131 | */ |
| 7132 | void |
| 7133 | xmlXPathSumFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7134 | xmlXPathObjectPtr cur; |
| 7135 | int i; |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7136 | double res = 0.0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7137 | |
| 7138 | CHECK_ARITY(1); |
| 7139 | if ((ctxt->value == NULL) || |
| 7140 | ((ctxt->value->type != XPATH_NODESET) && |
| 7141 | (ctxt->value->type != XPATH_XSLT_TREE))) |
| 7142 | XP_ERROR(XPATH_INVALID_TYPE); |
| 7143 | cur = valuePop(ctxt); |
| 7144 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7145 | if ((cur->nodesetval != NULL) && (cur->nodesetval->nodeNr != 0)) { |
Daniel Veillard | ba0b8c9 | 2001-05-15 09:43:47 +0000 | [diff] [blame] | 7146 | for (i = 0; i < cur->nodesetval->nodeNr; i++) { |
| 7147 | res += xmlXPathCastNodeToNumber(cur->nodesetval->nodeTab[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7148 | } |
| 7149 | } |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7150 | valuePush(ctxt, xmlXPathNewFloat(res)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7151 | xmlXPathFreeObject(cur); |
| 7152 | } |
| 7153 | |
| 7154 | /** |
| 7155 | * xmlXPathFloorFunction: |
| 7156 | * @ctxt: the XPath Parser context |
| 7157 | * @nargs: the number of arguments |
| 7158 | * |
| 7159 | * Implement the floor() XPath function |
| 7160 | * number floor(number) |
| 7161 | * The floor function returns the largest (closest to positive infinity) |
| 7162 | * number that is not greater than the argument and that is an integer. |
| 7163 | */ |
| 7164 | void |
| 7165 | xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7166 | double f; |
| 7167 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7168 | CHECK_ARITY(1); |
| 7169 | CAST_TO_NUMBER; |
| 7170 | CHECK_TYPE(XPATH_NUMBER); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7171 | |
| 7172 | f = (double)((int) ctxt->value->floatval); |
| 7173 | if (f != ctxt->value->floatval) { |
| 7174 | if (ctxt->value->floatval > 0) |
| 7175 | ctxt->value->floatval = f; |
| 7176 | else |
| 7177 | ctxt->value->floatval = f - 1; |
| 7178 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7179 | } |
| 7180 | |
| 7181 | /** |
| 7182 | * xmlXPathCeilingFunction: |
| 7183 | * @ctxt: the XPath Parser context |
| 7184 | * @nargs: the number of arguments |
| 7185 | * |
| 7186 | * Implement the ceiling() XPath function |
| 7187 | * number ceiling(number) |
| 7188 | * The ceiling function returns the smallest (closest to negative infinity) |
| 7189 | * number that is not less than the argument and that is an integer. |
| 7190 | */ |
| 7191 | void |
| 7192 | xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7193 | double f; |
| 7194 | |
| 7195 | CHECK_ARITY(1); |
| 7196 | CAST_TO_NUMBER; |
| 7197 | CHECK_TYPE(XPATH_NUMBER); |
| 7198 | |
| 7199 | #if 0 |
| 7200 | ctxt->value->floatval = ceil(ctxt->value->floatval); |
| 7201 | #else |
| 7202 | f = (double)((int) ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7203 | if (f != ctxt->value->floatval) { |
| 7204 | if (ctxt->value->floatval > 0) |
| 7205 | ctxt->value->floatval = f + 1; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7206 | else { |
| 7207 | if (ctxt->value->floatval < 0 && f == 0) |
| 7208 | ctxt->value->floatval = xmlXPathNZERO; |
| 7209 | else |
| 7210 | ctxt->value->floatval = f; |
| 7211 | } |
| 7212 | |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7213 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7214 | #endif |
| 7215 | } |
| 7216 | |
| 7217 | /** |
| 7218 | * xmlXPathRoundFunction: |
| 7219 | * @ctxt: the XPath Parser context |
| 7220 | * @nargs: the number of arguments |
| 7221 | * |
| 7222 | * Implement the round() XPath function |
| 7223 | * number round(number) |
| 7224 | * The round function returns the number that is closest to the |
| 7225 | * argument and that is an integer. If there are two such numbers, |
| 7226 | * then the one that is even is returned. |
| 7227 | */ |
| 7228 | void |
| 7229 | xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 7230 | double f; |
| 7231 | |
| 7232 | CHECK_ARITY(1); |
| 7233 | CAST_TO_NUMBER; |
| 7234 | CHECK_TYPE(XPATH_NUMBER); |
| 7235 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 7236 | if ((xmlXPathIsNaN(ctxt->value->floatval)) || |
| 7237 | (xmlXPathIsInf(ctxt->value->floatval) == 1) || |
| 7238 | (xmlXPathIsInf(ctxt->value->floatval) == -1) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7239 | (ctxt->value->floatval == 0.0)) |
| 7240 | return; |
| 7241 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7242 | f = (double)((int) ctxt->value->floatval); |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7243 | if (ctxt->value->floatval < 0) { |
| 7244 | if (ctxt->value->floatval < f - 0.5) |
| 7245 | ctxt->value->floatval = f - 1; |
| 7246 | else |
| 7247 | ctxt->value->floatval = f; |
Daniel Veillard | 5fc1f08 | 2002-03-27 09:05:40 +0000 | [diff] [blame] | 7248 | if (ctxt->value->floatval == 0) |
| 7249 | ctxt->value->floatval = xmlXPathNZERO; |
Daniel Veillard | 56cd18b | 2002-03-22 14:14:43 +0000 | [diff] [blame] | 7250 | } else { |
| 7251 | if (ctxt->value->floatval < f + 0.5) |
| 7252 | ctxt->value->floatval = f; |
| 7253 | else |
| 7254 | ctxt->value->floatval = f + 1; |
| 7255 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7256 | } |
| 7257 | |
| 7258 | /************************************************************************ |
| 7259 | * * |
| 7260 | * The Parser * |
| 7261 | * * |
| 7262 | ************************************************************************/ |
| 7263 | |
| 7264 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7265 | * a few forward declarations since we use a recursive call based |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7266 | * implementation. |
| 7267 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7268 | static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 7269 | static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7270 | static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7271 | static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7272 | static xmlChar * xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, |
| 7273 | int qualified); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7274 | |
| 7275 | /** |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7276 | * xmlXPathCurrentChar: |
| 7277 | * @ctxt: the XPath parser context |
| 7278 | * @cur: pointer to the beginning of the char |
| 7279 | * @len: pointer to the length of the char read |
| 7280 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7281 | * The current char value, if using UTF-8 this may actually span multiple |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7282 | * bytes in the input buffer. |
| 7283 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 7284 | * Returns the current char value and its length |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7285 | */ |
| 7286 | |
| 7287 | static int |
| 7288 | xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt, int *len) { |
| 7289 | unsigned char c; |
| 7290 | unsigned int val; |
| 7291 | const xmlChar *cur; |
| 7292 | |
| 7293 | if (ctxt == NULL) |
| 7294 | return(0); |
| 7295 | cur = ctxt->cur; |
| 7296 | |
| 7297 | /* |
| 7298 | * We are supposed to handle UTF8, check it's valid |
| 7299 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 7300 | * |
| 7301 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 7302 | * 0000 0000-0000 007F 0xxxxxxx |
| 7303 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 7304 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 7305 | * |
| 7306 | * Check for the 0x110000 limit too |
| 7307 | */ |
| 7308 | c = *cur; |
| 7309 | if (c & 0x80) { |
| 7310 | if ((cur[1] & 0xc0) != 0x80) |
| 7311 | goto encoding_error; |
| 7312 | if ((c & 0xe0) == 0xe0) { |
| 7313 | |
| 7314 | if ((cur[2] & 0xc0) != 0x80) |
| 7315 | goto encoding_error; |
| 7316 | if ((c & 0xf0) == 0xf0) { |
| 7317 | if (((c & 0xf8) != 0xf0) || |
| 7318 | ((cur[3] & 0xc0) != 0x80)) |
| 7319 | goto encoding_error; |
| 7320 | /* 4-byte code */ |
| 7321 | *len = 4; |
| 7322 | val = (cur[0] & 0x7) << 18; |
| 7323 | val |= (cur[1] & 0x3f) << 12; |
| 7324 | val |= (cur[2] & 0x3f) << 6; |
| 7325 | val |= cur[3] & 0x3f; |
| 7326 | } else { |
| 7327 | /* 3-byte code */ |
| 7328 | *len = 3; |
| 7329 | val = (cur[0] & 0xf) << 12; |
| 7330 | val |= (cur[1] & 0x3f) << 6; |
| 7331 | val |= cur[2] & 0x3f; |
| 7332 | } |
| 7333 | } else { |
| 7334 | /* 2-byte code */ |
| 7335 | *len = 2; |
| 7336 | val = (cur[0] & 0x1f) << 6; |
| 7337 | val |= cur[1] & 0x3f; |
| 7338 | } |
| 7339 | if (!IS_CHAR(val)) { |
| 7340 | XP_ERROR0(XPATH_INVALID_CHAR_ERROR); |
| 7341 | } |
| 7342 | return(val); |
| 7343 | } else { |
| 7344 | /* 1-byte code */ |
| 7345 | *len = 1; |
| 7346 | return((int) *cur); |
| 7347 | } |
| 7348 | encoding_error: |
| 7349 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7350 | * If we detect an UTF8 error that probably means that the |
| 7351 | * input encoding didn't get properly advertised in the |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7352 | * declaration header. Report the error and switch the encoding |
| 7353 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 7354 | * encoding !) |
| 7355 | */ |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 7356 | *len = 0; |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7357 | XP_ERROR0(XPATH_ENCODING_ERROR); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7358 | } |
| 7359 | |
| 7360 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7361 | * xmlXPathParseNCName: |
| 7362 | * @ctxt: the XPath Parser context |
| 7363 | * |
| 7364 | * parse an XML namespace non qualified name. |
| 7365 | * |
| 7366 | * [NS 3] NCName ::= (Letter | '_') (NCNameChar)* |
| 7367 | * |
| 7368 | * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 7369 | * CombiningChar | Extender |
| 7370 | * |
| 7371 | * Returns the namespace name or NULL |
| 7372 | */ |
| 7373 | |
| 7374 | xmlChar * |
| 7375 | xmlXPathParseNCName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7376 | const xmlChar *in; |
| 7377 | xmlChar *ret; |
| 7378 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7379 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7380 | if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7381 | /* |
| 7382 | * Accelerator for simple ASCII names |
| 7383 | */ |
| 7384 | in = ctxt->cur; |
| 7385 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7386 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7387 | (*in == '_')) { |
| 7388 | in++; |
| 7389 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7390 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7391 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 9a89a8a | 2001-06-27 11:13:35 +0000 | [diff] [blame] | 7392 | (*in == '_') || (*in == '.') || |
| 7393 | (*in == '-')) |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7394 | in++; |
| 7395 | if ((*in == ' ') || (*in == '>') || (*in == '/') || |
| 7396 | (*in == '[') || (*in == ']') || (*in == ':') || |
| 7397 | (*in == '@') || (*in == '*')) { |
| 7398 | count = in - ctxt->cur; |
| 7399 | if (count == 0) |
| 7400 | return(NULL); |
| 7401 | ret = xmlStrndup(ctxt->cur, count); |
| 7402 | ctxt->cur = in; |
| 7403 | return(ret); |
| 7404 | } |
| 7405 | } |
| 7406 | return(xmlXPathParseNameComplex(ctxt, 0)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7407 | } |
| 7408 | |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7409 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7410 | /** |
| 7411 | * xmlXPathParseQName: |
| 7412 | * @ctxt: the XPath Parser context |
| 7413 | * @prefix: a xmlChar ** |
| 7414 | * |
| 7415 | * parse an XML qualified name |
| 7416 | * |
| 7417 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 7418 | * |
| 7419 | * [NS 6] Prefix ::= NCName |
| 7420 | * |
| 7421 | * [NS 7] LocalPart ::= NCName |
| 7422 | * |
| 7423 | * Returns the function returns the local part, and prefix is updated |
| 7424 | * to get the Prefix if any. |
| 7425 | */ |
| 7426 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7427 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7428 | xmlXPathParseQName(xmlXPathParserContextPtr ctxt, xmlChar **prefix) { |
| 7429 | xmlChar *ret = NULL; |
| 7430 | |
| 7431 | *prefix = NULL; |
| 7432 | ret = xmlXPathParseNCName(ctxt); |
| 7433 | if (CUR == ':') { |
| 7434 | *prefix = ret; |
| 7435 | NEXT; |
| 7436 | ret = xmlXPathParseNCName(ctxt); |
| 7437 | } |
| 7438 | return(ret); |
| 7439 | } |
| 7440 | |
| 7441 | /** |
| 7442 | * xmlXPathParseName: |
| 7443 | * @ctxt: the XPath Parser context |
| 7444 | * |
| 7445 | * parse an XML name |
| 7446 | * |
| 7447 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 7448 | * CombiningChar | Extender |
| 7449 | * |
| 7450 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 7451 | * |
| 7452 | * Returns the namespace name or NULL |
| 7453 | */ |
| 7454 | |
| 7455 | xmlChar * |
| 7456 | xmlXPathParseName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7457 | const xmlChar *in; |
| 7458 | xmlChar *ret; |
| 7459 | int count = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7460 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 7461 | if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7462 | /* |
| 7463 | * Accelerator for simple ASCII names |
| 7464 | */ |
| 7465 | in = ctxt->cur; |
| 7466 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7467 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7468 | (*in == '_') || (*in == ':')) { |
| 7469 | in++; |
| 7470 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7471 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7472 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 7473 | (*in == '_') || (*in == '-') || |
| 7474 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7475 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 7476 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7477 | count = in - ctxt->cur; |
| 7478 | ret = xmlStrndup(ctxt->cur, count); |
| 7479 | ctxt->cur = in; |
| 7480 | return(ret); |
| 7481 | } |
| 7482 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7483 | return(xmlXPathParseNameComplex(ctxt, 1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7484 | } |
| 7485 | |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7486 | static xmlChar * |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7487 | xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt, int qualified) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7488 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 7489 | int len = 0, l; |
| 7490 | int c; |
| 7491 | |
| 7492 | /* |
| 7493 | * Handler for more complex cases |
| 7494 | */ |
| 7495 | c = CUR_CHAR(l); |
| 7496 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7497 | (c == '[') || (c == ']') || (c == '@') || /* accelerators */ |
| 7498 | (c == '*') || /* accelerators */ |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7499 | (!IS_LETTER(c) && (c != '_') && |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7500 | ((qualified) && (c != ':')))) { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7501 | return(NULL); |
| 7502 | } |
| 7503 | |
| 7504 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 7505 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 7506 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7507 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7508 | (IS_COMBINING(c)) || |
| 7509 | (IS_EXTENDER(c)))) { |
| 7510 | COPY_BUF(l,buf,len,c); |
| 7511 | NEXTL(l); |
| 7512 | c = CUR_CHAR(l); |
| 7513 | if (len >= XML_MAX_NAMELEN) { |
| 7514 | /* |
| 7515 | * Okay someone managed to make a huge name, so he's ready to pay |
| 7516 | * for the processing speed. |
| 7517 | */ |
| 7518 | xmlChar *buffer; |
| 7519 | int max = len * 2; |
| 7520 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 7521 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7522 | if (buffer == NULL) { |
| 7523 | XP_ERROR0(XPATH_MEMORY_ERROR); |
| 7524 | } |
| 7525 | memcpy(buffer, buf, len); |
| 7526 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigname.xml */ |
| 7527 | (c == '.') || (c == '-') || |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7528 | (c == '_') || ((qualified) && (c == ':')) || |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7529 | (IS_COMBINING(c)) || |
| 7530 | (IS_EXTENDER(c))) { |
| 7531 | if (len + 10 > max) { |
| 7532 | max *= 2; |
| 7533 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 7534 | max * sizeof(xmlChar)); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7535 | if (buffer == NULL) { |
| 7536 | XP_ERROR0(XPATH_MEMORY_ERROR); |
| 7537 | } |
| 7538 | } |
| 7539 | COPY_BUF(l,buffer,len,c); |
| 7540 | NEXTL(l); |
| 7541 | c = CUR_CHAR(l); |
| 7542 | } |
| 7543 | buffer[len] = 0; |
| 7544 | return(buffer); |
| 7545 | } |
| 7546 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 7547 | if (len == 0) |
| 7548 | return(NULL); |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 7549 | return(xmlStrndup(buf, len)); |
| 7550 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7551 | |
| 7552 | #define MAX_FRAC 20 |
| 7553 | |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 7554 | /* |
| 7555 | * These are used as divisors for the fractional part of a number. |
| 7556 | * Since the table includes 1.0 (representing '0' fractional digits), |
| 7557 | * it must be dimensioned at MAX_FRAC+1 (bug 133921) |
| 7558 | */ |
| 7559 | static double my_pow10[MAX_FRAC+1] = { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7560 | 1.0, 10.0, 100.0, 1000.0, 10000.0, |
| 7561 | 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0, |
| 7562 | 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0, |
| 7563 | 100000000000000.0, |
| 7564 | 1000000000000000.0, 10000000000000000.0, 100000000000000000.0, |
William M. Brack | 372a445 | 2004-02-17 13:09:23 +0000 | [diff] [blame] | 7565 | 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0 |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7566 | }; |
| 7567 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7568 | /** |
| 7569 | * xmlXPathStringEvalNumber: |
| 7570 | * @str: A string to scan |
| 7571 | * |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7572 | * [30a] Float ::= Number ('e' Digits?)? |
| 7573 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7574 | * [30] Number ::= Digits ('.' Digits?)? |
| 7575 | * | '.' Digits |
| 7576 | * [31] Digits ::= [0-9]+ |
| 7577 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7578 | * Compile a Number in the string |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7579 | * In complement of the Number expression, this function also handles |
| 7580 | * negative values : '-' Number. |
| 7581 | * |
| 7582 | * Returns the double value. |
| 7583 | */ |
| 7584 | double |
| 7585 | xmlXPathStringEvalNumber(const xmlChar *str) { |
| 7586 | const xmlChar *cur = str; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7587 | double ret; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7588 | int ok = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7589 | int isneg = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7590 | int exponent = 0; |
| 7591 | int is_exponent_negative = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7592 | #ifdef __GNUC__ |
| 7593 | unsigned long tmp = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7594 | double temp; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7595 | #endif |
Daniel Veillard | eca8281 | 2002-04-24 11:42:02 +0000 | [diff] [blame] | 7596 | if (cur == NULL) return(0); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7597 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7598 | if ((*cur != '.') && ((*cur < '0') || (*cur > '9')) && (*cur != '-')) { |
| 7599 | return(xmlXPathNAN); |
| 7600 | } |
| 7601 | if (*cur == '-') { |
| 7602 | isneg = 1; |
| 7603 | cur++; |
| 7604 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7605 | |
| 7606 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7607 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7608 | * tmp/temp is a workaround against a gcc compiler bug |
| 7609 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7610 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7611 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7612 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7613 | ret = ret * 10; |
| 7614 | tmp = (*cur - '0'); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7615 | ok = 1; |
| 7616 | cur++; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7617 | temp = (double) tmp; |
| 7618 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7619 | } |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7620 | #else |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7621 | ret = 0; |
Daniel Veillard | b06c614 | 2001-08-27 14:26:30 +0000 | [diff] [blame] | 7622 | while ((*cur >= '0') && (*cur <= '9')) { |
| 7623 | ret = ret * 10 + (*cur - '0'); |
| 7624 | ok = 1; |
| 7625 | cur++; |
| 7626 | } |
| 7627 | #endif |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7628 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7629 | if (*cur == '.') { |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7630 | int v, frac = 0; |
| 7631 | double fraction = 0; |
| 7632 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7633 | cur++; |
| 7634 | if (((*cur < '0') || (*cur > '9')) && (!ok)) { |
| 7635 | return(xmlXPathNAN); |
| 7636 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7637 | while (((*cur >= '0') && (*cur <= '9')) && (frac < MAX_FRAC)) { |
| 7638 | v = (*cur - '0'); |
| 7639 | fraction = fraction * 10 + v; |
| 7640 | frac = frac + 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7641 | cur++; |
| 7642 | } |
Daniel Veillard | 3cd7240 | 2002-05-13 10:33:30 +0000 | [diff] [blame] | 7643 | fraction /= my_pow10[frac]; |
| 7644 | ret = ret + fraction; |
| 7645 | while ((*cur >= '0') && (*cur <= '9')) |
| 7646 | cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7647 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7648 | if ((*cur == 'e') || (*cur == 'E')) { |
| 7649 | cur++; |
| 7650 | if (*cur == '-') { |
| 7651 | is_exponent_negative = 1; |
| 7652 | cur++; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 7653 | } else if (*cur == '+') { |
| 7654 | cur++; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7655 | } |
| 7656 | while ((*cur >= '0') && (*cur <= '9')) { |
| 7657 | exponent = exponent * 10 + (*cur - '0'); |
| 7658 | cur++; |
| 7659 | } |
| 7660 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7661 | while (IS_BLANK_CH(*cur)) cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7662 | if (*cur != 0) return(xmlXPathNAN); |
| 7663 | if (isneg) ret = -ret; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7664 | if (is_exponent_negative) exponent = -exponent; |
| 7665 | ret *= pow(10.0, (double)exponent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7666 | return(ret); |
| 7667 | } |
| 7668 | |
| 7669 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7670 | * xmlXPathCompNumber: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7671 | * @ctxt: the XPath Parser context |
| 7672 | * |
| 7673 | * [30] Number ::= Digits ('.' Digits?)? |
| 7674 | * | '.' Digits |
| 7675 | * [31] Digits ::= [0-9]+ |
| 7676 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7677 | * Compile a Number, then push it on the stack |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7678 | * |
| 7679 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7680 | static void |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7681 | xmlXPathCompNumber(xmlXPathParserContextPtr ctxt) |
| 7682 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7683 | double ret = 0.0; |
| 7684 | double mult = 1; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7685 | int ok = 0; |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7686 | int exponent = 0; |
| 7687 | int is_exponent_negative = 0; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7688 | #ifdef __GNUC__ |
| 7689 | unsigned long tmp = 0; |
| 7690 | double temp; |
| 7691 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7692 | |
| 7693 | CHECK_ERROR; |
| 7694 | if ((CUR != '.') && ((CUR < '0') || (CUR > '9'))) { |
| 7695 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 7696 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7697 | #ifdef __GNUC__ |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7698 | /* |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7699 | * tmp/temp is a workaround against a gcc compiler bug |
| 7700 | * http://veillard.com/gcc.bug |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7701 | */ |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7702 | ret = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7703 | while ((CUR >= '0') && (CUR <= '9')) { |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7704 | ret = ret * 10; |
| 7705 | tmp = (CUR - '0'); |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7706 | ok = 1; |
| 7707 | NEXT; |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7708 | temp = (double) tmp; |
| 7709 | ret = ret + temp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7710 | } |
Daniel Veillard | 7b41613 | 2002-03-07 08:36:03 +0000 | [diff] [blame] | 7711 | #else |
| 7712 | ret = 0; |
| 7713 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7714 | ret = ret * 10 + (CUR - '0'); |
| 7715 | ok = 1; |
| 7716 | NEXT; |
| 7717 | } |
| 7718 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7719 | if (CUR == '.') { |
| 7720 | NEXT; |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7721 | if (((CUR < '0') || (CUR > '9')) && (!ok)) { |
| 7722 | XP_ERROR(XPATH_NUMBER_ERROR); |
| 7723 | } |
| 7724 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7725 | mult /= 10; |
| 7726 | ret = ret + (CUR - '0') * mult; |
| 7727 | NEXT; |
| 7728 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7729 | } |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7730 | if ((CUR == 'e') || (CUR == 'E')) { |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7731 | NEXT; |
| 7732 | if (CUR == '-') { |
| 7733 | is_exponent_negative = 1; |
| 7734 | NEXT; |
William M. Brack | 9912705 | 2004-05-24 02:52:28 +0000 | [diff] [blame] | 7735 | } else if (CUR == '+') { |
| 7736 | NEXT; |
| 7737 | } |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7738 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7739 | exponent = exponent * 10 + (CUR - '0'); |
| 7740 | NEXT; |
| 7741 | } |
| 7742 | if (is_exponent_negative) |
| 7743 | exponent = -exponent; |
| 7744 | ret *= pow(10.0, (double) exponent); |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 7745 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7746 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_NUMBER, 0, 0, |
Daniel Veillard | d79bcd1 | 2001-06-21 22:07:42 +0000 | [diff] [blame] | 7747 | xmlXPathNewFloat(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7748 | } |
| 7749 | |
| 7750 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7751 | * xmlXPathParseLiteral: |
| 7752 | * @ctxt: the XPath Parser context |
| 7753 | * |
| 7754 | * Parse a Literal |
| 7755 | * |
| 7756 | * [29] Literal ::= '"' [^"]* '"' |
| 7757 | * | "'" [^']* "'" |
| 7758 | * |
| 7759 | * Returns the value found or NULL in case of error |
| 7760 | */ |
| 7761 | static xmlChar * |
| 7762 | xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt) { |
| 7763 | const xmlChar *q; |
| 7764 | xmlChar *ret = NULL; |
| 7765 | |
| 7766 | if (CUR == '"') { |
| 7767 | NEXT; |
| 7768 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7769 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7770 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7771 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7772 | XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7773 | } else { |
| 7774 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7775 | NEXT; |
| 7776 | } |
| 7777 | } else if (CUR == '\'') { |
| 7778 | NEXT; |
| 7779 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7780 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7781 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7782 | if (!IS_CHAR_CH(CUR)) { |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7783 | XP_ERROR0(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7784 | } else { |
| 7785 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7786 | NEXT; |
| 7787 | } |
| 7788 | } else { |
| 7789 | XP_ERROR0(XPATH_START_LITERAL_ERROR); |
| 7790 | } |
| 7791 | return(ret); |
| 7792 | } |
| 7793 | |
| 7794 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7795 | * xmlXPathCompLiteral: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7796 | * @ctxt: the XPath Parser context |
| 7797 | * |
| 7798 | * Parse a Literal and push it on the stack. |
| 7799 | * |
| 7800 | * [29] Literal ::= '"' [^"]* '"' |
| 7801 | * | "'" [^']* "'" |
| 7802 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7803 | * TODO: xmlXPathCompLiteral memory allocation could be improved. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7804 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7805 | static void |
| 7806 | xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7807 | const xmlChar *q; |
| 7808 | xmlChar *ret = NULL; |
| 7809 | |
| 7810 | if (CUR == '"') { |
| 7811 | NEXT; |
| 7812 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7813 | while ((IS_CHAR_CH(CUR)) && (CUR != '"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7814 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7815 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7816 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7817 | } else { |
| 7818 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7819 | NEXT; |
| 7820 | } |
| 7821 | } else if (CUR == '\'') { |
| 7822 | NEXT; |
| 7823 | q = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7824 | while ((IS_CHAR_CH(CUR)) && (CUR != '\'')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7825 | NEXT; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7826 | if (!IS_CHAR_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7827 | XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); |
| 7828 | } else { |
| 7829 | ret = xmlStrndup(q, CUR_PTR - q); |
| 7830 | NEXT; |
| 7831 | } |
| 7832 | } else { |
| 7833 | XP_ERROR(XPATH_START_LITERAL_ERROR); |
| 7834 | } |
| 7835 | if (ret == NULL) return; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7836 | PUSH_LONG_EXPR(XPATH_OP_VALUE, XPATH_STRING, 0, 0, |
| 7837 | xmlXPathNewString(ret), NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7838 | xmlFree(ret); |
| 7839 | } |
| 7840 | |
| 7841 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7842 | * xmlXPathCompVariableReference: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7843 | * @ctxt: the XPath Parser context |
| 7844 | * |
| 7845 | * Parse a VariableReference, evaluate it and push it on the stack. |
| 7846 | * |
| 7847 | * The variable bindings consist of a mapping from variable names |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 7848 | * to variable values. The value of a variable is an object, which can be |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7849 | * of any of the types that are possible for the value of an expression, |
| 7850 | * and may also be of additional types not specified here. |
| 7851 | * |
| 7852 | * Early evaluation is possible since: |
| 7853 | * The variable bindings [...] used to evaluate a subexpression are |
| 7854 | * always the same as those used to evaluate the containing expression. |
| 7855 | * |
| 7856 | * [36] VariableReference ::= '$' QName |
| 7857 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7858 | static void |
| 7859 | xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7860 | xmlChar *name; |
| 7861 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7862 | |
| 7863 | SKIP_BLANKS; |
| 7864 | if (CUR != '$') { |
| 7865 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 7866 | } |
| 7867 | NEXT; |
| 7868 | name = xmlXPathParseQName(ctxt, &prefix); |
| 7869 | if (name == NULL) { |
| 7870 | XP_ERROR(XPATH_VARIABLE_REF_ERROR); |
| 7871 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 7872 | ctxt->comp->last = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7873 | PUSH_LONG_EXPR(XPATH_OP_VARIABLE, 0, 0, 0, |
| 7874 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7875 | SKIP_BLANKS; |
| 7876 | } |
| 7877 | |
| 7878 | /** |
| 7879 | * xmlXPathIsNodeType: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7880 | * @name: a name string |
| 7881 | * |
| 7882 | * Is the name given a NodeType one. |
| 7883 | * |
| 7884 | * [38] NodeType ::= 'comment' |
| 7885 | * | 'text' |
| 7886 | * | 'processing-instruction' |
| 7887 | * | 'node' |
| 7888 | * |
| 7889 | * Returns 1 if true 0 otherwise |
| 7890 | */ |
| 7891 | int |
| 7892 | xmlXPathIsNodeType(const xmlChar *name) { |
| 7893 | if (name == NULL) |
| 7894 | return(0); |
| 7895 | |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7896 | if (xmlStrEqual(name, BAD_CAST "node")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7897 | return(1); |
| 7898 | if (xmlStrEqual(name, BAD_CAST "text")) |
| 7899 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7900 | if (xmlStrEqual(name, BAD_CAST "comment")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7901 | return(1); |
Daniel Veillard | 1971ee2 | 2002-01-31 20:29:19 +0000 | [diff] [blame] | 7902 | if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7903 | return(1); |
| 7904 | return(0); |
| 7905 | } |
| 7906 | |
| 7907 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7908 | * xmlXPathCompFunctionCall: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7909 | * @ctxt: the XPath Parser context |
| 7910 | * |
| 7911 | * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')' |
| 7912 | * [17] Argument ::= Expr |
| 7913 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7914 | * Compile a function call, the evaluation of all arguments are |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7915 | * pushed on the stack |
| 7916 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7917 | static void |
| 7918 | xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7919 | xmlChar *name; |
| 7920 | xmlChar *prefix; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7921 | int nbargs = 0; |
| 7922 | |
| 7923 | name = xmlXPathParseQName(ctxt, &prefix); |
| 7924 | if (name == NULL) { |
| 7925 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7926 | } |
| 7927 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7928 | #ifdef DEBUG_EXPR |
| 7929 | if (prefix == NULL) |
| 7930 | xmlGenericError(xmlGenericErrorContext, "Calling function %s\n", |
| 7931 | name); |
| 7932 | else |
| 7933 | xmlGenericError(xmlGenericErrorContext, "Calling function %s:%s\n", |
| 7934 | prefix, name); |
| 7935 | #endif |
| 7936 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7937 | if (CUR != '(') { |
| 7938 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7939 | } |
| 7940 | NEXT; |
| 7941 | SKIP_BLANKS; |
| 7942 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7943 | ctxt->comp->last = -1; |
Daniel Veillard | 71f9d73 | 2003-01-14 16:07:16 +0000 | [diff] [blame] | 7944 | if (CUR != ')') { |
| 7945 | while (CUR != 0) { |
| 7946 | int op1 = ctxt->comp->last; |
| 7947 | ctxt->comp->last = -1; |
| 7948 | xmlXPathCompileExpr(ctxt); |
| 7949 | CHECK_ERROR; |
| 7950 | PUSH_BINARY_EXPR(XPATH_OP_ARG, op1, ctxt->comp->last, 0, 0); |
| 7951 | nbargs++; |
| 7952 | if (CUR == ')') break; |
| 7953 | if (CUR != ',') { |
| 7954 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7955 | } |
| 7956 | NEXT; |
| 7957 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7958 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7959 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 7960 | PUSH_LONG_EXPR(XPATH_OP_FUNCTION, nbargs, 0, 0, |
| 7961 | name, prefix); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7962 | NEXT; |
| 7963 | SKIP_BLANKS; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7964 | } |
| 7965 | |
| 7966 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7967 | * xmlXPathCompPrimaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7968 | * @ctxt: the XPath Parser context |
| 7969 | * |
| 7970 | * [15] PrimaryExpr ::= VariableReference |
| 7971 | * | '(' Expr ')' |
| 7972 | * | Literal |
| 7973 | * | Number |
| 7974 | * | FunctionCall |
| 7975 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7976 | * Compile a primary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7977 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7978 | static void |
| 7979 | xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7980 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7981 | if (CUR == '$') xmlXPathCompVariableReference(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7982 | else if (CUR == '(') { |
| 7983 | NEXT; |
| 7984 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7985 | xmlXPathCompileExpr(ctxt); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 7986 | CHECK_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7987 | if (CUR != ')') { |
| 7988 | XP_ERROR(XPATH_EXPR_ERROR); |
| 7989 | } |
| 7990 | NEXT; |
| 7991 | SKIP_BLANKS; |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 7992 | } else if (IS_ASCII_DIGIT(CUR) || (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7993 | xmlXPathCompNumber(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7994 | } else if ((CUR == '\'') || (CUR == '"')) { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7995 | xmlXPathCompLiteral(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7996 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 7997 | xmlXPathCompFunctionCall(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7998 | } |
| 7999 | SKIP_BLANKS; |
| 8000 | } |
| 8001 | |
| 8002 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8003 | * xmlXPathCompFilterExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8004 | * @ctxt: the XPath Parser context |
| 8005 | * |
| 8006 | * [20] FilterExpr ::= PrimaryExpr |
| 8007 | * | FilterExpr Predicate |
| 8008 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8009 | * Compile a filter expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8010 | * Square brackets are used to filter expressions in the same way that |
| 8011 | * they are used in location paths. It is an error if the expression to |
| 8012 | * be filtered does not evaluate to a node-set. The context node list |
| 8013 | * used for evaluating the expression in square brackets is the node-set |
| 8014 | * to be filtered listed in document order. |
| 8015 | */ |
| 8016 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8017 | static void |
| 8018 | xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt) { |
| 8019 | xmlXPathCompPrimaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8020 | CHECK_ERROR; |
| 8021 | SKIP_BLANKS; |
| 8022 | |
| 8023 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8024 | xmlXPathCompPredicate(ctxt, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8025 | SKIP_BLANKS; |
| 8026 | } |
| 8027 | |
| 8028 | |
| 8029 | } |
| 8030 | |
| 8031 | /** |
| 8032 | * xmlXPathScanName: |
| 8033 | * @ctxt: the XPath Parser context |
| 8034 | * |
| 8035 | * Trickery: parse an XML name but without consuming the input flow |
| 8036 | * Needed to avoid insanity in the parser state. |
| 8037 | * |
| 8038 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 8039 | * CombiningChar | Extender |
| 8040 | * |
| 8041 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 8042 | * |
| 8043 | * [6] Names ::= Name (S Name)* |
| 8044 | * |
| 8045 | * Returns the Name parsed or NULL |
| 8046 | */ |
| 8047 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8048 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8049 | xmlXPathScanName(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8050 | int len = 0, l; |
| 8051 | int c; |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8052 | const xmlChar *cur; |
| 8053 | xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8054 | |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8055 | cur = ctxt->cur; |
| 8056 | |
| 8057 | c = CUR_CHAR(l); |
| 8058 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 8059 | (!IS_LETTER(c) && (c != '_') && |
| 8060 | (c != ':'))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8061 | return(NULL); |
| 8062 | } |
| 8063 | |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8064 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 8065 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 8066 | (c == '.') || (c == '-') || |
| 8067 | (c == '_') || (c == ':') || |
| 8068 | (IS_COMBINING(c)) || |
| 8069 | (IS_EXTENDER(c)))) { |
| 8070 | len += l; |
| 8071 | NEXTL(l); |
| 8072 | c = CUR_CHAR(l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8073 | } |
Daniel Veillard | 0322681 | 2004-11-01 14:55:21 +0000 | [diff] [blame] | 8074 | ret = xmlStrndup(cur, ctxt->cur - cur); |
| 8075 | ctxt->cur = cur; |
| 8076 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8077 | } |
| 8078 | |
| 8079 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8080 | * xmlXPathCompPathExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8081 | * @ctxt: the XPath Parser context |
| 8082 | * |
| 8083 | * [19] PathExpr ::= LocationPath |
| 8084 | * | FilterExpr |
| 8085 | * | FilterExpr '/' RelativeLocationPath |
| 8086 | * | FilterExpr '//' RelativeLocationPath |
| 8087 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8088 | * Compile a path expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8089 | * The / operator and // operators combine an arbitrary expression |
| 8090 | * and a relative location path. It is an error if the expression |
| 8091 | * does not evaluate to a node-set. |
| 8092 | * The / operator does composition in the same way as when / is |
| 8093 | * used in a location path. As in location paths, // is short for |
| 8094 | * /descendant-or-self::node()/. |
| 8095 | */ |
| 8096 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8097 | static void |
| 8098 | xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8099 | int lc = 1; /* Should we branch to LocationPath ? */ |
| 8100 | xmlChar *name = NULL; /* we may have to preparse a name to find out */ |
| 8101 | |
| 8102 | SKIP_BLANKS; |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 8103 | if ((CUR == '$') || (CUR == '(') || |
| 8104 | (IS_ASCII_DIGIT(CUR)) || |
| 8105 | (CUR == '\'') || (CUR == '"') || |
| 8106 | (CUR == '.' && IS_ASCII_DIGIT(NXT(1)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8107 | lc = 0; |
| 8108 | } else if (CUR == '*') { |
| 8109 | /* relative or absolute location path */ |
| 8110 | lc = 1; |
| 8111 | } else if (CUR == '/') { |
| 8112 | /* relative or absolute location path */ |
| 8113 | lc = 1; |
| 8114 | } else if (CUR == '@') { |
| 8115 | /* relative abbreviated attribute location path */ |
| 8116 | lc = 1; |
| 8117 | } else if (CUR == '.') { |
| 8118 | /* relative abbreviated attribute location path */ |
| 8119 | lc = 1; |
| 8120 | } else { |
| 8121 | /* |
| 8122 | * Problem is finding if we have a name here whether it's: |
| 8123 | * - a nodetype |
| 8124 | * - a function call in which case it's followed by '(' |
| 8125 | * - an axis in which case it's followed by ':' |
| 8126 | * - a element name |
| 8127 | * We do an a priori analysis here rather than having to |
| 8128 | * maintain parsed token content through the recursive function |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8129 | * calls. This looks uglier but makes the code easier to |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8130 | * read/write/debug. |
| 8131 | */ |
| 8132 | SKIP_BLANKS; |
| 8133 | name = xmlXPathScanName(ctxt); |
| 8134 | if ((name != NULL) && (xmlStrstr(name, (xmlChar *) "::") != NULL)) { |
| 8135 | #ifdef DEBUG_STEP |
| 8136 | xmlGenericError(xmlGenericErrorContext, |
| 8137 | "PathExpr: Axis\n"); |
| 8138 | #endif |
| 8139 | lc = 1; |
| 8140 | xmlFree(name); |
| 8141 | } else if (name != NULL) { |
| 8142 | int len =xmlStrlen(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8143 | |
| 8144 | |
| 8145 | while (NXT(len) != 0) { |
| 8146 | if (NXT(len) == '/') { |
| 8147 | /* element name */ |
| 8148 | #ifdef DEBUG_STEP |
| 8149 | xmlGenericError(xmlGenericErrorContext, |
| 8150 | "PathExpr: AbbrRelLocation\n"); |
| 8151 | #endif |
| 8152 | lc = 1; |
| 8153 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8154 | } else if (IS_BLANK_CH(NXT(len))) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8155 | /* ignore blanks */ |
| 8156 | ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8157 | } else if (NXT(len) == ':') { |
| 8158 | #ifdef DEBUG_STEP |
| 8159 | xmlGenericError(xmlGenericErrorContext, |
| 8160 | "PathExpr: AbbrRelLocation\n"); |
| 8161 | #endif |
| 8162 | lc = 1; |
| 8163 | break; |
| 8164 | } else if ((NXT(len) == '(')) { |
| 8165 | /* Note Type or Function */ |
| 8166 | if (xmlXPathIsNodeType(name)) { |
| 8167 | #ifdef DEBUG_STEP |
| 8168 | xmlGenericError(xmlGenericErrorContext, |
| 8169 | "PathExpr: Type search\n"); |
| 8170 | #endif |
| 8171 | lc = 1; |
| 8172 | } else { |
| 8173 | #ifdef DEBUG_STEP |
| 8174 | xmlGenericError(xmlGenericErrorContext, |
| 8175 | "PathExpr: function call\n"); |
| 8176 | #endif |
| 8177 | lc = 0; |
| 8178 | } |
| 8179 | break; |
| 8180 | } else if ((NXT(len) == '[')) { |
| 8181 | /* element name */ |
| 8182 | #ifdef DEBUG_STEP |
| 8183 | xmlGenericError(xmlGenericErrorContext, |
| 8184 | "PathExpr: AbbrRelLocation\n"); |
| 8185 | #endif |
| 8186 | lc = 1; |
| 8187 | break; |
| 8188 | } else if ((NXT(len) == '<') || (NXT(len) == '>') || |
| 8189 | (NXT(len) == '=')) { |
| 8190 | lc = 1; |
| 8191 | break; |
| 8192 | } else { |
| 8193 | lc = 1; |
| 8194 | break; |
| 8195 | } |
| 8196 | len++; |
| 8197 | } |
| 8198 | if (NXT(len) == 0) { |
| 8199 | #ifdef DEBUG_STEP |
| 8200 | xmlGenericError(xmlGenericErrorContext, |
| 8201 | "PathExpr: AbbrRelLocation\n"); |
| 8202 | #endif |
| 8203 | /* element name */ |
| 8204 | lc = 1; |
| 8205 | } |
| 8206 | xmlFree(name); |
| 8207 | } else { |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8208 | /* make sure all cases are covered explicitly */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8209 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8210 | } |
| 8211 | } |
| 8212 | |
| 8213 | if (lc) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8214 | if (CUR == '/') { |
| 8215 | PUSH_LEAVE_EXPR(XPATH_OP_ROOT, 0, 0); |
| 8216 | } else { |
| 8217 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8218 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8219 | xmlXPathCompLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8220 | } else { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8221 | xmlXPathCompFilterExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8222 | CHECK_ERROR; |
| 8223 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8224 | SKIP(2); |
| 8225 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8226 | |
| 8227 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8228 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
| 8229 | PUSH_UNARY_EXPR(XPATH_OP_RESET, ctxt->comp->last, 1, 0); |
| 8230 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8231 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8232 | } else if (CUR == '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8233 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8234 | } |
| 8235 | } |
| 8236 | SKIP_BLANKS; |
| 8237 | } |
| 8238 | |
| 8239 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8240 | * xmlXPathCompUnionExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8241 | * @ctxt: the XPath Parser context |
| 8242 | * |
| 8243 | * [18] UnionExpr ::= PathExpr |
| 8244 | * | UnionExpr '|' PathExpr |
| 8245 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8246 | * Compile an union expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8247 | */ |
| 8248 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8249 | static void |
| 8250 | xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt) { |
| 8251 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8252 | CHECK_ERROR; |
| 8253 | SKIP_BLANKS; |
| 8254 | while (CUR == '|') { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8255 | int op1 = ctxt->comp->last; |
| 8256 | PUSH_LEAVE_EXPR(XPATH_OP_NODE, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8257 | |
| 8258 | NEXT; |
| 8259 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8260 | xmlXPathCompPathExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8261 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8262 | PUSH_BINARY_EXPR(XPATH_OP_UNION, op1, ctxt->comp->last, 0, 0); |
| 8263 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8264 | SKIP_BLANKS; |
| 8265 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8266 | } |
| 8267 | |
| 8268 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8269 | * xmlXPathCompUnaryExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8270 | * @ctxt: the XPath Parser context |
| 8271 | * |
| 8272 | * [27] UnaryExpr ::= UnionExpr |
| 8273 | * | '-' UnaryExpr |
| 8274 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8275 | * Compile an unary expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8276 | */ |
| 8277 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8278 | static void |
| 8279 | xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8280 | int minus = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8281 | int found = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8282 | |
| 8283 | SKIP_BLANKS; |
Daniel Veillard | 68d7b67 | 2001-03-12 18:22:04 +0000 | [diff] [blame] | 8284 | while (CUR == '-') { |
| 8285 | minus = 1 - minus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8286 | found = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8287 | NEXT; |
| 8288 | SKIP_BLANKS; |
| 8289 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8290 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8291 | xmlXPathCompUnionExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8292 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8293 | if (found) { |
| 8294 | if (minus) |
| 8295 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 2, 0); |
| 8296 | else |
| 8297 | PUSH_UNARY_EXPR(XPATH_OP_PLUS, ctxt->comp->last, 3, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8298 | } |
| 8299 | } |
| 8300 | |
| 8301 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8302 | * xmlXPathCompMultiplicativeExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8303 | * @ctxt: the XPath Parser context |
| 8304 | * |
| 8305 | * [26] MultiplicativeExpr ::= UnaryExpr |
| 8306 | * | MultiplicativeExpr MultiplyOperator UnaryExpr |
| 8307 | * | MultiplicativeExpr 'div' UnaryExpr |
| 8308 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 8309 | * [34] MultiplyOperator ::= '*' |
| 8310 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8311 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8312 | */ |
| 8313 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8314 | static void |
| 8315 | xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt) { |
| 8316 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8317 | CHECK_ERROR; |
| 8318 | SKIP_BLANKS; |
| 8319 | while ((CUR == '*') || |
| 8320 | ((CUR == 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) || |
| 8321 | ((CUR == 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) { |
| 8322 | int op = -1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8323 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8324 | |
| 8325 | if (CUR == '*') { |
| 8326 | op = 0; |
| 8327 | NEXT; |
| 8328 | } else if (CUR == 'd') { |
| 8329 | op = 1; |
| 8330 | SKIP(3); |
| 8331 | } else if (CUR == 'm') { |
| 8332 | op = 2; |
| 8333 | SKIP(3); |
| 8334 | } |
| 8335 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8336 | xmlXPathCompUnaryExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8337 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8338 | PUSH_BINARY_EXPR(XPATH_OP_MULT, op1, ctxt->comp->last, op, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8339 | SKIP_BLANKS; |
| 8340 | } |
| 8341 | } |
| 8342 | |
| 8343 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8344 | * xmlXPathCompAdditiveExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8345 | * @ctxt: the XPath Parser context |
| 8346 | * |
| 8347 | * [25] AdditiveExpr ::= MultiplicativeExpr |
| 8348 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8349 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8350 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8351 | * Compile an Additive expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8352 | */ |
| 8353 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8354 | static void |
| 8355 | xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8356 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8357 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8358 | CHECK_ERROR; |
| 8359 | SKIP_BLANKS; |
| 8360 | while ((CUR == '+') || (CUR == '-')) { |
| 8361 | int plus; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8362 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8363 | |
| 8364 | if (CUR == '+') plus = 1; |
| 8365 | else plus = 0; |
| 8366 | NEXT; |
| 8367 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8368 | xmlXPathCompMultiplicativeExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8369 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8370 | PUSH_BINARY_EXPR(XPATH_OP_PLUS, op1, ctxt->comp->last, plus, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8371 | SKIP_BLANKS; |
| 8372 | } |
| 8373 | } |
| 8374 | |
| 8375 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8376 | * xmlXPathCompRelationalExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8377 | * @ctxt: the XPath Parser context |
| 8378 | * |
| 8379 | * [24] RelationalExpr ::= AdditiveExpr |
| 8380 | * | RelationalExpr '<' AdditiveExpr |
| 8381 | * | RelationalExpr '>' AdditiveExpr |
| 8382 | * | RelationalExpr '<=' AdditiveExpr |
| 8383 | * | RelationalExpr '>=' AdditiveExpr |
| 8384 | * |
| 8385 | * A <= B > C is allowed ? Answer from James, yes with |
| 8386 | * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr |
| 8387 | * which is basically what got implemented. |
| 8388 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8389 | * Compile a Relational expression, then push the result |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8390 | * on the stack |
| 8391 | */ |
| 8392 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8393 | static void |
| 8394 | xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt) { |
| 8395 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8396 | CHECK_ERROR; |
| 8397 | SKIP_BLANKS; |
| 8398 | while ((CUR == '<') || |
| 8399 | (CUR == '>') || |
| 8400 | ((CUR == '<') && (NXT(1) == '=')) || |
| 8401 | ((CUR == '>') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8402 | int inf, strict; |
| 8403 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8404 | |
| 8405 | if (CUR == '<') inf = 1; |
| 8406 | else inf = 0; |
| 8407 | if (NXT(1) == '=') strict = 0; |
| 8408 | else strict = 1; |
| 8409 | NEXT; |
| 8410 | if (!strict) NEXT; |
| 8411 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8412 | xmlXPathCompAdditiveExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8413 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8414 | PUSH_BINARY_EXPR(XPATH_OP_CMP, op1, ctxt->comp->last, inf, strict); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8415 | SKIP_BLANKS; |
| 8416 | } |
| 8417 | } |
| 8418 | |
| 8419 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8420 | * xmlXPathCompEqualityExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8421 | * @ctxt: the XPath Parser context |
| 8422 | * |
| 8423 | * [23] EqualityExpr ::= RelationalExpr |
| 8424 | * | EqualityExpr '=' RelationalExpr |
| 8425 | * | EqualityExpr '!=' RelationalExpr |
| 8426 | * |
| 8427 | * A != B != C is allowed ? Answer from James, yes with |
| 8428 | * (RelationalExpr = RelationalExpr) = RelationalExpr |
| 8429 | * (RelationalExpr != RelationalExpr) != RelationalExpr |
| 8430 | * which is basically what got implemented. |
| 8431 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8432 | * Compile an Equality expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8433 | * |
| 8434 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8435 | static void |
| 8436 | xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt) { |
| 8437 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8438 | CHECK_ERROR; |
| 8439 | SKIP_BLANKS; |
| 8440 | while ((CUR == '=') || ((CUR == '!') && (NXT(1) == '='))) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8441 | int eq; |
| 8442 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8443 | |
| 8444 | if (CUR == '=') eq = 1; |
| 8445 | else eq = 0; |
| 8446 | NEXT; |
| 8447 | if (!eq) NEXT; |
| 8448 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8449 | xmlXPathCompRelationalExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8450 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8451 | PUSH_BINARY_EXPR(XPATH_OP_EQUAL, op1, ctxt->comp->last, eq, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8452 | SKIP_BLANKS; |
| 8453 | } |
| 8454 | } |
| 8455 | |
| 8456 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8457 | * xmlXPathCompAndExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8458 | * @ctxt: the XPath Parser context |
| 8459 | * |
| 8460 | * [22] AndExpr ::= EqualityExpr |
| 8461 | * | AndExpr 'and' EqualityExpr |
| 8462 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8463 | * Compile an AND expression. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8464 | * |
| 8465 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8466 | static void |
| 8467 | xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt) { |
| 8468 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8469 | CHECK_ERROR; |
| 8470 | SKIP_BLANKS; |
| 8471 | while ((CUR == 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8472 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8473 | SKIP(3); |
| 8474 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8475 | xmlXPathCompEqualityExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8476 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8477 | PUSH_BINARY_EXPR(XPATH_OP_AND, op1, ctxt->comp->last, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8478 | SKIP_BLANKS; |
| 8479 | } |
| 8480 | } |
| 8481 | |
| 8482 | /** |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 8483 | * xmlXPathCompileExpr: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8484 | * @ctxt: the XPath Parser context |
| 8485 | * |
| 8486 | * [14] Expr ::= OrExpr |
| 8487 | * [21] OrExpr ::= AndExpr |
| 8488 | * | OrExpr 'or' AndExpr |
| 8489 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8490 | * Parse and compile an expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8491 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8492 | static void |
| 8493 | xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt) { |
| 8494 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8495 | CHECK_ERROR; |
| 8496 | SKIP_BLANKS; |
| 8497 | while ((CUR == 'o') && (NXT(1) == 'r')) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8498 | int op1 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8499 | SKIP(2); |
| 8500 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8501 | xmlXPathCompAndExpr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8502 | CHECK_ERROR; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8503 | PUSH_BINARY_EXPR(XPATH_OP_OR, op1, ctxt->comp->last, 0, 0); |
| 8504 | op1 = ctxt->comp->nbStep; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8505 | SKIP_BLANKS; |
| 8506 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8507 | if (ctxt->comp->steps[ctxt->comp->last].op != XPATH_OP_VALUE) { |
| 8508 | /* more ops could be optimized too */ |
| 8509 | PUSH_UNARY_EXPR(XPATH_OP_SORT, ctxt->comp->last , 0, 0); |
| 8510 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8511 | } |
| 8512 | |
| 8513 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8514 | * xmlXPathCompPredicate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8515 | * @ctxt: the XPath Parser context |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8516 | * @filter: act as a filter |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8517 | * |
| 8518 | * [8] Predicate ::= '[' PredicateExpr ']' |
| 8519 | * [9] PredicateExpr ::= Expr |
| 8520 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8521 | * Compile a predicate expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8522 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8523 | static void |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8524 | xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt, int filter) { |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8525 | int op1 = ctxt->comp->last; |
| 8526 | |
| 8527 | SKIP_BLANKS; |
| 8528 | if (CUR != '[') { |
| 8529 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8530 | } |
| 8531 | NEXT; |
| 8532 | SKIP_BLANKS; |
| 8533 | |
| 8534 | ctxt->comp->last = -1; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8535 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8536 | CHECK_ERROR; |
| 8537 | |
| 8538 | if (CUR != ']') { |
| 8539 | XP_ERROR(XPATH_INVALID_PREDICATE_ERROR); |
| 8540 | } |
| 8541 | |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8542 | if (filter) |
| 8543 | PUSH_BINARY_EXPR(XPATH_OP_FILTER, op1, ctxt->comp->last, 0, 0); |
| 8544 | else |
| 8545 | PUSH_BINARY_EXPR(XPATH_OP_PREDICATE, op1, ctxt->comp->last, 0, 0); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8546 | |
| 8547 | NEXT; |
| 8548 | SKIP_BLANKS; |
| 8549 | } |
| 8550 | |
| 8551 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8552 | * xmlXPathCompNodeTest: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8553 | * @ctxt: the XPath Parser context |
| 8554 | * @test: pointer to a xmlXPathTestVal |
| 8555 | * @type: pointer to a xmlXPathTypeVal |
| 8556 | * @prefix: placeholder for a possible name prefix |
| 8557 | * |
| 8558 | * [7] NodeTest ::= NameTest |
| 8559 | * | NodeType '(' ')' |
| 8560 | * | 'processing-instruction' '(' Literal ')' |
| 8561 | * |
| 8562 | * [37] NameTest ::= '*' |
| 8563 | * | NCName ':' '*' |
| 8564 | * | QName |
| 8565 | * [38] NodeType ::= 'comment' |
| 8566 | * | 'text' |
| 8567 | * | 'processing-instruction' |
| 8568 | * | 'node' |
| 8569 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 8570 | * Returns the name found and updates @test, @type and @prefix appropriately |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8571 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8572 | static xmlChar * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8573 | xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt, xmlXPathTestVal *test, |
| 8574 | xmlXPathTypeVal *type, const xmlChar **prefix, |
| 8575 | xmlChar *name) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8576 | int blanks; |
| 8577 | |
| 8578 | if ((test == NULL) || (type == NULL) || (prefix == NULL)) { |
| 8579 | STRANGE; |
| 8580 | return(NULL); |
| 8581 | } |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8582 | *type = (xmlXPathTypeVal) 0; |
| 8583 | *test = (xmlXPathTestVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8584 | *prefix = NULL; |
| 8585 | SKIP_BLANKS; |
| 8586 | |
| 8587 | if ((name == NULL) && (CUR == '*')) { |
| 8588 | /* |
| 8589 | * All elements |
| 8590 | */ |
| 8591 | NEXT; |
| 8592 | *test = NODE_TEST_ALL; |
| 8593 | return(NULL); |
| 8594 | } |
| 8595 | |
| 8596 | if (name == NULL) |
| 8597 | name = xmlXPathParseNCName(ctxt); |
| 8598 | if (name == NULL) { |
| 8599 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8600 | } |
| 8601 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8602 | blanks = IS_BLANK_CH(CUR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8603 | SKIP_BLANKS; |
| 8604 | if (CUR == '(') { |
| 8605 | NEXT; |
| 8606 | /* |
| 8607 | * NodeType or PI search |
| 8608 | */ |
| 8609 | if (xmlStrEqual(name, BAD_CAST "comment")) |
| 8610 | *type = NODE_TYPE_COMMENT; |
| 8611 | else if (xmlStrEqual(name, BAD_CAST "node")) |
| 8612 | *type = NODE_TYPE_NODE; |
| 8613 | else if (xmlStrEqual(name, BAD_CAST "processing-instruction")) |
| 8614 | *type = NODE_TYPE_PI; |
| 8615 | else if (xmlStrEqual(name, BAD_CAST "text")) |
| 8616 | *type = NODE_TYPE_TEXT; |
| 8617 | else { |
| 8618 | if (name != NULL) |
| 8619 | xmlFree(name); |
| 8620 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8621 | } |
| 8622 | |
| 8623 | *test = NODE_TEST_TYPE; |
| 8624 | |
| 8625 | SKIP_BLANKS; |
| 8626 | if (*type == NODE_TYPE_PI) { |
| 8627 | /* |
| 8628 | * Specific case: search a PI by name. |
| 8629 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8630 | if (name != NULL) |
| 8631 | xmlFree(name); |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 8632 | name = NULL; |
| 8633 | if (CUR != ')') { |
| 8634 | name = xmlXPathParseLiteral(ctxt); |
| 8635 | CHECK_ERROR 0; |
Daniel Veillard | ed23b7d | 2002-05-27 12:16:02 +0000 | [diff] [blame] | 8636 | *test = NODE_TEST_PI; |
Daniel Veillard | 82e4971 | 2001-04-26 14:38:03 +0000 | [diff] [blame] | 8637 | SKIP_BLANKS; |
| 8638 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8639 | } |
| 8640 | if (CUR != ')') { |
| 8641 | if (name != NULL) |
| 8642 | xmlFree(name); |
| 8643 | XP_ERROR0(XPATH_UNCLOSED_ERROR); |
| 8644 | } |
| 8645 | NEXT; |
| 8646 | return(name); |
| 8647 | } |
| 8648 | *test = NODE_TEST_NAME; |
| 8649 | if ((!blanks) && (CUR == ':')) { |
| 8650 | NEXT; |
| 8651 | |
| 8652 | /* |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8653 | * Since currently the parser context don't have a |
| 8654 | * namespace list associated: |
| 8655 | * The namespace name for this prefix can be computed |
| 8656 | * only at evaluation time. The compilation is done |
| 8657 | * outside of any context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8658 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8659 | #if 0 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8660 | *prefix = xmlXPathNsLookup(ctxt->context, name); |
| 8661 | if (name != NULL) |
| 8662 | xmlFree(name); |
| 8663 | if (*prefix == NULL) { |
| 8664 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 8665 | } |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8666 | #else |
| 8667 | *prefix = name; |
| 8668 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8669 | |
| 8670 | if (CUR == '*') { |
| 8671 | /* |
| 8672 | * All elements |
| 8673 | */ |
| 8674 | NEXT; |
| 8675 | *test = NODE_TEST_ALL; |
| 8676 | return(NULL); |
| 8677 | } |
| 8678 | |
| 8679 | name = xmlXPathParseNCName(ctxt); |
| 8680 | if (name == NULL) { |
| 8681 | XP_ERROR0(XPATH_EXPR_ERROR); |
| 8682 | } |
| 8683 | } |
| 8684 | return(name); |
| 8685 | } |
| 8686 | |
| 8687 | /** |
| 8688 | * xmlXPathIsAxisName: |
| 8689 | * @name: a preparsed name token |
| 8690 | * |
| 8691 | * [6] AxisName ::= 'ancestor' |
| 8692 | * | 'ancestor-or-self' |
| 8693 | * | 'attribute' |
| 8694 | * | 'child' |
| 8695 | * | 'descendant' |
| 8696 | * | 'descendant-or-self' |
| 8697 | * | 'following' |
| 8698 | * | 'following-sibling' |
| 8699 | * | 'namespace' |
| 8700 | * | 'parent' |
| 8701 | * | 'preceding' |
| 8702 | * | 'preceding-sibling' |
| 8703 | * | 'self' |
| 8704 | * |
| 8705 | * Returns the axis or 0 |
| 8706 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8707 | static xmlXPathAxisVal |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8708 | xmlXPathIsAxisName(const xmlChar *name) { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8709 | xmlXPathAxisVal ret = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8710 | switch (name[0]) { |
| 8711 | case 'a': |
| 8712 | if (xmlStrEqual(name, BAD_CAST "ancestor")) |
| 8713 | ret = AXIS_ANCESTOR; |
| 8714 | if (xmlStrEqual(name, BAD_CAST "ancestor-or-self")) |
| 8715 | ret = AXIS_ANCESTOR_OR_SELF; |
| 8716 | if (xmlStrEqual(name, BAD_CAST "attribute")) |
| 8717 | ret = AXIS_ATTRIBUTE; |
| 8718 | break; |
| 8719 | case 'c': |
| 8720 | if (xmlStrEqual(name, BAD_CAST "child")) |
| 8721 | ret = AXIS_CHILD; |
| 8722 | break; |
| 8723 | case 'd': |
| 8724 | if (xmlStrEqual(name, BAD_CAST "descendant")) |
| 8725 | ret = AXIS_DESCENDANT; |
| 8726 | if (xmlStrEqual(name, BAD_CAST "descendant-or-self")) |
| 8727 | ret = AXIS_DESCENDANT_OR_SELF; |
| 8728 | break; |
| 8729 | case 'f': |
| 8730 | if (xmlStrEqual(name, BAD_CAST "following")) |
| 8731 | ret = AXIS_FOLLOWING; |
| 8732 | if (xmlStrEqual(name, BAD_CAST "following-sibling")) |
| 8733 | ret = AXIS_FOLLOWING_SIBLING; |
| 8734 | break; |
| 8735 | case 'n': |
| 8736 | if (xmlStrEqual(name, BAD_CAST "namespace")) |
| 8737 | ret = AXIS_NAMESPACE; |
| 8738 | break; |
| 8739 | case 'p': |
| 8740 | if (xmlStrEqual(name, BAD_CAST "parent")) |
| 8741 | ret = AXIS_PARENT; |
| 8742 | if (xmlStrEqual(name, BAD_CAST "preceding")) |
| 8743 | ret = AXIS_PRECEDING; |
| 8744 | if (xmlStrEqual(name, BAD_CAST "preceding-sibling")) |
| 8745 | ret = AXIS_PRECEDING_SIBLING; |
| 8746 | break; |
| 8747 | case 's': |
| 8748 | if (xmlStrEqual(name, BAD_CAST "self")) |
| 8749 | ret = AXIS_SELF; |
| 8750 | break; |
| 8751 | } |
| 8752 | return(ret); |
| 8753 | } |
| 8754 | |
| 8755 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8756 | * xmlXPathCompStep: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8757 | * @ctxt: the XPath Parser context |
| 8758 | * |
| 8759 | * [4] Step ::= AxisSpecifier NodeTest Predicate* |
| 8760 | * | AbbreviatedStep |
| 8761 | * |
| 8762 | * [12] AbbreviatedStep ::= '.' | '..' |
| 8763 | * |
| 8764 | * [5] AxisSpecifier ::= AxisName '::' |
| 8765 | * | AbbreviatedAxisSpecifier |
| 8766 | * |
| 8767 | * [13] AbbreviatedAxisSpecifier ::= '@'? |
| 8768 | * |
| 8769 | * Modified for XPtr range support as: |
| 8770 | * |
| 8771 | * [4xptr] Step ::= AxisSpecifier NodeTest Predicate* |
| 8772 | * | AbbreviatedStep |
| 8773 | * | 'range-to' '(' Expr ')' Predicate* |
| 8774 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8775 | * Compile one step in a Location Path |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8776 | * A location step of . is short for self::node(). This is |
| 8777 | * particularly useful in conjunction with //. For example, the |
| 8778 | * location path .//para is short for |
| 8779 | * self::node()/descendant-or-self::node()/child::para |
| 8780 | * and so will select all para descendant elements of the context |
| 8781 | * node. |
| 8782 | * Similarly, a location step of .. is short for parent::node(). |
| 8783 | * For example, ../title is short for parent::node()/child::title |
| 8784 | * and so will select the title children of the parent of the context |
| 8785 | * node. |
| 8786 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8787 | static void |
| 8788 | xmlXPathCompStep(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8789 | #ifdef LIBXML_XPTR_ENABLED |
| 8790 | int rangeto = 0; |
| 8791 | int op2 = -1; |
| 8792 | #endif |
| 8793 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8794 | SKIP_BLANKS; |
| 8795 | if ((CUR == '.') && (NXT(1) == '.')) { |
| 8796 | SKIP(2); |
| 8797 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8798 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_PARENT, |
| 8799 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8800 | } else if (CUR == '.') { |
| 8801 | NEXT; |
| 8802 | SKIP_BLANKS; |
| 8803 | } else { |
| 8804 | xmlChar *name = NULL; |
| 8805 | const xmlChar *prefix = NULL; |
| 8806 | xmlXPathTestVal test; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 8807 | xmlXPathAxisVal axis = (xmlXPathAxisVal) 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8808 | xmlXPathTypeVal type; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8809 | int op1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8810 | |
| 8811 | /* |
| 8812 | * The modification needed for XPointer change to the production |
| 8813 | */ |
| 8814 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 8815 | if (ctxt->xptr) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8816 | name = xmlXPathParseNCName(ctxt); |
| 8817 | if ((name != NULL) && (xmlStrEqual(name, BAD_CAST "range-to"))) { |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8818 | op2 = ctxt->comp->last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8819 | xmlFree(name); |
| 8820 | SKIP_BLANKS; |
| 8821 | if (CUR != '(') { |
| 8822 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8823 | } |
| 8824 | NEXT; |
| 8825 | SKIP_BLANKS; |
| 8826 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8827 | xmlXPathCompileExpr(ctxt); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8828 | /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8829 | CHECK_ERROR; |
| 8830 | |
| 8831 | SKIP_BLANKS; |
| 8832 | if (CUR != ')') { |
| 8833 | XP_ERROR(XPATH_EXPR_ERROR); |
| 8834 | } |
| 8835 | NEXT; |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8836 | rangeto = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8837 | goto eval_predicates; |
| 8838 | } |
| 8839 | } |
| 8840 | #endif |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8841 | if (CUR == '*') { |
| 8842 | axis = AXIS_CHILD; |
| 8843 | } else { |
| 8844 | if (name == NULL) |
| 8845 | name = xmlXPathParseNCName(ctxt); |
| 8846 | if (name != NULL) { |
| 8847 | axis = xmlXPathIsAxisName(name); |
| 8848 | if (axis != 0) { |
| 8849 | SKIP_BLANKS; |
| 8850 | if ((CUR == ':') && (NXT(1) == ':')) { |
| 8851 | SKIP(2); |
| 8852 | xmlFree(name); |
| 8853 | name = NULL; |
| 8854 | } else { |
| 8855 | /* an element name can conflict with an axis one :-\ */ |
| 8856 | axis = AXIS_CHILD; |
| 8857 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8858 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8859 | axis = AXIS_CHILD; |
| 8860 | } |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8861 | } else if (CUR == '@') { |
| 8862 | NEXT; |
| 8863 | axis = AXIS_ATTRIBUTE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8864 | } else { |
Daniel Veillard | 2156a56 | 2001-04-28 12:24:34 +0000 | [diff] [blame] | 8865 | axis = AXIS_CHILD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8866 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8867 | } |
| 8868 | |
| 8869 | CHECK_ERROR; |
| 8870 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8871 | name = xmlXPathCompNodeTest(ctxt, &test, &type, &prefix, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8872 | if (test == 0) |
| 8873 | return; |
| 8874 | |
| 8875 | #ifdef DEBUG_STEP |
| 8876 | xmlGenericError(xmlGenericErrorContext, |
| 8877 | "Basis : computing new set\n"); |
| 8878 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8879 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8880 | #ifdef DEBUG_STEP |
| 8881 | xmlGenericError(xmlGenericErrorContext, "Basis : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8882 | if (ctxt->value == NULL) |
| 8883 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 8884 | else if (ctxt->value->nodesetval == NULL) |
| 8885 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 8886 | else |
| 8887 | xmlGenericErrorContextNodeSet(stdout, ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8888 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8889 | |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 8890 | #ifdef LIBXML_XPTR_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8891 | eval_predicates: |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 8892 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8893 | op1 = ctxt->comp->last; |
| 8894 | ctxt->comp->last = -1; |
| 8895 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8896 | SKIP_BLANKS; |
| 8897 | while (CUR == '[') { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8898 | xmlXPathCompPredicate(ctxt, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8899 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8900 | |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8901 | #ifdef LIBXML_XPTR_ENABLED |
| 8902 | if (rangeto) { |
| 8903 | PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, op1, 0, 0); |
| 8904 | } else |
| 8905 | #endif |
| 8906 | PUSH_FULL_EXPR(XPATH_OP_COLLECT, op1, ctxt->comp->last, axis, |
| 8907 | test, type, (void *)prefix, (void *)name); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 8908 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8909 | } |
| 8910 | #ifdef DEBUG_STEP |
| 8911 | xmlGenericError(xmlGenericErrorContext, "Step : "); |
Daniel Veillard | fd0c3eb | 2001-04-22 19:13:10 +0000 | [diff] [blame] | 8912 | if (ctxt->value == NULL) |
| 8913 | xmlGenericError(xmlGenericErrorContext, "no value\n"); |
| 8914 | else if (ctxt->value->nodesetval == NULL) |
| 8915 | xmlGenericError(xmlGenericErrorContext, "Empty\n"); |
| 8916 | else |
| 8917 | xmlGenericErrorContextNodeSet(xmlGenericErrorContext, |
| 8918 | ctxt->value->nodesetval); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8919 | #endif |
| 8920 | } |
| 8921 | |
| 8922 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8923 | * xmlXPathCompRelativeLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8924 | * @ctxt: the XPath Parser context |
| 8925 | * |
| 8926 | * [3] RelativeLocationPath ::= Step |
| 8927 | * | RelativeLocationPath '/' Step |
| 8928 | * | AbbreviatedRelativeLocationPath |
| 8929 | * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step |
| 8930 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8931 | * Compile a relative location path. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8932 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8933 | static void |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8934 | xmlXPathCompRelativeLocationPath |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8935 | (xmlXPathParserContextPtr ctxt) { |
| 8936 | SKIP_BLANKS; |
| 8937 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8938 | SKIP(2); |
| 8939 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8940 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8941 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8942 | } else if (CUR == '/') { |
| 8943 | NEXT; |
| 8944 | SKIP_BLANKS; |
| 8945 | } |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8946 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8947 | SKIP_BLANKS; |
| 8948 | while (CUR == '/') { |
| 8949 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8950 | SKIP(2); |
| 8951 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8952 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8953 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8954 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8955 | } else if (CUR == '/') { |
| 8956 | NEXT; |
| 8957 | SKIP_BLANKS; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8958 | xmlXPathCompStep(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8959 | } |
| 8960 | SKIP_BLANKS; |
| 8961 | } |
| 8962 | } |
| 8963 | |
| 8964 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8965 | * xmlXPathCompLocationPath: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8966 | * @ctxt: the XPath Parser context |
| 8967 | * |
| 8968 | * [1] LocationPath ::= RelativeLocationPath |
| 8969 | * | AbsoluteLocationPath |
| 8970 | * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath? |
| 8971 | * | AbbreviatedAbsoluteLocationPath |
| 8972 | * [10] AbbreviatedAbsoluteLocationPath ::= |
| 8973 | * '//' RelativeLocationPath |
| 8974 | * |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8975 | * Compile a location path |
| 8976 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8977 | * // is short for /descendant-or-self::node()/. For example, |
| 8978 | * //para is short for /descendant-or-self::node()/child::para and |
| 8979 | * so will select any para element in the document (even a para element |
| 8980 | * that is a document element will be selected by //para since the |
| 8981 | * document element node is a child of the root node); div//para is |
| 8982 | * short for div/descendant-or-self::node()/child::para and so will |
| 8983 | * select all para descendants of div children. |
| 8984 | */ |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8985 | static void |
| 8986 | xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8987 | SKIP_BLANKS; |
| 8988 | if (CUR != '/') { |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8989 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8990 | } else { |
| 8991 | while (CUR == '/') { |
| 8992 | if ((CUR == '/') && (NXT(1) == '/')) { |
| 8993 | SKIP(2); |
| 8994 | SKIP_BLANKS; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 8995 | PUSH_LONG_EXPR(XPATH_OP_COLLECT, AXIS_DESCENDANT_OR_SELF, |
| 8996 | NODE_TEST_TYPE, NODE_TYPE_NODE, NULL, NULL); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 8997 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8998 | } else if (CUR == '/') { |
| 8999 | NEXT; |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 9000 | SKIP_BLANKS; |
| 9001 | if ((CUR != 0 ) && |
William M. Brack | d1757ab | 2004-10-02 22:07:48 +0000 | [diff] [blame] | 9002 | ((IS_ASCII_LETTER(CUR)) || (CUR == '_') || (CUR == '.') || |
Daniel Veillard | 608ad07 | 2001-06-14 08:32:28 +0000 | [diff] [blame] | 9003 | (CUR == '@') || (CUR == '*'))) |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 9004 | xmlXPathCompRelativeLocationPath(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9005 | } |
| 9006 | } |
| 9007 | } |
| 9008 | } |
| 9009 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 9010 | /************************************************************************ |
| 9011 | * * |
| 9012 | * XPath precompiled expression evaluation * |
| 9013 | * * |
| 9014 | ************************************************************************/ |
| 9015 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9016 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9017 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op); |
| 9018 | |
| 9019 | /** |
| 9020 | * xmlXPathNodeCollectAndTest: |
| 9021 | * @ctxt: the XPath Parser context |
| 9022 | * @op: the XPath precompiled step operation |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9023 | * @first: pointer to the first element in document order |
| 9024 | * @last: pointer to the last element in document order |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9025 | * |
| 9026 | * This is the function implementing a step: based on the current list |
| 9027 | * of nodes, it builds up a new list, looking at all nodes under that |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9028 | * axis and selecting them. It also does the predicate filtering |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9029 | * |
| 9030 | * Pushes the new NodeSet resulting from the search. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9031 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9032 | * Returns the number of nodes traversed |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9033 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9034 | static int |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9035 | xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9036 | xmlXPathStepOpPtr op, |
| 9037 | xmlNodePtr * first, xmlNodePtr * last) |
| 9038 | { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9039 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 9040 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 9041 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9042 | const xmlChar *prefix = op->value4; |
| 9043 | const xmlChar *name = op->value5; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9044 | const xmlChar *URI = NULL; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9045 | |
| 9046 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9047 | int n = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9048 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9049 | int i, t = 0; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9050 | xmlNodeSetPtr ret, list; |
| 9051 | xmlXPathTraversalFunction next = NULL; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9052 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9053 | xmlNodeSetPtr (*mergeNodeSet) (xmlNodeSetPtr, xmlNodeSetPtr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9054 | xmlNodePtr cur = NULL; |
| 9055 | xmlXPathObjectPtr obj; |
| 9056 | xmlNodeSetPtr nodelist; |
| 9057 | xmlNodePtr tmp; |
| 9058 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9059 | CHECK_TYPE0(XPATH_NODESET); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9060 | obj = valuePop(ctxt); |
| 9061 | addNode = xmlXPathNodeSetAdd; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9062 | mergeNodeSet = xmlXPathNodeSetMerge; |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9063 | if (prefix != NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9064 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
| 9065 | if (URI == NULL) |
| 9066 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
Daniel Veillard | e043ee1 | 2001-04-16 14:08:07 +0000 | [diff] [blame] | 9067 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9068 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9069 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9070 | #endif |
| 9071 | switch (axis) { |
| 9072 | case AXIS_ANCESTOR: |
| 9073 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9074 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9075 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9076 | first = NULL; |
| 9077 | next = xmlXPathNextAncestor; |
| 9078 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9079 | case AXIS_ANCESTOR_OR_SELF: |
| 9080 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9081 | xmlGenericError(xmlGenericErrorContext, |
| 9082 | "axis 'ancestors-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9083 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9084 | first = NULL; |
| 9085 | next = xmlXPathNextAncestorOrSelf; |
| 9086 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9087 | case AXIS_ATTRIBUTE: |
| 9088 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9089 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9090 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9091 | first = NULL; |
| 9092 | last = NULL; |
| 9093 | next = xmlXPathNextAttribute; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9094 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9095 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9096 | case AXIS_CHILD: |
| 9097 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9098 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9099 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9100 | last = NULL; |
| 9101 | next = xmlXPathNextChild; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9102 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9103 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9104 | case AXIS_DESCENDANT: |
| 9105 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9106 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9107 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9108 | last = NULL; |
| 9109 | next = xmlXPathNextDescendant; |
| 9110 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9111 | case AXIS_DESCENDANT_OR_SELF: |
| 9112 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9113 | xmlGenericError(xmlGenericErrorContext, |
| 9114 | "axis 'descendant-or-self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9115 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9116 | last = NULL; |
| 9117 | next = xmlXPathNextDescendantOrSelf; |
| 9118 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9119 | case AXIS_FOLLOWING: |
| 9120 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9121 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9122 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9123 | last = NULL; |
| 9124 | next = xmlXPathNextFollowing; |
| 9125 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9126 | case AXIS_FOLLOWING_SIBLING: |
| 9127 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9128 | xmlGenericError(xmlGenericErrorContext, |
| 9129 | "axis 'following-siblings' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9130 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9131 | last = NULL; |
| 9132 | next = xmlXPathNextFollowingSibling; |
| 9133 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9134 | case AXIS_NAMESPACE: |
| 9135 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9136 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9137 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9138 | first = NULL; |
| 9139 | last = NULL; |
| 9140 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9141 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9142 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9143 | case AXIS_PARENT: |
| 9144 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9145 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9146 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9147 | first = NULL; |
| 9148 | next = xmlXPathNextParent; |
| 9149 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9150 | case AXIS_PRECEDING: |
| 9151 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9152 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9153 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9154 | first = NULL; |
| 9155 | next = xmlXPathNextPrecedingInternal; |
| 9156 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9157 | case AXIS_PRECEDING_SIBLING: |
| 9158 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9159 | xmlGenericError(xmlGenericErrorContext, |
| 9160 | "axis 'preceding-sibling' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9161 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9162 | first = NULL; |
| 9163 | next = xmlXPathNextPrecedingSibling; |
| 9164 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9165 | case AXIS_SELF: |
| 9166 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9167 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9168 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9169 | first = NULL; |
| 9170 | last = NULL; |
| 9171 | next = xmlXPathNextSelf; |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9172 | mergeNodeSet = xmlXPathNodeSetMergeUnique; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9173 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9174 | } |
| 9175 | if (next == NULL) |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9176 | return(0); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9177 | |
| 9178 | nodelist = obj->nodesetval; |
| 9179 | if (nodelist == NULL) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9180 | xmlXPathFreeObject(obj); |
| 9181 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 9182 | return(0); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9183 | } |
| 9184 | addNode = xmlXPathNodeSetAddUnique; |
| 9185 | ret = NULL; |
| 9186 | #ifdef DEBUG_STEP |
| 9187 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9188 | " context contains %d nodes\n", nodelist->nodeNr); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9189 | switch (test) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9190 | case NODE_TEST_NONE: |
| 9191 | xmlGenericError(xmlGenericErrorContext, |
| 9192 | " searching for none !!!\n"); |
| 9193 | break; |
| 9194 | case NODE_TEST_TYPE: |
| 9195 | xmlGenericError(xmlGenericErrorContext, |
| 9196 | " searching for type %d\n", type); |
| 9197 | break; |
| 9198 | case NODE_TEST_PI: |
| 9199 | xmlGenericError(xmlGenericErrorContext, |
| 9200 | " searching for PI !!!\n"); |
| 9201 | break; |
| 9202 | case NODE_TEST_ALL: |
| 9203 | xmlGenericError(xmlGenericErrorContext, |
| 9204 | " searching for *\n"); |
| 9205 | break; |
| 9206 | case NODE_TEST_NS: |
| 9207 | xmlGenericError(xmlGenericErrorContext, |
| 9208 | " searching for namespace %s\n", |
| 9209 | prefix); |
| 9210 | break; |
| 9211 | case NODE_TEST_NAME: |
| 9212 | xmlGenericError(xmlGenericErrorContext, |
| 9213 | " searching for name %s\n", name); |
| 9214 | if (prefix != NULL) |
| 9215 | xmlGenericError(xmlGenericErrorContext, |
| 9216 | " with namespace %s\n", prefix); |
| 9217 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9218 | } |
| 9219 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 9220 | #endif |
| 9221 | /* |
| 9222 | * 2.3 Node Tests |
| 9223 | * - For the attribute axis, the principal node type is attribute. |
| 9224 | * - For the namespace axis, the principal node type is namespace. |
| 9225 | * - For other axes, the principal node type is element. |
| 9226 | * |
| 9227 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9228 | * principal node type. For example, child::* will |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9229 | * select all element children of the context node |
| 9230 | */ |
| 9231 | tmp = ctxt->context->node; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9232 | for (i = 0; i < nodelist->nodeNr; i++) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9233 | ctxt->context->node = nodelist->nodeTab[i]; |
| 9234 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9235 | cur = NULL; |
| 9236 | list = xmlXPathNodeSetCreate(NULL); |
| 9237 | do { |
| 9238 | cur = next(ctxt, cur); |
| 9239 | if (cur == NULL) |
| 9240 | break; |
| 9241 | if ((first != NULL) && (*first == cur)) |
| 9242 | break; |
| 9243 | if (((t % 256) == 0) && |
| 9244 | (first != NULL) && (*first != NULL) && |
| 9245 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 9246 | break; |
| 9247 | if ((last != NULL) && (*last == cur)) |
| 9248 | break; |
| 9249 | if (((t % 256) == 0) && |
| 9250 | (last != NULL) && (*last != NULL) && |
| 9251 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 9252 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9253 | t++; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9254 | #ifdef DEBUG_STEP |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9255 | xmlGenericError(xmlGenericErrorContext, " %s", cur->name); |
| 9256 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9257 | switch (test) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9258 | case NODE_TEST_NONE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9259 | ctxt->context->node = tmp; |
| 9260 | STRANGE return(t); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9261 | case NODE_TEST_TYPE: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9262 | if ((cur->type == type) || |
| 9263 | ((type == NODE_TYPE_NODE) && |
| 9264 | ((cur->type == XML_DOCUMENT_NODE) || |
| 9265 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 9266 | (cur->type == XML_ELEMENT_NODE) || |
Aleksey Sanin | f8cb6dd | 2002-06-04 04:27:06 +0000 | [diff] [blame] | 9267 | (cur->type == XML_NAMESPACE_DECL) || |
| 9268 | (cur->type == XML_ATTRIBUTE_NODE) || |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9269 | (cur->type == XML_PI_NODE) || |
| 9270 | (cur->type == XML_COMMENT_NODE) || |
| 9271 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9272 | (cur->type == XML_TEXT_NODE))) || |
| 9273 | ((type == NODE_TYPE_TEXT) && |
| 9274 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9275 | #ifdef DEBUG_STEP |
| 9276 | n++; |
| 9277 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9278 | addNode(list, cur); |
| 9279 | } |
| 9280 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9281 | case NODE_TEST_PI: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9282 | if (cur->type == XML_PI_NODE) { |
| 9283 | if ((name != NULL) && |
| 9284 | (!xmlStrEqual(name, cur->name))) |
| 9285 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9286 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9287 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9288 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9289 | addNode(list, cur); |
| 9290 | } |
| 9291 | break; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9292 | case NODE_TEST_ALL: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9293 | if (axis == AXIS_ATTRIBUTE) { |
| 9294 | if (cur->type == XML_ATTRIBUTE_NODE) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9295 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9296 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9297 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9298 | addNode(list, cur); |
| 9299 | } |
| 9300 | } else if (axis == AXIS_NAMESPACE) { |
| 9301 | if (cur->type == XML_NAMESPACE_DECL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9302 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9303 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9304 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9305 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 9306 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9307 | } |
| 9308 | } else { |
| 9309 | if (cur->type == XML_ELEMENT_NODE) { |
| 9310 | if (prefix == NULL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9311 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9312 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9313 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9314 | addNode(list, cur); |
| 9315 | } else if ((cur->ns != NULL) && |
| 9316 | (xmlStrEqual(URI, cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9317 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9318 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9319 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9320 | addNode(list, cur); |
| 9321 | } |
| 9322 | } |
| 9323 | } |
| 9324 | break; |
| 9325 | case NODE_TEST_NS:{ |
| 9326 | TODO; |
| 9327 | break; |
| 9328 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9329 | case NODE_TEST_NAME: |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9330 | switch (cur->type) { |
| 9331 | case XML_ELEMENT_NODE: |
| 9332 | if (xmlStrEqual(name, cur->name)) { |
| 9333 | if (prefix == NULL) { |
| 9334 | if (cur->ns == NULL) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9335 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9336 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9337 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9338 | addNode(list, cur); |
| 9339 | } |
| 9340 | } else { |
| 9341 | if ((cur->ns != NULL) && |
| 9342 | (xmlStrEqual(URI, |
| 9343 | cur->ns->href))) { |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9344 | #ifdef DEBUG_STEP |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9345 | n++; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9346 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9347 | addNode(list, cur); |
| 9348 | } |
| 9349 | } |
| 9350 | } |
| 9351 | break; |
| 9352 | case XML_ATTRIBUTE_NODE:{ |
| 9353 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9354 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9355 | if (xmlStrEqual(name, attr->name)) { |
| 9356 | if (prefix == NULL) { |
| 9357 | if ((attr->ns == NULL) || |
| 9358 | (attr->ns->prefix == NULL)) { |
| 9359 | #ifdef DEBUG_STEP |
| 9360 | n++; |
| 9361 | #endif |
| 9362 | addNode(list, |
| 9363 | (xmlNodePtr) attr); |
| 9364 | } |
| 9365 | } else { |
| 9366 | if ((attr->ns != NULL) && |
| 9367 | (xmlStrEqual(URI, |
| 9368 | attr->ns-> |
| 9369 | href))) { |
| 9370 | #ifdef DEBUG_STEP |
| 9371 | n++; |
| 9372 | #endif |
| 9373 | addNode(list, |
| 9374 | (xmlNodePtr) attr); |
| 9375 | } |
| 9376 | } |
| 9377 | } |
| 9378 | break; |
| 9379 | } |
| 9380 | case XML_NAMESPACE_DECL: |
| 9381 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9382 | xmlNsPtr ns = (xmlNsPtr) cur; |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9383 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9384 | if ((ns->prefix != NULL) && (name != NULL) |
| 9385 | && (xmlStrEqual(ns->prefix, name))) { |
| 9386 | #ifdef DEBUG_STEP |
| 9387 | n++; |
| 9388 | #endif |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9389 | xmlXPathNodeSetAddNs(list, |
| 9390 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9391 | } |
| 9392 | } |
| 9393 | break; |
| 9394 | default: |
| 9395 | break; |
| 9396 | } |
| 9397 | break; |
| 9398 | break; |
| 9399 | } |
| 9400 | } while (cur != NULL); |
| 9401 | |
| 9402 | /* |
| 9403 | * If there is some predicate filtering do it now |
| 9404 | */ |
Daniel Veillard | 6fbcf42 | 2002-03-21 12:32:59 +0000 | [diff] [blame] | 9405 | if ((op->ch2 != -1) && (list != NULL) && (list->nodeNr > 0)) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9406 | xmlXPathObjectPtr obj2; |
| 9407 | |
| 9408 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
| 9409 | xmlXPathCompOpEval(ctxt, &ctxt->comp->steps[op->ch2]); |
| 9410 | CHECK_TYPE0(XPATH_NODESET); |
| 9411 | obj2 = valuePop(ctxt); |
| 9412 | list = obj2->nodesetval; |
| 9413 | obj2->nodesetval = NULL; |
| 9414 | xmlXPathFreeObject(obj2); |
| 9415 | } |
| 9416 | if (ret == NULL) { |
| 9417 | ret = list; |
| 9418 | } else { |
Daniel Veillard | 75be013 | 2002-03-13 10:03:35 +0000 | [diff] [blame] | 9419 | ret = mergeNodeSet(ret, list); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9420 | xmlXPathFreeNodeSet(list); |
| 9421 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9422 | } |
| 9423 | ctxt->context->node = tmp; |
| 9424 | #ifdef DEBUG_STEP |
| 9425 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9426 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 9427 | t, n); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9428 | #endif |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 9429 | valuePush(ctxt, xmlXPathWrapNodeSet(ret)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 9430 | if ((obj->boolval) && (obj->user != NULL)) { |
| 9431 | ctxt->value->boolval = 1; |
| 9432 | ctxt->value->user = obj->user; |
| 9433 | obj->user = NULL; |
| 9434 | obj->boolval = 0; |
| 9435 | } |
| 9436 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9437 | return(t); |
| 9438 | } |
| 9439 | |
| 9440 | /** |
| 9441 | * xmlXPathNodeCollectAndTestNth: |
| 9442 | * @ctxt: the XPath Parser context |
| 9443 | * @op: the XPath precompiled step operation |
| 9444 | * @indx: the index to collect |
| 9445 | * @first: pointer to the first element in document order |
| 9446 | * @last: pointer to the last element in document order |
| 9447 | * |
| 9448 | * This is the function implementing a step: based on the current list |
| 9449 | * of nodes, it builds up a new list, looking at all nodes under that |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9450 | * axis and selecting them. It also does the predicate filtering |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9451 | * |
| 9452 | * Pushes the new NodeSet resulting from the search. |
| 9453 | * Returns the number of node traversed |
| 9454 | */ |
| 9455 | static int |
| 9456 | xmlXPathNodeCollectAndTestNth(xmlXPathParserContextPtr ctxt, |
| 9457 | xmlXPathStepOpPtr op, int indx, |
| 9458 | xmlNodePtr * first, xmlNodePtr * last) |
| 9459 | { |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 9460 | xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; |
| 9461 | xmlXPathTestVal test = (xmlXPathTestVal) op->value2; |
| 9462 | xmlXPathTypeVal type = (xmlXPathTypeVal) op->value3; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9463 | const xmlChar *prefix = op->value4; |
| 9464 | const xmlChar *name = op->value5; |
| 9465 | const xmlChar *URI = NULL; |
| 9466 | int n = 0, t = 0; |
| 9467 | |
| 9468 | int i; |
| 9469 | xmlNodeSetPtr list; |
| 9470 | xmlXPathTraversalFunction next = NULL; |
| 9471 | void (*addNode) (xmlNodeSetPtr, xmlNodePtr); |
| 9472 | xmlNodePtr cur = NULL; |
| 9473 | xmlXPathObjectPtr obj; |
| 9474 | xmlNodeSetPtr nodelist; |
| 9475 | xmlNodePtr tmp; |
| 9476 | |
| 9477 | CHECK_TYPE0(XPATH_NODESET); |
| 9478 | obj = valuePop(ctxt); |
| 9479 | addNode = xmlXPathNodeSetAdd; |
| 9480 | if (prefix != NULL) { |
| 9481 | URI = xmlXPathNsLookup(ctxt->context, prefix); |
| 9482 | if (URI == NULL) |
| 9483 | XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR); |
| 9484 | } |
| 9485 | #ifdef DEBUG_STEP_NTH |
| 9486 | xmlGenericError(xmlGenericErrorContext, "new step : "); |
| 9487 | if (first != NULL) { |
| 9488 | if (*first != NULL) |
| 9489 | xmlGenericError(xmlGenericErrorContext, "first = %s ", |
| 9490 | (*first)->name); |
| 9491 | else |
| 9492 | xmlGenericError(xmlGenericErrorContext, "first = NULL "); |
| 9493 | } |
| 9494 | if (last != NULL) { |
| 9495 | if (*last != NULL) |
| 9496 | xmlGenericError(xmlGenericErrorContext, "last = %s ", |
| 9497 | (*last)->name); |
| 9498 | else |
| 9499 | xmlGenericError(xmlGenericErrorContext, "last = NULL "); |
| 9500 | } |
| 9501 | #endif |
| 9502 | switch (axis) { |
| 9503 | case AXIS_ANCESTOR: |
| 9504 | #ifdef DEBUG_STEP_NTH |
| 9505 | xmlGenericError(xmlGenericErrorContext, "axis 'ancestors' "); |
| 9506 | #endif |
| 9507 | first = NULL; |
| 9508 | next = xmlXPathNextAncestor; |
| 9509 | break; |
| 9510 | case AXIS_ANCESTOR_OR_SELF: |
| 9511 | #ifdef DEBUG_STEP_NTH |
| 9512 | xmlGenericError(xmlGenericErrorContext, |
| 9513 | "axis 'ancestors-or-self' "); |
| 9514 | #endif |
| 9515 | first = NULL; |
| 9516 | next = xmlXPathNextAncestorOrSelf; |
| 9517 | break; |
| 9518 | case AXIS_ATTRIBUTE: |
| 9519 | #ifdef DEBUG_STEP_NTH |
| 9520 | xmlGenericError(xmlGenericErrorContext, "axis 'attributes' "); |
| 9521 | #endif |
| 9522 | first = NULL; |
| 9523 | last = NULL; |
| 9524 | next = xmlXPathNextAttribute; |
| 9525 | break; |
| 9526 | case AXIS_CHILD: |
| 9527 | #ifdef DEBUG_STEP_NTH |
| 9528 | xmlGenericError(xmlGenericErrorContext, "axis 'child' "); |
| 9529 | #endif |
| 9530 | last = NULL; |
| 9531 | next = xmlXPathNextChild; |
| 9532 | break; |
| 9533 | case AXIS_DESCENDANT: |
| 9534 | #ifdef DEBUG_STEP_NTH |
| 9535 | xmlGenericError(xmlGenericErrorContext, "axis 'descendant' "); |
| 9536 | #endif |
| 9537 | last = NULL; |
| 9538 | next = xmlXPathNextDescendant; |
| 9539 | break; |
| 9540 | case AXIS_DESCENDANT_OR_SELF: |
| 9541 | #ifdef DEBUG_STEP_NTH |
| 9542 | xmlGenericError(xmlGenericErrorContext, |
| 9543 | "axis 'descendant-or-self' "); |
| 9544 | #endif |
| 9545 | last = NULL; |
| 9546 | next = xmlXPathNextDescendantOrSelf; |
| 9547 | break; |
| 9548 | case AXIS_FOLLOWING: |
| 9549 | #ifdef DEBUG_STEP_NTH |
| 9550 | xmlGenericError(xmlGenericErrorContext, "axis 'following' "); |
| 9551 | #endif |
| 9552 | last = NULL; |
| 9553 | next = xmlXPathNextFollowing; |
| 9554 | break; |
| 9555 | case AXIS_FOLLOWING_SIBLING: |
| 9556 | #ifdef DEBUG_STEP_NTH |
| 9557 | xmlGenericError(xmlGenericErrorContext, |
| 9558 | "axis 'following-siblings' "); |
| 9559 | #endif |
| 9560 | last = NULL; |
| 9561 | next = xmlXPathNextFollowingSibling; |
| 9562 | break; |
| 9563 | case AXIS_NAMESPACE: |
| 9564 | #ifdef DEBUG_STEP_NTH |
| 9565 | xmlGenericError(xmlGenericErrorContext, "axis 'namespace' "); |
| 9566 | #endif |
| 9567 | last = NULL; |
| 9568 | first = NULL; |
| 9569 | next = (xmlXPathTraversalFunction) xmlXPathNextNamespace; |
| 9570 | break; |
| 9571 | case AXIS_PARENT: |
| 9572 | #ifdef DEBUG_STEP_NTH |
| 9573 | xmlGenericError(xmlGenericErrorContext, "axis 'parent' "); |
| 9574 | #endif |
| 9575 | first = NULL; |
| 9576 | next = xmlXPathNextParent; |
| 9577 | break; |
| 9578 | case AXIS_PRECEDING: |
| 9579 | #ifdef DEBUG_STEP_NTH |
| 9580 | xmlGenericError(xmlGenericErrorContext, "axis 'preceding' "); |
| 9581 | #endif |
| 9582 | first = NULL; |
| 9583 | next = xmlXPathNextPrecedingInternal; |
| 9584 | break; |
| 9585 | case AXIS_PRECEDING_SIBLING: |
| 9586 | #ifdef DEBUG_STEP_NTH |
| 9587 | xmlGenericError(xmlGenericErrorContext, |
| 9588 | "axis 'preceding-sibling' "); |
| 9589 | #endif |
| 9590 | first = NULL; |
| 9591 | next = xmlXPathNextPrecedingSibling; |
| 9592 | break; |
| 9593 | case AXIS_SELF: |
| 9594 | #ifdef DEBUG_STEP_NTH |
| 9595 | xmlGenericError(xmlGenericErrorContext, "axis 'self' "); |
| 9596 | #endif |
| 9597 | first = NULL; |
| 9598 | last = NULL; |
| 9599 | next = xmlXPathNextSelf; |
| 9600 | break; |
| 9601 | } |
| 9602 | if (next == NULL) |
| 9603 | return(0); |
| 9604 | |
| 9605 | nodelist = obj->nodesetval; |
| 9606 | if (nodelist == NULL) { |
| 9607 | xmlXPathFreeObject(obj); |
| 9608 | valuePush(ctxt, xmlXPathWrapNodeSet(NULL)); |
| 9609 | return(0); |
| 9610 | } |
| 9611 | addNode = xmlXPathNodeSetAddUnique; |
| 9612 | #ifdef DEBUG_STEP_NTH |
| 9613 | xmlGenericError(xmlGenericErrorContext, |
| 9614 | " context contains %d nodes\n", nodelist->nodeNr); |
| 9615 | switch (test) { |
| 9616 | case NODE_TEST_NONE: |
| 9617 | xmlGenericError(xmlGenericErrorContext, |
| 9618 | " searching for none !!!\n"); |
| 9619 | break; |
| 9620 | case NODE_TEST_TYPE: |
| 9621 | xmlGenericError(xmlGenericErrorContext, |
| 9622 | " searching for type %d\n", type); |
| 9623 | break; |
| 9624 | case NODE_TEST_PI: |
| 9625 | xmlGenericError(xmlGenericErrorContext, |
| 9626 | " searching for PI !!!\n"); |
| 9627 | break; |
| 9628 | case NODE_TEST_ALL: |
| 9629 | xmlGenericError(xmlGenericErrorContext, |
| 9630 | " searching for *\n"); |
| 9631 | break; |
| 9632 | case NODE_TEST_NS: |
| 9633 | xmlGenericError(xmlGenericErrorContext, |
| 9634 | " searching for namespace %s\n", |
| 9635 | prefix); |
| 9636 | break; |
| 9637 | case NODE_TEST_NAME: |
| 9638 | xmlGenericError(xmlGenericErrorContext, |
| 9639 | " searching for name %s\n", name); |
| 9640 | if (prefix != NULL) |
| 9641 | xmlGenericError(xmlGenericErrorContext, |
| 9642 | " with namespace %s\n", prefix); |
| 9643 | break; |
| 9644 | } |
| 9645 | xmlGenericError(xmlGenericErrorContext, "Testing : "); |
| 9646 | #endif |
| 9647 | /* |
| 9648 | * 2.3 Node Tests |
| 9649 | * - For the attribute axis, the principal node type is attribute. |
| 9650 | * - For the namespace axis, the principal node type is namespace. |
| 9651 | * - For other axes, the principal node type is element. |
| 9652 | * |
| 9653 | * A node test * is true for any node of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9654 | * principal node type. For example, child::* will |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9655 | * select all element children of the context node |
| 9656 | */ |
| 9657 | tmp = ctxt->context->node; |
| 9658 | list = xmlXPathNodeSetCreate(NULL); |
| 9659 | for (i = 0; i < nodelist->nodeNr; i++) { |
| 9660 | ctxt->context->node = nodelist->nodeTab[i]; |
| 9661 | |
| 9662 | cur = NULL; |
| 9663 | n = 0; |
| 9664 | do { |
| 9665 | cur = next(ctxt, cur); |
| 9666 | if (cur == NULL) |
| 9667 | break; |
| 9668 | if ((first != NULL) && (*first == cur)) |
| 9669 | break; |
| 9670 | if (((t % 256) == 0) && |
| 9671 | (first != NULL) && (*first != NULL) && |
| 9672 | (xmlXPathCmpNodes(*first, cur) >= 0)) |
| 9673 | break; |
| 9674 | if ((last != NULL) && (*last == cur)) |
| 9675 | break; |
| 9676 | if (((t % 256) == 0) && |
| 9677 | (last != NULL) && (*last != NULL) && |
| 9678 | (xmlXPathCmpNodes(cur, *last) >= 0)) |
| 9679 | break; |
| 9680 | t++; |
| 9681 | switch (test) { |
| 9682 | case NODE_TEST_NONE: |
| 9683 | ctxt->context->node = tmp; |
| 9684 | STRANGE return(0); |
| 9685 | case NODE_TEST_TYPE: |
| 9686 | if ((cur->type == type) || |
| 9687 | ((type == NODE_TYPE_NODE) && |
| 9688 | ((cur->type == XML_DOCUMENT_NODE) || |
| 9689 | (cur->type == XML_HTML_DOCUMENT_NODE) || |
| 9690 | (cur->type == XML_ELEMENT_NODE) || |
| 9691 | (cur->type == XML_PI_NODE) || |
| 9692 | (cur->type == XML_COMMENT_NODE) || |
| 9693 | (cur->type == XML_CDATA_SECTION_NODE) || |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 9694 | (cur->type == XML_TEXT_NODE))) || |
| 9695 | ((type == NODE_TYPE_TEXT) && |
| 9696 | (cur->type == XML_CDATA_SECTION_NODE))) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9697 | n++; |
| 9698 | if (n == indx) |
| 9699 | addNode(list, cur); |
| 9700 | } |
| 9701 | break; |
| 9702 | case NODE_TEST_PI: |
| 9703 | if (cur->type == XML_PI_NODE) { |
| 9704 | if ((name != NULL) && |
| 9705 | (!xmlStrEqual(name, cur->name))) |
| 9706 | break; |
| 9707 | n++; |
| 9708 | if (n == indx) |
| 9709 | addNode(list, cur); |
| 9710 | } |
| 9711 | break; |
| 9712 | case NODE_TEST_ALL: |
| 9713 | if (axis == AXIS_ATTRIBUTE) { |
| 9714 | if (cur->type == XML_ATTRIBUTE_NODE) { |
| 9715 | n++; |
| 9716 | if (n == indx) |
| 9717 | addNode(list, cur); |
| 9718 | } |
| 9719 | } else if (axis == AXIS_NAMESPACE) { |
| 9720 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9721 | n++; |
| 9722 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9723 | xmlXPathNodeSetAddNs(list, ctxt->context->node, |
| 9724 | (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9725 | } |
| 9726 | } else { |
| 9727 | if (cur->type == XML_ELEMENT_NODE) { |
| 9728 | if (prefix == NULL) { |
| 9729 | n++; |
| 9730 | if (n == indx) |
| 9731 | addNode(list, cur); |
| 9732 | } else if ((cur->ns != NULL) && |
| 9733 | (xmlStrEqual(URI, cur->ns->href))) { |
| 9734 | n++; |
| 9735 | if (n == indx) |
| 9736 | addNode(list, cur); |
| 9737 | } |
| 9738 | } |
| 9739 | } |
| 9740 | break; |
| 9741 | case NODE_TEST_NS:{ |
| 9742 | TODO; |
| 9743 | break; |
| 9744 | } |
| 9745 | case NODE_TEST_NAME: |
| 9746 | switch (cur->type) { |
| 9747 | case XML_ELEMENT_NODE: |
| 9748 | if (xmlStrEqual(name, cur->name)) { |
| 9749 | if (prefix == NULL) { |
| 9750 | if (cur->ns == NULL) { |
| 9751 | n++; |
| 9752 | if (n == indx) |
| 9753 | addNode(list, cur); |
| 9754 | } |
| 9755 | } else { |
| 9756 | if ((cur->ns != NULL) && |
| 9757 | (xmlStrEqual(URI, |
| 9758 | cur->ns->href))) { |
| 9759 | n++; |
| 9760 | if (n == indx) |
| 9761 | addNode(list, cur); |
| 9762 | } |
| 9763 | } |
| 9764 | } |
| 9765 | break; |
| 9766 | case XML_ATTRIBUTE_NODE:{ |
| 9767 | xmlAttrPtr attr = (xmlAttrPtr) cur; |
| 9768 | |
| 9769 | if (xmlStrEqual(name, attr->name)) { |
| 9770 | if (prefix == NULL) { |
| 9771 | if ((attr->ns == NULL) || |
| 9772 | (attr->ns->prefix == NULL)) { |
| 9773 | n++; |
| 9774 | if (n == indx) |
| 9775 | addNode(list, cur); |
| 9776 | } |
| 9777 | } else { |
| 9778 | if ((attr->ns != NULL) && |
| 9779 | (xmlStrEqual(URI, |
| 9780 | attr->ns-> |
| 9781 | href))) { |
| 9782 | n++; |
| 9783 | if (n == indx) |
| 9784 | addNode(list, cur); |
| 9785 | } |
| 9786 | } |
| 9787 | } |
| 9788 | break; |
| 9789 | } |
| 9790 | case XML_NAMESPACE_DECL: |
| 9791 | if (cur->type == XML_NAMESPACE_DECL) { |
| 9792 | xmlNsPtr ns = (xmlNsPtr) cur; |
| 9793 | |
| 9794 | if ((ns->prefix != NULL) && (name != NULL) |
| 9795 | && (xmlStrEqual(ns->prefix, name))) { |
| 9796 | n++; |
| 9797 | if (n == indx) |
Daniel Veillard | 044fc6b | 2002-03-04 17:09:44 +0000 | [diff] [blame] | 9798 | xmlXPathNodeSetAddNs(list, |
| 9799 | ctxt->context->node, (xmlNsPtr) cur); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9800 | } |
| 9801 | } |
| 9802 | break; |
| 9803 | default: |
| 9804 | break; |
| 9805 | } |
| 9806 | break; |
| 9807 | break; |
| 9808 | } |
| 9809 | } while (n < indx); |
| 9810 | } |
| 9811 | ctxt->context->node = tmp; |
| 9812 | #ifdef DEBUG_STEP_NTH |
| 9813 | xmlGenericError(xmlGenericErrorContext, |
| 9814 | "\nExamined %d nodes, found %d nodes at that step\n", |
| 9815 | t, list->nodeNr); |
| 9816 | #endif |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9817 | valuePush(ctxt, xmlXPathWrapNodeSet(list)); |
Daniel Veillard | 0ab5cab | 2001-08-14 16:43:10 +0000 | [diff] [blame] | 9818 | if ((obj->boolval) && (obj->user != NULL)) { |
| 9819 | ctxt->value->boolval = 1; |
| 9820 | ctxt->value->user = obj->user; |
| 9821 | obj->user = NULL; |
| 9822 | obj->boolval = 0; |
| 9823 | } |
| 9824 | xmlXPathFreeObject(obj); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9825 | return(t); |
| 9826 | } |
| 9827 | |
| 9828 | /** |
| 9829 | * xmlXPathCompOpEvalFirst: |
| 9830 | * @ctxt: the XPath parser context with the compiled expression |
| 9831 | * @op: an XPath compiled operation |
| 9832 | * @first: the first elem found so far |
| 9833 | * |
| 9834 | * Evaluate the Precompiled XPath operation searching only the first |
| 9835 | * element in document order |
| 9836 | * |
| 9837 | * Returns the number of examined objects. |
| 9838 | */ |
| 9839 | static int |
| 9840 | xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt, |
| 9841 | xmlXPathStepOpPtr op, xmlNodePtr * first) |
| 9842 | { |
| 9843 | int total = 0, cur; |
| 9844 | xmlXPathCompExprPtr comp; |
| 9845 | xmlXPathObjectPtr arg1, arg2; |
| 9846 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9847 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9848 | comp = ctxt->comp; |
| 9849 | switch (op->op) { |
| 9850 | case XPATH_OP_END: |
| 9851 | return (0); |
| 9852 | case XPATH_OP_UNION: |
| 9853 | total = |
| 9854 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 9855 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9856 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9857 | if ((ctxt->value != NULL) |
| 9858 | && (ctxt->value->type == XPATH_NODESET) |
| 9859 | && (ctxt->value->nodesetval != NULL) |
| 9860 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9861 | /* |
| 9862 | * limit tree traversing to first node in the result |
| 9863 | */ |
| 9864 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9865 | *first = ctxt->value->nodesetval->nodeTab[0]; |
| 9866 | } |
| 9867 | cur = |
| 9868 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch2], |
| 9869 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9870 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9871 | CHECK_TYPE0(XPATH_NODESET); |
| 9872 | arg2 = valuePop(ctxt); |
| 9873 | |
| 9874 | CHECK_TYPE0(XPATH_NODESET); |
| 9875 | arg1 = valuePop(ctxt); |
| 9876 | |
| 9877 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 9878 | arg2->nodesetval); |
| 9879 | valuePush(ctxt, arg1); |
| 9880 | xmlXPathFreeObject(arg2); |
| 9881 | /* optimizer */ |
| 9882 | if (total > cur) |
| 9883 | xmlXPathCompSwap(op); |
| 9884 | return (total + cur); |
| 9885 | case XPATH_OP_ROOT: |
| 9886 | xmlXPathRoot(ctxt); |
| 9887 | return (0); |
| 9888 | case XPATH_OP_NODE: |
| 9889 | if (op->ch1 != -1) |
| 9890 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9891 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9892 | if (op->ch2 != -1) |
| 9893 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9894 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9895 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 9896 | return (total); |
| 9897 | case XPATH_OP_RESET: |
| 9898 | if (op->ch1 != -1) |
| 9899 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9900 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9901 | if (op->ch2 != -1) |
| 9902 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9903 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9904 | ctxt->context->node = NULL; |
| 9905 | return (total); |
| 9906 | case XPATH_OP_COLLECT:{ |
| 9907 | if (op->ch1 == -1) |
| 9908 | return (total); |
| 9909 | |
| 9910 | total = xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9911 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9912 | |
| 9913 | /* |
| 9914 | * Optimization for [n] selection where n is a number |
| 9915 | */ |
| 9916 | if ((op->ch2 != -1) && |
| 9917 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 9918 | (comp->steps[op->ch2].ch1 == -1) && |
| 9919 | (comp->steps[op->ch2].ch2 != -1) && |
| 9920 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 9921 | XPATH_OP_VALUE)) { |
| 9922 | xmlXPathObjectPtr val; |
| 9923 | |
| 9924 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 9925 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 9926 | int indx = (int) val->floatval; |
| 9927 | |
| 9928 | if (val->floatval == (float) indx) { |
| 9929 | xmlXPathNodeCollectAndTestNth(ctxt, op, indx, |
| 9930 | first, NULL); |
| 9931 | return (total); |
| 9932 | } |
| 9933 | } |
| 9934 | } |
| 9935 | total += xmlXPathNodeCollectAndTest(ctxt, op, first, NULL); |
| 9936 | return (total); |
| 9937 | } |
| 9938 | case XPATH_OP_VALUE: |
| 9939 | valuePush(ctxt, |
| 9940 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 9941 | return (0); |
| 9942 | case XPATH_OP_SORT: |
| 9943 | if (op->ch1 != -1) |
| 9944 | total += |
| 9945 | xmlXPathCompOpEvalFirst(ctxt, &comp->steps[op->ch1], |
| 9946 | first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9947 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9948 | if ((ctxt->value != NULL) |
| 9949 | && (ctxt->value->type == XPATH_NODESET) |
| 9950 | && (ctxt->value->nodesetval != NULL)) |
| 9951 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 9952 | return (total); |
| 9953 | default: |
| 9954 | return (xmlXPathCompOpEval(ctxt, op)); |
| 9955 | } |
| 9956 | } |
| 9957 | |
| 9958 | /** |
| 9959 | * xmlXPathCompOpEvalLast: |
| 9960 | * @ctxt: the XPath parser context with the compiled expression |
| 9961 | * @op: an XPath compiled operation |
| 9962 | * @last: the last elem found so far |
| 9963 | * |
| 9964 | * Evaluate the Precompiled XPath operation searching only the last |
| 9965 | * element in document order |
| 9966 | * |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 9967 | * Returns the number of nodes traversed |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9968 | */ |
| 9969 | static int |
| 9970 | xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op, |
| 9971 | xmlNodePtr * last) |
| 9972 | { |
| 9973 | int total = 0, cur; |
| 9974 | xmlXPathCompExprPtr comp; |
| 9975 | xmlXPathObjectPtr arg1, arg2; |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 9976 | xmlNodePtr bak; |
| 9977 | xmlDocPtr bakd; |
| 9978 | int pp; |
| 9979 | int cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9980 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9981 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9982 | comp = ctxt->comp; |
| 9983 | switch (op->op) { |
| 9984 | case XPATH_OP_END: |
| 9985 | return (0); |
| 9986 | case XPATH_OP_UNION: |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 9987 | bakd = ctxt->context->doc; |
| 9988 | bak = ctxt->context->node; |
| 9989 | pp = ctxt->context->proximityPosition; |
| 9990 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9991 | total = |
| 9992 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 9993 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 9994 | if ((ctxt->value != NULL) |
| 9995 | && (ctxt->value->type == XPATH_NODESET) |
| 9996 | && (ctxt->value->nodesetval != NULL) |
| 9997 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 9998 | /* |
| 9999 | * limit tree traversing to first node in the result |
| 10000 | */ |
| 10001 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10002 | *last = |
| 10003 | ctxt->value->nodesetval->nodeTab[ctxt->value-> |
| 10004 | nodesetval->nodeNr - |
| 10005 | 1]; |
| 10006 | } |
William M. Brack | ce4fc56 | 2004-01-22 02:47:18 +0000 | [diff] [blame] | 10007 | ctxt->context->doc = bakd; |
| 10008 | ctxt->context->node = bak; |
| 10009 | ctxt->context->proximityPosition = pp; |
| 10010 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10011 | cur = |
| 10012 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch2], last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10013 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10014 | if ((ctxt->value != NULL) |
| 10015 | && (ctxt->value->type == XPATH_NODESET) |
| 10016 | && (ctxt->value->nodesetval != NULL) |
| 10017 | && (ctxt->value->nodesetval->nodeNr >= 1)) { |
| 10018 | } |
| 10019 | CHECK_TYPE0(XPATH_NODESET); |
| 10020 | arg2 = valuePop(ctxt); |
| 10021 | |
| 10022 | CHECK_TYPE0(XPATH_NODESET); |
| 10023 | arg1 = valuePop(ctxt); |
| 10024 | |
| 10025 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 10026 | arg2->nodesetval); |
| 10027 | valuePush(ctxt, arg1); |
| 10028 | xmlXPathFreeObject(arg2); |
| 10029 | /* optimizer */ |
| 10030 | if (total > cur) |
| 10031 | xmlXPathCompSwap(op); |
| 10032 | return (total + cur); |
| 10033 | case XPATH_OP_ROOT: |
| 10034 | xmlXPathRoot(ctxt); |
| 10035 | return (0); |
| 10036 | case XPATH_OP_NODE: |
| 10037 | if (op->ch1 != -1) |
| 10038 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10039 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10040 | if (op->ch2 != -1) |
| 10041 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10042 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10043 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 10044 | return (total); |
| 10045 | case XPATH_OP_RESET: |
| 10046 | if (op->ch1 != -1) |
| 10047 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10048 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10049 | if (op->ch2 != -1) |
| 10050 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10051 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10052 | ctxt->context->node = NULL; |
| 10053 | return (total); |
| 10054 | case XPATH_OP_COLLECT:{ |
| 10055 | if (op->ch1 == -1) |
| 10056 | return (0); |
| 10057 | |
| 10058 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10059 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10060 | |
| 10061 | /* |
| 10062 | * Optimization for [n] selection where n is a number |
| 10063 | */ |
| 10064 | if ((op->ch2 != -1) && |
| 10065 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 10066 | (comp->steps[op->ch2].ch1 == -1) && |
| 10067 | (comp->steps[op->ch2].ch2 != -1) && |
| 10068 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 10069 | XPATH_OP_VALUE)) { |
| 10070 | xmlXPathObjectPtr val; |
| 10071 | |
| 10072 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 10073 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 10074 | int indx = (int) val->floatval; |
| 10075 | |
| 10076 | if (val->floatval == (float) indx) { |
| 10077 | total += |
| 10078 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 10079 | indx, NULL, |
| 10080 | last); |
| 10081 | return (total); |
| 10082 | } |
| 10083 | } |
| 10084 | } |
| 10085 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, last); |
| 10086 | return (total); |
| 10087 | } |
| 10088 | case XPATH_OP_VALUE: |
| 10089 | valuePush(ctxt, |
| 10090 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10091 | return (0); |
| 10092 | case XPATH_OP_SORT: |
| 10093 | if (op->ch1 != -1) |
| 10094 | total += |
| 10095 | xmlXPathCompOpEvalLast(ctxt, &comp->steps[op->ch1], |
| 10096 | last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10097 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10098 | if ((ctxt->value != NULL) |
| 10099 | && (ctxt->value->type == XPATH_NODESET) |
| 10100 | && (ctxt->value->nodesetval != NULL)) |
| 10101 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10102 | return (total); |
| 10103 | default: |
| 10104 | return (xmlXPathCompOpEval(ctxt, op)); |
| 10105 | } |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10106 | } |
| 10107 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10108 | /** |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10109 | * xmlXPathCompOpEval: |
| 10110 | * @ctxt: the XPath parser context with the compiled expression |
| 10111 | * @op: an XPath compiled operation |
| 10112 | * |
| 10113 | * Evaluate the Precompiled XPath operation |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10114 | * Returns the number of nodes traversed |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10115 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10116 | static int |
| 10117 | xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt, xmlXPathStepOpPtr op) |
| 10118 | { |
| 10119 | int total = 0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10120 | int equal, ret; |
| 10121 | xmlXPathCompExprPtr comp; |
| 10122 | xmlXPathObjectPtr arg1, arg2; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10123 | xmlNodePtr bak; |
| 10124 | xmlDocPtr bakd; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10125 | int pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10126 | int cs; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10127 | |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10128 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10129 | comp = ctxt->comp; |
| 10130 | switch (op->op) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10131 | case XPATH_OP_END: |
| 10132 | return (0); |
| 10133 | case XPATH_OP_AND: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10134 | bakd = ctxt->context->doc; |
| 10135 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10136 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10137 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10138 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10139 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10140 | xmlXPathBooleanFunction(ctxt, 1); |
| 10141 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 0)) |
| 10142 | return (total); |
| 10143 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10144 | ctxt->context->doc = bakd; |
| 10145 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10146 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10147 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10148 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10149 | if (ctxt->error) { |
| 10150 | xmlXPathFreeObject(arg2); |
| 10151 | return(0); |
| 10152 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10153 | xmlXPathBooleanFunction(ctxt, 1); |
| 10154 | arg1 = valuePop(ctxt); |
| 10155 | arg1->boolval &= arg2->boolval; |
| 10156 | valuePush(ctxt, arg1); |
| 10157 | xmlXPathFreeObject(arg2); |
| 10158 | return (total); |
| 10159 | case XPATH_OP_OR: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10160 | bakd = ctxt->context->doc; |
| 10161 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10162 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10163 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10164 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10165 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10166 | xmlXPathBooleanFunction(ctxt, 1); |
| 10167 | if ((ctxt->value == NULL) || (ctxt->value->boolval == 1)) |
| 10168 | return (total); |
| 10169 | arg2 = valuePop(ctxt); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10170 | ctxt->context->doc = bakd; |
| 10171 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10172 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10173 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10174 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10175 | if (ctxt->error) { |
| 10176 | xmlXPathFreeObject(arg2); |
| 10177 | return(0); |
| 10178 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10179 | xmlXPathBooleanFunction(ctxt, 1); |
| 10180 | arg1 = valuePop(ctxt); |
| 10181 | arg1->boolval |= arg2->boolval; |
| 10182 | valuePush(ctxt, arg1); |
| 10183 | xmlXPathFreeObject(arg2); |
| 10184 | return (total); |
| 10185 | case XPATH_OP_EQUAL: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10186 | bakd = ctxt->context->doc; |
| 10187 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10188 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10189 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10190 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10191 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10192 | ctxt->context->doc = bakd; |
| 10193 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10194 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10195 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10196 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10197 | CHECK_ERROR0; |
William M. Brack | 0c022ad | 2002-07-12 00:56:01 +0000 | [diff] [blame] | 10198 | if (op->value) |
| 10199 | equal = xmlXPathEqualValues(ctxt); |
| 10200 | else |
| 10201 | equal = xmlXPathNotEqualValues(ctxt); |
| 10202 | valuePush(ctxt, xmlXPathNewBoolean(equal)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10203 | return (total); |
| 10204 | case XPATH_OP_CMP: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10205 | bakd = ctxt->context->doc; |
| 10206 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10207 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10208 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10209 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10210 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10211 | ctxt->context->doc = bakd; |
| 10212 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10213 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10214 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10215 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10216 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10217 | ret = xmlXPathCompareValues(ctxt, op->value, op->value2); |
| 10218 | valuePush(ctxt, xmlXPathNewBoolean(ret)); |
| 10219 | return (total); |
| 10220 | case XPATH_OP_PLUS: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10221 | bakd = ctxt->context->doc; |
| 10222 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10223 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10224 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10225 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10226 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10227 | if (op->ch2 != -1) { |
| 10228 | ctxt->context->doc = bakd; |
| 10229 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10230 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10231 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10232 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10233 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10234 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10235 | if (op->value == 0) |
| 10236 | xmlXPathSubValues(ctxt); |
| 10237 | else if (op->value == 1) |
| 10238 | xmlXPathAddValues(ctxt); |
| 10239 | else if (op->value == 2) |
| 10240 | xmlXPathValueFlipSign(ctxt); |
| 10241 | else if (op->value == 3) { |
| 10242 | CAST_TO_NUMBER; |
| 10243 | CHECK_TYPE0(XPATH_NUMBER); |
| 10244 | } |
| 10245 | return (total); |
| 10246 | case XPATH_OP_MULT: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10247 | bakd = ctxt->context->doc; |
| 10248 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10249 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10250 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10251 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10252 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10253 | ctxt->context->doc = bakd; |
| 10254 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10255 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10256 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10257 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10258 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10259 | if (op->value == 0) |
| 10260 | xmlXPathMultValues(ctxt); |
| 10261 | else if (op->value == 1) |
| 10262 | xmlXPathDivValues(ctxt); |
| 10263 | else if (op->value == 2) |
| 10264 | xmlXPathModValues(ctxt); |
| 10265 | return (total); |
| 10266 | case XPATH_OP_UNION: |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10267 | bakd = ctxt->context->doc; |
| 10268 | bak = ctxt->context->node; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10269 | pp = ctxt->context->proximityPosition; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10270 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10271 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10272 | CHECK_ERROR0; |
Daniel Veillard | 7089d6b | 2002-03-29 17:28:10 +0000 | [diff] [blame] | 10273 | ctxt->context->doc = bakd; |
| 10274 | ctxt->context->node = bak; |
William M. Brack | 6000af5 | 2002-06-28 11:43:13 +0000 | [diff] [blame] | 10275 | ctxt->context->proximityPosition = pp; |
William M. Brack | 692092b | 2002-06-28 15:01:24 +0000 | [diff] [blame] | 10276 | ctxt->context->contextSize = cs; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10277 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10278 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10279 | CHECK_TYPE0(XPATH_NODESET); |
| 10280 | arg2 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10281 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10282 | CHECK_TYPE0(XPATH_NODESET); |
| 10283 | arg1 = valuePop(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10284 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10285 | arg1->nodesetval = xmlXPathNodeSetMerge(arg1->nodesetval, |
| 10286 | arg2->nodesetval); |
| 10287 | valuePush(ctxt, arg1); |
| 10288 | xmlXPathFreeObject(arg2); |
| 10289 | return (total); |
| 10290 | case XPATH_OP_ROOT: |
| 10291 | xmlXPathRoot(ctxt); |
| 10292 | return (total); |
| 10293 | case XPATH_OP_NODE: |
| 10294 | if (op->ch1 != -1) |
| 10295 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10296 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10297 | if (op->ch2 != -1) |
| 10298 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10299 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10300 | valuePush(ctxt, xmlXPathNewNodeSet(ctxt->context->node)); |
| 10301 | return (total); |
| 10302 | case XPATH_OP_RESET: |
| 10303 | if (op->ch1 != -1) |
| 10304 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10305 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10306 | if (op->ch2 != -1) |
| 10307 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10308 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10309 | ctxt->context->node = NULL; |
| 10310 | return (total); |
| 10311 | case XPATH_OP_COLLECT:{ |
| 10312 | if (op->ch1 == -1) |
| 10313 | return (total); |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10314 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10315 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10316 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10317 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10318 | /* |
| 10319 | * Optimization for [n] selection where n is a number |
| 10320 | */ |
| 10321 | if ((op->ch2 != -1) && |
| 10322 | (comp->steps[op->ch2].op == XPATH_OP_PREDICATE) && |
| 10323 | (comp->steps[op->ch2].ch1 == -1) && |
| 10324 | (comp->steps[op->ch2].ch2 != -1) && |
| 10325 | (comp->steps[comp->steps[op->ch2].ch2].op == |
| 10326 | XPATH_OP_VALUE)) { |
| 10327 | xmlXPathObjectPtr val; |
Daniel Veillard | 42596ad | 2001-05-22 16:57:14 +0000 | [diff] [blame] | 10328 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10329 | val = comp->steps[comp->steps[op->ch2].ch2].value4; |
| 10330 | if ((val != NULL) && (val->type == XPATH_NUMBER)) { |
| 10331 | int indx = (int) val->floatval; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10332 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10333 | if (val->floatval == (float) indx) { |
| 10334 | total += |
| 10335 | xmlXPathNodeCollectAndTestNth(ctxt, op, |
| 10336 | indx, NULL, |
| 10337 | NULL); |
| 10338 | return (total); |
| 10339 | } |
| 10340 | } |
| 10341 | } |
| 10342 | total += xmlXPathNodeCollectAndTest(ctxt, op, NULL, NULL); |
| 10343 | return (total); |
| 10344 | } |
| 10345 | case XPATH_OP_VALUE: |
| 10346 | valuePush(ctxt, |
| 10347 | xmlXPathObjectCopy((xmlXPathObjectPtr) op->value4)); |
| 10348 | return (total); |
| 10349 | case XPATH_OP_VARIABLE:{ |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10350 | xmlXPathObjectPtr val; |
| 10351 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10352 | if (op->ch1 != -1) |
| 10353 | total += |
| 10354 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10355 | if (op->value5 == NULL) { |
| 10356 | val = xmlXPathVariableLookup(ctxt->context, op->value4); |
| 10357 | if (val == NULL) { |
| 10358 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 10359 | return(0); |
| 10360 | } |
| 10361 | valuePush(ctxt, val); |
| 10362 | } else { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10363 | const xmlChar *URI; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10364 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10365 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 10366 | if (URI == NULL) { |
| 10367 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10368 | "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10369 | op->value4, op->value5); |
| 10370 | return (total); |
| 10371 | } |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10372 | val = xmlXPathVariableLookupNS(ctxt->context, |
| 10373 | op->value4, URI); |
| 10374 | if (val == NULL) { |
| 10375 | ctxt->error = XPATH_UNDEF_VARIABLE_ERROR; |
| 10376 | return(0); |
| 10377 | } |
| 10378 | valuePush(ctxt, val); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10379 | } |
| 10380 | return (total); |
| 10381 | } |
| 10382 | case XPATH_OP_FUNCTION:{ |
| 10383 | xmlXPathFunction func; |
| 10384 | const xmlChar *oldFunc, *oldFuncURI; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10385 | int i; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10386 | |
| 10387 | if (op->ch1 != -1) |
| 10388 | total += |
| 10389 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10390 | if (ctxt->valueNr < op->value) { |
| 10391 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10392 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10393 | ctxt->error = XPATH_INVALID_OPERAND; |
| 10394 | return (total); |
| 10395 | } |
| 10396 | for (i = 0; i < op->value; i++) |
| 10397 | if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) { |
| 10398 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10399 | "xmlXPathCompOpEval: parameter error\n"); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10400 | ctxt->error = XPATH_INVALID_OPERAND; |
| 10401 | return (total); |
| 10402 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10403 | if (op->cache != NULL) |
| 10404 | func = (xmlXPathFunction) op->cache; |
| 10405 | else { |
| 10406 | const xmlChar *URI = NULL; |
| 10407 | |
| 10408 | if (op->value5 == NULL) |
| 10409 | func = |
| 10410 | xmlXPathFunctionLookup(ctxt->context, |
| 10411 | op->value4); |
| 10412 | else { |
| 10413 | URI = xmlXPathNsLookup(ctxt->context, op->value5); |
| 10414 | if (URI == NULL) { |
| 10415 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10416 | "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10417 | op->value4, op->value5); |
| 10418 | return (total); |
| 10419 | } |
| 10420 | func = xmlXPathFunctionLookupNS(ctxt->context, |
| 10421 | op->value4, URI); |
| 10422 | } |
| 10423 | if (func == NULL) { |
| 10424 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10425 | "xmlXPathCompOpEval: function %s not found\n", |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10426 | op->value4); |
| 10427 | XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10428 | } |
| 10429 | op->cache = (void *) func; |
| 10430 | op->cacheURI = (void *) URI; |
| 10431 | } |
| 10432 | oldFunc = ctxt->context->function; |
| 10433 | oldFuncURI = ctxt->context->functionURI; |
| 10434 | ctxt->context->function = op->value4; |
| 10435 | ctxt->context->functionURI = op->cacheURI; |
| 10436 | func(ctxt, op->value); |
| 10437 | ctxt->context->function = oldFunc; |
| 10438 | ctxt->context->functionURI = oldFuncURI; |
| 10439 | return (total); |
| 10440 | } |
| 10441 | case XPATH_OP_ARG: |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 10442 | bakd = ctxt->context->doc; |
| 10443 | bak = ctxt->context->node; |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 10444 | pp = ctxt->context->proximityPosition; |
| 10445 | cs = ctxt->context->contextSize; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10446 | if (op->ch1 != -1) |
| 10447 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 10448 | ctxt->context->contextSize = cs; |
| 10449 | ctxt->context->proximityPosition = pp; |
Daniel Veillard | 088bf11 | 2002-05-14 11:03:59 +0000 | [diff] [blame] | 10450 | ctxt->context->node = bak; |
William M. Brack | 645a924 | 2004-11-09 12:20:42 +0000 | [diff] [blame] | 10451 | ctxt->context->doc = bakd; |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10452 | CHECK_ERROR0; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10453 | if (op->ch2 != -1) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10454 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch2]); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10455 | ctxt->context->doc = bakd; |
| 10456 | ctxt->context->node = bak; |
| 10457 | CHECK_ERROR0; |
| 10458 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10459 | return (total); |
| 10460 | case XPATH_OP_PREDICATE: |
| 10461 | case XPATH_OP_FILTER:{ |
| 10462 | xmlXPathObjectPtr res; |
| 10463 | xmlXPathObjectPtr obj, tmp; |
| 10464 | xmlNodeSetPtr newset = NULL; |
| 10465 | xmlNodeSetPtr oldset; |
| 10466 | xmlNodePtr oldnode; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10467 | xmlDocPtr oldDoc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10468 | int i; |
| 10469 | |
| 10470 | /* |
| 10471 | * Optimization for ()[1] selection i.e. the first elem |
| 10472 | */ |
| 10473 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 10474 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 10475 | (comp->steps[op->ch2].op == XPATH_OP_VALUE)) { |
| 10476 | xmlXPathObjectPtr val; |
| 10477 | |
| 10478 | val = comp->steps[op->ch2].value4; |
| 10479 | if ((val != NULL) && (val->type == XPATH_NUMBER) && |
| 10480 | (val->floatval == 1.0)) { |
| 10481 | xmlNodePtr first = NULL; |
| 10482 | |
| 10483 | total += |
| 10484 | xmlXPathCompOpEvalFirst(ctxt, |
| 10485 | &comp->steps[op->ch1], |
| 10486 | &first); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10487 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10488 | /* |
| 10489 | * The nodeset should be in document order, |
| 10490 | * Keep only the first value |
| 10491 | */ |
| 10492 | if ((ctxt->value != NULL) && |
| 10493 | (ctxt->value->type == XPATH_NODESET) && |
| 10494 | (ctxt->value->nodesetval != NULL) && |
| 10495 | (ctxt->value->nodesetval->nodeNr > 1)) |
| 10496 | ctxt->value->nodesetval->nodeNr = 1; |
| 10497 | return (total); |
| 10498 | } |
| 10499 | } |
| 10500 | /* |
| 10501 | * Optimization for ()[last()] selection i.e. the last elem |
| 10502 | */ |
| 10503 | if ((op->ch1 != -1) && (op->ch2 != -1) && |
| 10504 | (comp->steps[op->ch1].op == XPATH_OP_SORT) && |
| 10505 | (comp->steps[op->ch2].op == XPATH_OP_SORT)) { |
| 10506 | int f = comp->steps[op->ch2].ch1; |
| 10507 | |
| 10508 | if ((f != -1) && |
| 10509 | (comp->steps[f].op == XPATH_OP_FUNCTION) && |
| 10510 | (comp->steps[f].value5 == NULL) && |
| 10511 | (comp->steps[f].value == 0) && |
| 10512 | (comp->steps[f].value4 != NULL) && |
| 10513 | (xmlStrEqual |
| 10514 | (comp->steps[f].value4, BAD_CAST "last"))) { |
| 10515 | xmlNodePtr last = NULL; |
| 10516 | |
| 10517 | total += |
| 10518 | xmlXPathCompOpEvalLast(ctxt, |
| 10519 | &comp->steps[op->ch1], |
| 10520 | &last); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10521 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10522 | /* |
| 10523 | * The nodeset should be in document order, |
| 10524 | * Keep only the last value |
| 10525 | */ |
| 10526 | if ((ctxt->value != NULL) && |
| 10527 | (ctxt->value->type == XPATH_NODESET) && |
| 10528 | (ctxt->value->nodesetval != NULL) && |
| 10529 | (ctxt->value->nodesetval->nodeTab != NULL) && |
| 10530 | (ctxt->value->nodesetval->nodeNr > 1)) { |
| 10531 | ctxt->value->nodesetval->nodeTab[0] = |
| 10532 | ctxt->value->nodesetval->nodeTab[ctxt-> |
| 10533 | value-> |
| 10534 | nodesetval-> |
| 10535 | nodeNr - |
| 10536 | 1]; |
| 10537 | ctxt->value->nodesetval->nodeNr = 1; |
| 10538 | } |
| 10539 | return (total); |
| 10540 | } |
| 10541 | } |
| 10542 | |
| 10543 | if (op->ch1 != -1) |
| 10544 | total += |
| 10545 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10546 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10547 | if (op->ch2 == -1) |
| 10548 | return (total); |
| 10549 | if (ctxt->value == NULL) |
| 10550 | return (total); |
| 10551 | |
| 10552 | oldnode = ctxt->context->node; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10553 | |
| 10554 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10555 | /* |
| 10556 | * Hum are we filtering the result of an XPointer expression |
| 10557 | */ |
| 10558 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 10559 | xmlLocationSetPtr newlocset = NULL; |
| 10560 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10561 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10562 | /* |
| 10563 | * Extract the old locset, and then evaluate the result of the |
| 10564 | * expression for all the element in the locset. use it to grow |
| 10565 | * up a new locset. |
| 10566 | */ |
| 10567 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 10568 | obj = valuePop(ctxt); |
| 10569 | oldlocset = obj->user; |
| 10570 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10571 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10572 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
| 10573 | ctxt->context->contextSize = 0; |
| 10574 | ctxt->context->proximityPosition = 0; |
| 10575 | if (op->ch2 != -1) |
| 10576 | total += |
| 10577 | xmlXPathCompOpEval(ctxt, |
| 10578 | &comp->steps[op->ch2]); |
| 10579 | res = valuePop(ctxt); |
| 10580 | if (res != NULL) |
| 10581 | xmlXPathFreeObject(res); |
| 10582 | valuePush(ctxt, obj); |
| 10583 | CHECK_ERROR0; |
| 10584 | return (total); |
| 10585 | } |
| 10586 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10587 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10588 | for (i = 0; i < oldlocset->locNr; i++) { |
| 10589 | /* |
| 10590 | * Run the evaluation with a node list made of a |
| 10591 | * single item in the nodelocset. |
| 10592 | */ |
| 10593 | ctxt->context->node = oldlocset->locTab[i]->user; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10594 | ctxt->context->contextSize = oldlocset->locNr; |
| 10595 | ctxt->context->proximityPosition = i + 1; |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 10596 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10597 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10598 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10599 | if (op->ch2 != -1) |
| 10600 | total += |
| 10601 | xmlXPathCompOpEval(ctxt, |
| 10602 | &comp->steps[op->ch2]); |
| 10603 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10604 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10605 | /* |
| 10606 | * The result of the evaluation need to be tested to |
| 10607 | * decided whether the filter succeeded or not |
| 10608 | */ |
| 10609 | res = valuePop(ctxt); |
| 10610 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10611 | xmlXPtrLocationSetAdd(newlocset, |
| 10612 | xmlXPathObjectCopy |
| 10613 | (oldlocset->locTab[i])); |
| 10614 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10615 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10616 | /* |
| 10617 | * Cleanup |
| 10618 | */ |
| 10619 | if (res != NULL) |
| 10620 | xmlXPathFreeObject(res); |
| 10621 | if (ctxt->value == tmp) { |
| 10622 | res = valuePop(ctxt); |
| 10623 | xmlXPathFreeObject(res); |
| 10624 | } |
| 10625 | |
| 10626 | ctxt->context->node = NULL; |
| 10627 | } |
| 10628 | |
| 10629 | /* |
| 10630 | * The result is used as the new evaluation locset. |
| 10631 | */ |
| 10632 | xmlXPathFreeObject(obj); |
| 10633 | ctxt->context->node = NULL; |
| 10634 | ctxt->context->contextSize = -1; |
| 10635 | ctxt->context->proximityPosition = -1; |
| 10636 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
| 10637 | ctxt->context->node = oldnode; |
| 10638 | return (total); |
| 10639 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10640 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10641 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10642 | /* |
| 10643 | * Extract the old set, and then evaluate the result of the |
| 10644 | * expression for all the element in the set. use it to grow |
| 10645 | * up a new set. |
| 10646 | */ |
| 10647 | CHECK_TYPE0(XPATH_NODESET); |
| 10648 | obj = valuePop(ctxt); |
| 10649 | oldset = obj->nodesetval; |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 10650 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10651 | oldnode = ctxt->context->node; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10652 | oldDoc = ctxt->context->doc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10653 | ctxt->context->node = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10654 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10655 | if ((oldset == NULL) || (oldset->nodeNr == 0)) { |
| 10656 | ctxt->context->contextSize = 0; |
| 10657 | ctxt->context->proximityPosition = 0; |
William M. Brack | 8fad8bf | 2004-06-02 08:26:25 +0000 | [diff] [blame] | 10658 | /* |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10659 | if (op->ch2 != -1) |
| 10660 | total += |
| 10661 | xmlXPathCompOpEval(ctxt, |
| 10662 | &comp->steps[op->ch2]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10663 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10664 | res = valuePop(ctxt); |
| 10665 | if (res != NULL) |
| 10666 | xmlXPathFreeObject(res); |
William M. Brack | 8fad8bf | 2004-06-02 08:26:25 +0000 | [diff] [blame] | 10667 | */ |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10668 | valuePush(ctxt, obj); |
| 10669 | ctxt->context->node = oldnode; |
| 10670 | CHECK_ERROR0; |
| 10671 | } else { |
| 10672 | /* |
| 10673 | * Initialize the new set. |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10674 | * Also set the xpath document in case things like |
| 10675 | * key() evaluation are attempted on the predicate |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10676 | */ |
| 10677 | newset = xmlXPathNodeSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10678 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10679 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10680 | /* |
| 10681 | * Run the evaluation with a node list made of |
| 10682 | * a single item in the nodeset. |
| 10683 | */ |
| 10684 | ctxt->context->node = oldset->nodeTab[i]; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10685 | if ((oldset->nodeTab[i]->type != XML_NAMESPACE_DECL) && |
| 10686 | (oldset->nodeTab[i]->doc != NULL)) |
| 10687 | ctxt->context->doc = oldset->nodeTab[i]->doc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10688 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10689 | valuePush(ctxt, tmp); |
| 10690 | ctxt->context->contextSize = oldset->nodeNr; |
| 10691 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10692 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10693 | if (op->ch2 != -1) |
| 10694 | total += |
| 10695 | xmlXPathCompOpEval(ctxt, |
| 10696 | &comp->steps[op->ch2]); |
| 10697 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10698 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10699 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10700 | * The result of the evaluation needs to be tested to |
| 10701 | * decide whether the filter succeeded or not |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10702 | */ |
| 10703 | res = valuePop(ctxt); |
| 10704 | if (xmlXPathEvaluatePredicateResult(ctxt, res)) { |
| 10705 | xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); |
| 10706 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10707 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10708 | /* |
| 10709 | * Cleanup |
| 10710 | */ |
| 10711 | if (res != NULL) |
| 10712 | xmlXPathFreeObject(res); |
| 10713 | if (ctxt->value == tmp) { |
| 10714 | res = valuePop(ctxt); |
| 10715 | xmlXPathFreeObject(res); |
| 10716 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10717 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10718 | ctxt->context->node = NULL; |
| 10719 | } |
| 10720 | |
| 10721 | /* |
| 10722 | * The result is used as the new evaluation set. |
| 10723 | */ |
| 10724 | xmlXPathFreeObject(obj); |
| 10725 | ctxt->context->node = NULL; |
| 10726 | ctxt->context->contextSize = -1; |
| 10727 | ctxt->context->proximityPosition = -1; |
William M. Brack | 3794b9e | 2004-07-13 15:06:20 +0000 | [diff] [blame] | 10728 | /* may want to move this past the '}' later */ |
| 10729 | ctxt->context->doc = oldDoc; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10730 | valuePush(ctxt, xmlXPathWrapNodeSet(newset)); |
| 10731 | } |
| 10732 | ctxt->context->node = oldnode; |
| 10733 | return (total); |
| 10734 | } |
| 10735 | case XPATH_OP_SORT: |
| 10736 | if (op->ch1 != -1) |
| 10737 | total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
Daniel Veillard | 556c668 | 2001-10-06 09:59:51 +0000 | [diff] [blame] | 10738 | CHECK_ERROR0; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10739 | if ((ctxt->value != NULL) && |
| 10740 | (ctxt->value->type == XPATH_NODESET) && |
| 10741 | (ctxt->value->nodesetval != NULL)) |
| 10742 | xmlXPathNodeSetSort(ctxt->value->nodesetval); |
| 10743 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10744 | #ifdef LIBXML_XPTR_ENABLED |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10745 | case XPATH_OP_RANGETO:{ |
| 10746 | xmlXPathObjectPtr range; |
| 10747 | xmlXPathObjectPtr res, obj; |
| 10748 | xmlXPathObjectPtr tmp; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10749 | xmlLocationSetPtr newlocset = NULL; |
| 10750 | xmlLocationSetPtr oldlocset; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10751 | xmlNodeSetPtr oldset; |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10752 | int i, j; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10753 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10754 | if (op->ch1 != -1) |
| 10755 | total += |
| 10756 | xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); |
| 10757 | if (op->ch2 == -1) |
| 10758 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10759 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10760 | if (ctxt->value->type == XPATH_LOCATIONSET) { |
| 10761 | /* |
| 10762 | * Extract the old locset, and then evaluate the result of the |
| 10763 | * expression for all the element in the locset. use it to grow |
| 10764 | * up a new locset. |
| 10765 | */ |
| 10766 | CHECK_TYPE0(XPATH_LOCATIONSET); |
| 10767 | obj = valuePop(ctxt); |
| 10768 | oldlocset = obj->user; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10769 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10770 | if ((oldlocset == NULL) || (oldlocset->locNr == 0)) { |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10771 | ctxt->context->node = NULL; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10772 | ctxt->context->contextSize = 0; |
| 10773 | ctxt->context->proximityPosition = 0; |
| 10774 | total += xmlXPathCompOpEval(ctxt,&comp->steps[op->ch2]); |
| 10775 | res = valuePop(ctxt); |
| 10776 | if (res != NULL) |
| 10777 | xmlXPathFreeObject(res); |
| 10778 | valuePush(ctxt, obj); |
| 10779 | CHECK_ERROR0; |
| 10780 | return (total); |
| 10781 | } |
| 10782 | newlocset = xmlXPtrLocationSetCreate(NULL); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10783 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10784 | for (i = 0; i < oldlocset->locNr; i++) { |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10785 | /* |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10786 | * Run the evaluation with a node list made of a |
| 10787 | * single item in the nodelocset. |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10788 | */ |
William M. Brack | f7eb794 | 2003-12-31 07:59:17 +0000 | [diff] [blame] | 10789 | ctxt->context->node = oldlocset->locTab[i]->user; |
| 10790 | ctxt->context->contextSize = oldlocset->locNr; |
| 10791 | ctxt->context->proximityPosition = i + 1; |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10792 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10793 | valuePush(ctxt, tmp); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10794 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10795 | if (op->ch2 != -1) |
| 10796 | total += |
| 10797 | xmlXPathCompOpEval(ctxt, |
| 10798 | &comp->steps[op->ch2]); |
| 10799 | CHECK_ERROR0; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10800 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10801 | res = valuePop(ctxt); |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10802 | if (res->type == XPATH_LOCATIONSET) { |
| 10803 | xmlLocationSetPtr rloc = |
| 10804 | (xmlLocationSetPtr)res->user; |
| 10805 | for (j=0; j<rloc->locNr; j++) { |
| 10806 | range = xmlXPtrNewRange( |
| 10807 | oldlocset->locTab[i]->user, |
| 10808 | oldlocset->locTab[i]->index, |
| 10809 | rloc->locTab[j]->user2, |
| 10810 | rloc->locTab[j]->index2); |
| 10811 | if (range != NULL) { |
| 10812 | xmlXPtrLocationSetAdd(newlocset, range); |
| 10813 | } |
| 10814 | } |
| 10815 | } else { |
| 10816 | range = xmlXPtrNewRangeNodeObject( |
| 10817 | (xmlNodePtr)oldlocset->locTab[i]->user, res); |
| 10818 | if (range != NULL) { |
| 10819 | xmlXPtrLocationSetAdd(newlocset,range); |
| 10820 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10821 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10822 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10823 | /* |
| 10824 | * Cleanup |
| 10825 | */ |
| 10826 | if (res != NULL) |
| 10827 | xmlXPathFreeObject(res); |
| 10828 | if (ctxt->value == tmp) { |
| 10829 | res = valuePop(ctxt); |
| 10830 | xmlXPathFreeObject(res); |
| 10831 | } |
| 10832 | |
| 10833 | ctxt->context->node = NULL; |
| 10834 | } |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 10835 | } else { /* Not a location set */ |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10836 | CHECK_TYPE0(XPATH_NODESET); |
| 10837 | obj = valuePop(ctxt); |
| 10838 | oldset = obj->nodesetval; |
| 10839 | ctxt->context->node = NULL; |
| 10840 | |
| 10841 | newlocset = xmlXPtrLocationSetCreate(NULL); |
| 10842 | |
| 10843 | if (oldset != NULL) { |
| 10844 | for (i = 0; i < oldset->nodeNr; i++) { |
| 10845 | /* |
| 10846 | * Run the evaluation with a node list made of a single item |
| 10847 | * in the nodeset. |
| 10848 | */ |
| 10849 | ctxt->context->node = oldset->nodeTab[i]; |
| 10850 | tmp = xmlXPathNewNodeSet(ctxt->context->node); |
| 10851 | valuePush(ctxt, tmp); |
| 10852 | |
| 10853 | if (op->ch2 != -1) |
| 10854 | total += |
| 10855 | xmlXPathCompOpEval(ctxt, |
| 10856 | &comp->steps[op->ch2]); |
| 10857 | CHECK_ERROR0; |
| 10858 | |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10859 | res = valuePop(ctxt); |
| 10860 | range = |
| 10861 | xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], |
| 10862 | res); |
| 10863 | if (range != NULL) { |
| 10864 | xmlXPtrLocationSetAdd(newlocset, range); |
| 10865 | } |
| 10866 | |
| 10867 | /* |
| 10868 | * Cleanup |
| 10869 | */ |
| 10870 | if (res != NULL) |
| 10871 | xmlXPathFreeObject(res); |
| 10872 | if (ctxt->value == tmp) { |
| 10873 | res = valuePop(ctxt); |
| 10874 | xmlXPathFreeObject(res); |
| 10875 | } |
| 10876 | |
| 10877 | ctxt->context->node = NULL; |
| 10878 | } |
| 10879 | } |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10880 | } |
| 10881 | |
| 10882 | /* |
| 10883 | * The result is used as the new evaluation set. |
| 10884 | */ |
| 10885 | xmlXPathFreeObject(obj); |
| 10886 | ctxt->context->node = NULL; |
| 10887 | ctxt->context->contextSize = -1; |
| 10888 | ctxt->context->proximityPosition = -1; |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 10889 | valuePush(ctxt, xmlXPtrWrapLocationSet(newlocset)); |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10890 | return (total); |
| 10891 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10892 | #endif /* LIBXML_XPTR_ENABLED */ |
| 10893 | } |
| 10894 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 10895 | "XPath: unknown precompiled operation %d\n", op->op); |
| 10896 | return (total); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10897 | } |
| 10898 | |
| 10899 | /** |
| 10900 | * xmlXPathRunEval: |
| 10901 | * @ctxt: the XPath parser context with the compiled expression |
| 10902 | * |
| 10903 | * Evaluate the Precompiled XPath expression in the given context. |
| 10904 | */ |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10905 | static void |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10906 | xmlXPathRunEval(xmlXPathParserContextPtr ctxt) { |
| 10907 | xmlXPathCompExprPtr comp; |
| 10908 | |
| 10909 | if ((ctxt == NULL) || (ctxt->comp == NULL)) |
| 10910 | return; |
| 10911 | |
| 10912 | if (ctxt->valueTab == NULL) { |
| 10913 | /* Allocate the value stack */ |
| 10914 | ctxt->valueTab = (xmlXPathObjectPtr *) |
| 10915 | xmlMalloc(10 * sizeof(xmlXPathObjectPtr)); |
| 10916 | if (ctxt->valueTab == NULL) { |
Daniel Veillard | d96f6d3 | 2003-10-07 21:25:12 +0000 | [diff] [blame] | 10917 | xmlXPathPErrMemory(ctxt, "creating evaluation context\n"); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10918 | xmlFree(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10919 | } |
| 10920 | ctxt->valueNr = 0; |
| 10921 | ctxt->valueMax = 10; |
| 10922 | ctxt->value = NULL; |
| 10923 | } |
| 10924 | comp = ctxt->comp; |
Aleksey Sanin | 29b6f76 | 2002-05-05 06:59:57 +0000 | [diff] [blame] | 10925 | if(comp->last < 0) { |
| 10926 | xmlGenericError(xmlGenericErrorContext, |
| 10927 | "xmlXPathRunEval: last is less than zero\n"); |
| 10928 | return; |
| 10929 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 10930 | xmlXPathCompOpEval(ctxt, &comp->steps[comp->last]); |
| 10931 | } |
| 10932 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10933 | /************************************************************************ |
| 10934 | * * |
| 10935 | * Public interfaces * |
| 10936 | * * |
| 10937 | ************************************************************************/ |
| 10938 | |
| 10939 | /** |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10940 | * xmlXPathEvalPredicate: |
| 10941 | * @ctxt: the XPath context |
| 10942 | * @res: the Predicate Expression evaluation result |
| 10943 | * |
| 10944 | * Evaluate a predicate result for the current node. |
| 10945 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 10946 | * the result to a boolean. If the result is a number, the result will |
| 10947 | * be converted to true if the number is equal to the position of the |
| 10948 | * context node in the context node list (as returned by the position |
| 10949 | * function) and will be converted to false otherwise; if the result |
| 10950 | * is not a number, then the result will be converted as if by a call |
| 10951 | * to the boolean function. |
| 10952 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10953 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10954 | */ |
| 10955 | int |
| 10956 | xmlXPathEvalPredicate(xmlXPathContextPtr ctxt, xmlXPathObjectPtr res) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 10957 | if ((ctxt == NULL) || (res == NULL)) return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10958 | switch (res->type) { |
| 10959 | case XPATH_BOOLEAN: |
| 10960 | return(res->boolval); |
| 10961 | case XPATH_NUMBER: |
| 10962 | return(res->floatval == ctxt->proximityPosition); |
| 10963 | case XPATH_NODESET: |
| 10964 | case XPATH_XSLT_TREE: |
Daniel Veillard | d8df6c0 | 2001-04-05 16:54:14 +0000 | [diff] [blame] | 10965 | if (res->nodesetval == NULL) |
| 10966 | return(0); |
Daniel Veillard | fbf8a2d | 2001-03-19 15:58:54 +0000 | [diff] [blame] | 10967 | return(res->nodesetval->nodeNr != 0); |
| 10968 | case XPATH_STRING: |
| 10969 | return((res->stringval != NULL) && |
| 10970 | (xmlStrlen(res->stringval) != 0)); |
| 10971 | default: |
| 10972 | STRANGE |
| 10973 | } |
| 10974 | return(0); |
| 10975 | } |
| 10976 | |
| 10977 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10978 | * xmlXPathEvaluatePredicateResult: |
| 10979 | * @ctxt: the XPath Parser context |
| 10980 | * @res: the Predicate Expression evaluation result |
| 10981 | * |
| 10982 | * Evaluate a predicate result for the current node. |
| 10983 | * A PredicateExpr is evaluated by evaluating the Expr and converting |
| 10984 | * the result to a boolean. If the result is a number, the result will |
| 10985 | * be converted to true if the number is equal to the position of the |
| 10986 | * context node in the context node list (as returned by the position |
| 10987 | * function) and will be converted to false otherwise; if the result |
| 10988 | * is not a number, then the result will be converted as if by a call |
| 10989 | * to the boolean function. |
| 10990 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10991 | * Returns 1 if predicate is true, 0 otherwise |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10992 | */ |
| 10993 | int |
| 10994 | xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt, |
| 10995 | xmlXPathObjectPtr res) { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 10996 | if ((ctxt == NULL) || (res == NULL)) return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 10997 | switch (res->type) { |
| 10998 | case XPATH_BOOLEAN: |
| 10999 | return(res->boolval); |
| 11000 | case XPATH_NUMBER: |
Daniel Veillard | 9ea6231 | 2004-04-29 14:04:09 +0000 | [diff] [blame] | 11001 | #if defined(__BORLANDC__) || (defined(_MSC_VER) && (_MSC_VER == 1200)) |
Daniel Veillard | 7c4eb63 | 2004-04-19 21:29:12 +0000 | [diff] [blame] | 11002 | return((res->floatval == ctxt->context->proximityPosition) && |
| 11003 | (!xmlXPathIsNaN(res->floatval))); /* MSC pbm Mark Vakoc !*/ |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 11004 | #else |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11005 | return(res->floatval == ctxt->context->proximityPosition); |
Daniel Veillard | 2582a33 | 2004-04-18 19:49:46 +0000 | [diff] [blame] | 11006 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11007 | case XPATH_NODESET: |
| 11008 | case XPATH_XSLT_TREE: |
Daniel Veillard | 73639a7 | 2001-04-10 14:31:39 +0000 | [diff] [blame] | 11009 | if (res->nodesetval == NULL) |
Daniel Veillard | 911f49a | 2001-04-07 15:39:35 +0000 | [diff] [blame] | 11010 | return(0); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11011 | return(res->nodesetval->nodeNr != 0); |
| 11012 | case XPATH_STRING: |
| 11013 | return((res->stringval != NULL) && |
| 11014 | (xmlStrlen(res->stringval) != 0)); |
William M. Brack | 0817191 | 2003-12-29 02:52:11 +0000 | [diff] [blame] | 11015 | #ifdef LIBXML_XPTR_ENABLED |
| 11016 | case XPATH_LOCATIONSET:{ |
| 11017 | xmlLocationSetPtr ptr = res->user; |
| 11018 | if (ptr == NULL) |
| 11019 | return(0); |
| 11020 | return (ptr->locNr != 0); |
| 11021 | } |
| 11022 | #endif |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11023 | default: |
| 11024 | STRANGE |
| 11025 | } |
| 11026 | return(0); |
| 11027 | } |
| 11028 | |
| 11029 | /** |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 11030 | * xmlXPathCtxtCompile: |
| 11031 | * @ctxt: an XPath context |
| 11032 | * @str: the XPath expression |
| 11033 | * |
| 11034 | * Compile an XPath expression |
| 11035 | * |
| 11036 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
| 11037 | * the caller has to free the object. |
| 11038 | */ |
| 11039 | xmlXPathCompExprPtr |
| 11040 | xmlXPathCtxtCompile(xmlXPathContextPtr ctxt, const xmlChar *str) { |
| 11041 | xmlXPathParserContextPtr pctxt; |
| 11042 | xmlXPathCompExprPtr comp; |
| 11043 | |
| 11044 | xmlXPathInit(); |
| 11045 | |
| 11046 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 11047 | xmlXPathCompileExpr(pctxt); |
| 11048 | |
| 11049 | if( pctxt->error != XPATH_EXPRESSION_OK ) |
| 11050 | { |
| 11051 | xmlXPathFreeParserContext(pctxt); |
| 11052 | return (0); |
| 11053 | } |
| 11054 | |
| 11055 | if (*pctxt->cur != 0) { |
| 11056 | /* |
| 11057 | * aleksey: in some cases this line prints *second* error message |
| 11058 | * (see bug #78858) and probably this should be fixed. |
| 11059 | * However, we are not sure that all error messages are printed |
| 11060 | * out in other places. It's not critical so we leave it as-is for now |
| 11061 | */ |
| 11062 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11063 | comp = NULL; |
| 11064 | } else { |
| 11065 | comp = pctxt->comp; |
| 11066 | pctxt->comp = NULL; |
| 11067 | } |
| 11068 | xmlXPathFreeParserContext(pctxt); |
| 11069 | if (comp != NULL) { |
| 11070 | comp->expr = xmlStrdup(str); |
| 11071 | #ifdef DEBUG_EVAL_COUNTS |
| 11072 | comp->string = xmlStrdup(str); |
| 11073 | comp->nb = 0; |
| 11074 | #endif |
| 11075 | } |
| 11076 | return(comp); |
| 11077 | } |
| 11078 | |
| 11079 | /** |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11080 | * xmlXPathCompile: |
| 11081 | * @str: the XPath expression |
| 11082 | * |
| 11083 | * Compile an XPath expression |
| 11084 | * |
Daniel Veillard | 591b4be | 2003-02-09 23:33:36 +0000 | [diff] [blame] | 11085 | * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL. |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11086 | * the caller has to free the object. |
| 11087 | */ |
| 11088 | xmlXPathCompExprPtr |
| 11089 | xmlXPathCompile(const xmlChar *str) { |
Daniel Veillard | 4773df2 | 2004-01-23 13:15:13 +0000 | [diff] [blame] | 11090 | return(xmlXPathCtxtCompile(NULL, str)); |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11091 | } |
| 11092 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11093 | /** |
| 11094 | * xmlXPathCompiledEval: |
| 11095 | * @comp: the compiled XPath expression |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11096 | * @ctx: the XPath context |
| 11097 | * |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11098 | * Evaluate the Precompiled XPath expression in the given context. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11099 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11100 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11101 | * the caller has to free the object. |
| 11102 | */ |
| 11103 | xmlXPathObjectPtr |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11104 | xmlXPathCompiledEval(xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11105 | xmlXPathParserContextPtr ctxt; |
| 11106 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 11107 | int stack = 0; |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 11108 | #ifndef LIBXML_THREAD_ENABLED |
| 11109 | static int reentance = 0; |
| 11110 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11111 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11112 | CHECK_CTXT(ctx) |
| 11113 | |
| 11114 | if (comp == NULL) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11115 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11116 | xmlXPathInit(); |
| 11117 | |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 11118 | #ifndef LIBXML_THREAD_ENABLED |
| 11119 | reentance++; |
| 11120 | if (reentance > 1) |
| 11121 | xmlXPathDisableOptimizer = 1; |
| 11122 | #endif |
| 11123 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11124 | #ifdef DEBUG_EVAL_COUNTS |
| 11125 | comp->nb++; |
| 11126 | if ((comp->string != NULL) && (comp->nb > 100)) { |
| 11127 | fprintf(stderr, "100 x %s\n", comp->string); |
| 11128 | comp->nb = 0; |
| 11129 | } |
| 11130 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11131 | ctxt = xmlXPathCompParserContext(comp, ctx); |
| 11132 | xmlXPathRunEval(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11133 | |
| 11134 | if (ctxt->value == NULL) { |
| 11135 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11136 | "xmlXPathCompiledEval: evaluation failed\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11137 | res = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11138 | } else { |
| 11139 | res = valuePop(ctxt); |
| 11140 | } |
| 11141 | |
Daniel Veillard | f06307e | 2001-07-03 10:35:50 +0000 | [diff] [blame] | 11142 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11143 | do { |
| 11144 | tmp = valuePop(ctxt); |
| 11145 | if (tmp != NULL) { |
| 11146 | if (tmp != init) |
| 11147 | stack++; |
| 11148 | xmlXPathFreeObject(tmp); |
| 11149 | } |
| 11150 | } while (tmp != NULL); |
| 11151 | if ((stack != 0) && (res != NULL)) { |
| 11152 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11153 | "xmlXPathCompiledEval: %d object left on the stack\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11154 | stack); |
| 11155 | } |
| 11156 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11157 | xmlXPathFreeObject(res); |
| 11158 | res = NULL; |
| 11159 | } |
| 11160 | |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11161 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11162 | ctxt->comp = NULL; |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11163 | xmlXPathFreeParserContext(ctxt); |
Daniel Veillard | 8146394 | 2001-10-16 12:34:39 +0000 | [diff] [blame] | 11164 | #ifndef LIBXML_THREAD_ENABLED |
| 11165 | reentance--; |
| 11166 | #endif |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11167 | return(res); |
| 11168 | } |
| 11169 | |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11170 | /** |
| 11171 | * xmlXPathEvalExpr: |
| 11172 | * @ctxt: the XPath Parser context |
| 11173 | * |
| 11174 | * Parse and evaluate an XPath expression in the given context, |
| 11175 | * then push the result on the context stack |
| 11176 | */ |
| 11177 | void |
| 11178 | xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 11179 | if (ctxt == NULL) return; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11180 | xmlXPathCompileExpr(ctxt); |
Aleksey Sanin | 50fe8b1 | 2002-05-07 16:21:36 +0000 | [diff] [blame] | 11181 | CHECK_ERROR; |
Daniel Veillard | afcbe1c | 2001-03-19 10:57:13 +0000 | [diff] [blame] | 11182 | xmlXPathRunEval(ctxt); |
| 11183 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11184 | |
| 11185 | /** |
| 11186 | * xmlXPathEval: |
| 11187 | * @str: the XPath expression |
| 11188 | * @ctx: the XPath context |
| 11189 | * |
| 11190 | * Evaluate the XPath Location Path in the given context. |
| 11191 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11192 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11193 | * the caller has to free the object. |
| 11194 | */ |
| 11195 | xmlXPathObjectPtr |
| 11196 | xmlXPathEval(const xmlChar *str, xmlXPathContextPtr ctx) { |
| 11197 | xmlXPathParserContextPtr ctxt; |
| 11198 | xmlXPathObjectPtr res, tmp, init = NULL; |
| 11199 | int stack = 0; |
| 11200 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11201 | CHECK_CTXT(ctx) |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11202 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11203 | xmlXPathInit(); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11204 | |
| 11205 | ctxt = xmlXPathNewParserContext(str, ctx); |
| 11206 | xmlXPathEvalExpr(ctxt); |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 11207 | |
| 11208 | if (ctxt->value == NULL) { |
| 11209 | xmlGenericError(xmlGenericErrorContext, |
| 11210 | "xmlXPathEval: evaluation failed\n"); |
| 11211 | res = NULL; |
| 11212 | } else if (*ctxt->cur != 0) { |
| 11213 | xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11214 | res = NULL; |
| 11215 | } else { |
| 11216 | res = valuePop(ctxt); |
| 11217 | } |
| 11218 | |
| 11219 | do { |
| 11220 | tmp = valuePop(ctxt); |
| 11221 | if (tmp != NULL) { |
| 11222 | if (tmp != init) |
| 11223 | stack++; |
| 11224 | xmlXPathFreeObject(tmp); |
| 11225 | } |
| 11226 | } while (tmp != NULL); |
| 11227 | if ((stack != 0) && (res != NULL)) { |
| 11228 | xmlGenericError(xmlGenericErrorContext, |
| 11229 | "xmlXPathEval: %d object left on the stack\n", |
| 11230 | stack); |
| 11231 | } |
| 11232 | if (ctxt->error != XPATH_EXPRESSION_OK) { |
| 11233 | xmlXPathFreeObject(res); |
| 11234 | res = NULL; |
| 11235 | } |
| 11236 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11237 | xmlXPathFreeParserContext(ctxt); |
| 11238 | return(res); |
| 11239 | } |
| 11240 | |
| 11241 | /** |
| 11242 | * xmlXPathEvalExpression: |
| 11243 | * @str: the XPath expression |
| 11244 | * @ctxt: the XPath context |
| 11245 | * |
| 11246 | * Evaluate the XPath expression in the given context. |
| 11247 | * |
| 11248 | * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL. |
| 11249 | * the caller has to free the object. |
| 11250 | */ |
| 11251 | xmlXPathObjectPtr |
| 11252 | xmlXPathEvalExpression(const xmlChar *str, xmlXPathContextPtr ctxt) { |
| 11253 | xmlXPathParserContextPtr pctxt; |
| 11254 | xmlXPathObjectPtr res, tmp; |
| 11255 | int stack = 0; |
| 11256 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11257 | CHECK_CTXT(ctxt) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11258 | |
William M. Brack | f13f77f | 2004-11-12 16:03:48 +0000 | [diff] [blame] | 11259 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11260 | |
| 11261 | pctxt = xmlXPathNewParserContext(str, ctxt); |
| 11262 | xmlXPathEvalExpr(pctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11263 | |
| 11264 | if (*pctxt->cur != 0) { |
| 11265 | xmlXPatherror(pctxt, __FILE__, __LINE__, XPATH_EXPR_ERROR); |
| 11266 | res = NULL; |
| 11267 | } else { |
| 11268 | res = valuePop(pctxt); |
| 11269 | } |
| 11270 | do { |
| 11271 | tmp = valuePop(pctxt); |
| 11272 | if (tmp != NULL) { |
| 11273 | xmlXPathFreeObject(tmp); |
| 11274 | stack++; |
| 11275 | } |
| 11276 | } while (tmp != NULL); |
| 11277 | if ((stack != 0) && (res != NULL)) { |
| 11278 | xmlGenericError(xmlGenericErrorContext, |
| 11279 | "xmlXPathEvalExpression: %d object left on the stack\n", |
| 11280 | stack); |
| 11281 | } |
| 11282 | xmlXPathFreeParserContext(pctxt); |
| 11283 | return(res); |
| 11284 | } |
| 11285 | |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11286 | /************************************************************************ |
| 11287 | * * |
| 11288 | * Extra functions not pertaining to the XPath spec * |
| 11289 | * * |
| 11290 | ************************************************************************/ |
| 11291 | /** |
| 11292 | * xmlXPathEscapeUriFunction: |
| 11293 | * @ctxt: the XPath Parser context |
| 11294 | * @nargs: the number of arguments |
| 11295 | * |
| 11296 | * Implement the escape-uri() XPath function |
| 11297 | * string escape-uri(string $str, bool $escape-reserved) |
| 11298 | * |
| 11299 | * This function applies the URI escaping rules defined in section 2 of [RFC |
| 11300 | * 2396] to the string supplied as $uri-part, which typically represents all |
| 11301 | * or part of a URI. The effect of the function is to replace any special |
| 11302 | * character in the string by an escape sequence of the form %xx%yy..., |
| 11303 | * where xxyy... is the hexadecimal representation of the octets used to |
| 11304 | * represent the character in UTF-8. |
| 11305 | * |
| 11306 | * The set of characters that are escaped depends on the setting of the |
| 11307 | * boolean argument $escape-reserved. |
| 11308 | * |
| 11309 | * If $escape-reserved is true, all characters are escaped other than lower |
| 11310 | * case letters a-z, upper case letters A-Z, digits 0-9, and the characters |
| 11311 | * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!" |
| 11312 | * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only |
| 11313 | * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and |
| 11314 | * A-F). |
| 11315 | * |
| 11316 | * If $escape-reserved is false, the behavior differs in that characters |
| 11317 | * referred to in [RFC 2396] as reserved characters are not escaped. These |
| 11318 | * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",". |
| 11319 | * |
| 11320 | * [RFC 2396] does not define whether escaped URIs should use lower case or |
| 11321 | * upper case for hexadecimal digits. To ensure that escaped URIs can be |
| 11322 | * compared using string comparison functions, this function must always use |
| 11323 | * the upper-case letters A-F. |
| 11324 | * |
| 11325 | * Generally, $escape-reserved should be set to true when escaping a string |
| 11326 | * that is to form a single part of a URI, and to false when escaping an |
| 11327 | * entire URI or URI reference. |
| 11328 | * |
| 11329 | * In the case of non-ascii characters, the string is encoded according to |
| 11330 | * utf-8 and then converted according to RFC 2396. |
| 11331 | * |
| 11332 | * Examples |
| 11333 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true()) |
| 11334 | * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean" |
| 11335 | * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false()) |
| 11336 | * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean" |
| 11337 | * |
| 11338 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 11339 | static void |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11340 | xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
| 11341 | xmlXPathObjectPtr str; |
| 11342 | int escape_reserved; |
| 11343 | xmlBufferPtr target; |
| 11344 | xmlChar *cptr; |
| 11345 | xmlChar escape[4]; |
| 11346 | |
| 11347 | CHECK_ARITY(2); |
| 11348 | |
| 11349 | escape_reserved = xmlXPathPopBoolean(ctxt); |
| 11350 | |
| 11351 | CAST_TO_STRING; |
| 11352 | str = valuePop(ctxt); |
| 11353 | |
| 11354 | target = xmlBufferCreate(); |
| 11355 | |
| 11356 | escape[0] = '%'; |
| 11357 | escape[3] = 0; |
| 11358 | |
| 11359 | if (target) { |
| 11360 | for (cptr = str->stringval; *cptr; cptr++) { |
| 11361 | if ((*cptr >= 'A' && *cptr <= 'Z') || |
| 11362 | (*cptr >= 'a' && *cptr <= 'z') || |
| 11363 | (*cptr >= '0' && *cptr <= '9') || |
| 11364 | *cptr == '-' || *cptr == '_' || *cptr == '.' || |
| 11365 | *cptr == '!' || *cptr == '~' || *cptr == '*' || |
| 11366 | *cptr == '\''|| *cptr == '(' || *cptr == ')' || |
| 11367 | (*cptr == '%' && |
| 11368 | ((cptr[1] >= 'A' && cptr[1] <= 'F') || |
| 11369 | (cptr[1] >= 'a' && cptr[1] <= 'f') || |
| 11370 | (cptr[1] >= '0' && cptr[1] <= '9')) && |
| 11371 | ((cptr[2] >= 'A' && cptr[2] <= 'F') || |
| 11372 | (cptr[2] >= 'a' && cptr[2] <= 'f') || |
| 11373 | (cptr[2] >= '0' && cptr[2] <= '9'))) || |
| 11374 | (!escape_reserved && |
| 11375 | (*cptr == ';' || *cptr == '/' || *cptr == '?' || |
| 11376 | *cptr == ':' || *cptr == '@' || *cptr == '&' || |
| 11377 | *cptr == '=' || *cptr == '+' || *cptr == '$' || |
| 11378 | *cptr == ','))) { |
| 11379 | xmlBufferAdd(target, cptr, 1); |
| 11380 | } else { |
| 11381 | if ((*cptr >> 4) < 10) |
| 11382 | escape[1] = '0' + (*cptr >> 4); |
| 11383 | else |
| 11384 | escape[1] = 'A' - 10 + (*cptr >> 4); |
| 11385 | if ((*cptr & 0xF) < 10) |
| 11386 | escape[2] = '0' + (*cptr & 0xF); |
| 11387 | else |
| 11388 | escape[2] = 'A' - 10 + (*cptr & 0xF); |
| 11389 | |
| 11390 | xmlBufferAdd(target, &escape[0], 3); |
| 11391 | } |
| 11392 | } |
| 11393 | } |
| 11394 | valuePush(ctxt, xmlXPathNewString(xmlBufferContent(target))); |
| 11395 | xmlBufferFree(target); |
| 11396 | xmlXPathFreeObject(str); |
| 11397 | } |
| 11398 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11399 | /** |
| 11400 | * xmlXPathRegisterAllFunctions: |
| 11401 | * @ctxt: the XPath context |
| 11402 | * |
| 11403 | * Registers all default XPath functions in this context |
| 11404 | */ |
| 11405 | void |
| 11406 | xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt) |
| 11407 | { |
| 11408 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"boolean", |
| 11409 | xmlXPathBooleanFunction); |
| 11410 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"ceiling", |
| 11411 | xmlXPathCeilingFunction); |
| 11412 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"count", |
| 11413 | xmlXPathCountFunction); |
| 11414 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"concat", |
| 11415 | xmlXPathConcatFunction); |
| 11416 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"contains", |
| 11417 | xmlXPathContainsFunction); |
| 11418 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"id", |
| 11419 | xmlXPathIdFunction); |
| 11420 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"false", |
| 11421 | xmlXPathFalseFunction); |
| 11422 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"floor", |
| 11423 | xmlXPathFloorFunction); |
| 11424 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"last", |
| 11425 | xmlXPathLastFunction); |
| 11426 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"lang", |
| 11427 | xmlXPathLangFunction); |
| 11428 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"local-name", |
| 11429 | xmlXPathLocalNameFunction); |
| 11430 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"not", |
| 11431 | xmlXPathNotFunction); |
| 11432 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"name", |
| 11433 | xmlXPathNameFunction); |
| 11434 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"namespace-uri", |
| 11435 | xmlXPathNamespaceURIFunction); |
| 11436 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"normalize-space", |
| 11437 | xmlXPathNormalizeFunction); |
| 11438 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"number", |
| 11439 | xmlXPathNumberFunction); |
| 11440 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"position", |
| 11441 | xmlXPathPositionFunction); |
| 11442 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"round", |
| 11443 | xmlXPathRoundFunction); |
| 11444 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string", |
| 11445 | xmlXPathStringFunction); |
| 11446 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"string-length", |
| 11447 | xmlXPathStringLengthFunction); |
| 11448 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"starts-with", |
| 11449 | xmlXPathStartsWithFunction); |
| 11450 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring", |
| 11451 | xmlXPathSubstringFunction); |
| 11452 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-before", |
| 11453 | xmlXPathSubstringBeforeFunction); |
| 11454 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"substring-after", |
| 11455 | xmlXPathSubstringAfterFunction); |
| 11456 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"sum", |
| 11457 | xmlXPathSumFunction); |
| 11458 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"true", |
| 11459 | xmlXPathTrueFunction); |
| 11460 | xmlXPathRegisterFunc(ctxt, (const xmlChar *)"translate", |
| 11461 | xmlXPathTranslateFunction); |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11462 | |
| 11463 | xmlXPathRegisterFuncNS(ctxt, (const xmlChar *)"escape-uri", |
| 11464 | (const xmlChar *)"http://www.w3.org/2002/08/xquery-functions", |
| 11465 | xmlXPathEscapeUriFunction); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11466 | } |
| 11467 | |
| 11468 | #endif /* LIBXML_XPATH_ENABLED */ |