2 * pattern.c: Implemetation of the template match compilation and lookup
5 * http://www.w3.org/TR/1999/REC-xslt-19991116
7 * See Copyright for the status of this software.
13 * TODO: handle pathological cases like *[*[@a="b"]]
14 * TODO: detect [number] at compilation, optimize accordingly
22 #include <libxml/xmlmemory.h>
23 #include <libxml/tree.h>
24 #include <libxml/valid.h>
25 #include <libxml/hash.h>
26 #include <libxml/xmlerror.h>
27 #include <libxml/parserInternals.h>
29 #include "xsltInternals.h"
30 #include "xsltutils.h"
32 #include "templates.h"
36 #ifdef WITH_XSLT_DEBUG
37 #define WITH_XSLT_DEBUG_PATTERN
63 typedef struct _xsltStepState xsltStepState;
64 typedef xsltStepState *xsltStepStatePtr;
65 struct _xsltStepState {
70 typedef struct _xsltStepStates xsltStepStates;
71 typedef xsltStepStates *xsltStepStatesPtr;
72 struct _xsltStepStates {
75 xsltStepStatePtr states;
78 typedef struct _xsltStepOp xsltStepOp;
79 typedef xsltStepOp *xsltStepOpPtr;
85 xmlXPathCompExprPtr comp;
87 * Optimisations for count
94 struct _xsltCompMatch {
95 struct _xsltCompMatch *next; /* siblings in the name hash */
96 float priority; /* the priority */
97 const xmlChar *pattern; /* the pattern */
98 const xmlChar *mode; /* the mode */
99 const xmlChar *modeURI; /* the mode URI */
100 xsltTemplatePtr template; /* the associated template */
103 /* TODO fix the statically allocated size steps[] */
106 xmlNsPtr *nsList; /* the namespaces in scope */
107 int nsNr; /* the number of namespaces in scope */
108 xsltStepOp steps[40]; /* ops for computation */
111 typedef struct _xsltParserContext xsltParserContext;
112 typedef xsltParserContext *xsltParserContextPtr;
113 struct _xsltParserContext {
114 xsltStylesheetPtr style; /* the stylesheet */
115 xsltTransformContextPtr ctxt; /* the transformation or NULL */
116 const xmlChar *cur; /* the current char being parsed */
117 const xmlChar *base; /* the full expression */
118 xmlDocPtr doc; /* the source document */
119 xmlNodePtr elem; /* the source element */
120 int error; /* error code */
121 xsltCompMatchPtr comp; /* the result */
124 /************************************************************************
128 ************************************************************************/
133 * Create a new XSLT CompMatch
135 * Returns the newly allocated xsltCompMatchPtr or NULL in case of error
137 static xsltCompMatchPtr
138 xsltNewCompMatch(void) {
139 xsltCompMatchPtr cur;
141 cur = (xsltCompMatchPtr) xmlMalloc(sizeof(xsltCompMatch));
143 xsltTransformError(NULL, NULL, NULL,
144 "xsltNewCompMatch : malloc failed\n");
147 memset(cur, 0, sizeof(xsltCompMatch));
157 * @comp: an XSLT comp
159 * Free up the memory allocated by @comp
162 xsltFreeCompMatch(xsltCompMatchPtr comp) {
168 if (comp->pattern != NULL)
169 xmlFree((xmlChar *)comp->pattern);
170 if (comp->nsList != NULL)
171 xmlFree(comp->nsList);
172 for (i = 0;i < comp->nbStep;i++) {
173 op = &comp->steps[i];
174 if (op->value != NULL)
176 if (op->value2 != NULL)
178 if (op->value3 != NULL)
180 if (op->comp != NULL)
181 xmlXPathFreeCompExpr(op->comp);
183 memset(comp, -1, sizeof(xsltCompMatch));
188 * xsltFreeCompMatchList:
189 * @comp: an XSLT comp list
191 * Free up the memory allocated by all the elements of @comp
194 xsltFreeCompMatchList(xsltCompMatchPtr comp) {
195 xsltCompMatchPtr cur;
197 while (comp != NULL) {
200 xsltFreeCompMatch(cur);
205 * xsltNormalizeCompSteps:
206 * @payload: pointer to template hash table entry
207 * @data: pointer to the stylesheet
208 * @name: template match name
210 * This is a hashtable scanner function to normalize the compiled
211 * steps of an imported stylesheet.
213 void xsltNormalizeCompSteps(void *payload,
214 void *data, const xmlChar *name ATTRIBUTE_UNUSED) {
215 xsltCompMatchPtr comp = payload;
216 xsltStylesheetPtr style = data;
219 for (ix = 0; ix < comp->nbStep; ix++) {
220 comp->steps[ix].previousExtra += style->extrasNr;
221 comp->steps[ix].indexExtra += style->extrasNr;
222 comp->steps[ix].lenExtra += style->extrasNr;
227 * xsltNewParserContext:
228 * @style: the stylesheet
229 * @ctxt: the transformation context, if done at run-time
231 * Create a new XSLT ParserContext
233 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
235 static xsltParserContextPtr
236 xsltNewParserContext(xsltStylesheetPtr style, xsltTransformContextPtr ctxt) {
237 xsltParserContextPtr cur;
239 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
241 xsltTransformError(NULL, NULL, NULL,
242 "xsltNewParserContext : malloc failed\n");
245 memset(cur, 0, sizeof(xsltParserContext));
252 * xsltFreeParserContext:
253 * @ctxt: an XSLT parser context
255 * Free up the memory allocated by @ctxt
258 xsltFreeParserContext(xsltParserContextPtr ctxt) {
261 memset(ctxt, -1, sizeof(xsltParserContext));
267 * @comp: the compiled match expression
269 * @value: the first value
270 * @value2: the second value
272 * Add an step to an XSLT Compiled Match
274 * Returns -1 in case of failure, 0 otherwise.
277 xsltCompMatchAdd(xsltParserContextPtr ctxt, xsltCompMatchPtr comp,
278 xsltOp op, xmlChar * value, xmlChar * value2)
280 if (comp->nbStep >= 40) {
281 xsltTransformError(NULL, NULL, NULL,
282 "xsltCompMatchAdd: overflow\n");
285 comp->steps[comp->nbStep].op = op;
286 comp->steps[comp->nbStep].value = value;
287 comp->steps[comp->nbStep].value2 = value2;
288 if (ctxt->ctxt != NULL) {
289 comp->steps[comp->nbStep].previousExtra =
290 xsltAllocateExtraCtxt(ctxt->ctxt);
291 comp->steps[comp->nbStep].indexExtra =
292 xsltAllocateExtraCtxt(ctxt->ctxt);
293 comp->steps[comp->nbStep].lenExtra =
294 xsltAllocateExtraCtxt(ctxt->ctxt);
296 comp->steps[comp->nbStep].previousExtra =
297 xsltAllocateExtra(ctxt->style);
298 comp->steps[comp->nbStep].indexExtra =
299 xsltAllocateExtra(ctxt->style);
300 comp->steps[comp->nbStep].lenExtra =
301 xsltAllocateExtra(ctxt->style);
303 if (op == XSLT_OP_PREDICATE) {
304 xmlXPathContextPtr xctxt;
306 if (ctxt->style != NULL)
307 xctxt = xmlXPathNewContext(ctxt->style->doc);
309 xctxt = xmlXPathNewContext(NULL);
310 #ifdef XML_XPATH_NOVAR
311 xctxt->flags = XML_XPATH_NOVAR;
313 if (ctxt->style != NULL)
314 xctxt->dict = ctxt->style->dict;
315 comp->steps[comp->nbStep].comp = xmlXPathCtxtCompile(xctxt, value);
316 xmlXPathFreeContext(xctxt);
317 if (comp->steps[comp->nbStep].comp == NULL) {
318 xsltTransformError(NULL, ctxt->style, ctxt->elem,
319 "Failed to compile predicate\n");
320 ctxt->style->errors++;
328 * xsltSwapTopCompMatch:
329 * @comp: the compiled match expression
331 * reverse the two top steps.
334 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
336 int j = comp->nbStep - 1;
339 register xmlChar *tmp;
341 register xmlXPathCompExprPtr expr;
343 tmp = comp->steps[i].value;
344 comp->steps[i].value = comp->steps[j].value;
345 comp->steps[j].value = tmp;
346 tmp = comp->steps[i].value2;
347 comp->steps[i].value2 = comp->steps[j].value2;
348 comp->steps[j].value2 = tmp;
349 op = comp->steps[i].op;
350 comp->steps[i].op = comp->steps[j].op;
351 comp->steps[j].op = op;
352 expr = comp->steps[i].comp;
353 comp->steps[i].comp = comp->steps[j].comp;
354 comp->steps[j].comp = expr;
359 * xsltReverseCompMatch:
360 * @comp: the compiled match expression
362 * reverse all the stack of expressions
365 xsltReverseCompMatch(xsltCompMatchPtr comp) {
367 int j = comp->nbStep - 1;
370 register xmlChar *tmp;
372 register xmlXPathCompExprPtr expr;
373 tmp = comp->steps[i].value;
374 comp->steps[i].value = comp->steps[j].value;
375 comp->steps[j].value = tmp;
376 tmp = comp->steps[i].value2;
377 comp->steps[i].value2 = comp->steps[j].value2;
378 comp->steps[j].value2 = tmp;
379 op = comp->steps[i].op;
380 comp->steps[i].op = comp->steps[j].op;
381 comp->steps[j].op = op;
382 expr = comp->steps[i].comp;
383 comp->steps[i].comp = comp->steps[j].comp;
384 comp->steps[j].comp = expr;
388 comp->steps[comp->nbStep++].op = XSLT_OP_END;
390 * detect consecutive XSLT_OP_PREDICATE indicating a direct
391 * matching should be done.
393 for (i = 0;i < comp->nbStep - 1;i++) {
394 if ((comp->steps[i].op == XSLT_OP_PREDICATE) &&
395 (comp->steps[i + 1].op == XSLT_OP_PREDICATE)) {
398 if (comp->pattern[0] != '/') {
401 query = xmlStrdup((const xmlChar *)"//");
402 query = xmlStrcat(query, comp->pattern);
404 xmlFree((xmlChar *) comp->pattern);
405 comp->pattern = query;
412 /************************************************************************
414 * The interpreter for the precompiled patterns *
416 ************************************************************************/
419 xsltPatPushState(xsltStepStates *states, int step, xmlNodePtr node) {
420 if ((states->states == NULL) || (states->maxstates <= 0)) {
421 states->maxstates = 4;
422 states->nbstates = 0;
423 states->states = xmlMalloc(4 * sizeof(xsltStepState));
425 else if (states->maxstates <= states->nbstates) {
428 tmp = (xsltStepStatePtr) xmlRealloc(states->states,
429 2 * states->maxstates * sizeof(xsltStepState));
432 states->states = tmp;
433 states->maxstates *= 2;
435 states->states[states->nbstates].step = step;
436 states->states[states->nbstates++].node = node;
438 fprintf(stderr, "Push: %d, %s\n", step, node->name);
444 * xsltTestCompMatchDirect:
445 * @ctxt: a XSLT process context
446 * @comp: the precompiled pattern
449 * Test whether the node matches the pattern, do a direct evalutation
450 * and not a step by step evaluation.
452 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
455 xsltTestCompMatchDirect(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
457 xsltStepOpPtr sel = NULL;
460 xmlXPathObjectPtr list;
467 (doc->name != NULL) &&
468 (doc->name[0] == ' ') &&
469 (xmlStrEqual(BAD_CAST doc->name,
470 BAD_CAST " fake node libxslt")))
474 sel = &comp->steps[0]; /* store extra in first step arbitrarily */
476 prevdoc = (xmlDocPtr)
477 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
478 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
479 list = (xmlXPathObjectPtr)
480 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra);
482 if ((list == NULL) || (prevdoc != doc)) {
483 xmlXPathObjectPtr newlist;
484 xmlNodePtr parent = node->parent;
488 oldnode = ctxt->xpathCtxt->node;
489 olddoc = ctxt->xpathCtxt->doc;
490 ctxt->xpathCtxt->node = node;
491 ctxt->xpathCtxt->doc = doc;
492 newlist = xmlXPathEval(comp->pattern, ctxt->xpathCtxt);
493 ctxt->xpathCtxt->node = oldnode;
494 ctxt->xpathCtxt->doc = olddoc;
497 if (newlist->type != XPATH_NODESET) {
498 xmlXPathFreeObject(newlist);
503 if ((parent == NULL) || (node->doc == NULL) || isRVT)
508 xmlXPathFreeObject(list);
511 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra) =
513 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
515 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
517 XSLT_RUNTIME_EXTRA_FREE(ctxt, sel->lenExtra) =
518 (xmlFreeFunc) xmlXPathFreeObject;
522 if ((list->nodesetval == NULL) ||
523 (list->nodesetval->nodeNr <= 0)) {
525 xmlXPathFreeObject(list);
528 /* TODO: store the index and use it for the scan */
530 for (j = 0;j < list->nodesetval->nodeNr;j++) {
531 if (list->nodesetval->nodeTab[j] == node) {
533 xmlXPathFreeObject(list);
540 xmlXPathFreeObject(list);
546 * @ctxt: a XSLT process context
547 * @comp: the precompiled pattern
549 * @mode: the mode name or NULL
550 * @modeURI: the mode URI or NULL
552 * Test whether the node matches the pattern
554 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
557 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
558 xmlNodePtr node, const xmlChar *mode,
559 const xmlChar *modeURI) {
561 xsltStepOpPtr step, sel = NULL;
562 xsltStepStates states = {0, 0, NULL}; /* // may require backtrack */
564 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
565 xsltTransformError(ctxt, NULL, node,
566 "xsltTestCompMatch: null arg\n");
570 if (comp->mode == NULL)
573 * both mode strings must be interned on the stylesheet dictionary
575 if (comp->mode != mode)
578 if (comp->mode != NULL)
581 if (modeURI != NULL) {
582 if (comp->modeURI == NULL)
585 * both modeURI strings must be interned on the stylesheet dictionary
587 if (comp->modeURI != modeURI)
590 if (comp->modeURI != NULL)
596 for (;i < comp->nbStep;i++) {
597 step = &comp->steps[i];
598 if (step->op != XSLT_OP_PREDICATE)
604 if ((node->type == XML_DOCUMENT_NODE) ||
605 #ifdef LIBXML_DOCB_ENABLED
606 (node->type == XML_DOCB_DOCUMENT_NODE) ||
608 (node->type == XML_HTML_DOCUMENT_NODE))
610 if ((node->type == XML_ELEMENT_NODE) && (node->name[0] == ' '))
614 if (node->type != XML_ELEMENT_NODE)
616 if (step->value == NULL)
618 if (step->value[0] != node->name[0])
620 if (!xmlStrEqual(step->value, node->name))
624 if (node->ns == NULL) {
625 if (step->value2 != NULL)
627 } else if (node->ns->href != NULL) {
628 if (step->value2 == NULL)
630 if (!xmlStrEqual(step->value2, node->ns->href))
634 case XSLT_OP_CHILD: {
637 if ((node->type != XML_ELEMENT_NODE) &&
638 (node->type != XML_DOCUMENT_NODE) &&
639 #ifdef LIBXML_DOCB_ENABLED
640 (node->type != XML_DOCB_DOCUMENT_NODE) &&
642 (node->type != XML_HTML_DOCUMENT_NODE))
645 lst = node->children;
647 if (step->value != NULL) {
648 while (lst != NULL) {
649 if ((lst->type == XML_ELEMENT_NODE) &&
650 (step->value[0] == lst->name[0]) &&
651 (xmlStrEqual(step->value, lst->name)))
661 if (node->type != XML_ATTRIBUTE_NODE)
663 if (step->value != NULL) {
664 if (step->value[0] != node->name[0])
666 if (!xmlStrEqual(step->value, node->name))
670 if (node->ns == NULL) {
671 if (step->value2 != NULL)
673 } else if (step->value2 != NULL) {
674 if (!xmlStrEqual(step->value2, node->ns->href))
679 if ((node->type == XML_DOCUMENT_NODE) ||
680 (node->type == XML_HTML_DOCUMENT_NODE) ||
681 #ifdef LIBXML_DOCB_ENABLED
682 (node->type == XML_DOCB_DOCUMENT_NODE) ||
684 (node->type == XML_NAMESPACE_DECL))
689 if (step->value == NULL)
691 if (step->value[0] != node->name[0])
693 if (!xmlStrEqual(step->value, node->name))
696 if (node->ns == NULL) {
697 if (step->value2 != NULL)
699 } else if (node->ns->href != NULL) {
700 if (step->value2 == NULL)
702 if (!xmlStrEqual(step->value2, node->ns->href))
706 case XSLT_OP_ANCESTOR:
707 /* TODO: implement coalescing of ANCESTOR/NODE ops */
708 if (step->value == NULL) {
709 step = &comp->steps[i+1];
710 if (step->op == XSLT_OP_ROOT)
712 /* added NS, ID and KEY as a result of bug 168208 */
713 if ((step->op != XSLT_OP_ELEM) &&
714 (step->op != XSLT_OP_ALL) &&
715 (step->op != XSLT_OP_NS) &&
716 (step->op != XSLT_OP_ID) &&
717 (step->op != XSLT_OP_KEY))
722 if ((node->type == XML_DOCUMENT_NODE) ||
723 (node->type == XML_HTML_DOCUMENT_NODE) ||
724 #ifdef LIBXML_DOCB_ENABLED
725 (node->type == XML_DOCB_DOCUMENT_NODE) ||
727 (node->type == XML_NAMESPACE_DECL))
730 if ((step->op != XSLT_OP_ELEM) && step->op != XSLT_OP_ALL) {
731 xsltPatPushState(&states, i, node);
735 if (step->value == NULL) {
736 xsltPatPushState(&states, i - 1, node);
739 while (node != NULL) {
742 if ((node->type == XML_ELEMENT_NODE) &&
743 (step->value[0] == node->name[0]) &&
744 (xmlStrEqual(step->value, node->name))) {
746 if (node->ns == NULL) {
747 if (step->value2 == NULL)
749 } else if (node->ns->href != NULL) {
750 if ((step->value2 != NULL) &&
751 (xmlStrEqual(step->value2, node->ns->href)))
759 xsltPatPushState(&states, i - 1, node);
762 /* TODO Handle IDs decently, must be done differently */
765 if (node->type != XML_ELEMENT_NODE)
768 id = xmlGetID(node->doc, step->value);
769 if ((id == NULL) || (id->parent != node))
777 list = xsltGetKey(ctxt, step->value,
778 step->value3, step->value2);
781 for (indx = 0;indx < list->nodeNr;indx++)
782 if (list->nodeTab[indx] == node)
784 if (indx >= list->nodeNr)
789 if (node->type != XML_ELEMENT_NODE)
791 if (node->ns == NULL) {
792 if (step->value != NULL)
794 } else if (node->ns->href != NULL) {
795 if (step->value == NULL)
797 if (!xmlStrEqual(step->value, node->ns->href))
802 if (node->type != XML_ELEMENT_NODE)
805 case XSLT_OP_PREDICATE: {
809 int pos = 0, len = 0;
813 * when there is cascading XSLT_OP_PREDICATE, then use a
814 * direct computation approach. It's not done directly
815 * at the beginning of the routine to filter out as much
816 * as possible this costly computation.
819 if (states.states != NULL) {
820 /* Free the rollback states */
821 xmlFree(states.states);
823 return(xsltTestCompMatchDirect(ctxt, comp, node));
828 (doc->name != NULL) &&
829 (doc->name[0] == ' ') &&
830 (xmlStrEqual(BAD_CAST doc->name,
831 BAD_CAST " fake node libxslt")))
837 * Depending on the last selection, one may need to
838 * recompute contextSize and proximityPosition.
840 oldCS = ctxt->xpathCtxt->contextSize;
841 oldCP = ctxt->xpathCtxt->proximityPosition;
843 (sel->op == XSLT_OP_ELEM) &&
844 (sel->value != NULL) &&
845 (node->type == XML_ELEMENT_NODE) &&
846 (node->parent != NULL)) {
850 previous = (xmlNodePtr)
851 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
852 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
853 if ((previous != NULL) &&
854 (previous->parent == node->parent)) {
856 * just walk back to adjust the index
859 xmlNodePtr sibling = node;
861 while (sibling != NULL) {
862 if (sibling == previous)
864 if ((previous->type == XML_ELEMENT_NODE) &&
865 (previous->name != NULL) &&
866 (sibling->name != NULL) &&
867 (previous->name[0] == sibling->name[0]) &&
868 (xmlStrEqual(previous->name, sibling->name))) {
869 if ((sel->value2 == NULL) ||
870 ((sibling->ns != NULL) &&
871 (xmlStrEqual(sel->value2,
872 sibling->ns->href))))
875 sibling = sibling->prev;
877 if (sibling == NULL) {
878 /* hum going backward in document order ... */
881 while (sibling != NULL) {
882 if (sibling == previous)
884 if ((sel->value2 == NULL) ||
885 ((sibling->ns != NULL) &&
886 (xmlStrEqual(sel->value2,
887 sibling->ns->href))))
889 sibling = sibling->next;
892 if (sibling != NULL) {
895 * If the node is in a Value Tree we need to
896 * save len, but cannot cache the node!
897 * (bugs 153137 and 158840)
899 if (node->doc != NULL) {
900 len = XSLT_RUNTIME_EXTRA(ctxt,
901 sel->lenExtra, ival);
903 XSLT_RUNTIME_EXTRA(ctxt,
904 sel->previousExtra, ptr) = node;
905 XSLT_RUNTIME_EXTRA(ctxt,
906 sel->indexExtra, ival) = pos;
914 * recompute the index
916 xmlNodePtr siblings = node->parent->children;
917 xmlNodePtr parent = node->parent;
919 while (siblings != NULL) {
920 if (siblings->type == XML_ELEMENT_NODE) {
921 if (siblings == node) {
924 } else if ((node->name != NULL) &&
925 (siblings->name != NULL) &&
926 (node->name[0] == siblings->name[0]) &&
927 (xmlStrEqual(node->name, siblings->name))) {
928 if ((sel->value2 == NULL) ||
929 ((siblings->ns != NULL) &&
930 (xmlStrEqual(sel->value2,
931 siblings->ns->href))))
935 siblings = siblings->next;
937 if ((parent == NULL) || (node->doc == NULL))
940 while (parent->parent != NULL)
941 parent = parent->parent;
942 if (((parent->type != XML_DOCUMENT_NODE) &&
943 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
944 (parent != (xmlNodePtr) node->doc))
949 ctxt->xpathCtxt->contextSize = len;
950 ctxt->xpathCtxt->proximityPosition = pos;
952 * If the node is in a Value Tree we cannot
955 if ((!isRVT) && (node->doc != NULL) &&
957 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
959 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
961 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) =
965 } else if ((sel != NULL) && (sel->op == XSLT_OP_ALL) &&
966 (node->type == XML_ELEMENT_NODE)) {
970 previous = (xmlNodePtr)
971 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
972 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
973 if ((previous != NULL) &&
974 (previous->parent == node->parent)) {
976 * just walk back to adjust the index
979 xmlNodePtr sibling = node;
981 while (sibling != NULL) {
982 if (sibling == previous)
984 if (sibling->type == XML_ELEMENT_NODE)
986 sibling = sibling->prev;
988 if (sibling == NULL) {
989 /* hum going backward in document order ... */
992 while (sibling != NULL) {
993 if (sibling == previous)
995 if (sibling->type == XML_ELEMENT_NODE)
997 sibling = sibling->next;
1000 if (sibling != NULL) {
1003 * If the node is in a Value Tree we cannot
1006 if ((node->doc != NULL) && !isRVT) {
1007 len = XSLT_RUNTIME_EXTRA(ctxt,
1008 sel->lenExtra, ival);
1009 XSLT_RUNTIME_EXTRA(ctxt,
1010 sel->previousExtra, ptr) = node;
1011 XSLT_RUNTIME_EXTRA(ctxt,
1012 sel->indexExtra, ival) = pos;
1018 * recompute the index
1020 xmlNodePtr siblings = node->parent->children;
1021 xmlNodePtr parent = node->parent;
1023 while (siblings != NULL) {
1024 if (siblings->type == XML_ELEMENT_NODE) {
1026 if (siblings == node) {
1030 siblings = siblings->next;
1032 if ((parent == NULL) || (node->doc == NULL))
1035 while (parent->parent != NULL)
1036 parent = parent->parent;
1037 if (((parent->type != XML_DOCUMENT_NODE) &&
1038 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
1039 (parent != (xmlNodePtr) node->doc))
1044 ctxt->xpathCtxt->contextSize = len;
1045 ctxt->xpathCtxt->proximityPosition = pos;
1047 * If the node is in a Value Tree we cannot
1050 if ((node->doc != NULL) && (nocache == 0) && !isRVT) {
1051 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
1053 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
1055 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) =
1060 oldNode = ctxt->node;
1063 if (step->value == NULL)
1065 if (step->comp == NULL)
1068 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
1073 ctxt->xpathCtxt->contextSize = oldCS;
1074 ctxt->xpathCtxt->proximityPosition = oldCP;
1076 ctxt->node = oldNode;
1080 ctxt->xpathCtxt->contextSize = oldCS;
1081 ctxt->xpathCtxt->proximityPosition = oldCP;
1083 ctxt->node = oldNode;
1087 if (node->type != XML_PI_NODE)
1089 if (step->value != NULL) {
1090 if (!xmlStrEqual(step->value, node->name))
1094 case XSLT_OP_COMMENT:
1095 if (node->type != XML_COMMENT_NODE)
1099 if ((node->type != XML_TEXT_NODE) &&
1100 (node->type != XML_CDATA_SECTION_NODE))
1104 switch (node->type) {
1105 case XML_ELEMENT_NODE:
1106 case XML_CDATA_SECTION_NODE:
1108 case XML_COMMENT_NODE:
1118 if (states.states != NULL) {
1119 /* Free the rollback states */
1120 xmlFree(states.states);
1124 /* got an error try to rollback */
1125 if (states.states == NULL)
1127 if (states.nbstates <= 0) {
1128 xmlFree(states.states);
1132 i = states.states[states.nbstates].step;
1133 node = states.states[states.nbstates].node;
1135 fprintf(stderr, "Pop: %d, %s\n", i, node->name);
1141 * xsltTestCompMatchList:
1142 * @ctxt: a XSLT process context
1144 * @comp: the precompiled pattern list
1146 * Test whether the node matches one of the patterns in the list
1148 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
1151 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
1152 xsltCompMatchPtr comp) {
1155 if ((ctxt == NULL) || (node == NULL))
1157 while (comp != NULL) {
1158 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
1166 /************************************************************************
1168 * Dedicated parser for templates *
1170 ************************************************************************/
1172 #define CUR (*ctxt->cur)
1173 #define SKIP(val) ctxt->cur += (val)
1174 #define NXT(val) ctxt->cur[(val)]
1175 #define CUR_PTR ctxt->cur
1177 #define SKIP_BLANKS \
1178 while (IS_BLANK_CH(CUR)) NEXT
1180 #define CURRENT (*ctxt->cur)
1181 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1184 #define PUSH(op, val, val2) \
1185 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1188 xsltSwapTopCompMatch(ctxt->comp);
1190 #define XSLT_ERROR(X) \
1191 { xsltError(ctxt, __FILE__, __LINE__, X); \
1192 ctxt->error = (X); return; }
1194 #define XSLT_ERROR0(X) \
1195 { xsltError(ctxt, __FILE__, __LINE__, X); \
1196 ctxt->error = (X); return(0); }
1200 * @ctxt: the XPath Parser context
1202 * Parse an XPath Litteral:
1204 * [29] Literal ::= '"' [^"]* '"'
1207 * Returns the Literal parsed or NULL
1211 xsltScanLiteral(xsltParserContextPtr ctxt) {
1212 const xmlChar *q, *cur;
1213 xmlChar *ret = NULL;
1220 val = xmlStringCurrentChar(NULL, cur, &len);
1221 while ((IS_CHAR(val)) && (val != '"')) {
1223 val = xmlStringCurrentChar(NULL, cur, &len);
1225 if (!IS_CHAR(val)) {
1229 ret = xmlStrndup(q, cur - q);
1233 } else if (CUR == '\'') {
1236 val = xmlStringCurrentChar(NULL, cur, &len);
1237 while ((IS_CHAR(val)) && (val != '\'')) {
1239 val = xmlStringCurrentChar(NULL, cur, &len);
1241 if (!IS_CHAR(val)) {
1245 ret = xmlStrndup(q, cur - q);
1250 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1259 * @ctxt: the XPath Parser context
1261 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1262 * CombiningChar | Extender
1264 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1266 * [6] Names ::= Name (S Name)*
1268 * Returns the Name parsed or NULL
1272 xsltScanName(xsltParserContextPtr ctxt) {
1273 const xmlChar *q, *cur;
1274 xmlChar *ret = NULL;
1280 val = xmlStringCurrentChar(NULL, cur, &len);
1281 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1284 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1285 (val == '.') || (val == '-') ||
1287 (IS_COMBINING(val)) ||
1288 (IS_EXTENDER(val))) {
1290 val = xmlStringCurrentChar(NULL, cur, &len);
1292 ret = xmlStrndup(q, cur - q);
1299 * @ctxt: the XPath Parser context
1301 * Parses a non qualified name
1303 * Returns the Name parsed or NULL
1307 xsltScanNCName(xsltParserContextPtr ctxt) {
1308 const xmlChar *q, *cur;
1309 xmlChar *ret = NULL;
1315 val = xmlStringCurrentChar(NULL, cur, &len);
1316 if (!IS_LETTER(val) && (val != '_'))
1319 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1320 (val == '.') || (val == '-') ||
1322 (IS_COMBINING(val)) ||
1323 (IS_EXTENDER(val))) {
1325 val = xmlStringCurrentChar(NULL, cur, &len);
1327 ret = xmlStrndup(q, cur - q);
1334 * @ctxt: the XPath Parser context
1335 * @prefix: the place to store the prefix
1337 * Parse a qualified name
1339 * Returns the Name parsed or NULL
1343 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1344 xmlChar *ret = NULL;
1347 ret = xsltScanNCName(ctxt);
1351 ret = xsltScanNCName(ctxt);
1357 * xsltCompileIdKeyPattern:
1358 * @ctxt: the compilation context
1359 * @name: a preparsed name
1360 * @aid: whether id/key are allowed there
1362 * Compile the XSLT LocationIdKeyPattern
1363 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1364 * | 'key' '(' Literal ',' Literal ')'
1366 * also handle NodeType and PI from:
1368 * [7] NodeTest ::= NameTest
1369 * | NodeType '(' ')'
1370 * | 'processing-instruction' '(' Literal ')'
1373 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1374 xmlChar *lit = NULL;
1375 xmlChar *lit2 = NULL;
1378 xsltTransformError(NULL, NULL, NULL,
1379 "xsltCompileIdKeyPattern : ( expected\n");
1383 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1386 lit = xsltScanLiteral(ctxt);
1391 xsltTransformError(NULL, NULL, NULL,
1392 "xsltCompileIdKeyPattern : ) expected\n");
1397 PUSH(XSLT_OP_ID, lit, NULL);
1398 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1401 lit = xsltScanLiteral(ctxt);
1406 xsltTransformError(NULL, NULL, NULL,
1407 "xsltCompileIdKeyPattern : , expected\n");
1413 lit2 = xsltScanLiteral(ctxt);
1418 xsltTransformError(NULL, NULL, NULL,
1419 "xsltCompileIdKeyPattern : ) expected\n");
1424 /* TODO: support namespace in keys */
1425 PUSH(XSLT_OP_KEY, lit, lit2);
1426 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1430 lit = xsltScanLiteral(ctxt);
1435 xsltTransformError(NULL, NULL, NULL,
1436 "xsltCompileIdKeyPattern : ) expected\n");
1442 PUSH(XSLT_OP_PI, lit, NULL);
1443 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1447 xsltTransformError(NULL, NULL, NULL,
1448 "xsltCompileIdKeyPattern : ) expected\n");
1453 PUSH(XSLT_OP_TEXT, NULL, NULL);
1454 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1458 xsltTransformError(NULL, NULL, NULL,
1459 "xsltCompileIdKeyPattern : ) expected\n");
1464 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1465 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1469 xsltTransformError(NULL, NULL, NULL,
1470 "xsltCompileIdKeyPattern : ) expected\n");
1475 PUSH(XSLT_OP_NODE, NULL, NULL);
1477 xsltTransformError(NULL, NULL, NULL,
1478 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1482 xsltTransformError(NULL, NULL, NULL,
1483 "xsltCompileIdKeyPattern : node type\n");
1493 * xsltCompileStepPattern:
1494 * @ctxt: the compilation context
1495 * @token: a posible precompiled name
1497 * Compile the XSLT StepPattern and generates a precompiled
1498 * form suitable for fast matching.
1500 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1501 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1502 * | ('child' | 'attribute') '::'
1504 * [7] NodeTest ::= NameTest
1505 * | NodeType '(' ')'
1506 * | 'processing-instruction' '(' Literal ')'
1507 * [8] Predicate ::= '[' PredicateExpr ']'
1508 * [9] PredicateExpr ::= Expr
1509 * [13] AbbreviatedAxisSpecifier ::= '@'?
1510 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1514 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1515 xmlChar *name = NULL;
1516 const xmlChar *URI = NULL;
1517 xmlChar *URL = NULL;
1521 if ((token == NULL) && (CUR == '@')) {
1522 xmlChar *prefix = NULL;
1527 PUSH(XSLT_OP_ATTR, NULL, NULL);
1528 goto parse_predicate;
1530 token = xsltScanQName(ctxt, &prefix);
1531 if (prefix != NULL) {
1534 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1536 xsltTransformError(NULL, NULL, NULL,
1537 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1540 URL = xmlStrdup(ns->href);
1544 if (token == NULL) {
1547 PUSH(XSLT_OP_ATTR, NULL, URL);
1550 xsltTransformError(NULL, NULL, NULL,
1551 "xsltCompileStepPattern : Name expected\n");
1555 PUSH(XSLT_OP_ATTR, token, URL);
1556 goto parse_predicate;
1559 token = xsltScanName(ctxt);
1560 if (token == NULL) {
1563 PUSH(XSLT_OP_ALL, token, NULL);
1564 goto parse_predicate;
1566 xsltTransformError(NULL, NULL, NULL,
1567 "xsltCompileStepPattern : Name expected\n");
1576 xsltCompileIdKeyPattern(ctxt, token, 0);
1579 } else if (CUR == ':') {
1582 xmlChar *prefix = token;
1586 * This is a namespace match
1588 token = xsltScanName(ctxt);
1589 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1591 xsltTransformError(NULL, NULL, NULL,
1592 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1597 URL = xmlStrdup(ns->href);
1600 if (token == NULL) {
1603 PUSH(XSLT_OP_NS, URL, NULL);
1605 xsltTransformError(NULL, NULL, NULL,
1606 "xsltCompileStepPattern : Name expected\n");
1611 PUSH(XSLT_OP_ELEM, token, URL);
1615 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1617 token = xsltScanName(ctxt);
1618 if (token == NULL) {
1621 PUSH(XSLT_OP_ALL, token, NULL);
1622 goto parse_predicate;
1624 xsltTransformError(NULL, NULL, NULL,
1625 "xsltCompileStepPattern : QName expected\n");
1630 URI = xsltGetQNameURI(ctxt->elem, &token);
1631 if (token == NULL) {
1635 name = xmlStrdup(token);
1637 URL = xmlStrdup(URI);
1639 PUSH(XSLT_OP_CHILD, name, URL);
1640 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1642 token = xsltScanName(ctxt);
1643 if (token == NULL) {
1644 xsltTransformError(NULL, NULL, NULL,
1645 "xsltCompileStepPattern : QName expected\n");
1649 URI = xsltGetQNameURI(ctxt->elem, &token);
1650 if (token == NULL) {
1654 name = xmlStrdup(token);
1656 URL = xmlStrdup(URI);
1658 PUSH(XSLT_OP_ATTR, name, URL);
1660 xsltTransformError(NULL, NULL, NULL,
1661 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1667 } else if (CUR == '*') {
1669 PUSH(XSLT_OP_ALL, token, NULL);
1671 URI = xsltGetQNameURI(ctxt->elem, &token);
1672 if (token == NULL) {
1677 URL = xmlStrdup(URI);
1678 PUSH(XSLT_OP_ELEM, token, URL);
1683 while (CUR == '[') {
1685 xmlChar *ret = NULL;
1691 /* Skip over nested predicates */
1694 else if (CUR == ']') {
1698 } else if (CUR == '"') {
1700 while ((CUR != 0) && (CUR != '"'))
1702 } else if (CUR == '\'') {
1704 while ((CUR != 0) && (CUR != '\''))
1710 xsltTransformError(NULL, NULL, NULL,
1711 "xsltCompileStepPattern : ']' expected\n");
1715 ret = xmlStrndup(q, CUR_PTR - q);
1716 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1717 /* push the predicate lower than local test */
1731 * xsltCompileRelativePathPattern:
1732 * @comp: the compilation context
1733 * @token: a posible precompiled name
1735 * Compile the XSLT RelativePathPattern and generates a precompiled
1736 * form suitable for fast matching.
1738 * [4] RelativePathPattern ::= StepPattern
1739 * | RelativePathPattern '/' StepPattern
1740 * | RelativePathPattern '//' StepPattern
1743 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1744 xsltCompileStepPattern(ctxt, token);
1748 while ((CUR != 0) && (CUR != '|')) {
1749 if ((CUR == '/') && (NXT(1) == '/')) {
1750 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1754 xsltCompileStepPattern(ctxt, NULL);
1755 } else if (CUR == '/') {
1756 PUSH(XSLT_OP_PARENT, NULL, NULL);
1759 if ((CUR != 0) && (CUR != '|')) {
1760 xsltCompileRelativePathPattern(ctxt, NULL);
1774 * xsltCompileLocationPathPattern:
1775 * @ctxt: the compilation context
1777 * Compile the XSLT LocationPathPattern and generates a precompiled
1778 * form suitable for fast matching.
1780 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1781 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1782 * | '//'? RelativePathPattern
1785 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1787 if ((CUR == '/') && (NXT(1) == '/')) {
1789 * since we reverse the query
1790 * a leading // can be safely ignored
1794 ctxt->comp->priority = 0.5; /* '//' means not 0 priority */
1795 xsltCompileRelativePathPattern(ctxt, NULL);
1796 } else if (CUR == '/') {
1798 * We need to find root as the parent
1802 PUSH(XSLT_OP_ROOT, NULL, NULL);
1803 if ((CUR != 0) && (CUR != '|')) {
1804 PUSH(XSLT_OP_PARENT, NULL, NULL);
1805 xsltCompileRelativePathPattern(ctxt, NULL);
1807 } else if (CUR == '*') {
1808 xsltCompileRelativePathPattern(ctxt, NULL);
1809 } else if (CUR == '@') {
1810 xsltCompileRelativePathPattern(ctxt, NULL);
1813 name = xsltScanName(ctxt);
1815 xsltTransformError(NULL, NULL, NULL,
1816 "xsltCompileLocationPathPattern : Name expected\n");
1821 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1822 xsltCompileIdKeyPattern(ctxt, name, 1);
1823 if ((CUR == '/') && (NXT(1) == '/')) {
1824 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1828 xsltCompileRelativePathPattern(ctxt, NULL);
1829 } else if (CUR == '/') {
1830 PUSH(XSLT_OP_PARENT, NULL, NULL);
1833 xsltCompileRelativePathPattern(ctxt, NULL);
1837 xsltCompileRelativePathPattern(ctxt, name);
1844 * xsltCompilePattern:
1845 * @pattern: an XSLT pattern
1846 * @doc: the containing document
1847 * @node: the containing element
1848 * @style: the stylesheet
1849 * @runtime: the transformation context, if done at run-time
1851 * Compile the XSLT pattern and generates a list of precompiled form suitable
1852 * for fast matching.
1854 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1856 * Returns the generated pattern list or NULL in case of failure
1860 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1861 xmlNodePtr node, xsltStylesheetPtr style,
1862 xsltTransformContextPtr runtime) {
1863 xsltParserContextPtr ctxt = NULL;
1864 xsltCompMatchPtr element, first = NULL, previous = NULL;
1865 int current, start, end, level, j;
1867 if (pattern == NULL) {
1868 xsltTransformError(NULL, NULL, node,
1869 "xsltCompilePattern : NULL pattern\n");
1873 ctxt = xsltNewParserContext(style, runtime);
1879 while (pattern[current] != 0) {
1881 while (IS_BLANK_CH(pattern[current]))
1885 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1886 if (pattern[end] == '[')
1888 else if (pattern[end] == ']')
1890 else if (pattern[end] == '\'') {
1892 while ((pattern[end] != 0) && (pattern[end] != '\''))
1894 } else if (pattern[end] == '"') {
1896 while ((pattern[end] != 0) && (pattern[end] != '"'))
1901 if (current == end) {
1902 xsltTransformError(NULL, NULL, node,
1903 "xsltCompilePattern : NULL pattern\n");
1906 element = xsltNewCompMatch();
1907 if (element == NULL) {
1912 else if (previous != NULL)
1913 previous->next = element;
1916 ctxt->comp = element;
1917 ctxt->base = xmlStrndup(&pattern[start], end - start);
1918 if (ctxt->base == NULL)
1920 ctxt->cur = &(ctxt->base)[current - start];
1921 element->pattern = ctxt->base;
1922 element->nsList = xmlGetNsList(doc, node);
1924 if (element->nsList != NULL) {
1925 while (element->nsList[j] != NULL)
1931 #ifdef WITH_XSLT_DEBUG_PATTERN
1932 xsltGenericDebug(xsltGenericDebugContext,
1933 "xsltCompilePattern : parsing '%s'\n",
1937 Preset default priority to be zero.
1938 This may be changed by xsltCompileLocationPathPattern.
1940 element->priority = 0;
1941 xsltCompileLocationPathPattern(ctxt);
1943 xsltTransformError(NULL, style, node,
1944 "xsltCompilePattern : failed to compile '%s'\n",
1946 if (style != NULL) style->errors++;
1951 * Reverse for faster interpretation.
1953 xsltReverseCompMatch(element);
1956 * Set-up the priority
1958 if (element->priority == 0) { /* if not yet determined */
1959 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1960 (element->steps[0].op == XSLT_OP_ATTR) ||
1961 (element->steps[0].op == XSLT_OP_PI)) &&
1962 (element->steps[0].value != NULL) &&
1963 (element->steps[1].op == XSLT_OP_END)) {
1964 ; /* previously preset */
1965 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1966 (element->steps[0].value2 != NULL) &&
1967 (element->steps[1].op == XSLT_OP_END)) {
1968 element->priority = -0.25;
1969 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1970 (element->steps[0].value != NULL) &&
1971 (element->steps[1].op == XSLT_OP_END)) {
1972 element->priority = -0.25;
1973 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1974 (element->steps[0].value == NULL) &&
1975 (element->steps[0].value2 == NULL) &&
1976 (element->steps[1].op == XSLT_OP_END)) {
1977 element->priority = -0.5;
1978 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1979 (element->steps[0].op == XSLT_OP_TEXT) ||
1980 (element->steps[0].op == XSLT_OP_ALL) ||
1981 (element->steps[0].op == XSLT_OP_NODE) ||
1982 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1983 (element->steps[1].op == XSLT_OP_END)) {
1984 element->priority = -0.5;
1986 element->priority = 0.5;
1989 #ifdef WITH_XSLT_DEBUG_PATTERN
1990 xsltGenericDebug(xsltGenericDebugContext,
1991 "xsltCompilePattern : parsed %s, default priority %f\n",
1992 element->pattern, element->priority);
1994 if (pattern[end] == '|')
1999 xsltTransformError(NULL, style, node,
2000 "xsltCompilePattern : NULL pattern\n");
2001 if (style != NULL) style->errors++;
2005 xsltFreeParserContext(ctxt);
2010 xsltFreeParserContext(ctxt);
2012 xsltFreeCompMatchList(first);
2016 /************************************************************************
2018 * Module interfaces *
2020 ************************************************************************/
2024 * @style: an XSLT stylesheet
2025 * @cur: an XSLT template
2026 * @mode: the mode name or NULL
2027 * @modeURI: the mode URI or NULL
2029 * Register the XSLT pattern associated to @cur
2031 * Returns -1 in case of error, 0 otherwise
2034 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
2035 const xmlChar *mode, const xmlChar *modeURI) {
2036 xsltCompMatchPtr pat, list, *top = NULL, next;
2037 const xmlChar *name = NULL;
2038 float priority; /* the priority */
2040 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
2043 priority = cur->priority;
2044 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
2050 pat->template = cur;
2052 pat->mode = xmlDictLookup(style->dict, mode, -1);
2053 if (modeURI != NULL)
2054 pat->modeURI = xmlDictLookup(style->dict, modeURI, -1);
2055 if (priority != XSLT_PAT_NO_PRIORITY)
2056 pat->priority = priority;
2059 * insert it in the hash table list corresponding to its lookup name
2061 switch (pat->steps[0].op) {
2063 if (pat->steps[0].value != NULL)
2064 name = pat->steps[0].value;
2066 top = (xsltCompMatchPtr *) &(style->attrMatch);
2069 case XSLT_OP_PARENT:
2070 case XSLT_OP_ANCESTOR:
2071 top = (xsltCompMatchPtr *) &(style->elemMatch);
2074 top = (xsltCompMatchPtr *) &(style->rootMatch);
2077 top = (xsltCompMatchPtr *) &(style->keyMatch);
2080 /* TODO optimize ID !!! */
2083 top = (xsltCompMatchPtr *) &(style->elemMatch);
2086 case XSLT_OP_PREDICATE:
2087 xsltTransformError(NULL, style, NULL,
2088 "xsltAddTemplate: invalid compiled pattern\n");
2089 xsltFreeCompMatch(pat);
2092 * TODO: some flags at the top level about type based patterns
2093 * would be faster than inclusion in the hash table.
2096 if (pat->steps[0].value != NULL)
2097 name = pat->steps[0].value;
2099 top = (xsltCompMatchPtr *) &(style->piMatch);
2101 case XSLT_OP_COMMENT:
2102 top = (xsltCompMatchPtr *) &(style->commentMatch);
2105 top = (xsltCompMatchPtr *) &(style->textMatch);
2109 if (pat->steps[0].value != NULL)
2110 name = pat->steps[0].value;
2112 top = (xsltCompMatchPtr *) &(style->elemMatch);
2116 if (style->templatesHash == NULL) {
2117 style->templatesHash = xmlHashCreate(1024);
2118 if (style->templatesHash == NULL) {
2119 xsltFreeCompMatch(pat);
2122 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
2124 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
2125 name, mode, modeURI);
2127 xmlHashAddEntry3(style->templatesHash, name,
2128 mode, modeURI, pat);
2131 * Note '<=' since one must choose among the matching
2132 * template rules that are left, the one that occurs
2133 * last in the stylesheet
2135 if (list->priority <= pat->priority) {
2137 xmlHashUpdateEntry3(style->templatesHash, name,
2138 mode, modeURI, pat, NULL);
2140 while (list->next != NULL) {
2141 if (list->next->priority <= pat->priority)
2145 pat->next = list->next;
2150 } else if (top != NULL) {
2155 } else if (list->priority <= pat->priority) {
2159 while (list->next != NULL) {
2160 if (list->next->priority <= pat->priority)
2164 pat->next = list->next;
2168 xsltTransformError(NULL, style, NULL,
2169 "xsltAddTemplate: invalid compiled pattern\n");
2170 xsltFreeCompMatch(pat);
2173 #ifdef WITH_XSLT_DEBUG_PATTERN
2175 xsltGenericDebug(xsltGenericDebugContext,
2176 "added pattern : '%s' mode '%s' priority %f\n",
2177 pat->pattern, pat->mode, pat->priority);
2179 xsltGenericDebug(xsltGenericDebugContext,
2180 "added pattern : '%s' priority %f\n",
2181 pat->pattern, pat->priority);
2191 * @ctxt: a XSLT process context
2192 * @node: the node being processed
2193 * @style: the current style
2195 * Finds the template applying to this node, if @style is non-NULL
2196 * it means one needs to look for the next imported template in scope.
2198 * Returns the xsltTemplatePtr or NULL if not found
2201 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2202 xsltStylesheetPtr style) {
2203 xsltStylesheetPtr curstyle;
2204 xsltTemplatePtr ret = NULL;
2205 const xmlChar *name = NULL;
2206 xsltCompMatchPtr list = NULL;
2210 if ((ctxt == NULL) || (node == NULL))
2213 if (style == NULL) {
2214 curstyle = ctxt->style;
2216 curstyle = xsltNextImport(style);
2219 while ((curstyle != NULL) && (curstyle != style)) {
2220 priority = XSLT_PAT_NO_PRIORITY;
2221 /* TODO : handle IDs/keys here ! */
2222 if (curstyle->templatesHash != NULL) {
2224 * Use the top name as selector
2226 switch (node->type) {
2227 case XML_ELEMENT_NODE:
2228 if (node->name[0] == ' ')
2230 case XML_ATTRIBUTE_NODE:
2234 case XML_DOCUMENT_NODE:
2235 case XML_HTML_DOCUMENT_NODE:
2237 case XML_CDATA_SECTION_NODE:
2238 case XML_COMMENT_NODE:
2239 case XML_ENTITY_REF_NODE:
2240 case XML_ENTITY_NODE:
2241 case XML_DOCUMENT_TYPE_NODE:
2242 case XML_DOCUMENT_FRAG_NODE:
2243 case XML_NOTATION_NODE:
2245 case XML_ELEMENT_DECL:
2246 case XML_ATTRIBUTE_DECL:
2247 case XML_ENTITY_DECL:
2248 case XML_NAMESPACE_DECL:
2249 case XML_XINCLUDE_START:
2250 case XML_XINCLUDE_END:
2259 * find the list of applicable expressions based on the name
2261 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2262 name, ctxt->mode, ctxt->modeURI);
2265 while (list != NULL) {
2266 if (xsltTestCompMatch(ctxt, list, node,
2267 ctxt->mode, ctxt->modeURI)) {
2268 ret = list->template;
2269 priority = list->priority;
2277 * find alternate generic matches
2279 switch (node->type) {
2280 case XML_ELEMENT_NODE:
2281 if (node->name[0] == ' ')
2282 list = curstyle->rootMatch;
2284 list = curstyle->elemMatch;
2285 if (node->psvi != NULL) keyed = 1;
2287 case XML_ATTRIBUTE_NODE: {
2290 list = curstyle->attrMatch;
2291 attr = (xmlAttrPtr) node;
2292 if (attr->psvi != NULL) keyed = 1;
2296 list = curstyle->piMatch;
2297 if (node->psvi != NULL) keyed = 1;
2299 case XML_DOCUMENT_NODE:
2300 case XML_HTML_DOCUMENT_NODE: {
2303 list = curstyle->rootMatch;
2304 doc = (xmlDocPtr) node;
2305 if (doc->psvi != NULL) keyed = 1;
2309 case XML_CDATA_SECTION_NODE:
2310 list = curstyle->textMatch;
2311 if (node->psvi != NULL) keyed = 1;
2313 case XML_COMMENT_NODE:
2314 list = curstyle->commentMatch;
2315 if (node->psvi != NULL) keyed = 1;
2317 case XML_ENTITY_REF_NODE:
2318 case XML_ENTITY_NODE:
2319 case XML_DOCUMENT_TYPE_NODE:
2320 case XML_DOCUMENT_FRAG_NODE:
2321 case XML_NOTATION_NODE:
2323 case XML_ELEMENT_DECL:
2324 case XML_ATTRIBUTE_DECL:
2325 case XML_ENTITY_DECL:
2326 case XML_NAMESPACE_DECL:
2327 case XML_XINCLUDE_START:
2328 case XML_XINCLUDE_END:
2333 while ((list != NULL) &&
2334 ((ret == NULL) || (list->priority > priority))) {
2335 if (xsltTestCompMatch(ctxt, list, node,
2336 ctxt->mode, ctxt->modeURI)) {
2337 ret = list->template;
2338 priority = list->priority;
2344 * Some of the tests for elements can also apply to documents
2346 if ((node->type == XML_DOCUMENT_NODE) ||
2347 (node->type == XML_HTML_DOCUMENT_NODE) ||
2348 (node->type == XML_TEXT_NODE)) {
2349 list = curstyle->elemMatch;
2350 while ((list != NULL) &&
2351 ((ret == NULL) || (list->priority > priority))) {
2352 if (xsltTestCompMatch(ctxt, list, node,
2353 ctxt->mode, ctxt->modeURI)) {
2354 ret = list->template;
2355 priority = list->priority;
2360 } else if ((node->type == XML_PI_NODE) ||
2361 (node->type == XML_COMMENT_NODE)) {
2362 list = curstyle->elemMatch;
2363 while ((list != NULL) &&
2364 ((ret == NULL) || (list->priority > priority))) {
2365 if (xsltTestCompMatch(ctxt, list, node,
2366 ctxt->mode, ctxt->modeURI)) {
2367 ret = list->template;
2368 priority = list->priority;
2376 list = curstyle->keyMatch;
2377 while ((list != NULL) &&
2378 ((ret == NULL) || (list->priority > priority))) {
2379 if (xsltTestCompMatch(ctxt, list, node,
2380 ctxt->mode, ctxt->modeURI)) {
2381 ret = list->template;
2382 priority = list->priority;
2392 * Cycle on next curstylesheet import.
2394 curstyle = xsltNextImport(curstyle);
2400 * xsltCleanupTemplates:
2401 * @style: an XSLT stylesheet
2403 * Cleanup the state of the templates used by the stylesheet and
2404 * the ones it imports.
2407 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2411 * xsltFreeTemplateHashes:
2412 * @style: an XSLT stylesheet
2414 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2417 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2418 if (style->templatesHash != NULL)
2419 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2420 (xmlHashDeallocator) xsltFreeCompMatchList);
2421 if (style->rootMatch != NULL)
2422 xsltFreeCompMatchList(style->rootMatch);
2423 if (style->keyMatch != NULL)
2424 xsltFreeCompMatchList(style->keyMatch);
2425 if (style->elemMatch != NULL)
2426 xsltFreeCompMatchList(style->elemMatch);
2427 if (style->attrMatch != NULL)
2428 xsltFreeCompMatchList(style->attrMatch);
2429 if (style->parentMatch != NULL)
2430 xsltFreeCompMatchList(style->parentMatch);
2431 if (style->textMatch != NULL)
2432 xsltFreeCompMatchList(style->textMatch);
2433 if (style->piMatch != NULL)
2434 xsltFreeCompMatchList(style->piMatch);
2435 if (style->commentMatch != NULL)
2436 xsltFreeCompMatchList(style->commentMatch);