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))
403 if ((node->type == XML_ELEMENT_NODE) && (node->name[0] == ' '))
407 if (node->type != XML_ELEMENT_NODE)
409 if (step->value == NULL)
411 if (step->value[0] != node->name[0])
413 if (!xmlStrEqual(step->value, node->name))
417 if (node->ns == NULL) {
418 if (step->value2 != NULL)
420 } else if (node->ns->href != NULL) {
421 if (step->value2 == NULL)
423 if (!xmlStrEqual(step->value2, node->ns->href))
427 case XSLT_OP_CHILD: {
430 if ((node->type != XML_ELEMENT_NODE) &&
431 (node->type != XML_DOCUMENT_NODE) &&
432 #ifdef LIBXML_DOCB_ENABLED
433 (node->type != XML_DOCB_DOCUMENT_NODE) &&
435 (node->type != XML_HTML_DOCUMENT_NODE))
438 lst = node->children;
440 if (step->value != NULL) {
441 while (lst != NULL) {
442 if ((lst->type == XML_ELEMENT_NODE) &&
443 (step->value[0] == lst->name[0]) &&
444 (xmlStrEqual(step->value, lst->name)))
454 if (node->type != XML_ATTRIBUTE_NODE)
456 if (step->value != NULL) {
457 if (step->value[0] != node->name[0])
459 if (!xmlStrEqual(step->value, node->name))
463 if (node->ns == NULL) {
464 if (step->value2 != NULL)
466 } else if (step->value2 != NULL) {
467 if (!xmlStrEqual(step->value2, node->ns->href))
472 if ((node->type == XML_DOCUMENT_NODE) ||
473 (node->type == XML_HTML_DOCUMENT_NODE) ||
474 #ifdef LIBXML_DOCB_ENABLED
475 (node->type == XML_DOCB_DOCUMENT_NODE) ||
477 (node->type == XML_NAMESPACE_DECL))
482 if (step->value == NULL)
484 if (step->value[0] != node->name[0])
486 if (!xmlStrEqual(step->value, node->name))
489 if (node->ns == NULL) {
490 if (step->value2 != NULL)
492 } else if (node->ns->href != NULL) {
493 if (step->value2 == NULL)
495 if (!xmlStrEqual(step->value2, node->ns->href))
499 case XSLT_OP_ANCESTOR:
500 /* TODO: implement coalescing of ANCESTOR/NODE ops */
501 if (step->value == NULL) {
503 step = &comp->steps[i];
504 if (step->op == XSLT_OP_ROOT)
506 if (step->op != XSLT_OP_ELEM)
508 if (step->value == NULL)
513 if ((node->type == XML_DOCUMENT_NODE) ||
514 (node->type == XML_HTML_DOCUMENT_NODE) ||
515 #ifdef LIBXML_DOCB_ENABLED
516 (node->type == XML_DOCB_DOCUMENT_NODE) ||
518 (node->type == XML_NAMESPACE_DECL))
521 while (node != NULL) {
524 if ((node->type == XML_ELEMENT_NODE) &&
525 (step->value[0] == node->name[0]) &&
526 (xmlStrEqual(step->value, node->name))) {
528 if (node->ns == NULL) {
529 if (step->value2 == NULL)
531 } else if (node->ns->href != NULL) {
532 if ((step->value2 != NULL) &&
533 (xmlStrEqual(step->value2, node->ns->href)))
543 /* TODO Handle IDs decently, must be done differently */
546 if (node->type != XML_ELEMENT_NODE)
549 id = xmlGetID(node->doc, step->value);
550 if ((id == NULL) || (id->parent != node))
558 list = xsltGetKey(ctxt, step->value,
559 step->value3, step->value2);
562 for (indx = 0;indx < list->nodeNr;indx++)
563 if (list->nodeTab[indx] == node)
565 if (indx >= list->nodeNr)
570 if (node->type != XML_ELEMENT_NODE)
572 if (node->ns == NULL) {
573 if (step->value != NULL)
575 } else if (node->ns->href != NULL) {
576 if (step->value == NULL)
578 if (!xmlStrEqual(step->value, node->ns->href))
583 if (node->type != XML_ELEMENT_NODE)
586 case XSLT_OP_PREDICATE: {
589 int pos = 0, len = 0;
591 * The simple existing predicate code cannot handle
592 * properly cascaded predicates. If in this situation
593 * compute directly the full node list once and check
594 * if the node is in the result list.
596 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
597 xmlDocPtr prevdoc, doc;
598 xmlXPathObjectPtr list;
602 prevdoc = (xmlDocPtr)
603 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
605 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
606 list = (xmlXPathObjectPtr)
607 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra);
610 if ((list == NULL) || (prevdoc != doc)) {
612 xmlXPathObjectPtr newlist;
613 xmlNodePtr parent = node->parent;
615 if (comp->pattern[0] == '/')
616 query = xmlStrdup(comp->pattern);
618 query = xmlStrdup((const xmlChar *)"//");
619 query = xmlStrcat(query, comp->pattern);
621 newlist = xmlXPathEval(query, ctxt->xpathCtxt);
625 if (newlist->type != XPATH_NODESET) {
626 xmlXPathFreeObject(newlist);
630 if ((parent == NULL) || (node->doc == NULL))
633 while (parent->parent != NULL)
634 parent = parent->parent;
635 if (((parent->type != XML_DOCUMENT_NODE) &&
636 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
637 (parent != (xmlNodePtr) node->doc))
642 xmlXPathFreeObject(list);
645 XSLT_RUNTIME_EXTRA_LST(ctxt, select->lenExtra) =
647 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
649 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
650 (xmlFreeFunc) xmlXPathFreeObject;
654 if ((list->nodesetval == NULL) ||
655 (list->nodesetval->nodeNr <= 0)) {
657 xmlXPathFreeObject(list);
660 /* TODO: store the index and use it for the scan */
662 for (j = 0;j < list->nodesetval->nodeNr;j++) {
663 if (list->nodesetval->nodeTab[j] == node) {
665 xmlXPathFreeObject(list);
672 xmlXPathFreeObject(list);
676 * Depending on the last selection, one may need to
677 * recompute contextSize and proximityPosition.
679 * TODO: make this thread safe !
681 oldCS = ctxt->xpathCtxt->contextSize;
682 oldCP = ctxt->xpathCtxt->proximityPosition;
683 if ((select != NULL) &&
684 (select->op == XSLT_OP_ELEM) &&
685 (select->value != NULL) &&
686 (node->type == XML_ELEMENT_NODE) &&
687 (node->parent != NULL)) {
689 int index, nocache = 0;
691 previous = (xmlNodePtr)
692 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
694 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
695 if ((previous != NULL) &&
696 (previous->parent == node->parent)) {
698 * just walk back to adjust the index
701 xmlNodePtr sibling = node;
703 while (sibling != NULL) {
704 if (sibling == previous)
706 if ((node->type == XML_ELEMENT_NODE) &&
707 (node->name != NULL) &&
708 (sibling->name != NULL) &&
709 (node->name[0] == sibling->name[0]) &&
710 (xmlStrEqual(node->name, sibling->name))) {
711 if ((select->value2 == NULL) ||
712 ((sibling->ns != NULL) &&
713 (xmlStrEqual(select->value2,
714 sibling->ns->href))))
717 sibling = sibling->prev;
719 if (sibling == NULL) {
720 /* hum going backward in document order ... */
723 while (sibling != NULL) {
724 if (sibling == previous)
726 if ((select->value2 == NULL) ||
727 ((sibling->ns != NULL) &&
728 (xmlStrEqual(select->value2,
729 sibling->ns->href))))
731 sibling = sibling->next;
734 if (sibling != NULL) {
737 * If the node is in a Value Tree we cannot
740 if (node->doc != NULL) {
742 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
743 XSLT_RUNTIME_EXTRA(ctxt,
744 select->previousExtra) = node;
745 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
753 * recompute the index
755 xmlNodePtr siblings = node->parent->children;
756 xmlNodePtr parent = node->parent;
758 while (siblings != NULL) {
759 if (siblings->type == XML_ELEMENT_NODE) {
760 if (siblings == node) {
763 } else if ((node->name != NULL) &&
764 (siblings->name != NULL) &&
765 (node->name[0] == siblings->name[0]) &&
766 (xmlStrEqual(node->name, siblings->name))) {
767 if ((select->value2 == NULL) ||
768 ((siblings->ns != NULL) &&
769 (xmlStrEqual(select->value2,
770 siblings->ns->href))))
774 siblings = siblings->next;
776 if ((parent == NULL) || (node->doc == NULL))
779 while (parent->parent != NULL)
780 parent = parent->parent;
781 if (((parent->type != XML_DOCUMENT_NODE) &&
782 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
783 (parent != (xmlNodePtr) node->doc))
788 ctxt->xpathCtxt->contextSize = len;
789 ctxt->xpathCtxt->proximityPosition = pos;
791 * If the node is in a Value Tree we cannot
794 if ((node->doc != NULL) && (nocache == 0)) {
795 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
797 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
799 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
803 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
804 (node->type == XML_ELEMENT_NODE)) {
806 int index, nocache = 0;
808 previous = (xmlNodePtr)
809 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
811 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
812 if ((previous != NULL) &&
813 (previous->parent == node->parent)) {
815 * just walk back to adjust the index
818 xmlNodePtr sibling = node;
820 while (sibling != NULL) {
821 if (sibling == previous)
823 if (sibling->type == XML_ELEMENT_NODE)
825 sibling = sibling->prev;
827 if (sibling == NULL) {
828 /* hum going backward in document order ... */
831 while (sibling != NULL) {
832 if (sibling == previous)
834 if (sibling->type == XML_ELEMENT_NODE)
836 sibling = sibling->next;
839 if (sibling != NULL) {
842 * If the node is in a Value Tree we cannot
845 if (node->doc != NULL) {
847 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
848 XSLT_RUNTIME_EXTRA(ctxt,
849 select->previousExtra) = node;
850 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
857 * recompute the index
859 xmlNodePtr siblings = node->parent->children;
860 xmlNodePtr parent = node->parent;
862 while (siblings != NULL) {
863 if (siblings->type == XML_ELEMENT_NODE) {
865 if (siblings == node) {
869 siblings = siblings->next;
871 if ((parent == NULL) || (node->doc == NULL))
874 while (parent->parent != NULL)
875 parent = parent->parent;
876 if (((parent->type != XML_DOCUMENT_NODE) &&
877 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
878 (parent != (xmlNodePtr) node->doc))
883 ctxt->xpathCtxt->contextSize = len;
884 ctxt->xpathCtxt->proximityPosition = pos;
886 * If the node is in a Value Tree we cannot
889 if ((node->doc != NULL) && (nocache == 0)) {
890 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
892 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
894 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
899 oldNode = ctxt->node;
902 if (step->value == NULL)
904 if (step->comp == NULL)
907 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
912 ctxt->xpathCtxt->contextSize = oldCS;
913 ctxt->xpathCtxt->proximityPosition = oldCP;
915 ctxt->node = oldNode;
919 ctxt->xpathCtxt->contextSize = oldCS;
920 ctxt->xpathCtxt->proximityPosition = oldCP;
922 ctxt->node = oldNode;
926 if (node->type != XML_PI_NODE)
928 if (step->value != NULL) {
929 if (!xmlStrEqual(step->value, node->name))
933 case XSLT_OP_COMMENT:
934 if (node->type != XML_COMMENT_NODE)
938 if ((node->type != XML_TEXT_NODE) &&
939 (node->type != XML_CDATA_SECTION_NODE))
943 switch (node->type) {
944 case XML_ELEMENT_NODE:
945 case XML_CDATA_SECTION_NODE:
947 case XML_COMMENT_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 xsltTransformError(NULL, NULL, NULL,
1198 "xsltCompileIdKeyPattern : ( expected\n");
1202 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1205 lit = xsltScanLiteral(ctxt);
1210 xsltTransformError(NULL, NULL, NULL,
1211 "xsltCompileIdKeyPattern : ) expected\n");
1216 PUSH(XSLT_OP_ID, lit, NULL);
1217 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1220 lit = xsltScanLiteral(ctxt);
1225 xsltTransformError(NULL, NULL, NULL,
1226 "xsltCompileIdKeyPattern : , expected\n");
1232 lit2 = xsltScanLiteral(ctxt);
1237 xsltTransformError(NULL, NULL, NULL,
1238 "xsltCompileIdKeyPattern : ) expected\n");
1243 /* TODO: support namespace in keys */
1244 PUSH(XSLT_OP_KEY, lit, lit2);
1245 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1249 lit = xsltScanLiteral(ctxt);
1254 xsltTransformError(NULL, NULL, NULL,
1255 "xsltCompileIdKeyPattern : ) expected\n");
1261 PUSH(XSLT_OP_PI, lit, NULL);
1262 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1266 xsltTransformError(NULL, NULL, NULL,
1267 "xsltCompileIdKeyPattern : ) expected\n");
1272 PUSH(XSLT_OP_TEXT, NULL, NULL);
1273 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1277 xsltTransformError(NULL, NULL, NULL,
1278 "xsltCompileIdKeyPattern : ) expected\n");
1283 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1284 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1288 xsltTransformError(NULL, NULL, NULL,
1289 "xsltCompileIdKeyPattern : ) expected\n");
1294 PUSH(XSLT_OP_NODE, NULL, NULL);
1296 xsltTransformError(NULL, NULL, NULL,
1297 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1301 xsltTransformError(NULL, NULL, NULL,
1302 "xsltCompileIdKeyPattern : node type\n");
1312 * xsltCompileStepPattern:
1313 * @ctxt: the compilation context
1314 * @token: a posible precompiled name
1316 * Compile the XSLT StepPattern and generates a precompiled
1317 * form suitable for fast matching.
1319 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1320 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1321 * | ('child' | 'attribute') '::'
1323 * [7] NodeTest ::= NameTest
1324 * | NodeType '(' ')'
1325 * | 'processing-instruction' '(' Literal ')'
1326 * [8] Predicate ::= '[' PredicateExpr ']'
1327 * [9] PredicateExpr ::= Expr
1328 * [13] AbbreviatedAxisSpecifier ::= '@'?
1329 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1333 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1334 xmlChar *name = NULL;
1335 const xmlChar *URI = NULL;
1336 xmlChar *URL = NULL;
1340 if ((token == NULL) && (CUR == '@')) {
1341 xmlChar *prefix = NULL;
1346 PUSH(XSLT_OP_ATTR, NULL, NULL);
1347 goto parse_predicate;
1349 token = xsltScanQName(ctxt, &prefix);
1350 if (prefix != NULL) {
1353 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1355 xsltTransformError(NULL, NULL, NULL,
1356 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1359 URL = xmlStrdup(ns->href);
1363 if (token == NULL) {
1366 PUSH(XSLT_OP_ATTR, NULL, URL);
1369 xsltTransformError(NULL, NULL, NULL,
1370 "xsltCompileStepPattern : Name expected\n");
1374 PUSH(XSLT_OP_ATTR, token, URL);
1375 goto parse_predicate;
1378 token = xsltScanName(ctxt);
1379 if (token == NULL) {
1382 PUSH(XSLT_OP_ALL, token, NULL);
1383 goto parse_predicate;
1385 xsltTransformError(NULL, NULL, NULL,
1386 "xsltCompileStepPattern : Name expected\n");
1395 xsltCompileIdKeyPattern(ctxt, token, 0);
1398 } else if (CUR == ':') {
1401 xmlChar *prefix = token;
1405 * This is a namespace match
1407 token = xsltScanName(ctxt);
1408 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1410 xsltTransformError(NULL, NULL, NULL,
1411 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1416 URL = xmlStrdup(ns->href);
1419 if (token == NULL) {
1422 PUSH(XSLT_OP_NS, URL, NULL);
1424 xsltTransformError(NULL, NULL, NULL,
1425 "xsltCompileStepPattern : Name expected\n");
1430 PUSH(XSLT_OP_ELEM, token, URL);
1434 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1436 token = xsltScanName(ctxt);
1437 if (token == NULL) {
1440 PUSH(XSLT_OP_ALL, token, NULL);
1441 goto parse_predicate;
1443 xsltTransformError(NULL, NULL, NULL,
1444 "xsltCompileStepPattern : QName expected\n");
1449 URI = xsltGetQNameURI(ctxt->elem, &token);
1450 if (token == NULL) {
1454 name = xmlStrdup(token);
1456 URL = xmlStrdup(URI);
1458 PUSH(XSLT_OP_CHILD, name, URL);
1459 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1461 token = xsltScanName(ctxt);
1462 if (token == NULL) {
1463 xsltTransformError(NULL, NULL, NULL,
1464 "xsltCompileStepPattern : QName expected\n");
1468 URI = xsltGetQNameURI(ctxt->elem, &token);
1469 if (token == NULL) {
1473 name = xmlStrdup(token);
1475 URL = xmlStrdup(URI);
1477 PUSH(XSLT_OP_ATTR, name, URL);
1479 xsltTransformError(NULL, NULL, NULL,
1480 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1486 } else if (CUR == '*') {
1488 PUSH(XSLT_OP_ALL, token, NULL);
1490 URI = xsltGetQNameURI(ctxt->elem, &token);
1491 if (token == NULL) {
1496 URL = xmlStrdup(URI);
1497 PUSH(XSLT_OP_ELEM, token, URL);
1502 while (CUR == '[') {
1504 xmlChar *ret = NULL;
1510 /* Skip over nested predicates */
1513 else if (CUR == ']') {
1517 } else if (CUR == '"') {
1519 while ((CUR != 0) && (CUR != '"'))
1521 } else if (CUR == '\'') {
1523 while ((CUR != 0) && (CUR != '\''))
1529 xsltTransformError(NULL, NULL, NULL,
1530 "xsltCompileStepPattern : ']' expected\n");
1534 ret = xmlStrndup(q, CUR_PTR - q);
1535 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1536 /* push the predicate lower than local test */
1550 * xsltCompileRelativePathPattern:
1551 * @comp: the compilation context
1552 * @token: a posible precompiled name
1554 * Compile the XSLT RelativePathPattern and generates a precompiled
1555 * form suitable for fast matching.
1557 * [4] RelativePathPattern ::= StepPattern
1558 * | RelativePathPattern '/' StepPattern
1559 * | RelativePathPattern '//' StepPattern
1562 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1563 xsltCompileStepPattern(ctxt, token);
1567 while ((CUR != 0) && (CUR != '|')) {
1568 if ((CUR == '/') && (NXT(1) == '/')) {
1569 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1573 xsltCompileStepPattern(ctxt, NULL);
1574 } else if (CUR == '/') {
1575 PUSH(XSLT_OP_PARENT, NULL, NULL);
1578 if ((CUR != 0) || (CUR == '|')) {
1579 xsltCompileRelativePathPattern(ctxt, NULL);
1593 * xsltCompileLocationPathPattern:
1594 * @ctxt: the compilation context
1596 * Compile the XSLT LocationPathPattern and generates a precompiled
1597 * form suitable for fast matching.
1599 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1600 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1601 * | '//'? RelativePathPattern
1604 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1606 if ((CUR == '/') && (NXT(1) == '/')) {
1608 * since we reverse the query
1609 * a leading // can be safely ignored
1613 xsltCompileRelativePathPattern(ctxt, NULL);
1614 } else if (CUR == '/') {
1616 * We need to find root as the parent
1620 PUSH(XSLT_OP_ROOT, NULL, NULL);
1621 if ((CUR != 0) || (CUR == '|')) {
1622 PUSH(XSLT_OP_PARENT, NULL, NULL);
1623 xsltCompileRelativePathPattern(ctxt, NULL);
1625 } else if (CUR == '*') {
1626 xsltCompileRelativePathPattern(ctxt, NULL);
1627 } else if (CUR == '@') {
1628 xsltCompileRelativePathPattern(ctxt, NULL);
1631 name = xsltScanName(ctxt);
1633 xsltTransformError(NULL, NULL, NULL,
1634 "xsltCompileLocationPathPattern : Name expected\n");
1639 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1640 xsltCompileIdKeyPattern(ctxt, name, 1);
1641 if ((CUR == '/') && (NXT(1) == '/')) {
1642 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1646 xsltCompileRelativePathPattern(ctxt, NULL);
1647 } else if (CUR == '/') {
1648 PUSH(XSLT_OP_PARENT, NULL, NULL);
1651 xsltCompileRelativePathPattern(ctxt, NULL);
1655 xsltCompileRelativePathPattern(ctxt, name);
1662 * xsltCompilePattern:
1663 * @pattern: an XSLT pattern
1664 * @doc: the containing document
1665 * @node: the containing element
1666 * @style: the stylesheet
1667 * @runtime: the transformation context, if done at run-time
1669 * Compile the XSLT pattern and generates a list of precompiled form suitable
1670 * for fast matching.
1672 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1674 * Returns the generated pattern list or NULL in case of failure
1678 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1679 xmlNodePtr node, xsltStylesheetPtr style,
1680 xsltTransformContextPtr runtime) {
1681 xsltParserContextPtr ctxt = NULL;
1682 xsltCompMatchPtr element, first = NULL, previous = NULL;
1683 int current, start, end, level, j;
1685 if (pattern == NULL) {
1686 xsltTransformError(NULL, NULL, node,
1687 "xsltCompilePattern : NULL pattern\n");
1691 ctxt = xsltNewParserContext(style, runtime);
1697 while (pattern[current] != 0) {
1699 while (IS_BLANK(pattern[current]))
1703 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1704 if (pattern[end] == '[')
1706 else if (pattern[end] == ']')
1708 else if (pattern[end] == '\'') {
1710 while ((pattern[end] != 0) && (pattern[end] != '\''))
1712 } else if (pattern[end] == '"') {
1714 while ((pattern[end] != 0) && (pattern[end] != '"'))
1719 if (current == end) {
1720 xsltTransformError(NULL, NULL, node,
1721 "xsltCompilePattern : NULL pattern\n");
1724 element = xsltNewCompMatch();
1725 if (element == NULL) {
1730 else if (previous != NULL)
1731 previous->next = element;
1734 ctxt->comp = element;
1735 ctxt->base = xmlStrndup(&pattern[start], end - start);
1736 if (ctxt->base == NULL)
1738 ctxt->cur = &(ctxt->base)[current - start];
1739 element->pattern = ctxt->base;
1740 element->nsList = xmlGetNsList(doc, node);
1742 if (element->nsList != NULL) {
1743 while (element->nsList[j] != NULL)
1749 #ifdef WITH_XSLT_DEBUG_PATTERN
1750 xsltGenericDebug(xsltGenericDebugContext,
1751 "xsltCompilePattern : parsing '%s'\n",
1754 xsltCompileLocationPathPattern(ctxt);
1756 xsltTransformError(NULL, style, node,
1757 "xsltCompilePattern : failed to compile '%s'\n",
1764 * Reverse for faster interpretation.
1766 xsltReverseCompMatch(element);
1769 * Set-up the priority
1771 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1772 (element->steps[0].op == XSLT_OP_ATTR)) &&
1773 (element->steps[0].value != NULL) &&
1774 (element->steps[1].op == XSLT_OP_END)) {
1775 element->priority = 0;
1777 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1778 (element->steps[1].op == XSLT_OP_END)) {
1779 element->priority = 0;
1781 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1782 (element->steps[0].value != NULL) &&
1783 (element->steps[1].op == XSLT_OP_END)) {
1784 element->priority = 0;
1785 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1786 (element->steps[0].value2 != NULL) &&
1787 (element->steps[1].op == XSLT_OP_END)) {
1788 element->priority = -0.25;
1789 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1790 (element->steps[0].value != NULL) &&
1791 (element->steps[1].op == XSLT_OP_END)) {
1792 element->priority = -0.25;
1793 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1794 (element->steps[0].value == NULL) &&
1795 (element->steps[0].value2 == NULL) &&
1796 (element->steps[1].op == XSLT_OP_END)) {
1797 element->priority = -0.5;
1798 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1799 (element->steps[0].op == XSLT_OP_TEXT) ||
1800 (element->steps[0].op == XSLT_OP_ALL) ||
1801 (element->steps[0].op == XSLT_OP_NODE) ||
1802 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1803 (element->steps[1].op == XSLT_OP_END)) {
1804 element->priority = -0.5;
1806 element->priority = 0.5;
1808 #ifdef WITH_XSLT_DEBUG_PATTERN
1809 xsltGenericDebug(xsltGenericDebugContext,
1810 "xsltCompilePattern : parsed %s, default priority %f\n",
1811 element->pattern, element->priority);
1813 if (pattern[end] == '|')
1818 xsltTransformError(NULL, style, node,
1819 "xsltCompilePattern : NULL pattern\n");
1824 xsltFreeParserContext(ctxt);
1829 xsltFreeParserContext(ctxt);
1831 xsltFreeCompMatchList(first);
1835 /************************************************************************
1837 * Module interfaces *
1839 ************************************************************************/
1843 * @style: an XSLT stylesheet
1844 * @cur: an XSLT template
1845 * @mode: the mode name or NULL
1846 * @modeURI: the mode URI or NULL
1848 * Register the XSLT pattern associated to @cur
1850 * Returns -1 in case of error, 0 otherwise
1853 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1854 const xmlChar *mode, const xmlChar *modeURI) {
1855 xsltCompMatchPtr pat, list, *top = NULL, next;
1856 const xmlChar *name = NULL;
1857 float priority; /* the priority */
1859 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1862 priority = cur->priority;
1863 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1869 pat->template = cur;
1871 pat->mode = xmlStrdup(mode);
1872 if (modeURI != NULL)
1873 pat->modeURI = xmlStrdup(modeURI);
1874 if (priority != XSLT_PAT_NO_PRIORITY)
1875 pat->priority = priority;
1878 * insert it in the hash table list corresponding to its lookup name
1880 switch (pat->steps[0].op) {
1882 if (pat->steps[0].value != NULL)
1883 name = pat->steps[0].value;
1885 top = (xsltCompMatchPtr *) &(style->attrMatch);
1888 case XSLT_OP_PARENT:
1889 case XSLT_OP_ANCESTOR:
1890 top = (xsltCompMatchPtr *) &(style->elemMatch);
1893 top = (xsltCompMatchPtr *) &(style->rootMatch);
1896 top = (xsltCompMatchPtr *) &(style->keyMatch);
1899 /* TODO optimize ID !!! */
1902 top = (xsltCompMatchPtr *) &(style->elemMatch);
1905 case XSLT_OP_PREDICATE:
1906 xsltTransformError(NULL, style, NULL,
1907 "xsltAddTemplate: invalid compiled pattern\n");
1908 xsltFreeCompMatch(pat);
1911 * TODO: some flags at the top level about type based patterns
1912 * would be faster than inclusion in the hash table.
1915 if (pat->steps[0].value != NULL)
1916 name = pat->steps[0].value;
1918 top = (xsltCompMatchPtr *) &(style->piMatch);
1920 case XSLT_OP_COMMENT:
1921 top = (xsltCompMatchPtr *) &(style->commentMatch);
1924 top = (xsltCompMatchPtr *) &(style->textMatch);
1928 if (pat->steps[0].value != NULL)
1929 name = pat->steps[0].value;
1931 top = (xsltCompMatchPtr *) &(style->elemMatch);
1935 if (style->templatesHash == NULL) {
1936 style->templatesHash = xmlHashCreate(1024);
1937 if (style->templatesHash == NULL) {
1938 xsltFreeCompMatch(pat);
1941 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1943 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1944 name, mode, modeURI);
1946 xmlHashAddEntry3(style->templatesHash, name,
1947 mode, modeURI, pat);
1950 * Note '<=' since one must choose among the matching
1951 * template rules that are left, the one that occurs
1952 * last in the stylesheet
1954 if (list->priority <= pat->priority) {
1956 xmlHashUpdateEntry3(style->templatesHash, name,
1957 mode, modeURI, pat, NULL);
1959 while (list->next != NULL) {
1960 if (list->next->priority <= pat->priority)
1964 pat->next = list->next;
1969 } else if (top != NULL) {
1974 } else if (list->priority <= pat->priority) {
1978 while (list->next != NULL) {
1979 if (list->next->priority <= pat->priority)
1983 pat->next = list->next;
1987 xsltTransformError(NULL, style, NULL,
1988 "xsltAddTemplate: invalid compiled pattern\n");
1989 xsltFreeCompMatch(pat);
1992 #ifdef WITH_XSLT_DEBUG_PATTERN
1994 xsltGenericDebug(xsltGenericDebugContext,
1995 "added pattern : '%s' mode '%s' priority %f\n",
1996 pat->pattern, pat->mode, pat->priority);
1998 xsltGenericDebug(xsltGenericDebugContext,
1999 "added pattern : '%s' priority %f\n",
2000 pat->pattern, pat->priority);
2010 * @ctxt: a XSLT process context
2011 * @node: the node being processed
2012 * @style: the current style
2014 * Finds the template applying to this node, if @style is non-NULL
2015 * it means one needs to look for the next imported template in scope.
2017 * Returns the xsltTemplatePtr or NULL if not found
2020 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2021 xsltStylesheetPtr style) {
2022 xsltStylesheetPtr curstyle;
2023 xsltTemplatePtr ret = NULL;
2024 const xmlChar *name = NULL;
2025 xsltCompMatchPtr list = NULL;
2028 if ((ctxt == NULL) || (node == NULL))
2031 if (style == NULL) {
2032 curstyle = ctxt->style;
2034 curstyle = xsltNextImport(style);
2037 while ((curstyle != NULL) && (curstyle != style)) {
2038 priority = XSLT_PAT_NO_PRIORITY;
2039 /* TODO : handle IDs/keys here ! */
2040 if (curstyle->templatesHash != NULL) {
2042 * Use the top name as selector
2044 switch (node->type) {
2045 case XML_ELEMENT_NODE:
2046 if (node->name[0] == ' ')
2048 case XML_ATTRIBUTE_NODE:
2052 case XML_DOCUMENT_NODE:
2053 case XML_HTML_DOCUMENT_NODE:
2055 case XML_CDATA_SECTION_NODE:
2056 case XML_COMMENT_NODE:
2057 case XML_ENTITY_REF_NODE:
2058 case XML_ENTITY_NODE:
2059 case XML_DOCUMENT_TYPE_NODE:
2060 case XML_DOCUMENT_FRAG_NODE:
2061 case XML_NOTATION_NODE:
2063 case XML_ELEMENT_DECL:
2064 case XML_ATTRIBUTE_DECL:
2065 case XML_ENTITY_DECL:
2066 case XML_NAMESPACE_DECL:
2067 case XML_XINCLUDE_START:
2068 case XML_XINCLUDE_END:
2077 * find the list of appliable expressions based on the name
2079 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2080 name, ctxt->mode, ctxt->modeURI);
2083 while (list != NULL) {
2084 if (xsltTestCompMatch(ctxt, list, node,
2085 ctxt->mode, ctxt->modeURI)) {
2086 ret = list->template;
2087 priority = list->priority;
2095 * find alternate generic matches
2097 switch (node->type) {
2098 case XML_ELEMENT_NODE:
2099 if (node->name[0] == ' ')
2100 list = curstyle->rootMatch;
2102 list = curstyle->elemMatch;
2104 case XML_ATTRIBUTE_NODE:
2105 list = curstyle->attrMatch;
2108 list = curstyle->piMatch;
2110 case XML_DOCUMENT_NODE:
2111 case XML_HTML_DOCUMENT_NODE:
2112 list = curstyle->rootMatch;
2115 case XML_CDATA_SECTION_NODE:
2116 list = curstyle->textMatch;
2118 case XML_COMMENT_NODE:
2119 list = curstyle->commentMatch;
2121 case XML_ENTITY_REF_NODE:
2122 case XML_ENTITY_NODE:
2123 case XML_DOCUMENT_TYPE_NODE:
2124 case XML_DOCUMENT_FRAG_NODE:
2125 case XML_NOTATION_NODE:
2127 case XML_ELEMENT_DECL:
2128 case XML_ATTRIBUTE_DECL:
2129 case XML_ENTITY_DECL:
2130 case XML_NAMESPACE_DECL:
2131 case XML_XINCLUDE_START:
2132 case XML_XINCLUDE_END:
2138 while ((list != NULL) &&
2139 ((ret == NULL) || (list->priority > priority))) {
2140 if (xsltTestCompMatch(ctxt, list, node,
2141 ctxt->mode, ctxt->modeURI)) {
2142 ret = list->template;
2143 priority = list->priority;
2149 * Some of the tests for elements can also apply to documents
2151 if ((node->type == XML_DOCUMENT_NODE) ||
2152 (node->type == XML_HTML_DOCUMENT_NODE) ||
2153 (node->type == XML_TEXT_NODE)) {
2154 list = curstyle->elemMatch;
2155 while ((list != NULL) &&
2156 ((ret == NULL) || (list->priority > priority))) {
2157 if (xsltTestCompMatch(ctxt, list, node,
2158 ctxt->mode, ctxt->modeURI)) {
2159 ret = list->template;
2160 priority = list->priority;
2167 if (node->_private != NULL) {
2168 list = curstyle->keyMatch;
2169 while ((list != NULL) &&
2170 ((ret == NULL) || (list->priority > priority))) {
2171 if (xsltTestCompMatch(ctxt, list, node,
2172 ctxt->mode, ctxt->modeURI)) {
2173 ret = list->template;
2174 priority = list->priority;
2184 * Cycle on next curstylesheet import.
2186 curstyle = xsltNextImport(curstyle);
2192 * xsltCleanupTemplates:
2193 * @style: an XSLT stylesheet
2195 * Cleanup the state of the templates used by the stylesheet and
2196 * the ones it imports.
2199 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2203 * xsltFreeTemplateHashes:
2204 * @style: an XSLT stylesheet
2206 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2209 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2210 if (style->templatesHash != NULL)
2211 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2212 (xmlHashDeallocator) xsltFreeCompMatchList);
2213 if (style->rootMatch != NULL)
2214 xsltFreeCompMatchList(style->rootMatch);
2215 if (style->keyMatch != NULL)
2216 xsltFreeCompMatchList(style->keyMatch);
2217 if (style->elemMatch != NULL)
2218 xsltFreeCompMatchList(style->elemMatch);
2219 if (style->attrMatch != NULL)
2220 xsltFreeCompMatchList(style->attrMatch);
2221 if (style->parentMatch != NULL)
2222 xsltFreeCompMatchList(style->parentMatch);
2223 if (style->textMatch != NULL)
2224 xsltFreeCompMatchList(style->textMatch);
2225 if (style->piMatch != NULL)
2226 xsltFreeCompMatchList(style->piMatch);
2227 if (style->commentMatch != NULL)
2228 xsltFreeCompMatchList(style->commentMatch);