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(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(ctxt, select->lenExtra) =
649 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
651 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
652 (xmlFreeFunc) xmlXPathFreeObject;
655 if ((list->nodesetval == NULL) ||
656 (list->nodesetval->nodeNr <= 0))
658 /* TODO: store the index and use it for the scan */
660 for (j = 0;j < list->nodesetval->nodeNr;j++) {
661 if (list->nodesetval->nodeTab[j] == node) {
663 xmlXPathFreeObject(list);
670 xmlXPathFreeObject(list);
674 * Depending on the last selection, one may need to
675 * recompute contextSize and proximityPosition.
677 * TODO: make this thread safe !
679 oldCS = ctxt->xpathCtxt->contextSize;
680 oldCP = ctxt->xpathCtxt->proximityPosition;
681 if ((select != NULL) &&
682 (select->op == XSLT_OP_ELEM) &&
683 (select->value != NULL) &&
684 (node->type == XML_ELEMENT_NODE) &&
685 (node->parent != NULL)) {
687 int index, nocache = 0;
689 previous = (xmlNodePtr)
690 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
692 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
693 if ((previous != NULL) &&
694 (previous->parent == node->parent)) {
696 * just walk back to adjust the index
699 xmlNodePtr sibling = node;
701 while (sibling != NULL) {
702 if (sibling == previous)
704 if ((node->type == XML_ELEMENT_NODE) &&
705 (node->name[0] == sibling->name[0]) &&
706 (xmlStrEqual(node->name, sibling->name))) {
707 if ((select->value2 == NULL) ||
708 ((sibling->ns != NULL) &&
709 (xmlStrEqual(select->value2,
710 sibling->ns->href))))
713 sibling = sibling->prev;
715 if (sibling == NULL) {
716 /* hum going backward in document order ... */
719 while (sibling != NULL) {
720 if (sibling == previous)
722 if ((select->value2 == NULL) ||
723 ((sibling->ns != NULL) &&
724 (xmlStrEqual(select->value2,
725 sibling->ns->href))))
727 sibling = sibling->next;
730 if (sibling != NULL) {
733 * If the node is in a Value Tree we cannot
736 if (node->doc != NULL) {
738 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
739 XSLT_RUNTIME_EXTRA(ctxt,
740 select->previousExtra) = node;
741 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
749 * recompute the index
751 xmlNodePtr siblings = node->parent->children;
752 xmlNodePtr parent = node->parent;
754 while (siblings != NULL) {
755 if (siblings->type == XML_ELEMENT_NODE) {
756 if (siblings == node) {
759 } else if ((node->name[0] == siblings->name[0])
760 && (xmlStrEqual(node->name, siblings->name))) {
761 if ((select->value2 == NULL) ||
762 ((siblings->ns != NULL) &&
763 (xmlStrEqual(select->value2,
764 siblings->ns->href))))
768 siblings = siblings->next;
770 if ((parent == NULL) || (node->doc == NULL))
773 while (parent->parent != NULL)
774 parent = parent->parent;
775 if (((parent->type != XML_DOCUMENT_NODE) &&
776 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
777 (parent != (xmlNodePtr) node->doc))
782 ctxt->xpathCtxt->contextSize = len;
783 ctxt->xpathCtxt->proximityPosition = pos;
785 * If the node is in a Value Tree we cannot
788 if ((node->doc != NULL) && (nocache == 0)) {
789 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
791 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
793 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
797 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
798 (node->type == XML_ELEMENT_NODE)) {
800 int index, nocache = 0;
802 previous = (xmlNodePtr)
803 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
805 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
806 if ((previous != NULL) &&
807 (previous->parent == node->parent)) {
809 * just walk back to adjust the index
812 xmlNodePtr sibling = node;
814 while (sibling != NULL) {
815 if (sibling == previous)
817 if (sibling->type == XML_ELEMENT_NODE)
819 sibling = sibling->prev;
821 if (sibling == NULL) {
822 /* hum going backward in document order ... */
825 while (sibling != NULL) {
826 if (sibling == previous)
828 if (sibling->type == XML_ELEMENT_NODE)
830 sibling = sibling->next;
833 if (sibling != NULL) {
836 * If the node is in a Value Tree we cannot
839 if (node->doc != NULL) {
841 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
842 XSLT_RUNTIME_EXTRA(ctxt,
843 select->previousExtra) = node;
844 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
851 * recompute the index
853 xmlNodePtr siblings = node->parent->children;
854 xmlNodePtr parent = node->parent;
856 while (siblings != NULL) {
857 if (siblings->type == XML_ELEMENT_NODE) {
859 if (siblings == node) {
863 siblings = siblings->next;
865 if ((parent == NULL) || (node->doc == NULL))
868 while (parent->parent != NULL)
869 parent = parent->parent;
870 if (((parent->type != XML_DOCUMENT_NODE) &&
871 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
872 (parent != (xmlNodePtr) node->doc))
877 ctxt->xpathCtxt->contextSize = len;
878 ctxt->xpathCtxt->proximityPosition = pos;
880 * If the node is in a Value Tree we cannot
883 if ((node->doc != NULL) && (nocache == 0)) {
884 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
886 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
888 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
893 oldNode = ctxt->node;
896 if (step->value == NULL)
898 if (step->comp == NULL)
901 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
906 ctxt->xpathCtxt->contextSize = oldCS;
907 ctxt->xpathCtxt->proximityPosition = oldCP;
909 ctxt->node = oldNode;
913 ctxt->xpathCtxt->contextSize = oldCS;
914 ctxt->xpathCtxt->proximityPosition = oldCP;
916 ctxt->node = oldNode;
920 if (node->type != XML_PI_NODE)
922 if (step->value != NULL) {
923 if (!xmlStrEqual(step->value, node->name))
927 case XSLT_OP_COMMENT:
928 if (node->type != XML_COMMENT_NODE)
932 if ((node->type != XML_TEXT_NODE) &&
933 (node->type != XML_CDATA_SECTION_NODE))
937 switch (node->type) {
938 case XML_DOCUMENT_NODE:
939 case XML_HTML_DOCUMENT_NODE:
940 #ifdef LIBXML_DOCB_ENABLED
941 case XML_DOCB_DOCUMENT_NODE:
943 case XML_ELEMENT_NODE:
944 case XML_CDATA_SECTION_NODE:
946 case XML_COMMENT_NODE:
948 case XML_ATTRIBUTE_NODE:
960 * xsltTestCompMatchList:
961 * @ctxt: a XSLT process context
963 * @comp: the precompiled pattern list
965 * Test wether the node matches one of the patterns in the list
967 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
970 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
971 xsltCompMatchPtr comp) {
974 if ((ctxt == NULL) || (node == NULL))
976 while (comp != NULL) {
977 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
985 /************************************************************************
987 * Dedicated parser for templates *
989 ************************************************************************/
991 #define CUR (*ctxt->cur)
992 #define SKIP(val) ctxt->cur += (val)
993 #define NXT(val) ctxt->cur[(val)]
994 #define CUR_PTR ctxt->cur
996 #define SKIP_BLANKS \
997 while (IS_BLANK(CUR)) NEXT
999 #define CURRENT (*ctxt->cur)
1000 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
1003 #define PUSH(op, val, val2) \
1004 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1007 xsltSwapTopCompMatch(ctxt->comp);
1009 #define XSLT_ERROR(X) \
1010 { xsltError(ctxt, __FILE__, __LINE__, X); \
1011 ctxt->error = (X); return; }
1013 #define XSLT_ERROR0(X) \
1014 { xsltError(ctxt, __FILE__, __LINE__, X); \
1015 ctxt->error = (X); return(0); }
1019 * @ctxt: the XPath Parser context
1021 * Parse an XPath Litteral:
1023 * [29] Literal ::= '"' [^"]* '"'
1026 * Returns the Literal parsed or NULL
1030 xsltScanLiteral(xsltParserContextPtr ctxt) {
1031 const xmlChar *q, *cur;
1032 xmlChar *ret = NULL;
1039 val = xmlStringCurrentChar(NULL, cur, &len);
1040 while ((IS_CHAR(val)) && (val != '"')) {
1042 val = xmlStringCurrentChar(NULL, cur, &len);
1044 if (!IS_CHAR(val)) {
1048 ret = xmlStrndup(q, cur - q);
1052 } else if (CUR == '\'') {
1055 val = xmlStringCurrentChar(NULL, cur, &len);
1056 while ((IS_CHAR(val)) && (val != '\'')) {
1058 val = xmlStringCurrentChar(NULL, cur, &len);
1060 if (!IS_CHAR(val)) {
1064 ret = xmlStrndup(q, cur - q);
1069 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1078 * @ctxt: the XPath Parser context
1080 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1081 * CombiningChar | Extender
1083 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1085 * [6] Names ::= Name (S Name)*
1087 * Returns the Name parsed or NULL
1091 xsltScanName(xsltParserContextPtr ctxt) {
1092 const xmlChar *q, *cur;
1093 xmlChar *ret = NULL;
1099 val = xmlStringCurrentChar(NULL, cur, &len);
1100 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1103 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1104 (val == '.') || (val == '-') ||
1106 (IS_COMBINING(val)) ||
1107 (IS_EXTENDER(val))) {
1109 val = xmlStringCurrentChar(NULL, cur, &len);
1111 ret = xmlStrndup(q, cur - q);
1118 * @ctxt: the XPath Parser context
1120 * Parses a non qualified name
1122 * Returns the Name parsed or NULL
1126 xsltScanNCName(xsltParserContextPtr ctxt) {
1127 const xmlChar *q, *cur;
1128 xmlChar *ret = NULL;
1134 val = xmlStringCurrentChar(NULL, cur, &len);
1135 if (!IS_LETTER(val) && (val != '_'))
1138 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1139 (val == '.') || (val == '-') ||
1141 (IS_COMBINING(val)) ||
1142 (IS_EXTENDER(val))) {
1144 val = xmlStringCurrentChar(NULL, cur, &len);
1146 ret = xmlStrndup(q, cur - q);
1153 * @ctxt: the XPath Parser context
1154 * @prefix: the place to store the prefix
1156 * Parse a qualified name
1158 * Returns the Name parsed or NULL
1162 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1163 xmlChar *ret = NULL;
1166 ret = xsltScanNCName(ctxt);
1170 ret = xsltScanNCName(ctxt);
1176 * xsltCompileIdKeyPattern:
1177 * @ctxt: the compilation context
1178 * @name: a preparsed name
1179 * @aid: whether id/key are allowed there
1181 * Compile the XSLT LocationIdKeyPattern
1182 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1183 * | 'key' '(' Literal ',' Literal ')'
1185 * also handle NodeType and PI from:
1187 * [7] NodeTest ::= NameTest
1188 * | NodeType '(' ')'
1189 * | 'processing-instruction' '(' Literal ')'
1192 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1193 xmlChar *lit = NULL;
1194 xmlChar *lit2 = NULL;
1197 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1198 xsltGenericError(xsltGenericErrorContext,
1199 "xsltCompileIdKeyPattern : ( expected\n");
1203 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1206 lit = xsltScanLiteral(ctxt);
1211 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1212 xsltGenericError(xsltGenericErrorContext,
1213 "xsltCompileIdKeyPattern : ) expected\n");
1218 PUSH(XSLT_OP_ID, lit, NULL);
1219 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1222 lit = xsltScanLiteral(ctxt);
1227 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1228 xsltGenericError(xsltGenericErrorContext,
1229 "xsltCompileIdKeyPattern : , expected\n");
1235 lit2 = xsltScanLiteral(ctxt);
1240 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1241 xsltGenericError(xsltGenericErrorContext,
1242 "xsltCompileIdKeyPattern : ) expected\n");
1247 /* TODO: support namespace in keys */
1248 PUSH(XSLT_OP_KEY, lit, lit2);
1249 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1253 lit = xsltScanLiteral(ctxt);
1258 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1259 xsltGenericError(xsltGenericErrorContext,
1260 "xsltCompileIdKeyPattern : ) expected\n");
1266 PUSH(XSLT_OP_PI, lit, NULL);
1267 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1271 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1272 xsltGenericError(xsltGenericErrorContext,
1273 "xsltCompileIdKeyPattern : ) expected\n");
1278 PUSH(XSLT_OP_TEXT, NULL, NULL);
1279 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1283 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1284 xsltGenericError(xsltGenericErrorContext,
1285 "xsltCompileIdKeyPattern : ) expected\n");
1290 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1291 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1295 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1296 xsltGenericError(xsltGenericErrorContext,
1297 "xsltCompileIdKeyPattern : ) expected\n");
1302 PUSH(XSLT_OP_NODE, NULL, NULL);
1304 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1305 xsltGenericError(xsltGenericErrorContext,
1306 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1310 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1311 xsltGenericError(xsltGenericErrorContext,
1312 "xsltCompileIdKeyPattern : node type\n");
1322 * xsltCompileStepPattern:
1323 * @ctxt: the compilation context
1324 * @token: a posible precompiled name
1326 * Compile the XSLT StepPattern and generates a precompiled
1327 * form suitable for fast matching.
1329 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1330 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1331 * | ('child' | 'attribute') '::'
1333 * [7] NodeTest ::= NameTest
1334 * | NodeType '(' ')'
1335 * | 'processing-instruction' '(' Literal ')'
1336 * [8] Predicate ::= '[' PredicateExpr ']'
1337 * [9] PredicateExpr ::= Expr
1338 * [13] AbbreviatedAxisSpecifier ::= '@'?
1339 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1343 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1344 xmlChar *name = NULL;
1345 const xmlChar *URI = NULL;
1346 xmlChar *URL = NULL;
1350 if ((token == NULL) && (CUR == '@')) {
1351 xmlChar *prefix = NULL;
1356 PUSH(XSLT_OP_ATTR, NULL, NULL);
1359 token = xsltScanQName(ctxt, &prefix);
1360 if (prefix != NULL) {
1363 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1365 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1366 xsltGenericError(xsltGenericErrorContext,
1367 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1370 URL = xmlStrdup(ns->href);
1374 if (token == NULL) {
1377 PUSH(XSLT_OP_ATTR, NULL, URL);
1380 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1381 xsltGenericError(xsltGenericErrorContext,
1382 "xsltCompileStepPattern : Name expected\n");
1386 PUSH(XSLT_OP_ATTR, token, URL);
1387 goto parse_predicate;
1390 token = xsltScanName(ctxt);
1391 if (token == NULL) {
1394 PUSH(XSLT_OP_ALL, token, NULL);
1395 goto parse_predicate;
1397 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1398 xsltGenericError(xsltGenericErrorContext,
1399 "xsltCompileStepPattern : Name expected\n");
1408 xsltCompileIdKeyPattern(ctxt, token, 0);
1411 } else if (CUR == ':') {
1414 xmlChar *prefix = token;
1418 * This is a namespace match
1420 token = xsltScanName(ctxt);
1421 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1423 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1424 xsltGenericError(xsltGenericErrorContext,
1425 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1430 URL = xmlStrdup(ns->href);
1433 if (token == NULL) {
1436 PUSH(XSLT_OP_NS, URL, NULL);
1438 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1439 xsltGenericError(xsltGenericErrorContext,
1440 "xsltCompileStepPattern : Name expected\n");
1445 PUSH(XSLT_OP_ELEM, token, URL);
1449 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1451 token = xsltScanName(ctxt);
1452 if (token == NULL) {
1455 PUSH(XSLT_OP_ALL, token, NULL);
1456 goto parse_predicate;
1458 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1459 xsltGenericError(xsltGenericErrorContext,
1460 "xsltCompileStepPattern : QName expected\n");
1465 URI = xsltGetQNameURI(ctxt->elem, &token);
1466 if (token == NULL) {
1470 name = xmlStrdup(token);
1472 URL = xmlStrdup(URI);
1474 PUSH(XSLT_OP_CHILD, name, URL);
1475 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1477 token = xsltScanName(ctxt);
1478 if (token == NULL) {
1479 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1480 xsltGenericError(xsltGenericErrorContext,
1481 "xsltCompileStepPattern : QName expected\n");
1485 URI = xsltGetQNameURI(ctxt->elem, &token);
1486 if (token == NULL) {
1490 name = xmlStrdup(token);
1492 URL = xmlStrdup(URI);
1494 PUSH(XSLT_OP_ATTR, name, URL);
1496 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1497 xsltGenericError(xsltGenericErrorContext,
1498 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1504 } else if (CUR == '*') {
1506 PUSH(XSLT_OP_ALL, token, NULL);
1508 URI = xsltGetQNameURI(ctxt->elem, &token);
1509 if (token == NULL) {
1514 URL = xmlStrdup(URI);
1515 PUSH(XSLT_OP_ELEM, token, URL);
1520 while (CUR == '[') {
1522 xmlChar *ret = NULL;
1528 /* Skip over nested predicates */
1531 else if (CUR == ']') {
1535 } else if (CUR == '"') {
1537 while ((CUR != 0) && (CUR != '"'))
1539 } else if (CUR == '\'') {
1541 while ((CUR != 0) && (CUR != '\''))
1547 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1548 xsltGenericError(xsltGenericErrorContext,
1549 "xsltCompileStepPattern : ']' expected\n");
1553 ret = xmlStrndup(q, CUR_PTR - q);
1554 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1555 /* push the predicate lower than local test */
1569 * xsltCompileRelativePathPattern:
1570 * @comp: the compilation context
1571 * @token: a posible precompiled name
1573 * Compile the XSLT RelativePathPattern and generates a precompiled
1574 * form suitable for fast matching.
1576 * [4] RelativePathPattern ::= StepPattern
1577 * | RelativePathPattern '/' StepPattern
1578 * | RelativePathPattern '//' StepPattern
1581 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1582 xsltCompileStepPattern(ctxt, token);
1586 while ((CUR != 0) && (CUR != '|')) {
1587 if ((CUR == '/') && (NXT(1) == '/')) {
1588 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1592 xsltCompileStepPattern(ctxt, NULL);
1593 } else if (CUR == '/') {
1594 PUSH(XSLT_OP_PARENT, NULL, NULL);
1597 if ((CUR != 0) || (CUR == '|')) {
1598 xsltCompileRelativePathPattern(ctxt, NULL);
1612 * xsltCompileLocationPathPattern:
1613 * @ctxt: the compilation context
1615 * Compile the XSLT LocationPathPattern and generates a precompiled
1616 * form suitable for fast matching.
1618 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1619 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1620 * | '//'? RelativePathPattern
1623 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1625 if ((CUR == '/') && (NXT(1) == '/')) {
1627 * since we reverse the query
1628 * a leading // can be safely ignored
1632 xsltCompileRelativePathPattern(ctxt, NULL);
1633 } else if (CUR == '/') {
1635 * We need to find root as the parent
1639 PUSH(XSLT_OP_ROOT, NULL, NULL);
1640 if ((CUR != 0) || (CUR == '|')) {
1641 PUSH(XSLT_OP_PARENT, NULL, NULL);
1642 xsltCompileRelativePathPattern(ctxt, NULL);
1644 } else if (CUR == '*') {
1645 xsltCompileRelativePathPattern(ctxt, NULL);
1646 } else if (CUR == '@') {
1647 xsltCompileRelativePathPattern(ctxt, NULL);
1650 name = xsltScanName(ctxt);
1652 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1653 xsltGenericError(xsltGenericErrorContext,
1654 "xsltCompileLocationPathPattern : Name expected\n");
1659 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1660 xsltCompileIdKeyPattern(ctxt, name, 1);
1661 if ((CUR == '/') && (NXT(1) == '/')) {
1662 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1666 xsltCompileRelativePathPattern(ctxt, NULL);
1667 } else if (CUR == '/') {
1668 PUSH(XSLT_OP_PARENT, NULL, NULL);
1671 xsltCompileRelativePathPattern(ctxt, NULL);
1675 xsltCompileRelativePathPattern(ctxt, name);
1682 * xsltCompilePattern:
1683 * @pattern: an XSLT pattern
1684 * @doc: the containing document
1685 * @node: the containing element
1686 * @style: the stylesheet
1687 * @runtime: the transformation context, if done at run-time
1689 * Compile the XSLT pattern and generates a list of precompiled form suitable
1690 * for fast matching.
1692 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1694 * Returns the generated pattern list or NULL in case of failure
1698 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1699 xmlNodePtr node, xsltStylesheetPtr style,
1700 xsltTransformContextPtr runtime) {
1701 xsltParserContextPtr ctxt = NULL;
1702 xsltCompMatchPtr element, first = NULL, previous = NULL;
1703 int current, start, end, level, j;
1705 if (pattern == NULL) {
1706 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1707 xsltGenericError(xsltGenericErrorContext,
1708 "xsltCompilePattern : NULL pattern\n");
1712 ctxt = xsltNewParserContext(style, runtime);
1718 while (pattern[current] != 0) {
1720 while (IS_BLANK(pattern[current]))
1724 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1725 if (pattern[end] == '[')
1727 else if (pattern[end] == ']')
1729 else if (pattern[end] == '\'') {
1731 while ((pattern[end] != 0) && (pattern[end] != '\''))
1733 } else if (pattern[end] == '"') {
1735 while ((pattern[end] != 0) && (pattern[end] != '"'))
1740 if (current == end) {
1741 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1742 xsltGenericError(xsltGenericErrorContext,
1743 "xsltCompilePattern : NULL pattern\n");
1746 element = xsltNewCompMatch();
1747 if (element == NULL) {
1752 else if (previous != NULL)
1753 previous->next = element;
1756 ctxt->comp = element;
1757 ctxt->base = xmlStrndup(&pattern[start], end - start);
1758 if (ctxt->base == NULL)
1760 ctxt->cur = &(ctxt->base)[current - start];
1761 element->pattern = ctxt->base;
1762 element->nsList = xmlGetNsList(doc, node);
1764 if (element->nsList != NULL) {
1765 while (element->nsList[j] != NULL)
1771 #ifdef WITH_XSLT_DEBUG_PATTERN
1772 xsltGenericDebug(xsltGenericDebugContext,
1773 "xsltCompilePattern : parsing '%s'\n",
1776 xsltCompileLocationPathPattern(ctxt);
1778 xsltPrintErrorContext(NULL, style, node);
1779 xsltGenericError(xsltGenericErrorContext,
1780 "xsltCompilePattern : failed to compile '%s'\n",
1787 * Reverse for faster interpretation.
1789 xsltReverseCompMatch(element);
1792 * Set-up the priority
1794 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1795 (element->steps[0].op == XSLT_OP_ATTR)) &&
1796 (element->steps[0].value != NULL) &&
1797 (element->steps[1].op == XSLT_OP_END)) {
1798 element->priority = 0;
1800 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1801 (element->steps[1].op == XSLT_OP_END)) {
1802 element->priority = 0;
1804 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1805 (element->steps[0].value != NULL) &&
1806 (element->steps[1].op == XSLT_OP_END)) {
1807 element->priority = 0;
1808 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1809 (element->steps[0].value2 != NULL) &&
1810 (element->steps[1].op == XSLT_OP_END)) {
1811 element->priority = -0.25;
1812 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1813 (element->steps[0].value != NULL) &&
1814 (element->steps[1].op == XSLT_OP_END)) {
1815 element->priority = -0.25;
1816 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1817 (element->steps[0].value == NULL) &&
1818 (element->steps[0].value2 == NULL) &&
1819 (element->steps[1].op == XSLT_OP_END)) {
1820 element->priority = -0.5;
1821 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1822 (element->steps[0].op == XSLT_OP_TEXT) ||
1823 (element->steps[0].op == XSLT_OP_ALL) ||
1824 (element->steps[0].op == XSLT_OP_NODE) ||
1825 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1826 (element->steps[1].op == XSLT_OP_END)) {
1827 element->priority = -0.5;
1829 element->priority = 0.5;
1831 #ifdef WITH_XSLT_DEBUG_PATTERN
1832 xsltGenericDebug(xsltGenericDebugContext,
1833 "xsltCompilePattern : parsed %s, default priority %f\n",
1834 element->pattern, element->priority);
1836 if (pattern[end] == '|')
1841 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1842 xsltGenericError(xsltGenericErrorContext,
1843 "xsltCompilePattern : NULL pattern\n");
1848 xsltFreeParserContext(ctxt);
1853 xsltFreeParserContext(ctxt);
1855 xsltFreeCompMatchList(first);
1859 /************************************************************************
1861 * Module interfaces *
1863 ************************************************************************/
1867 * @style: an XSLT stylesheet
1868 * @cur: an XSLT template
1869 * @mode: the mode name or NULL
1870 * @modeURI: the mode URI or NULL
1872 * Register the XSLT pattern associated to @cur
1874 * Returns -1 in case of error, 0 otherwise
1877 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1878 const xmlChar *mode, const xmlChar *modeURI) {
1879 xsltCompMatchPtr pat, list, *top = NULL, next;
1880 const xmlChar *name = NULL;
1881 float priority; /* the priority */
1883 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1886 priority = cur->priority;
1887 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1893 pat->template = cur;
1895 pat->mode = xmlStrdup(mode);
1896 if (modeURI != NULL)
1897 pat->modeURI = xmlStrdup(modeURI);
1898 if (priority != XSLT_PAT_NO_PRIORITY)
1899 pat->priority = priority;
1902 * insert it in the hash table list corresponding to its lookup name
1904 switch (pat->steps[0].op) {
1906 if (pat->steps[0].value != NULL)
1907 name = pat->steps[0].value;
1909 top = (xsltCompMatchPtr *) &(style->attrMatch);
1912 case XSLT_OP_PARENT:
1913 case XSLT_OP_ANCESTOR:
1914 top = (xsltCompMatchPtr *) &(style->elemMatch);
1917 top = (xsltCompMatchPtr *) &(style->rootMatch);
1920 top = (xsltCompMatchPtr *) &(style->keyMatch);
1923 /* TODO optimize ID !!! */
1926 top = (xsltCompMatchPtr *) &(style->elemMatch);
1929 case XSLT_OP_PREDICATE:
1930 xsltPrintErrorContext(NULL, style, NULL);
1931 xsltGenericError(xsltGenericErrorContext,
1932 "xsltAddTemplate: invalid compiled pattern\n");
1933 xsltFreeCompMatch(pat);
1936 * TODO: some flags at the top level about type based patterns
1937 * would be faster than inclusion in the hash table.
1940 if (pat->steps[0].value != NULL)
1941 name = pat->steps[0].value;
1943 top = (xsltCompMatchPtr *) &(style->piMatch);
1945 case XSLT_OP_COMMENT:
1946 top = (xsltCompMatchPtr *) &(style->commentMatch);
1949 top = (xsltCompMatchPtr *) &(style->textMatch);
1953 if (pat->steps[0].value != NULL)
1954 name = pat->steps[0].value;
1956 top = (xsltCompMatchPtr *) &(style->elemMatch);
1960 if (style->templatesHash == NULL) {
1961 style->templatesHash = xmlHashCreate(1024);
1962 if (style->templatesHash == NULL) {
1963 xsltFreeCompMatch(pat);
1966 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1968 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1969 name, mode, modeURI);
1971 xmlHashAddEntry3(style->templatesHash, name,
1972 mode, modeURI, pat);
1975 * Note '<=' since one must choose among the matching
1976 * template rules that are left, the one that occurs
1977 * last in the stylesheet
1979 if (list->priority <= pat->priority) {
1981 xmlHashUpdateEntry3(style->templatesHash, name,
1982 mode, modeURI, pat, NULL);
1984 while (list->next != NULL) {
1985 if (list->next->priority <= pat->priority)
1989 pat->next = list->next;
1994 } else if (top != NULL) {
1999 } else if (list->priority <= pat->priority) {
2003 while (list->next != NULL) {
2004 if (list->next->priority <= pat->priority)
2008 pat->next = list->next;
2012 xsltPrintErrorContext(NULL, style, NULL);
2013 xsltGenericError(xsltGenericErrorContext,
2014 "xsltAddTemplate: invalid compiled pattern\n");
2015 xsltFreeCompMatch(pat);
2018 #ifdef WITH_XSLT_DEBUG_PATTERN
2020 xsltGenericDebug(xsltGenericDebugContext,
2021 "added pattern : '%s' mode '%s' priority %f\n",
2022 pat->pattern, pat->mode, pat->priority);
2024 xsltGenericDebug(xsltGenericDebugContext,
2025 "added pattern : '%s' priority %f\n",
2026 pat->pattern, pat->priority);
2036 * @ctxt: a XSLT process context
2037 * @node: the node being processed
2038 * @style: the current style
2040 * Finds the template applying to this node, if @style is non-NULL
2041 * it means one needs to look for the next imported template in scope.
2043 * Returns the xsltTemplatePtr or NULL if not found
2046 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2047 xsltStylesheetPtr style) {
2048 xsltStylesheetPtr curstyle;
2049 xsltTemplatePtr ret = NULL;
2050 const xmlChar *name = NULL;
2051 xsltCompMatchPtr list = NULL;
2054 if ((ctxt == NULL) || (node == NULL))
2057 if (style == NULL) {
2058 curstyle = ctxt->style;
2060 curstyle = xsltNextImport(style);
2063 while ((curstyle != NULL) && (curstyle != style)) {
2064 priority = XSLT_PAT_NO_PRIORITY;
2065 /* TODO : handle IDs/keys here ! */
2066 if (curstyle->templatesHash != NULL) {
2068 * Use the top name as selector
2070 switch (node->type) {
2071 case XML_ELEMENT_NODE:
2072 case XML_ATTRIBUTE_NODE:
2076 case XML_DOCUMENT_NODE:
2077 case XML_HTML_DOCUMENT_NODE:
2079 case XML_CDATA_SECTION_NODE:
2080 case XML_COMMENT_NODE:
2081 case XML_ENTITY_REF_NODE:
2082 case XML_ENTITY_NODE:
2083 case XML_DOCUMENT_TYPE_NODE:
2084 case XML_DOCUMENT_FRAG_NODE:
2085 case XML_NOTATION_NODE:
2087 case XML_ELEMENT_DECL:
2088 case XML_ATTRIBUTE_DECL:
2089 case XML_ENTITY_DECL:
2090 case XML_NAMESPACE_DECL:
2091 case XML_XINCLUDE_START:
2092 case XML_XINCLUDE_END:
2101 * find the list of appliable expressions based on the name
2103 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2104 name, ctxt->mode, ctxt->modeURI);
2107 while (list != NULL) {
2108 if (xsltTestCompMatch(ctxt, list, node,
2109 ctxt->mode, ctxt->modeURI)) {
2110 ret = list->template;
2111 priority = list->priority;
2119 * find alternate generic matches
2121 switch (node->type) {
2122 case XML_ELEMENT_NODE:
2123 list = curstyle->elemMatch;
2125 case XML_ATTRIBUTE_NODE:
2126 list = curstyle->attrMatch;
2129 list = curstyle->piMatch;
2131 case XML_DOCUMENT_NODE:
2132 case XML_HTML_DOCUMENT_NODE:
2133 list = curstyle->rootMatch;
2136 case XML_CDATA_SECTION_NODE:
2137 list = curstyle->textMatch;
2139 case XML_COMMENT_NODE:
2140 list = curstyle->commentMatch;
2142 case XML_ENTITY_REF_NODE:
2143 case XML_ENTITY_NODE:
2144 case XML_DOCUMENT_TYPE_NODE:
2145 case XML_DOCUMENT_FRAG_NODE:
2146 case XML_NOTATION_NODE:
2148 case XML_ELEMENT_DECL:
2149 case XML_ATTRIBUTE_DECL:
2150 case XML_ENTITY_DECL:
2151 case XML_NAMESPACE_DECL:
2152 case XML_XINCLUDE_START:
2153 case XML_XINCLUDE_END:
2159 while ((list != NULL) &&
2160 ((ret == NULL) || (list->priority > priority))) {
2161 if (xsltTestCompMatch(ctxt, list, node,
2162 ctxt->mode, ctxt->modeURI)) {
2163 ret = list->template;
2164 priority = list->priority;
2170 * Some of the tests for elements can also apply to documents
2172 if ((node->type == XML_DOCUMENT_NODE) ||
2173 (node->type == XML_HTML_DOCUMENT_NODE)) {
2174 list = curstyle->elemMatch;
2175 while ((list != NULL) &&
2176 ((ret == NULL) || (list->priority > priority))) {
2177 if (xsltTestCompMatch(ctxt, list, node,
2178 ctxt->mode, ctxt->modeURI)) {
2179 ret = list->template;
2180 priority = list->priority;
2187 if (node->_private != NULL) {
2188 list = curstyle->keyMatch;
2189 while ((list != NULL) &&
2190 ((ret == NULL) || (list->priority > priority))) {
2191 if (xsltTestCompMatch(ctxt, list, node,
2192 ctxt->mode, ctxt->modeURI)) {
2193 ret = list->template;
2194 priority = list->priority;
2204 * Cycle on next curstylesheet import.
2206 curstyle = xsltNextImport(curstyle);
2212 * xsltCleanupTemplates:
2213 * @style: an XSLT stylesheet
2215 * Cleanup the state of the templates used by the stylesheet and
2216 * the ones it imports.
2219 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2223 * xsltFreeTemplateHashes:
2224 * @style: an XSLT stylesheet
2226 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2229 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2230 if (style->templatesHash != NULL)
2231 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2232 (xmlHashDeallocator) xsltFreeCompMatchList);
2233 if (style->rootMatch != NULL)
2234 xsltFreeCompMatchList(style->rootMatch);
2235 if (style->keyMatch != NULL)
2236 xsltFreeCompMatchList(style->keyMatch);
2237 if (style->elemMatch != NULL)
2238 xsltFreeCompMatchList(style->elemMatch);
2239 if (style->attrMatch != NULL)
2240 xsltFreeCompMatchList(style->attrMatch);
2241 if (style->parentMatch != NULL)
2242 xsltFreeCompMatchList(style->parentMatch);
2243 if (style->textMatch != NULL)
2244 xsltFreeCompMatchList(style->textMatch);
2245 if (style->piMatch != NULL)
2246 xsltFreeCompMatchList(style->piMatch);
2247 if (style->commentMatch != NULL)
2248 xsltFreeCompMatchList(style->commentMatch);
2254 * @node: a node in the source tree
2255 * @pattern: an XSLT pattern
2256 * @ctxtdoc: context document (for namespaces)
2257 * @ctxtnode: context node (for namespaces)
2259 * Determine if a node matches a pattern.
2262 xsltMatchPattern(xsltTransformContextPtr context,
2264 const xmlChar *pattern,
2266 xmlNodePtr ctxtnode)
2269 xsltCompMatchPtr first, comp;
2271 if ((context != NULL) && (pattern != NULL)) {
2272 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2273 for (comp = first; comp != NULL; comp = comp->next) {
2274 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2279 xsltFreeCompMatchList(first);