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_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 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1196 xsltGenericError(xsltGenericErrorContext,
1197 "xsltCompileIdKeyPattern : ( expected\n");
1201 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1204 lit = xsltScanLiteral(ctxt);
1209 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1210 xsltGenericError(xsltGenericErrorContext,
1211 "xsltCompileIdKeyPattern : ) expected\n");
1216 PUSH(XSLT_OP_ID, lit, NULL);
1217 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1220 lit = xsltScanLiteral(ctxt);
1225 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1226 xsltGenericError(xsltGenericErrorContext,
1227 "xsltCompileIdKeyPattern : , expected\n");
1233 lit2 = xsltScanLiteral(ctxt);
1238 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1239 xsltGenericError(xsltGenericErrorContext,
1240 "xsltCompileIdKeyPattern : ) expected\n");
1245 /* TODO: support namespace in keys */
1246 PUSH(XSLT_OP_KEY, lit, lit2);
1247 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1251 lit = xsltScanLiteral(ctxt);
1256 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1257 xsltGenericError(xsltGenericErrorContext,
1258 "xsltCompileIdKeyPattern : ) expected\n");
1264 PUSH(XSLT_OP_PI, lit, NULL);
1265 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1269 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1270 xsltGenericError(xsltGenericErrorContext,
1271 "xsltCompileIdKeyPattern : ) expected\n");
1276 PUSH(XSLT_OP_TEXT, NULL, NULL);
1277 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1281 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1282 xsltGenericError(xsltGenericErrorContext,
1283 "xsltCompileIdKeyPattern : ) expected\n");
1288 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1289 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1293 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1294 xsltGenericError(xsltGenericErrorContext,
1295 "xsltCompileIdKeyPattern : ) expected\n");
1300 PUSH(XSLT_OP_NODE, NULL, NULL);
1302 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1303 xsltGenericError(xsltGenericErrorContext,
1304 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1308 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1309 xsltGenericError(xsltGenericErrorContext,
1310 "xsltCompileIdKeyPattern : node type\n");
1320 * xsltCompileStepPattern:
1321 * @ctxt: the compilation context
1322 * @token: a posible precompiled name
1324 * Compile the XSLT StepPattern and generates a precompiled
1325 * form suitable for fast matching.
1327 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1328 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1329 * | ('child' | 'attribute') '::'
1331 * [7] NodeTest ::= NameTest
1332 * | NodeType '(' ')'
1333 * | 'processing-instruction' '(' Literal ')'
1334 * [8] Predicate ::= '[' PredicateExpr ']'
1335 * [9] PredicateExpr ::= Expr
1336 * [13] AbbreviatedAxisSpecifier ::= '@'?
1337 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1341 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1342 xmlChar *name = NULL;
1343 const xmlChar *URI = NULL;
1344 xmlChar *URL = NULL;
1348 if ((token == NULL) && (CUR == '@')) {
1349 xmlChar *prefix = NULL;
1354 PUSH(XSLT_OP_ATTR, NULL, NULL);
1357 token = xsltScanQName(ctxt, &prefix);
1358 if (prefix != NULL) {
1361 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1363 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1364 xsltGenericError(xsltGenericErrorContext,
1365 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1368 URL = xmlStrdup(ns->href);
1372 if (token == NULL) {
1375 PUSH(XSLT_OP_ATTR, NULL, URL);
1378 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1379 xsltGenericError(xsltGenericErrorContext,
1380 "xsltCompileStepPattern : Name expected\n");
1384 PUSH(XSLT_OP_ATTR, token, URL);
1385 goto parse_predicate;
1388 token = xsltScanName(ctxt);
1389 if (token == NULL) {
1392 PUSH(XSLT_OP_ALL, token, NULL);
1393 goto parse_predicate;
1395 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1396 xsltGenericError(xsltGenericErrorContext,
1397 "xsltCompileStepPattern : Name expected\n");
1406 xsltCompileIdKeyPattern(ctxt, token, 0);
1409 } else if (CUR == ':') {
1412 xmlChar *prefix = token;
1416 * This is a namespace match
1418 token = xsltScanName(ctxt);
1419 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1421 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1422 xsltGenericError(xsltGenericErrorContext,
1423 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1428 URL = xmlStrdup(ns->href);
1431 if (token == NULL) {
1434 PUSH(XSLT_OP_NS, URL, NULL);
1436 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1437 xsltGenericError(xsltGenericErrorContext,
1438 "xsltCompileStepPattern : Name expected\n");
1443 PUSH(XSLT_OP_ELEM, token, URL);
1447 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1449 token = xsltScanName(ctxt);
1450 if (token == NULL) {
1453 PUSH(XSLT_OP_ALL, token, NULL);
1454 goto parse_predicate;
1456 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1457 xsltGenericError(xsltGenericErrorContext,
1458 "xsltCompileStepPattern : QName expected\n");
1463 URI = xsltGetQNameURI(ctxt->elem, &token);
1464 if (token == NULL) {
1468 name = xmlStrdup(token);
1470 URL = xmlStrdup(URI);
1472 PUSH(XSLT_OP_CHILD, name, URL);
1473 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1475 token = xsltScanName(ctxt);
1476 if (token == NULL) {
1477 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1478 xsltGenericError(xsltGenericErrorContext,
1479 "xsltCompileStepPattern : QName expected\n");
1483 URI = xsltGetQNameURI(ctxt->elem, &token);
1484 if (token == NULL) {
1488 name = xmlStrdup(token);
1490 URL = xmlStrdup(URI);
1492 PUSH(XSLT_OP_ATTR, name, URL);
1494 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1495 xsltGenericError(xsltGenericErrorContext,
1496 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1502 } else if (CUR == '*') {
1504 PUSH(XSLT_OP_ALL, token, NULL);
1506 URI = xsltGetQNameURI(ctxt->elem, &token);
1507 if (token == NULL) {
1512 URL = xmlStrdup(URI);
1513 PUSH(XSLT_OP_ELEM, token, URL);
1518 while (CUR == '[') {
1520 xmlChar *ret = NULL;
1526 /* Skip over nested predicates */
1529 else if (CUR == ']') {
1533 } else if (CUR == '"') {
1535 while ((CUR != 0) && (CUR != '"'))
1537 } else if (CUR == '\'') {
1539 while ((CUR != 0) && (CUR != '\''))
1545 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1546 xsltGenericError(xsltGenericErrorContext,
1547 "xsltCompileStepPattern : ']' expected\n");
1551 ret = xmlStrndup(q, CUR_PTR - q);
1552 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1553 /* push the predicate lower than local test */
1567 * xsltCompileRelativePathPattern:
1568 * @comp: the compilation context
1569 * @token: a posible precompiled name
1571 * Compile the XSLT RelativePathPattern and generates a precompiled
1572 * form suitable for fast matching.
1574 * [4] RelativePathPattern ::= StepPattern
1575 * | RelativePathPattern '/' StepPattern
1576 * | RelativePathPattern '//' StepPattern
1579 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1580 xsltCompileStepPattern(ctxt, token);
1584 while ((CUR != 0) && (CUR != '|')) {
1585 if ((CUR == '/') && (NXT(1) == '/')) {
1586 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1590 xsltCompileStepPattern(ctxt, NULL);
1591 } else if (CUR == '/') {
1592 PUSH(XSLT_OP_PARENT, NULL, NULL);
1595 if ((CUR != 0) || (CUR == '|')) {
1596 xsltCompileRelativePathPattern(ctxt, NULL);
1610 * xsltCompileLocationPathPattern:
1611 * @ctxt: the compilation context
1613 * Compile the XSLT LocationPathPattern and generates a precompiled
1614 * form suitable for fast matching.
1616 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1617 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1618 * | '//'? RelativePathPattern
1621 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1623 if ((CUR == '/') && (NXT(1) == '/')) {
1625 * since we reverse the query
1626 * a leading // can be safely ignored
1630 xsltCompileRelativePathPattern(ctxt, NULL);
1631 } else if (CUR == '/') {
1633 * We need to find root as the parent
1637 PUSH(XSLT_OP_ROOT, NULL, NULL);
1638 if ((CUR != 0) || (CUR == '|')) {
1639 PUSH(XSLT_OP_PARENT, NULL, NULL);
1640 xsltCompileRelativePathPattern(ctxt, NULL);
1642 } else if (CUR == '*') {
1643 xsltCompileRelativePathPattern(ctxt, NULL);
1644 } else if (CUR == '@') {
1645 xsltCompileRelativePathPattern(ctxt, NULL);
1648 name = xsltScanName(ctxt);
1650 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1651 xsltGenericError(xsltGenericErrorContext,
1652 "xsltCompileLocationPathPattern : Name expected\n");
1657 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1658 xsltCompileIdKeyPattern(ctxt, name, 1);
1659 if ((CUR == '/') && (NXT(1) == '/')) {
1660 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1664 xsltCompileRelativePathPattern(ctxt, NULL);
1665 } else if (CUR == '/') {
1666 PUSH(XSLT_OP_PARENT, NULL, NULL);
1669 xsltCompileRelativePathPattern(ctxt, NULL);
1673 xsltCompileRelativePathPattern(ctxt, name);
1680 * xsltCompilePattern:
1681 * @pattern: an XSLT pattern
1682 * @doc: the containing document
1683 * @node: the containing element
1684 * @style: the stylesheet
1685 * @runtime: the transformation context, if done at run-time
1687 * Compile the XSLT pattern and generates a list of precompiled form suitable
1688 * for fast matching.
1690 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1692 * Returns the generated pattern list or NULL in case of failure
1696 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1697 xmlNodePtr node, xsltStylesheetPtr style,
1698 xsltTransformContextPtr runtime) {
1699 xsltParserContextPtr ctxt = NULL;
1700 xsltCompMatchPtr element, first = NULL, previous = NULL;
1701 int current, start, end, level, j;
1703 if (pattern == NULL) {
1704 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1705 xsltGenericError(xsltGenericErrorContext,
1706 "xsltCompilePattern : NULL pattern\n");
1710 ctxt = xsltNewParserContext(style, runtime);
1716 while (pattern[current] != 0) {
1718 while (IS_BLANK(pattern[current]))
1722 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1723 if (pattern[end] == '[')
1725 else if (pattern[end] == ']')
1727 else if (pattern[end] == '\'') {
1729 while ((pattern[end] != 0) && (pattern[end] != '\''))
1731 } else if (pattern[end] == '"') {
1733 while ((pattern[end] != 0) && (pattern[end] != '"'))
1738 if (current == end) {
1739 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1740 xsltGenericError(xsltGenericErrorContext,
1741 "xsltCompilePattern : NULL pattern\n");
1744 element = xsltNewCompMatch();
1745 if (element == NULL) {
1750 else if (previous != NULL)
1751 previous->next = element;
1754 ctxt->comp = element;
1755 ctxt->base = xmlStrndup(&pattern[start], end - start);
1756 if (ctxt->base == NULL)
1758 ctxt->cur = &(ctxt->base)[current - start];
1759 element->pattern = ctxt->base;
1760 element->nsList = xmlGetNsList(doc, node);
1762 if (element->nsList != NULL) {
1763 while (element->nsList[j] != NULL)
1769 #ifdef WITH_XSLT_DEBUG_PATTERN
1770 xsltGenericDebug(xsltGenericDebugContext,
1771 "xsltCompilePattern : parsing '%s'\n",
1774 xsltCompileLocationPathPattern(ctxt);
1776 xsltPrintErrorContext(NULL, style, node);
1777 xsltGenericError(xsltGenericErrorContext,
1778 "xsltCompilePattern : failed to compile '%s'\n",
1785 * Reverse for faster interpretation.
1787 xsltReverseCompMatch(element);
1790 * Set-up the priority
1792 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1793 (element->steps[0].op == XSLT_OP_ATTR)) &&
1794 (element->steps[0].value != NULL) &&
1795 (element->steps[1].op == XSLT_OP_END)) {
1796 element->priority = 0;
1798 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1799 (element->steps[1].op == XSLT_OP_END)) {
1800 element->priority = 0;
1802 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1803 (element->steps[0].value != NULL) &&
1804 (element->steps[1].op == XSLT_OP_END)) {
1805 element->priority = 0;
1806 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1807 (element->steps[0].value2 != NULL) &&
1808 (element->steps[1].op == XSLT_OP_END)) {
1809 element->priority = -0.25;
1810 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1811 (element->steps[0].value != NULL) &&
1812 (element->steps[1].op == XSLT_OP_END)) {
1813 element->priority = -0.25;
1814 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1815 (element->steps[0].value == NULL) &&
1816 (element->steps[0].value2 == NULL) &&
1817 (element->steps[1].op == XSLT_OP_END)) {
1818 element->priority = -0.5;
1819 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1820 (element->steps[0].op == XSLT_OP_TEXT) ||
1821 (element->steps[0].op == XSLT_OP_ALL) ||
1822 (element->steps[0].op == XSLT_OP_NODE) ||
1823 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1824 (element->steps[1].op == XSLT_OP_END)) {
1825 element->priority = -0.5;
1827 element->priority = 0.5;
1829 #ifdef WITH_XSLT_DEBUG_PATTERN
1830 xsltGenericDebug(xsltGenericDebugContext,
1831 "xsltCompilePattern : parsed %s, default priority %f\n",
1832 element->pattern, element->priority);
1834 if (pattern[end] == '|')
1839 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1840 xsltGenericError(xsltGenericErrorContext,
1841 "xsltCompilePattern : NULL pattern\n");
1846 xsltFreeParserContext(ctxt);
1851 xsltFreeParserContext(ctxt);
1853 xsltFreeCompMatchList(first);
1857 /************************************************************************
1859 * Module interfaces *
1861 ************************************************************************/
1865 * @style: an XSLT stylesheet
1866 * @cur: an XSLT template
1867 * @mode: the mode name or NULL
1868 * @modeURI: the mode URI or NULL
1870 * Register the XSLT pattern associated to @cur
1872 * Returns -1 in case of error, 0 otherwise
1875 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1876 const xmlChar *mode, const xmlChar *modeURI) {
1877 xsltCompMatchPtr pat, list, *top = NULL, next;
1878 const xmlChar *name = NULL;
1879 float priority; /* the priority */
1881 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1884 priority = cur->priority;
1885 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1891 pat->template = cur;
1893 pat->mode = xmlStrdup(mode);
1894 if (modeURI != NULL)
1895 pat->modeURI = xmlStrdup(modeURI);
1896 if (priority != XSLT_PAT_NO_PRIORITY)
1897 pat->priority = priority;
1900 * insert it in the hash table list corresponding to its lookup name
1902 switch (pat->steps[0].op) {
1904 if (pat->steps[0].value != NULL)
1905 name = pat->steps[0].value;
1907 top = (xsltCompMatchPtr *) &(style->attrMatch);
1910 case XSLT_OP_PARENT:
1911 case XSLT_OP_ANCESTOR:
1912 top = (xsltCompMatchPtr *) &(style->elemMatch);
1915 top = (xsltCompMatchPtr *) &(style->rootMatch);
1918 top = (xsltCompMatchPtr *) &(style->keyMatch);
1921 /* TODO optimize ID !!! */
1924 top = (xsltCompMatchPtr *) &(style->elemMatch);
1927 case XSLT_OP_PREDICATE:
1928 xsltPrintErrorContext(NULL, style, NULL);
1929 xsltGenericError(xsltGenericErrorContext,
1930 "xsltAddTemplate: invalid compiled pattern\n");
1931 xsltFreeCompMatch(pat);
1934 * TODO: some flags at the top level about type based patterns
1935 * would be faster than inclusion in the hash table.
1938 if (pat->steps[0].value != NULL)
1939 name = pat->steps[0].value;
1941 top = (xsltCompMatchPtr *) &(style->piMatch);
1943 case XSLT_OP_COMMENT:
1944 top = (xsltCompMatchPtr *) &(style->commentMatch);
1947 top = (xsltCompMatchPtr *) &(style->textMatch);
1951 if (pat->steps[0].value != NULL)
1952 name = pat->steps[0].value;
1954 top = (xsltCompMatchPtr *) &(style->elemMatch);
1958 if (style->templatesHash == NULL) {
1959 style->templatesHash = xmlHashCreate(1024);
1960 if (style->templatesHash == NULL) {
1961 xsltFreeCompMatch(pat);
1964 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1966 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1967 name, mode, modeURI);
1969 xmlHashAddEntry3(style->templatesHash, name,
1970 mode, modeURI, pat);
1973 * Note '<=' since one must choose among the matching
1974 * template rules that are left, the one that occurs
1975 * last in the stylesheet
1977 if (list->priority <= pat->priority) {
1979 xmlHashUpdateEntry3(style->templatesHash, name,
1980 mode, modeURI, pat, NULL);
1982 while (list->next != NULL) {
1983 if (list->next->priority <= pat->priority)
1987 pat->next = list->next;
1992 } else if (top != NULL) {
1997 } else if (list->priority <= pat->priority) {
2001 while (list->next != NULL) {
2002 if (list->next->priority <= pat->priority)
2006 pat->next = list->next;
2010 xsltPrintErrorContext(NULL, style, NULL);
2011 xsltGenericError(xsltGenericErrorContext,
2012 "xsltAddTemplate: invalid compiled pattern\n");
2013 xsltFreeCompMatch(pat);
2016 #ifdef WITH_XSLT_DEBUG_PATTERN
2018 xsltGenericDebug(xsltGenericDebugContext,
2019 "added pattern : '%s' mode '%s' priority %f\n",
2020 pat->pattern, pat->mode, pat->priority);
2022 xsltGenericDebug(xsltGenericDebugContext,
2023 "added pattern : '%s' priority %f\n",
2024 pat->pattern, pat->priority);
2034 * @ctxt: a XSLT process context
2035 * @node: the node being processed
2036 * @style: the current style
2038 * Finds the template applying to this node, if @style is non-NULL
2039 * it means one needs to look for the next imported template in scope.
2041 * Returns the xsltTemplatePtr or NULL if not found
2044 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2045 xsltStylesheetPtr style) {
2046 xsltStylesheetPtr curstyle;
2047 xsltTemplatePtr ret = NULL;
2048 const xmlChar *name = NULL;
2049 xsltCompMatchPtr list = NULL;
2052 if ((ctxt == NULL) || (node == NULL))
2055 if (style == NULL) {
2056 curstyle = ctxt->style;
2058 curstyle = xsltNextImport(style);
2061 while ((curstyle != NULL) && (curstyle != style)) {
2062 priority = XSLT_PAT_NO_PRIORITY;
2063 /* TODO : handle IDs/keys here ! */
2064 if (curstyle->templatesHash != NULL) {
2066 * Use the top name as selector
2068 switch (node->type) {
2069 case XML_ELEMENT_NODE:
2070 case XML_ATTRIBUTE_NODE:
2074 case XML_DOCUMENT_NODE:
2075 case XML_HTML_DOCUMENT_NODE:
2077 case XML_CDATA_SECTION_NODE:
2078 case XML_COMMENT_NODE:
2079 case XML_ENTITY_REF_NODE:
2080 case XML_ENTITY_NODE:
2081 case XML_DOCUMENT_TYPE_NODE:
2082 case XML_DOCUMENT_FRAG_NODE:
2083 case XML_NOTATION_NODE:
2085 case XML_ELEMENT_DECL:
2086 case XML_ATTRIBUTE_DECL:
2087 case XML_ENTITY_DECL:
2088 case XML_NAMESPACE_DECL:
2089 case XML_XINCLUDE_START:
2090 case XML_XINCLUDE_END:
2099 * find the list of appliable expressions based on the name
2101 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2102 name, ctxt->mode, ctxt->modeURI);
2105 while (list != NULL) {
2106 if (xsltTestCompMatch(ctxt, list, node,
2107 ctxt->mode, ctxt->modeURI)) {
2108 ret = list->template;
2109 priority = list->priority;
2117 * find alternate generic matches
2119 switch (node->type) {
2120 case XML_ELEMENT_NODE:
2121 list = curstyle->elemMatch;
2123 case XML_ATTRIBUTE_NODE:
2124 list = curstyle->attrMatch;
2127 list = curstyle->piMatch;
2129 case XML_DOCUMENT_NODE:
2130 case XML_HTML_DOCUMENT_NODE:
2131 list = curstyle->rootMatch;
2134 case XML_CDATA_SECTION_NODE:
2135 list = curstyle->textMatch;
2137 case XML_COMMENT_NODE:
2138 list = curstyle->commentMatch;
2140 case XML_ENTITY_REF_NODE:
2141 case XML_ENTITY_NODE:
2142 case XML_DOCUMENT_TYPE_NODE:
2143 case XML_DOCUMENT_FRAG_NODE:
2144 case XML_NOTATION_NODE:
2146 case XML_ELEMENT_DECL:
2147 case XML_ATTRIBUTE_DECL:
2148 case XML_ENTITY_DECL:
2149 case XML_NAMESPACE_DECL:
2150 case XML_XINCLUDE_START:
2151 case XML_XINCLUDE_END:
2157 while ((list != NULL) &&
2158 ((ret == NULL) || (list->priority > priority))) {
2159 if (xsltTestCompMatch(ctxt, list, node,
2160 ctxt->mode, ctxt->modeURI)) {
2161 ret = list->template;
2162 priority = list->priority;
2168 * Some of the tests for elements can also apply to documents
2170 if ((node->type == XML_DOCUMENT_NODE) ||
2171 (node->type == XML_HTML_DOCUMENT_NODE) ||
2172 (node->type == XML_TEXT_NODE)) {
2173 list = curstyle->elemMatch;
2174 while ((list != NULL) &&
2175 ((ret == NULL) || (list->priority > priority))) {
2176 if (xsltTestCompMatch(ctxt, list, node,
2177 ctxt->mode, ctxt->modeURI)) {
2178 ret = list->template;
2179 priority = list->priority;
2186 if (node->_private != NULL) {
2187 list = curstyle->keyMatch;
2188 while ((list != NULL) &&
2189 ((ret == NULL) || (list->priority > priority))) {
2190 if (xsltTestCompMatch(ctxt, list, node,
2191 ctxt->mode, ctxt->modeURI)) {
2192 ret = list->template;
2193 priority = list->priority;
2203 * Cycle on next curstylesheet import.
2205 curstyle = xsltNextImport(curstyle);
2211 * xsltCleanupTemplates:
2212 * @style: an XSLT stylesheet
2214 * Cleanup the state of the templates used by the stylesheet and
2215 * the ones it imports.
2218 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2222 * xsltFreeTemplateHashes:
2223 * @style: an XSLT stylesheet
2225 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2228 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2229 if (style->templatesHash != NULL)
2230 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2231 (xmlHashDeallocator) xsltFreeCompMatchList);
2232 if (style->rootMatch != NULL)
2233 xsltFreeCompMatchList(style->rootMatch);
2234 if (style->keyMatch != NULL)
2235 xsltFreeCompMatchList(style->keyMatch);
2236 if (style->elemMatch != NULL)
2237 xsltFreeCompMatchList(style->elemMatch);
2238 if (style->attrMatch != NULL)
2239 xsltFreeCompMatchList(style->attrMatch);
2240 if (style->parentMatch != NULL)
2241 xsltFreeCompMatchList(style->parentMatch);
2242 if (style->textMatch != NULL)
2243 xsltFreeCompMatchList(style->textMatch);
2244 if (style->piMatch != NULL)
2245 xsltFreeCompMatchList(style->piMatch);
2246 if (style->commentMatch != NULL)
2247 xsltFreeCompMatchList(style->commentMatch);
2253 * @node: a node in the source tree
2254 * @pattern: an XSLT pattern
2255 * @ctxtdoc: context document (for namespaces)
2256 * @ctxtnode: context node (for namespaces)
2258 * Determine if a node matches a pattern.
2261 xsltMatchPattern(xsltTransformContextPtr context,
2263 const xmlChar *pattern,
2265 xmlNodePtr ctxtnode)
2268 xsltCompMatchPtr first, comp;
2270 if ((context != NULL) && (pattern != NULL)) {
2271 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2272 for (comp = first; comp != NULL; comp = comp->next) {
2273 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2278 xsltFreeCompMatchList(first);