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 whether 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, sel = 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) && (step->op != XSLT_OP_ALL))
530 if ((step->value == NULL) && (step->op != XSLT_OP_ALL))
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 if (step->value == NULL)
545 while (node != NULL) {
548 if ((node->type == XML_ELEMENT_NODE) &&
549 (step->value[0] == node->name[0]) &&
550 (xmlStrEqual(step->value, node->name))) {
552 if (node->ns == NULL) {
553 if (step->value2 == NULL)
555 } else if (node->ns->href != NULL) {
556 if ((step->value2 != NULL) &&
557 (xmlStrEqual(step->value2, node->ns->href)))
567 /* TODO Handle IDs decently, must be done differently */
570 if (node->type != XML_ELEMENT_NODE)
573 id = xmlGetID(node->doc, step->value);
574 if ((id == NULL) || (id->parent != node))
582 list = xsltGetKey(ctxt, step->value,
583 step->value3, step->value2);
586 for (indx = 0;indx < list->nodeNr;indx++)
587 if (list->nodeTab[indx] == node)
589 if (indx >= list->nodeNr)
594 if (node->type != XML_ELEMENT_NODE)
596 if (node->ns == NULL) {
597 if (step->value != NULL)
599 } else if (node->ns->href != NULL) {
600 if (step->value == NULL)
602 if (!xmlStrEqual(step->value, node->ns->href))
607 if (node->type != XML_ELEMENT_NODE)
610 case XSLT_OP_PREDICATE: {
614 int pos = 0, len = 0;
619 (doc->name != NULL) &&
620 (doc->name[0] == ' ') &&
621 (xmlStrEqual(BAD_CAST doc->name,
622 BAD_CAST " fake node libxslt")))
627 * The simple existing predicate code cannot handle
628 * properly cascaded predicates. If in this situation
629 * compute directly the full node list once and check
630 * if the node is in the result list.
632 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
634 xmlXPathObjectPtr list;
638 prevdoc = (xmlDocPtr)
639 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
640 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
641 list = (xmlXPathObjectPtr)
642 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra);
644 if ((list == NULL) || (prevdoc != doc)) {
646 xmlXPathObjectPtr newlist;
647 xmlNodePtr parent = node->parent;
651 if (comp->pattern[0] == '/')
652 query = xmlStrdup(comp->pattern);
654 query = xmlStrdup((const xmlChar *)"//");
655 query = xmlStrcat(query, comp->pattern);
657 oldnode = ctxt->xpathCtxt->node;
658 olddoc = ctxt->xpathCtxt->doc;
659 ctxt->xpathCtxt->node = node;
660 ctxt->xpathCtxt->doc = doc;
661 newlist = xmlXPathEval(query, ctxt->xpathCtxt);
662 ctxt->xpathCtxt->node = oldnode;
663 ctxt->xpathCtxt->doc = olddoc;
667 if (newlist->type != XPATH_NODESET) {
668 xmlXPathFreeObject(newlist);
673 if ((parent == NULL) || (node->doc == NULL) || isRVT)
678 xmlXPathFreeObject(list);
681 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra) =
683 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
685 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
687 XSLT_RUNTIME_EXTRA_FREE(ctxt, sel->lenExtra) =
688 (xmlFreeFunc) xmlXPathFreeObject;
692 if ((list->nodesetval == NULL) ||
693 (list->nodesetval->nodeNr <= 0)) {
695 xmlXPathFreeObject(list);
698 /* TODO: store the index and use it for the scan */
700 for (j = 0;j < list->nodesetval->nodeNr;j++) {
701 if (list->nodesetval->nodeTab[j] == node) {
703 xmlXPathFreeObject(list);
710 xmlXPathFreeObject(list);
714 * Depending on the last selection, one may need to
715 * recompute contextSize and proximityPosition.
717 * TODO: make this thread safe !
719 oldCS = ctxt->xpathCtxt->contextSize;
720 oldCP = ctxt->xpathCtxt->proximityPosition;
722 (sel->op == XSLT_OP_ELEM) &&
723 (sel->value != NULL) &&
724 (node->type == XML_ELEMENT_NODE) &&
725 (node->parent != NULL)) {
729 previous = (xmlNodePtr)
730 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
731 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
732 if ((previous != NULL) &&
733 (previous->parent == node->parent)) {
735 * just walk back to adjust the index
738 xmlNodePtr sibling = node;
740 while (sibling != NULL) {
741 if (sibling == previous)
743 if ((previous->type == XML_ELEMENT_NODE) &&
744 (previous->name != NULL) &&
745 (sibling->name != NULL) &&
746 (previous->name[0] == sibling->name[0]) &&
747 (xmlStrEqual(previous->name, sibling->name))) {
748 if ((sel->value2 == NULL) ||
749 ((sibling->ns != NULL) &&
750 (xmlStrEqual(sel->value2,
751 sibling->ns->href))))
754 sibling = sibling->prev;
756 if (sibling == NULL) {
757 /* hum going backward in document order ... */
760 while (sibling != NULL) {
761 if (sibling == previous)
763 if ((sel->value2 == NULL) ||
764 ((sibling->ns != NULL) &&
765 (xmlStrEqual(sel->value2,
766 sibling->ns->href))))
768 sibling = sibling->next;
771 if (sibling != NULL) {
774 * If the node is in a Value Tree we need to
775 * save len, but cannot cache the node!
776 * (bugs 153137 and 158840)
778 if (node->doc != NULL) {
779 len = XSLT_RUNTIME_EXTRA(ctxt,
780 sel->lenExtra, ival);
782 XSLT_RUNTIME_EXTRA(ctxt,
783 sel->previousExtra, ptr) = node;
784 XSLT_RUNTIME_EXTRA(ctxt,
785 sel->indexExtra, ival) = pos;
793 * recompute the index
795 xmlNodePtr siblings = node->parent->children;
796 xmlNodePtr parent = node->parent;
798 while (siblings != NULL) {
799 if (siblings->type == XML_ELEMENT_NODE) {
800 if (siblings == node) {
803 } else if ((node->name != NULL) &&
804 (siblings->name != NULL) &&
805 (node->name[0] == siblings->name[0]) &&
806 (xmlStrEqual(node->name, siblings->name))) {
807 if ((sel->value2 == NULL) ||
808 ((siblings->ns != NULL) &&
809 (xmlStrEqual(sel->value2,
810 siblings->ns->href))))
814 siblings = siblings->next;
816 if ((parent == NULL) || (node->doc == NULL))
819 while (parent->parent != NULL)
820 parent = parent->parent;
821 if (((parent->type != XML_DOCUMENT_NODE) &&
822 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
823 (parent != (xmlNodePtr) node->doc))
828 ctxt->xpathCtxt->contextSize = len;
829 ctxt->xpathCtxt->proximityPosition = pos;
831 * If the node is in a Value Tree we cannot
834 if ((!isRVT) && (node->doc != NULL) &&
836 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
838 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
840 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) =
844 } else if ((sel != NULL) && (sel->op == XSLT_OP_ALL) &&
845 (node->type == XML_ELEMENT_NODE)) {
849 previous = (xmlNodePtr)
850 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr);
851 ix = XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival);
852 if ((previous != NULL) &&
853 (previous->parent == node->parent)) {
855 * just walk back to adjust the index
858 xmlNodePtr sibling = node;
860 while (sibling != NULL) {
861 if (sibling == previous)
863 if (sibling->type == XML_ELEMENT_NODE)
865 sibling = sibling->prev;
867 if (sibling == NULL) {
868 /* hum going backward in document order ... */
871 while (sibling != NULL) {
872 if (sibling == previous)
874 if (sibling->type == XML_ELEMENT_NODE)
876 sibling = sibling->next;
879 if (sibling != NULL) {
882 * If the node is in a Value Tree we cannot
885 if ((node->doc != NULL) && !isRVT) {
886 len = XSLT_RUNTIME_EXTRA(ctxt,
887 sel->lenExtra, ival);
888 XSLT_RUNTIME_EXTRA(ctxt,
889 sel->previousExtra, ptr) = node;
890 XSLT_RUNTIME_EXTRA(ctxt,
891 sel->indexExtra, ival) = pos;
897 * recompute the index
899 xmlNodePtr siblings = node->parent->children;
900 xmlNodePtr parent = node->parent;
902 while (siblings != NULL) {
903 if (siblings->type == XML_ELEMENT_NODE) {
905 if (siblings == node) {
909 siblings = siblings->next;
911 if ((parent == NULL) || (node->doc == NULL))
914 while (parent->parent != NULL)
915 parent = parent->parent;
916 if (((parent->type != XML_DOCUMENT_NODE) &&
917 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
918 (parent != (xmlNodePtr) node->doc))
923 ctxt->xpathCtxt->contextSize = len;
924 ctxt->xpathCtxt->proximityPosition = pos;
926 * If the node is in a Value Tree we cannot
929 if ((node->doc != NULL) && (nocache == 0) && !isRVT) {
930 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra, ptr) =
932 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra, ival) =
934 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra, ival) =
939 oldNode = ctxt->node;
942 if (step->value == NULL)
944 if (step->comp == NULL)
947 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
952 ctxt->xpathCtxt->contextSize = oldCS;
953 ctxt->xpathCtxt->proximityPosition = oldCP;
955 ctxt->node = oldNode;
959 ctxt->xpathCtxt->contextSize = oldCS;
960 ctxt->xpathCtxt->proximityPosition = oldCP;
962 ctxt->node = oldNode;
966 if (node->type != XML_PI_NODE)
968 if (step->value != NULL) {
969 if (!xmlStrEqual(step->value, node->name))
973 case XSLT_OP_COMMENT:
974 if (node->type != XML_COMMENT_NODE)
978 if ((node->type != XML_TEXT_NODE) &&
979 (node->type != XML_CDATA_SECTION_NODE))
983 switch (node->type) {
984 case XML_ELEMENT_NODE:
985 case XML_CDATA_SECTION_NODE:
987 case XML_COMMENT_NODE:
1000 * xsltTestCompMatchList:
1001 * @ctxt: a XSLT process context
1003 * @comp: the precompiled pattern list
1005 * Test whether the node matches one of the patterns in the list
1007 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
1010 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
1011 xsltCompMatchPtr comp) {
1014 if ((ctxt == NULL) || (node == NULL))
1016 while (comp != NULL) {
1017 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
1025 /************************************************************************
1027 * Dedicated parser for templates *
1029 ************************************************************************/
1031 #define CUR (*ctxt->cur)
1032 #define SKIP(val) ctxt->cur += (val)
1033 #define NXT(val) ctxt->cur[(val)]
1034 #define CUR_PTR ctxt->cur
1036 #define SKIP_BLANKS \
1037 while (IS_BLANK_CH(CUR)) NEXT
1039 #define CURRENT (*ctxt->cur)
1040 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1043 #define PUSH(op, val, val2) \
1044 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1047 xsltSwapTopCompMatch(ctxt->comp);
1049 #define XSLT_ERROR(X) \
1050 { xsltError(ctxt, __FILE__, __LINE__, X); \
1051 ctxt->error = (X); return; }
1053 #define XSLT_ERROR0(X) \
1054 { xsltError(ctxt, __FILE__, __LINE__, X); \
1055 ctxt->error = (X); return(0); }
1059 * @ctxt: the XPath Parser context
1061 * Parse an XPath Litteral:
1063 * [29] Literal ::= '"' [^"]* '"'
1066 * Returns the Literal parsed or NULL
1070 xsltScanLiteral(xsltParserContextPtr ctxt) {
1071 const xmlChar *q, *cur;
1072 xmlChar *ret = NULL;
1079 val = xmlStringCurrentChar(NULL, cur, &len);
1080 while ((IS_CHAR(val)) && (val != '"')) {
1082 val = xmlStringCurrentChar(NULL, cur, &len);
1084 if (!IS_CHAR(val)) {
1088 ret = xmlStrndup(q, cur - q);
1092 } else if (CUR == '\'') {
1095 val = xmlStringCurrentChar(NULL, cur, &len);
1096 while ((IS_CHAR(val)) && (val != '\'')) {
1098 val = xmlStringCurrentChar(NULL, cur, &len);
1100 if (!IS_CHAR(val)) {
1104 ret = xmlStrndup(q, cur - q);
1109 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1118 * @ctxt: the XPath Parser context
1120 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1121 * CombiningChar | Extender
1123 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1125 * [6] Names ::= Name (S Name)*
1127 * Returns the Name parsed or NULL
1131 xsltScanName(xsltParserContextPtr ctxt) {
1132 const xmlChar *q, *cur;
1133 xmlChar *ret = NULL;
1139 val = xmlStringCurrentChar(NULL, cur, &len);
1140 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1143 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1144 (val == '.') || (val == '-') ||
1146 (IS_COMBINING(val)) ||
1147 (IS_EXTENDER(val))) {
1149 val = xmlStringCurrentChar(NULL, cur, &len);
1151 ret = xmlStrndup(q, cur - q);
1158 * @ctxt: the XPath Parser context
1160 * Parses a non qualified name
1162 * Returns the Name parsed or NULL
1166 xsltScanNCName(xsltParserContextPtr ctxt) {
1167 const xmlChar *q, *cur;
1168 xmlChar *ret = NULL;
1174 val = xmlStringCurrentChar(NULL, cur, &len);
1175 if (!IS_LETTER(val) && (val != '_'))
1178 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1179 (val == '.') || (val == '-') ||
1181 (IS_COMBINING(val)) ||
1182 (IS_EXTENDER(val))) {
1184 val = xmlStringCurrentChar(NULL, cur, &len);
1186 ret = xmlStrndup(q, cur - q);
1193 * @ctxt: the XPath Parser context
1194 * @prefix: the place to store the prefix
1196 * Parse a qualified name
1198 * Returns the Name parsed or NULL
1202 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1203 xmlChar *ret = NULL;
1206 ret = xsltScanNCName(ctxt);
1210 ret = xsltScanNCName(ctxt);
1216 * xsltCompileIdKeyPattern:
1217 * @ctxt: the compilation context
1218 * @name: a preparsed name
1219 * @aid: whether id/key are allowed there
1221 * Compile the XSLT LocationIdKeyPattern
1222 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1223 * | 'key' '(' Literal ',' Literal ')'
1225 * also handle NodeType and PI from:
1227 * [7] NodeTest ::= NameTest
1228 * | NodeType '(' ')'
1229 * | 'processing-instruction' '(' Literal ')'
1232 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1233 xmlChar *lit = NULL;
1234 xmlChar *lit2 = NULL;
1237 xsltTransformError(NULL, NULL, NULL,
1238 "xsltCompileIdKeyPattern : ( expected\n");
1242 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1245 lit = xsltScanLiteral(ctxt);
1250 xsltTransformError(NULL, NULL, NULL,
1251 "xsltCompileIdKeyPattern : ) expected\n");
1256 PUSH(XSLT_OP_ID, lit, NULL);
1257 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1260 lit = xsltScanLiteral(ctxt);
1265 xsltTransformError(NULL, NULL, NULL,
1266 "xsltCompileIdKeyPattern : , expected\n");
1272 lit2 = xsltScanLiteral(ctxt);
1277 xsltTransformError(NULL, NULL, NULL,
1278 "xsltCompileIdKeyPattern : ) expected\n");
1283 /* TODO: support namespace in keys */
1284 PUSH(XSLT_OP_KEY, lit, lit2);
1285 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1289 lit = xsltScanLiteral(ctxt);
1294 xsltTransformError(NULL, NULL, NULL,
1295 "xsltCompileIdKeyPattern : ) expected\n");
1301 PUSH(XSLT_OP_PI, lit, NULL);
1302 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1306 xsltTransformError(NULL, NULL, NULL,
1307 "xsltCompileIdKeyPattern : ) expected\n");
1312 PUSH(XSLT_OP_TEXT, NULL, NULL);
1313 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1317 xsltTransformError(NULL, NULL, NULL,
1318 "xsltCompileIdKeyPattern : ) expected\n");
1323 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1324 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1328 xsltTransformError(NULL, NULL, NULL,
1329 "xsltCompileIdKeyPattern : ) expected\n");
1334 PUSH(XSLT_OP_NODE, NULL, NULL);
1336 xsltTransformError(NULL, NULL, NULL,
1337 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1341 xsltTransformError(NULL, NULL, NULL,
1342 "xsltCompileIdKeyPattern : node type\n");
1352 * xsltCompileStepPattern:
1353 * @ctxt: the compilation context
1354 * @token: a posible precompiled name
1356 * Compile the XSLT StepPattern and generates a precompiled
1357 * form suitable for fast matching.
1359 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1360 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1361 * | ('child' | 'attribute') '::'
1363 * [7] NodeTest ::= NameTest
1364 * | NodeType '(' ')'
1365 * | 'processing-instruction' '(' Literal ')'
1366 * [8] Predicate ::= '[' PredicateExpr ']'
1367 * [9] PredicateExpr ::= Expr
1368 * [13] AbbreviatedAxisSpecifier ::= '@'?
1369 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1373 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1374 xmlChar *name = NULL;
1375 const xmlChar *URI = NULL;
1376 xmlChar *URL = NULL;
1380 if ((token == NULL) && (CUR == '@')) {
1381 xmlChar *prefix = NULL;
1386 PUSH(XSLT_OP_ATTR, NULL, NULL);
1387 goto parse_predicate;
1389 token = xsltScanQName(ctxt, &prefix);
1390 if (prefix != NULL) {
1393 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1395 xsltTransformError(NULL, NULL, NULL,
1396 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1399 URL = xmlStrdup(ns->href);
1403 if (token == NULL) {
1406 PUSH(XSLT_OP_ATTR, NULL, URL);
1409 xsltTransformError(NULL, NULL, NULL,
1410 "xsltCompileStepPattern : Name expected\n");
1414 PUSH(XSLT_OP_ATTR, token, URL);
1415 goto parse_predicate;
1418 token = xsltScanName(ctxt);
1419 if (token == NULL) {
1422 PUSH(XSLT_OP_ALL, token, NULL);
1423 goto parse_predicate;
1425 xsltTransformError(NULL, NULL, NULL,
1426 "xsltCompileStepPattern : Name expected\n");
1435 xsltCompileIdKeyPattern(ctxt, token, 0);
1438 } else if (CUR == ':') {
1441 xmlChar *prefix = token;
1445 * This is a namespace match
1447 token = xsltScanName(ctxt);
1448 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1450 xsltTransformError(NULL, NULL, NULL,
1451 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1456 URL = xmlStrdup(ns->href);
1459 if (token == NULL) {
1462 PUSH(XSLT_OP_NS, URL, NULL);
1464 xsltTransformError(NULL, NULL, NULL,
1465 "xsltCompileStepPattern : Name expected\n");
1470 PUSH(XSLT_OP_ELEM, token, URL);
1474 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1476 token = xsltScanName(ctxt);
1477 if (token == NULL) {
1480 PUSH(XSLT_OP_ALL, token, NULL);
1481 goto parse_predicate;
1483 xsltTransformError(NULL, NULL, NULL,
1484 "xsltCompileStepPattern : QName expected\n");
1489 URI = xsltGetQNameURI(ctxt->elem, &token);
1490 if (token == NULL) {
1494 name = xmlStrdup(token);
1496 URL = xmlStrdup(URI);
1498 PUSH(XSLT_OP_CHILD, name, URL);
1499 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1501 token = xsltScanName(ctxt);
1502 if (token == NULL) {
1503 xsltTransformError(NULL, NULL, NULL,
1504 "xsltCompileStepPattern : QName expected\n");
1508 URI = xsltGetQNameURI(ctxt->elem, &token);
1509 if (token == NULL) {
1513 name = xmlStrdup(token);
1515 URL = xmlStrdup(URI);
1517 PUSH(XSLT_OP_ATTR, name, URL);
1519 xsltTransformError(NULL, NULL, NULL,
1520 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1526 } else if (CUR == '*') {
1528 PUSH(XSLT_OP_ALL, token, NULL);
1530 URI = xsltGetQNameURI(ctxt->elem, &token);
1531 if (token == NULL) {
1536 URL = xmlStrdup(URI);
1537 PUSH(XSLT_OP_ELEM, token, URL);
1542 while (CUR == '[') {
1544 xmlChar *ret = NULL;
1550 /* Skip over nested predicates */
1553 else if (CUR == ']') {
1557 } else if (CUR == '"') {
1559 while ((CUR != 0) && (CUR != '"'))
1561 } else if (CUR == '\'') {
1563 while ((CUR != 0) && (CUR != '\''))
1569 xsltTransformError(NULL, NULL, NULL,
1570 "xsltCompileStepPattern : ']' expected\n");
1574 ret = xmlStrndup(q, CUR_PTR - q);
1575 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1576 /* push the predicate lower than local test */
1590 * xsltCompileRelativePathPattern:
1591 * @comp: the compilation context
1592 * @token: a posible precompiled name
1594 * Compile the XSLT RelativePathPattern and generates a precompiled
1595 * form suitable for fast matching.
1597 * [4] RelativePathPattern ::= StepPattern
1598 * | RelativePathPattern '/' StepPattern
1599 * | RelativePathPattern '//' StepPattern
1602 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1603 xsltCompileStepPattern(ctxt, token);
1607 while ((CUR != 0) && (CUR != '|')) {
1608 if ((CUR == '/') && (NXT(1) == '/')) {
1609 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1613 xsltCompileStepPattern(ctxt, NULL);
1614 } else if (CUR == '/') {
1615 PUSH(XSLT_OP_PARENT, NULL, NULL);
1618 if ((CUR != 0) && (CUR != '|')) {
1619 xsltCompileRelativePathPattern(ctxt, NULL);
1633 * xsltCompileLocationPathPattern:
1634 * @ctxt: the compilation context
1636 * Compile the XSLT LocationPathPattern and generates a precompiled
1637 * form suitable for fast matching.
1639 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1640 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1641 * | '//'? RelativePathPattern
1644 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1646 if ((CUR == '/') && (NXT(1) == '/')) {
1648 * since we reverse the query
1649 * a leading // can be safely ignored
1653 ctxt->comp->priority = 0.5; /* '//' means not 0 priority */
1654 xsltCompileRelativePathPattern(ctxt, NULL);
1655 } else if (CUR == '/') {
1657 * We need to find root as the parent
1661 PUSH(XSLT_OP_ROOT, NULL, NULL);
1662 if ((CUR != 0) && (CUR != '|')) {
1663 PUSH(XSLT_OP_PARENT, NULL, NULL);
1664 xsltCompileRelativePathPattern(ctxt, NULL);
1666 } else if (CUR == '*') {
1667 xsltCompileRelativePathPattern(ctxt, NULL);
1668 } else if (CUR == '@') {
1669 xsltCompileRelativePathPattern(ctxt, NULL);
1672 name = xsltScanName(ctxt);
1674 xsltTransformError(NULL, NULL, NULL,
1675 "xsltCompileLocationPathPattern : Name expected\n");
1680 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1681 xsltCompileIdKeyPattern(ctxt, name, 1);
1682 if ((CUR == '/') && (NXT(1) == '/')) {
1683 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1687 xsltCompileRelativePathPattern(ctxt, NULL);
1688 } else if (CUR == '/') {
1689 PUSH(XSLT_OP_PARENT, NULL, NULL);
1692 xsltCompileRelativePathPattern(ctxt, NULL);
1696 xsltCompileRelativePathPattern(ctxt, name);
1703 * xsltCompilePattern:
1704 * @pattern: an XSLT pattern
1705 * @doc: the containing document
1706 * @node: the containing element
1707 * @style: the stylesheet
1708 * @runtime: the transformation context, if done at run-time
1710 * Compile the XSLT pattern and generates a list of precompiled form suitable
1711 * for fast matching.
1713 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1715 * Returns the generated pattern list or NULL in case of failure
1719 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1720 xmlNodePtr node, xsltStylesheetPtr style,
1721 xsltTransformContextPtr runtime) {
1722 xsltParserContextPtr ctxt = NULL;
1723 xsltCompMatchPtr element, first = NULL, previous = NULL;
1724 int current, start, end, level, j;
1726 if (pattern == NULL) {
1727 xsltTransformError(NULL, NULL, node,
1728 "xsltCompilePattern : NULL pattern\n");
1732 ctxt = xsltNewParserContext(style, runtime);
1738 while (pattern[current] != 0) {
1740 while (IS_BLANK_CH(pattern[current]))
1744 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1745 if (pattern[end] == '[')
1747 else if (pattern[end] == ']')
1749 else if (pattern[end] == '\'') {
1751 while ((pattern[end] != 0) && (pattern[end] != '\''))
1753 } else if (pattern[end] == '"') {
1755 while ((pattern[end] != 0) && (pattern[end] != '"'))
1760 if (current == end) {
1761 xsltTransformError(NULL, NULL, node,
1762 "xsltCompilePattern : NULL pattern\n");
1765 element = xsltNewCompMatch();
1766 if (element == NULL) {
1771 else if (previous != NULL)
1772 previous->next = element;
1775 ctxt->comp = element;
1776 ctxt->base = xmlStrndup(&pattern[start], end - start);
1777 if (ctxt->base == NULL)
1779 ctxt->cur = &(ctxt->base)[current - start];
1780 element->pattern = ctxt->base;
1781 element->nsList = xmlGetNsList(doc, node);
1783 if (element->nsList != NULL) {
1784 while (element->nsList[j] != NULL)
1790 #ifdef WITH_XSLT_DEBUG_PATTERN
1791 xsltGenericDebug(xsltGenericDebugContext,
1792 "xsltCompilePattern : parsing '%s'\n",
1796 Preset default priority to be zero.
1797 This may be changed by xsltCompileLocationPathPattern.
1799 element->priority = 0;
1800 xsltCompileLocationPathPattern(ctxt);
1802 xsltTransformError(NULL, style, node,
1803 "xsltCompilePattern : failed to compile '%s'\n",
1805 if (style != NULL) style->errors++;
1810 * Reverse for faster interpretation.
1812 xsltReverseCompMatch(element);
1815 * Set-up the priority
1817 if (element->priority == 0) { /* if not yet determined */
1818 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1819 (element->steps[0].op == XSLT_OP_ATTR) ||
1820 (element->steps[0].op == XSLT_OP_PI)) &&
1821 (element->steps[0].value != NULL) &&
1822 (element->steps[1].op == XSLT_OP_END)) {
1823 ; /* previously preset */
1824 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1825 (element->steps[0].value2 != NULL) &&
1826 (element->steps[1].op == XSLT_OP_END)) {
1827 element->priority = -0.25;
1828 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1829 (element->steps[0].value != NULL) &&
1830 (element->steps[1].op == XSLT_OP_END)) {
1831 element->priority = -0.25;
1832 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1833 (element->steps[0].value == NULL) &&
1834 (element->steps[0].value2 == NULL) &&
1835 (element->steps[1].op == XSLT_OP_END)) {
1836 element->priority = -0.5;
1837 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1838 (element->steps[0].op == XSLT_OP_TEXT) ||
1839 (element->steps[0].op == XSLT_OP_ALL) ||
1840 (element->steps[0].op == XSLT_OP_NODE) ||
1841 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1842 (element->steps[1].op == XSLT_OP_END)) {
1843 element->priority = -0.5;
1845 element->priority = 0.5;
1848 #ifdef WITH_XSLT_DEBUG_PATTERN
1849 xsltGenericDebug(xsltGenericDebugContext,
1850 "xsltCompilePattern : parsed %s, default priority %f\n",
1851 element->pattern, element->priority);
1853 if (pattern[end] == '|')
1858 xsltTransformError(NULL, style, node,
1859 "xsltCompilePattern : NULL pattern\n");
1860 if (style != NULL) style->errors++;
1864 xsltFreeParserContext(ctxt);
1869 xsltFreeParserContext(ctxt);
1871 xsltFreeCompMatchList(first);
1875 /************************************************************************
1877 * Module interfaces *
1879 ************************************************************************/
1883 * @style: an XSLT stylesheet
1884 * @cur: an XSLT template
1885 * @mode: the mode name or NULL
1886 * @modeURI: the mode URI or NULL
1888 * Register the XSLT pattern associated to @cur
1890 * Returns -1 in case of error, 0 otherwise
1893 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1894 const xmlChar *mode, const xmlChar *modeURI) {
1895 xsltCompMatchPtr pat, list, *top = NULL, next;
1896 const xmlChar *name = NULL;
1897 float priority; /* the priority */
1899 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1902 priority = cur->priority;
1903 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1909 pat->template = cur;
1911 pat->mode = xmlStrdup(mode);
1912 if (modeURI != NULL)
1913 pat->modeURI = xmlStrdup(modeURI);
1914 if (priority != XSLT_PAT_NO_PRIORITY)
1915 pat->priority = priority;
1918 * insert it in the hash table list corresponding to its lookup name
1920 switch (pat->steps[0].op) {
1922 if (pat->steps[0].value != NULL)
1923 name = pat->steps[0].value;
1925 top = (xsltCompMatchPtr *) &(style->attrMatch);
1928 case XSLT_OP_PARENT:
1929 case XSLT_OP_ANCESTOR:
1930 top = (xsltCompMatchPtr *) &(style->elemMatch);
1933 top = (xsltCompMatchPtr *) &(style->rootMatch);
1936 top = (xsltCompMatchPtr *) &(style->keyMatch);
1939 /* TODO optimize ID !!! */
1942 top = (xsltCompMatchPtr *) &(style->elemMatch);
1945 case XSLT_OP_PREDICATE:
1946 xsltTransformError(NULL, style, NULL,
1947 "xsltAddTemplate: invalid compiled pattern\n");
1948 xsltFreeCompMatch(pat);
1951 * TODO: some flags at the top level about type based patterns
1952 * would be faster than inclusion in the hash table.
1955 if (pat->steps[0].value != NULL)
1956 name = pat->steps[0].value;
1958 top = (xsltCompMatchPtr *) &(style->piMatch);
1960 case XSLT_OP_COMMENT:
1961 top = (xsltCompMatchPtr *) &(style->commentMatch);
1964 top = (xsltCompMatchPtr *) &(style->textMatch);
1968 if (pat->steps[0].value != NULL)
1969 name = pat->steps[0].value;
1971 top = (xsltCompMatchPtr *) &(style->elemMatch);
1975 if (style->templatesHash == NULL) {
1976 style->templatesHash = xmlHashCreate(1024);
1977 if (style->templatesHash == NULL) {
1978 xsltFreeCompMatch(pat);
1981 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1983 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1984 name, mode, modeURI);
1986 xmlHashAddEntry3(style->templatesHash, name,
1987 mode, modeURI, pat);
1990 * Note '<=' since one must choose among the matching
1991 * template rules that are left, the one that occurs
1992 * last in the stylesheet
1994 if (list->priority <= pat->priority) {
1996 xmlHashUpdateEntry3(style->templatesHash, name,
1997 mode, modeURI, pat, NULL);
1999 while (list->next != NULL) {
2000 if (list->next->priority <= pat->priority)
2004 pat->next = list->next;
2009 } else if (top != NULL) {
2014 } else if (list->priority <= pat->priority) {
2018 while (list->next != NULL) {
2019 if (list->next->priority <= pat->priority)
2023 pat->next = list->next;
2027 xsltTransformError(NULL, style, NULL,
2028 "xsltAddTemplate: invalid compiled pattern\n");
2029 xsltFreeCompMatch(pat);
2032 #ifdef WITH_XSLT_DEBUG_PATTERN
2034 xsltGenericDebug(xsltGenericDebugContext,
2035 "added pattern : '%s' mode '%s' priority %f\n",
2036 pat->pattern, pat->mode, pat->priority);
2038 xsltGenericDebug(xsltGenericDebugContext,
2039 "added pattern : '%s' priority %f\n",
2040 pat->pattern, pat->priority);
2050 * @ctxt: a XSLT process context
2051 * @node: the node being processed
2052 * @style: the current style
2054 * Finds the template applying to this node, if @style is non-NULL
2055 * it means one needs to look for the next imported template in scope.
2057 * Returns the xsltTemplatePtr or NULL if not found
2060 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2061 xsltStylesheetPtr style) {
2062 xsltStylesheetPtr curstyle;
2063 xsltTemplatePtr ret = NULL;
2064 const xmlChar *name = NULL;
2065 xsltCompMatchPtr list = NULL;
2069 if ((ctxt == NULL) || (node == NULL))
2072 if (style == NULL) {
2073 curstyle = ctxt->style;
2075 curstyle = xsltNextImport(style);
2078 while ((curstyle != NULL) && (curstyle != style)) {
2079 priority = XSLT_PAT_NO_PRIORITY;
2080 /* TODO : handle IDs/keys here ! */
2081 if (curstyle->templatesHash != NULL) {
2083 * Use the top name as selector
2085 switch (node->type) {
2086 case XML_ELEMENT_NODE:
2087 if (node->name[0] == ' ')
2089 case XML_ATTRIBUTE_NODE:
2093 case XML_DOCUMENT_NODE:
2094 case XML_HTML_DOCUMENT_NODE:
2096 case XML_CDATA_SECTION_NODE:
2097 case XML_COMMENT_NODE:
2098 case XML_ENTITY_REF_NODE:
2099 case XML_ENTITY_NODE:
2100 case XML_DOCUMENT_TYPE_NODE:
2101 case XML_DOCUMENT_FRAG_NODE:
2102 case XML_NOTATION_NODE:
2104 case XML_ELEMENT_DECL:
2105 case XML_ATTRIBUTE_DECL:
2106 case XML_ENTITY_DECL:
2107 case XML_NAMESPACE_DECL:
2108 case XML_XINCLUDE_START:
2109 case XML_XINCLUDE_END:
2118 * find the list of applicable expressions based on the name
2120 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2121 name, ctxt->mode, ctxt->modeURI);
2124 while (list != NULL) {
2125 if (xsltTestCompMatch(ctxt, list, node,
2126 ctxt->mode, ctxt->modeURI)) {
2127 ret = list->template;
2128 priority = list->priority;
2136 * find alternate generic matches
2138 switch (node->type) {
2139 case XML_ELEMENT_NODE:
2140 if (node->name[0] == ' ')
2141 list = curstyle->rootMatch;
2143 list = curstyle->elemMatch;
2144 if (node->psvi != NULL) keyed = 1;
2146 case XML_ATTRIBUTE_NODE: {
2149 list = curstyle->attrMatch;
2150 attr = (xmlAttrPtr) node;
2151 if (attr->psvi != NULL) keyed = 1;
2155 list = curstyle->piMatch;
2156 if (node->psvi != NULL) keyed = 1;
2158 case XML_DOCUMENT_NODE:
2159 case XML_HTML_DOCUMENT_NODE: {
2162 list = curstyle->rootMatch;
2163 doc = (xmlDocPtr) node;
2164 if (doc->psvi != NULL) keyed = 1;
2168 case XML_CDATA_SECTION_NODE:
2169 list = curstyle->textMatch;
2170 if (node->psvi != NULL) keyed = 1;
2172 case XML_COMMENT_NODE:
2173 list = curstyle->commentMatch;
2174 if (node->psvi != NULL) keyed = 1;
2176 case XML_ENTITY_REF_NODE:
2177 case XML_ENTITY_NODE:
2178 case XML_DOCUMENT_TYPE_NODE:
2179 case XML_DOCUMENT_FRAG_NODE:
2180 case XML_NOTATION_NODE:
2182 case XML_ELEMENT_DECL:
2183 case XML_ATTRIBUTE_DECL:
2184 case XML_ENTITY_DECL:
2185 case XML_NAMESPACE_DECL:
2186 case XML_XINCLUDE_START:
2187 case XML_XINCLUDE_END:
2192 while ((list != NULL) &&
2193 ((ret == NULL) || (list->priority > priority))) {
2194 if (xsltTestCompMatch(ctxt, list, node,
2195 ctxt->mode, ctxt->modeURI)) {
2196 ret = list->template;
2197 priority = list->priority;
2203 * Some of the tests for elements can also apply to documents
2205 if ((node->type == XML_DOCUMENT_NODE) ||
2206 (node->type == XML_HTML_DOCUMENT_NODE) ||
2207 (node->type == XML_TEXT_NODE)) {
2208 list = curstyle->elemMatch;
2209 while ((list != NULL) &&
2210 ((ret == NULL) || (list->priority > priority))) {
2211 if (xsltTestCompMatch(ctxt, list, node,
2212 ctxt->mode, ctxt->modeURI)) {
2213 ret = list->template;
2214 priority = list->priority;
2219 } else if ((node->type == XML_PI_NODE) ||
2220 (node->type == XML_COMMENT_NODE)) {
2221 list = curstyle->elemMatch;
2222 while ((list != NULL) &&
2223 ((ret == NULL) || (list->priority > priority))) {
2224 if (xsltTestCompMatch(ctxt, list, node,
2225 ctxt->mode, ctxt->modeURI)) {
2226 ret = list->template;
2227 priority = list->priority;
2235 list = curstyle->keyMatch;
2236 while ((list != NULL) &&
2237 ((ret == NULL) || (list->priority > priority))) {
2238 if (xsltTestCompMatch(ctxt, list, node,
2239 ctxt->mode, ctxt->modeURI)) {
2240 ret = list->template;
2241 priority = list->priority;
2251 * Cycle on next curstylesheet import.
2253 curstyle = xsltNextImport(curstyle);
2259 * xsltCleanupTemplates:
2260 * @style: an XSLT stylesheet
2262 * Cleanup the state of the templates used by the stylesheet and
2263 * the ones it imports.
2266 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2270 * xsltFreeTemplateHashes:
2271 * @style: an XSLT stylesheet
2273 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2276 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2277 if (style->templatesHash != NULL)
2278 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2279 (xmlHashDeallocator) xsltFreeCompMatchList);
2280 if (style->rootMatch != NULL)
2281 xsltFreeCompMatchList(style->rootMatch);
2282 if (style->keyMatch != NULL)
2283 xsltFreeCompMatchList(style->keyMatch);
2284 if (style->elemMatch != NULL)
2285 xsltFreeCompMatchList(style->elemMatch);
2286 if (style->attrMatch != NULL)
2287 xsltFreeCompMatchList(style->attrMatch);
2288 if (style->parentMatch != NULL)
2289 xsltFreeCompMatchList(style->parentMatch);
2290 if (style->textMatch != NULL)
2291 xsltFreeCompMatchList(style->textMatch);
2292 if (style->piMatch != NULL)
2293 xsltFreeCompMatchList(style->piMatch);
2294 if (style->commentMatch != NULL)
2295 xsltFreeCompMatchList(style->commentMatch);