2 * xpointer.c : Code to handle XML Pointer
4 * Base implementation was made accordingly to
5 * W3C Candidate Recommendation 7 June 2000
6 * http://www.w3.org/TR/2000/CR-xptr-20000607
8 * Added support for the element() scheme described in:
9 * W3C Proposed Recommendation 13 November 2002
10 * http://www.w3.org/TR/2002/PR-xptr-element-20021113/
12 * See Copyright for the status of this software.
21 * TODO: better handling of error cases, the full expression should
22 * be parsed beforehand instead of a progressive evaluation
23 * TODO: Access into entities references are not supported now ...
24 * need a start to be able to pop out of entities refs since
25 * parent is the endity declaration, not the ref.
29 #include <libxml/xpointer.h>
30 #include <libxml/xmlmemory.h>
31 #include <libxml/parserInternals.h>
32 #include <libxml/uri.h>
33 #include <libxml/xpath.h>
34 #include <libxml/xpathInternals.h>
35 #include <libxml/xmlerror.h>
36 #include <libxml/globals.h>
38 #ifdef LIBXML_XPTR_ENABLED
40 /* Add support of the xmlns() xpointer scheme to initialize the namespaces */
41 #define XPTR_XMLNS_SCHEME
43 /* #define DEBUG_RANGES */
45 #ifdef LIBXML_DEBUG_ENABLED
46 #include <libxml/debugXML.h>
51 xmlGenericError(xmlGenericErrorContext, \
52 "Unimplemented block at %s:%d\n", \
56 xmlGenericError(xmlGenericErrorContext, \
57 "Internal error at %s:%d\n", \
60 /************************************************************************
62 * A few helper functions for child sequences *
64 ************************************************************************/
66 xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur);
71 * Returns the number of child for an element, -1 in case of error
74 xmlXPtrGetArity(xmlNodePtr cur) {
79 for (i = 0;cur != NULL;cur = cur->next) {
80 if ((cur->type == XML_ELEMENT_NODE) ||
81 (cur->type == XML_DOCUMENT_NODE) ||
82 (cur->type == XML_HTML_DOCUMENT_NODE)) {
93 * Returns the index of the node in its parent children list, -1
97 xmlXPtrGetIndex(xmlNodePtr cur) {
101 for (i = 1;cur != NULL;cur = cur->prev) {
102 if ((cur->type == XML_ELEMENT_NODE) ||
103 (cur->type == XML_DOCUMENT_NODE) ||
104 (cur->type == XML_HTML_DOCUMENT_NODE)) {
112 * xmlXPtrGetNthChild:
114 * @no: the child number
116 * Returns the @no'th element child of @cur or NULL
119 xmlXPtrGetNthChild(xmlNodePtr cur, int no) {
124 for (i = 0;i <= no;cur = cur->next) {
127 if ((cur->type == XML_ELEMENT_NODE) ||
128 (cur->type == XML_DOCUMENT_NODE) ||
129 (cur->type == XML_HTML_DOCUMENT_NODE)) {
138 /************************************************************************
140 * Handling of XPointer specific types *
142 ************************************************************************/
146 * @node1: the first node
147 * @index1: the first index
148 * @node2: the second node
149 * @index2: the second index
151 * Compare two points w.r.t document order
153 * Returns -2 in case of error 1 if first point < second point, 0 if
154 * that's the same point, -1 otherwise
157 xmlXPtrCmpPoints(xmlNodePtr node1, int index1, xmlNodePtr node2, int index2) {
158 if ((node1 == NULL) || (node2 == NULL))
161 * a couple of optimizations which will avoid computations in most cases
163 if (node1 == node2) {
170 return(xmlXPathCmpNodes(node1, node2));
175 * @node: the xmlNodePtr
176 * @indx: the indx within the node
178 * Create a new xmlXPathObjectPtr of type point
180 * Returns the newly created object.
182 static xmlXPathObjectPtr
183 xmlXPtrNewPoint(xmlNodePtr node, int indx) {
184 xmlXPathObjectPtr ret;
191 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
193 xmlGenericError(xmlGenericErrorContext,
194 "xmlXPtrNewPoint: out of memory\n");
197 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
198 ret->type = XPATH_POINT;
199 ret->user = (void *) node;
205 * xmlXPtrRangeCheckOrder:
206 * @range: an object range
208 * Make sure the points in the range are in the right order
211 xmlXPtrRangeCheckOrder(xmlXPathObjectPtr range) {
216 if (range->type != XPATH_RANGE)
218 if (range->user2 == NULL)
220 tmp = xmlXPtrCmpPoints(range->user, range->index,
221 range->user2, range->index2);
224 range->user = range->user2;
227 range->index = range->index2;
233 * xmlXPtrRangesEqual:
234 * @range1: the first range
235 * @range2: the second range
239 * Returns 1 if equal, 0 otherwise
242 xmlXPtrRangesEqual(xmlXPathObjectPtr range1, xmlXPathObjectPtr range2) {
243 if (range1 == range2)
245 if ((range1 == NULL) || (range2 == NULL))
247 if (range1->type != range2->type)
249 if (range1->type != XPATH_RANGE)
251 if (range1->user != range2->user)
253 if (range1->index != range2->index)
255 if (range1->user2 != range2->user2)
257 if (range1->index2 != range2->index2)
264 * @start: the starting node
265 * @startindex: the start index
266 * @end: the ending point
267 * @endindex: the ending index
269 * Create a new xmlXPathObjectPtr of type range
271 * Returns the newly created object.
274 xmlXPtrNewRange(xmlNodePtr start, int startindex,
275 xmlNodePtr end, int endindex) {
276 xmlXPathObjectPtr ret;
287 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
289 xmlGenericError(xmlGenericErrorContext,
290 "xmlXPtrNewRange: out of memory\n");
293 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
294 ret->type = XPATH_RANGE;
296 ret->index = startindex;
298 ret->index2 = endindex;
299 xmlXPtrRangeCheckOrder(ret);
304 * xmlXPtrNewRangePoints:
305 * @start: the starting point
306 * @end: the ending point
308 * Create a new xmlXPathObjectPtr of type range using 2 Points
310 * Returns the newly created object.
313 xmlXPtrNewRangePoints(xmlXPathObjectPtr start, xmlXPathObjectPtr end) {
314 xmlXPathObjectPtr ret;
320 if (start->type != XPATH_POINT)
322 if (end->type != XPATH_POINT)
325 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
327 xmlGenericError(xmlGenericErrorContext,
328 "xmlXPtrNewRangePoints: out of memory\n");
331 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
332 ret->type = XPATH_RANGE;
333 ret->user = start->user;
334 ret->index = start->index;
335 ret->user2 = end->user;
336 ret->index2 = end->index;
337 xmlXPtrRangeCheckOrder(ret);
342 * xmlXPtrNewRangePointNode:
343 * @start: the starting point
344 * @end: the ending node
346 * Create a new xmlXPathObjectPtr of type range from a point to a node
348 * Returns the newly created object.
351 xmlXPtrNewRangePointNode(xmlXPathObjectPtr start, xmlNodePtr end) {
352 xmlXPathObjectPtr ret;
358 if (start->type != XPATH_POINT)
361 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
363 xmlGenericError(xmlGenericErrorContext,
364 "xmlXPtrNewRangePointNode: out of memory\n");
367 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
368 ret->type = XPATH_RANGE;
369 ret->user = start->user;
370 ret->index = start->index;
373 xmlXPtrRangeCheckOrder(ret);
378 * xmlXPtrNewRangeNodePoint:
379 * @start: the starting node
380 * @end: the ending point
382 * Create a new xmlXPathObjectPtr of type range from a node to a point
384 * Returns the newly created object.
387 xmlXPtrNewRangeNodePoint(xmlNodePtr start, xmlXPathObjectPtr end) {
388 xmlXPathObjectPtr ret;
394 if (start->type != XPATH_POINT)
396 if (end->type != XPATH_POINT)
399 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
401 xmlGenericError(xmlGenericErrorContext,
402 "xmlXPtrNewRangeNodePoint: out of memory\n");
405 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
406 ret->type = XPATH_RANGE;
409 ret->user2 = end->user;
410 ret->index2 = end->index;
411 xmlXPtrRangeCheckOrder(ret);
416 * xmlXPtrNewRangeNodes:
417 * @start: the starting node
418 * @end: the ending node
420 * Create a new xmlXPathObjectPtr of type range using 2 nodes
422 * Returns the newly created object.
425 xmlXPtrNewRangeNodes(xmlNodePtr start, xmlNodePtr end) {
426 xmlXPathObjectPtr ret;
433 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
435 xmlGenericError(xmlGenericErrorContext,
436 "xmlXPtrNewRangeNodes: out of memory\n");
439 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
440 ret->type = XPATH_RANGE;
445 xmlXPtrRangeCheckOrder(ret);
450 * xmlXPtrNewCollapsedRange:
451 * @start: the starting and ending node
453 * Create a new xmlXPathObjectPtr of type range using a single nodes
455 * Returns the newly created object.
458 xmlXPtrNewCollapsedRange(xmlNodePtr start) {
459 xmlXPathObjectPtr ret;
464 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
466 xmlGenericError(xmlGenericErrorContext,
467 "xmlXPtrNewCollapsedRange: out of memory\n");
470 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
471 ret->type = XPATH_RANGE;
480 * xmlXPtrNewRangeNodeObject:
481 * @start: the starting node
482 * @end: the ending object
484 * Create a new xmlXPathObjectPtr of type range from a not to an object
486 * Returns the newly created object.
489 xmlXPtrNewRangeNodeObject(xmlNodePtr start, xmlXPathObjectPtr end) {
490 xmlXPathObjectPtr ret;
503 if (end->nodesetval->nodeNr <= 0)
511 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
513 xmlGenericError(xmlGenericErrorContext,
514 "xmlXPtrNewRangeNodeObject: out of memory\n");
517 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
518 ret->type = XPATH_RANGE;
523 ret->user2 = end->user;
524 ret->index2 = end->index;
525 case XPATH_NODESET: {
526 ret->user2 = end->nodesetval->nodeTab[end->nodesetval->nodeNr - 1];
534 xmlXPtrRangeCheckOrder(ret);
538 #define XML_RANGESET_DEFAULT 10
541 * xmlXPtrLocationSetCreate:
542 * @val: an initial xmlXPathObjectPtr, or NULL
544 * Create a new xmlLocationSetPtr of type double and of value @val
546 * Returns the newly created object.
549 xmlXPtrLocationSetCreate(xmlXPathObjectPtr val) {
550 xmlLocationSetPtr ret;
552 ret = (xmlLocationSetPtr) xmlMalloc(sizeof(xmlLocationSet));
554 xmlGenericError(xmlGenericErrorContext,
555 "xmlXPtrLocationSetCreate: out of memory\n");
558 memset(ret, 0 , (size_t) sizeof(xmlLocationSet));
560 ret->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
561 sizeof(xmlXPathObjectPtr));
562 if (ret->locTab == NULL) {
563 xmlGenericError(xmlGenericErrorContext,
564 "xmlXPtrLocationSetCreate: out of memory\n");
567 memset(ret->locTab, 0 ,
568 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
569 ret->locMax = XML_RANGESET_DEFAULT;
570 ret->locTab[ret->locNr++] = val;
576 * xmlXPtrLocationSetAdd:
577 * @cur: the initial range set
578 * @val: a new xmlXPathObjectPtr
580 * add a new xmlXPathObjectPtr to an existing LocationSet
581 * If the location already exist in the set @val is freed.
584 xmlXPtrLocationSetAdd(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
587 if (val == NULL) return;
590 * check against doublons
592 for (i = 0;i < cur->locNr;i++) {
593 if (xmlXPtrRangesEqual(cur->locTab[i], val)) {
594 xmlXPathFreeObject(val);
600 * grow the locTab if needed
602 if (cur->locMax == 0) {
603 cur->locTab = (xmlXPathObjectPtr *) xmlMalloc(XML_RANGESET_DEFAULT *
604 sizeof(xmlXPathObjectPtr));
605 if (cur->locTab == NULL) {
606 xmlGenericError(xmlGenericErrorContext,
607 "xmlXPtrLocationSetAdd: out of memory\n");
610 memset(cur->locTab, 0 ,
611 XML_RANGESET_DEFAULT * (size_t) sizeof(xmlXPathObjectPtr));
612 cur->locMax = XML_RANGESET_DEFAULT;
613 } else if (cur->locNr == cur->locMax) {
614 xmlXPathObjectPtr *temp;
617 temp = (xmlXPathObjectPtr *) xmlRealloc(cur->locTab, cur->locMax *
618 sizeof(xmlXPathObjectPtr));
620 xmlGenericError(xmlGenericErrorContext,
621 "xmlXPtrLocationSetAdd: out of memory\n");
626 cur->locTab[cur->locNr++] = val;
630 * xmlXPtrLocationSetMerge:
631 * @val1: the first LocationSet
632 * @val2: the second LocationSet
634 * Merges two rangesets, all ranges from @val2 are added to @val1
636 * Returns val1 once extended or NULL in case of error.
639 xmlXPtrLocationSetMerge(xmlLocationSetPtr val1, xmlLocationSetPtr val2) {
642 if (val1 == NULL) return(NULL);
643 if (val2 == NULL) return(val1);
646 * !!!!! this can be optimized a lot, knowing that both
647 * val1 and val2 already have unicity of their values.
650 for (i = 0;i < val2->locNr;i++)
651 xmlXPtrLocationSetAdd(val1, val2->locTab[i]);
657 * xmlXPtrLocationSetDel:
658 * @cur: the initial range set
659 * @val: an xmlXPathObjectPtr
661 * Removes an xmlXPathObjectPtr from an existing LocationSet
664 xmlXPtrLocationSetDel(xmlLocationSetPtr cur, xmlXPathObjectPtr val) {
667 if (cur == NULL) return;
668 if (val == NULL) return;
671 * check against doublons
673 for (i = 0;i < cur->locNr;i++)
674 if (cur->locTab[i] == val) break;
676 if (i >= cur->locNr) {
678 xmlGenericError(xmlGenericErrorContext,
679 "xmlXPtrLocationSetDel: Range wasn't found in RangeList\n");
684 for (;i < cur->locNr;i++)
685 cur->locTab[i] = cur->locTab[i + 1];
686 cur->locTab[cur->locNr] = NULL;
690 * xmlXPtrLocationSetRemove:
691 * @cur: the initial range set
692 * @val: the index to remove
694 * Removes an entry from an existing LocationSet list.
697 xmlXPtrLocationSetRemove(xmlLocationSetPtr cur, int val) {
698 if (cur == NULL) return;
699 if (val >= cur->locNr) return;
701 for (;val < cur->locNr;val++)
702 cur->locTab[val] = cur->locTab[val + 1];
703 cur->locTab[cur->locNr] = NULL;
707 * xmlXPtrFreeLocationSet:
708 * @obj: the xmlLocationSetPtr to free
710 * Free the LocationSet compound (not the actual ranges !).
713 xmlXPtrFreeLocationSet(xmlLocationSetPtr obj) {
716 if (obj == NULL) return;
717 if (obj->locTab != NULL) {
718 for (i = 0;i < obj->locNr; i++) {
719 xmlXPathFreeObject(obj->locTab[i]);
721 xmlFree(obj->locTab);
727 * xmlXPtrNewLocationSetNodes:
728 * @start: the start NodePtr value
729 * @end: the end NodePtr value or NULL
731 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
732 * it with the single range made of the two nodes @start and @end
734 * Returns the newly created object.
737 xmlXPtrNewLocationSetNodes(xmlNodePtr start, xmlNodePtr end) {
738 xmlXPathObjectPtr ret;
740 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
742 xmlGenericError(xmlGenericErrorContext,
743 "xmlXPtrNewLocationSetNodes: out of memory\n");
746 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
747 ret->type = XPATH_LOCATIONSET;
749 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewCollapsedRange(start));
751 ret->user = xmlXPtrLocationSetCreate(xmlXPtrNewRangeNodes(start,end));
756 * xmlXPtrNewLocationSetNodeSet:
759 * Create a new xmlXPathObjectPtr of type LocationSet and initialize
760 * it with all the nodes from @set
762 * Returns the newly created object.
765 xmlXPtrNewLocationSetNodeSet(xmlNodeSetPtr set) {
766 xmlXPathObjectPtr ret;
768 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
770 xmlGenericError(xmlGenericErrorContext,
771 "xmlXPtrNewLocationSetNodeSet: out of memory\n");
774 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
775 ret->type = XPATH_LOCATIONSET;
778 xmlLocationSetPtr newset;
780 newset = xmlXPtrLocationSetCreate(NULL);
784 for (i = 0;i < set->nodeNr;i++)
785 xmlXPtrLocationSetAdd(newset,
786 xmlXPtrNewCollapsedRange(set->nodeTab[i]));
788 ret->user = (void *) newset;
794 * xmlXPtrWrapLocationSet:
795 * @val: the LocationSet value
797 * Wrap the LocationSet @val in a new xmlXPathObjectPtr
799 * Returns the newly created object.
802 xmlXPtrWrapLocationSet(xmlLocationSetPtr val) {
803 xmlXPathObjectPtr ret;
805 ret = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
807 xmlGenericError(xmlGenericErrorContext,
808 "xmlXPtrWrapLocationSet: out of memory\n");
811 memset(ret, 0 , (size_t) sizeof(xmlXPathObject));
812 ret->type = XPATH_LOCATIONSET;
813 ret->user = (void *) val;
817 /************************************************************************
821 ************************************************************************/
823 static void xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name);
826 * Macros for accessing the content. Those should be used only by the parser,
829 * Dirty macros, i.e. one need to make assumption on the context to use them
831 * CUR_PTR return the current pointer to the xmlChar to be parsed.
832 * CUR returns the current xmlChar value, i.e. a 8 bit value
833 * in ISO-Latin or UTF-8.
834 * This should be used internally by the parser
835 * only to compare to ASCII values otherwise it would break when
836 * running with UTF-8 encoding.
837 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
838 * to compare on ASCII based substring.
839 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
840 * strings within the parser.
841 * CURRENT Returns the current char value, with the full decoding of
842 * UTF-8 if we are using this mode. It returns an int.
843 * NEXT Skip to the next character, this does the proper decoding
844 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
845 * It returns the pointer to the current xmlChar.
848 #define CUR (*ctxt->cur)
849 #define SKIP(val) ctxt->cur += (val)
850 #define NXT(val) ctxt->cur[(val)]
851 #define CUR_PTR ctxt->cur
853 #define SKIP_BLANKS \
854 while (IS_BLANK(*(ctxt->cur))) NEXT
856 #define CURRENT (*ctxt->cur)
857 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
861 * @ctxt: the XPointer Parser context
862 * @index: the child number
864 * Move the current node of the nodeset on the stack to the
865 * given child if found
868 xmlXPtrGetChildNo(xmlXPathParserContextPtr ctxt, int indx) {
869 xmlNodePtr cur = NULL;
870 xmlXPathObjectPtr obj;
871 xmlNodeSetPtr oldset;
873 CHECK_TYPE(XPATH_NODESET);
874 obj = valuePop(ctxt);
875 oldset = obj->nodesetval;
876 if ((indx <= 0) || (oldset == NULL) || (oldset->nodeNr != 1)) {
877 xmlXPathFreeObject(obj);
878 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
881 cur = xmlXPtrGetNthChild(oldset->nodeTab[0], indx);
883 xmlXPathFreeObject(obj);
884 valuePush(ctxt, xmlXPathNewNodeSet(NULL));
887 oldset->nodeTab[0] = cur;
888 valuePush(ctxt, obj);
892 * xmlXPtrEvalXPtrPart:
893 * @ctxt: the XPointer Parser context
894 * @name: the preparsed Scheme for the XPtrPart
896 * XPtrPart ::= 'xpointer' '(' XPtrExpr ')'
897 * | Scheme '(' SchemeSpecificExpr ')'
899 * Scheme ::= NCName - 'xpointer' [VC: Non-XPointer schemes]
901 * SchemeSpecificExpr ::= StringWithBalancedParens
903 * StringWithBalancedParens ::=
904 * [^()]* ('(' StringWithBalancedParens ')' [^()]*)*
905 * [VC: Parenthesis escaping]
907 * XPtrExpr ::= Expr [VC: Parenthesis escaping]
909 * VC: Parenthesis escaping:
910 * The end of an XPointer part is signaled by the right parenthesis ")"
911 * character that is balanced with the left parenthesis "(" character
912 * that began the part. Any unbalanced parenthesis character inside the
913 * expression, even within literals, must be escaped with a circumflex (^)
914 * character preceding it. If the expression contains any literal
915 * occurrences of the circumflex, each must be escaped with an additional
916 * circumflex (that is, ^^). If the unescaped parentheses in the expression
917 * are not balanced, a syntax error results.
919 * Parse and evaluate an XPtrPart. Basically it generates the unescaped
920 * string and if the scheme is 'xpointer' it will call the XPath interpreter.
922 * TODO: there is no new scheme registration mechanism
926 xmlXPtrEvalXPtrPart(xmlXPathParserContextPtr ctxt, xmlChar *name) {
927 xmlChar *buffer, *cur;
932 name = xmlXPathParseName(ctxt);
934 XP_ERROR(XPATH_EXPR_ERROR);
937 XP_ERROR(XPATH_EXPR_ERROR);
941 len = xmlStrlen(ctxt->cur);
943 buffer = (xmlChar *) xmlMallocAtomic(len * sizeof (xmlChar));
944 if (buffer == NULL) {
945 xmlGenericError(xmlGenericErrorContext,
946 "xmlXPtrEvalXPtrPart: out of memory\n");
959 } else if (CUR == '(') {
962 } else if (CUR == '^') {
964 if ((CUR == ')') || (CUR == '(') || (CUR == '^')) {
977 if ((level != 0) && (CUR == 0)) {
979 XP_ERROR(XPTR_SYNTAX_ERROR);
982 if (xmlStrEqual(name, (xmlChar *) "xpointer")) {
983 const xmlChar *left = CUR_PTR;
986 xmlXPathEvalExpr(ctxt);
988 } else if (xmlStrEqual(name, (xmlChar *) "element")) {
989 const xmlChar *left = CUR_PTR;
993 if (buffer[0] == '/') {
995 xmlXPtrEvalChildSeq(ctxt, NULL);
997 name2 = xmlXPathParseName(ctxt);
1001 XP_ERROR(XPATH_EXPR_ERROR);
1003 xmlXPtrEvalChildSeq(ctxt, name2);
1006 #ifdef XPTR_XMLNS_SCHEME
1007 } else if (xmlStrEqual(name, (xmlChar *) "xmlns")) {
1008 const xmlChar *left = CUR_PTR;
1014 prefix = xmlXPathParseNCName(ctxt);
1015 if (prefix == NULL) {
1018 XP_ERROR(XPTR_SYNTAX_ERROR);
1025 XP_ERROR(XPTR_SYNTAX_ERROR);
1029 /* @@ check escaping in the XPointer WD */
1031 value = xmlParseURI((const char *)ctxt->cur);
1032 if (value == NULL) {
1036 XP_ERROR(XPTR_SYNTAX_ERROR);
1038 URI = xmlSaveUri(value);
1044 XP_ERROR(XPATH_MEMORY_ERROR);
1047 xmlXPathRegisterNs(ctxt->context, prefix, URI);
1051 #endif /* XPTR_XMLNS_SCHEME */
1053 xmlGenericError(xmlGenericErrorContext,
1054 "unsupported scheme '%s'\n", name);
1061 * xmlXPtrEvalFullXPtr:
1062 * @ctxt: the XPointer Parser context
1063 * @name: the preparsed Scheme for the first XPtrPart
1065 * FullXPtr ::= XPtrPart (S? XPtrPart)*
1067 * As the specs says:
1069 * When multiple XPtrParts are provided, they must be evaluated in
1070 * left-to-right order. If evaluation of one part fails, the nexti
1071 * is evaluated. The following conditions cause XPointer part failure:
1073 * - An unknown scheme
1074 * - A scheme that does not locate any sub-resource present in the resource
1075 * - A scheme that is not applicable to the media type of the resource
1077 * The XPointer application must consume a failed XPointer part and
1078 * attempt to evaluate the next one, if any. The result of the first
1079 * XPointer part whose evaluation succeeds is taken to be the fragment
1080 * located by the XPointer as a whole. If all the parts fail, the result
1081 * for the XPointer as a whole is a sub-resource error.
1084 * Parse and evaluate a Full XPtr i.e. possibly a cascade of XPath based
1085 * expressions or other schemes.
1088 xmlXPtrEvalFullXPtr(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1090 name = xmlXPathParseName(ctxt);
1092 XP_ERROR(XPATH_EXPR_ERROR);
1093 while (name != NULL) {
1094 xmlXPtrEvalXPtrPart(ctxt, name);
1096 /* in case of syntax error, break here */
1097 if (ctxt->error != XPATH_EXPRESSION_OK)
1101 * If the returned value is a non-empty nodeset
1102 * or location set, return here.
1104 if (ctxt->value != NULL) {
1105 xmlXPathObjectPtr obj = ctxt->value;
1107 switch (obj->type) {
1108 case XPATH_LOCATIONSET: {
1109 xmlLocationSetPtr loc = ctxt->value->user;
1110 if ((loc != NULL) && (loc->locNr > 0))
1114 case XPATH_NODESET: {
1115 xmlNodeSetPtr loc = ctxt->value->nodesetval;
1116 if ((loc != NULL) && (loc->nodeNr > 0))
1125 * Evaluating to improper values is equivalent to
1126 * a sub-resource error, clean-up the stack
1129 obj = valuePop(ctxt);
1131 xmlXPathFreeObject(obj);
1133 } while (obj != NULL);
1137 * Is there another XPointer part.
1140 name = xmlXPathParseName(ctxt);
1145 * xmlXPtrEvalChildSeq:
1146 * @ctxt: the XPointer Parser context
1147 * @name: a possible ID name of the child sequence
1149 * ChildSeq ::= '/1' ('/' [0-9]*)*
1150 * | Name ('/' [0-9]*)+
1152 * Parse and evaluate a Child Sequence. This routine also handle the
1153 * case of a Bare Name used to get a document ID.
1156 xmlXPtrEvalChildSeq(xmlXPathParserContextPtr ctxt, xmlChar *name) {
1158 * XPointer don't allow by syntax to address in mutirooted trees
1159 * this might prove useful in some cases, warn about it.
1161 if ((name == NULL) && (CUR == '/') && (NXT(1) != '1')) {
1162 xmlGenericError(xmlGenericErrorContext,
1163 "warning: ChildSeq not starting by /1\n");
1167 valuePush(ctxt, xmlXPathNewString(name));
1169 xmlXPathIdFunction(ctxt, 1);
1173 while (CUR == '/') {
1177 while ((CUR >= '0') && (CUR <= '9')) {
1178 child = child * 10 + (CUR - '0');
1181 xmlXPtrGetChildNo(ctxt, child);
1187 * xmlXPtrEvalXPointer:
1188 * @ctxt: the XPointer Parser context
1194 * Parse and evaluate an XPointer
1197 xmlXPtrEvalXPointer(xmlXPathParserContextPtr ctxt) {
1198 if (ctxt->valueTab == NULL) {
1199 /* Allocate the value stack */
1200 ctxt->valueTab = (xmlXPathObjectPtr *)
1201 xmlMalloc(10 * sizeof(xmlXPathObjectPtr));
1202 if (ctxt->valueTab == NULL) {
1204 xmlGenericError(xmlGenericErrorContext,
1205 "xmlXPathEvalXPointer: out of memory\n");
1209 ctxt->valueMax = 10;
1215 xmlXPtrEvalChildSeq(ctxt, NULL);
1219 name = xmlXPathParseName(ctxt);
1221 XP_ERROR(XPATH_EXPR_ERROR);
1223 xmlXPtrEvalFullXPtr(ctxt, name);
1224 /* Short evaluation */
1227 /* this handle both Bare Names and Child Sequences */
1228 xmlXPtrEvalChildSeq(ctxt, name);
1233 XP_ERROR(XPATH_EXPR_ERROR);
1237 /************************************************************************
1239 * General routines *
1241 ************************************************************************/
1243 void xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1244 void xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1245 void xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs);
1246 void xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs);
1247 void xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs);
1248 void xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs);
1249 void xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs);
1252 * xmlXPtrNewContext:
1253 * @doc: the XML document
1254 * @here: the node that directly contains the XPointer being evaluated or NULL
1255 * @origin: the element from which a user or program initiated traversal of
1256 * the link, or NULL.
1258 * Create a new XPointer context
1260 * Returns the xmlXPathContext just allocated.
1263 xmlXPtrNewContext(xmlDocPtr doc, xmlNodePtr here, xmlNodePtr origin) {
1264 xmlXPathContextPtr ret;
1266 ret = xmlXPathNewContext(doc);
1271 ret->origin = origin;
1273 xmlXPathRegisterFunc(ret, (xmlChar *)"range-to",
1274 xmlXPtrRangeToFunction);
1275 xmlXPathRegisterFunc(ret, (xmlChar *)"range",
1276 xmlXPtrRangeFunction);
1277 xmlXPathRegisterFunc(ret, (xmlChar *)"range-inside",
1278 xmlXPtrRangeInsideFunction);
1279 xmlXPathRegisterFunc(ret, (xmlChar *)"string-range",
1280 xmlXPtrStringRangeFunction);
1281 xmlXPathRegisterFunc(ret, (xmlChar *)"start-point",
1282 xmlXPtrStartPointFunction);
1283 xmlXPathRegisterFunc(ret, (xmlChar *)"end-point",
1284 xmlXPtrEndPointFunction);
1285 xmlXPathRegisterFunc(ret, (xmlChar *)"here",
1286 xmlXPtrHereFunction);
1287 xmlXPathRegisterFunc(ret, (xmlChar *)" origin",
1288 xmlXPtrOriginFunction);
1295 * @str: the XPointer expression
1296 * @ctx: the XPointer context
1298 * Evaluate the XPath Location Path in the given context.
1300 * Returns the xmlXPathObjectPtr resulting from the evaluation or NULL.
1301 * the caller has to free the object.
1304 xmlXPtrEval(const xmlChar *str, xmlXPathContextPtr ctx) {
1305 xmlXPathParserContextPtr ctxt;
1306 xmlXPathObjectPtr res = NULL, tmp;
1307 xmlXPathObjectPtr init = NULL;
1312 if ((ctx == NULL) || (str == NULL))
1315 ctxt = xmlXPathNewParserContext(str, ctx);
1317 xmlXPtrEvalXPointer(ctxt);
1319 if ((ctxt->value != NULL) &&
1320 (ctxt->value->type != XPATH_NODESET) &&
1321 (ctxt->value->type != XPATH_LOCATIONSET)) {
1322 xmlGenericError(xmlGenericErrorContext,
1323 "xmlXPtrEval: evaluation failed to return a node set\n");
1325 res = valuePop(ctxt);
1329 tmp = valuePop(ctxt);
1332 if (tmp->type == XPATH_NODESET) {
1334 * Evaluation may push a root nodeset which is unused
1337 set = tmp->nodesetval;
1338 if ((set->nodeNr != 1) ||
1339 (set->nodeTab[0] != (xmlNodePtr) ctx->doc))
1344 xmlXPathFreeObject(tmp);
1346 } while (tmp != NULL);
1348 xmlGenericError(xmlGenericErrorContext,
1349 "xmlXPtrEval: %d object left on the stack\n",
1352 if (ctxt->error != XPATH_EXPRESSION_OK) {
1353 xmlXPathFreeObject(res);
1357 xmlXPathFreeParserContext(ctxt);
1362 * xmlXPtrBuildRangeNodeList:
1363 * @range: a range object
1365 * Build a node list tree copy of the range
1367 * Returns an xmlNodePtr list or NULL.
1368 * the caller has to free the node tree.
1371 xmlXPtrBuildRangeNodeList(xmlXPathObjectPtr range) {
1372 /* pointers to generated nodes */
1373 xmlNodePtr list = NULL, last = NULL, parent = NULL, tmp;
1374 /* pointers to traversal nodes */
1375 xmlNodePtr start, cur, end;
1380 if (range->type != XPATH_RANGE)
1382 start = (xmlNodePtr) range->user;
1388 return(xmlCopyNode(start, 1));
1391 index1 = range->index;
1392 index2 = range->index2;
1393 while (cur != NULL) {
1395 if (cur->type == XML_TEXT_NODE) {
1396 const xmlChar *content = cur->content;
1399 if (content == NULL) {
1400 tmp = xmlNewTextLen(NULL, 0);
1403 if ((cur == start) && (index1 > 1)) {
1404 content += (index1 - 1);
1405 len -= (index1 - 1);
1410 tmp = xmlNewTextLen(content, len);
1412 /* single sub text node selection */
1415 /* prune and return full set */
1417 xmlAddNextSibling(last, tmp);
1419 xmlAddChild(parent, tmp);
1422 tmp = xmlCopyNode(cur, 0);
1427 xmlAddNextSibling(last, tmp);
1429 xmlAddChild(parent, tmp);
1435 end = xmlXPtrGetNthChild(cur, index2 - 1);
1438 if ((cur == start) && (index1 > 1)) {
1439 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1442 cur = cur->children;
1445 * Now gather the remaining nodes from cur to end
1447 continue; /* while */
1449 } else if ((cur == start) &&
1450 (list == NULL) /* looks superfluous but ... */ ) {
1451 if ((cur->type == XML_TEXT_NODE) ||
1452 (cur->type == XML_CDATA_SECTION_NODE)) {
1453 const xmlChar *content = cur->content;
1455 if (content == NULL) {
1456 tmp = xmlNewTextLen(NULL, 0);
1459 content += (index1 - 1);
1461 tmp = xmlNewText(content);
1465 if ((cur == start) && (index1 > 1)) {
1466 tmp = xmlCopyNode(cur, 0);
1470 cur = xmlXPtrGetNthChild(cur, index1 - 1);
1473 * Now gather the remaining nodes from cur to end
1475 continue; /* while */
1477 tmp = xmlCopyNode(cur, 1);
1484 switch (cur->type) {
1486 case XML_ELEMENT_DECL:
1487 case XML_ATTRIBUTE_DECL:
1488 case XML_ENTITY_NODE:
1489 /* Do not copy DTD informations */
1491 case XML_ENTITY_DECL:
1492 TODO /* handle crossing entities -> stack needed */
1494 case XML_XINCLUDE_START:
1495 case XML_XINCLUDE_END:
1496 /* don't consider it part of the tree content */
1498 case XML_ATTRIBUTE_NODE:
1499 /* Humm, should not happen ! */
1503 tmp = xmlCopyNode(cur, 1);
1507 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1512 xmlAddNextSibling(last, tmp);
1514 xmlAddChild(parent, tmp);
1520 * Skip to next node in document order
1522 if ((list == NULL) || ((last == NULL) && (parent == NULL))) {
1526 cur = xmlXPtrAdvanceNode(cur);
1532 * xmlXPtrBuildNodeList:
1533 * @obj: the XPointer result from the evaluation.
1535 * Build a node list tree copy of the XPointer result.
1536 * This will drop Attributes and Namespace declarations.
1538 * Returns an xmlNodePtr list or NULL.
1539 * the caller has to free the node tree.
1542 xmlXPtrBuildNodeList(xmlXPathObjectPtr obj) {
1543 xmlNodePtr list = NULL, last = NULL;
1548 switch (obj->type) {
1549 case XPATH_NODESET: {
1550 xmlNodeSetPtr set = obj->nodesetval;
1553 for (i = 0;i < set->nodeNr;i++) {
1554 if (set->nodeTab[i] == NULL)
1556 switch (set->nodeTab[i]->type) {
1558 case XML_CDATA_SECTION_NODE:
1559 case XML_ELEMENT_NODE:
1560 case XML_ENTITY_REF_NODE:
1561 case XML_ENTITY_NODE:
1563 case XML_COMMENT_NODE:
1564 case XML_DOCUMENT_NODE:
1565 case XML_HTML_DOCUMENT_NODE:
1566 #ifdef LIBXML_DOCB_ENABLED
1567 case XML_DOCB_DOCUMENT_NODE:
1569 case XML_XINCLUDE_START:
1570 case XML_XINCLUDE_END:
1572 case XML_ATTRIBUTE_NODE:
1573 case XML_NAMESPACE_DECL:
1574 case XML_DOCUMENT_TYPE_NODE:
1575 case XML_DOCUMENT_FRAG_NODE:
1576 case XML_NOTATION_NODE:
1578 case XML_ELEMENT_DECL:
1579 case XML_ATTRIBUTE_DECL:
1580 case XML_ENTITY_DECL:
1584 list = last = xmlCopyNode(set->nodeTab[i], 1);
1586 xmlAddNextSibling(last, xmlCopyNode(set->nodeTab[i], 1));
1587 if (last->next != NULL)
1593 case XPATH_LOCATIONSET: {
1594 xmlLocationSetPtr set = (xmlLocationSetPtr) obj->user;
1597 for (i = 0;i < set->locNr;i++) {
1599 list = last = xmlXPtrBuildNodeList(set->locTab[i]);
1601 xmlAddNextSibling(last,
1602 xmlXPtrBuildNodeList(set->locTab[i]));
1604 while (last->next != NULL)
1611 return(xmlXPtrBuildRangeNodeList(obj));
1613 return(xmlCopyNode(obj->user, 0));
1620 /************************************************************************
1622 * XPointer functions *
1624 ************************************************************************/
1627 * xmlXPtrNbLocChildren:
1628 * @node: an xmlNodePtr
1630 * Count the number of location children of @node or the length of the
1631 * string value in case of text/PI/Comments nodes
1633 * Returns the number of location children
1636 xmlXPtrNbLocChildren(xmlNodePtr node) {
1640 switch (node->type) {
1641 case XML_HTML_DOCUMENT_NODE:
1642 case XML_DOCUMENT_NODE:
1643 case XML_ELEMENT_NODE:
1644 node = node->children;
1645 while (node != NULL) {
1646 if (node->type == XML_ELEMENT_NODE)
1651 case XML_ATTRIBUTE_NODE:
1655 case XML_COMMENT_NODE:
1657 case XML_CDATA_SECTION_NODE:
1658 case XML_ENTITY_REF_NODE:
1659 ret = xmlStrlen(node->content);
1668 * xmlXPtrHereFunction:
1669 * @ctxt: the XPointer Parser context
1670 * @nargs: the number of args
1672 * Function implementing here() operation
1673 * as described in 5.4.3
1676 xmlXPtrHereFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1679 if (ctxt->context->here == NULL)
1680 XP_ERROR(XPTR_SYNTAX_ERROR);
1682 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->here, NULL));
1686 * xmlXPtrOriginFunction:
1687 * @ctxt: the XPointer Parser context
1688 * @nargs: the number of args
1690 * Function implementing origin() operation
1691 * as described in 5.4.3
1694 xmlXPtrOriginFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1697 if (ctxt->context->origin == NULL)
1698 XP_ERROR(XPTR_SYNTAX_ERROR);
1700 valuePush(ctxt, xmlXPtrNewLocationSetNodes(ctxt->context->origin, NULL));
1704 * xmlXPtrStartPointFunction:
1705 * @ctxt: the XPointer Parser context
1706 * @nargs: the number of args
1708 * Function implementing start-point() operation
1709 * as described in 5.4.3
1711 * location-set start-point(location-set)
1713 * For each location x in the argument location-set, start-point adds a
1714 * location of type point to the result location-set. That point represents
1715 * the start point of location x and is determined by the following rules:
1717 * - If x is of type point, the start point is x.
1718 * - If x is of type range, the start point is the start point of x.
1719 * - If x is of type root, element, text, comment, or processing instruction,
1720 * - the container node of the start point is x and the index is 0.
1721 * - If x is of type attribute or namespace, the function must signal a
1727 xmlXPtrStartPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1728 xmlXPathObjectPtr tmp, obj, point;
1729 xmlLocationSetPtr newset = NULL;
1730 xmlLocationSetPtr oldset = NULL;
1733 if ((ctxt->value == NULL) ||
1734 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1735 (ctxt->value->type != XPATH_NODESET)))
1736 XP_ERROR(XPATH_INVALID_TYPE)
1738 obj = valuePop(ctxt);
1739 if (obj->type == XPATH_NODESET) {
1741 * First convert to a location set
1743 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1744 xmlXPathFreeObject(obj);
1748 newset = xmlXPtrLocationSetCreate(NULL);
1749 if (newset == NULL) {
1750 xmlXPathFreeObject(obj);
1751 XP_ERROR(XPATH_MEMORY_ERROR);
1753 oldset = (xmlLocationSetPtr) obj->user;
1754 if (oldset != NULL) {
1757 for (i = 0; i < oldset->locNr; i++) {
1758 tmp = oldset->locTab[i];
1762 switch (tmp->type) {
1764 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1767 xmlNodePtr node = tmp->user;
1769 if (node->type == XML_ATTRIBUTE_NODE) {
1770 /* TODO: Namespace Nodes ??? */
1771 xmlXPathFreeObject(obj);
1772 xmlXPtrFreeLocationSet(newset);
1773 XP_ERROR(XPTR_SYNTAX_ERROR);
1775 point = xmlXPtrNewPoint(node, tmp->index);
1780 /*** Should we raise an error ?
1781 xmlXPathFreeObject(obj);
1782 xmlXPathFreeObject(newset);
1783 XP_ERROR(XPATH_INVALID_TYPE)
1788 xmlXPtrLocationSetAdd(newset, point);
1791 xmlXPathFreeObject(obj);
1792 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1796 * xmlXPtrEndPointFunction:
1797 * @ctxt: the XPointer Parser context
1798 * @nargs: the number of args
1800 * Function implementing end-point() operation
1801 * as described in 5.4.3
1802 * ----------------------------
1803 * location-set end-point(location-set)
1805 * For each location x in the argument location-set, end-point adds a
1806 * location of type point to the result location-set. That point represents
1807 * the end point of location x and is determined by the following rules:
1809 * - If x is of type point, the resulting point is x.
1810 * - If x is of type range, the resulting point is the end point of x.
1811 * - If x is of type root or element, the container node of the resulting
1812 * point is x and the index is the number of location children of x.
1813 * - If x is of type text, comment, or processing instruction, the container
1814 * node of the resulting point is x and the index is the length of the
1815 * string-value of x.
1816 * - If x is of type attribute or namespace, the function must signal a
1818 * ----------------------------
1821 xmlXPtrEndPointFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1822 xmlXPathObjectPtr tmp, obj, point;
1823 xmlLocationSetPtr newset = NULL;
1824 xmlLocationSetPtr oldset = NULL;
1827 if ((ctxt->value == NULL) ||
1828 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1829 (ctxt->value->type != XPATH_NODESET)))
1830 XP_ERROR(XPATH_INVALID_TYPE)
1832 obj = valuePop(ctxt);
1833 if (obj->type == XPATH_NODESET) {
1835 * First convert to a location set
1837 tmp = xmlXPtrNewLocationSetNodeSet(obj->nodesetval);
1838 xmlXPathFreeObject(obj);
1842 newset = xmlXPtrLocationSetCreate(NULL);
1843 oldset = (xmlLocationSetPtr) obj->user;
1844 if (oldset != NULL) {
1847 for (i = 0; i < oldset->locNr; i++) {
1848 tmp = oldset->locTab[i];
1852 switch (tmp->type) {
1854 point = xmlXPtrNewPoint(tmp->user, tmp->index);
1857 xmlNodePtr node = tmp->user2;
1859 if (node->type == XML_ATTRIBUTE_NODE) {
1860 /* TODO: Namespace Nodes ??? */
1861 xmlXPathFreeObject(obj);
1862 xmlXPtrFreeLocationSet(newset);
1863 XP_ERROR(XPTR_SYNTAX_ERROR);
1865 point = xmlXPtrNewPoint(node, tmp->index2);
1866 } else if (tmp->user == NULL) {
1867 point = xmlXPtrNewPoint(node,
1868 xmlXPtrNbLocChildren(node));
1873 /*** Should we raise an error ?
1874 xmlXPathFreeObject(obj);
1875 xmlXPathFreeObject(newset);
1876 XP_ERROR(XPATH_INVALID_TYPE)
1881 xmlXPtrLocationSetAdd(newset, point);
1884 xmlXPathFreeObject(obj);
1885 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
1890 * xmlXPtrCoveringRange:
1891 * @ctxt: the XPointer Parser context
1892 * @loc: the location for which the covering range must be computed
1894 * A covering range is a range that wholly encompasses a location
1895 * Section 5.3.3. Covering Ranges for All Location Types
1896 * http://www.w3.org/TR/xptr#N2267
1898 * Returns a new location or NULL in case of error
1900 static xmlXPathObjectPtr
1901 xmlXPtrCoveringRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
1904 if ((ctxt == NULL) || (ctxt->context == NULL) ||
1905 (ctxt->context->doc == NULL))
1907 switch (loc->type) {
1909 return(xmlXPtrNewRange(loc->user, loc->index,
1910 loc->user, loc->index));
1912 if (loc->user2 != NULL) {
1913 return(xmlXPtrNewRange(loc->user, loc->index,
1914 loc->user2, loc->index2));
1916 xmlNodePtr node = (xmlNodePtr) loc->user;
1917 if (node == (xmlNodePtr) ctxt->context->doc) {
1918 return(xmlXPtrNewRange(node, 0, node,
1919 xmlXPtrGetArity(node)));
1921 switch (node->type) {
1922 case XML_ATTRIBUTE_NODE:
1923 /* !!! our model is slightly different than XPath */
1924 return(xmlXPtrNewRange(node, 0, node,
1925 xmlXPtrGetArity(node)));
1926 case XML_ELEMENT_NODE:
1928 case XML_CDATA_SECTION_NODE:
1929 case XML_ENTITY_REF_NODE:
1931 case XML_COMMENT_NODE:
1932 case XML_DOCUMENT_NODE:
1933 case XML_NOTATION_NODE:
1934 case XML_HTML_DOCUMENT_NODE: {
1935 int indx = xmlXPtrGetIndex(node);
1937 node = node->parent;
1938 return(xmlXPtrNewRange(node, indx - 1,
1947 TODO /* missed one case ??? */
1953 * xmlXPtrRangeFunction:
1954 * @ctxt: the XPointer Parser context
1955 * @nargs: the number of args
1957 * Function implementing the range() function 5.4.3
1958 * location-set range(location-set )
1960 * The range function returns ranges covering the locations in
1961 * the argument location-set. For each location x in the argument
1962 * location-set, a range location representing the covering range of
1963 * x is added to the result location-set.
1966 xmlXPtrRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
1968 xmlXPathObjectPtr set;
1969 xmlLocationSetPtr oldset;
1970 xmlLocationSetPtr newset;
1973 if ((ctxt->value == NULL) ||
1974 ((ctxt->value->type != XPATH_LOCATIONSET) &&
1975 (ctxt->value->type != XPATH_NODESET)))
1976 XP_ERROR(XPATH_INVALID_TYPE)
1978 set = valuePop(ctxt);
1979 if (set->type == XPATH_NODESET) {
1980 xmlXPathObjectPtr tmp;
1983 * First convert to a location set
1985 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
1986 xmlXPathFreeObject(set);
1989 oldset = (xmlLocationSetPtr) set->user;
1992 * The loop is to compute the covering range for each item and add it
1994 newset = xmlXPtrLocationSetCreate(NULL);
1995 for (i = 0;i < oldset->locNr;i++) {
1996 xmlXPtrLocationSetAdd(newset,
1997 xmlXPtrCoveringRange(ctxt, oldset->locTab[i]));
2001 * Save the new value and cleanup
2003 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2004 xmlXPathFreeObject(set);
2008 * xmlXPtrInsideRange:
2009 * @ctxt: the XPointer Parser context
2010 * @loc: the location for which the inside range must be computed
2012 * A inside range is a range described in the range-inside() description
2014 * Returns a new location or NULL in case of error
2016 static xmlXPathObjectPtr
2017 xmlXPtrInsideRange(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr loc) {
2020 if ((ctxt == NULL) || (ctxt->context == NULL) ||
2021 (ctxt->context->doc == NULL))
2023 switch (loc->type) {
2025 xmlNodePtr node = (xmlNodePtr) loc->user;
2026 switch (node->type) {
2028 case XML_COMMENT_NODE:
2030 case XML_CDATA_SECTION_NODE: {
2031 if (node->content == NULL) {
2032 return(xmlXPtrNewRange(node, 0, node, 0));
2034 return(xmlXPtrNewRange(node, 0, node,
2035 xmlStrlen(node->content)));
2038 case XML_ATTRIBUTE_NODE:
2039 case XML_ELEMENT_NODE:
2040 case XML_ENTITY_REF_NODE:
2041 case XML_DOCUMENT_NODE:
2042 case XML_NOTATION_NODE:
2043 case XML_HTML_DOCUMENT_NODE: {
2044 return(xmlXPtrNewRange(node, 0, node,
2045 xmlXPtrGetArity(node)));
2053 xmlNodePtr node = (xmlNodePtr) loc->user;
2054 if (loc->user2 != NULL) {
2055 return(xmlXPtrNewRange(node, loc->index,
2056 loc->user2, loc->index2));
2058 switch (node->type) {
2060 case XML_COMMENT_NODE:
2062 case XML_CDATA_SECTION_NODE: {
2063 if (node->content == NULL) {
2064 return(xmlXPtrNewRange(node, 0, node, 0));
2066 return(xmlXPtrNewRange(node, 0, node,
2067 xmlStrlen(node->content)));
2070 case XML_ATTRIBUTE_NODE:
2071 case XML_ELEMENT_NODE:
2072 case XML_ENTITY_REF_NODE:
2073 case XML_DOCUMENT_NODE:
2074 case XML_NOTATION_NODE:
2075 case XML_HTML_DOCUMENT_NODE: {
2076 return(xmlXPtrNewRange(node, 0, node,
2077 xmlXPtrGetArity(node)));
2086 TODO /* missed one case ??? */
2092 * xmlXPtrRangeInsideFunction:
2093 * @ctxt: the XPointer Parser context
2094 * @nargs: the number of args
2096 * Function implementing the range-inside() function 5.4.3
2097 * location-set range-inside(location-set )
2099 * The range-inside function returns ranges covering the contents of
2100 * the locations in the argument location-set. For each location x in
2101 * the argument location-set, a range location is added to the result
2102 * location-set. If x is a range location, then x is added to the
2103 * result location-set. If x is not a range location, then x is used
2104 * as the container location of the start and end points of the range
2105 * location to be added; the index of the start point of the range is
2106 * zero; if the end point is a character point then its index is the
2107 * length of the string-value of x, and otherwise is the number of
2108 * location children of x.
2112 xmlXPtrRangeInsideFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2114 xmlXPathObjectPtr set;
2115 xmlLocationSetPtr oldset;
2116 xmlLocationSetPtr newset;
2119 if ((ctxt->value == NULL) ||
2120 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2121 (ctxt->value->type != XPATH_NODESET)))
2122 XP_ERROR(XPATH_INVALID_TYPE)
2124 set = valuePop(ctxt);
2125 if (set->type == XPATH_NODESET) {
2126 xmlXPathObjectPtr tmp;
2129 * First convert to a location set
2131 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2132 xmlXPathFreeObject(set);
2135 oldset = (xmlLocationSetPtr) set->user;
2138 * The loop is to compute the covering range for each item and add it
2140 newset = xmlXPtrLocationSetCreate(NULL);
2141 for (i = 0;i < oldset->locNr;i++) {
2142 xmlXPtrLocationSetAdd(newset,
2143 xmlXPtrInsideRange(ctxt, oldset->locTab[i]));
2147 * Save the new value and cleanup
2149 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2150 xmlXPathFreeObject(set);
2154 * xmlXPtrRangeToFunction:
2155 * @ctxt: the XPointer Parser context
2156 * @nargs: the number of args
2158 * Implement the range-to() XPointer function
2161 xmlXPtrRangeToFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2162 xmlXPathObjectPtr range;
2164 xmlXPathObjectPtr res, obj;
2165 xmlXPathObjectPtr tmp;
2166 xmlLocationSetPtr newset = NULL;
2167 xmlNodeSetPtr oldset;
2172 * Save the expression pointer since we will have to evaluate
2173 * it multiple times. Initialize the new set.
2175 CHECK_TYPE(XPATH_NODESET);
2176 obj = valuePop(ctxt);
2177 oldset = obj->nodesetval;
2178 ctxt->context->node = NULL;
2181 newset = xmlXPtrLocationSetCreate(NULL);
2183 for (i = 0; i < oldset->nodeNr; i++) {
2187 * Run the evaluation with a node list made of a single item
2190 ctxt->context->node = oldset->nodeTab[i];
2191 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2192 valuePush(ctxt, tmp);
2194 xmlXPathEvalExpr(ctxt);
2198 * The result of the evaluation need to be tested to
2199 * decided whether the filter succeeded or not
2201 res = valuePop(ctxt);
2202 range = xmlXPtrNewRangeNodeObject(oldset->nodeTab[i], res);
2203 if (range != NULL) {
2204 xmlXPtrLocationSetAdd(newset, range);
2211 xmlXPathFreeObject(res);
2212 if (ctxt->value == tmp) {
2213 res = valuePop(ctxt);
2214 xmlXPathFreeObject(res);
2217 ctxt->context->node = NULL;
2221 * The result is used as the new evaluation set.
2223 xmlXPathFreeObject(obj);
2224 ctxt->context->node = NULL;
2225 ctxt->context->contextSize = -1;
2226 ctxt->context->proximityPosition = -1;
2227 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2231 * xmlXPtrAdvanceNode:
2234 * Advance to the next element or text node in document order
2235 * TODO: add a stack for entering/exiting entities
2237 * Returns -1 in case of failure, 0 otherwise
2240 xmlXPtrAdvanceNode(xmlNodePtr cur) {
2244 if (cur->children != NULL) {
2245 cur = cur->children ;
2248 if (cur->next != NULL) {
2254 if (cur == NULL) return(NULL);
2255 if (cur->next != NULL) {
2259 } while (cur != NULL);
2262 if ((cur->type != XML_ELEMENT_NODE) &&
2263 (cur->type != XML_TEXT_NODE) &&
2264 (cur->type != XML_DOCUMENT_NODE) &&
2265 (cur->type != XML_HTML_DOCUMENT_NODE) &&
2266 (cur->type != XML_CDATA_SECTION_NODE))
2268 if (cur->type == XML_ENTITY_REF_NODE) {
2275 * xmlXPtrAdvanceChar:
2278 * @bytes: the number of bytes
2280 * Advance a point of the associated number of bytes (not UTF8 chars)
2282 * Returns -1 in case of failure, 0 otherwise
2285 xmlXPtrAdvanceChar(xmlNodePtr *node, int *indx, int bytes) {
2290 if ((node == NULL) || (indx == NULL))
2297 while (bytes >= 0) {
2299 * First position to the beginning of the first text node
2300 * corresponding to this point
2302 while ((cur != NULL) &&
2303 ((cur->type == XML_ELEMENT_NODE) ||
2304 (cur->type == XML_DOCUMENT_NODE) ||
2305 (cur->type == XML_HTML_DOCUMENT_NODE))) {
2307 cur = xmlXPtrGetNthChild(cur, pos);
2310 cur = xmlXPtrAdvanceNode(cur);
2322 * if there is no move needed return the current value.
2324 if (pos == 0) pos = 1;
2331 * We should have a text (or cdata) node ...
2334 if ((cur->type != XML_ELEMENT_NODE) &&
2335 (cur->content != NULL)) {
2336 len = xmlStrlen(cur->content);
2339 /* Strange, the indx in the text node is greater than it's len */
2343 if (pos + bytes >= len) {
2344 bytes -= (len - pos);
2345 cur = xmlXPtrAdvanceNode(cur);
2347 } else if (pos + bytes < len) {
2358 * xmlXPtrMatchString:
2359 * @string: the string to search
2360 * @start: the start textnode
2361 * @startindex: the start index
2362 * @end: the end textnode IN/OUT
2363 * @endindex: the end index IN/OUT
2365 * Check whether the document contains @string at the position
2366 * (@start, @startindex) and limited by the (@end, @endindex) point
2368 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2369 * (@start, @startindex) will indicate the position of the beginning
2370 * of the range and (@end, @endindex) will indicate the end
2374 xmlXPtrMatchString(const xmlChar *string, xmlNodePtr start, int startindex,
2375 xmlNodePtr *end, int *endindex) {
2377 int pos; /* 0 based */
2378 int len; /* in bytes */
2379 int stringlen; /* in bytes */
2386 if ((end == NULL) || (endindex == NULL))
2391 pos = startindex - 1;
2392 stringlen = xmlStrlen(string);
2394 while (stringlen > 0) {
2395 if ((cur == *end) && (pos + stringlen > *endindex))
2398 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
2399 len = xmlStrlen(cur->content);
2400 if (len >= pos + stringlen) {
2401 match = (!xmlStrncmp(&cur->content[pos], string, stringlen));
2404 xmlGenericError(xmlGenericErrorContext,
2405 "found range %d bytes at index %d of ->",
2406 stringlen, pos + 1);
2407 xmlDebugDumpString(stdout, cur->content);
2408 xmlGenericError(xmlGenericErrorContext, "\n");
2411 *endindex = pos + stringlen;
2417 int sub = len - pos;
2418 match = (!xmlStrncmp(&cur->content[pos], string, sub));
2421 xmlGenericError(xmlGenericErrorContext,
2422 "found subrange %d bytes at index %d of ->",
2424 xmlDebugDumpString(stdout, cur->content);
2425 xmlGenericError(xmlGenericErrorContext, "\n");
2427 string = &string[sub];
2434 cur = xmlXPtrAdvanceNode(cur);
2443 * xmlXPtrSearchString:
2444 * @string: the string to search
2445 * @start: the start textnode IN/OUT
2446 * @startindex: the start index IN/OUT
2447 * @end: the end textnode
2448 * @endindex: the end index
2450 * Search the next occurrence of @string within the document content
2451 * until the (@end, @endindex) point is reached
2453 * Returns -1 in case of failure, 0 if not found, 1 if found in which case
2454 * (@start, @startindex) will indicate the position of the beginning
2455 * of the range and (@end, @endindex) will indicate the end
2459 xmlXPtrSearchString(const xmlChar *string, xmlNodePtr *start, int *startindex,
2460 xmlNodePtr *end, int *endindex) {
2463 int pos; /* 0 based */
2464 int len; /* in bytes */
2469 if ((start == NULL) || (startindex == NULL))
2471 if ((end == NULL) || (endindex == NULL))
2476 pos = *startindex - 1;
2479 while (cur != NULL) {
2480 if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
2481 len = xmlStrlen(cur->content);
2482 while (pos <= len) {
2484 str = xmlStrchr(&cur->content[pos], first);
2486 pos = (str - (xmlChar *)(cur->content));
2488 xmlGenericError(xmlGenericErrorContext,
2489 "found '%c' at index %d of ->",
2491 xmlDebugDumpString(stdout, cur->content);
2492 xmlGenericError(xmlGenericErrorContext, "\n");
2494 if (xmlXPtrMatchString(string, cur, pos + 1,
2497 *startindex = pos + 1;
2506 * An empty string is considered to match before each
2507 * character of the string-value and after the final
2511 xmlGenericError(xmlGenericErrorContext,
2512 "found '' at index %d of ->",
2514 xmlDebugDumpString(stdout, cur->content);
2515 xmlGenericError(xmlGenericErrorContext, "\n");
2518 *startindex = pos + 1;
2520 *endindex = pos + 1;
2525 if ((cur == *end) && (pos >= *endindex))
2527 cur = xmlXPtrAdvanceNode(cur);
2536 * xmlXPtrGetLastChar:
2540 * Computes the point coordinates of the last char of this point
2542 * Returns -1 in case of failure, 0 otherwise
2545 xmlXPtrGetLastChar(xmlNodePtr *node, int *indx) {
2549 if ((node == NULL) || (indx == NULL))
2557 if ((cur->type == XML_ELEMENT_NODE) ||
2558 (cur->type == XML_DOCUMENT_NODE) ||
2559 (cur->type == XML_HTML_DOCUMENT_NODE)) {
2561 cur = xmlXPtrGetNthChild(cur, pos);
2565 while (cur != NULL) {
2566 if (cur->last != NULL)
2568 else if ((cur->type != XML_ELEMENT_NODE) &&
2569 (cur->content != NULL)) {
2570 len = xmlStrlen(cur->content);
2584 * xmlXPtrGetStartPoint:
2586 * @node: the resulting node
2587 * @indx: the resulting index
2589 * read the object and return the start point coordinates.
2591 * Returns -1 in case of failure, 0 otherwise
2594 xmlXPtrGetStartPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2595 if ((obj == NULL) || (node == NULL) || (indx == NULL))
2598 switch (obj->type) {
2601 if (obj->index <= 0)
2608 if (obj->index <= 0)
2620 * xmlXPtrGetEndPoint:
2622 * @node: the resulting node
2623 * @indx: the resulting indx
2625 * read the object and return the end point coordinates.
2627 * Returns -1 in case of failure, 0 otherwise
2630 xmlXPtrGetEndPoint(xmlXPathObjectPtr obj, xmlNodePtr *node, int *indx) {
2631 if ((obj == NULL) || (node == NULL) || (indx == NULL))
2634 switch (obj->type) {
2637 if (obj->index <= 0)
2644 if (obj->index <= 0)
2656 * xmlXPtrStringRangeFunction:
2657 * @ctxt: the XPointer Parser context
2658 * @nargs: the number of args
2660 * Function implementing the string-range() function
2661 * range as described in 5.4.2
2663 * ------------------------------
2664 * [Definition: For each location in the location-set argument,
2665 * string-range returns a set of string ranges, a set of substrings in a
2666 * string. Specifically, the string-value of the location is searched for
2667 * substrings that match the string argument, and the resulting location-set
2668 * will contain a range location for each non-overlapping match.]
2669 * An empty string is considered to match before each character of the
2670 * string-value and after the final character. Whitespace in a string
2671 * is matched literally, with no normalization except that provided by
2672 * XML for line ends. The third argument gives the position of the first
2673 * character to be in the resulting range, relative to the start of the
2674 * match. The default value is 1, which makes the range start immediately
2675 * before the first character of the matched string. The fourth argument
2676 * gives the number of characters in the range; the default is that the
2677 * range extends to the end of the matched string.
2679 * Element boundaries, as well as entire embedded nodes such as processing
2680 * instructions and comments, are ignored as defined in [XPath].
2682 * If the string in the second argument is not found in the string-value
2683 * of the location, or if a value in the third or fourth argument indicates
2684 * a string that is beyond the beginning or end of the document, the
2687 * The points of the range-locations in the returned location-set will
2688 * all be character points.
2689 * ------------------------------
2692 xmlXPtrStringRangeFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2693 int i, startindex, endindex, fendindex;
2694 xmlNodePtr start, end, fend;
2695 xmlXPathObjectPtr set;
2696 xmlLocationSetPtr oldset;
2697 xmlLocationSetPtr newset;
2698 xmlXPathObjectPtr string;
2699 xmlXPathObjectPtr position = NULL;
2700 xmlXPathObjectPtr number = NULL;
2701 int found, pos = 0, num = 0;
2704 * Grab the arguments
2706 if ((nargs < 2) || (nargs > 4))
2707 XP_ERROR(XPATH_INVALID_ARITY);
2710 CHECK_TYPE(XPATH_NUMBER);
2711 number = valuePop(ctxt);
2713 num = (int) number->floatval;
2716 CHECK_TYPE(XPATH_NUMBER);
2717 position = valuePop(ctxt);
2718 if (position != NULL)
2719 pos = (int) position->floatval;
2721 CHECK_TYPE(XPATH_STRING);
2722 string = valuePop(ctxt);
2723 if ((ctxt->value == NULL) ||
2724 ((ctxt->value->type != XPATH_LOCATIONSET) &&
2725 (ctxt->value->type != XPATH_NODESET)))
2726 XP_ERROR(XPATH_INVALID_TYPE)
2728 set = valuePop(ctxt);
2729 if (set->type == XPATH_NODESET) {
2730 xmlXPathObjectPtr tmp;
2733 * First convert to a location set
2735 tmp = xmlXPtrNewLocationSetNodeSet(set->nodesetval);
2736 xmlXPathFreeObject(set);
2739 oldset = (xmlLocationSetPtr) set->user;
2742 * The loop is to search for each element in the location set
2743 * the list of location set corresponding to that search
2745 newset = xmlXPtrLocationSetCreate(NULL);
2746 for (i = 0;i < oldset->locNr;i++) {
2748 xmlXPathDebugDumpObject(stdout, oldset->locTab[i], 0);
2751 xmlXPtrGetStartPoint(oldset->locTab[i], &start, &startindex);
2752 xmlXPtrGetEndPoint(oldset->locTab[i], &end, &endindex);
2753 xmlXPtrAdvanceChar(&start, &startindex, 0);
2754 xmlXPtrGetLastChar(&end, &endindex);
2757 xmlGenericError(xmlGenericErrorContext,
2758 "from index %d of ->", startindex);
2759 xmlDebugDumpString(stdout, start->content);
2760 xmlGenericError(xmlGenericErrorContext, "\n");
2761 xmlGenericError(xmlGenericErrorContext,
2762 "to index %d of ->", endindex);
2763 xmlDebugDumpString(stdout, end->content);
2764 xmlGenericError(xmlGenericErrorContext, "\n");
2768 fendindex = endindex;
2769 found = xmlXPtrSearchString(string->stringval, &start, &startindex,
2772 if (position == NULL) {
2773 xmlXPtrLocationSetAdd(newset,
2774 xmlXPtrNewRange(start, startindex, fend, fendindex));
2775 } else if (xmlXPtrAdvanceChar(&start, &startindex,
2777 if ((number != NULL) && (num > 0)) {
2781 rindx = startindex - 1;
2782 if (xmlXPtrAdvanceChar(&rend, &rindx,
2784 xmlXPtrLocationSetAdd(newset,
2785 xmlXPtrNewRange(start, startindex,
2788 } else if ((number != NULL) && (num <= 0)) {
2789 xmlXPtrLocationSetAdd(newset,
2790 xmlXPtrNewRange(start, startindex,
2791 start, startindex));
2793 xmlXPtrLocationSetAdd(newset,
2794 xmlXPtrNewRange(start, startindex,
2799 startindex = fendindex;
2800 if (string->stringval[0] == 0)
2803 } while (found == 1);
2807 * Save the new value and cleanup
2809 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2810 xmlXPathFreeObject(set);
2811 xmlXPathFreeObject(string);
2812 if (position) xmlXPathFreeObject(position);
2813 if (number) xmlXPathFreeObject(number);
2817 * xmlXPtrEvalRangePredicate:
2818 * @ctxt: the XPointer Parser context
2820 * [8] Predicate ::= '[' PredicateExpr ']'
2821 * [9] PredicateExpr ::= Expr
2823 * Evaluate a predicate as in xmlXPathEvalPredicate() but for
2824 * a Location Set instead of a node set
2827 xmlXPtrEvalRangePredicate(xmlXPathParserContextPtr ctxt) {
2829 xmlXPathObjectPtr res;
2830 xmlXPathObjectPtr obj, tmp;
2831 xmlLocationSetPtr newset = NULL;
2832 xmlLocationSetPtr oldset;
2837 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);
2843 * Extract the old set, and then evaluate the result of the
2844 * expression for all the element in the set. use it to grow
2847 CHECK_TYPE(XPATH_LOCATIONSET);
2848 obj = valuePop(ctxt);
2850 ctxt->context->node = NULL;
2852 if ((oldset == NULL) || (oldset->locNr == 0)) {
2853 ctxt->context->contextSize = 0;
2854 ctxt->context->proximityPosition = 0;
2855 xmlXPathEvalExpr(ctxt);
2856 res = valuePop(ctxt);
2858 xmlXPathFreeObject(res);
2859 valuePush(ctxt, obj);
2863 * Save the expression pointer since we will have to evaluate
2864 * it multiple times. Initialize the new set.
2867 newset = xmlXPtrLocationSetCreate(NULL);
2869 for (i = 0; i < oldset->locNr; i++) {
2873 * Run the evaluation with a node list made of a single item
2876 ctxt->context->node = oldset->locTab[i]->user;
2877 tmp = xmlXPathNewNodeSet(ctxt->context->node);
2878 valuePush(ctxt, tmp);
2879 ctxt->context->contextSize = oldset->locNr;
2880 ctxt->context->proximityPosition = i + 1;
2882 xmlXPathEvalExpr(ctxt);
2886 * The result of the evaluation need to be tested to
2887 * decided whether the filter succeeded or not
2889 res = valuePop(ctxt);
2890 if (xmlXPathEvaluatePredicateResult(ctxt, res)) {
2891 xmlXPtrLocationSetAdd(newset,
2892 xmlXPathObjectCopy(oldset->locTab[i]));
2899 xmlXPathFreeObject(res);
2900 if (ctxt->value == tmp) {
2901 res = valuePop(ctxt);
2902 xmlXPathFreeObject(res);
2905 ctxt->context->node = NULL;
2909 * The result is used as the new evaluation set.
2911 xmlXPathFreeObject(obj);
2912 ctxt->context->node = NULL;
2913 ctxt->context->contextSize = -1;
2914 ctxt->context->proximityPosition = -1;
2915 valuePush(ctxt, xmlXPtrWrapLocationSet(newset));
2918 XP_ERROR(XPATH_INVALID_PREDICATE_ERROR);