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, 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)
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: {
612 int pos = 0, len = 0;
617 (doc->name != NULL) &&
618 (doc->name[0] == ' ') &&
619 (xmlStrEqual(BAD_CAST doc->name,
620 BAD_CAST " fake node libxslt")))
625 * The simple existing predicate code cannot handle
626 * properly cascaded predicates. If in this situation
627 * compute directly the full node list once and check
628 * if the node is in the result list.
630 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
632 xmlXPathObjectPtr list;
636 prevdoc = (xmlDocPtr)
637 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra);
639 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra);
640 list = (xmlXPathObjectPtr)
641 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra);
643 if ((list == NULL) || (prevdoc != doc)) {
645 xmlXPathObjectPtr newlist;
646 xmlNodePtr parent = node->parent;
650 if (comp->pattern[0] == '/')
651 query = xmlStrdup(comp->pattern);
653 query = xmlStrdup((const xmlChar *)"//");
654 query = xmlStrcat(query, comp->pattern);
656 oldnode = ctxt->xpathCtxt->node;
657 olddoc = ctxt->xpathCtxt->doc;
658 ctxt->xpathCtxt->node = node;
659 ctxt->xpathCtxt->doc = doc;
660 newlist = xmlXPathEval(query, ctxt->xpathCtxt);
661 ctxt->xpathCtxt->node = oldnode;
662 ctxt->xpathCtxt->doc = olddoc;
666 if (newlist->type != XPATH_NODESET) {
667 xmlXPathFreeObject(newlist);
672 if ((parent == NULL) || (node->doc == NULL) || isRVT)
677 xmlXPathFreeObject(list);
680 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->lenExtra) =
682 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra) =
684 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra) =
686 XSLT_RUNTIME_EXTRA_FREE(ctxt, sel->lenExtra) =
687 (xmlFreeFunc) xmlXPathFreeObject;
691 if ((list->nodesetval == NULL) ||
692 (list->nodesetval->nodeNr <= 0)) {
694 xmlXPathFreeObject(list);
697 /* TODO: store the index and use it for the scan */
699 for (j = 0;j < list->nodesetval->nodeNr;j++) {
700 if (list->nodesetval->nodeTab[j] == node) {
702 xmlXPathFreeObject(list);
709 xmlXPathFreeObject(list);
713 * Depending on the last selection, one may need to
714 * recompute contextSize and proximityPosition.
716 * TODO: make this thread safe !
718 oldCS = ctxt->xpathCtxt->contextSize;
719 oldCP = ctxt->xpathCtxt->proximityPosition;
721 (sel->op == XSLT_OP_ELEM) &&
722 (sel->value != NULL) &&
723 (node->type == XML_ELEMENT_NODE) &&
724 (node->parent != NULL)) {
728 previous = (xmlNodePtr)
729 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra);
731 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra);
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) {
780 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra);
782 XSLT_RUNTIME_EXTRA(ctxt,
783 sel->previousExtra) = node;
784 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra) =
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) =
838 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra) =
840 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra) =
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);
852 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra);
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) {
888 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra);
889 XSLT_RUNTIME_EXTRA(ctxt,
890 sel->previousExtra) = node;
891 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra) =
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) =
933 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra) =
935 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra) =
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 wether 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 = xmlStrdup(mode);
1913 if (modeURI != NULL)
1914 pat->modeURI = xmlStrdup(modeURI);
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 appliable 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);