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) {
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 = xmlXPathCompile(value);
299 * xsltSwapTopCompMatch:
300 * @comp: the compiled match expression
302 * reverse the two top steps.
305 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
307 int j = comp->nbStep - 1;
310 register xmlChar *tmp;
312 register xmlXPathCompExprPtr expr;
314 tmp = comp->steps[i].value;
315 comp->steps[i].value = comp->steps[j].value;
316 comp->steps[j].value = tmp;
317 tmp = comp->steps[i].value2;
318 comp->steps[i].value2 = comp->steps[j].value2;
319 comp->steps[j].value2 = tmp;
320 op = comp->steps[i].op;
321 comp->steps[i].op = comp->steps[j].op;
322 comp->steps[j].op = op;
323 expr = comp->steps[i].comp;
324 comp->steps[i].comp = comp->steps[j].comp;
325 comp->steps[j].comp = expr;
330 * xsltReverseCompMatch:
331 * @comp: the compiled match expression
333 * reverse all the stack of expressions
336 xsltReverseCompMatch(xsltCompMatchPtr comp) {
338 int j = comp->nbStep - 1;
341 register xmlChar *tmp;
343 register xmlXPathCompExprPtr expr;
344 tmp = comp->steps[i].value;
345 comp->steps[i].value = comp->steps[j].value;
346 comp->steps[j].value = tmp;
347 tmp = comp->steps[i].value2;
348 comp->steps[i].value2 = comp->steps[j].value2;
349 comp->steps[j].value2 = tmp;
350 op = comp->steps[i].op;
351 comp->steps[i].op = comp->steps[j].op;
352 comp->steps[j].op = op;
353 expr = comp->steps[i].comp;
354 comp->steps[i].comp = comp->steps[j].comp;
355 comp->steps[j].comp = expr;
359 comp->steps[comp->nbStep++].op = XSLT_OP_END;
362 /************************************************************************
364 * The interpreter for the precompiled patterns *
366 ************************************************************************/
370 * @ctxt: a XSLT process context
371 * @comp: the precompiled pattern
373 * @mode: the mode name or NULL
374 * @modeURI: the mode URI or NULL
376 * Test wether the node matches the pattern
378 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
381 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
382 xmlNodePtr node, const xmlChar *mode,
383 const xmlChar *modeURI) {
385 xsltStepOpPtr step, select = NULL;
387 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
388 xsltTransformError(ctxt, NULL, node,
389 "xsltTestCompMatch: null arg\n");
393 if (comp->mode == NULL)
395 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
398 if (comp->mode != NULL)
401 if (modeURI != NULL) {
402 if (comp->modeURI == NULL)
404 if ((comp->modeURI != modeURI) &&
405 (!xmlStrEqual(comp->modeURI, modeURI)))
408 if (comp->modeURI != NULL)
411 for (i = 0;i < comp->nbStep;i++) {
412 step = &comp->steps[i];
413 if (step->op != XSLT_OP_PREDICATE)
419 if ((node->type == XML_DOCUMENT_NODE) ||
420 #ifdef LIBXML_DOCB_ENABLED
421 (node->type == XML_DOCB_DOCUMENT_NODE) ||
423 (node->type == XML_HTML_DOCUMENT_NODE))
425 if ((node->type == XML_ELEMENT_NODE) && (node->name[0] == ' '))
429 if (node->type != XML_ELEMENT_NODE)
431 if (step->value == NULL)
433 if (step->value[0] != node->name[0])
435 if (!xmlStrEqual(step->value, node->name))
439 if (node->ns == NULL) {
440 if (step->value2 != NULL)
442 } else if (node->ns->href != NULL) {
443 if (step->value2 == NULL)
445 if (!xmlStrEqual(step->value2, node->ns->href))
449 case XSLT_OP_CHILD: {
452 if ((node->type != XML_ELEMENT_NODE) &&
453 (node->type != XML_DOCUMENT_NODE) &&
454 #ifdef LIBXML_DOCB_ENABLED
455 (node->type != XML_DOCB_DOCUMENT_NODE) &&
457 (node->type != XML_HTML_DOCUMENT_NODE))
460 lst = node->children;
462 if (step->value != NULL) {
463 while (lst != NULL) {
464 if ((lst->type == XML_ELEMENT_NODE) &&
465 (step->value[0] == lst->name[0]) &&
466 (xmlStrEqual(step->value, lst->name)))
476 if (node->type != XML_ATTRIBUTE_NODE)
478 if (step->value != NULL) {
479 if (step->value[0] != node->name[0])
481 if (!xmlStrEqual(step->value, node->name))
485 if (node->ns == NULL) {
486 if (step->value2 != NULL)
488 } else if (step->value2 != NULL) {
489 if (!xmlStrEqual(step->value2, node->ns->href))
494 if ((node->type == XML_DOCUMENT_NODE) ||
495 (node->type == XML_HTML_DOCUMENT_NODE) ||
496 #ifdef LIBXML_DOCB_ENABLED
497 (node->type == XML_DOCB_DOCUMENT_NODE) ||
499 (node->type == XML_NAMESPACE_DECL))
504 if (step->value == NULL)
506 if (step->value[0] != node->name[0])
508 if (!xmlStrEqual(step->value, node->name))
511 if (node->ns == NULL) {
512 if (step->value2 != NULL)
514 } else if (node->ns->href != NULL) {
515 if (step->value2 == NULL)
517 if (!xmlStrEqual(step->value2, node->ns->href))
521 case XSLT_OP_ANCESTOR:
522 /* TODO: implement coalescing of ANCESTOR/NODE ops */
523 if (step->value == NULL) {
525 step = &comp->steps[i];
526 if (step->op == XSLT_OP_ROOT)
528 if (step->op != XSLT_OP_ELEM)
530 if (step->value == NULL)
535 if ((node->type == XML_DOCUMENT_NODE) ||
536 (node->type == XML_HTML_DOCUMENT_NODE) ||
537 #ifdef LIBXML_DOCB_ENABLED
538 (node->type == XML_DOCB_DOCUMENT_NODE) ||
540 (node->type == XML_NAMESPACE_DECL))
543 while (node != NULL) {
546 if ((node->type == XML_ELEMENT_NODE) &&
547 (step->value[0] == node->name[0]) &&
548 (xmlStrEqual(step->value, node->name))) {
550 if (node->ns == NULL) {
551 if (step->value2 == NULL)
553 } else if (node->ns->href != NULL) {
554 if ((step->value2 != NULL) &&
555 (xmlStrEqual(step->value2, node->ns->href)))
565 /* TODO Handle IDs decently, must be done differently */
568 if (node->type != XML_ELEMENT_NODE)
571 id = xmlGetID(node->doc, step->value);
572 if ((id == NULL) || (id->parent != node))
580 list = xsltGetKey(ctxt, step->value,
581 step->value3, step->value2);
584 for (indx = 0;indx < list->nodeNr;indx++)
585 if (list->nodeTab[indx] == node)
587 if (indx >= list->nodeNr)
592 if (node->type != XML_ELEMENT_NODE)
594 if (node->ns == NULL) {
595 if (step->value != NULL)
597 } else if (node->ns->href != NULL) {
598 if (step->value == NULL)
600 if (!xmlStrEqual(step->value, node->ns->href))
605 if (node->type != XML_ELEMENT_NODE)
608 case XSLT_OP_PREDICATE: {
611 int pos = 0, len = 0;
613 * The simple existing predicate code cannot handle
614 * properly cascaded predicates. If in this situation
615 * compute directly the full node list once and check
616 * if the node is in the result list.
618 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
619 xmlDocPtr prevdoc, doc;
620 xmlXPathObjectPtr list;
624 prevdoc = (xmlDocPtr)
625 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
627 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
628 list = (xmlXPathObjectPtr)
629 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra);
632 if ((list == NULL) || (prevdoc != doc)) {
634 xmlXPathObjectPtr newlist;
635 xmlNodePtr parent = node->parent;
639 if (comp->pattern[0] == '/')
640 query = xmlStrdup(comp->pattern);
642 query = xmlStrdup((const xmlChar *)"//");
643 query = xmlStrcat(query, comp->pattern);
645 oldnode = ctxt->xpathCtxt->node;
646 olddoc = ctxt->xpathCtxt->doc;
647 ctxt->xpathCtxt->node = node;
648 ctxt->xpathCtxt->doc = doc;
649 newlist = xmlXPathEval(query, ctxt->xpathCtxt);
650 ctxt->xpathCtxt->node = oldnode;
651 ctxt->xpathCtxt->doc = olddoc;
655 if (newlist->type != XPATH_NODESET) {
656 xmlXPathFreeObject(newlist);
660 if ((parent == NULL) || (node->doc == NULL))
663 if ((doc->name != NULL) &&
664 (doc->name[0] == ' ') &&
665 (xmlStrEqual(BAD_CAST doc->name,
666 BAD_CAST " fake node libxslt")))
671 xmlXPathFreeObject(list);
674 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra) =
676 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
678 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
679 (xmlFreeFunc) xmlXPathFreeObject;
683 if ((list->nodesetval == NULL) ||
684 (list->nodesetval->nodeNr <= 0)) {
686 xmlXPathFreeObject(list);
689 /* TODO: store the index and use it for the scan */
691 for (j = 0;j < list->nodesetval->nodeNr;j++) {
692 if (list->nodesetval->nodeTab[j] == node) {
694 xmlXPathFreeObject(list);
701 xmlXPathFreeObject(list);
705 * Depending on the last selection, one may need to
706 * recompute contextSize and proximityPosition.
708 * TODO: make this thread safe !
710 oldCS = ctxt->xpathCtxt->contextSize;
711 oldCP = ctxt->xpathCtxt->proximityPosition;
712 if ((select != NULL) &&
713 (select->op == XSLT_OP_ELEM) &&
714 (select->value != NULL) &&
715 (node->type == XML_ELEMENT_NODE) &&
716 (node->parent != NULL)) {
718 int index, nocache = 0;
720 previous = (xmlNodePtr)
721 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
723 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
724 if ((previous != NULL) &&
725 (previous->parent == node->parent)) {
727 * just walk back to adjust the index
730 xmlNodePtr sibling = node;
732 while (sibling != NULL) {
733 if (sibling == previous)
735 if ((node->type == XML_ELEMENT_NODE) &&
736 (node->name != NULL) &&
737 (sibling->name != NULL) &&
738 (node->name[0] == sibling->name[0]) &&
739 (xmlStrEqual(node->name, sibling->name))) {
740 if ((select->value2 == NULL) ||
741 ((sibling->ns != NULL) &&
742 (xmlStrEqual(select->value2,
743 sibling->ns->href))))
746 sibling = sibling->prev;
748 if (sibling == NULL) {
749 /* hum going backward in document order ... */
752 while (sibling != NULL) {
753 if (sibling == previous)
755 if ((select->value2 == NULL) ||
756 ((sibling->ns != NULL) &&
757 (xmlStrEqual(select->value2,
758 sibling->ns->href))))
760 sibling = sibling->next;
763 if (sibling != NULL) {
766 * If the node is in a Value Tree we cannot
769 if (node->doc != NULL) {
771 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
772 XSLT_RUNTIME_EXTRA(ctxt,
773 select->previousExtra) = node;
774 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
782 * recompute the index
784 xmlNodePtr siblings = node->parent->children;
785 xmlNodePtr parent = node->parent;
787 while (siblings != NULL) {
788 if (siblings->type == XML_ELEMENT_NODE) {
789 if (siblings == node) {
792 } else if ((node->name != NULL) &&
793 (siblings->name != NULL) &&
794 (node->name[0] == siblings->name[0]) &&
795 (xmlStrEqual(node->name, siblings->name))) {
796 if ((select->value2 == NULL) ||
797 ((siblings->ns != NULL) &&
798 (xmlStrEqual(select->value2,
799 siblings->ns->href))))
803 siblings = siblings->next;
805 if ((parent == NULL) || (node->doc == NULL))
808 while (parent->parent != NULL)
809 parent = parent->parent;
810 if (((parent->type != XML_DOCUMENT_NODE) &&
811 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
812 (parent != (xmlNodePtr) node->doc))
817 ctxt->xpathCtxt->contextSize = len;
818 ctxt->xpathCtxt->proximityPosition = pos;
820 * If the node is in a Value Tree we cannot
823 if ((node->doc != NULL) && (nocache == 0)) {
824 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
826 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
828 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
832 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
833 (node->type == XML_ELEMENT_NODE)) {
835 int index, nocache = 0;
837 previous = (xmlNodePtr)
838 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
840 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
841 if ((previous != NULL) &&
842 (previous->parent == node->parent)) {
844 * just walk back to adjust the index
847 xmlNodePtr sibling = node;
849 while (sibling != NULL) {
850 if (sibling == previous)
852 if (sibling->type == XML_ELEMENT_NODE)
854 sibling = sibling->prev;
856 if (sibling == NULL) {
857 /* hum going backward in document order ... */
860 while (sibling != NULL) {
861 if (sibling == previous)
863 if (sibling->type == XML_ELEMENT_NODE)
865 sibling = sibling->next;
868 if (sibling != NULL) {
871 * If the node is in a Value Tree we cannot
874 if (node->doc != NULL) {
876 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
877 XSLT_RUNTIME_EXTRA(ctxt,
878 select->previousExtra) = node;
879 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
886 * recompute the index
888 xmlNodePtr siblings = node->parent->children;
889 xmlNodePtr parent = node->parent;
891 while (siblings != NULL) {
892 if (siblings->type == XML_ELEMENT_NODE) {
894 if (siblings == node) {
898 siblings = siblings->next;
900 if ((parent == NULL) || (node->doc == NULL))
903 while (parent->parent != NULL)
904 parent = parent->parent;
905 if (((parent->type != XML_DOCUMENT_NODE) &&
906 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
907 (parent != (xmlNodePtr) node->doc))
912 ctxt->xpathCtxt->contextSize = len;
913 ctxt->xpathCtxt->proximityPosition = pos;
915 * If the node is in a Value Tree we cannot
918 if ((node->doc != NULL) && (nocache == 0)) {
919 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
921 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
923 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
928 oldNode = ctxt->node;
931 if (step->value == NULL)
933 if (step->comp == NULL)
936 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
941 ctxt->xpathCtxt->contextSize = oldCS;
942 ctxt->xpathCtxt->proximityPosition = oldCP;
944 ctxt->node = oldNode;
948 ctxt->xpathCtxt->contextSize = oldCS;
949 ctxt->xpathCtxt->proximityPosition = oldCP;
951 ctxt->node = oldNode;
955 if (node->type != XML_PI_NODE)
957 if (step->value != NULL) {
958 if (!xmlStrEqual(step->value, node->name))
962 case XSLT_OP_COMMENT:
963 if (node->type != XML_COMMENT_NODE)
967 if ((node->type != XML_TEXT_NODE) &&
968 (node->type != XML_CDATA_SECTION_NODE))
972 switch (node->type) {
973 case XML_ELEMENT_NODE:
974 case XML_CDATA_SECTION_NODE:
976 case XML_COMMENT_NODE:
989 * xsltTestCompMatchList:
990 * @ctxt: a XSLT process context
992 * @comp: the precompiled pattern list
994 * Test wether the node matches one of the patterns in the list
996 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
999 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
1000 xsltCompMatchPtr comp) {
1003 if ((ctxt == NULL) || (node == NULL))
1005 while (comp != NULL) {
1006 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
1014 /************************************************************************
1016 * Dedicated parser for templates *
1018 ************************************************************************/
1020 #define CUR (*ctxt->cur)
1021 #define SKIP(val) ctxt->cur += (val)
1022 #define NXT(val) ctxt->cur[(val)]
1023 #define CUR_PTR ctxt->cur
1025 #define SKIP_BLANKS \
1026 while (IS_BLANK(CUR)) NEXT
1028 #define CURRENT (*ctxt->cur)
1029 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1032 #define PUSH(op, val, val2) \
1033 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1036 xsltSwapTopCompMatch(ctxt->comp);
1038 #define XSLT_ERROR(X) \
1039 { xsltError(ctxt, __FILE__, __LINE__, X); \
1040 ctxt->error = (X); return; }
1042 #define XSLT_ERROR0(X) \
1043 { xsltError(ctxt, __FILE__, __LINE__, X); \
1044 ctxt->error = (X); return(0); }
1048 * @ctxt: the XPath Parser context
1050 * Parse an XPath Litteral:
1052 * [29] Literal ::= '"' [^"]* '"'
1055 * Returns the Literal parsed or NULL
1059 xsltScanLiteral(xsltParserContextPtr ctxt) {
1060 const xmlChar *q, *cur;
1061 xmlChar *ret = NULL;
1068 val = xmlStringCurrentChar(NULL, cur, &len);
1069 while ((IS_CHAR(val)) && (val != '"')) {
1071 val = xmlStringCurrentChar(NULL, cur, &len);
1073 if (!IS_CHAR(val)) {
1077 ret = xmlStrndup(q, cur - q);
1081 } else if (CUR == '\'') {
1084 val = xmlStringCurrentChar(NULL, cur, &len);
1085 while ((IS_CHAR(val)) && (val != '\'')) {
1087 val = xmlStringCurrentChar(NULL, cur, &len);
1089 if (!IS_CHAR(val)) {
1093 ret = xmlStrndup(q, cur - q);
1098 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1107 * @ctxt: the XPath Parser context
1109 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1110 * CombiningChar | Extender
1112 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1114 * [6] Names ::= Name (S Name)*
1116 * Returns the Name parsed or NULL
1120 xsltScanName(xsltParserContextPtr ctxt) {
1121 const xmlChar *q, *cur;
1122 xmlChar *ret = NULL;
1128 val = xmlStringCurrentChar(NULL, cur, &len);
1129 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1132 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1133 (val == '.') || (val == '-') ||
1135 (IS_COMBINING(val)) ||
1136 (IS_EXTENDER(val))) {
1138 val = xmlStringCurrentChar(NULL, cur, &len);
1140 ret = xmlStrndup(q, cur - q);
1147 * @ctxt: the XPath Parser context
1149 * Parses a non qualified name
1151 * Returns the Name parsed or NULL
1155 xsltScanNCName(xsltParserContextPtr ctxt) {
1156 const xmlChar *q, *cur;
1157 xmlChar *ret = NULL;
1163 val = xmlStringCurrentChar(NULL, cur, &len);
1164 if (!IS_LETTER(val) && (val != '_'))
1167 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1168 (val == '.') || (val == '-') ||
1170 (IS_COMBINING(val)) ||
1171 (IS_EXTENDER(val))) {
1173 val = xmlStringCurrentChar(NULL, cur, &len);
1175 ret = xmlStrndup(q, cur - q);
1182 * @ctxt: the XPath Parser context
1183 * @prefix: the place to store the prefix
1185 * Parse a qualified name
1187 * Returns the Name parsed or NULL
1191 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1192 xmlChar *ret = NULL;
1195 ret = xsltScanNCName(ctxt);
1199 ret = xsltScanNCName(ctxt);
1205 * xsltCompileIdKeyPattern:
1206 * @ctxt: the compilation context
1207 * @name: a preparsed name
1208 * @aid: whether id/key are allowed there
1210 * Compile the XSLT LocationIdKeyPattern
1211 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1212 * | 'key' '(' Literal ',' Literal ')'
1214 * also handle NodeType and PI from:
1216 * [7] NodeTest ::= NameTest
1217 * | NodeType '(' ')'
1218 * | 'processing-instruction' '(' Literal ')'
1221 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1222 xmlChar *lit = NULL;
1223 xmlChar *lit2 = NULL;
1226 xsltTransformError(NULL, NULL, NULL,
1227 "xsltCompileIdKeyPattern : ( expected\n");
1231 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1234 lit = xsltScanLiteral(ctxt);
1239 xsltTransformError(NULL, NULL, NULL,
1240 "xsltCompileIdKeyPattern : ) expected\n");
1245 PUSH(XSLT_OP_ID, lit, NULL);
1246 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1249 lit = xsltScanLiteral(ctxt);
1254 xsltTransformError(NULL, NULL, NULL,
1255 "xsltCompileIdKeyPattern : , expected\n");
1261 lit2 = xsltScanLiteral(ctxt);
1266 xsltTransformError(NULL, NULL, NULL,
1267 "xsltCompileIdKeyPattern : ) expected\n");
1272 /* TODO: support namespace in keys */
1273 PUSH(XSLT_OP_KEY, lit, lit2);
1274 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1278 lit = xsltScanLiteral(ctxt);
1283 xsltTransformError(NULL, NULL, NULL,
1284 "xsltCompileIdKeyPattern : ) expected\n");
1290 PUSH(XSLT_OP_PI, lit, NULL);
1291 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1295 xsltTransformError(NULL, NULL, NULL,
1296 "xsltCompileIdKeyPattern : ) expected\n");
1301 PUSH(XSLT_OP_TEXT, NULL, NULL);
1302 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1306 xsltTransformError(NULL, NULL, NULL,
1307 "xsltCompileIdKeyPattern : ) expected\n");
1312 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1313 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1317 xsltTransformError(NULL, NULL, NULL,
1318 "xsltCompileIdKeyPattern : ) expected\n");
1323 PUSH(XSLT_OP_NODE, NULL, NULL);
1325 xsltTransformError(NULL, NULL, NULL,
1326 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1330 xsltTransformError(NULL, NULL, NULL,
1331 "xsltCompileIdKeyPattern : node type\n");
1341 * xsltCompileStepPattern:
1342 * @ctxt: the compilation context
1343 * @token: a posible precompiled name
1345 * Compile the XSLT StepPattern and generates a precompiled
1346 * form suitable for fast matching.
1348 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1349 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1350 * | ('child' | 'attribute') '::'
1352 * [7] NodeTest ::= NameTest
1353 * | NodeType '(' ')'
1354 * | 'processing-instruction' '(' Literal ')'
1355 * [8] Predicate ::= '[' PredicateExpr ']'
1356 * [9] PredicateExpr ::= Expr
1357 * [13] AbbreviatedAxisSpecifier ::= '@'?
1358 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1362 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1363 xmlChar *name = NULL;
1364 const xmlChar *URI = NULL;
1365 xmlChar *URL = NULL;
1369 if ((token == NULL) && (CUR == '@')) {
1370 xmlChar *prefix = NULL;
1375 PUSH(XSLT_OP_ATTR, NULL, NULL);
1376 goto parse_predicate;
1378 token = xsltScanQName(ctxt, &prefix);
1379 if (prefix != NULL) {
1382 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1384 xsltTransformError(NULL, NULL, NULL,
1385 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1388 URL = xmlStrdup(ns->href);
1392 if (token == NULL) {
1395 PUSH(XSLT_OP_ATTR, NULL, URL);
1398 xsltTransformError(NULL, NULL, NULL,
1399 "xsltCompileStepPattern : Name expected\n");
1403 PUSH(XSLT_OP_ATTR, token, URL);
1404 goto parse_predicate;
1407 token = xsltScanName(ctxt);
1408 if (token == NULL) {
1411 PUSH(XSLT_OP_ALL, token, NULL);
1412 goto parse_predicate;
1414 xsltTransformError(NULL, NULL, NULL,
1415 "xsltCompileStepPattern : Name expected\n");
1424 xsltCompileIdKeyPattern(ctxt, token, 0);
1427 } else if (CUR == ':') {
1430 xmlChar *prefix = token;
1434 * This is a namespace match
1436 token = xsltScanName(ctxt);
1437 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1439 xsltTransformError(NULL, NULL, NULL,
1440 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1445 URL = xmlStrdup(ns->href);
1448 if (token == NULL) {
1451 PUSH(XSLT_OP_NS, URL, NULL);
1453 xsltTransformError(NULL, NULL, NULL,
1454 "xsltCompileStepPattern : Name expected\n");
1459 PUSH(XSLT_OP_ELEM, token, URL);
1463 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1465 token = xsltScanName(ctxt);
1466 if (token == NULL) {
1469 PUSH(XSLT_OP_ALL, token, NULL);
1470 goto parse_predicate;
1472 xsltTransformError(NULL, NULL, NULL,
1473 "xsltCompileStepPattern : QName expected\n");
1478 URI = xsltGetQNameURI(ctxt->elem, &token);
1479 if (token == NULL) {
1483 name = xmlStrdup(token);
1485 URL = xmlStrdup(URI);
1487 PUSH(XSLT_OP_CHILD, name, URL);
1488 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1490 token = xsltScanName(ctxt);
1491 if (token == NULL) {
1492 xsltTransformError(NULL, NULL, NULL,
1493 "xsltCompileStepPattern : QName expected\n");
1497 URI = xsltGetQNameURI(ctxt->elem, &token);
1498 if (token == NULL) {
1502 name = xmlStrdup(token);
1504 URL = xmlStrdup(URI);
1506 PUSH(XSLT_OP_ATTR, name, URL);
1508 xsltTransformError(NULL, NULL, NULL,
1509 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1515 } else if (CUR == '*') {
1517 PUSH(XSLT_OP_ALL, token, NULL);
1519 URI = xsltGetQNameURI(ctxt->elem, &token);
1520 if (token == NULL) {
1525 URL = xmlStrdup(URI);
1526 PUSH(XSLT_OP_ELEM, token, URL);
1531 while (CUR == '[') {
1533 xmlChar *ret = NULL;
1539 /* Skip over nested predicates */
1542 else if (CUR == ']') {
1546 } else if (CUR == '"') {
1548 while ((CUR != 0) && (CUR != '"'))
1550 } else if (CUR == '\'') {
1552 while ((CUR != 0) && (CUR != '\''))
1558 xsltTransformError(NULL, NULL, NULL,
1559 "xsltCompileStepPattern : ']' expected\n");
1563 ret = xmlStrndup(q, CUR_PTR - q);
1564 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1565 /* push the predicate lower than local test */
1579 * xsltCompileRelativePathPattern:
1580 * @comp: the compilation context
1581 * @token: a posible precompiled name
1583 * Compile the XSLT RelativePathPattern and generates a precompiled
1584 * form suitable for fast matching.
1586 * [4] RelativePathPattern ::= StepPattern
1587 * | RelativePathPattern '/' StepPattern
1588 * | RelativePathPattern '//' StepPattern
1591 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1592 xsltCompileStepPattern(ctxt, token);
1596 while ((CUR != 0) && (CUR != '|')) {
1597 if ((CUR == '/') && (NXT(1) == '/')) {
1598 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1602 xsltCompileStepPattern(ctxt, NULL);
1603 } else if (CUR == '/') {
1604 PUSH(XSLT_OP_PARENT, NULL, NULL);
1607 if ((CUR != 0) || (CUR == '|')) {
1608 xsltCompileRelativePathPattern(ctxt, NULL);
1622 * xsltCompileLocationPathPattern:
1623 * @ctxt: the compilation context
1625 * Compile the XSLT LocationPathPattern and generates a precompiled
1626 * form suitable for fast matching.
1628 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1629 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1630 * | '//'? RelativePathPattern
1633 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1635 if ((CUR == '/') && (NXT(1) == '/')) {
1637 * since we reverse the query
1638 * a leading // can be safely ignored
1642 xsltCompileRelativePathPattern(ctxt, NULL);
1643 } else if (CUR == '/') {
1645 * We need to find root as the parent
1649 PUSH(XSLT_OP_ROOT, NULL, NULL);
1650 if ((CUR != 0) || (CUR == '|')) {
1651 PUSH(XSLT_OP_PARENT, NULL, NULL);
1652 xsltCompileRelativePathPattern(ctxt, NULL);
1654 } else if (CUR == '*') {
1655 xsltCompileRelativePathPattern(ctxt, NULL);
1656 } else if (CUR == '@') {
1657 xsltCompileRelativePathPattern(ctxt, NULL);
1660 name = xsltScanName(ctxt);
1662 xsltTransformError(NULL, NULL, NULL,
1663 "xsltCompileLocationPathPattern : Name expected\n");
1668 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1669 xsltCompileIdKeyPattern(ctxt, name, 1);
1670 if ((CUR == '/') && (NXT(1) == '/')) {
1671 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1675 xsltCompileRelativePathPattern(ctxt, NULL);
1676 } else if (CUR == '/') {
1677 PUSH(XSLT_OP_PARENT, NULL, NULL);
1680 xsltCompileRelativePathPattern(ctxt, NULL);
1684 xsltCompileRelativePathPattern(ctxt, name);
1691 * xsltCompilePattern:
1692 * @pattern: an XSLT pattern
1693 * @doc: the containing document
1694 * @node: the containing element
1695 * @style: the stylesheet
1696 * @runtime: the transformation context, if done at run-time
1698 * Compile the XSLT pattern and generates a list of precompiled form suitable
1699 * for fast matching.
1701 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1703 * Returns the generated pattern list or NULL in case of failure
1707 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1708 xmlNodePtr node, xsltStylesheetPtr style,
1709 xsltTransformContextPtr runtime) {
1710 xsltParserContextPtr ctxt = NULL;
1711 xsltCompMatchPtr element, first = NULL, previous = NULL;
1712 int current, start, end, level, j;
1714 if (pattern == NULL) {
1715 xsltTransformError(NULL, NULL, node,
1716 "xsltCompilePattern : NULL pattern\n");
1720 ctxt = xsltNewParserContext(style, runtime);
1726 while (pattern[current] != 0) {
1728 while (IS_BLANK(pattern[current]))
1732 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1733 if (pattern[end] == '[')
1735 else if (pattern[end] == ']')
1737 else if (pattern[end] == '\'') {
1739 while ((pattern[end] != 0) && (pattern[end] != '\''))
1741 } else if (pattern[end] == '"') {
1743 while ((pattern[end] != 0) && (pattern[end] != '"'))
1748 if (current == end) {
1749 xsltTransformError(NULL, NULL, node,
1750 "xsltCompilePattern : NULL pattern\n");
1753 element = xsltNewCompMatch();
1754 if (element == NULL) {
1759 else if (previous != NULL)
1760 previous->next = element;
1763 ctxt->comp = element;
1764 ctxt->base = xmlStrndup(&pattern[start], end - start);
1765 if (ctxt->base == NULL)
1767 ctxt->cur = &(ctxt->base)[current - start];
1768 element->pattern = ctxt->base;
1769 element->nsList = xmlGetNsList(doc, node);
1771 if (element->nsList != NULL) {
1772 while (element->nsList[j] != NULL)
1778 #ifdef WITH_XSLT_DEBUG_PATTERN
1779 xsltGenericDebug(xsltGenericDebugContext,
1780 "xsltCompilePattern : parsing '%s'\n",
1783 xsltCompileLocationPathPattern(ctxt);
1785 xsltTransformError(NULL, style, node,
1786 "xsltCompilePattern : failed to compile '%s'\n",
1788 if (style != NULL) style->errors++;
1793 * Reverse for faster interpretation.
1795 xsltReverseCompMatch(element);
1798 * Set-up the priority
1800 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1801 (element->steps[0].op == XSLT_OP_ATTR)) &&
1802 (element->steps[0].value != NULL) &&
1803 (element->steps[1].op == XSLT_OP_END)) {
1804 element->priority = 0;
1806 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1807 (element->steps[1].op == XSLT_OP_END)) {
1808 element->priority = 0;
1810 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1811 (element->steps[0].value != NULL) &&
1812 (element->steps[1].op == XSLT_OP_END)) {
1813 element->priority = 0;
1814 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1815 (element->steps[0].value2 != NULL) &&
1816 (element->steps[1].op == XSLT_OP_END)) {
1817 element->priority = -0.25;
1818 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1819 (element->steps[0].value != NULL) &&
1820 (element->steps[1].op == XSLT_OP_END)) {
1821 element->priority = -0.25;
1822 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1823 (element->steps[0].value == NULL) &&
1824 (element->steps[0].value2 == NULL) &&
1825 (element->steps[1].op == XSLT_OP_END)) {
1826 element->priority = -0.5;
1827 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1828 (element->steps[0].op == XSLT_OP_TEXT) ||
1829 (element->steps[0].op == XSLT_OP_ALL) ||
1830 (element->steps[0].op == XSLT_OP_NODE) ||
1831 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1832 (element->steps[1].op == XSLT_OP_END)) {
1833 element->priority = -0.5;
1835 element->priority = 0.5;
1837 #ifdef WITH_XSLT_DEBUG_PATTERN
1838 xsltGenericDebug(xsltGenericDebugContext,
1839 "xsltCompilePattern : parsed %s, default priority %f\n",
1840 element->pattern, element->priority);
1842 if (pattern[end] == '|')
1847 xsltTransformError(NULL, style, node,
1848 "xsltCompilePattern : NULL pattern\n");
1849 if (style != NULL) style->errors++;
1853 xsltFreeParserContext(ctxt);
1858 xsltFreeParserContext(ctxt);
1860 xsltFreeCompMatchList(first);
1864 /************************************************************************
1866 * Module interfaces *
1868 ************************************************************************/
1872 * @style: an XSLT stylesheet
1873 * @cur: an XSLT template
1874 * @mode: the mode name or NULL
1875 * @modeURI: the mode URI or NULL
1877 * Register the XSLT pattern associated to @cur
1879 * Returns -1 in case of error, 0 otherwise
1882 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1883 const xmlChar *mode, const xmlChar *modeURI) {
1884 xsltCompMatchPtr pat, list, *top = NULL, next;
1885 const xmlChar *name = NULL;
1886 float priority; /* the priority */
1888 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1891 priority = cur->priority;
1892 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1898 pat->template = cur;
1900 pat->mode = xmlStrdup(mode);
1901 if (modeURI != NULL)
1902 pat->modeURI = xmlStrdup(modeURI);
1903 if (priority != XSLT_PAT_NO_PRIORITY)
1904 pat->priority = priority;
1907 * insert it in the hash table list corresponding to its lookup name
1909 switch (pat->steps[0].op) {
1911 if (pat->steps[0].value != NULL)
1912 name = pat->steps[0].value;
1914 top = (xsltCompMatchPtr *) &(style->attrMatch);
1917 case XSLT_OP_PARENT:
1918 case XSLT_OP_ANCESTOR:
1919 top = (xsltCompMatchPtr *) &(style->elemMatch);
1922 top = (xsltCompMatchPtr *) &(style->rootMatch);
1925 top = (xsltCompMatchPtr *) &(style->keyMatch);
1928 /* TODO optimize ID !!! */
1931 top = (xsltCompMatchPtr *) &(style->elemMatch);
1934 case XSLT_OP_PREDICATE:
1935 xsltTransformError(NULL, style, NULL,
1936 "xsltAddTemplate: invalid compiled pattern\n");
1937 xsltFreeCompMatch(pat);
1940 * TODO: some flags at the top level about type based patterns
1941 * would be faster than inclusion in the hash table.
1944 if (pat->steps[0].value != NULL)
1945 name = pat->steps[0].value;
1947 top = (xsltCompMatchPtr *) &(style->piMatch);
1949 case XSLT_OP_COMMENT:
1950 top = (xsltCompMatchPtr *) &(style->commentMatch);
1953 top = (xsltCompMatchPtr *) &(style->textMatch);
1957 if (pat->steps[0].value != NULL)
1958 name = pat->steps[0].value;
1960 top = (xsltCompMatchPtr *) &(style->elemMatch);
1964 if (style->templatesHash == NULL) {
1965 style->templatesHash = xmlHashCreate(1024);
1966 if (style->templatesHash == NULL) {
1967 xsltFreeCompMatch(pat);
1970 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1972 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1973 name, mode, modeURI);
1975 xmlHashAddEntry3(style->templatesHash, name,
1976 mode, modeURI, pat);
1979 * Note '<=' since one must choose among the matching
1980 * template rules that are left, the one that occurs
1981 * last in the stylesheet
1983 if (list->priority <= pat->priority) {
1985 xmlHashUpdateEntry3(style->templatesHash, name,
1986 mode, modeURI, pat, NULL);
1988 while (list->next != NULL) {
1989 if (list->next->priority <= pat->priority)
1993 pat->next = list->next;
1998 } else if (top != NULL) {
2003 } else if (list->priority <= pat->priority) {
2007 while (list->next != NULL) {
2008 if (list->next->priority <= pat->priority)
2012 pat->next = list->next;
2016 xsltTransformError(NULL, style, NULL,
2017 "xsltAddTemplate: invalid compiled pattern\n");
2018 xsltFreeCompMatch(pat);
2021 #ifdef WITH_XSLT_DEBUG_PATTERN
2023 xsltGenericDebug(xsltGenericDebugContext,
2024 "added pattern : '%s' mode '%s' priority %f\n",
2025 pat->pattern, pat->mode, pat->priority);
2027 xsltGenericDebug(xsltGenericDebugContext,
2028 "added pattern : '%s' priority %f\n",
2029 pat->pattern, pat->priority);
2039 * @ctxt: a XSLT process context
2040 * @node: the node being processed
2041 * @style: the current style
2043 * Finds the template applying to this node, if @style is non-NULL
2044 * it means one needs to look for the next imported template in scope.
2046 * Returns the xsltTemplatePtr or NULL if not found
2049 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2050 xsltStylesheetPtr style) {
2051 xsltStylesheetPtr curstyle;
2052 xsltTemplatePtr ret = NULL;
2053 const xmlChar *name = NULL;
2054 xsltCompMatchPtr list = NULL;
2057 if ((ctxt == NULL) || (node == NULL))
2060 if (style == NULL) {
2061 curstyle = ctxt->style;
2063 curstyle = xsltNextImport(style);
2066 while ((curstyle != NULL) && (curstyle != style)) {
2067 priority = XSLT_PAT_NO_PRIORITY;
2068 /* TODO : handle IDs/keys here ! */
2069 if (curstyle->templatesHash != NULL) {
2071 * Use the top name as selector
2073 switch (node->type) {
2074 case XML_ELEMENT_NODE:
2075 if (node->name[0] == ' ')
2077 case XML_ATTRIBUTE_NODE:
2081 case XML_DOCUMENT_NODE:
2082 case XML_HTML_DOCUMENT_NODE:
2084 case XML_CDATA_SECTION_NODE:
2085 case XML_COMMENT_NODE:
2086 case XML_ENTITY_REF_NODE:
2087 case XML_ENTITY_NODE:
2088 case XML_DOCUMENT_TYPE_NODE:
2089 case XML_DOCUMENT_FRAG_NODE:
2090 case XML_NOTATION_NODE:
2092 case XML_ELEMENT_DECL:
2093 case XML_ATTRIBUTE_DECL:
2094 case XML_ENTITY_DECL:
2095 case XML_NAMESPACE_DECL:
2096 case XML_XINCLUDE_START:
2097 case XML_XINCLUDE_END:
2106 * find the list of appliable expressions based on the name
2108 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2109 name, ctxt->mode, ctxt->modeURI);
2112 while (list != NULL) {
2113 if (xsltTestCompMatch(ctxt, list, node,
2114 ctxt->mode, ctxt->modeURI)) {
2115 ret = list->template;
2116 priority = list->priority;
2124 * find alternate generic matches
2126 switch (node->type) {
2127 case XML_ELEMENT_NODE:
2128 if (node->name[0] == ' ')
2129 list = curstyle->rootMatch;
2131 list = curstyle->elemMatch;
2133 case XML_ATTRIBUTE_NODE:
2134 list = curstyle->attrMatch;
2137 list = curstyle->piMatch;
2139 case XML_DOCUMENT_NODE:
2140 case XML_HTML_DOCUMENT_NODE:
2141 list = curstyle->rootMatch;
2144 case XML_CDATA_SECTION_NODE:
2145 list = curstyle->textMatch;
2147 case XML_COMMENT_NODE:
2148 list = curstyle->commentMatch;
2150 case XML_ENTITY_REF_NODE:
2151 case XML_ENTITY_NODE:
2152 case XML_DOCUMENT_TYPE_NODE:
2153 case XML_DOCUMENT_FRAG_NODE:
2154 case XML_NOTATION_NODE:
2156 case XML_ELEMENT_DECL:
2157 case XML_ATTRIBUTE_DECL:
2158 case XML_ENTITY_DECL:
2159 case XML_NAMESPACE_DECL:
2160 case XML_XINCLUDE_START:
2161 case XML_XINCLUDE_END:
2167 while ((list != NULL) &&
2168 ((ret == NULL) || (list->priority > priority))) {
2169 if (xsltTestCompMatch(ctxt, list, node,
2170 ctxt->mode, ctxt->modeURI)) {
2171 ret = list->template;
2172 priority = list->priority;
2178 * Some of the tests for elements can also apply to documents
2180 if ((node->type == XML_DOCUMENT_NODE) ||
2181 (node->type == XML_HTML_DOCUMENT_NODE) ||
2182 (node->type == XML_TEXT_NODE)) {
2183 list = curstyle->elemMatch;
2184 while ((list != NULL) &&
2185 ((ret == NULL) || (list->priority > priority))) {
2186 if (xsltTestCompMatch(ctxt, list, node,
2187 ctxt->mode, ctxt->modeURI)) {
2188 ret = list->template;
2189 priority = list->priority;
2194 } else if ((node->type == XML_PI_NODE) ||
2195 (node->type == XML_COMMENT_NODE)) {
2196 list = curstyle->elemMatch;
2197 while ((list != NULL) &&
2198 ((ret == NULL) || (list->priority > priority))) {
2199 if (xsltTestCompMatch(ctxt, list, node,
2200 ctxt->mode, ctxt->modeURI)) {
2201 ret = list->template;
2202 priority = list->priority;
2209 if (node->_private != NULL) {
2210 list = curstyle->keyMatch;
2211 while ((list != NULL) &&
2212 ((ret == NULL) || (list->priority > priority))) {
2213 if (xsltTestCompMatch(ctxt, list, node,
2214 ctxt->mode, ctxt->modeURI)) {
2215 ret = list->template;
2216 priority = list->priority;
2226 * Cycle on next curstylesheet import.
2228 curstyle = xsltNextImport(curstyle);
2234 * xsltCleanupTemplates:
2235 * @style: an XSLT stylesheet
2237 * Cleanup the state of the templates used by the stylesheet and
2238 * the ones it imports.
2241 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2245 * xsltFreeTemplateHashes:
2246 * @style: an XSLT stylesheet
2248 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2251 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2252 if (style->templatesHash != NULL)
2253 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2254 (xmlHashDeallocator) xsltFreeCompMatchList);
2255 if (style->rootMatch != NULL)
2256 xsltFreeCompMatchList(style->rootMatch);
2257 if (style->keyMatch != NULL)
2258 xsltFreeCompMatchList(style->keyMatch);
2259 if (style->elemMatch != NULL)
2260 xsltFreeCompMatchList(style->elemMatch);
2261 if (style->attrMatch != NULL)
2262 xsltFreeCompMatchList(style->attrMatch);
2263 if (style->parentMatch != NULL)
2264 xsltFreeCompMatchList(style->parentMatch);
2265 if (style->textMatch != NULL)
2266 xsltFreeCompMatchList(style->textMatch);
2267 if (style->piMatch != NULL)
2268 xsltFreeCompMatchList(style->piMatch);
2269 if (style->commentMatch != NULL)
2270 xsltFreeCompMatchList(style->commentMatch);