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;
1446 /* Skip over nested predicates */
1449 else if (CUR == ']') {
1453 } else if (CUR == '"') {
1455 while ((CUR != 0) && (CUR != '"'))
1457 } else if (CUR == '\'') {
1459 while ((CUR != 0) && (CUR != '\''))
1465 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1466 xsltGenericError(xsltGenericErrorContext,
1467 "xsltCompileStepPattern : ']' expected\n");
1471 ret = xmlStrndup(q, CUR_PTR - q);
1472 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1473 /* push the predicate lower than local test */
1487 * xsltCompileRelativePathPattern:
1488 * @comp: the compilation context
1489 * @token: a posible precompiled name
1491 * Compile the XSLT RelativePathPattern and generates a precompiled
1492 * form suitable for fast matching.
1494 * [4] RelativePathPattern ::= StepPattern
1495 * | RelativePathPattern '/' StepPattern
1496 * | RelativePathPattern '//' StepPattern
1499 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1500 xsltCompileStepPattern(ctxt, token);
1504 while ((CUR != 0) && (CUR != '|')) {
1505 if ((CUR == '/') && (NXT(1) == '/')) {
1506 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1510 xsltCompileStepPattern(ctxt, NULL);
1511 } else if (CUR == '/') {
1512 PUSH(XSLT_OP_PARENT, NULL, NULL);
1515 if ((CUR != 0) || (CUR == '|')) {
1516 xsltCompileRelativePathPattern(ctxt, NULL);
1530 * xsltCompileLocationPathPattern:
1531 * @ctxt: the compilation context
1533 * Compile the XSLT LocationPathPattern and generates a precompiled
1534 * form suitable for fast matching.
1536 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1537 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1538 * | '//'? RelativePathPattern
1541 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1543 if ((CUR == '/') && (NXT(1) == '/')) {
1545 * since we reverse the query
1546 * a leading // can be safely ignored
1550 xsltCompileRelativePathPattern(ctxt, NULL);
1551 } else if (CUR == '/') {
1553 * We need to find root as the parent
1557 PUSH(XSLT_OP_ROOT, NULL, NULL);
1558 if ((CUR != 0) || (CUR == '|')) {
1559 PUSH(XSLT_OP_PARENT, NULL, NULL);
1560 xsltCompileRelativePathPattern(ctxt, NULL);
1562 } else if (CUR == '*') {
1563 xsltCompileRelativePathPattern(ctxt, NULL);
1564 } else if (CUR == '@') {
1565 xsltCompileRelativePathPattern(ctxt, NULL);
1568 name = xsltScanName(ctxt);
1570 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1571 xsltGenericError(xsltGenericErrorContext,
1572 "xsltCompileLocationPathPattern : Name expected\n");
1577 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1578 xsltCompileIdKeyPattern(ctxt, name, 1);
1579 if ((CUR == '/') && (NXT(1) == '/')) {
1580 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1584 xsltCompileRelativePathPattern(ctxt, NULL);
1585 } else if (CUR == '/') {
1586 PUSH(XSLT_OP_PARENT, NULL, NULL);
1589 xsltCompileRelativePathPattern(ctxt, NULL);
1593 xsltCompileRelativePathPattern(ctxt, name);
1600 * xsltCompilePattern:
1601 * @pattern: an XSLT pattern
1602 * @doc: the containing document
1603 * @node: the containing element
1604 * @style: the stylesheet
1605 * @runtime: the transformation context, if done at run-time
1607 * Compile the XSLT pattern and generates a list of precompiled form suitable
1608 * for fast matching.
1610 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1612 * Returns the generated pattern list or NULL in case of failure
1616 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1617 xmlNodePtr node, xsltStylesheetPtr style,
1618 xsltTransformContextPtr runtime) {
1619 xsltParserContextPtr ctxt = NULL;
1620 xsltCompMatchPtr element, first = NULL, previous = NULL;
1621 int current, start, end, level;
1623 if (pattern == NULL) {
1624 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1625 xsltGenericError(xsltGenericErrorContext,
1626 "xsltCompilePattern : NULL pattern\n");
1630 ctxt = xsltNewParserContext(style, runtime);
1636 while (pattern[current] != 0) {
1638 while (IS_BLANK(pattern[current]))
1642 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1643 if (pattern[end] == '[')
1645 else if (pattern[end] == ']')
1647 else if (pattern[end] == '\'') {
1649 while ((pattern[end] != 0) && (pattern[end] != '\''))
1651 } else if (pattern[end] == '"') {
1653 while ((pattern[end] != 0) && (pattern[end] != '"'))
1658 if (current == end) {
1659 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1660 xsltGenericError(xsltGenericErrorContext,
1661 "xsltCompilePattern : NULL pattern\n");
1664 element = xsltNewCompMatch();
1665 if (element == NULL) {
1670 else if (previous != NULL)
1671 previous->next = element;
1674 ctxt->comp = element;
1675 ctxt->base = xmlStrndup(&pattern[start], end - start);
1676 if (ctxt->base == NULL)
1678 ctxt->cur = &(ctxt->base)[current - start];
1679 element->pattern = ctxt->base;
1681 #ifdef WITH_XSLT_DEBUG_PATTERN
1682 xsltGenericDebug(xsltGenericDebugContext,
1683 "xsltCompilePattern : parsing '%s'\n",
1686 xsltCompileLocationPathPattern(ctxt);
1691 * Reverse for faster interpretation.
1693 xsltReverseCompMatch(element);
1696 * Set-up the priority
1698 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1699 (element->steps[0].op == XSLT_OP_ATTR)) &&
1700 (element->steps[0].value != NULL) &&
1701 (element->steps[1].op == XSLT_OP_END)) {
1702 element->priority = 0;
1704 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1705 (element->steps[1].op == XSLT_OP_END)) {
1706 element->priority = 0;
1708 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1709 (element->steps[0].value != NULL) &&
1710 (element->steps[1].op == XSLT_OP_END)) {
1711 element->priority = 0;
1712 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1713 (element->steps[0].value2 != NULL) &&
1714 (element->steps[1].op == XSLT_OP_END)) {
1715 element->priority = -0.25;
1716 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1717 (element->steps[0].value != NULL) &&
1718 (element->steps[1].op == XSLT_OP_END)) {
1719 element->priority = -0.25;
1720 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1721 (element->steps[0].value == NULL) &&
1722 (element->steps[0].value2 == NULL) &&
1723 (element->steps[1].op == XSLT_OP_END)) {
1724 element->priority = -0.5;
1725 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1726 (element->steps[0].op == XSLT_OP_TEXT) ||
1727 (element->steps[0].op == XSLT_OP_ALL) ||
1728 (element->steps[0].op == XSLT_OP_NODE) ||
1729 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1730 (element->steps[1].op == XSLT_OP_END)) {
1731 element->priority = -0.5;
1733 element->priority = 0.5;
1735 #ifdef WITH_XSLT_DEBUG_PATTERN
1736 xsltGenericDebug(xsltGenericDebugContext,
1737 "xsltCompilePattern : parsed %s, default priority %f\n",
1738 element->pattern, element->priority);
1740 if (pattern[end] == '|')
1745 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1746 xsltGenericError(xsltGenericErrorContext,
1747 "xsltCompilePattern : NULL pattern\n");
1751 xsltFreeParserContext(ctxt);
1756 xsltFreeParserContext(ctxt);
1758 xsltFreeCompMatchList(first);
1762 /************************************************************************
1764 * Module interfaces *
1766 ************************************************************************/
1770 * @style: an XSLT stylesheet
1771 * @cur: an XSLT template
1772 * @mode: the mode name or NULL
1773 * @modeURI: the mode URI or NULL
1775 * Register the XSLT pattern associated to @cur
1777 * Returns -1 in case of error, 0 otherwise
1780 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1781 const xmlChar *mode, const xmlChar *modeURI) {
1782 xsltCompMatchPtr pat, list, *top = NULL, next;
1783 const xmlChar *name = NULL;
1784 float priority; /* the priority */
1786 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1789 priority = cur->priority;
1790 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1796 pat->template = cur;
1798 pat->mode = xmlStrdup(mode);
1799 if (modeURI != NULL)
1800 pat->modeURI = xmlStrdup(modeURI);
1801 if (priority != XSLT_PAT_NO_PRIORITY)
1802 pat->priority = priority;
1805 * insert it in the hash table list corresponding to its lookup name
1807 switch (pat->steps[0].op) {
1809 if (pat->steps[0].value != NULL)
1810 name = pat->steps[0].value;
1812 top = (xsltCompMatchPtr *) &(style->attrMatch);
1816 case XSLT_OP_PARENT:
1817 case XSLT_OP_ANCESTOR:
1818 top = (xsltCompMatchPtr *) &(style->elemMatch);
1821 top = (xsltCompMatchPtr *) &(style->rootMatch);
1824 top = (xsltCompMatchPtr *) &(style->keyMatch);
1827 /* TODO optimize ID !!! */
1830 top = (xsltCompMatchPtr *) &(style->elemMatch);
1833 case XSLT_OP_PREDICATE:
1834 xsltPrintErrorContext(NULL, style, NULL);
1835 xsltGenericError(xsltGenericErrorContext,
1836 "xsltAddTemplate: invalid compiled pattern\n");
1837 xsltFreeCompMatch(pat);
1840 * TODO: some flags at the top level about type based patterns
1841 * would be faster than inclusion in the hash table.
1844 if (pat->steps[0].value != NULL)
1845 name = pat->steps[0].value;
1847 top = (xsltCompMatchPtr *) &(style->piMatch);
1849 case XSLT_OP_COMMENT:
1850 top = (xsltCompMatchPtr *) &(style->commentMatch);
1853 top = (xsltCompMatchPtr *) &(style->textMatch);
1856 if (pat->steps[0].value != NULL)
1857 name = pat->steps[0].value;
1859 top = (xsltCompMatchPtr *) &(style->elemMatch);
1864 if (style->templatesHash == NULL) {
1865 style->templatesHash = xmlHashCreate(1024);
1866 if (style->templatesHash == NULL) {
1867 xsltFreeCompMatch(pat);
1870 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1872 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1873 name, mode, modeURI);
1875 xmlHashAddEntry3(style->templatesHash, name,
1876 mode, modeURI, pat);
1879 * Note '<=' since one must choose among the matching
1880 * template rules that are left, the one that occurs
1881 * last in the stylesheet
1883 if (list->priority <= pat->priority) {
1885 xmlHashUpdateEntry3(style->templatesHash, name,
1886 mode, modeURI, pat, NULL);
1888 while (list->next != NULL) {
1889 if (list->next->priority <= pat->priority)
1893 pat->next = list->next;
1898 } else if (top != NULL) {
1903 } else if (list->priority <= pat->priority) {
1907 while (list->next != NULL) {
1908 if (list->next->priority <= pat->priority)
1912 pat->next = list->next;
1916 xsltPrintErrorContext(NULL, style, NULL);
1917 xsltGenericError(xsltGenericErrorContext,
1918 "xsltAddTemplate: invalid compiled pattern\n");
1919 xsltFreeCompMatch(pat);
1922 #ifdef WITH_XSLT_DEBUG_PATTERN
1924 xsltGenericDebug(xsltGenericDebugContext,
1925 "added pattern : '%s' mode '%s' priority %f\n",
1926 pat->pattern, pat->mode, pat->priority);
1928 xsltGenericDebug(xsltGenericDebugContext,
1929 "added pattern : '%s' priority %f\n",
1930 pat->pattern, pat->priority);
1940 * @ctxt: a XSLT process context
1941 * @node: the node being processed
1942 * @style: the current style
1944 * Finds the template applying to this node, if @style is non-NULL
1945 * it means one needs to look for the next imported template in scope.
1947 * Returns the xsltTemplatePtr or NULL if not found
1950 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
1951 xsltStylesheetPtr style) {
1952 xsltStylesheetPtr curstyle;
1953 xsltTemplatePtr ret = NULL;
1954 const xmlChar *name = NULL;
1955 xsltCompMatchPtr list = NULL;
1958 if ((ctxt == NULL) || (node == NULL))
1961 if (style == NULL) {
1962 curstyle = ctxt->style;
1964 curstyle = xsltNextImport(style);
1967 while ((curstyle != NULL) && (curstyle != style)) {
1968 priority = XSLT_PAT_NO_PRIORITY;
1969 /* TODO : handle IDs/keys here ! */
1970 if (curstyle->templatesHash != NULL) {
1972 * Use the top name as selector
1974 switch (node->type) {
1975 case XML_ELEMENT_NODE:
1976 case XML_ATTRIBUTE_NODE:
1980 case XML_DOCUMENT_NODE:
1981 case XML_HTML_DOCUMENT_NODE:
1983 case XML_CDATA_SECTION_NODE:
1984 case XML_COMMENT_NODE:
1985 case XML_ENTITY_REF_NODE:
1986 case XML_ENTITY_NODE:
1987 case XML_DOCUMENT_TYPE_NODE:
1988 case XML_DOCUMENT_FRAG_NODE:
1989 case XML_NOTATION_NODE:
1991 case XML_ELEMENT_DECL:
1992 case XML_ATTRIBUTE_DECL:
1993 case XML_ENTITY_DECL:
1994 case XML_NAMESPACE_DECL:
1995 case XML_XINCLUDE_START:
1996 case XML_XINCLUDE_END:
2005 * find the list of appliable expressions based on the name
2007 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2008 name, ctxt->mode, ctxt->modeURI);
2011 while (list != NULL) {
2012 if (xsltTestCompMatch(ctxt, list, node,
2013 ctxt->mode, ctxt->modeURI)) {
2014 ret = list->template;
2015 priority = list->priority;
2023 * find alternate generic matches
2025 switch (node->type) {
2026 case XML_ELEMENT_NODE:
2027 list = curstyle->elemMatch;
2029 case XML_ATTRIBUTE_NODE:
2030 list = curstyle->attrMatch;
2033 list = curstyle->piMatch;
2035 case XML_DOCUMENT_NODE:
2036 case XML_HTML_DOCUMENT_NODE:
2037 list = curstyle->rootMatch;
2040 case XML_CDATA_SECTION_NODE:
2041 list = curstyle->textMatch;
2043 case XML_COMMENT_NODE:
2044 list = curstyle->commentMatch;
2046 case XML_ENTITY_REF_NODE:
2047 case XML_ENTITY_NODE:
2048 case XML_DOCUMENT_TYPE_NODE:
2049 case XML_DOCUMENT_FRAG_NODE:
2050 case XML_NOTATION_NODE:
2052 case XML_ELEMENT_DECL:
2053 case XML_ATTRIBUTE_DECL:
2054 case XML_ENTITY_DECL:
2055 case XML_NAMESPACE_DECL:
2056 case XML_XINCLUDE_START:
2057 case XML_XINCLUDE_END:
2063 while ((list != NULL) &&
2064 ((ret == NULL) || (list->priority > priority))) {
2065 if (xsltTestCompMatch(ctxt, list, node,
2066 ctxt->mode, ctxt->modeURI)) {
2067 ret = list->template;
2068 priority = list->priority;
2074 * Some of the tests for elements can also apply to documents
2076 if ((node->type == XML_DOCUMENT_NODE) ||
2077 (node->type == XML_HTML_DOCUMENT_NODE)) {
2078 list = curstyle->elemMatch;
2079 while ((list != NULL) &&
2080 ((ret == NULL) || (list->priority > priority))) {
2081 if (xsltTestCompMatch(ctxt, list, node,
2082 ctxt->mode, ctxt->modeURI)) {
2083 ret = list->template;
2084 priority = list->priority;
2091 if (node->_private != NULL) {
2092 list = curstyle->keyMatch;
2093 while ((list != NULL) &&
2094 ((ret == NULL) || (list->priority > priority))) {
2095 if (xsltTestCompMatch(ctxt, list, node,
2096 ctxt->mode, ctxt->modeURI)) {
2097 ret = list->template;
2098 priority = list->priority;
2108 * Cycle on next curstylesheet import.
2110 curstyle = xsltNextImport(curstyle);
2116 * xsltCleanupTemplates:
2117 * @style: an XSLT stylesheet
2119 * Cleanup the state of the templates used by the stylesheet and
2120 * the ones it imports.
2123 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2127 * xsltFreeTemplateHashes:
2128 * @style: an XSLT stylesheet
2130 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2133 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2134 if (style->templatesHash != NULL)
2135 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2136 (xmlHashDeallocator) xsltFreeCompMatchList);
2137 if (style->rootMatch != NULL)
2138 xsltFreeCompMatchList(style->rootMatch);
2139 if (style->keyMatch != NULL)
2140 xsltFreeCompMatchList(style->keyMatch);
2141 if (style->elemMatch != NULL)
2142 xsltFreeCompMatchList(style->elemMatch);
2143 if (style->attrMatch != NULL)
2144 xsltFreeCompMatchList(style->attrMatch);
2145 if (style->parentMatch != NULL)
2146 xsltFreeCompMatchList(style->parentMatch);
2147 if (style->textMatch != NULL)
2148 xsltFreeCompMatchList(style->textMatch);
2149 if (style->piMatch != NULL)
2150 xsltFreeCompMatchList(style->piMatch);
2151 if (style->commentMatch != NULL)
2152 xsltFreeCompMatchList(style->commentMatch);
2158 * @node: a node in the source tree
2159 * @pattern: an XSLT pattern
2160 * @ctxtdoc: context document (for namespaces)
2161 * @ctxtnode: context node (for namespaces)
2163 * Determine if a node matches a pattern.
2166 xsltMatchPattern(xsltTransformContextPtr context,
2168 const xmlChar *pattern,
2170 xmlNodePtr ctxtnode)
2173 xsltCompMatchPtr first, comp;
2175 if ((context != NULL) && (pattern != NULL)) {
2176 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2177 for (comp = first; comp != NULL; comp = comp->next) {
2178 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2183 xsltFreeCompMatchList(first);