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.
9 * Daniel.Veillard@imag.fr
13 * TODO: handle pathological cases like *[*[@a="b"]]
14 * TODO: detect [number] at compilation, optimize accordingly
17 #include "xsltconfig.h"
21 #include <libxml/xmlmemory.h>
22 #include <libxml/tree.h>
23 #include <libxml/valid.h>
24 #include <libxml/hash.h>
25 #include <libxml/xmlerror.h>
26 #include <libxml/parserInternals.h>
28 #include "xsltInternals.h"
29 #include "xsltutils.h"
31 #include "templates.h"
35 #ifdef WITH_XSLT_DEBUG
36 #define WITH_XSLT_DEBUG_PATTERN
63 typedef struct _xsltStepOp xsltStepOp;
64 typedef xsltStepOp *xsltStepOpPtr;
70 xmlXPathCompExprPtr comp;
72 * Optimisations for count
79 struct _xsltCompMatch {
80 struct _xsltCompMatch *next; /* siblings in the name hash */
81 float priority; /* the priority */
82 const xmlChar *mode; /* the mode */
83 const xmlChar *modeURI; /* the mode URI */
84 xsltTemplatePtr template; /* the associated template */
86 /* TODO fix the statically allocated size steps[] */
89 xmlNsPtr *nsList; /* the namespaces in scope */
90 int nsNr; /* the number of namespaces in scope */
91 xsltStepOp steps[20]; /* ops for computation */
94 typedef struct _xsltParserContext xsltParserContext;
95 typedef xsltParserContext *xsltParserContextPtr;
96 struct _xsltParserContext {
97 const xmlChar *cur; /* the current char being parsed */
98 const xmlChar *base; /* the full expression */
99 xmlDocPtr doc; /* the source document */
100 xmlNodePtr elem; /* the source element */
101 int error; /* error code */
102 xsltCompMatchPtr comp; /* the result */
105 /************************************************************************
109 ************************************************************************/
114 * Create a new XSLT CompMatch
116 * Returns the newly allocated xsltCompMatchPtr or NULL in case of error
118 static xsltCompMatchPtr
119 xsltNewCompMatch(void) {
120 xsltCompMatchPtr cur;
122 cur = (xsltCompMatchPtr) xmlMalloc(sizeof(xsltCompMatch));
124 xsltGenericError(xsltGenericErrorContext,
125 "xsltNewCompMatch : malloc failed\n");
128 memset(cur, 0, sizeof(xsltCompMatch));
137 * @comp: an XSLT comp
139 * Free up the memory allocated by @comp
142 xsltFreeCompMatch(xsltCompMatchPtr comp) {
148 if (comp->mode != NULL)
149 xmlFree((xmlChar *)comp->mode);
150 if (comp->modeURI != NULL)
151 xmlFree((xmlChar *)comp->modeURI);
152 if (comp->nsList != NULL)
153 xmlFree(comp->nsList);
154 for (i = 0;i < comp->nbStep;i++) {
155 op = &comp->steps[i];
156 if (op->value != NULL)
158 if (op->value2 != NULL)
160 if (op->value3 != NULL)
162 if (op->comp != NULL)
163 xmlXPathFreeCompExpr(op->comp);
165 memset(comp, -1, sizeof(xsltCompMatch));
170 * xsltFreeCompMatchList:
171 * @comp: an XSLT comp list
173 * Free up the memory allocated by all the elements of @comp
176 xsltFreeCompMatchList(xsltCompMatchPtr comp) {
177 xsltCompMatchPtr cur;
179 while (comp != NULL) {
182 xsltFreeCompMatch(cur);
187 * xsltNewParserContext:
189 * Create a new XSLT ParserContext
191 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
193 static xsltParserContextPtr
194 xsltNewParserContext(void) {
195 xsltParserContextPtr cur;
197 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
199 xsltGenericError(xsltGenericErrorContext,
200 "xsltNewParserContext : malloc failed\n");
203 memset(cur, 0, sizeof(xsltParserContext));
208 * xsltFreeParserContext:
209 * @ctxt: an XSLT parser context
211 * Free up the memory allocated by @ctxt
214 xsltFreeParserContext(xsltParserContextPtr ctxt) {
217 memset(ctxt, -1, sizeof(xsltParserContext));
223 * @comp: the compiled match expression
225 * @value: the first value
226 * @value2: the second value
228 * Add an step to an XSLT Compiled Match
230 * Returns -1 in case of failure, 0 otherwise.
233 xsltCompMatchAdd(xsltCompMatchPtr comp, xsltOp op, xmlChar *value,
235 if (comp->nbStep >= 20) {
236 xsltGenericError(xsltGenericErrorContext,
237 "xsltCompMatchAddOp: overflow\n");
240 comp->steps[comp->nbStep].op = op;
241 comp->steps[comp->nbStep].value = value;
242 comp->steps[comp->nbStep].value2 = value2;
248 * xsltSwapTopCompMatch:
249 * @comp: the compiled match expression
251 * reverse the two top steps.
254 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
256 int j = comp->nbStep - 1;
259 register xmlChar *tmp;
262 tmp = comp->steps[i].value;
263 comp->steps[i].value = comp->steps[j].value;
264 comp->steps[j].value = tmp;
265 tmp = comp->steps[i].value2;
266 comp->steps[i].value2 = comp->steps[j].value2;
267 comp->steps[j].value2 = tmp;
268 op = comp->steps[i].op;
269 comp->steps[i].op = comp->steps[j].op;
270 comp->steps[j].op = op;
275 * xsltReverseCompMatch:
276 * @comp: the compiled match expression
278 * reverse all the stack of expressions
281 xsltReverseCompMatch(xsltCompMatchPtr comp) {
283 int j = comp->nbStep - 1;
286 register xmlChar *tmp;
288 tmp = comp->steps[i].value;
289 comp->steps[i].value = comp->steps[j].value;
290 comp->steps[j].value = tmp;
291 tmp = comp->steps[i].value2;
292 comp->steps[i].value2 = comp->steps[j].value2;
293 comp->steps[j].value2 = tmp;
294 op = comp->steps[i].op;
295 comp->steps[i].op = comp->steps[j].op;
296 comp->steps[j].op = op;
300 comp->steps[comp->nbStep++].op = XSLT_OP_END;
304 * xsltCleanupCompMatch:
305 * @comp: the compiled match expression
307 * remove all computation state from the pattern
310 xsltCleanupCompMatch(xsltCompMatchPtr comp) {
313 for (i = 0;i < comp->nbStep;i++) {
314 comp->steps[i].previous = NULL;
318 /************************************************************************
320 * The interpreter for the precompiled patterns *
322 ************************************************************************/
326 * @ctxt: a XSLT process context
327 * @comp: the precompiled pattern
329 * @mode: the mode name or NULL
330 * @modeURI: the mode URI or NULL
332 * Test wether the node matches the pattern
334 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
337 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
338 xmlNodePtr node, const xmlChar *mode,
339 const xmlChar *modeURI) {
341 xsltStepOpPtr step, select = NULL;
343 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
344 xsltGenericError(xsltGenericErrorContext,
345 "xsltTestCompMatch: null arg\n");
349 if (comp->mode == NULL)
351 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
354 if (comp->mode != NULL)
357 if (modeURI != NULL) {
358 if (comp->modeURI == NULL)
360 if ((comp->modeURI != modeURI) &&
361 (!xmlStrEqual(comp->modeURI, modeURI)))
364 if (comp->modeURI != NULL)
367 for (i = 0;i < comp->nbStep;i++) {
368 step = &comp->steps[i];
369 if (step->op != XSLT_OP_PREDICATE)
375 if ((node->type == XML_DOCUMENT_NODE) ||
376 (node->type == XML_HTML_DOCUMENT_NODE))
380 if (node->type != XML_ELEMENT_NODE)
382 if (step->value == NULL)
384 if (!xmlStrEqual(step->value, node->name))
388 if (node->ns == NULL) {
389 if (step->value2 != NULL)
391 } else if (node->ns->href != NULL) {
392 if (step->value2 == NULL)
394 if (!xmlStrEqual(step->value2, node->ns->href))
399 TODO /* Handle OP_CHILD */
402 if (node->type != XML_ATTRIBUTE_NODE)
404 if (step->value == NULL)
406 if (!xmlStrEqual(step->value, node->name))
410 if (node->ns == NULL) {
411 if (step->value2 != NULL)
413 } else if (node->ns->href != NULL) {
414 if (step->value2 == NULL)
416 if (!xmlStrEqual(step->value2, node->ns->href))
424 if (step->value == NULL)
426 if (!xmlStrEqual(step->value, node->name))
429 if (node->ns == NULL) {
430 if (step->value2 != NULL)
432 } else if (node->ns->href != NULL) {
433 if (step->value2 == NULL)
435 if (!xmlStrEqual(step->value2, node->ns->href))
439 case XSLT_OP_ANCESTOR:
440 /* TODO: implement coalescing of ANCESTOR/NODE ops */
441 if (step->value == NULL) {
443 step = &comp->steps[i];
444 if (step->op == XSLT_OP_ROOT)
446 if (step->op != XSLT_OP_ELEM)
448 if (step->value == NULL)
454 while (node != NULL) {
457 if (xmlStrEqual(step->value, node->name)) {
459 if (node->ns == NULL) {
460 if (step->value2 == NULL)
462 } else if (node->ns->href != NULL) {
463 if ((step->value2 != NULL) &&
464 (xmlStrEqual(step->value2, node->ns->href)))
474 /* TODO Handle IDs decently, must be done differently */
477 id = xmlGetID(node->doc, step->value);
478 if ((id == NULL) || (id->parent != node))
486 list = xsltGetKey(ctxt, step->value,
487 step->value3, step->value2);
490 for (indx = 0;indx < list->nodeNr;indx++)
491 if (list->nodeTab[indx] == node)
493 if (indx >= list->nodeNr)
499 if (node->ns == NULL) {
500 if (step->value != NULL)
502 } else if (node->ns->href != NULL) {
503 if (step->value == NULL)
505 if (!xmlStrEqual(step->value, node->ns->href))
510 switch (node->type) {
511 case XML_DOCUMENT_NODE:
512 case XML_HTML_DOCUMENT_NODE:
513 case XML_ELEMENT_NODE:
519 case XSLT_OP_PREDICATE: {
522 int pos = 0, len = 0;
524 * Depending on the last selection, one may need to
525 * recompute contextSize and proximityPosition.
527 oldCS = ctxt->xpathCtxt->contextSize;
528 oldCP = ctxt->xpathCtxt->proximityPosition;
529 if ((select != NULL) &&
530 (select->op == XSLT_OP_ELEM) &&
531 (select->value != NULL) &&
532 (node->type == XML_ELEMENT_NODE) &&
533 (node->parent != NULL)) {
535 if ((select->previous != NULL) &&
536 (select->previous->parent == node->parent)) {
538 * just walk back to adjust the index
541 xmlNodePtr sibling = node;
543 while (sibling != NULL) {
544 if (sibling == select->previous)
546 if (xmlStrEqual(node->name, sibling->name)) {
547 if ((select->value2 == NULL) ||
548 ((sibling->ns != NULL) &&
549 (xmlStrEqual(select->value2,
550 sibling->ns->href))))
553 sibling = sibling->prev;
555 if (sibling == NULL) {
556 /* hum going backward in document order ... */
559 while (sibling != NULL) {
560 if (sibling == select->previous)
562 if ((select->value2 == NULL) ||
563 ((sibling->ns != NULL) &&
564 (xmlStrEqual(select->value2,
565 sibling->ns->href))))
567 sibling = sibling->next;
570 if (sibling != NULL) {
571 pos = select->index + indx;
573 select->previous = node;
579 * recompute the index
581 xmlNodePtr siblings = node->parent->children;
583 while (siblings != NULL) {
584 if (siblings->type == XML_ELEMENT_NODE) {
585 if (siblings == node) {
588 } else if (xmlStrEqual(node->name,
590 if ((select->value2 == NULL) ||
591 ((siblings->ns != NULL) &&
592 (xmlStrEqual(select->value2,
593 siblings->ns->href))))
597 siblings = siblings->next;
601 ctxt->xpathCtxt->contextSize = len;
602 ctxt->xpathCtxt->proximityPosition = pos;
603 select->previous = node;
607 } else if ((select != NULL) && (select->op == XSLT_OP_ALL)) {
608 if ((select->previous != NULL) &&
609 (select->previous->parent == node->parent)) {
611 * just walk back to adjust the index
614 xmlNodePtr sibling = node;
616 while (sibling != NULL) {
617 if (sibling == select->previous)
619 if (sibling->type == XML_ELEMENT_NODE)
621 sibling = sibling->prev;
623 if (sibling == NULL) {
624 /* hum going backward in document order ... */
627 while (sibling != NULL) {
628 if (sibling == select->previous)
630 if (sibling->type == XML_ELEMENT_NODE)
632 sibling = sibling->next;
635 if (sibling != NULL) {
636 pos = select->index + indx;
638 select->previous = node;
644 * recompute the index
646 xmlNodePtr siblings = node->parent->children;
648 while (siblings != NULL) {
649 if (siblings->type == XML_ELEMENT_NODE) {
651 if (siblings == node) {
655 siblings = siblings->next;
659 ctxt->xpathCtxt->contextSize = len;
660 ctxt->xpathCtxt->proximityPosition = pos;
661 select->previous = node;
666 oldNode = ctxt->node;
669 if (step->value == NULL)
672 if (step->comp == NULL) {
673 step->comp = xmlXPathCompile(step->value);
674 if (step->comp == NULL)
677 if (comp->nsList == NULL) {
680 comp->nsList = xmlGetNsList(node->doc, node);
681 if (comp->nsList != NULL) {
682 while (comp->nsList[j] != NULL)
687 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
692 ctxt->xpathCtxt->contextSize = oldCS;
693 ctxt->xpathCtxt->proximityPosition = oldCP;
695 ctxt->node = oldNode;
699 ctxt->xpathCtxt->contextSize = oldCS;
700 ctxt->xpathCtxt->proximityPosition = oldCP;
702 ctxt->node = oldNode;
706 if (node->type != XML_PI_NODE)
708 if (step->value != NULL) {
709 if (!xmlStrEqual(step->value, node->name))
713 case XSLT_OP_COMMENT:
714 if (node->type != XML_COMMENT_NODE)
718 if ((node->type != XML_TEXT_NODE) &&
719 (node->type != XML_CDATA_SECTION_NODE))
723 switch (node->type) {
724 case XML_DOCUMENT_NODE:
725 case XML_HTML_DOCUMENT_NODE:
726 case XML_ELEMENT_NODE:
727 case XML_CDATA_SECTION_NODE:
729 case XML_COMMENT_NODE:
731 case XML_ATTRIBUTE_NODE:
743 * xsltTestCompMatchList:
744 * @ctxt: a XSLT process context
746 * @comp: the precompiled pattern list
748 * Test wether the node matches one of the patterns in the list
750 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
753 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
754 xsltCompMatchPtr comp) {
757 if ((ctxt == NULL) || (node == NULL))
759 while (comp != NULL) {
760 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
768 /************************************************************************
770 * Dedicated parser for templates *
772 ************************************************************************/
774 #define CUR (*ctxt->cur)
775 #define SKIP(val) ctxt->cur += (val)
776 #define NXT(val) ctxt->cur[(val)]
777 #define CUR_PTR ctxt->cur
779 #define SKIP_BLANKS \
780 while (IS_BLANK(CUR)) NEXT
782 #define CURRENT (*ctxt->cur)
783 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
786 #define PUSH(op, val, val2) \
787 if (xsltCompMatchAdd(ctxt->comp, (op), (val), (val2))) goto error;
790 xsltSwapTopCompMatch(ctxt->comp);
792 #define XSLT_ERROR(X) \
793 { xsltError(ctxt, __FILE__, __LINE__, X); \
794 ctxt->error = (X); return; }
796 #define XSLT_ERROR0(X) \
797 { xsltError(ctxt, __FILE__, __LINE__, X); \
798 ctxt->error = (X); return(0); }
802 * @ctxt: the XPath Parser context
804 * Parse an XPath Litteral:
806 * [29] Literal ::= '"' [^"]* '"'
809 * Returns the Literal parsed or NULL
813 xsltScanLiteral(xsltParserContextPtr ctxt) {
821 while ((IS_CHAR(CUR)) && (CUR != '"'))
824 /* XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); */
828 ret = xmlStrndup(q, CUR_PTR - q);
831 } else if (CUR == '\'') {
834 while ((IS_CHAR(CUR)) && (CUR != '\''))
837 /* XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); */
841 ret = xmlStrndup(q, CUR_PTR - q);
845 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
854 * @ctxt: the XPath Parser context
856 * Trickery: parse an XML name but without consuming the input flow
857 * Needed to avoid insanity in the parser state.
859 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
860 * CombiningChar | Extender
862 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
864 * [6] Names ::= Name (S Name)*
866 * Returns the Name parsed or NULL
870 xsltScanName(xsltParserContextPtr ctxt) {
871 xmlChar buf[XML_MAX_NAMELEN];
875 if (!IS_LETTER(CUR) && (CUR != '_') &&
880 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
881 (NXT(len) == '.') || (NXT(len) == '-') ||
882 (NXT(len) == '_') || (NXT(len) == ':') ||
883 (IS_COMBINING(NXT(len))) ||
884 (IS_EXTENDER(NXT(len)))) {
887 if (len >= XML_MAX_NAMELEN) {
888 xmlGenericError(xmlGenericErrorContext,
889 "xmlScanName: reached XML_MAX_NAMELEN limit\n");
890 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
891 (NXT(len) == '.') || (NXT(len) == '-') ||
892 (NXT(len) == '_') || (NXT(len) == ':') ||
893 (IS_COMBINING(NXT(len))) ||
894 (IS_EXTENDER(NXT(len))))
900 return(xmlStrndup(buf, len));
903 * xsltCompileIdKeyPattern:
904 * @comp: the compilation context
905 * @name: a preparsed name
906 * @aid: whether id/key are allowed there
908 * Compile the XSLT LocationIdKeyPattern
909 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
910 * | 'key' '(' Literal ',' Literal ')'
912 * also handle NodeType and PI from:
914 * [7] NodeTest ::= NameTest
916 * | 'processing-instruction' '(' Literal ')'
919 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
921 xmlChar *lit2 = NULL;
924 xsltGenericError(xsltGenericErrorContext,
925 "xsltCompileIdKeyPattern : ( expected\n");
929 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
932 lit = xsltScanLiteral(ctxt);
937 xsltGenericError(xsltGenericErrorContext,
938 "xsltCompileIdKeyPattern : ) expected\n");
943 PUSH(XSLT_OP_ID, lit, NULL);
944 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
947 lit = xsltScanLiteral(ctxt);
952 xsltGenericError(xsltGenericErrorContext,
953 "xsltCompileIdKeyPattern : , expected\n");
959 lit2 = xsltScanLiteral(ctxt);
964 xsltGenericError(xsltGenericErrorContext,
965 "xsltCompileIdKeyPattern : ) expected\n");
970 /* TODO: support namespace in keys */
971 PUSH(XSLT_OP_KEY, lit, lit2);
972 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
976 lit = xsltScanLiteral(ctxt);
981 xsltGenericError(xsltGenericErrorContext,
982 "xsltCompileIdKeyPattern : ) expected\n");
988 PUSH(XSLT_OP_PI, lit, NULL);
989 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
993 xsltGenericError(xsltGenericErrorContext,
994 "xsltCompileIdKeyPattern : ) expected\n");
999 PUSH(XSLT_OP_TEXT, NULL, NULL);
1000 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1004 xsltGenericError(xsltGenericErrorContext,
1005 "xsltCompileIdKeyPattern : ) expected\n");
1010 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1011 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1015 xsltGenericError(xsltGenericErrorContext,
1016 "xsltCompileIdKeyPattern : ) expected\n");
1021 PUSH(XSLT_OP_NODE, NULL, NULL);
1023 xsltGenericError(xsltGenericErrorContext,
1024 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1028 xsltGenericError(xsltGenericErrorContext,
1029 "xsltCompileIdKeyPattern : node type\n");
1039 * xsltCompileStepPattern:
1040 * @comp: the compilation context
1041 * @token: a posible precompiled name
1043 * Compile the XSLT StepPattern and generates a precompiled
1044 * form suitable for fast matching.
1046 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1047 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1048 * | ('child' | 'attribute') '::'
1050 * [7] NodeTest ::= NameTest
1051 * | NodeType '(' ')'
1052 * | 'processing-instruction' '(' Literal ')'
1053 * [8] Predicate ::= '[' PredicateExpr ']'
1054 * [9] PredicateExpr ::= Expr
1055 * [13] AbbreviatedAxisSpecifier ::= '@'?
1056 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1060 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1061 xmlChar *name = NULL;
1062 xmlChar *prefix = NULL;
1063 xmlChar *ncname = NULL;
1064 xmlChar *URL = NULL;
1068 if ((token == NULL) && (CUR == '@')) {
1072 PUSH(XSLT_OP_ATTR, NULL, NULL);
1075 token = xsltScanName(ctxt);
1076 if (token == NULL) {
1077 xsltGenericError(xsltGenericErrorContext,
1078 "xsltCompileStepPattern : Name expected\n");
1082 PUSH(XSLT_OP_ATTR, token, NULL);
1086 token = xsltScanName(ctxt);
1087 if (token == NULL) {
1090 PUSH(XSLT_OP_ALL, token, NULL);
1091 goto parse_predicate;
1093 xsltGenericError(xsltGenericErrorContext,
1094 "xsltCompileStepPattern : Name expected\n");
1103 xsltCompileIdKeyPattern(ctxt, token, 0);
1106 } else if (CUR == ':') {
1108 if (NXT(1) != ':') {
1109 xsltGenericError(xsltGenericErrorContext,
1110 "xsltCompileStepPattern : sequence '::' expected\n");
1115 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1116 name = xsltScanName(ctxt);
1118 xsltGenericError(xsltGenericErrorContext,
1119 "xsltCompileStepPattern : QName expected\n");
1123 ncname = xmlSplitQName2(name, &prefix);
1124 if (ncname != NULL) {
1125 if (prefix != NULL) {
1128 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1130 xsltGenericError(xsltGenericErrorContext,
1131 "xsl: pattern, no namespace bound to prefix %s\n",
1134 URL = xmlStrdup(ns->href);
1141 PUSH(XSLT_OP_CHILD, name, URL);
1142 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1143 name = xsltScanName(ctxt);
1145 xsltGenericError(xsltGenericErrorContext,
1146 "xsltCompileStepPattern : QName expected\n");
1150 ncname = xmlSplitQName2(name, &prefix);
1151 if (ncname != NULL) {
1152 if (prefix != NULL) {
1155 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1157 xsltGenericError(xsltGenericErrorContext,
1158 "xsl: pattern, no namespace bound to prefix %s\n",
1161 URL = xmlStrdup(ns->href);
1168 PUSH(XSLT_OP_ATTR, name, URL);
1170 xsltGenericError(xsltGenericErrorContext,
1171 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1176 } else if (CUR == '*') {
1178 PUSH(XSLT_OP_ALL, token, NULL);
1180 ncname = xmlSplitQName2(token, &prefix);
1181 if (ncname != NULL) {
1182 if (prefix != NULL) {
1185 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1187 xsltGenericError(xsltGenericErrorContext,
1188 "xsl: pattern, no namespace bound to prefix %s\n",
1191 URL = xmlStrdup(ns->href);
1198 PUSH(XSLT_OP_ELEM, token, URL);
1203 while (CUR == '[') {
1205 xmlChar *ret = NULL;
1210 /* TODO: avoid breaking in strings ... */
1211 while (IS_CHAR(CUR)) {
1212 /* Skip over nested predicates */
1222 if (!IS_CHAR(CUR)) {
1223 xsltGenericError(xsltGenericErrorContext,
1224 "xsltCompileStepPattern : ']' expected\n");
1228 ret = xmlStrndup(q, CUR_PTR - q);
1229 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1230 /* push the predicate lower than local test */
1243 * xsltCompileRelativePathPattern:
1244 * @comp: the compilation context
1245 * @token: a posible precompiled name
1247 * Compile the XSLT RelativePathPattern and generates a precompiled
1248 * form suitable for fast matching.
1250 * [4] RelativePathPattern ::= StepPattern
1251 * | RelativePathPattern '/' StepPattern
1252 * | RelativePathPattern '//' StepPattern
1255 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1256 xsltCompileStepPattern(ctxt, token);
1260 while ((CUR != 0) && (CUR != '|')) {
1261 if ((CUR == '/') && (NXT(1) == '/')) {
1262 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1266 xsltCompileStepPattern(ctxt, NULL);
1267 } else if (CUR == '/') {
1268 PUSH(XSLT_OP_PARENT, NULL, NULL);
1271 if ((CUR != 0) || (CUR == '|')) {
1272 xsltCompileRelativePathPattern(ctxt, NULL);
1286 * xsltCompileLocationPathPattern:
1287 * @comp: the compilation context
1289 * Compile the XSLT LocationPathPattern and generates a precompiled
1290 * form suitable for fast matching.
1292 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1293 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1294 * | '//'? RelativePathPattern
1297 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1299 if ((CUR == '/') && (NXT(1) == '/')) {
1301 * since we reverse the query
1302 * a leading // can be safely ignored
1306 xsltCompileRelativePathPattern(ctxt, NULL);
1307 } else if (CUR == '/') {
1309 * We need to find root as the parent
1313 PUSH(XSLT_OP_ROOT, NULL, NULL);
1314 if ((CUR != 0) || (CUR == '|')) {
1315 PUSH(XSLT_OP_PARENT, NULL, NULL);
1316 xsltCompileRelativePathPattern(ctxt, NULL);
1318 } else if (CUR == '*') {
1319 xsltCompileRelativePathPattern(ctxt, NULL);
1320 } else if (CUR == '@') {
1321 xsltCompileRelativePathPattern(ctxt, NULL);
1324 name = xsltScanName(ctxt);
1326 xsltGenericError(xsltGenericErrorContext,
1327 "xsltCompileLocationPathPattern : Name expected\n");
1332 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1333 xsltCompileIdKeyPattern(ctxt, name, 1);
1334 if ((CUR == '/') && (NXT(1) == '/')) {
1335 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1339 xsltCompileRelativePathPattern(ctxt, NULL);
1340 } else if (CUR == '/') {
1341 PUSH(XSLT_OP_PARENT, NULL, NULL);
1344 xsltCompileRelativePathPattern(ctxt, NULL);
1348 xsltCompileRelativePathPattern(ctxt, name);
1355 * xsltCompilePattern:
1356 * @pattern an XSLT pattern
1357 * @doc: the containing document
1358 * @node: the containing element
1360 * Compile the XSLT pattern and generates a list of precompiled form suitable
1361 * for fast matching.
1363 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1365 * Returns the generated pattern list or NULL in case of failure
1369 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc, xmlNodePtr node) {
1370 xsltParserContextPtr ctxt = NULL;
1371 xsltCompMatchPtr element, first = NULL, previous = NULL;
1372 int current, start, end;
1374 if (pattern == NULL) {
1375 xsltGenericError(xsltGenericErrorContext,
1376 "xsltCompilePattern : NULL pattern\n");
1380 #ifdef WITH_XSLT_DEBUG_PATTERN
1381 xsltGenericDebug(xsltGenericDebugContext,
1382 "xsltCompilePattern : parsing '%s'\n", pattern);
1385 ctxt = xsltNewParserContext();
1391 while (pattern[current] != 0) {
1393 while (IS_BLANK(pattern[current]))
1396 while ((pattern[end] != 0) && (pattern[end] != '|'))
1398 if (current == end) {
1399 xsltGenericError(xsltGenericErrorContext,
1400 "xsltCompilePattern : NULL pattern\n");
1403 element = xsltNewCompMatch();
1404 if (element == NULL) {
1409 else if (previous != NULL)
1410 previous->next = element;
1413 ctxt->comp = element;
1414 ctxt->base = xmlStrndup(&pattern[start], end - start);
1415 ctxt->cur = &(ctxt->base)[current - start];
1416 xsltCompileLocationPathPattern(ctxt);
1418 xmlFree((xmlChar *)ctxt->base);
1423 * Reverse for faster interpretation.
1425 xsltReverseCompMatch(element);
1428 * Set-up the priority
1430 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1431 (element->steps[0].op == XSLT_OP_ATTR)) &&
1432 (element->steps[0].value != NULL) &&
1433 (element->steps[1].op == XSLT_OP_END)) {
1434 element->priority = 0;
1435 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1436 (element->steps[1].op == XSLT_OP_END)) {
1437 element->priority = 0;
1438 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1439 (element->steps[0].value != NULL) &&
1440 (element->steps[1].op == XSLT_OP_END)) {
1441 element->priority = 0;
1442 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1443 (element->steps[0].value != NULL) &&
1444 (element->steps[1].op == XSLT_OP_END)) {
1445 element->priority = -0.25;
1446 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1447 (element->steps[0].op == XSLT_OP_TEXT) ||
1448 (element->steps[0].op == XSLT_OP_ALL) ||
1449 (element->steps[0].op == XSLT_OP_NODE) ||
1450 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1451 (element->steps[1].op == XSLT_OP_END)) {
1452 element->priority = -0.5;
1454 element->priority = 0.5;
1456 if (pattern[end] == '|')
1461 xsltGenericError(xsltGenericErrorContext,
1462 "xsltCompilePattern : NULL pattern\n");
1466 xsltFreeParserContext(ctxt);
1471 xsltFreeParserContext(ctxt);
1473 xsltFreeCompMatchList(first);
1477 /************************************************************************
1479 * Module interfaces *
1481 ************************************************************************/
1485 * @style: an XSLT stylesheet
1486 * @cur: an XSLT template
1487 * @mode: the mode name or NULL
1488 * @modeURI: the mode URI or NULL
1490 * Register the XSLT pattern associated to @cur
1492 * Returns -1 in case of error, 0 otherwise
1495 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1496 const xmlChar *mode, const xmlChar *modeURI) {
1497 xsltCompMatchPtr pat, list, *top = NULL, next;
1498 const xmlChar *name = NULL;
1500 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1503 pat = xsltCompilePattern(cur->match, style->doc, cur->elem);
1508 pat->template = cur;
1510 pat->mode = xmlStrdup(mode);
1511 if (modeURI != NULL)
1512 pat->modeURI = xmlStrdup(modeURI);
1513 if (cur->priority == XSLT_PAT_NO_PRIORITY)
1514 cur->priority = pat->priority;
1516 pat->priority = cur->priority;
1519 * insert it in the hash table list corresponding to its lookup name
1521 switch (pat->steps[0].op) {
1523 if (pat->steps[0].value != NULL)
1524 name = pat->steps[0].value;
1526 top = (xsltCompMatchPtr *) &(style->attrMatch);
1530 case XSLT_OP_PARENT:
1531 case XSLT_OP_ANCESTOR:
1533 name = pat->steps[0].value;
1536 top = (xsltCompMatchPtr *) &(style->rootMatch);
1539 top = (xsltCompMatchPtr *) &(style->keyMatch);
1542 /* TODO optimize ID !!! */
1544 top = (xsltCompMatchPtr *) &(style->elemMatch);
1547 case XSLT_OP_PREDICATE:
1548 xsltGenericError(xsltGenericErrorContext,
1549 "xsltAddTemplate: invalid compiled pattern\n");
1550 xsltFreeCompMatch(pat);
1553 * TODO: some flags at the top level about type based patterns
1554 * would be faster than inclusion in the hash table.
1557 if (pat->steps[0].value != NULL)
1558 name = pat->steps[0].value;
1560 top = (xsltCompMatchPtr *) &(style->piMatch);
1562 case XSLT_OP_COMMENT:
1563 top = (xsltCompMatchPtr *) &(style->commentMatch);
1566 top = (xsltCompMatchPtr *) &(style->textMatch);
1569 if (pat->steps[0].value != NULL)
1570 name = pat->steps[0].value;
1572 top = (xsltCompMatchPtr *) &(style->elemMatch);
1577 if (style->templatesHash == NULL) {
1578 style->templatesHash = xmlHashCreate(1024);
1579 if (style->templatesHash == NULL) {
1580 xsltFreeCompMatch(pat);
1583 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1585 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1586 name, mode, modeURI);
1588 xmlHashAddEntry3(style->templatesHash, name,
1589 mode, modeURI, pat);
1592 * Note '<=' since one must choose among the matching
1593 * template rules that are left, the one that occurs
1594 * last in the stylesheet
1596 if (list->priority <= pat->priority) {
1598 xmlHashUpdateEntry3(style->templatesHash, name,
1599 mode, modeURI, pat, NULL);
1601 while (list->next != NULL) {
1602 if (list->next->priority <= pat->priority)
1606 pat->next = list->next;
1611 } else if (top != NULL) {
1616 } else if (list->priority <= pat->priority) {
1620 while (list->next != NULL) {
1621 if (list->next->priority <= pat->priority)
1625 pat->next = list->next;
1629 xsltGenericError(xsltGenericErrorContext,
1630 "xsltAddTemplate: invalid compiled pattern\n");
1631 xsltFreeCompMatch(pat);
1634 #ifdef WITH_XSLT_DEBUG_PATTERN
1636 xsltGenericDebug(xsltGenericDebugContext,
1637 "added pattern : '%s' mode '%s' priority %f\n",
1638 pat->template->match, pat->mode, pat->priority);
1640 xsltGenericDebug(xsltGenericDebugContext,
1641 "added pattern : '%s' priority %f\n",
1642 pat->template->match, pat->priority);
1652 * @ctxt: a XSLT process context
1654 * @style: the current style
1656 * Finds the template applying to this node, if @style is non-NULL
1657 * it means one need to look for the next imported template in scope.
1659 * Returns the xsltTemplatePtr or NULL if not found
1662 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
1663 xsltStylesheetPtr style) {
1664 xsltStylesheetPtr curstyle;
1665 xsltTemplatePtr ret = NULL;
1666 const xmlChar *name = NULL;
1667 xsltCompMatchPtr list = NULL;
1669 if ((ctxt == NULL) || (node == NULL))
1672 if (style == NULL) {
1673 curstyle = ctxt->style;
1675 curstyle = xsltNextImport(style);
1678 while ((curstyle != NULL) && (curstyle != style)) {
1679 /* TODO : handle IDs/keys here ! */
1680 if (curstyle->templatesHash != NULL) {
1682 * Use the top name as selector
1684 switch (node->type) {
1685 case XML_ELEMENT_NODE:
1686 case XML_ATTRIBUTE_NODE:
1690 case XML_DOCUMENT_NODE:
1691 case XML_HTML_DOCUMENT_NODE:
1693 case XML_CDATA_SECTION_NODE:
1694 case XML_COMMENT_NODE:
1695 case XML_ENTITY_REF_NODE:
1696 case XML_ENTITY_NODE:
1697 case XML_DOCUMENT_TYPE_NODE:
1698 case XML_DOCUMENT_FRAG_NODE:
1699 case XML_NOTATION_NODE:
1701 case XML_ELEMENT_DECL:
1702 case XML_ATTRIBUTE_DECL:
1703 case XML_ENTITY_DECL:
1704 case XML_NAMESPACE_DECL:
1705 case XML_XINCLUDE_START:
1706 case XML_XINCLUDE_END:
1715 * find the list of appliable expressions based on the name
1717 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
1718 name, ctxt->mode, ctxt->modeURI);
1721 while (list != NULL) {
1722 if (xsltTestCompMatch(ctxt, list, node,
1723 ctxt->mode, ctxt->modeURI)) {
1724 ret = list->template;
1732 * find alternate generic matches
1734 switch (node->type) {
1735 case XML_ELEMENT_NODE:
1736 list = curstyle->elemMatch;
1738 case XML_ATTRIBUTE_NODE:
1739 list = curstyle->attrMatch;
1742 list = curstyle->piMatch;
1744 case XML_DOCUMENT_NODE:
1745 case XML_HTML_DOCUMENT_NODE:
1746 list = curstyle->rootMatch;
1749 case XML_CDATA_SECTION_NODE:
1750 list = curstyle->textMatch;
1752 case XML_COMMENT_NODE:
1753 list = curstyle->commentMatch;
1755 case XML_ENTITY_REF_NODE:
1756 case XML_ENTITY_NODE:
1757 case XML_DOCUMENT_TYPE_NODE:
1758 case XML_DOCUMENT_FRAG_NODE:
1759 case XML_NOTATION_NODE:
1761 case XML_ELEMENT_DECL:
1762 case XML_ATTRIBUTE_DECL:
1763 case XML_ENTITY_DECL:
1764 case XML_NAMESPACE_DECL:
1765 case XML_XINCLUDE_START:
1766 case XML_XINCLUDE_END:
1772 while ((list != NULL) &&
1773 ((ret == NULL) || (list->priority > ret->priority))) {
1774 if (xsltTestCompMatch(ctxt, list, node,
1775 ctxt->mode, ctxt->modeURI)) {
1776 ret = list->template;
1781 if (node->_private != NULL) {
1782 list = curstyle->keyMatch;
1783 while ((list != NULL) &&
1784 ((ret == NULL) || (list->priority > ret->priority))) {
1785 if (xsltTestCompMatch(ctxt, list, node,
1786 ctxt->mode, ctxt->modeURI)) {
1787 ret = list->template;
1797 * Cycle on next curstylesheet import.
1799 curstyle = xsltNextImport(curstyle);
1805 * xsltCleanupTemplates:
1806 * @style: an XSLT stylesheet
1808 * Cleanup the state of the templates used by the stylesheet and
1809 * the ones it imports.
1812 xsltCleanupTemplates(xsltStylesheetPtr style) {
1813 while (style != NULL) {
1814 xmlHashScan((xmlHashTablePtr) style->templatesHash,
1815 (xmlHashScanner) xsltCleanupCompMatch, NULL);
1817 style = xsltNextImport(style);
1822 * xsltFreeTemplateHashes:
1823 * @style: an XSLT stylesheet
1825 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
1828 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
1829 if (style->templatesHash != NULL)
1830 xmlHashFree((xmlHashTablePtr) style->templatesHash,
1831 (xmlHashDeallocator) xsltFreeCompMatchList);
1832 if (style->rootMatch != NULL)
1833 xsltFreeCompMatchList(style->rootMatch);
1834 if (style->keyMatch != NULL)
1835 xsltFreeCompMatchList(style->keyMatch);
1836 if (style->elemMatch != NULL)
1837 xsltFreeCompMatchList(style->elemMatch);
1838 if (style->attrMatch != NULL)
1839 xsltFreeCompMatchList(style->attrMatch);
1840 if (style->parentMatch != NULL)
1841 xsltFreeCompMatchList(style->parentMatch);
1842 if (style->textMatch != NULL)
1843 xsltFreeCompMatchList(style->textMatch);
1844 if (style->piMatch != NULL)
1845 xsltFreeCompMatchList(style->piMatch);
1846 if (style->commentMatch != NULL)
1847 xsltFreeCompMatchList(style->commentMatch);
1853 * @node: a node in the source tree
1854 * @pattern: an XSLT pattern
1856 * Determine if a node matches a pattern.
1859 xsltMatchPattern(xsltTransformContextPtr context,
1861 const xmlChar *pattern)
1864 xsltCompMatchPtr first, comp;
1866 if ((context != NULL) && (pattern != NULL)) {
1867 first = xsltCompilePattern(pattern);
1868 for (comp = first; comp != NULL; comp = comp->next) {
1869 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
1874 xsltFreeCompMatchList(first);