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
64 typedef struct _xsltStepOp xsltStepOp;
65 typedef xsltStepOp *xsltStepOpPtr;
71 xmlXPathCompExprPtr comp;
73 * Optimisations for count
80 struct _xsltCompMatch {
81 struct _xsltCompMatch *next; /* siblings in the name hash */
82 float priority; /* the priority */
83 const xmlChar *pattern; /* the pattern */
84 const xmlChar *mode; /* the mode */
85 const xmlChar *modeURI; /* the mode URI */
86 xsltTemplatePtr template; /* the associated template */
88 /* TODO fix the statically allocated size steps[] */
91 xmlNsPtr *nsList; /* the namespaces in scope */
92 int nsNr; /* the number of namespaces in scope */
93 xsltStepOp steps[40]; /* ops for computation */
96 typedef struct _xsltParserContext xsltParserContext;
97 typedef xsltParserContext *xsltParserContextPtr;
98 struct _xsltParserContext {
99 xsltStylesheetPtr style; /* the stylesheet */
100 xsltTransformContextPtr ctxt; /* the transformation or NULL */
101 const xmlChar *cur; /* the current char being parsed */
102 const xmlChar *base; /* the full expression */
103 xmlDocPtr doc; /* the source document */
104 xmlNodePtr elem; /* the source element */
105 int error; /* error code */
106 xsltCompMatchPtr comp; /* the result */
109 /************************************************************************
113 ************************************************************************/
118 * Create a new XSLT CompMatch
120 * Returns the newly allocated xsltCompMatchPtr or NULL in case of error
122 static xsltCompMatchPtr
123 xsltNewCompMatch(void) {
124 xsltCompMatchPtr cur;
126 cur = (xsltCompMatchPtr) xmlMalloc(sizeof(xsltCompMatch));
128 xsltTransformError(NULL, NULL, NULL,
129 "xsltNewCompMatch : malloc failed\n");
132 memset(cur, 0, sizeof(xsltCompMatch));
141 * @comp: an XSLT comp
143 * Free up the memory allocated by @comp
146 xsltFreeCompMatch(xsltCompMatchPtr comp) {
152 if (comp->pattern != NULL)
153 xmlFree((xmlChar *)comp->pattern);
154 if (comp->nsList != NULL)
155 xmlFree(comp->nsList);
156 for (i = 0;i < comp->nbStep;i++) {
157 op = &comp->steps[i];
158 if (op->value != NULL)
160 if (op->value2 != NULL)
162 if (op->value3 != NULL)
164 if (op->comp != NULL)
165 xmlXPathFreeCompExpr(op->comp);
167 memset(comp, -1, sizeof(xsltCompMatch));
172 * xsltFreeCompMatchList:
173 * @comp: an XSLT comp list
175 * Free up the memory allocated by all the elements of @comp
178 xsltFreeCompMatchList(xsltCompMatchPtr comp) {
179 xsltCompMatchPtr cur;
181 while (comp != NULL) {
184 xsltFreeCompMatch(cur);
189 * xsltNormalizeCompSteps:
190 * @payload: pointer to template hash table entry
191 * @data: pointer to the stylesheet
192 * @name: template match name
194 * This is a hashtable scanner function to normalize the compiled
195 * steps of an imported stylesheet.
197 void xsltNormalizeCompSteps(void *payload,
198 void *data, const xmlChar *name ATTRIBUTE_UNUSED) {
199 xsltCompMatchPtr comp = payload;
200 xsltStylesheetPtr style = data;
203 for (ix = 0; ix < comp->nbStep; ix++) {
204 comp->steps[ix].previousExtra += style->extrasNr;
205 comp->steps[ix].indexExtra += style->extrasNr;
206 comp->steps[ix].lenExtra += style->extrasNr;
211 * xsltNewParserContext:
212 * @style: the stylesheet
213 * @ctxt: the transformation context, if done at run-time
215 * Create a new XSLT ParserContext
217 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
219 static xsltParserContextPtr
220 xsltNewParserContext(xsltStylesheetPtr style, xsltTransformContextPtr ctxt) {
221 xsltParserContextPtr cur;
223 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
225 xsltTransformError(NULL, NULL, NULL,
226 "xsltNewParserContext : malloc failed\n");
229 memset(cur, 0, sizeof(xsltParserContext));
236 * xsltFreeParserContext:
237 * @ctxt: an XSLT parser context
239 * Free up the memory allocated by @ctxt
242 xsltFreeParserContext(xsltParserContextPtr ctxt) {
245 memset(ctxt, -1, sizeof(xsltParserContext));
251 * @comp: the compiled match expression
253 * @value: the first value
254 * @value2: the second value
256 * Add an step to an XSLT Compiled Match
258 * Returns -1 in case of failure, 0 otherwise.
261 xsltCompMatchAdd(xsltParserContextPtr ctxt, xsltCompMatchPtr comp,
262 xsltOp op, xmlChar * value, xmlChar * value2)
264 if (comp->nbStep >= 40) {
265 xsltTransformError(NULL, NULL, NULL,
266 "xsltCompMatchAdd: overflow\n");
269 comp->steps[comp->nbStep].op = op;
270 comp->steps[comp->nbStep].value = value;
271 comp->steps[comp->nbStep].value2 = value2;
272 if (ctxt->ctxt != NULL) {
273 comp->steps[comp->nbStep].previousExtra =
274 xsltAllocateExtraCtxt(ctxt->ctxt);
275 comp->steps[comp->nbStep].indexExtra =
276 xsltAllocateExtraCtxt(ctxt->ctxt);
277 comp->steps[comp->nbStep].lenExtra =
278 xsltAllocateExtraCtxt(ctxt->ctxt);
280 comp->steps[comp->nbStep].previousExtra =
281 xsltAllocateExtra(ctxt->style);
282 comp->steps[comp->nbStep].indexExtra =
283 xsltAllocateExtra(ctxt->style);
284 comp->steps[comp->nbStep].lenExtra =
285 xsltAllocateExtra(ctxt->style);
287 if (op == XSLT_OP_PREDICATE) {
288 comp->steps[comp->nbStep].comp = xsltXPathCompile(ctxt->style, value);
295 * xsltSwapTopCompMatch:
296 * @comp: the compiled match expression
298 * reverse the two top steps.
301 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
303 int j = comp->nbStep - 1;
306 register xmlChar *tmp;
308 register xmlXPathCompExprPtr expr;
310 tmp = comp->steps[i].value;
311 comp->steps[i].value = comp->steps[j].value;
312 comp->steps[j].value = tmp;
313 tmp = comp->steps[i].value2;
314 comp->steps[i].value2 = comp->steps[j].value2;
315 comp->steps[j].value2 = tmp;
316 op = comp->steps[i].op;
317 comp->steps[i].op = comp->steps[j].op;
318 comp->steps[j].op = op;
319 expr = comp->steps[i].comp;
320 comp->steps[i].comp = comp->steps[j].comp;
321 comp->steps[j].comp = expr;
326 * xsltReverseCompMatch:
327 * @comp: the compiled match expression
329 * reverse all the stack of expressions
332 xsltReverseCompMatch(xsltCompMatchPtr comp) {
334 int j = comp->nbStep - 1;
337 register xmlChar *tmp;
339 register xmlXPathCompExprPtr expr;
340 tmp = comp->steps[i].value;
341 comp->steps[i].value = comp->steps[j].value;
342 comp->steps[j].value = tmp;
343 tmp = comp->steps[i].value2;
344 comp->steps[i].value2 = comp->steps[j].value2;
345 comp->steps[j].value2 = tmp;
346 op = comp->steps[i].op;
347 comp->steps[i].op = comp->steps[j].op;
348 comp->steps[j].op = op;
349 expr = comp->steps[i].comp;
350 comp->steps[i].comp = comp->steps[j].comp;
351 comp->steps[j].comp = expr;
355 comp->steps[comp->nbStep++].op = XSLT_OP_END;
358 /************************************************************************
360 * The interpreter for the precompiled patterns *
362 ************************************************************************/
366 * @ctxt: a XSLT process context
367 * @comp: the precompiled pattern
369 * @mode: the mode name or NULL
370 * @modeURI: the mode URI or NULL
372 * Test whether the node matches the pattern
374 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
377 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
378 xmlNodePtr node, const xmlChar *mode,
379 const xmlChar *modeURI) {
381 xsltStepOpPtr step, sel = NULL;
383 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
384 xsltTransformError(ctxt, NULL, node,
385 "xsltTestCompMatch: null arg\n");
389 if (comp->mode == NULL)
392 * both mode strings must be interned on the stylesheet dictionary
394 if (comp->mode != mode)
397 if (comp->mode != NULL)
400 if (modeURI != NULL) {
401 if (comp->modeURI == NULL)
404 * both modeURI strings must be interned on the stylesheet dictionary
406 if (comp->modeURI != modeURI)
409 if (comp->modeURI != NULL)
412 for (i = 0;i < comp->nbStep;i++) {
413 step = &comp->steps[i];
414 if (step->op != XSLT_OP_PREDICATE)
420 if ((node->type == XML_DOCUMENT_NODE) ||
421 #ifdef LIBXML_DOCB_ENABLED
422 (node->type == XML_DOCB_DOCUMENT_NODE) ||
424 (node->type == XML_HTML_DOCUMENT_NODE))
426 if ((node->type == XML_ELEMENT_NODE) && (node->name[0] == ' '))
430 if (node->type != XML_ELEMENT_NODE)
432 if (step->value == NULL)
434 if (step->value[0] != node->name[0])
436 if (!xmlStrEqual(step->value, node->name))
440 if (node->ns == NULL) {
441 if (step->value2 != NULL)
443 } else if (node->ns->href != NULL) {
444 if (step->value2 == NULL)
446 if (!xmlStrEqual(step->value2, node->ns->href))
450 case XSLT_OP_CHILD: {
453 if ((node->type != XML_ELEMENT_NODE) &&
454 (node->type != XML_DOCUMENT_NODE) &&
455 #ifdef LIBXML_DOCB_ENABLED
456 (node->type != XML_DOCB_DOCUMENT_NODE) &&
458 (node->type != XML_HTML_DOCUMENT_NODE))
461 lst = node->children;
463 if (step->value != NULL) {
464 while (lst != NULL) {
465 if ((lst->type == XML_ELEMENT_NODE) &&
466 (step->value[0] == lst->name[0]) &&
467 (xmlStrEqual(step->value, lst->name)))
477 if (node->type != XML_ATTRIBUTE_NODE)
479 if (step->value != NULL) {
480 if (step->value[0] != node->name[0])
482 if (!xmlStrEqual(step->value, node->name))
486 if (node->ns == NULL) {
487 if (step->value2 != NULL)
489 } else if (step->value2 != NULL) {
490 if (!xmlStrEqual(step->value2, node->ns->href))
495 if ((node->type == XML_DOCUMENT_NODE) ||
496 (node->type == XML_HTML_DOCUMENT_NODE) ||
497 #ifdef LIBXML_DOCB_ENABLED
498 (node->type == XML_DOCB_DOCUMENT_NODE) ||
500 (node->type == XML_NAMESPACE_DECL))
505 if (step->value == NULL)
507 if (step->value[0] != node->name[0])
509 if (!xmlStrEqual(step->value, node->name))
512 if (node->ns == NULL) {
513 if (step->value2 != NULL)
515 } else if (node->ns->href != NULL) {
516 if (step->value2 == NULL)
518 if (!xmlStrEqual(step->value2, node->ns->href))
522 case XSLT_OP_ANCESTOR:
523 /* TODO: implement coalescing of ANCESTOR/NODE ops */
524 if (step->value == NULL) {
525 step = &comp->steps[i+1];
526 if (step->op == XSLT_OP_ROOT)
528 /* added NS, ID and KEY as a result of bug 168208 */
529 if ((step->op != XSLT_OP_ELEM) &&
530 (step->op != XSLT_OP_ALL) &&
531 (step->op != XSLT_OP_NS) &&
532 (step->op != XSLT_OP_ID) &&
533 (step->op != XSLT_OP_KEY))
538 if ((node->type == XML_DOCUMENT_NODE) ||
539 (node->type == XML_HTML_DOCUMENT_NODE) ||
540 #ifdef LIBXML_DOCB_ENABLED
541 (node->type == XML_DOCB_DOCUMENT_NODE) ||
543 (node->type == XML_NAMESPACE_DECL))
546 if ((step->op != XSLT_OP_ELEM) && step->op != XSLT_OP_ALL)
549 if (step->value == NULL)
551 while (node != NULL) {
554 if ((node->type == XML_ELEMENT_NODE) &&
555 (step->value[0] == node->name[0]) &&
556 (xmlStrEqual(step->value, node->name))) {
558 if (node->ns == NULL) {
559 if (step->value2 == NULL)
561 } else if (node->ns->href != NULL) {
562 if ((step->value2 != NULL) &&
563 (xmlStrEqual(step->value2, node->ns->href)))
573 /* TODO Handle IDs decently, must be done differently */
576 if (node->type != XML_ELEMENT_NODE)
579 id = xmlGetID(node->doc, step->value);
580 if ((id == NULL) || (id->parent != node))
588 list = xsltGetKey(ctxt, step->value,
589 step->value3, step->value2);
592 for (indx = 0;indx < list->nodeNr;indx++)
593 if (list->nodeTab[indx] == node)
595 if (indx >= list->nodeNr)
600 if (node->type != XML_ELEMENT_NODE)
602 if (node->ns == NULL) {
603 if (step->value != NULL)
605 } else if (node->ns->href != NULL) {
606 if (step->value == NULL)
608 if (!xmlStrEqual(step->value, node->ns->href))
613 if (node->type != XML_ELEMENT_NODE)
616 case XSLT_OP_PREDICATE: {
620 int pos = 0, len = 0;
625 (doc->name != NULL) &&
626 (doc->name[0] == ' ') &&
627 (xmlStrEqual(BAD_CAST doc->name,
628 BAD_CAST " fake node libxslt")))
633 * The simple existing predicate code cannot handle
634 * properly cascaded predicates. If in this situation
635 * compute directly the full node list once and check
636 * if the node is in the result list.
638 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
640 xmlXPathObjectPtr list;
644 prevdoc = (xmlDocPtr)
645 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
646 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
647 list = (xmlXPathObjectPtr)
648 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra);
650 if ((list == NULL) || (prevdoc != doc)) {
652 xmlXPathObjectPtr newlist;
653 xmlNodePtr parent = node->parent;
657 if (comp->pattern[0] == '/')
658 query = xmlStrdup(comp->pattern);
660 query = xmlStrdup((const xmlChar *)"//");
661 query = xmlStrcat(query, comp->pattern);
663 oldnode = ctxt->xpathCtxt->node;
664 olddoc = ctxt->xpathCtxt->doc;
665 ctxt->xpathCtxt->node = node;
666 ctxt->xpathCtxt->doc = doc;
667 newlist = xmlXPathEval(query, ctxt->xpathCtxt);
668 ctxt->xpathCtxt->node = oldnode;
669 ctxt->xpathCtxt->doc = olddoc;
673 if (newlist->type != XPATH_NODESET) {
674 xmlXPathFreeObject(newlist);
679 if ((parent == NULL) || (node->doc == NULL) || isRVT)
684 xmlXPathFreeObject(list);
687 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra) =
689 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
691 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
693 XSLT_RUNTIME_EXTRA_FREE(ctxt, sel->lenExtra) =
694 (xmlFreeFunc) xmlXPathFreeObject;
698 if ((list->nodesetval == NULL) ||
699 (list->nodesetval->nodeNr <= 0)) {
701 xmlXPathFreeObject(list);
704 /* TODO: store the index and use it for the scan */
706 for (j = 0;j < list->nodesetval->nodeNr;j++) {
707 if (list->nodesetval->nodeTab[j] == node) {
709 xmlXPathFreeObject(list);
716 xmlXPathFreeObject(list);
720 * Depending on the last selection, one may need to
721 * recompute contextSize and proximityPosition.
723 * TODO: make this thread safe !
725 oldCS = ctxt->xpathCtxt->contextSize;
726 oldCP = ctxt->xpathCtxt->proximityPosition;
728 (sel->op == XSLT_OP_ELEM) &&
729 (sel->value != NULL) &&
730 (node->type == XML_ELEMENT_NODE) &&
731 (node->parent != NULL)) {
735 previous = (xmlNodePtr)
736 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
737 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
738 if ((previous != NULL) &&
739 (previous->parent == node->parent)) {
741 * just walk back to adjust the index
744 xmlNodePtr sibling = node;
746 while (sibling != NULL) {
747 if (sibling == previous)
749 if ((previous->type == XML_ELEMENT_NODE) &&
750 (previous->name != NULL) &&
751 (sibling->name != NULL) &&
752 (previous->name[0] == sibling->name[0]) &&
753 (xmlStrEqual(previous->name, sibling->name))) {
754 if ((sel->value2 == NULL) ||
755 ((sibling->ns != NULL) &&
756 (xmlStrEqual(sel->value2,
757 sibling->ns->href))))
760 sibling = sibling->prev;
762 if (sibling == NULL) {
763 /* hum going backward in document order ... */
766 while (sibling != NULL) {
767 if (sibling == previous)
769 if ((sel->value2 == NULL) ||
770 ((sibling->ns != NULL) &&
771 (xmlStrEqual(sel->value2,
772 sibling->ns->href))))
774 sibling = sibling->next;
777 if (sibling != NULL) {
780 * If the node is in a Value Tree we need to
781 * save len, but cannot cache the node!
782 * (bugs 153137 and 158840)
784 if (node->doc != NULL) {
785 len = XSLT_RUNTIME_EXTRA(ctxt,
786 sel->lenExtra, ival);
788 XSLT_RUNTIME_EXTRA(ctxt,
789 sel->previousExtra, ptr) = node;
790 XSLT_RUNTIME_EXTRA(ctxt,
791 sel->indexExtra, ival) = pos;
799 * recompute the index
801 xmlNodePtr siblings = node->parent->children;
802 xmlNodePtr parent = node->parent;
804 while (siblings != NULL) {
805 if (siblings->type == XML_ELEMENT_NODE) {
806 if (siblings == node) {
809 } else if ((node->name != NULL) &&
810 (siblings->name != NULL) &&
811 (node->name[0] == siblings->name[0]) &&
812 (xmlStrEqual(node->name, siblings->name))) {
813 if ((sel->value2 == NULL) ||
814 ((siblings->ns != NULL) &&
815 (xmlStrEqual(sel->value2,
816 siblings->ns->href))))
820 siblings = siblings->next;
822 if ((parent == NULL) || (node->doc == NULL))
825 while (parent->parent != NULL)
826 parent = parent->parent;
827 if (((parent->type != XML_DOCUMENT_NODE) &&
828 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
829 (parent != (xmlNodePtr) node->doc))
834 ctxt->xpathCtxt->contextSize = len;
835 ctxt->xpathCtxt->proximityPosition = pos;
837 * If the node is in a Value Tree we cannot
840 if ((!isRVT) && (node->doc != NULL) &&
842 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
844 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
846 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) =
850 } else if ((sel != NULL) && (sel->op == XSLT_OP_ALL) &&
851 (node->type == XML_ELEMENT_NODE)) {
855 previous = (xmlNodePtr)
856 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
857 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
858 if ((previous != NULL) &&
859 (previous->parent == node->parent)) {
861 * just walk back to adjust the index
864 xmlNodePtr sibling = node;
866 while (sibling != NULL) {
867 if (sibling == previous)
869 if (sibling->type == XML_ELEMENT_NODE)
871 sibling = sibling->prev;
873 if (sibling == NULL) {
874 /* hum going backward in document order ... */
877 while (sibling != NULL) {
878 if (sibling == previous)
880 if (sibling->type == XML_ELEMENT_NODE)
882 sibling = sibling->next;
885 if (sibling != NULL) {
888 * If the node is in a Value Tree we cannot
891 if ((node->doc != NULL) && !isRVT) {
892 len = XSLT_RUNTIME_EXTRA(ctxt,
893 sel->lenExtra, ival);
894 XSLT_RUNTIME_EXTRA(ctxt,
895 sel->previousExtra, ptr) = node;
896 XSLT_RUNTIME_EXTRA(ctxt,
897 sel->indexExtra, ival) = pos;
903 * recompute the index
905 xmlNodePtr siblings = node->parent->children;
906 xmlNodePtr parent = node->parent;
908 while (siblings != NULL) {
909 if (siblings->type == XML_ELEMENT_NODE) {
911 if (siblings == node) {
915 siblings = siblings->next;
917 if ((parent == NULL) || (node->doc == NULL))
920 while (parent->parent != NULL)
921 parent = parent->parent;
922 if (((parent->type != XML_DOCUMENT_NODE) &&
923 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
924 (parent != (xmlNodePtr) node->doc))
929 ctxt->xpathCtxt->contextSize = len;
930 ctxt->xpathCtxt->proximityPosition = pos;
932 * If the node is in a Value Tree we cannot
935 if ((node->doc != NULL) && (nocache == 0) && !isRVT) {
936 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
938 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
940 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) =
945 oldNode = ctxt->node;
948 if (step->value == NULL)
950 if (step->comp == NULL)
953 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
958 ctxt->xpathCtxt->contextSize = oldCS;
959 ctxt->xpathCtxt->proximityPosition = oldCP;
961 ctxt->node = oldNode;
965 ctxt->xpathCtxt->contextSize = oldCS;
966 ctxt->xpathCtxt->proximityPosition = oldCP;
968 ctxt->node = oldNode;
972 if (node->type != XML_PI_NODE)
974 if (step->value != NULL) {
975 if (!xmlStrEqual(step->value, node->name))
979 case XSLT_OP_COMMENT:
980 if (node->type != XML_COMMENT_NODE)
984 if ((node->type != XML_TEXT_NODE) &&
985 (node->type != XML_CDATA_SECTION_NODE))
989 switch (node->type) {
990 case XML_ELEMENT_NODE:
991 case XML_CDATA_SECTION_NODE:
993 case XML_COMMENT_NODE:
1006 * xsltTestCompMatchList:
1007 * @ctxt: a XSLT process context
1009 * @comp: the precompiled pattern list
1011 * Test whether the node matches one of the patterns in the list
1013 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
1016 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
1017 xsltCompMatchPtr comp) {
1020 if ((ctxt == NULL) || (node == NULL))
1022 while (comp != NULL) {
1023 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
1031 /************************************************************************
1033 * Dedicated parser for templates *
1035 ************************************************************************/
1037 #define CUR (*ctxt->cur)
1038 #define SKIP(val) ctxt->cur += (val)
1039 #define NXT(val) ctxt->cur[(val)]
1040 #define CUR_PTR ctxt->cur
1042 #define SKIP_BLANKS \
1043 while (IS_BLANK_CH(CUR)) NEXT
1045 #define CURRENT (*ctxt->cur)
1046 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1049 #define PUSH(op, val, val2) \
1050 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1053 xsltSwapTopCompMatch(ctxt->comp);
1055 #define XSLT_ERROR(X) \
1056 { xsltError(ctxt, __FILE__, __LINE__, X); \
1057 ctxt->error = (X); return; }
1059 #define XSLT_ERROR0(X) \
1060 { xsltError(ctxt, __FILE__, __LINE__, X); \
1061 ctxt->error = (X); return(0); }
1065 * @ctxt: the XPath Parser context
1067 * Parse an XPath Litteral:
1069 * [29] Literal ::= '"' [^"]* '"'
1072 * Returns the Literal parsed or NULL
1076 xsltScanLiteral(xsltParserContextPtr ctxt) {
1077 const xmlChar *q, *cur;
1078 xmlChar *ret = NULL;
1085 val = xmlStringCurrentChar(NULL, cur, &len);
1086 while ((IS_CHAR(val)) && (val != '"')) {
1088 val = xmlStringCurrentChar(NULL, cur, &len);
1090 if (!IS_CHAR(val)) {
1094 ret = xmlStrndup(q, cur - q);
1098 } else if (CUR == '\'') {
1101 val = xmlStringCurrentChar(NULL, cur, &len);
1102 while ((IS_CHAR(val)) && (val != '\'')) {
1104 val = xmlStringCurrentChar(NULL, cur, &len);
1106 if (!IS_CHAR(val)) {
1110 ret = xmlStrndup(q, cur - q);
1115 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1124 * @ctxt: the XPath Parser context
1126 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1127 * CombiningChar | Extender
1129 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1131 * [6] Names ::= Name (S Name)*
1133 * Returns the Name parsed or NULL
1137 xsltScanName(xsltParserContextPtr ctxt) {
1138 const xmlChar *q, *cur;
1139 xmlChar *ret = NULL;
1145 val = xmlStringCurrentChar(NULL, cur, &len);
1146 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1149 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1150 (val == '.') || (val == '-') ||
1152 (IS_COMBINING(val)) ||
1153 (IS_EXTENDER(val))) {
1155 val = xmlStringCurrentChar(NULL, cur, &len);
1157 ret = xmlStrndup(q, cur - q);
1164 * @ctxt: the XPath Parser context
1166 * Parses a non qualified name
1168 * Returns the Name parsed or NULL
1172 xsltScanNCName(xsltParserContextPtr ctxt) {
1173 const xmlChar *q, *cur;
1174 xmlChar *ret = NULL;
1180 val = xmlStringCurrentChar(NULL, cur, &len);
1181 if (!IS_LETTER(val) && (val != '_'))
1184 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1185 (val == '.') || (val == '-') ||
1187 (IS_COMBINING(val)) ||
1188 (IS_EXTENDER(val))) {
1190 val = xmlStringCurrentChar(NULL, cur, &len);
1192 ret = xmlStrndup(q, cur - q);
1199 * @ctxt: the XPath Parser context
1200 * @prefix: the place to store the prefix
1202 * Parse a qualified name
1204 * Returns the Name parsed or NULL
1208 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1209 xmlChar *ret = NULL;
1212 ret = xsltScanNCName(ctxt);
1216 ret = xsltScanNCName(ctxt);
1222 * xsltCompileIdKeyPattern:
1223 * @ctxt: the compilation context
1224 * @name: a preparsed name
1225 * @aid: whether id/key are allowed there
1227 * Compile the XSLT LocationIdKeyPattern
1228 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1229 * | 'key' '(' Literal ',' Literal ')'
1231 * also handle NodeType and PI from:
1233 * [7] NodeTest ::= NameTest
1234 * | NodeType '(' ')'
1235 * | 'processing-instruction' '(' Literal ')'
1238 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1239 xmlChar *lit = NULL;
1240 xmlChar *lit2 = NULL;
1243 xsltTransformError(NULL, NULL, NULL,
1244 "xsltCompileIdKeyPattern : ( expected\n");
1248 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1251 lit = xsltScanLiteral(ctxt);
1256 xsltTransformError(NULL, NULL, NULL,
1257 "xsltCompileIdKeyPattern : ) expected\n");
1262 PUSH(XSLT_OP_ID, lit, NULL);
1263 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1266 lit = xsltScanLiteral(ctxt);
1271 xsltTransformError(NULL, NULL, NULL,
1272 "xsltCompileIdKeyPattern : , expected\n");
1278 lit2 = xsltScanLiteral(ctxt);
1283 xsltTransformError(NULL, NULL, NULL,
1284 "xsltCompileIdKeyPattern : ) expected\n");
1289 /* TODO: support namespace in keys */
1290 PUSH(XSLT_OP_KEY, lit, lit2);
1291 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1295 lit = xsltScanLiteral(ctxt);
1300 xsltTransformError(NULL, NULL, NULL,
1301 "xsltCompileIdKeyPattern : ) expected\n");
1307 PUSH(XSLT_OP_PI, lit, NULL);
1308 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1312 xsltTransformError(NULL, NULL, NULL,
1313 "xsltCompileIdKeyPattern : ) expected\n");
1318 PUSH(XSLT_OP_TEXT, NULL, NULL);
1319 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1323 xsltTransformError(NULL, NULL, NULL,
1324 "xsltCompileIdKeyPattern : ) expected\n");
1329 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1330 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1334 xsltTransformError(NULL, NULL, NULL,
1335 "xsltCompileIdKeyPattern : ) expected\n");
1340 PUSH(XSLT_OP_NODE, NULL, NULL);
1342 xsltTransformError(NULL, NULL, NULL,
1343 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1347 xsltTransformError(NULL, NULL, NULL,
1348 "xsltCompileIdKeyPattern : node type\n");
1358 * xsltCompileStepPattern:
1359 * @ctxt: the compilation context
1360 * @token: a posible precompiled name
1362 * Compile the XSLT StepPattern and generates a precompiled
1363 * form suitable for fast matching.
1365 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1366 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1367 * | ('child' | 'attribute') '::'
1369 * [7] NodeTest ::= NameTest
1370 * | NodeType '(' ')'
1371 * | 'processing-instruction' '(' Literal ')'
1372 * [8] Predicate ::= '[' PredicateExpr ']'
1373 * [9] PredicateExpr ::= Expr
1374 * [13] AbbreviatedAxisSpecifier ::= '@'?
1375 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1379 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1380 xmlChar *name = NULL;
1381 const xmlChar *URI = NULL;
1382 xmlChar *URL = NULL;
1386 if ((token == NULL) && (CUR == '@')) {
1387 xmlChar *prefix = NULL;
1392 PUSH(XSLT_OP_ATTR, NULL, NULL);
1393 goto parse_predicate;
1395 token = xsltScanQName(ctxt, &prefix);
1396 if (prefix != NULL) {
1399 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1401 xsltTransformError(NULL, NULL, NULL,
1402 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1405 URL = xmlStrdup(ns->href);
1409 if (token == NULL) {
1412 PUSH(XSLT_OP_ATTR, NULL, URL);
1415 xsltTransformError(NULL, NULL, NULL,
1416 "xsltCompileStepPattern : Name expected\n");
1420 PUSH(XSLT_OP_ATTR, token, URL);
1421 goto parse_predicate;
1424 token = xsltScanName(ctxt);
1425 if (token == NULL) {
1428 PUSH(XSLT_OP_ALL, token, NULL);
1429 goto parse_predicate;
1431 xsltTransformError(NULL, NULL, NULL,
1432 "xsltCompileStepPattern : Name expected\n");
1441 xsltCompileIdKeyPattern(ctxt, token, 0);
1444 } else if (CUR == ':') {
1447 xmlChar *prefix = token;
1451 * This is a namespace match
1453 token = xsltScanName(ctxt);
1454 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1456 xsltTransformError(NULL, NULL, NULL,
1457 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1462 URL = xmlStrdup(ns->href);
1465 if (token == NULL) {
1468 PUSH(XSLT_OP_NS, URL, NULL);
1470 xsltTransformError(NULL, NULL, NULL,
1471 "xsltCompileStepPattern : Name expected\n");
1476 PUSH(XSLT_OP_ELEM, token, URL);
1480 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1482 token = xsltScanName(ctxt);
1483 if (token == NULL) {
1486 PUSH(XSLT_OP_ALL, token, NULL);
1487 goto parse_predicate;
1489 xsltTransformError(NULL, NULL, NULL,
1490 "xsltCompileStepPattern : QName expected\n");
1495 URI = xsltGetQNameURI(ctxt->elem, &token);
1496 if (token == NULL) {
1500 name = xmlStrdup(token);
1502 URL = xmlStrdup(URI);
1504 PUSH(XSLT_OP_CHILD, name, URL);
1505 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1507 token = xsltScanName(ctxt);
1508 if (token == NULL) {
1509 xsltTransformError(NULL, NULL, NULL,
1510 "xsltCompileStepPattern : QName expected\n");
1514 URI = xsltGetQNameURI(ctxt->elem, &token);
1515 if (token == NULL) {
1519 name = xmlStrdup(token);
1521 URL = xmlStrdup(URI);
1523 PUSH(XSLT_OP_ATTR, name, URL);
1525 xsltTransformError(NULL, NULL, NULL,
1526 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1532 } else if (CUR == '*') {
1534 PUSH(XSLT_OP_ALL, token, NULL);
1536 URI = xsltGetQNameURI(ctxt->elem, &token);
1537 if (token == NULL) {
1542 URL = xmlStrdup(URI);
1543 PUSH(XSLT_OP_ELEM, token, URL);
1548 while (CUR == '[') {
1550 xmlChar *ret = NULL;
1556 /* Skip over nested predicates */
1559 else if (CUR == ']') {
1563 } else if (CUR == '"') {
1565 while ((CUR != 0) && (CUR != '"'))
1567 } else if (CUR == '\'') {
1569 while ((CUR != 0) && (CUR != '\''))
1575 xsltTransformError(NULL, NULL, NULL,
1576 "xsltCompileStepPattern : ']' expected\n");
1580 ret = xmlStrndup(q, CUR_PTR - q);
1581 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1582 /* push the predicate lower than local test */
1596 * xsltCompileRelativePathPattern:
1597 * @comp: the compilation context
1598 * @token: a posible precompiled name
1600 * Compile the XSLT RelativePathPattern and generates a precompiled
1601 * form suitable for fast matching.
1603 * [4] RelativePathPattern ::= StepPattern
1604 * | RelativePathPattern '/' StepPattern
1605 * | RelativePathPattern '//' StepPattern
1608 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1609 xsltCompileStepPattern(ctxt, token);
1613 while ((CUR != 0) && (CUR != '|')) {
1614 if ((CUR == '/') && (NXT(1) == '/')) {
1615 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1619 xsltCompileStepPattern(ctxt, NULL);
1620 } else if (CUR == '/') {
1621 PUSH(XSLT_OP_PARENT, NULL, NULL);
1624 if ((CUR != 0) && (CUR != '|')) {
1625 xsltCompileRelativePathPattern(ctxt, NULL);
1639 * xsltCompileLocationPathPattern:
1640 * @ctxt: the compilation context
1642 * Compile the XSLT LocationPathPattern and generates a precompiled
1643 * form suitable for fast matching.
1645 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1646 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1647 * | '//'? RelativePathPattern
1650 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1652 if ((CUR == '/') && (NXT(1) == '/')) {
1654 * since we reverse the query
1655 * a leading // can be safely ignored
1659 ctxt->comp->priority = 0.5; /* '//' means not 0 priority */
1660 xsltCompileRelativePathPattern(ctxt, NULL);
1661 } else if (CUR == '/') {
1663 * We need to find root as the parent
1667 PUSH(XSLT_OP_ROOT, NULL, NULL);
1668 if ((CUR != 0) && (CUR != '|')) {
1669 PUSH(XSLT_OP_PARENT, NULL, NULL);
1670 xsltCompileRelativePathPattern(ctxt, NULL);
1672 } else if (CUR == '*') {
1673 xsltCompileRelativePathPattern(ctxt, NULL);
1674 } else if (CUR == '@') {
1675 xsltCompileRelativePathPattern(ctxt, NULL);
1678 name = xsltScanName(ctxt);
1680 xsltTransformError(NULL, NULL, NULL,
1681 "xsltCompileLocationPathPattern : Name expected\n");
1686 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1687 xsltCompileIdKeyPattern(ctxt, name, 1);
1688 if ((CUR == '/') && (NXT(1) == '/')) {
1689 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1693 xsltCompileRelativePathPattern(ctxt, NULL);
1694 } else if (CUR == '/') {
1695 PUSH(XSLT_OP_PARENT, NULL, NULL);
1698 xsltCompileRelativePathPattern(ctxt, NULL);
1702 xsltCompileRelativePathPattern(ctxt, name);
1709 * xsltCompilePattern:
1710 * @pattern: an XSLT pattern
1711 * @doc: the containing document
1712 * @node: the containing element
1713 * @style: the stylesheet
1714 * @runtime: the transformation context, if done at run-time
1716 * Compile the XSLT pattern and generates a list of precompiled form suitable
1717 * for fast matching.
1719 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1721 * Returns the generated pattern list or NULL in case of failure
1725 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1726 xmlNodePtr node, xsltStylesheetPtr style,
1727 xsltTransformContextPtr runtime) {
1728 xsltParserContextPtr ctxt = NULL;
1729 xsltCompMatchPtr element, first = NULL, previous = NULL;
1730 int current, start, end, level, j;
1732 if (pattern == NULL) {
1733 xsltTransformError(NULL, NULL, node,
1734 "xsltCompilePattern : NULL pattern\n");
1738 ctxt = xsltNewParserContext(style, runtime);
1744 while (pattern[current] != 0) {
1746 while (IS_BLANK_CH(pattern[current]))
1750 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1751 if (pattern[end] == '[')
1753 else if (pattern[end] == ']')
1755 else if (pattern[end] == '\'') {
1757 while ((pattern[end] != 0) && (pattern[end] != '\''))
1759 } else if (pattern[end] == '"') {
1761 while ((pattern[end] != 0) && (pattern[end] != '"'))
1766 if (current == end) {
1767 xsltTransformError(NULL, NULL, node,
1768 "xsltCompilePattern : NULL pattern\n");
1771 element = xsltNewCompMatch();
1772 if (element == NULL) {
1777 else if (previous != NULL)
1778 previous->next = element;
1781 ctxt->comp = element;
1782 ctxt->base = xmlStrndup(&pattern[start], end - start);
1783 if (ctxt->base == NULL)
1785 ctxt->cur = &(ctxt->base)[current - start];
1786 element->pattern = ctxt->base;
1787 element->nsList = xmlGetNsList(doc, node);
1789 if (element->nsList != NULL) {
1790 while (element->nsList[j] != NULL)
1796 #ifdef WITH_XSLT_DEBUG_PATTERN
1797 xsltGenericDebug(xsltGenericDebugContext,
1798 "xsltCompilePattern : parsing '%s'\n",
1802 Preset default priority to be zero.
1803 This may be changed by xsltCompileLocationPathPattern.
1805 element->priority = 0;
1806 xsltCompileLocationPathPattern(ctxt);
1808 xsltTransformError(NULL, style, node,
1809 "xsltCompilePattern : failed to compile '%s'\n",
1811 if (style != NULL) style->errors++;
1816 * Reverse for faster interpretation.
1818 xsltReverseCompMatch(element);
1821 * Set-up the priority
1823 if (element->priority == 0) { /* if not yet determined */
1824 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1825 (element->steps[0].op == XSLT_OP_ATTR) ||
1826 (element->steps[0].op == XSLT_OP_PI)) &&
1827 (element->steps[0].value != NULL) &&
1828 (element->steps[1].op == XSLT_OP_END)) {
1829 ; /* previously preset */
1830 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1831 (element->steps[0].value2 != NULL) &&
1832 (element->steps[1].op == XSLT_OP_END)) {
1833 element->priority = -0.25;
1834 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1835 (element->steps[0].value != NULL) &&
1836 (element->steps[1].op == XSLT_OP_END)) {
1837 element->priority = -0.25;
1838 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1839 (element->steps[0].value == NULL) &&
1840 (element->steps[0].value2 == NULL) &&
1841 (element->steps[1].op == XSLT_OP_END)) {
1842 element->priority = -0.5;
1843 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1844 (element->steps[0].op == XSLT_OP_TEXT) ||
1845 (element->steps[0].op == XSLT_OP_ALL) ||
1846 (element->steps[0].op == XSLT_OP_NODE) ||
1847 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1848 (element->steps[1].op == XSLT_OP_END)) {
1849 element->priority = -0.5;
1851 element->priority = 0.5;
1854 #ifdef WITH_XSLT_DEBUG_PATTERN
1855 xsltGenericDebug(xsltGenericDebugContext,
1856 "xsltCompilePattern : parsed %s, default priority %f\n",
1857 element->pattern, element->priority);
1859 if (pattern[end] == '|')
1864 xsltTransformError(NULL, style, node,
1865 "xsltCompilePattern : NULL pattern\n");
1866 if (style != NULL) style->errors++;
1870 xsltFreeParserContext(ctxt);
1875 xsltFreeParserContext(ctxt);
1877 xsltFreeCompMatchList(first);
1881 /************************************************************************
1883 * Module interfaces *
1885 ************************************************************************/
1889 * @style: an XSLT stylesheet
1890 * @cur: an XSLT template
1891 * @mode: the mode name or NULL
1892 * @modeURI: the mode URI or NULL
1894 * Register the XSLT pattern associated to @cur
1896 * Returns -1 in case of error, 0 otherwise
1899 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1900 const xmlChar *mode, const xmlChar *modeURI) {
1901 xsltCompMatchPtr pat, list, *top = NULL, next;
1902 const xmlChar *name = NULL;
1903 float priority; /* the priority */
1905 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1908 priority = cur->priority;
1909 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1915 pat->template = cur;
1917 pat->mode = xmlDictLookup(style->dict, mode, -1);
1918 if (modeURI != NULL)
1919 pat->modeURI = xmlDictLookup(style->dict, modeURI, -1);
1920 if (priority != XSLT_PAT_NO_PRIORITY)
1921 pat->priority = priority;
1924 * insert it in the hash table list corresponding to its lookup name
1926 switch (pat->steps[0].op) {
1928 if (pat->steps[0].value != NULL)
1929 name = pat->steps[0].value;
1931 top = (xsltCompMatchPtr *) &(style->attrMatch);
1934 case XSLT_OP_PARENT:
1935 case XSLT_OP_ANCESTOR:
1936 top = (xsltCompMatchPtr *) &(style->elemMatch);
1939 top = (xsltCompMatchPtr *) &(style->rootMatch);
1942 top = (xsltCompMatchPtr *) &(style->keyMatch);
1945 /* TODO optimize ID !!! */
1948 top = (xsltCompMatchPtr *) &(style->elemMatch);
1951 case XSLT_OP_PREDICATE:
1952 xsltTransformError(NULL, style, NULL,
1953 "xsltAddTemplate: invalid compiled pattern\n");
1954 xsltFreeCompMatch(pat);
1957 * TODO: some flags at the top level about type based patterns
1958 * would be faster than inclusion in the hash table.
1961 if (pat->steps[0].value != NULL)
1962 name = pat->steps[0].value;
1964 top = (xsltCompMatchPtr *) &(style->piMatch);
1966 case XSLT_OP_COMMENT:
1967 top = (xsltCompMatchPtr *) &(style->commentMatch);
1970 top = (xsltCompMatchPtr *) &(style->textMatch);
1974 if (pat->steps[0].value != NULL)
1975 name = pat->steps[0].value;
1977 top = (xsltCompMatchPtr *) &(style->elemMatch);
1981 if (style->templatesHash == NULL) {
1982 style->templatesHash = xmlHashCreate(1024);
1983 if (style->templatesHash == NULL) {
1984 xsltFreeCompMatch(pat);
1987 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1989 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1990 name, mode, modeURI);
1992 xmlHashAddEntry3(style->templatesHash, name,
1993 mode, modeURI, pat);
1996 * Note '<=' since one must choose among the matching
1997 * template rules that are left, the one that occurs
1998 * last in the stylesheet
2000 if (list->priority <= pat->priority) {
2002 xmlHashUpdateEntry3(style->templatesHash, name,
2003 mode, modeURI, pat, NULL);
2005 while (list->next != NULL) {
2006 if (list->next->priority <= pat->priority)
2010 pat->next = list->next;
2015 } else if (top != NULL) {
2020 } else if (list->priority <= pat->priority) {
2024 while (list->next != NULL) {
2025 if (list->next->priority <= pat->priority)
2029 pat->next = list->next;
2033 xsltTransformError(NULL, style, NULL,
2034 "xsltAddTemplate: invalid compiled pattern\n");
2035 xsltFreeCompMatch(pat);
2038 #ifdef WITH_XSLT_DEBUG_PATTERN
2040 xsltGenericDebug(xsltGenericDebugContext,
2041 "added pattern : '%s' mode '%s' priority %f\n",
2042 pat->pattern, pat->mode, pat->priority);
2044 xsltGenericDebug(xsltGenericDebugContext,
2045 "added pattern : '%s' priority %f\n",
2046 pat->pattern, pat->priority);
2056 * @ctxt: a XSLT process context
2057 * @node: the node being processed
2058 * @style: the current style
2060 * Finds the template applying to this node, if @style is non-NULL
2061 * it means one needs to look for the next imported template in scope.
2063 * Returns the xsltTemplatePtr or NULL if not found
2066 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2067 xsltStylesheetPtr style) {
2068 xsltStylesheetPtr curstyle;
2069 xsltTemplatePtr ret = NULL;
2070 const xmlChar *name = NULL;
2071 xsltCompMatchPtr list = NULL;
2075 if ((ctxt == NULL) || (node == NULL))
2078 if (style == NULL) {
2079 curstyle = ctxt->style;
2081 curstyle = xsltNextImport(style);
2084 while ((curstyle != NULL) && (curstyle != style)) {
2085 priority = XSLT_PAT_NO_PRIORITY;
2086 /* TODO : handle IDs/keys here ! */
2087 if (curstyle->templatesHash != NULL) {
2089 * Use the top name as selector
2091 switch (node->type) {
2092 case XML_ELEMENT_NODE:
2093 if (node->name[0] == ' ')
2095 case XML_ATTRIBUTE_NODE:
2099 case XML_DOCUMENT_NODE:
2100 case XML_HTML_DOCUMENT_NODE:
2102 case XML_CDATA_SECTION_NODE:
2103 case XML_COMMENT_NODE:
2104 case XML_ENTITY_REF_NODE:
2105 case XML_ENTITY_NODE:
2106 case XML_DOCUMENT_TYPE_NODE:
2107 case XML_DOCUMENT_FRAG_NODE:
2108 case XML_NOTATION_NODE:
2110 case XML_ELEMENT_DECL:
2111 case XML_ATTRIBUTE_DECL:
2112 case XML_ENTITY_DECL:
2113 case XML_NAMESPACE_DECL:
2114 case XML_XINCLUDE_START:
2115 case XML_XINCLUDE_END:
2124 * find the list of applicable expressions based on the name
2126 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2127 name, ctxt->mode, ctxt->modeURI);
2130 while (list != NULL) {
2131 if (xsltTestCompMatch(ctxt, list, node,
2132 ctxt->mode, ctxt->modeURI)) {
2133 ret = list->template;
2134 priority = list->priority;
2142 * find alternate generic matches
2144 switch (node->type) {
2145 case XML_ELEMENT_NODE:
2146 if (node->name[0] == ' ')
2147 list = curstyle->rootMatch;
2149 list = curstyle->elemMatch;
2150 if (node->psvi != NULL) keyed = 1;
2152 case XML_ATTRIBUTE_NODE: {
2155 list = curstyle->attrMatch;
2156 attr = (xmlAttrPtr) node;
2157 if (attr->psvi != NULL) keyed = 1;
2161 list = curstyle->piMatch;
2162 if (node->psvi != NULL) keyed = 1;
2164 case XML_DOCUMENT_NODE:
2165 case XML_HTML_DOCUMENT_NODE: {
2168 list = curstyle->rootMatch;
2169 doc = (xmlDocPtr) node;
2170 if (doc->psvi != NULL) keyed = 1;
2174 case XML_CDATA_SECTION_NODE:
2175 list = curstyle->textMatch;
2176 if (node->psvi != NULL) keyed = 1;
2178 case XML_COMMENT_NODE:
2179 list = curstyle->commentMatch;
2180 if (node->psvi != NULL) keyed = 1;
2182 case XML_ENTITY_REF_NODE:
2183 case XML_ENTITY_NODE:
2184 case XML_DOCUMENT_TYPE_NODE:
2185 case XML_DOCUMENT_FRAG_NODE:
2186 case XML_NOTATION_NODE:
2188 case XML_ELEMENT_DECL:
2189 case XML_ATTRIBUTE_DECL:
2190 case XML_ENTITY_DECL:
2191 case XML_NAMESPACE_DECL:
2192 case XML_XINCLUDE_START:
2193 case XML_XINCLUDE_END:
2198 while ((list != NULL) &&
2199 ((ret == NULL) || (list->priority > priority))) {
2200 if (xsltTestCompMatch(ctxt, list, node,
2201 ctxt->mode, ctxt->modeURI)) {
2202 ret = list->template;
2203 priority = list->priority;
2209 * Some of the tests for elements can also apply to documents
2211 if ((node->type == XML_DOCUMENT_NODE) ||
2212 (node->type == XML_HTML_DOCUMENT_NODE) ||
2213 (node->type == XML_TEXT_NODE)) {
2214 list = curstyle->elemMatch;
2215 while ((list != NULL) &&
2216 ((ret == NULL) || (list->priority > priority))) {
2217 if (xsltTestCompMatch(ctxt, list, node,
2218 ctxt->mode, ctxt->modeURI)) {
2219 ret = list->template;
2220 priority = list->priority;
2225 } else if ((node->type == XML_PI_NODE) ||
2226 (node->type == XML_COMMENT_NODE)) {
2227 list = curstyle->elemMatch;
2228 while ((list != NULL) &&
2229 ((ret == NULL) || (list->priority > priority))) {
2230 if (xsltTestCompMatch(ctxt, list, node,
2231 ctxt->mode, ctxt->modeURI)) {
2232 ret = list->template;
2233 priority = list->priority;
2241 list = curstyle->keyMatch;
2242 while ((list != NULL) &&
2243 ((ret == NULL) || (list->priority > priority))) {
2244 if (xsltTestCompMatch(ctxt, list, node,
2245 ctxt->mode, ctxt->modeURI)) {
2246 ret = list->template;
2247 priority = list->priority;
2257 * Cycle on next curstylesheet import.
2259 curstyle = xsltNextImport(curstyle);
2265 * xsltCleanupTemplates:
2266 * @style: an XSLT stylesheet
2268 * Cleanup the state of the templates used by the stylesheet and
2269 * the ones it imports.
2272 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2276 * xsltFreeTemplateHashes:
2277 * @style: an XSLT stylesheet
2279 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2282 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2283 if (style->templatesHash != NULL)
2284 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2285 (xmlHashDeallocator) xsltFreeCompMatchList);
2286 if (style->rootMatch != NULL)
2287 xsltFreeCompMatchList(style->rootMatch);
2288 if (style->keyMatch != NULL)
2289 xsltFreeCompMatchList(style->keyMatch);
2290 if (style->elemMatch != NULL)
2291 xsltFreeCompMatchList(style->elemMatch);
2292 if (style->attrMatch != NULL)
2293 xsltFreeCompMatchList(style->attrMatch);
2294 if (style->parentMatch != NULL)
2295 xsltFreeCompMatchList(style->parentMatch);
2296 if (style->textMatch != NULL)
2297 xsltFreeCompMatchList(style->textMatch);
2298 if (style->piMatch != NULL)
2299 xsltFreeCompMatchList(style->piMatch);
2300 if (style->commentMatch != NULL)
2301 xsltFreeCompMatchList(style->commentMatch);