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 xsltTransformError(NULL, NULL, NULL,
129 "xsltNewCompMatch : malloc failed\n");
132 memset(cur, 0, sizeof(xsltCompMatch));
141 * @comp: an XSLT comp
143 * Free up the memory allocated by @comp
146 xsltFreeCompMatch(xsltCompMatchPtr comp) {
152 if (comp->pattern != NULL)
153 xmlFree((xmlChar *)comp->pattern);
154 if (comp->mode != NULL)
155 xmlFree((xmlChar *)comp->mode);
156 if (comp->modeURI != NULL)
157 xmlFree((xmlChar *)comp->modeURI);
158 if (comp->nsList != NULL)
159 xmlFree(comp->nsList);
160 for (i = 0;i < comp->nbStep;i++) {
161 op = &comp->steps[i];
162 if (op->value != NULL)
164 if (op->value2 != NULL)
166 if (op->value3 != NULL)
168 if (op->comp != NULL)
169 xmlXPathFreeCompExpr(op->comp);
171 memset(comp, -1, sizeof(xsltCompMatch));
176 * xsltFreeCompMatchList:
177 * @comp: an XSLT comp list
179 * Free up the memory allocated by all the elements of @comp
182 xsltFreeCompMatchList(xsltCompMatchPtr comp) {
183 xsltCompMatchPtr cur;
185 while (comp != NULL) {
188 xsltFreeCompMatch(cur);
193 * xsltNewParserContext:
194 * @style: the stylesheet
195 * @ctxt: the transformation context, if done at run-time
197 * Create a new XSLT ParserContext
199 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
201 static xsltParserContextPtr
202 xsltNewParserContext(xsltStylesheetPtr style, xsltTransformContextPtr ctxt) {
203 xsltParserContextPtr cur;
205 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
207 xsltTransformError(NULL, NULL, NULL,
208 "xsltNewParserContext : malloc failed\n");
211 memset(cur, 0, sizeof(xsltParserContext));
218 * xsltFreeParserContext:
219 * @ctxt: an XSLT parser context
221 * Free up the memory allocated by @ctxt
224 xsltFreeParserContext(xsltParserContextPtr ctxt) {
227 memset(ctxt, -1, sizeof(xsltParserContext));
233 * @comp: the compiled match expression
235 * @value: the first value
236 * @value2: the second value
238 * Add an step to an XSLT Compiled Match
240 * Returns -1 in case of failure, 0 otherwise.
243 xsltCompMatchAdd(xsltParserContextPtr ctxt, xsltCompMatchPtr comp,
244 xsltOp op, xmlChar * value, xmlChar * value2)
246 if (comp->nbStep >= 40) {
247 xsltTransformError(NULL, NULL, NULL,
248 "xsltCompMatchAdd: overflow\n");
251 comp->steps[comp->nbStep].op = op;
252 comp->steps[comp->nbStep].value = value;
253 comp->steps[comp->nbStep].value2 = value2;
254 if (ctxt->ctxt != NULL) {
255 comp->steps[comp->nbStep].previousExtra =
256 xsltAllocateExtraCtxt(ctxt->ctxt);
257 comp->steps[comp->nbStep].indexExtra =
258 xsltAllocateExtraCtxt(ctxt->ctxt);
259 comp->steps[comp->nbStep].lenExtra =
260 xsltAllocateExtraCtxt(ctxt->ctxt);
262 comp->steps[comp->nbStep].previousExtra =
263 xsltAllocateExtra(ctxt->style);
264 comp->steps[comp->nbStep].indexExtra =
265 xsltAllocateExtra(ctxt->style);
266 comp->steps[comp->nbStep].lenExtra =
267 xsltAllocateExtra(ctxt->style);
269 if (op == XSLT_OP_PREDICATE) {
270 comp->steps[comp->nbStep].comp = xmlXPathCompile(value);
277 * xsltSwapTopCompMatch:
278 * @comp: the compiled match expression
280 * reverse the two top steps.
283 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
285 int j = comp->nbStep - 1;
288 register xmlChar *tmp;
290 register xmlXPathCompExprPtr expr;
292 tmp = comp->steps[i].value;
293 comp->steps[i].value = comp->steps[j].value;
294 comp->steps[j].value = tmp;
295 tmp = comp->steps[i].value2;
296 comp->steps[i].value2 = comp->steps[j].value2;
297 comp->steps[j].value2 = tmp;
298 op = comp->steps[i].op;
299 comp->steps[i].op = comp->steps[j].op;
300 comp->steps[j].op = op;
301 expr = comp->steps[i].comp;
302 comp->steps[i].comp = comp->steps[j].comp;
303 comp->steps[j].comp = expr;
308 * xsltReverseCompMatch:
309 * @comp: the compiled match expression
311 * reverse all the stack of expressions
314 xsltReverseCompMatch(xsltCompMatchPtr comp) {
316 int j = comp->nbStep - 1;
319 register xmlChar *tmp;
321 register xmlXPathCompExprPtr expr;
322 tmp = comp->steps[i].value;
323 comp->steps[i].value = comp->steps[j].value;
324 comp->steps[j].value = tmp;
325 tmp = comp->steps[i].value2;
326 comp->steps[i].value2 = comp->steps[j].value2;
327 comp->steps[j].value2 = tmp;
328 op = comp->steps[i].op;
329 comp->steps[i].op = comp->steps[j].op;
330 comp->steps[j].op = op;
331 expr = comp->steps[i].comp;
332 comp->steps[i].comp = comp->steps[j].comp;
333 comp->steps[j].comp = expr;
337 comp->steps[comp->nbStep++].op = XSLT_OP_END;
340 /************************************************************************
342 * The interpreter for the precompiled patterns *
344 ************************************************************************/
348 * @ctxt: a XSLT process context
349 * @comp: the precompiled pattern
351 * @mode: the mode name or NULL
352 * @modeURI: the mode URI or NULL
354 * Test wether the node matches the pattern
356 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
359 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
360 xmlNodePtr node, const xmlChar *mode,
361 const xmlChar *modeURI) {
363 xsltStepOpPtr step, select = NULL;
365 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
366 xsltTransformError(ctxt, NULL, node,
367 "xsltTestCompMatch: null arg\n");
371 if (comp->mode == NULL)
373 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
376 if (comp->mode != NULL)
379 if (modeURI != NULL) {
380 if (comp->modeURI == NULL)
382 if ((comp->modeURI != modeURI) &&
383 (!xmlStrEqual(comp->modeURI, modeURI)))
386 if (comp->modeURI != NULL)
389 for (i = 0;i < comp->nbStep;i++) {
390 step = &comp->steps[i];
391 if (step->op != XSLT_OP_PREDICATE)
397 if ((node->type == XML_DOCUMENT_NODE) ||
398 #ifdef LIBXML_DOCB_ENABLED
399 (node->type == XML_DOCB_DOCUMENT_NODE) ||
401 (node->type == XML_HTML_DOCUMENT_NODE))
405 if (node->type != XML_ELEMENT_NODE)
407 if (step->value == NULL)
409 if (step->value[0] != node->name[0])
411 if (!xmlStrEqual(step->value, node->name))
415 if (node->ns == NULL) {
416 if (step->value2 != NULL)
418 } else if (node->ns->href != NULL) {
419 if (step->value2 == NULL)
421 if (!xmlStrEqual(step->value2, node->ns->href))
425 case XSLT_OP_CHILD: {
428 if ((node->type != XML_ELEMENT_NODE) &&
429 (node->type != XML_DOCUMENT_NODE) &&
430 #ifdef LIBXML_DOCB_ENABLED
431 (node->type != XML_DOCB_DOCUMENT_NODE) &&
433 (node->type != XML_HTML_DOCUMENT_NODE))
436 lst = node->children;
438 if (step->value != NULL) {
439 while (lst != NULL) {
440 if ((lst->type == XML_ELEMENT_NODE) &&
441 (step->value[0] == lst->name[0]) &&
442 (xmlStrEqual(step->value, lst->name)))
452 if (node->type != XML_ATTRIBUTE_NODE)
454 if (step->value != NULL) {
455 if (step->value[0] != node->name[0])
457 if (!xmlStrEqual(step->value, node->name))
461 if (node->ns == NULL) {
462 if (step->value2 != NULL)
464 } else if (step->value2 != NULL) {
465 if (!xmlStrEqual(step->value2, node->ns->href))
470 if ((node->type == XML_DOCUMENT_NODE) ||
471 (node->type == XML_HTML_DOCUMENT_NODE) ||
472 #ifdef LIBXML_DOCB_ENABLED
473 (node->type == XML_DOCB_DOCUMENT_NODE) ||
475 (node->type == XML_NAMESPACE_DECL))
480 if (step->value == NULL)
482 if (step->value[0] != node->name[0])
484 if (!xmlStrEqual(step->value, node->name))
487 if (node->ns == NULL) {
488 if (step->value2 != NULL)
490 } else if (node->ns->href != NULL) {
491 if (step->value2 == NULL)
493 if (!xmlStrEqual(step->value2, node->ns->href))
497 case XSLT_OP_ANCESTOR:
498 /* TODO: implement coalescing of ANCESTOR/NODE ops */
499 if (step->value == NULL) {
501 step = &comp->steps[i];
502 if (step->op == XSLT_OP_ROOT)
504 if (step->op != XSLT_OP_ELEM)
506 if (step->value == NULL)
511 if ((node->type == XML_DOCUMENT_NODE) ||
512 (node->type == XML_HTML_DOCUMENT_NODE) ||
513 #ifdef LIBXML_DOCB_ENABLED
514 (node->type == XML_DOCB_DOCUMENT_NODE) ||
516 (node->type == XML_NAMESPACE_DECL))
519 while (node != NULL) {
522 if ((node->type == XML_ELEMENT_NODE) &&
523 (step->value[0] == node->name[0]) &&
524 (xmlStrEqual(step->value, node->name))) {
526 if (node->ns == NULL) {
527 if (step->value2 == NULL)
529 } else if (node->ns->href != NULL) {
530 if ((step->value2 != NULL) &&
531 (xmlStrEqual(step->value2, node->ns->href)))
541 /* TODO Handle IDs decently, must be done differently */
544 if (node->type != XML_ELEMENT_NODE)
547 id = xmlGetID(node->doc, step->value);
548 if ((id == NULL) || (id->parent != node))
556 list = xsltGetKey(ctxt, step->value,
557 step->value3, step->value2);
560 for (indx = 0;indx < list->nodeNr;indx++)
561 if (list->nodeTab[indx] == node)
563 if (indx >= list->nodeNr)
568 if (node->type != XML_ELEMENT_NODE)
570 if (node->ns == NULL) {
571 if (step->value != NULL)
573 } else if (node->ns->href != NULL) {
574 if (step->value == NULL)
576 if (!xmlStrEqual(step->value, node->ns->href))
581 if (node->type != XML_ELEMENT_NODE)
584 case XSLT_OP_PREDICATE: {
587 int pos = 0, len = 0;
589 * The simple existing predicate code cannot handle
590 * properly cascaded predicates. If in this situation
591 * compute directly the full node list once and check
592 * if the node is in the result list.
594 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
595 xmlDocPtr prevdoc, doc;
596 xmlXPathObjectPtr list;
600 prevdoc = (xmlDocPtr)
601 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
603 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
604 list = (xmlXPathObjectPtr)
605 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra);
608 if ((list == NULL) || (prevdoc != doc)) {
610 xmlXPathObjectPtr newlist;
611 xmlNodePtr parent = node->parent;
613 if (comp->pattern[0] == '/')
614 query = xmlStrdup(comp->pattern);
616 query = xmlStrdup((const xmlChar *)"//");
617 query = xmlStrcat(query, comp->pattern);
619 newlist = xmlXPathEval(query, ctxt->xpathCtxt);
623 if (newlist->type != XPATH_NODESET) {
624 xmlXPathFreeObject(newlist);
628 if ((parent == NULL) || (node->doc == NULL))
631 while (parent->parent != NULL)
632 parent = parent->parent;
633 if (((parent->type != XML_DOCUMENT_NODE) &&
634 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
635 (parent != (xmlNodePtr) node->doc))
640 xmlXPathFreeObject(list);
643 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra) =
645 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
647 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
648 (xmlFreeFunc) xmlXPathFreeObject;
652 if ((list->nodesetval == NULL) ||
653 (list->nodesetval->nodeNr <= 0)) {
655 xmlXPathFreeObject(list);
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_ELEMENT_NODE:
939 case XML_CDATA_SECTION_NODE:
941 case XML_COMMENT_NODE:
954 * xsltTestCompMatchList:
955 * @ctxt: a XSLT process context
957 * @comp: the precompiled pattern list
959 * Test wether the node matches one of the patterns in the list
961 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
964 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
965 xsltCompMatchPtr comp) {
968 if ((ctxt == NULL) || (node == NULL))
970 while (comp != NULL) {
971 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
979 /************************************************************************
981 * Dedicated parser for templates *
983 ************************************************************************/
985 #define CUR (*ctxt->cur)
986 #define SKIP(val) ctxt->cur += (val)
987 #define NXT(val) ctxt->cur[(val)]
988 #define CUR_PTR ctxt->cur
990 #define SKIP_BLANKS \
991 while (IS_BLANK(CUR)) NEXT
993 #define CURRENT (*ctxt->cur)
994 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
997 #define PUSH(op, val, val2) \
998 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
1001 xsltSwapTopCompMatch(ctxt->comp);
1003 #define XSLT_ERROR(X) \
1004 { xsltError(ctxt, __FILE__, __LINE__, X); \
1005 ctxt->error = (X); return; }
1007 #define XSLT_ERROR0(X) \
1008 { xsltError(ctxt, __FILE__, __LINE__, X); \
1009 ctxt->error = (X); return(0); }
1013 * @ctxt: the XPath Parser context
1015 * Parse an XPath Litteral:
1017 * [29] Literal ::= '"' [^"]* '"'
1020 * Returns the Literal parsed or NULL
1024 xsltScanLiteral(xsltParserContextPtr ctxt) {
1025 const xmlChar *q, *cur;
1026 xmlChar *ret = NULL;
1033 val = xmlStringCurrentChar(NULL, cur, &len);
1034 while ((IS_CHAR(val)) && (val != '"')) {
1036 val = xmlStringCurrentChar(NULL, cur, &len);
1038 if (!IS_CHAR(val)) {
1042 ret = xmlStrndup(q, cur - q);
1046 } else if (CUR == '\'') {
1049 val = xmlStringCurrentChar(NULL, cur, &len);
1050 while ((IS_CHAR(val)) && (val != '\'')) {
1052 val = xmlStringCurrentChar(NULL, cur, &len);
1054 if (!IS_CHAR(val)) {
1058 ret = xmlStrndup(q, cur - q);
1063 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1072 * @ctxt: the XPath Parser context
1074 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1075 * CombiningChar | Extender
1077 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1079 * [6] Names ::= Name (S Name)*
1081 * Returns the Name parsed or NULL
1085 xsltScanName(xsltParserContextPtr ctxt) {
1086 const xmlChar *q, *cur;
1087 xmlChar *ret = NULL;
1093 val = xmlStringCurrentChar(NULL, cur, &len);
1094 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1097 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1098 (val == '.') || (val == '-') ||
1100 (IS_COMBINING(val)) ||
1101 (IS_EXTENDER(val))) {
1103 val = xmlStringCurrentChar(NULL, cur, &len);
1105 ret = xmlStrndup(q, cur - q);
1112 * @ctxt: the XPath Parser context
1114 * Parses a non qualified name
1116 * Returns the Name parsed or NULL
1120 xsltScanNCName(xsltParserContextPtr ctxt) {
1121 const xmlChar *q, *cur;
1122 xmlChar *ret = NULL;
1128 val = xmlStringCurrentChar(NULL, cur, &len);
1129 if (!IS_LETTER(val) && (val != '_'))
1132 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1133 (val == '.') || (val == '-') ||
1135 (IS_COMBINING(val)) ||
1136 (IS_EXTENDER(val))) {
1138 val = xmlStringCurrentChar(NULL, cur, &len);
1140 ret = xmlStrndup(q, cur - q);
1147 * @ctxt: the XPath Parser context
1148 * @prefix: the place to store the prefix
1150 * Parse a qualified name
1152 * Returns the Name parsed or NULL
1156 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1157 xmlChar *ret = NULL;
1160 ret = xsltScanNCName(ctxt);
1164 ret = xsltScanNCName(ctxt);
1170 * xsltCompileIdKeyPattern:
1171 * @ctxt: the compilation context
1172 * @name: a preparsed name
1173 * @aid: whether id/key are allowed there
1175 * Compile the XSLT LocationIdKeyPattern
1176 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1177 * | 'key' '(' Literal ',' Literal ')'
1179 * also handle NodeType and PI from:
1181 * [7] NodeTest ::= NameTest
1182 * | NodeType '(' ')'
1183 * | 'processing-instruction' '(' Literal ')'
1186 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1187 xmlChar *lit = NULL;
1188 xmlChar *lit2 = NULL;
1191 xsltTransformError(NULL, NULL, NULL,
1192 "xsltCompileIdKeyPattern : ( expected\n");
1196 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1199 lit = xsltScanLiteral(ctxt);
1204 xsltTransformError(NULL, NULL, NULL,
1205 "xsltCompileIdKeyPattern : ) expected\n");
1210 PUSH(XSLT_OP_ID, lit, NULL);
1211 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1214 lit = xsltScanLiteral(ctxt);
1219 xsltTransformError(NULL, NULL, NULL,
1220 "xsltCompileIdKeyPattern : , expected\n");
1226 lit2 = xsltScanLiteral(ctxt);
1231 xsltTransformError(NULL, NULL, NULL,
1232 "xsltCompileIdKeyPattern : ) expected\n");
1237 /* TODO: support namespace in keys */
1238 PUSH(XSLT_OP_KEY, lit, lit2);
1239 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1243 lit = xsltScanLiteral(ctxt);
1248 xsltTransformError(NULL, NULL, NULL,
1249 "xsltCompileIdKeyPattern : ) expected\n");
1255 PUSH(XSLT_OP_PI, lit, NULL);
1256 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1260 xsltTransformError(NULL, NULL, NULL,
1261 "xsltCompileIdKeyPattern : ) expected\n");
1266 PUSH(XSLT_OP_TEXT, NULL, NULL);
1267 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1271 xsltTransformError(NULL, NULL, NULL,
1272 "xsltCompileIdKeyPattern : ) expected\n");
1277 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1278 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1282 xsltTransformError(NULL, NULL, NULL,
1283 "xsltCompileIdKeyPattern : ) expected\n");
1288 PUSH(XSLT_OP_NODE, NULL, NULL);
1290 xsltTransformError(NULL, NULL, NULL,
1291 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1295 xsltTransformError(NULL, NULL, NULL,
1296 "xsltCompileIdKeyPattern : node type\n");
1306 * xsltCompileStepPattern:
1307 * @ctxt: the compilation context
1308 * @token: a posible precompiled name
1310 * Compile the XSLT StepPattern and generates a precompiled
1311 * form suitable for fast matching.
1313 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1314 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1315 * | ('child' | 'attribute') '::'
1317 * [7] NodeTest ::= NameTest
1318 * | NodeType '(' ')'
1319 * | 'processing-instruction' '(' Literal ')'
1320 * [8] Predicate ::= '[' PredicateExpr ']'
1321 * [9] PredicateExpr ::= Expr
1322 * [13] AbbreviatedAxisSpecifier ::= '@'?
1323 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1327 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1328 xmlChar *name = NULL;
1329 const xmlChar *URI = NULL;
1330 xmlChar *URL = NULL;
1334 if ((token == NULL) && (CUR == '@')) {
1335 xmlChar *prefix = NULL;
1340 PUSH(XSLT_OP_ATTR, NULL, NULL);
1343 token = xsltScanQName(ctxt, &prefix);
1344 if (prefix != NULL) {
1347 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1349 xsltTransformError(NULL, NULL, NULL,
1350 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1353 URL = xmlStrdup(ns->href);
1357 if (token == NULL) {
1360 PUSH(XSLT_OP_ATTR, NULL, URL);
1363 xsltTransformError(NULL, NULL, NULL,
1364 "xsltCompileStepPattern : Name expected\n");
1368 PUSH(XSLT_OP_ATTR, token, URL);
1369 goto parse_predicate;
1372 token = xsltScanName(ctxt);
1373 if (token == NULL) {
1376 PUSH(XSLT_OP_ALL, token, NULL);
1377 goto parse_predicate;
1379 xsltTransformError(NULL, NULL, NULL,
1380 "xsltCompileStepPattern : Name expected\n");
1389 xsltCompileIdKeyPattern(ctxt, token, 0);
1392 } else if (CUR == ':') {
1395 xmlChar *prefix = token;
1399 * This is a namespace match
1401 token = xsltScanName(ctxt);
1402 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1404 xsltTransformError(NULL, NULL, NULL,
1405 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1410 URL = xmlStrdup(ns->href);
1413 if (token == NULL) {
1416 PUSH(XSLT_OP_NS, URL, NULL);
1418 xsltTransformError(NULL, NULL, NULL,
1419 "xsltCompileStepPattern : Name expected\n");
1424 PUSH(XSLT_OP_ELEM, token, URL);
1428 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1430 token = xsltScanName(ctxt);
1431 if (token == NULL) {
1434 PUSH(XSLT_OP_ALL, token, NULL);
1435 goto parse_predicate;
1437 xsltTransformError(NULL, NULL, NULL,
1438 "xsltCompileStepPattern : QName expected\n");
1443 URI = xsltGetQNameURI(ctxt->elem, &token);
1444 if (token == NULL) {
1448 name = xmlStrdup(token);
1450 URL = xmlStrdup(URI);
1452 PUSH(XSLT_OP_CHILD, name, URL);
1453 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1455 token = xsltScanName(ctxt);
1456 if (token == NULL) {
1457 xsltTransformError(NULL, NULL, NULL,
1458 "xsltCompileStepPattern : QName expected\n");
1462 URI = xsltGetQNameURI(ctxt->elem, &token);
1463 if (token == NULL) {
1467 name = xmlStrdup(token);
1469 URL = xmlStrdup(URI);
1471 PUSH(XSLT_OP_ATTR, name, URL);
1473 xsltTransformError(NULL, NULL, NULL,
1474 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1480 } else if (CUR == '*') {
1482 PUSH(XSLT_OP_ALL, token, NULL);
1484 URI = xsltGetQNameURI(ctxt->elem, &token);
1485 if (token == NULL) {
1490 URL = xmlStrdup(URI);
1491 PUSH(XSLT_OP_ELEM, token, URL);
1496 while (CUR == '[') {
1498 xmlChar *ret = NULL;
1504 /* Skip over nested predicates */
1507 else if (CUR == ']') {
1511 } else if (CUR == '"') {
1513 while ((CUR != 0) && (CUR != '"'))
1515 } else if (CUR == '\'') {
1517 while ((CUR != 0) && (CUR != '\''))
1523 xsltTransformError(NULL, NULL, NULL,
1524 "xsltCompileStepPattern : ']' expected\n");
1528 ret = xmlStrndup(q, CUR_PTR - q);
1529 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1530 /* push the predicate lower than local test */
1544 * xsltCompileRelativePathPattern:
1545 * @comp: the compilation context
1546 * @token: a posible precompiled name
1548 * Compile the XSLT RelativePathPattern and generates a precompiled
1549 * form suitable for fast matching.
1551 * [4] RelativePathPattern ::= StepPattern
1552 * | RelativePathPattern '/' StepPattern
1553 * | RelativePathPattern '//' StepPattern
1556 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1557 xsltCompileStepPattern(ctxt, token);
1561 while ((CUR != 0) && (CUR != '|')) {
1562 if ((CUR == '/') && (NXT(1) == '/')) {
1563 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1567 xsltCompileStepPattern(ctxt, NULL);
1568 } else if (CUR == '/') {
1569 PUSH(XSLT_OP_PARENT, NULL, NULL);
1572 if ((CUR != 0) || (CUR == '|')) {
1573 xsltCompileRelativePathPattern(ctxt, NULL);
1587 * xsltCompileLocationPathPattern:
1588 * @ctxt: the compilation context
1590 * Compile the XSLT LocationPathPattern and generates a precompiled
1591 * form suitable for fast matching.
1593 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1594 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1595 * | '//'? RelativePathPattern
1598 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1600 if ((CUR == '/') && (NXT(1) == '/')) {
1602 * since we reverse the query
1603 * a leading // can be safely ignored
1607 xsltCompileRelativePathPattern(ctxt, NULL);
1608 } else if (CUR == '/') {
1610 * We need to find root as the parent
1614 PUSH(XSLT_OP_ROOT, NULL, NULL);
1615 if ((CUR != 0) || (CUR == '|')) {
1616 PUSH(XSLT_OP_PARENT, NULL, NULL);
1617 xsltCompileRelativePathPattern(ctxt, NULL);
1619 } else if (CUR == '*') {
1620 xsltCompileRelativePathPattern(ctxt, NULL);
1621 } else if (CUR == '@') {
1622 xsltCompileRelativePathPattern(ctxt, NULL);
1625 name = xsltScanName(ctxt);
1627 xsltTransformError(NULL, NULL, NULL,
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 xsltTransformError(NULL, NULL, node,
1681 "xsltCompilePattern : NULL pattern\n");
1685 ctxt = xsltNewParserContext(style, runtime);
1691 while (pattern[current] != 0) {
1693 while (IS_BLANK(pattern[current]))
1697 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1698 if (pattern[end] == '[')
1700 else if (pattern[end] == ']')
1702 else if (pattern[end] == '\'') {
1704 while ((pattern[end] != 0) && (pattern[end] != '\''))
1706 } else if (pattern[end] == '"') {
1708 while ((pattern[end] != 0) && (pattern[end] != '"'))
1713 if (current == end) {
1714 xsltTransformError(NULL, NULL, node,
1715 "xsltCompilePattern : NULL pattern\n");
1718 element = xsltNewCompMatch();
1719 if (element == NULL) {
1724 else if (previous != NULL)
1725 previous->next = element;
1728 ctxt->comp = element;
1729 ctxt->base = xmlStrndup(&pattern[start], end - start);
1730 if (ctxt->base == NULL)
1732 ctxt->cur = &(ctxt->base)[current - start];
1733 element->pattern = ctxt->base;
1734 element->nsList = xmlGetNsList(doc, node);
1736 if (element->nsList != NULL) {
1737 while (element->nsList[j] != NULL)
1743 #ifdef WITH_XSLT_DEBUG_PATTERN
1744 xsltGenericDebug(xsltGenericDebugContext,
1745 "xsltCompilePattern : parsing '%s'\n",
1748 xsltCompileLocationPathPattern(ctxt);
1750 xsltTransformError(NULL, style, node,
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 xsltTransformError(NULL, style, node,
1813 "xsltCompilePattern : NULL pattern\n");
1818 xsltFreeParserContext(ctxt);
1823 xsltFreeParserContext(ctxt);
1825 xsltFreeCompMatchList(first);
1829 /************************************************************************
1831 * Module interfaces *
1833 ************************************************************************/
1837 * @style: an XSLT stylesheet
1838 * @cur: an XSLT template
1839 * @mode: the mode name or NULL
1840 * @modeURI: the mode URI or NULL
1842 * Register the XSLT pattern associated to @cur
1844 * Returns -1 in case of error, 0 otherwise
1847 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1848 const xmlChar *mode, const xmlChar *modeURI) {
1849 xsltCompMatchPtr pat, list, *top = NULL, next;
1850 const xmlChar *name = NULL;
1851 float priority; /* the priority */
1853 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1856 priority = cur->priority;
1857 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1863 pat->template = cur;
1865 pat->mode = xmlStrdup(mode);
1866 if (modeURI != NULL)
1867 pat->modeURI = xmlStrdup(modeURI);
1868 if (priority != XSLT_PAT_NO_PRIORITY)
1869 pat->priority = priority;
1872 * insert it in the hash table list corresponding to its lookup name
1874 switch (pat->steps[0].op) {
1876 if (pat->steps[0].value != NULL)
1877 name = pat->steps[0].value;
1879 top = (xsltCompMatchPtr *) &(style->attrMatch);
1882 case XSLT_OP_PARENT:
1883 case XSLT_OP_ANCESTOR:
1884 top = (xsltCompMatchPtr *) &(style->elemMatch);
1887 top = (xsltCompMatchPtr *) &(style->rootMatch);
1890 top = (xsltCompMatchPtr *) &(style->keyMatch);
1893 /* TODO optimize ID !!! */
1896 top = (xsltCompMatchPtr *) &(style->elemMatch);
1899 case XSLT_OP_PREDICATE:
1900 xsltTransformError(NULL, style, NULL,
1901 "xsltAddTemplate: invalid compiled pattern\n");
1902 xsltFreeCompMatch(pat);
1905 * TODO: some flags at the top level about type based patterns
1906 * would be faster than inclusion in the hash table.
1909 if (pat->steps[0].value != NULL)
1910 name = pat->steps[0].value;
1912 top = (xsltCompMatchPtr *) &(style->piMatch);
1914 case XSLT_OP_COMMENT:
1915 top = (xsltCompMatchPtr *) &(style->commentMatch);
1918 top = (xsltCompMatchPtr *) &(style->textMatch);
1922 if (pat->steps[0].value != NULL)
1923 name = pat->steps[0].value;
1925 top = (xsltCompMatchPtr *) &(style->elemMatch);
1929 if (style->templatesHash == NULL) {
1930 style->templatesHash = xmlHashCreate(1024);
1931 if (style->templatesHash == NULL) {
1932 xsltFreeCompMatch(pat);
1935 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1937 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1938 name, mode, modeURI);
1940 xmlHashAddEntry3(style->templatesHash, name,
1941 mode, modeURI, pat);
1944 * Note '<=' since one must choose among the matching
1945 * template rules that are left, the one that occurs
1946 * last in the stylesheet
1948 if (list->priority <= pat->priority) {
1950 xmlHashUpdateEntry3(style->templatesHash, name,
1951 mode, modeURI, pat, NULL);
1953 while (list->next != NULL) {
1954 if (list->next->priority <= pat->priority)
1958 pat->next = list->next;
1963 } else if (top != NULL) {
1968 } else if (list->priority <= pat->priority) {
1972 while (list->next != NULL) {
1973 if (list->next->priority <= pat->priority)
1977 pat->next = list->next;
1981 xsltTransformError(NULL, style, NULL,
1982 "xsltAddTemplate: invalid compiled pattern\n");
1983 xsltFreeCompMatch(pat);
1986 #ifdef WITH_XSLT_DEBUG_PATTERN
1988 xsltGenericDebug(xsltGenericDebugContext,
1989 "added pattern : '%s' mode '%s' priority %f\n",
1990 pat->pattern, pat->mode, pat->priority);
1992 xsltGenericDebug(xsltGenericDebugContext,
1993 "added pattern : '%s' priority %f\n",
1994 pat->pattern, pat->priority);
2004 * @ctxt: a XSLT process context
2005 * @node: the node being processed
2006 * @style: the current style
2008 * Finds the template applying to this node, if @style is non-NULL
2009 * it means one needs to look for the next imported template in scope.
2011 * Returns the xsltTemplatePtr or NULL if not found
2014 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2015 xsltStylesheetPtr style) {
2016 xsltStylesheetPtr curstyle;
2017 xsltTemplatePtr ret = NULL;
2018 const xmlChar *name = NULL;
2019 xsltCompMatchPtr list = NULL;
2022 if ((ctxt == NULL) || (node == NULL))
2025 if (style == NULL) {
2026 curstyle = ctxt->style;
2028 curstyle = xsltNextImport(style);
2031 while ((curstyle != NULL) && (curstyle != style)) {
2032 priority = XSLT_PAT_NO_PRIORITY;
2033 /* TODO : handle IDs/keys here ! */
2034 if (curstyle->templatesHash != NULL) {
2036 * Use the top name as selector
2038 switch (node->type) {
2039 case XML_ELEMENT_NODE:
2040 case XML_ATTRIBUTE_NODE:
2044 case XML_DOCUMENT_NODE:
2045 case XML_HTML_DOCUMENT_NODE:
2047 case XML_CDATA_SECTION_NODE:
2048 case XML_COMMENT_NODE:
2049 case XML_ENTITY_REF_NODE:
2050 case XML_ENTITY_NODE:
2051 case XML_DOCUMENT_TYPE_NODE:
2052 case XML_DOCUMENT_FRAG_NODE:
2053 case XML_NOTATION_NODE:
2055 case XML_ELEMENT_DECL:
2056 case XML_ATTRIBUTE_DECL:
2057 case XML_ENTITY_DECL:
2058 case XML_NAMESPACE_DECL:
2059 case XML_XINCLUDE_START:
2060 case XML_XINCLUDE_END:
2069 * find the list of appliable expressions based on the name
2071 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2072 name, ctxt->mode, ctxt->modeURI);
2075 while (list != NULL) {
2076 if (xsltTestCompMatch(ctxt, list, node,
2077 ctxt->mode, ctxt->modeURI)) {
2078 ret = list->template;
2079 priority = list->priority;
2087 * find alternate generic matches
2089 switch (node->type) {
2090 case XML_ELEMENT_NODE:
2091 list = curstyle->elemMatch;
2093 case XML_ATTRIBUTE_NODE:
2094 list = curstyle->attrMatch;
2097 list = curstyle->piMatch;
2099 case XML_DOCUMENT_NODE:
2100 case XML_HTML_DOCUMENT_NODE:
2101 list = curstyle->rootMatch;
2104 case XML_CDATA_SECTION_NODE:
2105 list = curstyle->textMatch;
2107 case XML_COMMENT_NODE:
2108 list = curstyle->commentMatch;
2110 case XML_ENTITY_REF_NODE:
2111 case XML_ENTITY_NODE:
2112 case XML_DOCUMENT_TYPE_NODE:
2113 case XML_DOCUMENT_FRAG_NODE:
2114 case XML_NOTATION_NODE:
2116 case XML_ELEMENT_DECL:
2117 case XML_ATTRIBUTE_DECL:
2118 case XML_ENTITY_DECL:
2119 case XML_NAMESPACE_DECL:
2120 case XML_XINCLUDE_START:
2121 case XML_XINCLUDE_END:
2127 while ((list != NULL) &&
2128 ((ret == NULL) || (list->priority > priority))) {
2129 if (xsltTestCompMatch(ctxt, list, node,
2130 ctxt->mode, ctxt->modeURI)) {
2131 ret = list->template;
2132 priority = list->priority;
2138 * Some of the tests for elements can also apply to documents
2140 if ((node->type == XML_DOCUMENT_NODE) ||
2141 (node->type == XML_HTML_DOCUMENT_NODE) ||
2142 (node->type == XML_TEXT_NODE)) {
2143 list = curstyle->elemMatch;
2144 while ((list != NULL) &&
2145 ((ret == NULL) || (list->priority > priority))) {
2146 if (xsltTestCompMatch(ctxt, list, node,
2147 ctxt->mode, ctxt->modeURI)) {
2148 ret = list->template;
2149 priority = list->priority;
2156 if (node->_private != NULL) {
2157 list = curstyle->keyMatch;
2158 while ((list != NULL) &&
2159 ((ret == NULL) || (list->priority > priority))) {
2160 if (xsltTestCompMatch(ctxt, list, node,
2161 ctxt->mode, ctxt->modeURI)) {
2162 ret = list->template;
2163 priority = list->priority;
2173 * Cycle on next curstylesheet import.
2175 curstyle = xsltNextImport(curstyle);
2181 * xsltCleanupTemplates:
2182 * @style: an XSLT stylesheet
2184 * Cleanup the state of the templates used by the stylesheet and
2185 * the ones it imports.
2188 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2192 * xsltFreeTemplateHashes:
2193 * @style: an XSLT stylesheet
2195 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2198 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2199 if (style->templatesHash != NULL)
2200 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2201 (xmlHashDeallocator) xsltFreeCompMatchList);
2202 if (style->rootMatch != NULL)
2203 xsltFreeCompMatchList(style->rootMatch);
2204 if (style->keyMatch != NULL)
2205 xsltFreeCompMatchList(style->keyMatch);
2206 if (style->elemMatch != NULL)
2207 xsltFreeCompMatchList(style->elemMatch);
2208 if (style->attrMatch != NULL)
2209 xsltFreeCompMatchList(style->attrMatch);
2210 if (style->parentMatch != NULL)
2211 xsltFreeCompMatchList(style->parentMatch);
2212 if (style->textMatch != NULL)
2213 xsltFreeCompMatchList(style->textMatch);
2214 if (style->piMatch != NULL)
2215 xsltFreeCompMatchList(style->piMatch);
2216 if (style->commentMatch != NULL)
2217 xsltFreeCompMatchList(style->commentMatch);
2223 * @node: a node in the source tree
2224 * @pattern: an XSLT pattern
2225 * @ctxtdoc: context document (for namespaces)
2226 * @ctxtnode: context node (for namespaces)
2228 * Determine if a node matches a pattern.
2231 xsltMatchPattern(xsltTransformContextPtr context,
2233 const xmlChar *pattern,
2235 xmlNodePtr ctxtnode)
2238 xsltCompMatchPtr first, comp;
2240 if ((context != NULL) && (pattern != NULL)) {
2241 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2242 for (comp = first; comp != NULL; comp = comp->next) {
2243 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2248 xsltFreeCompMatchList(first);