2 * pattern.c: Implemetation of the template match compilation and lookup
5 * http://www.w3.org/TR/1999/REC-xslt-19991116
7 * See Copyright for the status of this software.
13 * TODO: handle pathological cases like *[*[@a="b"]]
14 * TODO: detect [number] at compilation, optimize accordingly
22 #include <libxml/xmlmemory.h>
23 #include <libxml/tree.h>
24 #include <libxml/valid.h>
25 #include <libxml/hash.h>
26 #include <libxml/xmlerror.h>
27 #include <libxml/parserInternals.h>
29 #include "xsltInternals.h"
30 #include "xsltutils.h"
32 #include "templates.h"
36 #ifdef WITH_XSLT_DEBUG
37 #define WITH_XSLT_DEBUG_PATTERN
64 typedef struct _xsltStepOp xsltStepOp;
65 typedef xsltStepOp *xsltStepOpPtr;
71 xmlXPathCompExprPtr comp;
73 * Optimisations for count
80 struct _xsltCompMatch {
81 struct _xsltCompMatch *next; /* siblings in the name hash */
82 float priority; /* the priority */
83 const xmlChar *pattern; /* the pattern */
84 const xmlChar *mode; /* the mode */
85 const xmlChar *modeURI; /* the mode URI */
86 xsltTemplatePtr template; /* the associated template */
88 /* TODO fix the statically allocated size steps[] */
91 xmlNsPtr *nsList; /* the namespaces in scope */
92 int nsNr; /* the number of namespaces in scope */
93 xsltStepOp steps[40]; /* ops for computation */
96 typedef struct _xsltParserContext xsltParserContext;
97 typedef xsltParserContext *xsltParserContextPtr;
98 struct _xsltParserContext {
99 xsltStylesheetPtr style; /* the stylesheet */
100 xsltTransformContextPtr ctxt; /* the transformation or NULL */
101 const xmlChar *cur; /* the current char being parsed */
102 const xmlChar *base; /* the full expression */
103 xmlDocPtr doc; /* the source document */
104 xmlNodePtr elem; /* the source element */
105 int error; /* error code */
106 xsltCompMatchPtr comp; /* the result */
109 /************************************************************************
113 ************************************************************************/
118 * Create a new XSLT CompMatch
120 * Returns the newly allocated xsltCompMatchPtr or NULL in case of error
122 static xsltCompMatchPtr
123 xsltNewCompMatch(void) {
124 xsltCompMatchPtr cur;
126 cur = (xsltCompMatchPtr) xmlMalloc(sizeof(xsltCompMatch));
128 xsltPrintErrorContext(NULL, NULL, NULL);
129 xsltGenericError(xsltGenericErrorContext,
130 "xsltNewCompMatch : malloc failed\n");
133 memset(cur, 0, sizeof(xsltCompMatch));
142 * @comp: an XSLT comp
144 * Free up the memory allocated by @comp
147 xsltFreeCompMatch(xsltCompMatchPtr comp) {
153 if (comp->pattern != NULL)
154 xmlFree((xmlChar *)comp->pattern);
155 if (comp->mode != NULL)
156 xmlFree((xmlChar *)comp->mode);
157 if (comp->modeURI != NULL)
158 xmlFree((xmlChar *)comp->modeURI);
159 if (comp->nsList != NULL)
160 xmlFree(comp->nsList);
161 for (i = 0;i < comp->nbStep;i++) {
162 op = &comp->steps[i];
163 if (op->value != NULL)
165 if (op->value2 != NULL)
167 if (op->value3 != NULL)
169 if (op->comp != NULL)
170 xmlXPathFreeCompExpr(op->comp);
172 memset(comp, -1, sizeof(xsltCompMatch));
177 * xsltFreeCompMatchList:
178 * @comp: an XSLT comp list
180 * Free up the memory allocated by all the elements of @comp
183 xsltFreeCompMatchList(xsltCompMatchPtr comp) {
184 xsltCompMatchPtr cur;
186 while (comp != NULL) {
189 xsltFreeCompMatch(cur);
194 * xsltNewParserContext:
195 * @style: the stylesheet
196 * @ctxt: the transformation context, if done at run-time
198 * Create a new XSLT ParserContext
200 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
202 static xsltParserContextPtr
203 xsltNewParserContext(xsltStylesheetPtr style, xsltTransformContextPtr ctxt) {
204 xsltParserContextPtr cur;
206 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
208 xsltPrintErrorContext(NULL, NULL, NULL);
209 xsltGenericError(xsltGenericErrorContext,
210 "xsltNewParserContext : malloc failed\n");
213 memset(cur, 0, sizeof(xsltParserContext));
220 * xsltFreeParserContext:
221 * @ctxt: an XSLT parser context
223 * Free up the memory allocated by @ctxt
226 xsltFreeParserContext(xsltParserContextPtr ctxt) {
229 memset(ctxt, -1, sizeof(xsltParserContext));
235 * @comp: the compiled match expression
237 * @value: the first value
238 * @value2: the second value
240 * Add an step to an XSLT Compiled Match
242 * Returns -1 in case of failure, 0 otherwise.
245 xsltCompMatchAdd(xsltParserContextPtr ctxt, xsltCompMatchPtr comp,
246 xsltOp op, xmlChar * value, xmlChar * value2)
248 if (comp->nbStep >= 40) {
249 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
250 xsltGenericError(xsltGenericErrorContext,
251 "xsltCompMatchAdd: overflow\n");
254 comp->steps[comp->nbStep].op = op;
255 comp->steps[comp->nbStep].value = value;
256 comp->steps[comp->nbStep].value2 = value2;
257 if (ctxt->ctxt != NULL) {
258 comp->steps[comp->nbStep].previousExtra =
259 xsltAllocateExtraCtxt(ctxt->ctxt);
260 comp->steps[comp->nbStep].indexExtra =
261 xsltAllocateExtraCtxt(ctxt->ctxt);
262 comp->steps[comp->nbStep].lenExtra =
263 xsltAllocateExtraCtxt(ctxt->ctxt);
265 comp->steps[comp->nbStep].previousExtra =
266 xsltAllocateExtra(ctxt->style);
267 comp->steps[comp->nbStep].indexExtra =
268 xsltAllocateExtra(ctxt->style);
269 comp->steps[comp->nbStep].lenExtra =
270 xsltAllocateExtra(ctxt->style);
272 if (op == XSLT_OP_PREDICATE) {
273 comp->steps[comp->nbStep].comp = xmlXPathCompile(value);
280 * xsltSwapTopCompMatch:
281 * @comp: the compiled match expression
283 * reverse the two top steps.
286 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
288 int j = comp->nbStep - 1;
291 register xmlChar *tmp;
293 register xmlXPathCompExprPtr expr;
295 tmp = comp->steps[i].value;
296 comp->steps[i].value = comp->steps[j].value;
297 comp->steps[j].value = tmp;
298 tmp = comp->steps[i].value2;
299 comp->steps[i].value2 = comp->steps[j].value2;
300 comp->steps[j].value2 = tmp;
301 op = comp->steps[i].op;
302 comp->steps[i].op = comp->steps[j].op;
303 comp->steps[j].op = op;
304 expr = comp->steps[i].comp;
305 comp->steps[i].comp = comp->steps[j].comp;
306 comp->steps[j].comp = expr;
311 * xsltReverseCompMatch:
312 * @comp: the compiled match expression
314 * reverse all the stack of expressions
317 xsltReverseCompMatch(xsltCompMatchPtr comp) {
319 int j = comp->nbStep - 1;
322 register xmlChar *tmp;
324 register xmlXPathCompExprPtr expr;
325 tmp = comp->steps[i].value;
326 comp->steps[i].value = comp->steps[j].value;
327 comp->steps[j].value = tmp;
328 tmp = comp->steps[i].value2;
329 comp->steps[i].value2 = comp->steps[j].value2;
330 comp->steps[j].value2 = tmp;
331 op = comp->steps[i].op;
332 comp->steps[i].op = comp->steps[j].op;
333 comp->steps[j].op = op;
334 expr = comp->steps[i].comp;
335 comp->steps[i].comp = comp->steps[j].comp;
336 comp->steps[j].comp = expr;
340 comp->steps[comp->nbStep++].op = XSLT_OP_END;
343 /************************************************************************
345 * The interpreter for the precompiled patterns *
347 ************************************************************************/
351 * @ctxt: a XSLT process context
352 * @comp: the precompiled pattern
354 * @mode: the mode name or NULL
355 * @modeURI: the mode URI or NULL
357 * Test wether the node matches the pattern
359 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
362 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
363 xmlNodePtr node, const xmlChar *mode,
364 const xmlChar *modeURI) {
366 xsltStepOpPtr step, select = NULL;
368 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
369 xsltPrintErrorContext(ctxt, NULL, node);
370 xsltGenericError(xsltGenericErrorContext,
371 "xsltTestCompMatch: null arg\n");
375 if (comp->mode == NULL)
377 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
380 if (comp->mode != NULL)
383 if (modeURI != NULL) {
384 if (comp->modeURI == NULL)
386 if ((comp->modeURI != modeURI) &&
387 (!xmlStrEqual(comp->modeURI, modeURI)))
390 if (comp->modeURI != NULL)
393 for (i = 0;i < comp->nbStep;i++) {
394 step = &comp->steps[i];
395 if (step->op != XSLT_OP_PREDICATE)
401 if ((node->type == XML_DOCUMENT_NODE) ||
402 #ifdef LIBXML_DOCB_ENABLED
403 (node->type == XML_DOCB_DOCUMENT_NODE) ||
405 (node->type == XML_HTML_DOCUMENT_NODE))
409 if (node->type != XML_ELEMENT_NODE)
411 if (step->value == NULL)
413 if (step->value[0] != node->name[0])
415 if (!xmlStrEqual(step->value, node->name))
419 if (node->ns == NULL) {
420 if (step->value2 != NULL)
422 } else if (node->ns->href != NULL) {
423 if (step->value2 == NULL)
425 if (!xmlStrEqual(step->value2, node->ns->href))
429 case XSLT_OP_CHILD: {
432 if ((node->type != XML_ELEMENT_NODE) &&
433 (node->type != XML_DOCUMENT_NODE) &&
434 #ifdef LIBXML_DOCB_ENABLED
435 (node->type != XML_DOCB_DOCUMENT_NODE) &&
437 (node->type != XML_HTML_DOCUMENT_NODE))
440 lst = node->children;
442 if (step->value != NULL) {
443 while (lst != NULL) {
444 if ((lst->type == XML_ELEMENT_NODE) &&
445 (step->value[0] == lst->name[0]) &&
446 (xmlStrEqual(step->value, lst->name)))
456 if (node->type != XML_ATTRIBUTE_NODE)
458 if (step->value != NULL) {
459 if (step->value[0] != node->name[0])
461 if (!xmlStrEqual(step->value, node->name))
465 if (node->ns == NULL) {
466 if (step->value2 != NULL)
468 } else if (step->value2 != NULL) {
469 if (!xmlStrEqual(step->value2, node->ns->href))
474 if ((node->type == XML_DOCUMENT_NODE) ||
475 (node->type == XML_HTML_DOCUMENT_NODE) ||
476 #ifdef LIBXML_DOCB_ENABLED
477 (node->type == XML_DOCB_DOCUMENT_NODE) ||
479 (node->type == XML_NAMESPACE_DECL))
484 if (step->value == NULL)
486 if (step->value[0] != node->name[0])
488 if (!xmlStrEqual(step->value, node->name))
491 if (node->ns == NULL) {
492 if (step->value2 != NULL)
494 } else if (node->ns->href != NULL) {
495 if (step->value2 == NULL)
497 if (!xmlStrEqual(step->value2, node->ns->href))
501 case XSLT_OP_ANCESTOR:
502 /* TODO: implement coalescing of ANCESTOR/NODE ops */
503 if (step->value == NULL) {
505 step = &comp->steps[i];
506 if (step->op == XSLT_OP_ROOT)
508 if (step->op != XSLT_OP_ELEM)
510 if (step->value == NULL)
515 if ((node->type == XML_DOCUMENT_NODE) ||
516 (node->type == XML_HTML_DOCUMENT_NODE) ||
517 #ifdef LIBXML_DOCB_ENABLED
518 (node->type == XML_DOCB_DOCUMENT_NODE) ||
520 (node->type == XML_NAMESPACE_DECL))
523 while (node != NULL) {
526 if ((node->type == XML_ELEMENT_NODE) &&
527 (step->value[0] == node->name[0]) &&
528 (xmlStrEqual(step->value, node->name))) {
530 if (node->ns == NULL) {
531 if (step->value2 == NULL)
533 } else if (node->ns->href != NULL) {
534 if ((step->value2 != NULL) &&
535 (xmlStrEqual(step->value2, node->ns->href)))
545 /* TODO Handle IDs decently, must be done differently */
548 if (node->type != XML_ELEMENT_NODE)
551 id = xmlGetID(node->doc, step->value);
552 if ((id == NULL) || (id->parent != node))
560 list = xsltGetKey(ctxt, step->value,
561 step->value3, step->value2);
564 for (indx = 0;indx < list->nodeNr;indx++)
565 if (list->nodeTab[indx] == node)
567 if (indx >= list->nodeNr)
572 if (node->type != XML_ELEMENT_NODE)
574 if (node->ns == NULL) {
575 if (step->value != NULL)
577 } else if (node->ns->href != NULL) {
578 if (step->value == NULL)
580 if (!xmlStrEqual(step->value, node->ns->href))
585 if (node->type != XML_ELEMENT_NODE)
588 case XSLT_OP_PREDICATE: {
591 int pos = 0, len = 0;
593 * The simple existing predicate code cannot handle
594 * properly cascaded predicates. If in this situation
595 * compute directly the full node list once and check
596 * if the node is in the result list.
598 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
600 xmlXPathObjectPtr list;
603 previous = (xmlNodePtr)
604 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
606 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
607 list = (xmlXPathObjectPtr)
608 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
612 if (comp->pattern[0] == '/')
613 query = xmlStrdup(comp->pattern);
615 query = xmlStrdup((const xmlChar *)"//");
616 query = xmlStrcat(query, comp->pattern);
618 list = xmlXPathEval(query, ctxt->xpathCtxt);
622 if (list->type != XPATH_NODESET) {
623 xmlXPathFreeObject(list);
626 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
628 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
629 (xmlFreeFunc) xmlXPathFreeObject;
631 if ((list->nodesetval == NULL) ||
632 (list->nodesetval->nodeNr <= 0))
635 for (j = 0;j < list->nodesetval->nodeNr;j++) {
636 if (list->nodesetval->nodeTab[j] == node) {
645 * Depending on the last selection, one may need to
646 * recompute contextSize and proximityPosition.
648 * TODO: make this thread safe !
650 oldCS = ctxt->xpathCtxt->contextSize;
651 oldCP = ctxt->xpathCtxt->proximityPosition;
652 if ((select != NULL) &&
653 (select->op == XSLT_OP_ELEM) &&
654 (select->value != NULL) &&
655 (node->type == XML_ELEMENT_NODE) &&
656 (node->parent != NULL)) {
658 int index, nocache = 0;
660 previous = (xmlNodePtr)
661 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
663 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
664 if ((previous != NULL) &&
665 (previous->parent == node->parent)) {
667 * just walk back to adjust the index
670 xmlNodePtr sibling = node;
672 while (sibling != NULL) {
673 if (sibling == previous)
675 if ((node->type == XML_ELEMENT_NODE) &&
676 (node->name[0] == sibling->name[0]) &&
677 (xmlStrEqual(node->name, sibling->name))) {
678 if ((select->value2 == NULL) ||
679 ((sibling->ns != NULL) &&
680 (xmlStrEqual(select->value2,
681 sibling->ns->href))))
684 sibling = sibling->prev;
686 if (sibling == NULL) {
687 /* hum going backward in document order ... */
690 while (sibling != NULL) {
691 if (sibling == previous)
693 if ((select->value2 == NULL) ||
694 ((sibling->ns != NULL) &&
695 (xmlStrEqual(select->value2,
696 sibling->ns->href))))
698 sibling = sibling->next;
701 if (sibling != NULL) {
704 * If the node is in a Value Tree we cannot
707 if (node->doc != NULL) {
709 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
710 XSLT_RUNTIME_EXTRA(ctxt,
711 select->previousExtra) = node;
712 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
720 * recompute the index
722 xmlNodePtr siblings = node->parent->children;
723 xmlNodePtr parent = node->parent;
725 while (siblings != NULL) {
726 if (siblings->type == XML_ELEMENT_NODE) {
727 if (siblings == node) {
730 } else if ((node->name[0] == siblings->name[0])
731 && (xmlStrEqual(node->name, siblings->name))) {
732 if ((select->value2 == NULL) ||
733 ((siblings->ns != NULL) &&
734 (xmlStrEqual(select->value2,
735 siblings->ns->href))))
739 siblings = siblings->next;
741 if ((parent == NULL) || (node->doc == NULL))
744 while (parent->parent != NULL)
745 parent = parent->parent;
746 if (((parent->type != XML_DOCUMENT_NODE) &&
747 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
748 (parent != (xmlNodePtr) node->doc))
753 ctxt->xpathCtxt->contextSize = len;
754 ctxt->xpathCtxt->proximityPosition = pos;
756 * If the node is in a Value Tree we cannot
759 if ((node->doc != NULL) && (nocache == 0)) {
760 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
762 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
764 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
768 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
769 (node->type == XML_ELEMENT_NODE)) {
771 int index, nocache = 0;
773 previous = (xmlNodePtr)
774 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
776 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
777 if ((previous != NULL) &&
778 (previous->parent == node->parent)) {
780 * just walk back to adjust the index
783 xmlNodePtr sibling = node;
785 while (sibling != NULL) {
786 if (sibling == previous)
788 if (sibling->type == XML_ELEMENT_NODE)
790 sibling = sibling->prev;
792 if (sibling == NULL) {
793 /* hum going backward in document order ... */
796 while (sibling != NULL) {
797 if (sibling == previous)
799 if (sibling->type == XML_ELEMENT_NODE)
801 sibling = sibling->next;
804 if (sibling != NULL) {
807 * If the node is in a Value Tree we cannot
810 if (node->doc != NULL) {
812 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
813 XSLT_RUNTIME_EXTRA(ctxt,
814 select->previousExtra) = node;
815 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
822 * recompute the index
824 xmlNodePtr siblings = node->parent->children;
825 xmlNodePtr parent = node->parent;
827 while (siblings != NULL) {
828 if (siblings->type == XML_ELEMENT_NODE) {
830 if (siblings == node) {
834 siblings = siblings->next;
836 if ((parent == NULL) || (node->doc == NULL))
839 while (parent->parent != NULL)
840 parent = parent->parent;
841 if (((parent->type != XML_DOCUMENT_NODE) &&
842 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
843 (parent != (xmlNodePtr) node->doc))
848 ctxt->xpathCtxt->contextSize = len;
849 ctxt->xpathCtxt->proximityPosition = pos;
851 * If the node is in a Value Tree we cannot
854 if ((node->doc != NULL) && (nocache == 0)) {
855 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
857 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
859 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
864 oldNode = ctxt->node;
867 if (step->value == NULL)
869 if (step->comp == NULL)
872 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
877 ctxt->xpathCtxt->contextSize = oldCS;
878 ctxt->xpathCtxt->proximityPosition = oldCP;
880 ctxt->node = oldNode;
884 ctxt->xpathCtxt->contextSize = oldCS;
885 ctxt->xpathCtxt->proximityPosition = oldCP;
887 ctxt->node = oldNode;
891 if (node->type != XML_PI_NODE)
893 if (step->value != NULL) {
894 if (!xmlStrEqual(step->value, node->name))
898 case XSLT_OP_COMMENT:
899 if (node->type != XML_COMMENT_NODE)
903 if ((node->type != XML_TEXT_NODE) &&
904 (node->type != XML_CDATA_SECTION_NODE))
908 switch (node->type) {
909 case XML_DOCUMENT_NODE:
910 case XML_HTML_DOCUMENT_NODE:
911 #ifdef LIBXML_DOCB_ENABLED
912 case XML_DOCB_DOCUMENT_NODE:
914 case XML_ELEMENT_NODE:
915 case XML_CDATA_SECTION_NODE:
917 case XML_COMMENT_NODE:
919 case XML_ATTRIBUTE_NODE:
931 * xsltTestCompMatchList:
932 * @ctxt: a XSLT process context
934 * @comp: the precompiled pattern list
936 * Test wether the node matches one of the patterns in the list
938 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
941 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
942 xsltCompMatchPtr comp) {
945 if ((ctxt == NULL) || (node == NULL))
947 while (comp != NULL) {
948 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
956 /************************************************************************
958 * Dedicated parser for templates *
960 ************************************************************************/
962 #define CUR (*ctxt->cur)
963 #define SKIP(val) ctxt->cur += (val)
964 #define NXT(val) ctxt->cur[(val)]
965 #define CUR_PTR ctxt->cur
967 #define SKIP_BLANKS \
968 while (IS_BLANK(CUR)) NEXT
970 #define CURRENT (*ctxt->cur)
971 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
974 #define PUSH(op, val, val2) \
975 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
978 xsltSwapTopCompMatch(ctxt->comp);
980 #define XSLT_ERROR(X) \
981 { xsltError(ctxt, __FILE__, __LINE__, X); \
982 ctxt->error = (X); return; }
984 #define XSLT_ERROR0(X) \
985 { xsltError(ctxt, __FILE__, __LINE__, X); \
986 ctxt->error = (X); return(0); }
990 * @ctxt: the XPath Parser context
992 * Parse an XPath Litteral:
994 * [29] Literal ::= '"' [^"]* '"'
997 * Returns the Literal parsed or NULL
1001 xsltScanLiteral(xsltParserContextPtr ctxt) {
1002 const xmlChar *q, *cur;
1003 xmlChar *ret = NULL;
1010 val = xmlStringCurrentChar(NULL, cur, &len);
1011 while ((IS_CHAR(val)) && (val != '"')) {
1013 val = xmlStringCurrentChar(NULL, cur, &len);
1015 if (!IS_CHAR(val)) {
1019 ret = xmlStrndup(q, cur - q);
1023 } else if (CUR == '\'') {
1026 val = xmlStringCurrentChar(NULL, cur, &len);
1027 while ((IS_CHAR(val)) && (val != '\'')) {
1029 val = xmlStringCurrentChar(NULL, cur, &len);
1031 if (!IS_CHAR(val)) {
1035 ret = xmlStrndup(q, cur - q);
1040 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1049 * @ctxt: the XPath Parser context
1051 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1052 * CombiningChar | Extender
1054 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1056 * [6] Names ::= Name (S Name)*
1058 * Returns the Name parsed or NULL
1062 xsltScanName(xsltParserContextPtr ctxt) {
1063 const xmlChar *q, *cur;
1064 xmlChar *ret = NULL;
1070 val = xmlStringCurrentChar(NULL, cur, &len);
1071 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1074 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1075 (val == '.') || (val == '-') ||
1077 (IS_COMBINING(val)) ||
1078 (IS_EXTENDER(val))) {
1080 val = xmlStringCurrentChar(NULL, cur, &len);
1082 ret = xmlStrndup(q, cur - q);
1089 * @ctxt: the XPath Parser context
1091 * Parses a non qualified name
1093 * Returns the Name parsed or NULL
1097 xsltScanNCName(xsltParserContextPtr ctxt) {
1098 const xmlChar *q, *cur;
1099 xmlChar *ret = NULL;
1105 val = xmlStringCurrentChar(NULL, cur, &len);
1106 if (!IS_LETTER(val) && (val != '_'))
1109 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1110 (val == '.') || (val == '-') ||
1112 (IS_COMBINING(val)) ||
1113 (IS_EXTENDER(val))) {
1115 val = xmlStringCurrentChar(NULL, cur, &len);
1117 ret = xmlStrndup(q, cur - q);
1124 * @ctxt: the XPath Parser context
1125 * @prefix: the place to store the prefix
1127 * Parse a qualified name
1129 * Returns the Name parsed or NULL
1133 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1134 xmlChar *ret = NULL;
1137 ret = xsltScanNCName(ctxt);
1141 ret = xsltScanNCName(ctxt);
1147 * xsltCompileIdKeyPattern:
1148 * @ctxt: the compilation context
1149 * @name: a preparsed name
1150 * @aid: whether id/key are allowed there
1152 * Compile the XSLT LocationIdKeyPattern
1153 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1154 * | 'key' '(' Literal ',' Literal ')'
1156 * also handle NodeType and PI from:
1158 * [7] NodeTest ::= NameTest
1159 * | NodeType '(' ')'
1160 * | 'processing-instruction' '(' Literal ')'
1163 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1164 xmlChar *lit = NULL;
1165 xmlChar *lit2 = NULL;
1168 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1169 xsltGenericError(xsltGenericErrorContext,
1170 "xsltCompileIdKeyPattern : ( expected\n");
1174 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1177 lit = xsltScanLiteral(ctxt);
1182 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1183 xsltGenericError(xsltGenericErrorContext,
1184 "xsltCompileIdKeyPattern : ) expected\n");
1189 PUSH(XSLT_OP_ID, lit, NULL);
1190 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1193 lit = xsltScanLiteral(ctxt);
1198 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1199 xsltGenericError(xsltGenericErrorContext,
1200 "xsltCompileIdKeyPattern : , expected\n");
1206 lit2 = xsltScanLiteral(ctxt);
1211 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1212 xsltGenericError(xsltGenericErrorContext,
1213 "xsltCompileIdKeyPattern : ) expected\n");
1218 /* TODO: support namespace in keys */
1219 PUSH(XSLT_OP_KEY, lit, lit2);
1220 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1224 lit = xsltScanLiteral(ctxt);
1229 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1230 xsltGenericError(xsltGenericErrorContext,
1231 "xsltCompileIdKeyPattern : ) expected\n");
1237 PUSH(XSLT_OP_PI, lit, NULL);
1238 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1242 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1243 xsltGenericError(xsltGenericErrorContext,
1244 "xsltCompileIdKeyPattern : ) expected\n");
1249 PUSH(XSLT_OP_TEXT, NULL, NULL);
1250 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1254 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1255 xsltGenericError(xsltGenericErrorContext,
1256 "xsltCompileIdKeyPattern : ) expected\n");
1261 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1262 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1266 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1267 xsltGenericError(xsltGenericErrorContext,
1268 "xsltCompileIdKeyPattern : ) expected\n");
1273 PUSH(XSLT_OP_NODE, NULL, NULL);
1275 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1276 xsltGenericError(xsltGenericErrorContext,
1277 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1281 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1282 xsltGenericError(xsltGenericErrorContext,
1283 "xsltCompileIdKeyPattern : node type\n");
1293 * xsltCompileStepPattern:
1294 * @ctxt: the compilation context
1295 * @token: a posible precompiled name
1297 * Compile the XSLT StepPattern and generates a precompiled
1298 * form suitable for fast matching.
1300 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1301 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1302 * | ('child' | 'attribute') '::'
1304 * [7] NodeTest ::= NameTest
1305 * | NodeType '(' ')'
1306 * | 'processing-instruction' '(' Literal ')'
1307 * [8] Predicate ::= '[' PredicateExpr ']'
1308 * [9] PredicateExpr ::= Expr
1309 * [13] AbbreviatedAxisSpecifier ::= '@'?
1310 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1314 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1315 xmlChar *name = NULL;
1316 const xmlChar *URI = NULL;
1317 xmlChar *URL = NULL;
1321 if ((token == NULL) && (CUR == '@')) {
1322 xmlChar *prefix = NULL;
1327 PUSH(XSLT_OP_ATTR, NULL, NULL);
1330 token = xsltScanQName(ctxt, &prefix);
1331 if (prefix != NULL) {
1334 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1336 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1337 xsltGenericError(xsltGenericErrorContext,
1338 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1341 URL = xmlStrdup(ns->href);
1345 if (token == NULL) {
1348 PUSH(XSLT_OP_ATTR, NULL, URL);
1351 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1352 xsltGenericError(xsltGenericErrorContext,
1353 "xsltCompileStepPattern : Name expected\n");
1357 PUSH(XSLT_OP_ATTR, token, URL);
1361 token = xsltScanName(ctxt);
1362 if (token == NULL) {
1365 PUSH(XSLT_OP_ALL, token, NULL);
1366 goto parse_predicate;
1368 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1369 xsltGenericError(xsltGenericErrorContext,
1370 "xsltCompileStepPattern : Name expected\n");
1379 xsltCompileIdKeyPattern(ctxt, token, 0);
1382 } else if (CUR == ':') {
1385 xmlChar *prefix = token;
1389 * This is a namespace match
1391 token = xsltScanName(ctxt);
1392 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1394 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1395 xsltGenericError(xsltGenericErrorContext,
1396 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1401 URL = xmlStrdup(ns->href);
1404 if (token == NULL) {
1407 PUSH(XSLT_OP_NS, URL, NULL);
1409 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1410 xsltGenericError(xsltGenericErrorContext,
1411 "xsltCompileStepPattern : Name expected\n");
1416 PUSH(XSLT_OP_ELEM, token, URL);
1420 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1422 token = xsltScanName(ctxt);
1423 if (token == NULL) {
1426 PUSH(XSLT_OP_ALL, token, NULL);
1427 goto parse_predicate;
1429 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1430 xsltGenericError(xsltGenericErrorContext,
1431 "xsltCompileStepPattern : QName expected\n");
1436 URI = xsltGetQNameURI(ctxt->elem, &token);
1437 if (token == NULL) {
1441 name = xmlStrdup(token);
1443 URL = xmlStrdup(URI);
1445 PUSH(XSLT_OP_CHILD, name, URL);
1446 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1448 token = xsltScanName(ctxt);
1449 if (token == NULL) {
1450 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1451 xsltGenericError(xsltGenericErrorContext,
1452 "xsltCompileStepPattern : QName expected\n");
1456 URI = xsltGetQNameURI(ctxt->elem, &token);
1457 if (token == NULL) {
1461 name = xmlStrdup(token);
1463 URL = xmlStrdup(URI);
1465 PUSH(XSLT_OP_ATTR, name, URL);
1467 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1468 xsltGenericError(xsltGenericErrorContext,
1469 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1475 } else if (CUR == '*') {
1477 PUSH(XSLT_OP_ALL, token, NULL);
1479 URI = xsltGetQNameURI(ctxt->elem, &token);
1480 if (token == NULL) {
1485 URL = xmlStrdup(URI);
1486 PUSH(XSLT_OP_ELEM, token, URL);
1491 while (CUR == '[') {
1493 xmlChar *ret = NULL;
1499 /* Skip over nested predicates */
1502 else if (CUR == ']') {
1506 } else if (CUR == '"') {
1508 while ((CUR != 0) && (CUR != '"'))
1510 } else if (CUR == '\'') {
1512 while ((CUR != 0) && (CUR != '\''))
1518 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1519 xsltGenericError(xsltGenericErrorContext,
1520 "xsltCompileStepPattern : ']' expected\n");
1524 ret = xmlStrndup(q, CUR_PTR - q);
1525 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1526 /* push the predicate lower than local test */
1540 * xsltCompileRelativePathPattern:
1541 * @comp: the compilation context
1542 * @token: a posible precompiled name
1544 * Compile the XSLT RelativePathPattern and generates a precompiled
1545 * form suitable for fast matching.
1547 * [4] RelativePathPattern ::= StepPattern
1548 * | RelativePathPattern '/' StepPattern
1549 * | RelativePathPattern '//' StepPattern
1552 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1553 xsltCompileStepPattern(ctxt, token);
1557 while ((CUR != 0) && (CUR != '|')) {
1558 if ((CUR == '/') && (NXT(1) == '/')) {
1559 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1563 xsltCompileStepPattern(ctxt, NULL);
1564 } else if (CUR == '/') {
1565 PUSH(XSLT_OP_PARENT, NULL, NULL);
1568 if ((CUR != 0) || (CUR == '|')) {
1569 xsltCompileRelativePathPattern(ctxt, NULL);
1583 * xsltCompileLocationPathPattern:
1584 * @ctxt: the compilation context
1586 * Compile the XSLT LocationPathPattern and generates a precompiled
1587 * form suitable for fast matching.
1589 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1590 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1591 * | '//'? RelativePathPattern
1594 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1596 if ((CUR == '/') && (NXT(1) == '/')) {
1598 * since we reverse the query
1599 * a leading // can be safely ignored
1603 xsltCompileRelativePathPattern(ctxt, NULL);
1604 } else if (CUR == '/') {
1606 * We need to find root as the parent
1610 PUSH(XSLT_OP_ROOT, NULL, NULL);
1611 if ((CUR != 0) || (CUR == '|')) {
1612 PUSH(XSLT_OP_PARENT, NULL, NULL);
1613 xsltCompileRelativePathPattern(ctxt, NULL);
1615 } else if (CUR == '*') {
1616 xsltCompileRelativePathPattern(ctxt, NULL);
1617 } else if (CUR == '@') {
1618 xsltCompileRelativePathPattern(ctxt, NULL);
1621 name = xsltScanName(ctxt);
1623 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1624 xsltGenericError(xsltGenericErrorContext,
1625 "xsltCompileLocationPathPattern : Name expected\n");
1630 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1631 xsltCompileIdKeyPattern(ctxt, name, 1);
1632 if ((CUR == '/') && (NXT(1) == '/')) {
1633 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1637 xsltCompileRelativePathPattern(ctxt, NULL);
1638 } else if (CUR == '/') {
1639 PUSH(XSLT_OP_PARENT, NULL, NULL);
1642 xsltCompileRelativePathPattern(ctxt, NULL);
1646 xsltCompileRelativePathPattern(ctxt, name);
1653 * xsltCompilePattern:
1654 * @pattern: an XSLT pattern
1655 * @doc: the containing document
1656 * @node: the containing element
1657 * @style: the stylesheet
1658 * @runtime: the transformation context, if done at run-time
1660 * Compile the XSLT pattern and generates a list of precompiled form suitable
1661 * for fast matching.
1663 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1665 * Returns the generated pattern list or NULL in case of failure
1669 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1670 xmlNodePtr node, xsltStylesheetPtr style,
1671 xsltTransformContextPtr runtime) {
1672 xsltParserContextPtr ctxt = NULL;
1673 xsltCompMatchPtr element, first = NULL, previous = NULL;
1674 int current, start, end, level, j;
1676 if (pattern == NULL) {
1677 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1678 xsltGenericError(xsltGenericErrorContext,
1679 "xsltCompilePattern : NULL pattern\n");
1683 ctxt = xsltNewParserContext(style, runtime);
1689 while (pattern[current] != 0) {
1691 while (IS_BLANK(pattern[current]))
1695 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1696 if (pattern[end] == '[')
1698 else if (pattern[end] == ']')
1700 else if (pattern[end] == '\'') {
1702 while ((pattern[end] != 0) && (pattern[end] != '\''))
1704 } else if (pattern[end] == '"') {
1706 while ((pattern[end] != 0) && (pattern[end] != '"'))
1711 if (current == end) {
1712 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1713 xsltGenericError(xsltGenericErrorContext,
1714 "xsltCompilePattern : NULL pattern\n");
1717 element = xsltNewCompMatch();
1718 if (element == NULL) {
1723 else if (previous != NULL)
1724 previous->next = element;
1727 ctxt->comp = element;
1728 ctxt->base = xmlStrndup(&pattern[start], end - start);
1729 if (ctxt->base == NULL)
1731 ctxt->cur = &(ctxt->base)[current - start];
1732 element->pattern = ctxt->base;
1733 element->nsList = xmlGetNsList(doc, node);
1735 if (element->nsList != NULL) {
1736 while (element->nsList[j] != NULL)
1742 #ifdef WITH_XSLT_DEBUG_PATTERN
1743 xsltGenericDebug(xsltGenericDebugContext,
1744 "xsltCompilePattern : parsing '%s'\n",
1747 xsltCompileLocationPathPattern(ctxt);
1752 * Reverse for faster interpretation.
1754 xsltReverseCompMatch(element);
1757 * Set-up the priority
1759 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1760 (element->steps[0].op == XSLT_OP_ATTR)) &&
1761 (element->steps[0].value != NULL) &&
1762 (element->steps[1].op == XSLT_OP_END)) {
1763 element->priority = 0;
1765 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1766 (element->steps[1].op == XSLT_OP_END)) {
1767 element->priority = 0;
1769 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1770 (element->steps[0].value != NULL) &&
1771 (element->steps[1].op == XSLT_OP_END)) {
1772 element->priority = 0;
1773 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1774 (element->steps[0].value2 != NULL) &&
1775 (element->steps[1].op == XSLT_OP_END)) {
1776 element->priority = -0.25;
1777 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1778 (element->steps[0].value != NULL) &&
1779 (element->steps[1].op == XSLT_OP_END)) {
1780 element->priority = -0.25;
1781 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1782 (element->steps[0].value == NULL) &&
1783 (element->steps[0].value2 == NULL) &&
1784 (element->steps[1].op == XSLT_OP_END)) {
1785 element->priority = -0.5;
1786 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1787 (element->steps[0].op == XSLT_OP_TEXT) ||
1788 (element->steps[0].op == XSLT_OP_ALL) ||
1789 (element->steps[0].op == XSLT_OP_NODE) ||
1790 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1791 (element->steps[1].op == XSLT_OP_END)) {
1792 element->priority = -0.5;
1794 element->priority = 0.5;
1796 #ifdef WITH_XSLT_DEBUG_PATTERN
1797 xsltGenericDebug(xsltGenericDebugContext,
1798 "xsltCompilePattern : parsed %s, default priority %f\n",
1799 element->pattern, element->priority);
1801 if (pattern[end] == '|')
1806 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1807 xsltGenericError(xsltGenericErrorContext,
1808 "xsltCompilePattern : NULL pattern\n");
1812 xsltFreeParserContext(ctxt);
1817 xsltFreeParserContext(ctxt);
1819 xsltFreeCompMatchList(first);
1823 /************************************************************************
1825 * Module interfaces *
1827 ************************************************************************/
1831 * @style: an XSLT stylesheet
1832 * @cur: an XSLT template
1833 * @mode: the mode name or NULL
1834 * @modeURI: the mode URI or NULL
1836 * Register the XSLT pattern associated to @cur
1838 * Returns -1 in case of error, 0 otherwise
1841 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1842 const xmlChar *mode, const xmlChar *modeURI) {
1843 xsltCompMatchPtr pat, list, *top = NULL, next;
1844 const xmlChar *name = NULL;
1845 float priority; /* the priority */
1847 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1850 priority = cur->priority;
1851 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1857 pat->template = cur;
1859 pat->mode = xmlStrdup(mode);
1860 if (modeURI != NULL)
1861 pat->modeURI = xmlStrdup(modeURI);
1862 if (priority != XSLT_PAT_NO_PRIORITY)
1863 pat->priority = priority;
1866 * insert it in the hash table list corresponding to its lookup name
1868 switch (pat->steps[0].op) {
1870 if (pat->steps[0].value != NULL)
1871 name = pat->steps[0].value;
1873 top = (xsltCompMatchPtr *) &(style->attrMatch);
1876 case XSLT_OP_PARENT:
1877 case XSLT_OP_ANCESTOR:
1878 top = (xsltCompMatchPtr *) &(style->elemMatch);
1881 top = (xsltCompMatchPtr *) &(style->rootMatch);
1884 top = (xsltCompMatchPtr *) &(style->keyMatch);
1887 /* TODO optimize ID !!! */
1890 top = (xsltCompMatchPtr *) &(style->elemMatch);
1893 case XSLT_OP_PREDICATE:
1894 xsltPrintErrorContext(NULL, style, NULL);
1895 xsltGenericError(xsltGenericErrorContext,
1896 "xsltAddTemplate: invalid compiled pattern\n");
1897 xsltFreeCompMatch(pat);
1900 * TODO: some flags at the top level about type based patterns
1901 * would be faster than inclusion in the hash table.
1904 if (pat->steps[0].value != NULL)
1905 name = pat->steps[0].value;
1907 top = (xsltCompMatchPtr *) &(style->piMatch);
1909 case XSLT_OP_COMMENT:
1910 top = (xsltCompMatchPtr *) &(style->commentMatch);
1913 top = (xsltCompMatchPtr *) &(style->textMatch);
1917 if (pat->steps[0].value != NULL)
1918 name = pat->steps[0].value;
1920 top = (xsltCompMatchPtr *) &(style->elemMatch);
1924 if (style->templatesHash == NULL) {
1925 style->templatesHash = xmlHashCreate(1024);
1926 if (style->templatesHash == NULL) {
1927 xsltFreeCompMatch(pat);
1930 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1932 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1933 name, mode, modeURI);
1935 xmlHashAddEntry3(style->templatesHash, name,
1936 mode, modeURI, pat);
1939 * Note '<=' since one must choose among the matching
1940 * template rules that are left, the one that occurs
1941 * last in the stylesheet
1943 if (list->priority <= pat->priority) {
1945 xmlHashUpdateEntry3(style->templatesHash, name,
1946 mode, modeURI, pat, NULL);
1948 while (list->next != NULL) {
1949 if (list->next->priority <= pat->priority)
1953 pat->next = list->next;
1958 } else if (top != NULL) {
1963 } else if (list->priority <= pat->priority) {
1967 while (list->next != NULL) {
1968 if (list->next->priority <= pat->priority)
1972 pat->next = list->next;
1976 xsltPrintErrorContext(NULL, style, NULL);
1977 xsltGenericError(xsltGenericErrorContext,
1978 "xsltAddTemplate: invalid compiled pattern\n");
1979 xsltFreeCompMatch(pat);
1982 #ifdef WITH_XSLT_DEBUG_PATTERN
1984 xsltGenericDebug(xsltGenericDebugContext,
1985 "added pattern : '%s' mode '%s' priority %f\n",
1986 pat->pattern, pat->mode, pat->priority);
1988 xsltGenericDebug(xsltGenericDebugContext,
1989 "added pattern : '%s' priority %f\n",
1990 pat->pattern, pat->priority);
2000 * @ctxt: a XSLT process context
2001 * @node: the node being processed
2002 * @style: the current style
2004 * Finds the template applying to this node, if @style is non-NULL
2005 * it means one needs to look for the next imported template in scope.
2007 * Returns the xsltTemplatePtr or NULL if not found
2010 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2011 xsltStylesheetPtr style) {
2012 xsltStylesheetPtr curstyle;
2013 xsltTemplatePtr ret = NULL;
2014 const xmlChar *name = NULL;
2015 xsltCompMatchPtr list = NULL;
2018 if ((ctxt == NULL) || (node == NULL))
2021 if (style == NULL) {
2022 curstyle = ctxt->style;
2024 curstyle = xsltNextImport(style);
2027 while ((curstyle != NULL) && (curstyle != style)) {
2028 priority = XSLT_PAT_NO_PRIORITY;
2029 /* TODO : handle IDs/keys here ! */
2030 if (curstyle->templatesHash != NULL) {
2032 * Use the top name as selector
2034 switch (node->type) {
2035 case XML_ELEMENT_NODE:
2036 case XML_ATTRIBUTE_NODE:
2040 case XML_DOCUMENT_NODE:
2041 case XML_HTML_DOCUMENT_NODE:
2043 case XML_CDATA_SECTION_NODE:
2044 case XML_COMMENT_NODE:
2045 case XML_ENTITY_REF_NODE:
2046 case XML_ENTITY_NODE:
2047 case XML_DOCUMENT_TYPE_NODE:
2048 case XML_DOCUMENT_FRAG_NODE:
2049 case XML_NOTATION_NODE:
2051 case XML_ELEMENT_DECL:
2052 case XML_ATTRIBUTE_DECL:
2053 case XML_ENTITY_DECL:
2054 case XML_NAMESPACE_DECL:
2055 case XML_XINCLUDE_START:
2056 case XML_XINCLUDE_END:
2065 * find the list of appliable expressions based on the name
2067 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2068 name, ctxt->mode, ctxt->modeURI);
2071 while (list != NULL) {
2072 if (xsltTestCompMatch(ctxt, list, node,
2073 ctxt->mode, ctxt->modeURI)) {
2074 ret = list->template;
2075 priority = list->priority;
2083 * find alternate generic matches
2085 switch (node->type) {
2086 case XML_ELEMENT_NODE:
2087 list = curstyle->elemMatch;
2089 case XML_ATTRIBUTE_NODE:
2090 list = curstyle->attrMatch;
2093 list = curstyle->piMatch;
2095 case XML_DOCUMENT_NODE:
2096 case XML_HTML_DOCUMENT_NODE:
2097 list = curstyle->rootMatch;
2100 case XML_CDATA_SECTION_NODE:
2101 list = curstyle->textMatch;
2103 case XML_COMMENT_NODE:
2104 list = curstyle->commentMatch;
2106 case XML_ENTITY_REF_NODE:
2107 case XML_ENTITY_NODE:
2108 case XML_DOCUMENT_TYPE_NODE:
2109 case XML_DOCUMENT_FRAG_NODE:
2110 case XML_NOTATION_NODE:
2112 case XML_ELEMENT_DECL:
2113 case XML_ATTRIBUTE_DECL:
2114 case XML_ENTITY_DECL:
2115 case XML_NAMESPACE_DECL:
2116 case XML_XINCLUDE_START:
2117 case XML_XINCLUDE_END:
2123 while ((list != NULL) &&
2124 ((ret == NULL) || (list->priority > priority))) {
2125 if (xsltTestCompMatch(ctxt, list, node,
2126 ctxt->mode, ctxt->modeURI)) {
2127 ret = list->template;
2128 priority = list->priority;
2134 * Some of the tests for elements can also apply to documents
2136 if ((node->type == XML_DOCUMENT_NODE) ||
2137 (node->type == XML_HTML_DOCUMENT_NODE)) {
2138 list = curstyle->elemMatch;
2139 while ((list != NULL) &&
2140 ((ret == NULL) || (list->priority > priority))) {
2141 if (xsltTestCompMatch(ctxt, list, node,
2142 ctxt->mode, ctxt->modeURI)) {
2143 ret = list->template;
2144 priority = list->priority;
2151 if (node->_private != NULL) {
2152 list = curstyle->keyMatch;
2153 while ((list != NULL) &&
2154 ((ret == NULL) || (list->priority > priority))) {
2155 if (xsltTestCompMatch(ctxt, list, node,
2156 ctxt->mode, ctxt->modeURI)) {
2157 ret = list->template;
2158 priority = list->priority;
2168 * Cycle on next curstylesheet import.
2170 curstyle = xsltNextImport(curstyle);
2176 * xsltCleanupTemplates:
2177 * @style: an XSLT stylesheet
2179 * Cleanup the state of the templates used by the stylesheet and
2180 * the ones it imports.
2183 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2187 * xsltFreeTemplateHashes:
2188 * @style: an XSLT stylesheet
2190 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2193 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2194 if (style->templatesHash != NULL)
2195 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2196 (xmlHashDeallocator) xsltFreeCompMatchList);
2197 if (style->rootMatch != NULL)
2198 xsltFreeCompMatchList(style->rootMatch);
2199 if (style->keyMatch != NULL)
2200 xsltFreeCompMatchList(style->keyMatch);
2201 if (style->elemMatch != NULL)
2202 xsltFreeCompMatchList(style->elemMatch);
2203 if (style->attrMatch != NULL)
2204 xsltFreeCompMatchList(style->attrMatch);
2205 if (style->parentMatch != NULL)
2206 xsltFreeCompMatchList(style->parentMatch);
2207 if (style->textMatch != NULL)
2208 xsltFreeCompMatchList(style->textMatch);
2209 if (style->piMatch != NULL)
2210 xsltFreeCompMatchList(style->piMatch);
2211 if (style->commentMatch != NULL)
2212 xsltFreeCompMatchList(style->commentMatch);
2218 * @node: a node in the source tree
2219 * @pattern: an XSLT pattern
2220 * @ctxtdoc: context document (for namespaces)
2221 * @ctxtnode: context node (for namespaces)
2223 * Determine if a node matches a pattern.
2226 xsltMatchPattern(xsltTransformContextPtr context,
2228 const xmlChar *pattern,
2230 xmlNodePtr ctxtnode)
2233 xsltCompMatchPtr first, comp;
2235 if ((context != NULL) && (pattern != NULL)) {
2236 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2237 for (comp = first; comp != NULL; comp = comp->next) {
2238 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2243 xsltFreeCompMatchList(first);