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
6 * Reference: W3C Recommendation 16 November 1999
7 * http://www.w3.org/TR/1999/REC-xpath-19991116
9 * http://www.w3.org/TR/xpath
11 * See Copyright for the status of this software
13 * Author: daniel@veillard.com
22 #ifdef HAVE_SYS_TYPES_H
23 #include <sys/types.h>
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>
48 #ifdef LIBXML_DEBUG_ENABLED
49 #include <libxml/debugXML.h>
51 #include <libxml/xmlerror.h>
52 #include <libxml/threads.h>
53 #include <libxml/globals.h>
54 #ifdef LIBXML_PATTERN_ENABLED
55 #include <libxml/pattern.h>
58 #ifdef LIBXML_PATTERN_ENABLED
59 #define XPATH_STREAMING
63 xmlGenericError(xmlGenericErrorContext, \
64 "Unimplemented block at %s:%d\n", \
68 * XP_OPTIMIZED_NON_ELEM_COMPARISON:
69 * If defined, this will use xmlXPathCmpNodesExt() instead of
70 * xmlXPathCmpNodes(). The new function is optimized comparison of
71 * non-element nodes; actually it will speed up comparison only if
72 * xmlXPathOrderDocElems() was called in order to index the elements of
73 * a tree in document order; Libxslt does such an indexing, thus it will
74 * benefit from this optimization.
76 #define XP_OPTIMIZED_NON_ELEM_COMPARISON
79 * XP_OPTIMIZED_FILTER_FIRST:
80 * If defined, this will optimize expressions like "key('foo', 'val')[b][1]"
81 * in a way, that it stop evaluation at the first node.
83 #define XP_OPTIMIZED_FILTER_FIRST
87 * Internal flag to enable tracking of how much XPath objects have been
90 /* #define XP_DEBUG_OBJ_USAGE */
94 * There are a few spots where some tests are done which depend upon ascii
95 * data. These should be enhanced for full UTF8 support (see particularly
96 * any use of the macros IS_ASCII_CHARACTER and IS_ASCII_DIGIT)
99 #if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
101 /************************************************************************
103 * Floating point stuff *
105 ************************************************************************/
107 #ifndef TRIO_REPLACE_STDIO
108 #define TRIO_PUBLIC static
113 * The lack of portability of this section of the libc is annoying !
115 double xmlXPathNAN
= 0;
116 double xmlXPathPINF
= 1;
117 double xmlXPathNINF
= -1;
118 static double xmlXPathNZERO
= 0; /* not exported from headers */
119 static int xmlXPathInitialized
= 0;
124 * Initialize the XPath environment
128 if (xmlXPathInitialized
) return;
130 xmlXPathPINF
= trio_pinf();
131 xmlXPathNINF
= trio_ninf();
132 xmlXPathNAN
= trio_nan();
133 xmlXPathNZERO
= trio_nzero();
135 xmlXPathInitialized
= 1;
140 * @val: a double value
142 * Provides a portable isnan() function to detect whether a double
143 * is a NotaNumber. Based on trio code
144 * http://sourceforge.net/projects/ctrio/
146 * Returns 1 if the value is a NaN, 0 otherwise
149 xmlXPathIsNaN(double val
) {
150 return(trio_isnan(val
));
155 * @val: a double value
157 * Provides a portable isinf() function to detect whether a double
158 * is a +Infinite or -Infinite. Based on trio code
159 * http://sourceforge.net/projects/ctrio/
161 * Returns 1 vi the value is +Infinite, -1 if -Infinite, 0 otherwise
164 xmlXPathIsInf(double val
) {
165 return(trio_isinf(val
));
168 #endif /* SCHEMAS or XPATH */
169 #ifdef LIBXML_XPATH_ENABLED
172 * @val: a double value
174 * Provides a portable function to detect the sign of a double
175 * Modified from trio code
176 * http://sourceforge.net/projects/ctrio/
178 * Returns 1 if the value is Negative, 0 if positive
181 xmlXPathGetSign(double val
) {
182 return(trio_signbit(val
));
187 * TODO: when compatibility allows remove all "fake node libxslt" strings
188 * the test should just be name[0] = ' '
190 #ifdef DEBUG_XPATH_EXPRESSION
193 #define DEBUG_EVAL_COUNTS
196 static xmlNs xmlXPathXMLNamespaceStruct
= {
204 static xmlNsPtr xmlXPathXMLNamespace
= &xmlXPathXMLNamespaceStruct
;
205 #ifndef LIBXML_THREAD_ENABLED
207 * Optimizer is disabled only when threaded apps are detected while
208 * the library ain't compiled for thread safety.
210 static int xmlXPathDisableOptimizer
= 0;
213 /************************************************************************
215 * Error handling routines *
217 ************************************************************************/
223 * Macro to raise an XPath error and return NULL.
225 #define XP_ERRORNULL(X) \
226 { xmlXPathErr(ctxt, X); return(NULL); }
229 * The array xmlXPathErrorMessages corresponds to the enum xmlXPathError
231 static const char *xmlXPathErrorMessages
[] = {
234 "Unfinished literal\n",
235 "Start of literal\n",
236 "Expected $ for variable reference\n",
237 "Undefined variable\n",
238 "Invalid predicate\n",
239 "Invalid expression\n",
240 "Missing closing curly brace\n",
241 "Unregistered function\n",
244 "Invalid number of arguments\n",
245 "Invalid context size\n",
246 "Invalid context position\n",
247 "Memory allocation error\n",
250 "Sub resource error\n",
251 "Undefined namespace prefix\n",
253 "Char out of XML range\n",
254 "Invalid or incomplete context\n",
255 "?? Unknown error ??\n" /* Must be last in the list! */
257 #define MAXERRNO ((int)(sizeof(xmlXPathErrorMessages) / \
258 sizeof(xmlXPathErrorMessages[0])) - 1)
261 * @ctxt: an XPath context
262 * @extra: extra informations
264 * Handle a redefinition of attribute error
267 xmlXPathErrMemory(xmlXPathContextPtr ctxt
, const char *extra
)
273 xmlStrPrintf(buf
, 200,
274 BAD_CAST
"Memory allocation failed : %s\n",
276 ctxt
->lastError
.message
= (char *) xmlStrdup(buf
);
278 ctxt
->lastError
.message
= (char *)
279 xmlStrdup(BAD_CAST
"Memory allocation failed\n");
281 ctxt
->lastError
.domain
= XML_FROM_XPATH
;
282 ctxt
->lastError
.code
= XML_ERR_NO_MEMORY
;
283 if (ctxt
->error
!= NULL
)
284 ctxt
->error(ctxt
->userData
, &ctxt
->lastError
);
287 __xmlRaiseError(NULL
, NULL
, NULL
,
288 NULL
, NULL
, XML_FROM_XPATH
,
289 XML_ERR_NO_MEMORY
, XML_ERR_FATAL
, NULL
, 0,
290 extra
, NULL
, NULL
, 0, 0,
291 "Memory allocation failed : %s\n", extra
);
293 __xmlRaiseError(NULL
, NULL
, NULL
,
294 NULL
, NULL
, XML_FROM_XPATH
,
295 XML_ERR_NO_MEMORY
, XML_ERR_FATAL
, NULL
, 0,
296 NULL
, NULL
, NULL
, 0, 0,
297 "Memory allocation failed\n");
302 * xmlXPathPErrMemory:
303 * @ctxt: an XPath parser context
304 * @extra: extra informations
306 * Handle a redefinition of attribute error
309 xmlXPathPErrMemory(xmlXPathParserContextPtr ctxt
, const char *extra
)
312 xmlXPathErrMemory(NULL
, extra
);
314 ctxt
->error
= XPATH_MEMORY_ERROR
;
315 xmlXPathErrMemory(ctxt
->context
, extra
);
321 * @ctxt: a XPath parser context
322 * @error: the error code
324 * Handle an XPath error
327 xmlXPathErr(xmlXPathParserContextPtr ctxt
, int error
)
329 if ((error
< 0) || (error
> MAXERRNO
))
332 __xmlRaiseError(NULL
, NULL
, NULL
,
333 NULL
, NULL
, XML_FROM_XPATH
,
334 error
+ XML_XPATH_EXPRESSION_OK
- XPATH_EXPRESSION_OK
,
335 XML_ERR_ERROR
, NULL
, 0,
336 NULL
, NULL
, NULL
, 0, 0,
337 "%s", xmlXPathErrorMessages
[error
]);
341 if (ctxt
->context
== NULL
) {
342 __xmlRaiseError(NULL
, NULL
, NULL
,
343 NULL
, NULL
, XML_FROM_XPATH
,
344 error
+ XML_XPATH_EXPRESSION_OK
- XPATH_EXPRESSION_OK
,
345 XML_ERR_ERROR
, NULL
, 0,
346 (const char *) ctxt
->base
, NULL
, NULL
,
347 ctxt
->cur
- ctxt
->base
, 0,
348 "%s", xmlXPathErrorMessages
[error
]);
352 /* cleanup current last error */
353 xmlResetError(&ctxt
->context
->lastError
);
355 ctxt
->context
->lastError
.domain
= XML_FROM_XPATH
;
356 ctxt
->context
->lastError
.code
= error
+ XML_XPATH_EXPRESSION_OK
-
358 ctxt
->context
->lastError
.level
= XML_ERR_ERROR
;
359 ctxt
->context
->lastError
.str1
= (char *) xmlStrdup(ctxt
->base
);
360 ctxt
->context
->lastError
.int1
= ctxt
->cur
- ctxt
->base
;
361 ctxt
->context
->lastError
.node
= ctxt
->context
->debugNode
;
362 if (ctxt
->context
->error
!= NULL
) {
363 ctxt
->context
->error(ctxt
->context
->userData
,
364 &ctxt
->context
->lastError
);
366 __xmlRaiseError(NULL
, NULL
, NULL
,
367 NULL
, ctxt
->context
->debugNode
, XML_FROM_XPATH
,
368 error
+ XML_XPATH_EXPRESSION_OK
- XPATH_EXPRESSION_OK
,
369 XML_ERR_ERROR
, NULL
, 0,
370 (const char *) ctxt
->base
, NULL
, NULL
,
371 ctxt
->cur
- ctxt
->base
, 0,
372 "%s", xmlXPathErrorMessages
[error
]);
379 * @ctxt: the XPath Parser context
380 * @file: the file name
381 * @line: the line number
382 * @no: the error number
384 * Formats an error message.
387 xmlXPatherror(xmlXPathParserContextPtr ctxt
, const char *file ATTRIBUTE_UNUSED
,
388 int line ATTRIBUTE_UNUSED
, int no
) {
389 xmlXPathErr(ctxt
, no
);
392 /************************************************************************
396 ************************************************************************/
401 * Pointer-list for various purposes.
403 typedef struct _xmlPointerList xmlPointerList
;
404 typedef xmlPointerList
*xmlPointerListPtr
;
405 struct _xmlPointerList
{
411 * TODO: Since such a list-handling is used in xmlschemas.c and libxslt
412 * and here, we should make the functions public.
415 xmlPointerListAddSize(xmlPointerListPtr list
,
419 if (list
->items
== NULL
) {
420 if (initialSize
<= 0)
422 list
->items
= (void **) xmlMalloc(
423 initialSize
* sizeof(void *));
424 if (list
->items
== NULL
) {
425 xmlXPathErrMemory(NULL
,
426 "xmlPointerListCreate: allocating item\n");
430 list
->size
= initialSize
;
431 } else if (list
->size
<= list
->number
) {
433 list
->items
= (void **) xmlRealloc(list
->items
,
434 list
->size
* sizeof(void *));
435 if (list
->items
== NULL
) {
436 xmlXPathErrMemory(NULL
,
437 "xmlPointerListCreate: re-allocating item\n");
442 list
->items
[list
->number
++] = item
;
447 * xsltPointerListCreate:
449 * Creates an xsltPointerList structure.
451 * Returns a xsltPointerList structure or NULL in case of an error.
453 static xmlPointerListPtr
454 xmlPointerListCreate(int initialSize
)
456 xmlPointerListPtr ret
;
458 ret
= xmlMalloc(sizeof(xmlPointerList
));
460 xmlXPathErrMemory(NULL
,
461 "xmlPointerListCreate: allocating item\n");
464 memset(ret
, 0, sizeof(xmlPointerList
));
465 if (initialSize
> 0) {
466 xmlPointerListAddSize(ret
, NULL
, initialSize
);
473 * xsltPointerListFree:
475 * Frees the xsltPointerList structure. This does not free
476 * the content of the list.
479 xmlPointerListFree(xmlPointerListPtr list
)
483 if (list
->items
!= NULL
)
484 xmlFree(list
->items
);
488 /************************************************************************
492 ************************************************************************/
509 XPATH_OP_RESET
, /* 10 */
511 XPATH_OP_VALUE
, /* 12 */
516 XPATH_OP_FILTER
, /* 17 */
517 XPATH_OP_SORT
/* 18 */
518 #ifdef LIBXML_XPTR_ENABLED
525 AXIS_ANCESTOR_OR_SELF
,
529 AXIS_DESCENDANT_OR_SELF
,
531 AXIS_FOLLOWING_SIBLING
,
535 AXIS_PRECEDING_SIBLING
,
550 NODE_TYPE_COMMENT
= XML_COMMENT_NODE
,
551 NODE_TYPE_TEXT
= XML_TEXT_NODE
,
552 NODE_TYPE_PI
= XML_PI_NODE
555 #define XP_REWRITE_DOS_CHILD_ELEM 1
557 typedef struct _xmlXPathStepOp xmlXPathStepOp
;
558 typedef xmlXPathStepOp
*xmlXPathStepOpPtr
;
559 struct _xmlXPathStepOp
{
560 xmlXPathOp op
; /* The identifier of the operation */
561 int ch1
; /* First child */
562 int ch2
; /* Second child */
573 struct _xmlXPathCompExpr
{
574 int nbStep
; /* Number of steps in this expression */
575 int maxStep
; /* Maximum number of steps allocated */
576 xmlXPathStepOp
*steps
; /* ops for computation of this expression */
577 int last
; /* index of last step in expression */
578 xmlChar
*expr
; /* the expression being computed */
579 xmlDictPtr dict
; /* the dictionnary to use if any */
580 #ifdef DEBUG_EVAL_COUNTS
584 #ifdef XPATH_STREAMING
585 xmlPatternPtr stream
;
589 /************************************************************************
591 * Forward declarations *
593 ************************************************************************/
595 xmlXPathFreeValueTree(xmlNodeSetPtr obj
);
597 xmlXPathReleaseObject(xmlXPathContextPtr ctxt
, xmlXPathObjectPtr obj
);
599 xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt
,
600 xmlXPathStepOpPtr op
, xmlNodePtr
*first
);
602 xmlXPathCompOpEvalToBoolean(xmlXPathParserContextPtr ctxt
,
603 xmlXPathStepOpPtr op
,
606 /************************************************************************
608 * Parser Type functions *
610 ************************************************************************/
613 * xmlXPathNewCompExpr:
615 * Create a new Xpath component
617 * Returns the newly allocated xmlXPathCompExprPtr or NULL in case of error
619 static xmlXPathCompExprPtr
620 xmlXPathNewCompExpr(void) {
621 xmlXPathCompExprPtr cur
;
623 cur
= (xmlXPathCompExprPtr
) xmlMalloc(sizeof(xmlXPathCompExpr
));
625 xmlXPathErrMemory(NULL
, "allocating component\n");
628 memset(cur
, 0, sizeof(xmlXPathCompExpr
));
631 cur
->steps
= (xmlXPathStepOp
*) xmlMalloc(cur
->maxStep
*
632 sizeof(xmlXPathStepOp
));
633 if (cur
->steps
== NULL
) {
634 xmlXPathErrMemory(NULL
, "allocating steps\n");
638 memset(cur
->steps
, 0, cur
->maxStep
* sizeof(xmlXPathStepOp
));
640 #ifdef DEBUG_EVAL_COUNTS
647 * xmlXPathFreeCompExpr:
648 * @comp: an XPATH comp
650 * Free up the memory allocated by @comp
653 xmlXPathFreeCompExpr(xmlXPathCompExprPtr comp
)
655 xmlXPathStepOpPtr op
;
660 if (comp
->dict
== NULL
) {
661 for (i
= 0; i
< comp
->nbStep
; i
++) {
662 op
= &comp
->steps
[i
];
663 if (op
->value4
!= NULL
) {
664 if (op
->op
== XPATH_OP_VALUE
)
665 xmlXPathFreeObject(op
->value4
);
669 if (op
->value5
!= NULL
)
673 for (i
= 0; i
< comp
->nbStep
; i
++) {
674 op
= &comp
->steps
[i
];
675 if (op
->value4
!= NULL
) {
676 if (op
->op
== XPATH_OP_VALUE
)
677 xmlXPathFreeObject(op
->value4
);
680 xmlDictFree(comp
->dict
);
682 if (comp
->steps
!= NULL
) {
683 xmlFree(comp
->steps
);
685 #ifdef DEBUG_EVAL_COUNTS
686 if (comp
->string
!= NULL
) {
687 xmlFree(comp
->string
);
690 #ifdef XPATH_STREAMING
691 if (comp
->stream
!= NULL
) {
692 xmlFreePatternList(comp
->stream
);
695 if (comp
->expr
!= NULL
) {
703 * xmlXPathCompExprAdd:
704 * @comp: the compiled expression
705 * @ch1: first child index
706 * @ch2: second child index
708 * @value: the first int value
709 * @value2: the second int value
710 * @value3: the third int value
711 * @value4: the first string value
712 * @value5: the second string value
714 * Add a step to an XPath Compiled Expression
716 * Returns -1 in case of failure, the index otherwise
719 xmlXPathCompExprAdd(xmlXPathCompExprPtr comp
, int ch1
, int ch2
,
720 xmlXPathOp op
, int value
,
721 int value2
, int value3
, void *value4
, void *value5
) {
722 if (comp
->nbStep
>= comp
->maxStep
) {
723 xmlXPathStepOp
*real
;
726 real
= (xmlXPathStepOp
*) xmlRealloc(comp
->steps
,
727 comp
->maxStep
* sizeof(xmlXPathStepOp
));
730 xmlXPathErrMemory(NULL
, "adding step\n");
735 comp
->last
= comp
->nbStep
;
736 comp
->steps
[comp
->nbStep
].rewriteType
= 0;
737 comp
->steps
[comp
->nbStep
].ch1
= ch1
;
738 comp
->steps
[comp
->nbStep
].ch2
= ch2
;
739 comp
->steps
[comp
->nbStep
].op
= op
;
740 comp
->steps
[comp
->nbStep
].value
= value
;
741 comp
->steps
[comp
->nbStep
].value2
= value2
;
742 comp
->steps
[comp
->nbStep
].value3
= value3
;
743 if ((comp
->dict
!= NULL
) &&
744 ((op
== XPATH_OP_FUNCTION
) || (op
== XPATH_OP_VARIABLE
) ||
745 (op
== XPATH_OP_COLLECT
))) {
746 if (value4
!= NULL
) {
747 comp
->steps
[comp
->nbStep
].value4
= (xmlChar
*)
748 (void *)xmlDictLookup(comp
->dict
, value4
, -1);
751 comp
->steps
[comp
->nbStep
].value4
= NULL
;
752 if (value5
!= NULL
) {
753 comp
->steps
[comp
->nbStep
].value5
= (xmlChar
*)
754 (void *)xmlDictLookup(comp
->dict
, value5
, -1);
757 comp
->steps
[comp
->nbStep
].value5
= NULL
;
759 comp
->steps
[comp
->nbStep
].value4
= value4
;
760 comp
->steps
[comp
->nbStep
].value5
= value5
;
762 comp
->steps
[comp
->nbStep
].cache
= NULL
;
763 return(comp
->nbStep
++);
768 * @comp: the compiled expression
769 * @op: operation index
771 * Swaps 2 operations in the compiled expression
774 xmlXPathCompSwap(xmlXPathStepOpPtr op
) {
777 #ifndef LIBXML_THREAD_ENABLED
779 * Since this manipulates possibly shared variables, this is
780 * disabled if one detects that the library is used in a multithreaded
783 if (xmlXPathDisableOptimizer
)
792 #define PUSH_FULL_EXPR(op, op1, op2, val, val2, val3, val4, val5) \
793 xmlXPathCompExprAdd(ctxt->comp, (op1), (op2), \
794 (op), (val), (val2), (val3), (val4), (val5))
795 #define PUSH_LONG_EXPR(op, val, val2, val3, val4, val5) \
796 xmlXPathCompExprAdd(ctxt->comp, ctxt->comp->last, -1, \
797 (op), (val), (val2), (val3), (val4), (val5))
799 #define PUSH_LEAVE_EXPR(op, val, val2) \
800 xmlXPathCompExprAdd(ctxt->comp, -1, -1, (op), (val), (val2), 0 ,NULL ,NULL)
802 #define PUSH_UNARY_EXPR(op, ch, val, val2) \
803 xmlXPathCompExprAdd(ctxt->comp, (ch), -1, (op), (val), (val2), 0 ,NULL ,NULL)
805 #define PUSH_BINARY_EXPR(op, ch1, ch2, val, val2) \
806 xmlXPathCompExprAdd(ctxt->comp, (ch1), (ch2), (op), \
807 (val), (val2), 0 ,NULL ,NULL)
809 /************************************************************************
811 * XPath object cache structures *
813 ************************************************************************/
815 /* #define XP_DEFAULT_CACHE_ON */
817 #define XP_HAS_CACHE(c) ((c != NULL) && ((c)->cache != NULL))
819 typedef struct _xmlXPathContextCache xmlXPathContextCache
;
820 typedef xmlXPathContextCache
*xmlXPathContextCachePtr
;
821 struct _xmlXPathContextCache
{
822 xmlPointerListPtr nodesetObjs
; /* contains xmlXPathObjectPtr */
823 xmlPointerListPtr stringObjs
; /* contains xmlXPathObjectPtr */
824 xmlPointerListPtr booleanObjs
; /* contains xmlXPathObjectPtr */
825 xmlPointerListPtr numberObjs
; /* contains xmlXPathObjectPtr */
826 xmlPointerListPtr miscObjs
; /* contains xmlXPathObjectPtr */
832 #ifdef XP_DEBUG_OBJ_USAGE
834 int dbgCachedNodeset
;
842 int dbgCachedXSLTTree
;
843 int dbgCachedUndefined
;
847 int dbgReusedNodeset
;
855 int dbgReusedXSLTTree
;
856 int dbgReusedUndefined
;
861 /************************************************************************
863 * Debugging related functions *
865 ************************************************************************/
868 xmlGenericError(xmlGenericErrorContext, \
869 "Internal error at %s:%d\n", \
872 #ifdef LIBXML_DEBUG_ENABLED
874 xmlXPathDebugDumpNode(FILE *output
, xmlNodePtr cur
, int depth
) {
878 for (i
= 0;((i
< depth
) && (i
< 25));i
++)
879 shift
[2 * i
] = shift
[2 * i
+ 1] = ' ';
880 shift
[2 * i
] = shift
[2 * i
+ 1] = 0;
882 fprintf(output
, "%s", shift
);
883 fprintf(output
, "Node is NULL !\n");
888 if ((cur
->type
== XML_DOCUMENT_NODE
) ||
889 (cur
->type
== XML_HTML_DOCUMENT_NODE
)) {
890 fprintf(output
, "%s", shift
);
891 fprintf(output
, " /\n");
892 } else if (cur
->type
== XML_ATTRIBUTE_NODE
)
893 xmlDebugDumpAttr(output
, (xmlAttrPtr
)cur
, depth
);
895 xmlDebugDumpOneNode(output
, cur
, depth
);
898 xmlXPathDebugDumpNodeList(FILE *output
, xmlNodePtr cur
, int depth
) {
903 for (i
= 0;((i
< depth
) && (i
< 25));i
++)
904 shift
[2 * i
] = shift
[2 * i
+ 1] = ' ';
905 shift
[2 * i
] = shift
[2 * i
+ 1] = 0;
907 fprintf(output
, "%s", shift
);
908 fprintf(output
, "Node is NULL !\n");
913 while (cur
!= NULL
) {
916 xmlDebugDumpOneNode(output
, tmp
, depth
);
921 xmlXPathDebugDumpNodeSet(FILE *output
, xmlNodeSetPtr cur
, int depth
) {
925 for (i
= 0;((i
< depth
) && (i
< 25));i
++)
926 shift
[2 * i
] = shift
[2 * i
+ 1] = ' ';
927 shift
[2 * i
] = shift
[2 * i
+ 1] = 0;
930 fprintf(output
, "%s", shift
);
931 fprintf(output
, "NodeSet is NULL !\n");
937 fprintf(output
, "Set contains %d nodes:\n", cur
->nodeNr
);
938 for (i
= 0;i
< cur
->nodeNr
;i
++) {
939 fprintf(output
, "%s", shift
);
940 fprintf(output
, "%d", i
+ 1);
941 xmlXPathDebugDumpNode(output
, cur
->nodeTab
[i
], depth
+ 1);
947 xmlXPathDebugDumpValueTree(FILE *output
, xmlNodeSetPtr cur
, int depth
) {
951 for (i
= 0;((i
< depth
) && (i
< 25));i
++)
952 shift
[2 * i
] = shift
[2 * i
+ 1] = ' ';
953 shift
[2 * i
] = shift
[2 * i
+ 1] = 0;
955 if ((cur
== NULL
) || (cur
->nodeNr
== 0) || (cur
->nodeTab
[0] == NULL
)) {
956 fprintf(output
, "%s", shift
);
957 fprintf(output
, "Value Tree is NULL !\n");
962 fprintf(output
, "%s", shift
);
963 fprintf(output
, "%d", i
+ 1);
964 xmlXPathDebugDumpNodeList(output
, cur
->nodeTab
[0]->children
, depth
+ 1);
966 #if defined(LIBXML_XPTR_ENABLED)
968 xmlXPathDebugDumpLocationSet(FILE *output
, xmlLocationSetPtr cur
, int depth
) {
972 for (i
= 0;((i
< depth
) && (i
< 25));i
++)
973 shift
[2 * i
] = shift
[2 * i
+ 1] = ' ';
974 shift
[2 * i
] = shift
[2 * i
+ 1] = 0;
977 fprintf(output
, "%s", shift
);
978 fprintf(output
, "LocationSet is NULL !\n");
983 for (i
= 0;i
< cur
->locNr
;i
++) {
984 fprintf(output
, "%s", shift
);
985 fprintf(output
, "%d : ", i
+ 1);
986 xmlXPathDebugDumpObject(output
, cur
->locTab
[i
], depth
+ 1);
989 #endif /* LIBXML_XPTR_ENABLED */
992 * xmlXPathDebugDumpObject:
993 * @output: the FILE * to dump the output
994 * @cur: the object to inspect
995 * @depth: indentation level
997 * Dump the content of the object for debugging purposes
1000 xmlXPathDebugDumpObject(FILE *output
, xmlXPathObjectPtr cur
, int depth
) {
1004 if (output
== NULL
) return;
1006 for (i
= 0;((i
< depth
) && (i
< 25));i
++)
1007 shift
[2 * i
] = shift
[2 * i
+ 1] = ' ';
1008 shift
[2 * i
] = shift
[2 * i
+ 1] = 0;
1011 fprintf(output
, "%s", shift
);
1014 fprintf(output
, "Object is empty (NULL)\n");
1018 case XPATH_UNDEFINED
:
1019 fprintf(output
, "Object is uninitialized\n");
1022 fprintf(output
, "Object is a Node Set :\n");
1023 xmlXPathDebugDumpNodeSet(output
, cur
->nodesetval
, depth
);
1025 case XPATH_XSLT_TREE
:
1026 fprintf(output
, "Object is an XSLT value tree :\n");
1027 xmlXPathDebugDumpValueTree(output
, cur
->nodesetval
, depth
);
1030 fprintf(output
, "Object is a Boolean : ");
1031 if (cur
->boolval
) fprintf(output
, "true\n");
1032 else fprintf(output
, "false\n");
1035 switch (xmlXPathIsInf(cur
->floatval
)) {
1037 fprintf(output
, "Object is a number : Infinity\n");
1040 fprintf(output
, "Object is a number : -Infinity\n");
1043 if (xmlXPathIsNaN(cur
->floatval
)) {
1044 fprintf(output
, "Object is a number : NaN\n");
1045 } else if (cur
->floatval
== 0 && xmlXPathGetSign(cur
->floatval
) != 0) {
1046 fprintf(output
, "Object is a number : 0\n");
1048 fprintf(output
, "Object is a number : %0g\n", cur
->floatval
);
1053 fprintf(output
, "Object is a string : ");
1054 xmlDebugDumpString(output
, cur
->stringval
);
1055 fprintf(output
, "\n");
1058 fprintf(output
, "Object is a point : index %d in node", cur
->index
);
1059 xmlXPathDebugDumpNode(output
, (xmlNodePtr
) cur
->user
, depth
+ 1);
1060 fprintf(output
, "\n");
1063 if ((cur
->user2
== NULL
) ||
1064 ((cur
->user2
== cur
->user
) && (cur
->index
== cur
->index2
))) {
1065 fprintf(output
, "Object is a collapsed range :\n");
1066 fprintf(output
, "%s", shift
);
1067 if (cur
->index
>= 0)
1068 fprintf(output
, "index %d in ", cur
->index
);
1069 fprintf(output
, "node\n");
1070 xmlXPathDebugDumpNode(output
, (xmlNodePtr
) cur
->user
,
1073 fprintf(output
, "Object is a range :\n");
1074 fprintf(output
, "%s", shift
);
1075 fprintf(output
, "From ");
1076 if (cur
->index
>= 0)
1077 fprintf(output
, "index %d in ", cur
->index
);
1078 fprintf(output
, "node\n");
1079 xmlXPathDebugDumpNode(output
, (xmlNodePtr
) cur
->user
,
1081 fprintf(output
, "%s", shift
);
1082 fprintf(output
, "To ");
1083 if (cur
->index2
>= 0)
1084 fprintf(output
, "index %d in ", cur
->index2
);
1085 fprintf(output
, "node\n");
1086 xmlXPathDebugDumpNode(output
, (xmlNodePtr
) cur
->user2
,
1088 fprintf(output
, "\n");
1091 case XPATH_LOCATIONSET
:
1092 #if defined(LIBXML_XPTR_ENABLED)
1093 fprintf(output
, "Object is a Location Set:\n");
1094 xmlXPathDebugDumpLocationSet(output
,
1095 (xmlLocationSetPtr
) cur
->user
, depth
);
1099 fprintf(output
, "Object is user defined\n");
1105 xmlXPathDebugDumpStepOp(FILE *output
, xmlXPathCompExprPtr comp
,
1106 xmlXPathStepOpPtr op
, int depth
) {
1110 for (i
= 0;((i
< depth
) && (i
< 25));i
++)
1111 shift
[2 * i
] = shift
[2 * i
+ 1] = ' ';
1112 shift
[2 * i
] = shift
[2 * i
+ 1] = 0;
1114 fprintf(output
, "%s", shift
);
1116 fprintf(output
, "Step is NULL\n");
1121 fprintf(output
, "END"); break;
1123 fprintf(output
, "AND"); break;
1125 fprintf(output
, "OR"); break;
1126 case XPATH_OP_EQUAL
:
1128 fprintf(output
, "EQUAL =");
1130 fprintf(output
, "EQUAL !=");
1134 fprintf(output
, "CMP <");
1136 fprintf(output
, "CMP >");
1138 fprintf(output
, "=");
1142 fprintf(output
, "PLUS -");
1143 else if (op
->value
== 1)
1144 fprintf(output
, "PLUS +");
1145 else if (op
->value
== 2)
1146 fprintf(output
, "PLUS unary -");
1147 else if (op
->value
== 3)
1148 fprintf(output
, "PLUS unary - -");
1152 fprintf(output
, "MULT *");
1153 else if (op
->value
== 1)
1154 fprintf(output
, "MULT div");
1156 fprintf(output
, "MULT mod");
1158 case XPATH_OP_UNION
:
1159 fprintf(output
, "UNION"); break;
1161 fprintf(output
, "ROOT"); break;
1163 fprintf(output
, "NODE"); break;
1164 case XPATH_OP_RESET
:
1165 fprintf(output
, "RESET"); break;
1167 fprintf(output
, "SORT"); break;
1168 case XPATH_OP_COLLECT
: {
1169 xmlXPathAxisVal axis
= (xmlXPathAxisVal
)op
->value
;
1170 xmlXPathTestVal test
= (xmlXPathTestVal
)op
->value2
;
1171 xmlXPathTypeVal type
= (xmlXPathTypeVal
)op
->value3
;
1172 const xmlChar
*prefix
= op
->value4
;
1173 const xmlChar
*name
= op
->value5
;
1175 fprintf(output
, "COLLECT ");
1178 fprintf(output
, " 'ancestors' "); break;
1179 case AXIS_ANCESTOR_OR_SELF
:
1180 fprintf(output
, " 'ancestors-or-self' "); break;
1181 case AXIS_ATTRIBUTE
:
1182 fprintf(output
, " 'attributes' "); break;
1184 fprintf(output
, " 'child' "); break;
1185 case AXIS_DESCENDANT
:
1186 fprintf(output
, " 'descendant' "); break;
1187 case AXIS_DESCENDANT_OR_SELF
:
1188 fprintf(output
, " 'descendant-or-self' "); break;
1189 case AXIS_FOLLOWING
:
1190 fprintf(output
, " 'following' "); break;
1191 case AXIS_FOLLOWING_SIBLING
:
1192 fprintf(output
, " 'following-siblings' "); break;
1193 case AXIS_NAMESPACE
:
1194 fprintf(output
, " 'namespace' "); break;
1196 fprintf(output
, " 'parent' "); break;
1197 case AXIS_PRECEDING
:
1198 fprintf(output
, " 'preceding' "); break;
1199 case AXIS_PRECEDING_SIBLING
:
1200 fprintf(output
, " 'preceding-sibling' "); break;
1202 fprintf(output
, " 'self' "); break;
1205 case NODE_TEST_NONE
:
1206 fprintf(output
, "'none' "); break;
1207 case NODE_TEST_TYPE
:
1208 fprintf(output
, "'type' "); break;
1210 fprintf(output
, "'PI' "); break;
1212 fprintf(output
, "'all' "); break;
1214 fprintf(output
, "'namespace' "); break;
1215 case NODE_TEST_NAME
:
1216 fprintf(output
, "'name' "); break;
1219 case NODE_TYPE_NODE
:
1220 fprintf(output
, "'node' "); break;
1221 case NODE_TYPE_COMMENT
:
1222 fprintf(output
, "'comment' "); break;
1223 case NODE_TYPE_TEXT
:
1224 fprintf(output
, "'text' "); break;
1226 fprintf(output
, "'PI' "); break;
1229 fprintf(output
, "%s:", prefix
);
1231 fprintf(output
, "%s", (const char *) name
);
1235 case XPATH_OP_VALUE
: {
1236 xmlXPathObjectPtr object
= (xmlXPathObjectPtr
) op
->value4
;
1238 fprintf(output
, "ELEM ");
1239 xmlXPathDebugDumpObject(output
, object
, 0);
1242 case XPATH_OP_VARIABLE
: {
1243 const xmlChar
*prefix
= op
->value5
;
1244 const xmlChar
*name
= op
->value4
;
1247 fprintf(output
, "VARIABLE %s:%s", prefix
, name
);
1249 fprintf(output
, "VARIABLE %s", name
);
1252 case XPATH_OP_FUNCTION
: {
1253 int nbargs
= op
->value
;
1254 const xmlChar
*prefix
= op
->value5
;
1255 const xmlChar
*name
= op
->value4
;
1258 fprintf(output
, "FUNCTION %s:%s(%d args)",
1259 prefix
, name
, nbargs
);
1261 fprintf(output
, "FUNCTION %s(%d args)", name
, nbargs
);
1264 case XPATH_OP_ARG
: fprintf(output
, "ARG"); break;
1265 case XPATH_OP_PREDICATE
: fprintf(output
, "PREDICATE"); break;
1266 case XPATH_OP_FILTER
: fprintf(output
, "FILTER"); break;
1267 #ifdef LIBXML_XPTR_ENABLED
1268 case XPATH_OP_RANGETO
: fprintf(output
, "RANGETO"); break;
1271 fprintf(output
, "UNKNOWN %d\n", op
->op
); return;
1273 fprintf(output
, "\n");
1276 xmlXPathDebugDumpStepOp(output
, comp
, &comp
->steps
[op
->ch1
], depth
+ 1);
1278 xmlXPathDebugDumpStepOp(output
, comp
, &comp
->steps
[op
->ch2
], depth
+ 1);
1282 * xmlXPathDebugDumpCompExpr:
1283 * @output: the FILE * for the output
1284 * @comp: the precompiled XPath expression
1285 * @depth: the indentation level.
1287 * Dumps the tree of the compiled XPath expression.
1290 xmlXPathDebugDumpCompExpr(FILE *output
, xmlXPathCompExprPtr comp
,
1295 if ((output
== NULL
) || (comp
== NULL
)) return;
1297 for (i
= 0;((i
< depth
) && (i
< 25));i
++)
1298 shift
[2 * i
] = shift
[2 * i
+ 1] = ' ';
1299 shift
[2 * i
] = shift
[2 * i
+ 1] = 0;
1301 fprintf(output
, "%s", shift
);
1303 fprintf(output
, "Compiled Expression : %d elements\n",
1306 xmlXPathDebugDumpStepOp(output
, comp
, &comp
->steps
[i
], depth
+ 1);
1309 #ifdef XP_DEBUG_OBJ_USAGE
1312 * XPath object usage related debugging variables.
1314 static int xmlXPathDebugObjCounterUndefined
= 0;
1315 static int xmlXPathDebugObjCounterNodeset
= 0;
1316 static int xmlXPathDebugObjCounterBool
= 0;
1317 static int xmlXPathDebugObjCounterNumber
= 0;
1318 static int xmlXPathDebugObjCounterString
= 0;
1319 static int xmlXPathDebugObjCounterPoint
= 0;
1320 static int xmlXPathDebugObjCounterRange
= 0;
1321 static int xmlXPathDebugObjCounterLocset
= 0;
1322 static int xmlXPathDebugObjCounterUsers
= 0;
1323 static int xmlXPathDebugObjCounterXSLTTree
= 0;
1324 static int xmlXPathDebugObjCounterAll
= 0;
1326 static int xmlXPathDebugObjTotalUndefined
= 0;
1327 static int xmlXPathDebugObjTotalNodeset
= 0;
1328 static int xmlXPathDebugObjTotalBool
= 0;
1329 static int xmlXPathDebugObjTotalNumber
= 0;
1330 static int xmlXPathDebugObjTotalString
= 0;
1331 static int xmlXPathDebugObjTotalPoint
= 0;
1332 static int xmlXPathDebugObjTotalRange
= 0;
1333 static int xmlXPathDebugObjTotalLocset
= 0;
1334 static int xmlXPathDebugObjTotalUsers
= 0;
1335 static int xmlXPathDebugObjTotalXSLTTree
= 0;
1336 static int xmlXPathDebugObjTotalAll
= 0;
1338 static int xmlXPathDebugObjMaxUndefined
= 0;
1339 static int xmlXPathDebugObjMaxNodeset
= 0;
1340 static int xmlXPathDebugObjMaxBool
= 0;
1341 static int xmlXPathDebugObjMaxNumber
= 0;
1342 static int xmlXPathDebugObjMaxString
= 0;
1343 static int xmlXPathDebugObjMaxPoint
= 0;
1344 static int xmlXPathDebugObjMaxRange
= 0;
1345 static int xmlXPathDebugObjMaxLocset
= 0;
1346 static int xmlXPathDebugObjMaxUsers
= 0;
1347 static int xmlXPathDebugObjMaxXSLTTree
= 0;
1348 static int xmlXPathDebugObjMaxAll
= 0;
1350 /* REVISIT TODO: Make this static when committing */
1352 xmlXPathDebugObjUsageReset(xmlXPathContextPtr ctxt
)
1355 if (ctxt
->cache
!= NULL
) {
1356 xmlXPathContextCachePtr cache
=
1357 (xmlXPathContextCachePtr
) ctxt
->cache
;
1359 cache
->dbgCachedAll
= 0;
1360 cache
->dbgCachedNodeset
= 0;
1361 cache
->dbgCachedString
= 0;
1362 cache
->dbgCachedBool
= 0;
1363 cache
->dbgCachedNumber
= 0;
1364 cache
->dbgCachedPoint
= 0;
1365 cache
->dbgCachedRange
= 0;
1366 cache
->dbgCachedLocset
= 0;
1367 cache
->dbgCachedUsers
= 0;
1368 cache
->dbgCachedXSLTTree
= 0;
1369 cache
->dbgCachedUndefined
= 0;
1371 cache
->dbgReusedAll
= 0;
1372 cache
->dbgReusedNodeset
= 0;
1373 cache
->dbgReusedString
= 0;
1374 cache
->dbgReusedBool
= 0;
1375 cache
->dbgReusedNumber
= 0;
1376 cache
->dbgReusedPoint
= 0;
1377 cache
->dbgReusedRange
= 0;
1378 cache
->dbgReusedLocset
= 0;
1379 cache
->dbgReusedUsers
= 0;
1380 cache
->dbgReusedXSLTTree
= 0;
1381 cache
->dbgReusedUndefined
= 0;
1385 xmlXPathDebugObjCounterUndefined
= 0;
1386 xmlXPathDebugObjCounterNodeset
= 0;
1387 xmlXPathDebugObjCounterBool
= 0;
1388 xmlXPathDebugObjCounterNumber
= 0;
1389 xmlXPathDebugObjCounterString
= 0;
1390 xmlXPathDebugObjCounterPoint
= 0;
1391 xmlXPathDebugObjCounterRange
= 0;
1392 xmlXPathDebugObjCounterLocset
= 0;
1393 xmlXPathDebugObjCounterUsers
= 0;
1394 xmlXPathDebugObjCounterXSLTTree
= 0;
1395 xmlXPathDebugObjCounterAll
= 0;
1397 xmlXPathDebugObjTotalUndefined
= 0;
1398 xmlXPathDebugObjTotalNodeset
= 0;
1399 xmlXPathDebugObjTotalBool
= 0;
1400 xmlXPathDebugObjTotalNumber
= 0;
1401 xmlXPathDebugObjTotalString
= 0;
1402 xmlXPathDebugObjTotalPoint
= 0;
1403 xmlXPathDebugObjTotalRange
= 0;
1404 xmlXPathDebugObjTotalLocset
= 0;
1405 xmlXPathDebugObjTotalUsers
= 0;
1406 xmlXPathDebugObjTotalXSLTTree
= 0;
1407 xmlXPathDebugObjTotalAll
= 0;
1409 xmlXPathDebugObjMaxUndefined
= 0;
1410 xmlXPathDebugObjMaxNodeset
= 0;
1411 xmlXPathDebugObjMaxBool
= 0;
1412 xmlXPathDebugObjMaxNumber
= 0;
1413 xmlXPathDebugObjMaxString
= 0;
1414 xmlXPathDebugObjMaxPoint
= 0;
1415 xmlXPathDebugObjMaxRange
= 0;
1416 xmlXPathDebugObjMaxLocset
= 0;
1417 xmlXPathDebugObjMaxUsers
= 0;
1418 xmlXPathDebugObjMaxXSLTTree
= 0;
1419 xmlXPathDebugObjMaxAll
= 0;
1424 xmlXPathDebugObjUsageRequested(xmlXPathContextPtr ctxt
,
1425 xmlXPathObjectType objType
)
1430 if (ctxt
->cache
!= NULL
) {
1431 xmlXPathContextCachePtr cache
=
1432 (xmlXPathContextCachePtr
) ctxt
->cache
;
1436 cache
->dbgReusedAll
++;
1438 case XPATH_UNDEFINED
:
1439 cache
->dbgReusedUndefined
++;
1442 cache
->dbgReusedNodeset
++;
1445 cache
->dbgReusedBool
++;
1448 cache
->dbgReusedNumber
++;
1451 cache
->dbgReusedString
++;
1454 cache
->dbgReusedPoint
++;
1457 cache
->dbgReusedRange
++;
1459 case XPATH_LOCATIONSET
:
1460 cache
->dbgReusedLocset
++;
1463 cache
->dbgReusedUsers
++;
1465 case XPATH_XSLT_TREE
:
1466 cache
->dbgReusedXSLTTree
++;
1475 case XPATH_UNDEFINED
:
1477 xmlXPathDebugObjTotalUndefined
++;
1478 xmlXPathDebugObjCounterUndefined
++;
1479 if (xmlXPathDebugObjCounterUndefined
>
1480 xmlXPathDebugObjMaxUndefined
)
1481 xmlXPathDebugObjMaxUndefined
=
1482 xmlXPathDebugObjCounterUndefined
;
1486 xmlXPathDebugObjTotalNodeset
++;
1487 xmlXPathDebugObjCounterNodeset
++;
1488 if (xmlXPathDebugObjCounterNodeset
>
1489 xmlXPathDebugObjMaxNodeset
)
1490 xmlXPathDebugObjMaxNodeset
=
1491 xmlXPathDebugObjCounterNodeset
;
1495 xmlXPathDebugObjTotalBool
++;
1496 xmlXPathDebugObjCounterBool
++;
1497 if (xmlXPathDebugObjCounterBool
>
1498 xmlXPathDebugObjMaxBool
)
1499 xmlXPathDebugObjMaxBool
=
1500 xmlXPathDebugObjCounterBool
;
1504 xmlXPathDebugObjTotalNumber
++;
1505 xmlXPathDebugObjCounterNumber
++;
1506 if (xmlXPathDebugObjCounterNumber
>
1507 xmlXPathDebugObjMaxNumber
)
1508 xmlXPathDebugObjMaxNumber
=
1509 xmlXPathDebugObjCounterNumber
;
1513 xmlXPathDebugObjTotalString
++;
1514 xmlXPathDebugObjCounterString
++;
1515 if (xmlXPathDebugObjCounterString
>
1516 xmlXPathDebugObjMaxString
)
1517 xmlXPathDebugObjMaxString
=
1518 xmlXPathDebugObjCounterString
;
1522 xmlXPathDebugObjTotalPoint
++;
1523 xmlXPathDebugObjCounterPoint
++;
1524 if (xmlXPathDebugObjCounterPoint
>
1525 xmlXPathDebugObjMaxPoint
)
1526 xmlXPathDebugObjMaxPoint
=
1527 xmlXPathDebugObjCounterPoint
;
1531 xmlXPathDebugObjTotalRange
++;
1532 xmlXPathDebugObjCounterRange
++;
1533 if (xmlXPathDebugObjCounterRange
>
1534 xmlXPathDebugObjMaxRange
)
1535 xmlXPathDebugObjMaxRange
=
1536 xmlXPathDebugObjCounterRange
;
1538 case XPATH_LOCATIONSET
:
1540 xmlXPathDebugObjTotalLocset
++;
1541 xmlXPathDebugObjCounterLocset
++;
1542 if (xmlXPathDebugObjCounterLocset
>
1543 xmlXPathDebugObjMaxLocset
)
1544 xmlXPathDebugObjMaxLocset
=
1545 xmlXPathDebugObjCounterLocset
;
1549 xmlXPathDebugObjTotalUsers
++;
1550 xmlXPathDebugObjCounterUsers
++;
1551 if (xmlXPathDebugObjCounterUsers
>
1552 xmlXPathDebugObjMaxUsers
)
1553 xmlXPathDebugObjMaxUsers
=
1554 xmlXPathDebugObjCounterUsers
;
1556 case XPATH_XSLT_TREE
:
1558 xmlXPathDebugObjTotalXSLTTree
++;
1559 xmlXPathDebugObjCounterXSLTTree
++;
1560 if (xmlXPathDebugObjCounterXSLTTree
>
1561 xmlXPathDebugObjMaxXSLTTree
)
1562 xmlXPathDebugObjMaxXSLTTree
=
1563 xmlXPathDebugObjCounterXSLTTree
;
1569 xmlXPathDebugObjTotalAll
++;
1570 xmlXPathDebugObjCounterAll
++;
1571 if (xmlXPathDebugObjCounterAll
>
1572 xmlXPathDebugObjMaxAll
)
1573 xmlXPathDebugObjMaxAll
=
1574 xmlXPathDebugObjCounterAll
;
1578 xmlXPathDebugObjUsageReleased(xmlXPathContextPtr ctxt
,
1579 xmlXPathObjectType objType
)
1584 if (ctxt
->cache
!= NULL
) {
1585 xmlXPathContextCachePtr cache
=
1586 (xmlXPathContextCachePtr
) ctxt
->cache
;
1590 cache
->dbgCachedAll
++;
1592 case XPATH_UNDEFINED
:
1593 cache
->dbgCachedUndefined
++;
1596 cache
->dbgCachedNodeset
++;
1599 cache
->dbgCachedBool
++;
1602 cache
->dbgCachedNumber
++;
1605 cache
->dbgCachedString
++;
1608 cache
->dbgCachedPoint
++;
1611 cache
->dbgCachedRange
++;
1613 case XPATH_LOCATIONSET
:
1614 cache
->dbgCachedLocset
++;
1617 cache
->dbgCachedUsers
++;
1619 case XPATH_XSLT_TREE
:
1620 cache
->dbgCachedXSLTTree
++;
1629 case XPATH_UNDEFINED
:
1630 xmlXPathDebugObjCounterUndefined
--;
1633 xmlXPathDebugObjCounterNodeset
--;
1636 xmlXPathDebugObjCounterBool
--;
1639 xmlXPathDebugObjCounterNumber
--;
1642 xmlXPathDebugObjCounterString
--;
1645 xmlXPathDebugObjCounterPoint
--;
1648 xmlXPathDebugObjCounterRange
--;
1650 case XPATH_LOCATIONSET
:
1651 xmlXPathDebugObjCounterLocset
--;
1654 xmlXPathDebugObjCounterUsers
--;
1656 case XPATH_XSLT_TREE
:
1657 xmlXPathDebugObjCounterXSLTTree
--;
1662 xmlXPathDebugObjCounterAll
--;
1665 /* REVISIT TODO: Make this static when committing */
1667 xmlXPathDebugObjUsageDisplay(xmlXPathContextPtr ctxt
)
1669 int reqAll
, reqNodeset
, reqString
, reqBool
, reqNumber
,
1670 reqXSLTTree
, reqUndefined
;
1671 int caAll
= 0, caNodeset
= 0, caString
= 0, caBool
= 0,
1672 caNumber
= 0, caXSLTTree
= 0, caUndefined
= 0;
1673 int reAll
= 0, reNodeset
= 0, reString
= 0, reBool
= 0,
1674 reNumber
= 0, reXSLTTree
= 0, reUndefined
= 0;
1675 int leftObjs
= xmlXPathDebugObjCounterAll
;
1677 reqAll
= xmlXPathDebugObjTotalAll
;
1678 reqNodeset
= xmlXPathDebugObjTotalNodeset
;
1679 reqString
= xmlXPathDebugObjTotalString
;
1680 reqBool
= xmlXPathDebugObjTotalBool
;
1681 reqNumber
= xmlXPathDebugObjTotalNumber
;
1682 reqXSLTTree
= xmlXPathDebugObjTotalXSLTTree
;
1683 reqUndefined
= xmlXPathDebugObjTotalUndefined
;
1685 printf("# XPath object usage:\n");
1688 if (ctxt
->cache
!= NULL
) {
1689 xmlXPathContextCachePtr cache
=
1690 (xmlXPathContextCachePtr
) ctxt
->cache
;
1692 reAll
= cache
->dbgReusedAll
;
1694 reNodeset
= cache
->dbgReusedNodeset
;
1695 reqNodeset
+= reNodeset
;
1696 reString
= cache
->dbgReusedString
;
1697 reqString
+= reString
;
1698 reBool
= cache
->dbgReusedBool
;
1700 reNumber
= cache
->dbgReusedNumber
;
1701 reqNumber
+= reNumber
;
1702 reXSLTTree
= cache
->dbgReusedXSLTTree
;
1703 reqXSLTTree
+= reXSLTTree
;
1704 reUndefined
= cache
->dbgReusedUndefined
;
1705 reqUndefined
+= reUndefined
;
1707 caAll
= cache
->dbgCachedAll
;
1708 caBool
= cache
->dbgCachedBool
;
1709 caNodeset
= cache
->dbgCachedNodeset
;
1710 caString
= cache
->dbgCachedString
;
1711 caNumber
= cache
->dbgCachedNumber
;
1712 caXSLTTree
= cache
->dbgCachedXSLTTree
;
1713 caUndefined
= cache
->dbgCachedUndefined
;
1715 if (cache
->nodesetObjs
)
1716 leftObjs
-= cache
->nodesetObjs
->number
;
1717 if (cache
->stringObjs
)
1718 leftObjs
-= cache
->stringObjs
->number
;
1719 if (cache
->booleanObjs
)
1720 leftObjs
-= cache
->booleanObjs
->number
;
1721 if (cache
->numberObjs
)
1722 leftObjs
-= cache
->numberObjs
->number
;
1723 if (cache
->miscObjs
)
1724 leftObjs
-= cache
->miscObjs
->number
;
1729 printf("# total : %d\n", reqAll
);
1730 printf("# left : %d\n", leftObjs
);
1731 printf("# created: %d\n", xmlXPathDebugObjTotalAll
);
1732 printf("# reused : %d\n", reAll
);
1733 printf("# max : %d\n", xmlXPathDebugObjMaxAll
);
1735 printf("# node-sets\n");
1736 printf("# total : %d\n", reqNodeset
);
1737 printf("# created: %d\n", xmlXPathDebugObjTotalNodeset
);
1738 printf("# reused : %d\n", reNodeset
);
1739 printf("# max : %d\n", xmlXPathDebugObjMaxNodeset
);
1741 printf("# strings\n");
1742 printf("# total : %d\n", reqString
);
1743 printf("# created: %d\n", xmlXPathDebugObjTotalString
);
1744 printf("# reused : %d\n", reString
);
1745 printf("# max : %d\n", xmlXPathDebugObjMaxString
);
1747 printf("# booleans\n");
1748 printf("# total : %d\n", reqBool
);
1749 printf("# created: %d\n", xmlXPathDebugObjTotalBool
);
1750 printf("# reused : %d\n", reBool
);
1751 printf("# max : %d\n", xmlXPathDebugObjMaxBool
);
1753 printf("# numbers\n");
1754 printf("# total : %d\n", reqNumber
);
1755 printf("# created: %d\n", xmlXPathDebugObjTotalNumber
);
1756 printf("# reused : %d\n", reNumber
);
1757 printf("# max : %d\n", xmlXPathDebugObjMaxNumber
);
1759 printf("# XSLT result tree fragments\n");
1760 printf("# total : %d\n", reqXSLTTree
);
1761 printf("# created: %d\n", xmlXPathDebugObjTotalXSLTTree
);
1762 printf("# reused : %d\n", reXSLTTree
);
1763 printf("# max : %d\n", xmlXPathDebugObjMaxXSLTTree
);
1765 printf("# undefined\n");
1766 printf("# total : %d\n", reqUndefined
);
1767 printf("# created: %d\n", xmlXPathDebugObjTotalUndefined
);
1768 printf("# reused : %d\n", reUndefined
);
1769 printf("# max : %d\n", xmlXPathDebugObjMaxUndefined
);
1773 #endif /* XP_DEBUG_OBJ_USAGE */
1775 #endif /* LIBXML_DEBUG_ENABLED */
1777 /************************************************************************
1779 * XPath object caching *
1781 ************************************************************************/
1786 * Create a new object cache
1788 * Returns the xmlXPathCache just allocated.
1790 static xmlXPathContextCachePtr
1791 xmlXPathNewCache(void)
1793 xmlXPathContextCachePtr ret
;
1795 ret
= (xmlXPathContextCachePtr
) xmlMalloc(sizeof(xmlXPathContextCache
));
1797 xmlXPathErrMemory(NULL
, "creating object cache\n");
1800 memset(ret
, 0 , (size_t) sizeof(xmlXPathContextCache
));
1801 ret
->maxNodeset
= 100;
1802 ret
->maxString
= 100;
1803 ret
->maxBoolean
= 100;
1804 ret
->maxNumber
= 100;
1810 xmlXPathCacheFreeObjectList(xmlPointerListPtr list
)
1813 xmlXPathObjectPtr obj
;
1818 for (i
= 0; i
< list
->number
; i
++) {
1819 obj
= list
->items
[i
];
1821 * Note that it is already assured that we don't need to
1822 * look out for namespace nodes in the node-set.
1824 if (obj
->nodesetval
!= NULL
) {
1825 if (obj
->nodesetval
->nodeTab
!= NULL
)
1826 xmlFree(obj
->nodesetval
->nodeTab
);
1827 xmlFree(obj
->nodesetval
);
1830 #ifdef XP_DEBUG_OBJ_USAGE
1831 xmlXPathDebugObjCounterAll
--;
1834 xmlPointerListFree(list
);
1838 xmlXPathFreeCache(xmlXPathContextCachePtr cache
)
1842 if (cache
->nodesetObjs
)
1843 xmlXPathCacheFreeObjectList(cache
->nodesetObjs
);
1844 if (cache
->stringObjs
)
1845 xmlXPathCacheFreeObjectList(cache
->stringObjs
);
1846 if (cache
->booleanObjs
)
1847 xmlXPathCacheFreeObjectList(cache
->booleanObjs
);
1848 if (cache
->numberObjs
)
1849 xmlXPathCacheFreeObjectList(cache
->numberObjs
);
1850 if (cache
->miscObjs
)
1851 xmlXPathCacheFreeObjectList(cache
->miscObjs
);
1856 * xmlXPathContextSetCache:
1858 * @ctxt: the XPath context
1859 * @active: enables/disables (creates/frees) the cache
1860 * @value: a value with semantics dependant on @options
1861 * @options: options (currently only the value 0 is used)
1863 * Creates/frees an object cache on the XPath context.
1864 * If activates XPath objects (xmlXPathObject) will be cached internally
1867 * 0: This will set the XPath object caching:
1869 * This will set the maximum number of XPath objects
1870 * to be cached per slot
1871 * There are 5 slots for: node-set, string, number, boolean, and
1872 * misc objects. Use <0 for the default number (100).
1873 * Other values for @options have currently no effect.
1875 * Returns 0 if the setting succeeded, and -1 on API or internal errors.
1878 xmlXPathContextSetCache(xmlXPathContextPtr ctxt
,
1886 xmlXPathContextCachePtr cache
;
1888 if (ctxt
->cache
== NULL
) {
1889 ctxt
->cache
= xmlXPathNewCache();
1890 if (ctxt
->cache
== NULL
)
1893 cache
= (xmlXPathContextCachePtr
) ctxt
->cache
;
1897 cache
->maxNodeset
= value
;
1898 cache
->maxString
= value
;
1899 cache
->maxNumber
= value
;
1900 cache
->maxBoolean
= value
;
1901 cache
->maxMisc
= value
;
1903 } else if (ctxt
->cache
!= NULL
) {
1904 xmlXPathFreeCache((xmlXPathContextCachePtr
) ctxt
->cache
);
1911 * xmlXPathCacheWrapNodeSet:
1912 * @ctxt: the XPath context
1913 * @val: the NodePtr value
1915 * This is the cached version of xmlXPathWrapNodeSet().
1916 * Wrap the Nodeset @val in a new xmlXPathObjectPtr
1918 * Returns the created or reused object.
1920 static xmlXPathObjectPtr
1921 xmlXPathCacheWrapNodeSet(xmlXPathContextPtr ctxt
, xmlNodeSetPtr val
)
1923 if ((ctxt
!= NULL
) && (ctxt
->cache
!= NULL
)) {
1924 xmlXPathContextCachePtr cache
=
1925 (xmlXPathContextCachePtr
) ctxt
->cache
;
1927 if ((cache
->miscObjs
!= NULL
) &&
1928 (cache
->miscObjs
->number
!= 0))
1930 xmlXPathObjectPtr ret
;
1932 ret
= (xmlXPathObjectPtr
)
1933 cache
->miscObjs
->items
[--cache
->miscObjs
->number
];
1934 ret
->type
= XPATH_NODESET
;
1935 ret
->nodesetval
= val
;
1936 #ifdef XP_DEBUG_OBJ_USAGE
1937 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_NODESET
);
1943 return(xmlXPathWrapNodeSet(val
));
1948 * xmlXPathCacheWrapString:
1949 * @ctxt: the XPath context
1950 * @val: the xmlChar * value
1952 * This is the cached version of xmlXPathWrapString().
1953 * Wraps the @val string into an XPath object.
1955 * Returns the created or reused object.
1957 static xmlXPathObjectPtr
1958 xmlXPathCacheWrapString(xmlXPathContextPtr ctxt
, xmlChar
*val
)
1960 if ((ctxt
!= NULL
) && (ctxt
->cache
!= NULL
)) {
1961 xmlXPathContextCachePtr cache
= (xmlXPathContextCachePtr
) ctxt
->cache
;
1963 if ((cache
->stringObjs
!= NULL
) &&
1964 (cache
->stringObjs
->number
!= 0))
1967 xmlXPathObjectPtr ret
;
1969 ret
= (xmlXPathObjectPtr
)
1970 cache
->stringObjs
->items
[--cache
->stringObjs
->number
];
1971 ret
->type
= XPATH_STRING
;
1972 ret
->stringval
= val
;
1973 #ifdef XP_DEBUG_OBJ_USAGE
1974 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_STRING
);
1977 } else if ((cache
->miscObjs
!= NULL
) &&
1978 (cache
->miscObjs
->number
!= 0))
1980 xmlXPathObjectPtr ret
;
1982 * Fallback to misc-cache.
1984 ret
= (xmlXPathObjectPtr
)
1985 cache
->miscObjs
->items
[--cache
->miscObjs
->number
];
1987 ret
->type
= XPATH_STRING
;
1988 ret
->stringval
= val
;
1989 #ifdef XP_DEBUG_OBJ_USAGE
1990 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_STRING
);
1995 return(xmlXPathWrapString(val
));
1999 * xmlXPathCacheNewNodeSet:
2000 * @ctxt: the XPath context
2001 * @val: the NodePtr value
2003 * This is the cached version of xmlXPathNewNodeSet().
2004 * Acquire an xmlXPathObjectPtr of type NodeSet and initialize
2005 * it with the single Node @val
2007 * Returns the created or reused object.
2009 static xmlXPathObjectPtr
2010 xmlXPathCacheNewNodeSet(xmlXPathContextPtr ctxt
, xmlNodePtr val
)
2012 if ((ctxt
!= NULL
) && (ctxt
->cache
)) {
2013 xmlXPathContextCachePtr cache
= (xmlXPathContextCachePtr
) ctxt
->cache
;
2015 if ((cache
->nodesetObjs
!= NULL
) &&
2016 (cache
->nodesetObjs
->number
!= 0))
2018 xmlXPathObjectPtr ret
;
2020 * Use the nodset-cache.
2022 ret
= (xmlXPathObjectPtr
)
2023 cache
->nodesetObjs
->items
[--cache
->nodesetObjs
->number
];
2024 ret
->type
= XPATH_NODESET
;
2027 if ((ret
->nodesetval
->nodeMax
== 0) ||
2028 (val
->type
== XML_NAMESPACE_DECL
))
2030 xmlXPathNodeSetAddUnique(ret
->nodesetval
, val
);
2032 ret
->nodesetval
->nodeTab
[0] = val
;
2033 ret
->nodesetval
->nodeNr
= 1;
2036 #ifdef XP_DEBUG_OBJ_USAGE
2037 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_NODESET
);
2040 } else if ((cache
->miscObjs
!= NULL
) &&
2041 (cache
->miscObjs
->number
!= 0))
2043 xmlXPathObjectPtr ret
;
2045 * Fallback to misc-cache.
2048 ret
= (xmlXPathObjectPtr
)
2049 cache
->miscObjs
->items
[--cache
->miscObjs
->number
];
2051 ret
->type
= XPATH_NODESET
;
2053 ret
->nodesetval
= xmlXPathNodeSetCreate(val
);
2054 #ifdef XP_DEBUG_OBJ_USAGE
2055 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_NODESET
);
2060 return(xmlXPathNewNodeSet(val
));
2064 * xmlXPathCacheNewCString:
2065 * @ctxt: the XPath context
2066 * @val: the char * value
2068 * This is the cached version of xmlXPathNewCString().
2069 * Acquire an xmlXPathObjectPtr of type string and of value @val
2071 * Returns the created or reused object.
2073 static xmlXPathObjectPtr
2074 xmlXPathCacheNewCString(xmlXPathContextPtr ctxt
, const char *val
)
2076 if ((ctxt
!= NULL
) && (ctxt
->cache
)) {
2077 xmlXPathContextCachePtr cache
= (xmlXPathContextCachePtr
) ctxt
->cache
;
2079 if ((cache
->stringObjs
!= NULL
) &&
2080 (cache
->stringObjs
->number
!= 0))
2082 xmlXPathObjectPtr ret
;
2084 ret
= (xmlXPathObjectPtr
)
2085 cache
->stringObjs
->items
[--cache
->stringObjs
->number
];
2087 ret
->type
= XPATH_STRING
;
2088 ret
->stringval
= xmlStrdup(BAD_CAST val
);
2089 #ifdef XP_DEBUG_OBJ_USAGE
2090 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_STRING
);
2093 } else if ((cache
->miscObjs
!= NULL
) &&
2094 (cache
->miscObjs
->number
!= 0))
2096 xmlXPathObjectPtr ret
;
2098 ret
= (xmlXPathObjectPtr
)
2099 cache
->miscObjs
->items
[--cache
->miscObjs
->number
];
2101 ret
->type
= XPATH_STRING
;
2102 ret
->stringval
= xmlStrdup(BAD_CAST val
);
2103 #ifdef XP_DEBUG_OBJ_USAGE
2104 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_STRING
);
2109 return(xmlXPathNewCString(val
));
2113 * xmlXPathCacheNewString:
2114 * @ctxt: the XPath context
2115 * @val: the xmlChar * value
2117 * This is the cached version of xmlXPathNewString().
2118 * Acquire an xmlXPathObjectPtr of type string and of value @val
2120 * Returns the created or reused object.
2122 static xmlXPathObjectPtr
2123 xmlXPathCacheNewString(xmlXPathContextPtr ctxt
, const xmlChar
*val
)
2125 if ((ctxt
!= NULL
) && (ctxt
->cache
)) {
2126 xmlXPathContextCachePtr cache
= (xmlXPathContextCachePtr
) ctxt
->cache
;
2128 if ((cache
->stringObjs
!= NULL
) &&
2129 (cache
->stringObjs
->number
!= 0))
2131 xmlXPathObjectPtr ret
;
2133 ret
= (xmlXPathObjectPtr
)
2134 cache
->stringObjs
->items
[--cache
->stringObjs
->number
];
2135 ret
->type
= XPATH_STRING
;
2137 ret
->stringval
= xmlStrdup(val
);
2139 ret
->stringval
= xmlStrdup((const xmlChar
*)"");
2140 #ifdef XP_DEBUG_OBJ_USAGE
2141 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_STRING
);
2144 } else if ((cache
->miscObjs
!= NULL
) &&
2145 (cache
->miscObjs
->number
!= 0))
2147 xmlXPathObjectPtr ret
;
2149 ret
= (xmlXPathObjectPtr
)
2150 cache
->miscObjs
->items
[--cache
->miscObjs
->number
];
2152 ret
->type
= XPATH_STRING
;
2154 ret
->stringval
= xmlStrdup(val
);
2156 ret
->stringval
= xmlStrdup((const xmlChar
*)"");
2157 #ifdef XP_DEBUG_OBJ_USAGE
2158 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_STRING
);
2163 return(xmlXPathNewString(val
));
2167 * xmlXPathCacheNewBoolean:
2168 * @ctxt: the XPath context
2169 * @val: the boolean value
2171 * This is the cached version of xmlXPathNewBoolean().
2172 * Acquires an xmlXPathObjectPtr of type boolean and of value @val
2174 * Returns the created or reused object.
2176 static xmlXPathObjectPtr
2177 xmlXPathCacheNewBoolean(xmlXPathContextPtr ctxt
, int val
)
2179 if ((ctxt
!= NULL
) && (ctxt
->cache
)) {
2180 xmlXPathContextCachePtr cache
= (xmlXPathContextCachePtr
) ctxt
->cache
;
2182 if ((cache
->booleanObjs
!= NULL
) &&
2183 (cache
->booleanObjs
->number
!= 0))
2185 xmlXPathObjectPtr ret
;
2187 ret
= (xmlXPathObjectPtr
)
2188 cache
->booleanObjs
->items
[--cache
->booleanObjs
->number
];
2189 ret
->type
= XPATH_BOOLEAN
;
2190 ret
->boolval
= (val
!= 0);
2191 #ifdef XP_DEBUG_OBJ_USAGE
2192 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_BOOLEAN
);
2195 } else if ((cache
->miscObjs
!= NULL
) &&
2196 (cache
->miscObjs
->number
!= 0))
2198 xmlXPathObjectPtr ret
;
2200 ret
= (xmlXPathObjectPtr
)
2201 cache
->miscObjs
->items
[--cache
->miscObjs
->number
];
2203 ret
->type
= XPATH_BOOLEAN
;
2204 ret
->boolval
= (val
!= 0);
2205 #ifdef XP_DEBUG_OBJ_USAGE
2206 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_BOOLEAN
);
2211 return(xmlXPathNewBoolean(val
));
2215 * xmlXPathCacheNewFloat:
2216 * @ctxt: the XPath context
2217 * @val: the double value
2219 * This is the cached version of xmlXPathNewFloat().
2220 * Acquires an xmlXPathObjectPtr of type double and of value @val
2222 * Returns the created or reused object.
2224 static xmlXPathObjectPtr
2225 xmlXPathCacheNewFloat(xmlXPathContextPtr ctxt
, double val
)
2227 if ((ctxt
!= NULL
) && (ctxt
->cache
)) {
2228 xmlXPathContextCachePtr cache
= (xmlXPathContextCachePtr
) ctxt
->cache
;
2230 if ((cache
->numberObjs
!= NULL
) &&
2231 (cache
->numberObjs
->number
!= 0))
2233 xmlXPathObjectPtr ret
;
2235 ret
= (xmlXPathObjectPtr
)
2236 cache
->numberObjs
->items
[--cache
->numberObjs
->number
];
2237 ret
->type
= XPATH_NUMBER
;
2238 ret
->floatval
= val
;
2239 #ifdef XP_DEBUG_OBJ_USAGE
2240 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_NUMBER
);
2243 } else if ((cache
->miscObjs
!= NULL
) &&
2244 (cache
->miscObjs
->number
!= 0))
2246 xmlXPathObjectPtr ret
;
2248 ret
= (xmlXPathObjectPtr
)
2249 cache
->miscObjs
->items
[--cache
->miscObjs
->number
];
2251 ret
->type
= XPATH_NUMBER
;
2252 ret
->floatval
= val
;
2253 #ifdef XP_DEBUG_OBJ_USAGE
2254 xmlXPathDebugObjUsageRequested(ctxt
, XPATH_NUMBER
);
2259 return(xmlXPathNewFloat(val
));
2263 * xmlXPathCacheConvertString:
2264 * @ctxt: the XPath context
2265 * @val: an XPath object
2267 * This is the cached version of xmlXPathConvertString().
2268 * Converts an existing object to its string() equivalent
2270 * Returns a created or reused object, the old one is freed (cached)
2271 * (or the operation is done directly on @val)
2274 static xmlXPathObjectPtr
2275 xmlXPathCacheConvertString(xmlXPathContextPtr ctxt
, xmlXPathObjectPtr val
) {
2276 xmlChar
*res
= NULL
;
2279 return(xmlXPathCacheNewCString(ctxt
, ""));
2281 switch (val
->type
) {
2282 case XPATH_UNDEFINED
:
2284 xmlGenericError(xmlGenericErrorContext
, "STRING: undefined\n");
2288 case XPATH_XSLT_TREE
:
2289 res
= xmlXPathCastNodeSetToString(val
->nodesetval
);
2294 res
= xmlXPathCastBooleanToString(val
->boolval
);
2297 res
= xmlXPathCastNumberToString(val
->floatval
);
2302 case XPATH_LOCATIONSET
:
2306 xmlXPathReleaseObject(ctxt
, val
);
2308 return(xmlXPathCacheNewCString(ctxt
, ""));
2309 return(xmlXPathCacheWrapString(ctxt
, res
));
2313 * xmlXPathCacheObjectCopy:
2314 * @ctxt: the XPath context
2315 * @val: the original object
2317 * This is the cached version of xmlXPathObjectCopy().
2318 * Acquire a copy of a given object
2320 * Returns a created or reused created object.
2322 static xmlXPathObjectPtr
2323 xmlXPathCacheObjectCopy(xmlXPathContextPtr ctxt
, xmlXPathObjectPtr val
)
2328 if (XP_HAS_CACHE(ctxt
)) {
2329 switch (val
->type
) {
2331 return(xmlXPathCacheWrapNodeSet(ctxt
,
2332 xmlXPathNodeSetMerge(NULL
, val
->nodesetval
)));
2334 return(xmlXPathCacheNewString(ctxt
, val
->stringval
));
2336 return(xmlXPathCacheNewBoolean(ctxt
, val
->boolval
));
2338 return(xmlXPathCacheNewFloat(ctxt
, val
->floatval
));
2343 return(xmlXPathObjectCopy(val
));
2347 * xmlXPathCacheConvertBoolean:
2348 * @ctxt: the XPath context
2349 * @val: an XPath object
2351 * This is the cached version of xmlXPathConvertBoolean().
2352 * Converts an existing object to its boolean() equivalent
2354 * Returns a created or reused object, the old one is freed (or the operation
2355 * is done directly on @val)
2357 static xmlXPathObjectPtr
2358 xmlXPathCacheConvertBoolean(xmlXPathContextPtr ctxt
, xmlXPathObjectPtr val
) {
2359 xmlXPathObjectPtr ret
;
2362 return(xmlXPathCacheNewBoolean(ctxt
, 0));
2363 if (val
->type
== XPATH_BOOLEAN
)
2365 ret
= xmlXPathCacheNewBoolean(ctxt
, xmlXPathCastToBoolean(val
));
2366 xmlXPathReleaseObject(ctxt
, val
);
2371 * xmlXPathCacheConvertNumber:
2372 * @ctxt: the XPath context
2373 * @val: an XPath object
2375 * This is the cached version of xmlXPathConvertNumber().
2376 * Converts an existing object to its number() equivalent
2378 * Returns a created or reused object, the old one is freed (or the operation
2379 * is done directly on @val)
2381 static xmlXPathObjectPtr
2382 xmlXPathCacheConvertNumber(xmlXPathContextPtr ctxt
, xmlXPathObjectPtr val
) {
2383 xmlXPathObjectPtr ret
;
2386 return(xmlXPathCacheNewFloat(ctxt
, 0.0));
2387 if (val
->type
== XPATH_NUMBER
)
2389 ret
= xmlXPathCacheNewFloat(ctxt
, xmlXPathCastToNumber(val
));
2390 xmlXPathReleaseObject(ctxt
, val
);
2394 /************************************************************************
2396 * Parser stacks related functions and macros *
2398 ************************************************************************/
2402 * @ctxt: an XPath evaluation context
2404 * Pops the top XPath object from the value stack
2406 * Returns the XPath object just removed
2409 valuePop(xmlXPathParserContextPtr ctxt
)
2411 xmlXPathObjectPtr ret
;
2413 if ((ctxt
== NULL
) || (ctxt
->valueNr
<= 0))
2416 if (ctxt
->valueNr
> 0)
2417 ctxt
->value
= ctxt
->valueTab
[ctxt
->valueNr
- 1];
2420 ret
= ctxt
->valueTab
[ctxt
->valueNr
];
2421 ctxt
->valueTab
[ctxt
->valueNr
] = NULL
;
2426 * @ctxt: an XPath evaluation context
2427 * @value: the XPath object
2429 * Pushes a new XPath object on top of the value stack
2431 * returns the number of items on the value stack
2434 valuePush(xmlXPathParserContextPtr ctxt
, xmlXPathObjectPtr value
)
2436 if ((ctxt
== NULL
) || (value
== NULL
)) return(-1);
2437 if (ctxt
->valueNr
>= ctxt
->valueMax
) {
2438 xmlXPathObjectPtr
*tmp
;
2440 tmp
= (xmlXPathObjectPtr
*) xmlRealloc(ctxt
->valueTab
,
2441 2 * ctxt
->valueMax
*
2442 sizeof(ctxt
->valueTab
[0]));
2444 xmlGenericError(xmlGenericErrorContext
, "realloc failed !\n");
2447 ctxt
->valueMax
*= 2;
2448 ctxt
->valueTab
= tmp
;
2450 ctxt
->valueTab
[ctxt
->valueNr
] = value
;
2451 ctxt
->value
= value
;
2452 return (ctxt
->valueNr
++);
2456 * xmlXPathPopBoolean:
2457 * @ctxt: an XPath parser context
2459 * Pops a boolean from the stack, handling conversion if needed.
2460 * Check error with #xmlXPathCheckError.
2462 * Returns the boolean
2465 xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt
) {
2466 xmlXPathObjectPtr obj
;
2469 obj
= valuePop(ctxt
);
2471 xmlXPathSetError(ctxt
, XPATH_INVALID_OPERAND
);
2474 if (obj
->type
!= XPATH_BOOLEAN
)
2475 ret
= xmlXPathCastToBoolean(obj
);
2478 xmlXPathReleaseObject(ctxt
->context
, obj
);
2483 * xmlXPathPopNumber:
2484 * @ctxt: an XPath parser context
2486 * Pops a number from the stack, handling conversion if needed.
2487 * Check error with #xmlXPathCheckError.
2489 * Returns the number
2492 xmlXPathPopNumber (xmlXPathParserContextPtr ctxt
) {
2493 xmlXPathObjectPtr obj
;
2496 obj
= valuePop(ctxt
);
2498 xmlXPathSetError(ctxt
, XPATH_INVALID_OPERAND
);
2501 if (obj
->type
!= XPATH_NUMBER
)
2502 ret
= xmlXPathCastToNumber(obj
);
2504 ret
= obj
->floatval
;
2505 xmlXPathReleaseObject(ctxt
->context
, obj
);
2510 * xmlXPathPopString:
2511 * @ctxt: an XPath parser context
2513 * Pops a string from the stack, handling conversion if needed.
2514 * Check error with #xmlXPathCheckError.
2516 * Returns the string
2519 xmlXPathPopString (xmlXPathParserContextPtr ctxt
) {
2520 xmlXPathObjectPtr obj
;
2523 obj
= valuePop(ctxt
);
2525 xmlXPathSetError(ctxt
, XPATH_INVALID_OPERAND
);
2528 ret
= xmlXPathCastToString(obj
); /* this does required strdup */
2529 /* TODO: needs refactoring somewhere else */
2530 if (obj
->stringval
== ret
)
2531 obj
->stringval
= NULL
;
2532 xmlXPathReleaseObject(ctxt
->context
, obj
);
2537 * xmlXPathPopNodeSet:
2538 * @ctxt: an XPath parser context
2540 * Pops a node-set from the stack, handling conversion if needed.
2541 * Check error with #xmlXPathCheckError.
2543 * Returns the node-set
2546 xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt
) {
2547 xmlXPathObjectPtr obj
;
2550 if (ctxt
== NULL
) return(NULL
);
2551 if (ctxt
->value
== NULL
) {
2552 xmlXPathSetError(ctxt
, XPATH_INVALID_OPERAND
);
2555 if (!xmlXPathStackIsNodeSet(ctxt
)) {
2556 xmlXPathSetTypeError(ctxt
);
2559 obj
= valuePop(ctxt
);
2560 ret
= obj
->nodesetval
;
2562 /* to fix memory leak of not clearing obj->user */
2563 if (obj
->boolval
&& obj
->user
!= NULL
)
2564 xmlFreeNodeList((xmlNodePtr
) obj
->user
);
2566 obj
->nodesetval
= NULL
;
2567 xmlXPathReleaseObject(ctxt
->context
, obj
);
2572 * xmlXPathPopExternal:
2573 * @ctxt: an XPath parser context
2575 * Pops an external object from the stack, handling conversion if needed.
2576 * Check error with #xmlXPathCheckError.
2578 * Returns the object
2581 xmlXPathPopExternal (xmlXPathParserContextPtr ctxt
) {
2582 xmlXPathObjectPtr obj
;
2585 if ((ctxt
== NULL
) || (ctxt
->value
== NULL
)) {
2586 xmlXPathSetError(ctxt
, XPATH_INVALID_OPERAND
);
2589 if (ctxt
->value
->type
!= XPATH_USERS
) {
2590 xmlXPathSetTypeError(ctxt
);
2593 obj
= valuePop(ctxt
);
2596 xmlXPathReleaseObject(ctxt
->context
, obj
);
2601 * Macros for accessing the content. Those should be used only by the parser,
2604 * Dirty macros, i.e. one need to make assumption on the context to use them
2606 * CUR_PTR return the current pointer to the xmlChar to be parsed.
2607 * CUR returns the current xmlChar value, i.e. a 8 bit value
2608 * in ISO-Latin or UTF-8.
2609 * This should be used internally by the parser
2610 * only to compare to ASCII values otherwise it would break when
2611 * running with UTF-8 encoding.
2612 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
2613 * to compare on ASCII based substring.
2614 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
2615 * strings within the parser.
2616 * CURRENT Returns the current char value, with the full decoding of
2617 * UTF-8 if we are using this mode. It returns an int.
2618 * NEXT Skip to the next character, this does the proper decoding
2619 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
2620 * It returns the pointer to the current xmlChar.
2623 #define CUR (*ctxt->cur)
2624 #define SKIP(val) ctxt->cur += (val)
2625 #define NXT(val) ctxt->cur[(val)]
2626 #define CUR_PTR ctxt->cur
2627 #define CUR_CHAR(l) xmlXPathCurrentChar(ctxt, &l)
2629 #define COPY_BUF(l,b,i,v) \
2630 if (l == 1) b[i++] = (xmlChar) v; \
2631 else i += xmlCopyChar(l,&b[i],v)
2633 #define NEXTL(l) ctxt->cur += l
2635 #define SKIP_BLANKS \
2636 while (IS_BLANK_CH(*(ctxt->cur))) NEXT
2638 #define CURRENT (*ctxt->cur)
2639 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
2646 #define DBL_EPSILON 1E-9
2649 #define UPPER_DOUBLE 1E9
2650 #define LOWER_DOUBLE 1E-5
2651 #define LOWER_DOUBLE_EXP 5
2653 #define INTEGER_DIGITS DBL_DIG
2654 #define FRACTION_DIGITS (DBL_DIG + 1 + (LOWER_DOUBLE_EXP))
2655 #define EXPONENT_DIGITS (3 + 2)
2658 * xmlXPathFormatNumber:
2659 * @number: number to format
2660 * @buffer: output buffer
2661 * @buffersize: size of output buffer
2663 * Convert the number into a string representation.
2666 xmlXPathFormatNumber(double number
, char buffer
[], int buffersize
)
2668 switch (xmlXPathIsInf(number
)) {
2670 if (buffersize
> (int)sizeof("Infinity"))
2671 snprintf(buffer
, buffersize
, "Infinity");
2674 if (buffersize
> (int)sizeof("-Infinity"))
2675 snprintf(buffer
, buffersize
, "-Infinity");
2678 if (xmlXPathIsNaN(number
)) {
2679 if (buffersize
> (int)sizeof("NaN"))
2680 snprintf(buffer
, buffersize
, "NaN");
2681 } else if (number
== 0 && xmlXPathGetSign(number
) != 0) {
2682 snprintf(buffer
, buffersize
, "0");
2683 } else if (number
== ((int) number
)) {
2686 int value
= (int) number
;
2692 snprintf(work
, 29, "%d", value
);
2694 while ((*cur
) && (ptr
- buffer
< buffersize
)) {
2698 if (ptr
- buffer
< buffersize
) {
2700 } else if (buffersize
> 0) {
2706 For the dimension of work,
2707 DBL_DIG is number of significant digits
2708 EXPONENT is only needed for "scientific notation"
2709 3 is sign, decimal point, and terminating zero
2710 LOWER_DOUBLE_EXP is max number of leading zeroes in fraction
2711 Note that this dimension is slightly (a few characters)
2712 larger than actually necessary.
2714 char work
[DBL_DIG
+ EXPONENT_DIGITS
+ 3 + LOWER_DOUBLE_EXP
];
2715 int integer_place
, fraction_place
;
2717 char *after_fraction
;
2718 double absolute_value
;
2721 absolute_value
= fabs(number
);
2724 * First choose format - scientific or regular floating point.
2725 * In either case, result is in work, and after_fraction points
2726 * just past the fractional part.
2728 if ( ((absolute_value
> UPPER_DOUBLE
) ||
2729 (absolute_value
< LOWER_DOUBLE
)) &&
2730 (absolute_value
!= 0.0) ) {
2731 /* Use scientific notation */
2732 integer_place
= DBL_DIG
+ EXPONENT_DIGITS
+ 1;
2733 fraction_place
= DBL_DIG
- 1;
2734 size
= snprintf(work
, sizeof(work
),"%*.*e",
2735 integer_place
, fraction_place
, number
);
2736 while ((size
> 0) && (work
[size
] != 'e')) size
--;
2740 /* Use regular notation */
2741 if (absolute_value
> 0.0) {
2742 integer_place
= (int)log10(absolute_value
);
2743 if (integer_place
> 0)
2744 fraction_place
= DBL_DIG
- integer_place
- 1;
2746 fraction_place
= DBL_DIG
- integer_place
;
2750 size
= snprintf(work
, sizeof(work
), "%0.*f",
2751 fraction_place
, number
);
2754 /* Remove fractional trailing zeroes */
2755 after_fraction
= work
+ size
;
2756 ptr
= after_fraction
;
2757 while (*(--ptr
) == '0')
2761 while ((*ptr
++ = *after_fraction
++) != 0);
2763 /* Finally copy result back to caller */
2764 size
= strlen(work
) + 1;
2765 if (size
> buffersize
) {
2766 work
[buffersize
- 1] = 0;
2769 memmove(buffer
, work
, size
);
2776 /************************************************************************
2778 * Routines to handle NodeSets *
2780 ************************************************************************/
2783 * xmlXPathOrderDocElems:
2784 * @doc: an input document
2786 * Call this routine to speed up XPath computation on static documents.
2787 * This stamps all the element nodes with the document order
2788 * Like for line information, the order is kept in the element->content
2789 * field, the value stored is actually - the node number (starting at -1)
2790 * to be able to differentiate from line numbers.
2792 * Returns the number of elements found in the document or -1 in case
2796 xmlXPathOrderDocElems(xmlDocPtr doc
) {
2802 cur
= doc
->children
;
2803 while (cur
!= NULL
) {
2804 if (cur
->type
== XML_ELEMENT_NODE
) {
2805 cur
->content
= (void *) (-(++count
));
2806 if (cur
->children
!= NULL
) {
2807 cur
= cur
->children
;
2811 if (cur
->next
!= NULL
) {
2819 if (cur
== (xmlNodePtr
) doc
) {
2823 if (cur
->next
!= NULL
) {
2827 } while (cur
!= NULL
);
2834 * @node1: the first node
2835 * @node2: the second node
2837 * Compare two nodes w.r.t document order
2839 * Returns -2 in case of error 1 if first point < second point, 0 if
2840 * it's the same node, -1 otherwise
2843 xmlXPathCmpNodes(xmlNodePtr node1
, xmlNodePtr node2
) {
2845 int attr1
= 0, attr2
= 0;
2846 xmlNodePtr attrNode1
= NULL
, attrNode2
= NULL
;
2847 xmlNodePtr cur
, root
;
2849 if ((node1
== NULL
) || (node2
== NULL
))
2852 * a couple of optimizations which will avoid computations in most cases
2854 if (node1
== node2
) /* trivial case */
2856 if (node1
->type
== XML_ATTRIBUTE_NODE
) {
2859 node1
= node1
->parent
;
2861 if (node2
->type
== XML_ATTRIBUTE_NODE
) {
2864 node2
= node2
->parent
;
2866 if (node1
== node2
) {
2867 if (attr1
== attr2
) {
2868 /* not required, but we keep attributes in order */
2870 cur
= attrNode2
->prev
;
2871 while (cur
!= NULL
) {
2872 if (cur
== attrNode1
)
2884 if ((node1
->type
== XML_NAMESPACE_DECL
) ||
2885 (node2
->type
== XML_NAMESPACE_DECL
))
2887 if (node1
== node2
->prev
)
2889 if (node1
== node2
->next
)
2893 * Speedup using document order if availble.
2895 if ((node1
->type
== XML_ELEMENT_NODE
) &&
2896 (node2
->type
== XML_ELEMENT_NODE
) &&
2897 (0 > (long) node1
->content
) &&
2898 (0 > (long) node2
->content
) &&
2899 (node1
->doc
== node2
->doc
)) {
2902 l1
= -((long) node1
->content
);
2903 l2
= -((long) node2
->content
);
2911 * compute depth to root
2913 for (depth2
= 0, cur
= node2
;cur
->parent
!= NULL
;cur
= cur
->parent
) {
2919 for (depth1
= 0, cur
= node1
;cur
->parent
!= NULL
;cur
= cur
->parent
) {
2925 * Distinct document (or distinct entities :-( ) case.
2931 * get the nearest common ancestor.
2933 while (depth1
> depth2
) {
2935 node1
= node1
->parent
;
2937 while (depth2
> depth1
) {
2939 node2
= node2
->parent
;
2941 while (node1
->parent
!= node2
->parent
) {
2942 node1
= node1
->parent
;
2943 node2
= node2
->parent
;
2944 /* should not happen but just in case ... */
2945 if ((node1
== NULL
) || (node2
== NULL
))
2951 if (node1
== node2
->prev
)
2953 if (node1
== node2
->next
)
2956 * Speedup using document order if availble.
2958 if ((node1
->type
== XML_ELEMENT_NODE
) &&
2959 (node2
->type
== XML_ELEMENT_NODE
) &&
2960 (0 > (long) node1
->content
) &&
2961 (0 > (long) node2
->content
) &&
2962 (node1
->doc
== node2
->doc
)) {
2965 l1
= -((long) node1
->content
);
2966 l2
= -((long) node2
->content
);
2973 for (cur
= node1
->next
;cur
!= NULL
;cur
= cur
->next
)
2976 return(-1); /* assume there is no sibling list corruption */
2979 #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON
2981 * xmlXPathCmpNodesExt:
2982 * @node1: the first node
2983 * @node2: the second node
2985 * Compare two nodes w.r.t document order.
2986 * This one is optimized for handling of non-element nodes.
2988 * Returns -2 in case of error 1 if first point < second point, 0 if
2989 * it's the same node, -1 otherwise
2992 xmlXPathCmpNodesExt(xmlNodePtr node1
, xmlNodePtr node2
) {
2994 int misc
= 0, precedence1
= 0, precedence2
= 0;
2995 xmlNodePtr miscNode1
= NULL
, miscNode2
= NULL
;
2996 xmlNodePtr cur
, root
;
2999 if ((node1
== NULL
) || (node2
== NULL
))
3006 * a couple of optimizations which will avoid computations in most cases
3008 switch (node1
->type
) {
3009 case XML_ELEMENT_NODE
:
3010 if (node2
->type
== XML_ELEMENT_NODE
) {
3011 if ((0 > (long) node1
->content
) && /* TODO: Would a != 0 suffice here? */
3012 (0 > (long) node2
->content
) &&
3013 (node1
->doc
== node2
->doc
))
3015 l1
= -((long) node1
->content
);
3016 l2
= -((long) node2
->content
);
3022 goto turtle_comparison
;
3025 case XML_ATTRIBUTE_NODE
:
3026 precedence1
= 1; /* element is owner */
3028 node1
= node1
->parent
;
3032 case XML_CDATA_SECTION_NODE
:
3033 case XML_COMMENT_NODE
:
3037 * Find nearest element node.
3039 if (node1
->prev
!= NULL
) {
3041 node1
= node1
->prev
;
3042 if (node1
->type
== XML_ELEMENT_NODE
) {
3043 precedence1
= 3; /* element in prev-sibl axis */
3046 if (node1
->prev
== NULL
) {
3047 precedence1
= 2; /* element is parent */
3049 * URGENT TODO: Are there any cases, where the
3050 * parent of such a node is not an element node?
3052 node1
= node1
->parent
;
3057 precedence1
= 2; /* element is parent */
3058 node1
= node1
->parent
;
3060 if ((node1
== NULL
) || (node1
->type
!= XML_ELEMENT_NODE
) ||
3061 (0 <= (long) node1
->content
)) {
3063 * Fallback for whatever case.
3071 case XML_NAMESPACE_DECL
:
3073 * TODO: why do we return 1 for namespace nodes?
3079 switch (node2
->type
) {
3080 case XML_ELEMENT_NODE
:
3082 case XML_ATTRIBUTE_NODE
:
3083 precedence2
= 1; /* element is owner */
3085 node2
= node2
->parent
;
3089 case XML_CDATA_SECTION_NODE
:
3090 case XML_COMMENT_NODE
:
3093 if (node2
->prev
!= NULL
) {
3095 node2
= node2
->prev
;
3096 if (node2
->type
== XML_ELEMENT_NODE
) {
3097 precedence2
= 3; /* element in prev-sibl axis */
3100 if (node2
->prev
== NULL
) {
3101 precedence2
= 2; /* element is parent */
3102 node2
= node2
->parent
;
3107 precedence2
= 2; /* element is parent */
3108 node2
= node2
->parent
;
3110 if ((node2
== NULL
) || (node2
->type
!= XML_ELEMENT_NODE
) ||
3111 (0 <= (long) node1
->content
))
3119 case XML_NAMESPACE_DECL
:
3125 if (node1
== node2
) {
3126 if (precedence1
== precedence2
) {
3128 * The ugly case; but normally there aren't many
3129 * adjacent non-element nodes around.
3131 cur
= miscNode2
->prev
;
3132 while (cur
!= NULL
) {
3133 if (cur
== miscNode1
)
3135 if (cur
->type
== XML_ELEMENT_NODE
)
3142 * Evaluate based on higher precedence wrt to the element.
3143 * TODO: This assumes attributes are sorted before content.
3144 * Is this 100% correct?
3146 if (precedence1
< precedence2
)
3153 * Special case: One of the helper-elements is contained by the other.
3156 * <node1>Text-1(precedence1 == 2)</node1>
3158 * Text-6(precedence2 == 3)
3161 if ((precedence2
== 3) && (precedence1
> 1)) {
3162 cur
= node1
->parent
;
3169 if ((precedence1
== 3) && (precedence2
> 1)) {
3170 cur
= node2
->parent
;
3180 * Speedup using document order if availble.
3182 if ((node1
->type
== XML_ELEMENT_NODE
) &&
3183 (node2
->type
== XML_ELEMENT_NODE
) &&
3184 (0 > (long) node1
->content
) &&
3185 (0 > (long) node2
->content
) &&
3186 (node1
->doc
== node2
->doc
)) {
3188 l1
= -((long) node1
->content
);
3189 l2
= -((long) node2
->content
);
3198 if (node1
== node2
->prev
)
3200 if (node1
== node2
->next
)
3203 * compute depth to root
3205 for (depth2
= 0, cur
= node2
;cur
->parent
!= NULL
;cur
= cur
->parent
) {
3211 for (depth1
= 0, cur
= node1
;cur
->parent
!= NULL
;cur
= cur
->parent
) {
3217 * Distinct document (or distinct entities :-( ) case.
3223 * get the nearest common ancestor.
3225 while (depth1
> depth2
) {
3227 node1
= node1
->parent
;
3229 while (depth2
> depth1
) {
3231 node2
= node2
->parent
;
3233 while (node1
->parent
!= node2
->parent
) {
3234 node1
= node1
->parent
;
3235 node2
= node2
->parent
;
3236 /* should not happen but just in case ... */
3237 if ((node1
== NULL
) || (node2
== NULL
))
3243 if (node1
== node2
->prev
)
3245 if (node1
== node2
->next
)
3248 * Speedup using document order if availble.
3250 if ((node1
->type
== XML_ELEMENT_NODE
) &&
3251 (node2
->type
== XML_ELEMENT_NODE
) &&
3252 (0 > (long) node1
->content
) &&
3253 (0 > (long) node2
->content
) &&
3254 (node1
->doc
== node2
->doc
)) {
3256 l1
= -((long) node1
->content
);
3257 l2
= -((long) node2
->content
);
3264 for (cur
= node1
->next
;cur
!= NULL
;cur
= cur
->next
)
3267 return(-1); /* assume there is no sibling list corruption */
3269 #endif /* XP_OPTIMIZED_NON_ELEM_COMPARISON */
3272 * xmlXPathNodeSetSort:
3273 * @set: the node set
3275 * Sort the node set in document order
3278 xmlXPathNodeSetSort(xmlNodeSetPtr set
) {
3279 int i
, j
, incr
, len
;
3285 /* Use Shell's sort to sort the node-set */
3287 for (incr
= len
/ 2; incr
> 0; incr
/= 2) {
3288 for (i
= incr
; i
< len
; i
++) {
3291 #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON
3292 if (xmlXPathCmpNodesExt(set
->nodeTab
[j
],
3293 set
->nodeTab
[j
+ incr
]) == -1)
3295 if (xmlXPathCmpNodes(set
->nodeTab
[j
],
3296 set
->nodeTab
[j
+ incr
]) == -1)
3299 tmp
= set
->nodeTab
[j
];
3300 set
->nodeTab
[j
] = set
->nodeTab
[j
+ incr
];
3301 set
->nodeTab
[j
+ incr
] = tmp
;
3310 #define XML_NODESET_DEFAULT 10
3312 * xmlXPathNodeSetDupNs:
3313 * @node: the parent node of the namespace XPath node
3314 * @ns: the libxml namespace declaration node.
3316 * Namespace node in libxml don't match the XPath semantic. In a node set
3317 * the namespace nodes are duplicated and the next pointer is set to the
3318 * parent node in the XPath semantic.
3320 * Returns the newly created object.
3323 xmlXPathNodeSetDupNs(xmlNodePtr node
, xmlNsPtr ns
) {
3326 if ((ns
== NULL
) || (ns
->type
!= XML_NAMESPACE_DECL
))
3328 if ((node
== NULL
) || (node
->type
== XML_NAMESPACE_DECL
))
3329 return((xmlNodePtr
) ns
);
3332 * Allocate a new Namespace and fill the fields.
3334 cur
= (xmlNsPtr
) xmlMalloc(sizeof(xmlNs
));
3336 xmlXPathErrMemory(NULL
, "duplicating namespace\n");
3339 memset(cur
, 0, sizeof(xmlNs
));
3340 cur
->type
= XML_NAMESPACE_DECL
;
3341 if (ns
->href
!= NULL
)
3342 cur
->href
= xmlStrdup(ns
->href
);
3343 if (ns
->prefix
!= NULL
)
3344 cur
->prefix
= xmlStrdup(ns
->prefix
);
3345 cur
->next
= (xmlNsPtr
) node
;
3346 return((xmlNodePtr
) cur
);
3350 * xmlXPathNodeSetFreeNs:
3351 * @ns: the XPath namespace node found in a nodeset.
3353 * Namespace nodes in libxml don't match the XPath semantic. In a node set
3354 * the namespace nodes are duplicated and the next pointer is set to the
3355 * parent node in the XPath semantic. Check if such a node needs to be freed
3358 xmlXPathNodeSetFreeNs(xmlNsPtr ns
) {
3359 if ((ns
== NULL
) || (ns
->type
!= XML_NAMESPACE_DECL
))
3362 if ((ns
->next
!= NULL
) && (ns
->next
->type
!= XML_NAMESPACE_DECL
)) {
3363 if (ns
->href
!= NULL
)
3364 xmlFree((xmlChar
*)ns
->href
);
3365 if (ns
->prefix
!= NULL
)
3366 xmlFree((xmlChar
*)ns
->prefix
);
3372 * xmlXPathNodeSetCreate:
3373 * @val: an initial xmlNodePtr, or NULL
3375 * Create a new xmlNodeSetPtr of type double and of value @val
3377 * Returns the newly created object.
3380 xmlXPathNodeSetCreate(xmlNodePtr val
) {
3383 ret
= (xmlNodeSetPtr
) xmlMalloc(sizeof(xmlNodeSet
));
3385 xmlXPathErrMemory(NULL
, "creating nodeset\n");
3388 memset(ret
, 0 , (size_t) sizeof(xmlNodeSet
));
3390 ret
->nodeTab
= (xmlNodePtr
*) xmlMalloc(XML_NODESET_DEFAULT
*
3391 sizeof(xmlNodePtr
));
3392 if (ret
->nodeTab
== NULL
) {
3393 xmlXPathErrMemory(NULL
, "creating nodeset\n");
3397 memset(ret
->nodeTab
, 0 ,
3398 XML_NODESET_DEFAULT
* (size_t) sizeof(xmlNodePtr
));
3399 ret
->nodeMax
= XML_NODESET_DEFAULT
;
3400 if (val
->type
== XML_NAMESPACE_DECL
) {
3401 xmlNsPtr ns
= (xmlNsPtr
) val
;
3403 ret
->nodeTab
[ret
->nodeNr
++] =
3404 xmlXPathNodeSetDupNs((xmlNodePtr
) ns
->next
, ns
);
3406 ret
->nodeTab
[ret
->nodeNr
++] = val
;
3412 * xmlXPathNodeSetCreateSize:
3413 * @size: the initial size of the set
3415 * Create a new xmlNodeSetPtr of type double and of value @val
3417 * Returns the newly created object.
3419 static xmlNodeSetPtr
3420 xmlXPathNodeSetCreateSize(int size
) {
3423 ret
= (xmlNodeSetPtr
) xmlMalloc(sizeof(xmlNodeSet
));
3425 xmlXPathErrMemory(NULL
, "creating nodeset\n");
3428 memset(ret
, 0 , (size_t) sizeof(xmlNodeSet
));
3429 if (size
< XML_NODESET_DEFAULT
)
3430 size
= XML_NODESET_DEFAULT
;
3431 ret
->nodeTab
= (xmlNodePtr
*) xmlMalloc(size
* sizeof(xmlNodePtr
));
3432 if (ret
->nodeTab
== NULL
) {
3433 xmlXPathErrMemory(NULL
, "creating nodeset\n");
3437 memset(ret
->nodeTab
, 0 , size
* (size_t) sizeof(xmlNodePtr
));
3438 ret
->nodeMax
= size
;
3443 * xmlXPathNodeSetContains:
3444 * @cur: the node-set
3447 * checks whether @cur contains @val
3449 * Returns true (1) if @cur contains @val, false (0) otherwise
3452 xmlXPathNodeSetContains (xmlNodeSetPtr cur
, xmlNodePtr val
) {
3455 if ((cur
== NULL
) || (val
== NULL
)) return(0);
3456 if (val
->type
== XML_NAMESPACE_DECL
) {
3457 for (i
= 0; i
< cur
->nodeNr
; i
++) {
3458 if (cur
->nodeTab
[i
]->type
== XML_NAMESPACE_DECL
) {
3461 ns1
= (xmlNsPtr
) val
;
3462 ns2
= (xmlNsPtr
) cur
->nodeTab
[i
];
3465 if ((ns1
->next
!= NULL
) && (ns2
->next
== ns1
->next
) &&
3466 (xmlStrEqual(ns1
->prefix
, ns2
->prefix
)))
3471 for (i
= 0; i
< cur
->nodeNr
; i
++) {
3472 if (cur
->nodeTab
[i
] == val
)
3480 * xmlXPathNodeSetAddNs:
3481 * @cur: the initial node set
3482 * @node: the hosting node
3483 * @ns: a the namespace node
3485 * add a new namespace node to an existing NodeSet
3488 xmlXPathNodeSetAddNs(xmlNodeSetPtr cur
, xmlNodePtr node
, xmlNsPtr ns
) {
3492 if ((cur
== NULL
) || (ns
== NULL
) || (node
== NULL
) ||
3493 (ns
->type
!= XML_NAMESPACE_DECL
) ||
3494 (node
->type
!= XML_ELEMENT_NODE
))
3497 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
3499 * prevent duplicates
3501 for (i
= 0;i
< cur
->nodeNr
;i
++) {
3502 if ((cur
->nodeTab
[i
] != NULL
) &&
3503 (cur
->nodeTab
[i
]->type
== XML_NAMESPACE_DECL
) &&
3504 (((xmlNsPtr
)cur
->nodeTab
[i
])->next
== (xmlNsPtr
) node
) &&
3505 (xmlStrEqual(ns
->prefix
, ((xmlNsPtr
)cur
->nodeTab
[i
])->prefix
)))
3510 * grow the nodeTab if needed
3512 if (cur
->nodeMax
== 0) {
3513 cur
->nodeTab
= (xmlNodePtr
*) xmlMalloc(XML_NODESET_DEFAULT
*
3514 sizeof(xmlNodePtr
));
3515 if (cur
->nodeTab
== NULL
) {
3516 xmlXPathErrMemory(NULL
, "growing nodeset\n");
3519 memset(cur
->nodeTab
, 0 ,
3520 XML_NODESET_DEFAULT
* (size_t) sizeof(xmlNodePtr
));
3521 cur
->nodeMax
= XML_NODESET_DEFAULT
;
3522 } else if (cur
->nodeNr
== cur
->nodeMax
) {
3526 temp
= (xmlNodePtr
*) xmlRealloc(cur
->nodeTab
, cur
->nodeMax
*
3527 sizeof(xmlNodePtr
));
3529 xmlXPathErrMemory(NULL
, "growing nodeset\n");
3532 cur
->nodeTab
= temp
;
3534 cur
->nodeTab
[cur
->nodeNr
++] = xmlXPathNodeSetDupNs(node
, ns
);
3538 * xmlXPathNodeSetAdd:
3539 * @cur: the initial node set
3540 * @val: a new xmlNodePtr
3542 * add a new xmlNodePtr to an existing NodeSet
3545 xmlXPathNodeSetAdd(xmlNodeSetPtr cur
, xmlNodePtr val
) {
3548 if ((cur
== NULL
) || (val
== NULL
)) return;
3551 if ((val
->type
== XML_ELEMENT_NODE
) && (val
->name
[0] == ' '))
3552 return; /* an XSLT fake node */
3555 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
3559 for (i
= 0;i
< cur
->nodeNr
;i
++)
3560 if (cur
->nodeTab
[i
] == val
) return;
3563 * grow the nodeTab if needed
3565 if (cur
->nodeMax
== 0) {
3566 cur
->nodeTab
= (xmlNodePtr
*) xmlMalloc(XML_NODESET_DEFAULT
*
3567 sizeof(xmlNodePtr
));
3568 if (cur
->nodeTab
== NULL
) {
3569 xmlXPathErrMemory(NULL
, "growing nodeset\n");
3572 memset(cur
->nodeTab
, 0 ,
3573 XML_NODESET_DEFAULT
* (size_t) sizeof(xmlNodePtr
));
3574 cur
->nodeMax
= XML_NODESET_DEFAULT
;
3575 } else if (cur
->nodeNr
== cur
->nodeMax
) {
3579 temp
= (xmlNodePtr
*) xmlRealloc(cur
->nodeTab
, cur
->nodeMax
*
3580 sizeof(xmlNodePtr
));
3582 xmlXPathErrMemory(NULL
, "growing nodeset\n");
3585 cur
->nodeTab
= temp
;
3587 if (val
->type
== XML_NAMESPACE_DECL
) {
3588 xmlNsPtr ns
= (xmlNsPtr
) val
;
3590 cur
->nodeTab
[cur
->nodeNr
++] =
3591 xmlXPathNodeSetDupNs((xmlNodePtr
) ns
->next
, ns
);
3593 cur
->nodeTab
[cur
->nodeNr
++] = val
;
3597 * xmlXPathNodeSetAddUnique:
3598 * @cur: the initial node set
3599 * @val: a new xmlNodePtr
3601 * add a new xmlNodePtr to an existing NodeSet, optimized version
3602 * when we are sure the node is not already in the set.
3605 xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur
, xmlNodePtr val
) {
3606 if ((cur
== NULL
) || (val
== NULL
)) return;
3609 if ((val
->type
== XML_ELEMENT_NODE
) && (val
->name
[0] == ' '))
3610 return; /* an XSLT fake node */
3613 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
3615 * grow the nodeTab if needed
3617 if (cur
->nodeMax
== 0) {
3618 cur
->nodeTab
= (xmlNodePtr
*) xmlMalloc(XML_NODESET_DEFAULT
*
3619 sizeof(xmlNodePtr
));
3620 if (cur
->nodeTab
== NULL
) {
3621 xmlXPathErrMemory(NULL
, "growing nodeset\n");
3624 memset(cur
->nodeTab
, 0 ,
3625 XML_NODESET_DEFAULT
* (size_t) sizeof(xmlNodePtr
));
3626 cur
->nodeMax
= XML_NODESET_DEFAULT
;
3627 } else if (cur
->nodeNr
== cur
->nodeMax
) {
3631 temp
= (xmlNodePtr
*) xmlRealloc(cur
->nodeTab
, cur
->nodeMax
*
3632 sizeof(xmlNodePtr
));
3634 xmlXPathErrMemory(NULL
, "growing nodeset\n");
3637 cur
->nodeTab
= temp
;
3639 if (val
->type
== XML_NAMESPACE_DECL
) {
3640 xmlNsPtr ns
= (xmlNsPtr
) val
;
3642 cur
->nodeTab
[cur
->nodeNr
++] =
3643 xmlXPathNodeSetDupNs((xmlNodePtr
) ns
->next
, ns
);
3645 cur
->nodeTab
[cur
->nodeNr
++] = val
;
3649 * xmlXPathNodeSetMerge:
3650 * @val1: the first NodeSet or NULL
3651 * @val2: the second NodeSet
3653 * Merges two nodesets, all nodes from @val2 are added to @val1
3654 * if @val1 is NULL, a new set is created and copied from @val2
3656 * Returns @val1 once extended or NULL in case of error.
3659 xmlXPathNodeSetMerge(xmlNodeSetPtr val1
, xmlNodeSetPtr val2
) {
3660 int i
, j
, initNr
, skip
;
3663 if (val2
== NULL
) return(val1
);
3665 val1
= xmlXPathNodeSetCreate(NULL
);
3670 * TODO: The optimization won't work in every case, since
3671 * those nasty namespace nodes need to be added with
3672 * xmlXPathNodeSetDupNs() to the set; thus a pure
3673 * memcpy is not possible.
3674 * If there was a flag on the nodesetval, indicating that
3675 * some temporary nodes are in, that would be helpfull.
3678 * Optimization: Create an equally sized node-set
3679 * and memcpy the content.
3681 val1
= xmlXPathNodeSetCreateSize(val2
->nodeNr
);
3684 if (val2
->nodeNr
!= 0) {
3685 if (val2
->nodeNr
== 1)
3686 *(val1
->nodeTab
) = *(val2
->nodeTab
);
3688 memcpy(val1
->nodeTab
, val2
->nodeTab
,
3689 val2
->nodeNr
* sizeof(xmlNodePtr
));
3691 val1
->nodeNr
= val2
->nodeNr
;
3697 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
3698 initNr
= val1
->nodeNr
;
3700 for (i
= 0;i
< val2
->nodeNr
;i
++) {
3701 n2
= val2
->nodeTab
[i
];
3703 * check against duplicates
3706 for (j
= 0; j
< initNr
; j
++) {
3707 n1
= val1
->nodeTab
[j
];
3711 } else if ((n1
->type
== XML_NAMESPACE_DECL
) &&
3712 (n2
->type
== XML_NAMESPACE_DECL
)) {
3713 if ((((xmlNsPtr
) n1
)->next
== ((xmlNsPtr
) n2
)->next
) &&
3714 (xmlStrEqual(((xmlNsPtr
) n1
)->prefix
,
3715 ((xmlNsPtr
) n2
)->prefix
)))
3726 * grow the nodeTab if needed
3728 if (val1
->nodeMax
== 0) {
3729 val1
->nodeTab
= (xmlNodePtr
*) xmlMalloc(XML_NODESET_DEFAULT
*
3730 sizeof(xmlNodePtr
));
3731 if (val1
->nodeTab
== NULL
) {
3732 xmlXPathErrMemory(NULL
, "merging nodeset\n");
3735 memset(val1
->nodeTab
, 0 ,
3736 XML_NODESET_DEFAULT
* (size_t) sizeof(xmlNodePtr
));
3737 val1
->nodeMax
= XML_NODESET_DEFAULT
;
3738 } else if (val1
->nodeNr
== val1
->nodeMax
) {
3742 temp
= (xmlNodePtr
*) xmlRealloc(val1
->nodeTab
, val1
->nodeMax
*
3743 sizeof(xmlNodePtr
));
3745 xmlXPathErrMemory(NULL
, "merging nodeset\n");
3748 val1
->nodeTab
= temp
;
3750 if (n2
->type
== XML_NAMESPACE_DECL
) {
3751 xmlNsPtr ns
= (xmlNsPtr
) n2
;
3753 val1
->nodeTab
[val1
->nodeNr
++] =
3754 xmlXPathNodeSetDupNs((xmlNodePtr
) ns
->next
, ns
);
3756 val1
->nodeTab
[val1
->nodeNr
++] = n2
;
3762 #if 0 /* xmlXPathNodeSetMergeUnique() is currently not used anymore */
3764 * xmlXPathNodeSetMergeUnique:
3765 * @val1: the first NodeSet or NULL
3766 * @val2: the second NodeSet
3768 * Merges two nodesets, all nodes from @val2 are added to @val1
3769 * if @val1 is NULL, a new set is created and copied from @val2
3771 * Returns @val1 once extended or NULL in case of error.
3773 static xmlNodeSetPtr
3774 xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1
, xmlNodeSetPtr val2
) {
3777 if (val2
== NULL
) return(val1
);
3779 val1
= xmlXPathNodeSetCreate(NULL
);
3784 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
3786 for (i
= 0;i
< val2
->nodeNr
;i
++) {
3788 * grow the nodeTab if needed
3790 if (val1
->nodeMax
== 0) {
3791 val1
->nodeTab
= (xmlNodePtr
*) xmlMalloc(XML_NODESET_DEFAULT
*
3792 sizeof(xmlNodePtr
));
3793 if (val1
->nodeTab
== NULL
) {
3794 xmlXPathErrMemory(NULL
, "merging nodeset\n");
3797 memset(val1
->nodeTab
, 0 ,
3798 XML_NODESET_DEFAULT
* (size_t) sizeof(xmlNodePtr
));
3799 val1
->nodeMax
= XML_NODESET_DEFAULT
;
3800 } else if (val1
->nodeNr
== val1
->nodeMax
) {
3804 temp
= (xmlNodePtr
*) xmlRealloc(val1
->nodeTab
, val1
->nodeMax
*
3805 sizeof(xmlNodePtr
));
3807 xmlXPathErrMemory(NULL
, "merging nodeset\n");
3810 val1
->nodeTab
= temp
;
3812 if (val2
->nodeTab
[i
]->type
== XML_NAMESPACE_DECL
) {
3813 xmlNsPtr ns
= (xmlNsPtr
) val2
->nodeTab
[i
];
3815 val1
->nodeTab
[val1
->nodeNr
++] =
3816 xmlXPathNodeSetDupNs((xmlNodePtr
) ns
->next
, ns
);
3818 val1
->nodeTab
[val1
->nodeNr
++] = val2
->nodeTab
[i
];
3823 #endif /* xmlXPathNodeSetMergeUnique() is currently not used anymore */
3826 * xmlXPathNodeSetMergeAndClear:
3827 * @set1: the first NodeSet or NULL
3828 * @set2: the second NodeSet
3829 * @hasSet2NsNodes: 1 if set2 contains namespaces nodes
3831 * Merges two nodesets, all nodes from @set2 are added to @set1
3832 * if @set1 is NULL, a new set is created and copied from @set2.
3833 * Checks for duplicate nodes. Clears set2.
3835 * Returns @set1 once extended or NULL in case of error.
3837 static xmlNodeSetPtr
3838 xmlXPathNodeSetMergeAndClear(xmlNodeSetPtr set1
, xmlNodeSetPtr set2
,
3841 if ((set1
== NULL
) && (hasNullEntries
== 0)) {
3843 * Note that doing a memcpy of the list, namespace nodes are
3844 * just assigned to set1, since set2 is cleared anyway.
3846 set1
= xmlXPathNodeSetCreateSize(set2
->nodeNr
);
3849 if (set2
->nodeNr
!= 0) {
3850 memcpy(set1
->nodeTab
, set2
->nodeTab
,
3851 set2
->nodeNr
* sizeof(xmlNodePtr
));
3852 set1
->nodeNr
= set2
->nodeNr
;
3855 int i
, j
, initNbSet1
;
3859 set1
= xmlXPathNodeSetCreate(NULL
);
3863 initNbSet1
= set1
->nodeNr
;
3864 for (i
= 0;i
< set2
->nodeNr
;i
++) {
3865 n2
= set2
->nodeTab
[i
];
3867 * Skip NULLed entries.
3874 for (j
= 0; j
< initNbSet1
; j
++) {
3875 n1
= set1
->nodeTab
[j
];
3878 } else if ((n1
->type
== XML_NAMESPACE_DECL
) &&
3879 (n2
->type
== XML_NAMESPACE_DECL
))
3881 if ((((xmlNsPtr
) n1
)->next
== ((xmlNsPtr
) n2
)->next
) &&
3882 (xmlStrEqual(((xmlNsPtr
) n1
)->prefix
,
3883 ((xmlNsPtr
) n2
)->prefix
)))
3886 * Free the namespace node.
3888 set2
->nodeTab
[i
] = NULL
;
3889 xmlXPathNodeSetFreeNs((xmlNsPtr
) n2
);
3895 * grow the nodeTab if needed
3897 if (set1
->nodeMax
== 0) {
3898 set1
->nodeTab
= (xmlNodePtr
*) xmlMalloc(
3899 XML_NODESET_DEFAULT
* sizeof(xmlNodePtr
));
3900 if (set1
->nodeTab
== NULL
) {
3901 xmlXPathErrMemory(NULL
, "merging nodeset\n");
3904 memset(set1
->nodeTab
, 0,
3905 XML_NODESET_DEFAULT
* (size_t) sizeof(xmlNodePtr
));
3906 set1
->nodeMax
= XML_NODESET_DEFAULT
;
3907 } else if (set1
->nodeNr
>= set1
->nodeMax
) {
3911 temp
= (xmlNodePtr
*) xmlRealloc(
3912 set1
->nodeTab
, set1
->nodeMax
* sizeof(xmlNodePtr
));
3914 xmlXPathErrMemory(NULL
, "merging nodeset\n");
3917 set1
->nodeTab
= temp
;
3919 if (n2
->type
== XML_NAMESPACE_DECL
) {
3920 xmlNsPtr ns
= (xmlNsPtr
) n2
;
3922 set1
->nodeTab
[set1
->nodeNr
++] =
3923 xmlXPathNodeSetDupNs((xmlNodePtr
) ns
->next
, ns
);
3925 set1
->nodeTab
[set1
->nodeNr
++] = n2
;
3935 * xmlXPathNodeSetMergeAndClearNoDupls:
3936 * @set1: the first NodeSet or NULL
3937 * @set2: the second NodeSet
3938 * @hasSet2NsNodes: 1 if set2 contains namespaces nodes
3940 * Merges two nodesets, all nodes from @set2 are added to @set1
3941 * if @set1 is NULL, a new set is created and copied from @set2.
3942 * Doesn't chack for duplicate nodes. Clears set2.
3944 * Returns @set1 once extended or NULL in case of error.
3946 static xmlNodeSetPtr
3947 xmlXPathNodeSetMergeAndClearNoDupls(xmlNodeSetPtr set1
, xmlNodeSetPtr set2
,
3952 if ((set1
== NULL
) && (hasNullEntries
== 0)) {
3954 * Note that doing a memcpy of the list, namespace nodes are
3955 * just assigned to set1, since set2 is cleared anyway.
3957 set1
= xmlXPathNodeSetCreateSize(set2
->nodeNr
);
3960 if (set2
->nodeNr
!= 0) {
3961 memcpy(set1
->nodeTab
, set2
->nodeTab
,
3962 set2
->nodeNr
* sizeof(xmlNodePtr
));
3963 set1
->nodeNr
= set2
->nodeNr
;
3970 set1
= xmlXPathNodeSetCreate(NULL
);
3974 for (i
= 0;i
< set2
->nodeNr
;i
++) {
3975 n2
= set2
->nodeTab
[i
];
3977 * Skip NULLed entries.
3981 if (set1
->nodeMax
== 0) {
3982 set1
->nodeTab
= (xmlNodePtr
*) xmlMalloc(
3983 XML_NODESET_DEFAULT
* sizeof(xmlNodePtr
));
3984 if (set1
->nodeTab
== NULL
) {
3985 xmlXPathErrMemory(NULL
, "merging nodeset\n");
3988 memset(set1
->nodeTab
, 0,
3989 XML_NODESET_DEFAULT
* (size_t) sizeof(xmlNodePtr
));
3990 set1
->nodeMax
= XML_NODESET_DEFAULT
;
3991 } else if (set1
->nodeNr
>= set1
->nodeMax
) {
3995 temp
= (xmlNodePtr
*) xmlRealloc(
3996 set1
->nodeTab
, set1
->nodeMax
* sizeof(xmlNodePtr
));
3998 xmlXPathErrMemory(NULL
, "merging nodeset\n");
4001 set1
->nodeTab
= temp
;
4003 set1
->nodeTab
[set1
->nodeNr
++] = n2
;
4011 * xmlXPathNodeSetDel:
4012 * @cur: the initial node set
4013 * @val: an xmlNodePtr
4015 * Removes an xmlNodePtr from an existing NodeSet
4018 xmlXPathNodeSetDel(xmlNodeSetPtr cur
, xmlNodePtr val
) {
4021 if (cur
== NULL
) return;
4022 if (val
== NULL
) return;
4025 * find node in nodeTab
4027 for (i
= 0;i
< cur
->nodeNr
;i
++)
4028 if (cur
->nodeTab
[i
] == val
) break;
4030 if (i
>= cur
->nodeNr
) { /* not found */
4032 xmlGenericError(xmlGenericErrorContext
,
4033 "xmlXPathNodeSetDel: Node %s wasn't found in NodeList\n",
4038 if ((cur
->nodeTab
[i
] != NULL
) &&
4039 (cur
->nodeTab
[i
]->type
== XML_NAMESPACE_DECL
))
4040 xmlXPathNodeSetFreeNs((xmlNsPtr
) cur
->nodeTab
[i
]);
4042 for (;i
< cur
->nodeNr
;i
++)
4043 cur
->nodeTab
[i
] = cur
->nodeTab
[i
+ 1];
4044 cur
->nodeTab
[cur
->nodeNr
] = NULL
;
4048 * xmlXPathNodeSetRemove:
4049 * @cur: the initial node set
4050 * @val: the index to remove
4052 * Removes an entry from an existing NodeSet list.
4055 xmlXPathNodeSetRemove(xmlNodeSetPtr cur
, int val
) {
4056 if (cur
== NULL
) return;
4057 if (val
>= cur
->nodeNr
) return;
4058 if ((cur
->nodeTab
[val
] != NULL
) &&
4059 (cur
->nodeTab
[val
]->type
== XML_NAMESPACE_DECL
))
4060 xmlXPathNodeSetFreeNs((xmlNsPtr
) cur
->nodeTab
[val
]);
4062 for (;val
< cur
->nodeNr
;val
++)
4063 cur
->nodeTab
[val
] = cur
->nodeTab
[val
+ 1];
4064 cur
->nodeTab
[cur
->nodeNr
] = NULL
;
4068 * xmlXPathFreeNodeSet:
4069 * @obj: the xmlNodeSetPtr to free
4071 * Free the NodeSet compound (not the actual nodes !).
4074 xmlXPathFreeNodeSet(xmlNodeSetPtr obj
) {
4075 if (obj
== NULL
) return;
4076 if (obj
->nodeTab
!= NULL
) {
4079 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
4080 for (i
= 0;i
< obj
->nodeNr
;i
++)
4081 if ((obj
->nodeTab
[i
] != NULL
) &&
4082 (obj
->nodeTab
[i
]->type
== XML_NAMESPACE_DECL
))
4083 xmlXPathNodeSetFreeNs((xmlNsPtr
) obj
->nodeTab
[i
]);
4084 xmlFree(obj
->nodeTab
);
4090 * xmlXPathNodeSetClear:
4091 * @set: the node set to clear
4093 * Clears the list from all temporary XPath objects (e.g. namespace nodes
4094 * are feed), but does *not* free the list itself. Sets the length of the
4098 xmlXPathNodeSetClear(xmlNodeSetPtr set
, int hasNsNodes
)
4100 if ((set
== NULL
) || (set
->nodeNr
<= 0))
4102 else if (hasNsNodes
) {
4106 for (i
= 0; i
< set
->nodeNr
; i
++) {
4107 node
= set
->nodeTab
[i
];
4108 if ((node
!= NULL
) &&
4109 (node
->type
== XML_NAMESPACE_DECL
))
4110 xmlXPathNodeSetFreeNs((xmlNsPtr
) node
);
4117 * xmlXPathNodeSetClearFromPos:
4118 * @set: the node set to be cleared
4119 * @pos: the start position to clear from
4121 * Clears the list from temporary XPath objects (e.g. namespace nodes
4122 * are feed) starting with the entry at @pos, but does *not* free the list
4123 * itself. Sets the length of the list to @pos.
4126 xmlXPathNodeSetClearFromPos(xmlNodeSetPtr set
, int pos
, int hasNsNodes
)
4128 if ((set
== NULL
) || (set
->nodeNr
<= 0) || (pos
>= set
->nodeNr
))
4130 else if ((hasNsNodes
)) {
4134 for (i
= pos
; i
< set
->nodeNr
; i
++) {
4135 node
= set
->nodeTab
[i
];
4136 if ((node
!= NULL
) &&
4137 (node
->type
== XML_NAMESPACE_DECL
))
4138 xmlXPathNodeSetFreeNs((xmlNsPtr
) node
);
4145 * xmlXPathFreeValueTree:
4146 * @obj: the xmlNodeSetPtr to free
4148 * Free the NodeSet compound and the actual tree, this is different
4149 * from xmlXPathFreeNodeSet()
4152 xmlXPathFreeValueTree(xmlNodeSetPtr obj
) {
4155 if (obj
== NULL
) return;
4157 if (obj
->nodeTab
!= NULL
) {
4158 for (i
= 0;i
< obj
->nodeNr
;i
++) {
4159 if (obj
->nodeTab
[i
] != NULL
) {
4160 if (obj
->nodeTab
[i
]->type
== XML_NAMESPACE_DECL
) {
4161 xmlXPathNodeSetFreeNs((xmlNsPtr
) obj
->nodeTab
[i
]);
4163 xmlFreeNodeList(obj
->nodeTab
[i
]);
4167 xmlFree(obj
->nodeTab
);
4172 #if defined(DEBUG) || defined(DEBUG_STEP)
4174 * xmlGenericErrorContextNodeSet:
4175 * @output: a FILE * for the output
4176 * @obj: the xmlNodeSetPtr to display
4178 * Quick display of a NodeSet
4181 xmlGenericErrorContextNodeSet(FILE *output
, xmlNodeSetPtr obj
) {
4184 if (output
== NULL
) output
= xmlGenericErrorContext
;
4186 fprintf(output
, "NodeSet == NULL !\n");
4189 if (obj
->nodeNr
== 0) {
4190 fprintf(output
, "NodeSet is empty\n");
4193 if (obj
->nodeTab
== NULL
) {
4194 fprintf(output
, " nodeTab == NULL !\n");
4197 for (i
= 0; i
< obj
->nodeNr
; i
++) {
4198 if (obj
->nodeTab
[i
] == NULL
) {
4199 fprintf(output
, " NULL !\n");
4202 if ((obj
->nodeTab
[i
]->type
== XML_DOCUMENT_NODE
) ||
4203 (obj
->nodeTab
[i
]->type
== XML_HTML_DOCUMENT_NODE
))
4204 fprintf(output
, " /");
4205 else if (obj
->nodeTab
[i
]->name
== NULL
)
4206 fprintf(output
, " noname!");
4207 else fprintf(output
, " %s", obj
->nodeTab
[i
]->name
);
4209 fprintf(output
, "\n");
4214 * xmlXPathNewNodeSet:
4215 * @val: the NodePtr value
4217 * Create a new xmlXPathObjectPtr of type NodeSet and initialize
4218 * it with the single Node @val
4220 * Returns the newly created object.
4223 xmlXPathNewNodeSet(xmlNodePtr val
) {
4224 xmlXPathObjectPtr ret
;
4226 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
4228 xmlXPathErrMemory(NULL
, "creating nodeset\n");
4231 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
4232 ret
->type
= XPATH_NODESET
;
4234 ret
->nodesetval
= xmlXPathNodeSetCreate(val
);
4235 /* @@ with_ns to check whether namespace nodes should be looked at @@ */
4236 #ifdef XP_DEBUG_OBJ_USAGE
4237 xmlXPathDebugObjUsageRequested(NULL
, XPATH_NODESET
);
4243 * xmlXPathNewValueTree:
4244 * @val: the NodePtr value
4246 * Create a new xmlXPathObjectPtr of type Value Tree (XSLT) and initialize
4247 * it with the tree root @val
4249 * Returns the newly created object.
4252 xmlXPathNewValueTree(xmlNodePtr val
) {
4253 xmlXPathObjectPtr ret
;
4255 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
4257 xmlXPathErrMemory(NULL
, "creating result value tree\n");
4260 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
4261 ret
->type
= XPATH_XSLT_TREE
;
4263 ret
->user
= (void *) val
;
4264 ret
->nodesetval
= xmlXPathNodeSetCreate(val
);
4265 #ifdef XP_DEBUG_OBJ_USAGE
4266 xmlXPathDebugObjUsageRequested(NULL
, XPATH_XSLT_TREE
);
4272 * xmlXPathNewNodeSetList:
4273 * @val: an existing NodeSet
4275 * Create a new xmlXPathObjectPtr of type NodeSet and initialize
4276 * it with the Nodeset @val
4278 * Returns the newly created object.
4281 xmlXPathNewNodeSetList(xmlNodeSetPtr val
)
4283 xmlXPathObjectPtr ret
;
4288 else if (val
->nodeTab
== NULL
)
4289 ret
= xmlXPathNewNodeSet(NULL
);
4291 ret
= xmlXPathNewNodeSet(val
->nodeTab
[0]);
4293 for (i
= 1; i
< val
->nodeNr
; ++i
)
4294 xmlXPathNodeSetAddUnique(ret
->nodesetval
, val
->nodeTab
[i
]);
4301 * xmlXPathWrapNodeSet:
4302 * @val: the NodePtr value
4304 * Wrap the Nodeset @val in a new xmlXPathObjectPtr
4306 * Returns the newly created object.
4309 xmlXPathWrapNodeSet(xmlNodeSetPtr val
) {
4310 xmlXPathObjectPtr ret
;
4312 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
4314 xmlXPathErrMemory(NULL
, "creating node set object\n");
4317 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
4318 ret
->type
= XPATH_NODESET
;
4319 ret
->nodesetval
= val
;
4320 #ifdef XP_DEBUG_OBJ_USAGE
4321 xmlXPathDebugObjUsageRequested(NULL
, XPATH_NODESET
);
4327 * xmlXPathFreeNodeSetList:
4328 * @obj: an existing NodeSetList object
4330 * Free up the xmlXPathObjectPtr @obj but don't deallocate the objects in
4331 * the list contrary to xmlXPathFreeObject().
4334 xmlXPathFreeNodeSetList(xmlXPathObjectPtr obj
) {
4335 if (obj
== NULL
) return;
4336 #ifdef XP_DEBUG_OBJ_USAGE
4337 xmlXPathDebugObjUsageReleased(NULL
, obj
->type
);
4343 * xmlXPathDifference:
4344 * @nodes1: a node-set
4345 * @nodes2: a node-set
4347 * Implements the EXSLT - Sets difference() function:
4348 * node-set set:difference (node-set, node-set)
4350 * Returns the difference between the two node sets, or nodes1 if
4354 xmlXPathDifference (xmlNodeSetPtr nodes1
, xmlNodeSetPtr nodes2
) {
4359 if (xmlXPathNodeSetIsEmpty(nodes2
))
4362 ret
= xmlXPathNodeSetCreate(NULL
);
4363 if (xmlXPathNodeSetIsEmpty(nodes1
))
4366 l1
= xmlXPathNodeSetGetLength(nodes1
);
4368 for (i
= 0; i
< l1
; i
++) {
4369 cur
= xmlXPathNodeSetItem(nodes1
, i
);
4370 if (!xmlXPathNodeSetContains(nodes2
, cur
))
4371 xmlXPathNodeSetAddUnique(ret
, cur
);
4377 * xmlXPathIntersection:
4378 * @nodes1: a node-set
4379 * @nodes2: a node-set
4381 * Implements the EXSLT - Sets intersection() function:
4382 * node-set set:intersection (node-set, node-set)
4384 * Returns a node set comprising the nodes that are within both the
4385 * node sets passed as arguments
4388 xmlXPathIntersection (xmlNodeSetPtr nodes1
, xmlNodeSetPtr nodes2
) {
4389 xmlNodeSetPtr ret
= xmlXPathNodeSetCreate(NULL
);
4395 if (xmlXPathNodeSetIsEmpty(nodes1
))
4397 if (xmlXPathNodeSetIsEmpty(nodes2
))
4400 l1
= xmlXPathNodeSetGetLength(nodes1
);
4402 for (i
= 0; i
< l1
; i
++) {
4403 cur
= xmlXPathNodeSetItem(nodes1
, i
);
4404 if (xmlXPathNodeSetContains(nodes2
, cur
))
4405 xmlXPathNodeSetAddUnique(ret
, cur
);
4411 * xmlXPathDistinctSorted:
4412 * @nodes: a node-set, sorted by document order
4414 * Implements the EXSLT - Sets distinct() function:
4415 * node-set set:distinct (node-set)
4417 * Returns a subset of the nodes contained in @nodes, or @nodes if
4421 xmlXPathDistinctSorted (xmlNodeSetPtr nodes
) {
4423 xmlHashTablePtr hash
;
4428 if (xmlXPathNodeSetIsEmpty(nodes
))
4431 ret
= xmlXPathNodeSetCreate(NULL
);
4434 l
= xmlXPathNodeSetGetLength(nodes
);
4435 hash
= xmlHashCreate (l
);
4436 for (i
= 0; i
< l
; i
++) {
4437 cur
= xmlXPathNodeSetItem(nodes
, i
);
4438 strval
= xmlXPathCastNodeToString(cur
);
4439 if (xmlHashLookup(hash
, strval
) == NULL
) {
4440 xmlHashAddEntry(hash
, strval
, strval
);
4441 xmlXPathNodeSetAddUnique(ret
, cur
);
4446 xmlHashFree(hash
, (xmlHashDeallocator
) xmlFree
);
4452 * @nodes: a node-set
4454 * Implements the EXSLT - Sets distinct() function:
4455 * node-set set:distinct (node-set)
4456 * @nodes is sorted by document order, then #exslSetsDistinctSorted
4457 * is called with the sorted node-set
4459 * Returns a subset of the nodes contained in @nodes, or @nodes if
4463 xmlXPathDistinct (xmlNodeSetPtr nodes
) {
4464 if (xmlXPathNodeSetIsEmpty(nodes
))
4467 xmlXPathNodeSetSort(nodes
);
4468 return(xmlXPathDistinctSorted(nodes
));
4472 * xmlXPathHasSameNodes:
4473 * @nodes1: a node-set
4474 * @nodes2: a node-set
4476 * Implements the EXSLT - Sets has-same-nodes function:
4477 * boolean set:has-same-node(node-set, node-set)
4479 * Returns true (1) if @nodes1 shares any node with @nodes2, false (0)
4483 xmlXPathHasSameNodes (xmlNodeSetPtr nodes1
, xmlNodeSetPtr nodes2
) {
4487 if (xmlXPathNodeSetIsEmpty(nodes1
) ||
4488 xmlXPathNodeSetIsEmpty(nodes2
))
4491 l
= xmlXPathNodeSetGetLength(nodes1
);
4492 for (i
= 0; i
< l
; i
++) {
4493 cur
= xmlXPathNodeSetItem(nodes1
, i
);
4494 if (xmlXPathNodeSetContains(nodes2
, cur
))
4501 * xmlXPathNodeLeadingSorted:
4502 * @nodes: a node-set, sorted by document order
4505 * Implements the EXSLT - Sets leading() function:
4506 * node-set set:leading (node-set, node-set)
4508 * Returns the nodes in @nodes that precede @node in document order,
4509 * @nodes if @node is NULL or an empty node-set if @nodes
4510 * doesn't contain @node
4513 xmlXPathNodeLeadingSorted (xmlNodeSetPtr nodes
, xmlNodePtr node
) {
4521 ret
= xmlXPathNodeSetCreate(NULL
);
4524 if (xmlXPathNodeSetIsEmpty(nodes
) ||
4525 (!xmlXPathNodeSetContains(nodes
, node
)))
4528 l
= xmlXPathNodeSetGetLength(nodes
);
4529 for (i
= 0; i
< l
; i
++) {
4530 cur
= xmlXPathNodeSetItem(nodes
, i
);
4533 xmlXPathNodeSetAddUnique(ret
, cur
);
4539 * xmlXPathNodeLeading:
4540 * @nodes: a node-set
4543 * Implements the EXSLT - Sets leading() function:
4544 * node-set set:leading (node-set, node-set)
4545 * @nodes is sorted by document order, then #exslSetsNodeLeadingSorted
4548 * Returns the nodes in @nodes that precede @node in document order,
4549 * @nodes if @node is NULL or an empty node-set if @nodes
4550 * doesn't contain @node
4553 xmlXPathNodeLeading (xmlNodeSetPtr nodes
, xmlNodePtr node
) {
4554 xmlXPathNodeSetSort(nodes
);
4555 return(xmlXPathNodeLeadingSorted(nodes
, node
));
4559 * xmlXPathLeadingSorted:
4560 * @nodes1: a node-set, sorted by document order
4561 * @nodes2: a node-set, sorted by document order
4563 * Implements the EXSLT - Sets leading() function:
4564 * node-set set:leading (node-set, node-set)
4566 * Returns the nodes in @nodes1 that precede the first node in @nodes2
4567 * in document order, @nodes1 if @nodes2 is NULL or empty or
4568 * an empty node-set if @nodes1 doesn't contain @nodes2
4571 xmlXPathLeadingSorted (xmlNodeSetPtr nodes1
, xmlNodeSetPtr nodes2
) {
4572 if (xmlXPathNodeSetIsEmpty(nodes2
))
4574 return(xmlXPathNodeLeadingSorted(nodes1
,
4575 xmlXPathNodeSetItem(nodes2
, 1)));
4580 * @nodes1: a node-set
4581 * @nodes2: a node-set
4583 * Implements the EXSLT - Sets leading() function:
4584 * node-set set:leading (node-set, node-set)
4585 * @nodes1 and @nodes2 are sorted by document order, then
4586 * #exslSetsLeadingSorted is called.
4588 * Returns the nodes in @nodes1 that precede the first node in @nodes2
4589 * in document order, @nodes1 if @nodes2 is NULL or empty or
4590 * an empty node-set if @nodes1 doesn't contain @nodes2
4593 xmlXPathLeading (xmlNodeSetPtr nodes1
, xmlNodeSetPtr nodes2
) {
4594 if (xmlXPathNodeSetIsEmpty(nodes2
))
4596 if (xmlXPathNodeSetIsEmpty(nodes1
))
4597 return(xmlXPathNodeSetCreate(NULL
));
4598 xmlXPathNodeSetSort(nodes1
);
4599 xmlXPathNodeSetSort(nodes2
);
4600 return(xmlXPathNodeLeadingSorted(nodes1
,
4601 xmlXPathNodeSetItem(nodes2
, 1)));
4605 * xmlXPathNodeTrailingSorted:
4606 * @nodes: a node-set, sorted by document order
4609 * Implements the EXSLT - Sets trailing() function:
4610 * node-set set:trailing (node-set, node-set)
4612 * Returns the nodes in @nodes that follow @node in document order,
4613 * @nodes if @node is NULL or an empty node-set if @nodes
4614 * doesn't contain @node
4617 xmlXPathNodeTrailingSorted (xmlNodeSetPtr nodes
, xmlNodePtr node
) {
4625 ret
= xmlXPathNodeSetCreate(NULL
);
4628 if (xmlXPathNodeSetIsEmpty(nodes
) ||
4629 (!xmlXPathNodeSetContains(nodes
, node
)))
4632 l
= xmlXPathNodeSetGetLength(nodes
);
4633 for (i
= l
- 1; i
>= 0; i
--) {
4634 cur
= xmlXPathNodeSetItem(nodes
, i
);
4637 xmlXPathNodeSetAddUnique(ret
, cur
);
4639 xmlXPathNodeSetSort(ret
); /* bug 413451 */
4644 * xmlXPathNodeTrailing:
4645 * @nodes: a node-set
4648 * Implements the EXSLT - Sets trailing() function:
4649 * node-set set:trailing (node-set, node-set)
4650 * @nodes is sorted by document order, then #xmlXPathNodeTrailingSorted
4653 * Returns the nodes in @nodes that follow @node in document order,
4654 * @nodes if @node is NULL or an empty node-set if @nodes
4655 * doesn't contain @node
4658 xmlXPathNodeTrailing (xmlNodeSetPtr nodes
, xmlNodePtr node
) {
4659 xmlXPathNodeSetSort(nodes
);
4660 return(xmlXPathNodeTrailingSorted(nodes
, node
));
4664 * xmlXPathTrailingSorted:
4665 * @nodes1: a node-set, sorted by document order
4666 * @nodes2: a node-set, sorted by document order
4668 * Implements the EXSLT - Sets trailing() function:
4669 * node-set set:trailing (node-set, node-set)
4671 * Returns the nodes in @nodes1 that follow the first node in @nodes2
4672 * in document order, @nodes1 if @nodes2 is NULL or empty or
4673 * an empty node-set if @nodes1 doesn't contain @nodes2
4676 xmlXPathTrailingSorted (xmlNodeSetPtr nodes1
, xmlNodeSetPtr nodes2
) {
4677 if (xmlXPathNodeSetIsEmpty(nodes2
))
4679 return(xmlXPathNodeTrailingSorted(nodes1
,
4680 xmlXPathNodeSetItem(nodes2
, 0)));
4685 * @nodes1: a node-set
4686 * @nodes2: a node-set
4688 * Implements the EXSLT - Sets trailing() function:
4689 * node-set set:trailing (node-set, node-set)
4690 * @nodes1 and @nodes2 are sorted by document order, then
4691 * #xmlXPathTrailingSorted is called.
4693 * Returns the nodes in @nodes1 that follow the first node in @nodes2
4694 * in document order, @nodes1 if @nodes2 is NULL or empty or
4695 * an empty node-set if @nodes1 doesn't contain @nodes2
4698 xmlXPathTrailing (xmlNodeSetPtr nodes1
, xmlNodeSetPtr nodes2
) {
4699 if (xmlXPathNodeSetIsEmpty(nodes2
))
4701 if (xmlXPathNodeSetIsEmpty(nodes1
))
4702 return(xmlXPathNodeSetCreate(NULL
));
4703 xmlXPathNodeSetSort(nodes1
);
4704 xmlXPathNodeSetSort(nodes2
);
4705 return(xmlXPathNodeTrailingSorted(nodes1
,
4706 xmlXPathNodeSetItem(nodes2
, 0)));
4709 /************************************************************************
4711 * Routines to handle extra functions *
4713 ************************************************************************/
4716 * xmlXPathRegisterFunc:
4717 * @ctxt: the XPath context
4718 * @name: the function name
4719 * @f: the function implementation or NULL
4721 * Register a new function. If @f is NULL it unregisters the function
4723 * Returns 0 in case of success, -1 in case of error
4726 xmlXPathRegisterFunc(xmlXPathContextPtr ctxt
, const xmlChar
*name
,
4727 xmlXPathFunction f
) {
4728 return(xmlXPathRegisterFuncNS(ctxt
, name
, NULL
, f
));
4732 * xmlXPathRegisterFuncNS:
4733 * @ctxt: the XPath context
4734 * @name: the function name
4735 * @ns_uri: the function namespace URI
4736 * @f: the function implementation or NULL
4738 * Register a new function. If @f is NULL it unregisters the function
4740 * Returns 0 in case of success, -1 in case of error
4743 xmlXPathRegisterFuncNS(xmlXPathContextPtr ctxt
, const xmlChar
*name
,
4744 const xmlChar
*ns_uri
, xmlXPathFunction f
) {
4750 if (ctxt
->funcHash
== NULL
)
4751 ctxt
->funcHash
= xmlHashCreate(0);
4752 if (ctxt
->funcHash
== NULL
)
4755 return(xmlHashRemoveEntry2(ctxt
->funcHash
, name
, ns_uri
, NULL
));
4756 return(xmlHashAddEntry2(ctxt
->funcHash
, name
, ns_uri
, XML_CAST_FPTR(f
)));
4760 * xmlXPathRegisterFuncLookup:
4761 * @ctxt: the XPath context
4762 * @f: the lookup function
4763 * @funcCtxt: the lookup data
4765 * Registers an external mechanism to do function lookup.
4768 xmlXPathRegisterFuncLookup (xmlXPathContextPtr ctxt
,
4769 xmlXPathFuncLookupFunc f
,
4773 ctxt
->funcLookupFunc
= f
;
4774 ctxt
->funcLookupData
= funcCtxt
;
4778 * xmlXPathFunctionLookup:
4779 * @ctxt: the XPath context
4780 * @name: the function name
4782 * Search in the Function array of the context for the given
4785 * Returns the xmlXPathFunction or NULL if not found
4788 xmlXPathFunctionLookup(xmlXPathContextPtr ctxt
, const xmlChar
*name
) {
4792 if (ctxt
->funcLookupFunc
!= NULL
) {
4793 xmlXPathFunction ret
;
4794 xmlXPathFuncLookupFunc f
;
4796 f
= ctxt
->funcLookupFunc
;
4797 ret
= f(ctxt
->funcLookupData
, name
, NULL
);
4801 return(xmlXPathFunctionLookupNS(ctxt
, name
, NULL
));
4805 * xmlXPathFunctionLookupNS:
4806 * @ctxt: the XPath context
4807 * @name: the function name
4808 * @ns_uri: the function namespace URI
4810 * Search in the Function array of the context for the given
4813 * Returns the xmlXPathFunction or NULL if not found
4816 xmlXPathFunctionLookupNS(xmlXPathContextPtr ctxt
, const xmlChar
*name
,
4817 const xmlChar
*ns_uri
) {
4818 xmlXPathFunction ret
;
4825 if (ctxt
->funcLookupFunc
!= NULL
) {
4826 xmlXPathFuncLookupFunc f
;
4828 f
= ctxt
->funcLookupFunc
;
4829 ret
= f(ctxt
->funcLookupData
, name
, ns_uri
);
4834 if (ctxt
->funcHash
== NULL
)
4837 XML_CAST_FPTR(ret
) = xmlHashLookup2(ctxt
->funcHash
, name
, ns_uri
);
4842 * xmlXPathRegisteredFuncsCleanup:
4843 * @ctxt: the XPath context
4845 * Cleanup the XPath context data associated to registered functions
4848 xmlXPathRegisteredFuncsCleanup(xmlXPathContextPtr ctxt
) {
4852 xmlHashFree(ctxt
->funcHash
, NULL
);
4853 ctxt
->funcHash
= NULL
;
4856 /************************************************************************
4858 * Routines to handle Variables *
4860 ************************************************************************/
4863 * xmlXPathRegisterVariable:
4864 * @ctxt: the XPath context
4865 * @name: the variable name
4866 * @value: the variable value or NULL
4868 * Register a new variable value. If @value is NULL it unregisters
4871 * Returns 0 in case of success, -1 in case of error
4874 xmlXPathRegisterVariable(xmlXPathContextPtr ctxt
, const xmlChar
*name
,
4875 xmlXPathObjectPtr value
) {
4876 return(xmlXPathRegisterVariableNS(ctxt
, name
, NULL
, value
));
4880 * xmlXPathRegisterVariableNS:
4881 * @ctxt: the XPath context
4882 * @name: the variable name
4883 * @ns_uri: the variable namespace URI
4884 * @value: the variable value or NULL
4886 * Register a new variable value. If @value is NULL it unregisters
4889 * Returns 0 in case of success, -1 in case of error
4892 xmlXPathRegisterVariableNS(xmlXPathContextPtr ctxt
, const xmlChar
*name
,
4893 const xmlChar
*ns_uri
,
4894 xmlXPathObjectPtr value
) {
4900 if (ctxt
->varHash
== NULL
)
4901 ctxt
->varHash
= xmlHashCreate(0);
4902 if (ctxt
->varHash
== NULL
)
4905 return(xmlHashRemoveEntry2(ctxt
->varHash
, name
, ns_uri
,
4906 (xmlHashDeallocator
)xmlXPathFreeObject
));
4907 return(xmlHashUpdateEntry2(ctxt
->varHash
, name
, ns_uri
,
4909 (xmlHashDeallocator
)xmlXPathFreeObject
));
4913 * xmlXPathRegisterVariableLookup:
4914 * @ctxt: the XPath context
4915 * @f: the lookup function
4916 * @data: the lookup data
4918 * register an external mechanism to do variable lookup
4921 xmlXPathRegisterVariableLookup(xmlXPathContextPtr ctxt
,
4922 xmlXPathVariableLookupFunc f
, void *data
) {
4925 ctxt
->varLookupFunc
= f
;
4926 ctxt
->varLookupData
= data
;
4930 * xmlXPathVariableLookup:
4931 * @ctxt: the XPath context
4932 * @name: the variable name
4934 * Search in the Variable array of the context for the given
4937 * Returns a copy of the value or NULL if not found
4940 xmlXPathVariableLookup(xmlXPathContextPtr ctxt
, const xmlChar
*name
) {
4944 if (ctxt
->varLookupFunc
!= NULL
) {
4945 xmlXPathObjectPtr ret
;
4947 ret
= ((xmlXPathVariableLookupFunc
)ctxt
->varLookupFunc
)
4948 (ctxt
->varLookupData
, name
, NULL
);
4951 return(xmlXPathVariableLookupNS(ctxt
, name
, NULL
));
4955 * xmlXPathVariableLookupNS:
4956 * @ctxt: the XPath context
4957 * @name: the variable name
4958 * @ns_uri: the variable namespace URI
4960 * Search in the Variable array of the context for the given
4963 * Returns the a copy of the value or NULL if not found
4966 xmlXPathVariableLookupNS(xmlXPathContextPtr ctxt
, const xmlChar
*name
,
4967 const xmlChar
*ns_uri
) {
4971 if (ctxt
->varLookupFunc
!= NULL
) {
4972 xmlXPathObjectPtr ret
;
4974 ret
= ((xmlXPathVariableLookupFunc
)ctxt
->varLookupFunc
)
4975 (ctxt
->varLookupData
, name
, ns_uri
);
4976 if (ret
!= NULL
) return(ret
);
4979 if (ctxt
->varHash
== NULL
)
4984 return(xmlXPathCacheObjectCopy(ctxt
, (xmlXPathObjectPtr
)
4985 xmlHashLookup2(ctxt
->varHash
, name
, ns_uri
)));
4989 * xmlXPathRegisteredVariablesCleanup:
4990 * @ctxt: the XPath context
4992 * Cleanup the XPath context data associated to registered variables
4995 xmlXPathRegisteredVariablesCleanup(xmlXPathContextPtr ctxt
) {
4999 xmlHashFree(ctxt
->varHash
, (xmlHashDeallocator
)xmlXPathFreeObject
);
5000 ctxt
->varHash
= NULL
;
5004 * xmlXPathRegisterNs:
5005 * @ctxt: the XPath context
5006 * @prefix: the namespace prefix cannot be NULL or empty string
5007 * @ns_uri: the namespace name
5009 * Register a new namespace. If @ns_uri is NULL it unregisters
5012 * Returns 0 in case of success, -1 in case of error
5015 xmlXPathRegisterNs(xmlXPathContextPtr ctxt
, const xmlChar
*prefix
,
5016 const xmlChar
*ns_uri
) {
5024 if (ctxt
->nsHash
== NULL
)
5025 ctxt
->nsHash
= xmlHashCreate(10);
5026 if (ctxt
->nsHash
== NULL
)
5029 return(xmlHashRemoveEntry(ctxt
->nsHash
, prefix
,
5030 (xmlHashDeallocator
)xmlFree
));
5031 return(xmlHashUpdateEntry(ctxt
->nsHash
, prefix
, (void *) xmlStrdup(ns_uri
),
5032 (xmlHashDeallocator
)xmlFree
));
5037 * @ctxt: the XPath context
5038 * @prefix: the namespace prefix value
5040 * Search in the namespace declaration array of the context for the given
5041 * namespace name associated to the given prefix
5043 * Returns the value or NULL if not found
5046 xmlXPathNsLookup(xmlXPathContextPtr ctxt
, const xmlChar
*prefix
) {
5052 #ifdef XML_XML_NAMESPACE
5053 if (xmlStrEqual(prefix
, (const xmlChar
*) "xml"))
5054 return(XML_XML_NAMESPACE
);
5057 if (ctxt
->namespaces
!= NULL
) {
5060 for (i
= 0;i
< ctxt
->nsNr
;i
++) {
5061 if ((ctxt
->namespaces
[i
] != NULL
) &&
5062 (xmlStrEqual(ctxt
->namespaces
[i
]->prefix
, prefix
)))
5063 return(ctxt
->namespaces
[i
]->href
);
5067 return((const xmlChar
*) xmlHashLookup(ctxt
->nsHash
, prefix
));
5071 * xmlXPathRegisteredNsCleanup:
5072 * @ctxt: the XPath context
5074 * Cleanup the XPath context data associated to registered variables
5077 xmlXPathRegisteredNsCleanup(xmlXPathContextPtr ctxt
) {
5081 xmlHashFree(ctxt
->nsHash
, (xmlHashDeallocator
)xmlFree
);
5082 ctxt
->nsHash
= NULL
;
5085 /************************************************************************
5087 * Routines to handle Values *
5089 ************************************************************************/
5091 /* Allocations are terrible, one needs to optimize all this !!! */
5095 * @val: the double value
5097 * Create a new xmlXPathObjectPtr of type double and of value @val
5099 * Returns the newly created object.
5102 xmlXPathNewFloat(double val
) {
5103 xmlXPathObjectPtr ret
;
5105 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
5107 xmlXPathErrMemory(NULL
, "creating float object\n");
5110 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
5111 ret
->type
= XPATH_NUMBER
;
5112 ret
->floatval
= val
;
5113 #ifdef XP_DEBUG_OBJ_USAGE
5114 xmlXPathDebugObjUsageRequested(NULL
, XPATH_NUMBER
);
5120 * xmlXPathNewBoolean:
5121 * @val: the boolean value
5123 * Create a new xmlXPathObjectPtr of type boolean and of value @val
5125 * Returns the newly created object.
5128 xmlXPathNewBoolean(int val
) {
5129 xmlXPathObjectPtr ret
;
5131 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
5133 xmlXPathErrMemory(NULL
, "creating boolean object\n");
5136 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
5137 ret
->type
= XPATH_BOOLEAN
;
5138 ret
->boolval
= (val
!= 0);
5139 #ifdef XP_DEBUG_OBJ_USAGE
5140 xmlXPathDebugObjUsageRequested(NULL
, XPATH_BOOLEAN
);
5146 * xmlXPathNewString:
5147 * @val: the xmlChar * value
5149 * Create a new xmlXPathObjectPtr of type string and of value @val
5151 * Returns the newly created object.
5154 xmlXPathNewString(const xmlChar
*val
) {
5155 xmlXPathObjectPtr ret
;
5157 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
5159 xmlXPathErrMemory(NULL
, "creating string object\n");
5162 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
5163 ret
->type
= XPATH_STRING
;
5165 ret
->stringval
= xmlStrdup(val
);
5167 ret
->stringval
= xmlStrdup((const xmlChar
*)"");
5168 #ifdef XP_DEBUG_OBJ_USAGE
5169 xmlXPathDebugObjUsageRequested(NULL
, XPATH_STRING
);
5175 * xmlXPathWrapString:
5176 * @val: the xmlChar * value
5178 * Wraps the @val string into an XPath object.
5180 * Returns the newly created object.
5183 xmlXPathWrapString (xmlChar
*val
) {
5184 xmlXPathObjectPtr ret
;
5186 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
5188 xmlXPathErrMemory(NULL
, "creating string object\n");
5191 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
5192 ret
->type
= XPATH_STRING
;
5193 ret
->stringval
= val
;
5194 #ifdef XP_DEBUG_OBJ_USAGE
5195 xmlXPathDebugObjUsageRequested(NULL
, XPATH_STRING
);
5201 * xmlXPathNewCString:
5202 * @val: the char * value
5204 * Create a new xmlXPathObjectPtr of type string and of value @val
5206 * Returns the newly created object.
5209 xmlXPathNewCString(const char *val
) {
5210 xmlXPathObjectPtr ret
;
5212 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
5214 xmlXPathErrMemory(NULL
, "creating string object\n");
5217 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
5218 ret
->type
= XPATH_STRING
;
5219 ret
->stringval
= xmlStrdup(BAD_CAST val
);
5220 #ifdef XP_DEBUG_OBJ_USAGE
5221 xmlXPathDebugObjUsageRequested(NULL
, XPATH_STRING
);
5227 * xmlXPathWrapCString:
5228 * @val: the char * value
5230 * Wraps a string into an XPath object.
5232 * Returns the newly created object.
5235 xmlXPathWrapCString (char * val
) {
5236 return(xmlXPathWrapString((xmlChar
*)(val
)));
5240 * xmlXPathWrapExternal:
5241 * @val: the user data
5243 * Wraps the @val data into an XPath object.
5245 * Returns the newly created object.
5248 xmlXPathWrapExternal (void *val
) {
5249 xmlXPathObjectPtr ret
;
5251 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
5253 xmlXPathErrMemory(NULL
, "creating user object\n");
5256 memset(ret
, 0 , (size_t) sizeof(xmlXPathObject
));
5257 ret
->type
= XPATH_USERS
;
5259 #ifdef XP_DEBUG_OBJ_USAGE
5260 xmlXPathDebugObjUsageRequested(NULL
, XPATH_USERS
);
5266 * xmlXPathObjectCopy:
5267 * @val: the original object
5269 * allocate a new copy of a given object
5271 * Returns the newly created object.
5274 xmlXPathObjectCopy(xmlXPathObjectPtr val
) {
5275 xmlXPathObjectPtr ret
;
5280 ret
= (xmlXPathObjectPtr
) xmlMalloc(sizeof(xmlXPathObject
));
5282 xmlXPathErrMemory(NULL
, "copying object\n");
5285 memcpy(ret
, val
, (size_t) sizeof(xmlXPathObject
));
5286 #ifdef XP_DEBUG_OBJ_USAGE
5287 xmlXPathDebugObjUsageRequested(NULL
, val
->type
);
5289 switch (val
->type
) {
5296 ret
->stringval
= xmlStrdup(val
->stringval
);
5298 case XPATH_XSLT_TREE
:
5301 Removed 11 July 2004 - the current handling of xslt tmpRVT nodes means that
5302 this previous handling is no longer correct, and can cause some serious
5303 problems (ref. bug 145547)
5305 if ((val
->nodesetval
!= NULL
) &&
5306 (val
->nodesetval
->nodeTab
!= NULL
)) {
5307 xmlNodePtr cur
, tmp
;
5311 top
= xmlNewDoc(NULL
);
5312 top
->name
= (char *)
5313 xmlStrdup(val
->nodesetval
->nodeTab
[0]->name
);
5317 cur
= val
->nodesetval
->nodeTab
[0]->children
;
5318 while (cur
!= NULL
) {
5319 tmp
= xmlDocCopyNode(cur
, top
, 1);
5320 xmlAddChild((xmlNodePtr
) top
, tmp
);
5325 ret
->nodesetval
= xmlXPathNodeSetCreate((xmlNodePtr
) top
);
5327 ret
->nodesetval
= xmlXPathNodeSetCreate(NULL
);
5328 /* Deallocate the copied tree value */
5332 ret
->nodesetval
= xmlXPathNodeSetMerge(NULL
, val
->nodesetval
);
5333 /* Do not deallocate the copied tree value */
5336 case XPATH_LOCATIONSET
:
5337 #ifdef LIBXML_XPTR_ENABLED
5339 xmlLocationSetPtr loc
= val
->user
;
5340 ret
->user
= (void *) xmlXPtrLocationSetMerge(NULL
, loc
);
5345 ret
->user
= val
->user
;
5347 case XPATH_UNDEFINED
:
5348 xmlGenericError(xmlGenericErrorContext
,
5349 "xmlXPathObjectCopy: unsupported type %d\n",
5357 * xmlXPathFreeObject:
5358 * @obj: the object to free
5360 * Free up an xmlXPathObjectPtr object.
5363 xmlXPathFreeObject(xmlXPathObjectPtr obj
) {
5364 if (obj
== NULL
) return;
5365 if ((obj
->type
== XPATH_NODESET
) || (obj
->type
== XPATH_XSLT_TREE
)) {
5368 if (obj
->user
!= NULL
) {
5369 xmlXPathFreeNodeSet(obj
->nodesetval
);
5370 xmlFreeNodeList((xmlNodePtr
) obj
->user
);
5373 obj
->type
= XPATH_XSLT_TREE
; /* TODO: Just for debugging. */
5374 if (obj
->nodesetval
!= NULL
)
5375 xmlXPathFreeValueTree(obj
->nodesetval
);
5377 if (obj
->nodesetval
!= NULL
)
5378 xmlXPathFreeNodeSet(obj
->nodesetval
);
5380 #ifdef LIBXML_XPTR_ENABLED
5381 } else if (obj
->type
== XPATH_LOCATIONSET
) {
5382 if (obj
->user
!= NULL
)
5383 xmlXPtrFreeLocationSet(obj
->user
);
5385 } else if (obj
->type
== XPATH_STRING
) {
5386 if (obj
->stringval
!= NULL
)
5387 xmlFree(obj
->stringval
);
5389 #ifdef XP_DEBUG_OBJ_USAGE
5390 xmlXPathDebugObjUsageReleased(NULL
, obj
->type
);
5396 * xmlXPathReleaseObject:
5397 * @obj: the xmlXPathObjectPtr to free or to cache
5399 * Depending on the state of the cache this frees the given
5400 * XPath object or stores it in the cache.
5403 xmlXPathReleaseObject(xmlXPathContextPtr ctxt
, xmlXPathObjectPtr obj
)
5405 #define XP_CACHE_ADD(sl, o) if (sl == NULL) { \
5406 sl = xmlPointerListCreate(10); if (sl == NULL) goto free_obj; } \
5407 if (xmlPointerListAddSize(sl, obj, 0) == -1) goto free_obj;
5409 #define XP_CACHE_WANTS(sl, n) ((sl == NULL) || ((sl)->number < n))
5413 if ((ctxt
== NULL
) || (ctxt
->cache
== NULL
)) {
5414 xmlXPathFreeObject(obj
);
5416 xmlXPathContextCachePtr cache
=
5417 (xmlXPathContextCachePtr
) ctxt
->cache
;
5419 switch (obj
->type
) {
5421 case XPATH_XSLT_TREE
:
5422 if (obj
->nodesetval
!= NULL
) {
5425 * It looks like the @boolval is used for
5426 * evaluation if this an XSLT Result Tree Fragment.
5427 * TODO: Check if this assumption is correct.
5429 obj
->type
= XPATH_XSLT_TREE
; /* just for debugging */
5430 xmlXPathFreeValueTree(obj
->nodesetval
);
5431 obj
->nodesetval
= NULL
;
5432 } else if ((obj
->nodesetval
->nodeMax
<= 40) &&
5433 (XP_CACHE_WANTS(cache
->nodesetObjs
,
5434 cache
->maxNodeset
)))
5436 XP_CACHE_ADD(cache
->nodesetObjs
, obj
);
5439 xmlXPathFreeNodeSet(obj
->nodesetval
);
5440 obj
->nodesetval
= NULL
;
5445 if (obj
->stringval
!= NULL
)
5446 xmlFree(obj
->stringval
);
5448 if (XP_CACHE_WANTS(cache
->stringObjs
, cache
->maxString
)) {
5449 XP_CACHE_ADD(cache
->stringObjs
, obj
);
5454 if (XP_CACHE_WANTS(cache
->booleanObjs
, cache
->maxBoolean
)) {
5455 XP_CACHE_ADD(cache
->booleanObjs
, obj
);
5460 if (XP_CACHE_WANTS(cache
->numberObjs
, cache
->maxNumber
)) {
5461 XP_CACHE_ADD(cache
->numberObjs
, obj
);
5465 #ifdef LIBXML_XPTR_ENABLED
5466 case XPATH_LOCATIONSET
:
5467 if (obj
->user
!= NULL
) {
5468 xmlXPtrFreeLocationSet(obj
->user
);
5477 * Fallback to adding to the misc-objects slot.
5479 if (XP_CACHE_WANTS(cache
->miscObjs
, cache
->maxMisc
)) {
5480 XP_CACHE_ADD(cache
->miscObjs
, obj
);
5486 #ifdef XP_DEBUG_OBJ_USAGE
5487 xmlXPathDebugObjUsageReleased(ctxt
, obj
->type
);
5490 if (obj
->nodesetval
!= NULL
) {
5491 xmlNodeSetPtr tmpset
= obj
->nodesetval
;
5494 * TODO: Due to those nasty ns-nodes, we need to traverse
5495 * the list and free the ns-nodes.
5496 * URGENT TODO: Check if it's actually slowing things down.
5497 * Maybe we shouldn't try to preserve the list.
5499 if (tmpset
->nodeNr
> 1) {
5503 for (i
= 0; i
< tmpset
->nodeNr
; i
++) {
5504 node
= tmpset
->nodeTab
[i
];
5505 if ((node
!= NULL
) &&
5506 (node
->type
== XML_NAMESPACE_DECL
))
5508 xmlXPathNodeSetFreeNs((xmlNsPtr
) node
);
5511 } else if (tmpset
->nodeNr
== 1) {
5512 if ((tmpset
->nodeTab
[0] != NULL
) &&
5513 (tmpset
->nodeTab
[0]->type
== XML_NAMESPACE_DECL
))
5514 xmlXPathNodeSetFreeNs((xmlNsPtr
) tmpset
->nodeTab
[0]);
5517 memset(obj
, 0, sizeof(xmlXPathObject
));
5518 obj
->nodesetval
= tmpset
;
5520 memset(obj
, 0, sizeof(xmlXPathObject
));
5526 * Cache is full; free the object.
5528 if (obj
->nodesetval
!= NULL
)
5529 xmlXPathFreeNodeSet(obj
->nodesetval
);
5530 #ifdef XP_DEBUG_OBJ_USAGE
5531 xmlXPathDebugObjUsageReleased(NULL
, obj
->type
);
5539 /************************************************************************
5541 * Type Casting Routines *
5543 ************************************************************************/
5546 * xmlXPathCastBooleanToString:
5549 * Converts a boolean to its string value.
5551 * Returns a newly allocated string.
5554 xmlXPathCastBooleanToString (int val
) {
5557 ret
= xmlStrdup((const xmlChar
*) "true");
5559 ret
= xmlStrdup((const xmlChar
*) "false");
5564 * xmlXPathCastNumberToString:
5567 * Converts a number to its string value.
5569 * Returns a newly allocated string.
5572 xmlXPathCastNumberToString (double val
) {
5574 switch (xmlXPathIsInf(val
)) {
5576 ret
= xmlStrdup((const xmlChar
*) "Infinity");
5579 ret
= xmlStrdup((const xmlChar
*) "-Infinity");
5582 if (xmlXPathIsNaN(val
)) {
5583 ret
= xmlStrdup((const xmlChar
*) "NaN");
5584 } else if (val
== 0 && xmlXPathGetSign(val
) != 0) {
5585 ret
= xmlStrdup((const xmlChar
*) "0");
5587 /* could be improved */
5589 xmlXPathFormatNumber(val
, buf
, 99);
5591 ret
= xmlStrdup((const xmlChar
*) buf
);
5598 * xmlXPathCastNodeToString:
5601 * Converts a node to its string value.
5603 * Returns a newly allocated string.
5606 xmlXPathCastNodeToString (xmlNodePtr node
) {
5608 if ((ret
= xmlNodeGetContent(node
)) == NULL
)
5609 ret
= xmlStrdup((const xmlChar
*) "");
5614 * xmlXPathCastNodeSetToString:
5617 * Converts a node-set to its string value.
5619 * Returns a newly allocated string.
5622 xmlXPathCastNodeSetToString (xmlNodeSetPtr ns
) {
5623 if ((ns
== NULL
) || (ns
->nodeNr
== 0) || (ns
->nodeTab
== NULL
))
5624 return(xmlStrdup((const xmlChar
*) ""));
5627 xmlXPathNodeSetSort(ns
);
5628 return(xmlXPathCastNodeToString(ns
->nodeTab
[0]));
5632 * xmlXPathCastToString:
5633 * @val: an XPath object
5635 * Converts an existing object to its string() equivalent
5637 * Returns the allocated string value of the object, NULL in case of error.
5638 * It's up to the caller to free the string memory with xmlFree().
5641 xmlXPathCastToString(xmlXPathObjectPtr val
) {
5642 xmlChar
*ret
= NULL
;
5645 return(xmlStrdup((const xmlChar
*) ""));
5646 switch (val
->type
) {
5647 case XPATH_UNDEFINED
:
5649 xmlGenericError(xmlGenericErrorContext
, "String: undefined\n");
5651 ret
= xmlStrdup((const xmlChar
*) "");
5654 case XPATH_XSLT_TREE
:
5655 ret
= xmlXPathCastNodeSetToString(val
->nodesetval
);
5658 return(xmlStrdup(val
->stringval
));
5660 ret
= xmlXPathCastBooleanToString(val
->boolval
);
5662 case XPATH_NUMBER
: {
5663 ret
= xmlXPathCastNumberToString(val
->floatval
);
5669 case XPATH_LOCATIONSET
:
5671 ret
= xmlStrdup((const xmlChar
*) "");
5678 * xmlXPathConvertString:
5679 * @val: an XPath object
5681 * Converts an existing object to its string() equivalent
5683 * Returns the new object, the old one is freed (or the operation
5684 * is done directly on @val)
5687 xmlXPathConvertString(xmlXPathObjectPtr val
) {
5688 xmlChar
*res
= NULL
;
5691 return(xmlXPathNewCString(""));
5693 switch (val
->type
) {
5694 case XPATH_UNDEFINED
:
5696 xmlGenericError(xmlGenericErrorContext
, "STRING: undefined\n");
5700 case XPATH_XSLT_TREE
:
5701 res
= xmlXPathCastNodeSetToString(val
->nodesetval
);
5706 res
= xmlXPathCastBooleanToString(val
->boolval
);
5709 res
= xmlXPathCastNumberToString(val
->floatval
);
5714 case XPATH_LOCATIONSET
:
5718 xmlXPathFreeObject(val
);
5720 return(xmlXPathNewCString(""));
5721 return(xmlXPathWrapString(res
));
5725 * xmlXPathCastBooleanToNumber:
5728 * Converts a boolean to its number value
5730 * Returns the number value
5733 xmlXPathCastBooleanToNumber(int val
) {
5740 * xmlXPathCastStringToNumber:
5743 * Converts a string to its number value
5745 * Returns the number value
5748 xmlXPathCastStringToNumber(const xmlChar
* val
) {
5749 return(xmlXPathStringEvalNumber(val
));
5753 * xmlXPathCastNodeToNumber:
5756 * Converts a node to its number value
5758 * Returns the number value
5761 xmlXPathCastNodeToNumber (xmlNodePtr node
) {
5766 return(xmlXPathNAN
);
5767 strval
= xmlXPathCastNodeToString(node
);
5769 return(xmlXPathNAN
);
5770 ret
= xmlXPathCastStringToNumber(strval
);
5777 * xmlXPathCastNodeSetToNumber:
5780 * Converts a node-set to its number value
5782 * Returns the number value
5785 xmlXPathCastNodeSetToNumber (xmlNodeSetPtr ns
) {
5790 return(xmlXPathNAN
);
5791 str
= xmlXPathCastNodeSetToString(ns
);
5792 ret
= xmlXPathCastStringToNumber(str
);
5798 * xmlXPathCastToNumber:
5799 * @val: an XPath object
5801 * Converts an XPath object to its number value
5803 * Returns the number value
5806 xmlXPathCastToNumber(xmlXPathObjectPtr val
) {
5810 return(xmlXPathNAN
);
5811 switch (val
->type
) {
5812 case XPATH_UNDEFINED
:
5814 xmlGenericError(xmlGenericErrorContext
, "NUMBER: undefined\n");
5819 case XPATH_XSLT_TREE
:
5820 ret
= xmlXPathCastNodeSetToNumber(val
->nodesetval
);
5823 ret
= xmlXPathCastStringToNumber(val
->stringval
);
5826 ret
= val
->floatval
;
5829 ret
= xmlXPathCastBooleanToNumber(val
->boolval
);
5834 case XPATH_LOCATIONSET
:
5843 * xmlXPathConvertNumber:
5844 * @val: an XPath object
5846 * Converts an existing object to its number() equivalent
5848 * Returns the new object, the old one is freed (or the operation
5849 * is done directly on @val)
5852 xmlXPathConvertNumber(xmlXPathObjectPtr val
) {
5853 xmlXPathObjectPtr ret
;
5856 return(xmlXPathNewFloat(0.0));
5857 if (val
->type
== XPATH_NUMBER
)
5859 ret
= xmlXPathNewFloat(xmlXPathCastToNumber(val
));
5860 xmlXPathFreeObject(val
);
5865 * xmlXPathCastNumberToBoolean:
5868 * Converts a number to its boolean value
5870 * Returns the boolean value
5873 xmlXPathCastNumberToBoolean (double val
) {
5874 if (xmlXPathIsNaN(val
) || (val
== 0.0))
5880 * xmlXPathCastStringToBoolean:
5883 * Converts a string to its boolean value
5885 * Returns the boolean value
5888 xmlXPathCastStringToBoolean (const xmlChar
*val
) {
5889 if ((val
== NULL
) || (xmlStrlen(val
) == 0))
5895 * xmlXPathCastNodeSetToBoolean:
5898 * Converts a node-set to its boolean value
5900 * Returns the boolean value
5903 xmlXPathCastNodeSetToBoolean (xmlNodeSetPtr ns
) {
5904 if ((ns
== NULL
) || (ns
->nodeNr
== 0))
5910 * xmlXPathCastToBoolean:
5911 * @val: an XPath object
5913 * Converts an XPath object to its boolean value
5915 * Returns the boolean value
5918 xmlXPathCastToBoolean (xmlXPathObjectPtr val
) {
5923 switch (val
->type
) {
5924 case XPATH_UNDEFINED
:
5926 xmlGenericError(xmlGenericErrorContext
, "BOOLEAN: undefined\n");
5931 case XPATH_XSLT_TREE
:
5932 ret
= xmlXPathCastNodeSetToBoolean(val
->nodesetval
);
5935 ret
= xmlXPathCastStringToBoolean(val
->stringval
);
5938 ret
= xmlXPathCastNumberToBoolean(val
->floatval
);
5946 case XPATH_LOCATIONSET
:
5956 * xmlXPathConvertBoolean:
5957 * @val: an XPath object
5959 * Converts an existing object to its boolean() equivalent
5961 * Returns the new object, the old one is freed (or the operation
5962 * is done directly on @val)
5965 xmlXPathConvertBoolean(xmlXPathObjectPtr val
) {
5966 xmlXPathObjectPtr ret
;
5969 return(xmlXPathNewBoolean(0));
5970 if (val
->type
== XPATH_BOOLEAN
)
5972 ret
= xmlXPathNewBoolean(xmlXPathCastToBoolean(val
));
5973 xmlXPathFreeObject(val
);
5977 /************************************************************************
5979 * Routines to handle XPath contexts *
5981 ************************************************************************/
5984 * xmlXPathNewContext:
5985 * @doc: the XML document
5987 * Create a new xmlXPathContext
5989 * Returns the xmlXPathContext just allocated. The caller will need to free it.
5992 xmlXPathNewContext(xmlDocPtr doc
) {
5993 xmlXPathContextPtr ret
;
5995 ret
= (xmlXPathContextPtr
) xmlMalloc(sizeof(xmlXPathContext
));
5997 xmlXPathErrMemory(NULL
, "creating context\n");
6000 memset(ret
, 0 , (size_t) sizeof(xmlXPathContext
));
6004 ret
->varHash
= NULL
;
6010 ret
->funcHash
= xmlHashCreate(0);
6019 ret
->contextSize
= -1;
6020 ret
->proximityPosition
= -1;
6022 #ifdef XP_DEFAULT_CACHE_ON
6023 if (xmlXPathContextSetCache(ret
, 1, -1, 0) == -1) {
6024 xmlXPathFreeContext(ret
);
6029 xmlXPathRegisterAllFunctions(ret
);
6035 * xmlXPathFreeContext:
6036 * @ctxt: the context to free
6038 * Free up an xmlXPathContext
6041 xmlXPathFreeContext(xmlXPathContextPtr ctxt
) {
6042 if (ctxt
== NULL
) return;
6044 if (ctxt
->cache
!= NULL
)
6045 xmlXPathFreeCache((xmlXPathContextCachePtr
) ctxt
->cache
);
6046 xmlXPathRegisteredNsCleanup(ctxt
);
6047 xmlXPathRegisteredFuncsCleanup(ctxt
);
6048 xmlXPathRegisteredVariablesCleanup(ctxt
);
6049 xmlResetError(&ctxt
->lastError
);
6053 /************************************************************************
6055 * Routines to handle XPath parser contexts *
6057 ************************************************************************/
6059 #define CHECK_CTXT(ctxt) \
6060 if (ctxt == NULL) { \
6061 __xmlRaiseError(NULL, NULL, NULL, \
6062 NULL, NULL, XML_FROM_XPATH, \
6063 XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \
6064 __FILE__, __LINE__, \
6065 NULL, NULL, NULL, 0, 0, \
6066 "NULL context pointer\n"); \
6070 #define CHECK_CTXT_NEG(ctxt) \
6071 if (ctxt == NULL) { \
6072 __xmlRaiseError(NULL, NULL, NULL, \
6073 NULL, NULL, XML_FROM_XPATH, \
6074 XML_ERR_INTERNAL_ERROR, XML_ERR_FATAL, \
6075 __FILE__, __LINE__, \
6076 NULL, NULL, NULL, 0, 0, \
6077 "NULL context pointer\n"); \
6082 #define CHECK_CONTEXT(ctxt) \
6083 if ((ctxt == NULL) || (ctxt->doc == NULL) || \
6084 (ctxt->doc->children == NULL)) { \
6085 xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_INVALID_CTXT); \
6091 * xmlXPathNewParserContext:
6092 * @str: the XPath expression
6093 * @ctxt: the XPath context
6095 * Create a new xmlXPathParserContext
6097 * Returns the xmlXPathParserContext just allocated.
6099 xmlXPathParserContextPtr
6100 xmlXPathNewParserContext(const xmlChar
*str
, xmlXPathContextPtr ctxt
) {
6101 xmlXPathParserContextPtr ret
;
6103 ret
= (xmlXPathParserContextPtr
) xmlMalloc(sizeof(xmlXPathParserContext
));
6105 xmlXPathErrMemory(ctxt
, "creating parser context\n");
6108 memset(ret
, 0 , (size_t) sizeof(xmlXPathParserContext
));
6109 ret
->cur
= ret
->base
= str
;
6110 ret
->context
= ctxt
;
6112 ret
->comp
= xmlXPathNewCompExpr();
6113 if (ret
->comp
== NULL
) {
6114 xmlFree(ret
->valueTab
);
6118 if ((ctxt
!= NULL
) && (ctxt
->dict
!= NULL
)) {
6119 ret
->comp
->dict
= ctxt
->dict
;
6120 xmlDictReference(ret
->comp
->dict
);
6127 * xmlXPathCompParserContext:
6128 * @comp: the XPath compiled expression
6129 * @ctxt: the XPath context
6131 * Create a new xmlXPathParserContext when processing a compiled expression
6133 * Returns the xmlXPathParserContext just allocated.
6135 static xmlXPathParserContextPtr
6136 xmlXPathCompParserContext(xmlXPathCompExprPtr comp
, xmlXPathContextPtr ctxt
) {
6137 xmlXPathParserContextPtr ret
;
6139 ret
= (xmlXPathParserContextPtr
) xmlMalloc(sizeof(xmlXPathParserContext
));
6141 xmlXPathErrMemory(ctxt
, "creating evaluation context\n");
6144 memset(ret
, 0 , (size_t) sizeof(xmlXPathParserContext
));
6146 /* Allocate the value stack */
6147 ret
->valueTab
= (xmlXPathObjectPtr
*)
6148 xmlMalloc(10 * sizeof(xmlXPathObjectPtr
));
6149 if (ret
->valueTab
== NULL
) {
6151 xmlXPathErrMemory(ctxt
, "creating evaluation context\n");
6158 ret
->context
= ctxt
;
6165 * xmlXPathFreeParserContext:
6166 * @ctxt: the context to free
6168 * Free up an xmlXPathParserContext
6171 xmlXPathFreeParserContext(xmlXPathParserContextPtr ctxt
) {
6172 if (ctxt
->valueTab
!= NULL
) {
6173 xmlFree(ctxt
->valueTab
);
6175 if (ctxt
->comp
!= NULL
) {
6176 #ifdef XPATH_STREAMING
6177 if (ctxt
->comp
->stream
!= NULL
) {
6178 xmlFreePatternList(ctxt
->comp
->stream
);
6179 ctxt
->comp
->stream
= NULL
;
6182 xmlXPathFreeCompExpr(ctxt
->comp
);
6187 /************************************************************************
6189 * The implicit core function library *
6191 ************************************************************************/
6194 * xmlXPathNodeValHash:
6195 * @node: a node pointer
6197 * Function computing the beginning of the string value of the node,
6198 * used to speed up comparisons
6200 * Returns an int usable as a hash
6203 xmlXPathNodeValHash(xmlNodePtr node
) {
6205 const xmlChar
* string
= NULL
;
6206 xmlNodePtr tmp
= NULL
;
6207 unsigned int ret
= 0;
6212 if (node
->type
== XML_DOCUMENT_NODE
) {
6213 tmp
= xmlDocGetRootElement((xmlDocPtr
) node
);
6215 node
= node
->children
;
6223 switch (node
->type
) {
6224 case XML_COMMENT_NODE
:
6226 case XML_CDATA_SECTION_NODE
:
6228 string
= node
->content
;
6233 return(((unsigned int) string
[0]) +
6234 (((unsigned int) string
[1]) << 8));
6235 case XML_NAMESPACE_DECL
:
6236 string
= ((xmlNsPtr
)node
)->href
;
6241 return(((unsigned int) string
[0]) +
6242 (((unsigned int) string
[1]) << 8));
6243 case XML_ATTRIBUTE_NODE
:
6244 tmp
= ((xmlAttrPtr
) node
)->children
;
6246 case XML_ELEMENT_NODE
:
6247 tmp
= node
->children
;
6252 while (tmp
!= NULL
) {
6253 switch (tmp
->type
) {
6254 case XML_COMMENT_NODE
:
6256 case XML_CDATA_SECTION_NODE
:
6258 string
= tmp
->content
;
6260 case XML_NAMESPACE_DECL
:
6261 string
= ((xmlNsPtr
)tmp
)->href
;
6266 if ((string
!= NULL
) && (string
[0] != 0)) {
6268 return(ret
+ (((unsigned int) string
[0]) << 8));
6270 if (string
[1] == 0) {
6272 ret
= (unsigned int) string
[0];
6274 return(((unsigned int) string
[0]) +
6275 (((unsigned int) string
[1]) << 8));
6281 if ((tmp
->children
!= NULL
) && (tmp
->type
!= XML_DTD_NODE
)) {
6282 if (tmp
->children
->type
!= XML_ENTITY_DECL
) {
6283 tmp
= tmp
->children
;
6290 if (tmp
->next
!= NULL
) {
6303 if (tmp
->next
!= NULL
) {
6307 } while (tmp
!= NULL
);
6313 * xmlXPathStringHash:
6316 * Function computing the beginning of the string value of the node,
6317 * used to speed up comparisons
6319 * Returns an int usable as a hash
6322 xmlXPathStringHash(const xmlChar
* string
) {
6324 return((unsigned int) 0);
6327 return(((unsigned int) string
[0]) +
6328 (((unsigned int) string
[1]) << 8));
6332 * xmlXPathCompareNodeSetFloat:
6333 * @ctxt: the XPath Parser context
6334 * @inf: less than (1) or greater than (0)
6335 * @strict: is the comparison strict
6336 * @arg: the node set
6339 * Implement the compare operation between a nodeset and a number
6340 * @ns < @val (1, 1, ...
6341 * @ns <= @val (1, 0, ...
6342 * @ns > @val (0, 1, ...
6343 * @ns >= @val (0, 0, ...
6345 * If one object to be compared is a node-set and the other is a number,
6346 * then the comparison will be true if and only if there is a node in the
6347 * node-set such that the result of performing the comparison on the number
6348 * to be compared and on the result of converting the string-value of that
6349 * node to a number using the number function is true.
6351 * Returns 0 or 1 depending on the results of the test.
6354 xmlXPathCompareNodeSetFloat(xmlXPathParserContextPtr ctxt
, int inf
, int strict
,
6355 xmlXPathObjectPtr arg
, xmlXPathObjectPtr f
) {
6360 if ((f
== NULL
) || (arg
== NULL
) ||
6361 ((arg
->type
!= XPATH_NODESET
) && (arg
->type
!= XPATH_XSLT_TREE
))) {
6362 xmlXPathReleaseObject(ctxt
->context
, arg
);
6363 xmlXPathReleaseObject(ctxt
->context
, f
);
6366 ns
= arg
->nodesetval
;
6368 for (i
= 0;i
< ns
->nodeNr
;i
++) {
6369 str2
= xmlXPathCastNodeToString(ns
->nodeTab
[i
]);
6372 xmlXPathCacheNewString(ctxt
->context
, str2
));
6374 xmlXPathNumberFunction(ctxt
, 1);
6375 valuePush(ctxt
, xmlXPathCacheObjectCopy(ctxt
->context
, f
));
6376 ret
= xmlXPathCompareValues(ctxt
, inf
, strict
);
6382 xmlXPathReleaseObject(ctxt
->context
, arg
);
6383 xmlXPathReleaseObject(ctxt
->context
, f
);
6388 * xmlXPathCompareNodeSetString:
6389 * @ctxt: the XPath Parser context
6390 * @inf: less than (1) or greater than (0)
6391 * @strict: is the comparison strict
6392 * @arg: the node set
6395 * Implement the compare operation between a nodeset and a string
6396 * @ns < @val (1, 1, ...
6397 * @ns <= @val (1, 0, ...
6398 * @ns > @val (0, 1, ...
6399 * @ns >= @val (0, 0, ...
6401 * If one object to be compared is a node-set and the other is a string,
6402 * then the comparison will be true if and only if there is a node in
6403 * the node-set such that the result of performing the comparison on the
6404 * string-value of the node and the other string is true.
6406 * Returns 0 or 1 depending on the results of the test.
6409 xmlXPathCompareNodeSetString(xmlXPathParserContextPtr ctxt
, int inf
, int strict
,
6410 xmlXPathObjectPtr arg
, xmlXPathObjectPtr s
) {
6415 if ((s
== NULL
) || (arg
== NULL
) ||
6416 ((arg
->type
!= XPATH_NODESET
) && (arg
->type
!= XPATH_XSLT_TREE
))) {
6417 xmlXPathReleaseObject(ctxt
->context
, arg
);
6418 xmlXPathReleaseObject(ctxt
->context
, s
);
6421 ns
= arg
->nodesetval
;
6423 for (i
= 0;i
< ns
->nodeNr
;i
++) {
6424 str2
= xmlXPathCastNodeToString(ns
->nodeTab
[i
]);
6427 xmlXPathCacheNewString(ctxt
->context
, str2
));
6429 valuePush(ctxt
, xmlXPathCacheObjectCopy(ctxt
->context
, s
));
6430 ret
= xmlXPathCompareValues(ctxt
, inf
, strict
);
6436 xmlXPathReleaseObject(ctxt
->context
, arg
);
6437 xmlXPathReleaseObject(ctxt
->context
, s
);
6442 * xmlXPathCompareNodeSets:
6443 * @inf: less than (1) or greater than (0)
6444 * @strict: is the comparison strict
6445 * @arg1: the first node set object
6446 * @arg2: the second node set object
6448 * Implement the compare operation on nodesets:
6450 * If both objects to be compared are node-sets, then the comparison
6451 * will be true if and only if there is a node in the first node-set
6452 * and a node in the second node-set such that the result of performing
6453 * the comparison on the string-values of the two nodes is true.
6455 * When neither object to be compared is a node-set and the operator
6456 * is <=, <, >= or >, then the objects are compared by converting both
6457 * objects to numbers and comparing the numbers according to IEEE 754.
6459 * The number function converts its argument to a number as follows:
6460 * - a string that consists of optional whitespace followed by an
6461 * optional minus sign followed by a Number followed by whitespace
6462 * is converted to the IEEE 754 number that is nearest (according
6463 * to the IEEE 754 round-to-nearest rule) to the mathematical value
6464 * represented by the string; any other string is converted to NaN
6466 * Conclusion all nodes need to be converted first to their string value
6467 * and then the comparison must be done when possible
6470 xmlXPathCompareNodeSets(int inf
, int strict
,
6471 xmlXPathObjectPtr arg1
, xmlXPathObjectPtr arg2
) {
6479 if ((arg1
== NULL
) ||
6480 ((arg1
->type
!= XPATH_NODESET
) && (arg1
->type
!= XPATH_XSLT_TREE
))) {
6481 xmlXPathFreeObject(arg2
);
6484 if ((arg2
== NULL
) ||
6485 ((arg2
->type
!= XPATH_NODESET
) && (arg2
->type
!= XPATH_XSLT_TREE
))) {
6486 xmlXPathFreeObject(arg1
);
6487 xmlXPathFreeObject(arg2
);
6491 ns1
= arg1
->nodesetval
;
6492 ns2
= arg2
->nodesetval
;
6494 if ((ns1
== NULL
) || (ns1
->nodeNr
<= 0)) {
6495 xmlXPathFreeObject(arg1
);
6496 xmlXPathFreeObject(arg2
);
6499 if ((ns2
== NULL
) || (ns2
->nodeNr
<= 0)) {
6500 xmlXPathFreeObject(arg1
);
6501 xmlXPathFreeObject(arg2
);
6505 values2
= (double *) xmlMalloc(ns2
->nodeNr
* sizeof(double));
6506 if (values2
== NULL
) {
6507 xmlXPathErrMemory(NULL
, "comparing nodesets\n");
6508 xmlXPathFreeObject(arg1
);
6509 xmlXPathFreeObject(arg2
);
6512 for (i
= 0;i
< ns1
->nodeNr
;i
++) {
6513 val1
= xmlXPathCastNodeToNumber(ns1
->nodeTab
[i
]);
6514 if (xmlXPathIsNaN(val1
))
6516 for (j
= 0;j
< ns2
->nodeNr
;j
++) {
6518 values2
[j
] = xmlXPathCastNodeToNumber(ns2
->nodeTab
[j
]);
6520 if (xmlXPathIsNaN(values2
[j
]))
6523 ret
= (val1
< values2
[j
]);
6524 else if (inf
&& !strict
)
6525 ret
= (val1
<= values2
[j
]);
6526 else if (!inf
&& strict
)
6527 ret
= (val1
> values2
[j
]);
6528 else if (!inf
&& !strict
)
6529 ret
= (val1
>= values2
[j
]);
6538 xmlXPathFreeObject(arg1
);
6539 xmlXPathFreeObject(arg2
);
6544 * xmlXPathCompareNodeSetValue:
6545 * @ctxt: the XPath Parser context
6546 * @inf: less than (1) or greater than (0)
6547 * @strict: is the comparison strict
6548 * @arg: the node set
6551 * Implement the compare operation between a nodeset and a value
6552 * @ns < @val (1, 1, ...
6553 * @ns <= @val (1, 0, ...
6554 * @ns > @val (0, 1, ...
6555 * @ns >= @val (0, 0, ...
6557 * If one object to be compared is a node-set and the other is a boolean,
6558 * then the comparison will be true if and only if the result of performing
6559 * the comparison on the boolean and on the result of converting
6560 * the node-set to a boolean using the boolean function is true.
6562 * Returns 0 or 1 depending on the results of the test.
6565 xmlXPathCompareNodeSetValue(xmlXPathParserContextPtr ctxt
, int inf
, int strict
,
6566 xmlXPathObjectPtr arg
, xmlXPathObjectPtr val
) {
6567 if ((val
== NULL
) || (arg
== NULL
) ||
6568 ((arg
->type
!= XPATH_NODESET
) && (arg
->type
!= XPATH_XSLT_TREE
)))
6573 return(xmlXPathCompareNodeSetFloat(ctxt
, inf
, strict
, arg
, val
));
6575 case XPATH_XSLT_TREE
:
6576 return(xmlXPathCompareNodeSets(inf
, strict
, arg
, val
));
6578 return(xmlXPathCompareNodeSetString(ctxt
, inf
, strict
, arg
, val
));
6580 valuePush(ctxt
, arg
);
6581 xmlXPathBooleanFunction(ctxt
, 1);
6582 valuePush(ctxt
, val
);
6583 return(xmlXPathCompareValues(ctxt
, inf
, strict
));
6591 * xmlXPathEqualNodeSetString:
6592 * @arg: the nodeset object argument
6593 * @str: the string to compare to.
6594 * @neq: flag to show whether for '=' (0) or '!=' (1)
6596 * Implement the equal operation on XPath objects content: @arg1 == @arg2
6597 * If one object to be compared is a node-set and the other is a string,
6598 * then the comparison will be true if and only if there is a node in
6599 * the node-set such that the result of performing the comparison on the
6600 * string-value of the node and the other string is true.
6602 * Returns 0 or 1 depending on the results of the test.
6605 xmlXPathEqualNodeSetString(xmlXPathObjectPtr arg
, const xmlChar
* str
, int neq
)
6612 if ((str
== NULL
) || (arg
== NULL
) ||
6613 ((arg
->type
!= XPATH_NODESET
) && (arg
->type
!= XPATH_XSLT_TREE
)))
6615 ns
= arg
->nodesetval
;
6617 * A NULL nodeset compared with a string is always false
6618 * (since there is no node equal, and no node not equal)
6620 if ((ns
== NULL
) || (ns
->nodeNr
<= 0) )
6622 hash
= xmlXPathStringHash(str
);
6623 for (i
= 0; i
< ns
->nodeNr
; i
++) {
6624 if (xmlXPathNodeValHash(ns
->nodeTab
[i
]) == hash
) {
6625 str2
= xmlNodeGetContent(ns
->nodeTab
[i
]);
6626 if ((str2
!= NULL
) && (xmlStrEqual(str
, str2
))) {
6631 } else if ((str2
== NULL
) && (xmlStrEqual(str
, BAD_CAST
""))) {
6649 * xmlXPathEqualNodeSetFloat:
6650 * @arg: the nodeset object argument
6651 * @f: the float to compare to
6652 * @neq: flag to show whether to compare '=' (0) or '!=' (1)
6654 * Implement the equal operation on XPath objects content: @arg1 == @arg2
6655 * If one object to be compared is a node-set and the other is a number,
6656 * then the comparison will be true if and only if there is a node in
6657 * the node-set such that the result of performing the comparison on the
6658 * number to be compared and on the result of converting the string-value
6659 * of that node to a number using the number function is true.
6661 * Returns 0 or 1 depending on the results of the test.
6664 xmlXPathEqualNodeSetFloat(xmlXPathParserContextPtr ctxt
,
6665 xmlXPathObjectPtr arg
, double f
, int neq
) {
6669 xmlXPathObjectPtr val
;
6672 if ((arg
== NULL
) ||
6673 ((arg
->type
!= XPATH_NODESET
) && (arg
->type
!= XPATH_XSLT_TREE
)))
6676 ns
= arg
->nodesetval
;
6678 for (i
=0;i
<ns
->nodeNr
;i
++) {
6679 str2
= xmlXPathCastNodeToString(ns
->nodeTab
[i
]);
6681 valuePush(ctxt
, xmlXPathCacheNewString(ctxt
->context
, str2
));
6683 xmlXPathNumberFunction(ctxt
, 1);
6684 val
= valuePop(ctxt
);
6686 xmlXPathReleaseObject(ctxt
->context
, val
);
6687 if (!xmlXPathIsNaN(v
)) {
6688 if ((!neq
) && (v
==f
)) {
6691 } else if ((neq
) && (v
!=f
)) {
6695 } else { /* NaN is unequal to any value */
6708 * xmlXPathEqualNodeSets:
6709 * @arg1: first nodeset object argument
6710 * @arg2: second nodeset object argument
6711 * @neq: flag to show whether to test '=' (0) or '!=' (1)
6713 * Implement the equal / not equal operation on XPath nodesets:
6714 * @arg1 == @arg2 or @arg1 != @arg2
6715 * If both objects to be compared are node-sets, then the comparison
6716 * will be true if and only if there is a node in the first node-set and
6717 * a node in the second node-set such that the result of performing the
6718 * comparison on the string-values of the two nodes is true.
6720 * (needless to say, this is a costly operation)
6722 * Returns 0 or 1 depending on the results of the test.
6725 xmlXPathEqualNodeSets(xmlXPathObjectPtr arg1
, xmlXPathObjectPtr arg2
, int neq
) {
6727 unsigned int *hashs1
;
6728 unsigned int *hashs2
;
6735 if ((arg1
== NULL
) ||
6736 ((arg1
->type
!= XPATH_NODESET
) && (arg1
->type
!= XPATH_XSLT_TREE
)))
6738 if ((arg2
== NULL
) ||
6739 ((arg2
->type
!= XPATH_NODESET
) && (arg2
->type
!= XPATH_XSLT_TREE
)))
6742 ns1
= arg1
->nodesetval
;
6743 ns2
= arg2
->nodesetval
;
6745 if ((ns1
== NULL
) || (ns1
->nodeNr
<= 0))
6747 if ((ns2
== NULL
) || (ns2
->nodeNr
<= 0))
6751 * for equal, check if there is a node pertaining to both sets
6754 for (i
= 0;i
< ns1
->nodeNr
;i
++)
6755 for (j
= 0;j
< ns2
->nodeNr
;j
++)
6756 if (ns1
->nodeTab
[i
] == ns2
->nodeTab
[j
])
6759 values1
= (xmlChar
**) xmlMalloc(ns1
->nodeNr
* sizeof(xmlChar
*));
6760 if (values1
== NULL
) {
6761 xmlXPathErrMemory(NULL
, "comparing nodesets\n");
6764 hashs1
= (unsigned int *) xmlMalloc(ns1
->nodeNr
* sizeof(unsigned int));
6765 if (hashs1
== NULL
) {
6766 xmlXPathErrMemory(NULL
, "comparing nodesets\n");
6770 memset(values1
, 0, ns1
->nodeNr
* sizeof(xmlChar
*));
6771 values2
= (xmlChar
**) xmlMalloc(ns2
->nodeNr
* sizeof(xmlChar
*));
6772 if (values2
== NULL
) {
6773 xmlXPathErrMemory(NULL
, "comparing nodesets\n");
6778 hashs2
= (unsigned int *) xmlMalloc(ns2
->nodeNr
* sizeof(unsigned int));
6779 if (hashs2
== NULL
) {
6780 xmlXPathErrMemory(NULL
, "comparing nodesets\n");
6786 memset(values2
, 0, ns2
->nodeNr
* sizeof(xmlChar
*));
6787 for (i
= 0;i
< ns1
->nodeNr
;i
++) {
6788 hashs1
[i
] = xmlXPathNodeValHash(ns1
->nodeTab
[i
]);
6789 for (j
= 0;j
< ns2
->nodeNr
;j
++) {
6791 hashs2
[j
] = xmlXPathNodeValHash(ns2
->nodeTab
[j
]);
6792 if (hashs1
[i
] != hashs2
[j
]) {
6799 if (values1
[i
] == NULL
)
6800 values1
[i
] = xmlNodeGetContent(ns1
->nodeTab
[i
]);
6801 if (values2
[j
] == NULL
)
6802 values2
[j
] = xmlNodeGetContent(ns2
->nodeTab
[j
]);
6803 ret
= xmlStrEqual(values1
[i
], values2
[j
]) ^ neq
;
6811 for (i
= 0;i
< ns1
->nodeNr
;i
++)
6812 if (values1
[i
] != NULL
)
6813 xmlFree(values1
[i
]);
6814 for (j
= 0;j
< ns2
->nodeNr
;j
++)
6815 if (values2
[j
] != NULL
)
6816 xmlFree(values2
[j
]);
6825 xmlXPathEqualValuesCommon(xmlXPathParserContextPtr ctxt
,
6826 xmlXPathObjectPtr arg1
, xmlXPathObjectPtr arg2
) {
6829 *At this point we are assured neither arg1 nor arg2
6830 *is a nodeset, so we can just pick the appropriate routine.
6832 switch (arg1
->type
) {
6833 case XPATH_UNDEFINED
:
6835 xmlGenericError(xmlGenericErrorContext
,
6836 "Equal: undefined\n");
6840 switch (arg2
->type
) {
6841 case XPATH_UNDEFINED
:
6843 xmlGenericError(xmlGenericErrorContext
,
6844 "Equal: undefined\n");
6849 xmlGenericError(xmlGenericErrorContext
,
6850 "Equal: %d boolean %d \n",
6851 arg1
->boolval
, arg2
->boolval
);
6853 ret
= (arg1
->boolval
== arg2
->boolval
);
6856 ret
= (arg1
->boolval
==
6857 xmlXPathCastNumberToBoolean(arg2
->floatval
));
6860 if ((arg2
->stringval
== NULL
) ||
6861 (arg2
->stringval
[0] == 0)) ret
= 0;
6864 ret
= (arg1
->boolval
== ret
);
6869 case XPATH_LOCATIONSET
:
6873 case XPATH_XSLT_TREE
:
6878 switch (arg2
->type
) {
6879 case XPATH_UNDEFINED
:
6881 xmlGenericError(xmlGenericErrorContext
,
6882 "Equal: undefined\n");
6886 ret
= (arg2
->boolval
==
6887 xmlXPathCastNumberToBoolean(arg1
->floatval
));
6890 valuePush(ctxt
, arg2
);
6891 xmlXPathNumberFunction(ctxt
, 1);
6892 arg2
= valuePop(ctxt
);
6893 /* no break on purpose */
6895 /* Hand check NaN and Infinity equalities */
6896 if (xmlXPathIsNaN(arg1
->floatval
) ||
6897 xmlXPathIsNaN(arg2
->floatval
)) {
6899 } else if (xmlXPathIsInf(arg1
->floatval
) == 1) {
6900 if (xmlXPathIsInf(arg2
->floatval
) == 1)
6904 } else if (xmlXPathIsInf(arg1
->floatval
) == -1) {
6905 if (xmlXPathIsInf(arg2
->floatval
) == -1)
6909 } else if (xmlXPathIsInf(arg2
->floatval
) == 1) {
6910 if (xmlXPathIsInf(arg1
->floatval
) == 1)
6914 } else if (xmlXPathIsInf(arg2
->floatval
) == -1) {
6915 if (xmlXPathIsInf(arg1
->floatval
) == -1)
6920 ret
= (arg1
->floatval
== arg2
->floatval
);
6926 case XPATH_LOCATIONSET
:
6930 case XPATH_XSLT_TREE
:
6935 switch (arg2
->type
) {
6936 case XPATH_UNDEFINED
:
6938 xmlGenericError(xmlGenericErrorContext
,
6939 "Equal: undefined\n");
6943 if ((arg1
->stringval
== NULL
) ||
6944 (arg1
->stringval
[0] == 0)) ret
= 0;
6947 ret
= (arg2
->boolval
== ret
);
6950 ret
= xmlStrEqual(arg1
->stringval
, arg2
->stringval
);
6953 valuePush(ctxt
, arg1
);
6954 xmlXPathNumberFunction(ctxt
, 1);
6955 arg1
= valuePop(ctxt
);
6956 /* Hand check NaN and Infinity equalities */
6957 if (xmlXPathIsNaN(arg1
->floatval
) ||
6958 xmlXPathIsNaN(arg2
->floatval
)) {
6960 } else if (xmlXPathIsInf(arg1
->floatval
) == 1) {
6961 if (xmlXPathIsInf(arg2
->floatval
) == 1)
6965 } else if (xmlXPathIsInf(arg1
->floatval
) == -1) {
6966 if (xmlXPathIsInf(arg2
->floatval
) == -1)
6970 } else if (xmlXPathIsInf(arg2
->floatval
) == 1) {
6971 if (xmlXPathIsInf(arg1
->floatval
) == 1)
6975 } else if (xmlXPathIsInf(arg2
->floatval
) == -1) {
6976 if (xmlXPathIsInf(arg1
->floatval
) == -1)
6981 ret
= (arg1
->floatval
== arg2
->floatval
);
6987 case XPATH_LOCATIONSET
:
6991 case XPATH_XSLT_TREE
:
6998 case XPATH_LOCATIONSET
:
7002 case XPATH_XSLT_TREE
:
7005 xmlXPathReleaseObject(ctxt
->context
, arg1
);
7006 xmlXPathReleaseObject(ctxt
->context
, arg2
);
7011 * xmlXPathEqualValues:
7012 * @ctxt: the XPath Parser context
7014 * Implement the equal operation on XPath objects content: @arg1 == @arg2
7016 * Returns 0 or 1 depending on the results of the test.
7019 xmlXPathEqualValues(xmlXPathParserContextPtr ctxt
) {
7020 xmlXPathObjectPtr arg1
, arg2
, argtmp
;
7023 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(0);
7024 arg2
= valuePop(ctxt
);
7025 arg1
= valuePop(ctxt
);
7026 if ((arg1
== NULL
) || (arg2
== NULL
)) {
7028 xmlXPathReleaseObject(ctxt
->context
, arg1
);
7030 xmlXPathReleaseObject(ctxt
->context
, arg2
);
7031 XP_ERROR0(XPATH_INVALID_OPERAND
);
7036 xmlGenericError(xmlGenericErrorContext
,
7037 "Equal: by pointer\n");
7039 xmlXPathFreeObject(arg1
);
7044 *If either argument is a nodeset, it's a 'special case'
7046 if ((arg2
->type
== XPATH_NODESET
) || (arg2
->type
== XPATH_XSLT_TREE
) ||
7047 (arg1
->type
== XPATH_NODESET
) || (arg1
->type
== XPATH_XSLT_TREE
)) {
7049 *Hack it to assure arg1 is the nodeset
7051 if ((arg1
->type
!= XPATH_NODESET
) && (arg1
->type
!= XPATH_XSLT_TREE
)) {
7056 switch (arg2
->type
) {
7057 case XPATH_UNDEFINED
:
7059 xmlGenericError(xmlGenericErrorContext
,
7060 "Equal: undefined\n");
7064 case XPATH_XSLT_TREE
:
7065 ret
= xmlXPathEqualNodeSets(arg1
, arg2
, 0);
7068 if ((arg1
->nodesetval
== NULL
) ||
7069 (arg1
->nodesetval
->nodeNr
== 0)) ret
= 0;
7072 ret
= (ret
== arg2
->boolval
);
7075 ret
= xmlXPathEqualNodeSetFloat(ctxt
, arg1
, arg2
->floatval
, 0);
7078 ret
= xmlXPathEqualNodeSetString(arg1
, arg2
->stringval
, 0);
7083 case XPATH_LOCATIONSET
:
7087 xmlXPathReleaseObject(ctxt
->context
, arg1
);
7088 xmlXPathReleaseObject(ctxt
->context
, arg2
);
7092 return (xmlXPathEqualValuesCommon(ctxt
, arg1
, arg2
));
7096 * xmlXPathNotEqualValues:
7097 * @ctxt: the XPath Parser context
7099 * Implement the equal operation on XPath objects content: @arg1 == @arg2
7101 * Returns 0 or 1 depending on the results of the test.
7104 xmlXPathNotEqualValues(xmlXPathParserContextPtr ctxt
) {
7105 xmlXPathObjectPtr arg1
, arg2
, argtmp
;
7108 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(0);
7109 arg2
= valuePop(ctxt
);
7110 arg1
= valuePop(ctxt
);
7111 if ((arg1
== NULL
) || (arg2
== NULL
)) {
7113 xmlXPathReleaseObject(ctxt
->context
, arg1
);
7115 xmlXPathReleaseObject(ctxt
->context
, arg2
);
7116 XP_ERROR0(XPATH_INVALID_OPERAND
);
7121 xmlGenericError(xmlGenericErrorContext
,
7122 "NotEqual: by pointer\n");
7124 xmlXPathReleaseObject(ctxt
->context
, arg1
);
7129 *If either argument is a nodeset, it's a 'special case'
7131 if ((arg2
->type
== XPATH_NODESET
) || (arg2
->type
== XPATH_XSLT_TREE
) ||
7132 (arg1
->type
== XPATH_NODESET
) || (arg1
->type
== XPATH_XSLT_TREE
)) {
7134 *Hack it to assure arg1 is the nodeset
7136 if ((arg1
->type
!= XPATH_NODESET
) && (arg1
->type
!= XPATH_XSLT_TREE
)) {
7141 switch (arg2
->type
) {
7142 case XPATH_UNDEFINED
:
7144 xmlGenericError(xmlGenericErrorContext
,
7145 "NotEqual: undefined\n");
7149 case XPATH_XSLT_TREE
:
7150 ret
= xmlXPathEqualNodeSets(arg1
, arg2
, 1);
7153 if ((arg1
->nodesetval
== NULL
) ||
7154 (arg1
->nodesetval
->nodeNr
== 0)) ret
= 0;
7157 ret
= (ret
!= arg2
->boolval
);
7160 ret
= xmlXPathEqualNodeSetFloat(ctxt
, arg1
, arg2
->floatval
, 1);
7163 ret
= xmlXPathEqualNodeSetString(arg1
, arg2
->stringval
,1);
7168 case XPATH_LOCATIONSET
:
7172 xmlXPathReleaseObject(ctxt
->context
, arg1
);
7173 xmlXPathReleaseObject(ctxt
->context
, arg2
);
7177 return (!xmlXPathEqualValuesCommon(ctxt
, arg1
, arg2
));
7181 * xmlXPathCompareValues:
7182 * @ctxt: the XPath Parser context
7183 * @inf: less than (1) or greater than (0)
7184 * @strict: is the comparison strict
7186 * Implement the compare operation on XPath objects:
7187 * @arg1 < @arg2 (1, 1, ...
7188 * @arg1 <= @arg2 (1, 0, ...
7189 * @arg1 > @arg2 (0, 1, ...
7190 * @arg1 >= @arg2 (0, 0, ...
7192 * When neither object to be compared is a node-set and the operator is
7193 * <=, <, >=, >, then the objects are compared by converted both objects
7194 * to numbers and comparing the numbers according to IEEE 754. The <
7195 * comparison will be true if and only if the first number is less than the
7196 * second number. The <= comparison will be true if and only if the first
7197 * number is less than or equal to the second number. The > comparison
7198 * will be true if and only if the first number is greater than the second
7199 * number. The >= comparison will be true if and only if the first number
7200 * is greater than or equal to the second number.
7202 * Returns 1 if the comparison succeeded, 0 if it failed
7205 xmlXPathCompareValues(xmlXPathParserContextPtr ctxt
, int inf
, int strict
) {
7206 int ret
= 0, arg1i
= 0, arg2i
= 0;
7207 xmlXPathObjectPtr arg1
, arg2
;
7209 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(0);
7210 arg2
= valuePop(ctxt
);
7211 arg1
= valuePop(ctxt
);
7212 if ((arg1
== NULL
) || (arg2
== NULL
)) {
7214 xmlXPathReleaseObject(ctxt
->context
, arg1
);
7216 xmlXPathReleaseObject(ctxt
->context
, arg2
);
7217 XP_ERROR0(XPATH_INVALID_OPERAND
);
7220 if ((arg2
->type
== XPATH_NODESET
) || (arg2
->type
== XPATH_XSLT_TREE
) ||
7221 (arg1
->type
== XPATH_NODESET
) || (arg1
->type
== XPATH_XSLT_TREE
)) {
7223 * If either argument is a XPATH_NODESET or XPATH_XSLT_TREE the two arguments
7224 * are not freed from within this routine; they will be freed from the
7225 * called routine, e.g. xmlXPathCompareNodeSets or xmlXPathCompareNodeSetValue
7227 if (((arg2
->type
== XPATH_NODESET
) || (arg2
->type
== XPATH_XSLT_TREE
)) &&
7228 ((arg1
->type
== XPATH_NODESET
) || (arg1
->type
== XPATH_XSLT_TREE
))){
7229 ret
= xmlXPathCompareNodeSets(inf
, strict
, arg1
, arg2
);
7231 if ((arg1
->type
== XPATH_NODESET
) || (arg1
->type
== XPATH_XSLT_TREE
)) {
7232 ret
= xmlXPathCompareNodeSetValue(ctxt
, inf
, strict
,
7235 ret
= xmlXPathCompareNodeSetValue(ctxt
, !inf
, strict
,
7242 if (arg1
->type
!= XPATH_NUMBER
) {
7243 valuePush(ctxt
, arg1
);
7244 xmlXPathNumberFunction(ctxt
, 1);
7245 arg1
= valuePop(ctxt
);
7247 if (arg1
->type
!= XPATH_NUMBER
) {
7248 xmlXPathFreeObject(arg1
);
7249 xmlXPathFreeObject(arg2
);
7250 XP_ERROR0(XPATH_INVALID_OPERAND
);
7252 if (arg2
->type
!= XPATH_NUMBER
) {
7253 valuePush(ctxt
, arg2
);
7254 xmlXPathNumberFunction(ctxt
, 1);
7255 arg2
= valuePop(ctxt
);
7257 if (arg2
->type
!= XPATH_NUMBER
) {
7258 xmlXPathReleaseObject(ctxt
->context
, arg1
);
7259 xmlXPathReleaseObject(ctxt
->context
, arg2
);
7260 XP_ERROR0(XPATH_INVALID_OPERAND
);
7263 * Add tests for infinity and nan
7264 * => feedback on 3.4 for Inf and NaN
7266 /* Hand check NaN and Infinity comparisons */
7267 if (xmlXPathIsNaN(arg1
->floatval
) || xmlXPathIsNaN(arg2
->floatval
)) {
7270 arg1i
=xmlXPathIsInf(arg1
->floatval
);
7271 arg2i
=xmlXPathIsInf(arg2
->floatval
);
7272 if (inf
&& strict
) {
7273 if ((arg1i
== -1 && arg2i
!= -1) ||
7274 (arg2i
== 1 && arg1i
!= 1)) {
7276 } else if (arg1i
== 0 && arg2i
== 0) {
7277 ret
= (arg1
->floatval
< arg2
->floatval
);
7282 else if (inf
&& !strict
) {
7283 if (arg1i
== -1 || arg2i
== 1) {
7285 } else if (arg1i
== 0 && arg2i
== 0) {
7286 ret
= (arg1
->floatval
<= arg2
->floatval
);
7291 else if (!inf
&& strict
) {
7292 if ((arg1i
== 1 && arg2i
!= 1) ||
7293 (arg2i
== -1 && arg1i
!= -1)) {
7295 } else if (arg1i
== 0 && arg2i
== 0) {
7296 ret
= (arg1
->floatval
> arg2
->floatval
);
7301 else if (!inf
&& !strict
) {
7302 if (arg1i
== 1 || arg2i
== -1) {
7304 } else if (arg1i
== 0 && arg2i
== 0) {
7305 ret
= (arg1
->floatval
>= arg2
->floatval
);
7311 xmlXPathReleaseObject(ctxt
->context
, arg1
);
7312 xmlXPathReleaseObject(ctxt
->context
, arg2
);
7317 * xmlXPathValueFlipSign:
7318 * @ctxt: the XPath Parser context
7320 * Implement the unary - operation on an XPath object
7321 * The numeric operators convert their operands to numbers as if
7322 * by calling the number function.
7325 xmlXPathValueFlipSign(xmlXPathParserContextPtr ctxt
) {
7326 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return;
7328 CHECK_TYPE(XPATH_NUMBER
);
7329 if (xmlXPathIsNaN(ctxt
->value
->floatval
))
7330 ctxt
->value
->floatval
=xmlXPathNAN
;
7331 else if (xmlXPathIsInf(ctxt
->value
->floatval
) == 1)
7332 ctxt
->value
->floatval
=xmlXPathNINF
;
7333 else if (xmlXPathIsInf(ctxt
->value
->floatval
) == -1)
7334 ctxt
->value
->floatval
=xmlXPathPINF
;
7335 else if (ctxt
->value
->floatval
== 0) {
7336 if (xmlXPathGetSign(ctxt
->value
->floatval
) == 0)
7337 ctxt
->value
->floatval
= xmlXPathNZERO
;
7339 ctxt
->value
->floatval
= 0;
7342 ctxt
->value
->floatval
= - ctxt
->value
->floatval
;
7346 * xmlXPathAddValues:
7347 * @ctxt: the XPath Parser context
7349 * Implement the add operation on XPath objects:
7350 * The numeric operators convert their operands to numbers as if
7351 * by calling the number function.
7354 xmlXPathAddValues(xmlXPathParserContextPtr ctxt
) {
7355 xmlXPathObjectPtr arg
;
7358 arg
= valuePop(ctxt
);
7360 XP_ERROR(XPATH_INVALID_OPERAND
);
7361 val
= xmlXPathCastToNumber(arg
);
7362 xmlXPathReleaseObject(ctxt
->context
, arg
);
7364 CHECK_TYPE(XPATH_NUMBER
);
7365 ctxt
->value
->floatval
+= val
;
7369 * xmlXPathSubValues:
7370 * @ctxt: the XPath Parser context
7372 * Implement the subtraction operation on XPath objects:
7373 * The numeric operators convert their operands to numbers as if
7374 * by calling the number function.
7377 xmlXPathSubValues(xmlXPathParserContextPtr ctxt
) {
7378 xmlXPathObjectPtr arg
;
7381 arg
= valuePop(ctxt
);
7383 XP_ERROR(XPATH_INVALID_OPERAND
);
7384 val
= xmlXPathCastToNumber(arg
);
7385 xmlXPathReleaseObject(ctxt
->context
, arg
);
7387 CHECK_TYPE(XPATH_NUMBER
);
7388 ctxt
->value
->floatval
-= val
;
7392 * xmlXPathMultValues:
7393 * @ctxt: the XPath Parser context
7395 * Implement the multiply operation on XPath objects:
7396 * The numeric operators convert their operands to numbers as if
7397 * by calling the number function.
7400 xmlXPathMultValues(xmlXPathParserContextPtr ctxt
) {
7401 xmlXPathObjectPtr arg
;
7404 arg
= valuePop(ctxt
);
7406 XP_ERROR(XPATH_INVALID_OPERAND
);
7407 val
= xmlXPathCastToNumber(arg
);
7408 xmlXPathReleaseObject(ctxt
->context
, arg
);
7410 CHECK_TYPE(XPATH_NUMBER
);
7411 ctxt
->value
->floatval
*= val
;
7415 * xmlXPathDivValues:
7416 * @ctxt: the XPath Parser context
7418 * Implement the div operation on XPath objects @arg1 / @arg2:
7419 * The numeric operators convert their operands to numbers as if
7420 * by calling the number function.
7423 xmlXPathDivValues(xmlXPathParserContextPtr ctxt
) {
7424 xmlXPathObjectPtr arg
;
7427 arg
= valuePop(ctxt
);
7429 XP_ERROR(XPATH_INVALID_OPERAND
);
7430 val
= xmlXPathCastToNumber(arg
);
7431 xmlXPathReleaseObject(ctxt
->context
, arg
);
7433 CHECK_TYPE(XPATH_NUMBER
);
7434 if (xmlXPathIsNaN(val
) || xmlXPathIsNaN(ctxt
->value
->floatval
))
7435 ctxt
->value
->floatval
= xmlXPathNAN
;
7436 else if (val
== 0 && xmlXPathGetSign(val
) != 0) {
7437 if (ctxt
->value
->floatval
== 0)
7438 ctxt
->value
->floatval
= xmlXPathNAN
;
7439 else if (ctxt
->value
->floatval
> 0)
7440 ctxt
->value
->floatval
= xmlXPathNINF
;
7441 else if (ctxt
->value
->floatval
< 0)
7442 ctxt
->value
->floatval
= xmlXPathPINF
;
7444 else if (val
== 0) {
7445 if (ctxt
->value
->floatval
== 0)
7446 ctxt
->value
->floatval
= xmlXPathNAN
;
7447 else if (ctxt
->value
->floatval
> 0)
7448 ctxt
->value
->floatval
= xmlXPathPINF
;
7449 else if (ctxt
->value
->floatval
< 0)
7450 ctxt
->value
->floatval
= xmlXPathNINF
;
7452 ctxt
->value
->floatval
/= val
;
7456 * xmlXPathModValues:
7457 * @ctxt: the XPath Parser context
7459 * Implement the mod operation on XPath objects: @arg1 / @arg2
7460 * The numeric operators convert their operands to numbers as if
7461 * by calling the number function.
7464 xmlXPathModValues(xmlXPathParserContextPtr ctxt
) {
7465 xmlXPathObjectPtr arg
;
7468 arg
= valuePop(ctxt
);
7470 XP_ERROR(XPATH_INVALID_OPERAND
);
7471 arg2
= xmlXPathCastToNumber(arg
);
7472 xmlXPathReleaseObject(ctxt
->context
, arg
);
7474 CHECK_TYPE(XPATH_NUMBER
);
7475 arg1
= ctxt
->value
->floatval
;
7477 ctxt
->value
->floatval
= xmlXPathNAN
;
7479 ctxt
->value
->floatval
= fmod(arg1
, arg2
);
7483 /************************************************************************
7485 * The traversal functions *
7487 ************************************************************************/
7490 * A traversal function enumerates nodes along an axis.
7491 * Initially it must be called with NULL, and it indicates
7492 * termination on the axis by returning NULL.
7494 typedef xmlNodePtr (*xmlXPathTraversalFunction
)
7495 (xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
);
7498 * xmlXPathTraversalFunctionExt:
7499 * A traversal function enumerates nodes along an axis.
7500 * Initially it must be called with NULL, and it indicates
7501 * termination on the axis by returning NULL.
7502 * The context node of the traversal is specified via @contextNode.
7504 typedef xmlNodePtr (*xmlXPathTraversalFunctionExt
)
7505 (xmlNodePtr cur
, xmlNodePtr contextNode
);
7508 * xmlXPathNodeSetMergeFunction:
7509 * Used for merging node sets in xmlXPathCollectAndTest().
7511 typedef xmlNodeSetPtr (*xmlXPathNodeSetMergeFunction
)
7512 (xmlNodeSetPtr
, xmlNodeSetPtr
, int);
7517 * @ctxt: the XPath Parser context
7518 * @cur: the current node in the traversal
7520 * Traversal function for the "self" direction
7521 * The self axis contains just the context node itself
7523 * Returns the next element following that axis
7526 xmlXPathNextSelf(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
7527 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
7529 return(ctxt
->context
->node
);
7534 * xmlXPathNextChild:
7535 * @ctxt: the XPath Parser context
7536 * @cur: the current node in the traversal
7538 * Traversal function for the "child" direction
7539 * The child axis contains the children of the context node in document order.
7541 * Returns the next element following that axis
7544 xmlXPathNextChild(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
7545 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
7547 if (ctxt
->context
->node
== NULL
) return(NULL
);
7548 switch (ctxt
->context
->node
->type
) {
7549 case XML_ELEMENT_NODE
:
7551 case XML_CDATA_SECTION_NODE
:
7552 case XML_ENTITY_REF_NODE
:
7553 case XML_ENTITY_NODE
:
7555 case XML_COMMENT_NODE
:
7556 case XML_NOTATION_NODE
:
7558 return(ctxt
->context
->node
->children
);
7559 case XML_DOCUMENT_NODE
:
7560 case XML_DOCUMENT_TYPE_NODE
:
7561 case XML_DOCUMENT_FRAG_NODE
:
7562 case XML_HTML_DOCUMENT_NODE
:
7563 #ifdef LIBXML_DOCB_ENABLED
7564 case XML_DOCB_DOCUMENT_NODE
:
7566 return(((xmlDocPtr
) ctxt
->context
->node
)->children
);
7567 case XML_ELEMENT_DECL
:
7568 case XML_ATTRIBUTE_DECL
:
7569 case XML_ENTITY_DECL
:
7570 case XML_ATTRIBUTE_NODE
:
7571 case XML_NAMESPACE_DECL
:
7572 case XML_XINCLUDE_START
:
7573 case XML_XINCLUDE_END
:
7578 if ((cur
->type
== XML_DOCUMENT_NODE
) ||
7579 (cur
->type
== XML_HTML_DOCUMENT_NODE
))
7585 * xmlXPathNextChildElement:
7586 * @ctxt: the XPath Parser context
7587 * @cur: the current node in the traversal
7589 * Traversal function for the "child" direction and nodes of type element.
7590 * The child axis contains the children of the context node in document order.
7592 * Returns the next element following that axis
7595 xmlXPathNextChildElement(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
7596 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
7598 cur
= ctxt
->context
->node
;
7599 if (cur
== NULL
) return(NULL
);
7601 * Get the first element child.
7603 switch (cur
->type
) {
7604 case XML_ELEMENT_NODE
:
7605 case XML_DOCUMENT_FRAG_NODE
:
7606 case XML_ENTITY_REF_NODE
: /* URGENT TODO: entify-refs as well? */
7607 case XML_ENTITY_NODE
:
7608 cur
= cur
->children
;
7610 if (cur
->type
== XML_ELEMENT_NODE
)
7614 } while ((cur
!= NULL
) &&
7615 (cur
->type
!= XML_ELEMENT_NODE
));
7619 case XML_DOCUMENT_NODE
:
7620 case XML_HTML_DOCUMENT_NODE
:
7621 #ifdef LIBXML_DOCB_ENABLED
7622 case XML_DOCB_DOCUMENT_NODE
:
7624 return(xmlDocGetRootElement((xmlDocPtr
) cur
));
7631 * Get the next sibling element node.
7633 switch (cur
->type
) {
7634 case XML_ELEMENT_NODE
:
7636 case XML_ENTITY_REF_NODE
:
7637 case XML_ENTITY_NODE
:
7638 case XML_CDATA_SECTION_NODE
:
7640 case XML_COMMENT_NODE
:
7641 case XML_XINCLUDE_END
:
7643 /* case XML_DTD_NODE: */ /* URGENT TODO: DTD-node as well? */
7647 if (cur
->next
!= NULL
) {
7648 if (cur
->next
->type
== XML_ELEMENT_NODE
)
7653 } while ((cur
!= NULL
) && (cur
->type
!= XML_ELEMENT_NODE
));
7660 * xmlXPathNextDescendantOrSelfElemParent:
7661 * @ctxt: the XPath Parser context
7662 * @cur: the current node in the traversal
7664 * Traversal function for the "descendant-or-self" axis.
7665 * Additionally it returns only nodes which can be parents of
7669 * Returns the next element following that axis
7672 xmlXPathNextDescendantOrSelfElemParent(xmlNodePtr cur
,
7673 xmlNodePtr contextNode
)
7676 if (contextNode
== NULL
)
7678 switch (contextNode
->type
) {
7679 case XML_ELEMENT_NODE
:
7680 case XML_XINCLUDE_START
:
7681 case XML_DOCUMENT_FRAG_NODE
:
7682 case XML_DOCUMENT_NODE
:
7683 #ifdef LIBXML_DOCB_ENABLED
7684 case XML_DOCB_DOCUMENT_NODE
:
7686 case XML_HTML_DOCUMENT_NODE
:
7687 return(contextNode
);
7693 xmlNodePtr start
= cur
;
7695 while (cur
!= NULL
) {
7696 switch (cur
->type
) {
7697 case XML_ELEMENT_NODE
:
7698 /* TODO: OK to have XInclude here? */
7699 case XML_XINCLUDE_START
:
7700 case XML_DOCUMENT_FRAG_NODE
:
7703 if (cur
->children
!= NULL
) {
7704 cur
= cur
->children
;
7708 /* Not sure if we need those here. */
7709 case XML_DOCUMENT_NODE
:
7710 #ifdef LIBXML_DOCB_ENABLED
7711 case XML_DOCB_DOCUMENT_NODE
:
7713 case XML_HTML_DOCUMENT_NODE
:
7716 return(xmlDocGetRootElement((xmlDocPtr
) cur
));
7722 if ((cur
== NULL
) || (cur
== contextNode
))
7724 if (cur
->next
!= NULL
) {
7736 * xmlXPathNextDescendant:
7737 * @ctxt: the XPath Parser context
7738 * @cur: the current node in the traversal
7740 * Traversal function for the "descendant" direction
7741 * the descendant axis contains the descendants of the context node in document
7742 * order; a descendant is a child or a child of a child and so on.
7744 * Returns the next element following that axis
7747 xmlXPathNextDescendant(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
7748 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
7750 if (ctxt
->context
->node
== NULL
)
7752 if ((ctxt
->context
->node
->type
== XML_ATTRIBUTE_NODE
) ||
7753 (ctxt
->context
->node
->type
== XML_NAMESPACE_DECL
))
7756 if (ctxt
->context
->node
== (xmlNodePtr
) ctxt
->context
->doc
)
7757 return(ctxt
->context
->doc
->children
);
7758 return(ctxt
->context
->node
->children
);
7761 if (cur
->children
!= NULL
) {
7763 * Do not descend on entities declarations
7765 if (cur
->children
->type
!= XML_ENTITY_DECL
) {
7766 cur
= cur
->children
;
7770 if (cur
->type
!= XML_DTD_NODE
)
7775 if (cur
== ctxt
->context
->node
) return(NULL
);
7777 while (cur
->next
!= NULL
) {
7779 if ((cur
->type
!= XML_ENTITY_DECL
) &&
7780 (cur
->type
!= XML_DTD_NODE
))
7786 if (cur
== NULL
) break;
7787 if (cur
== ctxt
->context
->node
) return(NULL
);
7788 if (cur
->next
!= NULL
) {
7792 } while (cur
!= NULL
);
7797 * xmlXPathNextDescendantOrSelf:
7798 * @ctxt: the XPath Parser context
7799 * @cur: the current node in the traversal
7801 * Traversal function for the "descendant-or-self" direction
7802 * the descendant-or-self axis contains the context node and the descendants
7803 * of the context node in document order; thus the context node is the first
7804 * node on the axis, and the first child of the context node is the second node
7807 * Returns the next element following that axis
7810 xmlXPathNextDescendantOrSelf(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
7811 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
7813 if (ctxt
->context
->node
== NULL
)
7815 if ((ctxt
->context
->node
->type
== XML_ATTRIBUTE_NODE
) ||
7816 (ctxt
->context
->node
->type
== XML_NAMESPACE_DECL
))
7818 return(ctxt
->context
->node
);
7821 return(xmlXPathNextDescendant(ctxt
, cur
));
7825 * xmlXPathNextParent:
7826 * @ctxt: the XPath Parser context
7827 * @cur: the current node in the traversal
7829 * Traversal function for the "parent" direction
7830 * The parent axis contains the parent of the context node, if there is one.
7832 * Returns the next element following that axis
7835 xmlXPathNextParent(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
7836 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
7838 * the parent of an attribute or namespace node is the element
7839 * to which the attribute or namespace node is attached
7840 * Namespace handling !!!
7843 if (ctxt
->context
->node
== NULL
) return(NULL
);
7844 switch (ctxt
->context
->node
->type
) {
7845 case XML_ELEMENT_NODE
:
7847 case XML_CDATA_SECTION_NODE
:
7848 case XML_ENTITY_REF_NODE
:
7849 case XML_ENTITY_NODE
:
7851 case XML_COMMENT_NODE
:
7852 case XML_NOTATION_NODE
:
7854 case XML_ELEMENT_DECL
:
7855 case XML_ATTRIBUTE_DECL
:
7856 case XML_XINCLUDE_START
:
7857 case XML_XINCLUDE_END
:
7858 case XML_ENTITY_DECL
:
7859 if (ctxt
->context
->node
->parent
== NULL
)
7860 return((xmlNodePtr
) ctxt
->context
->doc
);
7861 if ((ctxt
->context
->node
->parent
->type
== XML_ELEMENT_NODE
) &&
7862 ((ctxt
->context
->node
->parent
->name
[0] == ' ') ||
7863 (xmlStrEqual(ctxt
->context
->node
->parent
->name
,
7864 BAD_CAST
"fake node libxslt"))))
7866 return(ctxt
->context
->node
->parent
);
7867 case XML_ATTRIBUTE_NODE
: {
7868 xmlAttrPtr att
= (xmlAttrPtr
) ctxt
->context
->node
;
7870 return(att
->parent
);
7872 case XML_DOCUMENT_NODE
:
7873 case XML_DOCUMENT_TYPE_NODE
:
7874 case XML_DOCUMENT_FRAG_NODE
:
7875 case XML_HTML_DOCUMENT_NODE
:
7876 #ifdef LIBXML_DOCB_ENABLED
7877 case XML_DOCB_DOCUMENT_NODE
:
7880 case XML_NAMESPACE_DECL
: {
7881 xmlNsPtr ns
= (xmlNsPtr
) ctxt
->context
->node
;
7883 if ((ns
->next
!= NULL
) &&
7884 (ns
->next
->type
!= XML_NAMESPACE_DECL
))
7885 return((xmlNodePtr
) ns
->next
);
7894 * xmlXPathNextAncestor:
7895 * @ctxt: the XPath Parser context
7896 * @cur: the current node in the traversal
7898 * Traversal function for the "ancestor" direction
7899 * the ancestor axis contains the ancestors of the context node; the ancestors
7900 * of the context node consist of the parent of context node and the parent's
7901 * parent and so on; the nodes are ordered in reverse document order; thus the
7902 * parent is the first node on the axis, and the parent's parent is the second
7905 * Returns the next element following that axis
7908 xmlXPathNextAncestor(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
7909 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
7911 * the parent of an attribute or namespace node is the element
7912 * to which the attribute or namespace node is attached
7916 if (ctxt
->context
->node
== NULL
) return(NULL
);
7917 switch (ctxt
->context
->node
->type
) {
7918 case XML_ELEMENT_NODE
:
7920 case XML_CDATA_SECTION_NODE
:
7921 case XML_ENTITY_REF_NODE
:
7922 case XML_ENTITY_NODE
:
7924 case XML_COMMENT_NODE
:
7926 case XML_ELEMENT_DECL
:
7927 case XML_ATTRIBUTE_DECL
:
7928 case XML_ENTITY_DECL
:
7929 case XML_NOTATION_NODE
:
7930 case XML_XINCLUDE_START
:
7931 case XML_XINCLUDE_END
:
7932 if (ctxt
->context
->node
->parent
== NULL
)
7933 return((xmlNodePtr
) ctxt
->context
->doc
);
7934 if ((ctxt
->context
->node
->parent
->type
== XML_ELEMENT_NODE
) &&
7935 ((ctxt
->context
->node
->parent
->name
[0] == ' ') ||
7936 (xmlStrEqual(ctxt
->context
->node
->parent
->name
,
7937 BAD_CAST
"fake node libxslt"))))
7939 return(ctxt
->context
->node
->parent
);
7940 case XML_ATTRIBUTE_NODE
: {
7941 xmlAttrPtr tmp
= (xmlAttrPtr
) ctxt
->context
->node
;
7943 return(tmp
->parent
);
7945 case XML_DOCUMENT_NODE
:
7946 case XML_DOCUMENT_TYPE_NODE
:
7947 case XML_DOCUMENT_FRAG_NODE
:
7948 case XML_HTML_DOCUMENT_NODE
:
7949 #ifdef LIBXML_DOCB_ENABLED
7950 case XML_DOCB_DOCUMENT_NODE
:
7953 case XML_NAMESPACE_DECL
: {
7954 xmlNsPtr ns
= (xmlNsPtr
) ctxt
->context
->node
;
7956 if ((ns
->next
!= NULL
) &&
7957 (ns
->next
->type
!= XML_NAMESPACE_DECL
))
7958 return((xmlNodePtr
) ns
->next
);
7959 /* Bad, how did that namespace end up here ? */
7965 if (cur
== ctxt
->context
->doc
->children
)
7966 return((xmlNodePtr
) ctxt
->context
->doc
);
7967 if (cur
== (xmlNodePtr
) ctxt
->context
->doc
)
7969 switch (cur
->type
) {
7970 case XML_ELEMENT_NODE
:
7972 case XML_CDATA_SECTION_NODE
:
7973 case XML_ENTITY_REF_NODE
:
7974 case XML_ENTITY_NODE
:
7976 case XML_COMMENT_NODE
:
7977 case XML_NOTATION_NODE
:
7979 case XML_ELEMENT_DECL
:
7980 case XML_ATTRIBUTE_DECL
:
7981 case XML_ENTITY_DECL
:
7982 case XML_XINCLUDE_START
:
7983 case XML_XINCLUDE_END
:
7984 if (cur
->parent
== NULL
)
7986 if ((cur
->parent
->type
== XML_ELEMENT_NODE
) &&
7987 ((cur
->parent
->name
[0] == ' ') ||
7988 (xmlStrEqual(cur
->parent
->name
,
7989 BAD_CAST
"fake node libxslt"))))
7991 return(cur
->parent
);
7992 case XML_ATTRIBUTE_NODE
: {
7993 xmlAttrPtr att
= (xmlAttrPtr
) ctxt
->context
->node
;
7995 return(att
->parent
);
7997 case XML_NAMESPACE_DECL
: {
7998 xmlNsPtr ns
= (xmlNsPtr
) ctxt
->context
->node
;
8000 if ((ns
->next
!= NULL
) &&
8001 (ns
->next
->type
!= XML_NAMESPACE_DECL
))
8002 return((xmlNodePtr
) ns
->next
);
8003 /* Bad, how did that namespace end up here ? */
8006 case XML_DOCUMENT_NODE
:
8007 case XML_DOCUMENT_TYPE_NODE
:
8008 case XML_DOCUMENT_FRAG_NODE
:
8009 case XML_HTML_DOCUMENT_NODE
:
8010 #ifdef LIBXML_DOCB_ENABLED
8011 case XML_DOCB_DOCUMENT_NODE
:
8019 * xmlXPathNextAncestorOrSelf:
8020 * @ctxt: the XPath Parser context
8021 * @cur: the current node in the traversal
8023 * Traversal function for the "ancestor-or-self" direction
8024 * he ancestor-or-self axis contains the context node and ancestors of
8025 * the context node in reverse document order; thus the context node is
8026 * the first node on the axis, and the context node's parent the second;
8027 * parent here is defined the same as with the parent axis.
8029 * Returns the next element following that axis
8032 xmlXPathNextAncestorOrSelf(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
8033 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
8035 return(ctxt
->context
->node
);
8036 return(xmlXPathNextAncestor(ctxt
, cur
));
8040 * xmlXPathNextFollowingSibling:
8041 * @ctxt: the XPath Parser context
8042 * @cur: the current node in the traversal
8044 * Traversal function for the "following-sibling" direction
8045 * The following-sibling axis contains the following siblings of the context
8046 * node in document order.
8048 * Returns the next element following that axis
8051 xmlXPathNextFollowingSibling(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
8052 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
8053 if ((ctxt
->context
->node
->type
== XML_ATTRIBUTE_NODE
) ||
8054 (ctxt
->context
->node
->type
== XML_NAMESPACE_DECL
))
8056 if (cur
== (xmlNodePtr
) ctxt
->context
->doc
)
8059 return(ctxt
->context
->node
->next
);
8064 * xmlXPathNextPrecedingSibling:
8065 * @ctxt: the XPath Parser context
8066 * @cur: the current node in the traversal
8068 * Traversal function for the "preceding-sibling" direction
8069 * The preceding-sibling axis contains the preceding siblings of the context
8070 * node in reverse document order; the first preceding sibling is first on the
8071 * axis; the sibling preceding that node is the second on the axis and so on.
8073 * Returns the next element following that axis
8076 xmlXPathNextPrecedingSibling(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
8077 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
8078 if ((ctxt
->context
->node
->type
== XML_ATTRIBUTE_NODE
) ||
8079 (ctxt
->context
->node
->type
== XML_NAMESPACE_DECL
))
8081 if (cur
== (xmlNodePtr
) ctxt
->context
->doc
)
8084 return(ctxt
->context
->node
->prev
);
8085 if ((cur
->prev
!= NULL
) && (cur
->prev
->type
== XML_DTD_NODE
)) {
8088 return(ctxt
->context
->node
->prev
);
8094 * xmlXPathNextFollowing:
8095 * @ctxt: the XPath Parser context
8096 * @cur: the current node in the traversal
8098 * Traversal function for the "following" direction
8099 * The following axis contains all nodes in the same document as the context
8100 * node that are after the context node in document order, excluding any
8101 * descendants and excluding attribute nodes and namespace nodes; the nodes
8102 * are ordered in document order
8104 * Returns the next element following that axis
8107 xmlXPathNextFollowing(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
8108 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
8109 if (cur
!= NULL
&& cur
->children
!= NULL
)
8110 return cur
->children
;
8111 if (cur
== NULL
) cur
= ctxt
->context
->node
;
8112 if (cur
== NULL
) return(NULL
) ; /* ERROR */
8113 if (cur
->next
!= NULL
) return(cur
->next
) ;
8116 if (cur
== NULL
) break;
8117 if (cur
== (xmlNodePtr
) ctxt
->context
->doc
) return(NULL
);
8118 if (cur
->next
!= NULL
) return(cur
->next
);
8119 } while (cur
!= NULL
);
8124 * xmlXPathIsAncestor:
8125 * @ancestor: the ancestor node
8126 * @node: the current node
8128 * Check that @ancestor is a @node's ancestor
8130 * returns 1 if @ancestor is a @node's ancestor, 0 otherwise.
8133 xmlXPathIsAncestor(xmlNodePtr ancestor
, xmlNodePtr node
) {
8134 if ((ancestor
== NULL
) || (node
== NULL
)) return(0);
8135 /* nodes need to be in the same document */
8136 if (ancestor
->doc
!= node
->doc
) return(0);
8137 /* avoid searching if ancestor or node is the root node */
8138 if (ancestor
== (xmlNodePtr
) node
->doc
) return(1);
8139 if (node
== (xmlNodePtr
) ancestor
->doc
) return(0);
8140 while (node
->parent
!= NULL
) {
8141 if (node
->parent
== ancestor
)
8143 node
= node
->parent
;
8149 * xmlXPathNextPreceding:
8150 * @ctxt: the XPath Parser context
8151 * @cur: the current node in the traversal
8153 * Traversal function for the "preceding" direction
8154 * the preceding axis contains all nodes in the same document as the context
8155 * node that are before the context node in document order, excluding any
8156 * ancestors and excluding attribute nodes and namespace nodes; the nodes are
8157 * ordered in reverse document order
8159 * Returns the next element following that axis
8162 xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
)
8164 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
8166 cur
= ctxt
->context
->node
;
8169 if ((cur
->prev
!= NULL
) && (cur
->prev
->type
== XML_DTD_NODE
))
8172 if (cur
->prev
!= NULL
) {
8173 for (cur
= cur
->prev
; cur
->last
!= NULL
; cur
= cur
->last
) ;
8180 if (cur
== ctxt
->context
->doc
->children
)
8182 } while (xmlXPathIsAncestor(cur
, ctxt
->context
->node
));
8187 * xmlXPathNextPrecedingInternal:
8188 * @ctxt: the XPath Parser context
8189 * @cur: the current node in the traversal
8191 * Traversal function for the "preceding" direction
8192 * the preceding axis contains all nodes in the same document as the context
8193 * node that are before the context node in document order, excluding any
8194 * ancestors and excluding attribute nodes and namespace nodes; the nodes are
8195 * ordered in reverse document order
8196 * This is a faster implementation but internal only since it requires a
8197 * state kept in the parser context: ctxt->ancestor.
8199 * Returns the next element following that axis
8202 xmlXPathNextPrecedingInternal(xmlXPathParserContextPtr ctxt
,
8205 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
8207 cur
= ctxt
->context
->node
;
8210 if (cur
->type
== XML_NAMESPACE_DECL
)
8211 cur
= (xmlNodePtr
)((xmlNsPtr
)cur
)->next
;
8212 ctxt
->ancestor
= cur
->parent
;
8214 if ((cur
->prev
!= NULL
) && (cur
->prev
->type
== XML_DTD_NODE
))
8216 while (cur
->prev
== NULL
) {
8220 if (cur
== ctxt
->context
->doc
->children
)
8222 if (cur
!= ctxt
->ancestor
)
8224 ctxt
->ancestor
= cur
->parent
;
8227 while (cur
->last
!= NULL
)
8233 * xmlXPathNextNamespace:
8234 * @ctxt: the XPath Parser context
8235 * @cur: the current attribute in the traversal
8237 * Traversal function for the "namespace" direction
8238 * the namespace axis contains the namespace nodes of the context node;
8239 * the order of nodes on this axis is implementation-defined; the axis will
8240 * be empty unless the context node is an element
8242 * We keep the XML namespace node at the end of the list.
8244 * Returns the next element following that axis
8247 xmlXPathNextNamespace(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
8248 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
8249 if (ctxt
->context
->node
->type
!= XML_ELEMENT_NODE
) return(NULL
);
8250 if (ctxt
->context
->tmpNsList
== NULL
&& cur
!= (xmlNodePtr
) xmlXPathXMLNamespace
) {
8251 if (ctxt
->context
->tmpNsList
!= NULL
)
8252 xmlFree(ctxt
->context
->tmpNsList
);
8253 ctxt
->context
->tmpNsList
=
8254 xmlGetNsList(ctxt
->context
->doc
, ctxt
->context
->node
);
8255 ctxt
->context
->tmpNsNr
= 0;
8256 if (ctxt
->context
->tmpNsList
!= NULL
) {
8257 while (ctxt
->context
->tmpNsList
[ctxt
->context
->tmpNsNr
] != NULL
) {
8258 ctxt
->context
->tmpNsNr
++;
8261 return((xmlNodePtr
) xmlXPathXMLNamespace
);
8263 if (ctxt
->context
->tmpNsNr
> 0) {
8264 return (xmlNodePtr
)ctxt
->context
->tmpNsList
[--ctxt
->context
->tmpNsNr
];
8266 if (ctxt
->context
->tmpNsList
!= NULL
)
8267 xmlFree(ctxt
->context
->tmpNsList
);
8268 ctxt
->context
->tmpNsList
= NULL
;
8274 * xmlXPathNextAttribute:
8275 * @ctxt: the XPath Parser context
8276 * @cur: the current attribute in the traversal
8278 * Traversal function for the "attribute" direction
8279 * TODO: support DTD inherited default attributes
8281 * Returns the next element following that axis
8284 xmlXPathNextAttribute(xmlXPathParserContextPtr ctxt
, xmlNodePtr cur
) {
8285 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
)) return(NULL
);
8286 if (ctxt
->context
->node
== NULL
)
8288 if (ctxt
->context
->node
->type
!= XML_ELEMENT_NODE
)
8291 if (ctxt
->context
->node
== (xmlNodePtr
) ctxt
->context
->doc
)
8293 return((xmlNodePtr
)ctxt
->context
->node
->properties
);
8295 return((xmlNodePtr
)cur
->next
);
8298 /************************************************************************
8300 * NodeTest Functions *
8302 ************************************************************************/
8304 #define IS_FUNCTION 200
8307 /************************************************************************
8309 * Implicit tree core function library *
8311 ************************************************************************/
8315 * @ctxt: the XPath Parser context
8317 * Initialize the context to the root of the document
8320 xmlXPathRoot(xmlXPathParserContextPtr ctxt
) {
8321 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
))
8323 ctxt
->context
->node
= (xmlNodePtr
) ctxt
->context
->doc
;
8324 valuePush(ctxt
, xmlXPathCacheNewNodeSet(ctxt
->context
,
8325 ctxt
->context
->node
));
8328 /************************************************************************
8330 * The explicit core function library *
8331 *http://www.w3.org/Style/XSL/Group/1999/07/xpath-19990705.html#corelib *
8333 ************************************************************************/
8337 * xmlXPathLastFunction:
8338 * @ctxt: the XPath Parser context
8339 * @nargs: the number of arguments
8341 * Implement the last() XPath function
8343 * The last function returns the number of nodes in the context node list.
8346 xmlXPathLastFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
8348 if (ctxt
->context
->contextSize
>= 0) {
8350 xmlXPathCacheNewFloat(ctxt
->context
,
8351 (double) ctxt
->context
->contextSize
));
8353 xmlGenericError(xmlGenericErrorContext
,
8354 "last() : %d\n", ctxt
->context
->contextSize
);
8357 XP_ERROR(XPATH_INVALID_CTXT_SIZE
);
8362 * xmlXPathPositionFunction:
8363 * @ctxt: the XPath Parser context
8364 * @nargs: the number of arguments
8366 * Implement the position() XPath function
8368 * The position function returns the position of the context node in the
8369 * context node list. The first position is 1, and so the last position
8370 * will be equal to last().
8373 xmlXPathPositionFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
8375 if (ctxt
->context
->proximityPosition
>= 0) {
8377 xmlXPathCacheNewFloat(ctxt
->context
,
8378 (double) ctxt
->context
->proximityPosition
));
8380 xmlGenericError(xmlGenericErrorContext
, "position() : %d\n",
8381 ctxt
->context
->proximityPosition
);
8384 XP_ERROR(XPATH_INVALID_CTXT_POSITION
);
8389 * xmlXPathCountFunction:
8390 * @ctxt: the XPath Parser context
8391 * @nargs: the number of arguments
8393 * Implement the count() XPath function
8394 * number count(node-set)
8397 xmlXPathCountFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
8398 xmlXPathObjectPtr cur
;
8401 if ((ctxt
->value
== NULL
) ||
8402 ((ctxt
->value
->type
!= XPATH_NODESET
) &&
8403 (ctxt
->value
->type
!= XPATH_XSLT_TREE
)))
8404 XP_ERROR(XPATH_INVALID_TYPE
);
8405 cur
= valuePop(ctxt
);
8407 if ((cur
== NULL
) || (cur
->nodesetval
== NULL
))
8408 valuePush(ctxt
, xmlXPathCacheNewFloat(ctxt
->context
, (double) 0));
8409 else if ((cur
->type
== XPATH_NODESET
) || (cur
->type
== XPATH_XSLT_TREE
)) {
8410 valuePush(ctxt
, xmlXPathCacheNewFloat(ctxt
->context
,
8411 (double) cur
->nodesetval
->nodeNr
));
8413 if ((cur
->nodesetval
->nodeNr
!= 1) ||
8414 (cur
->nodesetval
->nodeTab
== NULL
)) {
8415 valuePush(ctxt
, xmlXPathCacheNewFloat(ctxt
->context
, (double) 0));
8420 tmp
= cur
->nodesetval
->nodeTab
[0];
8422 tmp
= tmp
->children
;
8423 while (tmp
!= NULL
) {
8428 valuePush(ctxt
, xmlXPathCacheNewFloat(ctxt
->context
, (double) i
));
8431 xmlXPathReleaseObject(ctxt
->context
, cur
);
8435 * xmlXPathGetElementsByIds:
8436 * @doc: the document
8437 * @ids: a whitespace separated list of IDs
8439 * Selects elements by their unique ID.
8441 * Returns a node-set of selected elements.
8443 static xmlNodeSetPtr
8444 xmlXPathGetElementsByIds (xmlDocPtr doc
, const xmlChar
*ids
) {
8446 const xmlChar
*cur
= ids
;
8449 xmlNodePtr elem
= NULL
;
8451 if (ids
== NULL
) return(NULL
);
8453 ret
= xmlXPathNodeSetCreate(NULL
);
8457 while (IS_BLANK_CH(*cur
)) cur
++;
8459 while ((!IS_BLANK_CH(*cur
)) && (*cur
!= 0))
8462 ID
= xmlStrndup(ids
, cur
- ids
);
8465 * We used to check the fact that the value passed
8466 * was an NCName, but this generated much troubles for
8467 * me and Aleksey Sanin, people blatantly violated that
8468 * constaint, like Visa3D spec.
8469 * if (xmlValidateNCName(ID, 1) == 0)
8471 attr
= xmlGetID(doc
, ID
);
8473 if (attr
->type
== XML_ATTRIBUTE_NODE
)
8474 elem
= attr
->parent
;
8475 else if (attr
->type
== XML_ELEMENT_NODE
)
8476 elem
= (xmlNodePtr
) attr
;
8480 xmlXPathNodeSetAdd(ret
, elem
);
8485 while (IS_BLANK_CH(*cur
)) cur
++;
8492 * xmlXPathIdFunction:
8493 * @ctxt: the XPath Parser context
8494 * @nargs: the number of arguments
8496 * Implement the id() XPath function
8497 * node-set id(object)
8498 * The id function selects elements by their unique ID
8499 * (see [5.2.1 Unique IDs]). When the argument to id is of type node-set,
8500 * then the result is the union of the result of applying id to the
8501 * string value of each of the nodes in the argument node-set. When the
8502 * argument to id is of any other type, the argument is converted to a
8503 * string as if by a call to the string function; the string is split
8504 * into a whitespace-separated list of tokens (whitespace is any sequence
8505 * of characters matching the production S); the result is a node-set
8506 * containing the elements in the same document as the context node that
8507 * have a unique ID equal to any of the tokens in the list.
8510 xmlXPathIdFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
8513 xmlXPathObjectPtr obj
;
8516 obj
= valuePop(ctxt
);
8517 if (obj
== NULL
) XP_ERROR(XPATH_INVALID_OPERAND
);
8518 if ((obj
->type
== XPATH_NODESET
) || (obj
->type
== XPATH_XSLT_TREE
)) {
8522 ret
= xmlXPathNodeSetCreate(NULL
);
8524 * FIXME -- in an out-of-memory condition this will behave badly.
8525 * The solution is not clear -- we already popped an item from
8526 * ctxt, so the object is in a corrupt state.
8529 if (obj
->nodesetval
!= NULL
) {
8530 for (i
= 0; i
< obj
->nodesetval
->nodeNr
; i
++) {
8532 xmlXPathCastNodeToString(obj
->nodesetval
->nodeTab
[i
]);
8533 ns
= xmlXPathGetElementsByIds(ctxt
->context
->doc
, tokens
);
8534 ret
= xmlXPathNodeSetMerge(ret
, ns
);
8535 xmlXPathFreeNodeSet(ns
);
8540 xmlXPathReleaseObject(ctxt
->context
, obj
);
8541 valuePush(ctxt
, xmlXPathCacheWrapNodeSet(ctxt
->context
, ret
));
8544 obj
= xmlXPathCacheConvertString(ctxt
->context
, obj
);
8545 ret
= xmlXPathGetElementsByIds(ctxt
->context
->doc
, obj
->stringval
);
8546 valuePush(ctxt
, xmlXPathCacheWrapNodeSet(ctxt
->context
, ret
));
8547 xmlXPathReleaseObject(ctxt
->context
, obj
);
8552 * xmlXPathLocalNameFunction:
8553 * @ctxt: the XPath Parser context
8554 * @nargs: the number of arguments
8556 * Implement the local-name() XPath function
8557 * string local-name(node-set?)
8558 * The local-name function returns a string containing the local part
8559 * of the name of the node in the argument node-set that is first in
8560 * document order. If the node-set is empty or the first node has no
8561 * name, an empty string is returned. If the argument is omitted it
8562 * defaults to the context node.
8565 xmlXPathLocalNameFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
8566 xmlXPathObjectPtr cur
;
8568 if (ctxt
== NULL
) return;
8571 valuePush(ctxt
, xmlXPathCacheNewNodeSet(ctxt
->context
,
8572 ctxt
->context
->node
));
8577 if ((ctxt
->value
== NULL
) ||
8578 ((ctxt
->value
->type
!= XPATH_NODESET
) &&
8579 (ctxt
->value
->type
!= XPATH_XSLT_TREE
)))
8580 XP_ERROR(XPATH_INVALID_TYPE
);
8581 cur
= valuePop(ctxt
);
8583 if ((cur
->nodesetval
== NULL
) || (cur
->nodesetval
->nodeNr
== 0)) {
8584 valuePush(ctxt
, xmlXPathCacheNewCString(ctxt
->context
, ""));
8586 int i
= 0; /* Should be first in document order !!!!! */
8587 switch (cur
->nodesetval
->nodeTab
[i
]->type
) {
8588 case XML_ELEMENT_NODE
:
8589 case XML_ATTRIBUTE_NODE
:
8591 if (cur
->nodesetval
->nodeTab
[i
]->name
[0] == ' ')
8592 valuePush(ctxt
, xmlXPathCacheNewCString(ctxt
->context
, ""));
8595 xmlXPathCacheNewString(ctxt
->context
,
8596 cur
->nodesetval
->nodeTab
[i
]->name
));
8598 case XML_NAMESPACE_DECL
:
8599 valuePush(ctxt
, xmlXPathCacheNewString(ctxt
->context
,
8600 ((xmlNsPtr
)cur
->nodesetval
->nodeTab
[i
])->prefix
));
8603 valuePush(ctxt
, xmlXPathCacheNewCString(ctxt
->context
, ""));
8606 xmlXPathReleaseObject(ctxt
->context
, cur
);
8610 * xmlXPathNamespaceURIFunction:
8611 * @ctxt: the XPath Parser context
8612 * @nargs: the number of arguments
8614 * Implement the namespace-uri() XPath function
8615 * string namespace-uri(node-set?)
8616 * The namespace-uri function returns a string containing the
8617 * namespace URI of the expanded name of the node in the argument
8618 * node-set that is first in document order. If the node-set is empty,
8619 * the first node has no name, or the expanded name has no namespace
8620 * URI, an empty string is returned. If the argument is omitted it
8621 * defaults to the context node.
8624 xmlXPathNamespaceURIFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
8625 xmlXPathObjectPtr cur
;
8627 if (ctxt
== NULL
) return;
8630 valuePush(ctxt
, xmlXPathCacheNewNodeSet(ctxt
->context
,
8631 ctxt
->context
->node
));
8635 if ((ctxt
->value
== NULL
) ||
8636 ((ctxt
->value
->type
!= XPATH_NODESET
) &&
8637 (ctxt
->value
->type
!= XPATH_XSLT_TREE
)))
8638 XP_ERROR(XPATH_INVALID_TYPE
);
8639 cur
= valuePop(ctxt
);
8641 if ((cur
->nodesetval
== NULL
) || (cur
->nodesetval
->nodeNr
== 0)) {
8642 valuePush(ctxt
, xmlXPathCacheNewCString(ctxt
->context
, ""));
8644 int i
= 0; /* Should be first in document order !!!!! */
8645 switch (cur
->nodesetval
->nodeTab
[i
]->type
) {
8646 case XML_ELEMENT_NODE
:
8647 case XML_ATTRIBUTE_NODE
:
8648 if (cur
->nodesetval
->nodeTab
[i
]->ns
== NULL
)
8649 valuePush(ctxt
, xmlXPathCacheNewCString(ctxt
->context
, ""));
8651 valuePush(ctxt
, xmlXPathCacheNewString(ctxt
->context
,
8652 cur
->nodesetval
->nodeTab
[i
]->ns
->href
));
8655 valuePush(ctxt
, xmlXPathCacheNewCString(ctxt
->context
, ""));
8658 xmlXPathReleaseObject(ctxt
->context
, cur
);
8662 * xmlXPathNameFunction:
8663 * @ctxt: the XPath Parser context
8664 * @nargs: the number of arguments
8666 * Implement the name() XPath function
8667 * string name(node-set?)
8668 * The name function returns a string containing a QName representing
8669 * the name of the node in the argument node-set that is first in document
8670 * order. The QName must represent the name with respect to the namespace
8671 * declarations in effect on the node whose name is being represented.
8672 * Typically, this will be the form in which the name occurred in the XML
8673 * source. This need not be the case if there are namespace declarations
8674 * in effect on the node that associate multiple prefixes with the same
8675 * namespace. However, an implementation may include information about
8676 * the original prefix in its representation of nodes; in this case, an
8677 * implementation can ensure that the returned string is always the same
8678 * as the QName used in the XML source. If the argument it omitted it
8679 * defaults to the context node.
8680 * Libxml keep the original prefix so the "real qualified name" used is
8684 xmlXPathNameFunction(xmlXPathParserContextPtr ctxt
, int nargs
)
8686 xmlXPathObjectPtr cur
;
8689 valuePush(ctxt
, xmlXPathCacheNewNodeSet(ctxt
->context
,
8690 ctxt
->context
->node
));
8695 if ((ctxt
->value
== NULL
) ||
8696 ((ctxt
->value
->type
!= XPATH_NODESET
) &&
8697 (ctxt
->value
->type
!= XPATH_XSLT_TREE
)))
8698 XP_ERROR(XPATH_INVALID_TYPE
);
8699 cur
= valuePop(ctxt
);
8701 if ((cur
->nodesetval
== NULL
) || (cur
->nodesetval
->nodeNr
== 0)) {
8702 valuePush(ctxt
, xmlXPathCacheNewCString(ctxt
->context
, ""));
8704 int i
= 0; /* Should be first in document order !!!!! */
8706 switch (cur
->nodesetval
->nodeTab
[i
]->type
) {
8707 case XML_ELEMENT_NODE
:
8708 case XML_ATTRIBUTE_NODE
:
8709 if (cur
->nodesetval
->nodeTab
[i
]->name
[0] == ' ')
8711 xmlXPathCacheNewCString(ctxt
->context
, ""));
8712 else if ((cur
->nodesetval
->nodeTab
[i
]->ns
== NULL
) ||
8713 (cur
->nodesetval
->nodeTab
[i
]->ns
->prefix
== NULL
)) {
8715 xmlXPathCacheNewString(ctxt
->context
,
8716 cur
->nodesetval
->nodeTab
[i
]->name
));
8720 fullname
= xmlBuildQName(cur
->nodesetval
->nodeTab
[i
]->name
,
8721 cur
->nodesetval
->nodeTab
[i
]->ns
->prefix
,
8723 if (fullname
== cur
->nodesetval
->nodeTab
[i
]->name
)
8724 fullname
= xmlStrdup(cur
->nodesetval
->nodeTab
[i
]->name
);
8725 if (fullname
== NULL
) {
8726 XP_ERROR(XPATH_MEMORY_ERROR
);
8728 valuePush(ctxt
, xmlXPathCacheWrapString(
8729 ctxt
->context
, fullname
));
8733 valuePush(ctxt
, xmlXPathCacheNewNodeSet(ctxt
->context
,
8734 cur
->nodesetval
->nodeTab
[i
]));
8735 xmlXPathLocalNameFunction(ctxt
, 1);
8738 xmlXPathReleaseObject(ctxt
->context
, cur
);
8743 * xmlXPathStringFunction:
8744 * @ctxt: the XPath Parser context
8745 * @nargs: the number of arguments
8747 * Implement the string() XPath function
8748 * string string(object?)
8749 * The string function converts an object to a string as follows:
8750 * - A node-set is converted to a string by returning the value of
8751 * the node in the node-set that is first in document order.
8752 * If the node-set is empty, an empty string is returned.
8753 * - A number is converted to a string as follows
8754 * + NaN is converted to the string NaN
8755 * + positive zero is converted to the string 0
8756 * + negative zero is converted to the string 0
8757 * + positive infinity is converted to the string Infinity
8758 * + negative infinity is converted to the string -Infinity
8759 * + if the number is an integer, the number is represented in
8760 * decimal form as a Number with no decimal point and no leading
8761 * zeros, preceded by a minus sign (-) if the number is negative
8762 * + otherwise, the number is represented in decimal form as a
8763 * Number including a decimal point with at least one digit
8764 * before the decimal point and at least one digit after the
8765 * decimal point, preceded by a minus sign (-) if the number
8766 * is negative; there must be no leading zeros before the decimal
8767 * point apart possibly from the one required digit immediately
8768 * before the decimal point; beyond the one required digit
8769 * after the decimal point there must be as many, but only as
8770 * many, more digits as are needed to uniquely distinguish the
8771 * number from all other IEEE 754 numeric values.
8772 * - The boolean false value is converted to the string false.
8773 * The boolean true value is converted to the string true.
8775 * If the argument is omitted, it defaults to a node-set with the
8776 * context node as its only member.
8779 xmlXPathStringFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
8780 xmlXPathObjectPtr cur
;
8782 if (ctxt
== NULL
) return;
8785 xmlXPathCacheWrapString(ctxt
->context
,
8786 xmlXPathCastNodeToString(ctxt
->context
->node
)));
8791 cur
= valuePop(ctxt
);
8792 if (cur
== NULL
) XP_ERROR(XPATH_INVALID_OPERAND
);
8793 valuePush(ctxt
, xmlXPathCacheConvertString(ctxt
->context
, cur
));
8797 * xmlXPathStringLengthFunction:
8798 * @ctxt: the XPath Parser context
8799 * @nargs: the number of arguments
8801 * Implement the string-length() XPath function
8802 * number string-length(string?)
8803 * The string-length returns the number of characters in the string
8804 * (see [3.6 Strings]). If the argument is omitted, it defaults to
8805 * the context node converted to a string, in other words the value
8806 * of the context node.
8809 xmlXPathStringLengthFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
8810 xmlXPathObjectPtr cur
;
8813 if ((ctxt
== NULL
) || (ctxt
->context
== NULL
))
8815 if (ctxt
->context
->node
== NULL
) {
8816 valuePush(ctxt
, xmlXPathCacheNewFloat(ctxt
->context
, 0));
8820 content
= xmlXPathCastNodeToString(ctxt
->context
->node
);
8821 valuePush(ctxt
, xmlXPathCacheNewFloat(ctxt
->context
,
8822 xmlUTF8Strlen(content
)));
8829 CHECK_TYPE(XPATH_STRING
);
8830 cur
= valuePop(ctxt
);
8831 valuePush(ctxt
, xmlXPathCacheNewFloat(ctxt
->context
,
8832 xmlUTF8Strlen(cur
->stringval
)));
8833 xmlXPathReleaseObject(ctxt
->context
, cur
);
8837 * xmlXPathConcatFunction:
8838 * @ctxt: the XPath Parser context
8839 * @nargs: the number of arguments
8841 * Implement the concat() XPath function
8842 * string concat(string, string, string*)
8843 * The concat function returns the concatenation of its arguments.
8846 xmlXPathConcatFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
8847 xmlXPathObjectPtr cur
, newobj
;
8850 if (ctxt
== NULL
) return;
8856 cur
= valuePop(ctxt
);
8857 if ((cur
== NULL
) || (cur
->type
!= XPATH_STRING
)) {
8858 xmlXPathReleaseObject(ctxt
->context
, cur
);
8865 newobj
= valuePop(ctxt
);
8866 if ((newobj
== NULL
) || (newobj
->type
!= XPATH_STRING
)) {
8867 xmlXPathReleaseObject(ctxt
->context
, newobj
);
8868 xmlXPathReleaseObject(ctxt
->context
, cur
);
8869 XP_ERROR(XPATH_INVALID_TYPE
);
8871 tmp
= xmlStrcat(newobj
->stringval
, cur
->stringval
);
8872 newobj
->stringval
= cur
->stringval
;
8873 cur
->stringval
= tmp
;
8874 xmlXPathReleaseObject(ctxt
->context
, newobj
);
8877 valuePush(ctxt
, cur
);
8881 * xmlXPathContainsFunction:
8882 * @ctxt: the XPath Parser context
8883 * @nargs: the number of arguments
8885 * Implement the contains() XPath function
8886 * boolean contains(string, string)
8887 * The contains function returns true if the first argument string
8888 * contains the second argument string, and otherwise returns false.
8891 xmlXPathContainsFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
8892 xmlXPathObjectPtr hay
, needle
;
8896 CHECK_TYPE(XPATH_STRING
);
8897 needle
= valuePop(ctxt
);
8899 hay
= valuePop(ctxt
);
8901 if ((hay
== NULL
) || (hay
->type
!= XPATH_STRING
)) {
8902 xmlXPathReleaseObject(ctxt
->context
, hay
);
8903 xmlXPathReleaseObject(ctxt
->context
, needle
);
8904 XP_ERROR(XPATH_INVALID_TYPE
);
8906 if (xmlStrstr(hay
->stringval
, needle
->stringval
))
8907 valuePush(ctxt
, xmlXPathCacheNewBoolean(ctxt
->context
, 1));
8909 valuePush(ctxt
, xmlXPathCacheNewBoolean(ctxt
->context
, 0));
8910 xmlXPathReleaseObject(ctxt
->context
, hay
);
8911 xmlXPathReleaseObject(ctxt
->context
, needle
);
8915 * xmlXPathStartsWithFunction:
8916 * @ctxt: the XPath Parser context
8917 * @nargs: the number of arguments
8919 * Implement the starts-with() XPath function
8920 * boolean starts-with(string, string)
8921 * The starts-with function returns true if the first argument string
8922 * starts with the second argument string, and otherwise returns false.
8925 xmlXPathStartsWithFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
8926 xmlXPathObjectPtr hay
, needle
;
8931 CHECK_TYPE(XPATH_STRING
);
8932 needle
= valuePop(ctxt
);
8934 hay
= valuePop(ctxt
);
8936 if ((hay
== NULL
) || (hay
->type
!= XPATH_STRING
)) {
8937 xmlXPathReleaseObject(ctxt
->context
, hay
);
8938 xmlXPathReleaseObject(ctxt
->context
, needle
);
8939 XP_ERROR(XPATH_INVALID_TYPE
);
8941 n
= xmlStrlen(needle
->stringval
);
8942 if (xmlStrncmp(hay
->stringval
, needle
->stringval
, n
))
8943 valuePush(ctxt
, xmlXPathCacheNewBoolean(ctxt
->context
, 0));
8945 valuePush(ctxt
, xmlXPathCacheNewBoolean(ctxt
->context
, 1));
8946 xmlXPathReleaseObject(ctxt
->context
, hay
);
8947 xmlXPathReleaseObject(ctxt
->context
, needle
);
8951 * xmlXPathSubstringFunction:
8952 * @ctxt: the XPath Parser context
8953 * @nargs: the number of arguments
8955 * Implement the substring() XPath function
8956 * string substring(string, number, number?)
8957 * The substring function returns the substring of the first argument
8958 * starting at the position specified in the second argument with
8959 * length specified in the third argument. For example,
8960 * substring("12345",2,3) returns "234". If the third argument is not
8961 * specified, it returns the substring starting at the position specified
8962 * in the second argument and continuing to the end of the string. For
8963 * example, substring("12345",2) returns "2345". More precisely, each
8964 * character in the string (see [3.6 Strings]) is considered to have a
8965 * numeric position: the position of the first character is 1, the position
8966 * of the second character is 2 and so on. The returned substring contains
8967 * those characters for which the position of the character is greater than
8968 * or equal to the second argument and, if the third argument is specified,
8969 * less than the sum of the second and third arguments; the comparisons
8970 * and addition used for the above follow the standard IEEE 754 rules. Thus:
8971 * - substring("12345", 1.5, 2.6) returns "234"
8972 * - substring("12345", 0, 3) returns "12"
8973 * - substring("12345", 0 div 0, 3) returns ""
8974 * - substring("12345", 1, 0 div 0) returns ""
8975 * - substring("12345", -42, 1 div 0) returns "12345"
8976 * - substring("12345", -1 div 0, 1 div 0) returns ""
8979 xmlXPathSubstringFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
8980 xmlXPathObjectPtr str
, start
, len
;
8992 * take care of possible last (position) argument
8996 CHECK_TYPE(XPATH_NUMBER
);
8997 len
= valuePop(ctxt
);
8999 xmlXPathReleaseObject(ctxt
->context
, len
);
9003 CHECK_TYPE(XPATH_NUMBER
);
9004 start
= valuePop(ctxt
);
9005 in
= start
->floatval
;
9006 xmlXPathReleaseObject(ctxt
->context
, start
);
9008 CHECK_TYPE(XPATH_STRING
);
9009 str
= valuePop(ctxt
);
9010 m
= xmlUTF8Strlen((const unsigned char *)str
->stringval
);
9013 * If last pos not present, calculate last position
9021 /* Need to check for the special cases where either
9022 * the index is NaN, the length is NaN, or both
9023 * arguments are infinity (relying on Inf + -Inf = NaN)
9025 if (!xmlXPathIsInf(in
) && !xmlXPathIsNaN(in
+ le
)) {
9027 * To meet the requirements of the spec, the arguments
9028 * must be converted to integer format before
9029 * initial index calculations are done
9031 * First we go to integer form, rounding up
9032 * and checking for special cases
9035 if (((double)i
)+0.5 <= in
) i
++;
9037 if (xmlXPathIsInf(le
) == 1) {
9042 else if (xmlXPathIsInf(le
) == -1 || le
< 0.0)
9046 if (((double)l
)+0.5 <= le
) l
++;
9049 /* Now we normalize inidices */
9057 /* number of chars to copy */
9060 ret
= xmlUTF8Strsub(str
->stringval
, i
, l
);
9066 valuePush(ctxt
, xmlXPathCacheNewCString(ctxt
->context
, ""));
9068 valuePush(ctxt
, xmlXPathCacheNewString(ctxt
->context
, ret
));
9071 xmlXPathReleaseObject(ctxt
->context
, str
);
9075 * xmlXPathSubstringBeforeFunction:
9076 * @ctxt: the XPath Parser context
9077 * @nargs: the number of arguments
9079 * Implement the substring-before() XPath function
9080 * string substring-before(string, string)
9081 * The substring-before function returns the substring of the first
9082 * argument string that precedes the first occurrence of the second
9083 * argument string in the first argument string, or the empty string
9084 * if the first argument string does not contain the second argument
9085 * string. For example, substring-before("1999/04/01","/") returns 1999.
9088 xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9089 xmlXPathObjectPtr str
;
9090 xmlXPathObjectPtr find
;
9091 xmlBufferPtr target
;
9092 const xmlChar
*point
;
9097 find
= valuePop(ctxt
);
9099 str
= valuePop(ctxt
);
9101 target
= xmlBufferCreate();
9103 point
= xmlStrstr(str
->stringval
, find
->stringval
);
9105 offset
= (int)(point
- str
->stringval
);
9106 xmlBufferAdd(target
, str
->stringval
, offset
);
9108 valuePush(ctxt
, xmlXPathCacheNewString(ctxt
->context
,
9109 xmlBufferContent(target
)));
9110 xmlBufferFree(target
);
9112 xmlXPathReleaseObject(ctxt
->context
, str
);
9113 xmlXPathReleaseObject(ctxt
->context
, find
);
9117 * xmlXPathSubstringAfterFunction:
9118 * @ctxt: the XPath Parser context
9119 * @nargs: the number of arguments
9121 * Implement the substring-after() XPath function
9122 * string substring-after(string, string)
9123 * The substring-after function returns the substring of the first
9124 * argument string that follows the first occurrence of the second
9125 * argument string in the first argument string, or the empty stringi
9126 * if the first argument string does not contain the second argument
9127 * string. For example, substring-after("1999/04/01","/") returns 04/01,
9128 * and substring-after("1999/04/01","19") returns 99/04/01.
9131 xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9132 xmlXPathObjectPtr str
;
9133 xmlXPathObjectPtr find
;
9134 xmlBufferPtr target
;
9135 const xmlChar
*point
;
9140 find
= valuePop(ctxt
);
9142 str
= valuePop(ctxt
);
9144 target
= xmlBufferCreate();
9146 point
= xmlStrstr(str
->stringval
, find
->stringval
);
9148 offset
= (int)(point
- str
->stringval
) + xmlStrlen(find
->stringval
);
9149 xmlBufferAdd(target
, &str
->stringval
[offset
],
9150 xmlStrlen(str
->stringval
) - offset
);
9152 valuePush(ctxt
, xmlXPathCacheNewString(ctxt
->context
,
9153 xmlBufferContent(target
)));
9154 xmlBufferFree(target
);
9156 xmlXPathReleaseObject(ctxt
->context
, str
);
9157 xmlXPathReleaseObject(ctxt
->context
, find
);
9161 * xmlXPathNormalizeFunction:
9162 * @ctxt: the XPath Parser context
9163 * @nargs: the number of arguments
9165 * Implement the normalize-space() XPath function
9166 * string normalize-space(string?)
9167 * The normalize-space function returns the argument string with white
9168 * space normalized by stripping leading and trailing whitespace
9169 * and replacing sequences of whitespace characters by a single
9170 * space. Whitespace characters are the same allowed by the S production
9171 * in XML. If the argument is omitted, it defaults to the context
9172 * node converted to a string, in other words the value of the context node.
9175 xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9176 xmlXPathObjectPtr obj
= NULL
;
9177 xmlChar
*source
= NULL
;
9178 xmlBufferPtr target
;
9181 if (ctxt
== NULL
) return;
9183 /* Use current context node */
9185 xmlXPathCacheWrapString(ctxt
->context
,
9186 xmlXPathCastNodeToString(ctxt
->context
->node
)));
9192 CHECK_TYPE(XPATH_STRING
);
9193 obj
= valuePop(ctxt
);
9194 source
= obj
->stringval
;
9196 target
= xmlBufferCreate();
9197 if (target
&& source
) {
9199 /* Skip leading whitespaces */
9200 while (IS_BLANK_CH(*source
))
9203 /* Collapse intermediate whitespaces, and skip trailing whitespaces */
9206 if (IS_BLANK_CH(*source
)) {
9210 xmlBufferAdd(target
, &blank
, 1);
9213 xmlBufferAdd(target
, source
, 1);
9217 valuePush(ctxt
, xmlXPathCacheNewString(ctxt
->context
,
9218 xmlBufferContent(target
)));
9219 xmlBufferFree(target
);
9221 xmlXPathReleaseObject(ctxt
->context
, obj
);
9225 * xmlXPathTranslateFunction:
9226 * @ctxt: the XPath Parser context
9227 * @nargs: the number of arguments
9229 * Implement the translate() XPath function
9230 * string translate(string, string, string)
9231 * The translate function returns the first argument string with
9232 * occurrences of characters in the second argument string replaced
9233 * by the character at the corresponding position in the third argument
9234 * string. For example, translate("bar","abc","ABC") returns the string
9235 * BAr. If there is a character in the second argument string with no
9236 * character at a corresponding position in the third argument string
9237 * (because the second argument string is longer than the third argument
9238 * string), then occurrences of that character in the first argument
9239 * string are removed. For example, translate("--aaa--","abc-","ABC")
9240 * returns "AAA". If a character occurs more than once in second
9241 * argument string, then the first occurrence determines the replacement
9242 * character. If the third argument string is longer than the second
9243 * argument string, then excess characters are ignored.
9246 xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9247 xmlXPathObjectPtr str
;
9248 xmlXPathObjectPtr from
;
9249 xmlXPathObjectPtr to
;
9250 xmlBufferPtr target
;
9253 const xmlChar
*point
;
9259 to
= valuePop(ctxt
);
9261 from
= valuePop(ctxt
);
9263 str
= valuePop(ctxt
);
9265 target
= xmlBufferCreate();
9267 max
= xmlUTF8Strlen(to
->stringval
);
9268 for (cptr
= str
->stringval
; (ch
=*cptr
); ) {
9269 offset
= xmlUTF8Strloc(from
->stringval
, cptr
);
9272 point
= xmlUTF8Strpos(to
->stringval
, offset
);
9274 xmlBufferAdd(target
, point
, xmlUTF8Strsize(point
, 1));
9277 xmlBufferAdd(target
, cptr
, xmlUTF8Strsize(cptr
, 1));
9279 /* Step to next character in input */
9282 /* if not simple ascii, verify proper format */
9283 if ( (ch
& 0xc0) != 0xc0 ) {
9284 xmlGenericError(xmlGenericErrorContext
,
9285 "xmlXPathTranslateFunction: Invalid UTF8 string\n");
9288 /* then skip over remaining bytes for this char */
9289 while ( (ch
<<= 1) & 0x80 )
9290 if ( (*cptr
++ & 0xc0) != 0x80 ) {
9291 xmlGenericError(xmlGenericErrorContext
,
9292 "xmlXPathTranslateFunction: Invalid UTF8 string\n");
9295 if (ch
& 0x80) /* must have had error encountered */
9300 valuePush(ctxt
, xmlXPathCacheNewString(ctxt
->context
,
9301 xmlBufferContent(target
)));
9302 xmlBufferFree(target
);
9303 xmlXPathReleaseObject(ctxt
->context
, str
);
9304 xmlXPathReleaseObject(ctxt
->context
, from
);
9305 xmlXPathReleaseObject(ctxt
->context
, to
);
9309 * xmlXPathBooleanFunction:
9310 * @ctxt: the XPath Parser context
9311 * @nargs: the number of arguments
9313 * Implement the boolean() XPath function
9314 * boolean boolean(object)
9315 * The boolean function converts its argument to a boolean as follows:
9316 * - a number is true if and only if it is neither positive or
9317 * negative zero nor NaN
9318 * - a node-set is true if and only if it is non-empty
9319 * - a string is true if and only if its length is non-zero
9322 xmlXPathBooleanFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9323 xmlXPathObjectPtr cur
;
9326 cur
= valuePop(ctxt
);
9327 if (cur
== NULL
) XP_ERROR(XPATH_INVALID_OPERAND
);
9328 cur
= xmlXPathCacheConvertBoolean(ctxt
->context
, cur
);
9329 valuePush(ctxt
, cur
);
9333 * xmlXPathNotFunction:
9334 * @ctxt: the XPath Parser context
9335 * @nargs: the number of arguments
9337 * Implement the not() XPath function
9338 * boolean not(boolean)
9339 * The not function returns true if its argument is false,
9340 * and false otherwise.
9343 xmlXPathNotFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9346 CHECK_TYPE(XPATH_BOOLEAN
);
9347 ctxt
->value
->boolval
= ! ctxt
->value
->boolval
;
9351 * xmlXPathTrueFunction:
9352 * @ctxt: the XPath Parser context
9353 * @nargs: the number of arguments
9355 * Implement the true() XPath function
9359 xmlXPathTrueFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9361 valuePush(ctxt
, xmlXPathCacheNewBoolean(ctxt
->context
, 1));
9365 * xmlXPathFalseFunction:
9366 * @ctxt: the XPath Parser context
9367 * @nargs: the number of arguments
9369 * Implement the false() XPath function
9373 xmlXPathFalseFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9375 valuePush(ctxt
, xmlXPathCacheNewBoolean(ctxt
->context
, 0));
9379 * xmlXPathLangFunction:
9380 * @ctxt: the XPath Parser context
9381 * @nargs: the number of arguments
9383 * Implement the lang() XPath function
9384 * boolean lang(string)
9385 * The lang function returns true or false depending on whether the
9386 * language of the context node as specified by xml:lang attributes
9387 * is the same as or is a sublanguage of the language specified by
9388 * the argument string. The language of the context node is determined
9389 * by the value of the xml:lang attribute on the context node, or, if
9390 * the context node has no xml:lang attribute, by the value of the
9391 * xml:lang attribute on the nearest ancestor of the context node that
9392 * has an xml:lang attribute. If there is no such attribute, then lang
9393 * returns false. If there is such an attribute, then lang returns
9394 * true if the attribute value is equal to the argument ignoring case,
9395 * or if there is some suffix starting with - such that the attribute
9396 * value is equal to the argument ignoring that suffix of the attribute
9397 * value and ignoring case.
9400 xmlXPathLangFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9401 xmlXPathObjectPtr val
= NULL
;
9402 const xmlChar
*theLang
= NULL
;
9403 const xmlChar
*lang
;
9409 CHECK_TYPE(XPATH_STRING
);
9410 val
= valuePop(ctxt
);
9411 lang
= val
->stringval
;
9412 theLang
= xmlNodeGetLang(ctxt
->context
->node
);
9413 if ((theLang
!= NULL
) && (lang
!= NULL
)) {
9414 for (i
= 0;lang
[i
] != 0;i
++)
9415 if (toupper(lang
[i
]) != toupper(theLang
[i
]))
9417 if ((theLang
[i
] == 0) || (theLang
[i
] == '-'))
9421 if (theLang
!= NULL
)
9422 xmlFree((void *)theLang
);
9424 xmlXPathReleaseObject(ctxt
->context
, val
);
9425 valuePush(ctxt
, xmlXPathCacheNewBoolean(ctxt
->context
, ret
));
9429 * xmlXPathNumberFunction:
9430 * @ctxt: the XPath Parser context
9431 * @nargs: the number of arguments
9433 * Implement the number() XPath function
9434 * number number(object?)
9437 xmlXPathNumberFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9438 xmlXPathObjectPtr cur
;
9441 if (ctxt
== NULL
) return;
9443 if (ctxt
->context
->node
== NULL
) {
9444 valuePush(ctxt
, xmlXPathCacheNewFloat(ctxt
->context
, 0.0));
9446 xmlChar
* content
= xmlNodeGetContent(ctxt
->context
->node
);
9448 res
= xmlXPathStringEvalNumber(content
);
9449 valuePush(ctxt
, xmlXPathCacheNewFloat(ctxt
->context
, res
));
9456 cur
= valuePop(ctxt
);
9457 valuePush(ctxt
, xmlXPathCacheConvertNumber(ctxt
->context
, cur
));
9461 * xmlXPathSumFunction:
9462 * @ctxt: the XPath Parser context
9463 * @nargs: the number of arguments
9465 * Implement the sum() XPath function
9466 * number sum(node-set)
9467 * The sum function returns the sum of the values of the nodes in
9468 * the argument node-set.
9471 xmlXPathSumFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9472 xmlXPathObjectPtr cur
;
9477 if ((ctxt
->value
== NULL
) ||
9478 ((ctxt
->value
->type
!= XPATH_NODESET
) &&
9479 (ctxt
->value
->type
!= XPATH_XSLT_TREE
)))
9480 XP_ERROR(XPATH_INVALID_TYPE
);
9481 cur
= valuePop(ctxt
);
9483 if ((cur
->nodesetval
!= NULL
) && (cur
->nodesetval
->nodeNr
!= 0)) {
9484 for (i
= 0; i
< cur
->nodesetval
->nodeNr
; i
++) {
9485 res
+= xmlXPathCastNodeToNumber(cur
->nodesetval
->nodeTab
[i
]);
9488 valuePush(ctxt
, xmlXPathCacheNewFloat(ctxt
->context
, res
));
9489 xmlXPathReleaseObject(ctxt
->context
, cur
);
9493 * To assure working code on multiple platforms, we want to only depend
9494 * upon the characteristic truncation of converting a floating point value
9495 * to an integer. Unfortunately, because of the different storage sizes
9496 * of our internal floating point value (double) and integer (int), we
9497 * can't directly convert (see bug 301162). This macro is a messy
9500 #define XTRUNC(f, v) \
9501 f = fmod((v), INT_MAX); \
9502 f = (v) - (f) + (double)((int)(f));
9505 * xmlXPathFloorFunction:
9506 * @ctxt: the XPath Parser context
9507 * @nargs: the number of arguments
9509 * Implement the floor() XPath function
9510 * number floor(number)
9511 * The floor function returns the largest (closest to positive infinity)
9512 * number that is not greater than the argument and that is an integer.
9515 xmlXPathFloorFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9520 CHECK_TYPE(XPATH_NUMBER
);
9522 XTRUNC(f
, ctxt
->value
->floatval
);
9523 if (f
!= ctxt
->value
->floatval
) {
9524 if (ctxt
->value
->floatval
> 0)
9525 ctxt
->value
->floatval
= f
;
9527 ctxt
->value
->floatval
= f
- 1;
9532 * xmlXPathCeilingFunction:
9533 * @ctxt: the XPath Parser context
9534 * @nargs: the number of arguments
9536 * Implement the ceiling() XPath function
9537 * number ceiling(number)
9538 * The ceiling function returns the smallest (closest to negative infinity)
9539 * number that is not less than the argument and that is an integer.
9542 xmlXPathCeilingFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9547 CHECK_TYPE(XPATH_NUMBER
);
9550 ctxt
->value
->floatval
= ceil(ctxt
->value
->floatval
);
9552 XTRUNC(f
, ctxt
->value
->floatval
);
9553 if (f
!= ctxt
->value
->floatval
) {
9554 if (ctxt
->value
->floatval
> 0)
9555 ctxt
->value
->floatval
= f
+ 1;
9557 if (ctxt
->value
->floatval
< 0 && f
== 0)
9558 ctxt
->value
->floatval
= xmlXPathNZERO
;
9560 ctxt
->value
->floatval
= f
;
9568 * xmlXPathRoundFunction:
9569 * @ctxt: the XPath Parser context
9570 * @nargs: the number of arguments
9572 * Implement the round() XPath function
9573 * number round(number)
9574 * The round function returns the number that is closest to the
9575 * argument and that is an integer. If there are two such numbers,
9576 * then the one that is even is returned.
9579 xmlXPathRoundFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
9584 CHECK_TYPE(XPATH_NUMBER
);
9586 if ((xmlXPathIsNaN(ctxt
->value
->floatval
)) ||
9587 (xmlXPathIsInf(ctxt
->value
->floatval
) == 1) ||
9588 (xmlXPathIsInf(ctxt
->value
->floatval
) == -1) ||
9589 (ctxt
->value
->floatval
== 0.0))
9592 XTRUNC(f
, ctxt
->value
->floatval
);
9593 if (ctxt
->value
->floatval
< 0) {
9594 if (ctxt
->value
->floatval
< f
- 0.5)
9595 ctxt
->value
->floatval
= f
- 1;
9597 ctxt
->value
->floatval
= f
;
9598 if (ctxt
->value
->floatval
== 0)
9599 ctxt
->value
->floatval
= xmlXPathNZERO
;
9601 if (ctxt
->value
->floatval
< f
+ 0.5)
9602 ctxt
->value
->floatval
= f
;
9604 ctxt
->value
->floatval
= f
+ 1;
9608 /************************************************************************
9612 ************************************************************************/
9615 * a few forward declarations since we use a recursive call based
9618 static void xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt
, int sort
);
9619 static void xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt
, int filter
);
9620 static void xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt
);
9621 static void xmlXPathCompRelativeLocationPath(xmlXPathParserContextPtr ctxt
);
9622 static xmlChar
* xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt
,
9626 * xmlXPathCurrentChar:
9627 * @ctxt: the XPath parser context
9628 * @cur: pointer to the beginning of the char
9629 * @len: pointer to the length of the char read
9631 * The current char value, if using UTF-8 this may actually span multiple
9632 * bytes in the input buffer.
9634 * Returns the current char value and its length
9638 xmlXPathCurrentChar(xmlXPathParserContextPtr ctxt
, int *len
) {
9648 * We are supposed to handle UTF8, check it's valid
9649 * From rfc2044: encoding of the Unicode values on UTF-8:
9651 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
9652 * 0000 0000-0000 007F 0xxxxxxx
9653 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
9654 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
9656 * Check for the 0x110000 limit too
9660 if ((cur
[1] & 0xc0) != 0x80)
9661 goto encoding_error
;
9662 if ((c
& 0xe0) == 0xe0) {
9664 if ((cur
[2] & 0xc0) != 0x80)
9665 goto encoding_error
;
9666 if ((c
& 0xf0) == 0xf0) {
9667 if (((c
& 0xf8) != 0xf0) ||
9668 ((cur
[3] & 0xc0) != 0x80))
9669 goto encoding_error
;
9672 val
= (cur
[0] & 0x7) << 18;
9673 val
|= (cur
[1] & 0x3f) << 12;
9674 val
|= (cur
[2] & 0x3f) << 6;
9675 val
|= cur
[3] & 0x3f;
9679 val
= (cur
[0] & 0xf) << 12;
9680 val
|= (cur
[1] & 0x3f) << 6;
9681 val
|= cur
[2] & 0x3f;
9686 val
= (cur
[0] & 0x1f) << 6;
9687 val
|= cur
[1] & 0x3f;
9689 if (!IS_CHAR(val
)) {
9690 XP_ERROR0(XPATH_INVALID_CHAR_ERROR
);
9700 * If we detect an UTF8 error that probably means that the
9701 * input encoding didn't get properly advertised in the
9702 * declaration header. Report the error and switch the encoding
9703 * to ISO-Latin-1 (if you don't like this policy, just declare the
9707 XP_ERROR0(XPATH_ENCODING_ERROR
);
9711 * xmlXPathParseNCName:
9712 * @ctxt: the XPath Parser context
9714 * parse an XML namespace non qualified name.
9716 * [NS 3] NCName ::= (Letter | '_') (NCNameChar)*
9718 * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
9719 * CombiningChar | Extender
9721 * Returns the namespace name or NULL
9725 xmlXPathParseNCName(xmlXPathParserContextPtr ctxt
) {
9730 if ((ctxt
== NULL
) || (ctxt
->cur
== NULL
)) return(NULL
);
9732 * Accelerator for simple ASCII names
9735 if (((*in
>= 0x61) && (*in
<= 0x7A)) ||
9736 ((*in
>= 0x41) && (*in
<= 0x5A)) ||
9739 while (((*in
>= 0x61) && (*in
<= 0x7A)) ||
9740 ((*in
>= 0x41) && (*in
<= 0x5A)) ||
9741 ((*in
>= 0x30) && (*in
<= 0x39)) ||
9742 (*in
== '_') || (*in
== '.') ||
9745 if ((*in
== ' ') || (*in
== '>') || (*in
== '/') ||
9746 (*in
== '[') || (*in
== ']') || (*in
== ':') ||
9747 (*in
== '@') || (*in
== '*')) {
9748 count
= in
- ctxt
->cur
;
9751 ret
= xmlStrndup(ctxt
->cur
, count
);
9756 return(xmlXPathParseNameComplex(ctxt
, 0));
9761 * xmlXPathParseQName:
9762 * @ctxt: the XPath Parser context
9763 * @prefix: a xmlChar **
9765 * parse an XML qualified name
9767 * [NS 5] QName ::= (Prefix ':')? LocalPart
9769 * [NS 6] Prefix ::= NCName
9771 * [NS 7] LocalPart ::= NCName
9773 * Returns the function returns the local part, and prefix is updated
9774 * to get the Prefix if any.
9778 xmlXPathParseQName(xmlXPathParserContextPtr ctxt
, xmlChar
**prefix
) {
9779 xmlChar
*ret
= NULL
;
9782 ret
= xmlXPathParseNCName(ctxt
);
9783 if (ret
&& CUR
== ':') {
9786 ret
= xmlXPathParseNCName(ctxt
);
9792 * xmlXPathParseName:
9793 * @ctxt: the XPath Parser context
9797 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
9798 * CombiningChar | Extender
9800 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
9802 * Returns the namespace name or NULL
9806 xmlXPathParseName(xmlXPathParserContextPtr ctxt
) {
9811 if ((ctxt
== NULL
) || (ctxt
->cur
== NULL
)) return(NULL
);
9813 * Accelerator for simple ASCII names
9816 if (((*in
>= 0x61) && (*in
<= 0x7A)) ||
9817 ((*in
>= 0x41) && (*in
<= 0x5A)) ||
9818 (*in
== '_') || (*in
== ':')) {
9820 while (((*in
>= 0x61) && (*in
<= 0x7A)) ||
9821 ((*in
>= 0x41) && (*in
<= 0x5A)) ||
9822 ((*in
>= 0x30) && (*in
<= 0x39)) ||
9823 (*in
== '_') || (*in
== '-') ||
9824 (*in
== ':') || (*in
== '.'))
9826 if ((*in
> 0) && (*in
< 0x80)) {
9827 count
= in
- ctxt
->cur
;
9828 ret
= xmlStrndup(ctxt
->cur
, count
);
9833 return(xmlXPathParseNameComplex(ctxt
, 1));
9837 xmlXPathParseNameComplex(xmlXPathParserContextPtr ctxt
, int qualified
) {
9838 xmlChar buf
[XML_MAX_NAMELEN
+ 5];
9843 * Handler for more complex cases
9846 if ((c
== ' ') || (c
== '>') || (c
== '/') || /* accelerators */
9847 (c
== '[') || (c
== ']') || (c
== '@') || /* accelerators */
9848 (c
== '*') || /* accelerators */
9849 (!IS_LETTER(c
) && (c
!= '_') &&
9850 ((qualified
) && (c
!= ':')))) {
9854 while ((c
!= ' ') && (c
!= '>') && (c
!= '/') && /* test bigname.xml */
9855 ((IS_LETTER(c
)) || (IS_DIGIT(c
)) ||
9856 (c
== '.') || (c
== '-') ||
9857 (c
== '_') || ((qualified
) && (c
== ':')) ||
9858 (IS_COMBINING(c
)) ||
9859 (IS_EXTENDER(c
)))) {
9860 COPY_BUF(l
,buf
,len
,c
);
9863 if (len
>= XML_MAX_NAMELEN
) {
9865 * Okay someone managed to make a huge name, so he's ready to pay
9866 * for the processing speed.
9871 buffer
= (xmlChar
*) xmlMallocAtomic(max
* sizeof(xmlChar
));
9872 if (buffer
== NULL
) {
9873 XP_ERRORNULL(XPATH_MEMORY_ERROR
);
9875 memcpy(buffer
, buf
, len
);
9876 while ((IS_LETTER(c
)) || (IS_DIGIT(c
)) || /* test bigname.xml */
9877 (c
== '.') || (c
== '-') ||
9878 (c
== '_') || ((qualified
) && (c
== ':')) ||
9879 (IS_COMBINING(c
)) ||
9881 if (len
+ 10 > max
) {
9883 buffer
= (xmlChar
*) xmlRealloc(buffer
,
9884 max
* sizeof(xmlChar
));
9885 if (buffer
== NULL
) {
9886 XP_ERRORNULL(XPATH_MEMORY_ERROR
);
9889 COPY_BUF(l
,buffer
,len
,c
);
9899 return(xmlStrndup(buf
, len
));
9905 * These are used as divisors for the fractional part of a number.
9906 * Since the table includes 1.0 (representing '0' fractional digits),
9907 * it must be dimensioned at MAX_FRAC+1 (bug 133921)
9909 static double my_pow10
[MAX_FRAC
+1] = {
9910 1.0, 10.0, 100.0, 1000.0, 10000.0,
9911 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0,
9912 10000000000.0, 100000000000.0, 1000000000000.0, 10000000000000.0,
9914 1000000000000000.0, 10000000000000000.0, 100000000000000000.0,
9915 1000000000000000000.0, 10000000000000000000.0, 100000000000000000000.0
9919 * xmlXPathStringEvalNumber:
9920 * @str: A string to scan
9922 * [30a] Float ::= Number ('e' Digits?)?
9924 * [30] Number ::= Digits ('.' Digits?)?
9926 * [31] Digits ::= [0-9]+
9928 * Compile a Number in the string
9929 * In complement of the Number expression, this function also handles
9930 * negative values : '-' Number.
9932 * Returns the double value.
9935 xmlXPathStringEvalNumber(const xmlChar
*str
) {
9936 const xmlChar
*cur
= str
;
9941 int is_exponent_negative
= 0;
9943 unsigned long tmp
= 0;
9946 if (cur
== NULL
) return(0);
9947 while (IS_BLANK_CH(*cur
)) cur
++;
9948 if ((*cur
!= '.') && ((*cur
< '0') || (*cur
> '9')) && (*cur
!= '-')) {
9949 return(xmlXPathNAN
);
9958 * tmp/temp is a workaround against a gcc compiler bug
9959 * http://veillard.com/gcc.bug
9962 while ((*cur
>= '0') && (*cur
<= '9')) {
9967 temp
= (double) tmp
;
9972 while ((*cur
>= '0') && (*cur
<= '9')) {
9973 ret
= ret
* 10 + (*cur
- '0');
9981 double fraction
= 0;
9984 if (((*cur
< '0') || (*cur
> '9')) && (!ok
)) {
9985 return(xmlXPathNAN
);
9987 while (((*cur
>= '0') && (*cur
<= '9')) && (frac
< MAX_FRAC
)) {
9989 fraction
= fraction
* 10 + v
;
9993 fraction
/= my_pow10
[frac
];
9994 ret
= ret
+ fraction
;
9995 while ((*cur
>= '0') && (*cur
<= '9'))
9998 if ((*cur
== 'e') || (*cur
== 'E')) {
10001 is_exponent_negative
= 1;
10003 } else if (*cur
== '+') {
10006 while ((*cur
>= '0') && (*cur
<= '9')) {
10007 exponent
= exponent
* 10 + (*cur
- '0');
10011 while (IS_BLANK_CH(*cur
)) cur
++;
10012 if (*cur
!= 0) return(xmlXPathNAN
);
10013 if (isneg
) ret
= -ret
;
10014 if (is_exponent_negative
) exponent
= -exponent
;
10015 ret
*= pow(10.0, (double)exponent
);
10020 * xmlXPathCompNumber:
10021 * @ctxt: the XPath Parser context
10023 * [30] Number ::= Digits ('.' Digits?)?
10025 * [31] Digits ::= [0-9]+
10027 * Compile a Number, then push it on the stack
10031 xmlXPathCompNumber(xmlXPathParserContextPtr ctxt
)
10037 int is_exponent_negative
= 0;
10039 unsigned long tmp
= 0;
10044 if ((CUR
!= '.') && ((CUR
< '0') || (CUR
> '9'))) {
10045 XP_ERROR(XPATH_NUMBER_ERROR
);
10049 * tmp/temp is a workaround against a gcc compiler bug
10050 * http://veillard.com/gcc.bug
10053 while ((CUR
>= '0') && (CUR
<= '9')) {
10058 temp
= (double) tmp
;
10063 while ((CUR
>= '0') && (CUR
<= '9')) {
10064 ret
= ret
* 10 + (CUR
- '0');
10071 if (((CUR
< '0') || (CUR
> '9')) && (!ok
)) {
10072 XP_ERROR(XPATH_NUMBER_ERROR
);
10074 while ((CUR
>= '0') && (CUR
<= '9')) {
10076 ret
= ret
+ (CUR
- '0') * mult
;
10080 if ((CUR
== 'e') || (CUR
== 'E')) {
10083 is_exponent_negative
= 1;
10085 } else if (CUR
== '+') {
10088 while ((CUR
>= '0') && (CUR
<= '9')) {
10089 exponent
= exponent
* 10 + (CUR
- '0');
10092 if (is_exponent_negative
)
10093 exponent
= -exponent
;
10094 ret
*= pow(10.0, (double) exponent
);
10096 PUSH_LONG_EXPR(XPATH_OP_VALUE
, XPATH_NUMBER
, 0, 0,
10097 xmlXPathCacheNewFloat(ctxt
->context
, ret
), NULL
);
10101 * xmlXPathParseLiteral:
10102 * @ctxt: the XPath Parser context
10106 * [29] Literal ::= '"' [^"]* '"'
10109 * Returns the value found or NULL in case of error
10112 xmlXPathParseLiteral(xmlXPathParserContextPtr ctxt
) {
10114 xmlChar
*ret
= NULL
;
10119 while ((IS_CHAR_CH(CUR
)) && (CUR
!= '"'))
10121 if (!IS_CHAR_CH(CUR
)) {
10122 XP_ERRORNULL(XPATH_UNFINISHED_LITERAL_ERROR
);
10124 ret
= xmlStrndup(q
, CUR_PTR
- q
);
10127 } else if (CUR
== '\'') {
10130 while ((IS_CHAR_CH(CUR
)) && (CUR
!= '\''))
10132 if (!IS_CHAR_CH(CUR
)) {
10133 XP_ERRORNULL(XPATH_UNFINISHED_LITERAL_ERROR
);
10135 ret
= xmlStrndup(q
, CUR_PTR
- q
);
10139 XP_ERRORNULL(XPATH_START_LITERAL_ERROR
);
10145 * xmlXPathCompLiteral:
10146 * @ctxt: the XPath Parser context
10148 * Parse a Literal and push it on the stack.
10150 * [29] Literal ::= '"' [^"]* '"'
10153 * TODO: xmlXPathCompLiteral memory allocation could be improved.
10156 xmlXPathCompLiteral(xmlXPathParserContextPtr ctxt
) {
10158 xmlChar
*ret
= NULL
;
10163 while ((IS_CHAR_CH(CUR
)) && (CUR
!= '"'))
10165 if (!IS_CHAR_CH(CUR
)) {
10166 XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR
);
10168 ret
= xmlStrndup(q
, CUR_PTR
- q
);
10171 } else if (CUR
== '\'') {
10174 while ((IS_CHAR_CH(CUR
)) && (CUR
!= '\''))
10176 if (!IS_CHAR_CH(CUR
)) {
10177 XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR
);
10179 ret
= xmlStrndup(q
, CUR_PTR
- q
);
10183 XP_ERROR(XPATH_START_LITERAL_ERROR
);
10185 if (ret
== NULL
) return;
10186 PUSH_LONG_EXPR(XPATH_OP_VALUE
, XPATH_STRING
, 0, 0,
10187 xmlXPathCacheNewString(ctxt
->context
, ret
), NULL
);
10192 * xmlXPathCompVariableReference:
10193 * @ctxt: the XPath Parser context
10195 * Parse a VariableReference, evaluate it and push it on the stack.
10197 * The variable bindings consist of a mapping from variable names
10198 * to variable values. The value of a variable is an object, which can be
10199 * of any of the types that are possible for the value of an expression,
10200 * and may also be of additional types not specified here.
10202 * Early evaluation is possible since:
10203 * The variable bindings [...] used to evaluate a subexpression are
10204 * always the same as those used to evaluate the containing expression.
10206 * [36] VariableReference ::= '$' QName
10209 xmlXPathCompVariableReference(xmlXPathParserContextPtr ctxt
) {
10215 XP_ERROR(XPATH_VARIABLE_REF_ERROR
);
10218 name
= xmlXPathParseQName(ctxt
, &prefix
);
10219 if (name
== NULL
) {
10220 XP_ERROR(XPATH_VARIABLE_REF_ERROR
);
10222 ctxt
->comp
->last
= -1;
10223 PUSH_LONG_EXPR(XPATH_OP_VARIABLE
, 0, 0, 0,
10226 if ((ctxt
->context
!= NULL
) && (ctxt
->context
->flags
& XML_XPATH_NOVAR
)) {
10227 XP_ERROR(XPATH_UNDEF_VARIABLE_ERROR
);
10232 * xmlXPathIsNodeType:
10233 * @name: a name string
10235 * Is the name given a NodeType one.
10237 * [38] NodeType ::= 'comment'
10239 * | 'processing-instruction'
10242 * Returns 1 if true 0 otherwise
10245 xmlXPathIsNodeType(const xmlChar
*name
) {
10249 if (xmlStrEqual(name
, BAD_CAST
"node"))
10251 if (xmlStrEqual(name
, BAD_CAST
"text"))
10253 if (xmlStrEqual(name
, BAD_CAST
"comment"))
10255 if (xmlStrEqual(name
, BAD_CAST
"processing-instruction"))
10261 * xmlXPathCompFunctionCall:
10262 * @ctxt: the XPath Parser context
10264 * [16] FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument)*)? ')'
10265 * [17] Argument ::= Expr
10267 * Compile a function call, the evaluation of all arguments are
10268 * pushed on the stack
10271 xmlXPathCompFunctionCall(xmlXPathParserContextPtr ctxt
) {
10277 name
= xmlXPathParseQName(ctxt
, &prefix
);
10278 if (name
== NULL
) {
10280 XP_ERROR(XPATH_EXPR_ERROR
);
10284 if (prefix
== NULL
)
10285 xmlGenericError(xmlGenericErrorContext
, "Calling function %s\n",
10288 xmlGenericError(xmlGenericErrorContext
, "Calling function %s:%s\n",
10293 XP_ERROR(XPATH_EXPR_ERROR
);
10299 * Optimization for count(): we don't need the node-set to be sorted.
10301 if ((prefix
== NULL
) && (name
[0] == 'c') &&
10302 xmlStrEqual(name
, BAD_CAST
"count"))
10306 ctxt
->comp
->last
= -1;
10309 int op1
= ctxt
->comp
->last
;
10310 ctxt
->comp
->last
= -1;
10311 xmlXPathCompileExpr(ctxt
, sort
);
10312 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
10317 PUSH_BINARY_EXPR(XPATH_OP_ARG
, op1
, ctxt
->comp
->last
, 0, 0);
10319 if (CUR
== ')') break;
10321 XP_ERROR(XPATH_EXPR_ERROR
);
10327 PUSH_LONG_EXPR(XPATH_OP_FUNCTION
, nbargs
, 0, 0,
10334 * xmlXPathCompPrimaryExpr:
10335 * @ctxt: the XPath Parser context
10337 * [15] PrimaryExpr ::= VariableReference
10343 * Compile a primary expression.
10346 xmlXPathCompPrimaryExpr(xmlXPathParserContextPtr ctxt
) {
10348 if (CUR
== '$') xmlXPathCompVariableReference(ctxt
);
10349 else if (CUR
== '(') {
10352 xmlXPathCompileExpr(ctxt
, 1);
10355 XP_ERROR(XPATH_EXPR_ERROR
);
10359 } else if (IS_ASCII_DIGIT(CUR
) || (CUR
== '.' && IS_ASCII_DIGIT(NXT(1)))) {
10360 xmlXPathCompNumber(ctxt
);
10361 } else if ((CUR
== '\'') || (CUR
== '"')) {
10362 xmlXPathCompLiteral(ctxt
);
10364 xmlXPathCompFunctionCall(ctxt
);
10370 * xmlXPathCompFilterExpr:
10371 * @ctxt: the XPath Parser context
10373 * [20] FilterExpr ::= PrimaryExpr
10374 * | FilterExpr Predicate
10376 * Compile a filter expression.
10377 * Square brackets are used to filter expressions in the same way that
10378 * they are used in location paths. It is an error if the expression to
10379 * be filtered does not evaluate to a node-set. The context node list
10380 * used for evaluating the expression in square brackets is the node-set
10381 * to be filtered listed in document order.
10385 xmlXPathCompFilterExpr(xmlXPathParserContextPtr ctxt
) {
10386 xmlXPathCompPrimaryExpr(ctxt
);
10390 while (CUR
== '[') {
10391 xmlXPathCompPredicate(ctxt
, 1);
10399 * xmlXPathScanName:
10400 * @ctxt: the XPath Parser context
10402 * Trickery: parse an XML name but without consuming the input flow
10403 * Needed to avoid insanity in the parser state.
10405 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
10406 * CombiningChar | Extender
10408 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
10410 * [6] Names ::= Name (S Name)*
10412 * Returns the Name parsed or NULL
10416 xmlXPathScanName(xmlXPathParserContextPtr ctxt
) {
10419 const xmlChar
*cur
;
10425 if ((c
== ' ') || (c
== '>') || (c
== '/') || /* accelerators */
10426 (!IS_LETTER(c
) && (c
!= '_') &&
10431 while ((c
!= ' ') && (c
!= '>') && (c
!= '/') && /* test bigname.xml */
10432 ((IS_LETTER(c
)) || (IS_DIGIT(c
)) ||
10433 (c
== '.') || (c
== '-') ||
10434 (c
== '_') || (c
== ':') ||
10435 (IS_COMBINING(c
)) ||
10436 (IS_EXTENDER(c
)))) {
10441 ret
= xmlStrndup(cur
, ctxt
->cur
- cur
);
10447 * xmlXPathCompPathExpr:
10448 * @ctxt: the XPath Parser context
10450 * [19] PathExpr ::= LocationPath
10452 * | FilterExpr '/' RelativeLocationPath
10453 * | FilterExpr '//' RelativeLocationPath
10455 * Compile a path expression.
10456 * The / operator and // operators combine an arbitrary expression
10457 * and a relative location path. It is an error if the expression
10458 * does not evaluate to a node-set.
10459 * The / operator does composition in the same way as when / is
10460 * used in a location path. As in location paths, // is short for
10461 * /descendant-or-self::node()/.
10465 xmlXPathCompPathExpr(xmlXPathParserContextPtr ctxt
) {
10466 int lc
= 1; /* Should we branch to LocationPath ? */
10467 xmlChar
*name
= NULL
; /* we may have to preparse a name to find out */
10470 if ((CUR
== '$') || (CUR
== '(') ||
10471 (IS_ASCII_DIGIT(CUR
)) ||
10472 (CUR
== '\'') || (CUR
== '"') ||
10473 (CUR
== '.' && IS_ASCII_DIGIT(NXT(1)))) {
10475 } else if (CUR
== '*') {
10476 /* relative or absolute location path */
10478 } else if (CUR
== '/') {
10479 /* relative or absolute location path */
10481 } else if (CUR
== '@') {
10482 /* relative abbreviated attribute location path */
10484 } else if (CUR
== '.') {
10485 /* relative abbreviated attribute location path */
10489 * Problem is finding if we have a name here whether it's:
10491 * - a function call in which case it's followed by '('
10492 * - an axis in which case it's followed by ':'
10494 * We do an a priori analysis here rather than having to
10495 * maintain parsed token content through the recursive function
10496 * calls. This looks uglier but makes the code easier to
10497 * read/write/debug.
10500 name
= xmlXPathScanName(ctxt
);
10501 if ((name
!= NULL
) && (xmlStrstr(name
, (xmlChar
*) "::") != NULL
)) {
10503 xmlGenericError(xmlGenericErrorContext
,
10504 "PathExpr: Axis\n");
10508 } else if (name
!= NULL
) {
10509 int len
=xmlStrlen(name
);
10512 while (NXT(len
) != 0) {
10513 if (NXT(len
) == '/') {
10516 xmlGenericError(xmlGenericErrorContext
,
10517 "PathExpr: AbbrRelLocation\n");
10521 } else if (IS_BLANK_CH(NXT(len
))) {
10522 /* ignore blanks */
10524 } else if (NXT(len
) == ':') {
10526 xmlGenericError(xmlGenericErrorContext
,
10527 "PathExpr: AbbrRelLocation\n");
10531 } else if ((NXT(len
) == '(')) {
10532 /* Note Type or Function */
10533 if (xmlXPathIsNodeType(name
)) {
10535 xmlGenericError(xmlGenericErrorContext
,
10536 "PathExpr: Type search\n");
10541 xmlGenericError(xmlGenericErrorContext
,
10542 "PathExpr: function call\n");
10547 } else if ((NXT(len
) == '[')) {
10550 xmlGenericError(xmlGenericErrorContext
,
10551 "PathExpr: AbbrRelLocation\n");
10555 } else if ((NXT(len
) == '<') || (NXT(len
) == '>') ||
10556 (NXT(len
) == '=')) {
10565 if (NXT(len
) == 0) {
10567 xmlGenericError(xmlGenericErrorContext
,
10568 "PathExpr: AbbrRelLocation\n");
10575 /* make sure all cases are covered explicitly */
10576 XP_ERROR(XPATH_EXPR_ERROR
);
10582 PUSH_LEAVE_EXPR(XPATH_OP_ROOT
, 0, 0);
10584 PUSH_LEAVE_EXPR(XPATH_OP_NODE
, 0, 0);
10586 xmlXPathCompLocationPath(ctxt
);
10588 xmlXPathCompFilterExpr(ctxt
);
10590 if ((CUR
== '/') && (NXT(1) == '/')) {
10594 PUSH_LONG_EXPR(XPATH_OP_COLLECT
, AXIS_DESCENDANT_OR_SELF
,
10595 NODE_TEST_TYPE
, NODE_TYPE_NODE
, NULL
, NULL
);
10596 PUSH_UNARY_EXPR(XPATH_OP_RESET
, ctxt
->comp
->last
, 1, 0);
10598 xmlXPathCompRelativeLocationPath(ctxt
);
10599 } else if (CUR
== '/') {
10600 xmlXPathCompRelativeLocationPath(ctxt
);
10607 * xmlXPathCompUnionExpr:
10608 * @ctxt: the XPath Parser context
10610 * [18] UnionExpr ::= PathExpr
10611 * | UnionExpr '|' PathExpr
10613 * Compile an union expression.
10617 xmlXPathCompUnionExpr(xmlXPathParserContextPtr ctxt
) {
10618 xmlXPathCompPathExpr(ctxt
);
10621 while (CUR
== '|') {
10622 int op1
= ctxt
->comp
->last
;
10623 PUSH_LEAVE_EXPR(XPATH_OP_NODE
, 0, 0);
10627 xmlXPathCompPathExpr(ctxt
);
10629 PUSH_BINARY_EXPR(XPATH_OP_UNION
, op1
, ctxt
->comp
->last
, 0, 0);
10636 * xmlXPathCompUnaryExpr:
10637 * @ctxt: the XPath Parser context
10639 * [27] UnaryExpr ::= UnionExpr
10642 * Compile an unary expression.
10646 xmlXPathCompUnaryExpr(xmlXPathParserContextPtr ctxt
) {
10651 while (CUR
== '-') {
10658 xmlXPathCompUnionExpr(ctxt
);
10662 PUSH_UNARY_EXPR(XPATH_OP_PLUS
, ctxt
->comp
->last
, 2, 0);
10664 PUSH_UNARY_EXPR(XPATH_OP_PLUS
, ctxt
->comp
->last
, 3, 0);
10669 * xmlXPathCompMultiplicativeExpr:
10670 * @ctxt: the XPath Parser context
10672 * [26] MultiplicativeExpr ::= UnaryExpr
10673 * | MultiplicativeExpr MultiplyOperator UnaryExpr
10674 * | MultiplicativeExpr 'div' UnaryExpr
10675 * | MultiplicativeExpr 'mod' UnaryExpr
10676 * [34] MultiplyOperator ::= '*'
10678 * Compile an Additive expression.
10682 xmlXPathCompMultiplicativeExpr(xmlXPathParserContextPtr ctxt
) {
10683 xmlXPathCompUnaryExpr(ctxt
);
10686 while ((CUR
== '*') ||
10687 ((CUR
== 'd') && (NXT(1) == 'i') && (NXT(2) == 'v')) ||
10688 ((CUR
== 'm') && (NXT(1) == 'o') && (NXT(2) == 'd'))) {
10690 int op1
= ctxt
->comp
->last
;
10695 } else if (CUR
== 'd') {
10698 } else if (CUR
== 'm') {
10703 xmlXPathCompUnaryExpr(ctxt
);
10705 PUSH_BINARY_EXPR(XPATH_OP_MULT
, op1
, ctxt
->comp
->last
, op
, 0);
10711 * xmlXPathCompAdditiveExpr:
10712 * @ctxt: the XPath Parser context
10714 * [25] AdditiveExpr ::= MultiplicativeExpr
10715 * | AdditiveExpr '+' MultiplicativeExpr
10716 * | AdditiveExpr '-' MultiplicativeExpr
10718 * Compile an Additive expression.
10722 xmlXPathCompAdditiveExpr(xmlXPathParserContextPtr ctxt
) {
10724 xmlXPathCompMultiplicativeExpr(ctxt
);
10727 while ((CUR
== '+') || (CUR
== '-')) {
10729 int op1
= ctxt
->comp
->last
;
10731 if (CUR
== '+') plus
= 1;
10735 xmlXPathCompMultiplicativeExpr(ctxt
);
10737 PUSH_BINARY_EXPR(XPATH_OP_PLUS
, op1
, ctxt
->comp
->last
, plus
, 0);
10743 * xmlXPathCompRelationalExpr:
10744 * @ctxt: the XPath Parser context
10746 * [24] RelationalExpr ::= AdditiveExpr
10747 * | RelationalExpr '<' AdditiveExpr
10748 * | RelationalExpr '>' AdditiveExpr
10749 * | RelationalExpr '<=' AdditiveExpr
10750 * | RelationalExpr '>=' AdditiveExpr
10752 * A <= B > C is allowed ? Answer from James, yes with
10753 * (AdditiveExpr <= AdditiveExpr) > AdditiveExpr
10754 * which is basically what got implemented.
10756 * Compile a Relational expression, then push the result
10761 xmlXPathCompRelationalExpr(xmlXPathParserContextPtr ctxt
) {
10762 xmlXPathCompAdditiveExpr(ctxt
);
10765 while ((CUR
== '<') ||
10767 ((CUR
== '<') && (NXT(1) == '=')) ||
10768 ((CUR
== '>') && (NXT(1) == '='))) {
10770 int op1
= ctxt
->comp
->last
;
10772 if (CUR
== '<') inf
= 1;
10774 if (NXT(1) == '=') strict
= 0;
10779 xmlXPathCompAdditiveExpr(ctxt
);
10781 PUSH_BINARY_EXPR(XPATH_OP_CMP
, op1
, ctxt
->comp
->last
, inf
, strict
);
10787 * xmlXPathCompEqualityExpr:
10788 * @ctxt: the XPath Parser context
10790 * [23] EqualityExpr ::= RelationalExpr
10791 * | EqualityExpr '=' RelationalExpr
10792 * | EqualityExpr '!=' RelationalExpr
10794 * A != B != C is allowed ? Answer from James, yes with
10795 * (RelationalExpr = RelationalExpr) = RelationalExpr
10796 * (RelationalExpr != RelationalExpr) != RelationalExpr
10797 * which is basically what got implemented.
10799 * Compile an Equality expression.
10803 xmlXPathCompEqualityExpr(xmlXPathParserContextPtr ctxt
) {
10804 xmlXPathCompRelationalExpr(ctxt
);
10807 while ((CUR
== '=') || ((CUR
== '!') && (NXT(1) == '='))) {
10809 int op1
= ctxt
->comp
->last
;
10811 if (CUR
== '=') eq
= 1;
10816 xmlXPathCompRelationalExpr(ctxt
);
10818 PUSH_BINARY_EXPR(XPATH_OP_EQUAL
, op1
, ctxt
->comp
->last
, eq
, 0);
10824 * xmlXPathCompAndExpr:
10825 * @ctxt: the XPath Parser context
10827 * [22] AndExpr ::= EqualityExpr
10828 * | AndExpr 'and' EqualityExpr
10830 * Compile an AND expression.
10834 xmlXPathCompAndExpr(xmlXPathParserContextPtr ctxt
) {
10835 xmlXPathCompEqualityExpr(ctxt
);
10838 while ((CUR
== 'a') && (NXT(1) == 'n') && (NXT(2) == 'd')) {
10839 int op1
= ctxt
->comp
->last
;
10842 xmlXPathCompEqualityExpr(ctxt
);
10844 PUSH_BINARY_EXPR(XPATH_OP_AND
, op1
, ctxt
->comp
->last
, 0, 0);
10850 * xmlXPathCompileExpr:
10851 * @ctxt: the XPath Parser context
10853 * [14] Expr ::= OrExpr
10854 * [21] OrExpr ::= AndExpr
10855 * | OrExpr 'or' AndExpr
10857 * Parse and compile an expression
10860 xmlXPathCompileExpr(xmlXPathParserContextPtr ctxt
, int sort
) {
10861 xmlXPathCompAndExpr(ctxt
);
10864 while ((CUR
== 'o') && (NXT(1) == 'r')) {
10865 int op1
= ctxt
->comp
->last
;
10868 xmlXPathCompAndExpr(ctxt
);
10870 PUSH_BINARY_EXPR(XPATH_OP_OR
, op1
, ctxt
->comp
->last
, 0, 0);
10873 if ((sort
) && (ctxt
->comp
->steps
[ctxt
->comp
->last
].op
!= XPATH_OP_VALUE
)) {
10874 /* more ops could be optimized too */
10876 * This is the main place to eliminate sorting for
10877 * operations which don't require a sorted node-set.
10880 PUSH_UNARY_EXPR(XPATH_OP_SORT
, ctxt
->comp
->last
, 0, 0);
10885 * xmlXPathCompPredicate:
10886 * @ctxt: the XPath Parser context
10887 * @filter: act as a filter
10889 * [8] Predicate ::= '[' PredicateExpr ']'
10890 * [9] PredicateExpr ::= Expr
10892 * Compile a predicate expression
10895 xmlXPathCompPredicate(xmlXPathParserContextPtr ctxt
, int filter
) {
10896 int op1
= ctxt
->comp
->last
;
10900 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR
);
10905 ctxt
->comp
->last
= -1;
10907 * This call to xmlXPathCompileExpr() will deactivate sorting
10908 * of the predicate result.
10909 * TODO: Sorting is still activated for filters, since I'm not
10910 * sure if needed. Normally sorting should not be needed, since
10911 * a filter can only diminish the number of items in a sequence,
10912 * but won't change its order; so if the initial sequence is sorted,
10913 * subsequent sorting is not needed.
10916 xmlXPathCompileExpr(ctxt
, 0);
10918 xmlXPathCompileExpr(ctxt
, 1);
10922 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR
);
10926 PUSH_BINARY_EXPR(XPATH_OP_FILTER
, op1
, ctxt
->comp
->last
, 0, 0);
10928 PUSH_BINARY_EXPR(XPATH_OP_PREDICATE
, op1
, ctxt
->comp
->last
, 0, 0);
10935 * xmlXPathCompNodeTest:
10936 * @ctxt: the XPath Parser context
10937 * @test: pointer to a xmlXPathTestVal
10938 * @type: pointer to a xmlXPathTypeVal
10939 * @prefix: placeholder for a possible name prefix
10941 * [7] NodeTest ::= NameTest
10942 * | NodeType '(' ')'
10943 * | 'processing-instruction' '(' Literal ')'
10945 * [37] NameTest ::= '*'
10948 * [38] NodeType ::= 'comment'
10950 * | 'processing-instruction'
10953 * Returns the name found and updates @test, @type and @prefix appropriately
10956 xmlXPathCompNodeTest(xmlXPathParserContextPtr ctxt
, xmlXPathTestVal
*test
,
10957 xmlXPathTypeVal
*type
, const xmlChar
**prefix
,
10961 if ((test
== NULL
) || (type
== NULL
) || (prefix
== NULL
)) {
10965 *type
= (xmlXPathTypeVal
) 0;
10966 *test
= (xmlXPathTestVal
) 0;
10970 if ((name
== NULL
) && (CUR
== '*')) {
10975 *test
= NODE_TEST_ALL
;
10980 name
= xmlXPathParseNCName(ctxt
);
10981 if (name
== NULL
) {
10982 XP_ERRORNULL(XPATH_EXPR_ERROR
);
10985 blanks
= IS_BLANK_CH(CUR
);
10990 * NodeType or PI search
10992 if (xmlStrEqual(name
, BAD_CAST
"comment"))
10993 *type
= NODE_TYPE_COMMENT
;
10994 else if (xmlStrEqual(name
, BAD_CAST
"node"))
10995 *type
= NODE_TYPE_NODE
;
10996 else if (xmlStrEqual(name
, BAD_CAST
"processing-instruction"))
10997 *type
= NODE_TYPE_PI
;
10998 else if (xmlStrEqual(name
, BAD_CAST
"text"))
10999 *type
= NODE_TYPE_TEXT
;
11003 XP_ERRORNULL(XPATH_EXPR_ERROR
);
11006 *test
= NODE_TEST_TYPE
;
11009 if (*type
== NODE_TYPE_PI
) {
11011 * Specific case: search a PI by name.
11017 name
= xmlXPathParseLiteral(ctxt
);
11019 *test
= NODE_TEST_PI
;
11026 XP_ERRORNULL(XPATH_UNCLOSED_ERROR
);
11031 *test
= NODE_TEST_NAME
;
11032 if ((!blanks
) && (CUR
== ':')) {
11036 * Since currently the parser context don't have a
11037 * namespace list associated:
11038 * The namespace name for this prefix can be computed
11039 * only at evaluation time. The compilation is done
11040 * outside of any context.
11043 *prefix
= xmlXPathNsLookup(ctxt
->context
, name
);
11046 if (*prefix
== NULL
) {
11047 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR
);
11058 *test
= NODE_TEST_ALL
;
11062 name
= xmlXPathParseNCName(ctxt
);
11063 if (name
== NULL
) {
11064 XP_ERRORNULL(XPATH_EXPR_ERROR
);
11071 * xmlXPathIsAxisName:
11072 * @name: a preparsed name token
11074 * [6] AxisName ::= 'ancestor'
11075 * | 'ancestor-or-self'
11079 * | 'descendant-or-self'
11081 * | 'following-sibling'
11085 * | 'preceding-sibling'
11088 * Returns the axis or 0
11090 static xmlXPathAxisVal
11091 xmlXPathIsAxisName(const xmlChar
*name
) {
11092 xmlXPathAxisVal ret
= (xmlXPathAxisVal
) 0;
11095 if (xmlStrEqual(name
, BAD_CAST
"ancestor"))
11096 ret
= AXIS_ANCESTOR
;
11097 if (xmlStrEqual(name
, BAD_CAST
"ancestor-or-self"))
11098 ret
= AXIS_ANCESTOR_OR_SELF
;
11099 if (xmlStrEqual(name
, BAD_CAST
"attribute"))
11100 ret
= AXIS_ATTRIBUTE
;
11103 if (xmlStrEqual(name
, BAD_CAST
"child"))
11107 if (xmlStrEqual(name
, BAD_CAST
"descendant"))
11108 ret
= AXIS_DESCENDANT
;
11109 if (xmlStrEqual(name
, BAD_CAST
"descendant-or-self"))
11110 ret
= AXIS_DESCENDANT_OR_SELF
;
11113 if (xmlStrEqual(name
, BAD_CAST
"following"))
11114 ret
= AXIS_FOLLOWING
;
11115 if (xmlStrEqual(name
, BAD_CAST
"following-sibling"))
11116 ret
= AXIS_FOLLOWING_SIBLING
;
11119 if (xmlStrEqual(name
, BAD_CAST
"namespace"))
11120 ret
= AXIS_NAMESPACE
;
11123 if (xmlStrEqual(name
, BAD_CAST
"parent"))
11125 if (xmlStrEqual(name
, BAD_CAST
"preceding"))
11126 ret
= AXIS_PRECEDING
;
11127 if (xmlStrEqual(name
, BAD_CAST
"preceding-sibling"))
11128 ret
= AXIS_PRECEDING_SIBLING
;
11131 if (xmlStrEqual(name
, BAD_CAST
"self"))
11139 * xmlXPathCompStep:
11140 * @ctxt: the XPath Parser context
11142 * [4] Step ::= AxisSpecifier NodeTest Predicate*
11143 * | AbbreviatedStep
11145 * [12] AbbreviatedStep ::= '.' | '..'
11147 * [5] AxisSpecifier ::= AxisName '::'
11148 * | AbbreviatedAxisSpecifier
11150 * [13] AbbreviatedAxisSpecifier ::= '@'?
11152 * Modified for XPtr range support as:
11154 * [4xptr] Step ::= AxisSpecifier NodeTest Predicate*
11155 * | AbbreviatedStep
11156 * | 'range-to' '(' Expr ')' Predicate*
11158 * Compile one step in a Location Path
11159 * A location step of . is short for self::node(). This is
11160 * particularly useful in conjunction with //. For example, the
11161 * location path .//para is short for
11162 * self::node()/descendant-or-self::node()/child::para
11163 * and so will select all para descendant elements of the context
11165 * Similarly, a location step of .. is short for parent::node().
11166 * For example, ../title is short for parent::node()/child::title
11167 * and so will select the title children of the parent of the context
11171 xmlXPathCompStep(xmlXPathParserContextPtr ctxt
) {
11172 #ifdef LIBXML_XPTR_ENABLED
11178 if ((CUR
== '.') && (NXT(1) == '.')) {
11181 PUSH_LONG_EXPR(XPATH_OP_COLLECT
, AXIS_PARENT
,
11182 NODE_TEST_TYPE
, NODE_TYPE_NODE
, NULL
, NULL
);
11183 } else if (CUR
== '.') {
11187 xmlChar
*name
= NULL
;
11188 const xmlChar
*prefix
= NULL
;
11189 xmlXPathTestVal test
= (xmlXPathTestVal
) 0;
11190 xmlXPathAxisVal axis
= (xmlXPathAxisVal
) 0;
11191 xmlXPathTypeVal type
= (xmlXPathTypeVal
) 0;
11195 * The modification needed for XPointer change to the production
11197 #ifdef LIBXML_XPTR_ENABLED
11199 name
= xmlXPathParseNCName(ctxt
);
11200 if ((name
!= NULL
) && (xmlStrEqual(name
, BAD_CAST
"range-to"))) {
11201 op2
= ctxt
->comp
->last
;
11205 XP_ERROR(XPATH_EXPR_ERROR
);
11210 xmlXPathCompileExpr(ctxt
, 1);
11211 /* PUSH_BINARY_EXPR(XPATH_OP_RANGETO, op2, ctxt->comp->last, 0, 0); */
11216 XP_ERROR(XPATH_EXPR_ERROR
);
11220 goto eval_predicates
;
11228 name
= xmlXPathParseNCName(ctxt
);
11229 if (name
!= NULL
) {
11230 axis
= xmlXPathIsAxisName(name
);
11233 if ((CUR
== ':') && (NXT(1) == ':')) {
11238 /* an element name can conflict with an axis one :-\ */
11244 } else if (CUR
== '@') {
11246 axis
= AXIS_ATTRIBUTE
;
11254 name
= xmlXPathCompNodeTest(ctxt
, &test
, &type
, &prefix
, name
);
11258 if ((prefix
!= NULL
) && (ctxt
->context
!= NULL
) &&
11259 (ctxt
->context
->flags
& XML_XPATH_CHECKNS
)) {
11260 if (xmlXPathNsLookup(ctxt
->context
, prefix
) == NULL
) {
11261 xmlXPathErr(ctxt
, XPATH_UNDEF_PREFIX_ERROR
);
11265 xmlGenericError(xmlGenericErrorContext
,
11266 "Basis : computing new set\n");
11270 xmlGenericError(xmlGenericErrorContext
, "Basis : ");
11271 if (ctxt
->value
== NULL
)
11272 xmlGenericError(xmlGenericErrorContext
, "no value\n");
11273 else if (ctxt
->value
->nodesetval
== NULL
)
11274 xmlGenericError(xmlGenericErrorContext
, "Empty\n");
11276 xmlGenericErrorContextNodeSet(stdout
, ctxt
->value
->nodesetval
);
11279 #ifdef LIBXML_XPTR_ENABLED
11282 op1
= ctxt
->comp
->last
;
11283 ctxt
->comp
->last
= -1;
11286 while (CUR
== '[') {
11287 xmlXPathCompPredicate(ctxt
, 0);
11290 #ifdef LIBXML_XPTR_ENABLED
11292 PUSH_BINARY_EXPR(XPATH_OP_RANGETO
, op2
, op1
, 0, 0);
11295 PUSH_FULL_EXPR(XPATH_OP_COLLECT
, op1
, ctxt
->comp
->last
, axis
,
11296 test
, type
, (void *)prefix
, (void *)name
);
11300 xmlGenericError(xmlGenericErrorContext
, "Step : ");
11301 if (ctxt
->value
== NULL
)
11302 xmlGenericError(xmlGenericErrorContext
, "no value\n");
11303 else if (ctxt
->value
->nodesetval
== NULL
)
11304 xmlGenericError(xmlGenericErrorContext
, "Empty\n");
11306 xmlGenericErrorContextNodeSet(xmlGenericErrorContext
,
11307 ctxt
->value
->nodesetval
);
11312 * xmlXPathCompRelativeLocationPath:
11313 * @ctxt: the XPath Parser context
11315 * [3] RelativeLocationPath ::= Step
11316 * | RelativeLocationPath '/' Step
11317 * | AbbreviatedRelativeLocationPath
11318 * [11] AbbreviatedRelativeLocationPath ::= RelativeLocationPath '//' Step
11320 * Compile a relative location path.
11323 xmlXPathCompRelativeLocationPath
11324 (xmlXPathParserContextPtr ctxt
) {
11326 if ((CUR
== '/') && (NXT(1) == '/')) {
11329 PUSH_LONG_EXPR(XPATH_OP_COLLECT
, AXIS_DESCENDANT_OR_SELF
,
11330 NODE_TEST_TYPE
, NODE_TYPE_NODE
, NULL
, NULL
);
11331 } else if (CUR
== '/') {
11335 xmlXPathCompStep(ctxt
);
11337 while (CUR
== '/') {
11338 if ((CUR
== '/') && (NXT(1) == '/')) {
11341 PUSH_LONG_EXPR(XPATH_OP_COLLECT
, AXIS_DESCENDANT_OR_SELF
,
11342 NODE_TEST_TYPE
, NODE_TYPE_NODE
, NULL
, NULL
);
11343 xmlXPathCompStep(ctxt
);
11344 } else if (CUR
== '/') {
11347 xmlXPathCompStep(ctxt
);
11354 * xmlXPathCompLocationPath:
11355 * @ctxt: the XPath Parser context
11357 * [1] LocationPath ::= RelativeLocationPath
11358 * | AbsoluteLocationPath
11359 * [2] AbsoluteLocationPath ::= '/' RelativeLocationPath?
11360 * | AbbreviatedAbsoluteLocationPath
11361 * [10] AbbreviatedAbsoluteLocationPath ::=
11362 * '//' RelativeLocationPath
11364 * Compile a location path
11366 * // is short for /descendant-or-self::node()/. For example,
11367 * //para is short for /descendant-or-self::node()/child::para and
11368 * so will select any para element in the document (even a para element
11369 * that is a document element will be selected by //para since the
11370 * document element node is a child of the root node); div//para is
11371 * short for div/descendant-or-self::node()/child::para and so will
11372 * select all para descendants of div children.
11375 xmlXPathCompLocationPath(xmlXPathParserContextPtr ctxt
) {
11378 xmlXPathCompRelativeLocationPath(ctxt
);
11380 while (CUR
== '/') {
11381 if ((CUR
== '/') && (NXT(1) == '/')) {
11384 PUSH_LONG_EXPR(XPATH_OP_COLLECT
, AXIS_DESCENDANT_OR_SELF
,
11385 NODE_TEST_TYPE
, NODE_TYPE_NODE
, NULL
, NULL
);
11386 xmlXPathCompRelativeLocationPath(ctxt
);
11387 } else if (CUR
== '/') {
11391 ((IS_ASCII_LETTER(CUR
)) || (CUR
== '_') || (CUR
== '.') ||
11392 (CUR
== '@') || (CUR
== '*')))
11393 xmlXPathCompRelativeLocationPath(ctxt
);
11399 /************************************************************************
11401 * XPath precompiled expression evaluation *
11403 ************************************************************************/
11406 xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt
, xmlXPathStepOpPtr op
);
11410 xmlXPathDebugDumpStepAxis(xmlXPathStepOpPtr op
,
11413 xmlGenericError(xmlGenericErrorContext
, "new step : ");
11414 switch (op
->value
) {
11415 case AXIS_ANCESTOR
:
11416 xmlGenericError(xmlGenericErrorContext
, "axis 'ancestors' ");
11418 case AXIS_ANCESTOR_OR_SELF
:
11419 xmlGenericError(xmlGenericErrorContext
,
11420 "axis 'ancestors-or-self' ");
11422 case AXIS_ATTRIBUTE
:
11423 xmlGenericError(xmlGenericErrorContext
, "axis 'attributes' ");
11426 xmlGenericError(xmlGenericErrorContext
, "axis 'child' ");
11428 case AXIS_DESCENDANT
:
11429 xmlGenericError(xmlGenericErrorContext
, "axis 'descendant' ");
11431 case AXIS_DESCENDANT_OR_SELF
:
11432 xmlGenericError(xmlGenericErrorContext
,
11433 "axis 'descendant-or-self' ");
11435 case AXIS_FOLLOWING
:
11436 xmlGenericError(xmlGenericErrorContext
, "axis 'following' ");
11438 case AXIS_FOLLOWING_SIBLING
:
11439 xmlGenericError(xmlGenericErrorContext
,
11440 "axis 'following-siblings' ");
11442 case AXIS_NAMESPACE
:
11443 xmlGenericError(xmlGenericErrorContext
, "axis 'namespace' ");
11446 xmlGenericError(xmlGenericErrorContext
, "axis 'parent' ");
11448 case AXIS_PRECEDING
:
11449 xmlGenericError(xmlGenericErrorContext
, "axis 'preceding' ");
11451 case AXIS_PRECEDING_SIBLING
:
11452 xmlGenericError(xmlGenericErrorContext
,
11453 "axis 'preceding-sibling' ");
11456 xmlGenericError(xmlGenericErrorContext
, "axis 'self' ");
11459 xmlGenericError(xmlGenericErrorContext
,
11460 " context contains %d nodes\n", nbNodes
);
11461 switch (op
->value2
) {
11462 case NODE_TEST_NONE
:
11463 xmlGenericError(xmlGenericErrorContext
,
11464 " searching for none !!!\n");
11466 case NODE_TEST_TYPE
:
11467 xmlGenericError(xmlGenericErrorContext
,
11468 " searching for type %d\n", op
->value3
);
11471 xmlGenericError(xmlGenericErrorContext
,
11472 " searching for PI !!!\n");
11474 case NODE_TEST_ALL
:
11475 xmlGenericError(xmlGenericErrorContext
,
11476 " searching for *\n");
11479 xmlGenericError(xmlGenericErrorContext
,
11480 " searching for namespace %s\n",
11483 case NODE_TEST_NAME
:
11484 xmlGenericError(xmlGenericErrorContext
,
11485 " searching for name %s\n", op
->value5
);
11487 xmlGenericError(xmlGenericErrorContext
,
11488 " with namespace %s\n", op
->value4
);
11491 xmlGenericError(xmlGenericErrorContext
, "Testing : ");
11493 #endif /* DEBUG_STEP */
11496 xmlXPathCompOpEvalPredicate(xmlXPathParserContextPtr ctxt
,
11497 xmlXPathStepOpPtr op
,
11502 if (op
->ch1
!= -1) {
11503 xmlXPathCompExprPtr comp
= ctxt
->comp
;
11505 * Process inner predicates first.
11507 if (comp
->steps
[op
->ch1
].op
!= XPATH_OP_PREDICATE
) {
11509 * TODO: raise an internal error.
11512 contextSize
= xmlXPathCompOpEvalPredicate(ctxt
,
11513 &comp
->steps
[op
->ch1
], set
, contextSize
, hasNsNodes
);
11515 if (contextSize
<= 0)
11518 if (op
->ch2
!= -1) {
11519 xmlXPathContextPtr xpctxt
= ctxt
->context
;
11520 xmlNodePtr contextNode
, oldContextNode
;
11521 xmlDocPtr oldContextDoc
;
11522 int i
, res
, contextPos
= 0, newContextSize
;
11523 xmlXPathStepOpPtr exprOp
;
11524 xmlXPathObjectPtr contextObj
= NULL
, exprRes
= NULL
;
11526 #ifdef LIBXML_XPTR_ENABLED
11528 * URGENT TODO: Check the following:
11529 * We don't expect location sets if evaluating prediates, right?
11530 * Only filters should expect location sets, right?
11535 * "For each node in the node-set to be filtered, the
11536 * PredicateExpr is evaluated with that node as the
11537 * context node, with the number of nodes in the
11538 * node-set as the context size, and with the proximity
11539 * position of the node in the node-set with respect to
11540 * the axis as the context position;"
11541 * @oldset is the node-set" to be filtered.
11544 * "only predicates change the context position and
11545 * context size (see [2.4 Predicates])."
11547 * node-set context pos
11551 * After applying predicate [position() > 1] :
11552 * node-set context pos
11556 oldContextNode
= xpctxt
->node
;
11557 oldContextDoc
= xpctxt
->doc
;
11559 * Get the expression of this predicate.
11561 exprOp
= &ctxt
->comp
->steps
[op
->ch2
];
11562 newContextSize
= 0;
11563 for (i
= 0; i
< set
->nodeNr
; i
++) {
11564 if (set
->nodeTab
[i
] == NULL
)
11567 contextNode
= set
->nodeTab
[i
];
11568 xpctxt
->node
= contextNode
;
11569 xpctxt
->contextSize
= contextSize
;
11570 xpctxt
->proximityPosition
= ++contextPos
;
11573 * Also set the xpath document in case things like
11574 * key() are evaluated in the predicate.
11576 if ((contextNode
->type
!= XML_NAMESPACE_DECL
) &&
11577 (contextNode
->doc
!= NULL
))
11578 xpctxt
->doc
= contextNode
->doc
;
11580 * Evaluate the predicate expression with 1 context node
11581 * at a time; this node is packaged into a node set; this
11582 * node set is handed over to the evaluation mechanism.
11584 if (contextObj
== NULL
)
11585 contextObj
= xmlXPathCacheNewNodeSet(xpctxt
, contextNode
);
11587 xmlXPathNodeSetAddUnique(contextObj
->nodesetval
,
11590 valuePush(ctxt
, contextObj
);
11592 res
= xmlXPathCompOpEvalToBoolean(ctxt
, exprOp
, 1);
11594 if ((ctxt
->error
!= XPATH_EXPRESSION_OK
) || (res
== -1)) {
11595 xmlXPathNodeSetClear(set
, hasNsNodes
);
11596 newContextSize
= 0;
11597 goto evaluation_exit
;
11604 * Remove the entry from the initial node set.
11606 set
->nodeTab
[i
] = NULL
;
11607 if (contextNode
->type
== XML_NAMESPACE_DECL
)
11608 xmlXPathNodeSetFreeNs((xmlNsPtr
) contextNode
);
11610 if (ctxt
->value
== contextObj
) {
11612 * Don't free the temporary XPath object holding the
11613 * context node, in order to avoid massive recreation
11614 * inside this loop.
11617 xmlXPathNodeSetClear(contextObj
->nodesetval
, hasNsNodes
);
11620 * TODO: The object was lost in the evaluation machinery.
11621 * Can this happen? Maybe in internal-error cases.
11627 if (contextObj
!= NULL
) {
11628 if (ctxt
->value
== contextObj
)
11630 xmlXPathReleaseObject(xpctxt
, contextObj
);
11633 if (exprRes
!= NULL
)
11634 xmlXPathReleaseObject(ctxt
->context
, exprRes
);
11636 * Reset/invalidate the context.
11638 xpctxt
->node
= oldContextNode
;
11639 xpctxt
->doc
= oldContextDoc
;
11640 xpctxt
->contextSize
= -1;
11641 xpctxt
->proximityPosition
= -1;
11642 return(newContextSize
);
11644 return(contextSize
);
11648 xmlXPathCompOpEvalPositionalPredicate(xmlXPathParserContextPtr ctxt
,
11649 xmlXPathStepOpPtr op
,
11656 if (op
->ch1
!= -1) {
11657 xmlXPathCompExprPtr comp
= ctxt
->comp
;
11658 if (comp
->steps
[op
->ch1
].op
!= XPATH_OP_PREDICATE
) {
11660 * TODO: raise an internal error.
11663 contextSize
= xmlXPathCompOpEvalPredicate(ctxt
,
11664 &comp
->steps
[op
->ch1
], set
, contextSize
, hasNsNodes
);
11666 if (contextSize
<= 0)
11670 * Check if the node set contains a sufficient number of nodes for
11671 * the requested range.
11673 if (contextSize
< minPos
) {
11674 xmlXPathNodeSetClear(set
, hasNsNodes
);
11677 if (op
->ch2
== -1) {
11679 * TODO: Can this ever happen?
11681 return (contextSize
);
11683 xmlDocPtr oldContextDoc
;
11684 int i
, pos
= 0, newContextSize
= 0, contextPos
= 0, res
;
11685 xmlXPathStepOpPtr exprOp
;
11686 xmlXPathObjectPtr contextObj
= NULL
, exprRes
= NULL
;
11687 xmlNodePtr oldContextNode
, contextNode
= NULL
;
11688 xmlXPathContextPtr xpctxt
= ctxt
->context
;
11690 #ifdef LIBXML_XPTR_ENABLED
11692 * URGENT TODO: Check the following:
11693 * We don't expect location sets if evaluating prediates, right?
11694 * Only filters should expect location sets, right?
11696 #endif /* LIBXML_XPTR_ENABLED */
11699 * Save old context.
11701 oldContextNode
= xpctxt
->node
;
11702 oldContextDoc
= xpctxt
->doc
;
11704 * Get the expression of this predicate.
11706 exprOp
= &ctxt
->comp
->steps
[op
->ch2
];
11707 for (i
= 0; i
< set
->nodeNr
; i
++) {
11708 if (set
->nodeTab
[i
] == NULL
)
11711 contextNode
= set
->nodeTab
[i
];
11712 xpctxt
->node
= contextNode
;
11713 xpctxt
->contextSize
= contextSize
;
11714 xpctxt
->proximityPosition
= ++contextPos
;
11717 * Initialize the new set.
11718 * Also set the xpath document in case things like
11719 * key() evaluation are attempted on the predicate
11721 if ((contextNode
->type
!= XML_NAMESPACE_DECL
) &&
11722 (contextNode
->doc
!= NULL
))
11723 xpctxt
->doc
= contextNode
->doc
;
11725 * Evaluate the predicate expression with 1 context node
11726 * at a time; this node is packaged into a node set; this
11727 * node set is handed over to the evaluation mechanism.
11729 if (contextObj
== NULL
)
11730 contextObj
= xmlXPathCacheNewNodeSet(xpctxt
, contextNode
);
11732 xmlXPathNodeSetAddUnique(contextObj
->nodesetval
,
11735 valuePush(ctxt
, contextObj
);
11736 res
= xmlXPathCompOpEvalToBoolean(ctxt
, exprOp
, 1);
11738 if ((ctxt
->error
!= XPATH_EXPRESSION_OK
) || (res
== -1)) {
11739 xmlXPathObjectPtr tmp
;
11740 /* pop the result */
11741 tmp
= valuePop(ctxt
);
11742 xmlXPathReleaseObject(xpctxt
, tmp
);
11743 /* then pop off contextObj, which will be freed later */
11745 goto evaluation_error
;
11751 if (res
&& (pos
>= minPos
) && (pos
<= maxPos
)) {
11753 * Fits in the requested range.
11756 if (minPos
== maxPos
) {
11758 * Only 1 node was requested.
11760 if (contextNode
->type
== XML_NAMESPACE_DECL
) {
11762 * As always: take care of those nasty
11765 set
->nodeTab
[i
] = NULL
;
11767 xmlXPathNodeSetClear(set
, hasNsNodes
);
11769 set
->nodeTab
[0] = contextNode
;
11770 goto evaluation_exit
;
11772 if (pos
== maxPos
) {
11776 xmlXPathNodeSetClearFromPos(set
, i
+1, hasNsNodes
);
11777 goto evaluation_exit
;
11781 * Remove the entry from the initial node set.
11783 set
->nodeTab
[i
] = NULL
;
11784 if (contextNode
->type
== XML_NAMESPACE_DECL
)
11785 xmlXPathNodeSetFreeNs((xmlNsPtr
) contextNode
);
11787 if (exprRes
!= NULL
) {
11788 xmlXPathReleaseObject(ctxt
->context
, exprRes
);
11791 if (ctxt
->value
== contextObj
) {
11793 * Don't free the temporary XPath object holding the
11794 * context node, in order to avoid massive recreation
11795 * inside this loop.
11798 xmlXPathNodeSetClear(contextObj
->nodesetval
, hasNsNodes
);
11801 * The object was lost in the evaluation machinery.
11802 * Can this happen? Maybe in case of internal-errors.
11807 goto evaluation_exit
;
11810 xmlXPathNodeSetClear(set
, hasNsNodes
);
11811 newContextSize
= 0;
11814 if (contextObj
!= NULL
) {
11815 if (ctxt
->value
== contextObj
)
11817 xmlXPathReleaseObject(xpctxt
, contextObj
);
11819 if (exprRes
!= NULL
)
11820 xmlXPathReleaseObject(ctxt
->context
, exprRes
);
11822 * Reset/invalidate the context.
11824 xpctxt
->node
= oldContextNode
;
11825 xpctxt
->doc
= oldContextDoc
;
11826 xpctxt
->contextSize
= -1;
11827 xpctxt
->proximityPosition
= -1;
11828 return(newContextSize
);
11830 return(contextSize
);
11834 xmlXPathIsPositionalPredicate(xmlXPathParserContextPtr ctxt
,
11835 xmlXPathStepOpPtr op
,
11839 xmlXPathStepOpPtr exprOp
;
11842 * BIG NOTE: This is not intended for XPATH_OP_FILTER yet!
11846 * If not -1, then ch1 will point to:
11847 * 1) For predicates (XPATH_OP_PREDICATE):
11848 * - an inner predicate operator
11849 * 2) For filters (XPATH_OP_FILTER):
11850 * - an inner filter operater OR
11851 * - an expression selecting the node set.
11852 * E.g. "key('a', 'b')" or "(//foo | //bar)".
11854 if ((op
->op
!= XPATH_OP_PREDICATE
) && (op
->op
!= XPATH_OP_FILTER
))
11857 if (op
->ch2
!= -1) {
11858 exprOp
= &ctxt
->comp
->steps
[op
->ch2
];
11862 if ((exprOp
!= NULL
) &&
11863 (exprOp
->op
== XPATH_OP_VALUE
) &&
11864 (exprOp
->value4
!= NULL
) &&
11865 (((xmlXPathObjectPtr
) exprOp
->value4
)->type
== XPATH_NUMBER
))
11868 * We have a "[n]" predicate here.
11869 * TODO: Unfortunately this simplistic test here is not
11870 * able to detect a position() predicate in compound
11871 * expressions like "[@attr = 'a" and position() = 1],
11872 * and even not the usage of position() in
11873 * "[position() = 1]"; thus - obviously - a position-range,
11874 * like it "[position() < 5]", is also not detected.
11875 * Maybe we could rewrite the AST to ease the optimization.
11877 *maxPos
= (int) ((xmlXPathObjectPtr
) exprOp
->value4
)->floatval
;
11879 if (((xmlXPathObjectPtr
) exprOp
->value4
)->floatval
==
11889 xmlXPathNodeCollectAndTest(xmlXPathParserContextPtr ctxt
,
11890 xmlXPathStepOpPtr op
,
11891 xmlNodePtr
* first
, xmlNodePtr
* last
,
11895 #define XP_TEST_HIT \
11896 if (hasAxisRange != 0) { \
11897 if (++pos == maxPos) { \
11898 addNode(seq, cur); \
11899 goto axis_range_end; } \
11901 addNode(seq, cur); \
11902 if (breakOnFirstHit) goto first_hit; }
11904 #define XP_TEST_HIT_NS \
11905 if (hasAxisRange != 0) { \
11906 if (++pos == maxPos) { \
11908 xmlXPathNodeSetAddNs(seq, xpctxt->node, (xmlNsPtr) cur); \
11909 goto axis_range_end; } \
11912 xmlXPathNodeSetAddNs(seq, \
11913 xpctxt->node, (xmlNsPtr) cur); \
11914 if (breakOnFirstHit) goto first_hit; }
11916 xmlXPathAxisVal axis
= (xmlXPathAxisVal
) op
->value
;
11917 xmlXPathTestVal test
= (xmlXPathTestVal
) op
->value2
;
11918 xmlXPathTypeVal type
= (xmlXPathTypeVal
) op
->value3
;
11919 const xmlChar
*prefix
= op
->value4
;
11920 const xmlChar
*name
= op
->value5
;
11921 const xmlChar
*URI
= NULL
;
11924 int nbMatches
= 0, prevMatches
= 0;
11926 int total
= 0, hasNsNodes
= 0;
11927 /* The popped object holding the context nodes */
11928 xmlXPathObjectPtr obj
;
11929 /* The set of context nodes for the node tests */
11930 xmlNodeSetPtr contextSeq
;
11932 xmlNodePtr contextNode
;
11933 /* The context node for a compound traversal */
11934 xmlNodePtr outerContextNode
;
11935 /* The final resulting node set wrt to all context nodes */
11936 xmlNodeSetPtr outSeq
;
11938 * The temporary resulting node set wrt 1 context node.
11939 * Used to feed predicate evaluation.
11943 /* First predicate operator */
11944 xmlXPathStepOpPtr predOp
;
11945 int maxPos
; /* The requested position() (when a "[n]" predicate) */
11946 int hasPredicateRange
, hasAxisRange
, pos
, size
, newSize
;
11947 int breakOnFirstHit
;
11949 xmlXPathTraversalFunction next
= NULL
;
11950 /* compound axis traversal */
11951 xmlXPathTraversalFunctionExt outerNext
= NULL
;
11952 void (*addNode
) (xmlNodeSetPtr
, xmlNodePtr
);
11953 xmlXPathNodeSetMergeFunction mergeAndClear
;
11954 xmlNodePtr oldContextNode
;
11955 xmlXPathContextPtr xpctxt
= ctxt
->context
;
11958 CHECK_TYPE0(XPATH_NODESET
);
11959 obj
= valuePop(ctxt
);
11961 * Setup namespaces.
11963 if (prefix
!= NULL
) {
11964 URI
= xmlXPathNsLookup(xpctxt
, prefix
);
11966 xmlXPathReleaseObject(xpctxt
, obj
);
11967 XP_ERROR0(XPATH_UNDEF_PREFIX_ERROR
);
11973 * MAYBE FUTURE TODO: merging optimizations:
11974 * - If the nodes to be traversed wrt to the initial nodes and
11975 * the current axis cannot overlap, then we could avoid searching
11976 * for duplicates during the merge.
11977 * But the question is how/when to evaluate if they cannot overlap.
11978 * Example: if we know that for two initial nodes, the one is
11979 * not in the ancestor-or-self axis of the other, then we could safely
11980 * avoid a duplicate-aware merge, if the axis to be traversed is e.g.
11981 * the descendant-or-self axis.
11983 mergeAndClear
= xmlXPathNodeSetMergeAndClear
;
11985 case AXIS_ANCESTOR
:
11987 next
= xmlXPathNextAncestor
;
11989 case AXIS_ANCESTOR_OR_SELF
:
11991 next
= xmlXPathNextAncestorOrSelf
;
11993 case AXIS_ATTRIBUTE
:
11996 next
= xmlXPathNextAttribute
;
11997 mergeAndClear
= xmlXPathNodeSetMergeAndClearNoDupls
;
12001 if (op
->rewriteType
== XP_REWRITE_DOS_CHILD_ELEM
) {
12003 * This iterator will give us only nodes which can
12004 * hold element nodes.
12006 outerNext
= xmlXPathNextDescendantOrSelfElemParent
;
12008 if (((test
== NODE_TEST_NAME
) || (test
== NODE_TEST_ALL
)) &&
12009 (type
== NODE_TYPE_NODE
))
12012 * Optimization if an element node type is 'element'.
12014 next
= xmlXPathNextChildElement
;
12016 next
= xmlXPathNextChild
;
12017 mergeAndClear
= xmlXPathNodeSetMergeAndClearNoDupls
;
12019 case AXIS_DESCENDANT
:
12021 next
= xmlXPathNextDescendant
;
12023 case AXIS_DESCENDANT_OR_SELF
:
12025 next
= xmlXPathNextDescendantOrSelf
;
12027 case AXIS_FOLLOWING
:
12029 next
= xmlXPathNextFollowing
;
12031 case AXIS_FOLLOWING_SIBLING
:
12033 next
= xmlXPathNextFollowingSibling
;
12035 case AXIS_NAMESPACE
:
12038 next
= (xmlXPathTraversalFunction
) xmlXPathNextNamespace
;
12039 mergeAndClear
= xmlXPathNodeSetMergeAndClearNoDupls
;
12043 next
= xmlXPathNextParent
;
12045 case AXIS_PRECEDING
:
12047 next
= xmlXPathNextPrecedingInternal
;
12049 case AXIS_PRECEDING_SIBLING
:
12051 next
= xmlXPathNextPrecedingSibling
;
12056 next
= xmlXPathNextSelf
;
12057 mergeAndClear
= xmlXPathNodeSetMergeAndClearNoDupls
;
12062 xmlXPathDebugDumpStepAxis(op
,
12063 (obj
->nodesetval
!= NULL
) ? obj
->nodesetval
->nodeNr
: 0);
12066 if (next
== NULL
) {
12067 xmlXPathReleaseObject(xpctxt
, obj
);
12070 contextSeq
= obj
->nodesetval
;
12071 if ((contextSeq
== NULL
) || (contextSeq
->nodeNr
<= 0)) {
12072 xmlXPathReleaseObject(xpctxt
, obj
);
12073 valuePush(ctxt
, xmlXPathCacheWrapNodeSet(xpctxt
, NULL
));
12077 * Predicate optimization ---------------------------------------------
12078 * If this step has a last predicate, which contains a position(),
12079 * then we'll optimize (although not exactly "position()", but only
12080 * the short-hand form, i.e., "[n]".
12082 * Example - expression "/foo[parent::bar][1]":
12084 * COLLECT 'child' 'name' 'node' foo -- op (we are here)
12086 * PREDICATE -- op->ch2 (predOp)
12087 * PREDICATE -- predOp->ch1 = [parent::bar]
12089 * COLLECT 'parent' 'name' 'node' bar
12091 * ELEM Object is a number : 1 -- predOp->ch2 = [1]
12096 hasPredicateRange
= 0;
12098 if (op
->ch2
!= -1) {
12100 * There's at least one predicate. 16 == XPATH_OP_PREDICATE
12102 predOp
= &ctxt
->comp
->steps
[op
->ch2
];
12103 if (xmlXPathIsPositionalPredicate(ctxt
, predOp
, &maxPos
)) {
12104 if (predOp
->ch1
!= -1) {
12106 * Use the next inner predicate operator.
12108 predOp
= &ctxt
->comp
->steps
[predOp
->ch1
];
12109 hasPredicateRange
= 1;
12112 * There's no other predicate than the [n] predicate.
12119 breakOnFirstHit
= ((toBool
) && (predOp
== NULL
)) ? 1 : 0;
12121 * Axis traversal -----------------------------------------------------
12125 * - For the attribute axis, the principal node type is attribute.
12126 * - For the namespace axis, the principal node type is namespace.
12127 * - For other axes, the principal node type is element.
12129 * A node test * is true for any node of the
12130 * principal node type. For example, child::* will
12131 * select all element children of the context node
12133 oldContextNode
= xpctxt
->node
;
12134 addNode
= xmlXPathNodeSetAddUnique
;
12137 outerContextNode
= NULL
;
12138 contextNode
= NULL
;
12142 while ((contextIdx
< contextSeq
->nodeNr
) || (contextNode
!= NULL
)) {
12143 if (outerNext
!= NULL
) {
12145 * This is a compound traversal.
12147 if (contextNode
== NULL
) {
12149 * Set the context for the outer traversal.
12151 outerContextNode
= contextSeq
->nodeTab
[contextIdx
++];
12152 contextNode
= outerNext(NULL
, outerContextNode
);
12154 contextNode
= outerNext(contextNode
, outerContextNode
);
12155 if (contextNode
== NULL
)
12158 * Set the context for the main traversal.
12160 xpctxt
->node
= contextNode
;
12162 xpctxt
->node
= contextSeq
->nodeTab
[contextIdx
++];
12165 seq
= xmlXPathNodeSetCreate(NULL
);
12172 * Traverse the axis and test the nodes.
12178 cur
= next(ctxt
, cur
);
12183 * QUESTION TODO: What does the "first" and "last" stuff do?
12185 if ((first
!= NULL
) && (*first
!= NULL
)) {
12188 if (((total
% 256) == 0) &&
12189 #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON
12190 (xmlXPathCmpNodesExt(*first
, cur
) >= 0))
12192 (xmlXPathCmpNodes(*first
, cur
) >= 0))
12198 if ((last
!= NULL
) && (*last
!= NULL
)) {
12201 if (((total
% 256) == 0) &&
12202 #ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON
12203 (xmlXPathCmpNodesExt(cur
, *last
) >= 0))
12205 (xmlXPathCmpNodes(cur
, *last
) >= 0))
12215 xmlGenericError(xmlGenericErrorContext
, " %s", cur
->name
);
12219 case NODE_TEST_NONE
:
12223 case NODE_TEST_TYPE
:
12225 * TODO: Don't we need to use
12226 * xmlXPathNodeSetAddNs() for namespace nodes here?
12227 * Surprisingly, some c14n tests fail, if we do this.
12229 if (type
== NODE_TYPE_NODE
) {
12230 switch (cur
->type
) {
12231 case XML_DOCUMENT_NODE
:
12232 case XML_HTML_DOCUMENT_NODE
:
12233 #ifdef LIBXML_DOCB_ENABLED
12234 case XML_DOCB_DOCUMENT_NODE
:
12236 case XML_ELEMENT_NODE
:
12237 case XML_ATTRIBUTE_NODE
:
12239 case XML_COMMENT_NODE
:
12240 case XML_CDATA_SECTION_NODE
:
12241 case XML_TEXT_NODE
:
12242 case XML_NAMESPACE_DECL
:
12248 } else if (cur
->type
== type
) {
12249 if (type
== XML_NAMESPACE_DECL
)
12253 } else if ((type
== NODE_TYPE_TEXT
) &&
12254 (cur
->type
== XML_CDATA_SECTION_NODE
))
12260 if ((cur
->type
== XML_PI_NODE
) &&
12261 ((name
== NULL
) || xmlStrEqual(name
, cur
->name
)))
12266 case NODE_TEST_ALL
:
12267 if (axis
== AXIS_ATTRIBUTE
) {
12268 if (cur
->type
== XML_ATTRIBUTE_NODE
)
12272 } else if (axis
== AXIS_NAMESPACE
) {
12273 if (cur
->type
== XML_NAMESPACE_DECL
)
12278 if (cur
->type
== XML_ELEMENT_NODE
) {
12279 if (prefix
== NULL
)
12283 } else if ((cur
->ns
!= NULL
) &&
12284 (xmlStrEqual(URI
, cur
->ns
->href
)))
12291 case NODE_TEST_NS
:{
12295 case NODE_TEST_NAME
:
12296 if (axis
== AXIS_ATTRIBUTE
) {
12297 if (cur
->type
!= XML_ATTRIBUTE_NODE
)
12299 } else if (axis
== AXIS_NAMESPACE
) {
12300 if (cur
->type
!= XML_NAMESPACE_DECL
)
12303 if (cur
->type
!= XML_ELEMENT_NODE
)
12306 switch (cur
->type
) {
12307 case XML_ELEMENT_NODE
:
12308 if (xmlStrEqual(name
, cur
->name
)) {
12309 if (prefix
== NULL
) {
12310 if (cur
->ns
== NULL
)
12315 if ((cur
->ns
!= NULL
) &&
12316 (xmlStrEqual(URI
, cur
->ns
->href
)))
12323 case XML_ATTRIBUTE_NODE
:{
12324 xmlAttrPtr attr
= (xmlAttrPtr
) cur
;
12326 if (xmlStrEqual(name
, attr
->name
)) {
12327 if (prefix
== NULL
) {
12328 if ((attr
->ns
== NULL
) ||
12329 (attr
->ns
->prefix
== NULL
))
12334 if ((attr
->ns
!= NULL
) &&
12344 case XML_NAMESPACE_DECL
:
12345 if (cur
->type
== XML_NAMESPACE_DECL
) {
12346 xmlNsPtr ns
= (xmlNsPtr
) cur
;
12348 if ((ns
->prefix
!= NULL
) && (name
!= NULL
)
12349 && (xmlStrEqual(ns
->prefix
, name
)))
12359 } /* switch(test) */
12360 } while (cur
!= NULL
);
12362 goto apply_predicates
;
12364 axis_range_end
: /* ----------------------------------------------------- */
12366 * We have a "/foo[n]", and position() = n was reached.
12367 * Note that we can have as well "/foo/::parent::foo[1]", so
12368 * a duplicate-aware merge is still needed.
12369 * Merge with the result.
12371 if (outSeq
== NULL
) {
12375 outSeq
= mergeAndClear(outSeq
, seq
, 0);
12377 * Break if only a true/false result was requested.
12383 first_hit
: /* ---------------------------------------------------------- */
12385 * Break if only a true/false result was requested and
12386 * no predicates existed and a node test succeeded.
12388 if (outSeq
== NULL
) {
12392 outSeq
= mergeAndClear(outSeq
, seq
, 0);
12397 nbMatches
+= seq
->nodeNr
;
12400 apply_predicates
: /* --------------------------------------------------- */
12402 * Apply predicates.
12404 if ((predOp
!= NULL
) && (seq
->nodeNr
> 0)) {
12406 * E.g. when we have a "/foo[some expression][n]".
12409 * QUESTION TODO: The old predicate evaluation took into
12410 * account location-sets.
12411 * (E.g. ctxt->value->type == XPATH_LOCATIONSET)
12412 * Do we expect such a set here?
12413 * All what I learned now from the evaluation semantics
12414 * does not indicate that a location-set will be processed
12415 * here, so this looks OK.
12418 * Iterate over all predicates, starting with the outermost
12420 * TODO: Problem: we cannot execute the inner predicates first
12421 * since we cannot go back *up* the operator tree!
12423 * 1) Use of recursive functions (like is it currently done
12424 * via xmlXPathCompOpEval())
12425 * 2) Add a predicate evaluation information stack to the
12427 * 3) Change the way the operators are linked; we need a
12428 * "parent" field on xmlXPathStepOp
12430 * For the moment, I'll try to solve this with a recursive
12431 * function: xmlXPathCompOpEvalPredicate().
12433 size
= seq
->nodeNr
;
12434 if (hasPredicateRange
!= 0)
12435 newSize
= xmlXPathCompOpEvalPositionalPredicate(ctxt
,
12436 predOp
, seq
, size
, maxPos
, maxPos
, hasNsNodes
);
12438 newSize
= xmlXPathCompOpEvalPredicate(ctxt
,
12439 predOp
, seq
, size
, hasNsNodes
);
12441 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
12446 * Add the filtered set of nodes to the result node set.
12448 if (newSize
== 0) {
12450 * The predicates filtered all nodes out.
12452 xmlXPathNodeSetClear(seq
, hasNsNodes
);
12453 } else if (seq
->nodeNr
> 0) {
12455 * Add to result set.
12457 if (outSeq
== NULL
) {
12458 if (size
!= newSize
) {
12460 * We need to merge and clear here, since
12461 * the sequence will contained NULLed entries.
12463 outSeq
= mergeAndClear(NULL
, seq
, 1);
12469 outSeq
= mergeAndClear(outSeq
, seq
,
12470 (size
!= newSize
) ? 1: 0);
12472 * Break if only a true/false result was requested.
12477 } else if (seq
->nodeNr
> 0) {
12479 * Add to result set.
12481 if (outSeq
== NULL
) {
12485 outSeq
= mergeAndClear(outSeq
, seq
, 0);
12491 if ((obj
->boolval
) && (obj
->user
!= NULL
)) {
12493 * QUESTION TODO: What does this do and why?
12494 * TODO: Do we have to do this also for the "error"
12495 * cleanup further down?
12497 ctxt
->value
->boolval
= 1;
12498 ctxt
->value
->user
= obj
->user
;
12502 xmlXPathReleaseObject(xpctxt
, obj
);
12505 * Ensure we return at least an emtpy set.
12507 if (outSeq
== NULL
) {
12508 if ((seq
!= NULL
) && (seq
->nodeNr
== 0))
12511 outSeq
= xmlXPathNodeSetCreate(NULL
);
12512 /* XXX what if xmlXPathNodeSetCreate returned NULL here? */
12514 if ((seq
!= NULL
) && (seq
!= outSeq
)) {
12515 xmlXPathFreeNodeSet(seq
);
12518 * Hand over the result. Better to push the set also in
12521 valuePush(ctxt
, xmlXPathCacheWrapNodeSet(xpctxt
, outSeq
));
12523 * Reset the context node.
12525 xpctxt
->node
= oldContextNode
;
12528 xmlGenericError(xmlGenericErrorContext
,
12529 "\nExamined %d nodes, found %d nodes at that step\n",
12537 xmlXPathCompOpEvalFilterFirst(xmlXPathParserContextPtr ctxt
,
12538 xmlXPathStepOpPtr op
, xmlNodePtr
* first
);
12541 * xmlXPathCompOpEvalFirst:
12542 * @ctxt: the XPath parser context with the compiled expression
12543 * @op: an XPath compiled operation
12544 * @first: the first elem found so far
12546 * Evaluate the Precompiled XPath operation searching only the first
12547 * element in document order
12549 * Returns the number of examined objects.
12552 xmlXPathCompOpEvalFirst(xmlXPathParserContextPtr ctxt
,
12553 xmlXPathStepOpPtr op
, xmlNodePtr
* first
)
12555 int total
= 0, cur
;
12556 xmlXPathCompExprPtr comp
;
12557 xmlXPathObjectPtr arg1
, arg2
;
12564 case XPATH_OP_UNION
:
12566 xmlXPathCompOpEvalFirst(ctxt
, &comp
->steps
[op
->ch1
],
12569 if ((ctxt
->value
!= NULL
)
12570 && (ctxt
->value
->type
== XPATH_NODESET
)
12571 && (ctxt
->value
->nodesetval
!= NULL
)
12572 && (ctxt
->value
->nodesetval
->nodeNr
>= 1)) {
12574 * limit tree traversing to first node in the result
12577 * OPTIMIZE TODO: This implicitely sorts
12578 * the result, even if not needed. E.g. if the argument
12579 * of the count() function, no sorting is needed.
12580 * OPTIMIZE TODO: How do we know if the node-list wasn't
12583 if (ctxt
->value
->nodesetval
->nodeNr
> 1)
12584 xmlXPathNodeSetSort(ctxt
->value
->nodesetval
);
12585 *first
= ctxt
->value
->nodesetval
->nodeTab
[0];
12588 xmlXPathCompOpEvalFirst(ctxt
, &comp
->steps
[op
->ch2
],
12591 CHECK_TYPE0(XPATH_NODESET
);
12592 arg2
= valuePop(ctxt
);
12594 CHECK_TYPE0(XPATH_NODESET
);
12595 arg1
= valuePop(ctxt
);
12597 arg1
->nodesetval
= xmlXPathNodeSetMerge(arg1
->nodesetval
,
12599 valuePush(ctxt
, arg1
);
12600 xmlXPathReleaseObject(ctxt
->context
, arg2
);
12603 xmlXPathCompSwap(op
);
12604 return (total
+ cur
);
12605 case XPATH_OP_ROOT
:
12606 xmlXPathRoot(ctxt
);
12608 case XPATH_OP_NODE
:
12610 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
12613 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
12615 valuePush(ctxt
, xmlXPathCacheNewNodeSet(ctxt
->context
,
12616 ctxt
->context
->node
));
12618 case XPATH_OP_RESET
:
12620 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
12623 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
12625 ctxt
->context
->node
= NULL
;
12627 case XPATH_OP_COLLECT
:{
12631 total
= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
12634 total
+= xmlXPathNodeCollectAndTest(ctxt
, op
, first
, NULL
, 0);
12637 case XPATH_OP_VALUE
:
12639 xmlXPathCacheObjectCopy(ctxt
->context
,
12640 (xmlXPathObjectPtr
) op
->value4
));
12642 case XPATH_OP_SORT
:
12645 xmlXPathCompOpEvalFirst(ctxt
, &comp
->steps
[op
->ch1
],
12648 if ((ctxt
->value
!= NULL
)
12649 && (ctxt
->value
->type
== XPATH_NODESET
)
12650 && (ctxt
->value
->nodesetval
!= NULL
)
12651 && (ctxt
->value
->nodesetval
->nodeNr
> 1))
12652 xmlXPathNodeSetSort(ctxt
->value
->nodesetval
);
12654 #ifdef XP_OPTIMIZED_FILTER_FIRST
12655 case XPATH_OP_FILTER
:
12656 total
=+ xmlXPathCompOpEvalFilterFirst(ctxt
, op
, first
);
12660 return (xmlXPathCompOpEval(ctxt
, op
));
12665 * xmlXPathCompOpEvalLast:
12666 * @ctxt: the XPath parser context with the compiled expression
12667 * @op: an XPath compiled operation
12668 * @last: the last elem found so far
12670 * Evaluate the Precompiled XPath operation searching only the last
12671 * element in document order
12673 * Returns the number of nodes traversed
12676 xmlXPathCompOpEvalLast(xmlXPathParserContextPtr ctxt
, xmlXPathStepOpPtr op
,
12679 int total
= 0, cur
;
12680 xmlXPathCompExprPtr comp
;
12681 xmlXPathObjectPtr arg1
, arg2
;
12692 case XPATH_OP_UNION
:
12693 bakd
= ctxt
->context
->doc
;
12694 bak
= ctxt
->context
->node
;
12695 pp
= ctxt
->context
->proximityPosition
;
12696 cs
= ctxt
->context
->contextSize
;
12698 xmlXPathCompOpEvalLast(ctxt
, &comp
->steps
[op
->ch1
], last
);
12700 if ((ctxt
->value
!= NULL
)
12701 && (ctxt
->value
->type
== XPATH_NODESET
)
12702 && (ctxt
->value
->nodesetval
!= NULL
)
12703 && (ctxt
->value
->nodesetval
->nodeNr
>= 1)) {
12705 * limit tree traversing to first node in the result
12707 if (ctxt
->value
->nodesetval
->nodeNr
> 1)
12708 xmlXPathNodeSetSort(ctxt
->value
->nodesetval
);
12710 ctxt
->value
->nodesetval
->nodeTab
[ctxt
->value
->
12711 nodesetval
->nodeNr
-
12714 ctxt
->context
->doc
= bakd
;
12715 ctxt
->context
->node
= bak
;
12716 ctxt
->context
->proximityPosition
= pp
;
12717 ctxt
->context
->contextSize
= cs
;
12719 xmlXPathCompOpEvalLast(ctxt
, &comp
->steps
[op
->ch2
], last
);
12721 if ((ctxt
->value
!= NULL
)
12722 && (ctxt
->value
->type
== XPATH_NODESET
)
12723 && (ctxt
->value
->nodesetval
!= NULL
)
12724 && (ctxt
->value
->nodesetval
->nodeNr
>= 1)) { /* TODO: NOP ? */
12726 CHECK_TYPE0(XPATH_NODESET
);
12727 arg2
= valuePop(ctxt
);
12729 CHECK_TYPE0(XPATH_NODESET
);
12730 arg1
= valuePop(ctxt
);
12732 arg1
->nodesetval
= xmlXPathNodeSetMerge(arg1
->nodesetval
,
12734 valuePush(ctxt
, arg1
);
12735 xmlXPathReleaseObject(ctxt
->context
, arg2
);
12738 xmlXPathCompSwap(op
);
12739 return (total
+ cur
);
12740 case XPATH_OP_ROOT
:
12741 xmlXPathRoot(ctxt
);
12743 case XPATH_OP_NODE
:
12745 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
12748 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
12750 valuePush(ctxt
, xmlXPathCacheNewNodeSet(ctxt
->context
,
12751 ctxt
->context
->node
));
12753 case XPATH_OP_RESET
:
12755 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
12758 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
12760 ctxt
->context
->node
= NULL
;
12762 case XPATH_OP_COLLECT
:{
12766 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
12769 total
+= xmlXPathNodeCollectAndTest(ctxt
, op
, NULL
, last
, 0);
12772 case XPATH_OP_VALUE
:
12774 xmlXPathCacheObjectCopy(ctxt
->context
,
12775 (xmlXPathObjectPtr
) op
->value4
));
12777 case XPATH_OP_SORT
:
12780 xmlXPathCompOpEvalLast(ctxt
, &comp
->steps
[op
->ch1
],
12783 if ((ctxt
->value
!= NULL
)
12784 && (ctxt
->value
->type
== XPATH_NODESET
)
12785 && (ctxt
->value
->nodesetval
!= NULL
)
12786 && (ctxt
->value
->nodesetval
->nodeNr
> 1))
12787 xmlXPathNodeSetSort(ctxt
->value
->nodesetval
);
12790 return (xmlXPathCompOpEval(ctxt
, op
));
12794 #ifdef XP_OPTIMIZED_FILTER_FIRST
12796 xmlXPathCompOpEvalFilterFirst(xmlXPathParserContextPtr ctxt
,
12797 xmlXPathStepOpPtr op
, xmlNodePtr
* first
)
12800 xmlXPathCompExprPtr comp
;
12801 xmlXPathObjectPtr res
;
12802 xmlXPathObjectPtr obj
;
12803 xmlNodeSetPtr oldset
;
12804 xmlNodePtr oldnode
;
12811 * Optimization for ()[last()] selection i.e. the last elem
12813 if ((op
->ch1
!= -1) && (op
->ch2
!= -1) &&
12814 (comp
->steps
[op
->ch1
].op
== XPATH_OP_SORT
) &&
12815 (comp
->steps
[op
->ch2
].op
== XPATH_OP_SORT
)) {
12816 int f
= comp
->steps
[op
->ch2
].ch1
;
12819 (comp
->steps
[f
].op
== XPATH_OP_FUNCTION
) &&
12820 (comp
->steps
[f
].value5
== NULL
) &&
12821 (comp
->steps
[f
].value
== 0) &&
12822 (comp
->steps
[f
].value4
!= NULL
) &&
12824 (comp
->steps
[f
].value4
, BAD_CAST
"last"))) {
12825 xmlNodePtr last
= NULL
;
12828 xmlXPathCompOpEvalLast(ctxt
,
12829 &comp
->steps
[op
->ch1
],
12833 * The nodeset should be in document order,
12834 * Keep only the last value
12836 if ((ctxt
->value
!= NULL
) &&
12837 (ctxt
->value
->type
== XPATH_NODESET
) &&
12838 (ctxt
->value
->nodesetval
!= NULL
) &&
12839 (ctxt
->value
->nodesetval
->nodeTab
!= NULL
) &&
12840 (ctxt
->value
->nodesetval
->nodeNr
> 1)) {
12841 ctxt
->value
->nodesetval
->nodeTab
[0] =
12842 ctxt
->value
->nodesetval
->nodeTab
[ctxt
->
12847 ctxt
->value
->nodesetval
->nodeNr
= 1;
12848 *first
= *(ctxt
->value
->nodesetval
->nodeTab
);
12855 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
12859 if (ctxt
->value
== NULL
)
12862 #ifdef LIBXML_XPTR_ENABLED
12863 oldnode
= ctxt
->context
->node
;
12865 * Hum are we filtering the result of an XPointer expression
12867 if (ctxt
->value
->type
== XPATH_LOCATIONSET
) {
12868 xmlXPathObjectPtr tmp
= NULL
;
12869 xmlLocationSetPtr newlocset
= NULL
;
12870 xmlLocationSetPtr oldlocset
;
12873 * Extract the old locset, and then evaluate the result of the
12874 * expression for all the element in the locset. use it to grow
12877 CHECK_TYPE0(XPATH_LOCATIONSET
);
12878 obj
= valuePop(ctxt
);
12879 oldlocset
= obj
->user
;
12880 ctxt
->context
->node
= NULL
;
12882 if ((oldlocset
== NULL
) || (oldlocset
->locNr
== 0)) {
12883 ctxt
->context
->contextSize
= 0;
12884 ctxt
->context
->proximityPosition
= 0;
12886 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
12887 res
= valuePop(ctxt
);
12889 xmlXPathReleaseObject(ctxt
->context
, res
);
12891 valuePush(ctxt
, obj
);
12895 newlocset
= xmlXPtrLocationSetCreate(NULL
);
12897 for (i
= 0; i
< oldlocset
->locNr
; i
++) {
12899 * Run the evaluation with a node list made of a
12900 * single item in the nodelocset.
12902 ctxt
->context
->node
= oldlocset
->locTab
[i
]->user
;
12903 ctxt
->context
->contextSize
= oldlocset
->locNr
;
12904 ctxt
->context
->proximityPosition
= i
+ 1;
12906 tmp
= xmlXPathCacheNewNodeSet(ctxt
->context
,
12907 ctxt
->context
->node
);
12909 xmlXPathNodeSetAddUnique(tmp
->nodesetval
,
12910 ctxt
->context
->node
);
12912 valuePush(ctxt
, tmp
);
12914 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
12915 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
12916 xmlXPathFreeObject(obj
);
12920 * The result of the evaluation need to be tested to
12921 * decided whether the filter succeeded or not
12923 res
= valuePop(ctxt
);
12924 if (xmlXPathEvaluatePredicateResult(ctxt
, res
)) {
12925 xmlXPtrLocationSetAdd(newlocset
,
12926 xmlXPathCacheObjectCopy(ctxt
->context
,
12927 oldlocset
->locTab
[i
]));
12933 xmlXPathReleaseObject(ctxt
->context
, res
);
12935 if (ctxt
->value
== tmp
) {
12937 xmlXPathNodeSetClear(tmp
->nodesetval
, 1);
12939 * REVISIT TODO: Don't create a temporary nodeset
12940 * for everly iteration.
12942 /* OLD: xmlXPathFreeObject(res); */
12945 ctxt
->context
->node
= NULL
;
12947 * Only put the first node in the result, then leave.
12949 if (newlocset
->locNr
> 0) {
12950 *first
= (xmlNodePtr
) oldlocset
->locTab
[i
]->user
;
12955 xmlXPathReleaseObject(ctxt
->context
, tmp
);
12958 * The result is used as the new evaluation locset.
12960 xmlXPathReleaseObject(ctxt
->context
, obj
);
12961 ctxt
->context
->node
= NULL
;
12962 ctxt
->context
->contextSize
= -1;
12963 ctxt
->context
->proximityPosition
= -1;
12964 valuePush(ctxt
, xmlXPtrWrapLocationSet(newlocset
));
12965 ctxt
->context
->node
= oldnode
;
12968 #endif /* LIBXML_XPTR_ENABLED */
12971 * Extract the old set, and then evaluate the result of the
12972 * expression for all the element in the set. use it to grow
12975 CHECK_TYPE0(XPATH_NODESET
);
12976 obj
= valuePop(ctxt
);
12977 oldset
= obj
->nodesetval
;
12979 oldnode
= ctxt
->context
->node
;
12980 oldDoc
= ctxt
->context
->doc
;
12981 ctxt
->context
->node
= NULL
;
12983 if ((oldset
== NULL
) || (oldset
->nodeNr
== 0)) {
12984 ctxt
->context
->contextSize
= 0;
12985 ctxt
->context
->proximityPosition
= 0;
12986 /* QUESTION TODO: Why was this code commented out?
12989 xmlXPathCompOpEval(ctxt,
12990 &comp->steps[op->ch2]);
12992 res = valuePop(ctxt);
12994 xmlXPathFreeObject(res);
12996 valuePush(ctxt
, obj
);
12997 ctxt
->context
->node
= oldnode
;
13000 xmlNodeSetPtr newset
;
13001 xmlXPathObjectPtr tmp
= NULL
;
13003 * Initialize the new set.
13004 * Also set the xpath document in case things like
13005 * key() evaluation are attempted on the predicate
13007 newset
= xmlXPathNodeSetCreate(NULL
);
13008 /* XXX what if xmlXPathNodeSetCreate returned NULL? */
13010 for (i
= 0; i
< oldset
->nodeNr
; i
++) {
13012 * Run the evaluation with a node list made of
13013 * a single item in the nodeset.
13015 ctxt
->context
->node
= oldset
->nodeTab
[i
];
13016 if ((oldset
->nodeTab
[i
]->type
!= XML_NAMESPACE_DECL
) &&
13017 (oldset
->nodeTab
[i
]->doc
!= NULL
))
13018 ctxt
->context
->doc
= oldset
->nodeTab
[i
]->doc
;
13020 tmp
= xmlXPathCacheNewNodeSet(ctxt
->context
,
13021 ctxt
->context
->node
);
13023 xmlXPathNodeSetAddUnique(tmp
->nodesetval
,
13024 ctxt
->context
->node
);
13026 valuePush(ctxt
, tmp
);
13027 ctxt
->context
->contextSize
= oldset
->nodeNr
;
13028 ctxt
->context
->proximityPosition
= i
+ 1;
13030 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
13031 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
13032 xmlXPathFreeNodeSet(newset
);
13033 xmlXPathFreeObject(obj
);
13037 * The result of the evaluation needs to be tested to
13038 * decide whether the filter succeeded or not
13040 res
= valuePop(ctxt
);
13041 if (xmlXPathEvaluatePredicateResult(ctxt
, res
)) {
13042 xmlXPathNodeSetAdd(newset
, oldset
->nodeTab
[i
]);
13048 xmlXPathReleaseObject(ctxt
->context
, res
);
13050 if (ctxt
->value
== tmp
) {
13053 * Don't free the temporary nodeset
13054 * in order to avoid massive recreation inside this
13057 xmlXPathNodeSetClear(tmp
->nodesetval
, 1);
13060 ctxt
->context
->node
= NULL
;
13062 * Only put the first node in the result, then leave.
13064 if (newset
->nodeNr
> 0) {
13065 *first
= *(newset
->nodeTab
);
13070 xmlXPathReleaseObject(ctxt
->context
, tmp
);
13073 * The result is used as the new evaluation set.
13075 xmlXPathReleaseObject(ctxt
->context
, obj
);
13076 ctxt
->context
->node
= NULL
;
13077 ctxt
->context
->contextSize
= -1;
13078 ctxt
->context
->proximityPosition
= -1;
13079 /* may want to move this past the '}' later */
13080 ctxt
->context
->doc
= oldDoc
;
13081 valuePush(ctxt
, xmlXPathCacheWrapNodeSet(ctxt
->context
, newset
));
13083 ctxt
->context
->node
= oldnode
;
13086 #endif /* XP_OPTIMIZED_FILTER_FIRST */
13089 * xmlXPathCompOpEval:
13090 * @ctxt: the XPath parser context with the compiled expression
13091 * @op: an XPath compiled operation
13093 * Evaluate the Precompiled XPath operation
13094 * Returns the number of nodes traversed
13097 xmlXPathCompOpEval(xmlXPathParserContextPtr ctxt
, xmlXPathStepOpPtr op
)
13101 xmlXPathCompExprPtr comp
;
13102 xmlXPathObjectPtr arg1
, arg2
;
13114 bakd
= ctxt
->context
->doc
;
13115 bak
= ctxt
->context
->node
;
13116 pp
= ctxt
->context
->proximityPosition
;
13117 cs
= ctxt
->context
->contextSize
;
13118 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13120 xmlXPathBooleanFunction(ctxt
, 1);
13121 if ((ctxt
->value
== NULL
) || (ctxt
->value
->boolval
== 0))
13123 arg2
= valuePop(ctxt
);
13124 ctxt
->context
->doc
= bakd
;
13125 ctxt
->context
->node
= bak
;
13126 ctxt
->context
->proximityPosition
= pp
;
13127 ctxt
->context
->contextSize
= cs
;
13128 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
13130 xmlXPathFreeObject(arg2
);
13133 xmlXPathBooleanFunction(ctxt
, 1);
13134 arg1
= valuePop(ctxt
);
13135 arg1
->boolval
&= arg2
->boolval
;
13136 valuePush(ctxt
, arg1
);
13137 xmlXPathReleaseObject(ctxt
->context
, arg2
);
13140 bakd
= ctxt
->context
->doc
;
13141 bak
= ctxt
->context
->node
;
13142 pp
= ctxt
->context
->proximityPosition
;
13143 cs
= ctxt
->context
->contextSize
;
13144 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13146 xmlXPathBooleanFunction(ctxt
, 1);
13147 if ((ctxt
->value
== NULL
) || (ctxt
->value
->boolval
== 1))
13149 arg2
= valuePop(ctxt
);
13150 ctxt
->context
->doc
= bakd
;
13151 ctxt
->context
->node
= bak
;
13152 ctxt
->context
->proximityPosition
= pp
;
13153 ctxt
->context
->contextSize
= cs
;
13154 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
13156 xmlXPathFreeObject(arg2
);
13159 xmlXPathBooleanFunction(ctxt
, 1);
13160 arg1
= valuePop(ctxt
);
13161 arg1
->boolval
|= arg2
->boolval
;
13162 valuePush(ctxt
, arg1
);
13163 xmlXPathReleaseObject(ctxt
->context
, arg2
);
13165 case XPATH_OP_EQUAL
:
13166 bakd
= ctxt
->context
->doc
;
13167 bak
= ctxt
->context
->node
;
13168 pp
= ctxt
->context
->proximityPosition
;
13169 cs
= ctxt
->context
->contextSize
;
13170 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13172 ctxt
->context
->doc
= bakd
;
13173 ctxt
->context
->node
= bak
;
13174 ctxt
->context
->proximityPosition
= pp
;
13175 ctxt
->context
->contextSize
= cs
;
13176 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
13179 equal
= xmlXPathEqualValues(ctxt
);
13181 equal
= xmlXPathNotEqualValues(ctxt
);
13182 valuePush(ctxt
, xmlXPathCacheNewBoolean(ctxt
->context
, equal
));
13185 bakd
= ctxt
->context
->doc
;
13186 bak
= ctxt
->context
->node
;
13187 pp
= ctxt
->context
->proximityPosition
;
13188 cs
= ctxt
->context
->contextSize
;
13189 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13191 ctxt
->context
->doc
= bakd
;
13192 ctxt
->context
->node
= bak
;
13193 ctxt
->context
->proximityPosition
= pp
;
13194 ctxt
->context
->contextSize
= cs
;
13195 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
13197 ret
= xmlXPathCompareValues(ctxt
, op
->value
, op
->value2
);
13198 valuePush(ctxt
, xmlXPathCacheNewBoolean(ctxt
->context
, ret
));
13200 case XPATH_OP_PLUS
:
13201 bakd
= ctxt
->context
->doc
;
13202 bak
= ctxt
->context
->node
;
13203 pp
= ctxt
->context
->proximityPosition
;
13204 cs
= ctxt
->context
->contextSize
;
13205 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13207 if (op
->ch2
!= -1) {
13208 ctxt
->context
->doc
= bakd
;
13209 ctxt
->context
->node
= bak
;
13210 ctxt
->context
->proximityPosition
= pp
;
13211 ctxt
->context
->contextSize
= cs
;
13212 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
13215 if (op
->value
== 0)
13216 xmlXPathSubValues(ctxt
);
13217 else if (op
->value
== 1)
13218 xmlXPathAddValues(ctxt
);
13219 else if (op
->value
== 2)
13220 xmlXPathValueFlipSign(ctxt
);
13221 else if (op
->value
== 3) {
13223 CHECK_TYPE0(XPATH_NUMBER
);
13226 case XPATH_OP_MULT
:
13227 bakd
= ctxt
->context
->doc
;
13228 bak
= ctxt
->context
->node
;
13229 pp
= ctxt
->context
->proximityPosition
;
13230 cs
= ctxt
->context
->contextSize
;
13231 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13233 ctxt
->context
->doc
= bakd
;
13234 ctxt
->context
->node
= bak
;
13235 ctxt
->context
->proximityPosition
= pp
;
13236 ctxt
->context
->contextSize
= cs
;
13237 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
13239 if (op
->value
== 0)
13240 xmlXPathMultValues(ctxt
);
13241 else if (op
->value
== 1)
13242 xmlXPathDivValues(ctxt
);
13243 else if (op
->value
== 2)
13244 xmlXPathModValues(ctxt
);
13246 case XPATH_OP_UNION
:
13247 bakd
= ctxt
->context
->doc
;
13248 bak
= ctxt
->context
->node
;
13249 pp
= ctxt
->context
->proximityPosition
;
13250 cs
= ctxt
->context
->contextSize
;
13251 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13253 ctxt
->context
->doc
= bakd
;
13254 ctxt
->context
->node
= bak
;
13255 ctxt
->context
->proximityPosition
= pp
;
13256 ctxt
->context
->contextSize
= cs
;
13257 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
13259 CHECK_TYPE0(XPATH_NODESET
);
13260 arg2
= valuePop(ctxt
);
13262 CHECK_TYPE0(XPATH_NODESET
);
13263 arg1
= valuePop(ctxt
);
13265 if ((arg1
->nodesetval
== NULL
) ||
13266 ((arg2
->nodesetval
!= NULL
) &&
13267 (arg2
->nodesetval
->nodeNr
!= 0)))
13269 arg1
->nodesetval
= xmlXPathNodeSetMerge(arg1
->nodesetval
,
13273 valuePush(ctxt
, arg1
);
13274 xmlXPathReleaseObject(ctxt
->context
, arg2
);
13276 case XPATH_OP_ROOT
:
13277 xmlXPathRoot(ctxt
);
13279 case XPATH_OP_NODE
:
13281 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13284 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
13286 valuePush(ctxt
, xmlXPathCacheNewNodeSet(ctxt
->context
,
13287 ctxt
->context
->node
));
13289 case XPATH_OP_RESET
:
13291 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13294 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
13296 ctxt
->context
->node
= NULL
;
13298 case XPATH_OP_COLLECT
:{
13302 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13305 total
+= xmlXPathNodeCollectAndTest(ctxt
, op
, NULL
, NULL
, 0);
13308 case XPATH_OP_VALUE
:
13310 xmlXPathCacheObjectCopy(ctxt
->context
,
13311 (xmlXPathObjectPtr
) op
->value4
));
13313 case XPATH_OP_VARIABLE
:{
13314 xmlXPathObjectPtr val
;
13318 xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13319 if (op
->value5
== NULL
) {
13320 val
= xmlXPathVariableLookup(ctxt
->context
, op
->value4
);
13322 ctxt
->error
= XPATH_UNDEF_VARIABLE_ERROR
;
13325 valuePush(ctxt
, val
);
13327 const xmlChar
*URI
;
13329 URI
= xmlXPathNsLookup(ctxt
->context
, op
->value5
);
13331 xmlGenericError(xmlGenericErrorContext
,
13332 "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n",
13333 (char *) op
->value4
, (char *)op
->value5
);
13336 val
= xmlXPathVariableLookupNS(ctxt
->context
,
13339 ctxt
->error
= XPATH_UNDEF_VARIABLE_ERROR
;
13342 valuePush(ctxt
, val
);
13346 case XPATH_OP_FUNCTION
:{
13347 xmlXPathFunction func
;
13348 const xmlChar
*oldFunc
, *oldFuncURI
;
13353 xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13354 if (ctxt
->valueNr
< op
->value
) {
13355 xmlGenericError(xmlGenericErrorContext
,
13356 "xmlXPathCompOpEval: parameter error\n");
13357 ctxt
->error
= XPATH_INVALID_OPERAND
;
13360 for (i
= 0; i
< op
->value
; i
++)
13361 if (ctxt
->valueTab
[(ctxt
->valueNr
- 1) - i
] == NULL
) {
13362 xmlGenericError(xmlGenericErrorContext
,
13363 "xmlXPathCompOpEval: parameter error\n");
13364 ctxt
->error
= XPATH_INVALID_OPERAND
;
13367 if (op
->cache
!= NULL
)
13368 XML_CAST_FPTR(func
) = op
->cache
;
13370 const xmlChar
*URI
= NULL
;
13372 if (op
->value5
== NULL
)
13374 xmlXPathFunctionLookup(ctxt
->context
,
13377 URI
= xmlXPathNsLookup(ctxt
->context
, op
->value5
);
13379 xmlGenericError(xmlGenericErrorContext
,
13380 "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n",
13381 (char *)op
->value4
, (char *)op
->value5
);
13384 func
= xmlXPathFunctionLookupNS(ctxt
->context
,
13387 if (func
== NULL
) {
13388 xmlGenericError(xmlGenericErrorContext
,
13389 "xmlXPathCompOpEval: function %s not found\n",
13390 (char *)op
->value4
);
13391 XP_ERROR0(XPATH_UNKNOWN_FUNC_ERROR
);
13393 op
->cache
= XML_CAST_FPTR(func
);
13394 op
->cacheURI
= (void *) URI
;
13396 oldFunc
= ctxt
->context
->function
;
13397 oldFuncURI
= ctxt
->context
->functionURI
;
13398 ctxt
->context
->function
= op
->value4
;
13399 ctxt
->context
->functionURI
= op
->cacheURI
;
13400 func(ctxt
, op
->value
);
13401 ctxt
->context
->function
= oldFunc
;
13402 ctxt
->context
->functionURI
= oldFuncURI
;
13406 bakd
= ctxt
->context
->doc
;
13407 bak
= ctxt
->context
->node
;
13408 pp
= ctxt
->context
->proximityPosition
;
13409 cs
= ctxt
->context
->contextSize
;
13411 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13412 ctxt
->context
->contextSize
= cs
;
13413 ctxt
->context
->proximityPosition
= pp
;
13414 ctxt
->context
->node
= bak
;
13415 ctxt
->context
->doc
= bakd
;
13417 if (op
->ch2
!= -1) {
13418 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch2
]);
13419 ctxt
->context
->doc
= bakd
;
13420 ctxt
->context
->node
= bak
;
13424 case XPATH_OP_PREDICATE
:
13425 case XPATH_OP_FILTER
:{
13426 xmlXPathObjectPtr res
;
13427 xmlXPathObjectPtr obj
, tmp
;
13428 xmlNodeSetPtr newset
= NULL
;
13429 xmlNodeSetPtr oldset
;
13430 xmlNodePtr oldnode
;
13435 * Optimization for ()[1] selection i.e. the first elem
13437 if ((op
->ch1
!= -1) && (op
->ch2
!= -1) &&
13438 #ifdef XP_OPTIMIZED_FILTER_FIRST
13440 * FILTER TODO: Can we assume that the inner processing
13441 * will result in an ordered list if we have an
13443 * What about an additional field or flag on
13444 * xmlXPathObject like @sorted ? This way we wouln'd need
13445 * to assume anything, so it would be more robust and
13446 * easier to optimize.
13448 ((comp
->steps
[op
->ch1
].op
== XPATH_OP_SORT
) || /* 18 */
13449 (comp
->steps
[op
->ch1
].op
== XPATH_OP_FILTER
)) && /* 17 */
13451 (comp
->steps
[op
->ch1
].op
== XPATH_OP_SORT
) &&
13453 (comp
->steps
[op
->ch2
].op
== XPATH_OP_VALUE
)) { /* 12 */
13454 xmlXPathObjectPtr val
;
13456 val
= comp
->steps
[op
->ch2
].value4
;
13457 if ((val
!= NULL
) && (val
->type
== XPATH_NUMBER
) &&
13458 (val
->floatval
== 1.0)) {
13459 xmlNodePtr first
= NULL
;
13462 xmlXPathCompOpEvalFirst(ctxt
,
13463 &comp
->steps
[op
->ch1
],
13467 * The nodeset should be in document order,
13468 * Keep only the first value
13470 if ((ctxt
->value
!= NULL
) &&
13471 (ctxt
->value
->type
== XPATH_NODESET
) &&
13472 (ctxt
->value
->nodesetval
!= NULL
) &&
13473 (ctxt
->value
->nodesetval
->nodeNr
> 1))
13474 ctxt
->value
->nodesetval
->nodeNr
= 1;
13479 * Optimization for ()[last()] selection i.e. the last elem
13481 if ((op
->ch1
!= -1) && (op
->ch2
!= -1) &&
13482 (comp
->steps
[op
->ch1
].op
== XPATH_OP_SORT
) &&
13483 (comp
->steps
[op
->ch2
].op
== XPATH_OP_SORT
)) {
13484 int f
= comp
->steps
[op
->ch2
].ch1
;
13487 (comp
->steps
[f
].op
== XPATH_OP_FUNCTION
) &&
13488 (comp
->steps
[f
].value5
== NULL
) &&
13489 (comp
->steps
[f
].value
== 0) &&
13490 (comp
->steps
[f
].value4
!= NULL
) &&
13492 (comp
->steps
[f
].value4
, BAD_CAST
"last"))) {
13493 xmlNodePtr last
= NULL
;
13496 xmlXPathCompOpEvalLast(ctxt
,
13497 &comp
->steps
[op
->ch1
],
13501 * The nodeset should be in document order,
13502 * Keep only the last value
13504 if ((ctxt
->value
!= NULL
) &&
13505 (ctxt
->value
->type
== XPATH_NODESET
) &&
13506 (ctxt
->value
->nodesetval
!= NULL
) &&
13507 (ctxt
->value
->nodesetval
->nodeTab
!= NULL
) &&
13508 (ctxt
->value
->nodesetval
->nodeNr
> 1)) {
13509 ctxt
->value
->nodesetval
->nodeTab
[0] =
13510 ctxt
->value
->nodesetval
->nodeTab
[ctxt
->
13515 ctxt
->value
->nodesetval
->nodeNr
= 1;
13521 * Process inner predicates first.
13522 * Example "index[parent::book][1]":
13524 * PREDICATE <-- we are here "[1]"
13525 * PREDICATE <-- process "[parent::book]" first
13527 * COLLECT 'parent' 'name' 'node' book
13529 * ELEM Object is a number : 1
13533 xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13537 if (ctxt
->value
== NULL
)
13540 oldnode
= ctxt
->context
->node
;
13542 #ifdef LIBXML_XPTR_ENABLED
13544 * Hum are we filtering the result of an XPointer expression
13546 if (ctxt
->value
->type
== XPATH_LOCATIONSET
) {
13547 xmlLocationSetPtr newlocset
= NULL
;
13548 xmlLocationSetPtr oldlocset
;
13551 * Extract the old locset, and then evaluate the result of the
13552 * expression for all the element in the locset. use it to grow
13555 CHECK_TYPE0(XPATH_LOCATIONSET
);
13556 obj
= valuePop(ctxt
);
13557 oldlocset
= obj
->user
;
13558 ctxt
->context
->node
= NULL
;
13560 if ((oldlocset
== NULL
) || (oldlocset
->locNr
== 0)) {
13561 ctxt
->context
->contextSize
= 0;
13562 ctxt
->context
->proximityPosition
= 0;
13565 xmlXPathCompOpEval(ctxt
,
13566 &comp
->steps
[op
->ch2
]);
13567 res
= valuePop(ctxt
);
13569 xmlXPathReleaseObject(ctxt
->context
, res
);
13571 valuePush(ctxt
, obj
);
13575 newlocset
= xmlXPtrLocationSetCreate(NULL
);
13577 for (i
= 0; i
< oldlocset
->locNr
; i
++) {
13579 * Run the evaluation with a node list made of a
13580 * single item in the nodelocset.
13582 ctxt
->context
->node
= oldlocset
->locTab
[i
]->user
;
13583 ctxt
->context
->contextSize
= oldlocset
->locNr
;
13584 ctxt
->context
->proximityPosition
= i
+ 1;
13585 tmp
= xmlXPathCacheNewNodeSet(ctxt
->context
,
13586 ctxt
->context
->node
);
13587 valuePush(ctxt
, tmp
);
13591 xmlXPathCompOpEval(ctxt
,
13592 &comp
->steps
[op
->ch2
]);
13593 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
13594 xmlXPathFreeObject(obj
);
13599 * The result of the evaluation need to be tested to
13600 * decided whether the filter succeeded or not
13602 res
= valuePop(ctxt
);
13603 if (xmlXPathEvaluatePredicateResult(ctxt
, res
)) {
13604 xmlXPtrLocationSetAdd(newlocset
,
13606 (oldlocset
->locTab
[i
]));
13613 xmlXPathReleaseObject(ctxt
->context
, res
);
13615 if (ctxt
->value
== tmp
) {
13616 res
= valuePop(ctxt
);
13617 xmlXPathReleaseObject(ctxt
->context
, res
);
13620 ctxt
->context
->node
= NULL
;
13624 * The result is used as the new evaluation locset.
13626 xmlXPathReleaseObject(ctxt
->context
, obj
);
13627 ctxt
->context
->node
= NULL
;
13628 ctxt
->context
->contextSize
= -1;
13629 ctxt
->context
->proximityPosition
= -1;
13630 valuePush(ctxt
, xmlXPtrWrapLocationSet(newlocset
));
13631 ctxt
->context
->node
= oldnode
;
13634 #endif /* LIBXML_XPTR_ENABLED */
13637 * Extract the old set, and then evaluate the result of the
13638 * expression for all the element in the set. use it to grow
13641 CHECK_TYPE0(XPATH_NODESET
);
13642 obj
= valuePop(ctxt
);
13643 oldset
= obj
->nodesetval
;
13645 oldnode
= ctxt
->context
->node
;
13646 oldDoc
= ctxt
->context
->doc
;
13647 ctxt
->context
->node
= NULL
;
13649 if ((oldset
== NULL
) || (oldset
->nodeNr
== 0)) {
13650 ctxt
->context
->contextSize
= 0;
13651 ctxt
->context
->proximityPosition
= 0;
13655 xmlXPathCompOpEval(ctxt,
13656 &comp->steps[op->ch2]);
13658 res = valuePop(ctxt);
13660 xmlXPathFreeObject(res);
13662 valuePush(ctxt
, obj
);
13663 ctxt
->context
->node
= oldnode
;
13668 * Initialize the new set.
13669 * Also set the xpath document in case things like
13670 * key() evaluation are attempted on the predicate
13672 newset
= xmlXPathNodeSetCreate(NULL
);
13675 * "For each node in the node-set to be filtered, the
13676 * PredicateExpr is evaluated with that node as the
13677 * context node, with the number of nodes in the
13678 * node-set as the context size, and with the proximity
13679 * position of the node in the node-set with respect to
13680 * the axis as the context position;"
13681 * @oldset is the node-set" to be filtered.
13684 * "only predicates change the context position and
13685 * context size (see [2.4 Predicates])."
13687 * node-set context pos
13691 * After applying predicate [position() > 1] :
13692 * node-set context pos
13696 * removed the first node in the node-set, then
13697 * the context position of the
13699 for (i
= 0; i
< oldset
->nodeNr
; i
++) {
13701 * Run the evaluation with a node list made of
13702 * a single item in the nodeset.
13704 ctxt
->context
->node
= oldset
->nodeTab
[i
];
13705 if ((oldset
->nodeTab
[i
]->type
!= XML_NAMESPACE_DECL
) &&
13706 (oldset
->nodeTab
[i
]->doc
!= NULL
))
13707 ctxt
->context
->doc
= oldset
->nodeTab
[i
]->doc
;
13709 tmp
= xmlXPathCacheNewNodeSet(ctxt
->context
,
13710 ctxt
->context
->node
);
13712 xmlXPathNodeSetAddUnique(tmp
->nodesetval
,
13713 ctxt
->context
->node
);
13715 valuePush(ctxt
, tmp
);
13716 ctxt
->context
->contextSize
= oldset
->nodeNr
;
13717 ctxt
->context
->proximityPosition
= i
+ 1;
13719 * Evaluate the predicate against the context node.
13720 * Can/should we optimize position() predicates
13721 * here (e.g. "[1]")?
13725 xmlXPathCompOpEval(ctxt
,
13726 &comp
->steps
[op
->ch2
]);
13727 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
13728 xmlXPathFreeNodeSet(newset
);
13729 xmlXPathFreeObject(obj
);
13734 * The result of the evaluation needs to be tested to
13735 * decide whether the filter succeeded or not
13738 * OPTIMIZE TODO: Can we use
13739 * xmlXPathNodeSetAdd*Unique()* instead?
13741 res
= valuePop(ctxt
);
13742 if (xmlXPathEvaluatePredicateResult(ctxt
, res
)) {
13743 xmlXPathNodeSetAdd(newset
, oldset
->nodeTab
[i
]);
13750 xmlXPathReleaseObject(ctxt
->context
, res
);
13752 if (ctxt
->value
== tmp
) {
13754 xmlXPathNodeSetClear(tmp
->nodesetval
, 1);
13756 * Don't free the temporary nodeset
13757 * in order to avoid massive recreation inside this
13762 ctxt
->context
->node
= NULL
;
13765 xmlXPathReleaseObject(ctxt
->context
, tmp
);
13767 * The result is used as the new evaluation set.
13769 xmlXPathReleaseObject(ctxt
->context
, obj
);
13770 ctxt
->context
->node
= NULL
;
13771 ctxt
->context
->contextSize
= -1;
13772 ctxt
->context
->proximityPosition
= -1;
13773 /* may want to move this past the '}' later */
13774 ctxt
->context
->doc
= oldDoc
;
13776 xmlXPathCacheWrapNodeSet(ctxt
->context
, newset
));
13778 ctxt
->context
->node
= oldnode
;
13781 case XPATH_OP_SORT
:
13783 total
+= xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13785 if ((ctxt
->value
!= NULL
) &&
13786 (ctxt
->value
->type
== XPATH_NODESET
) &&
13787 (ctxt
->value
->nodesetval
!= NULL
) &&
13788 (ctxt
->value
->nodesetval
->nodeNr
> 1))
13790 xmlXPathNodeSetSort(ctxt
->value
->nodesetval
);
13793 #ifdef LIBXML_XPTR_ENABLED
13794 case XPATH_OP_RANGETO
:{
13795 xmlXPathObjectPtr range
;
13796 xmlXPathObjectPtr res
, obj
;
13797 xmlXPathObjectPtr tmp
;
13798 xmlLocationSetPtr newlocset
= NULL
;
13799 xmlLocationSetPtr oldlocset
;
13800 xmlNodeSetPtr oldset
;
13805 xmlXPathCompOpEval(ctxt
, &comp
->steps
[op
->ch1
]);
13809 if (ctxt
->value
->type
== XPATH_LOCATIONSET
) {
13811 * Extract the old locset, and then evaluate the result of the
13812 * expression for all the element in the locset. use it to grow
13815 CHECK_TYPE0(XPATH_LOCATIONSET
);
13816 obj
= valuePop(ctxt
);
13817 oldlocset
= obj
->user
;
13819 if ((oldlocset
== NULL
) || (oldlocset
->locNr
== 0)) {
13820 ctxt
->context
->node
= NULL
;
13821 ctxt
->context
->contextSize
= 0;
13822 ctxt
->context
->proximityPosition
= 0;
13823 total
+= xmlXPathCompOpEval(ctxt
,&comp
->steps
[op
->ch2
]);
13824 res
= valuePop(ctxt
);
13826 xmlXPathReleaseObject(ctxt
->context
, res
);
13828 valuePush(ctxt
, obj
);
13832 newlocset
= xmlXPtrLocationSetCreate(NULL
);
13834 for (i
= 0; i
< oldlocset
->locNr
; i
++) {
13836 * Run the evaluation with a node list made of a
13837 * single item in the nodelocset.
13839 ctxt
->context
->node
= oldlocset
->locTab
[i
]->user
;
13840 ctxt
->context
->contextSize
= oldlocset
->locNr
;
13841 ctxt
->context
->proximityPosition
= i
+ 1;
13842 tmp
= xmlXPathCacheNewNodeSet(ctxt
->context
,
13843 ctxt
->context
->node
);
13844 valuePush(ctxt
, tmp
);
13848 xmlXPathCompOpEval(ctxt
,
13849 &comp
->steps
[op
->ch2
]);
13850 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
13851 xmlXPathFreeObject(obj
);
13855 res
= valuePop(ctxt
);
13856 if (res
->type
== XPATH_LOCATIONSET
) {
13857 xmlLocationSetPtr rloc
=
13858 (xmlLocationSetPtr
)res
->user
;
13859 for (j
=0; j
<rloc
->locNr
; j
++) {
13860 range
= xmlXPtrNewRange(
13861 oldlocset
->locTab
[i
]->user
,
13862 oldlocset
->locTab
[i
]->index
,
13863 rloc
->locTab
[j
]->user2
,
13864 rloc
->locTab
[j
]->index2
);
13865 if (range
!= NULL
) {
13866 xmlXPtrLocationSetAdd(newlocset
, range
);
13870 range
= xmlXPtrNewRangeNodeObject(
13871 (xmlNodePtr
)oldlocset
->locTab
[i
]->user
, res
);
13872 if (range
!= NULL
) {
13873 xmlXPtrLocationSetAdd(newlocset
,range
);
13881 xmlXPathReleaseObject(ctxt
->context
, res
);
13883 if (ctxt
->value
== tmp
) {
13884 res
= valuePop(ctxt
);
13885 xmlXPathReleaseObject(ctxt
->context
, res
);
13888 ctxt
->context
->node
= NULL
;
13890 } else { /* Not a location set */
13891 CHECK_TYPE0(XPATH_NODESET
);
13892 obj
= valuePop(ctxt
);
13893 oldset
= obj
->nodesetval
;
13894 ctxt
->context
->node
= NULL
;
13896 newlocset
= xmlXPtrLocationSetCreate(NULL
);
13898 if (oldset
!= NULL
) {
13899 for (i
= 0; i
< oldset
->nodeNr
; i
++) {
13901 * Run the evaluation with a node list made of a single item
13904 ctxt
->context
->node
= oldset
->nodeTab
[i
];
13906 * OPTIMIZE TODO: Avoid recreation for every iteration.
13908 tmp
= xmlXPathCacheNewNodeSet(ctxt
->context
,
13909 ctxt
->context
->node
);
13910 valuePush(ctxt
, tmp
);
13914 xmlXPathCompOpEval(ctxt
,
13915 &comp
->steps
[op
->ch2
]);
13916 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
13917 xmlXPathFreeObject(obj
);
13921 res
= valuePop(ctxt
);
13923 xmlXPtrNewRangeNodeObject(oldset
->nodeTab
[i
],
13925 if (range
!= NULL
) {
13926 xmlXPtrLocationSetAdd(newlocset
, range
);
13933 xmlXPathReleaseObject(ctxt
->context
, res
);
13935 if (ctxt
->value
== tmp
) {
13936 res
= valuePop(ctxt
);
13937 xmlXPathReleaseObject(ctxt
->context
, res
);
13940 ctxt
->context
->node
= NULL
;
13946 * The result is used as the new evaluation set.
13948 xmlXPathReleaseObject(ctxt
->context
, obj
);
13949 ctxt
->context
->node
= NULL
;
13950 ctxt
->context
->contextSize
= -1;
13951 ctxt
->context
->proximityPosition
= -1;
13952 valuePush(ctxt
, xmlXPtrWrapLocationSet(newlocset
));
13955 #endif /* LIBXML_XPTR_ENABLED */
13957 xmlGenericError(xmlGenericErrorContext
,
13958 "XPath: unknown precompiled operation %d\n", op
->op
);
13963 * xmlXPathCompOpEvalToBoolean:
13964 * @ctxt: the XPath parser context
13966 * Evaluates if the expression evaluates to true.
13968 * Returns 1 if true, 0 if false and -1 on API or internal errors.
13971 xmlXPathCompOpEvalToBoolean(xmlXPathParserContextPtr ctxt
,
13972 xmlXPathStepOpPtr op
,
13975 xmlXPathObjectPtr resObj
= NULL
;
13978 /* comp = ctxt->comp; */
13982 case XPATH_OP_VALUE
:
13983 resObj
= (xmlXPathObjectPtr
) op
->value4
;
13985 return(xmlXPathEvaluatePredicateResult(ctxt
, resObj
));
13986 return(xmlXPathCastToBoolean(resObj
));
13987 case XPATH_OP_SORT
:
13989 * We don't need sorting for boolean results. Skip this one.
13991 if (op
->ch1
!= -1) {
13992 op
= &ctxt
->comp
->steps
[op
->ch1
];
13996 case XPATH_OP_COLLECT
:
14000 xmlXPathCompOpEval(ctxt
, &ctxt
->comp
->steps
[op
->ch1
]);
14001 if (ctxt
->error
!= XPATH_EXPRESSION_OK
)
14004 xmlXPathNodeCollectAndTest(ctxt
, op
, NULL
, NULL
, 1);
14005 if (ctxt
->error
!= XPATH_EXPRESSION_OK
)
14008 resObj
= valuePop(ctxt
);
14009 if (resObj
== NULL
)
14014 * Fallback to call xmlXPathCompOpEval().
14016 xmlXPathCompOpEval(ctxt
, op
);
14017 if (ctxt
->error
!= XPATH_EXPRESSION_OK
)
14020 resObj
= valuePop(ctxt
);
14021 if (resObj
== NULL
)
14029 if (resObj
->type
== XPATH_BOOLEAN
) {
14030 res
= resObj
->boolval
;
14031 } else if (isPredicate
) {
14033 * For predicates a result of type "number" is handled
14036 * "If the result is a number, the result will be converted
14037 * to true if the number is equal to the context position
14038 * and will be converted to false otherwise;"
14040 res
= xmlXPathEvaluatePredicateResult(ctxt
, resObj
);
14042 res
= xmlXPathCastToBoolean(resObj
);
14044 xmlXPathReleaseObject(ctxt
->context
, resObj
);
14051 #ifdef XPATH_STREAMING
14053 * xmlXPathRunStreamEval:
14054 * @ctxt: the XPath parser context with the compiled expression
14056 * Evaluate the Precompiled Streamable XPath expression in the given context.
14059 xmlXPathRunStreamEval(xmlXPathContextPtr ctxt
, xmlPatternPtr comp
,
14060 xmlXPathObjectPtr
*resultSeq
, int toBool
)
14062 int max_depth
, min_depth
;
14065 int eval_all_nodes
;
14066 xmlNodePtr cur
= NULL
, limit
= NULL
;
14067 xmlStreamCtxtPtr patstream
= NULL
;
14071 if ((ctxt
== NULL
) || (comp
== NULL
))
14073 max_depth
= xmlPatternMaxDepth(comp
);
14074 if (max_depth
== -1)
14076 if (max_depth
== -2)
14078 min_depth
= xmlPatternMinDepth(comp
);
14079 if (min_depth
== -1)
14081 from_root
= xmlPatternFromRoot(comp
);
14085 printf("stream eval: depth %d from root %d\n", max_depth
, from_root
);
14089 if (resultSeq
== NULL
)
14091 *resultSeq
= xmlXPathCacheNewNodeSet(ctxt
, NULL
);
14092 if (*resultSeq
== NULL
)
14097 * handle the special cases of "/" amd "." being matched
14099 if (min_depth
== 0) {
14104 xmlXPathNodeSetAddUnique((*resultSeq
)->nodesetval
,
14105 (xmlNodePtr
) ctxt
->doc
);
14107 /* Select "self::node()" */
14110 xmlXPathNodeSetAddUnique((*resultSeq
)->nodesetval
, ctxt
->node
);
14113 if (max_depth
== 0) {
14118 cur
= (xmlNodePtr
)ctxt
->doc
;
14119 } else if (ctxt
->node
!= NULL
) {
14120 switch (ctxt
->node
->type
) {
14121 case XML_ELEMENT_NODE
:
14122 case XML_DOCUMENT_NODE
:
14123 case XML_DOCUMENT_FRAG_NODE
:
14124 case XML_HTML_DOCUMENT_NODE
:
14125 #ifdef LIBXML_DOCB_ENABLED
14126 case XML_DOCB_DOCUMENT_NODE
:
14130 case XML_ATTRIBUTE_NODE
:
14131 case XML_TEXT_NODE
:
14132 case XML_CDATA_SECTION_NODE
:
14133 case XML_ENTITY_REF_NODE
:
14134 case XML_ENTITY_NODE
:
14136 case XML_COMMENT_NODE
:
14137 case XML_NOTATION_NODE
:
14139 case XML_DOCUMENT_TYPE_NODE
:
14140 case XML_ELEMENT_DECL
:
14141 case XML_ATTRIBUTE_DECL
:
14142 case XML_ENTITY_DECL
:
14143 case XML_NAMESPACE_DECL
:
14144 case XML_XINCLUDE_START
:
14145 case XML_XINCLUDE_END
:
14154 patstream
= xmlPatternGetStreamCtxt(comp
);
14155 if (patstream
== NULL
) {
14157 * QUESTION TODO: Is this an error?
14162 eval_all_nodes
= xmlStreamWantsAnyNode(patstream
);
14165 ret
= xmlStreamPush(patstream
, NULL
, NULL
);
14167 } else if (ret
== 1) {
14170 xmlXPathNodeSetAddUnique((*resultSeq
)->nodesetval
, cur
);
14174 goto scan_children
;
14179 switch (cur
->type
) {
14180 case XML_ELEMENT_NODE
:
14181 case XML_TEXT_NODE
:
14182 case XML_CDATA_SECTION_NODE
:
14183 case XML_COMMENT_NODE
:
14185 if (cur
->type
== XML_ELEMENT_NODE
) {
14186 ret
= xmlStreamPush(patstream
, cur
->name
,
14187 (cur
->ns
? cur
->ns
->href
: NULL
));
14188 } else if (eval_all_nodes
)
14189 ret
= xmlStreamPushNode(patstream
, NULL
, NULL
, cur
->type
);
14195 } else if (ret
== 1) {
14198 xmlXPathNodeSetAddUnique((*resultSeq
)->nodesetval
, cur
);
14200 if ((cur
->children
== NULL
) || (depth
>= max_depth
)) {
14201 ret
= xmlStreamPop(patstream
);
14202 while (cur
->next
!= NULL
) {
14204 if ((cur
->type
!= XML_ENTITY_DECL
) &&
14205 (cur
->type
!= XML_DTD_NODE
))
14214 if ((cur
->children
!= NULL
) && (depth
< max_depth
)) {
14216 * Do not descend on entities declarations
14218 if (cur
->children
->type
!= XML_ENTITY_DECL
) {
14219 cur
= cur
->children
;
14224 if (cur
->type
!= XML_DTD_NODE
)
14232 while (cur
->next
!= NULL
) {
14234 if ((cur
->type
!= XML_ENTITY_DECL
) &&
14235 (cur
->type
!= XML_DTD_NODE
))
14242 if ((cur
== NULL
) || (cur
== limit
))
14244 if (cur
->type
== XML_ELEMENT_NODE
) {
14245 ret
= xmlStreamPop(patstream
);
14246 } else if ((eval_all_nodes
) &&
14247 ((cur
->type
== XML_TEXT_NODE
) ||
14248 (cur
->type
== XML_CDATA_SECTION_NODE
) ||
14249 (cur
->type
== XML_COMMENT_NODE
) ||
14250 (cur
->type
== XML_PI_NODE
)))
14252 ret
= xmlStreamPop(patstream
);
14254 if (cur
->next
!= NULL
) {
14258 } while (cur
!= NULL
);
14260 } while ((cur
!= NULL
) && (depth
>= 0));
14265 printf("stream eval: checked %d nodes selected %d\n",
14266 nb_nodes
, retObj
->nodesetval
->nodeNr
);
14270 xmlFreeStreamCtxt(patstream
);
14275 xmlFreeStreamCtxt(patstream
);
14278 #endif /* XPATH_STREAMING */
14282 * @ctxt: the XPath parser context with the compiled expression
14283 * @toBool: evaluate to a boolean result
14285 * Evaluate the Precompiled XPath expression in the given context.
14288 xmlXPathRunEval(xmlXPathParserContextPtr ctxt
, int toBool
)
14290 xmlXPathCompExprPtr comp
;
14292 if ((ctxt
== NULL
) || (ctxt
->comp
== NULL
))
14295 if (ctxt
->valueTab
== NULL
) {
14296 /* Allocate the value stack */
14297 ctxt
->valueTab
= (xmlXPathObjectPtr
*)
14298 xmlMalloc(10 * sizeof(xmlXPathObjectPtr
));
14299 if (ctxt
->valueTab
== NULL
) {
14300 xmlXPathPErrMemory(ctxt
, "creating evaluation context\n");
14304 ctxt
->valueMax
= 10;
14305 ctxt
->value
= NULL
;
14307 #ifdef XPATH_STREAMING
14308 if (ctxt
->comp
->stream
) {
14313 * Evaluation to boolean result.
14315 res
= xmlXPathRunStreamEval(ctxt
->context
,
14316 ctxt
->comp
->stream
, NULL
, 1);
14320 xmlXPathObjectPtr resObj
= NULL
;
14323 * Evaluation to a sequence.
14325 res
= xmlXPathRunStreamEval(ctxt
->context
,
14326 ctxt
->comp
->stream
, &resObj
, 0);
14328 if ((res
!= -1) && (resObj
!= NULL
)) {
14329 valuePush(ctxt
, resObj
);
14332 if (resObj
!= NULL
)
14333 xmlXPathReleaseObject(ctxt
->context
, resObj
);
14336 * QUESTION TODO: This falls back to normal XPath evaluation
14337 * if res == -1. Is this intended?
14342 if (comp
->last
< 0) {
14343 xmlGenericError(xmlGenericErrorContext
,
14344 "xmlXPathRunEval: last is less than zero\n");
14348 return(xmlXPathCompOpEvalToBoolean(ctxt
,
14349 &comp
->steps
[comp
->last
], 0));
14351 xmlXPathCompOpEval(ctxt
, &comp
->steps
[comp
->last
]);
14356 /************************************************************************
14358 * Public interfaces *
14360 ************************************************************************/
14363 * xmlXPathEvalPredicate:
14364 * @ctxt: the XPath context
14365 * @res: the Predicate Expression evaluation result
14367 * Evaluate a predicate result for the current node.
14368 * A PredicateExpr is evaluated by evaluating the Expr and converting
14369 * the result to a boolean. If the result is a number, the result will
14370 * be converted to true if the number is equal to the position of the
14371 * context node in the context node list (as returned by the position
14372 * function) and will be converted to false otherwise; if the result
14373 * is not a number, then the result will be converted as if by a call
14374 * to the boolean function.
14376 * Returns 1 if predicate is true, 0 otherwise
14379 xmlXPathEvalPredicate(xmlXPathContextPtr ctxt
, xmlXPathObjectPtr res
) {
14380 if ((ctxt
== NULL
) || (res
== NULL
)) return(0);
14381 switch (res
->type
) {
14382 case XPATH_BOOLEAN
:
14383 return(res
->boolval
);
14385 return(res
->floatval
== ctxt
->proximityPosition
);
14386 case XPATH_NODESET
:
14387 case XPATH_XSLT_TREE
:
14388 if (res
->nodesetval
== NULL
)
14390 return(res
->nodesetval
->nodeNr
!= 0);
14392 return((res
->stringval
!= NULL
) &&
14393 (xmlStrlen(res
->stringval
) != 0));
14401 * xmlXPathEvaluatePredicateResult:
14402 * @ctxt: the XPath Parser context
14403 * @res: the Predicate Expression evaluation result
14405 * Evaluate a predicate result for the current node.
14406 * A PredicateExpr is evaluated by evaluating the Expr and converting
14407 * the result to a boolean. If the result is a number, the result will
14408 * be converted to true if the number is equal to the position of the
14409 * context node in the context node list (as returned by the position
14410 * function) and will be converted to false otherwise; if the result
14411 * is not a number, then the result will be converted as if by a call
14412 * to the boolean function.
14414 * Returns 1 if predicate is true, 0 otherwise
14417 xmlXPathEvaluatePredicateResult(xmlXPathParserContextPtr ctxt
,
14418 xmlXPathObjectPtr res
) {
14419 if ((ctxt
== NULL
) || (res
== NULL
)) return(0);
14420 switch (res
->type
) {
14421 case XPATH_BOOLEAN
:
14422 return(res
->boolval
);
14424 #if defined(__BORLANDC__) || (defined(_MSC_VER) && (_MSC_VER == 1200))
14425 return((res
->floatval
== ctxt
->context
->proximityPosition
) &&
14426 (!xmlXPathIsNaN(res
->floatval
))); /* MSC pbm Mark Vakoc !*/
14428 return(res
->floatval
== ctxt
->context
->proximityPosition
);
14430 case XPATH_NODESET
:
14431 case XPATH_XSLT_TREE
:
14432 if (res
->nodesetval
== NULL
)
14434 return(res
->nodesetval
->nodeNr
!= 0);
14436 return((res
->stringval
!= NULL
) && (res
->stringval
[0] != 0));
14437 #ifdef LIBXML_XPTR_ENABLED
14438 case XPATH_LOCATIONSET
:{
14439 xmlLocationSetPtr ptr
= res
->user
;
14442 return (ptr
->locNr
!= 0);
14451 #ifdef XPATH_STREAMING
14453 * xmlXPathTryStreamCompile:
14454 * @ctxt: an XPath context
14455 * @str: the XPath expression
14457 * Try to compile the XPath expression as a streamable subset.
14459 * Returns the compiled expression or NULL if failed to compile.
14461 static xmlXPathCompExprPtr
14462 xmlXPathTryStreamCompile(xmlXPathContextPtr ctxt
, const xmlChar
*str
) {
14464 * Optimization: use streaming patterns when the XPath expression can
14465 * be compiled to a stream lookup
14467 xmlPatternPtr stream
;
14468 xmlXPathCompExprPtr comp
;
14469 xmlDictPtr dict
= NULL
;
14470 const xmlChar
**namespaces
= NULL
;
14474 if ((!xmlStrchr(str
, '[')) && (!xmlStrchr(str
, '(')) &&
14475 (!xmlStrchr(str
, '@'))) {
14476 const xmlChar
*tmp
;
14479 * We don't try to handle expressions using the verbose axis
14480 * specifiers ("::"), just the simplied form at this point.
14481 * Additionally, if there is no list of namespaces available and
14482 * there's a ":" in the expression, indicating a prefixed QName,
14483 * then we won't try to compile either. xmlPatterncompile() needs
14484 * to have a list of namespaces at compilation time in order to
14485 * compile prefixed name tests.
14487 tmp
= xmlStrchr(str
, ':');
14488 if ((tmp
!= NULL
) &&
14489 ((ctxt
== NULL
) || (ctxt
->nsNr
== 0) || (tmp
[1] == ':')))
14492 if (ctxt
!= NULL
) {
14494 if (ctxt
->nsNr
> 0) {
14495 namespaces
= xmlMalloc(2 * (ctxt
->nsNr
+ 1) * sizeof(xmlChar
*));
14496 if (namespaces
== NULL
) {
14497 xmlXPathErrMemory(ctxt
, "allocating namespaces array\n");
14500 for (i
= 0, j
= 0; (j
< ctxt
->nsNr
); j
++) {
14501 ns
= ctxt
->namespaces
[j
];
14502 namespaces
[i
++] = ns
->href
;
14503 namespaces
[i
++] = ns
->prefix
;
14505 namespaces
[i
++] = NULL
;
14506 namespaces
[i
] = NULL
;
14510 stream
= xmlPatterncompile(str
, dict
, XML_PATTERN_XPATH
,
14512 if (namespaces
!= NULL
) {
14513 xmlFree((xmlChar
**)namespaces
);
14515 if ((stream
!= NULL
) && (xmlPatternStreamable(stream
) == 1)) {
14516 comp
= xmlXPathNewCompExpr();
14517 if (comp
== NULL
) {
14518 xmlXPathErrMemory(ctxt
, "allocating streamable expression\n");
14521 comp
->stream
= stream
;
14524 xmlDictReference(comp
->dict
);
14527 xmlFreePattern(stream
);
14531 #endif /* XPATH_STREAMING */
14534 xmlXPathCanRewriteDosExpression(xmlChar
*expr
)
14539 if ((*expr
== '/') && (*(++expr
) == '/'))
14545 xmlXPathRewriteDOSExpression(xmlXPathCompExprPtr comp
, xmlXPathStepOpPtr op
)
14548 * Try to rewrite "descendant-or-self::node()/foo" to an optimized
14549 * internal representation.
14551 if (op
->ch1
!= -1) {
14552 if ((op
->op
== XPATH_OP_COLLECT
/* 11 */) &&
14553 ((xmlXPathAxisVal
) op
->value
== AXIS_CHILD
/* 4 */) &&
14554 ((xmlXPathTestVal
) op
->value2
== NODE_TEST_NAME
/* 5 */) &&
14555 ((xmlXPathTypeVal
) op
->value3
== NODE_TYPE_NODE
/* 0 */))
14558 * This is a "child::foo"
14560 xmlXPathStepOpPtr prevop
= &comp
->steps
[op
->ch1
];
14562 if ((prevop
->op
== XPATH_OP_COLLECT
/* 11 */) &&
14563 (prevop
->ch1
!= -1) &&
14564 ((xmlXPathAxisVal
) prevop
->value
==
14565 AXIS_DESCENDANT_OR_SELF
) &&
14566 (prevop
->ch2
== -1) &&
14567 ((xmlXPathTestVal
) prevop
->value2
== NODE_TEST_TYPE
) &&
14568 ((xmlXPathTypeVal
) prevop
->value3
== NODE_TYPE_NODE
) &&
14569 (comp
->steps
[prevop
->ch1
].op
== XPATH_OP_ROOT
))
14572 * This is a "/descendant-or-self::node()" without predicates.
14575 op
->ch1
= prevop
->ch1
;
14576 op
->rewriteType
= XP_REWRITE_DOS_CHILD_ELEM
;
14580 xmlXPathRewriteDOSExpression(comp
, &comp
->steps
[op
->ch1
]);
14583 xmlXPathRewriteDOSExpression(comp
, &comp
->steps
[op
->ch2
]);
14587 * xmlXPathCtxtCompile:
14588 * @ctxt: an XPath context
14589 * @str: the XPath expression
14591 * Compile an XPath expression
14593 * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL.
14594 * the caller has to free the object.
14596 xmlXPathCompExprPtr
14597 xmlXPathCtxtCompile(xmlXPathContextPtr ctxt
, const xmlChar
*str
) {
14598 xmlXPathParserContextPtr pctxt
;
14599 xmlXPathCompExprPtr comp
;
14601 #ifdef XPATH_STREAMING
14602 comp
= xmlXPathTryStreamCompile(ctxt
, str
);
14609 pctxt
= xmlXPathNewParserContext(str
, ctxt
);
14612 xmlXPathCompileExpr(pctxt
, 1);
14614 if( pctxt
->error
!= XPATH_EXPRESSION_OK
)
14616 xmlXPathFreeParserContext(pctxt
);
14620 if (*pctxt
->cur
!= 0) {
14622 * aleksey: in some cases this line prints *second* error message
14623 * (see bug #78858) and probably this should be fixed.
14624 * However, we are not sure that all error messages are printed
14625 * out in other places. It's not critical so we leave it as-is for now
14627 xmlXPatherror(pctxt
, __FILE__
, __LINE__
, XPATH_EXPR_ERROR
);
14630 comp
= pctxt
->comp
;
14631 pctxt
->comp
= NULL
;
14633 xmlXPathFreeParserContext(pctxt
);
14635 if (comp
!= NULL
) {
14636 comp
->expr
= xmlStrdup(str
);
14637 #ifdef DEBUG_EVAL_COUNTS
14638 comp
->string
= xmlStrdup(str
);
14641 if ((comp
->expr
!= NULL
) &&
14642 (comp
->nbStep
> 2) &&
14643 (comp
->last
>= 0) &&
14644 (xmlXPathCanRewriteDosExpression(comp
->expr
) == 1))
14646 xmlXPathRewriteDOSExpression(comp
, &comp
->steps
[comp
->last
]);
14654 * @str: the XPath expression
14656 * Compile an XPath expression
14658 * Returns the xmlXPathCompExprPtr resulting from the compilation or NULL.
14659 * the caller has to free the object.
14661 xmlXPathCompExprPtr
14662 xmlXPathCompile(const xmlChar
*str
) {
14663 return(xmlXPathCtxtCompile(NULL
, str
));
14667 * xmlXPathCompiledEvalInternal:
14668 * @comp: the compiled XPath expression
14669 * @ctxt: the XPath context
14670 * @resObj: the resulting XPath object or NULL
14671 * @toBool: 1 if only a boolean result is requested
14673 * Evaluate the Precompiled XPath expression in the given context.
14674 * The caller has to free @resObj.
14676 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
14677 * the caller has to free the object.
14680 xmlXPathCompiledEvalInternal(xmlXPathCompExprPtr comp
,
14681 xmlXPathContextPtr ctxt
,
14682 xmlXPathObjectPtr
*resObj
,
14685 xmlXPathParserContextPtr pctxt
;
14686 #ifndef LIBXML_THREAD_ENABLED
14687 static int reentance
= 0;
14691 CHECK_CTXT_NEG(ctxt
)
14697 #ifndef LIBXML_THREAD_ENABLED
14700 xmlXPathDisableOptimizer
= 1;
14703 #ifdef DEBUG_EVAL_COUNTS
14705 if ((comp
->string
!= NULL
) && (comp
->nb
> 100)) {
14706 fprintf(stderr
, "100 x %s\n", comp
->string
);
14710 pctxt
= xmlXPathCompParserContext(comp
, ctxt
);
14711 res
= xmlXPathRunEval(pctxt
, toBool
);
14714 if (pctxt
->value
== NULL
) {
14715 xmlGenericError(xmlGenericErrorContext
,
14716 "xmlXPathCompiledEval: evaluation failed\n");
14719 *resObj
= valuePop(pctxt
);
14724 * Pop all remaining objects from the stack.
14726 if (pctxt
->valueNr
> 0) {
14727 xmlXPathObjectPtr tmp
;
14731 tmp
= valuePop(pctxt
);
14734 xmlXPathReleaseObject(ctxt
, tmp
);
14736 } while (tmp
!= NULL
);
14737 if ((stack
!= 0) &&
14738 ((toBool
) || ((resObj
) && (*resObj
))))
14740 xmlGenericError(xmlGenericErrorContext
,
14741 "xmlXPathCompiledEval: %d objects left on the stack.\n",
14746 if ((pctxt
->error
!= XPATH_EXPRESSION_OK
) && (resObj
) && (*resObj
)) {
14747 xmlXPathFreeObject(*resObj
);
14750 pctxt
->comp
= NULL
;
14751 xmlXPathFreeParserContext(pctxt
);
14752 #ifndef LIBXML_THREAD_ENABLED
14760 * xmlXPathCompiledEval:
14761 * @comp: the compiled XPath expression
14762 * @ctx: the XPath context
14764 * Evaluate the Precompiled XPath expression in the given context.
14766 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
14767 * the caller has to free the object.
14770 xmlXPathCompiledEval(xmlXPathCompExprPtr comp
, xmlXPathContextPtr ctx
)
14772 xmlXPathObjectPtr res
= NULL
;
14774 xmlXPathCompiledEvalInternal(comp
, ctx
, &res
, 0);
14779 * xmlXPathCompiledEvalToBoolean:
14780 * @comp: the compiled XPath expression
14781 * @ctxt: the XPath context
14783 * Applies the XPath boolean() function on the result of the given
14784 * compiled expression.
14786 * Returns 1 if the expression evaluated to true, 0 if to false and
14787 * -1 in API and internal errors.
14790 xmlXPathCompiledEvalToBoolean(xmlXPathCompExprPtr comp
,
14791 xmlXPathContextPtr ctxt
)
14793 return(xmlXPathCompiledEvalInternal(comp
, ctxt
, NULL
, 1));
14797 * xmlXPathEvalExpr:
14798 * @ctxt: the XPath Parser context
14800 * Parse and evaluate an XPath expression in the given context,
14801 * then push the result on the context stack
14804 xmlXPathEvalExpr(xmlXPathParserContextPtr ctxt
) {
14805 #ifdef XPATH_STREAMING
14806 xmlXPathCompExprPtr comp
;
14809 if (ctxt
== NULL
) return;
14811 #ifdef XPATH_STREAMING
14812 comp
= xmlXPathTryStreamCompile(ctxt
->context
, ctxt
->base
);
14813 if (comp
!= NULL
) {
14814 if (ctxt
->comp
!= NULL
)
14815 xmlXPathFreeCompExpr(ctxt
->comp
);
14817 if (ctxt
->cur
!= NULL
)
14818 while (*ctxt
->cur
!= 0) ctxt
->cur
++;
14822 xmlXPathCompileExpr(ctxt
, 1);
14824 * In this scenario the expression string will sit in ctxt->base.
14826 if ((ctxt
->error
== XPATH_EXPRESSION_OK
) &&
14827 (ctxt
->comp
!= NULL
) &&
14828 (ctxt
->base
!= NULL
) &&
14829 (ctxt
->comp
->nbStep
> 2) &&
14830 (ctxt
->comp
->last
>= 0) &&
14831 (xmlXPathCanRewriteDosExpression((xmlChar
*) ctxt
->base
) == 1))
14833 xmlXPathRewriteDOSExpression(ctxt
->comp
,
14834 &ctxt
->comp
->steps
[ctxt
->comp
->last
]);
14838 xmlXPathRunEval(ctxt
, 0);
14843 * @str: the XPath expression
14844 * @ctx: the XPath context
14846 * Evaluate the XPath Location Path in the given context.
14848 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
14849 * the caller has to free the object.
14852 xmlXPathEval(const xmlChar
*str
, xmlXPathContextPtr ctx
) {
14853 xmlXPathParserContextPtr ctxt
;
14854 xmlXPathObjectPtr res
, tmp
, init
= NULL
;
14861 ctxt
= xmlXPathNewParserContext(str
, ctx
);
14864 xmlXPathEvalExpr(ctxt
);
14866 if (ctxt
->value
== NULL
) {
14867 xmlGenericError(xmlGenericErrorContext
,
14868 "xmlXPathEval: evaluation failed\n");
14870 } else if ((*ctxt
->cur
!= 0) && (ctxt
->comp
!= NULL
)
14871 #ifdef XPATH_STREAMING
14872 && (ctxt
->comp
->stream
== NULL
)
14875 xmlXPatherror(ctxt
, __FILE__
, __LINE__
, XPATH_EXPR_ERROR
);
14878 res
= valuePop(ctxt
);
14882 tmp
= valuePop(ctxt
);
14886 xmlXPathReleaseObject(ctx
, tmp
);
14888 } while (tmp
!= NULL
);
14889 if ((stack
!= 0) && (res
!= NULL
)) {
14890 xmlGenericError(xmlGenericErrorContext
,
14891 "xmlXPathEval: %d object left on the stack\n",
14894 if (ctxt
->error
!= XPATH_EXPRESSION_OK
) {
14895 xmlXPathFreeObject(res
);
14899 xmlXPathFreeParserContext(ctxt
);
14904 * xmlXPathEvalExpression:
14905 * @str: the XPath expression
14906 * @ctxt: the XPath context
14908 * Evaluate the XPath expression in the given context.
14910 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
14911 * the caller has to free the object.
14914 xmlXPathEvalExpression(const xmlChar
*str
, xmlXPathContextPtr ctxt
) {
14915 xmlXPathParserContextPtr pctxt
;
14916 xmlXPathObjectPtr res
, tmp
;
14923 pctxt
= xmlXPathNewParserContext(str
, ctxt
);
14926 xmlXPathEvalExpr(pctxt
);
14928 if ((*pctxt
->cur
!= 0) || (pctxt
->error
!= XPATH_EXPRESSION_OK
)) {
14929 xmlXPatherror(pctxt
, __FILE__
, __LINE__
, XPATH_EXPR_ERROR
);
14932 res
= valuePop(pctxt
);
14935 tmp
= valuePop(pctxt
);
14937 xmlXPathReleaseObject(ctxt
, tmp
);
14940 } while (tmp
!= NULL
);
14941 if ((stack
!= 0) && (res
!= NULL
)) {
14942 xmlGenericError(xmlGenericErrorContext
,
14943 "xmlXPathEvalExpression: %d object left on the stack\n",
14946 xmlXPathFreeParserContext(pctxt
);
14950 /************************************************************************
14952 * Extra functions not pertaining to the XPath spec *
14954 ************************************************************************/
14956 * xmlXPathEscapeUriFunction:
14957 * @ctxt: the XPath Parser context
14958 * @nargs: the number of arguments
14960 * Implement the escape-uri() XPath function
14961 * string escape-uri(string $str, bool $escape-reserved)
14963 * This function applies the URI escaping rules defined in section 2 of [RFC
14964 * 2396] to the string supplied as $uri-part, which typically represents all
14965 * or part of a URI. The effect of the function is to replace any special
14966 * character in the string by an escape sequence of the form %xx%yy...,
14967 * where xxyy... is the hexadecimal representation of the octets used to
14968 * represent the character in UTF-8.
14970 * The set of characters that are escaped depends on the setting of the
14971 * boolean argument $escape-reserved.
14973 * If $escape-reserved is true, all characters are escaped other than lower
14974 * case letters a-z, upper case letters A-Z, digits 0-9, and the characters
14975 * referred to in [RFC 2396] as "marks": specifically, "-" | "_" | "." | "!"
14976 * | "~" | "*" | "'" | "(" | ")". The "%" character itself is escaped only
14977 * if it is not followed by two hexadecimal digits (that is, 0-9, a-f, and
14980 * If $escape-reserved is false, the behavior differs in that characters
14981 * referred to in [RFC 2396] as reserved characters are not escaped. These
14982 * characters are ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ",".
14984 * [RFC 2396] does not define whether escaped URIs should use lower case or
14985 * upper case for hexadecimal digits. To ensure that escaped URIs can be
14986 * compared using string comparison functions, this function must always use
14987 * the upper-case letters A-F.
14989 * Generally, $escape-reserved should be set to true when escaping a string
14990 * that is to form a single part of a URI, and to false when escaping an
14991 * entire URI or URI reference.
14993 * In the case of non-ascii characters, the string is encoded according to
14994 * utf-8 and then converted according to RFC 2396.
14997 * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), true())
14998 * returns "gopher%3A%2F%2Fspinaltap.micro.umn.edu%2F00%2FWeather%2FCalifornia%2FLos%20Angeles%23ocean"
14999 * xf:escape-uri ("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#ocean"), false())
15000 * returns "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles%23ocean"
15004 xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt
, int nargs
) {
15005 xmlXPathObjectPtr str
;
15006 int escape_reserved
;
15007 xmlBufferPtr target
;
15013 escape_reserved
= xmlXPathPopBoolean(ctxt
);
15016 str
= valuePop(ctxt
);
15018 target
= xmlBufferCreate();
15024 for (cptr
= str
->stringval
; *cptr
; cptr
++) {
15025 if ((*cptr
>= 'A' && *cptr
<= 'Z') ||
15026 (*cptr
>= 'a' && *cptr
<= 'z') ||
15027 (*cptr
>= '0' && *cptr
<= '9') ||
15028 *cptr
== '-' || *cptr
== '_' || *cptr
== '.' ||
15029 *cptr
== '!' || *cptr
== '~' || *cptr
== '*' ||
15030 *cptr
== '\''|| *cptr
== '(' || *cptr
== ')' ||
15032 ((cptr
[1] >= 'A' && cptr
[1] <= 'F') ||
15033 (cptr
[1] >= 'a' && cptr
[1] <= 'f') ||
15034 (cptr
[1] >= '0' && cptr
[1] <= '9')) &&
15035 ((cptr
[2] >= 'A' && cptr
[2] <= 'F') ||
15036 (cptr
[2] >= 'a' && cptr
[2] <= 'f') ||
15037 (cptr
[2] >= '0' && cptr
[2] <= '9'))) ||
15038 (!escape_reserved
&&
15039 (*cptr
== ';' || *cptr
== '/' || *cptr
== '?' ||
15040 *cptr
== ':' || *cptr
== '@' || *cptr
== '&' ||
15041 *cptr
== '=' || *cptr
== '+' || *cptr
== '$' ||
15043 xmlBufferAdd(target
, cptr
, 1);
15045 if ((*cptr
>> 4) < 10)
15046 escape
[1] = '0' + (*cptr
>> 4);
15048 escape
[1] = 'A' - 10 + (*cptr
>> 4);
15049 if ((*cptr
& 0xF) < 10)
15050 escape
[2] = '0' + (*cptr
& 0xF);
15052 escape
[2] = 'A' - 10 + (*cptr
& 0xF);
15054 xmlBufferAdd(target
, &escape
[0], 3);
15058 valuePush(ctxt
, xmlXPathCacheNewString(ctxt
->context
,
15059 xmlBufferContent(target
)));
15060 xmlBufferFree(target
);
15061 xmlXPathReleaseObject(ctxt
->context
, str
);
15065 * xmlXPathRegisterAllFunctions:
15066 * @ctxt: the XPath context
15068 * Registers all default XPath functions in this context
15071 xmlXPathRegisterAllFunctions(xmlXPathContextPtr ctxt
)
15073 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"boolean",
15074 xmlXPathBooleanFunction
);
15075 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"ceiling",
15076 xmlXPathCeilingFunction
);
15077 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"count",
15078 xmlXPathCountFunction
);
15079 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"concat",
15080 xmlXPathConcatFunction
);
15081 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"contains",
15082 xmlXPathContainsFunction
);
15083 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"id",
15084 xmlXPathIdFunction
);
15085 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"false",
15086 xmlXPathFalseFunction
);
15087 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"floor",
15088 xmlXPathFloorFunction
);
15089 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"last",
15090 xmlXPathLastFunction
);
15091 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"lang",
15092 xmlXPathLangFunction
);
15093 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"local-name",
15094 xmlXPathLocalNameFunction
);
15095 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"not",
15096 xmlXPathNotFunction
);
15097 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"name",
15098 xmlXPathNameFunction
);
15099 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"namespace-uri",
15100 xmlXPathNamespaceURIFunction
);
15101 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"normalize-space",
15102 xmlXPathNormalizeFunction
);
15103 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"number",
15104 xmlXPathNumberFunction
);
15105 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"position",
15106 xmlXPathPositionFunction
);
15107 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"round",
15108 xmlXPathRoundFunction
);
15109 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"string",
15110 xmlXPathStringFunction
);
15111 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"string-length",
15112 xmlXPathStringLengthFunction
);
15113 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"starts-with",
15114 xmlXPathStartsWithFunction
);
15115 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"substring",
15116 xmlXPathSubstringFunction
);
15117 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"substring-before",
15118 xmlXPathSubstringBeforeFunction
);
15119 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"substring-after",
15120 xmlXPathSubstringAfterFunction
);
15121 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"sum",
15122 xmlXPathSumFunction
);
15123 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"true",
15124 xmlXPathTrueFunction
);
15125 xmlXPathRegisterFunc(ctxt
, (const xmlChar
*)"translate",
15126 xmlXPathTranslateFunction
);
15128 xmlXPathRegisterFuncNS(ctxt
, (const xmlChar
*)"escape-uri",
15129 (const xmlChar
*)"http://www.w3.org/2002/08/xquery-functions",
15130 xmlXPathEscapeUriFunction
);
15133 #endif /* LIBXML_XPATH_ENABLED */
15134 #define bottom_xpath
15135 #include "elfgcchack.h"