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 comp->steps[comp->nbStep].comp = xsltXPathCompile(ctxt->style, value);
311 * xsltSwapTopCompMatch:
312 * @comp: the compiled match expression
314 * reverse the two top steps.
317 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
319 int j = comp->nbStep - 1;
322 register xmlChar *tmp;
324 register xmlXPathCompExprPtr expr;
326 tmp = comp->steps[i].value;
327 comp->steps[i].value = comp->steps[j].value;
328 comp->steps[j].value = tmp;
329 tmp = comp->steps[i].value2;
330 comp->steps[i].value2 = comp->steps[j].value2;
331 comp->steps[j].value2 = tmp;
332 op = comp->steps[i].op;
333 comp->steps[i].op = comp->steps[j].op;
334 comp->steps[j].op = op;
335 expr = comp->steps[i].comp;
336 comp->steps[i].comp = comp->steps[j].comp;
337 comp->steps[j].comp = expr;
342 * xsltReverseCompMatch:
343 * @comp: the compiled match expression
345 * reverse all the stack of expressions
348 xsltReverseCompMatch(xsltCompMatchPtr comp) {
350 int j = comp->nbStep - 1;
353 register xmlChar *tmp;
355 register xmlXPathCompExprPtr expr;
356 tmp = comp->steps[i].value;
357 comp->steps[i].value = comp->steps[j].value;
358 comp->steps[j].value = tmp;
359 tmp = comp->steps[i].value2;
360 comp->steps[i].value2 = comp->steps[j].value2;
361 comp->steps[j].value2 = tmp;
362 op = comp->steps[i].op;
363 comp->steps[i].op = comp->steps[j].op;
364 comp->steps[j].op = op;
365 expr = comp->steps[i].comp;
366 comp->steps[i].comp = comp->steps[j].comp;
367 comp->steps[j].comp = expr;
371 comp->steps[comp->nbStep++].op = XSLT_OP_END;
373 * detect consecutive XSLT_OP_PREDICATE indicating a direct
374 * matching should be done.
376 for (i = 0;i < comp->nbStep - 1;i++) {
377 if ((comp->steps[i].op == XSLT_OP_PREDICATE) &&
378 (comp->steps[i + 1].op == XSLT_OP_PREDICATE)) {
381 if (comp->pattern[0] != '/') {
384 query = xmlStrdup((const xmlChar *)"//");
385 query = xmlStrcat(query, comp->pattern);
387 xmlFree((xmlChar *) comp->pattern);
388 comp->pattern = query;
395 /************************************************************************
397 * The interpreter for the precompiled patterns *
399 ************************************************************************/
402 xsltPatPushState(xsltStepStates *states, int step, xmlNodePtr node) {
403 if ((states->states == NULL) || (states->maxstates <= 0)) {
404 states->maxstates = 4;
405 states->nbstates = 0;
406 states->states = xmlMalloc(4 * sizeof(xsltStepState));
408 else if (states->maxstates <= states->nbstates) {
411 tmp = (xsltStepStatePtr) xmlRealloc(states->states,
412 2 * states->maxstates * sizeof(xsltStepState));
415 states->states = tmp;
416 states->maxstates *= 2;
418 states->states[states->nbstates].step = step;
419 states->states[states->nbstates++].node = node;
421 fprintf(stderr, "Push: %d, %s\n", step, node->name);
427 * xsltTestCompMatchDirect:
428 * @ctxt: a XSLT process context
429 * @comp: the precompiled pattern
432 * Test whether the node matches the pattern, do a direct evalutation
433 * and not a step by step evaluation.
435 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
438 xsltTestCompMatchDirect(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
440 xsltStepOpPtr sel = NULL;
443 xmlXPathObjectPtr list;
450 (doc->name != NULL) &&
451 (doc->name[0] == ' ') &&
452 (xmlStrEqual(BAD_CAST doc->name,
453 BAD_CAST " fake node libxslt")))
457 sel = &comp->steps[0]; /* store extra in first step arbitrarily */
459 prevdoc = (xmlDocPtr)
460 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
461 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
462 list = (xmlXPathObjectPtr)
463 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra);
465 if ((list == NULL) || (prevdoc != doc)) {
466 xmlXPathObjectPtr newlist;
467 xmlNodePtr parent = node->parent;
471 oldnode = ctxt->xpathCtxt->node;
472 olddoc = ctxt->xpathCtxt->doc;
473 ctxt->xpathCtxt->node = node;
474 ctxt->xpathCtxt->doc = doc;
475 newlist = xmlXPathEval(comp->pattern, ctxt->xpathCtxt);
476 ctxt->xpathCtxt->node = oldnode;
477 ctxt->xpathCtxt->doc = olddoc;
480 if (newlist->type != XPATH_NODESET) {
481 xmlXPathFreeObject(newlist);
486 if ((parent == NULL) || (node->doc == NULL) || isRVT)
491 xmlXPathFreeObject(list);
494 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra) =
496 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
498 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
500 XSLT_RUNTIME_EXTRA_FREE(ctxt, sel->lenExtra) =
501 (xmlFreeFunc) xmlXPathFreeObject;
505 if ((list->nodesetval == NULL) ||
506 (list->nodesetval->nodeNr <= 0)) {
508 xmlXPathFreeObject(list);
511 /* TODO: store the index and use it for the scan */
513 for (j = 0;j < list->nodesetval->nodeNr;j++) {
514 if (list->nodesetval->nodeTab[j] == node) {
516 xmlXPathFreeObject(list);
523 xmlXPathFreeObject(list);
529 * @ctxt: a XSLT process context
530 * @comp: the precompiled pattern
532 * @mode: the mode name or NULL
533 * @modeURI: the mode URI or NULL
535 * Test whether the node matches the pattern
537 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
540 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
541 xmlNodePtr node, const xmlChar *mode,
542 const xmlChar *modeURI) {
544 xsltStepOpPtr step, sel = NULL;
545 xsltStepStates states = {0, 0, NULL}; /* // may require backtrack */
547 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
548 xsltTransformError(ctxt, NULL, node,
549 "xsltTestCompMatch: null arg\n");
553 if (comp->mode == NULL)
556 * both mode strings must be interned on the stylesheet dictionary
558 if (comp->mode != mode)
561 if (comp->mode != NULL)
564 if (modeURI != NULL) {
565 if (comp->modeURI == NULL)
568 * both modeURI strings must be interned on the stylesheet dictionary
570 if (comp->modeURI != modeURI)
573 if (comp->modeURI != NULL)
579 for (;i < comp->nbStep;i++) {
580 step = &comp->steps[i];
581 if (step->op != XSLT_OP_PREDICATE)
587 if ((node->type == XML_DOCUMENT_NODE) ||
588 #ifdef LIBXML_DOCB_ENABLED
589 (node->type == XML_DOCB_DOCUMENT_NODE) ||
591 (node->type == XML_HTML_DOCUMENT_NODE))
593 if ((node->type == XML_ELEMENT_NODE) && (node->name[0] == ' '))
597 if (node->type != XML_ELEMENT_NODE)
599 if (step->value == NULL)
601 if (step->value[0] != node->name[0])
603 if (!xmlStrEqual(step->value, node->name))
607 if (node->ns == NULL) {
608 if (step->value2 != NULL)
610 } else if (node->ns->href != NULL) {
611 if (step->value2 == NULL)
613 if (!xmlStrEqual(step->value2, node->ns->href))
617 case XSLT_OP_CHILD: {
620 if ((node->type != XML_ELEMENT_NODE) &&
621 (node->type != XML_DOCUMENT_NODE) &&
622 #ifdef LIBXML_DOCB_ENABLED
623 (node->type != XML_DOCB_DOCUMENT_NODE) &&
625 (node->type != XML_HTML_DOCUMENT_NODE))
628 lst = node->children;
630 if (step->value != NULL) {
631 while (lst != NULL) {
632 if ((lst->type == XML_ELEMENT_NODE) &&
633 (step->value[0] == lst->name[0]) &&
634 (xmlStrEqual(step->value, lst->name)))
644 if (node->type != XML_ATTRIBUTE_NODE)
646 if (step->value != NULL) {
647 if (step->value[0] != node->name[0])
649 if (!xmlStrEqual(step->value, node->name))
653 if (node->ns == NULL) {
654 if (step->value2 != NULL)
656 } else if (step->value2 != NULL) {
657 if (!xmlStrEqual(step->value2, node->ns->href))
662 if ((node->type == XML_DOCUMENT_NODE) ||
663 (node->type == XML_HTML_DOCUMENT_NODE) ||
664 #ifdef LIBXML_DOCB_ENABLED
665 (node->type == XML_DOCB_DOCUMENT_NODE) ||
667 (node->type == XML_NAMESPACE_DECL))
672 if (step->value == NULL)
674 if (step->value[0] != node->name[0])
676 if (!xmlStrEqual(step->value, node->name))
679 if (node->ns == NULL) {
680 if (step->value2 != NULL)
682 } else if (node->ns->href != NULL) {
683 if (step->value2 == NULL)
685 if (!xmlStrEqual(step->value2, node->ns->href))
689 case XSLT_OP_ANCESTOR:
690 /* TODO: implement coalescing of ANCESTOR/NODE ops */
691 if (step->value == NULL) {
692 step = &comp->steps[i+1];
693 if (step->op == XSLT_OP_ROOT)
695 /* added NS, ID and KEY as a result of bug 168208 */
696 if ((step->op != XSLT_OP_ELEM) &&
697 (step->op != XSLT_OP_ALL) &&
698 (step->op != XSLT_OP_NS) &&
699 (step->op != XSLT_OP_ID) &&
700 (step->op != XSLT_OP_KEY))
705 if ((node->type == XML_DOCUMENT_NODE) ||
706 (node->type == XML_HTML_DOCUMENT_NODE) ||
707 #ifdef LIBXML_DOCB_ENABLED
708 (node->type == XML_DOCB_DOCUMENT_NODE) ||
710 (node->type == XML_NAMESPACE_DECL))
713 if ((step->op != XSLT_OP_ELEM) && step->op != XSLT_OP_ALL) {
714 xsltPatPushState(&states, i, node);
718 if (step->value == NULL) {
719 xsltPatPushState(&states, i - 1, node);
722 while (node != NULL) {
725 if ((node->type == XML_ELEMENT_NODE) &&
726 (step->value[0] == node->name[0]) &&
727 (xmlStrEqual(step->value, node->name))) {
729 if (node->ns == NULL) {
730 if (step->value2 == NULL)
732 } else if (node->ns->href != NULL) {
733 if ((step->value2 != NULL) &&
734 (xmlStrEqual(step->value2, node->ns->href)))
742 xsltPatPushState(&states, i - 1, node);
745 /* TODO Handle IDs decently, must be done differently */
748 if (node->type != XML_ELEMENT_NODE)
751 id = xmlGetID(node->doc, step->value);
752 if ((id == NULL) || (id->parent != node))
760 list = xsltGetKey(ctxt, step->value,
761 step->value3, step->value2);
764 for (indx = 0;indx < list->nodeNr;indx++)
765 if (list->nodeTab[indx] == node)
767 if (indx >= list->nodeNr)
772 if (node->type != XML_ELEMENT_NODE)
774 if (node->ns == NULL) {
775 if (step->value != NULL)
777 } else if (node->ns->href != NULL) {
778 if (step->value == NULL)
780 if (!xmlStrEqual(step->value, node->ns->href))
785 if (node->type != XML_ELEMENT_NODE)
788 case XSLT_OP_PREDICATE: {
792 int pos = 0, len = 0;
796 * when there is cascading XSLT_OP_PREDICATE, then use a
797 * direct computation approach. It's not done directly
798 * at the beginning of the routine to filter out as much
799 * as possible this costly computation.
802 if (states.states != NULL) {
803 /* Free the rollback states */
804 xmlFree(states.states);
806 return(xsltTestCompMatchDirect(ctxt, comp, node));
811 (doc->name != NULL) &&
812 (doc->name[0] == ' ') &&
813 (xmlStrEqual(BAD_CAST doc->name,
814 BAD_CAST " fake node libxslt")))
820 * Depending on the last selection, one may need to
821 * recompute contextSize and proximityPosition.
823 oldCS = ctxt->xpathCtxt->contextSize;
824 oldCP = ctxt->xpathCtxt->proximityPosition;
826 (sel->op == XSLT_OP_ELEM) &&
827 (sel->value != NULL) &&
828 (node->type == XML_ELEMENT_NODE) &&
829 (node->parent != NULL)) {
833 previous = (xmlNodePtr)
834 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
835 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
836 if ((previous != NULL) &&
837 (previous->parent == node->parent)) {
839 * just walk back to adjust the index
842 xmlNodePtr sibling = node;
844 while (sibling != NULL) {
845 if (sibling == previous)
847 if ((previous->type == XML_ELEMENT_NODE) &&
848 (previous->name != NULL) &&
849 (sibling->name != NULL) &&
850 (previous->name[0] == sibling->name[0]) &&
851 (xmlStrEqual(previous->name, sibling->name))) {
852 if ((sel->value2 == NULL) ||
853 ((sibling->ns != NULL) &&
854 (xmlStrEqual(sel->value2,
855 sibling->ns->href))))
858 sibling = sibling->prev;
860 if (sibling == NULL) {
861 /* hum going backward in document order ... */
864 while (sibling != NULL) {
865 if (sibling == previous)
867 if ((sel->value2 == NULL) ||
868 ((sibling->ns != NULL) &&
869 (xmlStrEqual(sel->value2,
870 sibling->ns->href))))
872 sibling = sibling->next;
875 if (sibling != NULL) {
878 * If the node is in a Value Tree we need to
879 * save len, but cannot cache the node!
880 * (bugs 153137 and 158840)
882 if (node->doc != NULL) {
883 len = XSLT_RUNTIME_EXTRA(ctxt,
884 sel->lenExtra, ival);
886 XSLT_RUNTIME_EXTRA(ctxt,
887 sel->previousExtra, ptr) = node;
888 XSLT_RUNTIME_EXTRA(ctxt,
889 sel->indexExtra, ival) = pos;
897 * recompute the index
899 xmlNodePtr siblings = node->parent->children;
900 xmlNodePtr parent = node->parent;
902 while (siblings != NULL) {
903 if (siblings->type == XML_ELEMENT_NODE) {
904 if (siblings == node) {
907 } else if ((node->name != NULL) &&
908 (siblings->name != NULL) &&
909 (node->name[0] == siblings->name[0]) &&
910 (xmlStrEqual(node->name, siblings->name))) {
911 if ((sel->value2 == NULL) ||
912 ((siblings->ns != NULL) &&
913 (xmlStrEqual(sel->value2,
914 siblings->ns->href))))
918 siblings = siblings->next;
920 if ((parent == NULL) || (node->doc == NULL))
923 while (parent->parent != NULL)
924 parent = parent->parent;
925 if (((parent->type != XML_DOCUMENT_NODE) &&
926 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
927 (parent != (xmlNodePtr) node->doc))
932 ctxt->xpathCtxt->contextSize = len;
933 ctxt->xpathCtxt->proximityPosition = pos;
935 * If the node is in a Value Tree we cannot
938 if ((!isRVT) && (node->doc != NULL) &&
940 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
942 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
944 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) =
948 } else if ((sel != NULL) && (sel->op == XSLT_OP_ALL) &&
949 (node->type == XML_ELEMENT_NODE)) {
953 previous = (xmlNodePtr)
954 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
955 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
956 if ((previous != NULL) &&
957 (previous->parent == node->parent)) {
959 * just walk back to adjust the index
962 xmlNodePtr sibling = node;
964 while (sibling != NULL) {
965 if (sibling == previous)
967 if (sibling->type == XML_ELEMENT_NODE)
969 sibling = sibling->prev;
971 if (sibling == NULL) {
972 /* hum going backward in document order ... */
975 while (sibling != NULL) {
976 if (sibling == previous)
978 if (sibling->type == XML_ELEMENT_NODE)
980 sibling = sibling->next;
983 if (sibling != NULL) {
986 * If the node is in a Value Tree we cannot
989 if ((node->doc != NULL) && !isRVT) {
990 len = XSLT_RUNTIME_EXTRA(ctxt,
991 sel->lenExtra, ival);
992 XSLT_RUNTIME_EXTRA(ctxt,
993 sel->previousExtra, ptr) = node;
994 XSLT_RUNTIME_EXTRA(ctxt,
995 sel->indexExtra, ival) = pos;
1001 * recompute the index
1003 xmlNodePtr siblings = node->parent->children;
1004 xmlNodePtr parent = node->parent;
1006 while (siblings != NULL) {
1007 if (siblings->type == XML_ELEMENT_NODE) {
1009 if (siblings == node) {
1013 siblings = siblings->next;
1015 if ((parent == NULL) || (node->doc == NULL))
1018 while (parent->parent != NULL)
1019 parent = parent->parent;
1020 if (((parent->type != XML_DOCUMENT_NODE) &&
1021 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
1022 (parent != (xmlNodePtr) node->doc))
1027 ctxt->xpathCtxt->contextSize = len;
1028 ctxt->xpathCtxt->proximityPosition = pos;
1030 * If the node is in a Value Tree we cannot
1033 if ((node->doc != NULL) && (nocache == 0) && !isRVT) {
1034 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
1036 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
1038 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) =
1043 oldNode = ctxt->node;
1046 if (step->value == NULL)
1048 if (step->comp == NULL)
1051 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
1056 ctxt->xpathCtxt->contextSize = oldCS;
1057 ctxt->xpathCtxt->proximityPosition = oldCP;
1059 ctxt->node = oldNode;
1063 ctxt->xpathCtxt->contextSize = oldCS;
1064 ctxt->xpathCtxt->proximityPosition = oldCP;
1066 ctxt->node = oldNode;
1070 if (node->type != XML_PI_NODE)
1072 if (step->value != NULL) {
1073 if (!xmlStrEqual(step->value, node->name))
1077 case XSLT_OP_COMMENT:
1078 if (node->type != XML_COMMENT_NODE)
1082 if ((node->type != XML_TEXT_NODE) &&
1083 (node->type != XML_CDATA_SECTION_NODE))
1087 switch (node->type) {
1088 case XML_ELEMENT_NODE:
1089 case XML_CDATA_SECTION_NODE:
1091 case XML_COMMENT_NODE:
1101 if (states.states != NULL) {
1102 /* Free the rollback states */
1103 xmlFree(states.states);
1107 /* got an error try to rollback */
1108 if (states.states == NULL)
1110 if (states.nbstates <= 0) {
1111 xmlFree(states.states);
1115 i = states.states[states.nbstates].step;
1116 node = states.states[states.nbstates].node;
1118 fprintf(stderr, "Pop: %d, %s\n", i, node->name);
1124 * xsltTestCompMatchList:
1125 * @ctxt: a XSLT process context
1127 * @comp: the precompiled pattern list
1129 * Test whether the node matches one of the patterns in the list
1131 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
1134 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
1135 xsltCompMatchPtr comp) {
1138 if ((ctxt == NULL) || (node == NULL))
1140 while (comp != NULL) {
1141 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
1149 /************************************************************************
1151 * Dedicated parser for templates *
1153 ************************************************************************/
1155 #define CUR (*ctxt->cur)
1156 #define SKIP(val) ctxt->cur += (val)
1157 #define NXT(val) ctxt->cur[(val)]
1158 #define CUR_PTR ctxt->cur
1160 #define SKIP_BLANKS \
1161 while (IS_BLANK_CH(CUR)) NEXT
1163 #define CURRENT (*ctxt->cur)
1164 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1167 #define PUSH(op, val, val2) \
1168 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1171 xsltSwapTopCompMatch(ctxt->comp);
1173 #define XSLT_ERROR(X) \
1174 { xsltError(ctxt, __FILE__, __LINE__, X); \
1175 ctxt->error = (X); return; }
1177 #define XSLT_ERROR0(X) \
1178 { xsltError(ctxt, __FILE__, __LINE__, X); \
1179 ctxt->error = (X); return(0); }
1183 * @ctxt: the XPath Parser context
1185 * Parse an XPath Litteral:
1187 * [29] Literal ::= '"' [^"]* '"'
1190 * Returns the Literal parsed or NULL
1194 xsltScanLiteral(xsltParserContextPtr ctxt) {
1195 const xmlChar *q, *cur;
1196 xmlChar *ret = NULL;
1203 val = xmlStringCurrentChar(NULL, cur, &len);
1204 while ((IS_CHAR(val)) && (val != '"')) {
1206 val = xmlStringCurrentChar(NULL, cur, &len);
1208 if (!IS_CHAR(val)) {
1212 ret = xmlStrndup(q, cur - q);
1216 } else if (CUR == '\'') {
1219 val = xmlStringCurrentChar(NULL, cur, &len);
1220 while ((IS_CHAR(val)) && (val != '\'')) {
1222 val = xmlStringCurrentChar(NULL, cur, &len);
1224 if (!IS_CHAR(val)) {
1228 ret = xmlStrndup(q, cur - q);
1233 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1242 * @ctxt: the XPath Parser context
1244 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1245 * CombiningChar | Extender
1247 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1249 * [6] Names ::= Name (S Name)*
1251 * Returns the Name parsed or NULL
1255 xsltScanName(xsltParserContextPtr ctxt) {
1256 const xmlChar *q, *cur;
1257 xmlChar *ret = NULL;
1263 val = xmlStringCurrentChar(NULL, cur, &len);
1264 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1267 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1268 (val == '.') || (val == '-') ||
1270 (IS_COMBINING(val)) ||
1271 (IS_EXTENDER(val))) {
1273 val = xmlStringCurrentChar(NULL, cur, &len);
1275 ret = xmlStrndup(q, cur - q);
1282 * @ctxt: the XPath Parser context
1284 * Parses a non qualified name
1286 * Returns the Name parsed or NULL
1290 xsltScanNCName(xsltParserContextPtr ctxt) {
1291 const xmlChar *q, *cur;
1292 xmlChar *ret = NULL;
1298 val = xmlStringCurrentChar(NULL, cur, &len);
1299 if (!IS_LETTER(val) && (val != '_'))
1302 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1303 (val == '.') || (val == '-') ||
1305 (IS_COMBINING(val)) ||
1306 (IS_EXTENDER(val))) {
1308 val = xmlStringCurrentChar(NULL, cur, &len);
1310 ret = xmlStrndup(q, cur - q);
1317 * @ctxt: the XPath Parser context
1318 * @prefix: the place to store the prefix
1320 * Parse a qualified name
1322 * Returns the Name parsed or NULL
1326 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1327 xmlChar *ret = NULL;
1330 ret = xsltScanNCName(ctxt);
1334 ret = xsltScanNCName(ctxt);
1340 * xsltCompileIdKeyPattern:
1341 * @ctxt: the compilation context
1342 * @name: a preparsed name
1343 * @aid: whether id/key are allowed there
1345 * Compile the XSLT LocationIdKeyPattern
1346 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1347 * | 'key' '(' Literal ',' Literal ')'
1349 * also handle NodeType and PI from:
1351 * [7] NodeTest ::= NameTest
1352 * | NodeType '(' ')'
1353 * | 'processing-instruction' '(' Literal ')'
1356 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1357 xmlChar *lit = NULL;
1358 xmlChar *lit2 = NULL;
1361 xsltTransformError(NULL, NULL, NULL,
1362 "xsltCompileIdKeyPattern : ( expected\n");
1366 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1369 lit = xsltScanLiteral(ctxt);
1374 xsltTransformError(NULL, NULL, NULL,
1375 "xsltCompileIdKeyPattern : ) expected\n");
1380 PUSH(XSLT_OP_ID, lit, NULL);
1381 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1384 lit = xsltScanLiteral(ctxt);
1389 xsltTransformError(NULL, NULL, NULL,
1390 "xsltCompileIdKeyPattern : , expected\n");
1396 lit2 = xsltScanLiteral(ctxt);
1401 xsltTransformError(NULL, NULL, NULL,
1402 "xsltCompileIdKeyPattern : ) expected\n");
1407 /* TODO: support namespace in keys */
1408 PUSH(XSLT_OP_KEY, lit, lit2);
1409 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1413 lit = xsltScanLiteral(ctxt);
1418 xsltTransformError(NULL, NULL, NULL,
1419 "xsltCompileIdKeyPattern : ) expected\n");
1425 PUSH(XSLT_OP_PI, lit, NULL);
1426 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1430 xsltTransformError(NULL, NULL, NULL,
1431 "xsltCompileIdKeyPattern : ) expected\n");
1436 PUSH(XSLT_OP_TEXT, NULL, NULL);
1437 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1441 xsltTransformError(NULL, NULL, NULL,
1442 "xsltCompileIdKeyPattern : ) expected\n");
1447 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1448 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1452 xsltTransformError(NULL, NULL, NULL,
1453 "xsltCompileIdKeyPattern : ) expected\n");
1458 PUSH(XSLT_OP_NODE, NULL, NULL);
1460 xsltTransformError(NULL, NULL, NULL,
1461 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1465 xsltTransformError(NULL, NULL, NULL,
1466 "xsltCompileIdKeyPattern : node type\n");
1476 * xsltCompileStepPattern:
1477 * @ctxt: the compilation context
1478 * @token: a posible precompiled name
1480 * Compile the XSLT StepPattern and generates a precompiled
1481 * form suitable for fast matching.
1483 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1484 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1485 * | ('child' | 'attribute') '::'
1487 * [7] NodeTest ::= NameTest
1488 * | NodeType '(' ')'
1489 * | 'processing-instruction' '(' Literal ')'
1490 * [8] Predicate ::= '[' PredicateExpr ']'
1491 * [9] PredicateExpr ::= Expr
1492 * [13] AbbreviatedAxisSpecifier ::= '@'?
1493 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1497 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1498 xmlChar *name = NULL;
1499 const xmlChar *URI = NULL;
1500 xmlChar *URL = NULL;
1504 if ((token == NULL) && (CUR == '@')) {
1505 xmlChar *prefix = NULL;
1510 PUSH(XSLT_OP_ATTR, NULL, NULL);
1511 goto parse_predicate;
1513 token = xsltScanQName(ctxt, &prefix);
1514 if (prefix != NULL) {
1517 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1519 xsltTransformError(NULL, NULL, NULL,
1520 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1523 URL = xmlStrdup(ns->href);
1527 if (token == NULL) {
1530 PUSH(XSLT_OP_ATTR, NULL, URL);
1533 xsltTransformError(NULL, NULL, NULL,
1534 "xsltCompileStepPattern : Name expected\n");
1538 PUSH(XSLT_OP_ATTR, token, URL);
1539 goto parse_predicate;
1542 token = xsltScanName(ctxt);
1543 if (token == NULL) {
1546 PUSH(XSLT_OP_ALL, token, NULL);
1547 goto parse_predicate;
1549 xsltTransformError(NULL, NULL, NULL,
1550 "xsltCompileStepPattern : Name expected\n");
1559 xsltCompileIdKeyPattern(ctxt, token, 0);
1562 } else if (CUR == ':') {
1565 xmlChar *prefix = token;
1569 * This is a namespace match
1571 token = xsltScanName(ctxt);
1572 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1574 xsltTransformError(NULL, NULL, NULL,
1575 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1580 URL = xmlStrdup(ns->href);
1583 if (token == NULL) {
1586 PUSH(XSLT_OP_NS, URL, NULL);
1588 xsltTransformError(NULL, NULL, NULL,
1589 "xsltCompileStepPattern : Name expected\n");
1594 PUSH(XSLT_OP_ELEM, token, URL);
1598 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1600 token = xsltScanName(ctxt);
1601 if (token == NULL) {
1604 PUSH(XSLT_OP_ALL, token, NULL);
1605 goto parse_predicate;
1607 xsltTransformError(NULL, NULL, NULL,
1608 "xsltCompileStepPattern : QName expected\n");
1613 URI = xsltGetQNameURI(ctxt->elem, &token);
1614 if (token == NULL) {
1618 name = xmlStrdup(token);
1620 URL = xmlStrdup(URI);
1622 PUSH(XSLT_OP_CHILD, name, URL);
1623 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1625 token = xsltScanName(ctxt);
1626 if (token == NULL) {
1627 xsltTransformError(NULL, NULL, NULL,
1628 "xsltCompileStepPattern : QName expected\n");
1632 URI = xsltGetQNameURI(ctxt->elem, &token);
1633 if (token == NULL) {
1637 name = xmlStrdup(token);
1639 URL = xmlStrdup(URI);
1641 PUSH(XSLT_OP_ATTR, name, URL);
1643 xsltTransformError(NULL, NULL, NULL,
1644 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1650 } else if (CUR == '*') {
1652 PUSH(XSLT_OP_ALL, token, NULL);
1654 URI = xsltGetQNameURI(ctxt->elem, &token);
1655 if (token == NULL) {
1660 URL = xmlStrdup(URI);
1661 PUSH(XSLT_OP_ELEM, token, URL);
1666 while (CUR == '[') {
1668 xmlChar *ret = NULL;
1674 /* Skip over nested predicates */
1677 else if (CUR == ']') {
1681 } else if (CUR == '"') {
1683 while ((CUR != 0) && (CUR != '"'))
1685 } else if (CUR == '\'') {
1687 while ((CUR != 0) && (CUR != '\''))
1693 xsltTransformError(NULL, NULL, NULL,
1694 "xsltCompileStepPattern : ']' expected\n");
1698 ret = xmlStrndup(q, CUR_PTR - q);
1699 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1700 /* push the predicate lower than local test */
1714 * xsltCompileRelativePathPattern:
1715 * @comp: the compilation context
1716 * @token: a posible precompiled name
1718 * Compile the XSLT RelativePathPattern and generates a precompiled
1719 * form suitable for fast matching.
1721 * [4] RelativePathPattern ::= StepPattern
1722 * | RelativePathPattern '/' StepPattern
1723 * | RelativePathPattern '//' StepPattern
1726 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1727 xsltCompileStepPattern(ctxt, token);
1731 while ((CUR != 0) && (CUR != '|')) {
1732 if ((CUR == '/') && (NXT(1) == '/')) {
1733 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1737 xsltCompileStepPattern(ctxt, NULL);
1738 } else if (CUR == '/') {
1739 PUSH(XSLT_OP_PARENT, NULL, NULL);
1742 if ((CUR != 0) && (CUR != '|')) {
1743 xsltCompileRelativePathPattern(ctxt, NULL);
1757 * xsltCompileLocationPathPattern:
1758 * @ctxt: the compilation context
1760 * Compile the XSLT LocationPathPattern and generates a precompiled
1761 * form suitable for fast matching.
1763 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1764 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1765 * | '//'? RelativePathPattern
1768 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1770 if ((CUR == '/') && (NXT(1) == '/')) {
1772 * since we reverse the query
1773 * a leading // can be safely ignored
1777 ctxt->comp->priority = 0.5; /* '//' means not 0 priority */
1778 xsltCompileRelativePathPattern(ctxt, NULL);
1779 } else if (CUR == '/') {
1781 * We need to find root as the parent
1785 PUSH(XSLT_OP_ROOT, NULL, NULL);
1786 if ((CUR != 0) && (CUR != '|')) {
1787 PUSH(XSLT_OP_PARENT, NULL, NULL);
1788 xsltCompileRelativePathPattern(ctxt, NULL);
1790 } else if (CUR == '*') {
1791 xsltCompileRelativePathPattern(ctxt, NULL);
1792 } else if (CUR == '@') {
1793 xsltCompileRelativePathPattern(ctxt, NULL);
1796 name = xsltScanName(ctxt);
1798 xsltTransformError(NULL, NULL, NULL,
1799 "xsltCompileLocationPathPattern : Name expected\n");
1804 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1805 xsltCompileIdKeyPattern(ctxt, name, 1);
1806 if ((CUR == '/') && (NXT(1) == '/')) {
1807 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1811 xsltCompileRelativePathPattern(ctxt, NULL);
1812 } else if (CUR == '/') {
1813 PUSH(XSLT_OP_PARENT, NULL, NULL);
1816 xsltCompileRelativePathPattern(ctxt, NULL);
1820 xsltCompileRelativePathPattern(ctxt, name);
1827 * xsltCompilePattern:
1828 * @pattern: an XSLT pattern
1829 * @doc: the containing document
1830 * @node: the containing element
1831 * @style: the stylesheet
1832 * @runtime: the transformation context, if done at run-time
1834 * Compile the XSLT pattern and generates a list of precompiled form suitable
1835 * for fast matching.
1837 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1839 * Returns the generated pattern list or NULL in case of failure
1843 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1844 xmlNodePtr node, xsltStylesheetPtr style,
1845 xsltTransformContextPtr runtime) {
1846 xsltParserContextPtr ctxt = NULL;
1847 xsltCompMatchPtr element, first = NULL, previous = NULL;
1848 int current, start, end, level, j;
1850 if (pattern == NULL) {
1851 xsltTransformError(NULL, NULL, node,
1852 "xsltCompilePattern : NULL pattern\n");
1856 ctxt = xsltNewParserContext(style, runtime);
1862 while (pattern[current] != 0) {
1864 while (IS_BLANK_CH(pattern[current]))
1868 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1869 if (pattern[end] == '[')
1871 else if (pattern[end] == ']')
1873 else if (pattern[end] == '\'') {
1875 while ((pattern[end] != 0) && (pattern[end] != '\''))
1877 } else if (pattern[end] == '"') {
1879 while ((pattern[end] != 0) && (pattern[end] != '"'))
1884 if (current == end) {
1885 xsltTransformError(NULL, NULL, node,
1886 "xsltCompilePattern : NULL pattern\n");
1889 element = xsltNewCompMatch();
1890 if (element == NULL) {
1895 else if (previous != NULL)
1896 previous->next = element;
1899 ctxt->comp = element;
1900 ctxt->base = xmlStrndup(&pattern[start], end - start);
1901 if (ctxt->base == NULL)
1903 ctxt->cur = &(ctxt->base)[current - start];
1904 element->pattern = ctxt->base;
1905 element->nsList = xmlGetNsList(doc, node);
1907 if (element->nsList != NULL) {
1908 while (element->nsList[j] != NULL)
1914 #ifdef WITH_XSLT_DEBUG_PATTERN
1915 xsltGenericDebug(xsltGenericDebugContext,
1916 "xsltCompilePattern : parsing '%s'\n",
1920 Preset default priority to be zero.
1921 This may be changed by xsltCompileLocationPathPattern.
1923 element->priority = 0;
1924 xsltCompileLocationPathPattern(ctxt);
1926 xsltTransformError(NULL, style, node,
1927 "xsltCompilePattern : failed to compile '%s'\n",
1929 if (style != NULL) style->errors++;
1934 * Reverse for faster interpretation.
1936 xsltReverseCompMatch(element);
1939 * Set-up the priority
1941 if (element->priority == 0) { /* if not yet determined */
1942 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1943 (element->steps[0].op == XSLT_OP_ATTR) ||
1944 (element->steps[0].op == XSLT_OP_PI)) &&
1945 (element->steps[0].value != NULL) &&
1946 (element->steps[1].op == XSLT_OP_END)) {
1947 ; /* previously preset */
1948 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1949 (element->steps[0].value2 != NULL) &&
1950 (element->steps[1].op == XSLT_OP_END)) {
1951 element->priority = -0.25;
1952 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1953 (element->steps[0].value != NULL) &&
1954 (element->steps[1].op == XSLT_OP_END)) {
1955 element->priority = -0.25;
1956 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1957 (element->steps[0].value == NULL) &&
1958 (element->steps[0].value2 == NULL) &&
1959 (element->steps[1].op == XSLT_OP_END)) {
1960 element->priority = -0.5;
1961 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1962 (element->steps[0].op == XSLT_OP_TEXT) ||
1963 (element->steps[0].op == XSLT_OP_ALL) ||
1964 (element->steps[0].op == XSLT_OP_NODE) ||
1965 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1966 (element->steps[1].op == XSLT_OP_END)) {
1967 element->priority = -0.5;
1969 element->priority = 0.5;
1972 #ifdef WITH_XSLT_DEBUG_PATTERN
1973 xsltGenericDebug(xsltGenericDebugContext,
1974 "xsltCompilePattern : parsed %s, default priority %f\n",
1975 element->pattern, element->priority);
1977 if (pattern[end] == '|')
1982 xsltTransformError(NULL, style, node,
1983 "xsltCompilePattern : NULL pattern\n");
1984 if (style != NULL) style->errors++;
1988 xsltFreeParserContext(ctxt);
1993 xsltFreeParserContext(ctxt);
1995 xsltFreeCompMatchList(first);
1999 /************************************************************************
2001 * Module interfaces *
2003 ************************************************************************/
2007 * @style: an XSLT stylesheet
2008 * @cur: an XSLT template
2009 * @mode: the mode name or NULL
2010 * @modeURI: the mode URI or NULL
2012 * Register the XSLT pattern associated to @cur
2014 * Returns -1 in case of error, 0 otherwise
2017 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
2018 const xmlChar *mode, const xmlChar *modeURI) {
2019 xsltCompMatchPtr pat, list, *top = NULL, next;
2020 const xmlChar *name = NULL;
2021 float priority; /* the priority */
2023 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
2026 priority = cur->priority;
2027 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
2033 pat->template = cur;
2035 pat->mode = xmlDictLookup(style->dict, mode, -1);
2036 if (modeURI != NULL)
2037 pat->modeURI = xmlDictLookup(style->dict, modeURI, -1);
2038 if (priority != XSLT_PAT_NO_PRIORITY)
2039 pat->priority = priority;
2042 * insert it in the hash table list corresponding to its lookup name
2044 switch (pat->steps[0].op) {
2046 if (pat->steps[0].value != NULL)
2047 name = pat->steps[0].value;
2049 top = (xsltCompMatchPtr *) &(style->attrMatch);
2052 case XSLT_OP_PARENT:
2053 case XSLT_OP_ANCESTOR:
2054 top = (xsltCompMatchPtr *) &(style->elemMatch);
2057 top = (xsltCompMatchPtr *) &(style->rootMatch);
2060 top = (xsltCompMatchPtr *) &(style->keyMatch);
2063 /* TODO optimize ID !!! */
2066 top = (xsltCompMatchPtr *) &(style->elemMatch);
2069 case XSLT_OP_PREDICATE:
2070 xsltTransformError(NULL, style, NULL,
2071 "xsltAddTemplate: invalid compiled pattern\n");
2072 xsltFreeCompMatch(pat);
2075 * TODO: some flags at the top level about type based patterns
2076 * would be faster than inclusion in the hash table.
2079 if (pat->steps[0].value != NULL)
2080 name = pat->steps[0].value;
2082 top = (xsltCompMatchPtr *) &(style->piMatch);
2084 case XSLT_OP_COMMENT:
2085 top = (xsltCompMatchPtr *) &(style->commentMatch);
2088 top = (xsltCompMatchPtr *) &(style->textMatch);
2092 if (pat->steps[0].value != NULL)
2093 name = pat->steps[0].value;
2095 top = (xsltCompMatchPtr *) &(style->elemMatch);
2099 if (style->templatesHash == NULL) {
2100 style->templatesHash = xmlHashCreate(1024);
2101 if (style->templatesHash == NULL) {
2102 xsltFreeCompMatch(pat);
2105 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
2107 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
2108 name, mode, modeURI);
2110 xmlHashAddEntry3(style->templatesHash, name,
2111 mode, modeURI, pat);
2114 * Note '<=' since one must choose among the matching
2115 * template rules that are left, the one that occurs
2116 * last in the stylesheet
2118 if (list->priority <= pat->priority) {
2120 xmlHashUpdateEntry3(style->templatesHash, name,
2121 mode, modeURI, pat, NULL);
2123 while (list->next != NULL) {
2124 if (list->next->priority <= pat->priority)
2128 pat->next = list->next;
2133 } else if (top != NULL) {
2138 } else if (list->priority <= pat->priority) {
2142 while (list->next != NULL) {
2143 if (list->next->priority <= pat->priority)
2147 pat->next = list->next;
2151 xsltTransformError(NULL, style, NULL,
2152 "xsltAddTemplate: invalid compiled pattern\n");
2153 xsltFreeCompMatch(pat);
2156 #ifdef WITH_XSLT_DEBUG_PATTERN
2158 xsltGenericDebug(xsltGenericDebugContext,
2159 "added pattern : '%s' mode '%s' priority %f\n",
2160 pat->pattern, pat->mode, pat->priority);
2162 xsltGenericDebug(xsltGenericDebugContext,
2163 "added pattern : '%s' priority %f\n",
2164 pat->pattern, pat->priority);
2174 * @ctxt: a XSLT process context
2175 * @node: the node being processed
2176 * @style: the current style
2178 * Finds the template applying to this node, if @style is non-NULL
2179 * it means one needs to look for the next imported template in scope.
2181 * Returns the xsltTemplatePtr or NULL if not found
2184 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2185 xsltStylesheetPtr style) {
2186 xsltStylesheetPtr curstyle;
2187 xsltTemplatePtr ret = NULL;
2188 const xmlChar *name = NULL;
2189 xsltCompMatchPtr list = NULL;
2193 if ((ctxt == NULL) || (node == NULL))
2196 if (style == NULL) {
2197 curstyle = ctxt->style;
2199 curstyle = xsltNextImport(style);
2202 while ((curstyle != NULL) && (curstyle != style)) {
2203 priority = XSLT_PAT_NO_PRIORITY;
2204 /* TODO : handle IDs/keys here ! */
2205 if (curstyle->templatesHash != NULL) {
2207 * Use the top name as selector
2209 switch (node->type) {
2210 case XML_ELEMENT_NODE:
2211 if (node->name[0] == ' ')
2213 case XML_ATTRIBUTE_NODE:
2217 case XML_DOCUMENT_NODE:
2218 case XML_HTML_DOCUMENT_NODE:
2220 case XML_CDATA_SECTION_NODE:
2221 case XML_COMMENT_NODE:
2222 case XML_ENTITY_REF_NODE:
2223 case XML_ENTITY_NODE:
2224 case XML_DOCUMENT_TYPE_NODE:
2225 case XML_DOCUMENT_FRAG_NODE:
2226 case XML_NOTATION_NODE:
2228 case XML_ELEMENT_DECL:
2229 case XML_ATTRIBUTE_DECL:
2230 case XML_ENTITY_DECL:
2231 case XML_NAMESPACE_DECL:
2232 case XML_XINCLUDE_START:
2233 case XML_XINCLUDE_END:
2242 * find the list of applicable expressions based on the name
2244 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2245 name, ctxt->mode, ctxt->modeURI);
2248 while (list != NULL) {
2249 if (xsltTestCompMatch(ctxt, list, node,
2250 ctxt->mode, ctxt->modeURI)) {
2251 ret = list->template;
2252 priority = list->priority;
2260 * find alternate generic matches
2262 switch (node->type) {
2263 case XML_ELEMENT_NODE:
2264 if (node->name[0] == ' ')
2265 list = curstyle->rootMatch;
2267 list = curstyle->elemMatch;
2268 if (node->psvi != NULL) keyed = 1;
2270 case XML_ATTRIBUTE_NODE: {
2273 list = curstyle->attrMatch;
2274 attr = (xmlAttrPtr) node;
2275 if (attr->psvi != NULL) keyed = 1;
2279 list = curstyle->piMatch;
2280 if (node->psvi != NULL) keyed = 1;
2282 case XML_DOCUMENT_NODE:
2283 case XML_HTML_DOCUMENT_NODE: {
2286 list = curstyle->rootMatch;
2287 doc = (xmlDocPtr) node;
2288 if (doc->psvi != NULL) keyed = 1;
2292 case XML_CDATA_SECTION_NODE:
2293 list = curstyle->textMatch;
2294 if (node->psvi != NULL) keyed = 1;
2296 case XML_COMMENT_NODE:
2297 list = curstyle->commentMatch;
2298 if (node->psvi != NULL) keyed = 1;
2300 case XML_ENTITY_REF_NODE:
2301 case XML_ENTITY_NODE:
2302 case XML_DOCUMENT_TYPE_NODE:
2303 case XML_DOCUMENT_FRAG_NODE:
2304 case XML_NOTATION_NODE:
2306 case XML_ELEMENT_DECL:
2307 case XML_ATTRIBUTE_DECL:
2308 case XML_ENTITY_DECL:
2309 case XML_NAMESPACE_DECL:
2310 case XML_XINCLUDE_START:
2311 case XML_XINCLUDE_END:
2316 while ((list != NULL) &&
2317 ((ret == NULL) || (list->priority > priority))) {
2318 if (xsltTestCompMatch(ctxt, list, node,
2319 ctxt->mode, ctxt->modeURI)) {
2320 ret = list->template;
2321 priority = list->priority;
2327 * Some of the tests for elements can also apply to documents
2329 if ((node->type == XML_DOCUMENT_NODE) ||
2330 (node->type == XML_HTML_DOCUMENT_NODE) ||
2331 (node->type == XML_TEXT_NODE)) {
2332 list = curstyle->elemMatch;
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;
2343 } else if ((node->type == XML_PI_NODE) ||
2344 (node->type == XML_COMMENT_NODE)) {
2345 list = curstyle->elemMatch;
2346 while ((list != NULL) &&
2347 ((ret == NULL) || (list->priority > priority))) {
2348 if (xsltTestCompMatch(ctxt, list, node,
2349 ctxt->mode, ctxt->modeURI)) {
2350 ret = list->template;
2351 priority = list->priority;
2359 list = curstyle->keyMatch;
2360 while ((list != NULL) &&
2361 ((ret == NULL) || (list->priority > priority))) {
2362 if (xsltTestCompMatch(ctxt, list, node,
2363 ctxt->mode, ctxt->modeURI)) {
2364 ret = list->template;
2365 priority = list->priority;
2375 * Cycle on next curstylesheet import.
2377 curstyle = xsltNextImport(curstyle);
2383 * xsltCleanupTemplates:
2384 * @style: an XSLT stylesheet
2386 * Cleanup the state of the templates used by the stylesheet and
2387 * the ones it imports.
2390 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2394 * xsltFreeTemplateHashes:
2395 * @style: an XSLT stylesheet
2397 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2400 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2401 if (style->templatesHash != NULL)
2402 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2403 (xmlHashDeallocator) xsltFreeCompMatchList);
2404 if (style->rootMatch != NULL)
2405 xsltFreeCompMatchList(style->rootMatch);
2406 if (style->keyMatch != NULL)
2407 xsltFreeCompMatchList(style->keyMatch);
2408 if (style->elemMatch != NULL)
2409 xsltFreeCompMatchList(style->elemMatch);
2410 if (style->attrMatch != NULL)
2411 xsltFreeCompMatchList(style->attrMatch);
2412 if (style->parentMatch != NULL)
2413 xsltFreeCompMatchList(style->parentMatch);
2414 if (style->textMatch != NULL)
2415 xsltFreeCompMatchList(style->textMatch);
2416 if (style->piMatch != NULL)
2417 xsltFreeCompMatchList(style->piMatch);
2418 if (style->commentMatch != NULL)
2419 xsltFreeCompMatchList(style->commentMatch);