2 * pattern.c: Implemetation of the template match compilation and lookup
5 * http://www.w3.org/TR/1999/REC-xslt-19991116
7 * See Copyright for the status of this software.
13 * TODO: handle pathological cases like *[*[@a="b"]]
14 * TODO: detect [number] at compilation, optimize accordingly
22 #include <libxml/xmlmemory.h>
23 #include <libxml/tree.h>
24 #include <libxml/valid.h>
25 #include <libxml/hash.h>
26 #include <libxml/xmlerror.h>
27 #include <libxml/parserInternals.h>
29 #include "xsltInternals.h"
30 #include "xsltutils.h"
32 #include "templates.h"
36 #ifdef WITH_XSLT_DEBUG
37 #define WITH_XSLT_DEBUG_PATTERN
64 typedef struct _xsltStepOp xsltStepOp;
65 typedef xsltStepOp *xsltStepOpPtr;
71 xmlXPathCompExprPtr comp;
73 * Optimisations for count
80 struct _xsltCompMatch {
81 struct _xsltCompMatch *next; /* siblings in the name hash */
82 float priority; /* the priority */
83 const xmlChar *pattern; /* the pattern */
84 const xmlChar *mode; /* the mode */
85 const xmlChar *modeURI; /* the mode URI */
86 xsltTemplatePtr template; /* the associated template */
88 /* TODO fix the statically allocated size steps[] */
91 xmlNsPtr *nsList; /* the namespaces in scope */
92 int nsNr; /* the number of namespaces in scope */
93 xsltStepOp steps[40]; /* ops for computation */
96 typedef struct _xsltParserContext xsltParserContext;
97 typedef xsltParserContext *xsltParserContextPtr;
98 struct _xsltParserContext {
99 xsltStylesheetPtr style; /* the stylesheet */
100 xsltTransformContextPtr ctxt; /* the transformation or NULL */
101 const xmlChar *cur; /* the current char being parsed */
102 const xmlChar *base; /* the full expression */
103 xmlDocPtr doc; /* the source document */
104 xmlNodePtr elem; /* the source element */
105 int error; /* error code */
106 xsltCompMatchPtr comp; /* the result */
109 /************************************************************************
113 ************************************************************************/
118 * Create a new XSLT CompMatch
120 * Returns the newly allocated xsltCompMatchPtr or NULL in case of error
122 static xsltCompMatchPtr
123 xsltNewCompMatch(void) {
124 xsltCompMatchPtr cur;
126 cur = (xsltCompMatchPtr) xmlMalloc(sizeof(xsltCompMatch));
128 xsltTransformError(NULL, NULL, NULL,
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 xsltTransformError(NULL, NULL, NULL,
208 "xsltNewParserContext : malloc failed\n");
211 memset(cur, 0, sizeof(xsltParserContext));
218 * xsltFreeParserContext:
219 * @ctxt: an XSLT parser context
221 * Free up the memory allocated by @ctxt
224 xsltFreeParserContext(xsltParserContextPtr ctxt) {
227 memset(ctxt, -1, sizeof(xsltParserContext));
233 * @comp: the compiled match expression
235 * @value: the first value
236 * @value2: the second value
238 * Add an step to an XSLT Compiled Match
240 * Returns -1 in case of failure, 0 otherwise.
243 xsltCompMatchAdd(xsltParserContextPtr ctxt, xsltCompMatchPtr comp,
244 xsltOp op, xmlChar * value, xmlChar * value2)
246 if (comp->nbStep >= 40) {
247 xsltTransformError(NULL, NULL, NULL,
248 "xsltCompMatchAdd: overflow\n");
251 comp->steps[comp->nbStep].op = op;
252 comp->steps[comp->nbStep].value = value;
253 comp->steps[comp->nbStep].value2 = value2;
254 if (ctxt->ctxt != NULL) {
255 comp->steps[comp->nbStep].previousExtra =
256 xsltAllocateExtraCtxt(ctxt->ctxt);
257 comp->steps[comp->nbStep].indexExtra =
258 xsltAllocateExtraCtxt(ctxt->ctxt);
259 comp->steps[comp->nbStep].lenExtra =
260 xsltAllocateExtraCtxt(ctxt->ctxt);
262 comp->steps[comp->nbStep].previousExtra =
263 xsltAllocateExtra(ctxt->style);
264 comp->steps[comp->nbStep].indexExtra =
265 xsltAllocateExtra(ctxt->style);
266 comp->steps[comp->nbStep].lenExtra =
267 xsltAllocateExtra(ctxt->style);
269 if (op == XSLT_OP_PREDICATE) {
270 comp->steps[comp->nbStep].comp = xmlXPathCompile(value);
277 * xsltSwapTopCompMatch:
278 * @comp: the compiled match expression
280 * reverse the two top steps.
283 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
285 int j = comp->nbStep - 1;
288 register xmlChar *tmp;
290 register xmlXPathCompExprPtr expr;
292 tmp = comp->steps[i].value;
293 comp->steps[i].value = comp->steps[j].value;
294 comp->steps[j].value = tmp;
295 tmp = comp->steps[i].value2;
296 comp->steps[i].value2 = comp->steps[j].value2;
297 comp->steps[j].value2 = tmp;
298 op = comp->steps[i].op;
299 comp->steps[i].op = comp->steps[j].op;
300 comp->steps[j].op = op;
301 expr = comp->steps[i].comp;
302 comp->steps[i].comp = comp->steps[j].comp;
303 comp->steps[j].comp = expr;
308 * xsltReverseCompMatch:
309 * @comp: the compiled match expression
311 * reverse all the stack of expressions
314 xsltReverseCompMatch(xsltCompMatchPtr comp) {
316 int j = comp->nbStep - 1;
319 register xmlChar *tmp;
321 register xmlXPathCompExprPtr expr;
322 tmp = comp->steps[i].value;
323 comp->steps[i].value = comp->steps[j].value;
324 comp->steps[j].value = tmp;
325 tmp = comp->steps[i].value2;
326 comp->steps[i].value2 = comp->steps[j].value2;
327 comp->steps[j].value2 = tmp;
328 op = comp->steps[i].op;
329 comp->steps[i].op = comp->steps[j].op;
330 comp->steps[j].op = op;
331 expr = comp->steps[i].comp;
332 comp->steps[i].comp = comp->steps[j].comp;
333 comp->steps[j].comp = expr;
337 comp->steps[comp->nbStep++].op = XSLT_OP_END;
340 /************************************************************************
342 * The interpreter for the precompiled patterns *
344 ************************************************************************/
348 * @ctxt: a XSLT process context
349 * @comp: the precompiled pattern
351 * @mode: the mode name or NULL
352 * @modeURI: the mode URI or NULL
354 * Test wether the node matches the pattern
356 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
359 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
360 xmlNodePtr node, const xmlChar *mode,
361 const xmlChar *modeURI) {
363 xsltStepOpPtr step, select = NULL;
365 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
366 xsltTransformError(ctxt, NULL, node,
367 "xsltTestCompMatch: null arg\n");
371 if (comp->mode == NULL)
373 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
376 if (comp->mode != NULL)
379 if (modeURI != NULL) {
380 if (comp->modeURI == NULL)
382 if ((comp->modeURI != modeURI) &&
383 (!xmlStrEqual(comp->modeURI, modeURI)))
386 if (comp->modeURI != NULL)
389 for (i = 0;i < comp->nbStep;i++) {
390 step = &comp->steps[i];
391 if (step->op != XSLT_OP_PREDICATE)
397 if ((node->type == XML_DOCUMENT_NODE) ||
398 #ifdef LIBXML_DOCB_ENABLED
399 (node->type == XML_DOCB_DOCUMENT_NODE) ||
401 (node->type == XML_HTML_DOCUMENT_NODE))
405 if (node->type != XML_ELEMENT_NODE)
407 if (step->value == NULL)
409 if (step->value[0] != node->name[0])
411 if (!xmlStrEqual(step->value, node->name))
415 if (node->ns == NULL) {
416 if (step->value2 != NULL)
418 } else if (node->ns->href != NULL) {
419 if (step->value2 == NULL)
421 if (!xmlStrEqual(step->value2, node->ns->href))
425 case XSLT_OP_CHILD: {
428 if ((node->type != XML_ELEMENT_NODE) &&
429 (node->type != XML_DOCUMENT_NODE) &&
430 #ifdef LIBXML_DOCB_ENABLED
431 (node->type != XML_DOCB_DOCUMENT_NODE) &&
433 (node->type != XML_HTML_DOCUMENT_NODE))
436 lst = node->children;
438 if (step->value != NULL) {
439 while (lst != NULL) {
440 if ((lst->type == XML_ELEMENT_NODE) &&
441 (step->value[0] == lst->name[0]) &&
442 (xmlStrEqual(step->value, lst->name)))
452 if (node->type != XML_ATTRIBUTE_NODE)
454 if (step->value != NULL) {
455 if (step->value[0] != node->name[0])
457 if (!xmlStrEqual(step->value, node->name))
461 if (node->ns == NULL) {
462 if (step->value2 != NULL)
464 } else if (step->value2 != NULL) {
465 if (!xmlStrEqual(step->value2, node->ns->href))
470 if ((node->type == XML_DOCUMENT_NODE) ||
471 (node->type == XML_HTML_DOCUMENT_NODE) ||
472 #ifdef LIBXML_DOCB_ENABLED
473 (node->type == XML_DOCB_DOCUMENT_NODE) ||
475 (node->type == XML_NAMESPACE_DECL))
480 if (step->value == NULL)
482 if (step->value[0] != node->name[0])
484 if (!xmlStrEqual(step->value, node->name))
487 if (node->ns == NULL) {
488 if (step->value2 != NULL)
490 } else if (node->ns->href != NULL) {
491 if (step->value2 == NULL)
493 if (!xmlStrEqual(step->value2, node->ns->href))
497 case XSLT_OP_ANCESTOR:
498 /* TODO: implement coalescing of ANCESTOR/NODE ops */
499 if (step->value == NULL) {
501 step = &comp->steps[i];
502 if (step->op == XSLT_OP_ROOT)
504 if (step->op != XSLT_OP_ELEM)
506 if (step->value == NULL)
511 if ((node->type == XML_DOCUMENT_NODE) ||
512 (node->type == XML_HTML_DOCUMENT_NODE) ||
513 #ifdef LIBXML_DOCB_ENABLED
514 (node->type == XML_DOCB_DOCUMENT_NODE) ||
516 (node->type == XML_NAMESPACE_DECL))
519 while (node != NULL) {
522 if ((node->type == XML_ELEMENT_NODE) &&
523 (step->value[0] == node->name[0]) &&
524 (xmlStrEqual(step->value, node->name))) {
526 if (node->ns == NULL) {
527 if (step->value2 == NULL)
529 } else if (node->ns->href != NULL) {
530 if ((step->value2 != NULL) &&
531 (xmlStrEqual(step->value2, node->ns->href)))
541 /* TODO Handle IDs decently, must be done differently */
544 if (node->type != XML_ELEMENT_NODE)
547 id = xmlGetID(node->doc, step->value);
548 if ((id == NULL) || (id->parent != node))
556 list = xsltGetKey(ctxt, step->value,
557 step->value3, step->value2);
560 for (indx = 0;indx < list->nodeNr;indx++)
561 if (list->nodeTab[indx] == node)
563 if (indx >= list->nodeNr)
568 if (node->type != XML_ELEMENT_NODE)
570 if (node->ns == NULL) {
571 if (step->value != NULL)
573 } else if (node->ns->href != NULL) {
574 if (step->value == NULL)
576 if (!xmlStrEqual(step->value, node->ns->href))
581 if (node->type != XML_ELEMENT_NODE)
584 case XSLT_OP_PREDICATE: {
587 int pos = 0, len = 0;
589 * The simple existing predicate code cannot handle
590 * properly cascaded predicates. If in this situation
591 * compute directly the full node list once and check
592 * if the node is in the result list.
594 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
595 xmlDocPtr prevdoc, doc;
596 xmlXPathObjectPtr list;
600 prevdoc = (xmlDocPtr)
601 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
603 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
604 list = (xmlXPathObjectPtr)
605 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra);
608 if ((list == NULL) || (prevdoc != doc)) {
610 xmlXPathObjectPtr newlist;
611 xmlNodePtr parent = node->parent;
613 if (comp->pattern[0] == '/')
614 query = xmlStrdup(comp->pattern);
616 query = xmlStrdup((const xmlChar *)"//");
617 query = xmlStrcat(query, comp->pattern);
619 newlist = xmlXPathEval(query, ctxt->xpathCtxt);
623 if (newlist->type != XPATH_NODESET) {
624 xmlXPathFreeObject(newlist);
628 if ((parent == NULL) || (node->doc == NULL))
631 while (parent->parent != NULL)
632 parent = parent->parent;
633 if (((parent->type != XML_DOCUMENT_NODE) &&
634 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
635 (parent != (xmlNodePtr) node->doc))
640 xmlXPathFreeObject(list);
643 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra) =
645 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
647 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
648 (xmlFreeFunc) xmlXPathFreeObject;
652 if ((list->nodesetval == NULL) ||
653 (list->nodesetval->nodeNr <= 0)) {
655 xmlXPathFreeObject(list);
658 /* TODO: store the index and use it for the scan */
660 for (j = 0;j < list->nodesetval->nodeNr;j++) {
661 if (list->nodesetval->nodeTab[j] == node) {
663 xmlXPathFreeObject(list);
670 xmlXPathFreeObject(list);
674 * Depending on the last selection, one may need to
675 * recompute contextSize and proximityPosition.
677 * TODO: make this thread safe !
679 oldCS = ctxt->xpathCtxt->contextSize;
680 oldCP = ctxt->xpathCtxt->proximityPosition;
681 if ((select != NULL) &&
682 (select->op == XSLT_OP_ELEM) &&
683 (select->value != NULL) &&
684 (node->type == XML_ELEMENT_NODE) &&
685 (node->parent != NULL)) {
687 int index, nocache = 0;
689 previous = (xmlNodePtr)
690 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
692 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
693 if ((previous != NULL) &&
694 (previous->parent == node->parent)) {
696 * just walk back to adjust the index
699 xmlNodePtr sibling = node;
701 while (sibling != NULL) {
702 if (sibling == previous)
704 if ((node->type == XML_ELEMENT_NODE) &&
705 (node->name != NULL) &&
706 (sibling->name != NULL) &&
707 (node->name[0] == sibling->name[0]) &&
708 (xmlStrEqual(node->name, sibling->name))) {
709 if ((select->value2 == NULL) ||
710 ((sibling->ns != NULL) &&
711 (xmlStrEqual(select->value2,
712 sibling->ns->href))))
715 sibling = sibling->prev;
717 if (sibling == NULL) {
718 /* hum going backward in document order ... */
721 while (sibling != NULL) {
722 if (sibling == previous)
724 if ((select->value2 == NULL) ||
725 ((sibling->ns != NULL) &&
726 (xmlStrEqual(select->value2,
727 sibling->ns->href))))
729 sibling = sibling->next;
732 if (sibling != NULL) {
735 * If the node is in a Value Tree we cannot
738 if (node->doc != NULL) {
740 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
741 XSLT_RUNTIME_EXTRA(ctxt,
742 select->previousExtra) = node;
743 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
751 * recompute the index
753 xmlNodePtr siblings = node->parent->children;
754 xmlNodePtr parent = node->parent;
756 while (siblings != NULL) {
757 if (siblings->type == XML_ELEMENT_NODE) {
758 if (siblings == node) {
761 } else if ((node->name != NULL) &&
762 (siblings->name != NULL) &&
763 (node->name[0] == siblings->name[0]) &&
764 (xmlStrEqual(node->name, siblings->name))) {
765 if ((select->value2 == NULL) ||
766 ((siblings->ns != NULL) &&
767 (xmlStrEqual(select->value2,
768 siblings->ns->href))))
772 siblings = siblings->next;
774 if ((parent == NULL) || (node->doc == NULL))
777 while (parent->parent != NULL)
778 parent = parent->parent;
779 if (((parent->type != XML_DOCUMENT_NODE) &&
780 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
781 (parent != (xmlNodePtr) node->doc))
786 ctxt->xpathCtxt->contextSize = len;
787 ctxt->xpathCtxt->proximityPosition = pos;
789 * If the node is in a Value Tree we cannot
792 if ((node->doc != NULL) && (nocache == 0)) {
793 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
795 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
797 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
801 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
802 (node->type == XML_ELEMENT_NODE)) {
804 int index, nocache = 0;
806 previous = (xmlNodePtr)
807 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
809 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
810 if ((previous != NULL) &&
811 (previous->parent == node->parent)) {
813 * just walk back to adjust the index
816 xmlNodePtr sibling = node;
818 while (sibling != NULL) {
819 if (sibling == previous)
821 if (sibling->type == XML_ELEMENT_NODE)
823 sibling = sibling->prev;
825 if (sibling == NULL) {
826 /* hum going backward in document order ... */
829 while (sibling != NULL) {
830 if (sibling == previous)
832 if (sibling->type == XML_ELEMENT_NODE)
834 sibling = sibling->next;
837 if (sibling != NULL) {
840 * If the node is in a Value Tree we cannot
843 if (node->doc != NULL) {
845 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
846 XSLT_RUNTIME_EXTRA(ctxt,
847 select->previousExtra) = node;
848 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
855 * recompute the index
857 xmlNodePtr siblings = node->parent->children;
858 xmlNodePtr parent = node->parent;
860 while (siblings != NULL) {
861 if (siblings->type == XML_ELEMENT_NODE) {
863 if (siblings == node) {
867 siblings = siblings->next;
869 if ((parent == NULL) || (node->doc == NULL))
872 while (parent->parent != NULL)
873 parent = parent->parent;
874 if (((parent->type != XML_DOCUMENT_NODE) &&
875 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
876 (parent != (xmlNodePtr) node->doc))
881 ctxt->xpathCtxt->contextSize = len;
882 ctxt->xpathCtxt->proximityPosition = pos;
884 * If the node is in a Value Tree we cannot
887 if ((node->doc != NULL) && (nocache == 0)) {
888 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
890 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
892 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
897 oldNode = ctxt->node;
900 if (step->value == NULL)
902 if (step->comp == NULL)
905 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
910 ctxt->xpathCtxt->contextSize = oldCS;
911 ctxt->xpathCtxt->proximityPosition = oldCP;
913 ctxt->node = oldNode;
917 ctxt->xpathCtxt->contextSize = oldCS;
918 ctxt->xpathCtxt->proximityPosition = oldCP;
920 ctxt->node = oldNode;
924 if (node->type != XML_PI_NODE)
926 if (step->value != NULL) {
927 if (!xmlStrEqual(step->value, node->name))
931 case XSLT_OP_COMMENT:
932 if (node->type != XML_COMMENT_NODE)
936 if ((node->type != XML_TEXT_NODE) &&
937 (node->type != XML_CDATA_SECTION_NODE))
941 switch (node->type) {
942 case XML_ELEMENT_NODE:
943 case XML_CDATA_SECTION_NODE:
945 case XML_COMMENT_NODE:
958 * xsltTestCompMatchList:
959 * @ctxt: a XSLT process context
961 * @comp: the precompiled pattern list
963 * Test wether the node matches one of the patterns in the list
965 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
968 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
969 xsltCompMatchPtr comp) {
972 if ((ctxt == NULL) || (node == NULL))
974 while (comp != NULL) {
975 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
983 /************************************************************************
985 * Dedicated parser for templates *
987 ************************************************************************/
989 #define CUR (*ctxt->cur)
990 #define SKIP(val) ctxt->cur += (val)
991 #define NXT(val) ctxt->cur[(val)]
992 #define CUR_PTR ctxt->cur
994 #define SKIP_BLANKS \
995 while (IS_BLANK(CUR)) NEXT
997 #define CURRENT (*ctxt->cur)
998 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1001 #define PUSH(op, val, val2) \
1002 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1005 xsltSwapTopCompMatch(ctxt->comp);
1007 #define XSLT_ERROR(X) \
1008 { xsltError(ctxt, __FILE__, __LINE__, X); \
1009 ctxt->error = (X); return; }
1011 #define XSLT_ERROR0(X) \
1012 { xsltError(ctxt, __FILE__, __LINE__, X); \
1013 ctxt->error = (X); return(0); }
1017 * @ctxt: the XPath Parser context
1019 * Parse an XPath Litteral:
1021 * [29] Literal ::= '"' [^"]* '"'
1024 * Returns the Literal parsed or NULL
1028 xsltScanLiteral(xsltParserContextPtr ctxt) {
1029 const xmlChar *q, *cur;
1030 xmlChar *ret = NULL;
1037 val = xmlStringCurrentChar(NULL, cur, &len);
1038 while ((IS_CHAR(val)) && (val != '"')) {
1040 val = xmlStringCurrentChar(NULL, cur, &len);
1042 if (!IS_CHAR(val)) {
1046 ret = xmlStrndup(q, cur - q);
1050 } else if (CUR == '\'') {
1053 val = xmlStringCurrentChar(NULL, cur, &len);
1054 while ((IS_CHAR(val)) && (val != '\'')) {
1056 val = xmlStringCurrentChar(NULL, cur, &len);
1058 if (!IS_CHAR(val)) {
1062 ret = xmlStrndup(q, cur - q);
1067 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1076 * @ctxt: the XPath Parser context
1078 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1079 * CombiningChar | Extender
1081 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1083 * [6] Names ::= Name (S Name)*
1085 * Returns the Name parsed or NULL
1089 xsltScanName(xsltParserContextPtr ctxt) {
1090 const xmlChar *q, *cur;
1091 xmlChar *ret = NULL;
1097 val = xmlStringCurrentChar(NULL, cur, &len);
1098 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1101 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1102 (val == '.') || (val == '-') ||
1104 (IS_COMBINING(val)) ||
1105 (IS_EXTENDER(val))) {
1107 val = xmlStringCurrentChar(NULL, cur, &len);
1109 ret = xmlStrndup(q, cur - q);
1116 * @ctxt: the XPath Parser context
1118 * Parses a non qualified name
1120 * Returns the Name parsed or NULL
1124 xsltScanNCName(xsltParserContextPtr ctxt) {
1125 const xmlChar *q, *cur;
1126 xmlChar *ret = NULL;
1132 val = xmlStringCurrentChar(NULL, cur, &len);
1133 if (!IS_LETTER(val) && (val != '_'))
1136 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1137 (val == '.') || (val == '-') ||
1139 (IS_COMBINING(val)) ||
1140 (IS_EXTENDER(val))) {
1142 val = xmlStringCurrentChar(NULL, cur, &len);
1144 ret = xmlStrndup(q, cur - q);
1151 * @ctxt: the XPath Parser context
1152 * @prefix: the place to store the prefix
1154 * Parse a qualified name
1156 * Returns the Name parsed or NULL
1160 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1161 xmlChar *ret = NULL;
1164 ret = xsltScanNCName(ctxt);
1168 ret = xsltScanNCName(ctxt);
1174 * xsltCompileIdKeyPattern:
1175 * @ctxt: the compilation context
1176 * @name: a preparsed name
1177 * @aid: whether id/key are allowed there
1179 * Compile the XSLT LocationIdKeyPattern
1180 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1181 * | 'key' '(' Literal ',' Literal ')'
1183 * also handle NodeType and PI from:
1185 * [7] NodeTest ::= NameTest
1186 * | NodeType '(' ')'
1187 * | 'processing-instruction' '(' Literal ')'
1190 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1191 xmlChar *lit = NULL;
1192 xmlChar *lit2 = NULL;
1195 xsltTransformError(NULL, NULL, NULL,
1196 "xsltCompileIdKeyPattern : ( expected\n");
1200 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1203 lit = xsltScanLiteral(ctxt);
1208 xsltTransformError(NULL, NULL, NULL,
1209 "xsltCompileIdKeyPattern : ) expected\n");
1214 PUSH(XSLT_OP_ID, lit, NULL);
1215 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1218 lit = xsltScanLiteral(ctxt);
1223 xsltTransformError(NULL, NULL, NULL,
1224 "xsltCompileIdKeyPattern : , expected\n");
1230 lit2 = xsltScanLiteral(ctxt);
1235 xsltTransformError(NULL, NULL, NULL,
1236 "xsltCompileIdKeyPattern : ) expected\n");
1241 /* TODO: support namespace in keys */
1242 PUSH(XSLT_OP_KEY, lit, lit2);
1243 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1247 lit = xsltScanLiteral(ctxt);
1252 xsltTransformError(NULL, NULL, NULL,
1253 "xsltCompileIdKeyPattern : ) expected\n");
1259 PUSH(XSLT_OP_PI, lit, NULL);
1260 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1264 xsltTransformError(NULL, NULL, NULL,
1265 "xsltCompileIdKeyPattern : ) expected\n");
1270 PUSH(XSLT_OP_TEXT, NULL, NULL);
1271 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1275 xsltTransformError(NULL, NULL, NULL,
1276 "xsltCompileIdKeyPattern : ) expected\n");
1281 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1282 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1286 xsltTransformError(NULL, NULL, NULL,
1287 "xsltCompileIdKeyPattern : ) expected\n");
1292 PUSH(XSLT_OP_NODE, NULL, NULL);
1294 xsltTransformError(NULL, NULL, NULL,
1295 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1299 xsltTransformError(NULL, NULL, NULL,
1300 "xsltCompileIdKeyPattern : node type\n");
1310 * xsltCompileStepPattern:
1311 * @ctxt: the compilation context
1312 * @token: a posible precompiled name
1314 * Compile the XSLT StepPattern and generates a precompiled
1315 * form suitable for fast matching.
1317 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1318 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1319 * | ('child' | 'attribute') '::'
1321 * [7] NodeTest ::= NameTest
1322 * | NodeType '(' ')'
1323 * | 'processing-instruction' '(' Literal ')'
1324 * [8] Predicate ::= '[' PredicateExpr ']'
1325 * [9] PredicateExpr ::= Expr
1326 * [13] AbbreviatedAxisSpecifier ::= '@'?
1327 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1331 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1332 xmlChar *name = NULL;
1333 const xmlChar *URI = NULL;
1334 xmlChar *URL = NULL;
1338 if ((token == NULL) && (CUR == '@')) {
1339 xmlChar *prefix = NULL;
1344 PUSH(XSLT_OP_ATTR, NULL, NULL);
1347 token = xsltScanQName(ctxt, &prefix);
1348 if (prefix != NULL) {
1351 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1353 xsltTransformError(NULL, NULL, NULL,
1354 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1357 URL = xmlStrdup(ns->href);
1361 if (token == NULL) {
1364 PUSH(XSLT_OP_ATTR, NULL, URL);
1367 xsltTransformError(NULL, NULL, NULL,
1368 "xsltCompileStepPattern : Name expected\n");
1372 PUSH(XSLT_OP_ATTR, token, URL);
1373 goto parse_predicate;
1376 token = xsltScanName(ctxt);
1377 if (token == NULL) {
1380 PUSH(XSLT_OP_ALL, token, NULL);
1381 goto parse_predicate;
1383 xsltTransformError(NULL, NULL, NULL,
1384 "xsltCompileStepPattern : Name expected\n");
1393 xsltCompileIdKeyPattern(ctxt, token, 0);
1396 } else if (CUR == ':') {
1399 xmlChar *prefix = token;
1403 * This is a namespace match
1405 token = xsltScanName(ctxt);
1406 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1408 xsltTransformError(NULL, NULL, NULL,
1409 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1414 URL = xmlStrdup(ns->href);
1417 if (token == NULL) {
1420 PUSH(XSLT_OP_NS, URL, NULL);
1422 xsltTransformError(NULL, NULL, NULL,
1423 "xsltCompileStepPattern : Name expected\n");
1428 PUSH(XSLT_OP_ELEM, token, URL);
1432 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1434 token = xsltScanName(ctxt);
1435 if (token == NULL) {
1438 PUSH(XSLT_OP_ALL, token, NULL);
1439 goto parse_predicate;
1441 xsltTransformError(NULL, NULL, NULL,
1442 "xsltCompileStepPattern : QName expected\n");
1447 URI = xsltGetQNameURI(ctxt->elem, &token);
1448 if (token == NULL) {
1452 name = xmlStrdup(token);
1454 URL = xmlStrdup(URI);
1456 PUSH(XSLT_OP_CHILD, name, URL);
1457 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1459 token = xsltScanName(ctxt);
1460 if (token == NULL) {
1461 xsltTransformError(NULL, NULL, NULL,
1462 "xsltCompileStepPattern : QName expected\n");
1466 URI = xsltGetQNameURI(ctxt->elem, &token);
1467 if (token == NULL) {
1471 name = xmlStrdup(token);
1473 URL = xmlStrdup(URI);
1475 PUSH(XSLT_OP_ATTR, name, URL);
1477 xsltTransformError(NULL, NULL, NULL,
1478 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1484 } else if (CUR == '*') {
1486 PUSH(XSLT_OP_ALL, token, NULL);
1488 URI = xsltGetQNameURI(ctxt->elem, &token);
1489 if (token == NULL) {
1494 URL = xmlStrdup(URI);
1495 PUSH(XSLT_OP_ELEM, token, URL);
1500 while (CUR == '[') {
1502 xmlChar *ret = NULL;
1508 /* Skip over nested predicates */
1511 else if (CUR == ']') {
1515 } else if (CUR == '"') {
1517 while ((CUR != 0) && (CUR != '"'))
1519 } else if (CUR == '\'') {
1521 while ((CUR != 0) && (CUR != '\''))
1527 xsltTransformError(NULL, NULL, NULL,
1528 "xsltCompileStepPattern : ']' expected\n");
1532 ret = xmlStrndup(q, CUR_PTR - q);
1533 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1534 /* push the predicate lower than local test */
1548 * xsltCompileRelativePathPattern:
1549 * @comp: the compilation context
1550 * @token: a posible precompiled name
1552 * Compile the XSLT RelativePathPattern and generates a precompiled
1553 * form suitable for fast matching.
1555 * [4] RelativePathPattern ::= StepPattern
1556 * | RelativePathPattern '/' StepPattern
1557 * | RelativePathPattern '//' StepPattern
1560 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1561 xsltCompileStepPattern(ctxt, token);
1565 while ((CUR != 0) && (CUR != '|')) {
1566 if ((CUR == '/') && (NXT(1) == '/')) {
1567 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1571 xsltCompileStepPattern(ctxt, NULL);
1572 } else if (CUR == '/') {
1573 PUSH(XSLT_OP_PARENT, NULL, NULL);
1576 if ((CUR != 0) || (CUR == '|')) {
1577 xsltCompileRelativePathPattern(ctxt, NULL);
1591 * xsltCompileLocationPathPattern:
1592 * @ctxt: the compilation context
1594 * Compile the XSLT LocationPathPattern and generates a precompiled
1595 * form suitable for fast matching.
1597 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1598 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1599 * | '//'? RelativePathPattern
1602 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1604 if ((CUR == '/') && (NXT(1) == '/')) {
1606 * since we reverse the query
1607 * a leading // can be safely ignored
1611 xsltCompileRelativePathPattern(ctxt, NULL);
1612 } else if (CUR == '/') {
1614 * We need to find root as the parent
1618 PUSH(XSLT_OP_ROOT, NULL, NULL);
1619 if ((CUR != 0) || (CUR == '|')) {
1620 PUSH(XSLT_OP_PARENT, NULL, NULL);
1621 xsltCompileRelativePathPattern(ctxt, NULL);
1623 } else if (CUR == '*') {
1624 xsltCompileRelativePathPattern(ctxt, NULL);
1625 } else if (CUR == '@') {
1626 xsltCompileRelativePathPattern(ctxt, NULL);
1629 name = xsltScanName(ctxt);
1631 xsltTransformError(NULL, NULL, NULL,
1632 "xsltCompileLocationPathPattern : Name expected\n");
1637 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1638 xsltCompileIdKeyPattern(ctxt, name, 1);
1639 if ((CUR == '/') && (NXT(1) == '/')) {
1640 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1644 xsltCompileRelativePathPattern(ctxt, NULL);
1645 } else if (CUR == '/') {
1646 PUSH(XSLT_OP_PARENT, NULL, NULL);
1649 xsltCompileRelativePathPattern(ctxt, NULL);
1653 xsltCompileRelativePathPattern(ctxt, name);
1660 * xsltCompilePattern:
1661 * @pattern: an XSLT pattern
1662 * @doc: the containing document
1663 * @node: the containing element
1664 * @style: the stylesheet
1665 * @runtime: the transformation context, if done at run-time
1667 * Compile the XSLT pattern and generates a list of precompiled form suitable
1668 * for fast matching.
1670 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1672 * Returns the generated pattern list or NULL in case of failure
1676 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1677 xmlNodePtr node, xsltStylesheetPtr style,
1678 xsltTransformContextPtr runtime) {
1679 xsltParserContextPtr ctxt = NULL;
1680 xsltCompMatchPtr element, first = NULL, previous = NULL;
1681 int current, start, end, level, j;
1683 if (pattern == NULL) {
1684 xsltTransformError(NULL, NULL, node,
1685 "xsltCompilePattern : NULL pattern\n");
1689 ctxt = xsltNewParserContext(style, runtime);
1695 while (pattern[current] != 0) {
1697 while (IS_BLANK(pattern[current]))
1701 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1702 if (pattern[end] == '[')
1704 else if (pattern[end] == ']')
1706 else if (pattern[end] == '\'') {
1708 while ((pattern[end] != 0) && (pattern[end] != '\''))
1710 } else if (pattern[end] == '"') {
1712 while ((pattern[end] != 0) && (pattern[end] != '"'))
1717 if (current == end) {
1718 xsltTransformError(NULL, NULL, node,
1719 "xsltCompilePattern : NULL pattern\n");
1722 element = xsltNewCompMatch();
1723 if (element == NULL) {
1728 else if (previous != NULL)
1729 previous->next = element;
1732 ctxt->comp = element;
1733 ctxt->base = xmlStrndup(&pattern[start], end - start);
1734 if (ctxt->base == NULL)
1736 ctxt->cur = &(ctxt->base)[current - start];
1737 element->pattern = ctxt->base;
1738 element->nsList = xmlGetNsList(doc, node);
1740 if (element->nsList != NULL) {
1741 while (element->nsList[j] != NULL)
1747 #ifdef WITH_XSLT_DEBUG_PATTERN
1748 xsltGenericDebug(xsltGenericDebugContext,
1749 "xsltCompilePattern : parsing '%s'\n",
1752 xsltCompileLocationPathPattern(ctxt);
1754 xsltTransformError(NULL, style, node,
1755 "xsltCompilePattern : failed to compile '%s'\n",
1762 * Reverse for faster interpretation.
1764 xsltReverseCompMatch(element);
1767 * Set-up the priority
1769 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1770 (element->steps[0].op == XSLT_OP_ATTR)) &&
1771 (element->steps[0].value != NULL) &&
1772 (element->steps[1].op == XSLT_OP_END)) {
1773 element->priority = 0;
1775 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1776 (element->steps[1].op == XSLT_OP_END)) {
1777 element->priority = 0;
1779 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1780 (element->steps[0].value != NULL) &&
1781 (element->steps[1].op == XSLT_OP_END)) {
1782 element->priority = 0;
1783 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1784 (element->steps[0].value2 != NULL) &&
1785 (element->steps[1].op == XSLT_OP_END)) {
1786 element->priority = -0.25;
1787 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1788 (element->steps[0].value != NULL) &&
1789 (element->steps[1].op == XSLT_OP_END)) {
1790 element->priority = -0.25;
1791 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1792 (element->steps[0].value == NULL) &&
1793 (element->steps[0].value2 == NULL) &&
1794 (element->steps[1].op == XSLT_OP_END)) {
1795 element->priority = -0.5;
1796 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1797 (element->steps[0].op == XSLT_OP_TEXT) ||
1798 (element->steps[0].op == XSLT_OP_ALL) ||
1799 (element->steps[0].op == XSLT_OP_NODE) ||
1800 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1801 (element->steps[1].op == XSLT_OP_END)) {
1802 element->priority = -0.5;
1804 element->priority = 0.5;
1806 #ifdef WITH_XSLT_DEBUG_PATTERN
1807 xsltGenericDebug(xsltGenericDebugContext,
1808 "xsltCompilePattern : parsed %s, default priority %f\n",
1809 element->pattern, element->priority);
1811 if (pattern[end] == '|')
1816 xsltTransformError(NULL, style, node,
1817 "xsltCompilePattern : NULL pattern\n");
1822 xsltFreeParserContext(ctxt);
1827 xsltFreeParserContext(ctxt);
1829 xsltFreeCompMatchList(first);
1833 /************************************************************************
1835 * Module interfaces *
1837 ************************************************************************/
1841 * @style: an XSLT stylesheet
1842 * @cur: an XSLT template
1843 * @mode: the mode name or NULL
1844 * @modeURI: the mode URI or NULL
1846 * Register the XSLT pattern associated to @cur
1848 * Returns -1 in case of error, 0 otherwise
1851 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1852 const xmlChar *mode, const xmlChar *modeURI) {
1853 xsltCompMatchPtr pat, list, *top = NULL, next;
1854 const xmlChar *name = NULL;
1855 float priority; /* the priority */
1857 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1860 priority = cur->priority;
1861 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1867 pat->template = cur;
1869 pat->mode = xmlStrdup(mode);
1870 if (modeURI != NULL)
1871 pat->modeURI = xmlStrdup(modeURI);
1872 if (priority != XSLT_PAT_NO_PRIORITY)
1873 pat->priority = priority;
1876 * insert it in the hash table list corresponding to its lookup name
1878 switch (pat->steps[0].op) {
1880 if (pat->steps[0].value != NULL)
1881 name = pat->steps[0].value;
1883 top = (xsltCompMatchPtr *) &(style->attrMatch);
1886 case XSLT_OP_PARENT:
1887 case XSLT_OP_ANCESTOR:
1888 top = (xsltCompMatchPtr *) &(style->elemMatch);
1891 top = (xsltCompMatchPtr *) &(style->rootMatch);
1894 top = (xsltCompMatchPtr *) &(style->keyMatch);
1897 /* TODO optimize ID !!! */
1900 top = (xsltCompMatchPtr *) &(style->elemMatch);
1903 case XSLT_OP_PREDICATE:
1904 xsltTransformError(NULL, style, NULL,
1905 "xsltAddTemplate: invalid compiled pattern\n");
1906 xsltFreeCompMatch(pat);
1909 * TODO: some flags at the top level about type based patterns
1910 * would be faster than inclusion in the hash table.
1913 if (pat->steps[0].value != NULL)
1914 name = pat->steps[0].value;
1916 top = (xsltCompMatchPtr *) &(style->piMatch);
1918 case XSLT_OP_COMMENT:
1919 top = (xsltCompMatchPtr *) &(style->commentMatch);
1922 top = (xsltCompMatchPtr *) &(style->textMatch);
1926 if (pat->steps[0].value != NULL)
1927 name = pat->steps[0].value;
1929 top = (xsltCompMatchPtr *) &(style->elemMatch);
1933 if (style->templatesHash == NULL) {
1934 style->templatesHash = xmlHashCreate(1024);
1935 if (style->templatesHash == NULL) {
1936 xsltFreeCompMatch(pat);
1939 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1941 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1942 name, mode, modeURI);
1944 xmlHashAddEntry3(style->templatesHash, name,
1945 mode, modeURI, pat);
1948 * Note '<=' since one must choose among the matching
1949 * template rules that are left, the one that occurs
1950 * last in the stylesheet
1952 if (list->priority <= pat->priority) {
1954 xmlHashUpdateEntry3(style->templatesHash, name,
1955 mode, modeURI, pat, NULL);
1957 while (list->next != NULL) {
1958 if (list->next->priority <= pat->priority)
1962 pat->next = list->next;
1967 } else if (top != NULL) {
1972 } else if (list->priority <= pat->priority) {
1976 while (list->next != NULL) {
1977 if (list->next->priority <= pat->priority)
1981 pat->next = list->next;
1985 xsltTransformError(NULL, style, NULL,
1986 "xsltAddTemplate: invalid compiled pattern\n");
1987 xsltFreeCompMatch(pat);
1990 #ifdef WITH_XSLT_DEBUG_PATTERN
1992 xsltGenericDebug(xsltGenericDebugContext,
1993 "added pattern : '%s' mode '%s' priority %f\n",
1994 pat->pattern, pat->mode, pat->priority);
1996 xsltGenericDebug(xsltGenericDebugContext,
1997 "added pattern : '%s' priority %f\n",
1998 pat->pattern, pat->priority);
2008 * @ctxt: a XSLT process context
2009 * @node: the node being processed
2010 * @style: the current style
2012 * Finds the template applying to this node, if @style is non-NULL
2013 * it means one needs to look for the next imported template in scope.
2015 * Returns the xsltTemplatePtr or NULL if not found
2018 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2019 xsltStylesheetPtr style) {
2020 xsltStylesheetPtr curstyle;
2021 xsltTemplatePtr ret = NULL;
2022 const xmlChar *name = NULL;
2023 xsltCompMatchPtr list = NULL;
2026 if ((ctxt == NULL) || (node == NULL))
2029 if (style == NULL) {
2030 curstyle = ctxt->style;
2032 curstyle = xsltNextImport(style);
2035 while ((curstyle != NULL) && (curstyle != style)) {
2036 priority = XSLT_PAT_NO_PRIORITY;
2037 /* TODO : handle IDs/keys here ! */
2038 if (curstyle->templatesHash != NULL) {
2040 * Use the top name as selector
2042 switch (node->type) {
2043 case XML_ELEMENT_NODE:
2044 case XML_ATTRIBUTE_NODE:
2048 case XML_DOCUMENT_NODE:
2049 case XML_HTML_DOCUMENT_NODE:
2051 case XML_CDATA_SECTION_NODE:
2052 case XML_COMMENT_NODE:
2053 case XML_ENTITY_REF_NODE:
2054 case XML_ENTITY_NODE:
2055 case XML_DOCUMENT_TYPE_NODE:
2056 case XML_DOCUMENT_FRAG_NODE:
2057 case XML_NOTATION_NODE:
2059 case XML_ELEMENT_DECL:
2060 case XML_ATTRIBUTE_DECL:
2061 case XML_ENTITY_DECL:
2062 case XML_NAMESPACE_DECL:
2063 case XML_XINCLUDE_START:
2064 case XML_XINCLUDE_END:
2073 * find the list of appliable expressions based on the name
2075 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2076 name, ctxt->mode, ctxt->modeURI);
2079 while (list != NULL) {
2080 if (xsltTestCompMatch(ctxt, list, node,
2081 ctxt->mode, ctxt->modeURI)) {
2082 ret = list->template;
2083 priority = list->priority;
2091 * find alternate generic matches
2093 switch (node->type) {
2094 case XML_ELEMENT_NODE:
2095 list = curstyle->elemMatch;
2097 case XML_ATTRIBUTE_NODE:
2098 list = curstyle->attrMatch;
2101 list = curstyle->piMatch;
2103 case XML_DOCUMENT_NODE:
2104 case XML_HTML_DOCUMENT_NODE:
2105 list = curstyle->rootMatch;
2108 case XML_CDATA_SECTION_NODE:
2109 list = curstyle->textMatch;
2111 case XML_COMMENT_NODE:
2112 list = curstyle->commentMatch;
2114 case XML_ENTITY_REF_NODE:
2115 case XML_ENTITY_NODE:
2116 case XML_DOCUMENT_TYPE_NODE:
2117 case XML_DOCUMENT_FRAG_NODE:
2118 case XML_NOTATION_NODE:
2120 case XML_ELEMENT_DECL:
2121 case XML_ATTRIBUTE_DECL:
2122 case XML_ENTITY_DECL:
2123 case XML_NAMESPACE_DECL:
2124 case XML_XINCLUDE_START:
2125 case XML_XINCLUDE_END:
2131 while ((list != NULL) &&
2132 ((ret == NULL) || (list->priority > priority))) {
2133 if (xsltTestCompMatch(ctxt, list, node,
2134 ctxt->mode, ctxt->modeURI)) {
2135 ret = list->template;
2136 priority = list->priority;
2142 * Some of the tests for elements can also apply to documents
2144 if ((node->type == XML_DOCUMENT_NODE) ||
2145 (node->type == XML_HTML_DOCUMENT_NODE) ||
2146 (node->type == XML_TEXT_NODE)) {
2147 list = curstyle->elemMatch;
2148 while ((list != NULL) &&
2149 ((ret == NULL) || (list->priority > priority))) {
2150 if (xsltTestCompMatch(ctxt, list, node,
2151 ctxt->mode, ctxt->modeURI)) {
2152 ret = list->template;
2153 priority = list->priority;
2160 if (node->_private != NULL) {
2161 list = curstyle->keyMatch;
2162 while ((list != NULL) &&
2163 ((ret == NULL) || (list->priority > priority))) {
2164 if (xsltTestCompMatch(ctxt, list, node,
2165 ctxt->mode, ctxt->modeURI)) {
2166 ret = list->template;
2167 priority = list->priority;
2177 * Cycle on next curstylesheet import.
2179 curstyle = xsltNextImport(curstyle);
2185 * xsltCleanupTemplates:
2186 * @style: an XSLT stylesheet
2188 * Cleanup the state of the templates used by the stylesheet and
2189 * the ones it imports.
2192 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2196 * xsltFreeTemplateHashes:
2197 * @style: an XSLT stylesheet
2199 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2202 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2203 if (style->templatesHash != NULL)
2204 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2205 (xmlHashDeallocator) xsltFreeCompMatchList);
2206 if (style->rootMatch != NULL)
2207 xsltFreeCompMatchList(style->rootMatch);
2208 if (style->keyMatch != NULL)
2209 xsltFreeCompMatchList(style->keyMatch);
2210 if (style->elemMatch != NULL)
2211 xsltFreeCompMatchList(style->elemMatch);
2212 if (style->attrMatch != NULL)
2213 xsltFreeCompMatchList(style->attrMatch);
2214 if (style->parentMatch != NULL)
2215 xsltFreeCompMatchList(style->parentMatch);
2216 if (style->textMatch != NULL)
2217 xsltFreeCompMatchList(style->textMatch);
2218 if (style->piMatch != NULL)
2219 xsltFreeCompMatchList(style->piMatch);
2220 if (style->commentMatch != NULL)
2221 xsltFreeCompMatchList(style->commentMatch);
2227 * @node: a node in the source tree
2228 * @pattern: an XSLT pattern
2229 * @ctxtdoc: context document (for namespaces)
2230 * @ctxtnode: context node (for namespaces)
2232 * Determine if a node matches a pattern.
2235 xsltMatchPattern(xsltTransformContextPtr context,
2237 const xmlChar *pattern,
2239 xmlNodePtr ctxtnode)
2242 xsltCompMatchPtr first, comp;
2244 if ((context != NULL) && (pattern != NULL)) {
2245 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2246 for (comp = first; comp != NULL; comp = comp->next) {
2247 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2252 xsltFreeCompMatchList(first);