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 xsltPrintErrorContext(NULL, NULL, NULL);
129 xsltGenericError(xsltGenericErrorContext,
130 "xsltNewCompMatch : malloc failed\n");
133 memset(cur, 0, sizeof(xsltCompMatch));
142 * @comp: an XSLT comp
144 * Free up the memory allocated by @comp
147 xsltFreeCompMatch(xsltCompMatchPtr comp) {
153 if (comp->pattern != NULL)
154 xmlFree((xmlChar *)comp->pattern);
155 if (comp->mode != NULL)
156 xmlFree((xmlChar *)comp->mode);
157 if (comp->modeURI != NULL)
158 xmlFree((xmlChar *)comp->modeURI);
159 if (comp->nsList != NULL)
160 xmlFree(comp->nsList);
161 for (i = 0;i < comp->nbStep;i++) {
162 op = &comp->steps[i];
163 if (op->value != NULL)
165 if (op->value2 != NULL)
167 if (op->value3 != NULL)
169 if (op->comp != NULL)
170 xmlXPathFreeCompExpr(op->comp);
172 memset(comp, -1, sizeof(xsltCompMatch));
177 * xsltFreeCompMatchList:
178 * @comp: an XSLT comp list
180 * Free up the memory allocated by all the elements of @comp
183 xsltFreeCompMatchList(xsltCompMatchPtr comp) {
184 xsltCompMatchPtr cur;
186 while (comp != NULL) {
189 xsltFreeCompMatch(cur);
194 * xsltNewParserContext:
195 * @style: the stylesheet
196 * @ctxt: the transformation context, if done at run-time
198 * Create a new XSLT ParserContext
200 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
202 static xsltParserContextPtr
203 xsltNewParserContext(xsltStylesheetPtr style, xsltTransformContextPtr ctxt) {
204 xsltParserContextPtr cur;
206 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
208 xsltPrintErrorContext(NULL, NULL, NULL);
209 xsltGenericError(xsltGenericErrorContext,
210 "xsltNewParserContext : malloc failed\n");
213 memset(cur, 0, sizeof(xsltParserContext));
220 * xsltFreeParserContext:
221 * @ctxt: an XSLT parser context
223 * Free up the memory allocated by @ctxt
226 xsltFreeParserContext(xsltParserContextPtr ctxt) {
229 memset(ctxt, -1, sizeof(xsltParserContext));
235 * @comp: the compiled match expression
237 * @value: the first value
238 * @value2: the second value
240 * Add an step to an XSLT Compiled Match
242 * Returns -1 in case of failure, 0 otherwise.
245 xsltCompMatchAdd(xsltParserContextPtr ctxt, xsltCompMatchPtr comp,
246 xsltOp op, xmlChar * value, xmlChar * value2)
248 if (comp->nbStep >= 40) {
249 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
250 xsltGenericError(xsltGenericErrorContext,
251 "xsltCompMatchAdd: overflow\n");
254 comp->steps[comp->nbStep].op = op;
255 comp->steps[comp->nbStep].value = value;
256 comp->steps[comp->nbStep].value2 = value2;
257 if (ctxt->ctxt != NULL) {
258 comp->steps[comp->nbStep].previousExtra =
259 xsltAllocateExtraCtxt(ctxt->ctxt);
260 comp->steps[comp->nbStep].indexExtra =
261 xsltAllocateExtraCtxt(ctxt->ctxt);
262 comp->steps[comp->nbStep].lenExtra =
263 xsltAllocateExtraCtxt(ctxt->ctxt);
265 comp->steps[comp->nbStep].previousExtra =
266 xsltAllocateExtra(ctxt->style);
267 comp->steps[comp->nbStep].indexExtra =
268 xsltAllocateExtra(ctxt->style);
269 comp->steps[comp->nbStep].lenExtra =
270 xsltAllocateExtra(ctxt->style);
272 if (op == XSLT_OP_PREDICATE) {
273 comp->steps[comp->nbStep].comp = xmlXPathCompile(value);
280 * xsltSwapTopCompMatch:
281 * @comp: the compiled match expression
283 * reverse the two top steps.
286 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
288 int j = comp->nbStep - 1;
291 register xmlChar *tmp;
293 register xmlXPathCompExprPtr expr;
295 tmp = comp->steps[i].value;
296 comp->steps[i].value = comp->steps[j].value;
297 comp->steps[j].value = tmp;
298 tmp = comp->steps[i].value2;
299 comp->steps[i].value2 = comp->steps[j].value2;
300 comp->steps[j].value2 = tmp;
301 op = comp->steps[i].op;
302 comp->steps[i].op = comp->steps[j].op;
303 comp->steps[j].op = op;
304 expr = comp->steps[i].comp;
305 comp->steps[i].comp = comp->steps[j].comp;
306 comp->steps[j].comp = expr;
311 * xsltReverseCompMatch:
312 * @comp: the compiled match expression
314 * reverse all the stack of expressions
317 xsltReverseCompMatch(xsltCompMatchPtr comp) {
319 int j = comp->nbStep - 1;
322 register xmlChar *tmp;
324 register xmlXPathCompExprPtr expr;
325 tmp = comp->steps[i].value;
326 comp->steps[i].value = comp->steps[j].value;
327 comp->steps[j].value = tmp;
328 tmp = comp->steps[i].value2;
329 comp->steps[i].value2 = comp->steps[j].value2;
330 comp->steps[j].value2 = tmp;
331 op = comp->steps[i].op;
332 comp->steps[i].op = comp->steps[j].op;
333 comp->steps[j].op = op;
334 expr = comp->steps[i].comp;
335 comp->steps[i].comp = comp->steps[j].comp;
336 comp->steps[j].comp = expr;
340 comp->steps[comp->nbStep++].op = XSLT_OP_END;
343 /************************************************************************
345 * The interpreter for the precompiled patterns *
347 ************************************************************************/
351 * @ctxt: a XSLT process context
352 * @comp: the precompiled pattern
354 * @mode: the mode name or NULL
355 * @modeURI: the mode URI or NULL
357 * Test wether the node matches the pattern
359 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
362 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
363 xmlNodePtr node, const xmlChar *mode,
364 const xmlChar *modeURI) {
366 xsltStepOpPtr step, select = NULL;
368 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
369 xsltPrintErrorContext(ctxt, NULL, node);
370 xsltGenericError(xsltGenericErrorContext,
371 "xsltTestCompMatch: null arg\n");
375 if (comp->mode == NULL)
377 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
380 if (comp->mode != NULL)
383 if (modeURI != NULL) {
384 if (comp->modeURI == NULL)
386 if ((comp->modeURI != modeURI) &&
387 (!xmlStrEqual(comp->modeURI, modeURI)))
390 if (comp->modeURI != NULL)
393 for (i = 0;i < comp->nbStep;i++) {
394 step = &comp->steps[i];
395 if (step->op != XSLT_OP_PREDICATE)
401 if ((node->type == XML_DOCUMENT_NODE) ||
402 #ifdef LIBXML_DOCB_ENABLED
403 (node->type == XML_DOCB_DOCUMENT_NODE) ||
405 (node->type == XML_HTML_DOCUMENT_NODE))
409 if (node->type != XML_ELEMENT_NODE)
411 if (step->value == NULL)
413 if (step->value[0] != node->name[0])
415 if (!xmlStrEqual(step->value, node->name))
419 if (node->ns == NULL) {
420 if (step->value2 != NULL)
422 } else if (node->ns->href != NULL) {
423 if (step->value2 == NULL)
425 if (!xmlStrEqual(step->value2, node->ns->href))
429 case XSLT_OP_CHILD: {
432 if ((node->type != XML_ELEMENT_NODE) &&
433 (node->type != XML_DOCUMENT_NODE) &&
434 #ifdef LIBXML_DOCB_ENABLED
435 (node->type != XML_DOCB_DOCUMENT_NODE) &&
437 (node->type != XML_HTML_DOCUMENT_NODE))
440 lst = node->children;
442 if (step->value != NULL) {
443 while (lst != NULL) {
444 if ((lst->type == XML_ELEMENT_NODE) &&
445 (step->value[0] == lst->name[0]) &&
446 (xmlStrEqual(step->value, lst->name)))
456 if (node->type != XML_ATTRIBUTE_NODE)
458 if (step->value != NULL) {
459 if (step->value[0] != node->name[0])
461 if (!xmlStrEqual(step->value, node->name))
465 if (node->ns == NULL) {
466 if (step->value2 != NULL)
468 } else if (step->value2 != NULL) {
469 if (!xmlStrEqual(step->value2, node->ns->href))
474 if ((node->type == XML_DOCUMENT_NODE) ||
475 (node->type == XML_HTML_DOCUMENT_NODE) ||
476 #ifdef LIBXML_DOCB_ENABLED
477 (node->type == XML_DOCB_DOCUMENT_NODE) ||
479 (node->type == XML_NAMESPACE_DECL))
484 if (step->value == NULL)
486 if (step->value[0] != node->name[0])
488 if (!xmlStrEqual(step->value, node->name))
491 if (node->ns == NULL) {
492 if (step->value2 != NULL)
494 } else if (node->ns->href != NULL) {
495 if (step->value2 == NULL)
497 if (!xmlStrEqual(step->value2, node->ns->href))
501 case XSLT_OP_ANCESTOR:
502 /* TODO: implement coalescing of ANCESTOR/NODE ops */
503 if (step->value == NULL) {
505 step = &comp->steps[i];
506 if (step->op == XSLT_OP_ROOT)
508 if (step->op != XSLT_OP_ELEM)
510 if (step->value == NULL)
515 if ((node->type == XML_DOCUMENT_NODE) ||
516 (node->type == XML_HTML_DOCUMENT_NODE) ||
517 #ifdef LIBXML_DOCB_ENABLED
518 (node->type == XML_DOCB_DOCUMENT_NODE) ||
520 (node->type == XML_NAMESPACE_DECL))
523 while (node != NULL) {
526 if ((node->type == XML_ELEMENT_NODE) &&
527 (step->value[0] == node->name[0]) &&
528 (xmlStrEqual(step->value, node->name))) {
530 if (node->ns == NULL) {
531 if (step->value2 == NULL)
533 } else if (node->ns->href != NULL) {
534 if ((step->value2 != NULL) &&
535 (xmlStrEqual(step->value2, node->ns->href)))
545 /* TODO Handle IDs decently, must be done differently */
548 if (node->type != XML_ELEMENT_NODE)
551 id = xmlGetID(node->doc, step->value);
552 if ((id == NULL) || (id->parent != node))
560 list = xsltGetKey(ctxt, step->value,
561 step->value3, step->value2);
564 for (indx = 0;indx < list->nodeNr;indx++)
565 if (list->nodeTab[indx] == node)
567 if (indx >= list->nodeNr)
572 if (node->type != XML_ELEMENT_NODE)
574 if (node->ns == NULL) {
575 if (step->value != NULL)
577 } else if (node->ns->href != NULL) {
578 if (step->value == NULL)
580 if (!xmlStrEqual(step->value, node->ns->href))
585 if (node->type != XML_ELEMENT_NODE)
588 case XSLT_OP_PREDICATE: {
591 int pos = 0, len = 0;
593 * The simple existing predicate code cannot handle
594 * properly cascaded predicates. If in this situation
595 * compute directly the full node list once and check
596 * if the node is in the result list.
598 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
599 xmlDocPtr prevdoc, doc;
600 xmlXPathObjectPtr list;
604 prevdoc = (xmlDocPtr)
605 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
607 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
608 list = (xmlXPathObjectPtr)
609 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra);
612 if ((list == NULL) || (prevdoc != doc)) {
614 xmlXPathObjectPtr newlist;
615 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 newlist = xmlXPathEval(query, ctxt->xpathCtxt);
627 if (newlist->type != XPATH_NODESET) {
628 xmlXPathFreeObject(newlist);
632 if ((parent == NULL) || (node->doc == NULL))
635 while (parent->parent != NULL)
636 parent = parent->parent;
637 if (((parent->type != XML_DOCUMENT_NODE) &&
638 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
639 (parent != (xmlNodePtr) node->doc))
644 xmlXPathFreeObject(list);
647 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra) =
649 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
651 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
652 (xmlFreeFunc) xmlXPathFreeObject;
656 if ((list->nodesetval == NULL) ||
657 (list->nodesetval->nodeNr <= 0)) {
659 xmlXPathFreeObject(list);
662 /* TODO: store the index and use it for the scan */
664 for (j = 0;j < list->nodesetval->nodeNr;j++) {
665 if (list->nodesetval->nodeTab[j] == node) {
667 xmlXPathFreeObject(list);
674 xmlXPathFreeObject(list);
678 * Depending on the last selection, one may need to
679 * recompute contextSize and proximityPosition.
681 * TODO: make this thread safe !
683 oldCS = ctxt->xpathCtxt->contextSize;
684 oldCP = ctxt->xpathCtxt->proximityPosition;
685 if ((select != NULL) &&
686 (select->op == XSLT_OP_ELEM) &&
687 (select->value != NULL) &&
688 (node->type == XML_ELEMENT_NODE) &&
689 (node->parent != NULL)) {
691 int index, nocache = 0;
693 previous = (xmlNodePtr)
694 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
696 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
697 if ((previous != NULL) &&
698 (previous->parent == node->parent)) {
700 * just walk back to adjust the index
703 xmlNodePtr sibling = node;
705 while (sibling != NULL) {
706 if (sibling == previous)
708 if ((node->type == XML_ELEMENT_NODE) &&
709 (node->name[0] == sibling->name[0]) &&
710 (xmlStrEqual(node->name, sibling->name))) {
711 if ((select->value2 == NULL) ||
712 ((sibling->ns != NULL) &&
713 (xmlStrEqual(select->value2,
714 sibling->ns->href))))
717 sibling = sibling->prev;
719 if (sibling == NULL) {
720 /* hum going backward in document order ... */
723 while (sibling != NULL) {
724 if (sibling == previous)
726 if ((select->value2 == NULL) ||
727 ((sibling->ns != NULL) &&
728 (xmlStrEqual(select->value2,
729 sibling->ns->href))))
731 sibling = sibling->next;
734 if (sibling != NULL) {
737 * If the node is in a Value Tree we cannot
740 if (node->doc != NULL) {
742 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
743 XSLT_RUNTIME_EXTRA(ctxt,
744 select->previousExtra) = node;
745 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
753 * recompute the index
755 xmlNodePtr siblings = node->parent->children;
756 xmlNodePtr parent = node->parent;
758 while (siblings != NULL) {
759 if (siblings->type == XML_ELEMENT_NODE) {
760 if (siblings == node) {
763 } else if ((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_DOCUMENT_NODE:
943 case XML_HTML_DOCUMENT_NODE:
944 #ifdef LIBXML_DOCB_ENABLED
945 case XML_DOCB_DOCUMENT_NODE:
947 case XML_ELEMENT_NODE:
948 case XML_CDATA_SECTION_NODE:
950 case XML_COMMENT_NODE:
952 case XML_ATTRIBUTE_NODE:
964 * xsltTestCompMatchList:
965 * @ctxt: a XSLT process context
967 * @comp: the precompiled pattern list
969 * Test wether the node matches one of the patterns in the list
971 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
974 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
975 xsltCompMatchPtr comp) {
978 if ((ctxt == NULL) || (node == NULL))
980 while (comp != NULL) {
981 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
989 /************************************************************************
991 * Dedicated parser for templates *
993 ************************************************************************/
995 #define CUR (*ctxt->cur)
996 #define SKIP(val) ctxt->cur += (val)
997 #define NXT(val) ctxt->cur[(val)]
998 #define CUR_PTR ctxt->cur
1000 #define SKIP_BLANKS \
1001 while (IS_BLANK(CUR)) NEXT
1003 #define CURRENT (*ctxt->cur)
1004 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1007 #define PUSH(op, val, val2) \
1008 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1011 xsltSwapTopCompMatch(ctxt->comp);
1013 #define XSLT_ERROR(X) \
1014 { xsltError(ctxt, __FILE__, __LINE__, X); \
1015 ctxt->error = (X); return; }
1017 #define XSLT_ERROR0(X) \
1018 { xsltError(ctxt, __FILE__, __LINE__, X); \
1019 ctxt->error = (X); return(0); }
1023 * @ctxt: the XPath Parser context
1025 * Parse an XPath Litteral:
1027 * [29] Literal ::= '"' [^"]* '"'
1030 * Returns the Literal parsed or NULL
1034 xsltScanLiteral(xsltParserContextPtr ctxt) {
1035 const xmlChar *q, *cur;
1036 xmlChar *ret = NULL;
1043 val = xmlStringCurrentChar(NULL, cur, &len);
1044 while ((IS_CHAR(val)) && (val != '"')) {
1046 val = xmlStringCurrentChar(NULL, cur, &len);
1048 if (!IS_CHAR(val)) {
1052 ret = xmlStrndup(q, cur - q);
1056 } else if (CUR == '\'') {
1059 val = xmlStringCurrentChar(NULL, cur, &len);
1060 while ((IS_CHAR(val)) && (val != '\'')) {
1062 val = xmlStringCurrentChar(NULL, cur, &len);
1064 if (!IS_CHAR(val)) {
1068 ret = xmlStrndup(q, cur - q);
1073 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1082 * @ctxt: the XPath Parser context
1084 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1085 * CombiningChar | Extender
1087 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1089 * [6] Names ::= Name (S Name)*
1091 * Returns the Name parsed or NULL
1095 xsltScanName(xsltParserContextPtr ctxt) {
1096 const xmlChar *q, *cur;
1097 xmlChar *ret = NULL;
1103 val = xmlStringCurrentChar(NULL, cur, &len);
1104 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1107 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1108 (val == '.') || (val == '-') ||
1110 (IS_COMBINING(val)) ||
1111 (IS_EXTENDER(val))) {
1113 val = xmlStringCurrentChar(NULL, cur, &len);
1115 ret = xmlStrndup(q, cur - q);
1122 * @ctxt: the XPath Parser context
1124 * Parses a non qualified name
1126 * Returns the Name parsed or NULL
1130 xsltScanNCName(xsltParserContextPtr ctxt) {
1131 const xmlChar *q, *cur;
1132 xmlChar *ret = NULL;
1138 val = xmlStringCurrentChar(NULL, cur, &len);
1139 if (!IS_LETTER(val) && (val != '_'))
1142 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1143 (val == '.') || (val == '-') ||
1145 (IS_COMBINING(val)) ||
1146 (IS_EXTENDER(val))) {
1148 val = xmlStringCurrentChar(NULL, cur, &len);
1150 ret = xmlStrndup(q, cur - q);
1157 * @ctxt: the XPath Parser context
1158 * @prefix: the place to store the prefix
1160 * Parse a qualified name
1162 * Returns the Name parsed or NULL
1166 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1167 xmlChar *ret = NULL;
1170 ret = xsltScanNCName(ctxt);
1174 ret = xsltScanNCName(ctxt);
1180 * xsltCompileIdKeyPattern:
1181 * @ctxt: the compilation context
1182 * @name: a preparsed name
1183 * @aid: whether id/key are allowed there
1185 * Compile the XSLT LocationIdKeyPattern
1186 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1187 * | 'key' '(' Literal ',' Literal ')'
1189 * also handle NodeType and PI from:
1191 * [7] NodeTest ::= NameTest
1192 * | NodeType '(' ')'
1193 * | 'processing-instruction' '(' Literal ')'
1196 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1197 xmlChar *lit = NULL;
1198 xmlChar *lit2 = NULL;
1201 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1202 xsltGenericError(xsltGenericErrorContext,
1203 "xsltCompileIdKeyPattern : ( expected\n");
1207 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1210 lit = xsltScanLiteral(ctxt);
1215 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1216 xsltGenericError(xsltGenericErrorContext,
1217 "xsltCompileIdKeyPattern : ) expected\n");
1222 PUSH(XSLT_OP_ID, lit, NULL);
1223 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1226 lit = xsltScanLiteral(ctxt);
1231 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1232 xsltGenericError(xsltGenericErrorContext,
1233 "xsltCompileIdKeyPattern : , expected\n");
1239 lit2 = xsltScanLiteral(ctxt);
1244 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1245 xsltGenericError(xsltGenericErrorContext,
1246 "xsltCompileIdKeyPattern : ) expected\n");
1251 /* TODO: support namespace in keys */
1252 PUSH(XSLT_OP_KEY, lit, lit2);
1253 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1257 lit = xsltScanLiteral(ctxt);
1262 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1263 xsltGenericError(xsltGenericErrorContext,
1264 "xsltCompileIdKeyPattern : ) expected\n");
1270 PUSH(XSLT_OP_PI, lit, NULL);
1271 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1275 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1276 xsltGenericError(xsltGenericErrorContext,
1277 "xsltCompileIdKeyPattern : ) expected\n");
1282 PUSH(XSLT_OP_TEXT, NULL, NULL);
1283 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1287 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1288 xsltGenericError(xsltGenericErrorContext,
1289 "xsltCompileIdKeyPattern : ) expected\n");
1294 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1295 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1299 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1300 xsltGenericError(xsltGenericErrorContext,
1301 "xsltCompileIdKeyPattern : ) expected\n");
1306 PUSH(XSLT_OP_NODE, NULL, NULL);
1308 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1309 xsltGenericError(xsltGenericErrorContext,
1310 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1314 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1315 xsltGenericError(xsltGenericErrorContext,
1316 "xsltCompileIdKeyPattern : node type\n");
1326 * xsltCompileStepPattern:
1327 * @ctxt: the compilation context
1328 * @token: a posible precompiled name
1330 * Compile the XSLT StepPattern and generates a precompiled
1331 * form suitable for fast matching.
1333 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1334 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1335 * | ('child' | 'attribute') '::'
1337 * [7] NodeTest ::= NameTest
1338 * | NodeType '(' ')'
1339 * | 'processing-instruction' '(' Literal ')'
1340 * [8] Predicate ::= '[' PredicateExpr ']'
1341 * [9] PredicateExpr ::= Expr
1342 * [13] AbbreviatedAxisSpecifier ::= '@'?
1343 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1347 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1348 xmlChar *name = NULL;
1349 const xmlChar *URI = NULL;
1350 xmlChar *URL = NULL;
1354 if ((token == NULL) && (CUR == '@')) {
1355 xmlChar *prefix = NULL;
1360 PUSH(XSLT_OP_ATTR, NULL, NULL);
1363 token = xsltScanQName(ctxt, &prefix);
1364 if (prefix != NULL) {
1367 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1369 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1370 xsltGenericError(xsltGenericErrorContext,
1371 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1374 URL = xmlStrdup(ns->href);
1378 if (token == NULL) {
1381 PUSH(XSLT_OP_ATTR, NULL, URL);
1384 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1385 xsltGenericError(xsltGenericErrorContext,
1386 "xsltCompileStepPattern : Name expected\n");
1390 PUSH(XSLT_OP_ATTR, token, URL);
1391 goto parse_predicate;
1394 token = xsltScanName(ctxt);
1395 if (token == NULL) {
1398 PUSH(XSLT_OP_ALL, token, NULL);
1399 goto parse_predicate;
1401 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1402 xsltGenericError(xsltGenericErrorContext,
1403 "xsltCompileStepPattern : Name expected\n");
1412 xsltCompileIdKeyPattern(ctxt, token, 0);
1415 } else if (CUR == ':') {
1418 xmlChar *prefix = token;
1422 * This is a namespace match
1424 token = xsltScanName(ctxt);
1425 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1427 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1428 xsltGenericError(xsltGenericErrorContext,
1429 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1434 URL = xmlStrdup(ns->href);
1437 if (token == NULL) {
1440 PUSH(XSLT_OP_NS, URL, NULL);
1442 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1443 xsltGenericError(xsltGenericErrorContext,
1444 "xsltCompileStepPattern : Name expected\n");
1449 PUSH(XSLT_OP_ELEM, token, URL);
1453 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1455 token = xsltScanName(ctxt);
1456 if (token == NULL) {
1459 PUSH(XSLT_OP_ALL, token, NULL);
1460 goto parse_predicate;
1462 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1463 xsltGenericError(xsltGenericErrorContext,
1464 "xsltCompileStepPattern : QName expected\n");
1469 URI = xsltGetQNameURI(ctxt->elem, &token);
1470 if (token == NULL) {
1474 name = xmlStrdup(token);
1476 URL = xmlStrdup(URI);
1478 PUSH(XSLT_OP_CHILD, name, URL);
1479 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1481 token = xsltScanName(ctxt);
1482 if (token == NULL) {
1483 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1484 xsltGenericError(xsltGenericErrorContext,
1485 "xsltCompileStepPattern : QName expected\n");
1489 URI = xsltGetQNameURI(ctxt->elem, &token);
1490 if (token == NULL) {
1494 name = xmlStrdup(token);
1496 URL = xmlStrdup(URI);
1498 PUSH(XSLT_OP_ATTR, name, URL);
1500 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1501 xsltGenericError(xsltGenericErrorContext,
1502 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1508 } else if (CUR == '*') {
1510 PUSH(XSLT_OP_ALL, token, NULL);
1512 URI = xsltGetQNameURI(ctxt->elem, &token);
1513 if (token == NULL) {
1518 URL = xmlStrdup(URI);
1519 PUSH(XSLT_OP_ELEM, token, URL);
1524 while (CUR == '[') {
1526 xmlChar *ret = NULL;
1532 /* Skip over nested predicates */
1535 else if (CUR == ']') {
1539 } else if (CUR == '"') {
1541 while ((CUR != 0) && (CUR != '"'))
1543 } else if (CUR == '\'') {
1545 while ((CUR != 0) && (CUR != '\''))
1551 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1552 xsltGenericError(xsltGenericErrorContext,
1553 "xsltCompileStepPattern : ']' expected\n");
1557 ret = xmlStrndup(q, CUR_PTR - q);
1558 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1559 /* push the predicate lower than local test */
1573 * xsltCompileRelativePathPattern:
1574 * @comp: the compilation context
1575 * @token: a posible precompiled name
1577 * Compile the XSLT RelativePathPattern and generates a precompiled
1578 * form suitable for fast matching.
1580 * [4] RelativePathPattern ::= StepPattern
1581 * | RelativePathPattern '/' StepPattern
1582 * | RelativePathPattern '//' StepPattern
1585 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1586 xsltCompileStepPattern(ctxt, token);
1590 while ((CUR != 0) && (CUR != '|')) {
1591 if ((CUR == '/') && (NXT(1) == '/')) {
1592 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1596 xsltCompileStepPattern(ctxt, NULL);
1597 } else if (CUR == '/') {
1598 PUSH(XSLT_OP_PARENT, NULL, NULL);
1601 if ((CUR != 0) || (CUR == '|')) {
1602 xsltCompileRelativePathPattern(ctxt, NULL);
1616 * xsltCompileLocationPathPattern:
1617 * @ctxt: the compilation context
1619 * Compile the XSLT LocationPathPattern and generates a precompiled
1620 * form suitable for fast matching.
1622 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1623 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1624 * | '//'? RelativePathPattern
1627 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1629 if ((CUR == '/') && (NXT(1) == '/')) {
1631 * since we reverse the query
1632 * a leading // can be safely ignored
1636 xsltCompileRelativePathPattern(ctxt, NULL);
1637 } else if (CUR == '/') {
1639 * We need to find root as the parent
1643 PUSH(XSLT_OP_ROOT, NULL, NULL);
1644 if ((CUR != 0) || (CUR == '|')) {
1645 PUSH(XSLT_OP_PARENT, NULL, NULL);
1646 xsltCompileRelativePathPattern(ctxt, NULL);
1648 } else if (CUR == '*') {
1649 xsltCompileRelativePathPattern(ctxt, NULL);
1650 } else if (CUR == '@') {
1651 xsltCompileRelativePathPattern(ctxt, NULL);
1654 name = xsltScanName(ctxt);
1656 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1657 xsltGenericError(xsltGenericErrorContext,
1658 "xsltCompileLocationPathPattern : Name expected\n");
1663 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1664 xsltCompileIdKeyPattern(ctxt, name, 1);
1665 if ((CUR == '/') && (NXT(1) == '/')) {
1666 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1670 xsltCompileRelativePathPattern(ctxt, NULL);
1671 } else if (CUR == '/') {
1672 PUSH(XSLT_OP_PARENT, NULL, NULL);
1675 xsltCompileRelativePathPattern(ctxt, NULL);
1679 xsltCompileRelativePathPattern(ctxt, name);
1686 * xsltCompilePattern:
1687 * @pattern: an XSLT pattern
1688 * @doc: the containing document
1689 * @node: the containing element
1690 * @style: the stylesheet
1691 * @runtime: the transformation context, if done at run-time
1693 * Compile the XSLT pattern and generates a list of precompiled form suitable
1694 * for fast matching.
1696 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1698 * Returns the generated pattern list or NULL in case of failure
1702 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1703 xmlNodePtr node, xsltStylesheetPtr style,
1704 xsltTransformContextPtr runtime) {
1705 xsltParserContextPtr ctxt = NULL;
1706 xsltCompMatchPtr element, first = NULL, previous = NULL;
1707 int current, start, end, level, j;
1709 if (pattern == NULL) {
1710 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1711 xsltGenericError(xsltGenericErrorContext,
1712 "xsltCompilePattern : NULL pattern\n");
1716 ctxt = xsltNewParserContext(style, runtime);
1722 while (pattern[current] != 0) {
1724 while (IS_BLANK(pattern[current]))
1728 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1729 if (pattern[end] == '[')
1731 else if (pattern[end] == ']')
1733 else if (pattern[end] == '\'') {
1735 while ((pattern[end] != 0) && (pattern[end] != '\''))
1737 } else if (pattern[end] == '"') {
1739 while ((pattern[end] != 0) && (pattern[end] != '"'))
1744 if (current == end) {
1745 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1746 xsltGenericError(xsltGenericErrorContext,
1747 "xsltCompilePattern : NULL pattern\n");
1750 element = xsltNewCompMatch();
1751 if (element == NULL) {
1756 else if (previous != NULL)
1757 previous->next = element;
1760 ctxt->comp = element;
1761 ctxt->base = xmlStrndup(&pattern[start], end - start);
1762 if (ctxt->base == NULL)
1764 ctxt->cur = &(ctxt->base)[current - start];
1765 element->pattern = ctxt->base;
1766 element->nsList = xmlGetNsList(doc, node);
1768 if (element->nsList != NULL) {
1769 while (element->nsList[j] != NULL)
1775 #ifdef WITH_XSLT_DEBUG_PATTERN
1776 xsltGenericDebug(xsltGenericDebugContext,
1777 "xsltCompilePattern : parsing '%s'\n",
1780 xsltCompileLocationPathPattern(ctxt);
1782 xsltPrintErrorContext(NULL, style, node);
1783 xsltGenericError(xsltGenericErrorContext,
1784 "xsltCompilePattern : failed to compile '%s'\n",
1791 * Reverse for faster interpretation.
1793 xsltReverseCompMatch(element);
1796 * Set-up the priority
1798 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1799 (element->steps[0].op == XSLT_OP_ATTR)) &&
1800 (element->steps[0].value != NULL) &&
1801 (element->steps[1].op == XSLT_OP_END)) {
1802 element->priority = 0;
1804 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1805 (element->steps[1].op == XSLT_OP_END)) {
1806 element->priority = 0;
1808 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1809 (element->steps[0].value != NULL) &&
1810 (element->steps[1].op == XSLT_OP_END)) {
1811 element->priority = 0;
1812 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1813 (element->steps[0].value2 != NULL) &&
1814 (element->steps[1].op == XSLT_OP_END)) {
1815 element->priority = -0.25;
1816 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1817 (element->steps[0].value != NULL) &&
1818 (element->steps[1].op == XSLT_OP_END)) {
1819 element->priority = -0.25;
1820 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1821 (element->steps[0].value == NULL) &&
1822 (element->steps[0].value2 == NULL) &&
1823 (element->steps[1].op == XSLT_OP_END)) {
1824 element->priority = -0.5;
1825 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1826 (element->steps[0].op == XSLT_OP_TEXT) ||
1827 (element->steps[0].op == XSLT_OP_ALL) ||
1828 (element->steps[0].op == XSLT_OP_NODE) ||
1829 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1830 (element->steps[1].op == XSLT_OP_END)) {
1831 element->priority = -0.5;
1833 element->priority = 0.5;
1835 #ifdef WITH_XSLT_DEBUG_PATTERN
1836 xsltGenericDebug(xsltGenericDebugContext,
1837 "xsltCompilePattern : parsed %s, default priority %f\n",
1838 element->pattern, element->priority);
1840 if (pattern[end] == '|')
1845 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1846 xsltGenericError(xsltGenericErrorContext,
1847 "xsltCompilePattern : NULL pattern\n");
1852 xsltFreeParserContext(ctxt);
1857 xsltFreeParserContext(ctxt);
1859 xsltFreeCompMatchList(first);
1863 /************************************************************************
1865 * Module interfaces *
1867 ************************************************************************/
1871 * @style: an XSLT stylesheet
1872 * @cur: an XSLT template
1873 * @mode: the mode name or NULL
1874 * @modeURI: the mode URI or NULL
1876 * Register the XSLT pattern associated to @cur
1878 * Returns -1 in case of error, 0 otherwise
1881 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1882 const xmlChar *mode, const xmlChar *modeURI) {
1883 xsltCompMatchPtr pat, list, *top = NULL, next;
1884 const xmlChar *name = NULL;
1885 float priority; /* the priority */
1887 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1890 priority = cur->priority;
1891 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1897 pat->template = cur;
1899 pat->mode = xmlStrdup(mode);
1900 if (modeURI != NULL)
1901 pat->modeURI = xmlStrdup(modeURI);
1902 if (priority != XSLT_PAT_NO_PRIORITY)
1903 pat->priority = priority;
1906 * insert it in the hash table list corresponding to its lookup name
1908 switch (pat->steps[0].op) {
1910 if (pat->steps[0].value != NULL)
1911 name = pat->steps[0].value;
1913 top = (xsltCompMatchPtr *) &(style->attrMatch);
1916 case XSLT_OP_PARENT:
1917 case XSLT_OP_ANCESTOR:
1918 top = (xsltCompMatchPtr *) &(style->elemMatch);
1921 top = (xsltCompMatchPtr *) &(style->rootMatch);
1924 top = (xsltCompMatchPtr *) &(style->keyMatch);
1927 /* TODO optimize ID !!! */
1930 top = (xsltCompMatchPtr *) &(style->elemMatch);
1933 case XSLT_OP_PREDICATE:
1934 xsltPrintErrorContext(NULL, style, NULL);
1935 xsltGenericError(xsltGenericErrorContext,
1936 "xsltAddTemplate: invalid compiled pattern\n");
1937 xsltFreeCompMatch(pat);
1940 * TODO: some flags at the top level about type based patterns
1941 * would be faster than inclusion in the hash table.
1944 if (pat->steps[0].value != NULL)
1945 name = pat->steps[0].value;
1947 top = (xsltCompMatchPtr *) &(style->piMatch);
1949 case XSLT_OP_COMMENT:
1950 top = (xsltCompMatchPtr *) &(style->commentMatch);
1953 top = (xsltCompMatchPtr *) &(style->textMatch);
1957 if (pat->steps[0].value != NULL)
1958 name = pat->steps[0].value;
1960 top = (xsltCompMatchPtr *) &(style->elemMatch);
1964 if (style->templatesHash == NULL) {
1965 style->templatesHash = xmlHashCreate(1024);
1966 if (style->templatesHash == NULL) {
1967 xsltFreeCompMatch(pat);
1970 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1972 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1973 name, mode, modeURI);
1975 xmlHashAddEntry3(style->templatesHash, name,
1976 mode, modeURI, pat);
1979 * Note '<=' since one must choose among the matching
1980 * template rules that are left, the one that occurs
1981 * last in the stylesheet
1983 if (list->priority <= pat->priority) {
1985 xmlHashUpdateEntry3(style->templatesHash, name,
1986 mode, modeURI, pat, NULL);
1988 while (list->next != NULL) {
1989 if (list->next->priority <= pat->priority)
1993 pat->next = list->next;
1998 } else if (top != NULL) {
2003 } else if (list->priority <= pat->priority) {
2007 while (list->next != NULL) {
2008 if (list->next->priority <= pat->priority)
2012 pat->next = list->next;
2016 xsltPrintErrorContext(NULL, style, NULL);
2017 xsltGenericError(xsltGenericErrorContext,
2018 "xsltAddTemplate: invalid compiled pattern\n");
2019 xsltFreeCompMatch(pat);
2022 #ifdef WITH_XSLT_DEBUG_PATTERN
2024 xsltGenericDebug(xsltGenericDebugContext,
2025 "added pattern : '%s' mode '%s' priority %f\n",
2026 pat->pattern, pat->mode, pat->priority);
2028 xsltGenericDebug(xsltGenericDebugContext,
2029 "added pattern : '%s' priority %f\n",
2030 pat->pattern, pat->priority);
2040 * @ctxt: a XSLT process context
2041 * @node: the node being processed
2042 * @style: the current style
2044 * Finds the template applying to this node, if @style is non-NULL
2045 * it means one needs to look for the next imported template in scope.
2047 * Returns the xsltTemplatePtr or NULL if not found
2050 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2051 xsltStylesheetPtr style) {
2052 xsltStylesheetPtr curstyle;
2053 xsltTemplatePtr ret = NULL;
2054 const xmlChar *name = NULL;
2055 xsltCompMatchPtr list = NULL;
2058 if ((ctxt == NULL) || (node == NULL))
2061 if (style == NULL) {
2062 curstyle = ctxt->style;
2064 curstyle = xsltNextImport(style);
2067 while ((curstyle != NULL) && (curstyle != style)) {
2068 priority = XSLT_PAT_NO_PRIORITY;
2069 /* TODO : handle IDs/keys here ! */
2070 if (curstyle->templatesHash != NULL) {
2072 * Use the top name as selector
2074 switch (node->type) {
2075 case XML_ELEMENT_NODE:
2076 case XML_ATTRIBUTE_NODE:
2080 case XML_DOCUMENT_NODE:
2081 case XML_HTML_DOCUMENT_NODE:
2083 case XML_CDATA_SECTION_NODE:
2084 case XML_COMMENT_NODE:
2085 case XML_ENTITY_REF_NODE:
2086 case XML_ENTITY_NODE:
2087 case XML_DOCUMENT_TYPE_NODE:
2088 case XML_DOCUMENT_FRAG_NODE:
2089 case XML_NOTATION_NODE:
2091 case XML_ELEMENT_DECL:
2092 case XML_ATTRIBUTE_DECL:
2093 case XML_ENTITY_DECL:
2094 case XML_NAMESPACE_DECL:
2095 case XML_XINCLUDE_START:
2096 case XML_XINCLUDE_END:
2105 * find the list of appliable expressions based on the name
2107 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2108 name, ctxt->mode, ctxt->modeURI);
2111 while (list != NULL) {
2112 if (xsltTestCompMatch(ctxt, list, node,
2113 ctxt->mode, ctxt->modeURI)) {
2114 ret = list->template;
2115 priority = list->priority;
2123 * find alternate generic matches
2125 switch (node->type) {
2126 case XML_ELEMENT_NODE:
2127 list = curstyle->elemMatch;
2129 case XML_ATTRIBUTE_NODE:
2130 list = curstyle->attrMatch;
2133 list = curstyle->piMatch;
2135 case XML_DOCUMENT_NODE:
2136 case XML_HTML_DOCUMENT_NODE:
2137 list = curstyle->rootMatch;
2140 case XML_CDATA_SECTION_NODE:
2141 list = curstyle->textMatch;
2143 case XML_COMMENT_NODE:
2144 list = curstyle->commentMatch;
2146 case XML_ENTITY_REF_NODE:
2147 case XML_ENTITY_NODE:
2148 case XML_DOCUMENT_TYPE_NODE:
2149 case XML_DOCUMENT_FRAG_NODE:
2150 case XML_NOTATION_NODE:
2152 case XML_ELEMENT_DECL:
2153 case XML_ATTRIBUTE_DECL:
2154 case XML_ENTITY_DECL:
2155 case XML_NAMESPACE_DECL:
2156 case XML_XINCLUDE_START:
2157 case XML_XINCLUDE_END:
2163 while ((list != NULL) &&
2164 ((ret == NULL) || (list->priority > priority))) {
2165 if (xsltTestCompMatch(ctxt, list, node,
2166 ctxt->mode, ctxt->modeURI)) {
2167 ret = list->template;
2168 priority = list->priority;
2174 * Some of the tests for elements can also apply to documents
2176 if ((node->type == XML_DOCUMENT_NODE) ||
2177 (node->type == XML_HTML_DOCUMENT_NODE)) {
2178 list = curstyle->elemMatch;
2179 while ((list != NULL) &&
2180 ((ret == NULL) || (list->priority > priority))) {
2181 if (xsltTestCompMatch(ctxt, list, node,
2182 ctxt->mode, ctxt->modeURI)) {
2183 ret = list->template;
2184 priority = list->priority;
2191 if (node->_private != NULL) {
2192 list = curstyle->keyMatch;
2193 while ((list != NULL) &&
2194 ((ret == NULL) || (list->priority > priority))) {
2195 if (xsltTestCompMatch(ctxt, list, node,
2196 ctxt->mode, ctxt->modeURI)) {
2197 ret = list->template;
2198 priority = list->priority;
2208 * Cycle on next curstylesheet import.
2210 curstyle = xsltNextImport(curstyle);
2216 * xsltCleanupTemplates:
2217 * @style: an XSLT stylesheet
2219 * Cleanup the state of the templates used by the stylesheet and
2220 * the ones it imports.
2223 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2227 * xsltFreeTemplateHashes:
2228 * @style: an XSLT stylesheet
2230 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2233 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2234 if (style->templatesHash != NULL)
2235 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2236 (xmlHashDeallocator) xsltFreeCompMatchList);
2237 if (style->rootMatch != NULL)
2238 xsltFreeCompMatchList(style->rootMatch);
2239 if (style->keyMatch != NULL)
2240 xsltFreeCompMatchList(style->keyMatch);
2241 if (style->elemMatch != NULL)
2242 xsltFreeCompMatchList(style->elemMatch);
2243 if (style->attrMatch != NULL)
2244 xsltFreeCompMatchList(style->attrMatch);
2245 if (style->parentMatch != NULL)
2246 xsltFreeCompMatchList(style->parentMatch);
2247 if (style->textMatch != NULL)
2248 xsltFreeCompMatchList(style->textMatch);
2249 if (style->piMatch != NULL)
2250 xsltFreeCompMatchList(style->piMatch);
2251 if (style->commentMatch != NULL)
2252 xsltFreeCompMatchList(style->commentMatch);
2258 * @node: a node in the source tree
2259 * @pattern: an XSLT pattern
2260 * @ctxtdoc: context document (for namespaces)
2261 * @ctxtnode: context node (for namespaces)
2263 * Determine if a node matches a pattern.
2266 xsltMatchPattern(xsltTransformContextPtr context,
2268 const xmlChar *pattern,
2270 xmlNodePtr ctxtnode)
2273 xsltCompMatchPtr first, comp;
2275 if ((context != NULL) && (pattern != NULL)) {
2276 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2277 for (comp = first; comp != NULL; comp = comp->next) {
2278 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2283 xsltFreeCompMatchList(first);