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))
403 if ((node->type == XML_ELEMENT_NODE) && (node->name[0] == ' '))
407 if (node->type != XML_ELEMENT_NODE)
409 if (step->value == NULL)
411 if (step->value[0] != node->name[0])
413 if (!xmlStrEqual(step->value, node->name))
417 if (node->ns == NULL) {
418 if (step->value2 != NULL)
420 } else if (node->ns->href != NULL) {
421 if (step->value2 == NULL)
423 if (!xmlStrEqual(step->value2, node->ns->href))
427 case XSLT_OP_CHILD: {
430 if ((node->type != XML_ELEMENT_NODE) &&
431 (node->type != XML_DOCUMENT_NODE) &&
432 #ifdef LIBXML_DOCB_ENABLED
433 (node->type != XML_DOCB_DOCUMENT_NODE) &&
435 (node->type != XML_HTML_DOCUMENT_NODE))
438 lst = node->children;
440 if (step->value != NULL) {
441 while (lst != NULL) {
442 if ((lst->type == XML_ELEMENT_NODE) &&
443 (step->value[0] == lst->name[0]) &&
444 (xmlStrEqual(step->value, lst->name)))
454 if (node->type != XML_ATTRIBUTE_NODE)
456 if (step->value != NULL) {
457 if (step->value[0] != node->name[0])
459 if (!xmlStrEqual(step->value, node->name))
463 if (node->ns == NULL) {
464 if (step->value2 != NULL)
466 } else if (step->value2 != NULL) {
467 if (!xmlStrEqual(step->value2, node->ns->href))
472 if ((node->type == XML_DOCUMENT_NODE) ||
473 (node->type == XML_HTML_DOCUMENT_NODE) ||
474 #ifdef LIBXML_DOCB_ENABLED
475 (node->type == XML_DOCB_DOCUMENT_NODE) ||
477 (node->type == XML_NAMESPACE_DECL))
482 if (step->value == NULL)
484 if (step->value[0] != node->name[0])
486 if (!xmlStrEqual(step->value, node->name))
489 if (node->ns == NULL) {
490 if (step->value2 != NULL)
492 } else if (node->ns->href != NULL) {
493 if (step->value2 == NULL)
495 if (!xmlStrEqual(step->value2, node->ns->href))
499 case XSLT_OP_ANCESTOR:
500 /* TODO: implement coalescing of ANCESTOR/NODE ops */
501 if (step->value == NULL) {
503 step = &comp->steps[i];
504 if (step->op == XSLT_OP_ROOT)
506 if (step->op != XSLT_OP_ELEM)
508 if (step->value == NULL)
513 if ((node->type == XML_DOCUMENT_NODE) ||
514 (node->type == XML_HTML_DOCUMENT_NODE) ||
515 #ifdef LIBXML_DOCB_ENABLED
516 (node->type == XML_DOCB_DOCUMENT_NODE) ||
518 (node->type == XML_NAMESPACE_DECL))
521 while (node != NULL) {
524 if ((node->type == XML_ELEMENT_NODE) &&
525 (step->value[0] == node->name[0]) &&
526 (xmlStrEqual(step->value, node->name))) {
528 if (node->ns == NULL) {
529 if (step->value2 == NULL)
531 } else if (node->ns->href != NULL) {
532 if ((step->value2 != NULL) &&
533 (xmlStrEqual(step->value2, node->ns->href)))
543 /* TODO Handle IDs decently, must be done differently */
546 if (node->type != XML_ELEMENT_NODE)
549 id = xmlGetID(node->doc, step->value);
550 if ((id == NULL) || (id->parent != node))
558 list = xsltGetKey(ctxt, step->value,
559 step->value3, step->value2);
562 for (indx = 0;indx < list->nodeNr;indx++)
563 if (list->nodeTab[indx] == node)
565 if (indx >= list->nodeNr)
570 if (node->type != XML_ELEMENT_NODE)
572 if (node->ns == NULL) {
573 if (step->value != NULL)
575 } else if (node->ns->href != NULL) {
576 if (step->value == NULL)
578 if (!xmlStrEqual(step->value, node->ns->href))
583 if (node->type != XML_ELEMENT_NODE)
586 case XSLT_OP_PREDICATE: {
589 int pos = 0, len = 0;
591 * The simple existing predicate code cannot handle
592 * properly cascaded predicates. If in this situation
593 * compute directly the full node list once and check
594 * if the node is in the result list.
596 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
597 xmlDocPtr prevdoc, doc;
598 xmlXPathObjectPtr list;
602 prevdoc = (xmlDocPtr)
603 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
605 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
606 list = (xmlXPathObjectPtr)
607 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra);
610 if ((list == NULL) || (prevdoc != doc)) {
612 xmlXPathObjectPtr newlist;
613 xmlNodePtr parent = node->parent;
617 if (comp->pattern[0] == '/')
618 query = xmlStrdup(comp->pattern);
620 query = xmlStrdup((const xmlChar *)"//");
621 query = xmlStrcat(query, comp->pattern);
623 oldnode = ctxt->xpathCtxt->node;
624 olddoc = ctxt->xpathCtxt->doc;
625 ctxt->xpathCtxt->node = node;
626 ctxt->xpathCtxt->doc = doc;
627 newlist = xmlXPathEval(query, ctxt->xpathCtxt);
628 ctxt->xpathCtxt->node = oldnode;
629 ctxt->xpathCtxt->doc = olddoc;
633 if (newlist->type != XPATH_NODESET) {
634 xmlXPathFreeObject(newlist);
638 if ((parent == NULL) || (node->doc == NULL))
641 if ((doc->name != NULL) &&
642 (doc->name[0] == ' ') &&
643 (xmlStrEqual(BAD_CAST doc->name,
644 BAD_CAST " fake node libxslt")))
649 xmlXPathFreeObject(list);
652 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra) =
654 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
656 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
657 (xmlFreeFunc) xmlXPathFreeObject;
661 if ((list->nodesetval == NULL) ||
662 (list->nodesetval->nodeNr <= 0)) {
664 xmlXPathFreeObject(list);
667 /* TODO: store the index and use it for the scan */
669 for (j = 0;j < list->nodesetval->nodeNr;j++) {
670 if (list->nodesetval->nodeTab[j] == node) {
672 xmlXPathFreeObject(list);
679 xmlXPathFreeObject(list);
683 * Depending on the last selection, one may need to
684 * recompute contextSize and proximityPosition.
686 * TODO: make this thread safe !
688 oldCS = ctxt->xpathCtxt->contextSize;
689 oldCP = ctxt->xpathCtxt->proximityPosition;
690 if ((select != NULL) &&
691 (select->op == XSLT_OP_ELEM) &&
692 (select->value != NULL) &&
693 (node->type == XML_ELEMENT_NODE) &&
694 (node->parent != NULL)) {
696 int index, nocache = 0;
698 previous = (xmlNodePtr)
699 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
701 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
702 if ((previous != NULL) &&
703 (previous->parent == node->parent)) {
705 * just walk back to adjust the index
708 xmlNodePtr sibling = node;
710 while (sibling != NULL) {
711 if (sibling == previous)
713 if ((node->type == XML_ELEMENT_NODE) &&
714 (node->name != NULL) &&
715 (sibling->name != NULL) &&
716 (node->name[0] == sibling->name[0]) &&
717 (xmlStrEqual(node->name, sibling->name))) {
718 if ((select->value2 == NULL) ||
719 ((sibling->ns != NULL) &&
720 (xmlStrEqual(select->value2,
721 sibling->ns->href))))
724 sibling = sibling->prev;
726 if (sibling == NULL) {
727 /* hum going backward in document order ... */
730 while (sibling != NULL) {
731 if (sibling == previous)
733 if ((select->value2 == NULL) ||
734 ((sibling->ns != NULL) &&
735 (xmlStrEqual(select->value2,
736 sibling->ns->href))))
738 sibling = sibling->next;
741 if (sibling != NULL) {
744 * If the node is in a Value Tree we cannot
747 if (node->doc != NULL) {
749 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
750 XSLT_RUNTIME_EXTRA(ctxt,
751 select->previousExtra) = node;
752 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
760 * recompute the index
762 xmlNodePtr siblings = node->parent->children;
763 xmlNodePtr parent = node->parent;
765 while (siblings != NULL) {
766 if (siblings->type == XML_ELEMENT_NODE) {
767 if (siblings == node) {
770 } else if ((node->name != NULL) &&
771 (siblings->name != NULL) &&
772 (node->name[0] == siblings->name[0]) &&
773 (xmlStrEqual(node->name, siblings->name))) {
774 if ((select->value2 == NULL) ||
775 ((siblings->ns != NULL) &&
776 (xmlStrEqual(select->value2,
777 siblings->ns->href))))
781 siblings = siblings->next;
783 if ((parent == NULL) || (node->doc == NULL))
786 while (parent->parent != NULL)
787 parent = parent->parent;
788 if (((parent->type != XML_DOCUMENT_NODE) &&
789 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
790 (parent != (xmlNodePtr) node->doc))
795 ctxt->xpathCtxt->contextSize = len;
796 ctxt->xpathCtxt->proximityPosition = pos;
798 * If the node is in a Value Tree we cannot
801 if ((node->doc != NULL) && (nocache == 0)) {
802 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
804 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
806 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
810 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
811 (node->type == XML_ELEMENT_NODE)) {
813 int index, nocache = 0;
815 previous = (xmlNodePtr)
816 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
818 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
819 if ((previous != NULL) &&
820 (previous->parent == node->parent)) {
822 * just walk back to adjust the index
825 xmlNodePtr sibling = node;
827 while (sibling != NULL) {
828 if (sibling == previous)
830 if (sibling->type == XML_ELEMENT_NODE)
832 sibling = sibling->prev;
834 if (sibling == NULL) {
835 /* hum going backward in document order ... */
838 while (sibling != NULL) {
839 if (sibling == previous)
841 if (sibling->type == XML_ELEMENT_NODE)
843 sibling = sibling->next;
846 if (sibling != NULL) {
849 * If the node is in a Value Tree we cannot
852 if (node->doc != NULL) {
854 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
855 XSLT_RUNTIME_EXTRA(ctxt,
856 select->previousExtra) = node;
857 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
864 * recompute the index
866 xmlNodePtr siblings = node->parent->children;
867 xmlNodePtr parent = node->parent;
869 while (siblings != NULL) {
870 if (siblings->type == XML_ELEMENT_NODE) {
872 if (siblings == node) {
876 siblings = siblings->next;
878 if ((parent == NULL) || (node->doc == NULL))
881 while (parent->parent != NULL)
882 parent = parent->parent;
883 if (((parent->type != XML_DOCUMENT_NODE) &&
884 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
885 (parent != (xmlNodePtr) node->doc))
890 ctxt->xpathCtxt->contextSize = len;
891 ctxt->xpathCtxt->proximityPosition = pos;
893 * If the node is in a Value Tree we cannot
896 if ((node->doc != NULL) && (nocache == 0)) {
897 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
899 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
901 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
906 oldNode = ctxt->node;
909 if (step->value == NULL)
911 if (step->comp == NULL)
914 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
919 ctxt->xpathCtxt->contextSize = oldCS;
920 ctxt->xpathCtxt->proximityPosition = oldCP;
922 ctxt->node = oldNode;
926 ctxt->xpathCtxt->contextSize = oldCS;
927 ctxt->xpathCtxt->proximityPosition = oldCP;
929 ctxt->node = oldNode;
933 if (node->type != XML_PI_NODE)
935 if (step->value != NULL) {
936 if (!xmlStrEqual(step->value, node->name))
940 case XSLT_OP_COMMENT:
941 if (node->type != XML_COMMENT_NODE)
945 if ((node->type != XML_TEXT_NODE) &&
946 (node->type != XML_CDATA_SECTION_NODE))
950 switch (node->type) {
951 case XML_ELEMENT_NODE:
952 case XML_CDATA_SECTION_NODE:
954 case XML_COMMENT_NODE:
967 * xsltTestCompMatchList:
968 * @ctxt: a XSLT process context
970 * @comp: the precompiled pattern list
972 * Test wether the node matches one of the patterns in the list
974 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
977 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
978 xsltCompMatchPtr comp) {
981 if ((ctxt == NULL) || (node == NULL))
983 while (comp != NULL) {
984 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
992 /************************************************************************
994 * Dedicated parser for templates *
996 ************************************************************************/
998 #define CUR (*ctxt->cur)
999 #define SKIP(val) ctxt->cur += (val)
1000 #define NXT(val) ctxt->cur[(val)]
1001 #define CUR_PTR ctxt->cur
1003 #define SKIP_BLANKS \
1004 while (IS_BLANK(CUR)) NEXT
1006 #define CURRENT (*ctxt->cur)
1007 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1010 #define PUSH(op, val, val2) \
1011 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1014 xsltSwapTopCompMatch(ctxt->comp);
1016 #define XSLT_ERROR(X) \
1017 { xsltError(ctxt, __FILE__, __LINE__, X); \
1018 ctxt->error = (X); return; }
1020 #define XSLT_ERROR0(X) \
1021 { xsltError(ctxt, __FILE__, __LINE__, X); \
1022 ctxt->error = (X); return(0); }
1026 * @ctxt: the XPath Parser context
1028 * Parse an XPath Litteral:
1030 * [29] Literal ::= '"' [^"]* '"'
1033 * Returns the Literal parsed or NULL
1037 xsltScanLiteral(xsltParserContextPtr ctxt) {
1038 const xmlChar *q, *cur;
1039 xmlChar *ret = NULL;
1046 val = xmlStringCurrentChar(NULL, cur, &len);
1047 while ((IS_CHAR(val)) && (val != '"')) {
1049 val = xmlStringCurrentChar(NULL, cur, &len);
1051 if (!IS_CHAR(val)) {
1055 ret = xmlStrndup(q, cur - q);
1059 } else if (CUR == '\'') {
1062 val = xmlStringCurrentChar(NULL, cur, &len);
1063 while ((IS_CHAR(val)) && (val != '\'')) {
1065 val = xmlStringCurrentChar(NULL, cur, &len);
1067 if (!IS_CHAR(val)) {
1071 ret = xmlStrndup(q, cur - q);
1076 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1085 * @ctxt: the XPath Parser context
1087 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1088 * CombiningChar | Extender
1090 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1092 * [6] Names ::= Name (S Name)*
1094 * Returns the Name parsed or NULL
1098 xsltScanName(xsltParserContextPtr ctxt) {
1099 const xmlChar *q, *cur;
1100 xmlChar *ret = NULL;
1106 val = xmlStringCurrentChar(NULL, cur, &len);
1107 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1110 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1111 (val == '.') || (val == '-') ||
1113 (IS_COMBINING(val)) ||
1114 (IS_EXTENDER(val))) {
1116 val = xmlStringCurrentChar(NULL, cur, &len);
1118 ret = xmlStrndup(q, cur - q);
1125 * @ctxt: the XPath Parser context
1127 * Parses a non qualified name
1129 * Returns the Name parsed or NULL
1133 xsltScanNCName(xsltParserContextPtr ctxt) {
1134 const xmlChar *q, *cur;
1135 xmlChar *ret = NULL;
1141 val = xmlStringCurrentChar(NULL, cur, &len);
1142 if (!IS_LETTER(val) && (val != '_'))
1145 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1146 (val == '.') || (val == '-') ||
1148 (IS_COMBINING(val)) ||
1149 (IS_EXTENDER(val))) {
1151 val = xmlStringCurrentChar(NULL, cur, &len);
1153 ret = xmlStrndup(q, cur - q);
1160 * @ctxt: the XPath Parser context
1161 * @prefix: the place to store the prefix
1163 * Parse a qualified name
1165 * Returns the Name parsed or NULL
1169 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1170 xmlChar *ret = NULL;
1173 ret = xsltScanNCName(ctxt);
1177 ret = xsltScanNCName(ctxt);
1183 * xsltCompileIdKeyPattern:
1184 * @ctxt: the compilation context
1185 * @name: a preparsed name
1186 * @aid: whether id/key are allowed there
1188 * Compile the XSLT LocationIdKeyPattern
1189 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1190 * | 'key' '(' Literal ',' Literal ')'
1192 * also handle NodeType and PI from:
1194 * [7] NodeTest ::= NameTest
1195 * | NodeType '(' ')'
1196 * | 'processing-instruction' '(' Literal ')'
1199 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1200 xmlChar *lit = NULL;
1201 xmlChar *lit2 = NULL;
1204 xsltTransformError(NULL, NULL, NULL,
1205 "xsltCompileIdKeyPattern : ( expected\n");
1209 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1212 lit = xsltScanLiteral(ctxt);
1217 xsltTransformError(NULL, NULL, NULL,
1218 "xsltCompileIdKeyPattern : ) expected\n");
1223 PUSH(XSLT_OP_ID, lit, NULL);
1224 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1227 lit = xsltScanLiteral(ctxt);
1232 xsltTransformError(NULL, NULL, NULL,
1233 "xsltCompileIdKeyPattern : , expected\n");
1239 lit2 = xsltScanLiteral(ctxt);
1244 xsltTransformError(NULL, NULL, NULL,
1245 "xsltCompileIdKeyPattern : ) expected\n");
1250 /* TODO: support namespace in keys */
1251 PUSH(XSLT_OP_KEY, lit, lit2);
1252 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1256 lit = xsltScanLiteral(ctxt);
1261 xsltTransformError(NULL, NULL, NULL,
1262 "xsltCompileIdKeyPattern : ) expected\n");
1268 PUSH(XSLT_OP_PI, lit, NULL);
1269 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1273 xsltTransformError(NULL, NULL, NULL,
1274 "xsltCompileIdKeyPattern : ) expected\n");
1279 PUSH(XSLT_OP_TEXT, NULL, NULL);
1280 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1284 xsltTransformError(NULL, NULL, NULL,
1285 "xsltCompileIdKeyPattern : ) expected\n");
1290 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1291 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1295 xsltTransformError(NULL, NULL, NULL,
1296 "xsltCompileIdKeyPattern : ) expected\n");
1301 PUSH(XSLT_OP_NODE, NULL, NULL);
1303 xsltTransformError(NULL, NULL, NULL,
1304 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1308 xsltTransformError(NULL, NULL, NULL,
1309 "xsltCompileIdKeyPattern : node type\n");
1319 * xsltCompileStepPattern:
1320 * @ctxt: the compilation context
1321 * @token: a posible precompiled name
1323 * Compile the XSLT StepPattern and generates a precompiled
1324 * form suitable for fast matching.
1326 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1327 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1328 * | ('child' | 'attribute') '::'
1330 * [7] NodeTest ::= NameTest
1331 * | NodeType '(' ')'
1332 * | 'processing-instruction' '(' Literal ')'
1333 * [8] Predicate ::= '[' PredicateExpr ']'
1334 * [9] PredicateExpr ::= Expr
1335 * [13] AbbreviatedAxisSpecifier ::= '@'?
1336 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1340 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1341 xmlChar *name = NULL;
1342 const xmlChar *URI = NULL;
1343 xmlChar *URL = NULL;
1347 if ((token == NULL) && (CUR == '@')) {
1348 xmlChar *prefix = NULL;
1353 PUSH(XSLT_OP_ATTR, NULL, NULL);
1354 goto parse_predicate;
1356 token = xsltScanQName(ctxt, &prefix);
1357 if (prefix != NULL) {
1360 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1362 xsltTransformError(NULL, NULL, NULL,
1363 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1366 URL = xmlStrdup(ns->href);
1370 if (token == NULL) {
1373 PUSH(XSLT_OP_ATTR, NULL, URL);
1376 xsltTransformError(NULL, NULL, NULL,
1377 "xsltCompileStepPattern : Name expected\n");
1381 PUSH(XSLT_OP_ATTR, token, URL);
1382 goto parse_predicate;
1385 token = xsltScanName(ctxt);
1386 if (token == NULL) {
1389 PUSH(XSLT_OP_ALL, token, NULL);
1390 goto parse_predicate;
1392 xsltTransformError(NULL, NULL, NULL,
1393 "xsltCompileStepPattern : Name expected\n");
1402 xsltCompileIdKeyPattern(ctxt, token, 0);
1405 } else if (CUR == ':') {
1408 xmlChar *prefix = token;
1412 * This is a namespace match
1414 token = xsltScanName(ctxt);
1415 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1417 xsltTransformError(NULL, NULL, NULL,
1418 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1423 URL = xmlStrdup(ns->href);
1426 if (token == NULL) {
1429 PUSH(XSLT_OP_NS, URL, NULL);
1431 xsltTransformError(NULL, NULL, NULL,
1432 "xsltCompileStepPattern : Name expected\n");
1437 PUSH(XSLT_OP_ELEM, token, URL);
1441 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1443 token = xsltScanName(ctxt);
1444 if (token == NULL) {
1447 PUSH(XSLT_OP_ALL, token, NULL);
1448 goto parse_predicate;
1450 xsltTransformError(NULL, NULL, NULL,
1451 "xsltCompileStepPattern : QName expected\n");
1456 URI = xsltGetQNameURI(ctxt->elem, &token);
1457 if (token == NULL) {
1461 name = xmlStrdup(token);
1463 URL = xmlStrdup(URI);
1465 PUSH(XSLT_OP_CHILD, name, URL);
1466 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1468 token = xsltScanName(ctxt);
1469 if (token == NULL) {
1470 xsltTransformError(NULL, NULL, NULL,
1471 "xsltCompileStepPattern : QName expected\n");
1475 URI = xsltGetQNameURI(ctxt->elem, &token);
1476 if (token == NULL) {
1480 name = xmlStrdup(token);
1482 URL = xmlStrdup(URI);
1484 PUSH(XSLT_OP_ATTR, name, URL);
1486 xsltTransformError(NULL, NULL, NULL,
1487 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1493 } else if (CUR == '*') {
1495 PUSH(XSLT_OP_ALL, token, NULL);
1497 URI = xsltGetQNameURI(ctxt->elem, &token);
1498 if (token == NULL) {
1503 URL = xmlStrdup(URI);
1504 PUSH(XSLT_OP_ELEM, token, URL);
1509 while (CUR == '[') {
1511 xmlChar *ret = NULL;
1517 /* Skip over nested predicates */
1520 else if (CUR == ']') {
1524 } else if (CUR == '"') {
1526 while ((CUR != 0) && (CUR != '"'))
1528 } else if (CUR == '\'') {
1530 while ((CUR != 0) && (CUR != '\''))
1536 xsltTransformError(NULL, NULL, NULL,
1537 "xsltCompileStepPattern : ']' expected\n");
1541 ret = xmlStrndup(q, CUR_PTR - q);
1542 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1543 /* push the predicate lower than local test */
1557 * xsltCompileRelativePathPattern:
1558 * @comp: the compilation context
1559 * @token: a posible precompiled name
1561 * Compile the XSLT RelativePathPattern and generates a precompiled
1562 * form suitable for fast matching.
1564 * [4] RelativePathPattern ::= StepPattern
1565 * | RelativePathPattern '/' StepPattern
1566 * | RelativePathPattern '//' StepPattern
1569 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1570 xsltCompileStepPattern(ctxt, token);
1574 while ((CUR != 0) && (CUR != '|')) {
1575 if ((CUR == '/') && (NXT(1) == '/')) {
1576 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1580 xsltCompileStepPattern(ctxt, NULL);
1581 } else if (CUR == '/') {
1582 PUSH(XSLT_OP_PARENT, NULL, NULL);
1585 if ((CUR != 0) || (CUR == '|')) {
1586 xsltCompileRelativePathPattern(ctxt, NULL);
1600 * xsltCompileLocationPathPattern:
1601 * @ctxt: the compilation context
1603 * Compile the XSLT LocationPathPattern and generates a precompiled
1604 * form suitable for fast matching.
1606 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1607 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1608 * | '//'? RelativePathPattern
1611 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1613 if ((CUR == '/') && (NXT(1) == '/')) {
1615 * since we reverse the query
1616 * a leading // can be safely ignored
1620 xsltCompileRelativePathPattern(ctxt, NULL);
1621 } else if (CUR == '/') {
1623 * We need to find root as the parent
1627 PUSH(XSLT_OP_ROOT, NULL, NULL);
1628 if ((CUR != 0) || (CUR == '|')) {
1629 PUSH(XSLT_OP_PARENT, NULL, NULL);
1630 xsltCompileRelativePathPattern(ctxt, NULL);
1632 } else if (CUR == '*') {
1633 xsltCompileRelativePathPattern(ctxt, NULL);
1634 } else if (CUR == '@') {
1635 xsltCompileRelativePathPattern(ctxt, NULL);
1638 name = xsltScanName(ctxt);
1640 xsltTransformError(NULL, NULL, NULL,
1641 "xsltCompileLocationPathPattern : Name expected\n");
1646 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1647 xsltCompileIdKeyPattern(ctxt, name, 1);
1648 if ((CUR == '/') && (NXT(1) == '/')) {
1649 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1653 xsltCompileRelativePathPattern(ctxt, NULL);
1654 } else if (CUR == '/') {
1655 PUSH(XSLT_OP_PARENT, NULL, NULL);
1658 xsltCompileRelativePathPattern(ctxt, NULL);
1662 xsltCompileRelativePathPattern(ctxt, name);
1669 * xsltCompilePattern:
1670 * @pattern: an XSLT pattern
1671 * @doc: the containing document
1672 * @node: the containing element
1673 * @style: the stylesheet
1674 * @runtime: the transformation context, if done at run-time
1676 * Compile the XSLT pattern and generates a list of precompiled form suitable
1677 * for fast matching.
1679 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1681 * Returns the generated pattern list or NULL in case of failure
1685 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1686 xmlNodePtr node, xsltStylesheetPtr style,
1687 xsltTransformContextPtr runtime) {
1688 xsltParserContextPtr ctxt = NULL;
1689 xsltCompMatchPtr element, first = NULL, previous = NULL;
1690 int current, start, end, level, j;
1692 if (pattern == NULL) {
1693 xsltTransformError(NULL, NULL, node,
1694 "xsltCompilePattern : NULL pattern\n");
1698 ctxt = xsltNewParserContext(style, runtime);
1704 while (pattern[current] != 0) {
1706 while (IS_BLANK(pattern[current]))
1710 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1711 if (pattern[end] == '[')
1713 else if (pattern[end] == ']')
1715 else if (pattern[end] == '\'') {
1717 while ((pattern[end] != 0) && (pattern[end] != '\''))
1719 } else if (pattern[end] == '"') {
1721 while ((pattern[end] != 0) && (pattern[end] != '"'))
1726 if (current == end) {
1727 xsltTransformError(NULL, NULL, node,
1728 "xsltCompilePattern : NULL pattern\n");
1731 element = xsltNewCompMatch();
1732 if (element == NULL) {
1737 else if (previous != NULL)
1738 previous->next = element;
1741 ctxt->comp = element;
1742 ctxt->base = xmlStrndup(&pattern[start], end - start);
1743 if (ctxt->base == NULL)
1745 ctxt->cur = &(ctxt->base)[current - start];
1746 element->pattern = ctxt->base;
1747 element->nsList = xmlGetNsList(doc, node);
1749 if (element->nsList != NULL) {
1750 while (element->nsList[j] != NULL)
1756 #ifdef WITH_XSLT_DEBUG_PATTERN
1757 xsltGenericDebug(xsltGenericDebugContext,
1758 "xsltCompilePattern : parsing '%s'\n",
1761 xsltCompileLocationPathPattern(ctxt);
1763 xsltTransformError(NULL, style, node,
1764 "xsltCompilePattern : failed to compile '%s'\n",
1766 if (style != NULL) style->errors++;
1771 * Reverse for faster interpretation.
1773 xsltReverseCompMatch(element);
1776 * Set-up the priority
1778 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1779 (element->steps[0].op == XSLT_OP_ATTR)) &&
1780 (element->steps[0].value != NULL) &&
1781 (element->steps[1].op == XSLT_OP_END)) {
1782 element->priority = 0;
1784 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1785 (element->steps[1].op == XSLT_OP_END)) {
1786 element->priority = 0;
1788 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1789 (element->steps[0].value != NULL) &&
1790 (element->steps[1].op == XSLT_OP_END)) {
1791 element->priority = 0;
1792 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1793 (element->steps[0].value2 != NULL) &&
1794 (element->steps[1].op == XSLT_OP_END)) {
1795 element->priority = -0.25;
1796 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1797 (element->steps[0].value != NULL) &&
1798 (element->steps[1].op == XSLT_OP_END)) {
1799 element->priority = -0.25;
1800 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1801 (element->steps[0].value == NULL) &&
1802 (element->steps[0].value2 == NULL) &&
1803 (element->steps[1].op == XSLT_OP_END)) {
1804 element->priority = -0.5;
1805 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1806 (element->steps[0].op == XSLT_OP_TEXT) ||
1807 (element->steps[0].op == XSLT_OP_ALL) ||
1808 (element->steps[0].op == XSLT_OP_NODE) ||
1809 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1810 (element->steps[1].op == XSLT_OP_END)) {
1811 element->priority = -0.5;
1813 element->priority = 0.5;
1815 #ifdef WITH_XSLT_DEBUG_PATTERN
1816 xsltGenericDebug(xsltGenericDebugContext,
1817 "xsltCompilePattern : parsed %s, default priority %f\n",
1818 element->pattern, element->priority);
1820 if (pattern[end] == '|')
1825 xsltTransformError(NULL, style, node,
1826 "xsltCompilePattern : NULL pattern\n");
1827 if (style != NULL) style->errors++;
1831 xsltFreeParserContext(ctxt);
1836 xsltFreeParserContext(ctxt);
1838 xsltFreeCompMatchList(first);
1842 /************************************************************************
1844 * Module interfaces *
1846 ************************************************************************/
1850 * @style: an XSLT stylesheet
1851 * @cur: an XSLT template
1852 * @mode: the mode name or NULL
1853 * @modeURI: the mode URI or NULL
1855 * Register the XSLT pattern associated to @cur
1857 * Returns -1 in case of error, 0 otherwise
1860 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1861 const xmlChar *mode, const xmlChar *modeURI) {
1862 xsltCompMatchPtr pat, list, *top = NULL, next;
1863 const xmlChar *name = NULL;
1864 float priority; /* the priority */
1866 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1869 priority = cur->priority;
1870 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1876 pat->template = cur;
1878 pat->mode = xmlStrdup(mode);
1879 if (modeURI != NULL)
1880 pat->modeURI = xmlStrdup(modeURI);
1881 if (priority != XSLT_PAT_NO_PRIORITY)
1882 pat->priority = priority;
1885 * insert it in the hash table list corresponding to its lookup name
1887 switch (pat->steps[0].op) {
1889 if (pat->steps[0].value != NULL)
1890 name = pat->steps[0].value;
1892 top = (xsltCompMatchPtr *) &(style->attrMatch);
1895 case XSLT_OP_PARENT:
1896 case XSLT_OP_ANCESTOR:
1897 top = (xsltCompMatchPtr *) &(style->elemMatch);
1900 top = (xsltCompMatchPtr *) &(style->rootMatch);
1903 top = (xsltCompMatchPtr *) &(style->keyMatch);
1906 /* TODO optimize ID !!! */
1909 top = (xsltCompMatchPtr *) &(style->elemMatch);
1912 case XSLT_OP_PREDICATE:
1913 xsltTransformError(NULL, style, NULL,
1914 "xsltAddTemplate: invalid compiled pattern\n");
1915 xsltFreeCompMatch(pat);
1918 * TODO: some flags at the top level about type based patterns
1919 * would be faster than inclusion in the hash table.
1922 if (pat->steps[0].value != NULL)
1923 name = pat->steps[0].value;
1925 top = (xsltCompMatchPtr *) &(style->piMatch);
1927 case XSLT_OP_COMMENT:
1928 top = (xsltCompMatchPtr *) &(style->commentMatch);
1931 top = (xsltCompMatchPtr *) &(style->textMatch);
1935 if (pat->steps[0].value != NULL)
1936 name = pat->steps[0].value;
1938 top = (xsltCompMatchPtr *) &(style->elemMatch);
1942 if (style->templatesHash == NULL) {
1943 style->templatesHash = xmlHashCreate(1024);
1944 if (style->templatesHash == NULL) {
1945 xsltFreeCompMatch(pat);
1948 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1950 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1951 name, mode, modeURI);
1953 xmlHashAddEntry3(style->templatesHash, name,
1954 mode, modeURI, pat);
1957 * Note '<=' since one must choose among the matching
1958 * template rules that are left, the one that occurs
1959 * last in the stylesheet
1961 if (list->priority <= pat->priority) {
1963 xmlHashUpdateEntry3(style->templatesHash, name,
1964 mode, modeURI, pat, NULL);
1966 while (list->next != NULL) {
1967 if (list->next->priority <= pat->priority)
1971 pat->next = list->next;
1976 } else if (top != NULL) {
1981 } else if (list->priority <= pat->priority) {
1985 while (list->next != NULL) {
1986 if (list->next->priority <= pat->priority)
1990 pat->next = list->next;
1994 xsltTransformError(NULL, style, NULL,
1995 "xsltAddTemplate: invalid compiled pattern\n");
1996 xsltFreeCompMatch(pat);
1999 #ifdef WITH_XSLT_DEBUG_PATTERN
2001 xsltGenericDebug(xsltGenericDebugContext,
2002 "added pattern : '%s' mode '%s' priority %f\n",
2003 pat->pattern, pat->mode, pat->priority);
2005 xsltGenericDebug(xsltGenericDebugContext,
2006 "added pattern : '%s' priority %f\n",
2007 pat->pattern, pat->priority);
2017 * @ctxt: a XSLT process context
2018 * @node: the node being processed
2019 * @style: the current style
2021 * Finds the template applying to this node, if @style is non-NULL
2022 * it means one needs to look for the next imported template in scope.
2024 * Returns the xsltTemplatePtr or NULL if not found
2027 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2028 xsltStylesheetPtr style) {
2029 xsltStylesheetPtr curstyle;
2030 xsltTemplatePtr ret = NULL;
2031 const xmlChar *name = NULL;
2032 xsltCompMatchPtr list = NULL;
2035 if ((ctxt == NULL) || (node == NULL))
2038 if (style == NULL) {
2039 curstyle = ctxt->style;
2041 curstyle = xsltNextImport(style);
2044 while ((curstyle != NULL) && (curstyle != style)) {
2045 priority = XSLT_PAT_NO_PRIORITY;
2046 /* TODO : handle IDs/keys here ! */
2047 if (curstyle->templatesHash != NULL) {
2049 * Use the top name as selector
2051 switch (node->type) {
2052 case XML_ELEMENT_NODE:
2053 if (node->name[0] == ' ')
2055 case XML_ATTRIBUTE_NODE:
2059 case XML_DOCUMENT_NODE:
2060 case XML_HTML_DOCUMENT_NODE:
2062 case XML_CDATA_SECTION_NODE:
2063 case XML_COMMENT_NODE:
2064 case XML_ENTITY_REF_NODE:
2065 case XML_ENTITY_NODE:
2066 case XML_DOCUMENT_TYPE_NODE:
2067 case XML_DOCUMENT_FRAG_NODE:
2068 case XML_NOTATION_NODE:
2070 case XML_ELEMENT_DECL:
2071 case XML_ATTRIBUTE_DECL:
2072 case XML_ENTITY_DECL:
2073 case XML_NAMESPACE_DECL:
2074 case XML_XINCLUDE_START:
2075 case XML_XINCLUDE_END:
2084 * find the list of appliable expressions based on the name
2086 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2087 name, ctxt->mode, ctxt->modeURI);
2090 while (list != NULL) {
2091 if (xsltTestCompMatch(ctxt, list, node,
2092 ctxt->mode, ctxt->modeURI)) {
2093 ret = list->template;
2094 priority = list->priority;
2102 * find alternate generic matches
2104 switch (node->type) {
2105 case XML_ELEMENT_NODE:
2106 if (node->name[0] == ' ')
2107 list = curstyle->rootMatch;
2109 list = curstyle->elemMatch;
2111 case XML_ATTRIBUTE_NODE:
2112 list = curstyle->attrMatch;
2115 list = curstyle->piMatch;
2117 case XML_DOCUMENT_NODE:
2118 case XML_HTML_DOCUMENT_NODE:
2119 list = curstyle->rootMatch;
2122 case XML_CDATA_SECTION_NODE:
2123 list = curstyle->textMatch;
2125 case XML_COMMENT_NODE:
2126 list = curstyle->commentMatch;
2128 case XML_ENTITY_REF_NODE:
2129 case XML_ENTITY_NODE:
2130 case XML_DOCUMENT_TYPE_NODE:
2131 case XML_DOCUMENT_FRAG_NODE:
2132 case XML_NOTATION_NODE:
2134 case XML_ELEMENT_DECL:
2135 case XML_ATTRIBUTE_DECL:
2136 case XML_ENTITY_DECL:
2137 case XML_NAMESPACE_DECL:
2138 case XML_XINCLUDE_START:
2139 case XML_XINCLUDE_END:
2145 while ((list != NULL) &&
2146 ((ret == NULL) || (list->priority > priority))) {
2147 if (xsltTestCompMatch(ctxt, list, node,
2148 ctxt->mode, ctxt->modeURI)) {
2149 ret = list->template;
2150 priority = list->priority;
2156 * Some of the tests for elements can also apply to documents
2158 if ((node->type == XML_DOCUMENT_NODE) ||
2159 (node->type == XML_HTML_DOCUMENT_NODE) ||
2160 (node->type == XML_TEXT_NODE)) {
2161 list = curstyle->elemMatch;
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;
2172 } else if ((node->type == XML_PI_NODE) ||
2173 (node->type == XML_COMMENT_NODE)) {
2174 list = curstyle->elemMatch;
2175 while ((list != NULL) &&
2176 ((ret == NULL) || (list->priority > priority))) {
2177 if (xsltTestCompMatch(ctxt, list, node,
2178 ctxt->mode, ctxt->modeURI)) {
2179 ret = list->template;
2180 priority = list->priority;
2187 if (node->_private != NULL) {
2188 list = curstyle->keyMatch;
2189 while ((list != NULL) &&
2190 ((ret == NULL) || (list->priority > priority))) {
2191 if (xsltTestCompMatch(ctxt, list, node,
2192 ctxt->mode, ctxt->modeURI)) {
2193 ret = list->template;
2194 priority = list->priority;
2204 * Cycle on next curstylesheet import.
2206 curstyle = xsltNextImport(curstyle);
2212 * xsltCleanupTemplates:
2213 * @style: an XSLT stylesheet
2215 * Cleanup the state of the templates used by the stylesheet and
2216 * the ones it imports.
2219 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2223 * xsltFreeTemplateHashes:
2224 * @style: an XSLT stylesheet
2226 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2229 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2230 if (style->templatesHash != NULL)
2231 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2232 (xmlHashDeallocator) xsltFreeCompMatchList);
2233 if (style->rootMatch != NULL)
2234 xsltFreeCompMatchList(style->rootMatch);
2235 if (style->keyMatch != NULL)
2236 xsltFreeCompMatchList(style->keyMatch);
2237 if (style->elemMatch != NULL)
2238 xsltFreeCompMatchList(style->elemMatch);
2239 if (style->attrMatch != NULL)
2240 xsltFreeCompMatchList(style->attrMatch);
2241 if (style->parentMatch != NULL)
2242 xsltFreeCompMatchList(style->parentMatch);
2243 if (style->textMatch != NULL)
2244 xsltFreeCompMatchList(style->textMatch);
2245 if (style->piMatch != NULL)
2246 xsltFreeCompMatchList(style->piMatch);
2247 if (style->commentMatch != NULL)
2248 xsltFreeCompMatchList(style->commentMatch);