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_DOCUMENT_NODE) ||
461 (node->type == XML_HTML_DOCUMENT_NODE) ||
462 #ifdef LIBXML_DOCB_ENABLED
463 (node->type == XML_DOCB_DOCUMENT_NODE) ||
465 (node->type == XML_NAMESPACE_DECL))
470 if (step->value == NULL)
472 if (!xmlStrEqual(step->value, node->name))
475 if (node->ns == NULL) {
476 if (step->value2 != NULL)
478 } else if (node->ns->href != NULL) {
479 if (step->value2 == NULL)
481 if (!xmlStrEqual(step->value2, node->ns->href))
485 case XSLT_OP_ANCESTOR:
486 /* TODO: implement coalescing of ANCESTOR/NODE ops */
487 if (step->value == NULL) {
489 step = &comp->steps[i];
490 if (step->op == XSLT_OP_ROOT)
492 if (step->op != XSLT_OP_ELEM)
494 if (step->value == NULL)
499 if ((node->type == XML_DOCUMENT_NODE) ||
500 (node->type == XML_HTML_DOCUMENT_NODE) ||
501 #ifdef LIBXML_DOCB_ENABLED
502 (node->type == XML_DOCB_DOCUMENT_NODE) ||
504 (node->type == XML_NAMESPACE_DECL))
507 while (node != NULL) {
510 if (xmlStrEqual(step->value, node->name)) {
512 if (node->ns == NULL) {
513 if (step->value2 == NULL)
515 } else if (node->ns->href != NULL) {
516 if ((step->value2 != NULL) &&
517 (xmlStrEqual(step->value2, node->ns->href)))
527 /* TODO Handle IDs decently, must be done differently */
530 if (node->type != XML_ELEMENT_NODE)
533 id = xmlGetID(node->doc, step->value);
534 if ((id == NULL) || (id->parent != node))
542 list = xsltGetKey(ctxt, step->value,
543 step->value3, step->value2);
546 for (indx = 0;indx < list->nodeNr;indx++)
547 if (list->nodeTab[indx] == node)
549 if (indx >= list->nodeNr)
554 if (node->type != XML_ELEMENT_NODE)
556 if (node->ns == NULL) {
557 if (step->value != NULL)
559 } else if (node->ns->href != NULL) {
560 if (step->value == NULL)
562 if (!xmlStrEqual(step->value, node->ns->href))
567 if (node->type != XML_ELEMENT_NODE)
570 case XSLT_OP_PREDICATE: {
573 int pos = 0, len = 0;
575 * The simple existing predicate code cannot handle
576 * properly cascaded predicates. If in this situation
577 * compute directly the full node list once and check
578 * if the node is in the result list.
580 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
582 xmlXPathObjectPtr list;
585 previous = (xmlNodePtr)
586 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
588 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
589 list = (xmlXPathObjectPtr)
590 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
594 if (comp->pattern[0] == '/')
595 query = xmlStrdup(comp->pattern);
597 query = xmlStrdup((const xmlChar *)"//");
598 query = xmlStrcat(query, comp->pattern);
600 list = xmlXPathEval(query, ctxt->xpathCtxt);
604 if (list->type != XPATH_NODESET) {
605 xmlXPathFreeObject(list);
608 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
610 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
611 (xmlFreeFunc) xmlXPathFreeObject;
613 if ((list->nodesetval == NULL) ||
614 (list->nodesetval->nodeNr <= 0))
617 for (j = 0;j < list->nodesetval->nodeNr;j++) {
618 if (list->nodesetval->nodeTab[j] == node) {
627 * Depending on the last selection, one may need to
628 * recompute contextSize and proximityPosition.
630 * TODO: make this thread safe !
632 oldCS = ctxt->xpathCtxt->contextSize;
633 oldCP = ctxt->xpathCtxt->proximityPosition;
634 if ((select != NULL) &&
635 (select->op == XSLT_OP_ELEM) &&
636 (select->value != NULL) &&
637 (node->type == XML_ELEMENT_NODE) &&
638 (node->parent != NULL)) {
642 previous = (xmlNodePtr)
643 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
645 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
646 if ((previous != NULL) &&
647 (previous->parent == node->parent)) {
649 * just walk back to adjust the index
652 xmlNodePtr sibling = node;
654 while (sibling != NULL) {
655 if (sibling == previous)
657 if (xmlStrEqual(node->name, sibling->name)) {
658 if ((select->value2 == NULL) ||
659 ((sibling->ns != NULL) &&
660 (xmlStrEqual(select->value2,
661 sibling->ns->href))))
664 sibling = sibling->prev;
666 if (sibling == NULL) {
667 /* hum going backward in document order ... */
670 while (sibling != NULL) {
671 if (sibling == previous)
673 if ((select->value2 == NULL) ||
674 ((sibling->ns != NULL) &&
675 (xmlStrEqual(select->value2,
676 sibling->ns->href))))
678 sibling = sibling->next;
681 if (sibling != NULL) {
684 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
685 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
687 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
694 * recompute the index
696 xmlNodePtr siblings = node->parent->children;
698 while (siblings != NULL) {
699 if (siblings->type == XML_ELEMENT_NODE) {
700 if (siblings == node) {
703 } else if (xmlStrEqual(node->name,
705 if ((select->value2 == NULL) ||
706 ((siblings->ns != NULL) &&
707 (xmlStrEqual(select->value2,
708 siblings->ns->href))))
712 siblings = siblings->next;
716 ctxt->xpathCtxt->contextSize = len;
717 ctxt->xpathCtxt->proximityPosition = pos;
718 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
720 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
722 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
725 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
726 (node->type == XML_ELEMENT_NODE)) {
730 previous = (xmlNodePtr)
731 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
733 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
734 if ((previous != NULL) &&
735 (previous->parent == node->parent)) {
737 * just walk back to adjust the index
740 xmlNodePtr sibling = node;
742 while (sibling != NULL) {
743 if (sibling == previous)
745 if (sibling->type == XML_ELEMENT_NODE)
747 sibling = sibling->prev;
749 if (sibling == NULL) {
750 /* hum going backward in document order ... */
753 while (sibling != NULL) {
754 if (sibling == previous)
756 if (sibling->type == XML_ELEMENT_NODE)
758 sibling = sibling->next;
761 if (sibling != NULL) {
764 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
765 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
767 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
773 * recompute the index
775 xmlNodePtr siblings = node->parent->children;
777 while (siblings != NULL) {
778 if (siblings->type == XML_ELEMENT_NODE) {
780 if (siblings == node) {
784 siblings = siblings->next;
788 ctxt->xpathCtxt->contextSize = len;
789 ctxt->xpathCtxt->proximityPosition = pos;
790 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
792 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
794 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
798 oldNode = ctxt->node;
801 if (step->value == NULL)
804 if (step->comp == NULL) {
805 step->comp = xmlXPathCompile(step->value);
806 if (step->comp == NULL)
809 if (comp->nsList == NULL) {
812 comp->nsList = xmlGetNsList(node->doc, node);
813 if (comp->nsList != NULL) {
814 while (comp->nsList[j] != NULL)
819 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
824 ctxt->xpathCtxt->contextSize = oldCS;
825 ctxt->xpathCtxt->proximityPosition = oldCP;
827 ctxt->node = oldNode;
831 ctxt->xpathCtxt->contextSize = oldCS;
832 ctxt->xpathCtxt->proximityPosition = oldCP;
834 ctxt->node = oldNode;
838 if (node->type != XML_PI_NODE)
840 if (step->value != NULL) {
841 if (!xmlStrEqual(step->value, node->name))
845 case XSLT_OP_COMMENT:
846 if (node->type != XML_COMMENT_NODE)
850 if ((node->type != XML_TEXT_NODE) &&
851 (node->type != XML_CDATA_SECTION_NODE))
855 switch (node->type) {
856 case XML_DOCUMENT_NODE:
857 case XML_HTML_DOCUMENT_NODE:
858 #ifdef LIBXML_DOCB_ENABLED
859 case XML_DOCB_DOCUMENT_NODE:
861 case XML_ELEMENT_NODE:
862 case XML_CDATA_SECTION_NODE:
864 case XML_COMMENT_NODE:
866 case XML_ATTRIBUTE_NODE:
878 * xsltTestCompMatchList:
879 * @ctxt: a XSLT process context
881 * @comp: the precompiled pattern list
883 * Test wether the node matches one of the patterns in the list
885 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
888 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
889 xsltCompMatchPtr comp) {
892 if ((ctxt == NULL) || (node == NULL))
894 while (comp != NULL) {
895 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
903 /************************************************************************
905 * Dedicated parser for templates *
907 ************************************************************************/
909 #define CUR (*ctxt->cur)
910 #define SKIP(val) ctxt->cur += (val)
911 #define NXT(val) ctxt->cur[(val)]
912 #define CUR_PTR ctxt->cur
914 #define SKIP_BLANKS \
915 while (IS_BLANK(CUR)) NEXT
917 #define CURRENT (*ctxt->cur)
918 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
921 #define PUSH(op, val, val2) \
922 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
925 xsltSwapTopCompMatch(ctxt->comp);
927 #define XSLT_ERROR(X) \
928 { xsltError(ctxt, __FILE__, __LINE__, X); \
929 ctxt->error = (X); return; }
931 #define XSLT_ERROR0(X) \
932 { xsltError(ctxt, __FILE__, __LINE__, X); \
933 ctxt->error = (X); return(0); }
937 * @ctxt: the XPath Parser context
939 * Parse an XPath Litteral:
941 * [29] Literal ::= '"' [^"]* '"'
944 * Returns the Literal parsed or NULL
948 xsltScanLiteral(xsltParserContextPtr ctxt) {
949 const xmlChar *q, *cur;
957 val = xmlStringCurrentChar(NULL, cur, &len);
958 while ((IS_CHAR(val)) && (val != '"')) {
960 val = xmlStringCurrentChar(NULL, cur, &len);
966 ret = xmlStrndup(q, cur - q);
970 } else if (CUR == '\'') {
973 val = xmlStringCurrentChar(NULL, cur, &len);
974 while ((IS_CHAR(val)) && (val != '\'')) {
976 val = xmlStringCurrentChar(NULL, cur, &len);
982 ret = xmlStrndup(q, cur - q);
987 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
996 * @ctxt: the XPath Parser context
998 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
999 * CombiningChar | Extender
1001 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1003 * [6] Names ::= Name (S Name)*
1005 * Returns the Name parsed or NULL
1009 xsltScanName(xsltParserContextPtr ctxt) {
1010 const xmlChar *q, *cur;
1011 xmlChar *ret = NULL;
1017 val = xmlStringCurrentChar(NULL, cur, &len);
1018 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1021 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1022 (val == '.') || (val == '-') ||
1024 (IS_COMBINING(val)) ||
1025 (IS_EXTENDER(val))) {
1027 val = xmlStringCurrentChar(NULL, cur, &len);
1029 ret = xmlStrndup(q, cur - q);
1036 * @ctxt: the XPath Parser context
1038 * Parses a non qualified name
1040 * Returns the Name parsed or NULL
1044 xsltScanNCName(xsltParserContextPtr ctxt) {
1045 const xmlChar *q, *cur;
1046 xmlChar *ret = NULL;
1052 val = xmlStringCurrentChar(NULL, cur, &len);
1053 if (!IS_LETTER(val) && (val != '_'))
1056 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1057 (val == '.') || (val == '-') ||
1059 (IS_COMBINING(val)) ||
1060 (IS_EXTENDER(val))) {
1062 val = xmlStringCurrentChar(NULL, cur, &len);
1064 ret = xmlStrndup(q, cur - q);
1071 * @ctxt: the XPath Parser context
1072 * @prefix: the place to store the prefix
1074 * Parse a qualified name
1076 * Returns the Name parsed or NULL
1080 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1081 xmlChar *ret = NULL;
1084 ret = xsltScanNCName(ctxt);
1088 ret = xsltScanNCName(ctxt);
1094 * xsltCompileIdKeyPattern:
1095 * @ctxt: the compilation context
1096 * @name: a preparsed name
1097 * @aid: whether id/key are allowed there
1099 * Compile the XSLT LocationIdKeyPattern
1100 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1101 * | 'key' '(' Literal ',' Literal ')'
1103 * also handle NodeType and PI from:
1105 * [7] NodeTest ::= NameTest
1106 * | NodeType '(' ')'
1107 * | 'processing-instruction' '(' Literal ')'
1110 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1111 xmlChar *lit = NULL;
1112 xmlChar *lit2 = NULL;
1115 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1116 xsltGenericError(xsltGenericErrorContext,
1117 "xsltCompileIdKeyPattern : ( expected\n");
1121 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1124 lit = xsltScanLiteral(ctxt);
1129 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1130 xsltGenericError(xsltGenericErrorContext,
1131 "xsltCompileIdKeyPattern : ) expected\n");
1136 PUSH(XSLT_OP_ID, lit, NULL);
1137 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1140 lit = xsltScanLiteral(ctxt);
1145 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1146 xsltGenericError(xsltGenericErrorContext,
1147 "xsltCompileIdKeyPattern : , expected\n");
1153 lit2 = xsltScanLiteral(ctxt);
1158 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1159 xsltGenericError(xsltGenericErrorContext,
1160 "xsltCompileIdKeyPattern : ) expected\n");
1165 /* TODO: support namespace in keys */
1166 PUSH(XSLT_OP_KEY, lit, lit2);
1167 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1171 lit = xsltScanLiteral(ctxt);
1176 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1177 xsltGenericError(xsltGenericErrorContext,
1178 "xsltCompileIdKeyPattern : ) expected\n");
1184 PUSH(XSLT_OP_PI, lit, NULL);
1185 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1189 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1190 xsltGenericError(xsltGenericErrorContext,
1191 "xsltCompileIdKeyPattern : ) expected\n");
1196 PUSH(XSLT_OP_TEXT, NULL, NULL);
1197 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1201 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1202 xsltGenericError(xsltGenericErrorContext,
1203 "xsltCompileIdKeyPattern : ) expected\n");
1208 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1209 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1213 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1214 xsltGenericError(xsltGenericErrorContext,
1215 "xsltCompileIdKeyPattern : ) expected\n");
1220 PUSH(XSLT_OP_NODE, NULL, NULL);
1222 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1223 xsltGenericError(xsltGenericErrorContext,
1224 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1228 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1229 xsltGenericError(xsltGenericErrorContext,
1230 "xsltCompileIdKeyPattern : node type\n");
1240 * xsltCompileStepPattern:
1241 * @ctxt: the compilation context
1242 * @token: a posible precompiled name
1244 * Compile the XSLT StepPattern and generates a precompiled
1245 * form suitable for fast matching.
1247 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1248 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1249 * | ('child' | 'attribute') '::'
1251 * [7] NodeTest ::= NameTest
1252 * | NodeType '(' ')'
1253 * | 'processing-instruction' '(' Literal ')'
1254 * [8] Predicate ::= '[' PredicateExpr ']'
1255 * [9] PredicateExpr ::= Expr
1256 * [13] AbbreviatedAxisSpecifier ::= '@'?
1257 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1261 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1262 xmlChar *name = NULL;
1263 const xmlChar *URI = NULL;
1264 xmlChar *URL = NULL;
1268 if ((token == NULL) && (CUR == '@')) {
1269 xmlChar *prefix = NULL;
1274 PUSH(XSLT_OP_ATTR, NULL, NULL);
1277 token = xsltScanQName(ctxt, &prefix);
1278 if (prefix != NULL) {
1281 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1283 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1284 xsltGenericError(xsltGenericErrorContext,
1285 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1288 URL = xmlStrdup(ns->href);
1292 if (token == NULL) {
1295 PUSH(XSLT_OP_ATTR, NULL, URL);
1298 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1299 xsltGenericError(xsltGenericErrorContext,
1300 "xsltCompileStepPattern : Name expected\n");
1304 PUSH(XSLT_OP_ATTR, token, URL);
1308 token = xsltScanName(ctxt);
1309 if (token == NULL) {
1312 PUSH(XSLT_OP_ALL, token, NULL);
1313 goto parse_predicate;
1315 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1316 xsltGenericError(xsltGenericErrorContext,
1317 "xsltCompileStepPattern : Name expected\n");
1326 xsltCompileIdKeyPattern(ctxt, token, 0);
1329 } else if (CUR == ':') {
1332 xmlChar *prefix = token;
1336 * This is a namespace match
1338 token = xsltScanName(ctxt);
1339 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1341 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1342 xsltGenericError(xsltGenericErrorContext,
1343 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1348 URL = xmlStrdup(ns->href);
1351 if (token == NULL) {
1354 PUSH(XSLT_OP_NS, URL, NULL);
1356 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1357 xsltGenericError(xsltGenericErrorContext,
1358 "xsltCompileStepPattern : Name expected\n");
1363 PUSH(XSLT_OP_ELEM, token, URL);
1367 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1369 token = xsltScanName(ctxt);
1370 if (token == NULL) {
1373 PUSH(XSLT_OP_ALL, token, NULL);
1374 goto parse_predicate;
1376 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1377 xsltGenericError(xsltGenericErrorContext,
1378 "xsltCompileStepPattern : QName expected\n");
1383 URI = xsltGetQNameURI(ctxt->elem, &token);
1384 if (token == NULL) {
1388 name = xmlStrdup(token);
1390 URL = xmlStrdup(URI);
1392 PUSH(XSLT_OP_CHILD, name, URL);
1393 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1395 token = xsltScanName(ctxt);
1396 if (token == NULL) {
1397 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1398 xsltGenericError(xsltGenericErrorContext,
1399 "xsltCompileStepPattern : QName expected\n");
1403 URI = xsltGetQNameURI(ctxt->elem, &token);
1404 if (token == NULL) {
1408 name = xmlStrdup(token);
1410 URL = xmlStrdup(URI);
1412 PUSH(XSLT_OP_ATTR, name, URL);
1414 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1415 xsltGenericError(xsltGenericErrorContext,
1416 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1422 } else if (CUR == '*') {
1424 PUSH(XSLT_OP_ALL, token, NULL);
1426 URI = xsltGetQNameURI(ctxt->elem, &token);
1427 if (token == NULL) {
1432 URL = xmlStrdup(URI);
1433 PUSH(XSLT_OP_ELEM, token, URL);
1438 while (CUR == '[') {
1440 xmlChar *ret = NULL;
1445 /* TODO: avoid breaking in strings ... */
1447 /* Skip over nested predicates */
1458 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1459 xsltGenericError(xsltGenericErrorContext,
1460 "xsltCompileStepPattern : ']' expected\n");
1464 ret = xmlStrndup(q, CUR_PTR - q);
1465 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1466 /* push the predicate lower than local test */
1480 * xsltCompileRelativePathPattern:
1481 * @comp: the compilation context
1482 * @token: a posible precompiled name
1484 * Compile the XSLT RelativePathPattern and generates a precompiled
1485 * form suitable for fast matching.
1487 * [4] RelativePathPattern ::= StepPattern
1488 * | RelativePathPattern '/' StepPattern
1489 * | RelativePathPattern '//' StepPattern
1492 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1493 xsltCompileStepPattern(ctxt, token);
1497 while ((CUR != 0) && (CUR != '|')) {
1498 if ((CUR == '/') && (NXT(1) == '/')) {
1499 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1503 xsltCompileStepPattern(ctxt, NULL);
1504 } else if (CUR == '/') {
1505 PUSH(XSLT_OP_PARENT, NULL, NULL);
1508 if ((CUR != 0) || (CUR == '|')) {
1509 xsltCompileRelativePathPattern(ctxt, NULL);
1523 * xsltCompileLocationPathPattern:
1524 * @ctxt: the compilation context
1526 * Compile the XSLT LocationPathPattern and generates a precompiled
1527 * form suitable for fast matching.
1529 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1530 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1531 * | '//'? RelativePathPattern
1534 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1536 if ((CUR == '/') && (NXT(1) == '/')) {
1538 * since we reverse the query
1539 * a leading // can be safely ignored
1543 xsltCompileRelativePathPattern(ctxt, NULL);
1544 } else if (CUR == '/') {
1546 * We need to find root as the parent
1550 PUSH(XSLT_OP_ROOT, NULL, NULL);
1551 if ((CUR != 0) || (CUR == '|')) {
1552 PUSH(XSLT_OP_PARENT, NULL, NULL);
1553 xsltCompileRelativePathPattern(ctxt, NULL);
1555 } else if (CUR == '*') {
1556 xsltCompileRelativePathPattern(ctxt, NULL);
1557 } else if (CUR == '@') {
1558 xsltCompileRelativePathPattern(ctxt, NULL);
1561 name = xsltScanName(ctxt);
1563 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1564 xsltGenericError(xsltGenericErrorContext,
1565 "xsltCompileLocationPathPattern : Name expected\n");
1570 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1571 xsltCompileIdKeyPattern(ctxt, name, 1);
1572 if ((CUR == '/') && (NXT(1) == '/')) {
1573 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1577 xsltCompileRelativePathPattern(ctxt, NULL);
1578 } else if (CUR == '/') {
1579 PUSH(XSLT_OP_PARENT, NULL, NULL);
1582 xsltCompileRelativePathPattern(ctxt, NULL);
1586 xsltCompileRelativePathPattern(ctxt, name);
1593 * xsltCompilePattern:
1594 * @pattern: an XSLT pattern
1595 * @doc: the containing document
1596 * @node: the containing element
1597 * @style: the stylesheet
1598 * @runtime: the transformation context, if done at run-time
1600 * Compile the XSLT pattern and generates a list of precompiled form suitable
1601 * for fast matching.
1603 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1605 * Returns the generated pattern list or NULL in case of failure
1609 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1610 xmlNodePtr node, xsltStylesheetPtr style,
1611 xsltTransformContextPtr runtime) {
1612 xsltParserContextPtr ctxt = NULL;
1613 xsltCompMatchPtr element, first = NULL, previous = NULL;
1614 int current, start, end;
1616 if (pattern == NULL) {
1617 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1618 xsltGenericError(xsltGenericErrorContext,
1619 "xsltCompilePattern : NULL pattern\n");
1623 ctxt = xsltNewParserContext(style, runtime);
1629 while (pattern[current] != 0) {
1631 while (IS_BLANK(pattern[current]))
1634 while ((pattern[end] != 0) && (pattern[end] != '|'))
1636 if (current == end) {
1637 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1638 xsltGenericError(xsltGenericErrorContext,
1639 "xsltCompilePattern : NULL pattern\n");
1642 element = xsltNewCompMatch();
1643 if (element == NULL) {
1648 else if (previous != NULL)
1649 previous->next = element;
1652 ctxt->comp = element;
1653 ctxt->base = xmlStrndup(&pattern[start], end - start);
1654 if (ctxt->base == NULL)
1656 ctxt->cur = &(ctxt->base)[current - start];
1657 element->pattern = ctxt->base;
1659 #ifdef WITH_XSLT_DEBUG_PATTERN
1660 xsltGenericDebug(xsltGenericDebugContext,
1661 "xsltCompilePattern : parsing '%s'\n",
1664 xsltCompileLocationPathPattern(ctxt);
1669 * Reverse for faster interpretation.
1671 xsltReverseCompMatch(element);
1674 * Set-up the priority
1676 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1677 (element->steps[0].op == XSLT_OP_ATTR)) &&
1678 (element->steps[0].value != NULL) &&
1679 (element->steps[1].op == XSLT_OP_END)) {
1680 element->priority = 0;
1682 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1683 (element->steps[1].op == XSLT_OP_END)) {
1684 element->priority = 0;
1686 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1687 (element->steps[0].value != NULL) &&
1688 (element->steps[1].op == XSLT_OP_END)) {
1689 element->priority = 0;
1690 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1691 (element->steps[0].value2 != NULL) &&
1692 (element->steps[1].op == XSLT_OP_END)) {
1693 element->priority = -0.25;
1694 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1695 (element->steps[0].value != NULL) &&
1696 (element->steps[1].op == XSLT_OP_END)) {
1697 element->priority = -0.25;
1698 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1699 (element->steps[0].value == NULL) &&
1700 (element->steps[0].value2 == NULL) &&
1701 (element->steps[1].op == XSLT_OP_END)) {
1702 element->priority = -0.5;
1703 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1704 (element->steps[0].op == XSLT_OP_TEXT) ||
1705 (element->steps[0].op == XSLT_OP_ALL) ||
1706 (element->steps[0].op == XSLT_OP_NODE) ||
1707 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1708 (element->steps[1].op == XSLT_OP_END)) {
1709 element->priority = -0.5;
1711 element->priority = 0.5;
1713 #ifdef WITH_XSLT_DEBUG_PATTERN
1714 xsltGenericDebug(xsltGenericDebugContext,
1715 "xsltCompilePattern : parsed %s, default priority %f\n",
1716 element->pattern, element->priority);
1718 if (pattern[end] == '|')
1723 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1724 xsltGenericError(xsltGenericErrorContext,
1725 "xsltCompilePattern : NULL pattern\n");
1729 xsltFreeParserContext(ctxt);
1734 xsltFreeParserContext(ctxt);
1736 xsltFreeCompMatchList(first);
1740 /************************************************************************
1742 * Module interfaces *
1744 ************************************************************************/
1748 * @style: an XSLT stylesheet
1749 * @cur: an XSLT template
1750 * @mode: the mode name or NULL
1751 * @modeURI: the mode URI or NULL
1753 * Register the XSLT pattern associated to @cur
1755 * Returns -1 in case of error, 0 otherwise
1758 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1759 const xmlChar *mode, const xmlChar *modeURI) {
1760 xsltCompMatchPtr pat, list, *top = NULL, next;
1761 const xmlChar *name = NULL;
1762 float priority; /* the priority */
1764 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1767 priority = cur->priority;
1768 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1774 pat->template = cur;
1776 pat->mode = xmlStrdup(mode);
1777 if (modeURI != NULL)
1778 pat->modeURI = xmlStrdup(modeURI);
1779 if (priority != XSLT_PAT_NO_PRIORITY)
1780 pat->priority = priority;
1783 * insert it in the hash table list corresponding to its lookup name
1785 switch (pat->steps[0].op) {
1787 if (pat->steps[0].value != NULL)
1788 name = pat->steps[0].value;
1790 top = (xsltCompMatchPtr *) &(style->attrMatch);
1794 case XSLT_OP_PARENT:
1795 case XSLT_OP_ANCESTOR:
1796 top = (xsltCompMatchPtr *) &(style->elemMatch);
1799 top = (xsltCompMatchPtr *) &(style->rootMatch);
1802 top = (xsltCompMatchPtr *) &(style->keyMatch);
1805 /* TODO optimize ID !!! */
1808 top = (xsltCompMatchPtr *) &(style->elemMatch);
1811 case XSLT_OP_PREDICATE:
1812 xsltPrintErrorContext(NULL, style, NULL);
1813 xsltGenericError(xsltGenericErrorContext,
1814 "xsltAddTemplate: invalid compiled pattern\n");
1815 xsltFreeCompMatch(pat);
1818 * TODO: some flags at the top level about type based patterns
1819 * would be faster than inclusion in the hash table.
1822 if (pat->steps[0].value != NULL)
1823 name = pat->steps[0].value;
1825 top = (xsltCompMatchPtr *) &(style->piMatch);
1827 case XSLT_OP_COMMENT:
1828 top = (xsltCompMatchPtr *) &(style->commentMatch);
1831 top = (xsltCompMatchPtr *) &(style->textMatch);
1834 if (pat->steps[0].value != NULL)
1835 name = pat->steps[0].value;
1837 top = (xsltCompMatchPtr *) &(style->elemMatch);
1842 if (style->templatesHash == NULL) {
1843 style->templatesHash = xmlHashCreate(1024);
1844 if (style->templatesHash == NULL) {
1845 xsltFreeCompMatch(pat);
1848 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1850 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1851 name, mode, modeURI);
1853 xmlHashAddEntry3(style->templatesHash, name,
1854 mode, modeURI, pat);
1857 * Note '<=' since one must choose among the matching
1858 * template rules that are left, the one that occurs
1859 * last in the stylesheet
1861 if (list->priority <= pat->priority) {
1863 xmlHashUpdateEntry3(style->templatesHash, name,
1864 mode, modeURI, pat, NULL);
1866 while (list->next != NULL) {
1867 if (list->next->priority <= pat->priority)
1871 pat->next = list->next;
1876 } else if (top != NULL) {
1881 } else if (list->priority <= pat->priority) {
1885 while (list->next != NULL) {
1886 if (list->next->priority <= pat->priority)
1890 pat->next = list->next;
1894 xsltPrintErrorContext(NULL, style, NULL);
1895 xsltGenericError(xsltGenericErrorContext,
1896 "xsltAddTemplate: invalid compiled pattern\n");
1897 xsltFreeCompMatch(pat);
1900 #ifdef WITH_XSLT_DEBUG_PATTERN
1902 xsltGenericDebug(xsltGenericDebugContext,
1903 "added pattern : '%s' mode '%s' priority %f\n",
1904 pat->pattern, pat->mode, pat->priority);
1906 xsltGenericDebug(xsltGenericDebugContext,
1907 "added pattern : '%s' priority %f\n",
1908 pat->pattern, pat->priority);
1918 * @ctxt: a XSLT process context
1919 * @node: the node being processed
1920 * @style: the current style
1922 * Finds the template applying to this node, if @style is non-NULL
1923 * it means one needs to look for the next imported template in scope.
1925 * Returns the xsltTemplatePtr or NULL if not found
1928 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
1929 xsltStylesheetPtr style) {
1930 xsltStylesheetPtr curstyle;
1931 xsltTemplatePtr ret = NULL;
1932 const xmlChar *name = NULL;
1933 xsltCompMatchPtr list = NULL;
1936 if ((ctxt == NULL) || (node == NULL))
1939 if (style == NULL) {
1940 curstyle = ctxt->style;
1942 curstyle = xsltNextImport(style);
1945 while ((curstyle != NULL) && (curstyle != style)) {
1946 priority = XSLT_PAT_NO_PRIORITY;
1947 /* TODO : handle IDs/keys here ! */
1948 if (curstyle->templatesHash != NULL) {
1950 * Use the top name as selector
1952 switch (node->type) {
1953 case XML_ELEMENT_NODE:
1954 case XML_ATTRIBUTE_NODE:
1958 case XML_DOCUMENT_NODE:
1959 case XML_HTML_DOCUMENT_NODE:
1961 case XML_CDATA_SECTION_NODE:
1962 case XML_COMMENT_NODE:
1963 case XML_ENTITY_REF_NODE:
1964 case XML_ENTITY_NODE:
1965 case XML_DOCUMENT_TYPE_NODE:
1966 case XML_DOCUMENT_FRAG_NODE:
1967 case XML_NOTATION_NODE:
1969 case XML_ELEMENT_DECL:
1970 case XML_ATTRIBUTE_DECL:
1971 case XML_ENTITY_DECL:
1972 case XML_NAMESPACE_DECL:
1973 case XML_XINCLUDE_START:
1974 case XML_XINCLUDE_END:
1983 * find the list of appliable expressions based on the name
1985 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
1986 name, ctxt->mode, ctxt->modeURI);
1989 while (list != NULL) {
1990 if (xsltTestCompMatch(ctxt, list, node,
1991 ctxt->mode, ctxt->modeURI)) {
1992 ret = list->template;
1993 priority = list->priority;
2001 * find alternate generic matches
2003 switch (node->type) {
2004 case XML_ELEMENT_NODE:
2005 list = curstyle->elemMatch;
2007 case XML_ATTRIBUTE_NODE:
2008 list = curstyle->attrMatch;
2011 list = curstyle->piMatch;
2013 case XML_DOCUMENT_NODE:
2014 case XML_HTML_DOCUMENT_NODE:
2015 list = curstyle->rootMatch;
2018 case XML_CDATA_SECTION_NODE:
2019 list = curstyle->textMatch;
2021 case XML_COMMENT_NODE:
2022 list = curstyle->commentMatch;
2024 case XML_ENTITY_REF_NODE:
2025 case XML_ENTITY_NODE:
2026 case XML_DOCUMENT_TYPE_NODE:
2027 case XML_DOCUMENT_FRAG_NODE:
2028 case XML_NOTATION_NODE:
2030 case XML_ELEMENT_DECL:
2031 case XML_ATTRIBUTE_DECL:
2032 case XML_ENTITY_DECL:
2033 case XML_NAMESPACE_DECL:
2034 case XML_XINCLUDE_START:
2035 case XML_XINCLUDE_END:
2041 while ((list != NULL) &&
2042 ((ret == NULL) || (list->priority > priority))) {
2043 if (xsltTestCompMatch(ctxt, list, node,
2044 ctxt->mode, ctxt->modeURI)) {
2045 ret = list->template;
2046 priority = list->priority;
2052 * Some of the tests for elements can also apply to documents
2054 if ((node->type == XML_DOCUMENT_NODE) ||
2055 (node->type == XML_HTML_DOCUMENT_NODE)) {
2056 list = curstyle->elemMatch;
2057 while ((list != NULL) &&
2058 ((ret == NULL) || (list->priority > priority))) {
2059 if (xsltTestCompMatch(ctxt, list, node,
2060 ctxt->mode, ctxt->modeURI)) {
2061 ret = list->template;
2062 priority = list->priority;
2069 if (node->_private != NULL) {
2070 list = curstyle->keyMatch;
2071 while ((list != NULL) &&
2072 ((ret == NULL) || (list->priority > priority))) {
2073 if (xsltTestCompMatch(ctxt, list, node,
2074 ctxt->mode, ctxt->modeURI)) {
2075 ret = list->template;
2076 priority = list->priority;
2086 * Cycle on next curstylesheet import.
2088 curstyle = xsltNextImport(curstyle);
2094 * xsltCleanupTemplates:
2095 * @style: an XSLT stylesheet
2097 * Cleanup the state of the templates used by the stylesheet and
2098 * the ones it imports.
2101 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2105 * xsltFreeTemplateHashes:
2106 * @style: an XSLT stylesheet
2108 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2111 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2112 if (style->templatesHash != NULL)
2113 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2114 (xmlHashDeallocator) xsltFreeCompMatchList);
2115 if (style->rootMatch != NULL)
2116 xsltFreeCompMatchList(style->rootMatch);
2117 if (style->keyMatch != NULL)
2118 xsltFreeCompMatchList(style->keyMatch);
2119 if (style->elemMatch != NULL)
2120 xsltFreeCompMatchList(style->elemMatch);
2121 if (style->attrMatch != NULL)
2122 xsltFreeCompMatchList(style->attrMatch);
2123 if (style->parentMatch != NULL)
2124 xsltFreeCompMatchList(style->parentMatch);
2125 if (style->textMatch != NULL)
2126 xsltFreeCompMatchList(style->textMatch);
2127 if (style->piMatch != NULL)
2128 xsltFreeCompMatchList(style->piMatch);
2129 if (style->commentMatch != NULL)
2130 xsltFreeCompMatchList(style->commentMatch);
2136 * @node: a node in the source tree
2137 * @pattern: an XSLT pattern
2138 * @ctxtdoc: context document (for namespaces)
2139 * @ctxtnode: context node (for namespaces)
2141 * Determine if a node matches a pattern.
2144 xsltMatchPattern(xsltTransformContextPtr context,
2146 const xmlChar *pattern,
2148 xmlNodePtr ctxtnode)
2151 xsltCompMatchPtr first, comp;
2153 if ((context != NULL) && (pattern != NULL)) {
2154 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2155 for (comp = first; comp != NULL; comp = comp->next) {
2156 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2161 xsltFreeCompMatchList(first);