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)
460 if (step->value[0] != node->name[0])
462 if (!xmlStrEqual(step->value, node->name))
466 if (node->ns == NULL) {
467 if (step->value2 != NULL)
469 } else if (node->ns->href != NULL) {
470 if (step->value2 == NULL)
472 if (!xmlStrEqual(step->value2, node->ns->href))
477 if ((node->type == XML_DOCUMENT_NODE) ||
478 (node->type == XML_HTML_DOCUMENT_NODE) ||
479 #ifdef LIBXML_DOCB_ENABLED
480 (node->type == XML_DOCB_DOCUMENT_NODE) ||
482 (node->type == XML_NAMESPACE_DECL))
487 if (step->value == NULL)
489 if (step->value[0] != node->name[0])
491 if (!xmlStrEqual(step->value, node->name))
494 if (node->ns == NULL) {
495 if (step->value2 != NULL)
497 } else if (node->ns->href != NULL) {
498 if (step->value2 == NULL)
500 if (!xmlStrEqual(step->value2, node->ns->href))
504 case XSLT_OP_ANCESTOR:
505 /* TODO: implement coalescing of ANCESTOR/NODE ops */
506 if (step->value == NULL) {
508 step = &comp->steps[i];
509 if (step->op == XSLT_OP_ROOT)
511 if (step->op != XSLT_OP_ELEM)
513 if (step->value == NULL)
518 if ((node->type == XML_DOCUMENT_NODE) ||
519 (node->type == XML_HTML_DOCUMENT_NODE) ||
520 #ifdef LIBXML_DOCB_ENABLED
521 (node->type == XML_DOCB_DOCUMENT_NODE) ||
523 (node->type == XML_NAMESPACE_DECL))
526 while (node != NULL) {
529 if ((node->type == XML_ELEMENT_NODE) &&
530 (step->value[0] == node->name[0]) &&
531 (xmlStrEqual(step->value, node->name))) {
533 if (node->ns == NULL) {
534 if (step->value2 == NULL)
536 } else if (node->ns->href != NULL) {
537 if ((step->value2 != NULL) &&
538 (xmlStrEqual(step->value2, node->ns->href)))
548 /* TODO Handle IDs decently, must be done differently */
551 if (node->type != XML_ELEMENT_NODE)
554 id = xmlGetID(node->doc, step->value);
555 if ((id == NULL) || (id->parent != node))
563 list = xsltGetKey(ctxt, step->value,
564 step->value3, step->value2);
567 for (indx = 0;indx < list->nodeNr;indx++)
568 if (list->nodeTab[indx] == node)
570 if (indx >= list->nodeNr)
575 if (node->type != XML_ELEMENT_NODE)
577 if (node->ns == NULL) {
578 if (step->value != NULL)
580 } else if (node->ns->href != NULL) {
581 if (step->value == NULL)
583 if (!xmlStrEqual(step->value, node->ns->href))
588 if (node->type != XML_ELEMENT_NODE)
591 case XSLT_OP_PREDICATE: {
594 int pos = 0, len = 0;
596 * The simple existing predicate code cannot handle
597 * properly cascaded predicates. If in this situation
598 * compute directly the full node list once and check
599 * if the node is in the result list.
601 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
603 xmlXPathObjectPtr list;
606 previous = (xmlNodePtr)
607 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
609 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
610 list = (xmlXPathObjectPtr)
611 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
615 if (comp->pattern[0] == '/')
616 query = xmlStrdup(comp->pattern);
618 query = xmlStrdup((const xmlChar *)"//");
619 query = xmlStrcat(query, comp->pattern);
621 list = xmlXPathEval(query, ctxt->xpathCtxt);
625 if (list->type != XPATH_NODESET) {
626 xmlXPathFreeObject(list);
629 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
631 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
632 (xmlFreeFunc) xmlXPathFreeObject;
634 if ((list->nodesetval == NULL) ||
635 (list->nodesetval->nodeNr <= 0))
638 for (j = 0;j < list->nodesetval->nodeNr;j++) {
639 if (list->nodesetval->nodeTab[j] == node) {
648 * Depending on the last selection, one may need to
649 * recompute contextSize and proximityPosition.
651 * TODO: make this thread safe !
653 oldCS = ctxt->xpathCtxt->contextSize;
654 oldCP = ctxt->xpathCtxt->proximityPosition;
655 if ((select != NULL) &&
656 (select->op == XSLT_OP_ELEM) &&
657 (select->value != NULL) &&
658 (node->type == XML_ELEMENT_NODE) &&
659 (node->parent != NULL)) {
661 int index, nocache = 0;
663 previous = (xmlNodePtr)
664 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
666 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
667 if ((previous != NULL) &&
668 (previous->parent == node->parent)) {
670 * just walk back to adjust the index
673 xmlNodePtr sibling = node;
675 while (sibling != NULL) {
676 if (sibling == previous)
678 if ((node->type == XML_ELEMENT_NODE) &&
679 (node->name[0] == sibling->name[0]) &&
680 (xmlStrEqual(node->name, sibling->name))) {
681 if ((select->value2 == NULL) ||
682 ((sibling->ns != NULL) &&
683 (xmlStrEqual(select->value2,
684 sibling->ns->href))))
687 sibling = sibling->prev;
689 if (sibling == NULL) {
690 /* hum going backward in document order ... */
693 while (sibling != NULL) {
694 if (sibling == previous)
696 if ((select->value2 == NULL) ||
697 ((sibling->ns != NULL) &&
698 (xmlStrEqual(select->value2,
699 sibling->ns->href))))
701 sibling = sibling->next;
704 if (sibling != NULL) {
707 * If the node is in a Value Tree we cannot
710 if (node->doc != NULL) {
712 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
713 XSLT_RUNTIME_EXTRA(ctxt,
714 select->previousExtra) = node;
715 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
723 * recompute the index
725 xmlNodePtr siblings = node->parent->children;
726 xmlNodePtr parent = node->parent;
728 while (siblings != NULL) {
729 if (siblings->type == XML_ELEMENT_NODE) {
730 if (siblings == node) {
733 } else if ((node->name[0] == siblings->name[0])
734 && (xmlStrEqual(node->name, siblings->name))) {
735 if ((select->value2 == NULL) ||
736 ((siblings->ns != NULL) &&
737 (xmlStrEqual(select->value2,
738 siblings->ns->href))))
742 siblings = siblings->next;
744 if ((parent == NULL) || (node->doc == NULL))
747 while (parent->parent != NULL)
748 parent = parent->parent;
749 if (((parent->type != XML_DOCUMENT_NODE) &&
750 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
751 (parent != (xmlNodePtr) node->doc))
756 ctxt->xpathCtxt->contextSize = len;
757 ctxt->xpathCtxt->proximityPosition = pos;
759 * If the node is in a Value Tree we cannot
762 if ((node->doc != NULL) && (nocache == 0)) {
763 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
765 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
767 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
771 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
772 (node->type == XML_ELEMENT_NODE)) {
774 int index, nocache = 0;
776 previous = (xmlNodePtr)
777 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
779 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
780 if ((previous != NULL) &&
781 (previous->parent == node->parent)) {
783 * just walk back to adjust the index
786 xmlNodePtr sibling = node;
788 while (sibling != NULL) {
789 if (sibling == previous)
791 if (sibling->type == XML_ELEMENT_NODE)
793 sibling = sibling->prev;
795 if (sibling == NULL) {
796 /* hum going backward in document order ... */
799 while (sibling != NULL) {
800 if (sibling == previous)
802 if (sibling->type == XML_ELEMENT_NODE)
804 sibling = sibling->next;
807 if (sibling != NULL) {
810 * If the node is in a Value Tree we cannot
813 if (node->doc != NULL) {
815 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
816 XSLT_RUNTIME_EXTRA(ctxt,
817 select->previousExtra) = node;
818 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
825 * recompute the index
827 xmlNodePtr siblings = node->parent->children;
828 xmlNodePtr parent = node->parent;
830 while (siblings != NULL) {
831 if (siblings->type == XML_ELEMENT_NODE) {
833 if (siblings == node) {
837 siblings = siblings->next;
839 if ((parent == NULL) || (node->doc == NULL))
842 while (parent->parent != NULL)
843 parent = parent->parent;
844 if (((parent->type != XML_DOCUMENT_NODE) &&
845 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
846 (parent != (xmlNodePtr) node->doc))
851 ctxt->xpathCtxt->contextSize = len;
852 ctxt->xpathCtxt->proximityPosition = pos;
854 * If the node is in a Value Tree we cannot
857 if ((node->doc != NULL) && (nocache == 0)) {
858 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
860 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
862 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
867 oldNode = ctxt->node;
870 if (step->value == NULL)
872 if (step->comp == NULL)
875 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
880 ctxt->xpathCtxt->contextSize = oldCS;
881 ctxt->xpathCtxt->proximityPosition = oldCP;
883 ctxt->node = oldNode;
887 ctxt->xpathCtxt->contextSize = oldCS;
888 ctxt->xpathCtxt->proximityPosition = oldCP;
890 ctxt->node = oldNode;
894 if (node->type != XML_PI_NODE)
896 if (step->value != NULL) {
897 if (!xmlStrEqual(step->value, node->name))
901 case XSLT_OP_COMMENT:
902 if (node->type != XML_COMMENT_NODE)
906 if ((node->type != XML_TEXT_NODE) &&
907 (node->type != XML_CDATA_SECTION_NODE))
911 switch (node->type) {
912 case XML_DOCUMENT_NODE:
913 case XML_HTML_DOCUMENT_NODE:
914 #ifdef LIBXML_DOCB_ENABLED
915 case XML_DOCB_DOCUMENT_NODE:
917 case XML_ELEMENT_NODE:
918 case XML_CDATA_SECTION_NODE:
920 case XML_COMMENT_NODE:
922 case XML_ATTRIBUTE_NODE:
934 * xsltTestCompMatchList:
935 * @ctxt: a XSLT process context
937 * @comp: the precompiled pattern list
939 * Test wether the node matches one of the patterns in the list
941 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
944 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
945 xsltCompMatchPtr comp) {
948 if ((ctxt == NULL) || (node == NULL))
950 while (comp != NULL) {
951 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
959 /************************************************************************
961 * Dedicated parser for templates *
963 ************************************************************************/
965 #define CUR (*ctxt->cur)
966 #define SKIP(val) ctxt->cur += (val)
967 #define NXT(val) ctxt->cur[(val)]
968 #define CUR_PTR ctxt->cur
970 #define SKIP_BLANKS \
971 while (IS_BLANK(CUR)) NEXT
973 #define CURRENT (*ctxt->cur)
974 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
977 #define PUSH(op, val, val2) \
978 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
981 xsltSwapTopCompMatch(ctxt->comp);
983 #define XSLT_ERROR(X) \
984 { xsltError(ctxt, __FILE__, __LINE__, X); \
985 ctxt->error = (X); return; }
987 #define XSLT_ERROR0(X) \
988 { xsltError(ctxt, __FILE__, __LINE__, X); \
989 ctxt->error = (X); return(0); }
993 * @ctxt: the XPath Parser context
995 * Parse an XPath Litteral:
997 * [29] Literal ::= '"' [^"]* '"'
1000 * Returns the Literal parsed or NULL
1004 xsltScanLiteral(xsltParserContextPtr ctxt) {
1005 const xmlChar *q, *cur;
1006 xmlChar *ret = NULL;
1013 val = xmlStringCurrentChar(NULL, cur, &len);
1014 while ((IS_CHAR(val)) && (val != '"')) {
1016 val = xmlStringCurrentChar(NULL, cur, &len);
1018 if (!IS_CHAR(val)) {
1022 ret = xmlStrndup(q, cur - q);
1026 } else if (CUR == '\'') {
1029 val = xmlStringCurrentChar(NULL, cur, &len);
1030 while ((IS_CHAR(val)) && (val != '\'')) {
1032 val = xmlStringCurrentChar(NULL, cur, &len);
1034 if (!IS_CHAR(val)) {
1038 ret = xmlStrndup(q, cur - q);
1043 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1052 * @ctxt: the XPath Parser context
1054 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1055 * CombiningChar | Extender
1057 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1059 * [6] Names ::= Name (S Name)*
1061 * Returns the Name parsed or NULL
1065 xsltScanName(xsltParserContextPtr ctxt) {
1066 const xmlChar *q, *cur;
1067 xmlChar *ret = NULL;
1073 val = xmlStringCurrentChar(NULL, cur, &len);
1074 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1077 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1078 (val == '.') || (val == '-') ||
1080 (IS_COMBINING(val)) ||
1081 (IS_EXTENDER(val))) {
1083 val = xmlStringCurrentChar(NULL, cur, &len);
1085 ret = xmlStrndup(q, cur - q);
1092 * @ctxt: the XPath Parser context
1094 * Parses a non qualified name
1096 * Returns the Name parsed or NULL
1100 xsltScanNCName(xsltParserContextPtr ctxt) {
1101 const xmlChar *q, *cur;
1102 xmlChar *ret = NULL;
1108 val = xmlStringCurrentChar(NULL, cur, &len);
1109 if (!IS_LETTER(val) && (val != '_'))
1112 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1113 (val == '.') || (val == '-') ||
1115 (IS_COMBINING(val)) ||
1116 (IS_EXTENDER(val))) {
1118 val = xmlStringCurrentChar(NULL, cur, &len);
1120 ret = xmlStrndup(q, cur - q);
1127 * @ctxt: the XPath Parser context
1128 * @prefix: the place to store the prefix
1130 * Parse a qualified name
1132 * Returns the Name parsed or NULL
1136 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1137 xmlChar *ret = NULL;
1140 ret = xsltScanNCName(ctxt);
1144 ret = xsltScanNCName(ctxt);
1150 * xsltCompileIdKeyPattern:
1151 * @ctxt: the compilation context
1152 * @name: a preparsed name
1153 * @aid: whether id/key are allowed there
1155 * Compile the XSLT LocationIdKeyPattern
1156 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1157 * | 'key' '(' Literal ',' Literal ')'
1159 * also handle NodeType and PI from:
1161 * [7] NodeTest ::= NameTest
1162 * | NodeType '(' ')'
1163 * | 'processing-instruction' '(' Literal ')'
1166 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1167 xmlChar *lit = NULL;
1168 xmlChar *lit2 = NULL;
1171 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1172 xsltGenericError(xsltGenericErrorContext,
1173 "xsltCompileIdKeyPattern : ( expected\n");
1177 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1180 lit = xsltScanLiteral(ctxt);
1185 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1186 xsltGenericError(xsltGenericErrorContext,
1187 "xsltCompileIdKeyPattern : ) expected\n");
1192 PUSH(XSLT_OP_ID, lit, NULL);
1193 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1196 lit = xsltScanLiteral(ctxt);
1201 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1202 xsltGenericError(xsltGenericErrorContext,
1203 "xsltCompileIdKeyPattern : , expected\n");
1209 lit2 = xsltScanLiteral(ctxt);
1214 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1215 xsltGenericError(xsltGenericErrorContext,
1216 "xsltCompileIdKeyPattern : ) expected\n");
1221 /* TODO: support namespace in keys */
1222 PUSH(XSLT_OP_KEY, lit, lit2);
1223 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1227 lit = xsltScanLiteral(ctxt);
1232 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1233 xsltGenericError(xsltGenericErrorContext,
1234 "xsltCompileIdKeyPattern : ) expected\n");
1240 PUSH(XSLT_OP_PI, lit, NULL);
1241 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1245 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1246 xsltGenericError(xsltGenericErrorContext,
1247 "xsltCompileIdKeyPattern : ) expected\n");
1252 PUSH(XSLT_OP_TEXT, NULL, NULL);
1253 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1257 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1258 xsltGenericError(xsltGenericErrorContext,
1259 "xsltCompileIdKeyPattern : ) expected\n");
1264 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1265 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1269 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1270 xsltGenericError(xsltGenericErrorContext,
1271 "xsltCompileIdKeyPattern : ) expected\n");
1276 PUSH(XSLT_OP_NODE, NULL, NULL);
1278 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1279 xsltGenericError(xsltGenericErrorContext,
1280 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1284 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1285 xsltGenericError(xsltGenericErrorContext,
1286 "xsltCompileIdKeyPattern : node type\n");
1296 * xsltCompileStepPattern:
1297 * @ctxt: the compilation context
1298 * @token: a posible precompiled name
1300 * Compile the XSLT StepPattern and generates a precompiled
1301 * form suitable for fast matching.
1303 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1304 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1305 * | ('child' | 'attribute') '::'
1307 * [7] NodeTest ::= NameTest
1308 * | NodeType '(' ')'
1309 * | 'processing-instruction' '(' Literal ')'
1310 * [8] Predicate ::= '[' PredicateExpr ']'
1311 * [9] PredicateExpr ::= Expr
1312 * [13] AbbreviatedAxisSpecifier ::= '@'?
1313 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1317 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1318 xmlChar *name = NULL;
1319 const xmlChar *URI = NULL;
1320 xmlChar *URL = NULL;
1324 if ((token == NULL) && (CUR == '@')) {
1325 xmlChar *prefix = NULL;
1330 PUSH(XSLT_OP_ATTR, NULL, NULL);
1333 token = xsltScanQName(ctxt, &prefix);
1334 if (prefix != NULL) {
1337 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1339 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1340 xsltGenericError(xsltGenericErrorContext,
1341 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1344 URL = xmlStrdup(ns->href);
1348 if (token == NULL) {
1351 PUSH(XSLT_OP_ATTR, NULL, URL);
1354 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1355 xsltGenericError(xsltGenericErrorContext,
1356 "xsltCompileStepPattern : Name expected\n");
1360 PUSH(XSLT_OP_ATTR, token, URL);
1364 token = xsltScanName(ctxt);
1365 if (token == NULL) {
1368 PUSH(XSLT_OP_ALL, token, NULL);
1369 goto parse_predicate;
1371 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1372 xsltGenericError(xsltGenericErrorContext,
1373 "xsltCompileStepPattern : Name expected\n");
1382 xsltCompileIdKeyPattern(ctxt, token, 0);
1385 } else if (CUR == ':') {
1388 xmlChar *prefix = token;
1392 * This is a namespace match
1394 token = xsltScanName(ctxt);
1395 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1397 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1398 xsltGenericError(xsltGenericErrorContext,
1399 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1404 URL = xmlStrdup(ns->href);
1407 if (token == NULL) {
1410 PUSH(XSLT_OP_NS, URL, NULL);
1412 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1413 xsltGenericError(xsltGenericErrorContext,
1414 "xsltCompileStepPattern : Name expected\n");
1419 PUSH(XSLT_OP_ELEM, token, URL);
1423 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1425 token = xsltScanName(ctxt);
1426 if (token == NULL) {
1429 PUSH(XSLT_OP_ALL, token, NULL);
1430 goto parse_predicate;
1432 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1433 xsltGenericError(xsltGenericErrorContext,
1434 "xsltCompileStepPattern : QName expected\n");
1439 URI = xsltGetQNameURI(ctxt->elem, &token);
1440 if (token == NULL) {
1444 name = xmlStrdup(token);
1446 URL = xmlStrdup(URI);
1448 PUSH(XSLT_OP_CHILD, name, URL);
1449 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1451 token = xsltScanName(ctxt);
1452 if (token == NULL) {
1453 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1454 xsltGenericError(xsltGenericErrorContext,
1455 "xsltCompileStepPattern : QName expected\n");
1459 URI = xsltGetQNameURI(ctxt->elem, &token);
1460 if (token == NULL) {
1464 name = xmlStrdup(token);
1466 URL = xmlStrdup(URI);
1468 PUSH(XSLT_OP_ATTR, name, URL);
1470 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1471 xsltGenericError(xsltGenericErrorContext,
1472 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1478 } else if (CUR == '*') {
1480 PUSH(XSLT_OP_ALL, token, NULL);
1482 URI = xsltGetQNameURI(ctxt->elem, &token);
1483 if (token == NULL) {
1488 URL = xmlStrdup(URI);
1489 PUSH(XSLT_OP_ELEM, token, URL);
1494 while (CUR == '[') {
1496 xmlChar *ret = NULL;
1502 /* Skip over nested predicates */
1505 else if (CUR == ']') {
1509 } else if (CUR == '"') {
1511 while ((CUR != 0) && (CUR != '"'))
1513 } else if (CUR == '\'') {
1515 while ((CUR != 0) && (CUR != '\''))
1521 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1522 xsltGenericError(xsltGenericErrorContext,
1523 "xsltCompileStepPattern : ']' expected\n");
1527 ret = xmlStrndup(q, CUR_PTR - q);
1528 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1529 /* push the predicate lower than local test */
1543 * xsltCompileRelativePathPattern:
1544 * @comp: the compilation context
1545 * @token: a posible precompiled name
1547 * Compile the XSLT RelativePathPattern and generates a precompiled
1548 * form suitable for fast matching.
1550 * [4] RelativePathPattern ::= StepPattern
1551 * | RelativePathPattern '/' StepPattern
1552 * | RelativePathPattern '//' StepPattern
1555 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1556 xsltCompileStepPattern(ctxt, token);
1560 while ((CUR != 0) && (CUR != '|')) {
1561 if ((CUR == '/') && (NXT(1) == '/')) {
1562 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1566 xsltCompileStepPattern(ctxt, NULL);
1567 } else if (CUR == '/') {
1568 PUSH(XSLT_OP_PARENT, NULL, NULL);
1571 if ((CUR != 0) || (CUR == '|')) {
1572 xsltCompileRelativePathPattern(ctxt, NULL);
1586 * xsltCompileLocationPathPattern:
1587 * @ctxt: the compilation context
1589 * Compile the XSLT LocationPathPattern and generates a precompiled
1590 * form suitable for fast matching.
1592 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1593 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1594 * | '//'? RelativePathPattern
1597 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1599 if ((CUR == '/') && (NXT(1) == '/')) {
1601 * since we reverse the query
1602 * a leading // can be safely ignored
1606 xsltCompileRelativePathPattern(ctxt, NULL);
1607 } else if (CUR == '/') {
1609 * We need to find root as the parent
1613 PUSH(XSLT_OP_ROOT, NULL, NULL);
1614 if ((CUR != 0) || (CUR == '|')) {
1615 PUSH(XSLT_OP_PARENT, NULL, NULL);
1616 xsltCompileRelativePathPattern(ctxt, NULL);
1618 } else if (CUR == '*') {
1619 xsltCompileRelativePathPattern(ctxt, NULL);
1620 } else if (CUR == '@') {
1621 xsltCompileRelativePathPattern(ctxt, NULL);
1624 name = xsltScanName(ctxt);
1626 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1627 xsltGenericError(xsltGenericErrorContext,
1628 "xsltCompileLocationPathPattern : Name expected\n");
1633 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1634 xsltCompileIdKeyPattern(ctxt, name, 1);
1635 if ((CUR == '/') && (NXT(1) == '/')) {
1636 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1640 xsltCompileRelativePathPattern(ctxt, NULL);
1641 } else if (CUR == '/') {
1642 PUSH(XSLT_OP_PARENT, NULL, NULL);
1645 xsltCompileRelativePathPattern(ctxt, NULL);
1649 xsltCompileRelativePathPattern(ctxt, name);
1656 * xsltCompilePattern:
1657 * @pattern: an XSLT pattern
1658 * @doc: the containing document
1659 * @node: the containing element
1660 * @style: the stylesheet
1661 * @runtime: the transformation context, if done at run-time
1663 * Compile the XSLT pattern and generates a list of precompiled form suitable
1664 * for fast matching.
1666 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1668 * Returns the generated pattern list or NULL in case of failure
1672 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1673 xmlNodePtr node, xsltStylesheetPtr style,
1674 xsltTransformContextPtr runtime) {
1675 xsltParserContextPtr ctxt = NULL;
1676 xsltCompMatchPtr element, first = NULL, previous = NULL;
1677 int current, start, end, level, j;
1679 if (pattern == NULL) {
1680 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1681 xsltGenericError(xsltGenericErrorContext,
1682 "xsltCompilePattern : NULL pattern\n");
1686 ctxt = xsltNewParserContext(style, runtime);
1692 while (pattern[current] != 0) {
1694 while (IS_BLANK(pattern[current]))
1698 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1699 if (pattern[end] == '[')
1701 else if (pattern[end] == ']')
1703 else if (pattern[end] == '\'') {
1705 while ((pattern[end] != 0) && (pattern[end] != '\''))
1707 } else if (pattern[end] == '"') {
1709 while ((pattern[end] != 0) && (pattern[end] != '"'))
1714 if (current == end) {
1715 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1716 xsltGenericError(xsltGenericErrorContext,
1717 "xsltCompilePattern : NULL pattern\n");
1720 element = xsltNewCompMatch();
1721 if (element == NULL) {
1726 else if (previous != NULL)
1727 previous->next = element;
1730 ctxt->comp = element;
1731 ctxt->base = xmlStrndup(&pattern[start], end - start);
1732 if (ctxt->base == NULL)
1734 ctxt->cur = &(ctxt->base)[current - start];
1735 element->pattern = ctxt->base;
1736 element->nsList = xmlGetNsList(doc, node);
1738 if (element->nsList != NULL) {
1739 while (element->nsList[j] != NULL)
1745 #ifdef WITH_XSLT_DEBUG_PATTERN
1746 xsltGenericDebug(xsltGenericDebugContext,
1747 "xsltCompilePattern : parsing '%s'\n",
1750 xsltCompileLocationPathPattern(ctxt);
1755 * Reverse for faster interpretation.
1757 xsltReverseCompMatch(element);
1760 * Set-up the priority
1762 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1763 (element->steps[0].op == XSLT_OP_ATTR)) &&
1764 (element->steps[0].value != NULL) &&
1765 (element->steps[1].op == XSLT_OP_END)) {
1766 element->priority = 0;
1768 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1769 (element->steps[1].op == XSLT_OP_END)) {
1770 element->priority = 0;
1772 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1773 (element->steps[0].value != NULL) &&
1774 (element->steps[1].op == XSLT_OP_END)) {
1775 element->priority = 0;
1776 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1777 (element->steps[0].value2 != NULL) &&
1778 (element->steps[1].op == XSLT_OP_END)) {
1779 element->priority = -0.25;
1780 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1781 (element->steps[0].value != NULL) &&
1782 (element->steps[1].op == XSLT_OP_END)) {
1783 element->priority = -0.25;
1784 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1785 (element->steps[0].value == NULL) &&
1786 (element->steps[0].value2 == NULL) &&
1787 (element->steps[1].op == XSLT_OP_END)) {
1788 element->priority = -0.5;
1789 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1790 (element->steps[0].op == XSLT_OP_TEXT) ||
1791 (element->steps[0].op == XSLT_OP_ALL) ||
1792 (element->steps[0].op == XSLT_OP_NODE) ||
1793 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1794 (element->steps[1].op == XSLT_OP_END)) {
1795 element->priority = -0.5;
1797 element->priority = 0.5;
1799 #ifdef WITH_XSLT_DEBUG_PATTERN
1800 xsltGenericDebug(xsltGenericDebugContext,
1801 "xsltCompilePattern : parsed %s, default priority %f\n",
1802 element->pattern, element->priority);
1804 if (pattern[end] == '|')
1809 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1810 xsltGenericError(xsltGenericErrorContext,
1811 "xsltCompilePattern : NULL pattern\n");
1815 xsltFreeParserContext(ctxt);
1820 xsltFreeParserContext(ctxt);
1822 xsltFreeCompMatchList(first);
1826 /************************************************************************
1828 * Module interfaces *
1830 ************************************************************************/
1834 * @style: an XSLT stylesheet
1835 * @cur: an XSLT template
1836 * @mode: the mode name or NULL
1837 * @modeURI: the mode URI or NULL
1839 * Register the XSLT pattern associated to @cur
1841 * Returns -1 in case of error, 0 otherwise
1844 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1845 const xmlChar *mode, const xmlChar *modeURI) {
1846 xsltCompMatchPtr pat, list, *top = NULL, next;
1847 const xmlChar *name = NULL;
1848 float priority; /* the priority */
1850 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1853 priority = cur->priority;
1854 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1860 pat->template = cur;
1862 pat->mode = xmlStrdup(mode);
1863 if (modeURI != NULL)
1864 pat->modeURI = xmlStrdup(modeURI);
1865 if (priority != XSLT_PAT_NO_PRIORITY)
1866 pat->priority = priority;
1869 * insert it in the hash table list corresponding to its lookup name
1871 switch (pat->steps[0].op) {
1873 if (pat->steps[0].value != NULL)
1874 name = pat->steps[0].value;
1876 top = (xsltCompMatchPtr *) &(style->attrMatch);
1879 case XSLT_OP_PARENT:
1880 case XSLT_OP_ANCESTOR:
1881 top = (xsltCompMatchPtr *) &(style->elemMatch);
1884 top = (xsltCompMatchPtr *) &(style->rootMatch);
1887 top = (xsltCompMatchPtr *) &(style->keyMatch);
1890 /* TODO optimize ID !!! */
1893 top = (xsltCompMatchPtr *) &(style->elemMatch);
1896 case XSLT_OP_PREDICATE:
1897 xsltPrintErrorContext(NULL, style, NULL);
1898 xsltGenericError(xsltGenericErrorContext,
1899 "xsltAddTemplate: invalid compiled pattern\n");
1900 xsltFreeCompMatch(pat);
1903 * TODO: some flags at the top level about type based patterns
1904 * would be faster than inclusion in the hash table.
1907 if (pat->steps[0].value != NULL)
1908 name = pat->steps[0].value;
1910 top = (xsltCompMatchPtr *) &(style->piMatch);
1912 case XSLT_OP_COMMENT:
1913 top = (xsltCompMatchPtr *) &(style->commentMatch);
1916 top = (xsltCompMatchPtr *) &(style->textMatch);
1920 if (pat->steps[0].value != NULL)
1921 name = pat->steps[0].value;
1923 top = (xsltCompMatchPtr *) &(style->elemMatch);
1927 if (style->templatesHash == NULL) {
1928 style->templatesHash = xmlHashCreate(1024);
1929 if (style->templatesHash == NULL) {
1930 xsltFreeCompMatch(pat);
1933 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1935 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1936 name, mode, modeURI);
1938 xmlHashAddEntry3(style->templatesHash, name,
1939 mode, modeURI, pat);
1942 * Note '<=' since one must choose among the matching
1943 * template rules that are left, the one that occurs
1944 * last in the stylesheet
1946 if (list->priority <= pat->priority) {
1948 xmlHashUpdateEntry3(style->templatesHash, name,
1949 mode, modeURI, pat, NULL);
1951 while (list->next != NULL) {
1952 if (list->next->priority <= pat->priority)
1956 pat->next = list->next;
1961 } else if (top != NULL) {
1966 } else if (list->priority <= pat->priority) {
1970 while (list->next != NULL) {
1971 if (list->next->priority <= pat->priority)
1975 pat->next = list->next;
1979 xsltPrintErrorContext(NULL, style, NULL);
1980 xsltGenericError(xsltGenericErrorContext,
1981 "xsltAddTemplate: invalid compiled pattern\n");
1982 xsltFreeCompMatch(pat);
1985 #ifdef WITH_XSLT_DEBUG_PATTERN
1987 xsltGenericDebug(xsltGenericDebugContext,
1988 "added pattern : '%s' mode '%s' priority %f\n",
1989 pat->pattern, pat->mode, pat->priority);
1991 xsltGenericDebug(xsltGenericDebugContext,
1992 "added pattern : '%s' priority %f\n",
1993 pat->pattern, pat->priority);
2003 * @ctxt: a XSLT process context
2004 * @node: the node being processed
2005 * @style: the current style
2007 * Finds the template applying to this node, if @style is non-NULL
2008 * it means one needs to look for the next imported template in scope.
2010 * Returns the xsltTemplatePtr or NULL if not found
2013 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2014 xsltStylesheetPtr style) {
2015 xsltStylesheetPtr curstyle;
2016 xsltTemplatePtr ret = NULL;
2017 const xmlChar *name = NULL;
2018 xsltCompMatchPtr list = NULL;
2021 if ((ctxt == NULL) || (node == NULL))
2024 if (style == NULL) {
2025 curstyle = ctxt->style;
2027 curstyle = xsltNextImport(style);
2030 while ((curstyle != NULL) && (curstyle != style)) {
2031 priority = XSLT_PAT_NO_PRIORITY;
2032 /* TODO : handle IDs/keys here ! */
2033 if (curstyle->templatesHash != NULL) {
2035 * Use the top name as selector
2037 switch (node->type) {
2038 case XML_ELEMENT_NODE:
2039 case XML_ATTRIBUTE_NODE:
2043 case XML_DOCUMENT_NODE:
2044 case XML_HTML_DOCUMENT_NODE:
2046 case XML_CDATA_SECTION_NODE:
2047 case XML_COMMENT_NODE:
2048 case XML_ENTITY_REF_NODE:
2049 case XML_ENTITY_NODE:
2050 case XML_DOCUMENT_TYPE_NODE:
2051 case XML_DOCUMENT_FRAG_NODE:
2052 case XML_NOTATION_NODE:
2054 case XML_ELEMENT_DECL:
2055 case XML_ATTRIBUTE_DECL:
2056 case XML_ENTITY_DECL:
2057 case XML_NAMESPACE_DECL:
2058 case XML_XINCLUDE_START:
2059 case XML_XINCLUDE_END:
2068 * find the list of appliable expressions based on the name
2070 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2071 name, ctxt->mode, ctxt->modeURI);
2074 while (list != NULL) {
2075 if (xsltTestCompMatch(ctxt, list, node,
2076 ctxt->mode, ctxt->modeURI)) {
2077 ret = list->template;
2078 priority = list->priority;
2086 * find alternate generic matches
2088 switch (node->type) {
2089 case XML_ELEMENT_NODE:
2090 list = curstyle->elemMatch;
2092 case XML_ATTRIBUTE_NODE:
2093 list = curstyle->attrMatch;
2096 list = curstyle->piMatch;
2098 case XML_DOCUMENT_NODE:
2099 case XML_HTML_DOCUMENT_NODE:
2100 list = curstyle->rootMatch;
2103 case XML_CDATA_SECTION_NODE:
2104 list = curstyle->textMatch;
2106 case XML_COMMENT_NODE:
2107 list = curstyle->commentMatch;
2109 case XML_ENTITY_REF_NODE:
2110 case XML_ENTITY_NODE:
2111 case XML_DOCUMENT_TYPE_NODE:
2112 case XML_DOCUMENT_FRAG_NODE:
2113 case XML_NOTATION_NODE:
2115 case XML_ELEMENT_DECL:
2116 case XML_ATTRIBUTE_DECL:
2117 case XML_ENTITY_DECL:
2118 case XML_NAMESPACE_DECL:
2119 case XML_XINCLUDE_START:
2120 case XML_XINCLUDE_END:
2126 while ((list != NULL) &&
2127 ((ret == NULL) || (list->priority > priority))) {
2128 if (xsltTestCompMatch(ctxt, list, node,
2129 ctxt->mode, ctxt->modeURI)) {
2130 ret = list->template;
2131 priority = list->priority;
2137 * Some of the tests for elements can also apply to documents
2139 if ((node->type == XML_DOCUMENT_NODE) ||
2140 (node->type == XML_HTML_DOCUMENT_NODE)) {
2141 list = curstyle->elemMatch;
2142 while ((list != NULL) &&
2143 ((ret == NULL) || (list->priority > priority))) {
2144 if (xsltTestCompMatch(ctxt, list, node,
2145 ctxt->mode, ctxt->modeURI)) {
2146 ret = list->template;
2147 priority = list->priority;
2154 if (node->_private != NULL) {
2155 list = curstyle->keyMatch;
2156 while ((list != NULL) &&
2157 ((ret == NULL) || (list->priority > priority))) {
2158 if (xsltTestCompMatch(ctxt, list, node,
2159 ctxt->mode, ctxt->modeURI)) {
2160 ret = list->template;
2161 priority = list->priority;
2171 * Cycle on next curstylesheet import.
2173 curstyle = xsltNextImport(curstyle);
2179 * xsltCleanupTemplates:
2180 * @style: an XSLT stylesheet
2182 * Cleanup the state of the templates used by the stylesheet and
2183 * the ones it imports.
2186 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2190 * xsltFreeTemplateHashes:
2191 * @style: an XSLT stylesheet
2193 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2196 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2197 if (style->templatesHash != NULL)
2198 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2199 (xmlHashDeallocator) xsltFreeCompMatchList);
2200 if (style->rootMatch != NULL)
2201 xsltFreeCompMatchList(style->rootMatch);
2202 if (style->keyMatch != NULL)
2203 xsltFreeCompMatchList(style->keyMatch);
2204 if (style->elemMatch != NULL)
2205 xsltFreeCompMatchList(style->elemMatch);
2206 if (style->attrMatch != NULL)
2207 xsltFreeCompMatchList(style->attrMatch);
2208 if (style->parentMatch != NULL)
2209 xsltFreeCompMatchList(style->parentMatch);
2210 if (style->textMatch != NULL)
2211 xsltFreeCompMatchList(style->textMatch);
2212 if (style->piMatch != NULL)
2213 xsltFreeCompMatchList(style->piMatch);
2214 if (style->commentMatch != NULL)
2215 xsltFreeCompMatchList(style->commentMatch);
2221 * @node: a node in the source tree
2222 * @pattern: an XSLT pattern
2223 * @ctxtdoc: context document (for namespaces)
2224 * @ctxtnode: context node (for namespaces)
2226 * Determine if a node matches a pattern.
2229 xsltMatchPattern(xsltTransformContextPtr context,
2231 const xmlChar *pattern,
2233 xmlNodePtr ctxtnode)
2236 xsltCompMatchPtr first, comp;
2238 if ((context != NULL) && (pattern != NULL)) {
2239 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2240 for (comp = first; comp != NULL; comp = comp->next) {
2241 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2246 xsltFreeCompMatchList(first);