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->mode != NULL)
155 xmlFree((xmlChar *)comp->mode);
156 if (comp->modeURI != NULL)
157 xmlFree((xmlChar *)comp->modeURI);
158 if (comp->nsList != NULL)
159 xmlFree(comp->nsList);
160 for (i = 0;i < comp->nbStep;i++) {
161 op = &comp->steps[i];
162 if (op->value != NULL)
164 if (op->value2 != NULL)
166 if (op->value3 != NULL)
168 if (op->comp != NULL)
169 xmlXPathFreeCompExpr(op->comp);
171 memset(comp, -1, sizeof(xsltCompMatch));
176 * xsltFreeCompMatchList:
177 * @comp: an XSLT comp list
179 * Free up the memory allocated by all the elements of @comp
182 xsltFreeCompMatchList(xsltCompMatchPtr comp) {
183 xsltCompMatchPtr cur;
185 while (comp != NULL) {
188 xsltFreeCompMatch(cur);
193 * xsltNormalizeCompSteps:
194 * @payload: pointer to template hash table entry
195 * @data: pointer to the stylesheet
196 * @name: template match name
198 * This is a hashtable scanner function to normalize the compiled
199 * steps of an imported stylesheet.
201 void xsltNormalizeCompSteps(void *payload,
202 void *data, const xmlChar *name ATTRIBUTE_UNUSED) {
203 xsltCompMatchPtr comp = payload;
204 xsltStylesheetPtr style = data;
207 for (ix = 0; ix < comp->nbStep; ix++) {
208 comp->steps[ix].previousExtra += style->extrasNr;
209 comp->steps[ix].indexExtra += style->extrasNr;
210 comp->steps[ix].lenExtra += style->extrasNr;
215 * xsltNewParserContext:
216 * @style: the stylesheet
217 * @ctxt: the transformation context, if done at run-time
219 * Create a new XSLT ParserContext
221 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
223 static xsltParserContextPtr
224 xsltNewParserContext(xsltStylesheetPtr style, xsltTransformContextPtr ctxt) {
225 xsltParserContextPtr cur;
227 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
229 xsltTransformError(NULL, NULL, NULL,
230 "xsltNewParserContext : malloc failed\n");
233 memset(cur, 0, sizeof(xsltParserContext));
240 * xsltFreeParserContext:
241 * @ctxt: an XSLT parser context
243 * Free up the memory allocated by @ctxt
246 xsltFreeParserContext(xsltParserContextPtr ctxt) {
249 memset(ctxt, -1, sizeof(xsltParserContext));
255 * @comp: the compiled match expression
257 * @value: the first value
258 * @value2: the second value
260 * Add an step to an XSLT Compiled Match
262 * Returns -1 in case of failure, 0 otherwise.
265 xsltCompMatchAdd(xsltParserContextPtr ctxt, xsltCompMatchPtr comp,
266 xsltOp op, xmlChar * value, xmlChar * value2)
268 if (comp->nbStep >= 40) {
269 xsltTransformError(NULL, NULL, NULL,
270 "xsltCompMatchAdd: overflow\n");
273 comp->steps[comp->nbStep].op = op;
274 comp->steps[comp->nbStep].value = value;
275 comp->steps[comp->nbStep].value2 = value2;
276 if (ctxt->ctxt != NULL) {
277 comp->steps[comp->nbStep].previousExtra =
278 xsltAllocateExtraCtxt(ctxt->ctxt);
279 comp->steps[comp->nbStep].indexExtra =
280 xsltAllocateExtraCtxt(ctxt->ctxt);
281 comp->steps[comp->nbStep].lenExtra =
282 xsltAllocateExtraCtxt(ctxt->ctxt);
284 comp->steps[comp->nbStep].previousExtra =
285 xsltAllocateExtra(ctxt->style);
286 comp->steps[comp->nbStep].indexExtra =
287 xsltAllocateExtra(ctxt->style);
288 comp->steps[comp->nbStep].lenExtra =
289 xsltAllocateExtra(ctxt->style);
291 if (op == XSLT_OP_PREDICATE) {
292 comp->steps[comp->nbStep].comp = xsltXPathCompile(ctxt->style, value);
299 * xsltSwapTopCompMatch:
300 * @comp: the compiled match expression
302 * reverse the two top steps.
305 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
307 int j = comp->nbStep - 1;
310 register xmlChar *tmp;
312 register xmlXPathCompExprPtr expr;
314 tmp = comp->steps[i].value;
315 comp->steps[i].value = comp->steps[j].value;
316 comp->steps[j].value = tmp;
317 tmp = comp->steps[i].value2;
318 comp->steps[i].value2 = comp->steps[j].value2;
319 comp->steps[j].value2 = tmp;
320 op = comp->steps[i].op;
321 comp->steps[i].op = comp->steps[j].op;
322 comp->steps[j].op = op;
323 expr = comp->steps[i].comp;
324 comp->steps[i].comp = comp->steps[j].comp;
325 comp->steps[j].comp = expr;
330 * xsltReverseCompMatch:
331 * @comp: the compiled match expression
333 * reverse all the stack of expressions
336 xsltReverseCompMatch(xsltCompMatchPtr comp) {
338 int j = comp->nbStep - 1;
341 register xmlChar *tmp;
343 register xmlXPathCompExprPtr expr;
344 tmp = comp->steps[i].value;
345 comp->steps[i].value = comp->steps[j].value;
346 comp->steps[j].value = tmp;
347 tmp = comp->steps[i].value2;
348 comp->steps[i].value2 = comp->steps[j].value2;
349 comp->steps[j].value2 = tmp;
350 op = comp->steps[i].op;
351 comp->steps[i].op = comp->steps[j].op;
352 comp->steps[j].op = op;
353 expr = comp->steps[i].comp;
354 comp->steps[i].comp = comp->steps[j].comp;
355 comp->steps[j].comp = expr;
359 comp->steps[comp->nbStep++].op = XSLT_OP_END;
362 /************************************************************************
364 * The interpreter for the precompiled patterns *
366 ************************************************************************/
370 * @ctxt: a XSLT process context
371 * @comp: the precompiled pattern
373 * @mode: the mode name or NULL
374 * @modeURI: the mode URI or NULL
376 * Test wether the node matches the pattern
378 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
381 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
382 xmlNodePtr node, const xmlChar *mode,
383 const xmlChar *modeURI) {
385 xsltStepOpPtr step, select = NULL;
387 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
388 xsltTransformError(ctxt, NULL, node,
389 "xsltTestCompMatch: null arg\n");
393 if (comp->mode == NULL)
395 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
398 if (comp->mode != NULL)
401 if (modeURI != NULL) {
402 if (comp->modeURI == NULL)
404 if ((comp->modeURI != modeURI) &&
405 (!xmlStrEqual(comp->modeURI, modeURI)))
408 if (comp->modeURI != NULL)
411 for (i = 0;i < comp->nbStep;i++) {
412 step = &comp->steps[i];
413 if (step->op != XSLT_OP_PREDICATE)
419 if ((node->type == XML_DOCUMENT_NODE) ||
420 #ifdef LIBXML_DOCB_ENABLED
421 (node->type == XML_DOCB_DOCUMENT_NODE) ||
423 (node->type == XML_HTML_DOCUMENT_NODE))
425 if ((node->type == XML_ELEMENT_NODE) && (node->name[0] == ' '))
429 if (node->type != XML_ELEMENT_NODE)
431 if (step->value == NULL)
433 if (step->value[0] != node->name[0])
435 if (!xmlStrEqual(step->value, node->name))
439 if (node->ns == NULL) {
440 if (step->value2 != NULL)
442 } else if (node->ns->href != NULL) {
443 if (step->value2 == NULL)
445 if (!xmlStrEqual(step->value2, node->ns->href))
449 case XSLT_OP_CHILD: {
452 if ((node->type != XML_ELEMENT_NODE) &&
453 (node->type != XML_DOCUMENT_NODE) &&
454 #ifdef LIBXML_DOCB_ENABLED
455 (node->type != XML_DOCB_DOCUMENT_NODE) &&
457 (node->type != XML_HTML_DOCUMENT_NODE))
460 lst = node->children;
462 if (step->value != NULL) {
463 while (lst != NULL) {
464 if ((lst->type == XML_ELEMENT_NODE) &&
465 (step->value[0] == lst->name[0]) &&
466 (xmlStrEqual(step->value, lst->name)))
476 if (node->type != XML_ATTRIBUTE_NODE)
478 if (step->value != NULL) {
479 if (step->value[0] != node->name[0])
481 if (!xmlStrEqual(step->value, node->name))
485 if (node->ns == NULL) {
486 if (step->value2 != NULL)
488 } else if (step->value2 != NULL) {
489 if (!xmlStrEqual(step->value2, node->ns->href))
494 if ((node->type == XML_DOCUMENT_NODE) ||
495 (node->type == XML_HTML_DOCUMENT_NODE) ||
496 #ifdef LIBXML_DOCB_ENABLED
497 (node->type == XML_DOCB_DOCUMENT_NODE) ||
499 (node->type == XML_NAMESPACE_DECL))
504 if (step->value == NULL)
506 if (step->value[0] != node->name[0])
508 if (!xmlStrEqual(step->value, node->name))
511 if (node->ns == NULL) {
512 if (step->value2 != NULL)
514 } else if (node->ns->href != NULL) {
515 if (step->value2 == NULL)
517 if (!xmlStrEqual(step->value2, node->ns->href))
521 case XSLT_OP_ANCESTOR:
522 /* TODO: implement coalescing of ANCESTOR/NODE ops */
523 if (step->value == NULL) {
525 step = &comp->steps[i];
526 if (step->op == XSLT_OP_ROOT)
528 if (step->op != XSLT_OP_ELEM)
530 if (step->value == NULL)
535 if ((node->type == XML_DOCUMENT_NODE) ||
536 (node->type == XML_HTML_DOCUMENT_NODE) ||
537 #ifdef LIBXML_DOCB_ENABLED
538 (node->type == XML_DOCB_DOCUMENT_NODE) ||
540 (node->type == XML_NAMESPACE_DECL))
543 while (node != NULL) {
546 if ((node->type == XML_ELEMENT_NODE) &&
547 (step->value[0] == node->name[0]) &&
548 (xmlStrEqual(step->value, node->name))) {
550 if (node->ns == NULL) {
551 if (step->value2 == NULL)
553 } else if (node->ns->href != NULL) {
554 if ((step->value2 != NULL) &&
555 (xmlStrEqual(step->value2, node->ns->href)))
565 /* TODO Handle IDs decently, must be done differently */
568 if (node->type != XML_ELEMENT_NODE)
571 id = xmlGetID(node->doc, step->value);
572 if ((id == NULL) || (id->parent != node))
580 list = xsltGetKey(ctxt, step->value,
581 step->value3, step->value2);
584 for (indx = 0;indx < list->nodeNr;indx++)
585 if (list->nodeTab[indx] == node)
587 if (indx >= list->nodeNr)
592 if (node->type != XML_ELEMENT_NODE)
594 if (node->ns == NULL) {
595 if (step->value != NULL)
597 } else if (node->ns->href != NULL) {
598 if (step->value == NULL)
600 if (!xmlStrEqual(step->value, node->ns->href))
605 if (node->type != XML_ELEMENT_NODE)
608 case XSLT_OP_PREDICATE: {
611 int pos = 0, len = 0;
613 * The simple existing predicate code cannot handle
614 * properly cascaded predicates. If in this situation
615 * compute directly the full node list once and check
616 * if the node is in the result list.
618 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
619 xmlDocPtr prevdoc, doc;
620 xmlXPathObjectPtr list;
624 prevdoc = (xmlDocPtr)
625 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
627 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
628 list = (xmlXPathObjectPtr)
629 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra);
632 if ((list == NULL) || (prevdoc != doc)) {
634 xmlXPathObjectPtr newlist;
635 xmlNodePtr parent = node->parent;
639 if (comp->pattern[0] == '/')
640 query = xmlStrdup(comp->pattern);
642 query = xmlStrdup((const xmlChar *)"//");
643 query = xmlStrcat(query, comp->pattern);
645 oldnode = ctxt->xpathCtxt->node;
646 olddoc = ctxt->xpathCtxt->doc;
647 ctxt->xpathCtxt->node = node;
648 ctxt->xpathCtxt->doc = doc;
649 newlist = xmlXPathEval(query, ctxt->xpathCtxt);
650 ctxt->xpathCtxt->node = oldnode;
651 ctxt->xpathCtxt->doc = olddoc;
655 if (newlist->type != XPATH_NODESET) {
656 xmlXPathFreeObject(newlist);
661 if ((parent == NULL) || (node->doc == NULL))
664 if ((doc->name != NULL) &&
665 (doc->name[0] == ' ') &&
666 (xmlStrEqual(BAD_CAST doc->name,
667 BAD_CAST " fake node libxslt")))
673 xmlXPathFreeObject(list);
676 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra) =
678 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
680 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
682 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
683 (xmlFreeFunc) xmlXPathFreeObject;
687 if ((list->nodesetval == NULL) ||
688 (list->nodesetval->nodeNr <= 0)) {
690 xmlXPathFreeObject(list);
693 /* TODO: store the index and use it for the scan */
695 for (j = 0;j < list->nodesetval->nodeNr;j++) {
696 if (list->nodesetval->nodeTab[j] == node) {
698 xmlXPathFreeObject(list);
705 xmlXPathFreeObject(list);
709 * Depending on the last selection, one may need to
710 * recompute contextSize and proximityPosition.
712 * TODO: make this thread safe !
714 oldCS = ctxt->xpathCtxt->contextSize;
715 oldCP = ctxt->xpathCtxt->proximityPosition;
716 if ((select != NULL) &&
717 (select->op == XSLT_OP_ELEM) &&
718 (select->value != NULL) &&
719 (node->type == XML_ELEMENT_NODE) &&
720 (node->parent != NULL)) {
722 int index, nocache = 0;
724 previous = (xmlNodePtr)
725 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
727 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
728 if ((previous != NULL) &&
729 (previous->parent == node->parent)) {
731 * just walk back to adjust the index
734 xmlNodePtr sibling = node;
736 while (sibling != NULL) {
737 if (sibling == previous)
739 if ((previous->type == XML_ELEMENT_NODE) &&
740 (previous->name != NULL) &&
741 (sibling->name != NULL) &&
742 (previous->name[0] == sibling->name[0]) &&
743 (xmlStrEqual(previous->name, sibling->name))) {
744 if ((select->value2 == NULL) ||
745 ((sibling->ns != NULL) &&
746 (xmlStrEqual(select->value2,
747 sibling->ns->href))))
750 sibling = sibling->prev;
752 if (sibling == NULL) {
753 /* hum going backward in document order ... */
756 while (sibling != NULL) {
757 if (sibling == previous)
759 if ((select->value2 == NULL) ||
760 ((sibling->ns != NULL) &&
761 (xmlStrEqual(select->value2,
762 sibling->ns->href))))
764 sibling = sibling->next;
767 if (sibling != NULL) {
770 * If the node is in a Value Tree we cannot
773 if ((node->doc != NULL) &&
774 (node->doc->name != NULL) &&
775 (node->doc->name[0] != ' ')) {
777 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
778 XSLT_RUNTIME_EXTRA(ctxt,
779 select->previousExtra) = node;
780 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
788 * recompute the index
790 xmlNodePtr siblings = node->parent->children;
791 xmlNodePtr parent = node->parent;
793 while (siblings != NULL) {
794 if (siblings->type == XML_ELEMENT_NODE) {
795 if (siblings == node) {
798 } else if ((node->name != NULL) &&
799 (siblings->name != NULL) &&
800 (node->name[0] == siblings->name[0]) &&
801 (xmlStrEqual(node->name, siblings->name))) {
802 if ((select->value2 == NULL) ||
803 ((siblings->ns != NULL) &&
804 (xmlStrEqual(select->value2,
805 siblings->ns->href))))
809 siblings = siblings->next;
811 if ((parent == NULL) || (node->doc == NULL))
814 while (parent->parent != NULL)
815 parent = parent->parent;
816 if (((parent->type != XML_DOCUMENT_NODE) &&
817 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
818 (parent != (xmlNodePtr) node->doc))
823 ctxt->xpathCtxt->contextSize = len;
824 ctxt->xpathCtxt->proximityPosition = pos;
826 * If the node is in a Value Tree we cannot
829 if ((node->doc != NULL) && (nocache == 0)) {
830 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
832 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
834 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
838 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
839 (node->type == XML_ELEMENT_NODE)) {
841 int index, nocache = 0;
843 previous = (xmlNodePtr)
844 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
846 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
847 if ((previous != NULL) &&
848 (previous->parent == node->parent)) {
850 * just walk back to adjust the index
853 xmlNodePtr sibling = node;
855 while (sibling != NULL) {
856 if (sibling == previous)
858 if (sibling->type == XML_ELEMENT_NODE)
860 sibling = sibling->prev;
862 if (sibling == NULL) {
863 /* hum going backward in document order ... */
866 while (sibling != NULL) {
867 if (sibling == previous)
869 if (sibling->type == XML_ELEMENT_NODE)
871 sibling = sibling->next;
874 if (sibling != NULL) {
877 * If the node is in a Value Tree we cannot
880 if ((node->doc != NULL) &&
881 (node->doc->name != NULL) &&
882 (node->doc->name[0] != ' ')) {
884 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
885 XSLT_RUNTIME_EXTRA(ctxt,
886 select->previousExtra) = node;
887 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
894 * recompute the index
896 xmlNodePtr siblings = node->parent->children;
897 xmlNodePtr parent = node->parent;
899 while (siblings != NULL) {
900 if (siblings->type == XML_ELEMENT_NODE) {
902 if (siblings == node) {
906 siblings = siblings->next;
908 if ((parent == NULL) || (node->doc == NULL))
911 while (parent->parent != NULL)
912 parent = parent->parent;
913 if (((parent->type != XML_DOCUMENT_NODE) &&
914 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
915 (parent != (xmlNodePtr) node->doc))
920 ctxt->xpathCtxt->contextSize = len;
921 ctxt->xpathCtxt->proximityPosition = pos;
923 * If the node is in a Value Tree we cannot
926 if ((node->doc != NULL) && (nocache == 0)) {
927 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
929 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
931 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
936 oldNode = ctxt->node;
939 if (step->value == NULL)
941 if (step->comp == NULL)
944 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
949 ctxt->xpathCtxt->contextSize = oldCS;
950 ctxt->xpathCtxt->proximityPosition = oldCP;
952 ctxt->node = oldNode;
956 ctxt->xpathCtxt->contextSize = oldCS;
957 ctxt->xpathCtxt->proximityPosition = oldCP;
959 ctxt->node = oldNode;
963 if (node->type != XML_PI_NODE)
965 if (step->value != NULL) {
966 if (!xmlStrEqual(step->value, node->name))
970 case XSLT_OP_COMMENT:
971 if (node->type != XML_COMMENT_NODE)
975 if ((node->type != XML_TEXT_NODE) &&
976 (node->type != XML_CDATA_SECTION_NODE))
980 switch (node->type) {
981 case XML_ELEMENT_NODE:
982 case XML_CDATA_SECTION_NODE:
984 case XML_COMMENT_NODE:
997 * xsltTestCompMatchList:
998 * @ctxt: a XSLT process context
1000 * @comp: the precompiled pattern list
1002 * Test wether the node matches one of the patterns in the list
1004 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
1007 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
1008 xsltCompMatchPtr comp) {
1011 if ((ctxt == NULL) || (node == NULL))
1013 while (comp != NULL) {
1014 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
1022 /************************************************************************
1024 * Dedicated parser for templates *
1026 ************************************************************************/
1028 #define CUR (*ctxt->cur)
1029 #define SKIP(val) ctxt->cur += (val)
1030 #define NXT(val) ctxt->cur[(val)]
1031 #define CUR_PTR ctxt->cur
1033 #define SKIP_BLANKS \
1034 while (IS_BLANK_CH(CUR)) NEXT
1036 #define CURRENT (*ctxt->cur)
1037 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1040 #define PUSH(op, val, val2) \
1041 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1044 xsltSwapTopCompMatch(ctxt->comp);
1046 #define XSLT_ERROR(X) \
1047 { xsltError(ctxt, __FILE__, __LINE__, X); \
1048 ctxt->error = (X); return; }
1050 #define XSLT_ERROR0(X) \
1051 { xsltError(ctxt, __FILE__, __LINE__, X); \
1052 ctxt->error = (X); return(0); }
1056 * @ctxt: the XPath Parser context
1058 * Parse an XPath Litteral:
1060 * [29] Literal ::= '"' [^"]* '"'
1063 * Returns the Literal parsed or NULL
1067 xsltScanLiteral(xsltParserContextPtr ctxt) {
1068 const xmlChar *q, *cur;
1069 xmlChar *ret = NULL;
1076 val = xmlStringCurrentChar(NULL, cur, &len);
1077 while ((IS_CHAR(val)) && (val != '"')) {
1079 val = xmlStringCurrentChar(NULL, cur, &len);
1081 if (!IS_CHAR(val)) {
1085 ret = xmlStrndup(q, cur - q);
1089 } else if (CUR == '\'') {
1092 val = xmlStringCurrentChar(NULL, cur, &len);
1093 while ((IS_CHAR(val)) && (val != '\'')) {
1095 val = xmlStringCurrentChar(NULL, cur, &len);
1097 if (!IS_CHAR(val)) {
1101 ret = xmlStrndup(q, cur - q);
1106 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1115 * @ctxt: the XPath Parser context
1117 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1118 * CombiningChar | Extender
1120 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1122 * [6] Names ::= Name (S Name)*
1124 * Returns the Name parsed or NULL
1128 xsltScanName(xsltParserContextPtr ctxt) {
1129 const xmlChar *q, *cur;
1130 xmlChar *ret = NULL;
1136 val = xmlStringCurrentChar(NULL, cur, &len);
1137 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1140 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1141 (val == '.') || (val == '-') ||
1143 (IS_COMBINING(val)) ||
1144 (IS_EXTENDER(val))) {
1146 val = xmlStringCurrentChar(NULL, cur, &len);
1148 ret = xmlStrndup(q, cur - q);
1155 * @ctxt: the XPath Parser context
1157 * Parses a non qualified name
1159 * Returns the Name parsed or NULL
1163 xsltScanNCName(xsltParserContextPtr ctxt) {
1164 const xmlChar *q, *cur;
1165 xmlChar *ret = NULL;
1171 val = xmlStringCurrentChar(NULL, cur, &len);
1172 if (!IS_LETTER(val) && (val != '_'))
1175 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1176 (val == '.') || (val == '-') ||
1178 (IS_COMBINING(val)) ||
1179 (IS_EXTENDER(val))) {
1181 val = xmlStringCurrentChar(NULL, cur, &len);
1183 ret = xmlStrndup(q, cur - q);
1190 * @ctxt: the XPath Parser context
1191 * @prefix: the place to store the prefix
1193 * Parse a qualified name
1195 * Returns the Name parsed or NULL
1199 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1200 xmlChar *ret = NULL;
1203 ret = xsltScanNCName(ctxt);
1207 ret = xsltScanNCName(ctxt);
1213 * xsltCompileIdKeyPattern:
1214 * @ctxt: the compilation context
1215 * @name: a preparsed name
1216 * @aid: whether id/key are allowed there
1218 * Compile the XSLT LocationIdKeyPattern
1219 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1220 * | 'key' '(' Literal ',' Literal ')'
1222 * also handle NodeType and PI from:
1224 * [7] NodeTest ::= NameTest
1225 * | NodeType '(' ')'
1226 * | 'processing-instruction' '(' Literal ')'
1229 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1230 xmlChar *lit = NULL;
1231 xmlChar *lit2 = NULL;
1234 xsltTransformError(NULL, NULL, NULL,
1235 "xsltCompileIdKeyPattern : ( expected\n");
1239 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1242 lit = xsltScanLiteral(ctxt);
1247 xsltTransformError(NULL, NULL, NULL,
1248 "xsltCompileIdKeyPattern : ) expected\n");
1253 PUSH(XSLT_OP_ID, lit, NULL);
1254 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1257 lit = xsltScanLiteral(ctxt);
1262 xsltTransformError(NULL, NULL, NULL,
1263 "xsltCompileIdKeyPattern : , expected\n");
1269 lit2 = xsltScanLiteral(ctxt);
1274 xsltTransformError(NULL, NULL, NULL,
1275 "xsltCompileIdKeyPattern : ) expected\n");
1280 /* TODO: support namespace in keys */
1281 PUSH(XSLT_OP_KEY, lit, lit2);
1282 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1286 lit = xsltScanLiteral(ctxt);
1291 xsltTransformError(NULL, NULL, NULL,
1292 "xsltCompileIdKeyPattern : ) expected\n");
1298 PUSH(XSLT_OP_PI, lit, NULL);
1299 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1303 xsltTransformError(NULL, NULL, NULL,
1304 "xsltCompileIdKeyPattern : ) expected\n");
1309 PUSH(XSLT_OP_TEXT, NULL, NULL);
1310 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1314 xsltTransformError(NULL, NULL, NULL,
1315 "xsltCompileIdKeyPattern : ) expected\n");
1320 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1321 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1325 xsltTransformError(NULL, NULL, NULL,
1326 "xsltCompileIdKeyPattern : ) expected\n");
1331 PUSH(XSLT_OP_NODE, NULL, NULL);
1333 xsltTransformError(NULL, NULL, NULL,
1334 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1338 xsltTransformError(NULL, NULL, NULL,
1339 "xsltCompileIdKeyPattern : node type\n");
1349 * xsltCompileStepPattern:
1350 * @ctxt: the compilation context
1351 * @token: a posible precompiled name
1353 * Compile the XSLT StepPattern and generates a precompiled
1354 * form suitable for fast matching.
1356 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1357 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1358 * | ('child' | 'attribute') '::'
1360 * [7] NodeTest ::= NameTest
1361 * | NodeType '(' ')'
1362 * | 'processing-instruction' '(' Literal ')'
1363 * [8] Predicate ::= '[' PredicateExpr ']'
1364 * [9] PredicateExpr ::= Expr
1365 * [13] AbbreviatedAxisSpecifier ::= '@'?
1366 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1370 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1371 xmlChar *name = NULL;
1372 const xmlChar *URI = NULL;
1373 xmlChar *URL = NULL;
1377 if ((token == NULL) && (CUR == '@')) {
1378 xmlChar *prefix = NULL;
1383 PUSH(XSLT_OP_ATTR, NULL, NULL);
1384 goto parse_predicate;
1386 token = xsltScanQName(ctxt, &prefix);
1387 if (prefix != NULL) {
1390 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1392 xsltTransformError(NULL, NULL, NULL,
1393 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1396 URL = xmlStrdup(ns->href);
1400 if (token == NULL) {
1403 PUSH(XSLT_OP_ATTR, NULL, URL);
1406 xsltTransformError(NULL, NULL, NULL,
1407 "xsltCompileStepPattern : Name expected\n");
1411 PUSH(XSLT_OP_ATTR, token, URL);
1412 goto parse_predicate;
1415 token = xsltScanName(ctxt);
1416 if (token == NULL) {
1419 PUSH(XSLT_OP_ALL, token, NULL);
1420 goto parse_predicate;
1422 xsltTransformError(NULL, NULL, NULL,
1423 "xsltCompileStepPattern : Name expected\n");
1432 xsltCompileIdKeyPattern(ctxt, token, 0);
1435 } else if (CUR == ':') {
1438 xmlChar *prefix = token;
1442 * This is a namespace match
1444 token = xsltScanName(ctxt);
1445 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1447 xsltTransformError(NULL, NULL, NULL,
1448 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1453 URL = xmlStrdup(ns->href);
1456 if (token == NULL) {
1459 PUSH(XSLT_OP_NS, URL, NULL);
1461 xsltTransformError(NULL, NULL, NULL,
1462 "xsltCompileStepPattern : Name expected\n");
1467 PUSH(XSLT_OP_ELEM, token, URL);
1471 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1473 token = xsltScanName(ctxt);
1474 if (token == NULL) {
1477 PUSH(XSLT_OP_ALL, token, NULL);
1478 goto parse_predicate;
1480 xsltTransformError(NULL, NULL, NULL,
1481 "xsltCompileStepPattern : QName expected\n");
1486 URI = xsltGetQNameURI(ctxt->elem, &token);
1487 if (token == NULL) {
1491 name = xmlStrdup(token);
1493 URL = xmlStrdup(URI);
1495 PUSH(XSLT_OP_CHILD, name, URL);
1496 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1498 token = xsltScanName(ctxt);
1499 if (token == NULL) {
1500 xsltTransformError(NULL, NULL, NULL,
1501 "xsltCompileStepPattern : QName expected\n");
1505 URI = xsltGetQNameURI(ctxt->elem, &token);
1506 if (token == NULL) {
1510 name = xmlStrdup(token);
1512 URL = xmlStrdup(URI);
1514 PUSH(XSLT_OP_ATTR, name, URL);
1516 xsltTransformError(NULL, NULL, NULL,
1517 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1523 } else if (CUR == '*') {
1525 PUSH(XSLT_OP_ALL, token, NULL);
1527 URI = xsltGetQNameURI(ctxt->elem, &token);
1528 if (token == NULL) {
1533 URL = xmlStrdup(URI);
1534 PUSH(XSLT_OP_ELEM, token, URL);
1539 while (CUR == '[') {
1541 xmlChar *ret = NULL;
1547 /* Skip over nested predicates */
1550 else if (CUR == ']') {
1554 } else if (CUR == '"') {
1556 while ((CUR != 0) && (CUR != '"'))
1558 } else if (CUR == '\'') {
1560 while ((CUR != 0) && (CUR != '\''))
1566 xsltTransformError(NULL, NULL, NULL,
1567 "xsltCompileStepPattern : ']' expected\n");
1571 ret = xmlStrndup(q, CUR_PTR - q);
1572 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1573 /* push the predicate lower than local test */
1587 * xsltCompileRelativePathPattern:
1588 * @comp: the compilation context
1589 * @token: a posible precompiled name
1591 * Compile the XSLT RelativePathPattern and generates a precompiled
1592 * form suitable for fast matching.
1594 * [4] RelativePathPattern ::= StepPattern
1595 * | RelativePathPattern '/' StepPattern
1596 * | RelativePathPattern '//' StepPattern
1599 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1600 xsltCompileStepPattern(ctxt, token);
1604 while ((CUR != 0) && (CUR != '|')) {
1605 if ((CUR == '/') && (NXT(1) == '/')) {
1606 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1610 xsltCompileStepPattern(ctxt, NULL);
1611 } else if (CUR == '/') {
1612 PUSH(XSLT_OP_PARENT, NULL, NULL);
1615 if ((CUR != 0) || (CUR == '|')) {
1616 xsltCompileRelativePathPattern(ctxt, NULL);
1630 * xsltCompileLocationPathPattern:
1631 * @ctxt: the compilation context
1633 * Compile the XSLT LocationPathPattern and generates a precompiled
1634 * form suitable for fast matching.
1636 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1637 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1638 * | '//'? RelativePathPattern
1641 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1643 if ((CUR == '/') && (NXT(1) == '/')) {
1645 * since we reverse the query
1646 * a leading // can be safely ignored
1650 ctxt->comp->priority = 0.5; /* '//' means not 0 priority */
1651 xsltCompileRelativePathPattern(ctxt, NULL);
1652 } else if (CUR == '/') {
1654 * We need to find root as the parent
1658 PUSH(XSLT_OP_ROOT, NULL, NULL);
1659 if ((CUR != 0) || (CUR == '|')) {
1660 PUSH(XSLT_OP_PARENT, NULL, NULL);
1661 xsltCompileRelativePathPattern(ctxt, NULL);
1663 } else if (CUR == '*') {
1664 xsltCompileRelativePathPattern(ctxt, NULL);
1665 } else if (CUR == '@') {
1666 xsltCompileRelativePathPattern(ctxt, NULL);
1669 name = xsltScanName(ctxt);
1671 xsltTransformError(NULL, NULL, NULL,
1672 "xsltCompileLocationPathPattern : Name expected\n");
1677 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1678 xsltCompileIdKeyPattern(ctxt, name, 1);
1679 if ((CUR == '/') && (NXT(1) == '/')) {
1680 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1684 xsltCompileRelativePathPattern(ctxt, NULL);
1685 } else if (CUR == '/') {
1686 PUSH(XSLT_OP_PARENT, NULL, NULL);
1689 xsltCompileRelativePathPattern(ctxt, NULL);
1693 xsltCompileRelativePathPattern(ctxt, name);
1700 * xsltCompilePattern:
1701 * @pattern: an XSLT pattern
1702 * @doc: the containing document
1703 * @node: the containing element
1704 * @style: the stylesheet
1705 * @runtime: the transformation context, if done at run-time
1707 * Compile the XSLT pattern and generates a list of precompiled form suitable
1708 * for fast matching.
1710 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1712 * Returns the generated pattern list or NULL in case of failure
1716 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1717 xmlNodePtr node, xsltStylesheetPtr style,
1718 xsltTransformContextPtr runtime) {
1719 xsltParserContextPtr ctxt = NULL;
1720 xsltCompMatchPtr element, first = NULL, previous = NULL;
1721 int current, start, end, level, j;
1723 if (pattern == NULL) {
1724 xsltTransformError(NULL, NULL, node,
1725 "xsltCompilePattern : NULL pattern\n");
1729 ctxt = xsltNewParserContext(style, runtime);
1735 while (pattern[current] != 0) {
1737 while (IS_BLANK_CH(pattern[current]))
1741 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1742 if (pattern[end] == '[')
1744 else if (pattern[end] == ']')
1746 else if (pattern[end] == '\'') {
1748 while ((pattern[end] != 0) && (pattern[end] != '\''))
1750 } else if (pattern[end] == '"') {
1752 while ((pattern[end] != 0) && (pattern[end] != '"'))
1757 if (current == end) {
1758 xsltTransformError(NULL, NULL, node,
1759 "xsltCompilePattern : NULL pattern\n");
1762 element = xsltNewCompMatch();
1763 if (element == NULL) {
1768 else if (previous != NULL)
1769 previous->next = element;
1772 ctxt->comp = element;
1773 ctxt->base = xmlStrndup(&pattern[start], end - start);
1774 if (ctxt->base == NULL)
1776 ctxt->cur = &(ctxt->base)[current - start];
1777 element->pattern = ctxt->base;
1778 element->nsList = xmlGetNsList(doc, node);
1780 if (element->nsList != NULL) {
1781 while (element->nsList[j] != NULL)
1787 #ifdef WITH_XSLT_DEBUG_PATTERN
1788 xsltGenericDebug(xsltGenericDebugContext,
1789 "xsltCompilePattern : parsing '%s'\n",
1793 Preset default priority to be zero.
1794 This may be changed by xsltCompileLocationPathPattern.
1796 element->priority = 0;
1797 xsltCompileLocationPathPattern(ctxt);
1799 xsltTransformError(NULL, style, node,
1800 "xsltCompilePattern : failed to compile '%s'\n",
1802 if (style != NULL) style->errors++;
1807 * Reverse for faster interpretation.
1809 xsltReverseCompMatch(element);
1812 * Set-up the priority
1814 if (element->priority == 0) { /* if not yet determined */
1815 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1816 (element->steps[0].op == XSLT_OP_ATTR) ||
1817 (element->steps[0].op == XSLT_OP_PI)) &&
1818 (element->steps[0].value != NULL) &&
1819 (element->steps[1].op == XSLT_OP_END)) {
1820 ; /* previously preset */
1821 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1822 (element->steps[0].value2 != NULL) &&
1823 (element->steps[1].op == XSLT_OP_END)) {
1824 element->priority = -0.25;
1825 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1826 (element->steps[0].value != NULL) &&
1827 (element->steps[1].op == XSLT_OP_END)) {
1828 element->priority = -0.25;
1829 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1830 (element->steps[0].value == NULL) &&
1831 (element->steps[0].value2 == NULL) &&
1832 (element->steps[1].op == XSLT_OP_END)) {
1833 element->priority = -0.5;
1834 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1835 (element->steps[0].op == XSLT_OP_TEXT) ||
1836 (element->steps[0].op == XSLT_OP_ALL) ||
1837 (element->steps[0].op == XSLT_OP_NODE) ||
1838 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1839 (element->steps[1].op == XSLT_OP_END)) {
1840 element->priority = -0.5;
1842 element->priority = 0.5;
1845 #ifdef WITH_XSLT_DEBUG_PATTERN
1846 xsltGenericDebug(xsltGenericDebugContext,
1847 "xsltCompilePattern : parsed %s, default priority %f\n",
1848 element->pattern, element->priority);
1850 if (pattern[end] == '|')
1855 xsltTransformError(NULL, style, node,
1856 "xsltCompilePattern : NULL pattern\n");
1857 if (style != NULL) style->errors++;
1861 xsltFreeParserContext(ctxt);
1866 xsltFreeParserContext(ctxt);
1868 xsltFreeCompMatchList(first);
1872 /************************************************************************
1874 * Module interfaces *
1876 ************************************************************************/
1880 * @style: an XSLT stylesheet
1881 * @cur: an XSLT template
1882 * @mode: the mode name or NULL
1883 * @modeURI: the mode URI or NULL
1885 * Register the XSLT pattern associated to @cur
1887 * Returns -1 in case of error, 0 otherwise
1890 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1891 const xmlChar *mode, const xmlChar *modeURI) {
1892 xsltCompMatchPtr pat, list, *top = NULL, next;
1893 const xmlChar *name = NULL;
1894 float priority; /* the priority */
1896 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1899 priority = cur->priority;
1900 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1906 pat->template = cur;
1908 pat->mode = xmlStrdup(mode);
1909 if (modeURI != NULL)
1910 pat->modeURI = xmlStrdup(modeURI);
1911 if (priority != XSLT_PAT_NO_PRIORITY)
1912 pat->priority = priority;
1915 * insert it in the hash table list corresponding to its lookup name
1917 switch (pat->steps[0].op) {
1919 if (pat->steps[0].value != NULL)
1920 name = pat->steps[0].value;
1922 top = (xsltCompMatchPtr *) &(style->attrMatch);
1925 case XSLT_OP_PARENT:
1926 case XSLT_OP_ANCESTOR:
1927 top = (xsltCompMatchPtr *) &(style->elemMatch);
1930 top = (xsltCompMatchPtr *) &(style->rootMatch);
1933 top = (xsltCompMatchPtr *) &(style->keyMatch);
1936 /* TODO optimize ID !!! */
1939 top = (xsltCompMatchPtr *) &(style->elemMatch);
1942 case XSLT_OP_PREDICATE:
1943 xsltTransformError(NULL, style, NULL,
1944 "xsltAddTemplate: invalid compiled pattern\n");
1945 xsltFreeCompMatch(pat);
1948 * TODO: some flags at the top level about type based patterns
1949 * would be faster than inclusion in the hash table.
1952 if (pat->steps[0].value != NULL)
1953 name = pat->steps[0].value;
1955 top = (xsltCompMatchPtr *) &(style->piMatch);
1957 case XSLT_OP_COMMENT:
1958 top = (xsltCompMatchPtr *) &(style->commentMatch);
1961 top = (xsltCompMatchPtr *) &(style->textMatch);
1965 if (pat->steps[0].value != NULL)
1966 name = pat->steps[0].value;
1968 top = (xsltCompMatchPtr *) &(style->elemMatch);
1972 if (style->templatesHash == NULL) {
1973 style->templatesHash = xmlHashCreate(1024);
1974 if (style->templatesHash == NULL) {
1975 xsltFreeCompMatch(pat);
1978 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1980 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1981 name, mode, modeURI);
1983 xmlHashAddEntry3(style->templatesHash, name,
1984 mode, modeURI, pat);
1987 * Note '<=' since one must choose among the matching
1988 * template rules that are left, the one that occurs
1989 * last in the stylesheet
1991 if (list->priority <= pat->priority) {
1993 xmlHashUpdateEntry3(style->templatesHash, name,
1994 mode, modeURI, pat, NULL);
1996 while (list->next != NULL) {
1997 if (list->next->priority <= pat->priority)
2001 pat->next = list->next;
2006 } else if (top != NULL) {
2011 } else if (list->priority <= pat->priority) {
2015 while (list->next != NULL) {
2016 if (list->next->priority <= pat->priority)
2020 pat->next = list->next;
2024 xsltTransformError(NULL, style, NULL,
2025 "xsltAddTemplate: invalid compiled pattern\n");
2026 xsltFreeCompMatch(pat);
2029 #ifdef WITH_XSLT_DEBUG_PATTERN
2031 xsltGenericDebug(xsltGenericDebugContext,
2032 "added pattern : '%s' mode '%s' priority %f\n",
2033 pat->pattern, pat->mode, pat->priority);
2035 xsltGenericDebug(xsltGenericDebugContext,
2036 "added pattern : '%s' priority %f\n",
2037 pat->pattern, pat->priority);
2047 * @ctxt: a XSLT process context
2048 * @node: the node being processed
2049 * @style: the current style
2051 * Finds the template applying to this node, if @style is non-NULL
2052 * it means one needs to look for the next imported template in scope.
2054 * Returns the xsltTemplatePtr or NULL if not found
2057 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2058 xsltStylesheetPtr style) {
2059 xsltStylesheetPtr curstyle;
2060 xsltTemplatePtr ret = NULL;
2061 const xmlChar *name = NULL;
2062 xsltCompMatchPtr list = NULL;
2066 if ((ctxt == NULL) || (node == NULL))
2069 if (style == NULL) {
2070 curstyle = ctxt->style;
2072 curstyle = xsltNextImport(style);
2075 while ((curstyle != NULL) && (curstyle != style)) {
2076 priority = XSLT_PAT_NO_PRIORITY;
2077 /* TODO : handle IDs/keys here ! */
2078 if (curstyle->templatesHash != NULL) {
2080 * Use the top name as selector
2082 switch (node->type) {
2083 case XML_ELEMENT_NODE:
2084 if (node->name[0] == ' ')
2086 case XML_ATTRIBUTE_NODE:
2090 case XML_DOCUMENT_NODE:
2091 case XML_HTML_DOCUMENT_NODE:
2093 case XML_CDATA_SECTION_NODE:
2094 case XML_COMMENT_NODE:
2095 case XML_ENTITY_REF_NODE:
2096 case XML_ENTITY_NODE:
2097 case XML_DOCUMENT_TYPE_NODE:
2098 case XML_DOCUMENT_FRAG_NODE:
2099 case XML_NOTATION_NODE:
2101 case XML_ELEMENT_DECL:
2102 case XML_ATTRIBUTE_DECL:
2103 case XML_ENTITY_DECL:
2104 case XML_NAMESPACE_DECL:
2105 case XML_XINCLUDE_START:
2106 case XML_XINCLUDE_END:
2115 * find the list of appliable expressions based on the name
2117 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2118 name, ctxt->mode, ctxt->modeURI);
2121 while (list != NULL) {
2122 if (xsltTestCompMatch(ctxt, list, node,
2123 ctxt->mode, ctxt->modeURI)) {
2124 ret = list->template;
2125 priority = list->priority;
2133 * find alternate generic matches
2135 switch (node->type) {
2136 case XML_ELEMENT_NODE:
2137 if (node->name[0] == ' ')
2138 list = curstyle->rootMatch;
2140 list = curstyle->elemMatch;
2141 if (node->psvi != NULL) keyed = 1;
2143 case XML_ATTRIBUTE_NODE: {
2146 list = curstyle->attrMatch;
2147 attr = (xmlAttrPtr) node;
2148 if (attr->psvi != NULL) keyed = 1;
2152 list = curstyle->piMatch;
2153 if (node->psvi != NULL) keyed = 1;
2155 case XML_DOCUMENT_NODE:
2156 case XML_HTML_DOCUMENT_NODE: {
2159 list = curstyle->rootMatch;
2160 doc = (xmlDocPtr) node;
2161 if (doc->psvi != NULL) keyed = 1;
2165 case XML_CDATA_SECTION_NODE:
2166 list = curstyle->textMatch;
2167 if (node->psvi != NULL) keyed = 1;
2169 case XML_COMMENT_NODE:
2170 list = curstyle->commentMatch;
2171 if (node->psvi != NULL) keyed = 1;
2173 case XML_ENTITY_REF_NODE:
2174 case XML_ENTITY_NODE:
2175 case XML_DOCUMENT_TYPE_NODE:
2176 case XML_DOCUMENT_FRAG_NODE:
2177 case XML_NOTATION_NODE:
2179 case XML_ELEMENT_DECL:
2180 case XML_ATTRIBUTE_DECL:
2181 case XML_ENTITY_DECL:
2182 case XML_NAMESPACE_DECL:
2183 case XML_XINCLUDE_START:
2184 case XML_XINCLUDE_END:
2189 while ((list != NULL) &&
2190 ((ret == NULL) || (list->priority > priority))) {
2191 if (xsltTestCompMatch(ctxt, list, node,
2192 ctxt->mode, ctxt->modeURI)) {
2193 ret = list->template;
2194 priority = list->priority;
2200 * Some of the tests for elements can also apply to documents
2202 if ((node->type == XML_DOCUMENT_NODE) ||
2203 (node->type == XML_HTML_DOCUMENT_NODE) ||
2204 (node->type == XML_TEXT_NODE)) {
2205 list = curstyle->elemMatch;
2206 while ((list != NULL) &&
2207 ((ret == NULL) || (list->priority > priority))) {
2208 if (xsltTestCompMatch(ctxt, list, node,
2209 ctxt->mode, ctxt->modeURI)) {
2210 ret = list->template;
2211 priority = list->priority;
2216 } else if ((node->type == XML_PI_NODE) ||
2217 (node->type == XML_COMMENT_NODE)) {
2218 list = curstyle->elemMatch;
2219 while ((list != NULL) &&
2220 ((ret == NULL) || (list->priority > priority))) {
2221 if (xsltTestCompMatch(ctxt, list, node,
2222 ctxt->mode, ctxt->modeURI)) {
2223 ret = list->template;
2224 priority = list->priority;
2232 list = curstyle->keyMatch;
2233 while ((list != NULL) &&
2234 ((ret == NULL) || (list->priority > priority))) {
2235 if (xsltTestCompMatch(ctxt, list, node,
2236 ctxt->mode, ctxt->modeURI)) {
2237 ret = list->template;
2238 priority = list->priority;
2248 * Cycle on next curstylesheet import.
2250 curstyle = xsltNextImport(curstyle);
2256 * xsltCleanupTemplates:
2257 * @style: an XSLT stylesheet
2259 * Cleanup the state of the templates used by the stylesheet and
2260 * the ones it imports.
2263 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2267 * xsltFreeTemplateHashes:
2268 * @style: an XSLT stylesheet
2270 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2273 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2274 if (style->templatesHash != NULL)
2275 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2276 (xmlHashDeallocator) xsltFreeCompMatchList);
2277 if (style->rootMatch != NULL)
2278 xsltFreeCompMatchList(style->rootMatch);
2279 if (style->keyMatch != NULL)
2280 xsltFreeCompMatchList(style->keyMatch);
2281 if (style->elemMatch != NULL)
2282 xsltFreeCompMatchList(style->elemMatch);
2283 if (style->attrMatch != NULL)
2284 xsltFreeCompMatchList(style->attrMatch);
2285 if (style->parentMatch != NULL)
2286 xsltFreeCompMatchList(style->parentMatch);
2287 if (style->textMatch != NULL)
2288 xsltFreeCompMatchList(style->textMatch);
2289 if (style->piMatch != NULL)
2290 xsltFreeCompMatchList(style->piMatch);
2291 if (style->commentMatch != NULL)
2292 xsltFreeCompMatchList(style->commentMatch);