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: {
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, sel->previousExtra);
627 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra);
628 list = (xmlXPathObjectPtr)
629 XSLT_RUNTIME_EXTRA_LST(ctxt, sel->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, sel->lenExtra) =
678 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra) =
680 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra) =
682 XSLT_RUNTIME_EXTRA_FREE(ctxt, sel->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;
717 (sel->op == XSLT_OP_ELEM) &&
718 (sel->value != NULL) &&
719 (node->type == XML_ELEMENT_NODE) &&
720 (node->parent != NULL)) {
724 previous = (xmlNodePtr)
725 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra);
727 XSLT_RUNTIME_EXTRA(ctxt, sel->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 ((sel->value2 == NULL) ||
745 ((sibling->ns != NULL) &&
746 (xmlStrEqual(sel->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 ((sel->value2 == NULL) ||
760 ((sibling->ns != NULL) &&
761 (xmlStrEqual(sel->value2,
762 sibling->ns->href))))
764 sibling = sibling->next;
767 if (sibling != NULL) {
770 * If the node is in a Value Tree we need to
771 * save len, but cannot cache the node!
772 * (bugs 153137 and 158840)
774 if (node->doc != NULL) {
776 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra);
777 if ((node->doc->name != NULL) &&
778 (node->doc->name[0] != ' ')) {
779 XSLT_RUNTIME_EXTRA(ctxt,
780 sel->previousExtra) = node;
781 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra) =
790 * recompute the index
792 xmlNodePtr siblings = node->parent->children;
793 xmlNodePtr parent = node->parent;
795 while (siblings != NULL) {
796 if (siblings->type == XML_ELEMENT_NODE) {
797 if (siblings == node) {
800 } else if ((node->name != NULL) &&
801 (siblings->name != NULL) &&
802 (node->name[0] == siblings->name[0]) &&
803 (xmlStrEqual(node->name, siblings->name))) {
804 if ((sel->value2 == NULL) ||
805 ((siblings->ns != NULL) &&
806 (xmlStrEqual(sel->value2,
807 siblings->ns->href))))
811 siblings = siblings->next;
813 if ((parent == NULL) || (node->doc == NULL))
816 while (parent->parent != NULL)
817 parent = parent->parent;
818 if (((parent->type != XML_DOCUMENT_NODE) &&
819 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
820 (parent != (xmlNodePtr) node->doc))
825 ctxt->xpathCtxt->contextSize = len;
826 ctxt->xpathCtxt->proximityPosition = pos;
828 * If the node is in a Value Tree we cannot
831 if ((node->doc != NULL) && (nocache == 0)) {
832 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra) =
834 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra) =
836 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra) =
840 } else if ((sel != NULL) && (sel->op == XSLT_OP_ALL) &&
841 (node->type == XML_ELEMENT_NODE)) {
845 previous = (xmlNodePtr)
846 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra);
848 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra);
849 if ((previous != NULL) &&
850 (previous->parent == node->parent)) {
852 * just walk back to adjust the index
855 xmlNodePtr sibling = node;
857 while (sibling != NULL) {
858 if (sibling == previous)
860 if (sibling->type == XML_ELEMENT_NODE)
862 sibling = sibling->prev;
864 if (sibling == NULL) {
865 /* hum going backward in document order ... */
868 while (sibling != NULL) {
869 if (sibling == previous)
871 if (sibling->type == XML_ELEMENT_NODE)
873 sibling = sibling->next;
876 if (sibling != NULL) {
879 * If the node is in a Value Tree we cannot
882 if ((node->doc != NULL) &&
883 (node->doc->name != NULL) &&
884 (node->doc->name[0] != ' ')) {
886 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra);
887 XSLT_RUNTIME_EXTRA(ctxt,
888 sel->previousExtra) = node;
889 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra) =
896 * recompute the index
898 xmlNodePtr siblings = node->parent->children;
899 xmlNodePtr parent = node->parent;
901 while (siblings != NULL) {
902 if (siblings->type == XML_ELEMENT_NODE) {
904 if (siblings == node) {
908 siblings = siblings->next;
910 if ((parent == NULL) || (node->doc == NULL))
913 while (parent->parent != NULL)
914 parent = parent->parent;
915 if (((parent->type != XML_DOCUMENT_NODE) &&
916 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
917 (parent != (xmlNodePtr) node->doc))
922 ctxt->xpathCtxt->contextSize = len;
923 ctxt->xpathCtxt->proximityPosition = pos;
925 * If the node is in a Value Tree we cannot
928 if ((node->doc != NULL) && (nocache == 0)) {
929 XSLT_RUNTIME_EXTRA(ctxt, sel->previousExtra) =
931 XSLT_RUNTIME_EXTRA(ctxt, sel->indexExtra) =
933 XSLT_RUNTIME_EXTRA(ctxt, sel->lenExtra) =
938 oldNode = ctxt->node;
941 if (step->value == NULL)
943 if (step->comp == NULL)
946 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
951 ctxt->xpathCtxt->contextSize = oldCS;
952 ctxt->xpathCtxt->proximityPosition = oldCP;
954 ctxt->node = oldNode;
958 ctxt->xpathCtxt->contextSize = oldCS;
959 ctxt->xpathCtxt->proximityPosition = oldCP;
961 ctxt->node = oldNode;
965 if (node->type != XML_PI_NODE)
967 if (step->value != NULL) {
968 if (!xmlStrEqual(step->value, node->name))
972 case XSLT_OP_COMMENT:
973 if (node->type != XML_COMMENT_NODE)
977 if ((node->type != XML_TEXT_NODE) &&
978 (node->type != XML_CDATA_SECTION_NODE))
982 switch (node->type) {
983 case XML_ELEMENT_NODE:
984 case XML_CDATA_SECTION_NODE:
986 case XML_COMMENT_NODE:
999 * xsltTestCompMatchList:
1000 * @ctxt: a XSLT process context
1002 * @comp: the precompiled pattern list
1004 * Test wether the node matches one of the patterns in the list
1006 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
1009 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
1010 xsltCompMatchPtr comp) {
1013 if ((ctxt == NULL) || (node == NULL))
1015 while (comp != NULL) {
1016 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
1024 /************************************************************************
1026 * Dedicated parser for templates *
1028 ************************************************************************/
1030 #define CUR (*ctxt->cur)
1031 #define SKIP(val) ctxt->cur += (val)
1032 #define NXT(val) ctxt->cur[(val)]
1033 #define CUR_PTR ctxt->cur
1035 #define SKIP_BLANKS \
1036 while (IS_BLANK_CH(CUR)) NEXT
1038 #define CURRENT (*ctxt->cur)
1039 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1042 #define PUSH(op, val, val2) \
1043 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1046 xsltSwapTopCompMatch(ctxt->comp);
1048 #define XSLT_ERROR(X) \
1049 { xsltError(ctxt, __FILE__, __LINE__, X); \
1050 ctxt->error = (X); return; }
1052 #define XSLT_ERROR0(X) \
1053 { xsltError(ctxt, __FILE__, __LINE__, X); \
1054 ctxt->error = (X); return(0); }
1058 * @ctxt: the XPath Parser context
1060 * Parse an XPath Litteral:
1062 * [29] Literal ::= '"' [^"]* '"'
1065 * Returns the Literal parsed or NULL
1069 xsltScanLiteral(xsltParserContextPtr ctxt) {
1070 const xmlChar *q, *cur;
1071 xmlChar *ret = NULL;
1078 val = xmlStringCurrentChar(NULL, cur, &len);
1079 while ((IS_CHAR(val)) && (val != '"')) {
1081 val = xmlStringCurrentChar(NULL, cur, &len);
1083 if (!IS_CHAR(val)) {
1087 ret = xmlStrndup(q, cur - q);
1091 } else if (CUR == '\'') {
1094 val = xmlStringCurrentChar(NULL, cur, &len);
1095 while ((IS_CHAR(val)) && (val != '\'')) {
1097 val = xmlStringCurrentChar(NULL, cur, &len);
1099 if (!IS_CHAR(val)) {
1103 ret = xmlStrndup(q, cur - q);
1108 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1117 * @ctxt: the XPath Parser context
1119 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1120 * CombiningChar | Extender
1122 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1124 * [6] Names ::= Name (S Name)*
1126 * Returns the Name parsed or NULL
1130 xsltScanName(xsltParserContextPtr ctxt) {
1131 const xmlChar *q, *cur;
1132 xmlChar *ret = NULL;
1138 val = xmlStringCurrentChar(NULL, cur, &len);
1139 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1142 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1143 (val == '.') || (val == '-') ||
1145 (IS_COMBINING(val)) ||
1146 (IS_EXTENDER(val))) {
1148 val = xmlStringCurrentChar(NULL, cur, &len);
1150 ret = xmlStrndup(q, cur - q);
1157 * @ctxt: the XPath Parser context
1159 * Parses a non qualified name
1161 * Returns the Name parsed or NULL
1165 xsltScanNCName(xsltParserContextPtr ctxt) {
1166 const xmlChar *q, *cur;
1167 xmlChar *ret = NULL;
1173 val = xmlStringCurrentChar(NULL, cur, &len);
1174 if (!IS_LETTER(val) && (val != '_'))
1177 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1178 (val == '.') || (val == '-') ||
1180 (IS_COMBINING(val)) ||
1181 (IS_EXTENDER(val))) {
1183 val = xmlStringCurrentChar(NULL, cur, &len);
1185 ret = xmlStrndup(q, cur - q);
1192 * @ctxt: the XPath Parser context
1193 * @prefix: the place to store the prefix
1195 * Parse a qualified name
1197 * Returns the Name parsed or NULL
1201 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1202 xmlChar *ret = NULL;
1205 ret = xsltScanNCName(ctxt);
1209 ret = xsltScanNCName(ctxt);
1215 * xsltCompileIdKeyPattern:
1216 * @ctxt: the compilation context
1217 * @name: a preparsed name
1218 * @aid: whether id/key are allowed there
1220 * Compile the XSLT LocationIdKeyPattern
1221 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1222 * | 'key' '(' Literal ',' Literal ')'
1224 * also handle NodeType and PI from:
1226 * [7] NodeTest ::= NameTest
1227 * | NodeType '(' ')'
1228 * | 'processing-instruction' '(' Literal ')'
1231 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1232 xmlChar *lit = NULL;
1233 xmlChar *lit2 = NULL;
1236 xsltTransformError(NULL, NULL, NULL,
1237 "xsltCompileIdKeyPattern : ( expected\n");
1241 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1244 lit = xsltScanLiteral(ctxt);
1249 xsltTransformError(NULL, NULL, NULL,
1250 "xsltCompileIdKeyPattern : ) expected\n");
1255 PUSH(XSLT_OP_ID, lit, NULL);
1256 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1259 lit = xsltScanLiteral(ctxt);
1264 xsltTransformError(NULL, NULL, NULL,
1265 "xsltCompileIdKeyPattern : , expected\n");
1271 lit2 = xsltScanLiteral(ctxt);
1276 xsltTransformError(NULL, NULL, NULL,
1277 "xsltCompileIdKeyPattern : ) expected\n");
1282 /* TODO: support namespace in keys */
1283 PUSH(XSLT_OP_KEY, lit, lit2);
1284 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1288 lit = xsltScanLiteral(ctxt);
1293 xsltTransformError(NULL, NULL, NULL,
1294 "xsltCompileIdKeyPattern : ) expected\n");
1300 PUSH(XSLT_OP_PI, lit, NULL);
1301 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1305 xsltTransformError(NULL, NULL, NULL,
1306 "xsltCompileIdKeyPattern : ) expected\n");
1311 PUSH(XSLT_OP_TEXT, NULL, NULL);
1312 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1316 xsltTransformError(NULL, NULL, NULL,
1317 "xsltCompileIdKeyPattern : ) expected\n");
1322 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1323 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1327 xsltTransformError(NULL, NULL, NULL,
1328 "xsltCompileIdKeyPattern : ) expected\n");
1333 PUSH(XSLT_OP_NODE, NULL, NULL);
1335 xsltTransformError(NULL, NULL, NULL,
1336 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1340 xsltTransformError(NULL, NULL, NULL,
1341 "xsltCompileIdKeyPattern : node type\n");
1351 * xsltCompileStepPattern:
1352 * @ctxt: the compilation context
1353 * @token: a posible precompiled name
1355 * Compile the XSLT StepPattern and generates a precompiled
1356 * form suitable for fast matching.
1358 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1359 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1360 * | ('child' | 'attribute') '::'
1362 * [7] NodeTest ::= NameTest
1363 * | NodeType '(' ')'
1364 * | 'processing-instruction' '(' Literal ')'
1365 * [8] Predicate ::= '[' PredicateExpr ']'
1366 * [9] PredicateExpr ::= Expr
1367 * [13] AbbreviatedAxisSpecifier ::= '@'?
1368 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1372 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1373 xmlChar *name = NULL;
1374 const xmlChar *URI = NULL;
1375 xmlChar *URL = NULL;
1379 if ((token == NULL) && (CUR == '@')) {
1380 xmlChar *prefix = NULL;
1385 PUSH(XSLT_OP_ATTR, NULL, NULL);
1386 goto parse_predicate;
1388 token = xsltScanQName(ctxt, &prefix);
1389 if (prefix != NULL) {
1392 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1394 xsltTransformError(NULL, NULL, NULL,
1395 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1398 URL = xmlStrdup(ns->href);
1402 if (token == NULL) {
1405 PUSH(XSLT_OP_ATTR, NULL, URL);
1408 xsltTransformError(NULL, NULL, NULL,
1409 "xsltCompileStepPattern : Name expected\n");
1413 PUSH(XSLT_OP_ATTR, token, URL);
1414 goto parse_predicate;
1417 token = xsltScanName(ctxt);
1418 if (token == NULL) {
1421 PUSH(XSLT_OP_ALL, token, NULL);
1422 goto parse_predicate;
1424 xsltTransformError(NULL, NULL, NULL,
1425 "xsltCompileStepPattern : Name expected\n");
1434 xsltCompileIdKeyPattern(ctxt, token, 0);
1437 } else if (CUR == ':') {
1440 xmlChar *prefix = token;
1444 * This is a namespace match
1446 token = xsltScanName(ctxt);
1447 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1449 xsltTransformError(NULL, NULL, NULL,
1450 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1455 URL = xmlStrdup(ns->href);
1458 if (token == NULL) {
1461 PUSH(XSLT_OP_NS, URL, NULL);
1463 xsltTransformError(NULL, NULL, NULL,
1464 "xsltCompileStepPattern : Name expected\n");
1469 PUSH(XSLT_OP_ELEM, token, URL);
1473 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1475 token = xsltScanName(ctxt);
1476 if (token == NULL) {
1479 PUSH(XSLT_OP_ALL, token, NULL);
1480 goto parse_predicate;
1482 xsltTransformError(NULL, NULL, NULL,
1483 "xsltCompileStepPattern : QName expected\n");
1488 URI = xsltGetQNameURI(ctxt->elem, &token);
1489 if (token == NULL) {
1493 name = xmlStrdup(token);
1495 URL = xmlStrdup(URI);
1497 PUSH(XSLT_OP_CHILD, name, URL);
1498 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1500 token = xsltScanName(ctxt);
1501 if (token == NULL) {
1502 xsltTransformError(NULL, NULL, NULL,
1503 "xsltCompileStepPattern : QName expected\n");
1507 URI = xsltGetQNameURI(ctxt->elem, &token);
1508 if (token == NULL) {
1512 name = xmlStrdup(token);
1514 URL = xmlStrdup(URI);
1516 PUSH(XSLT_OP_ATTR, name, URL);
1518 xsltTransformError(NULL, NULL, NULL,
1519 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1525 } else if (CUR == '*') {
1527 PUSH(XSLT_OP_ALL, token, NULL);
1529 URI = xsltGetQNameURI(ctxt->elem, &token);
1530 if (token == NULL) {
1535 URL = xmlStrdup(URI);
1536 PUSH(XSLT_OP_ELEM, token, URL);
1541 while (CUR == '[') {
1543 xmlChar *ret = NULL;
1549 /* Skip over nested predicates */
1552 else if (CUR == ']') {
1556 } else if (CUR == '"') {
1558 while ((CUR != 0) && (CUR != '"'))
1560 } else if (CUR == '\'') {
1562 while ((CUR != 0) && (CUR != '\''))
1568 xsltTransformError(NULL, NULL, NULL,
1569 "xsltCompileStepPattern : ']' expected\n");
1573 ret = xmlStrndup(q, CUR_PTR - q);
1574 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1575 /* push the predicate lower than local test */
1589 * xsltCompileRelativePathPattern:
1590 * @comp: the compilation context
1591 * @token: a posible precompiled name
1593 * Compile the XSLT RelativePathPattern and generates a precompiled
1594 * form suitable for fast matching.
1596 * [4] RelativePathPattern ::= StepPattern
1597 * | RelativePathPattern '/' StepPattern
1598 * | RelativePathPattern '//' StepPattern
1601 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1602 xsltCompileStepPattern(ctxt, token);
1606 while ((CUR != 0) && (CUR != '|')) {
1607 if ((CUR == '/') && (NXT(1) == '/')) {
1608 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1612 xsltCompileStepPattern(ctxt, NULL);
1613 } else if (CUR == '/') {
1614 PUSH(XSLT_OP_PARENT, NULL, NULL);
1617 if ((CUR != 0) || (CUR == '|')) {
1618 xsltCompileRelativePathPattern(ctxt, NULL);
1632 * xsltCompileLocationPathPattern:
1633 * @ctxt: the compilation context
1635 * Compile the XSLT LocationPathPattern and generates a precompiled
1636 * form suitable for fast matching.
1638 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1639 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1640 * | '//'? RelativePathPattern
1643 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1645 if ((CUR == '/') && (NXT(1) == '/')) {
1647 * since we reverse the query
1648 * a leading // can be safely ignored
1652 ctxt->comp->priority = 0.5; /* '//' means not 0 priority */
1653 xsltCompileRelativePathPattern(ctxt, NULL);
1654 } else if (CUR == '/') {
1656 * We need to find root as the parent
1660 PUSH(XSLT_OP_ROOT, NULL, NULL);
1661 if ((CUR != 0) || (CUR == '|')) {
1662 PUSH(XSLT_OP_PARENT, NULL, NULL);
1663 xsltCompileRelativePathPattern(ctxt, NULL);
1665 } else if (CUR == '*') {
1666 xsltCompileRelativePathPattern(ctxt, NULL);
1667 } else if (CUR == '@') {
1668 xsltCompileRelativePathPattern(ctxt, NULL);
1671 name = xsltScanName(ctxt);
1673 xsltTransformError(NULL, NULL, NULL,
1674 "xsltCompileLocationPathPattern : Name expected\n");
1679 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1680 xsltCompileIdKeyPattern(ctxt, name, 1);
1681 if ((CUR == '/') && (NXT(1) == '/')) {
1682 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1686 xsltCompileRelativePathPattern(ctxt, NULL);
1687 } else if (CUR == '/') {
1688 PUSH(XSLT_OP_PARENT, NULL, NULL);
1691 xsltCompileRelativePathPattern(ctxt, NULL);
1695 xsltCompileRelativePathPattern(ctxt, name);
1702 * xsltCompilePattern:
1703 * @pattern: an XSLT pattern
1704 * @doc: the containing document
1705 * @node: the containing element
1706 * @style: the stylesheet
1707 * @runtime: the transformation context, if done at run-time
1709 * Compile the XSLT pattern and generates a list of precompiled form suitable
1710 * for fast matching.
1712 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1714 * Returns the generated pattern list or NULL in case of failure
1718 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1719 xmlNodePtr node, xsltStylesheetPtr style,
1720 xsltTransformContextPtr runtime) {
1721 xsltParserContextPtr ctxt = NULL;
1722 xsltCompMatchPtr element, first = NULL, previous = NULL;
1723 int current, start, end, level, j;
1725 if (pattern == NULL) {
1726 xsltTransformError(NULL, NULL, node,
1727 "xsltCompilePattern : NULL pattern\n");
1731 ctxt = xsltNewParserContext(style, runtime);
1737 while (pattern[current] != 0) {
1739 while (IS_BLANK_CH(pattern[current]))
1743 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1744 if (pattern[end] == '[')
1746 else if (pattern[end] == ']')
1748 else if (pattern[end] == '\'') {
1750 while ((pattern[end] != 0) && (pattern[end] != '\''))
1752 } else if (pattern[end] == '"') {
1754 while ((pattern[end] != 0) && (pattern[end] != '"'))
1759 if (current == end) {
1760 xsltTransformError(NULL, NULL, node,
1761 "xsltCompilePattern : NULL pattern\n");
1764 element = xsltNewCompMatch();
1765 if (element == NULL) {
1770 else if (previous != NULL)
1771 previous->next = element;
1774 ctxt->comp = element;
1775 ctxt->base = xmlStrndup(&pattern[start], end - start);
1776 if (ctxt->base == NULL)
1778 ctxt->cur = &(ctxt->base)[current - start];
1779 element->pattern = ctxt->base;
1780 element->nsList = xmlGetNsList(doc, node);
1782 if (element->nsList != NULL) {
1783 while (element->nsList[j] != NULL)
1789 #ifdef WITH_XSLT_DEBUG_PATTERN
1790 xsltGenericDebug(xsltGenericDebugContext,
1791 "xsltCompilePattern : parsing '%s'\n",
1795 Preset default priority to be zero.
1796 This may be changed by xsltCompileLocationPathPattern.
1798 element->priority = 0;
1799 xsltCompileLocationPathPattern(ctxt);
1801 xsltTransformError(NULL, style, node,
1802 "xsltCompilePattern : failed to compile '%s'\n",
1804 if (style != NULL) style->errors++;
1809 * Reverse for faster interpretation.
1811 xsltReverseCompMatch(element);
1814 * Set-up the priority
1816 if (element->priority == 0) { /* if not yet determined */
1817 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1818 (element->steps[0].op == XSLT_OP_ATTR) ||
1819 (element->steps[0].op == XSLT_OP_PI)) &&
1820 (element->steps[0].value != NULL) &&
1821 (element->steps[1].op == XSLT_OP_END)) {
1822 ; /* previously preset */
1823 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1824 (element->steps[0].value2 != NULL) &&
1825 (element->steps[1].op == XSLT_OP_END)) {
1826 element->priority = -0.25;
1827 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1828 (element->steps[0].value != NULL) &&
1829 (element->steps[1].op == XSLT_OP_END)) {
1830 element->priority = -0.25;
1831 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1832 (element->steps[0].value == NULL) &&
1833 (element->steps[0].value2 == NULL) &&
1834 (element->steps[1].op == XSLT_OP_END)) {
1835 element->priority = -0.5;
1836 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1837 (element->steps[0].op == XSLT_OP_TEXT) ||
1838 (element->steps[0].op == XSLT_OP_ALL) ||
1839 (element->steps[0].op == XSLT_OP_NODE) ||
1840 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1841 (element->steps[1].op == XSLT_OP_END)) {
1842 element->priority = -0.5;
1844 element->priority = 0.5;
1847 #ifdef WITH_XSLT_DEBUG_PATTERN
1848 xsltGenericDebug(xsltGenericDebugContext,
1849 "xsltCompilePattern : parsed %s, default priority %f\n",
1850 element->pattern, element->priority);
1852 if (pattern[end] == '|')
1857 xsltTransformError(NULL, style, node,
1858 "xsltCompilePattern : NULL pattern\n");
1859 if (style != NULL) style->errors++;
1863 xsltFreeParserContext(ctxt);
1868 xsltFreeParserContext(ctxt);
1870 xsltFreeCompMatchList(first);
1874 /************************************************************************
1876 * Module interfaces *
1878 ************************************************************************/
1882 * @style: an XSLT stylesheet
1883 * @cur: an XSLT template
1884 * @mode: the mode name or NULL
1885 * @modeURI: the mode URI or NULL
1887 * Register the XSLT pattern associated to @cur
1889 * Returns -1 in case of error, 0 otherwise
1892 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1893 const xmlChar *mode, const xmlChar *modeURI) {
1894 xsltCompMatchPtr pat, list, *top = NULL, next;
1895 const xmlChar *name = NULL;
1896 float priority; /* the priority */
1898 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1901 priority = cur->priority;
1902 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1908 pat->template = cur;
1910 pat->mode = xmlStrdup(mode);
1911 if (modeURI != NULL)
1912 pat->modeURI = xmlStrdup(modeURI);
1913 if (priority != XSLT_PAT_NO_PRIORITY)
1914 pat->priority = priority;
1917 * insert it in the hash table list corresponding to its lookup name
1919 switch (pat->steps[0].op) {
1921 if (pat->steps[0].value != NULL)
1922 name = pat->steps[0].value;
1924 top = (xsltCompMatchPtr *) &(style->attrMatch);
1927 case XSLT_OP_PARENT:
1928 case XSLT_OP_ANCESTOR:
1929 top = (xsltCompMatchPtr *) &(style->elemMatch);
1932 top = (xsltCompMatchPtr *) &(style->rootMatch);
1935 top = (xsltCompMatchPtr *) &(style->keyMatch);
1938 /* TODO optimize ID !!! */
1941 top = (xsltCompMatchPtr *) &(style->elemMatch);
1944 case XSLT_OP_PREDICATE:
1945 xsltTransformError(NULL, style, NULL,
1946 "xsltAddTemplate: invalid compiled pattern\n");
1947 xsltFreeCompMatch(pat);
1950 * TODO: some flags at the top level about type based patterns
1951 * would be faster than inclusion in the hash table.
1954 if (pat->steps[0].value != NULL)
1955 name = pat->steps[0].value;
1957 top = (xsltCompMatchPtr *) &(style->piMatch);
1959 case XSLT_OP_COMMENT:
1960 top = (xsltCompMatchPtr *) &(style->commentMatch);
1963 top = (xsltCompMatchPtr *) &(style->textMatch);
1967 if (pat->steps[0].value != NULL)
1968 name = pat->steps[0].value;
1970 top = (xsltCompMatchPtr *) &(style->elemMatch);
1974 if (style->templatesHash == NULL) {
1975 style->templatesHash = xmlHashCreate(1024);
1976 if (style->templatesHash == NULL) {
1977 xsltFreeCompMatch(pat);
1980 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1982 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1983 name, mode, modeURI);
1985 xmlHashAddEntry3(style->templatesHash, name,
1986 mode, modeURI, pat);
1989 * Note '<=' since one must choose among the matching
1990 * template rules that are left, the one that occurs
1991 * last in the stylesheet
1993 if (list->priority <= pat->priority) {
1995 xmlHashUpdateEntry3(style->templatesHash, name,
1996 mode, modeURI, pat, NULL);
1998 while (list->next != NULL) {
1999 if (list->next->priority <= pat->priority)
2003 pat->next = list->next;
2008 } else if (top != NULL) {
2013 } else if (list->priority <= pat->priority) {
2017 while (list->next != NULL) {
2018 if (list->next->priority <= pat->priority)
2022 pat->next = list->next;
2026 xsltTransformError(NULL, style, NULL,
2027 "xsltAddTemplate: invalid compiled pattern\n");
2028 xsltFreeCompMatch(pat);
2031 #ifdef WITH_XSLT_DEBUG_PATTERN
2033 xsltGenericDebug(xsltGenericDebugContext,
2034 "added pattern : '%s' mode '%s' priority %f\n",
2035 pat->pattern, pat->mode, pat->priority);
2037 xsltGenericDebug(xsltGenericDebugContext,
2038 "added pattern : '%s' priority %f\n",
2039 pat->pattern, pat->priority);
2049 * @ctxt: a XSLT process context
2050 * @node: the node being processed
2051 * @style: the current style
2053 * Finds the template applying to this node, if @style is non-NULL
2054 * it means one needs to look for the next imported template in scope.
2056 * Returns the xsltTemplatePtr or NULL if not found
2059 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2060 xsltStylesheetPtr style) {
2061 xsltStylesheetPtr curstyle;
2062 xsltTemplatePtr ret = NULL;
2063 const xmlChar *name = NULL;
2064 xsltCompMatchPtr list = NULL;
2068 if ((ctxt == NULL) || (node == NULL))
2071 if (style == NULL) {
2072 curstyle = ctxt->style;
2074 curstyle = xsltNextImport(style);
2077 while ((curstyle != NULL) && (curstyle != style)) {
2078 priority = XSLT_PAT_NO_PRIORITY;
2079 /* TODO : handle IDs/keys here ! */
2080 if (curstyle->templatesHash != NULL) {
2082 * Use the top name as selector
2084 switch (node->type) {
2085 case XML_ELEMENT_NODE:
2086 if (node->name[0] == ' ')
2088 case XML_ATTRIBUTE_NODE:
2092 case XML_DOCUMENT_NODE:
2093 case XML_HTML_DOCUMENT_NODE:
2095 case XML_CDATA_SECTION_NODE:
2096 case XML_COMMENT_NODE:
2097 case XML_ENTITY_REF_NODE:
2098 case XML_ENTITY_NODE:
2099 case XML_DOCUMENT_TYPE_NODE:
2100 case XML_DOCUMENT_FRAG_NODE:
2101 case XML_NOTATION_NODE:
2103 case XML_ELEMENT_DECL:
2104 case XML_ATTRIBUTE_DECL:
2105 case XML_ENTITY_DECL:
2106 case XML_NAMESPACE_DECL:
2107 case XML_XINCLUDE_START:
2108 case XML_XINCLUDE_END:
2117 * find the list of appliable expressions based on the name
2119 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2120 name, ctxt->mode, ctxt->modeURI);
2123 while (list != NULL) {
2124 if (xsltTestCompMatch(ctxt, list, node,
2125 ctxt->mode, ctxt->modeURI)) {
2126 ret = list->template;
2127 priority = list->priority;
2135 * find alternate generic matches
2137 switch (node->type) {
2138 case XML_ELEMENT_NODE:
2139 if (node->name[0] == ' ')
2140 list = curstyle->rootMatch;
2142 list = curstyle->elemMatch;
2143 if (node->psvi != NULL) keyed = 1;
2145 case XML_ATTRIBUTE_NODE: {
2148 list = curstyle->attrMatch;
2149 attr = (xmlAttrPtr) node;
2150 if (attr->psvi != NULL) keyed = 1;
2154 list = curstyle->piMatch;
2155 if (node->psvi != NULL) keyed = 1;
2157 case XML_DOCUMENT_NODE:
2158 case XML_HTML_DOCUMENT_NODE: {
2161 list = curstyle->rootMatch;
2162 doc = (xmlDocPtr) node;
2163 if (doc->psvi != NULL) keyed = 1;
2167 case XML_CDATA_SECTION_NODE:
2168 list = curstyle->textMatch;
2169 if (node->psvi != NULL) keyed = 1;
2171 case XML_COMMENT_NODE:
2172 list = curstyle->commentMatch;
2173 if (node->psvi != NULL) keyed = 1;
2175 case XML_ENTITY_REF_NODE:
2176 case XML_ENTITY_NODE:
2177 case XML_DOCUMENT_TYPE_NODE:
2178 case XML_DOCUMENT_FRAG_NODE:
2179 case XML_NOTATION_NODE:
2181 case XML_ELEMENT_DECL:
2182 case XML_ATTRIBUTE_DECL:
2183 case XML_ENTITY_DECL:
2184 case XML_NAMESPACE_DECL:
2185 case XML_XINCLUDE_START:
2186 case XML_XINCLUDE_END:
2191 while ((list != NULL) &&
2192 ((ret == NULL) || (list->priority > priority))) {
2193 if (xsltTestCompMatch(ctxt, list, node,
2194 ctxt->mode, ctxt->modeURI)) {
2195 ret = list->template;
2196 priority = list->priority;
2202 * Some of the tests for elements can also apply to documents
2204 if ((node->type == XML_DOCUMENT_NODE) ||
2205 (node->type == XML_HTML_DOCUMENT_NODE) ||
2206 (node->type == XML_TEXT_NODE)) {
2207 list = curstyle->elemMatch;
2208 while ((list != NULL) &&
2209 ((ret == NULL) || (list->priority > priority))) {
2210 if (xsltTestCompMatch(ctxt, list, node,
2211 ctxt->mode, ctxt->modeURI)) {
2212 ret = list->template;
2213 priority = list->priority;
2218 } else if ((node->type == XML_PI_NODE) ||
2219 (node->type == XML_COMMENT_NODE)) {
2220 list = curstyle->elemMatch;
2221 while ((list != NULL) &&
2222 ((ret == NULL) || (list->priority > priority))) {
2223 if (xsltTestCompMatch(ctxt, list, node,
2224 ctxt->mode, ctxt->modeURI)) {
2225 ret = list->template;
2226 priority = list->priority;
2234 list = curstyle->keyMatch;
2235 while ((list != NULL) &&
2236 ((ret == NULL) || (list->priority > priority))) {
2237 if (xsltTestCompMatch(ctxt, list, node,
2238 ctxt->mode, ctxt->modeURI)) {
2239 ret = list->template;
2240 priority = list->priority;
2250 * Cycle on next curstylesheet import.
2252 curstyle = xsltNextImport(curstyle);
2258 * xsltCleanupTemplates:
2259 * @style: an XSLT stylesheet
2261 * Cleanup the state of the templates used by the stylesheet and
2262 * the ones it imports.
2265 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2269 * xsltFreeTemplateHashes:
2270 * @style: an XSLT stylesheet
2272 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2275 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2276 if (style->templatesHash != NULL)
2277 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2278 (xmlHashDeallocator) xsltFreeCompMatchList);
2279 if (style->rootMatch != NULL)
2280 xsltFreeCompMatchList(style->rootMatch);
2281 if (style->keyMatch != NULL)
2282 xsltFreeCompMatchList(style->keyMatch);
2283 if (style->elemMatch != NULL)
2284 xsltFreeCompMatchList(style->elemMatch);
2285 if (style->attrMatch != NULL)
2286 xsltFreeCompMatchList(style->attrMatch);
2287 if (style->parentMatch != NULL)
2288 xsltFreeCompMatchList(style->parentMatch);
2289 if (style->textMatch != NULL)
2290 xsltFreeCompMatchList(style->textMatch);
2291 if (style->piMatch != NULL)
2292 xsltFreeCompMatchList(style->piMatch);
2293 if (style->commentMatch != NULL)
2294 xsltFreeCompMatchList(style->commentMatch);