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) {
600 xmlXPathObjectPtr list;
603 previous = (xmlNodePtr)
604 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
606 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
607 list = (xmlXPathObjectPtr)
608 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
612 if (comp->pattern[0] == '/')
613 query = xmlStrdup(comp->pattern);
615 query = xmlStrdup((const xmlChar *)"//");
616 query = xmlStrcat(query, comp->pattern);
618 list = xmlXPathEval(query, ctxt->xpathCtxt);
622 if (list->type != XPATH_NODESET) {
623 xmlXPathFreeObject(list);
626 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
628 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
629 (xmlFreeFunc) xmlXPathFreeObject;
631 if ((list->nodesetval == NULL) ||
632 (list->nodesetval->nodeNr <= 0))
635 for (j = 0;j < list->nodesetval->nodeNr;j++) {
636 if (list->nodesetval->nodeTab[j] == node) {
645 * Depending on the last selection, one may need to
646 * recompute contextSize and proximityPosition.
648 * TODO: make this thread safe !
650 oldCS = ctxt->xpathCtxt->contextSize;
651 oldCP = ctxt->xpathCtxt->proximityPosition;
652 if ((select != NULL) &&
653 (select->op == XSLT_OP_ELEM) &&
654 (select->value != NULL) &&
655 (node->type == XML_ELEMENT_NODE) &&
656 (node->parent != NULL)) {
658 int index, nocache = 0;
660 previous = (xmlNodePtr)
661 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
663 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
664 if ((previous != NULL) &&
665 (previous->parent == node->parent)) {
667 * just walk back to adjust the index
670 xmlNodePtr sibling = node;
672 while (sibling != NULL) {
673 if (sibling == previous)
675 if ((node->type == XML_ELEMENT_NODE) &&
676 (node->name[0] == sibling->name[0]) &&
677 (xmlStrEqual(node->name, sibling->name))) {
678 if ((select->value2 == NULL) ||
679 ((sibling->ns != NULL) &&
680 (xmlStrEqual(select->value2,
681 sibling->ns->href))))
684 sibling = sibling->prev;
686 if (sibling == NULL) {
687 /* hum going backward in document order ... */
690 while (sibling != NULL) {
691 if (sibling == previous)
693 if ((select->value2 == NULL) ||
694 ((sibling->ns != NULL) &&
695 (xmlStrEqual(select->value2,
696 sibling->ns->href))))
698 sibling = sibling->next;
701 if (sibling != NULL) {
704 * If the node is in a Value Tree we cannot
707 if (node->doc != NULL) {
709 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
710 XSLT_RUNTIME_EXTRA(ctxt,
711 select->previousExtra) = node;
712 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
720 * recompute the index
722 xmlNodePtr siblings = node->parent->children;
723 xmlNodePtr parent = node->parent;
725 while (siblings != NULL) {
726 if (siblings->type == XML_ELEMENT_NODE) {
727 if (siblings == node) {
730 } else if ((node->name[0] == siblings->name[0])
731 && (xmlStrEqual(node->name, siblings->name))) {
732 if ((select->value2 == NULL) ||
733 ((siblings->ns != NULL) &&
734 (xmlStrEqual(select->value2,
735 siblings->ns->href))))
739 siblings = siblings->next;
741 if ((parent == NULL) || (node->doc == NULL))
744 while (parent->parent != NULL)
745 parent = parent->parent;
746 if (((parent->type != XML_DOCUMENT_NODE) &&
747 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
748 (parent != (xmlNodePtr) node->doc))
753 ctxt->xpathCtxt->contextSize = len;
754 ctxt->xpathCtxt->proximityPosition = pos;
756 * If the node is in a Value Tree we cannot
759 if ((node->doc != NULL) && (nocache == 0)) {
760 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
762 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
764 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
768 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
769 (node->type == XML_ELEMENT_NODE)) {
771 int index, nocache = 0;
773 previous = (xmlNodePtr)
774 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
776 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
777 if ((previous != NULL) &&
778 (previous->parent == node->parent)) {
780 * just walk back to adjust the index
783 xmlNodePtr sibling = node;
785 while (sibling != NULL) {
786 if (sibling == previous)
788 if (sibling->type == XML_ELEMENT_NODE)
790 sibling = sibling->prev;
792 if (sibling == NULL) {
793 /* hum going backward in document order ... */
796 while (sibling != NULL) {
797 if (sibling == previous)
799 if (sibling->type == XML_ELEMENT_NODE)
801 sibling = sibling->next;
804 if (sibling != NULL) {
807 * If the node is in a Value Tree we cannot
810 if (node->doc != NULL) {
812 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
813 XSLT_RUNTIME_EXTRA(ctxt,
814 select->previousExtra) = node;
815 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
822 * recompute the index
824 xmlNodePtr siblings = node->parent->children;
825 xmlNodePtr parent = node->parent;
827 while (siblings != NULL) {
828 if (siblings->type == XML_ELEMENT_NODE) {
830 if (siblings == node) {
834 siblings = siblings->next;
836 if ((parent == NULL) || (node->doc == NULL))
839 while (parent->parent != NULL)
840 parent = parent->parent;
841 if (((parent->type != XML_DOCUMENT_NODE) &&
842 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
843 (parent != (xmlNodePtr) node->doc))
848 ctxt->xpathCtxt->contextSize = len;
849 ctxt->xpathCtxt->proximityPosition = pos;
851 * If the node is in a Value Tree we cannot
854 if ((node->doc != NULL) && (nocache == 0)) {
855 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
857 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
859 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
864 oldNode = ctxt->node;
867 if (step->value == NULL)
869 if (step->comp == NULL)
872 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
877 ctxt->xpathCtxt->contextSize = oldCS;
878 ctxt->xpathCtxt->proximityPosition = oldCP;
880 ctxt->node = oldNode;
884 ctxt->xpathCtxt->contextSize = oldCS;
885 ctxt->xpathCtxt->proximityPosition = oldCP;
887 ctxt->node = oldNode;
891 if (node->type != XML_PI_NODE)
893 if (step->value != NULL) {
894 if (!xmlStrEqual(step->value, node->name))
898 case XSLT_OP_COMMENT:
899 if (node->type != XML_COMMENT_NODE)
903 if ((node->type != XML_TEXT_NODE) &&
904 (node->type != XML_CDATA_SECTION_NODE))
908 switch (node->type) {
909 case XML_DOCUMENT_NODE:
910 case XML_HTML_DOCUMENT_NODE:
911 #ifdef LIBXML_DOCB_ENABLED
912 case XML_DOCB_DOCUMENT_NODE:
914 case XML_ELEMENT_NODE:
915 case XML_CDATA_SECTION_NODE:
917 case XML_COMMENT_NODE:
919 case XML_ATTRIBUTE_NODE:
931 * xsltTestCompMatchList:
932 * @ctxt: a XSLT process context
934 * @comp: the precompiled pattern list
936 * Test wether the node matches one of the patterns in the list
938 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
941 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
942 xsltCompMatchPtr comp) {
945 if ((ctxt == NULL) || (node == NULL))
947 while (comp != NULL) {
948 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
956 /************************************************************************
958 * Dedicated parser for templates *
960 ************************************************************************/
962 #define CUR (*ctxt->cur)
963 #define SKIP(val) ctxt->cur += (val)
964 #define NXT(val) ctxt->cur[(val)]
965 #define CUR_PTR ctxt->cur
967 #define SKIP_BLANKS \
968 while (IS_BLANK(CUR)) NEXT
970 #define CURRENT (*ctxt->cur)
971 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
974 #define PUSH(op, val, val2) \
975 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
978 xsltSwapTopCompMatch(ctxt->comp);
980 #define XSLT_ERROR(X) \
981 { xsltError(ctxt, __FILE__, __LINE__, X); \
982 ctxt->error = (X); return; }
984 #define XSLT_ERROR0(X) \
985 { xsltError(ctxt, __FILE__, __LINE__, X); \
986 ctxt->error = (X); return(0); }
990 * @ctxt: the XPath Parser context
992 * Parse an XPath Litteral:
994 * [29] Literal ::= '"' [^"]* '"'
997 * Returns the Literal parsed or NULL
1001 xsltScanLiteral(xsltParserContextPtr ctxt) {
1002 const xmlChar *q, *cur;
1003 xmlChar *ret = NULL;
1010 val = xmlStringCurrentChar(NULL, cur, &len);
1011 while ((IS_CHAR(val)) && (val != '"')) {
1013 val = xmlStringCurrentChar(NULL, cur, &len);
1015 if (!IS_CHAR(val)) {
1019 ret = xmlStrndup(q, cur - q);
1023 } else if (CUR == '\'') {
1026 val = xmlStringCurrentChar(NULL, cur, &len);
1027 while ((IS_CHAR(val)) && (val != '\'')) {
1029 val = xmlStringCurrentChar(NULL, cur, &len);
1031 if (!IS_CHAR(val)) {
1035 ret = xmlStrndup(q, cur - q);
1040 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1049 * @ctxt: the XPath Parser context
1051 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1052 * CombiningChar | Extender
1054 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1056 * [6] Names ::= Name (S Name)*
1058 * Returns the Name parsed or NULL
1062 xsltScanName(xsltParserContextPtr ctxt) {
1063 const xmlChar *q, *cur;
1064 xmlChar *ret = NULL;
1070 val = xmlStringCurrentChar(NULL, cur, &len);
1071 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1074 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1075 (val == '.') || (val == '-') ||
1077 (IS_COMBINING(val)) ||
1078 (IS_EXTENDER(val))) {
1080 val = xmlStringCurrentChar(NULL, cur, &len);
1082 ret = xmlStrndup(q, cur - q);
1089 * @ctxt: the XPath Parser context
1091 * Parses a non qualified name
1093 * Returns the Name parsed or NULL
1097 xsltScanNCName(xsltParserContextPtr ctxt) {
1098 const xmlChar *q, *cur;
1099 xmlChar *ret = NULL;
1105 val = xmlStringCurrentChar(NULL, cur, &len);
1106 if (!IS_LETTER(val) && (val != '_'))
1109 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1110 (val == '.') || (val == '-') ||
1112 (IS_COMBINING(val)) ||
1113 (IS_EXTENDER(val))) {
1115 val = xmlStringCurrentChar(NULL, cur, &len);
1117 ret = xmlStrndup(q, cur - q);
1124 * @ctxt: the XPath Parser context
1125 * @prefix: the place to store the prefix
1127 * Parse a qualified name
1129 * Returns the Name parsed or NULL
1133 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1134 xmlChar *ret = NULL;
1137 ret = xsltScanNCName(ctxt);
1141 ret = xsltScanNCName(ctxt);
1147 * xsltCompileIdKeyPattern:
1148 * @ctxt: the compilation context
1149 * @name: a preparsed name
1150 * @aid: whether id/key are allowed there
1152 * Compile the XSLT LocationIdKeyPattern
1153 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1154 * | 'key' '(' Literal ',' Literal ')'
1156 * also handle NodeType and PI from:
1158 * [7] NodeTest ::= NameTest
1159 * | NodeType '(' ')'
1160 * | 'processing-instruction' '(' Literal ')'
1163 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1164 xmlChar *lit = NULL;
1165 xmlChar *lit2 = NULL;
1168 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1169 xsltGenericError(xsltGenericErrorContext,
1170 "xsltCompileIdKeyPattern : ( expected\n");
1174 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1177 lit = xsltScanLiteral(ctxt);
1182 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1183 xsltGenericError(xsltGenericErrorContext,
1184 "xsltCompileIdKeyPattern : ) expected\n");
1189 PUSH(XSLT_OP_ID, lit, NULL);
1190 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1193 lit = xsltScanLiteral(ctxt);
1198 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1199 xsltGenericError(xsltGenericErrorContext,
1200 "xsltCompileIdKeyPattern : , expected\n");
1206 lit2 = xsltScanLiteral(ctxt);
1211 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1212 xsltGenericError(xsltGenericErrorContext,
1213 "xsltCompileIdKeyPattern : ) expected\n");
1218 /* TODO: support namespace in keys */
1219 PUSH(XSLT_OP_KEY, lit, lit2);
1220 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1224 lit = xsltScanLiteral(ctxt);
1229 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1230 xsltGenericError(xsltGenericErrorContext,
1231 "xsltCompileIdKeyPattern : ) expected\n");
1237 PUSH(XSLT_OP_PI, lit, NULL);
1238 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1242 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1243 xsltGenericError(xsltGenericErrorContext,
1244 "xsltCompileIdKeyPattern : ) expected\n");
1249 PUSH(XSLT_OP_TEXT, NULL, NULL);
1250 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1254 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1255 xsltGenericError(xsltGenericErrorContext,
1256 "xsltCompileIdKeyPattern : ) expected\n");
1261 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1262 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1266 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1267 xsltGenericError(xsltGenericErrorContext,
1268 "xsltCompileIdKeyPattern : ) expected\n");
1273 PUSH(XSLT_OP_NODE, NULL, NULL);
1275 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1276 xsltGenericError(xsltGenericErrorContext,
1277 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1281 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1282 xsltGenericError(xsltGenericErrorContext,
1283 "xsltCompileIdKeyPattern : node type\n");
1293 * xsltCompileStepPattern:
1294 * @ctxt: the compilation context
1295 * @token: a posible precompiled name
1297 * Compile the XSLT StepPattern and generates a precompiled
1298 * form suitable for fast matching.
1300 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1301 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1302 * | ('child' | 'attribute') '::'
1304 * [7] NodeTest ::= NameTest
1305 * | NodeType '(' ')'
1306 * | 'processing-instruction' '(' Literal ')'
1307 * [8] Predicate ::= '[' PredicateExpr ']'
1308 * [9] PredicateExpr ::= Expr
1309 * [13] AbbreviatedAxisSpecifier ::= '@'?
1310 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1314 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1315 xmlChar *name = NULL;
1316 const xmlChar *URI = NULL;
1317 xmlChar *URL = NULL;
1321 if ((token == NULL) && (CUR == '@')) {
1322 xmlChar *prefix = NULL;
1327 PUSH(XSLT_OP_ATTR, NULL, NULL);
1330 token = xsltScanQName(ctxt, &prefix);
1331 if (prefix != NULL) {
1334 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1336 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1337 xsltGenericError(xsltGenericErrorContext,
1338 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1341 URL = xmlStrdup(ns->href);
1345 if (token == NULL) {
1348 PUSH(XSLT_OP_ATTR, NULL, URL);
1351 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1352 xsltGenericError(xsltGenericErrorContext,
1353 "xsltCompileStepPattern : Name expected\n");
1357 PUSH(XSLT_OP_ATTR, token, URL);
1358 goto parse_predicate;
1361 token = xsltScanName(ctxt);
1362 if (token == NULL) {
1365 PUSH(XSLT_OP_ALL, token, NULL);
1366 goto parse_predicate;
1368 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1369 xsltGenericError(xsltGenericErrorContext,
1370 "xsltCompileStepPattern : Name expected\n");
1379 xsltCompileIdKeyPattern(ctxt, token, 0);
1382 } else if (CUR == ':') {
1385 xmlChar *prefix = token;
1389 * This is a namespace match
1391 token = xsltScanName(ctxt);
1392 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1394 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1395 xsltGenericError(xsltGenericErrorContext,
1396 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1401 URL = xmlStrdup(ns->href);
1404 if (token == NULL) {
1407 PUSH(XSLT_OP_NS, URL, NULL);
1409 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1410 xsltGenericError(xsltGenericErrorContext,
1411 "xsltCompileStepPattern : Name expected\n");
1416 PUSH(XSLT_OP_ELEM, token, URL);
1420 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1422 token = xsltScanName(ctxt);
1423 if (token == NULL) {
1426 PUSH(XSLT_OP_ALL, token, NULL);
1427 goto parse_predicate;
1429 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1430 xsltGenericError(xsltGenericErrorContext,
1431 "xsltCompileStepPattern : QName expected\n");
1436 URI = xsltGetQNameURI(ctxt->elem, &token);
1437 if (token == NULL) {
1441 name = xmlStrdup(token);
1443 URL = xmlStrdup(URI);
1445 PUSH(XSLT_OP_CHILD, name, URL);
1446 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1448 token = xsltScanName(ctxt);
1449 if (token == NULL) {
1450 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1451 xsltGenericError(xsltGenericErrorContext,
1452 "xsltCompileStepPattern : QName expected\n");
1456 URI = xsltGetQNameURI(ctxt->elem, &token);
1457 if (token == NULL) {
1461 name = xmlStrdup(token);
1463 URL = xmlStrdup(URI);
1465 PUSH(XSLT_OP_ATTR, name, URL);
1467 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1468 xsltGenericError(xsltGenericErrorContext,
1469 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1475 } else if (CUR == '*') {
1477 PUSH(XSLT_OP_ALL, token, NULL);
1479 URI = xsltGetQNameURI(ctxt->elem, &token);
1480 if (token == NULL) {
1485 URL = xmlStrdup(URI);
1486 PUSH(XSLT_OP_ELEM, token, URL);
1491 while (CUR == '[') {
1493 xmlChar *ret = NULL;
1499 /* Skip over nested predicates */
1502 else if (CUR == ']') {
1506 } else if (CUR == '"') {
1508 while ((CUR != 0) && (CUR != '"'))
1510 } else if (CUR == '\'') {
1512 while ((CUR != 0) && (CUR != '\''))
1518 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1519 xsltGenericError(xsltGenericErrorContext,
1520 "xsltCompileStepPattern : ']' expected\n");
1524 ret = xmlStrndup(q, CUR_PTR - q);
1525 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1526 /* push the predicate lower than local test */
1540 * xsltCompileRelativePathPattern:
1541 * @comp: the compilation context
1542 * @token: a posible precompiled name
1544 * Compile the XSLT RelativePathPattern and generates a precompiled
1545 * form suitable for fast matching.
1547 * [4] RelativePathPattern ::= StepPattern
1548 * | RelativePathPattern '/' StepPattern
1549 * | RelativePathPattern '//' StepPattern
1552 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1553 xsltCompileStepPattern(ctxt, token);
1557 while ((CUR != 0) && (CUR != '|')) {
1558 if ((CUR == '/') && (NXT(1) == '/')) {
1559 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1563 xsltCompileStepPattern(ctxt, NULL);
1564 } else if (CUR == '/') {
1565 PUSH(XSLT_OP_PARENT, NULL, NULL);
1568 if ((CUR != 0) || (CUR == '|')) {
1569 xsltCompileRelativePathPattern(ctxt, NULL);
1583 * xsltCompileLocationPathPattern:
1584 * @ctxt: the compilation context
1586 * Compile the XSLT LocationPathPattern and generates a precompiled
1587 * form suitable for fast matching.
1589 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1590 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1591 * | '//'? RelativePathPattern
1594 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1596 if ((CUR == '/') && (NXT(1) == '/')) {
1598 * since we reverse the query
1599 * a leading // can be safely ignored
1603 xsltCompileRelativePathPattern(ctxt, NULL);
1604 } else if (CUR == '/') {
1606 * We need to find root as the parent
1610 PUSH(XSLT_OP_ROOT, NULL, NULL);
1611 if ((CUR != 0) || (CUR == '|')) {
1612 PUSH(XSLT_OP_PARENT, NULL, NULL);
1613 xsltCompileRelativePathPattern(ctxt, NULL);
1615 } else if (CUR == '*') {
1616 xsltCompileRelativePathPattern(ctxt, NULL);
1617 } else if (CUR == '@') {
1618 xsltCompileRelativePathPattern(ctxt, NULL);
1621 name = xsltScanName(ctxt);
1623 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1624 xsltGenericError(xsltGenericErrorContext,
1625 "xsltCompileLocationPathPattern : Name expected\n");
1630 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1631 xsltCompileIdKeyPattern(ctxt, name, 1);
1632 if ((CUR == '/') && (NXT(1) == '/')) {
1633 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1637 xsltCompileRelativePathPattern(ctxt, NULL);
1638 } else if (CUR == '/') {
1639 PUSH(XSLT_OP_PARENT, NULL, NULL);
1642 xsltCompileRelativePathPattern(ctxt, NULL);
1646 xsltCompileRelativePathPattern(ctxt, name);
1653 * xsltCompilePattern:
1654 * @pattern: an XSLT pattern
1655 * @doc: the containing document
1656 * @node: the containing element
1657 * @style: the stylesheet
1658 * @runtime: the transformation context, if done at run-time
1660 * Compile the XSLT pattern and generates a list of precompiled form suitable
1661 * for fast matching.
1663 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1665 * Returns the generated pattern list or NULL in case of failure
1669 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1670 xmlNodePtr node, xsltStylesheetPtr style,
1671 xsltTransformContextPtr runtime) {
1672 xsltParserContextPtr ctxt = NULL;
1673 xsltCompMatchPtr element, first = NULL, previous = NULL;
1674 int current, start, end, level, j;
1676 if (pattern == NULL) {
1677 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1678 xsltGenericError(xsltGenericErrorContext,
1679 "xsltCompilePattern : NULL pattern\n");
1683 ctxt = xsltNewParserContext(style, runtime);
1689 while (pattern[current] != 0) {
1691 while (IS_BLANK(pattern[current]))
1695 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1696 if (pattern[end] == '[')
1698 else if (pattern[end] == ']')
1700 else if (pattern[end] == '\'') {
1702 while ((pattern[end] != 0) && (pattern[end] != '\''))
1704 } else if (pattern[end] == '"') {
1706 while ((pattern[end] != 0) && (pattern[end] != '"'))
1711 if (current == end) {
1712 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1713 xsltGenericError(xsltGenericErrorContext,
1714 "xsltCompilePattern : NULL pattern\n");
1717 element = xsltNewCompMatch();
1718 if (element == NULL) {
1723 else if (previous != NULL)
1724 previous->next = element;
1727 ctxt->comp = element;
1728 ctxt->base = xmlStrndup(&pattern[start], end - start);
1729 if (ctxt->base == NULL)
1731 ctxt->cur = &(ctxt->base)[current - start];
1732 element->pattern = ctxt->base;
1733 element->nsList = xmlGetNsList(doc, node);
1735 if (element->nsList != NULL) {
1736 while (element->nsList[j] != NULL)
1742 #ifdef WITH_XSLT_DEBUG_PATTERN
1743 xsltGenericDebug(xsltGenericDebugContext,
1744 "xsltCompilePattern : parsing '%s'\n",
1747 xsltCompileLocationPathPattern(ctxt);
1749 xsltPrintErrorContext(NULL, style, node);
1750 xsltGenericError(xsltGenericErrorContext,
1751 "xsltCompilePattern : failed to compile '%s'\n",
1758 * Reverse for faster interpretation.
1760 xsltReverseCompMatch(element);
1763 * Set-up the priority
1765 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1766 (element->steps[0].op == XSLT_OP_ATTR)) &&
1767 (element->steps[0].value != NULL) &&
1768 (element->steps[1].op == XSLT_OP_END)) {
1769 element->priority = 0;
1771 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1772 (element->steps[1].op == XSLT_OP_END)) {
1773 element->priority = 0;
1775 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1776 (element->steps[0].value != NULL) &&
1777 (element->steps[1].op == XSLT_OP_END)) {
1778 element->priority = 0;
1779 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1780 (element->steps[0].value2 != NULL) &&
1781 (element->steps[1].op == XSLT_OP_END)) {
1782 element->priority = -0.25;
1783 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1784 (element->steps[0].value != NULL) &&
1785 (element->steps[1].op == XSLT_OP_END)) {
1786 element->priority = -0.25;
1787 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1788 (element->steps[0].value == NULL) &&
1789 (element->steps[0].value2 == NULL) &&
1790 (element->steps[1].op == XSLT_OP_END)) {
1791 element->priority = -0.5;
1792 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1793 (element->steps[0].op == XSLT_OP_TEXT) ||
1794 (element->steps[0].op == XSLT_OP_ALL) ||
1795 (element->steps[0].op == XSLT_OP_NODE) ||
1796 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1797 (element->steps[1].op == XSLT_OP_END)) {
1798 element->priority = -0.5;
1800 element->priority = 0.5;
1802 #ifdef WITH_XSLT_DEBUG_PATTERN
1803 xsltGenericDebug(xsltGenericDebugContext,
1804 "xsltCompilePattern : parsed %s, default priority %f\n",
1805 element->pattern, element->priority);
1807 if (pattern[end] == '|')
1812 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1813 xsltGenericError(xsltGenericErrorContext,
1814 "xsltCompilePattern : NULL pattern\n");
1819 xsltFreeParserContext(ctxt);
1824 xsltFreeParserContext(ctxt);
1826 xsltFreeCompMatchList(first);
1830 /************************************************************************
1832 * Module interfaces *
1834 ************************************************************************/
1838 * @style: an XSLT stylesheet
1839 * @cur: an XSLT template
1840 * @mode: the mode name or NULL
1841 * @modeURI: the mode URI or NULL
1843 * Register the XSLT pattern associated to @cur
1845 * Returns -1 in case of error, 0 otherwise
1848 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1849 const xmlChar *mode, const xmlChar *modeURI) {
1850 xsltCompMatchPtr pat, list, *top = NULL, next;
1851 const xmlChar *name = NULL;
1852 float priority; /* the priority */
1854 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1857 priority = cur->priority;
1858 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1864 pat->template = cur;
1866 pat->mode = xmlStrdup(mode);
1867 if (modeURI != NULL)
1868 pat->modeURI = xmlStrdup(modeURI);
1869 if (priority != XSLT_PAT_NO_PRIORITY)
1870 pat->priority = priority;
1873 * insert it in the hash table list corresponding to its lookup name
1875 switch (pat->steps[0].op) {
1877 if (pat->steps[0].value != NULL)
1878 name = pat->steps[0].value;
1880 top = (xsltCompMatchPtr *) &(style->attrMatch);
1883 case XSLT_OP_PARENT:
1884 case XSLT_OP_ANCESTOR:
1885 top = (xsltCompMatchPtr *) &(style->elemMatch);
1888 top = (xsltCompMatchPtr *) &(style->rootMatch);
1891 top = (xsltCompMatchPtr *) &(style->keyMatch);
1894 /* TODO optimize ID !!! */
1897 top = (xsltCompMatchPtr *) &(style->elemMatch);
1900 case XSLT_OP_PREDICATE:
1901 xsltPrintErrorContext(NULL, style, NULL);
1902 xsltGenericError(xsltGenericErrorContext,
1903 "xsltAddTemplate: invalid compiled pattern\n");
1904 xsltFreeCompMatch(pat);
1907 * TODO: some flags at the top level about type based patterns
1908 * would be faster than inclusion in the hash table.
1911 if (pat->steps[0].value != NULL)
1912 name = pat->steps[0].value;
1914 top = (xsltCompMatchPtr *) &(style->piMatch);
1916 case XSLT_OP_COMMENT:
1917 top = (xsltCompMatchPtr *) &(style->commentMatch);
1920 top = (xsltCompMatchPtr *) &(style->textMatch);
1924 if (pat->steps[0].value != NULL)
1925 name = pat->steps[0].value;
1927 top = (xsltCompMatchPtr *) &(style->elemMatch);
1931 if (style->templatesHash == NULL) {
1932 style->templatesHash = xmlHashCreate(1024);
1933 if (style->templatesHash == NULL) {
1934 xsltFreeCompMatch(pat);
1937 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1939 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1940 name, mode, modeURI);
1942 xmlHashAddEntry3(style->templatesHash, name,
1943 mode, modeURI, pat);
1946 * Note '<=' since one must choose among the matching
1947 * template rules that are left, the one that occurs
1948 * last in the stylesheet
1950 if (list->priority <= pat->priority) {
1952 xmlHashUpdateEntry3(style->templatesHash, name,
1953 mode, modeURI, pat, NULL);
1955 while (list->next != NULL) {
1956 if (list->next->priority <= pat->priority)
1960 pat->next = list->next;
1965 } else if (top != NULL) {
1970 } else if (list->priority <= pat->priority) {
1974 while (list->next != NULL) {
1975 if (list->next->priority <= pat->priority)
1979 pat->next = list->next;
1983 xsltPrintErrorContext(NULL, style, NULL);
1984 xsltGenericError(xsltGenericErrorContext,
1985 "xsltAddTemplate: invalid compiled pattern\n");
1986 xsltFreeCompMatch(pat);
1989 #ifdef WITH_XSLT_DEBUG_PATTERN
1991 xsltGenericDebug(xsltGenericDebugContext,
1992 "added pattern : '%s' mode '%s' priority %f\n",
1993 pat->pattern, pat->mode, pat->priority);
1995 xsltGenericDebug(xsltGenericDebugContext,
1996 "added pattern : '%s' priority %f\n",
1997 pat->pattern, pat->priority);
2007 * @ctxt: a XSLT process context
2008 * @node: the node being processed
2009 * @style: the current style
2011 * Finds the template applying to this node, if @style is non-NULL
2012 * it means one needs to look for the next imported template in scope.
2014 * Returns the xsltTemplatePtr or NULL if not found
2017 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2018 xsltStylesheetPtr style) {
2019 xsltStylesheetPtr curstyle;
2020 xsltTemplatePtr ret = NULL;
2021 const xmlChar *name = NULL;
2022 xsltCompMatchPtr list = NULL;
2025 if ((ctxt == NULL) || (node == NULL))
2028 if (style == NULL) {
2029 curstyle = ctxt->style;
2031 curstyle = xsltNextImport(style);
2034 while ((curstyle != NULL) && (curstyle != style)) {
2035 priority = XSLT_PAT_NO_PRIORITY;
2036 /* TODO : handle IDs/keys here ! */
2037 if (curstyle->templatesHash != NULL) {
2039 * Use the top name as selector
2041 switch (node->type) {
2042 case XML_ELEMENT_NODE:
2043 case XML_ATTRIBUTE_NODE:
2047 case XML_DOCUMENT_NODE:
2048 case XML_HTML_DOCUMENT_NODE:
2050 case XML_CDATA_SECTION_NODE:
2051 case XML_COMMENT_NODE:
2052 case XML_ENTITY_REF_NODE:
2053 case XML_ENTITY_NODE:
2054 case XML_DOCUMENT_TYPE_NODE:
2055 case XML_DOCUMENT_FRAG_NODE:
2056 case XML_NOTATION_NODE:
2058 case XML_ELEMENT_DECL:
2059 case XML_ATTRIBUTE_DECL:
2060 case XML_ENTITY_DECL:
2061 case XML_NAMESPACE_DECL:
2062 case XML_XINCLUDE_START:
2063 case XML_XINCLUDE_END:
2072 * find the list of appliable expressions based on the name
2074 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2075 name, ctxt->mode, ctxt->modeURI);
2078 while (list != NULL) {
2079 if (xsltTestCompMatch(ctxt, list, node,
2080 ctxt->mode, ctxt->modeURI)) {
2081 ret = list->template;
2082 priority = list->priority;
2090 * find alternate generic matches
2092 switch (node->type) {
2093 case XML_ELEMENT_NODE:
2094 list = curstyle->elemMatch;
2096 case XML_ATTRIBUTE_NODE:
2097 list = curstyle->attrMatch;
2100 list = curstyle->piMatch;
2102 case XML_DOCUMENT_NODE:
2103 case XML_HTML_DOCUMENT_NODE:
2104 list = curstyle->rootMatch;
2107 case XML_CDATA_SECTION_NODE:
2108 list = curstyle->textMatch;
2110 case XML_COMMENT_NODE:
2111 list = curstyle->commentMatch;
2113 case XML_ENTITY_REF_NODE:
2114 case XML_ENTITY_NODE:
2115 case XML_DOCUMENT_TYPE_NODE:
2116 case XML_DOCUMENT_FRAG_NODE:
2117 case XML_NOTATION_NODE:
2119 case XML_ELEMENT_DECL:
2120 case XML_ATTRIBUTE_DECL:
2121 case XML_ENTITY_DECL:
2122 case XML_NAMESPACE_DECL:
2123 case XML_XINCLUDE_START:
2124 case XML_XINCLUDE_END:
2130 while ((list != NULL) &&
2131 ((ret == NULL) || (list->priority > priority))) {
2132 if (xsltTestCompMatch(ctxt, list, node,
2133 ctxt->mode, ctxt->modeURI)) {
2134 ret = list->template;
2135 priority = list->priority;
2141 * Some of the tests for elements can also apply to documents
2143 if ((node->type == XML_DOCUMENT_NODE) ||
2144 (node->type == XML_HTML_DOCUMENT_NODE)) {
2145 list = curstyle->elemMatch;
2146 while ((list != NULL) &&
2147 ((ret == NULL) || (list->priority > priority))) {
2148 if (xsltTestCompMatch(ctxt, list, node,
2149 ctxt->mode, ctxt->modeURI)) {
2150 ret = list->template;
2151 priority = list->priority;
2158 if (node->_private != NULL) {
2159 list = curstyle->keyMatch;
2160 while ((list != NULL) &&
2161 ((ret == NULL) || (list->priority > priority))) {
2162 if (xsltTestCompMatch(ctxt, list, node,
2163 ctxt->mode, ctxt->modeURI)) {
2164 ret = list->template;
2165 priority = list->priority;
2175 * Cycle on next curstylesheet import.
2177 curstyle = xsltNextImport(curstyle);
2183 * xsltCleanupTemplates:
2184 * @style: an XSLT stylesheet
2186 * Cleanup the state of the templates used by the stylesheet and
2187 * the ones it imports.
2190 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2194 * xsltFreeTemplateHashes:
2195 * @style: an XSLT stylesheet
2197 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2200 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2201 if (style->templatesHash != NULL)
2202 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2203 (xmlHashDeallocator) xsltFreeCompMatchList);
2204 if (style->rootMatch != NULL)
2205 xsltFreeCompMatchList(style->rootMatch);
2206 if (style->keyMatch != NULL)
2207 xsltFreeCompMatchList(style->keyMatch);
2208 if (style->elemMatch != NULL)
2209 xsltFreeCompMatchList(style->elemMatch);
2210 if (style->attrMatch != NULL)
2211 xsltFreeCompMatchList(style->attrMatch);
2212 if (style->parentMatch != NULL)
2213 xsltFreeCompMatchList(style->parentMatch);
2214 if (style->textMatch != NULL)
2215 xsltFreeCompMatchList(style->textMatch);
2216 if (style->piMatch != NULL)
2217 xsltFreeCompMatchList(style->piMatch);
2218 if (style->commentMatch != NULL)
2219 xsltFreeCompMatchList(style->commentMatch);
2225 * @node: a node in the source tree
2226 * @pattern: an XSLT pattern
2227 * @ctxtdoc: context document (for namespaces)
2228 * @ctxtnode: context node (for namespaces)
2230 * Determine if a node matches a pattern.
2233 xsltMatchPattern(xsltTransformContextPtr context,
2235 const xmlChar *pattern,
2237 xmlNodePtr ctxtnode)
2240 xsltCompMatchPtr first, comp;
2242 if ((context != NULL) && (pattern != NULL)) {
2243 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2244 for (comp = first; comp != NULL; comp = comp->next) {
2245 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2250 xsltFreeCompMatchList(first);