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) {
526 step = &comp->steps[i];
527 if (step->op == XSLT_OP_ROOT)
529 if ((step->op != XSLT_OP_ELEM) && (step->op != XSLT_OP_ALL))
531 if ((step->value == NULL) && (step->op != XSLT_OP_ALL))
536 if ((node->type == XML_DOCUMENT_NODE) ||
537 (node->type == XML_HTML_DOCUMENT_NODE) ||
538 #ifdef LIBXML_DOCB_ENABLED
539 (node->type == XML_DOCB_DOCUMENT_NODE) ||
541 (node->type == XML_NAMESPACE_DECL))
544 if (step->value == NULL)
546 while (node != NULL) {
549 if ((node->type == XML_ELEMENT_NODE) &&
550 (step->value[0] == node->name[0]) &&
551 (xmlStrEqual(step->value, node->name))) {
553 if (node->ns == NULL) {
554 if (step->value2 == NULL)
556 } else if (node->ns->href != NULL) {
557 if ((step->value2 != NULL) &&
558 (xmlStrEqual(step->value2, node->ns->href)))
568 /* TODO Handle IDs decently, must be done differently */
571 if (node->type != XML_ELEMENT_NODE)
574 id = xmlGetID(node->doc, step->value);
575 if ((id == NULL) || (id->parent != node))
583 list = xsltGetKey(ctxt, step->value,
584 step->value3, step->value2);
587 for (indx = 0;indx < list->nodeNr;indx++)
588 if (list->nodeTab[indx] == node)
590 if (indx >= list->nodeNr)
595 if (node->type != XML_ELEMENT_NODE)
597 if (node->ns == NULL) {
598 if (step->value != NULL)
600 } else if (node->ns->href != NULL) {
601 if (step->value == NULL)
603 if (!xmlStrEqual(step->value, node->ns->href))
608 if (node->type != XML_ELEMENT_NODE)
611 case XSLT_OP_PREDICATE: {
615 int pos = 0, len = 0;
620 (doc->name != NULL) &&
621 (doc->name[0] == ' ') &&
622 (xmlStrEqual(BAD_CAST doc->name,
623 BAD_CAST " fake node libxslt")))
628 * The simple existing predicate code cannot handle
629 * properly cascaded predicates. If in this situation
630 * compute directly the full node list once and check
631 * if the node is in the result list.
633 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
635 xmlXPathObjectPtr list;
639 prevdoc = (xmlDocPtr)
640 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
641 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
642 list = (xmlXPathObjectPtr)
643 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra);
645 if ((list == NULL) || (prevdoc != doc)) {
647 xmlXPathObjectPtr newlist;
648 xmlNodePtr parent = node->parent;
652 if (comp->pattern[0] == '/')
653 query = xmlStrdup(comp->pattern);
655 query = xmlStrdup((const xmlChar *)"//");
656 query = xmlStrcat(query, comp->pattern);
658 oldnode = ctxt->xpathCtxt->node;
659 olddoc = ctxt->xpathCtxt->doc;
660 ctxt->xpathCtxt->node = node;
661 ctxt->xpathCtxt->doc = doc;
662 newlist = xmlXPathEval(query, ctxt->xpathCtxt);
663 ctxt->xpathCtxt->node = oldnode;
664 ctxt->xpathCtxt->doc = olddoc;
668 if (newlist->type != XPATH_NODESET) {
669 xmlXPathFreeObject(newlist);
674 if ((parent == NULL) || (node->doc == NULL) || isRVT)
679 xmlXPathFreeObject(list);
682 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra) =
684 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
686 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
688 XSLT_RUNTIME_EXTRA_FREE(ctxt, sel->lenExtra) =
689 (xmlFreeFunc) xmlXPathFreeObject;
693 if ((list->nodesetval == NULL) ||
694 (list->nodesetval->nodeNr <= 0)) {
696 xmlXPathFreeObject(list);
699 /* TODO: store the index and use it for the scan */
701 for (j = 0;j < list->nodesetval->nodeNr;j++) {
702 if (list->nodesetval->nodeTab[j] == node) {
704 xmlXPathFreeObject(list);
711 xmlXPathFreeObject(list);
715 * Depending on the last selection, one may need to
716 * recompute contextSize and proximityPosition.
718 * TODO: make this thread safe !
720 oldCS = ctxt->xpathCtxt->contextSize;
721 oldCP = ctxt->xpathCtxt->proximityPosition;
723 (sel->op == XSLT_OP_ELEM) &&
724 (sel->value != NULL) &&
725 (node->type == XML_ELEMENT_NODE) &&
726 (node->parent != NULL)) {
730 previous = (xmlNodePtr)
731 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
732 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
733 if ((previous != NULL) &&
734 (previous->parent == node->parent)) {
736 * just walk back to adjust the index
739 xmlNodePtr sibling = node;
741 while (sibling != NULL) {
742 if (sibling == previous)
744 if ((previous->type == XML_ELEMENT_NODE) &&
745 (previous->name != NULL) &&
746 (sibling->name != NULL) &&
747 (previous->name[0] == sibling->name[0]) &&
748 (xmlStrEqual(previous->name, sibling->name))) {
749 if ((sel->value2 == NULL) ||
750 ((sibling->ns != NULL) &&
751 (xmlStrEqual(sel->value2,
752 sibling->ns->href))))
755 sibling = sibling->prev;
757 if (sibling == NULL) {
758 /* hum going backward in document order ... */
761 while (sibling != NULL) {
762 if (sibling == previous)
764 if ((sel->value2 == NULL) ||
765 ((sibling->ns != NULL) &&
766 (xmlStrEqual(sel->value2,
767 sibling->ns->href))))
769 sibling = sibling->next;
772 if (sibling != NULL) {
775 * If the node is in a Value Tree we need to
776 * save len, but cannot cache the node!
777 * (bugs 153137 and 158840)
779 if (node->doc != NULL) {
780 len = XSLT_RUNTIME_EXTRA(ctxt,
781 sel->lenExtra, ival);
783 XSLT_RUNTIME_EXTRA(ctxt,
784 sel->previousExtra, ptr) = node;
785 XSLT_RUNTIME_EXTRA(ctxt,
786 sel->indexExtra, ival) = pos;
794 * recompute the index
796 xmlNodePtr siblings = node->parent->children;
797 xmlNodePtr parent = node->parent;
799 while (siblings != NULL) {
800 if (siblings->type == XML_ELEMENT_NODE) {
801 if (siblings == node) {
804 } else if ((node->name != NULL) &&
805 (siblings->name != NULL) &&
806 (node->name[0] == siblings->name[0]) &&
807 (xmlStrEqual(node->name, siblings->name))) {
808 if ((sel->value2 == NULL) ||
809 ((siblings->ns != NULL) &&
810 (xmlStrEqual(sel->value2,
811 siblings->ns->href))))
815 siblings = siblings->next;
817 if ((parent == NULL) || (node->doc == NULL))
820 while (parent->parent != NULL)
821 parent = parent->parent;
822 if (((parent->type != XML_DOCUMENT_NODE) &&
823 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
824 (parent != (xmlNodePtr) node->doc))
829 ctxt->xpathCtxt->contextSize = len;
830 ctxt->xpathCtxt->proximityPosition = pos;
832 * If the node is in a Value Tree we cannot
835 if ((!isRVT) && (node->doc != NULL) &&
837 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
839 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
841 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) =
845 } else if ((sel != NULL) && (sel->op == XSLT_OP_ALL) &&
846 (node->type == XML_ELEMENT_NODE)) {
850 previous = (xmlNodePtr)
851 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
852 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
853 if ((previous != NULL) &&
854 (previous->parent == node->parent)) {
856 * just walk back to adjust the index
859 xmlNodePtr sibling = node;
861 while (sibling != NULL) {
862 if (sibling == previous)
864 if (sibling->type == XML_ELEMENT_NODE)
866 sibling = sibling->prev;
868 if (sibling == NULL) {
869 /* hum going backward in document order ... */
872 while (sibling != NULL) {
873 if (sibling == previous)
875 if (sibling->type == XML_ELEMENT_NODE)
877 sibling = sibling->next;
880 if (sibling != NULL) {
883 * If the node is in a Value Tree we cannot
886 if ((node->doc != NULL) && !isRVT) {
887 len = XSLT_RUNTIME_EXTRA(ctxt,
888 sel->lenExtra, ival);
889 XSLT_RUNTIME_EXTRA(ctxt,
890 sel->previousExtra, ptr) = node;
891 XSLT_RUNTIME_EXTRA(ctxt,
892 sel->indexExtra, ival) = pos;
898 * recompute the index
900 xmlNodePtr siblings = node->parent->children;
901 xmlNodePtr parent = node->parent;
903 while (siblings != NULL) {
904 if (siblings->type == XML_ELEMENT_NODE) {
906 if (siblings == node) {
910 siblings = siblings->next;
912 if ((parent == NULL) || (node->doc == NULL))
915 while (parent->parent != NULL)
916 parent = parent->parent;
917 if (((parent->type != XML_DOCUMENT_NODE) &&
918 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
919 (parent != (xmlNodePtr) node->doc))
924 ctxt->xpathCtxt->contextSize = len;
925 ctxt->xpathCtxt->proximityPosition = pos;
927 * If the node is in a Value Tree we cannot
930 if ((node->doc != NULL) && (nocache == 0) && !isRVT) {
931 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
933 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
935 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) =
940 oldNode = ctxt->node;
943 if (step->value == NULL)
945 if (step->comp == NULL)
948 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
953 ctxt->xpathCtxt->contextSize = oldCS;
954 ctxt->xpathCtxt->proximityPosition = oldCP;
956 ctxt->node = oldNode;
960 ctxt->xpathCtxt->contextSize = oldCS;
961 ctxt->xpathCtxt->proximityPosition = oldCP;
963 ctxt->node = oldNode;
967 if (node->type != XML_PI_NODE)
969 if (step->value != NULL) {
970 if (!xmlStrEqual(step->value, node->name))
974 case XSLT_OP_COMMENT:
975 if (node->type != XML_COMMENT_NODE)
979 if ((node->type != XML_TEXT_NODE) &&
980 (node->type != XML_CDATA_SECTION_NODE))
984 switch (node->type) {
985 case XML_ELEMENT_NODE:
986 case XML_CDATA_SECTION_NODE:
988 case XML_COMMENT_NODE:
1001 * xsltTestCompMatchList:
1002 * @ctxt: a XSLT process context
1004 * @comp: the precompiled pattern list
1006 * Test whether the node matches one of the patterns in the list
1008 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
1011 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
1012 xsltCompMatchPtr comp) {
1015 if ((ctxt == NULL) || (node == NULL))
1017 while (comp != NULL) {
1018 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
1026 /************************************************************************
1028 * Dedicated parser for templates *
1030 ************************************************************************/
1032 #define CUR (*ctxt->cur)
1033 #define SKIP(val) ctxt->cur += (val)
1034 #define NXT(val) ctxt->cur[(val)]
1035 #define CUR_PTR ctxt->cur
1037 #define SKIP_BLANKS \
1038 while (IS_BLANK_CH(CUR)) NEXT
1040 #define CURRENT (*ctxt->cur)
1041 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1044 #define PUSH(op, val, val2) \
1045 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1048 xsltSwapTopCompMatch(ctxt->comp);
1050 #define XSLT_ERROR(X) \
1051 { xsltError(ctxt, __FILE__, __LINE__, X); \
1052 ctxt->error = (X); return; }
1054 #define XSLT_ERROR0(X) \
1055 { xsltError(ctxt, __FILE__, __LINE__, X); \
1056 ctxt->error = (X); return(0); }
1060 * @ctxt: the XPath Parser context
1062 * Parse an XPath Litteral:
1064 * [29] Literal ::= '"' [^"]* '"'
1067 * Returns the Literal parsed or NULL
1071 xsltScanLiteral(xsltParserContextPtr ctxt) {
1072 const xmlChar *q, *cur;
1073 xmlChar *ret = NULL;
1080 val = xmlStringCurrentChar(NULL, cur, &len);
1081 while ((IS_CHAR(val)) && (val != '"')) {
1083 val = xmlStringCurrentChar(NULL, cur, &len);
1085 if (!IS_CHAR(val)) {
1089 ret = xmlStrndup(q, cur - q);
1093 } else if (CUR == '\'') {
1096 val = xmlStringCurrentChar(NULL, cur, &len);
1097 while ((IS_CHAR(val)) && (val != '\'')) {
1099 val = xmlStringCurrentChar(NULL, cur, &len);
1101 if (!IS_CHAR(val)) {
1105 ret = xmlStrndup(q, cur - q);
1110 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1119 * @ctxt: the XPath Parser context
1121 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1122 * CombiningChar | Extender
1124 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1126 * [6] Names ::= Name (S Name)*
1128 * Returns the Name parsed or NULL
1132 xsltScanName(xsltParserContextPtr ctxt) {
1133 const xmlChar *q, *cur;
1134 xmlChar *ret = NULL;
1140 val = xmlStringCurrentChar(NULL, cur, &len);
1141 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1144 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1145 (val == '.') || (val == '-') ||
1147 (IS_COMBINING(val)) ||
1148 (IS_EXTENDER(val))) {
1150 val = xmlStringCurrentChar(NULL, cur, &len);
1152 ret = xmlStrndup(q, cur - q);
1159 * @ctxt: the XPath Parser context
1161 * Parses a non qualified name
1163 * Returns the Name parsed or NULL
1167 xsltScanNCName(xsltParserContextPtr ctxt) {
1168 const xmlChar *q, *cur;
1169 xmlChar *ret = NULL;
1175 val = xmlStringCurrentChar(NULL, cur, &len);
1176 if (!IS_LETTER(val) && (val != '_'))
1179 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1180 (val == '.') || (val == '-') ||
1182 (IS_COMBINING(val)) ||
1183 (IS_EXTENDER(val))) {
1185 val = xmlStringCurrentChar(NULL, cur, &len);
1187 ret = xmlStrndup(q, cur - q);
1194 * @ctxt: the XPath Parser context
1195 * @prefix: the place to store the prefix
1197 * Parse a qualified name
1199 * Returns the Name parsed or NULL
1203 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1204 xmlChar *ret = NULL;
1207 ret = xsltScanNCName(ctxt);
1211 ret = xsltScanNCName(ctxt);
1217 * xsltCompileIdKeyPattern:
1218 * @ctxt: the compilation context
1219 * @name: a preparsed name
1220 * @aid: whether id/key are allowed there
1222 * Compile the XSLT LocationIdKeyPattern
1223 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1224 * | 'key' '(' Literal ',' Literal ')'
1226 * also handle NodeType and PI from:
1228 * [7] NodeTest ::= NameTest
1229 * | NodeType '(' ')'
1230 * | 'processing-instruction' '(' Literal ')'
1233 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1234 xmlChar *lit = NULL;
1235 xmlChar *lit2 = NULL;
1238 xsltTransformError(NULL, NULL, NULL,
1239 "xsltCompileIdKeyPattern : ( expected\n");
1243 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1246 lit = xsltScanLiteral(ctxt);
1251 xsltTransformError(NULL, NULL, NULL,
1252 "xsltCompileIdKeyPattern : ) expected\n");
1257 PUSH(XSLT_OP_ID, lit, NULL);
1258 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1261 lit = xsltScanLiteral(ctxt);
1266 xsltTransformError(NULL, NULL, NULL,
1267 "xsltCompileIdKeyPattern : , expected\n");
1273 lit2 = xsltScanLiteral(ctxt);
1278 xsltTransformError(NULL, NULL, NULL,
1279 "xsltCompileIdKeyPattern : ) expected\n");
1284 /* TODO: support namespace in keys */
1285 PUSH(XSLT_OP_KEY, lit, lit2);
1286 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1290 lit = xsltScanLiteral(ctxt);
1295 xsltTransformError(NULL, NULL, NULL,
1296 "xsltCompileIdKeyPattern : ) expected\n");
1302 PUSH(XSLT_OP_PI, lit, NULL);
1303 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1307 xsltTransformError(NULL, NULL, NULL,
1308 "xsltCompileIdKeyPattern : ) expected\n");
1313 PUSH(XSLT_OP_TEXT, NULL, NULL);
1314 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1318 xsltTransformError(NULL, NULL, NULL,
1319 "xsltCompileIdKeyPattern : ) expected\n");
1324 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1325 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1329 xsltTransformError(NULL, NULL, NULL,
1330 "xsltCompileIdKeyPattern : ) expected\n");
1335 PUSH(XSLT_OP_NODE, NULL, NULL);
1337 xsltTransformError(NULL, NULL, NULL,
1338 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1342 xsltTransformError(NULL, NULL, NULL,
1343 "xsltCompileIdKeyPattern : node type\n");
1353 * xsltCompileStepPattern:
1354 * @ctxt: the compilation context
1355 * @token: a posible precompiled name
1357 * Compile the XSLT StepPattern and generates a precompiled
1358 * form suitable for fast matching.
1360 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1361 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1362 * | ('child' | 'attribute') '::'
1364 * [7] NodeTest ::= NameTest
1365 * | NodeType '(' ')'
1366 * | 'processing-instruction' '(' Literal ')'
1367 * [8] Predicate ::= '[' PredicateExpr ']'
1368 * [9] PredicateExpr ::= Expr
1369 * [13] AbbreviatedAxisSpecifier ::= '@'?
1370 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1374 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1375 xmlChar *name = NULL;
1376 const xmlChar *URI = NULL;
1377 xmlChar *URL = NULL;
1381 if ((token == NULL) && (CUR == '@')) {
1382 xmlChar *prefix = NULL;
1387 PUSH(XSLT_OP_ATTR, NULL, NULL);
1388 goto parse_predicate;
1390 token = xsltScanQName(ctxt, &prefix);
1391 if (prefix != NULL) {
1394 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1396 xsltTransformError(NULL, NULL, NULL,
1397 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1400 URL = xmlStrdup(ns->href);
1404 if (token == NULL) {
1407 PUSH(XSLT_OP_ATTR, NULL, URL);
1410 xsltTransformError(NULL, NULL, NULL,
1411 "xsltCompileStepPattern : Name expected\n");
1415 PUSH(XSLT_OP_ATTR, token, URL);
1416 goto parse_predicate;
1419 token = xsltScanName(ctxt);
1420 if (token == NULL) {
1423 PUSH(XSLT_OP_ALL, token, NULL);
1424 goto parse_predicate;
1426 xsltTransformError(NULL, NULL, NULL,
1427 "xsltCompileStepPattern : Name expected\n");
1436 xsltCompileIdKeyPattern(ctxt, token, 0);
1439 } else if (CUR == ':') {
1442 xmlChar *prefix = token;
1446 * This is a namespace match
1448 token = xsltScanName(ctxt);
1449 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1451 xsltTransformError(NULL, NULL, NULL,
1452 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1457 URL = xmlStrdup(ns->href);
1460 if (token == NULL) {
1463 PUSH(XSLT_OP_NS, URL, NULL);
1465 xsltTransformError(NULL, NULL, NULL,
1466 "xsltCompileStepPattern : Name expected\n");
1471 PUSH(XSLT_OP_ELEM, token, URL);
1475 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1477 token = xsltScanName(ctxt);
1478 if (token == NULL) {
1481 PUSH(XSLT_OP_ALL, token, NULL);
1482 goto parse_predicate;
1484 xsltTransformError(NULL, NULL, NULL,
1485 "xsltCompileStepPattern : QName expected\n");
1490 URI = xsltGetQNameURI(ctxt->elem, &token);
1491 if (token == NULL) {
1495 name = xmlStrdup(token);
1497 URL = xmlStrdup(URI);
1499 PUSH(XSLT_OP_CHILD, name, URL);
1500 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1502 token = xsltScanName(ctxt);
1503 if (token == NULL) {
1504 xsltTransformError(NULL, NULL, NULL,
1505 "xsltCompileStepPattern : QName expected\n");
1509 URI = xsltGetQNameURI(ctxt->elem, &token);
1510 if (token == NULL) {
1514 name = xmlStrdup(token);
1516 URL = xmlStrdup(URI);
1518 PUSH(XSLT_OP_ATTR, name, URL);
1520 xsltTransformError(NULL, NULL, NULL,
1521 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1527 } else if (CUR == '*') {
1529 PUSH(XSLT_OP_ALL, token, NULL);
1531 URI = xsltGetQNameURI(ctxt->elem, &token);
1532 if (token == NULL) {
1537 URL = xmlStrdup(URI);
1538 PUSH(XSLT_OP_ELEM, token, URL);
1543 while (CUR == '[') {
1545 xmlChar *ret = NULL;
1551 /* Skip over nested predicates */
1554 else if (CUR == ']') {
1558 } else if (CUR == '"') {
1560 while ((CUR != 0) && (CUR != '"'))
1562 } else if (CUR == '\'') {
1564 while ((CUR != 0) && (CUR != '\''))
1570 xsltTransformError(NULL, NULL, NULL,
1571 "xsltCompileStepPattern : ']' expected\n");
1575 ret = xmlStrndup(q, CUR_PTR - q);
1576 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1577 /* push the predicate lower than local test */
1591 * xsltCompileRelativePathPattern:
1592 * @comp: the compilation context
1593 * @token: a posible precompiled name
1595 * Compile the XSLT RelativePathPattern and generates a precompiled
1596 * form suitable for fast matching.
1598 * [4] RelativePathPattern ::= StepPattern
1599 * | RelativePathPattern '/' StepPattern
1600 * | RelativePathPattern '//' StepPattern
1603 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1604 xsltCompileStepPattern(ctxt, token);
1608 while ((CUR != 0) && (CUR != '|')) {
1609 if ((CUR == '/') && (NXT(1) == '/')) {
1610 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1614 xsltCompileStepPattern(ctxt, NULL);
1615 } else if (CUR == '/') {
1616 PUSH(XSLT_OP_PARENT, NULL, NULL);
1619 if ((CUR != 0) && (CUR != '|')) {
1620 xsltCompileRelativePathPattern(ctxt, NULL);
1634 * xsltCompileLocationPathPattern:
1635 * @ctxt: the compilation context
1637 * Compile the XSLT LocationPathPattern and generates a precompiled
1638 * form suitable for fast matching.
1640 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1641 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1642 * | '//'? RelativePathPattern
1645 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1647 if ((CUR == '/') && (NXT(1) == '/')) {
1649 * since we reverse the query
1650 * a leading // can be safely ignored
1654 ctxt->comp->priority = 0.5; /* '//' means not 0 priority */
1655 xsltCompileRelativePathPattern(ctxt, NULL);
1656 } else if (CUR == '/') {
1658 * We need to find root as the parent
1662 PUSH(XSLT_OP_ROOT, NULL, NULL);
1663 if ((CUR != 0) && (CUR != '|')) {
1664 PUSH(XSLT_OP_PARENT, NULL, NULL);
1665 xsltCompileRelativePathPattern(ctxt, NULL);
1667 } else if (CUR == '*') {
1668 xsltCompileRelativePathPattern(ctxt, NULL);
1669 } else if (CUR == '@') {
1670 xsltCompileRelativePathPattern(ctxt, NULL);
1673 name = xsltScanName(ctxt);
1675 xsltTransformError(NULL, NULL, NULL,
1676 "xsltCompileLocationPathPattern : Name expected\n");
1681 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1682 xsltCompileIdKeyPattern(ctxt, name, 1);
1683 if ((CUR == '/') && (NXT(1) == '/')) {
1684 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1688 xsltCompileRelativePathPattern(ctxt, NULL);
1689 } else if (CUR == '/') {
1690 PUSH(XSLT_OP_PARENT, NULL, NULL);
1693 xsltCompileRelativePathPattern(ctxt, NULL);
1697 xsltCompileRelativePathPattern(ctxt, name);
1704 * xsltCompilePattern:
1705 * @pattern: an XSLT pattern
1706 * @doc: the containing document
1707 * @node: the containing element
1708 * @style: the stylesheet
1709 * @runtime: the transformation context, if done at run-time
1711 * Compile the XSLT pattern and generates a list of precompiled form suitable
1712 * for fast matching.
1714 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1716 * Returns the generated pattern list or NULL in case of failure
1720 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1721 xmlNodePtr node, xsltStylesheetPtr style,
1722 xsltTransformContextPtr runtime) {
1723 xsltParserContextPtr ctxt = NULL;
1724 xsltCompMatchPtr element, first = NULL, previous = NULL;
1725 int current, start, end, level, j;
1727 if (pattern == NULL) {
1728 xsltTransformError(NULL, NULL, node,
1729 "xsltCompilePattern : NULL pattern\n");
1733 ctxt = xsltNewParserContext(style, runtime);
1739 while (pattern[current] != 0) {
1741 while (IS_BLANK_CH(pattern[current]))
1745 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1746 if (pattern[end] == '[')
1748 else if (pattern[end] == ']')
1750 else if (pattern[end] == '\'') {
1752 while ((pattern[end] != 0) && (pattern[end] != '\''))
1754 } else if (pattern[end] == '"') {
1756 while ((pattern[end] != 0) && (pattern[end] != '"'))
1761 if (current == end) {
1762 xsltTransformError(NULL, NULL, node,
1763 "xsltCompilePattern : NULL pattern\n");
1766 element = xsltNewCompMatch();
1767 if (element == NULL) {
1772 else if (previous != NULL)
1773 previous->next = element;
1776 ctxt->comp = element;
1777 ctxt->base = xmlStrndup(&pattern[start], end - start);
1778 if (ctxt->base == NULL)
1780 ctxt->cur = &(ctxt->base)[current - start];
1781 element->pattern = ctxt->base;
1782 element->nsList = xmlGetNsList(doc, node);
1784 if (element->nsList != NULL) {
1785 while (element->nsList[j] != NULL)
1791 #ifdef WITH_XSLT_DEBUG_PATTERN
1792 xsltGenericDebug(xsltGenericDebugContext,
1793 "xsltCompilePattern : parsing '%s'\n",
1797 Preset default priority to be zero.
1798 This may be changed by xsltCompileLocationPathPattern.
1800 element->priority = 0;
1801 xsltCompileLocationPathPattern(ctxt);
1803 xsltTransformError(NULL, style, node,
1804 "xsltCompilePattern : failed to compile '%s'\n",
1806 if (style != NULL) style->errors++;
1811 * Reverse for faster interpretation.
1813 xsltReverseCompMatch(element);
1816 * Set-up the priority
1818 if (element->priority == 0) { /* if not yet determined */
1819 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1820 (element->steps[0].op == XSLT_OP_ATTR) ||
1821 (element->steps[0].op == XSLT_OP_PI)) &&
1822 (element->steps[0].value != NULL) &&
1823 (element->steps[1].op == XSLT_OP_END)) {
1824 ; /* previously preset */
1825 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1826 (element->steps[0].value2 != NULL) &&
1827 (element->steps[1].op == XSLT_OP_END)) {
1828 element->priority = -0.25;
1829 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1830 (element->steps[0].value != NULL) &&
1831 (element->steps[1].op == XSLT_OP_END)) {
1832 element->priority = -0.25;
1833 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1834 (element->steps[0].value == NULL) &&
1835 (element->steps[0].value2 == NULL) &&
1836 (element->steps[1].op == XSLT_OP_END)) {
1837 element->priority = -0.5;
1838 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1839 (element->steps[0].op == XSLT_OP_TEXT) ||
1840 (element->steps[0].op == XSLT_OP_ALL) ||
1841 (element->steps[0].op == XSLT_OP_NODE) ||
1842 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1843 (element->steps[1].op == XSLT_OP_END)) {
1844 element->priority = -0.5;
1846 element->priority = 0.5;
1849 #ifdef WITH_XSLT_DEBUG_PATTERN
1850 xsltGenericDebug(xsltGenericDebugContext,
1851 "xsltCompilePattern : parsed %s, default priority %f\n",
1852 element->pattern, element->priority);
1854 if (pattern[end] == '|')
1859 xsltTransformError(NULL, style, node,
1860 "xsltCompilePattern : NULL pattern\n");
1861 if (style != NULL) style->errors++;
1865 xsltFreeParserContext(ctxt);
1870 xsltFreeParserContext(ctxt);
1872 xsltFreeCompMatchList(first);
1876 /************************************************************************
1878 * Module interfaces *
1880 ************************************************************************/
1884 * @style: an XSLT stylesheet
1885 * @cur: an XSLT template
1886 * @mode: the mode name or NULL
1887 * @modeURI: the mode URI or NULL
1889 * Register the XSLT pattern associated to @cur
1891 * Returns -1 in case of error, 0 otherwise
1894 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1895 const xmlChar *mode, const xmlChar *modeURI) {
1896 xsltCompMatchPtr pat, list, *top = NULL, next;
1897 const xmlChar *name = NULL;
1898 float priority; /* the priority */
1900 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1903 priority = cur->priority;
1904 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1910 pat->template = cur;
1912 pat->mode = xmlDictLookup(style->dict, mode, -1);
1913 if (modeURI != NULL)
1914 pat->modeURI = xmlDictLookup(style->dict, modeURI, -1);
1915 if (priority != XSLT_PAT_NO_PRIORITY)
1916 pat->priority = priority;
1919 * insert it in the hash table list corresponding to its lookup name
1921 switch (pat->steps[0].op) {
1923 if (pat->steps[0].value != NULL)
1924 name = pat->steps[0].value;
1926 top = (xsltCompMatchPtr *) &(style->attrMatch);
1929 case XSLT_OP_PARENT:
1930 case XSLT_OP_ANCESTOR:
1931 top = (xsltCompMatchPtr *) &(style->elemMatch);
1934 top = (xsltCompMatchPtr *) &(style->rootMatch);
1937 top = (xsltCompMatchPtr *) &(style->keyMatch);
1940 /* TODO optimize ID !!! */
1943 top = (xsltCompMatchPtr *) &(style->elemMatch);
1946 case XSLT_OP_PREDICATE:
1947 xsltTransformError(NULL, style, NULL,
1948 "xsltAddTemplate: invalid compiled pattern\n");
1949 xsltFreeCompMatch(pat);
1952 * TODO: some flags at the top level about type based patterns
1953 * would be faster than inclusion in the hash table.
1956 if (pat->steps[0].value != NULL)
1957 name = pat->steps[0].value;
1959 top = (xsltCompMatchPtr *) &(style->piMatch);
1961 case XSLT_OP_COMMENT:
1962 top = (xsltCompMatchPtr *) &(style->commentMatch);
1965 top = (xsltCompMatchPtr *) &(style->textMatch);
1969 if (pat->steps[0].value != NULL)
1970 name = pat->steps[0].value;
1972 top = (xsltCompMatchPtr *) &(style->elemMatch);
1976 if (style->templatesHash == NULL) {
1977 style->templatesHash = xmlHashCreate(1024);
1978 if (style->templatesHash == NULL) {
1979 xsltFreeCompMatch(pat);
1982 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1984 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1985 name, mode, modeURI);
1987 xmlHashAddEntry3(style->templatesHash, name,
1988 mode, modeURI, pat);
1991 * Note '<=' since one must choose among the matching
1992 * template rules that are left, the one that occurs
1993 * last in the stylesheet
1995 if (list->priority <= pat->priority) {
1997 xmlHashUpdateEntry3(style->templatesHash, name,
1998 mode, modeURI, pat, NULL);
2000 while (list->next != NULL) {
2001 if (list->next->priority <= pat->priority)
2005 pat->next = list->next;
2010 } else if (top != NULL) {
2015 } else if (list->priority <= pat->priority) {
2019 while (list->next != NULL) {
2020 if (list->next->priority <= pat->priority)
2024 pat->next = list->next;
2028 xsltTransformError(NULL, style, NULL,
2029 "xsltAddTemplate: invalid compiled pattern\n");
2030 xsltFreeCompMatch(pat);
2033 #ifdef WITH_XSLT_DEBUG_PATTERN
2035 xsltGenericDebug(xsltGenericDebugContext,
2036 "added pattern : '%s' mode '%s' priority %f\n",
2037 pat->pattern, pat->mode, pat->priority);
2039 xsltGenericDebug(xsltGenericDebugContext,
2040 "added pattern : '%s' priority %f\n",
2041 pat->pattern, pat->priority);
2051 * @ctxt: a XSLT process context
2052 * @node: the node being processed
2053 * @style: the current style
2055 * Finds the template applying to this node, if @style is non-NULL
2056 * it means one needs to look for the next imported template in scope.
2058 * Returns the xsltTemplatePtr or NULL if not found
2061 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2062 xsltStylesheetPtr style) {
2063 xsltStylesheetPtr curstyle;
2064 xsltTemplatePtr ret = NULL;
2065 const xmlChar *name = NULL;
2066 xsltCompMatchPtr list = NULL;
2070 if ((ctxt == NULL) || (node == NULL))
2073 if (style == NULL) {
2074 curstyle = ctxt->style;
2076 curstyle = xsltNextImport(style);
2079 while ((curstyle != NULL) && (curstyle != style)) {
2080 priority = XSLT_PAT_NO_PRIORITY;
2081 /* TODO : handle IDs/keys here ! */
2082 if (curstyle->templatesHash != NULL) {
2084 * Use the top name as selector
2086 switch (node->type) {
2087 case XML_ELEMENT_NODE:
2088 if (node->name[0] == ' ')
2090 case XML_ATTRIBUTE_NODE:
2094 case XML_DOCUMENT_NODE:
2095 case XML_HTML_DOCUMENT_NODE:
2097 case XML_CDATA_SECTION_NODE:
2098 case XML_COMMENT_NODE:
2099 case XML_ENTITY_REF_NODE:
2100 case XML_ENTITY_NODE:
2101 case XML_DOCUMENT_TYPE_NODE:
2102 case XML_DOCUMENT_FRAG_NODE:
2103 case XML_NOTATION_NODE:
2105 case XML_ELEMENT_DECL:
2106 case XML_ATTRIBUTE_DECL:
2107 case XML_ENTITY_DECL:
2108 case XML_NAMESPACE_DECL:
2109 case XML_XINCLUDE_START:
2110 case XML_XINCLUDE_END:
2119 * find the list of applicable expressions based on the name
2121 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2122 name, ctxt->mode, ctxt->modeURI);
2125 while (list != NULL) {
2126 if (xsltTestCompMatch(ctxt, list, node,
2127 ctxt->mode, ctxt->modeURI)) {
2128 ret = list->template;
2129 priority = list->priority;
2137 * find alternate generic matches
2139 switch (node->type) {
2140 case XML_ELEMENT_NODE:
2141 if (node->name[0] == ' ')
2142 list = curstyle->rootMatch;
2144 list = curstyle->elemMatch;
2145 if (node->psvi != NULL) keyed = 1;
2147 case XML_ATTRIBUTE_NODE: {
2150 list = curstyle->attrMatch;
2151 attr = (xmlAttrPtr) node;
2152 if (attr->psvi != NULL) keyed = 1;
2156 list = curstyle->piMatch;
2157 if (node->psvi != NULL) keyed = 1;
2159 case XML_DOCUMENT_NODE:
2160 case XML_HTML_DOCUMENT_NODE: {
2163 list = curstyle->rootMatch;
2164 doc = (xmlDocPtr) node;
2165 if (doc->psvi != NULL) keyed = 1;
2169 case XML_CDATA_SECTION_NODE:
2170 list = curstyle->textMatch;
2171 if (node->psvi != NULL) keyed = 1;
2173 case XML_COMMENT_NODE:
2174 list = curstyle->commentMatch;
2175 if (node->psvi != NULL) keyed = 1;
2177 case XML_ENTITY_REF_NODE:
2178 case XML_ENTITY_NODE:
2179 case XML_DOCUMENT_TYPE_NODE:
2180 case XML_DOCUMENT_FRAG_NODE:
2181 case XML_NOTATION_NODE:
2183 case XML_ELEMENT_DECL:
2184 case XML_ATTRIBUTE_DECL:
2185 case XML_ENTITY_DECL:
2186 case XML_NAMESPACE_DECL:
2187 case XML_XINCLUDE_START:
2188 case XML_XINCLUDE_END:
2193 while ((list != NULL) &&
2194 ((ret == NULL) || (list->priority > priority))) {
2195 if (xsltTestCompMatch(ctxt, list, node,
2196 ctxt->mode, ctxt->modeURI)) {
2197 ret = list->template;
2198 priority = list->priority;
2204 * Some of the tests for elements can also apply to documents
2206 if ((node->type == XML_DOCUMENT_NODE) ||
2207 (node->type == XML_HTML_DOCUMENT_NODE) ||
2208 (node->type == XML_TEXT_NODE)) {
2209 list = curstyle->elemMatch;
2210 while ((list != NULL) &&
2211 ((ret == NULL) || (list->priority > priority))) {
2212 if (xsltTestCompMatch(ctxt, list, node,
2213 ctxt->mode, ctxt->modeURI)) {
2214 ret = list->template;
2215 priority = list->priority;
2220 } else if ((node->type == XML_PI_NODE) ||
2221 (node->type == XML_COMMENT_NODE)) {
2222 list = curstyle->elemMatch;
2223 while ((list != NULL) &&
2224 ((ret == NULL) || (list->priority > priority))) {
2225 if (xsltTestCompMatch(ctxt, list, node,
2226 ctxt->mode, ctxt->modeURI)) {
2227 ret = list->template;
2228 priority = list->priority;
2236 list = curstyle->keyMatch;
2237 while ((list != NULL) &&
2238 ((ret == NULL) || (list->priority > priority))) {
2239 if (xsltTestCompMatch(ctxt, list, node,
2240 ctxt->mode, ctxt->modeURI)) {
2241 ret = list->template;
2242 priority = list->priority;
2252 * Cycle on next curstylesheet import.
2254 curstyle = xsltNextImport(curstyle);
2260 * xsltCleanupTemplates:
2261 * @style: an XSLT stylesheet
2263 * Cleanup the state of the templates used by the stylesheet and
2264 * the ones it imports.
2267 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2271 * xsltFreeTemplateHashes:
2272 * @style: an XSLT stylesheet
2274 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2277 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2278 if (style->templatesHash != NULL)
2279 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2280 (xmlHashDeallocator) xsltFreeCompMatchList);
2281 if (style->rootMatch != NULL)
2282 xsltFreeCompMatchList(style->rootMatch);
2283 if (style->keyMatch != NULL)
2284 xsltFreeCompMatchList(style->keyMatch);
2285 if (style->elemMatch != NULL)
2286 xsltFreeCompMatchList(style->elemMatch);
2287 if (style->attrMatch != NULL)
2288 xsltFreeCompMatchList(style->attrMatch);
2289 if (style->parentMatch != NULL)
2290 xsltFreeCompMatchList(style->parentMatch);
2291 if (style->textMatch != NULL)
2292 xsltFreeCompMatchList(style->textMatch);
2293 if (style->piMatch != NULL)
2294 xsltFreeCompMatchList(style->piMatch);
2295 if (style->commentMatch != NULL)
2296 xsltFreeCompMatchList(style->commentMatch);