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);
661 if ((parent == NULL) || (node->doc == NULL))
664 if ((doc->name != NULL) &&
665 (doc->name[0] == ' ') &&
666 (xmlStrEqual(BAD_CAST doc->name,
667 BAD_CAST " fake node libxslt")))
673 xmlXPathFreeObject(list);
676 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra) =
678 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
680 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
682 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
683 (xmlFreeFunc) xmlXPathFreeObject;
687 if ((list->nodesetval == NULL) ||
688 (list->nodesetval->nodeNr <= 0)) {
690 xmlXPathFreeObject(list);
693 /* TODO: store the index and use it for the scan */
695 for (j = 0;j < list->nodesetval->nodeNr;j++) {
696 if (list->nodesetval->nodeTab[j] == node) {
698 xmlXPathFreeObject(list);
705 xmlXPathFreeObject(list);
709 * Depending on the last selection, one may need to
710 * recompute contextSize and proximityPosition.
712 * TODO: make this thread safe !
714 oldCS = ctxt->xpathCtxt->contextSize;
715 oldCP = ctxt->xpathCtxt->proximityPosition;
716 if ((select != NULL) &&
717 (select->op == XSLT_OP_ELEM) &&
718 (select->value != NULL) &&
719 (node->type == XML_ELEMENT_NODE) &&
720 (node->parent != NULL)) {
722 int index, nocache = 0;
724 previous = (xmlNodePtr)
725 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
727 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
728 if ((previous != NULL) &&
729 (previous->parent == node->parent)) {
731 * just walk back to adjust the index
734 xmlNodePtr sibling = node;
736 while (sibling != NULL) {
737 if (sibling == previous)
739 if ((previous->type == XML_ELEMENT_NODE) &&
740 (previous->name != NULL) &&
741 (sibling->name != NULL) &&
742 (previous->name[0] == sibling->name[0]) &&
743 (xmlStrEqual(previous->name, sibling->name))) {
744 if ((select->value2 == NULL) ||
745 ((sibling->ns != NULL) &&
746 (xmlStrEqual(select->value2,
747 sibling->ns->href))))
750 sibling = sibling->prev;
752 if (sibling == NULL) {
753 /* hum going backward in document order ... */
756 while (sibling != NULL) {
757 if (sibling == previous)
759 if ((select->value2 == NULL) ||
760 ((sibling->ns != NULL) &&
761 (xmlStrEqual(select->value2,
762 sibling->ns->href))))
764 sibling = sibling->next;
767 if (sibling != NULL) {
770 * If the node is in a Value Tree we cannot
773 if (node->doc != NULL) {
775 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
776 XSLT_RUNTIME_EXTRA(ctxt,
777 select->previousExtra) = node;
778 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
786 * recompute the index
788 xmlNodePtr siblings = node->parent->children;
789 xmlNodePtr parent = node->parent;
791 while (siblings != NULL) {
792 if (siblings->type == XML_ELEMENT_NODE) {
793 if (siblings == node) {
796 } else if ((node->name != NULL) &&
797 (siblings->name != NULL) &&
798 (node->name[0] == siblings->name[0]) &&
799 (xmlStrEqual(node->name, siblings->name))) {
800 if ((select->value2 == NULL) ||
801 ((siblings->ns != NULL) &&
802 (xmlStrEqual(select->value2,
803 siblings->ns->href))))
807 siblings = siblings->next;
809 if ((parent == NULL) || (node->doc == NULL))
812 while (parent->parent != NULL)
813 parent = parent->parent;
814 if (((parent->type != XML_DOCUMENT_NODE) &&
815 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
816 (parent != (xmlNodePtr) node->doc))
821 ctxt->xpathCtxt->contextSize = len;
822 ctxt->xpathCtxt->proximityPosition = pos;
824 * If the node is in a Value Tree we cannot
827 if ((node->doc != NULL) && (nocache == 0)) {
828 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
830 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
832 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
836 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
837 (node->type == XML_ELEMENT_NODE)) {
839 int index, nocache = 0;
841 previous = (xmlNodePtr)
842 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
844 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
845 if ((previous != NULL) &&
846 (previous->parent == node->parent)) {
848 * just walk back to adjust the index
851 xmlNodePtr sibling = node;
853 while (sibling != NULL) {
854 if (sibling == previous)
856 if (sibling->type == XML_ELEMENT_NODE)
858 sibling = sibling->prev;
860 if (sibling == NULL) {
861 /* hum going backward in document order ... */
864 while (sibling != NULL) {
865 if (sibling == previous)
867 if (sibling->type == XML_ELEMENT_NODE)
869 sibling = sibling->next;
872 if (sibling != NULL) {
875 * If the node is in a Value Tree we cannot
878 if (node->doc != NULL) {
880 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
881 XSLT_RUNTIME_EXTRA(ctxt,
882 select->previousExtra) = node;
883 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
890 * recompute the index
892 xmlNodePtr siblings = node->parent->children;
893 xmlNodePtr parent = node->parent;
895 while (siblings != NULL) {
896 if (siblings->type == XML_ELEMENT_NODE) {
898 if (siblings == node) {
902 siblings = siblings->next;
904 if ((parent == NULL) || (node->doc == NULL))
907 while (parent->parent != NULL)
908 parent = parent->parent;
909 if (((parent->type != XML_DOCUMENT_NODE) &&
910 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
911 (parent != (xmlNodePtr) node->doc))
916 ctxt->xpathCtxt->contextSize = len;
917 ctxt->xpathCtxt->proximityPosition = pos;
919 * If the node is in a Value Tree we cannot
922 if ((node->doc != NULL) && (nocache == 0)) {
923 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
925 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
927 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
932 oldNode = ctxt->node;
935 if (step->value == NULL)
937 if (step->comp == NULL)
940 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
945 ctxt->xpathCtxt->contextSize = oldCS;
946 ctxt->xpathCtxt->proximityPosition = oldCP;
948 ctxt->node = oldNode;
952 ctxt->xpathCtxt->contextSize = oldCS;
953 ctxt->xpathCtxt->proximityPosition = oldCP;
955 ctxt->node = oldNode;
959 if (node->type != XML_PI_NODE)
961 if (step->value != NULL) {
962 if (!xmlStrEqual(step->value, node->name))
966 case XSLT_OP_COMMENT:
967 if (node->type != XML_COMMENT_NODE)
971 if ((node->type != XML_TEXT_NODE) &&
972 (node->type != XML_CDATA_SECTION_NODE))
976 switch (node->type) {
977 case XML_ELEMENT_NODE:
978 case XML_CDATA_SECTION_NODE:
980 case XML_COMMENT_NODE:
993 * xsltTestCompMatchList:
994 * @ctxt: a XSLT process context
996 * @comp: the precompiled pattern list
998 * Test wether the node matches one of the patterns in the list
1000 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
1003 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
1004 xsltCompMatchPtr comp) {
1007 if ((ctxt == NULL) || (node == NULL))
1009 while (comp != NULL) {
1010 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
1018 /************************************************************************
1020 * Dedicated parser for templates *
1022 ************************************************************************/
1024 #define CUR (*ctxt->cur)
1025 #define SKIP(val) ctxt->cur += (val)
1026 #define NXT(val) ctxt->cur[(val)]
1027 #define CUR_PTR ctxt->cur
1029 #define SKIP_BLANKS \
1030 while (IS_BLANK(CUR)) NEXT
1032 #define CURRENT (*ctxt->cur)
1033 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1036 #define PUSH(op, val, val2) \
1037 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1040 xsltSwapTopCompMatch(ctxt->comp);
1042 #define XSLT_ERROR(X) \
1043 { xsltError(ctxt, __FILE__, __LINE__, X); \
1044 ctxt->error = (X); return; }
1046 #define XSLT_ERROR0(X) \
1047 { xsltError(ctxt, __FILE__, __LINE__, X); \
1048 ctxt->error = (X); return(0); }
1052 * @ctxt: the XPath Parser context
1054 * Parse an XPath Litteral:
1056 * [29] Literal ::= '"' [^"]* '"'
1059 * Returns the Literal parsed or NULL
1063 xsltScanLiteral(xsltParserContextPtr ctxt) {
1064 const xmlChar *q, *cur;
1065 xmlChar *ret = NULL;
1072 val = xmlStringCurrentChar(NULL, cur, &len);
1073 while ((IS_CHAR(val)) && (val != '"')) {
1075 val = xmlStringCurrentChar(NULL, cur, &len);
1077 if (!IS_CHAR(val)) {
1081 ret = xmlStrndup(q, cur - q);
1085 } else if (CUR == '\'') {
1088 val = xmlStringCurrentChar(NULL, cur, &len);
1089 while ((IS_CHAR(val)) && (val != '\'')) {
1091 val = xmlStringCurrentChar(NULL, cur, &len);
1093 if (!IS_CHAR(val)) {
1097 ret = xmlStrndup(q, cur - q);
1102 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1111 * @ctxt: the XPath Parser context
1113 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1114 * CombiningChar | Extender
1116 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1118 * [6] Names ::= Name (S Name)*
1120 * Returns the Name parsed or NULL
1124 xsltScanName(xsltParserContextPtr ctxt) {
1125 const xmlChar *q, *cur;
1126 xmlChar *ret = NULL;
1132 val = xmlStringCurrentChar(NULL, cur, &len);
1133 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1136 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1137 (val == '.') || (val == '-') ||
1139 (IS_COMBINING(val)) ||
1140 (IS_EXTENDER(val))) {
1142 val = xmlStringCurrentChar(NULL, cur, &len);
1144 ret = xmlStrndup(q, cur - q);
1151 * @ctxt: the XPath Parser context
1153 * Parses a non qualified name
1155 * Returns the Name parsed or NULL
1159 xsltScanNCName(xsltParserContextPtr ctxt) {
1160 const xmlChar *q, *cur;
1161 xmlChar *ret = NULL;
1167 val = xmlStringCurrentChar(NULL, cur, &len);
1168 if (!IS_LETTER(val) && (val != '_'))
1171 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1172 (val == '.') || (val == '-') ||
1174 (IS_COMBINING(val)) ||
1175 (IS_EXTENDER(val))) {
1177 val = xmlStringCurrentChar(NULL, cur, &len);
1179 ret = xmlStrndup(q, cur - q);
1186 * @ctxt: the XPath Parser context
1187 * @prefix: the place to store the prefix
1189 * Parse a qualified name
1191 * Returns the Name parsed or NULL
1195 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1196 xmlChar *ret = NULL;
1199 ret = xsltScanNCName(ctxt);
1203 ret = xsltScanNCName(ctxt);
1209 * xsltCompileIdKeyPattern:
1210 * @ctxt: the compilation context
1211 * @name: a preparsed name
1212 * @aid: whether id/key are allowed there
1214 * Compile the XSLT LocationIdKeyPattern
1215 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1216 * | 'key' '(' Literal ',' Literal ')'
1218 * also handle NodeType and PI from:
1220 * [7] NodeTest ::= NameTest
1221 * | NodeType '(' ')'
1222 * | 'processing-instruction' '(' Literal ')'
1225 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1226 xmlChar *lit = NULL;
1227 xmlChar *lit2 = NULL;
1230 xsltTransformError(NULL, NULL, NULL,
1231 "xsltCompileIdKeyPattern : ( expected\n");
1235 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1238 lit = xsltScanLiteral(ctxt);
1243 xsltTransformError(NULL, NULL, NULL,
1244 "xsltCompileIdKeyPattern : ) expected\n");
1249 PUSH(XSLT_OP_ID, lit, NULL);
1250 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1253 lit = xsltScanLiteral(ctxt);
1258 xsltTransformError(NULL, NULL, NULL,
1259 "xsltCompileIdKeyPattern : , expected\n");
1265 lit2 = xsltScanLiteral(ctxt);
1270 xsltTransformError(NULL, NULL, NULL,
1271 "xsltCompileIdKeyPattern : ) expected\n");
1276 /* TODO: support namespace in keys */
1277 PUSH(XSLT_OP_KEY, lit, lit2);
1278 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1282 lit = xsltScanLiteral(ctxt);
1287 xsltTransformError(NULL, NULL, NULL,
1288 "xsltCompileIdKeyPattern : ) expected\n");
1294 PUSH(XSLT_OP_PI, lit, NULL);
1295 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1299 xsltTransformError(NULL, NULL, NULL,
1300 "xsltCompileIdKeyPattern : ) expected\n");
1305 PUSH(XSLT_OP_TEXT, NULL, NULL);
1306 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1310 xsltTransformError(NULL, NULL, NULL,
1311 "xsltCompileIdKeyPattern : ) expected\n");
1316 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1317 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1321 xsltTransformError(NULL, NULL, NULL,
1322 "xsltCompileIdKeyPattern : ) expected\n");
1327 PUSH(XSLT_OP_NODE, NULL, NULL);
1329 xsltTransformError(NULL, NULL, NULL,
1330 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1334 xsltTransformError(NULL, NULL, NULL,
1335 "xsltCompileIdKeyPattern : node type\n");
1345 * xsltCompileStepPattern:
1346 * @ctxt: the compilation context
1347 * @token: a posible precompiled name
1349 * Compile the XSLT StepPattern and generates a precompiled
1350 * form suitable for fast matching.
1352 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1353 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1354 * | ('child' | 'attribute') '::'
1356 * [7] NodeTest ::= NameTest
1357 * | NodeType '(' ')'
1358 * | 'processing-instruction' '(' Literal ')'
1359 * [8] Predicate ::= '[' PredicateExpr ']'
1360 * [9] PredicateExpr ::= Expr
1361 * [13] AbbreviatedAxisSpecifier ::= '@'?
1362 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1366 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1367 xmlChar *name = NULL;
1368 const xmlChar *URI = NULL;
1369 xmlChar *URL = NULL;
1373 if ((token == NULL) && (CUR == '@')) {
1374 xmlChar *prefix = NULL;
1379 PUSH(XSLT_OP_ATTR, NULL, NULL);
1380 goto parse_predicate;
1382 token = xsltScanQName(ctxt, &prefix);
1383 if (prefix != NULL) {
1386 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1388 xsltTransformError(NULL, NULL, NULL,
1389 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1392 URL = xmlStrdup(ns->href);
1396 if (token == NULL) {
1399 PUSH(XSLT_OP_ATTR, NULL, URL);
1402 xsltTransformError(NULL, NULL, NULL,
1403 "xsltCompileStepPattern : Name expected\n");
1407 PUSH(XSLT_OP_ATTR, token, URL);
1408 goto parse_predicate;
1411 token = xsltScanName(ctxt);
1412 if (token == NULL) {
1415 PUSH(XSLT_OP_ALL, token, NULL);
1416 goto parse_predicate;
1418 xsltTransformError(NULL, NULL, NULL,
1419 "xsltCompileStepPattern : Name expected\n");
1428 xsltCompileIdKeyPattern(ctxt, token, 0);
1431 } else if (CUR == ':') {
1434 xmlChar *prefix = token;
1438 * This is a namespace match
1440 token = xsltScanName(ctxt);
1441 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1443 xsltTransformError(NULL, NULL, NULL,
1444 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1449 URL = xmlStrdup(ns->href);
1452 if (token == NULL) {
1455 PUSH(XSLT_OP_NS, URL, NULL);
1457 xsltTransformError(NULL, NULL, NULL,
1458 "xsltCompileStepPattern : Name expected\n");
1463 PUSH(XSLT_OP_ELEM, token, URL);
1467 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1469 token = xsltScanName(ctxt);
1470 if (token == NULL) {
1473 PUSH(XSLT_OP_ALL, token, NULL);
1474 goto parse_predicate;
1476 xsltTransformError(NULL, NULL, NULL,
1477 "xsltCompileStepPattern : QName expected\n");
1482 URI = xsltGetQNameURI(ctxt->elem, &token);
1483 if (token == NULL) {
1487 name = xmlStrdup(token);
1489 URL = xmlStrdup(URI);
1491 PUSH(XSLT_OP_CHILD, name, URL);
1492 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1494 token = xsltScanName(ctxt);
1495 if (token == NULL) {
1496 xsltTransformError(NULL, NULL, NULL,
1497 "xsltCompileStepPattern : QName expected\n");
1501 URI = xsltGetQNameURI(ctxt->elem, &token);
1502 if (token == NULL) {
1506 name = xmlStrdup(token);
1508 URL = xmlStrdup(URI);
1510 PUSH(XSLT_OP_ATTR, name, URL);
1512 xsltTransformError(NULL, NULL, NULL,
1513 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1519 } else if (CUR == '*') {
1521 PUSH(XSLT_OP_ALL, token, NULL);
1523 URI = xsltGetQNameURI(ctxt->elem, &token);
1524 if (token == NULL) {
1529 URL = xmlStrdup(URI);
1530 PUSH(XSLT_OP_ELEM, token, URL);
1535 while (CUR == '[') {
1537 xmlChar *ret = NULL;
1543 /* Skip over nested predicates */
1546 else if (CUR == ']') {
1550 } else if (CUR == '"') {
1552 while ((CUR != 0) && (CUR != '"'))
1554 } else if (CUR == '\'') {
1556 while ((CUR != 0) && (CUR != '\''))
1562 xsltTransformError(NULL, NULL, NULL,
1563 "xsltCompileStepPattern : ']' expected\n");
1567 ret = xmlStrndup(q, CUR_PTR - q);
1568 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1569 /* push the predicate lower than local test */
1583 * xsltCompileRelativePathPattern:
1584 * @comp: the compilation context
1585 * @token: a posible precompiled name
1587 * Compile the XSLT RelativePathPattern and generates a precompiled
1588 * form suitable for fast matching.
1590 * [4] RelativePathPattern ::= StepPattern
1591 * | RelativePathPattern '/' StepPattern
1592 * | RelativePathPattern '//' StepPattern
1595 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1596 xsltCompileStepPattern(ctxt, token);
1600 while ((CUR != 0) && (CUR != '|')) {
1601 if ((CUR == '/') && (NXT(1) == '/')) {
1602 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1606 xsltCompileStepPattern(ctxt, NULL);
1607 } else if (CUR == '/') {
1608 PUSH(XSLT_OP_PARENT, NULL, NULL);
1611 if ((CUR != 0) || (CUR == '|')) {
1612 xsltCompileRelativePathPattern(ctxt, NULL);
1626 * xsltCompileLocationPathPattern:
1627 * @ctxt: the compilation context
1629 * Compile the XSLT LocationPathPattern and generates a precompiled
1630 * form suitable for fast matching.
1632 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1633 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1634 * | '//'? RelativePathPattern
1637 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1639 if ((CUR == '/') && (NXT(1) == '/')) {
1641 * since we reverse the query
1642 * a leading // can be safely ignored
1646 xsltCompileRelativePathPattern(ctxt, NULL);
1647 } else if (CUR == '/') {
1649 * We need to find root as the parent
1653 PUSH(XSLT_OP_ROOT, NULL, NULL);
1654 if ((CUR != 0) || (CUR == '|')) {
1655 PUSH(XSLT_OP_PARENT, NULL, NULL);
1656 xsltCompileRelativePathPattern(ctxt, NULL);
1658 } else if (CUR == '*') {
1659 xsltCompileRelativePathPattern(ctxt, NULL);
1660 } else if (CUR == '@') {
1661 xsltCompileRelativePathPattern(ctxt, NULL);
1664 name = xsltScanName(ctxt);
1666 xsltTransformError(NULL, NULL, NULL,
1667 "xsltCompileLocationPathPattern : Name expected\n");
1672 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1673 xsltCompileIdKeyPattern(ctxt, name, 1);
1674 if ((CUR == '/') && (NXT(1) == '/')) {
1675 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1679 xsltCompileRelativePathPattern(ctxt, NULL);
1680 } else if (CUR == '/') {
1681 PUSH(XSLT_OP_PARENT, NULL, NULL);
1684 xsltCompileRelativePathPattern(ctxt, NULL);
1688 xsltCompileRelativePathPattern(ctxt, name);
1695 * xsltCompilePattern:
1696 * @pattern: an XSLT pattern
1697 * @doc: the containing document
1698 * @node: the containing element
1699 * @style: the stylesheet
1700 * @runtime: the transformation context, if done at run-time
1702 * Compile the XSLT pattern and generates a list of precompiled form suitable
1703 * for fast matching.
1705 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1707 * Returns the generated pattern list or NULL in case of failure
1711 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1712 xmlNodePtr node, xsltStylesheetPtr style,
1713 xsltTransformContextPtr runtime) {
1714 xsltParserContextPtr ctxt = NULL;
1715 xsltCompMatchPtr element, first = NULL, previous = NULL;
1716 int current, start, end, level, j;
1718 if (pattern == NULL) {
1719 xsltTransformError(NULL, NULL, node,
1720 "xsltCompilePattern : NULL pattern\n");
1724 ctxt = xsltNewParserContext(style, runtime);
1730 while (pattern[current] != 0) {
1732 while (IS_BLANK(pattern[current]))
1736 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1737 if (pattern[end] == '[')
1739 else if (pattern[end] == ']')
1741 else if (pattern[end] == '\'') {
1743 while ((pattern[end] != 0) && (pattern[end] != '\''))
1745 } else if (pattern[end] == '"') {
1747 while ((pattern[end] != 0) && (pattern[end] != '"'))
1752 if (current == end) {
1753 xsltTransformError(NULL, NULL, node,
1754 "xsltCompilePattern : NULL pattern\n");
1757 element = xsltNewCompMatch();
1758 if (element == NULL) {
1763 else if (previous != NULL)
1764 previous->next = element;
1767 ctxt->comp = element;
1768 ctxt->base = xmlStrndup(&pattern[start], end - start);
1769 if (ctxt->base == NULL)
1771 ctxt->cur = &(ctxt->base)[current - start];
1772 element->pattern = ctxt->base;
1773 element->nsList = xmlGetNsList(doc, node);
1775 if (element->nsList != NULL) {
1776 while (element->nsList[j] != NULL)
1782 #ifdef WITH_XSLT_DEBUG_PATTERN
1783 xsltGenericDebug(xsltGenericDebugContext,
1784 "xsltCompilePattern : parsing '%s'\n",
1787 xsltCompileLocationPathPattern(ctxt);
1789 xsltTransformError(NULL, style, node,
1790 "xsltCompilePattern : failed to compile '%s'\n",
1792 if (style != NULL) style->errors++;
1797 * Reverse for faster interpretation.
1799 xsltReverseCompMatch(element);
1802 * Set-up the priority
1804 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1805 (element->steps[0].op == XSLT_OP_ATTR)) &&
1806 (element->steps[0].value != NULL) &&
1807 (element->steps[1].op == XSLT_OP_END)) {
1808 element->priority = 0;
1810 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1811 (element->steps[1].op == XSLT_OP_END)) {
1812 element->priority = 0;
1814 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1815 (element->steps[0].value != NULL) &&
1816 (element->steps[1].op == XSLT_OP_END)) {
1817 element->priority = 0;
1818 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1819 (element->steps[0].value2 != NULL) &&
1820 (element->steps[1].op == XSLT_OP_END)) {
1821 element->priority = -0.25;
1822 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1823 (element->steps[0].value != NULL) &&
1824 (element->steps[1].op == XSLT_OP_END)) {
1825 element->priority = -0.25;
1826 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1827 (element->steps[0].value == NULL) &&
1828 (element->steps[0].value2 == NULL) &&
1829 (element->steps[1].op == XSLT_OP_END)) {
1830 element->priority = -0.5;
1831 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1832 (element->steps[0].op == XSLT_OP_TEXT) ||
1833 (element->steps[0].op == XSLT_OP_ALL) ||
1834 (element->steps[0].op == XSLT_OP_NODE) ||
1835 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1836 (element->steps[1].op == XSLT_OP_END)) {
1837 element->priority = -0.5;
1839 element->priority = 0.5;
1841 #ifdef WITH_XSLT_DEBUG_PATTERN
1842 xsltGenericDebug(xsltGenericDebugContext,
1843 "xsltCompilePattern : parsed %s, default priority %f\n",
1844 element->pattern, element->priority);
1846 if (pattern[end] == '|')
1851 xsltTransformError(NULL, style, node,
1852 "xsltCompilePattern : NULL pattern\n");
1853 if (style != NULL) style->errors++;
1857 xsltFreeParserContext(ctxt);
1862 xsltFreeParserContext(ctxt);
1864 xsltFreeCompMatchList(first);
1868 /************************************************************************
1870 * Module interfaces *
1872 ************************************************************************/
1876 * @style: an XSLT stylesheet
1877 * @cur: an XSLT template
1878 * @mode: the mode name or NULL
1879 * @modeURI: the mode URI or NULL
1881 * Register the XSLT pattern associated to @cur
1883 * Returns -1 in case of error, 0 otherwise
1886 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1887 const xmlChar *mode, const xmlChar *modeURI) {
1888 xsltCompMatchPtr pat, list, *top = NULL, next;
1889 const xmlChar *name = NULL;
1890 float priority; /* the priority */
1892 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1895 priority = cur->priority;
1896 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1902 pat->template = cur;
1904 pat->mode = xmlStrdup(mode);
1905 if (modeURI != NULL)
1906 pat->modeURI = xmlStrdup(modeURI);
1907 if (priority != XSLT_PAT_NO_PRIORITY)
1908 pat->priority = priority;
1911 * insert it in the hash table list corresponding to its lookup name
1913 switch (pat->steps[0].op) {
1915 if (pat->steps[0].value != NULL)
1916 name = pat->steps[0].value;
1918 top = (xsltCompMatchPtr *) &(style->attrMatch);
1921 case XSLT_OP_PARENT:
1922 case XSLT_OP_ANCESTOR:
1923 top = (xsltCompMatchPtr *) &(style->elemMatch);
1926 top = (xsltCompMatchPtr *) &(style->rootMatch);
1929 top = (xsltCompMatchPtr *) &(style->keyMatch);
1932 /* TODO optimize ID !!! */
1935 top = (xsltCompMatchPtr *) &(style->elemMatch);
1938 case XSLT_OP_PREDICATE:
1939 xsltTransformError(NULL, style, NULL,
1940 "xsltAddTemplate: invalid compiled pattern\n");
1941 xsltFreeCompMatch(pat);
1944 * TODO: some flags at the top level about type based patterns
1945 * would be faster than inclusion in the hash table.
1948 if (pat->steps[0].value != NULL)
1949 name = pat->steps[0].value;
1951 top = (xsltCompMatchPtr *) &(style->piMatch);
1953 case XSLT_OP_COMMENT:
1954 top = (xsltCompMatchPtr *) &(style->commentMatch);
1957 top = (xsltCompMatchPtr *) &(style->textMatch);
1961 if (pat->steps[0].value != NULL)
1962 name = pat->steps[0].value;
1964 top = (xsltCompMatchPtr *) &(style->elemMatch);
1968 if (style->templatesHash == NULL) {
1969 style->templatesHash = xmlHashCreate(1024);
1970 if (style->templatesHash == NULL) {
1971 xsltFreeCompMatch(pat);
1974 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1976 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1977 name, mode, modeURI);
1979 xmlHashAddEntry3(style->templatesHash, name,
1980 mode, modeURI, pat);
1983 * Note '<=' since one must choose among the matching
1984 * template rules that are left, the one that occurs
1985 * last in the stylesheet
1987 if (list->priority <= pat->priority) {
1989 xmlHashUpdateEntry3(style->templatesHash, name,
1990 mode, modeURI, pat, NULL);
1992 while (list->next != NULL) {
1993 if (list->next->priority <= pat->priority)
1997 pat->next = list->next;
2002 } else if (top != NULL) {
2007 } else if (list->priority <= pat->priority) {
2011 while (list->next != NULL) {
2012 if (list->next->priority <= pat->priority)
2016 pat->next = list->next;
2020 xsltTransformError(NULL, style, NULL,
2021 "xsltAddTemplate: invalid compiled pattern\n");
2022 xsltFreeCompMatch(pat);
2025 #ifdef WITH_XSLT_DEBUG_PATTERN
2027 xsltGenericDebug(xsltGenericDebugContext,
2028 "added pattern : '%s' mode '%s' priority %f\n",
2029 pat->pattern, pat->mode, pat->priority);
2031 xsltGenericDebug(xsltGenericDebugContext,
2032 "added pattern : '%s' priority %f\n",
2033 pat->pattern, pat->priority);
2043 * @ctxt: a XSLT process context
2044 * @node: the node being processed
2045 * @style: the current style
2047 * Finds the template applying to this node, if @style is non-NULL
2048 * it means one needs to look for the next imported template in scope.
2050 * Returns the xsltTemplatePtr or NULL if not found
2053 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2054 xsltStylesheetPtr style) {
2055 xsltStylesheetPtr curstyle;
2056 xsltTemplatePtr ret = NULL;
2057 const xmlChar *name = NULL;
2058 xsltCompMatchPtr list = NULL;
2061 if ((ctxt == NULL) || (node == NULL))
2064 if (style == NULL) {
2065 curstyle = ctxt->style;
2067 curstyle = xsltNextImport(style);
2070 while ((curstyle != NULL) && (curstyle != style)) {
2071 priority = XSLT_PAT_NO_PRIORITY;
2072 /* TODO : handle IDs/keys here ! */
2073 if (curstyle->templatesHash != NULL) {
2075 * Use the top name as selector
2077 switch (node->type) {
2078 case XML_ELEMENT_NODE:
2079 if (node->name[0] == ' ')
2081 case XML_ATTRIBUTE_NODE:
2085 case XML_DOCUMENT_NODE:
2086 case XML_HTML_DOCUMENT_NODE:
2088 case XML_CDATA_SECTION_NODE:
2089 case XML_COMMENT_NODE:
2090 case XML_ENTITY_REF_NODE:
2091 case XML_ENTITY_NODE:
2092 case XML_DOCUMENT_TYPE_NODE:
2093 case XML_DOCUMENT_FRAG_NODE:
2094 case XML_NOTATION_NODE:
2096 case XML_ELEMENT_DECL:
2097 case XML_ATTRIBUTE_DECL:
2098 case XML_ENTITY_DECL:
2099 case XML_NAMESPACE_DECL:
2100 case XML_XINCLUDE_START:
2101 case XML_XINCLUDE_END:
2110 * find the list of appliable expressions based on the name
2112 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2113 name, ctxt->mode, ctxt->modeURI);
2116 while (list != NULL) {
2117 if (xsltTestCompMatch(ctxt, list, node,
2118 ctxt->mode, ctxt->modeURI)) {
2119 ret = list->template;
2120 priority = list->priority;
2128 * find alternate generic matches
2130 switch (node->type) {
2131 case XML_ELEMENT_NODE:
2132 if (node->name[0] == ' ')
2133 list = curstyle->rootMatch;
2135 list = curstyle->elemMatch;
2137 case XML_ATTRIBUTE_NODE:
2138 list = curstyle->attrMatch;
2141 list = curstyle->piMatch;
2143 case XML_DOCUMENT_NODE:
2144 case XML_HTML_DOCUMENT_NODE:
2145 list = curstyle->rootMatch;
2148 case XML_CDATA_SECTION_NODE:
2149 list = curstyle->textMatch;
2151 case XML_COMMENT_NODE:
2152 list = curstyle->commentMatch;
2154 case XML_ENTITY_REF_NODE:
2155 case XML_ENTITY_NODE:
2156 case XML_DOCUMENT_TYPE_NODE:
2157 case XML_DOCUMENT_FRAG_NODE:
2158 case XML_NOTATION_NODE:
2160 case XML_ELEMENT_DECL:
2161 case XML_ATTRIBUTE_DECL:
2162 case XML_ENTITY_DECL:
2163 case XML_NAMESPACE_DECL:
2164 case XML_XINCLUDE_START:
2165 case XML_XINCLUDE_END:
2171 while ((list != NULL) &&
2172 ((ret == NULL) || (list->priority > priority))) {
2173 if (xsltTestCompMatch(ctxt, list, node,
2174 ctxt->mode, ctxt->modeURI)) {
2175 ret = list->template;
2176 priority = list->priority;
2182 * Some of the tests for elements can also apply to documents
2184 if ((node->type == XML_DOCUMENT_NODE) ||
2185 (node->type == XML_HTML_DOCUMENT_NODE) ||
2186 (node->type == XML_TEXT_NODE)) {
2187 list = curstyle->elemMatch;
2188 while ((list != NULL) &&
2189 ((ret == NULL) || (list->priority > priority))) {
2190 if (xsltTestCompMatch(ctxt, list, node,
2191 ctxt->mode, ctxt->modeURI)) {
2192 ret = list->template;
2193 priority = list->priority;
2198 } else if ((node->type == XML_PI_NODE) ||
2199 (node->type == XML_COMMENT_NODE)) {
2200 list = curstyle->elemMatch;
2201 while ((list != NULL) &&
2202 ((ret == NULL) || (list->priority > priority))) {
2203 if (xsltTestCompMatch(ctxt, list, node,
2204 ctxt->mode, ctxt->modeURI)) {
2205 ret = list->template;
2206 priority = list->priority;
2213 if (node->_private != NULL) {
2214 list = curstyle->keyMatch;
2215 while ((list != NULL) &&
2216 ((ret == NULL) || (list->priority > priority))) {
2217 if (xsltTestCompMatch(ctxt, list, node,
2218 ctxt->mode, ctxt->modeURI)) {
2219 ret = list->template;
2220 priority = list->priority;
2230 * Cycle on next curstylesheet import.
2232 curstyle = xsltNextImport(curstyle);
2238 * xsltCleanupTemplates:
2239 * @style: an XSLT stylesheet
2241 * Cleanup the state of the templates used by the stylesheet and
2242 * the ones it imports.
2245 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2249 * xsltFreeTemplateHashes:
2250 * @style: an XSLT stylesheet
2252 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2255 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2256 if (style->templatesHash != NULL)
2257 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2258 (xmlHashDeallocator) xsltFreeCompMatchList);
2259 if (style->rootMatch != NULL)
2260 xsltFreeCompMatchList(style->rootMatch);
2261 if (style->keyMatch != NULL)
2262 xsltFreeCompMatchList(style->keyMatch);
2263 if (style->elemMatch != NULL)
2264 xsltFreeCompMatchList(style->elemMatch);
2265 if (style->attrMatch != NULL)
2266 xsltFreeCompMatchList(style->attrMatch);
2267 if (style->parentMatch != NULL)
2268 xsltFreeCompMatchList(style->parentMatch);
2269 if (style->textMatch != NULL)
2270 xsltFreeCompMatchList(style->textMatch);
2271 if (style->piMatch != NULL)
2272 xsltFreeCompMatchList(style->piMatch);
2273 if (style->commentMatch != NULL)
2274 xsltFreeCompMatchList(style->commentMatch);