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
21 #include <libxml/xmlmemory.h>
22 #include <libxml/tree.h>
23 #include <libxml/valid.h>
24 #include <libxml/hash.h>
25 #include <libxml/xmlerror.h>
26 #include <libxml/parserInternals.h>
28 #include "xsltInternals.h"
29 #include "xsltutils.h"
31 #include "templates.h"
35 #ifdef WITH_XSLT_DEBUG
36 #define WITH_XSLT_DEBUG_PATTERN
63 typedef struct _xsltStepOp xsltStepOp;
64 typedef xsltStepOp *xsltStepOpPtr;
70 xmlXPathCompExprPtr comp;
72 * Optimisations for count
79 struct _xsltCompMatch {
80 struct _xsltCompMatch *next; /* siblings in the name hash */
81 float priority; /* the priority */
82 const xmlChar *pattern; /* the pattern */
83 const xmlChar *mode; /* the mode */
84 const xmlChar *modeURI; /* the mode URI */
85 xsltTemplatePtr template; /* the associated template */
87 /* TODO fix the statically allocated size steps[] */
90 xmlNsPtr *nsList; /* the namespaces in scope */
91 int nsNr; /* the number of namespaces in scope */
92 xsltStepOp steps[40]; /* ops for computation */
95 typedef struct _xsltParserContext xsltParserContext;
96 typedef xsltParserContext *xsltParserContextPtr;
97 struct _xsltParserContext {
98 xsltStylesheetPtr style; /* the stylesheet */
99 xsltTransformContextPtr ctxt; /* the transformation or NULL */
100 const xmlChar *cur; /* the current char being parsed */
101 const xmlChar *base; /* the full expression */
102 xmlDocPtr doc; /* the source document */
103 xmlNodePtr elem; /* the source element */
104 int error; /* error code */
105 xsltCompMatchPtr comp; /* the result */
108 /************************************************************************
112 ************************************************************************/
117 * Create a new XSLT CompMatch
119 * Returns the newly allocated xsltCompMatchPtr or NULL in case of error
121 static xsltCompMatchPtr
122 xsltNewCompMatch(void) {
123 xsltCompMatchPtr cur;
125 cur = (xsltCompMatchPtr) xmlMalloc(sizeof(xsltCompMatch));
127 xsltPrintErrorContext(NULL, NULL, NULL);
128 xsltGenericError(xsltGenericErrorContext,
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 * xsltNewParserContext:
194 * @style: the stylesheet
195 * @ctxt: the transformation context, if done at run-time
197 * Create a new XSLT ParserContext
199 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
201 static xsltParserContextPtr
202 xsltNewParserContext(xsltStylesheetPtr style, xsltTransformContextPtr ctxt) {
203 xsltParserContextPtr cur;
205 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
207 xsltPrintErrorContext(NULL, NULL, NULL);
208 xsltGenericError(xsltGenericErrorContext,
209 "xsltNewParserContext : malloc failed\n");
212 memset(cur, 0, sizeof(xsltParserContext));
219 * xsltFreeParserContext:
220 * @ctxt: an XSLT parser context
222 * Free up the memory allocated by @ctxt
225 xsltFreeParserContext(xsltParserContextPtr ctxt) {
228 memset(ctxt, -1, sizeof(xsltParserContext));
234 * @comp: the compiled match expression
236 * @value: the first value
237 * @value2: the second value
239 * Add an step to an XSLT Compiled Match
241 * Returns -1 in case of failure, 0 otherwise.
244 xsltCompMatchAdd(xsltParserContextPtr ctxt, xsltCompMatchPtr comp,
245 xsltOp op, xmlChar * value, xmlChar * value2)
247 if (comp->nbStep >= 40) {
248 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
249 xsltGenericError(xsltGenericErrorContext,
250 "xsltCompMatchAdd: overflow\n");
253 comp->steps[comp->nbStep].op = op;
254 comp->steps[comp->nbStep].value = value;
255 comp->steps[comp->nbStep].value2 = value2;
256 if (ctxt->ctxt != NULL) {
257 comp->steps[comp->nbStep].previousExtra =
258 xsltAllocateExtraCtxt(ctxt->ctxt);
259 comp->steps[comp->nbStep].indexExtra =
260 xsltAllocateExtraCtxt(ctxt->ctxt);
261 comp->steps[comp->nbStep].lenExtra =
262 xsltAllocateExtraCtxt(ctxt->ctxt);
264 comp->steps[comp->nbStep].previousExtra =
265 xsltAllocateExtra(ctxt->style);
266 comp->steps[comp->nbStep].indexExtra =
267 xsltAllocateExtra(ctxt->style);
268 comp->steps[comp->nbStep].lenExtra =
269 xsltAllocateExtra(ctxt->style);
276 * xsltSwapTopCompMatch:
277 * @comp: the compiled match expression
279 * reverse the two top steps.
282 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
284 int j = comp->nbStep - 1;
287 register xmlChar *tmp;
290 tmp = comp->steps[i].value;
291 comp->steps[i].value = comp->steps[j].value;
292 comp->steps[j].value = tmp;
293 tmp = comp->steps[i].value2;
294 comp->steps[i].value2 = comp->steps[j].value2;
295 comp->steps[j].value2 = tmp;
296 op = comp->steps[i].op;
297 comp->steps[i].op = comp->steps[j].op;
298 comp->steps[j].op = op;
303 * xsltReverseCompMatch:
304 * @comp: the compiled match expression
306 * reverse all the stack of expressions
309 xsltReverseCompMatch(xsltCompMatchPtr comp) {
311 int j = comp->nbStep - 1;
314 register xmlChar *tmp;
316 tmp = comp->steps[i].value;
317 comp->steps[i].value = comp->steps[j].value;
318 comp->steps[j].value = tmp;
319 tmp = comp->steps[i].value2;
320 comp->steps[i].value2 = comp->steps[j].value2;
321 comp->steps[j].value2 = tmp;
322 op = comp->steps[i].op;
323 comp->steps[i].op = comp->steps[j].op;
324 comp->steps[j].op = op;
328 comp->steps[comp->nbStep++].op = XSLT_OP_END;
331 /************************************************************************
333 * The interpreter for the precompiled patterns *
335 ************************************************************************/
339 * @ctxt: a XSLT process context
340 * @comp: the precompiled pattern
342 * @mode: the mode name or NULL
343 * @modeURI: the mode URI or NULL
345 * Test wether the node matches the pattern
347 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
350 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
351 xmlNodePtr node, const xmlChar *mode,
352 const xmlChar *modeURI) {
354 xsltStepOpPtr step, select = NULL;
356 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
357 xsltPrintErrorContext(ctxt, NULL, node);
358 xsltGenericError(xsltGenericErrorContext,
359 "xsltTestCompMatch: null arg\n");
363 if (comp->mode == NULL)
365 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
368 if (comp->mode != NULL)
371 if (modeURI != NULL) {
372 if (comp->modeURI == NULL)
374 if ((comp->modeURI != modeURI) &&
375 (!xmlStrEqual(comp->modeURI, modeURI)))
378 if (comp->modeURI != NULL)
381 for (i = 0;i < comp->nbStep;i++) {
382 step = &comp->steps[i];
383 if (step->op != XSLT_OP_PREDICATE)
389 if ((node->type == XML_DOCUMENT_NODE) ||
390 #ifdef LIBXML_DOCB_ENABLED
391 (node->type == XML_DOCB_DOCUMENT_NODE) ||
393 (node->type == XML_HTML_DOCUMENT_NODE))
397 if (node->type != XML_ELEMENT_NODE)
399 if (step->value == NULL)
401 if (!xmlStrEqual(step->value, node->name))
405 if (node->ns == NULL) {
406 if (step->value2 != NULL)
408 } else if (node->ns->href != NULL) {
409 if (step->value2 == NULL)
411 if (!xmlStrEqual(step->value2, node->ns->href))
415 case XSLT_OP_CHILD: {
418 if ((node->type != XML_ELEMENT_NODE) &&
419 (node->type != XML_DOCUMENT_NODE) &&
420 #ifdef LIBXML_DOCB_ENABLED
421 (node->type != XML_DOCB_DOCUMENT_NODE) &&
423 (node->type != XML_HTML_DOCUMENT_NODE))
426 lst = node->children;
428 if (step->value != NULL) {
429 while (lst != NULL) {
430 if ((lst->type == XML_ELEMENT_NODE) &&
431 (xmlStrEqual(step->value, lst->name)))
441 if (node->type != XML_ATTRIBUTE_NODE)
443 if (step->value == NULL)
445 if (!xmlStrEqual(step->value, node->name))
449 if (node->ns == NULL) {
450 if (step->value2 != NULL)
452 } else if (node->ns->href != NULL) {
453 if (step->value2 == NULL)
455 if (!xmlStrEqual(step->value2, node->ns->href))
460 if ((node->type != XML_ELEMENT_NODE) &&
461 (node->type != XML_ATTRIBUTE_NODE))
466 if (step->value == NULL)
468 if (!xmlStrEqual(step->value, node->name))
471 if (node->ns == NULL) {
472 if (step->value2 != NULL)
474 } else if (node->ns->href != NULL) {
475 if (step->value2 == NULL)
477 if (!xmlStrEqual(step->value2, node->ns->href))
481 case XSLT_OP_ANCESTOR:
482 /* TODO: implement coalescing of ANCESTOR/NODE ops */
483 if (step->value == NULL) {
485 step = &comp->steps[i];
486 if (step->op == XSLT_OP_ROOT)
488 if (step->op != XSLT_OP_ELEM)
490 if (step->value == NULL)
496 while (node != NULL) {
499 if (xmlStrEqual(step->value, node->name)) {
501 if (node->ns == NULL) {
502 if (step->value2 == NULL)
504 } else if (node->ns->href != NULL) {
505 if ((step->value2 != NULL) &&
506 (xmlStrEqual(step->value2, node->ns->href)))
516 /* TODO Handle IDs decently, must be done differently */
519 if (node->type != XML_ELEMENT_NODE)
522 id = xmlGetID(node->doc, step->value);
523 if ((id == NULL) || (id->parent != node))
531 list = xsltGetKey(ctxt, step->value,
532 step->value3, step->value2);
535 for (indx = 0;indx < list->nodeNr;indx++)
536 if (list->nodeTab[indx] == node)
538 if (indx >= list->nodeNr)
543 if (node->type != XML_ELEMENT_NODE)
545 if (node->ns == NULL) {
546 if (step->value != NULL)
548 } else if (node->ns->href != NULL) {
549 if (step->value == NULL)
551 if (!xmlStrEqual(step->value, node->ns->href))
556 if (node->type != XML_ELEMENT_NODE)
559 case XSLT_OP_PREDICATE: {
562 int pos = 0, len = 0;
564 * Depending on the last selection, one may need to
565 * recompute contextSize and proximityPosition.
567 * TODO: make this thread safe !
569 oldCS = ctxt->xpathCtxt->contextSize;
570 oldCP = ctxt->xpathCtxt->proximityPosition;
571 if ((select != NULL) &&
572 (select->op == XSLT_OP_ELEM) &&
573 (select->value != NULL) &&
574 (node->type == XML_ELEMENT_NODE) &&
575 (node->parent != NULL)) {
579 previous = (xmlNodePtr)
580 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
582 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
583 if ((previous != NULL) &&
584 (previous->parent == node->parent)) {
586 * just walk back to adjust the index
589 xmlNodePtr sibling = node;
591 while (sibling != NULL) {
592 if (sibling == previous)
594 if (xmlStrEqual(node->name, sibling->name)) {
595 if ((select->value2 == NULL) ||
596 ((sibling->ns != NULL) &&
597 (xmlStrEqual(select->value2,
598 sibling->ns->href))))
601 sibling = sibling->prev;
603 if (sibling == NULL) {
604 /* hum going backward in document order ... */
607 while (sibling != NULL) {
608 if (sibling == previous)
610 if ((select->value2 == NULL) ||
611 ((sibling->ns != NULL) &&
612 (xmlStrEqual(select->value2,
613 sibling->ns->href))))
615 sibling = sibling->next;
618 if (sibling != NULL) {
621 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
622 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
624 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
631 * recompute the index
633 xmlNodePtr siblings = node->parent->children;
635 while (siblings != NULL) {
636 if (siblings->type == XML_ELEMENT_NODE) {
637 if (siblings == node) {
640 } else if (xmlStrEqual(node->name,
642 if ((select->value2 == NULL) ||
643 ((siblings->ns != NULL) &&
644 (xmlStrEqual(select->value2,
645 siblings->ns->href))))
649 siblings = siblings->next;
653 ctxt->xpathCtxt->contextSize = len;
654 ctxt->xpathCtxt->proximityPosition = pos;
655 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
657 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
659 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
662 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
663 (node->type == XML_ELEMENT_NODE)) {
667 previous = (xmlNodePtr)
668 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
670 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
671 if ((previous != NULL) &&
672 (previous->parent == node->parent)) {
674 * just walk back to adjust the index
677 xmlNodePtr sibling = node;
679 while (sibling != NULL) {
680 if (sibling == previous)
682 if (sibling->type == XML_ELEMENT_NODE)
684 sibling = sibling->prev;
686 if (sibling == NULL) {
687 /* hum going backward in document order ... */
690 while (sibling != NULL) {
691 if (sibling == previous)
693 if (sibling->type == XML_ELEMENT_NODE)
695 sibling = sibling->next;
698 if (sibling != NULL) {
701 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
702 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
704 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
710 * recompute the index
712 xmlNodePtr siblings = node->parent->children;
714 while (siblings != NULL) {
715 if (siblings->type == XML_ELEMENT_NODE) {
717 if (siblings == node) {
721 siblings = siblings->next;
725 ctxt->xpathCtxt->contextSize = len;
726 ctxt->xpathCtxt->proximityPosition = pos;
727 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
729 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
731 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
735 oldNode = ctxt->node;
738 if (step->value == NULL)
741 if (step->comp == NULL) {
742 step->comp = xmlXPathCompile(step->value);
743 if (step->comp == NULL)
746 if (comp->nsList == NULL) {
749 comp->nsList = xmlGetNsList(node->doc, node);
750 if (comp->nsList != NULL) {
751 while (comp->nsList[j] != NULL)
756 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
761 ctxt->xpathCtxt->contextSize = oldCS;
762 ctxt->xpathCtxt->proximityPosition = oldCP;
764 ctxt->node = oldNode;
768 ctxt->xpathCtxt->contextSize = oldCS;
769 ctxt->xpathCtxt->proximityPosition = oldCP;
771 ctxt->node = oldNode;
775 if (node->type != XML_PI_NODE)
777 if (step->value != NULL) {
778 if (!xmlStrEqual(step->value, node->name))
782 case XSLT_OP_COMMENT:
783 if (node->type != XML_COMMENT_NODE)
787 if ((node->type != XML_TEXT_NODE) &&
788 (node->type != XML_CDATA_SECTION_NODE))
792 switch (node->type) {
793 case XML_DOCUMENT_NODE:
794 case XML_HTML_DOCUMENT_NODE:
795 #ifdef LIBXML_DOCB_ENABLED
796 case XML_DOCB_DOCUMENT_NODE:
798 case XML_ELEMENT_NODE:
799 case XML_CDATA_SECTION_NODE:
801 case XML_COMMENT_NODE:
803 case XML_ATTRIBUTE_NODE:
815 * xsltTestCompMatchList:
816 * @ctxt: a XSLT process context
818 * @comp: the precompiled pattern list
820 * Test wether the node matches one of the patterns in the list
822 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
825 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
826 xsltCompMatchPtr comp) {
829 if ((ctxt == NULL) || (node == NULL))
831 while (comp != NULL) {
832 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
840 /************************************************************************
842 * Dedicated parser for templates *
844 ************************************************************************/
846 #define CUR (*ctxt->cur)
847 #define SKIP(val) ctxt->cur += (val)
848 #define NXT(val) ctxt->cur[(val)]
849 #define CUR_PTR ctxt->cur
851 #define SKIP_BLANKS \
852 while (IS_BLANK(CUR)) NEXT
854 #define CURRENT (*ctxt->cur)
855 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
858 #define PUSH(op, val, val2) \
859 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
862 xsltSwapTopCompMatch(ctxt->comp);
864 #define XSLT_ERROR(X) \
865 { xsltError(ctxt, __FILE__, __LINE__, X); \
866 ctxt->error = (X); return; }
868 #define XSLT_ERROR0(X) \
869 { xsltError(ctxt, __FILE__, __LINE__, X); \
870 ctxt->error = (X); return(0); }
874 * @ctxt: the XPath Parser context
876 * Parse an XPath Litteral:
878 * [29] Literal ::= '"' [^"]* '"'
881 * Returns the Literal parsed or NULL
885 xsltScanLiteral(xsltParserContextPtr ctxt) {
886 const xmlChar *q, *cur;
894 val = xmlStringCurrentChar(NULL, cur, &len);
895 while ((IS_CHAR(val)) && (val != '"')) {
897 val = xmlStringCurrentChar(NULL, cur, &len);
903 ret = xmlStrndup(q, cur - q);
907 } else if (CUR == '\'') {
910 val = xmlStringCurrentChar(NULL, cur, &len);
911 while ((IS_CHAR(val)) && (val != '\'')) {
913 val = xmlStringCurrentChar(NULL, cur, &len);
919 ret = xmlStrndup(q, cur - q);
924 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
933 * @ctxt: the XPath Parser context
935 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
936 * CombiningChar | Extender
938 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
940 * [6] Names ::= Name (S Name)*
942 * Returns the Name parsed or NULL
946 xsltScanName(xsltParserContextPtr ctxt) {
947 const xmlChar *q, *cur;
954 val = xmlStringCurrentChar(NULL, cur, &len);
955 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
958 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
959 (val == '.') || (val == '-') ||
961 (IS_COMBINING(val)) ||
962 (IS_EXTENDER(val))) {
964 val = xmlStringCurrentChar(NULL, cur, &len);
966 ret = xmlStrndup(q, cur - q);
973 * @ctxt: the XPath Parser context
975 * Parses a non qualified name
977 * Returns the Name parsed or NULL
981 xsltScanNCName(xsltParserContextPtr ctxt) {
982 const xmlChar *q, *cur;
989 val = xmlStringCurrentChar(NULL, cur, &len);
990 if (!IS_LETTER(val) && (val != '_'))
993 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
994 (val == '.') || (val == '-') ||
996 (IS_COMBINING(val)) ||
997 (IS_EXTENDER(val))) {
999 val = xmlStringCurrentChar(NULL, cur, &len);
1001 ret = xmlStrndup(q, cur - q);
1008 * @ctxt: the XPath Parser context
1009 * @prefix: the place to store the prefix
1011 * Parse a qualified name
1013 * Returns the Name parsed or NULL
1017 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1018 xmlChar *ret = NULL;
1021 ret = xsltScanNCName(ctxt);
1025 ret = xsltScanNCName(ctxt);
1031 * xsltCompileIdKeyPattern:
1032 * @ctxt: the compilation context
1033 * @name: a preparsed name
1034 * @aid: whether id/key are allowed there
1036 * Compile the XSLT LocationIdKeyPattern
1037 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1038 * | 'key' '(' Literal ',' Literal ')'
1040 * also handle NodeType and PI from:
1042 * [7] NodeTest ::= NameTest
1043 * | NodeType '(' ')'
1044 * | 'processing-instruction' '(' Literal ')'
1047 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1048 xmlChar *lit = NULL;
1049 xmlChar *lit2 = NULL;
1052 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1053 xsltGenericError(xsltGenericErrorContext,
1054 "xsltCompileIdKeyPattern : ( expected\n");
1058 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1061 lit = xsltScanLiteral(ctxt);
1066 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1067 xsltGenericError(xsltGenericErrorContext,
1068 "xsltCompileIdKeyPattern : ) expected\n");
1073 PUSH(XSLT_OP_ID, lit, NULL);
1074 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1077 lit = xsltScanLiteral(ctxt);
1082 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1083 xsltGenericError(xsltGenericErrorContext,
1084 "xsltCompileIdKeyPattern : , expected\n");
1090 lit2 = xsltScanLiteral(ctxt);
1095 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1096 xsltGenericError(xsltGenericErrorContext,
1097 "xsltCompileIdKeyPattern : ) expected\n");
1102 /* TODO: support namespace in keys */
1103 PUSH(XSLT_OP_KEY, lit, lit2);
1104 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1108 lit = xsltScanLiteral(ctxt);
1113 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1114 xsltGenericError(xsltGenericErrorContext,
1115 "xsltCompileIdKeyPattern : ) expected\n");
1121 PUSH(XSLT_OP_PI, lit, NULL);
1122 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1126 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1127 xsltGenericError(xsltGenericErrorContext,
1128 "xsltCompileIdKeyPattern : ) expected\n");
1133 PUSH(XSLT_OP_TEXT, NULL, NULL);
1134 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1138 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1139 xsltGenericError(xsltGenericErrorContext,
1140 "xsltCompileIdKeyPattern : ) expected\n");
1145 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1146 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1150 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1151 xsltGenericError(xsltGenericErrorContext,
1152 "xsltCompileIdKeyPattern : ) expected\n");
1157 PUSH(XSLT_OP_NODE, NULL, NULL);
1159 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1160 xsltGenericError(xsltGenericErrorContext,
1161 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1165 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1166 xsltGenericError(xsltGenericErrorContext,
1167 "xsltCompileIdKeyPattern : node type\n");
1177 * xsltCompileStepPattern:
1178 * @ctxt: the compilation context
1179 * @token: a posible precompiled name
1181 * Compile the XSLT StepPattern and generates a precompiled
1182 * form suitable for fast matching.
1184 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1185 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1186 * | ('child' | 'attribute') '::'
1188 * [7] NodeTest ::= NameTest
1189 * | NodeType '(' ')'
1190 * | 'processing-instruction' '(' Literal ')'
1191 * [8] Predicate ::= '[' PredicateExpr ']'
1192 * [9] PredicateExpr ::= Expr
1193 * [13] AbbreviatedAxisSpecifier ::= '@'?
1194 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1198 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1199 xmlChar *name = NULL;
1200 const xmlChar *URI = NULL;
1201 xmlChar *URL = NULL;
1205 if ((token == NULL) && (CUR == '@')) {
1206 xmlChar *prefix = NULL;
1211 PUSH(XSLT_OP_ATTR, NULL, NULL);
1214 token = xsltScanQName(ctxt, &prefix);
1215 if (prefix != NULL) {
1218 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1220 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1221 xsltGenericError(xsltGenericErrorContext,
1222 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1225 URL = xmlStrdup(ns->href);
1229 if (token == NULL) {
1232 PUSH(XSLT_OP_ATTR, NULL, URL);
1235 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1236 xsltGenericError(xsltGenericErrorContext,
1237 "xsltCompileStepPattern : Name expected\n");
1241 PUSH(XSLT_OP_ATTR, token, URL);
1245 token = xsltScanName(ctxt);
1246 if (token == NULL) {
1249 PUSH(XSLT_OP_ALL, token, NULL);
1250 goto parse_predicate;
1252 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1253 xsltGenericError(xsltGenericErrorContext,
1254 "xsltCompileStepPattern : Name expected\n");
1263 xsltCompileIdKeyPattern(ctxt, token, 0);
1266 } else if (CUR == ':') {
1269 xmlChar *prefix = token;
1273 * This is a namespace match
1275 token = xsltScanName(ctxt);
1276 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1278 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1279 xsltGenericError(xsltGenericErrorContext,
1280 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1285 URL = xmlStrdup(ns->href);
1288 if (token == NULL) {
1291 PUSH(XSLT_OP_NS, URL, NULL);
1293 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1294 xsltGenericError(xsltGenericErrorContext,
1295 "xsltCompileStepPattern : Name expected\n");
1300 PUSH(XSLT_OP_ELEM, token, URL);
1304 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1306 token = xsltScanName(ctxt);
1307 if (token == NULL) {
1310 PUSH(XSLT_OP_ALL, token, NULL);
1311 goto parse_predicate;
1313 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1314 xsltGenericError(xsltGenericErrorContext,
1315 "xsltCompileStepPattern : QName expected\n");
1320 URI = xsltGetQNameURI(ctxt->elem, &token);
1321 if (token == NULL) {
1325 name = xmlStrdup(token);
1327 URL = xmlStrdup(URI);
1329 PUSH(XSLT_OP_CHILD, name, URL);
1330 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1332 token = xsltScanName(ctxt);
1333 if (token == NULL) {
1334 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1335 xsltGenericError(xsltGenericErrorContext,
1336 "xsltCompileStepPattern : QName expected\n");
1340 URI = xsltGetQNameURI(ctxt->elem, &token);
1341 if (token == NULL) {
1345 name = xmlStrdup(token);
1347 URL = xmlStrdup(URI);
1349 PUSH(XSLT_OP_ATTR, name, URL);
1351 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1352 xsltGenericError(xsltGenericErrorContext,
1353 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1359 } else if (CUR == '*') {
1361 PUSH(XSLT_OP_ALL, token, NULL);
1363 URI = xsltGetQNameURI(ctxt->elem, &token);
1364 if (token == NULL) {
1369 URL = xmlStrdup(URI);
1370 PUSH(XSLT_OP_ELEM, token, URL);
1375 while (CUR == '[') {
1377 xmlChar *ret = NULL;
1382 /* TODO: avoid breaking in strings ... */
1384 /* Skip over nested predicates */
1395 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1396 xsltGenericError(xsltGenericErrorContext,
1397 "xsltCompileStepPattern : ']' expected\n");
1401 ret = xmlStrndup(q, CUR_PTR - q);
1402 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1403 /* push the predicate lower than local test */
1417 * xsltCompileRelativePathPattern:
1418 * @comp: the compilation context
1419 * @token: a posible precompiled name
1421 * Compile the XSLT RelativePathPattern and generates a precompiled
1422 * form suitable for fast matching.
1424 * [4] RelativePathPattern ::= StepPattern
1425 * | RelativePathPattern '/' StepPattern
1426 * | RelativePathPattern '//' StepPattern
1429 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1430 xsltCompileStepPattern(ctxt, token);
1434 while ((CUR != 0) && (CUR != '|')) {
1435 if ((CUR == '/') && (NXT(1) == '/')) {
1436 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1440 xsltCompileStepPattern(ctxt, NULL);
1441 } else if (CUR == '/') {
1442 PUSH(XSLT_OP_PARENT, NULL, NULL);
1445 if ((CUR != 0) || (CUR == '|')) {
1446 xsltCompileRelativePathPattern(ctxt, NULL);
1460 * xsltCompileLocationPathPattern:
1461 * @ctxt: the compilation context
1463 * Compile the XSLT LocationPathPattern and generates a precompiled
1464 * form suitable for fast matching.
1466 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1467 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1468 * | '//'? RelativePathPattern
1471 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1473 if ((CUR == '/') && (NXT(1) == '/')) {
1475 * since we reverse the query
1476 * a leading // can be safely ignored
1480 xsltCompileRelativePathPattern(ctxt, NULL);
1481 } else if (CUR == '/') {
1483 * We need to find root as the parent
1487 PUSH(XSLT_OP_ROOT, NULL, NULL);
1488 if ((CUR != 0) || (CUR == '|')) {
1489 PUSH(XSLT_OP_PARENT, NULL, NULL);
1490 xsltCompileRelativePathPattern(ctxt, NULL);
1492 } else if (CUR == '*') {
1493 xsltCompileRelativePathPattern(ctxt, NULL);
1494 } else if (CUR == '@') {
1495 xsltCompileRelativePathPattern(ctxt, NULL);
1498 name = xsltScanName(ctxt);
1500 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1501 xsltGenericError(xsltGenericErrorContext,
1502 "xsltCompileLocationPathPattern : Name expected\n");
1507 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1508 xsltCompileIdKeyPattern(ctxt, name, 1);
1509 if ((CUR == '/') && (NXT(1) == '/')) {
1510 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1514 xsltCompileRelativePathPattern(ctxt, NULL);
1515 } else if (CUR == '/') {
1516 PUSH(XSLT_OP_PARENT, NULL, NULL);
1519 xsltCompileRelativePathPattern(ctxt, NULL);
1523 xsltCompileRelativePathPattern(ctxt, name);
1530 * xsltCompilePattern:
1531 * @pattern: an XSLT pattern
1532 * @doc: the containing document
1533 * @node: the containing element
1534 * @style: the stylesheet
1535 * @runtime: the transformation context, if done at run-time
1537 * Compile the XSLT pattern and generates a list of precompiled form suitable
1538 * for fast matching.
1540 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1542 * Returns the generated pattern list or NULL in case of failure
1546 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1547 xmlNodePtr node, xsltStylesheetPtr style,
1548 xsltTransformContextPtr runtime) {
1549 xsltParserContextPtr ctxt = NULL;
1550 xsltCompMatchPtr element, first = NULL, previous = NULL;
1551 int current, start, end;
1553 if (pattern == NULL) {
1554 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1555 xsltGenericError(xsltGenericErrorContext,
1556 "xsltCompilePattern : NULL pattern\n");
1560 ctxt = xsltNewParserContext(style, runtime);
1566 while (pattern[current] != 0) {
1568 while (IS_BLANK(pattern[current]))
1571 while ((pattern[end] != 0) && (pattern[end] != '|'))
1573 if (current == end) {
1574 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1575 xsltGenericError(xsltGenericErrorContext,
1576 "xsltCompilePattern : NULL pattern\n");
1579 element = xsltNewCompMatch();
1580 if (element == NULL) {
1585 else if (previous != NULL)
1586 previous->next = element;
1589 ctxt->comp = element;
1590 ctxt->base = xmlStrndup(&pattern[start], end - start);
1591 if (ctxt->base == NULL)
1593 ctxt->cur = &(ctxt->base)[current - start];
1594 element->pattern = ctxt->base;
1596 #ifdef WITH_XSLT_DEBUG_PATTERN
1597 xsltGenericDebug(xsltGenericDebugContext,
1598 "xsltCompilePattern : parsing '%s'\n",
1601 xsltCompileLocationPathPattern(ctxt);
1606 * Reverse for faster interpretation.
1608 xsltReverseCompMatch(element);
1611 * Set-up the priority
1613 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1614 (element->steps[0].op == XSLT_OP_ATTR)) &&
1615 (element->steps[0].value != NULL) &&
1616 (element->steps[1].op == XSLT_OP_END)) {
1617 element->priority = 0;
1619 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1620 (element->steps[1].op == XSLT_OP_END)) {
1621 element->priority = 0;
1623 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1624 (element->steps[0].value != NULL) &&
1625 (element->steps[1].op == XSLT_OP_END)) {
1626 element->priority = 0;
1627 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1628 (element->steps[0].value2 != NULL) &&
1629 (element->steps[1].op == XSLT_OP_END)) {
1630 element->priority = -0.25;
1631 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1632 (element->steps[0].value != NULL) &&
1633 (element->steps[1].op == XSLT_OP_END)) {
1634 element->priority = -0.25;
1635 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1636 (element->steps[0].value == NULL) &&
1637 (element->steps[0].value2 == NULL) &&
1638 (element->steps[1].op == XSLT_OP_END)) {
1639 element->priority = -0.5;
1640 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1641 (element->steps[0].op == XSLT_OP_TEXT) ||
1642 (element->steps[0].op == XSLT_OP_ALL) ||
1643 (element->steps[0].op == XSLT_OP_NODE) ||
1644 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1645 (element->steps[1].op == XSLT_OP_END)) {
1646 element->priority = -0.5;
1648 element->priority = 0.5;
1650 #ifdef WITH_XSLT_DEBUG_PATTERN
1651 xsltGenericDebug(xsltGenericDebugContext,
1652 "xsltCompilePattern : parsed %s, default priority %f\n",
1653 element->pattern, element->priority);
1655 if (pattern[end] == '|')
1660 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1661 xsltGenericError(xsltGenericErrorContext,
1662 "xsltCompilePattern : NULL pattern\n");
1666 xsltFreeParserContext(ctxt);
1671 xsltFreeParserContext(ctxt);
1673 xsltFreeCompMatchList(first);
1677 /************************************************************************
1679 * Module interfaces *
1681 ************************************************************************/
1685 * @style: an XSLT stylesheet
1686 * @cur: an XSLT template
1687 * @mode: the mode name or NULL
1688 * @modeURI: the mode URI or NULL
1690 * Register the XSLT pattern associated to @cur
1692 * Returns -1 in case of error, 0 otherwise
1695 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1696 const xmlChar *mode, const xmlChar *modeURI) {
1697 xsltCompMatchPtr pat, list, *top = NULL, next;
1698 const xmlChar *name = NULL;
1699 float priority; /* the priority */
1701 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1704 priority = cur->priority;
1705 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1711 pat->template = cur;
1713 pat->mode = xmlStrdup(mode);
1714 if (modeURI != NULL)
1715 pat->modeURI = xmlStrdup(modeURI);
1716 if (priority != XSLT_PAT_NO_PRIORITY)
1717 pat->priority = priority;
1720 * insert it in the hash table list corresponding to its lookup name
1722 switch (pat->steps[0].op) {
1724 if (pat->steps[0].value != NULL)
1725 name = pat->steps[0].value;
1727 top = (xsltCompMatchPtr *) &(style->attrMatch);
1731 case XSLT_OP_PARENT:
1732 case XSLT_OP_ANCESTOR:
1733 top = (xsltCompMatchPtr *) &(style->elemMatch);
1736 top = (xsltCompMatchPtr *) &(style->rootMatch);
1739 top = (xsltCompMatchPtr *) &(style->keyMatch);
1742 /* TODO optimize ID !!! */
1745 top = (xsltCompMatchPtr *) &(style->elemMatch);
1748 case XSLT_OP_PREDICATE:
1749 xsltPrintErrorContext(NULL, style, NULL);
1750 xsltGenericError(xsltGenericErrorContext,
1751 "xsltAddTemplate: invalid compiled pattern\n");
1752 xsltFreeCompMatch(pat);
1755 * TODO: some flags at the top level about type based patterns
1756 * would be faster than inclusion in the hash table.
1759 if (pat->steps[0].value != NULL)
1760 name = pat->steps[0].value;
1762 top = (xsltCompMatchPtr *) &(style->piMatch);
1764 case XSLT_OP_COMMENT:
1765 top = (xsltCompMatchPtr *) &(style->commentMatch);
1768 top = (xsltCompMatchPtr *) &(style->textMatch);
1771 if (pat->steps[0].value != NULL)
1772 name = pat->steps[0].value;
1774 top = (xsltCompMatchPtr *) &(style->elemMatch);
1779 if (style->templatesHash == NULL) {
1780 style->templatesHash = xmlHashCreate(1024);
1781 if (style->templatesHash == NULL) {
1782 xsltFreeCompMatch(pat);
1785 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1787 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1788 name, mode, modeURI);
1790 xmlHashAddEntry3(style->templatesHash, name,
1791 mode, modeURI, pat);
1794 * Note '<=' since one must choose among the matching
1795 * template rules that are left, the one that occurs
1796 * last in the stylesheet
1798 if (list->priority <= pat->priority) {
1800 xmlHashUpdateEntry3(style->templatesHash, name,
1801 mode, modeURI, pat, NULL);
1803 while (list->next != NULL) {
1804 if (list->next->priority <= pat->priority)
1808 pat->next = list->next;
1813 } else if (top != NULL) {
1818 } else if (list->priority <= pat->priority) {
1822 while (list->next != NULL) {
1823 if (list->next->priority <= pat->priority)
1827 pat->next = list->next;
1831 xsltPrintErrorContext(NULL, style, NULL);
1832 xsltGenericError(xsltGenericErrorContext,
1833 "xsltAddTemplate: invalid compiled pattern\n");
1834 xsltFreeCompMatch(pat);
1837 #ifdef WITH_XSLT_DEBUG_PATTERN
1839 xsltGenericDebug(xsltGenericDebugContext,
1840 "added pattern : '%s' mode '%s' priority %f\n",
1841 pat->pattern, pat->mode, pat->priority);
1843 xsltGenericDebug(xsltGenericDebugContext,
1844 "added pattern : '%s' priority %f\n",
1845 pat->pattern, pat->priority);
1855 * @ctxt: a XSLT process context
1856 * @node: the node being processed
1857 * @style: the current style
1859 * Finds the template applying to this node, if @style is non-NULL
1860 * it means one needs to look for the next imported template in scope.
1862 * Returns the xsltTemplatePtr or NULL if not found
1865 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
1866 xsltStylesheetPtr style) {
1867 xsltStylesheetPtr curstyle;
1868 xsltTemplatePtr ret = NULL;
1869 const xmlChar *name = NULL;
1870 xsltCompMatchPtr list = NULL;
1873 if ((ctxt == NULL) || (node == NULL))
1876 if (style == NULL) {
1877 curstyle = ctxt->style;
1879 curstyle = xsltNextImport(style);
1882 while ((curstyle != NULL) && (curstyle != style)) {
1883 priority = XSLT_PAT_NO_PRIORITY;
1884 /* TODO : handle IDs/keys here ! */
1885 if (curstyle->templatesHash != NULL) {
1887 * Use the top name as selector
1889 switch (node->type) {
1890 case XML_ELEMENT_NODE:
1891 case XML_ATTRIBUTE_NODE:
1895 case XML_DOCUMENT_NODE:
1896 case XML_HTML_DOCUMENT_NODE:
1898 case XML_CDATA_SECTION_NODE:
1899 case XML_COMMENT_NODE:
1900 case XML_ENTITY_REF_NODE:
1901 case XML_ENTITY_NODE:
1902 case XML_DOCUMENT_TYPE_NODE:
1903 case XML_DOCUMENT_FRAG_NODE:
1904 case XML_NOTATION_NODE:
1906 case XML_ELEMENT_DECL:
1907 case XML_ATTRIBUTE_DECL:
1908 case XML_ENTITY_DECL:
1909 case XML_NAMESPACE_DECL:
1910 case XML_XINCLUDE_START:
1911 case XML_XINCLUDE_END:
1920 * find the list of appliable expressions based on the name
1922 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
1923 name, ctxt->mode, ctxt->modeURI);
1926 while (list != NULL) {
1927 if (xsltTestCompMatch(ctxt, list, node,
1928 ctxt->mode, ctxt->modeURI)) {
1929 ret = list->template;
1930 priority = list->priority;
1938 * find alternate generic matches
1940 switch (node->type) {
1941 case XML_ELEMENT_NODE:
1942 list = curstyle->elemMatch;
1944 case XML_ATTRIBUTE_NODE:
1945 list = curstyle->attrMatch;
1948 list = curstyle->piMatch;
1950 case XML_DOCUMENT_NODE:
1951 case XML_HTML_DOCUMENT_NODE:
1952 list = curstyle->rootMatch;
1955 case XML_CDATA_SECTION_NODE:
1956 list = curstyle->textMatch;
1958 case XML_COMMENT_NODE:
1959 list = curstyle->commentMatch;
1961 case XML_ENTITY_REF_NODE:
1962 case XML_ENTITY_NODE:
1963 case XML_DOCUMENT_TYPE_NODE:
1964 case XML_DOCUMENT_FRAG_NODE:
1965 case XML_NOTATION_NODE:
1967 case XML_ELEMENT_DECL:
1968 case XML_ATTRIBUTE_DECL:
1969 case XML_ENTITY_DECL:
1970 case XML_NAMESPACE_DECL:
1971 case XML_XINCLUDE_START:
1972 case XML_XINCLUDE_END:
1978 while ((list != NULL) &&
1979 ((ret == NULL) || (list->priority > priority))) {
1980 if (xsltTestCompMatch(ctxt, list, node,
1981 ctxt->mode, ctxt->modeURI)) {
1982 ret = list->template;
1983 priority = list->priority;
1989 * Some of the tests for elements can also apply to documents
1991 if ((node->type == XML_DOCUMENT_NODE) ||
1992 (node->type == XML_HTML_DOCUMENT_NODE)) {
1993 list = curstyle->elemMatch;
1994 while ((list != NULL) &&
1995 ((ret == NULL) || (list->priority > priority))) {
1996 if (xsltTestCompMatch(ctxt, list, node,
1997 ctxt->mode, ctxt->modeURI)) {
1998 ret = list->template;
1999 priority = list->priority;
2006 if (node->_private != NULL) {
2007 list = curstyle->keyMatch;
2008 while ((list != NULL) &&
2009 ((ret == NULL) || (list->priority > priority))) {
2010 if (xsltTestCompMatch(ctxt, list, node,
2011 ctxt->mode, ctxt->modeURI)) {
2012 ret = list->template;
2013 priority = list->priority;
2023 * Cycle on next curstylesheet import.
2025 curstyle = xsltNextImport(curstyle);
2031 * xsltCleanupTemplates:
2032 * @style: an XSLT stylesheet
2034 * Cleanup the state of the templates used by the stylesheet and
2035 * the ones it imports.
2038 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2042 * xsltFreeTemplateHashes:
2043 * @style: an XSLT stylesheet
2045 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2048 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2049 if (style->templatesHash != NULL)
2050 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2051 (xmlHashDeallocator) xsltFreeCompMatchList);
2052 if (style->rootMatch != NULL)
2053 xsltFreeCompMatchList(style->rootMatch);
2054 if (style->keyMatch != NULL)
2055 xsltFreeCompMatchList(style->keyMatch);
2056 if (style->elemMatch != NULL)
2057 xsltFreeCompMatchList(style->elemMatch);
2058 if (style->attrMatch != NULL)
2059 xsltFreeCompMatchList(style->attrMatch);
2060 if (style->parentMatch != NULL)
2061 xsltFreeCompMatchList(style->parentMatch);
2062 if (style->textMatch != NULL)
2063 xsltFreeCompMatchList(style->textMatch);
2064 if (style->piMatch != NULL)
2065 xsltFreeCompMatchList(style->piMatch);
2066 if (style->commentMatch != NULL)
2067 xsltFreeCompMatchList(style->commentMatch);
2073 * @node: a node in the source tree
2074 * @pattern: an XSLT pattern
2075 * @ctxtdoc: context document (for namespaces)
2076 * @ctxtnode: context node (for namespaces)
2078 * Determine if a node matches a pattern.
2081 xsltMatchPattern(xsltTransformContextPtr context,
2083 const xmlChar *pattern,
2085 xmlNodePtr ctxtnode)
2088 xsltCompMatchPtr first, comp;
2090 if ((context != NULL) && (pattern != NULL)) {
2091 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2092 for (comp = first; comp != NULL; comp = comp->next) {
2093 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2098 xsltFreeCompMatchList(first);