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 xsltPrintErrorContext(NULL, NULL, NULL);
129 xsltGenericError(xsltGenericErrorContext,
130 "xsltNewCompMatch : malloc failed\n");
133 memset(cur, 0, sizeof(xsltCompMatch));
142 * @comp: an XSLT comp
144 * Free up the memory allocated by @comp
147 xsltFreeCompMatch(xsltCompMatchPtr comp) {
153 if (comp->pattern != NULL)
154 xmlFree((xmlChar *)comp->pattern);
155 if (comp->mode != NULL)
156 xmlFree((xmlChar *)comp->mode);
157 if (comp->modeURI != NULL)
158 xmlFree((xmlChar *)comp->modeURI);
159 if (comp->nsList != NULL)
160 xmlFree(comp->nsList);
161 for (i = 0;i < comp->nbStep;i++) {
162 op = &comp->steps[i];
163 if (op->value != NULL)
165 if (op->value2 != NULL)
167 if (op->value3 != NULL)
169 if (op->comp != NULL)
170 xmlXPathFreeCompExpr(op->comp);
172 memset(comp, -1, sizeof(xsltCompMatch));
177 * xsltFreeCompMatchList:
178 * @comp: an XSLT comp list
180 * Free up the memory allocated by all the elements of @comp
183 xsltFreeCompMatchList(xsltCompMatchPtr comp) {
184 xsltCompMatchPtr cur;
186 while (comp != NULL) {
189 xsltFreeCompMatch(cur);
194 * xsltNewParserContext:
195 * @style: the stylesheet
196 * @ctxt: the transformation context, if done at run-time
198 * Create a new XSLT ParserContext
200 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
202 static xsltParserContextPtr
203 xsltNewParserContext(xsltStylesheetPtr style, xsltTransformContextPtr ctxt) {
204 xsltParserContextPtr cur;
206 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
208 xsltPrintErrorContext(NULL, NULL, NULL);
209 xsltGenericError(xsltGenericErrorContext,
210 "xsltNewParserContext : malloc failed\n");
213 memset(cur, 0, sizeof(xsltParserContext));
220 * xsltFreeParserContext:
221 * @ctxt: an XSLT parser context
223 * Free up the memory allocated by @ctxt
226 xsltFreeParserContext(xsltParserContextPtr ctxt) {
229 memset(ctxt, -1, sizeof(xsltParserContext));
235 * @comp: the compiled match expression
237 * @value: the first value
238 * @value2: the second value
240 * Add an step to an XSLT Compiled Match
242 * Returns -1 in case of failure, 0 otherwise.
245 xsltCompMatchAdd(xsltParserContextPtr ctxt, xsltCompMatchPtr comp,
246 xsltOp op, xmlChar * value, xmlChar * value2)
248 if (comp->nbStep >= 40) {
249 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
250 xsltGenericError(xsltGenericErrorContext,
251 "xsltCompMatchAdd: overflow\n");
254 comp->steps[comp->nbStep].op = op;
255 comp->steps[comp->nbStep].value = value;
256 comp->steps[comp->nbStep].value2 = value2;
257 if (ctxt->ctxt != NULL) {
258 comp->steps[comp->nbStep].previousExtra =
259 xsltAllocateExtraCtxt(ctxt->ctxt);
260 comp->steps[comp->nbStep].indexExtra =
261 xsltAllocateExtraCtxt(ctxt->ctxt);
262 comp->steps[comp->nbStep].lenExtra =
263 xsltAllocateExtraCtxt(ctxt->ctxt);
265 comp->steps[comp->nbStep].previousExtra =
266 xsltAllocateExtra(ctxt->style);
267 comp->steps[comp->nbStep].indexExtra =
268 xsltAllocateExtra(ctxt->style);
269 comp->steps[comp->nbStep].lenExtra =
270 xsltAllocateExtra(ctxt->style);
272 if (op == XSLT_OP_PREDICATE) {
273 comp->steps[comp->nbStep].comp = xmlXPathCompile(value);
280 * xsltSwapTopCompMatch:
281 * @comp: the compiled match expression
283 * reverse the two top steps.
286 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
288 int j = comp->nbStep - 1;
291 register xmlChar *tmp;
293 register xmlXPathCompExprPtr expr;
295 tmp = comp->steps[i].value;
296 comp->steps[i].value = comp->steps[j].value;
297 comp->steps[j].value = tmp;
298 tmp = comp->steps[i].value2;
299 comp->steps[i].value2 = comp->steps[j].value2;
300 comp->steps[j].value2 = tmp;
301 op = comp->steps[i].op;
302 comp->steps[i].op = comp->steps[j].op;
303 comp->steps[j].op = op;
304 expr = comp->steps[i].comp;
305 comp->steps[i].comp = comp->steps[j].comp;
306 comp->steps[j].comp = expr;
311 * xsltReverseCompMatch:
312 * @comp: the compiled match expression
314 * reverse all the stack of expressions
317 xsltReverseCompMatch(xsltCompMatchPtr comp) {
319 int j = comp->nbStep - 1;
322 register xmlChar *tmp;
324 register xmlXPathCompExprPtr expr;
325 tmp = comp->steps[i].value;
326 comp->steps[i].value = comp->steps[j].value;
327 comp->steps[j].value = tmp;
328 tmp = comp->steps[i].value2;
329 comp->steps[i].value2 = comp->steps[j].value2;
330 comp->steps[j].value2 = tmp;
331 op = comp->steps[i].op;
332 comp->steps[i].op = comp->steps[j].op;
333 comp->steps[j].op = op;
334 expr = comp->steps[i].comp;
335 comp->steps[i].comp = comp->steps[j].comp;
336 comp->steps[j].comp = expr;
340 comp->steps[comp->nbStep++].op = XSLT_OP_END;
343 /************************************************************************
345 * The interpreter for the precompiled patterns *
347 ************************************************************************/
351 * @ctxt: a XSLT process context
352 * @comp: the precompiled pattern
354 * @mode: the mode name or NULL
355 * @modeURI: the mode URI or NULL
357 * Test wether the node matches the pattern
359 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
362 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
363 xmlNodePtr node, const xmlChar *mode,
364 const xmlChar *modeURI) {
366 xsltStepOpPtr step, select = NULL;
368 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
369 xsltPrintErrorContext(ctxt, NULL, node);
370 xsltGenericError(xsltGenericErrorContext,
371 "xsltTestCompMatch: null arg\n");
375 if (comp->mode == NULL)
377 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
380 if (comp->mode != NULL)
383 if (modeURI != NULL) {
384 if (comp->modeURI == NULL)
386 if ((comp->modeURI != modeURI) &&
387 (!xmlStrEqual(comp->modeURI, modeURI)))
390 if (comp->modeURI != NULL)
393 for (i = 0;i < comp->nbStep;i++) {
394 step = &comp->steps[i];
395 if (step->op != XSLT_OP_PREDICATE)
401 if ((node->type == XML_DOCUMENT_NODE) ||
402 #ifdef LIBXML_DOCB_ENABLED
403 (node->type == XML_DOCB_DOCUMENT_NODE) ||
405 (node->type == XML_HTML_DOCUMENT_NODE))
409 if (node->type != XML_ELEMENT_NODE)
411 if (step->value == NULL)
413 if (!xmlStrEqual(step->value, node->name))
417 if (node->ns == NULL) {
418 if (step->value2 != NULL)
420 } else if (node->ns->href != NULL) {
421 if (step->value2 == NULL)
423 if (!xmlStrEqual(step->value2, node->ns->href))
427 case XSLT_OP_CHILD: {
430 if ((node->type != XML_ELEMENT_NODE) &&
431 (node->type != XML_DOCUMENT_NODE) &&
432 #ifdef LIBXML_DOCB_ENABLED
433 (node->type != XML_DOCB_DOCUMENT_NODE) &&
435 (node->type != XML_HTML_DOCUMENT_NODE))
438 lst = node->children;
440 if (step->value != NULL) {
441 while (lst != NULL) {
442 if ((lst->type == XML_ELEMENT_NODE) &&
443 (xmlStrEqual(step->value, lst->name)))
453 if (node->type != XML_ATTRIBUTE_NODE)
455 if (step->value == NULL)
457 if (!xmlStrEqual(step->value, node->name))
461 if (node->ns == NULL) {
462 if (step->value2 != NULL)
464 } else if (node->ns->href != NULL) {
465 if (step->value2 == NULL)
467 if (!xmlStrEqual(step->value2, node->ns->href))
472 if ((node->type == XML_DOCUMENT_NODE) ||
473 (node->type == XML_HTML_DOCUMENT_NODE) ||
474 #ifdef LIBXML_DOCB_ENABLED
475 (node->type == XML_DOCB_DOCUMENT_NODE) ||
477 (node->type == XML_NAMESPACE_DECL))
482 if (step->value == NULL)
484 if (!xmlStrEqual(step->value, node->name))
487 if (node->ns == NULL) {
488 if (step->value2 != NULL)
490 } else if (node->ns->href != NULL) {
491 if (step->value2 == NULL)
493 if (!xmlStrEqual(step->value2, node->ns->href))
497 case XSLT_OP_ANCESTOR:
498 /* TODO: implement coalescing of ANCESTOR/NODE ops */
499 if (step->value == NULL) {
501 step = &comp->steps[i];
502 if (step->op == XSLT_OP_ROOT)
504 if (step->op != XSLT_OP_ELEM)
506 if (step->value == NULL)
511 if ((node->type == XML_DOCUMENT_NODE) ||
512 (node->type == XML_HTML_DOCUMENT_NODE) ||
513 #ifdef LIBXML_DOCB_ENABLED
514 (node->type == XML_DOCB_DOCUMENT_NODE) ||
516 (node->type == XML_NAMESPACE_DECL))
519 while (node != NULL) {
522 if (xmlStrEqual(step->value, node->name)) {
524 if (node->ns == NULL) {
525 if (step->value2 == NULL)
527 } else if (node->ns->href != NULL) {
528 if ((step->value2 != NULL) &&
529 (xmlStrEqual(step->value2, node->ns->href)))
539 /* TODO Handle IDs decently, must be done differently */
542 if (node->type != XML_ELEMENT_NODE)
545 id = xmlGetID(node->doc, step->value);
546 if ((id == NULL) || (id->parent != node))
554 list = xsltGetKey(ctxt, step->value,
555 step->value3, step->value2);
558 for (indx = 0;indx < list->nodeNr;indx++)
559 if (list->nodeTab[indx] == node)
561 if (indx >= list->nodeNr)
566 if (node->type != XML_ELEMENT_NODE)
568 if (node->ns == NULL) {
569 if (step->value != NULL)
571 } else if (node->ns->href != NULL) {
572 if (step->value == NULL)
574 if (!xmlStrEqual(step->value, node->ns->href))
579 if (node->type != XML_ELEMENT_NODE)
582 case XSLT_OP_PREDICATE: {
585 int pos = 0, len = 0;
587 * The simple existing predicate code cannot handle
588 * properly cascaded predicates. If in this situation
589 * compute directly the full node list once and check
590 * if the node is in the result list.
592 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
594 xmlXPathObjectPtr list;
597 previous = (xmlNodePtr)
598 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
600 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
601 list = (xmlXPathObjectPtr)
602 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
606 if (comp->pattern[0] == '/')
607 query = xmlStrdup(comp->pattern);
609 query = xmlStrdup((const xmlChar *)"//");
610 query = xmlStrcat(query, comp->pattern);
612 list = xmlXPathEval(query, ctxt->xpathCtxt);
616 if (list->type != XPATH_NODESET) {
617 xmlXPathFreeObject(list);
620 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
622 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
623 (xmlFreeFunc) xmlXPathFreeObject;
625 if ((list->nodesetval == NULL) ||
626 (list->nodesetval->nodeNr <= 0))
629 for (j = 0;j < list->nodesetval->nodeNr;j++) {
630 if (list->nodesetval->nodeTab[j] == node) {
639 * Depending on the last selection, one may need to
640 * recompute contextSize and proximityPosition.
642 * TODO: make this thread safe !
644 oldCS = ctxt->xpathCtxt->contextSize;
645 oldCP = ctxt->xpathCtxt->proximityPosition;
646 if ((select != NULL) &&
647 (select->op == XSLT_OP_ELEM) &&
648 (select->value != NULL) &&
649 (node->type == XML_ELEMENT_NODE) &&
650 (node->parent != NULL)) {
652 int index, nocache = 0;
654 previous = (xmlNodePtr)
655 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
657 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
658 if ((previous != NULL) &&
659 (previous->parent == node->parent)) {
661 * just walk back to adjust the index
664 xmlNodePtr sibling = node;
666 while (sibling != NULL) {
667 if (sibling == previous)
669 if (xmlStrEqual(node->name, sibling->name)) {
670 if ((select->value2 == NULL) ||
671 ((sibling->ns != NULL) &&
672 (xmlStrEqual(select->value2,
673 sibling->ns->href))))
676 sibling = sibling->prev;
678 if (sibling == NULL) {
679 /* hum going backward in document order ... */
682 while (sibling != NULL) {
683 if (sibling == previous)
685 if ((select->value2 == NULL) ||
686 ((sibling->ns != NULL) &&
687 (xmlStrEqual(select->value2,
688 sibling->ns->href))))
690 sibling = sibling->next;
693 if (sibling != NULL) {
696 * If the node is in a Value Tree we cannot
699 if (node->doc != NULL) {
701 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
702 XSLT_RUNTIME_EXTRA(ctxt,
703 select->previousExtra) = node;
704 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
712 * recompute the index
714 xmlNodePtr siblings = node->parent->children;
715 xmlNodePtr parent = node->parent;
717 while (siblings != NULL) {
718 if (siblings->type == XML_ELEMENT_NODE) {
719 if (siblings == node) {
722 } else if (xmlStrEqual(node->name,
724 if ((select->value2 == NULL) ||
725 ((siblings->ns != NULL) &&
726 (xmlStrEqual(select->value2,
727 siblings->ns->href))))
731 siblings = siblings->next;
733 if ((parent == NULL) || (node->doc == NULL))
736 while (parent->parent != NULL)
737 parent = parent->parent;
738 if (((parent->type != XML_DOCUMENT_NODE) &&
739 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
740 (parent != (xmlNodePtr) node->doc))
745 ctxt->xpathCtxt->contextSize = len;
746 ctxt->xpathCtxt->proximityPosition = pos;
748 * If the node is in a Value Tree we cannot
751 if ((node->doc != NULL) && (nocache == 0)) {
752 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
754 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
756 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
760 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
761 (node->type == XML_ELEMENT_NODE)) {
765 previous = (xmlNodePtr)
766 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
768 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
769 if ((previous != NULL) &&
770 (previous->parent == node->parent)) {
772 * just walk back to adjust the index
775 xmlNodePtr sibling = node;
777 while (sibling != NULL) {
778 if (sibling == previous)
780 if (sibling->type == XML_ELEMENT_NODE)
782 sibling = sibling->prev;
784 if (sibling == NULL) {
785 /* hum going backward in document order ... */
788 while (sibling != NULL) {
789 if (sibling == previous)
791 if (sibling->type == XML_ELEMENT_NODE)
793 sibling = sibling->next;
796 if (sibling != NULL) {
799 * If the node is in a Value Tree we cannot
802 if (node->doc != NULL) {
804 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
805 XSLT_RUNTIME_EXTRA(ctxt,
806 select->previousExtra) = node;
807 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
814 * recompute the index
816 xmlNodePtr siblings = node->parent->children;
817 xmlNodePtr parent = node->parent;
819 while (siblings != NULL) {
820 if (siblings->type == XML_ELEMENT_NODE) {
822 if (siblings == node) {
826 siblings = siblings->next;
828 if ((parent == NULL) || (node->doc == NULL))
831 while (parent->parent != NULL)
832 parent = parent->parent;
833 if (((parent->type != XML_DOCUMENT_NODE) &&
834 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
835 (parent != (xmlNodePtr) node->doc))
840 ctxt->xpathCtxt->contextSize = len;
841 ctxt->xpathCtxt->proximityPosition = pos;
843 * If the node is in a Value Tree we cannot
846 if ((node->doc != NULL) && (nocache == 0)) {
847 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
849 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
851 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
856 oldNode = ctxt->node;
859 if (step->value == NULL)
861 if (step->comp == NULL)
864 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
869 ctxt->xpathCtxt->contextSize = oldCS;
870 ctxt->xpathCtxt->proximityPosition = oldCP;
872 ctxt->node = oldNode;
876 ctxt->xpathCtxt->contextSize = oldCS;
877 ctxt->xpathCtxt->proximityPosition = oldCP;
879 ctxt->node = oldNode;
883 if (node->type != XML_PI_NODE)
885 if (step->value != NULL) {
886 if (!xmlStrEqual(step->value, node->name))
890 case XSLT_OP_COMMENT:
891 if (node->type != XML_COMMENT_NODE)
895 if ((node->type != XML_TEXT_NODE) &&
896 (node->type != XML_CDATA_SECTION_NODE))
900 switch (node->type) {
901 case XML_DOCUMENT_NODE:
902 case XML_HTML_DOCUMENT_NODE:
903 #ifdef LIBXML_DOCB_ENABLED
904 case XML_DOCB_DOCUMENT_NODE:
906 case XML_ELEMENT_NODE:
907 case XML_CDATA_SECTION_NODE:
909 case XML_COMMENT_NODE:
911 case XML_ATTRIBUTE_NODE:
923 * xsltTestCompMatchList:
924 * @ctxt: a XSLT process context
926 * @comp: the precompiled pattern list
928 * Test wether the node matches one of the patterns in the list
930 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
933 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
934 xsltCompMatchPtr comp) {
937 if ((ctxt == NULL) || (node == NULL))
939 while (comp != NULL) {
940 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
948 /************************************************************************
950 * Dedicated parser for templates *
952 ************************************************************************/
954 #define CUR (*ctxt->cur)
955 #define SKIP(val) ctxt->cur += (val)
956 #define NXT(val) ctxt->cur[(val)]
957 #define CUR_PTR ctxt->cur
959 #define SKIP_BLANKS \
960 while (IS_BLANK(CUR)) NEXT
962 #define CURRENT (*ctxt->cur)
963 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
966 #define PUSH(op, val, val2) \
967 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
970 xsltSwapTopCompMatch(ctxt->comp);
972 #define XSLT_ERROR(X) \
973 { xsltError(ctxt, __FILE__, __LINE__, X); \
974 ctxt->error = (X); return; }
976 #define XSLT_ERROR0(X) \
977 { xsltError(ctxt, __FILE__, __LINE__, X); \
978 ctxt->error = (X); return(0); }
982 * @ctxt: the XPath Parser context
984 * Parse an XPath Litteral:
986 * [29] Literal ::= '"' [^"]* '"'
989 * Returns the Literal parsed or NULL
993 xsltScanLiteral(xsltParserContextPtr ctxt) {
994 const xmlChar *q, *cur;
1002 val = xmlStringCurrentChar(NULL, cur, &len);
1003 while ((IS_CHAR(val)) && (val != '"')) {
1005 val = xmlStringCurrentChar(NULL, cur, &len);
1007 if (!IS_CHAR(val)) {
1011 ret = xmlStrndup(q, cur - q);
1015 } else if (CUR == '\'') {
1018 val = xmlStringCurrentChar(NULL, cur, &len);
1019 while ((IS_CHAR(val)) && (val != '\'')) {
1021 val = xmlStringCurrentChar(NULL, cur, &len);
1023 if (!IS_CHAR(val)) {
1027 ret = xmlStrndup(q, cur - q);
1032 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1041 * @ctxt: the XPath Parser context
1043 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1044 * CombiningChar | Extender
1046 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1048 * [6] Names ::= Name (S Name)*
1050 * Returns the Name parsed or NULL
1054 xsltScanName(xsltParserContextPtr ctxt) {
1055 const xmlChar *q, *cur;
1056 xmlChar *ret = NULL;
1062 val = xmlStringCurrentChar(NULL, cur, &len);
1063 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1066 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1067 (val == '.') || (val == '-') ||
1069 (IS_COMBINING(val)) ||
1070 (IS_EXTENDER(val))) {
1072 val = xmlStringCurrentChar(NULL, cur, &len);
1074 ret = xmlStrndup(q, cur - q);
1081 * @ctxt: the XPath Parser context
1083 * Parses a non qualified name
1085 * Returns the Name parsed or NULL
1089 xsltScanNCName(xsltParserContextPtr ctxt) {
1090 const xmlChar *q, *cur;
1091 xmlChar *ret = NULL;
1097 val = xmlStringCurrentChar(NULL, cur, &len);
1098 if (!IS_LETTER(val) && (val != '_'))
1101 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1102 (val == '.') || (val == '-') ||
1104 (IS_COMBINING(val)) ||
1105 (IS_EXTENDER(val))) {
1107 val = xmlStringCurrentChar(NULL, cur, &len);
1109 ret = xmlStrndup(q, cur - q);
1116 * @ctxt: the XPath Parser context
1117 * @prefix: the place to store the prefix
1119 * Parse a qualified name
1121 * Returns the Name parsed or NULL
1125 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1126 xmlChar *ret = NULL;
1129 ret = xsltScanNCName(ctxt);
1133 ret = xsltScanNCName(ctxt);
1139 * xsltCompileIdKeyPattern:
1140 * @ctxt: the compilation context
1141 * @name: a preparsed name
1142 * @aid: whether id/key are allowed there
1144 * Compile the XSLT LocationIdKeyPattern
1145 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1146 * | 'key' '(' Literal ',' Literal ')'
1148 * also handle NodeType and PI from:
1150 * [7] NodeTest ::= NameTest
1151 * | NodeType '(' ')'
1152 * | 'processing-instruction' '(' Literal ')'
1155 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1156 xmlChar *lit = NULL;
1157 xmlChar *lit2 = NULL;
1160 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1161 xsltGenericError(xsltGenericErrorContext,
1162 "xsltCompileIdKeyPattern : ( expected\n");
1166 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1169 lit = xsltScanLiteral(ctxt);
1174 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1175 xsltGenericError(xsltGenericErrorContext,
1176 "xsltCompileIdKeyPattern : ) expected\n");
1181 PUSH(XSLT_OP_ID, lit, NULL);
1182 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1185 lit = xsltScanLiteral(ctxt);
1190 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1191 xsltGenericError(xsltGenericErrorContext,
1192 "xsltCompileIdKeyPattern : , expected\n");
1198 lit2 = xsltScanLiteral(ctxt);
1203 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1204 xsltGenericError(xsltGenericErrorContext,
1205 "xsltCompileIdKeyPattern : ) expected\n");
1210 /* TODO: support namespace in keys */
1211 PUSH(XSLT_OP_KEY, lit, lit2);
1212 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1216 lit = xsltScanLiteral(ctxt);
1221 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1222 xsltGenericError(xsltGenericErrorContext,
1223 "xsltCompileIdKeyPattern : ) expected\n");
1229 PUSH(XSLT_OP_PI, lit, NULL);
1230 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1234 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1235 xsltGenericError(xsltGenericErrorContext,
1236 "xsltCompileIdKeyPattern : ) expected\n");
1241 PUSH(XSLT_OP_TEXT, NULL, NULL);
1242 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1246 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1247 xsltGenericError(xsltGenericErrorContext,
1248 "xsltCompileIdKeyPattern : ) expected\n");
1253 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1254 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1258 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1259 xsltGenericError(xsltGenericErrorContext,
1260 "xsltCompileIdKeyPattern : ) expected\n");
1265 PUSH(XSLT_OP_NODE, NULL, NULL);
1267 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1268 xsltGenericError(xsltGenericErrorContext,
1269 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1273 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1274 xsltGenericError(xsltGenericErrorContext,
1275 "xsltCompileIdKeyPattern : node type\n");
1285 * xsltCompileStepPattern:
1286 * @ctxt: the compilation context
1287 * @token: a posible precompiled name
1289 * Compile the XSLT StepPattern and generates a precompiled
1290 * form suitable for fast matching.
1292 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1293 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1294 * | ('child' | 'attribute') '::'
1296 * [7] NodeTest ::= NameTest
1297 * | NodeType '(' ')'
1298 * | 'processing-instruction' '(' Literal ')'
1299 * [8] Predicate ::= '[' PredicateExpr ']'
1300 * [9] PredicateExpr ::= Expr
1301 * [13] AbbreviatedAxisSpecifier ::= '@'?
1302 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1306 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1307 xmlChar *name = NULL;
1308 const xmlChar *URI = NULL;
1309 xmlChar *URL = NULL;
1313 if ((token == NULL) && (CUR == '@')) {
1314 xmlChar *prefix = NULL;
1319 PUSH(XSLT_OP_ATTR, NULL, NULL);
1322 token = xsltScanQName(ctxt, &prefix);
1323 if (prefix != NULL) {
1326 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1328 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1329 xsltGenericError(xsltGenericErrorContext,
1330 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1333 URL = xmlStrdup(ns->href);
1337 if (token == NULL) {
1340 PUSH(XSLT_OP_ATTR, NULL, URL);
1343 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1344 xsltGenericError(xsltGenericErrorContext,
1345 "xsltCompileStepPattern : Name expected\n");
1349 PUSH(XSLT_OP_ATTR, token, URL);
1353 token = xsltScanName(ctxt);
1354 if (token == NULL) {
1357 PUSH(XSLT_OP_ALL, token, NULL);
1358 goto parse_predicate;
1360 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1361 xsltGenericError(xsltGenericErrorContext,
1362 "xsltCompileStepPattern : Name expected\n");
1371 xsltCompileIdKeyPattern(ctxt, token, 0);
1374 } else if (CUR == ':') {
1377 xmlChar *prefix = token;
1381 * This is a namespace match
1383 token = xsltScanName(ctxt);
1384 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1386 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1387 xsltGenericError(xsltGenericErrorContext,
1388 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1393 URL = xmlStrdup(ns->href);
1396 if (token == NULL) {
1399 PUSH(XSLT_OP_NS, URL, NULL);
1401 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1402 xsltGenericError(xsltGenericErrorContext,
1403 "xsltCompileStepPattern : Name expected\n");
1408 PUSH(XSLT_OP_ELEM, token, URL);
1412 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1414 token = xsltScanName(ctxt);
1415 if (token == NULL) {
1418 PUSH(XSLT_OP_ALL, token, NULL);
1419 goto parse_predicate;
1421 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1422 xsltGenericError(xsltGenericErrorContext,
1423 "xsltCompileStepPattern : QName expected\n");
1428 URI = xsltGetQNameURI(ctxt->elem, &token);
1429 if (token == NULL) {
1433 name = xmlStrdup(token);
1435 URL = xmlStrdup(URI);
1437 PUSH(XSLT_OP_CHILD, name, URL);
1438 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1440 token = xsltScanName(ctxt);
1441 if (token == NULL) {
1442 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1443 xsltGenericError(xsltGenericErrorContext,
1444 "xsltCompileStepPattern : QName expected\n");
1448 URI = xsltGetQNameURI(ctxt->elem, &token);
1449 if (token == NULL) {
1453 name = xmlStrdup(token);
1455 URL = xmlStrdup(URI);
1457 PUSH(XSLT_OP_ATTR, name, URL);
1459 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1460 xsltGenericError(xsltGenericErrorContext,
1461 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1467 } else if (CUR == '*') {
1469 PUSH(XSLT_OP_ALL, token, NULL);
1471 URI = xsltGetQNameURI(ctxt->elem, &token);
1472 if (token == NULL) {
1477 URL = xmlStrdup(URI);
1478 PUSH(XSLT_OP_ELEM, token, URL);
1483 while (CUR == '[') {
1485 xmlChar *ret = NULL;
1491 /* Skip over nested predicates */
1494 else if (CUR == ']') {
1498 } else if (CUR == '"') {
1500 while ((CUR != 0) && (CUR != '"'))
1502 } else if (CUR == '\'') {
1504 while ((CUR != 0) && (CUR != '\''))
1510 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1511 xsltGenericError(xsltGenericErrorContext,
1512 "xsltCompileStepPattern : ']' expected\n");
1516 ret = xmlStrndup(q, CUR_PTR - q);
1517 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1518 /* push the predicate lower than local test */
1532 * xsltCompileRelativePathPattern:
1533 * @comp: the compilation context
1534 * @token: a posible precompiled name
1536 * Compile the XSLT RelativePathPattern and generates a precompiled
1537 * form suitable for fast matching.
1539 * [4] RelativePathPattern ::= StepPattern
1540 * | RelativePathPattern '/' StepPattern
1541 * | RelativePathPattern '//' StepPattern
1544 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1545 xsltCompileStepPattern(ctxt, token);
1549 while ((CUR != 0) && (CUR != '|')) {
1550 if ((CUR == '/') && (NXT(1) == '/')) {
1551 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1555 xsltCompileStepPattern(ctxt, NULL);
1556 } else if (CUR == '/') {
1557 PUSH(XSLT_OP_PARENT, NULL, NULL);
1560 if ((CUR != 0) || (CUR == '|')) {
1561 xsltCompileRelativePathPattern(ctxt, NULL);
1575 * xsltCompileLocationPathPattern:
1576 * @ctxt: the compilation context
1578 * Compile the XSLT LocationPathPattern and generates a precompiled
1579 * form suitable for fast matching.
1581 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1582 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1583 * | '//'? RelativePathPattern
1586 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1588 if ((CUR == '/') && (NXT(1) == '/')) {
1590 * since we reverse the query
1591 * a leading // can be safely ignored
1595 xsltCompileRelativePathPattern(ctxt, NULL);
1596 } else if (CUR == '/') {
1598 * We need to find root as the parent
1602 PUSH(XSLT_OP_ROOT, NULL, NULL);
1603 if ((CUR != 0) || (CUR == '|')) {
1604 PUSH(XSLT_OP_PARENT, NULL, NULL);
1605 xsltCompileRelativePathPattern(ctxt, NULL);
1607 } else if (CUR == '*') {
1608 xsltCompileRelativePathPattern(ctxt, NULL);
1609 } else if (CUR == '@') {
1610 xsltCompileRelativePathPattern(ctxt, NULL);
1613 name = xsltScanName(ctxt);
1615 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1616 xsltGenericError(xsltGenericErrorContext,
1617 "xsltCompileLocationPathPattern : Name expected\n");
1622 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1623 xsltCompileIdKeyPattern(ctxt, name, 1);
1624 if ((CUR == '/') && (NXT(1) == '/')) {
1625 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1629 xsltCompileRelativePathPattern(ctxt, NULL);
1630 } else if (CUR == '/') {
1631 PUSH(XSLT_OP_PARENT, NULL, NULL);
1634 xsltCompileRelativePathPattern(ctxt, NULL);
1638 xsltCompileRelativePathPattern(ctxt, name);
1645 * xsltCompilePattern:
1646 * @pattern: an XSLT pattern
1647 * @doc: the containing document
1648 * @node: the containing element
1649 * @style: the stylesheet
1650 * @runtime: the transformation context, if done at run-time
1652 * Compile the XSLT pattern and generates a list of precompiled form suitable
1653 * for fast matching.
1655 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1657 * Returns the generated pattern list or NULL in case of failure
1661 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1662 xmlNodePtr node, xsltStylesheetPtr style,
1663 xsltTransformContextPtr runtime) {
1664 xsltParserContextPtr ctxt = NULL;
1665 xsltCompMatchPtr element, first = NULL, previous = NULL;
1666 int current, start, end, level, j;
1668 if (pattern == NULL) {
1669 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1670 xsltGenericError(xsltGenericErrorContext,
1671 "xsltCompilePattern : NULL pattern\n");
1675 ctxt = xsltNewParserContext(style, runtime);
1681 while (pattern[current] != 0) {
1683 while (IS_BLANK(pattern[current]))
1687 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1688 if (pattern[end] == '[')
1690 else if (pattern[end] == ']')
1692 else if (pattern[end] == '\'') {
1694 while ((pattern[end] != 0) && (pattern[end] != '\''))
1696 } else if (pattern[end] == '"') {
1698 while ((pattern[end] != 0) && (pattern[end] != '"'))
1703 if (current == end) {
1704 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1705 xsltGenericError(xsltGenericErrorContext,
1706 "xsltCompilePattern : NULL pattern\n");
1709 element = xsltNewCompMatch();
1710 if (element == NULL) {
1715 else if (previous != NULL)
1716 previous->next = element;
1719 ctxt->comp = element;
1720 ctxt->base = xmlStrndup(&pattern[start], end - start);
1721 if (ctxt->base == NULL)
1723 ctxt->cur = &(ctxt->base)[current - start];
1724 element->pattern = ctxt->base;
1725 element->nsList = xmlGetNsList(doc, node);
1727 if (element->nsList != NULL) {
1728 while (element->nsList[j] != NULL)
1734 #ifdef WITH_XSLT_DEBUG_PATTERN
1735 xsltGenericDebug(xsltGenericDebugContext,
1736 "xsltCompilePattern : parsing '%s'\n",
1739 xsltCompileLocationPathPattern(ctxt);
1744 * Reverse for faster interpretation.
1746 xsltReverseCompMatch(element);
1749 * Set-up the priority
1751 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1752 (element->steps[0].op == XSLT_OP_ATTR)) &&
1753 (element->steps[0].value != NULL) &&
1754 (element->steps[1].op == XSLT_OP_END)) {
1755 element->priority = 0;
1757 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1758 (element->steps[1].op == XSLT_OP_END)) {
1759 element->priority = 0;
1761 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1762 (element->steps[0].value != NULL) &&
1763 (element->steps[1].op == XSLT_OP_END)) {
1764 element->priority = 0;
1765 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1766 (element->steps[0].value2 != NULL) &&
1767 (element->steps[1].op == XSLT_OP_END)) {
1768 element->priority = -0.25;
1769 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1770 (element->steps[0].value != NULL) &&
1771 (element->steps[1].op == XSLT_OP_END)) {
1772 element->priority = -0.25;
1773 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1774 (element->steps[0].value == NULL) &&
1775 (element->steps[0].value2 == NULL) &&
1776 (element->steps[1].op == XSLT_OP_END)) {
1777 element->priority = -0.5;
1778 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1779 (element->steps[0].op == XSLT_OP_TEXT) ||
1780 (element->steps[0].op == XSLT_OP_ALL) ||
1781 (element->steps[0].op == XSLT_OP_NODE) ||
1782 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1783 (element->steps[1].op == XSLT_OP_END)) {
1784 element->priority = -0.5;
1786 element->priority = 0.5;
1788 #ifdef WITH_XSLT_DEBUG_PATTERN
1789 xsltGenericDebug(xsltGenericDebugContext,
1790 "xsltCompilePattern : parsed %s, default priority %f\n",
1791 element->pattern, element->priority);
1793 if (pattern[end] == '|')
1798 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1799 xsltGenericError(xsltGenericErrorContext,
1800 "xsltCompilePattern : NULL pattern\n");
1804 xsltFreeParserContext(ctxt);
1809 xsltFreeParserContext(ctxt);
1811 xsltFreeCompMatchList(first);
1815 /************************************************************************
1817 * Module interfaces *
1819 ************************************************************************/
1823 * @style: an XSLT stylesheet
1824 * @cur: an XSLT template
1825 * @mode: the mode name or NULL
1826 * @modeURI: the mode URI or NULL
1828 * Register the XSLT pattern associated to @cur
1830 * Returns -1 in case of error, 0 otherwise
1833 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1834 const xmlChar *mode, const xmlChar *modeURI) {
1835 xsltCompMatchPtr pat, list, *top = NULL, next;
1836 const xmlChar *name = NULL;
1837 float priority; /* the priority */
1839 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1842 priority = cur->priority;
1843 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1849 pat->template = cur;
1851 pat->mode = xmlStrdup(mode);
1852 if (modeURI != NULL)
1853 pat->modeURI = xmlStrdup(modeURI);
1854 if (priority != XSLT_PAT_NO_PRIORITY)
1855 pat->priority = priority;
1858 * insert it in the hash table list corresponding to its lookup name
1860 switch (pat->steps[0].op) {
1862 if (pat->steps[0].value != NULL)
1863 name = pat->steps[0].value;
1865 top = (xsltCompMatchPtr *) &(style->attrMatch);
1869 case XSLT_OP_PARENT:
1870 case XSLT_OP_ANCESTOR:
1871 top = (xsltCompMatchPtr *) &(style->elemMatch);
1874 top = (xsltCompMatchPtr *) &(style->rootMatch);
1877 top = (xsltCompMatchPtr *) &(style->keyMatch);
1880 /* TODO optimize ID !!! */
1883 top = (xsltCompMatchPtr *) &(style->elemMatch);
1886 case XSLT_OP_PREDICATE:
1887 xsltPrintErrorContext(NULL, style, NULL);
1888 xsltGenericError(xsltGenericErrorContext,
1889 "xsltAddTemplate: invalid compiled pattern\n");
1890 xsltFreeCompMatch(pat);
1893 * TODO: some flags at the top level about type based patterns
1894 * would be faster than inclusion in the hash table.
1897 if (pat->steps[0].value != NULL)
1898 name = pat->steps[0].value;
1900 top = (xsltCompMatchPtr *) &(style->piMatch);
1902 case XSLT_OP_COMMENT:
1903 top = (xsltCompMatchPtr *) &(style->commentMatch);
1906 top = (xsltCompMatchPtr *) &(style->textMatch);
1909 if (pat->steps[0].value != NULL)
1910 name = pat->steps[0].value;
1912 top = (xsltCompMatchPtr *) &(style->elemMatch);
1917 if (style->templatesHash == NULL) {
1918 style->templatesHash = xmlHashCreate(1024);
1919 if (style->templatesHash == NULL) {
1920 xsltFreeCompMatch(pat);
1923 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1925 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1926 name, mode, modeURI);
1928 xmlHashAddEntry3(style->templatesHash, name,
1929 mode, modeURI, pat);
1932 * Note '<=' since one must choose among the matching
1933 * template rules that are left, the one that occurs
1934 * last in the stylesheet
1936 if (list->priority <= pat->priority) {
1938 xmlHashUpdateEntry3(style->templatesHash, name,
1939 mode, modeURI, pat, NULL);
1941 while (list->next != NULL) {
1942 if (list->next->priority <= pat->priority)
1946 pat->next = list->next;
1951 } else if (top != NULL) {
1956 } else if (list->priority <= pat->priority) {
1960 while (list->next != NULL) {
1961 if (list->next->priority <= pat->priority)
1965 pat->next = list->next;
1969 xsltPrintErrorContext(NULL, style, NULL);
1970 xsltGenericError(xsltGenericErrorContext,
1971 "xsltAddTemplate: invalid compiled pattern\n");
1972 xsltFreeCompMatch(pat);
1975 #ifdef WITH_XSLT_DEBUG_PATTERN
1977 xsltGenericDebug(xsltGenericDebugContext,
1978 "added pattern : '%s' mode '%s' priority %f\n",
1979 pat->pattern, pat->mode, pat->priority);
1981 xsltGenericDebug(xsltGenericDebugContext,
1982 "added pattern : '%s' priority %f\n",
1983 pat->pattern, pat->priority);
1993 * @ctxt: a XSLT process context
1994 * @node: the node being processed
1995 * @style: the current style
1997 * Finds the template applying to this node, if @style is non-NULL
1998 * it means one needs to look for the next imported template in scope.
2000 * Returns the xsltTemplatePtr or NULL if not found
2003 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2004 xsltStylesheetPtr style) {
2005 xsltStylesheetPtr curstyle;
2006 xsltTemplatePtr ret = NULL;
2007 const xmlChar *name = NULL;
2008 xsltCompMatchPtr list = NULL;
2011 if ((ctxt == NULL) || (node == NULL))
2014 if (style == NULL) {
2015 curstyle = ctxt->style;
2017 curstyle = xsltNextImport(style);
2020 while ((curstyle != NULL) && (curstyle != style)) {
2021 priority = XSLT_PAT_NO_PRIORITY;
2022 /* TODO : handle IDs/keys here ! */
2023 if (curstyle->templatesHash != NULL) {
2025 * Use the top name as selector
2027 switch (node->type) {
2028 case XML_ELEMENT_NODE:
2029 case XML_ATTRIBUTE_NODE:
2033 case XML_DOCUMENT_NODE:
2034 case XML_HTML_DOCUMENT_NODE:
2036 case XML_CDATA_SECTION_NODE:
2037 case XML_COMMENT_NODE:
2038 case XML_ENTITY_REF_NODE:
2039 case XML_ENTITY_NODE:
2040 case XML_DOCUMENT_TYPE_NODE:
2041 case XML_DOCUMENT_FRAG_NODE:
2042 case XML_NOTATION_NODE:
2044 case XML_ELEMENT_DECL:
2045 case XML_ATTRIBUTE_DECL:
2046 case XML_ENTITY_DECL:
2047 case XML_NAMESPACE_DECL:
2048 case XML_XINCLUDE_START:
2049 case XML_XINCLUDE_END:
2058 * find the list of appliable expressions based on the name
2060 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2061 name, ctxt->mode, ctxt->modeURI);
2064 while (list != NULL) {
2065 if (xsltTestCompMatch(ctxt, list, node,
2066 ctxt->mode, ctxt->modeURI)) {
2067 ret = list->template;
2068 priority = list->priority;
2076 * find alternate generic matches
2078 switch (node->type) {
2079 case XML_ELEMENT_NODE:
2080 list = curstyle->elemMatch;
2082 case XML_ATTRIBUTE_NODE:
2083 list = curstyle->attrMatch;
2086 list = curstyle->piMatch;
2088 case XML_DOCUMENT_NODE:
2089 case XML_HTML_DOCUMENT_NODE:
2090 list = curstyle->rootMatch;
2093 case XML_CDATA_SECTION_NODE:
2094 list = curstyle->textMatch;
2096 case XML_COMMENT_NODE:
2097 list = curstyle->commentMatch;
2099 case XML_ENTITY_REF_NODE:
2100 case XML_ENTITY_NODE:
2101 case XML_DOCUMENT_TYPE_NODE:
2102 case XML_DOCUMENT_FRAG_NODE:
2103 case XML_NOTATION_NODE:
2105 case XML_ELEMENT_DECL:
2106 case XML_ATTRIBUTE_DECL:
2107 case XML_ENTITY_DECL:
2108 case XML_NAMESPACE_DECL:
2109 case XML_XINCLUDE_START:
2110 case XML_XINCLUDE_END:
2116 while ((list != NULL) &&
2117 ((ret == NULL) || (list->priority > priority))) {
2118 if (xsltTestCompMatch(ctxt, list, node,
2119 ctxt->mode, ctxt->modeURI)) {
2120 ret = list->template;
2121 priority = list->priority;
2127 * Some of the tests for elements can also apply to documents
2129 if ((node->type == XML_DOCUMENT_NODE) ||
2130 (node->type == XML_HTML_DOCUMENT_NODE)) {
2131 list = curstyle->elemMatch;
2132 while ((list != NULL) &&
2133 ((ret == NULL) || (list->priority > priority))) {
2134 if (xsltTestCompMatch(ctxt, list, node,
2135 ctxt->mode, ctxt->modeURI)) {
2136 ret = list->template;
2137 priority = list->priority;
2144 if (node->_private != NULL) {
2145 list = curstyle->keyMatch;
2146 while ((list != NULL) &&
2147 ((ret == NULL) || (list->priority > priority))) {
2148 if (xsltTestCompMatch(ctxt, list, node,
2149 ctxt->mode, ctxt->modeURI)) {
2150 ret = list->template;
2151 priority = list->priority;
2161 * Cycle on next curstylesheet import.
2163 curstyle = xsltNextImport(curstyle);
2169 * xsltCleanupTemplates:
2170 * @style: an XSLT stylesheet
2172 * Cleanup the state of the templates used by the stylesheet and
2173 * the ones it imports.
2176 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2180 * xsltFreeTemplateHashes:
2181 * @style: an XSLT stylesheet
2183 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2186 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2187 if (style->templatesHash != NULL)
2188 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2189 (xmlHashDeallocator) xsltFreeCompMatchList);
2190 if (style->rootMatch != NULL)
2191 xsltFreeCompMatchList(style->rootMatch);
2192 if (style->keyMatch != NULL)
2193 xsltFreeCompMatchList(style->keyMatch);
2194 if (style->elemMatch != NULL)
2195 xsltFreeCompMatchList(style->elemMatch);
2196 if (style->attrMatch != NULL)
2197 xsltFreeCompMatchList(style->attrMatch);
2198 if (style->parentMatch != NULL)
2199 xsltFreeCompMatchList(style->parentMatch);
2200 if (style->textMatch != NULL)
2201 xsltFreeCompMatchList(style->textMatch);
2202 if (style->piMatch != NULL)
2203 xsltFreeCompMatchList(style->piMatch);
2204 if (style->commentMatch != NULL)
2205 xsltFreeCompMatchList(style->commentMatch);
2211 * @node: a node in the source tree
2212 * @pattern: an XSLT pattern
2213 * @ctxtdoc: context document (for namespaces)
2214 * @ctxtnode: context node (for namespaces)
2216 * Determine if a node matches a pattern.
2219 xsltMatchPattern(xsltTransformContextPtr context,
2221 const xmlChar *pattern,
2223 xmlNodePtr ctxtnode)
2226 xsltCompMatchPtr first, comp;
2228 if ((context != NULL) && (pattern != NULL)) {
2229 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2230 for (comp = first; comp != NULL; comp = comp->next) {
2231 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2236 xsltFreeCompMatchList(first);