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);
271 if (op == XSLT_OP_PREDICATE) {
272 comp->steps[comp->nbStep].comp = xmlXPathCompile(value);
279 * xsltSwapTopCompMatch:
280 * @comp: the compiled match expression
282 * reverse the two top steps.
285 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
287 int j = comp->nbStep - 1;
290 register xmlChar *tmp;
292 register xmlXPathCompExprPtr expr;
294 tmp = comp->steps[i].value;
295 comp->steps[i].value = comp->steps[j].value;
296 comp->steps[j].value = tmp;
297 tmp = comp->steps[i].value2;
298 comp->steps[i].value2 = comp->steps[j].value2;
299 comp->steps[j].value2 = tmp;
300 op = comp->steps[i].op;
301 comp->steps[i].op = comp->steps[j].op;
302 comp->steps[j].op = op;
303 expr = comp->steps[i].comp;
304 comp->steps[i].comp = comp->steps[j].comp;
305 comp->steps[j].comp = expr;
310 * xsltReverseCompMatch:
311 * @comp: the compiled match expression
313 * reverse all the stack of expressions
316 xsltReverseCompMatch(xsltCompMatchPtr comp) {
318 int j = comp->nbStep - 1;
321 register xmlChar *tmp;
323 register xmlXPathCompExprPtr expr;
324 tmp = comp->steps[i].value;
325 comp->steps[i].value = comp->steps[j].value;
326 comp->steps[j].value = tmp;
327 tmp = comp->steps[i].value2;
328 comp->steps[i].value2 = comp->steps[j].value2;
329 comp->steps[j].value2 = tmp;
330 op = comp->steps[i].op;
331 comp->steps[i].op = comp->steps[j].op;
332 comp->steps[j].op = op;
333 expr = comp->steps[i].comp;
334 comp->steps[i].comp = comp->steps[j].comp;
335 comp->steps[j].comp = expr;
339 comp->steps[comp->nbStep++].op = XSLT_OP_END;
342 /************************************************************************
344 * The interpreter for the precompiled patterns *
346 ************************************************************************/
350 * @ctxt: a XSLT process context
351 * @comp: the precompiled pattern
353 * @mode: the mode name or NULL
354 * @modeURI: the mode URI or NULL
356 * Test wether the node matches the pattern
358 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
361 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
362 xmlNodePtr node, const xmlChar *mode,
363 const xmlChar *modeURI) {
365 xsltStepOpPtr step, select = NULL;
367 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
368 xsltPrintErrorContext(ctxt, NULL, node);
369 xsltGenericError(xsltGenericErrorContext,
370 "xsltTestCompMatch: null arg\n");
374 if (comp->mode == NULL)
376 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
379 if (comp->mode != NULL)
382 if (modeURI != NULL) {
383 if (comp->modeURI == NULL)
385 if ((comp->modeURI != modeURI) &&
386 (!xmlStrEqual(comp->modeURI, modeURI)))
389 if (comp->modeURI != NULL)
392 for (i = 0;i < comp->nbStep;i++) {
393 step = &comp->steps[i];
394 if (step->op != XSLT_OP_PREDICATE)
400 if ((node->type == XML_DOCUMENT_NODE) ||
401 #ifdef LIBXML_DOCB_ENABLED
402 (node->type == XML_DOCB_DOCUMENT_NODE) ||
404 (node->type == XML_HTML_DOCUMENT_NODE))
408 if (node->type != XML_ELEMENT_NODE)
410 if (step->value == NULL)
412 if (!xmlStrEqual(step->value, node->name))
416 if (node->ns == NULL) {
417 if (step->value2 != NULL)
419 } else if (node->ns->href != NULL) {
420 if (step->value2 == NULL)
422 if (!xmlStrEqual(step->value2, node->ns->href))
426 case XSLT_OP_CHILD: {
429 if ((node->type != XML_ELEMENT_NODE) &&
430 (node->type != XML_DOCUMENT_NODE) &&
431 #ifdef LIBXML_DOCB_ENABLED
432 (node->type != XML_DOCB_DOCUMENT_NODE) &&
434 (node->type != XML_HTML_DOCUMENT_NODE))
437 lst = node->children;
439 if (step->value != NULL) {
440 while (lst != NULL) {
441 if ((lst->type == XML_ELEMENT_NODE) &&
442 (xmlStrEqual(step->value, lst->name)))
452 if (node->type != XML_ATTRIBUTE_NODE)
454 if (step->value == NULL)
456 if (!xmlStrEqual(step->value, node->name))
460 if (node->ns == NULL) {
461 if (step->value2 != NULL)
463 } else if (node->ns->href != NULL) {
464 if (step->value2 == NULL)
466 if (!xmlStrEqual(step->value2, node->ns->href))
471 if ((node->type == XML_DOCUMENT_NODE) ||
472 (node->type == XML_HTML_DOCUMENT_NODE) ||
473 #ifdef LIBXML_DOCB_ENABLED
474 (node->type == XML_DOCB_DOCUMENT_NODE) ||
476 (node->type == XML_NAMESPACE_DECL))
481 if (step->value == NULL)
483 if (!xmlStrEqual(step->value, node->name))
486 if (node->ns == NULL) {
487 if (step->value2 != NULL)
489 } else if (node->ns->href != NULL) {
490 if (step->value2 == NULL)
492 if (!xmlStrEqual(step->value2, node->ns->href))
496 case XSLT_OP_ANCESTOR:
497 /* TODO: implement coalescing of ANCESTOR/NODE ops */
498 if (step->value == NULL) {
500 step = &comp->steps[i];
501 if (step->op == XSLT_OP_ROOT)
503 if (step->op != XSLT_OP_ELEM)
505 if (step->value == NULL)
510 if ((node->type == XML_DOCUMENT_NODE) ||
511 (node->type == XML_HTML_DOCUMENT_NODE) ||
512 #ifdef LIBXML_DOCB_ENABLED
513 (node->type == XML_DOCB_DOCUMENT_NODE) ||
515 (node->type == XML_NAMESPACE_DECL))
518 while (node != NULL) {
521 if (xmlStrEqual(step->value, node->name)) {
523 if (node->ns == NULL) {
524 if (step->value2 == NULL)
526 } else if (node->ns->href != NULL) {
527 if ((step->value2 != NULL) &&
528 (xmlStrEqual(step->value2, node->ns->href)))
538 /* TODO Handle IDs decently, must be done differently */
541 if (node->type != XML_ELEMENT_NODE)
544 id = xmlGetID(node->doc, step->value);
545 if ((id == NULL) || (id->parent != node))
553 list = xsltGetKey(ctxt, step->value,
554 step->value3, step->value2);
557 for (indx = 0;indx < list->nodeNr;indx++)
558 if (list->nodeTab[indx] == node)
560 if (indx >= list->nodeNr)
565 if (node->type != XML_ELEMENT_NODE)
567 if (node->ns == NULL) {
568 if (step->value != NULL)
570 } else if (node->ns->href != NULL) {
571 if (step->value == NULL)
573 if (!xmlStrEqual(step->value, node->ns->href))
578 if (node->type != XML_ELEMENT_NODE)
581 case XSLT_OP_PREDICATE: {
584 int pos = 0, len = 0;
586 * The simple existing predicate code cannot handle
587 * properly cascaded predicates. If in this situation
588 * compute directly the full node list once and check
589 * if the node is in the result list.
591 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
593 xmlXPathObjectPtr list;
596 previous = (xmlNodePtr)
597 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
599 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
600 list = (xmlXPathObjectPtr)
601 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
605 if (comp->pattern[0] == '/')
606 query = xmlStrdup(comp->pattern);
608 query = xmlStrdup((const xmlChar *)"//");
609 query = xmlStrcat(query, comp->pattern);
611 list = xmlXPathEval(query, ctxt->xpathCtxt);
615 if (list->type != XPATH_NODESET) {
616 xmlXPathFreeObject(list);
619 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
621 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
622 (xmlFreeFunc) xmlXPathFreeObject;
624 if ((list->nodesetval == NULL) ||
625 (list->nodesetval->nodeNr <= 0))
628 for (j = 0;j < list->nodesetval->nodeNr;j++) {
629 if (list->nodesetval->nodeTab[j] == node) {
638 * Depending on the last selection, one may need to
639 * recompute contextSize and proximityPosition.
641 * TODO: make this thread safe !
643 oldCS = ctxt->xpathCtxt->contextSize;
644 oldCP = ctxt->xpathCtxt->proximityPosition;
645 if ((select != NULL) &&
646 (select->op == XSLT_OP_ELEM) &&
647 (select->value != NULL) &&
648 (node->type == XML_ELEMENT_NODE) &&
649 (node->parent != NULL)) {
653 previous = (xmlNodePtr)
654 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
656 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
657 if ((previous != NULL) &&
658 (previous->parent == node->parent)) {
660 * just walk back to adjust the index
663 xmlNodePtr sibling = node;
665 while (sibling != NULL) {
666 if (sibling == previous)
668 if (xmlStrEqual(node->name, sibling->name)) {
669 if ((select->value2 == NULL) ||
670 ((sibling->ns != NULL) &&
671 (xmlStrEqual(select->value2,
672 sibling->ns->href))))
675 sibling = sibling->prev;
677 if (sibling == NULL) {
678 /* hum going backward in document order ... */
681 while (sibling != NULL) {
682 if (sibling == previous)
684 if ((select->value2 == NULL) ||
685 ((sibling->ns != NULL) &&
686 (xmlStrEqual(select->value2,
687 sibling->ns->href))))
689 sibling = sibling->next;
692 if (sibling != NULL) {
695 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
696 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
698 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
705 * recompute the index
707 xmlNodePtr siblings = node->parent->children;
709 while (siblings != NULL) {
710 if (siblings->type == XML_ELEMENT_NODE) {
711 if (siblings == node) {
714 } else if (xmlStrEqual(node->name,
716 if ((select->value2 == NULL) ||
717 ((siblings->ns != NULL) &&
718 (xmlStrEqual(select->value2,
719 siblings->ns->href))))
723 siblings = siblings->next;
727 ctxt->xpathCtxt->contextSize = len;
728 ctxt->xpathCtxt->proximityPosition = pos;
729 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
731 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
733 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
736 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
737 (node->type == XML_ELEMENT_NODE)) {
741 previous = (xmlNodePtr)
742 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
744 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
745 if ((previous != NULL) &&
746 (previous->parent == node->parent)) {
748 * just walk back to adjust the index
751 xmlNodePtr sibling = node;
753 while (sibling != NULL) {
754 if (sibling == previous)
756 if (sibling->type == XML_ELEMENT_NODE)
758 sibling = sibling->prev;
760 if (sibling == NULL) {
761 /* hum going backward in document order ... */
764 while (sibling != NULL) {
765 if (sibling == previous)
767 if (sibling->type == XML_ELEMENT_NODE)
769 sibling = sibling->next;
772 if (sibling != NULL) {
775 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
776 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
778 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
784 * recompute the index
786 xmlNodePtr siblings = node->parent->children;
788 while (siblings != NULL) {
789 if (siblings->type == XML_ELEMENT_NODE) {
791 if (siblings == node) {
795 siblings = siblings->next;
799 ctxt->xpathCtxt->contextSize = len;
800 ctxt->xpathCtxt->proximityPosition = pos;
801 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
803 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
805 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
809 oldNode = ctxt->node;
812 if (step->value == NULL)
814 if (step->comp == NULL)
817 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
822 ctxt->xpathCtxt->contextSize = oldCS;
823 ctxt->xpathCtxt->proximityPosition = oldCP;
825 ctxt->node = oldNode;
829 ctxt->xpathCtxt->contextSize = oldCS;
830 ctxt->xpathCtxt->proximityPosition = oldCP;
832 ctxt->node = oldNode;
836 if (node->type != XML_PI_NODE)
838 if (step->value != NULL) {
839 if (!xmlStrEqual(step->value, node->name))
843 case XSLT_OP_COMMENT:
844 if (node->type != XML_COMMENT_NODE)
848 if ((node->type != XML_TEXT_NODE) &&
849 (node->type != XML_CDATA_SECTION_NODE))
853 switch (node->type) {
854 case XML_DOCUMENT_NODE:
855 case XML_HTML_DOCUMENT_NODE:
856 #ifdef LIBXML_DOCB_ENABLED
857 case XML_DOCB_DOCUMENT_NODE:
859 case XML_ELEMENT_NODE:
860 case XML_CDATA_SECTION_NODE:
862 case XML_COMMENT_NODE:
864 case XML_ATTRIBUTE_NODE:
876 * xsltTestCompMatchList:
877 * @ctxt: a XSLT process context
879 * @comp: the precompiled pattern list
881 * Test wether the node matches one of the patterns in the list
883 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
886 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
887 xsltCompMatchPtr comp) {
890 if ((ctxt == NULL) || (node == NULL))
892 while (comp != NULL) {
893 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
901 /************************************************************************
903 * Dedicated parser for templates *
905 ************************************************************************/
907 #define CUR (*ctxt->cur)
908 #define SKIP(val) ctxt->cur += (val)
909 #define NXT(val) ctxt->cur[(val)]
910 #define CUR_PTR ctxt->cur
912 #define SKIP_BLANKS \
913 while (IS_BLANK(CUR)) NEXT
915 #define CURRENT (*ctxt->cur)
916 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
919 #define PUSH(op, val, val2) \
920 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
923 xsltSwapTopCompMatch(ctxt->comp);
925 #define XSLT_ERROR(X) \
926 { xsltError(ctxt, __FILE__, __LINE__, X); \
927 ctxt->error = (X); return; }
929 #define XSLT_ERROR0(X) \
930 { xsltError(ctxt, __FILE__, __LINE__, X); \
931 ctxt->error = (X); return(0); }
935 * @ctxt: the XPath Parser context
937 * Parse an XPath Litteral:
939 * [29] Literal ::= '"' [^"]* '"'
942 * Returns the Literal parsed or NULL
946 xsltScanLiteral(xsltParserContextPtr ctxt) {
947 const xmlChar *q, *cur;
955 val = xmlStringCurrentChar(NULL, cur, &len);
956 while ((IS_CHAR(val)) && (val != '"')) {
958 val = xmlStringCurrentChar(NULL, cur, &len);
964 ret = xmlStrndup(q, cur - q);
968 } else if (CUR == '\'') {
971 val = xmlStringCurrentChar(NULL, cur, &len);
972 while ((IS_CHAR(val)) && (val != '\'')) {
974 val = xmlStringCurrentChar(NULL, cur, &len);
980 ret = xmlStrndup(q, cur - q);
985 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
994 * @ctxt: the XPath Parser context
996 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
997 * CombiningChar | Extender
999 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1001 * [6] Names ::= Name (S Name)*
1003 * Returns the Name parsed or NULL
1007 xsltScanName(xsltParserContextPtr ctxt) {
1008 const xmlChar *q, *cur;
1009 xmlChar *ret = NULL;
1015 val = xmlStringCurrentChar(NULL, cur, &len);
1016 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1019 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1020 (val == '.') || (val == '-') ||
1022 (IS_COMBINING(val)) ||
1023 (IS_EXTENDER(val))) {
1025 val = xmlStringCurrentChar(NULL, cur, &len);
1027 ret = xmlStrndup(q, cur - q);
1034 * @ctxt: the XPath Parser context
1036 * Parses a non qualified name
1038 * Returns the Name parsed or NULL
1042 xsltScanNCName(xsltParserContextPtr ctxt) {
1043 const xmlChar *q, *cur;
1044 xmlChar *ret = NULL;
1050 val = xmlStringCurrentChar(NULL, cur, &len);
1051 if (!IS_LETTER(val) && (val != '_'))
1054 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1055 (val == '.') || (val == '-') ||
1057 (IS_COMBINING(val)) ||
1058 (IS_EXTENDER(val))) {
1060 val = xmlStringCurrentChar(NULL, cur, &len);
1062 ret = xmlStrndup(q, cur - q);
1069 * @ctxt: the XPath Parser context
1070 * @prefix: the place to store the prefix
1072 * Parse a qualified name
1074 * Returns the Name parsed or NULL
1078 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1079 xmlChar *ret = NULL;
1082 ret = xsltScanNCName(ctxt);
1086 ret = xsltScanNCName(ctxt);
1092 * xsltCompileIdKeyPattern:
1093 * @ctxt: the compilation context
1094 * @name: a preparsed name
1095 * @aid: whether id/key are allowed there
1097 * Compile the XSLT LocationIdKeyPattern
1098 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1099 * | 'key' '(' Literal ',' Literal ')'
1101 * also handle NodeType and PI from:
1103 * [7] NodeTest ::= NameTest
1104 * | NodeType '(' ')'
1105 * | 'processing-instruction' '(' Literal ')'
1108 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1109 xmlChar *lit = NULL;
1110 xmlChar *lit2 = NULL;
1113 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1114 xsltGenericError(xsltGenericErrorContext,
1115 "xsltCompileIdKeyPattern : ( expected\n");
1119 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1122 lit = xsltScanLiteral(ctxt);
1127 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1128 xsltGenericError(xsltGenericErrorContext,
1129 "xsltCompileIdKeyPattern : ) expected\n");
1134 PUSH(XSLT_OP_ID, lit, NULL);
1135 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1138 lit = xsltScanLiteral(ctxt);
1143 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1144 xsltGenericError(xsltGenericErrorContext,
1145 "xsltCompileIdKeyPattern : , expected\n");
1151 lit2 = xsltScanLiteral(ctxt);
1156 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1157 xsltGenericError(xsltGenericErrorContext,
1158 "xsltCompileIdKeyPattern : ) expected\n");
1163 /* TODO: support namespace in keys */
1164 PUSH(XSLT_OP_KEY, lit, lit2);
1165 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1169 lit = xsltScanLiteral(ctxt);
1174 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1175 xsltGenericError(xsltGenericErrorContext,
1176 "xsltCompileIdKeyPattern : ) expected\n");
1182 PUSH(XSLT_OP_PI, lit, NULL);
1183 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1187 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1188 xsltGenericError(xsltGenericErrorContext,
1189 "xsltCompileIdKeyPattern : ) expected\n");
1194 PUSH(XSLT_OP_TEXT, NULL, NULL);
1195 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1199 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1200 xsltGenericError(xsltGenericErrorContext,
1201 "xsltCompileIdKeyPattern : ) expected\n");
1206 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1207 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1211 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1212 xsltGenericError(xsltGenericErrorContext,
1213 "xsltCompileIdKeyPattern : ) expected\n");
1218 PUSH(XSLT_OP_NODE, NULL, NULL);
1220 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1221 xsltGenericError(xsltGenericErrorContext,
1222 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1226 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1227 xsltGenericError(xsltGenericErrorContext,
1228 "xsltCompileIdKeyPattern : node type\n");
1238 * xsltCompileStepPattern:
1239 * @ctxt: the compilation context
1240 * @token: a posible precompiled name
1242 * Compile the XSLT StepPattern and generates a precompiled
1243 * form suitable for fast matching.
1245 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1246 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1247 * | ('child' | 'attribute') '::'
1249 * [7] NodeTest ::= NameTest
1250 * | NodeType '(' ')'
1251 * | 'processing-instruction' '(' Literal ')'
1252 * [8] Predicate ::= '[' PredicateExpr ']'
1253 * [9] PredicateExpr ::= Expr
1254 * [13] AbbreviatedAxisSpecifier ::= '@'?
1255 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1259 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1260 xmlChar *name = NULL;
1261 const xmlChar *URI = NULL;
1262 xmlChar *URL = NULL;
1266 if ((token == NULL) && (CUR == '@')) {
1267 xmlChar *prefix = NULL;
1272 PUSH(XSLT_OP_ATTR, NULL, NULL);
1275 token = xsltScanQName(ctxt, &prefix);
1276 if (prefix != NULL) {
1279 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1281 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1282 xsltGenericError(xsltGenericErrorContext,
1283 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1286 URL = xmlStrdup(ns->href);
1290 if (token == NULL) {
1293 PUSH(XSLT_OP_ATTR, NULL, URL);
1296 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1297 xsltGenericError(xsltGenericErrorContext,
1298 "xsltCompileStepPattern : Name expected\n");
1302 PUSH(XSLT_OP_ATTR, token, URL);
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 : Name expected\n");
1324 xsltCompileIdKeyPattern(ctxt, token, 0);
1327 } else if (CUR == ':') {
1330 xmlChar *prefix = token;
1334 * This is a namespace match
1336 token = xsltScanName(ctxt);
1337 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1339 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1340 xsltGenericError(xsltGenericErrorContext,
1341 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1346 URL = xmlStrdup(ns->href);
1349 if (token == NULL) {
1352 PUSH(XSLT_OP_NS, URL, NULL);
1354 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1355 xsltGenericError(xsltGenericErrorContext,
1356 "xsltCompileStepPattern : Name expected\n");
1361 PUSH(XSLT_OP_ELEM, token, URL);
1365 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1367 token = xsltScanName(ctxt);
1368 if (token == NULL) {
1371 PUSH(XSLT_OP_ALL, token, NULL);
1372 goto parse_predicate;
1374 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1375 xsltGenericError(xsltGenericErrorContext,
1376 "xsltCompileStepPattern : QName expected\n");
1381 URI = xsltGetQNameURI(ctxt->elem, &token);
1382 if (token == NULL) {
1386 name = xmlStrdup(token);
1388 URL = xmlStrdup(URI);
1390 PUSH(XSLT_OP_CHILD, name, URL);
1391 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1393 token = xsltScanName(ctxt);
1394 if (token == NULL) {
1395 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1396 xsltGenericError(xsltGenericErrorContext,
1397 "xsltCompileStepPattern : QName expected\n");
1401 URI = xsltGetQNameURI(ctxt->elem, &token);
1402 if (token == NULL) {
1406 name = xmlStrdup(token);
1408 URL = xmlStrdup(URI);
1410 PUSH(XSLT_OP_ATTR, name, URL);
1412 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1413 xsltGenericError(xsltGenericErrorContext,
1414 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1420 } else if (CUR == '*') {
1422 PUSH(XSLT_OP_ALL, token, NULL);
1424 URI = xsltGetQNameURI(ctxt->elem, &token);
1425 if (token == NULL) {
1430 URL = xmlStrdup(URI);
1431 PUSH(XSLT_OP_ELEM, token, URL);
1436 while (CUR == '[') {
1438 xmlChar *ret = NULL;
1444 /* Skip over nested predicates */
1447 else if (CUR == ']') {
1451 } else if (CUR == '"') {
1453 while ((CUR != 0) && (CUR != '"'))
1455 } else if (CUR == '\'') {
1457 while ((CUR != 0) && (CUR != '\''))
1463 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1464 xsltGenericError(xsltGenericErrorContext,
1465 "xsltCompileStepPattern : ']' expected\n");
1469 ret = xmlStrndup(q, CUR_PTR - q);
1470 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1471 /* push the predicate lower than local test */
1485 * xsltCompileRelativePathPattern:
1486 * @comp: the compilation context
1487 * @token: a posible precompiled name
1489 * Compile the XSLT RelativePathPattern and generates a precompiled
1490 * form suitable for fast matching.
1492 * [4] RelativePathPattern ::= StepPattern
1493 * | RelativePathPattern '/' StepPattern
1494 * | RelativePathPattern '//' StepPattern
1497 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1498 xsltCompileStepPattern(ctxt, token);
1502 while ((CUR != 0) && (CUR != '|')) {
1503 if ((CUR == '/') && (NXT(1) == '/')) {
1504 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1508 xsltCompileStepPattern(ctxt, NULL);
1509 } else if (CUR == '/') {
1510 PUSH(XSLT_OP_PARENT, NULL, NULL);
1513 if ((CUR != 0) || (CUR == '|')) {
1514 xsltCompileRelativePathPattern(ctxt, NULL);
1528 * xsltCompileLocationPathPattern:
1529 * @ctxt: the compilation context
1531 * Compile the XSLT LocationPathPattern and generates a precompiled
1532 * form suitable for fast matching.
1534 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1535 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1536 * | '//'? RelativePathPattern
1539 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1541 if ((CUR == '/') && (NXT(1) == '/')) {
1543 * since we reverse the query
1544 * a leading // can be safely ignored
1548 xsltCompileRelativePathPattern(ctxt, NULL);
1549 } else if (CUR == '/') {
1551 * We need to find root as the parent
1555 PUSH(XSLT_OP_ROOT, NULL, NULL);
1556 if ((CUR != 0) || (CUR == '|')) {
1557 PUSH(XSLT_OP_PARENT, NULL, NULL);
1558 xsltCompileRelativePathPattern(ctxt, NULL);
1560 } else if (CUR == '*') {
1561 xsltCompileRelativePathPattern(ctxt, NULL);
1562 } else if (CUR == '@') {
1563 xsltCompileRelativePathPattern(ctxt, NULL);
1566 name = xsltScanName(ctxt);
1568 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1569 xsltGenericError(xsltGenericErrorContext,
1570 "xsltCompileLocationPathPattern : Name expected\n");
1575 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1576 xsltCompileIdKeyPattern(ctxt, name, 1);
1577 if ((CUR == '/') && (NXT(1) == '/')) {
1578 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1582 xsltCompileRelativePathPattern(ctxt, NULL);
1583 } else if (CUR == '/') {
1584 PUSH(XSLT_OP_PARENT, NULL, NULL);
1587 xsltCompileRelativePathPattern(ctxt, NULL);
1591 xsltCompileRelativePathPattern(ctxt, name);
1598 * xsltCompilePattern:
1599 * @pattern: an XSLT pattern
1600 * @doc: the containing document
1601 * @node: the containing element
1602 * @style: the stylesheet
1603 * @runtime: the transformation context, if done at run-time
1605 * Compile the XSLT pattern and generates a list of precompiled form suitable
1606 * for fast matching.
1608 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1610 * Returns the generated pattern list or NULL in case of failure
1614 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1615 xmlNodePtr node, xsltStylesheetPtr style,
1616 xsltTransformContextPtr runtime) {
1617 xsltParserContextPtr ctxt = NULL;
1618 xsltCompMatchPtr element, first = NULL, previous = NULL;
1619 int current, start, end, level, j;
1621 if (pattern == NULL) {
1622 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1623 xsltGenericError(xsltGenericErrorContext,
1624 "xsltCompilePattern : NULL pattern\n");
1628 ctxt = xsltNewParserContext(style, runtime);
1634 while (pattern[current] != 0) {
1636 while (IS_BLANK(pattern[current]))
1640 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1641 if (pattern[end] == '[')
1643 else if (pattern[end] == ']')
1645 else if (pattern[end] == '\'') {
1647 while ((pattern[end] != 0) && (pattern[end] != '\''))
1649 } else if (pattern[end] == '"') {
1651 while ((pattern[end] != 0) && (pattern[end] != '"'))
1656 if (current == end) {
1657 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1658 xsltGenericError(xsltGenericErrorContext,
1659 "xsltCompilePattern : NULL pattern\n");
1662 element = xsltNewCompMatch();
1663 if (element == NULL) {
1668 else if (previous != NULL)
1669 previous->next = element;
1672 ctxt->comp = element;
1673 ctxt->base = xmlStrndup(&pattern[start], end - start);
1674 if (ctxt->base == NULL)
1676 ctxt->cur = &(ctxt->base)[current - start];
1677 element->pattern = ctxt->base;
1678 element->nsList = xmlGetNsList(doc, node);
1680 if (element->nsList != NULL) {
1681 while (element->nsList[j] != NULL)
1687 #ifdef WITH_XSLT_DEBUG_PATTERN
1688 xsltGenericDebug(xsltGenericDebugContext,
1689 "xsltCompilePattern : parsing '%s'\n",
1692 xsltCompileLocationPathPattern(ctxt);
1697 * Reverse for faster interpretation.
1699 xsltReverseCompMatch(element);
1702 * Set-up the priority
1704 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1705 (element->steps[0].op == XSLT_OP_ATTR)) &&
1706 (element->steps[0].value != NULL) &&
1707 (element->steps[1].op == XSLT_OP_END)) {
1708 element->priority = 0;
1710 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1711 (element->steps[1].op == XSLT_OP_END)) {
1712 element->priority = 0;
1714 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1715 (element->steps[0].value != NULL) &&
1716 (element->steps[1].op == XSLT_OP_END)) {
1717 element->priority = 0;
1718 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1719 (element->steps[0].value2 != NULL) &&
1720 (element->steps[1].op == XSLT_OP_END)) {
1721 element->priority = -0.25;
1722 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1723 (element->steps[0].value != NULL) &&
1724 (element->steps[1].op == XSLT_OP_END)) {
1725 element->priority = -0.25;
1726 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1727 (element->steps[0].value == NULL) &&
1728 (element->steps[0].value2 == NULL) &&
1729 (element->steps[1].op == XSLT_OP_END)) {
1730 element->priority = -0.5;
1731 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1732 (element->steps[0].op == XSLT_OP_TEXT) ||
1733 (element->steps[0].op == XSLT_OP_ALL) ||
1734 (element->steps[0].op == XSLT_OP_NODE) ||
1735 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1736 (element->steps[1].op == XSLT_OP_END)) {
1737 element->priority = -0.5;
1739 element->priority = 0.5;
1741 #ifdef WITH_XSLT_DEBUG_PATTERN
1742 xsltGenericDebug(xsltGenericDebugContext,
1743 "xsltCompilePattern : parsed %s, default priority %f\n",
1744 element->pattern, element->priority);
1746 if (pattern[end] == '|')
1751 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1752 xsltGenericError(xsltGenericErrorContext,
1753 "xsltCompilePattern : NULL pattern\n");
1757 xsltFreeParserContext(ctxt);
1762 xsltFreeParserContext(ctxt);
1764 xsltFreeCompMatchList(first);
1768 /************************************************************************
1770 * Module interfaces *
1772 ************************************************************************/
1776 * @style: an XSLT stylesheet
1777 * @cur: an XSLT template
1778 * @mode: the mode name or NULL
1779 * @modeURI: the mode URI or NULL
1781 * Register the XSLT pattern associated to @cur
1783 * Returns -1 in case of error, 0 otherwise
1786 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1787 const xmlChar *mode, const xmlChar *modeURI) {
1788 xsltCompMatchPtr pat, list, *top = NULL, next;
1789 const xmlChar *name = NULL;
1790 float priority; /* the priority */
1792 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1795 priority = cur->priority;
1796 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1802 pat->template = cur;
1804 pat->mode = xmlStrdup(mode);
1805 if (modeURI != NULL)
1806 pat->modeURI = xmlStrdup(modeURI);
1807 if (priority != XSLT_PAT_NO_PRIORITY)
1808 pat->priority = priority;
1811 * insert it in the hash table list corresponding to its lookup name
1813 switch (pat->steps[0].op) {
1815 if (pat->steps[0].value != NULL)
1816 name = pat->steps[0].value;
1818 top = (xsltCompMatchPtr *) &(style->attrMatch);
1822 case XSLT_OP_PARENT:
1823 case XSLT_OP_ANCESTOR:
1824 top = (xsltCompMatchPtr *) &(style->elemMatch);
1827 top = (xsltCompMatchPtr *) &(style->rootMatch);
1830 top = (xsltCompMatchPtr *) &(style->keyMatch);
1833 /* TODO optimize ID !!! */
1836 top = (xsltCompMatchPtr *) &(style->elemMatch);
1839 case XSLT_OP_PREDICATE:
1840 xsltPrintErrorContext(NULL, style, NULL);
1841 xsltGenericError(xsltGenericErrorContext,
1842 "xsltAddTemplate: invalid compiled pattern\n");
1843 xsltFreeCompMatch(pat);
1846 * TODO: some flags at the top level about type based patterns
1847 * would be faster than inclusion in the hash table.
1850 if (pat->steps[0].value != NULL)
1851 name = pat->steps[0].value;
1853 top = (xsltCompMatchPtr *) &(style->piMatch);
1855 case XSLT_OP_COMMENT:
1856 top = (xsltCompMatchPtr *) &(style->commentMatch);
1859 top = (xsltCompMatchPtr *) &(style->textMatch);
1862 if (pat->steps[0].value != NULL)
1863 name = pat->steps[0].value;
1865 top = (xsltCompMatchPtr *) &(style->elemMatch);
1870 if (style->templatesHash == NULL) {
1871 style->templatesHash = xmlHashCreate(1024);
1872 if (style->templatesHash == NULL) {
1873 xsltFreeCompMatch(pat);
1876 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1878 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1879 name, mode, modeURI);
1881 xmlHashAddEntry3(style->templatesHash, name,
1882 mode, modeURI, pat);
1885 * Note '<=' since one must choose among the matching
1886 * template rules that are left, the one that occurs
1887 * last in the stylesheet
1889 if (list->priority <= pat->priority) {
1891 xmlHashUpdateEntry3(style->templatesHash, name,
1892 mode, modeURI, pat, NULL);
1894 while (list->next != NULL) {
1895 if (list->next->priority <= pat->priority)
1899 pat->next = list->next;
1904 } else if (top != NULL) {
1909 } else if (list->priority <= pat->priority) {
1913 while (list->next != NULL) {
1914 if (list->next->priority <= pat->priority)
1918 pat->next = list->next;
1922 xsltPrintErrorContext(NULL, style, NULL);
1923 xsltGenericError(xsltGenericErrorContext,
1924 "xsltAddTemplate: invalid compiled pattern\n");
1925 xsltFreeCompMatch(pat);
1928 #ifdef WITH_XSLT_DEBUG_PATTERN
1930 xsltGenericDebug(xsltGenericDebugContext,
1931 "added pattern : '%s' mode '%s' priority %f\n",
1932 pat->pattern, pat->mode, pat->priority);
1934 xsltGenericDebug(xsltGenericDebugContext,
1935 "added pattern : '%s' priority %f\n",
1936 pat->pattern, pat->priority);
1946 * @ctxt: a XSLT process context
1947 * @node: the node being processed
1948 * @style: the current style
1950 * Finds the template applying to this node, if @style is non-NULL
1951 * it means one needs to look for the next imported template in scope.
1953 * Returns the xsltTemplatePtr or NULL if not found
1956 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
1957 xsltStylesheetPtr style) {
1958 xsltStylesheetPtr curstyle;
1959 xsltTemplatePtr ret = NULL;
1960 const xmlChar *name = NULL;
1961 xsltCompMatchPtr list = NULL;
1964 if ((ctxt == NULL) || (node == NULL))
1967 if (style == NULL) {
1968 curstyle = ctxt->style;
1970 curstyle = xsltNextImport(style);
1973 while ((curstyle != NULL) && (curstyle != style)) {
1974 priority = XSLT_PAT_NO_PRIORITY;
1975 /* TODO : handle IDs/keys here ! */
1976 if (curstyle->templatesHash != NULL) {
1978 * Use the top name as selector
1980 switch (node->type) {
1981 case XML_ELEMENT_NODE:
1982 case XML_ATTRIBUTE_NODE:
1986 case XML_DOCUMENT_NODE:
1987 case XML_HTML_DOCUMENT_NODE:
1989 case XML_CDATA_SECTION_NODE:
1990 case XML_COMMENT_NODE:
1991 case XML_ENTITY_REF_NODE:
1992 case XML_ENTITY_NODE:
1993 case XML_DOCUMENT_TYPE_NODE:
1994 case XML_DOCUMENT_FRAG_NODE:
1995 case XML_NOTATION_NODE:
1997 case XML_ELEMENT_DECL:
1998 case XML_ATTRIBUTE_DECL:
1999 case XML_ENTITY_DECL:
2000 case XML_NAMESPACE_DECL:
2001 case XML_XINCLUDE_START:
2002 case XML_XINCLUDE_END:
2011 * find the list of appliable expressions based on the name
2013 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2014 name, ctxt->mode, ctxt->modeURI);
2017 while (list != NULL) {
2018 if (xsltTestCompMatch(ctxt, list, node,
2019 ctxt->mode, ctxt->modeURI)) {
2020 ret = list->template;
2021 priority = list->priority;
2029 * find alternate generic matches
2031 switch (node->type) {
2032 case XML_ELEMENT_NODE:
2033 list = curstyle->elemMatch;
2035 case XML_ATTRIBUTE_NODE:
2036 list = curstyle->attrMatch;
2039 list = curstyle->piMatch;
2041 case XML_DOCUMENT_NODE:
2042 case XML_HTML_DOCUMENT_NODE:
2043 list = curstyle->rootMatch;
2046 case XML_CDATA_SECTION_NODE:
2047 list = curstyle->textMatch;
2049 case XML_COMMENT_NODE:
2050 list = curstyle->commentMatch;
2052 case XML_ENTITY_REF_NODE:
2053 case XML_ENTITY_NODE:
2054 case XML_DOCUMENT_TYPE_NODE:
2055 case XML_DOCUMENT_FRAG_NODE:
2056 case XML_NOTATION_NODE:
2058 case XML_ELEMENT_DECL:
2059 case XML_ATTRIBUTE_DECL:
2060 case XML_ENTITY_DECL:
2061 case XML_NAMESPACE_DECL:
2062 case XML_XINCLUDE_START:
2063 case XML_XINCLUDE_END:
2069 while ((list != NULL) &&
2070 ((ret == NULL) || (list->priority > priority))) {
2071 if (xsltTestCompMatch(ctxt, list, node,
2072 ctxt->mode, ctxt->modeURI)) {
2073 ret = list->template;
2074 priority = list->priority;
2080 * Some of the tests for elements can also apply to documents
2082 if ((node->type == XML_DOCUMENT_NODE) ||
2083 (node->type == XML_HTML_DOCUMENT_NODE)) {
2084 list = curstyle->elemMatch;
2085 while ((list != NULL) &&
2086 ((ret == NULL) || (list->priority > priority))) {
2087 if (xsltTestCompMatch(ctxt, list, node,
2088 ctxt->mode, ctxt->modeURI)) {
2089 ret = list->template;
2090 priority = list->priority;
2097 if (node->_private != NULL) {
2098 list = curstyle->keyMatch;
2099 while ((list != NULL) &&
2100 ((ret == NULL) || (list->priority > priority))) {
2101 if (xsltTestCompMatch(ctxt, list, node,
2102 ctxt->mode, ctxt->modeURI)) {
2103 ret = list->template;
2104 priority = list->priority;
2114 * Cycle on next curstylesheet import.
2116 curstyle = xsltNextImport(curstyle);
2122 * xsltCleanupTemplates:
2123 * @style: an XSLT stylesheet
2125 * Cleanup the state of the templates used by the stylesheet and
2126 * the ones it imports.
2129 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2133 * xsltFreeTemplateHashes:
2134 * @style: an XSLT stylesheet
2136 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2139 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2140 if (style->templatesHash != NULL)
2141 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2142 (xmlHashDeallocator) xsltFreeCompMatchList);
2143 if (style->rootMatch != NULL)
2144 xsltFreeCompMatchList(style->rootMatch);
2145 if (style->keyMatch != NULL)
2146 xsltFreeCompMatchList(style->keyMatch);
2147 if (style->elemMatch != NULL)
2148 xsltFreeCompMatchList(style->elemMatch);
2149 if (style->attrMatch != NULL)
2150 xsltFreeCompMatchList(style->attrMatch);
2151 if (style->parentMatch != NULL)
2152 xsltFreeCompMatchList(style->parentMatch);
2153 if (style->textMatch != NULL)
2154 xsltFreeCompMatchList(style->textMatch);
2155 if (style->piMatch != NULL)
2156 xsltFreeCompMatchList(style->piMatch);
2157 if (style->commentMatch != NULL)
2158 xsltFreeCompMatchList(style->commentMatch);
2164 * @node: a node in the source tree
2165 * @pattern: an XSLT pattern
2166 * @ctxtdoc: context document (for namespaces)
2167 * @ctxtnode: context node (for namespaces)
2169 * Determine if a node matches a pattern.
2172 xsltMatchPattern(xsltTransformContextPtr context,
2174 const xmlChar *pattern,
2176 xmlNodePtr ctxtnode)
2179 xsltCompMatchPtr first, comp;
2181 if ((context != NULL) && (pattern != NULL)) {
2182 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2183 for (comp = first; comp != NULL; comp = comp->next) {
2184 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2189 xsltFreeCompMatchList(first);