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
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 *pattern; /* the pattern */
83 const xmlChar *mode; /* the mode */
84 const xmlChar *modeURI; /* the mode URI */
85 xsltTemplatePtr template; /* the associated template */
87 /* TODO fix the statically allocated size steps[] */
90 xmlNsPtr *nsList; /* the namespaces in scope */
91 int nsNr; /* the number of namespaces in scope */
92 xsltStepOp steps[20]; /* ops for computation */
95 typedef struct _xsltParserContext xsltParserContext;
96 typedef xsltParserContext *xsltParserContextPtr;
97 struct _xsltParserContext {
98 const xmlChar *cur; /* the current char being parsed */
99 const xmlChar *base; /* the full expression */
100 xmlDocPtr doc; /* the source document */
101 xmlNodePtr elem; /* the source element */
102 int error; /* error code */
103 xsltCompMatchPtr comp; /* the result */
106 /************************************************************************
110 ************************************************************************/
115 * Create a new XSLT CompMatch
117 * Returns the newly allocated xsltCompMatchPtr or NULL in case of error
119 static xsltCompMatchPtr
120 xsltNewCompMatch(void) {
121 xsltCompMatchPtr cur;
123 cur = (xsltCompMatchPtr) xmlMalloc(sizeof(xsltCompMatch));
125 xsltPrintErrorContext(NULL, NULL, NULL);
126 xsltGenericError(xsltGenericErrorContext,
127 "xsltNewCompMatch : malloc failed\n");
130 memset(cur, 0, sizeof(xsltCompMatch));
139 * @comp: an XSLT comp
141 * Free up the memory allocated by @comp
144 xsltFreeCompMatch(xsltCompMatchPtr comp) {
150 if (comp->pattern != NULL)
151 xmlFree((xmlChar *)comp->pattern);
152 if (comp->mode != NULL)
153 xmlFree((xmlChar *)comp->mode);
154 if (comp->modeURI != NULL)
155 xmlFree((xmlChar *)comp->modeURI);
156 if (comp->nsList != NULL)
157 xmlFree(comp->nsList);
158 for (i = 0;i < comp->nbStep;i++) {
159 op = &comp->steps[i];
160 if (op->value != NULL)
162 if (op->value2 != NULL)
164 if (op->value3 != NULL)
166 if (op->comp != NULL)
167 xmlXPathFreeCompExpr(op->comp);
169 memset(comp, -1, sizeof(xsltCompMatch));
174 * xsltFreeCompMatchList:
175 * @comp: an XSLT comp list
177 * Free up the memory allocated by all the elements of @comp
180 xsltFreeCompMatchList(xsltCompMatchPtr comp) {
181 xsltCompMatchPtr cur;
183 while (comp != NULL) {
186 xsltFreeCompMatch(cur);
191 * xsltNewParserContext:
193 * Create a new XSLT ParserContext
195 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
197 static xsltParserContextPtr
198 xsltNewParserContext(void) {
199 xsltParserContextPtr cur;
201 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
203 xsltPrintErrorContext(NULL, NULL, NULL);
204 xsltGenericError(xsltGenericErrorContext,
205 "xsltNewParserContext : malloc failed\n");
208 memset(cur, 0, sizeof(xsltParserContext));
213 * xsltFreeParserContext:
214 * @ctxt: an XSLT parser context
216 * Free up the memory allocated by @ctxt
219 xsltFreeParserContext(xsltParserContextPtr ctxt) {
222 memset(ctxt, -1, sizeof(xsltParserContext));
228 * @comp: the compiled match expression
230 * @value: the first value
231 * @value2: the second value
233 * Add an step to an XSLT Compiled Match
235 * Returns -1 in case of failure, 0 otherwise.
238 xsltCompMatchAdd(xsltCompMatchPtr comp, xsltOp op, xmlChar *value,
240 if (comp->nbStep >= 20) {
241 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
242 xsltGenericError(xsltGenericErrorContext,
243 "xsltCompMatchAdd: overflow\n");
246 comp->steps[comp->nbStep].op = op;
247 comp->steps[comp->nbStep].value = value;
248 comp->steps[comp->nbStep].value2 = value2;
254 * xsltSwapTopCompMatch:
255 * @comp: the compiled match expression
257 * reverse the two top steps.
260 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
262 int j = comp->nbStep - 1;
265 register xmlChar *tmp;
268 tmp = comp->steps[i].value;
269 comp->steps[i].value = comp->steps[j].value;
270 comp->steps[j].value = tmp;
271 tmp = comp->steps[i].value2;
272 comp->steps[i].value2 = comp->steps[j].value2;
273 comp->steps[j].value2 = tmp;
274 op = comp->steps[i].op;
275 comp->steps[i].op = comp->steps[j].op;
276 comp->steps[j].op = op;
281 * xsltReverseCompMatch:
282 * @comp: the compiled match expression
284 * reverse all the stack of expressions
287 xsltReverseCompMatch(xsltCompMatchPtr comp) {
289 int j = comp->nbStep - 1;
292 register xmlChar *tmp;
294 tmp = comp->steps[i].value;
295 comp->steps[i].value = comp->steps[j].value;
296 comp->steps[j].value = tmp;
297 tmp = comp->steps[i].value2;
298 comp->steps[i].value2 = comp->steps[j].value2;
299 comp->steps[j].value2 = tmp;
300 op = comp->steps[i].op;
301 comp->steps[i].op = comp->steps[j].op;
302 comp->steps[j].op = op;
306 comp->steps[comp->nbStep++].op = XSLT_OP_END;
310 * xsltCleanupCompMatch:
311 * @comp: the compiled match expression
313 * remove all computation state from the pattern
316 xsltCleanupCompMatch(xsltCompMatchPtr comp) {
319 for (i = 0;i < comp->nbStep;i++) {
320 comp->steps[i].previous = NULL;
324 /************************************************************************
326 * The interpreter for the precompiled patterns *
328 ************************************************************************/
332 * @ctxt: a XSLT process context
333 * @comp: the precompiled pattern
335 * @mode: the mode name or NULL
336 * @modeURI: the mode URI or NULL
338 * Test wether the node matches the pattern
340 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
343 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
344 xmlNodePtr node, const xmlChar *mode,
345 const xmlChar *modeURI) {
347 xsltStepOpPtr step, select = NULL;
349 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
350 xsltPrintErrorContext(ctxt, NULL, node);
351 xsltGenericError(xsltGenericErrorContext,
352 "xsltTestCompMatch: null arg\n");
356 if (comp->mode == NULL)
358 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
361 if (comp->mode != NULL)
364 if (modeURI != NULL) {
365 if (comp->modeURI == NULL)
367 if ((comp->modeURI != modeURI) &&
368 (!xmlStrEqual(comp->modeURI, modeURI)))
371 if (comp->modeURI != NULL)
374 for (i = 0;i < comp->nbStep;i++) {
375 step = &comp->steps[i];
376 if (step->op != XSLT_OP_PREDICATE)
382 if ((node->type == XML_DOCUMENT_NODE) ||
383 (node->type == XML_HTML_DOCUMENT_NODE))
387 if (node->type != XML_ELEMENT_NODE)
389 if (step->value == NULL)
391 if (!xmlStrEqual(step->value, node->name))
395 if (node->ns == NULL) {
396 if (step->value2 != NULL)
398 } else if (node->ns->href != NULL) {
399 if (step->value2 == NULL)
401 if (!xmlStrEqual(step->value2, node->ns->href))
406 TODO /* Handle OP_CHILD */
409 if (node->type != XML_ATTRIBUTE_NODE)
411 if (step->value == NULL)
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))
431 if (step->value == NULL)
433 if (!xmlStrEqual(step->value, node->name))
436 if (node->ns == NULL) {
437 if (step->value2 != NULL)
439 } else if (node->ns->href != NULL) {
440 if (step->value2 == NULL)
442 if (!xmlStrEqual(step->value2, node->ns->href))
446 case XSLT_OP_ANCESTOR:
447 /* TODO: implement coalescing of ANCESTOR/NODE ops */
448 if (step->value == NULL) {
450 step = &comp->steps[i];
451 if (step->op == XSLT_OP_ROOT)
453 if (step->op != XSLT_OP_ELEM)
455 if (step->value == NULL)
461 while (node != NULL) {
464 if (xmlStrEqual(step->value, node->name)) {
466 if (node->ns == NULL) {
467 if (step->value2 == NULL)
469 } else if (node->ns->href != NULL) {
470 if ((step->value2 != NULL) &&
471 (xmlStrEqual(step->value2, node->ns->href)))
481 /* TODO Handle IDs decently, must be done differently */
484 id = xmlGetID(node->doc, step->value);
485 if ((id == NULL) || (id->parent != node))
493 list = xsltGetKey(ctxt, step->value,
494 step->value3, step->value2);
497 for (indx = 0;indx < list->nodeNr;indx++)
498 if (list->nodeTab[indx] == node)
500 if (indx >= list->nodeNr)
506 if (node->ns == NULL) {
507 if (step->value != NULL)
509 } else if (node->ns->href != NULL) {
510 if (step->value == NULL)
512 if (!xmlStrEqual(step->value, node->ns->href))
517 switch (node->type) {
518 case XML_DOCUMENT_NODE:
519 case XML_HTML_DOCUMENT_NODE:
520 case XML_ELEMENT_NODE:
526 case XSLT_OP_PREDICATE: {
529 int pos = 0, len = 0;
531 * Depending on the last selection, one may need to
532 * recompute contextSize and proximityPosition.
534 oldCS = ctxt->xpathCtxt->contextSize;
535 oldCP = ctxt->xpathCtxt->proximityPosition;
536 if ((select != NULL) &&
537 (select->op == XSLT_OP_ELEM) &&
538 (select->value != NULL) &&
539 (node->type == XML_ELEMENT_NODE) &&
540 (node->parent != NULL)) {
542 if ((select->previous != NULL) &&
543 (select->previous->parent == node->parent)) {
545 * just walk back to adjust the index
548 xmlNodePtr sibling = node;
550 while (sibling != NULL) {
551 if (sibling == select->previous)
553 if (xmlStrEqual(node->name, sibling->name)) {
554 if ((select->value2 == NULL) ||
555 ((sibling->ns != NULL) &&
556 (xmlStrEqual(select->value2,
557 sibling->ns->href))))
560 sibling = sibling->prev;
562 if (sibling == NULL) {
563 /* hum going backward in document order ... */
566 while (sibling != NULL) {
567 if (sibling == select->previous)
569 if ((select->value2 == NULL) ||
570 ((sibling->ns != NULL) &&
571 (xmlStrEqual(select->value2,
572 sibling->ns->href))))
574 sibling = sibling->next;
577 if (sibling != NULL) {
578 pos = select->index + indx;
580 select->previous = node;
586 * recompute the index
588 xmlNodePtr siblings = node->parent->children;
590 while (siblings != NULL) {
591 if (siblings->type == XML_ELEMENT_NODE) {
592 if (siblings == node) {
595 } else if (xmlStrEqual(node->name,
597 if ((select->value2 == NULL) ||
598 ((siblings->ns != NULL) &&
599 (xmlStrEqual(select->value2,
600 siblings->ns->href))))
604 siblings = siblings->next;
608 ctxt->xpathCtxt->contextSize = len;
609 ctxt->xpathCtxt->proximityPosition = pos;
610 select->previous = node;
614 } else if ((select != NULL) && (select->op == XSLT_OP_ALL)) {
615 if ((select->previous != NULL) &&
616 (select->previous->parent == node->parent)) {
618 * just walk back to adjust the index
621 xmlNodePtr sibling = node;
623 while (sibling != NULL) {
624 if (sibling == select->previous)
626 if (sibling->type == XML_ELEMENT_NODE)
628 sibling = sibling->prev;
630 if (sibling == NULL) {
631 /* hum going backward in document order ... */
634 while (sibling != NULL) {
635 if (sibling == select->previous)
637 if (sibling->type == XML_ELEMENT_NODE)
639 sibling = sibling->next;
642 if (sibling != NULL) {
643 pos = select->index + indx;
645 select->previous = node;
651 * recompute the index
653 xmlNodePtr siblings = node->parent->children;
655 while (siblings != NULL) {
656 if (siblings->type == XML_ELEMENT_NODE) {
658 if (siblings == node) {
662 siblings = siblings->next;
666 ctxt->xpathCtxt->contextSize = len;
667 ctxt->xpathCtxt->proximityPosition = pos;
668 select->previous = node;
673 oldNode = ctxt->node;
676 if (step->value == NULL)
679 if (step->comp == NULL) {
680 step->comp = xmlXPathCompile(step->value);
681 if (step->comp == NULL)
684 if (comp->nsList == NULL) {
687 comp->nsList = xmlGetNsList(node->doc, node);
688 if (comp->nsList != NULL) {
689 while (comp->nsList[j] != NULL)
694 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
699 ctxt->xpathCtxt->contextSize = oldCS;
700 ctxt->xpathCtxt->proximityPosition = oldCP;
702 ctxt->node = oldNode;
706 ctxt->xpathCtxt->contextSize = oldCS;
707 ctxt->xpathCtxt->proximityPosition = oldCP;
709 ctxt->node = oldNode;
713 if (node->type != XML_PI_NODE)
715 if (step->value != NULL) {
716 if (!xmlStrEqual(step->value, node->name))
720 case XSLT_OP_COMMENT:
721 if (node->type != XML_COMMENT_NODE)
725 if ((node->type != XML_TEXT_NODE) &&
726 (node->type != XML_CDATA_SECTION_NODE))
730 switch (node->type) {
731 case XML_DOCUMENT_NODE:
732 case XML_HTML_DOCUMENT_NODE:
733 case XML_ELEMENT_NODE:
734 case XML_CDATA_SECTION_NODE:
736 case XML_COMMENT_NODE:
738 case XML_ATTRIBUTE_NODE:
750 * xsltTestCompMatchList:
751 * @ctxt: a XSLT process context
753 * @comp: the precompiled pattern list
755 * Test wether the node matches one of the patterns in the list
757 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
760 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
761 xsltCompMatchPtr comp) {
764 if ((ctxt == NULL) || (node == NULL))
766 while (comp != NULL) {
767 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
775 /************************************************************************
777 * Dedicated parser for templates *
779 ************************************************************************/
781 #define CUR (*ctxt->cur)
782 #define SKIP(val) ctxt->cur += (val)
783 #define NXT(val) ctxt->cur[(val)]
784 #define CUR_PTR ctxt->cur
786 #define SKIP_BLANKS \
787 while (IS_BLANK(CUR)) NEXT
789 #define CURRENT (*ctxt->cur)
790 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
793 #define PUSH(op, val, val2) \
794 if (xsltCompMatchAdd(ctxt->comp, (op), (val), (val2))) goto error;
797 xsltSwapTopCompMatch(ctxt->comp);
799 #define XSLT_ERROR(X) \
800 { xsltError(ctxt, __FILE__, __LINE__, X); \
801 ctxt->error = (X); return; }
803 #define XSLT_ERROR0(X) \
804 { xsltError(ctxt, __FILE__, __LINE__, X); \
805 ctxt->error = (X); return(0); }
809 * @ctxt: the XPath Parser context
811 * Parse an XPath Litteral:
813 * [29] Literal ::= '"' [^"]* '"'
816 * Returns the Literal parsed or NULL
820 xsltScanLiteral(xsltParserContextPtr ctxt) {
828 while ((IS_CHAR(CUR)) && (CUR != '"'))
831 /* XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); */
835 ret = xmlStrndup(q, CUR_PTR - q);
838 } else if (CUR == '\'') {
841 while ((IS_CHAR(CUR)) && (CUR != '\''))
844 /* XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); */
848 ret = xmlStrndup(q, CUR_PTR - q);
852 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
861 * @ctxt: the XPath Parser context
863 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
864 * CombiningChar | Extender
866 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
868 * [6] Names ::= Name (S Name)*
870 * Returns the Name parsed or NULL
874 xsltScanName(xsltParserContextPtr ctxt) {
875 xmlChar buf[XML_MAX_NAMELEN];
879 if (!IS_LETTER(CUR) && (CUR != '_') &&
884 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
885 (NXT(len) == '.') || (NXT(len) == '-') ||
886 (NXT(len) == '_') || (NXT(len) == ':') ||
887 (IS_COMBINING(NXT(len))) ||
888 (IS_EXTENDER(NXT(len)))) {
891 if (len >= XML_MAX_NAMELEN) {
892 xmlGenericError(xmlGenericErrorContext,
893 "xsltScanName: reached XML_MAX_NAMELEN limit\n");
894 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
895 (NXT(len) == '.') || (NXT(len) == '-') ||
896 (NXT(len) == '_') || (NXT(len) == ':') ||
897 (IS_COMBINING(NXT(len))) ||
898 (IS_EXTENDER(NXT(len))))
904 return(xmlStrndup(buf, len));
909 * @ctxt: the XPath Parser context
911 * Parses a non qualified name
913 * Returns the Name parsed or NULL
917 xsltScanNCName(xsltParserContextPtr ctxt) {
918 xmlChar buf[XML_MAX_NAMELEN];
922 if (!IS_LETTER(CUR) && (CUR != '_')) {
926 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
927 (NXT(len) == '.') || (NXT(len) == '-') ||
929 (IS_COMBINING(NXT(len))) ||
930 (IS_EXTENDER(NXT(len)))) {
933 if (len >= XML_MAX_NAMELEN) {
934 xmlGenericError(xmlGenericErrorContext,
935 "xsltScanNCName: reached XML_MAX_NAMELEN limit\n");
936 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
937 (NXT(len) == '.') || (NXT(len) == '-') ||
939 (IS_COMBINING(NXT(len))) ||
940 (IS_EXTENDER(NXT(len))))
946 return(xmlStrndup(buf, len));
951 * @ctxt: the XPath Parser context
952 * @prefix: the place to store the prefix
954 * Parse a qualified name
956 * Returns the Name parsed or NULL
960 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
964 ret = xsltScanNCName(ctxt);
968 ret = xsltScanNCName(ctxt);
974 * xsltCompileIdKeyPattern:
975 * @ctxt: the compilation context
976 * @name: a preparsed name
977 * @aid: whether id/key are allowed there
979 * Compile the XSLT LocationIdKeyPattern
980 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
981 * | 'key' '(' Literal ',' Literal ')'
983 * also handle NodeType and PI from:
985 * [7] NodeTest ::= NameTest
987 * | 'processing-instruction' '(' Literal ')'
990 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
992 xmlChar *lit2 = NULL;
995 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
996 xsltGenericError(xsltGenericErrorContext,
997 "xsltCompileIdKeyPattern : ( expected\n");
1001 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1004 lit = xsltScanLiteral(ctxt);
1009 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1010 xsltGenericError(xsltGenericErrorContext,
1011 "xsltCompileIdKeyPattern : ) expected\n");
1016 PUSH(XSLT_OP_ID, lit, NULL);
1017 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1020 lit = xsltScanLiteral(ctxt);
1025 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1026 xsltGenericError(xsltGenericErrorContext,
1027 "xsltCompileIdKeyPattern : , expected\n");
1033 lit2 = xsltScanLiteral(ctxt);
1038 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1039 xsltGenericError(xsltGenericErrorContext,
1040 "xsltCompileIdKeyPattern : ) expected\n");
1045 /* TODO: support namespace in keys */
1046 PUSH(XSLT_OP_KEY, lit, lit2);
1047 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1051 lit = xsltScanLiteral(ctxt);
1056 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1057 xsltGenericError(xsltGenericErrorContext,
1058 "xsltCompileIdKeyPattern : ) expected\n");
1064 PUSH(XSLT_OP_PI, lit, NULL);
1065 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1069 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1070 xsltGenericError(xsltGenericErrorContext,
1071 "xsltCompileIdKeyPattern : ) expected\n");
1076 PUSH(XSLT_OP_TEXT, NULL, NULL);
1077 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1081 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1082 xsltGenericError(xsltGenericErrorContext,
1083 "xsltCompileIdKeyPattern : ) expected\n");
1088 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1089 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1093 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1094 xsltGenericError(xsltGenericErrorContext,
1095 "xsltCompileIdKeyPattern : ) expected\n");
1100 PUSH(XSLT_OP_NODE, NULL, NULL);
1102 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1103 xsltGenericError(xsltGenericErrorContext,
1104 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1108 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1109 xsltGenericError(xsltGenericErrorContext,
1110 "xsltCompileIdKeyPattern : node type\n");
1120 * xsltCompileStepPattern:
1121 * @ctxt: the compilation context
1122 * @token: a posible precompiled name
1124 * Compile the XSLT StepPattern and generates a precompiled
1125 * form suitable for fast matching.
1127 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1128 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1129 * | ('child' | 'attribute') '::'
1131 * [7] NodeTest ::= NameTest
1132 * | NodeType '(' ')'
1133 * | 'processing-instruction' '(' Literal ')'
1134 * [8] Predicate ::= '[' PredicateExpr ']'
1135 * [9] PredicateExpr ::= Expr
1136 * [13] AbbreviatedAxisSpecifier ::= '@'?
1137 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1141 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1142 xmlChar *name = NULL;
1143 const xmlChar *URI = NULL;
1144 xmlChar *URL = NULL;
1148 if ((token == NULL) && (CUR == '@')) {
1149 xmlChar *prefix = NULL;
1154 PUSH(XSLT_OP_ATTR, NULL, NULL);
1157 token = xsltScanQName(ctxt, &prefix);
1158 if (prefix != NULL) {
1161 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1163 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1164 xsltGenericError(xsltGenericErrorContext,
1165 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1168 URL = xmlStrdup(ns->href);
1172 if (token == NULL) {
1175 PUSH(XSLT_OP_ATTR, NULL, URL);
1178 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1179 xsltGenericError(xsltGenericErrorContext,
1180 "xsltCompileStepPattern : Name expected\n");
1184 PUSH(XSLT_OP_ATTR, token, URL);
1188 token = xsltScanName(ctxt);
1189 if (token == NULL) {
1192 PUSH(XSLT_OP_ALL, token, NULL);
1193 goto parse_predicate;
1195 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1196 xsltGenericError(xsltGenericErrorContext,
1197 "xsltCompileStepPattern : Name expected\n");
1206 xsltCompileIdKeyPattern(ctxt, token, 0);
1209 } else if (CUR == ':') {
1211 if (NXT(1) != ':') {
1212 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1213 xsltGenericError(xsltGenericErrorContext,
1214 "xsltCompileStepPattern : sequence '::' expected\n");
1219 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1220 name = xsltScanName(ctxt);
1222 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1223 xsltGenericError(xsltGenericErrorContext,
1224 "xsltCompileStepPattern : QName expected\n");
1228 URI = xsltGetQNameURI(ctxt->elem, &token);
1229 if (token == NULL) {
1233 name = xmlStrdup(token);
1235 URL = xmlStrdup(URI);
1237 PUSH(XSLT_OP_CHILD, name, URL);
1238 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1239 name = xsltScanName(ctxt);
1241 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1242 xsltGenericError(xsltGenericErrorContext,
1243 "xsltCompileStepPattern : QName expected\n");
1247 URI = xsltGetQNameURI(ctxt->elem, &token);
1248 if (token == NULL) {
1252 name = xmlStrdup(token);
1254 URL = xmlStrdup(URI);
1256 PUSH(XSLT_OP_ATTR, name, URL);
1258 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1259 xsltGenericError(xsltGenericErrorContext,
1260 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1265 } else if (CUR == '*') {
1267 PUSH(XSLT_OP_ALL, token, NULL);
1269 URI = xsltGetQNameURI(ctxt->elem, &token);
1270 if (token == NULL) {
1275 URL = xmlStrdup(URI);
1276 PUSH(XSLT_OP_ELEM, token, URL);
1281 while (CUR == '[') {
1283 xmlChar *ret = NULL;
1288 /* TODO: avoid breaking in strings ... */
1289 while (IS_CHAR(CUR)) {
1290 /* Skip over nested predicates */
1300 if (!IS_CHAR(CUR)) {
1301 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1302 xsltGenericError(xsltGenericErrorContext,
1303 "xsltCompileStepPattern : ']' expected\n");
1307 ret = xmlStrndup(q, CUR_PTR - q);
1308 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1309 /* push the predicate lower than local test */
1322 * xsltCompileRelativePathPattern:
1323 * @comp: the compilation context
1324 * @token: a posible precompiled name
1326 * Compile the XSLT RelativePathPattern and generates a precompiled
1327 * form suitable for fast matching.
1329 * [4] RelativePathPattern ::= StepPattern
1330 * | RelativePathPattern '/' StepPattern
1331 * | RelativePathPattern '//' StepPattern
1334 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1335 xsltCompileStepPattern(ctxt, token);
1339 while ((CUR != 0) && (CUR != '|')) {
1340 if ((CUR == '/') && (NXT(1) == '/')) {
1341 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1345 xsltCompileStepPattern(ctxt, NULL);
1346 } else if (CUR == '/') {
1347 PUSH(XSLT_OP_PARENT, NULL, NULL);
1350 if ((CUR != 0) || (CUR == '|')) {
1351 xsltCompileRelativePathPattern(ctxt, NULL);
1365 * xsltCompileLocationPathPattern:
1366 * @ctxt: the compilation context
1368 * Compile the XSLT LocationPathPattern and generates a precompiled
1369 * form suitable for fast matching.
1371 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1372 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1373 * | '//'? RelativePathPattern
1376 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1378 if ((CUR == '/') && (NXT(1) == '/')) {
1380 * since we reverse the query
1381 * a leading // can be safely ignored
1385 xsltCompileRelativePathPattern(ctxt, NULL);
1386 } else if (CUR == '/') {
1388 * We need to find root as the parent
1392 PUSH(XSLT_OP_ROOT, NULL, NULL);
1393 if ((CUR != 0) || (CUR == '|')) {
1394 PUSH(XSLT_OP_PARENT, NULL, NULL);
1395 xsltCompileRelativePathPattern(ctxt, NULL);
1397 } else if (CUR == '*') {
1398 xsltCompileRelativePathPattern(ctxt, NULL);
1399 } else if (CUR == '@') {
1400 xsltCompileRelativePathPattern(ctxt, NULL);
1403 name = xsltScanName(ctxt);
1405 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1406 xsltGenericError(xsltGenericErrorContext,
1407 "xsltCompileLocationPathPattern : Name expected\n");
1412 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1413 xsltCompileIdKeyPattern(ctxt, name, 1);
1414 if ((CUR == '/') && (NXT(1) == '/')) {
1415 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1419 xsltCompileRelativePathPattern(ctxt, NULL);
1420 } else if (CUR == '/') {
1421 PUSH(XSLT_OP_PARENT, NULL, NULL);
1424 xsltCompileRelativePathPattern(ctxt, NULL);
1428 xsltCompileRelativePathPattern(ctxt, name);
1435 * xsltCompilePattern:
1436 * @pattern: an XSLT pattern
1437 * @doc: the containing document
1438 * @node: the containing element
1440 * Compile the XSLT pattern and generates a list of precompiled form suitable
1441 * for fast matching.
1443 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1445 * Returns the generated pattern list or NULL in case of failure
1449 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc, xmlNodePtr node) {
1450 xsltParserContextPtr ctxt = NULL;
1451 xsltCompMatchPtr element, first = NULL, previous = NULL;
1452 int current, start, end;
1454 if (pattern == NULL) {
1455 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1456 xsltGenericError(xsltGenericErrorContext,
1457 "xsltCompilePattern : NULL pattern\n");
1461 ctxt = xsltNewParserContext();
1467 while (pattern[current] != 0) {
1469 while (IS_BLANK(pattern[current]))
1472 while ((pattern[end] != 0) && (pattern[end] != '|'))
1474 if (current == end) {
1475 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1476 xsltGenericError(xsltGenericErrorContext,
1477 "xsltCompilePattern : NULL pattern\n");
1480 element = xsltNewCompMatch();
1481 if (element == NULL) {
1486 else if (previous != NULL)
1487 previous->next = element;
1490 ctxt->comp = element;
1491 ctxt->base = xmlStrndup(&pattern[start], end - start);
1492 if (ctxt->base == NULL)
1494 ctxt->cur = &(ctxt->base)[current - start];
1495 element->pattern = ctxt->base;
1497 #ifdef WITH_XSLT_DEBUG_PATTERN
1498 xsltGenericDebug(xsltGenericDebugContext,
1499 "xsltCompilePattern : parsing '%s'\n",
1502 xsltCompileLocationPathPattern(ctxt);
1507 * Reverse for faster interpretation.
1509 xsltReverseCompMatch(element);
1512 * Set-up the priority
1514 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1515 (element->steps[0].op == XSLT_OP_ATTR)) &&
1516 (element->steps[0].value != NULL) &&
1517 (element->steps[1].op == XSLT_OP_END)) {
1518 element->priority = 0;
1520 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1521 (element->steps[1].op == XSLT_OP_END)) {
1522 element->priority = 0;
1524 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1525 (element->steps[0].value != NULL) &&
1526 (element->steps[1].op == XSLT_OP_END)) {
1527 element->priority = 0;
1528 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1529 (element->steps[0].value2 != NULL) &&
1530 (element->steps[1].op == XSLT_OP_END)) {
1531 element->priority = -0.25;
1532 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1533 (element->steps[0].value != NULL) &&
1534 (element->steps[1].op == XSLT_OP_END)) {
1535 element->priority = -0.25;
1536 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1537 (element->steps[0].value == NULL) &&
1538 (element->steps[0].value2 == NULL) &&
1539 (element->steps[1].op == XSLT_OP_END)) {
1540 element->priority = -0.5;
1541 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1542 (element->steps[0].op == XSLT_OP_TEXT) ||
1543 (element->steps[0].op == XSLT_OP_ALL) ||
1544 (element->steps[0].op == XSLT_OP_NODE) ||
1545 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1546 (element->steps[1].op == XSLT_OP_END)) {
1547 element->priority = -0.5;
1549 element->priority = 0.5;
1551 #ifdef WITH_XSLT_DEBUG_PATTERN
1552 xsltGenericDebug(xsltGenericDebugContext,
1553 "xsltCompilePattern : parsed %s, default priority %f\n",
1554 element->pattern, element->priority);
1556 if (pattern[end] == '|')
1561 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1562 xsltGenericError(xsltGenericErrorContext,
1563 "xsltCompilePattern : NULL pattern\n");
1567 xsltFreeParserContext(ctxt);
1572 xsltFreeParserContext(ctxt);
1574 xsltFreeCompMatchList(first);
1578 /************************************************************************
1580 * Module interfaces *
1582 ************************************************************************/
1586 * @style: an XSLT stylesheet
1587 * @cur: an XSLT template
1588 * @mode: the mode name or NULL
1589 * @modeURI: the mode URI or NULL
1591 * Register the XSLT pattern associated to @cur
1593 * Returns -1 in case of error, 0 otherwise
1596 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1597 const xmlChar *mode, const xmlChar *modeURI) {
1598 xsltCompMatchPtr pat, list, *top = NULL, next;
1599 const xmlChar *name = NULL;
1600 float priority; /* the priority */
1602 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1605 priority = cur->priority;
1606 pat = xsltCompilePattern(cur->match, style->doc, cur->elem);
1612 pat->template = cur;
1614 pat->mode = xmlStrdup(mode);
1615 if (modeURI != NULL)
1616 pat->modeURI = xmlStrdup(modeURI);
1617 if (priority != XSLT_PAT_NO_PRIORITY)
1618 pat->priority = priority;
1621 * insert it in the hash table list corresponding to its lookup name
1623 switch (pat->steps[0].op) {
1625 if (pat->steps[0].value != NULL)
1626 name = pat->steps[0].value;
1628 top = (xsltCompMatchPtr *) &(style->attrMatch);
1632 case XSLT_OP_PARENT:
1633 case XSLT_OP_ANCESTOR:
1635 name = pat->steps[0].value;
1638 top = (xsltCompMatchPtr *) &(style->rootMatch);
1641 top = (xsltCompMatchPtr *) &(style->keyMatch);
1644 /* TODO optimize ID !!! */
1646 top = (xsltCompMatchPtr *) &(style->elemMatch);
1649 case XSLT_OP_PREDICATE:
1650 xsltPrintErrorContext(NULL, style, NULL);
1651 xsltGenericError(xsltGenericErrorContext,
1652 "xsltAddTemplate: invalid compiled pattern\n");
1653 xsltFreeCompMatch(pat);
1656 * TODO: some flags at the top level about type based patterns
1657 * would be faster than inclusion in the hash table.
1660 if (pat->steps[0].value != NULL)
1661 name = pat->steps[0].value;
1663 top = (xsltCompMatchPtr *) &(style->piMatch);
1665 case XSLT_OP_COMMENT:
1666 top = (xsltCompMatchPtr *) &(style->commentMatch);
1669 top = (xsltCompMatchPtr *) &(style->textMatch);
1672 if (pat->steps[0].value != NULL)
1673 name = pat->steps[0].value;
1675 top = (xsltCompMatchPtr *) &(style->elemMatch);
1680 if (style->templatesHash == NULL) {
1681 style->templatesHash = xmlHashCreate(1024);
1682 if (style->templatesHash == NULL) {
1683 xsltFreeCompMatch(pat);
1686 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1688 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1689 name, mode, modeURI);
1691 xmlHashAddEntry3(style->templatesHash, name,
1692 mode, modeURI, pat);
1695 * Note '<=' since one must choose among the matching
1696 * template rules that are left, the one that occurs
1697 * last in the stylesheet
1699 if (list->priority <= pat->priority) {
1701 xmlHashUpdateEntry3(style->templatesHash, name,
1702 mode, modeURI, pat, NULL);
1704 while (list->next != NULL) {
1705 if (list->next->priority <= pat->priority)
1709 pat->next = list->next;
1714 } else if (top != NULL) {
1719 } else if (list->priority <= pat->priority) {
1723 while (list->next != NULL) {
1724 if (list->next->priority <= pat->priority)
1728 pat->next = list->next;
1732 xsltPrintErrorContext(NULL, style, NULL);
1733 xsltGenericError(xsltGenericErrorContext,
1734 "xsltAddTemplate: invalid compiled pattern\n");
1735 xsltFreeCompMatch(pat);
1738 #ifdef WITH_XSLT_DEBUG_PATTERN
1740 xsltGenericDebug(xsltGenericDebugContext,
1741 "added pattern : '%s' mode '%s' priority %f\n",
1742 pat->pattern, pat->mode, pat->priority);
1744 xsltGenericDebug(xsltGenericDebugContext,
1745 "added pattern : '%s' priority %f\n",
1746 pat->pattern, pat->priority);
1756 * @ctxt: a XSLT process context
1757 * @node: the node being processed
1758 * @style: the current style
1760 * Finds the template applying to this node, if @style is non-NULL
1761 * it means one needs to look for the next imported template in scope.
1763 * Returns the xsltTemplatePtr or NULL if not found
1766 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
1767 xsltStylesheetPtr style) {
1768 xsltStylesheetPtr curstyle;
1769 xsltTemplatePtr ret = NULL;
1770 const xmlChar *name = NULL;
1771 xsltCompMatchPtr list = NULL;
1774 if ((ctxt == NULL) || (node == NULL))
1777 if (style == NULL) {
1778 curstyle = ctxt->style;
1780 curstyle = xsltNextImport(style);
1783 while ((curstyle != NULL) && (curstyle != style)) {
1784 priority = XSLT_PAT_NO_PRIORITY;
1785 /* TODO : handle IDs/keys here ! */
1786 if (curstyle->templatesHash != NULL) {
1788 * Use the top name as selector
1790 switch (node->type) {
1791 case XML_ELEMENT_NODE:
1792 case XML_ATTRIBUTE_NODE:
1796 case XML_DOCUMENT_NODE:
1797 case XML_HTML_DOCUMENT_NODE:
1799 case XML_CDATA_SECTION_NODE:
1800 case XML_COMMENT_NODE:
1801 case XML_ENTITY_REF_NODE:
1802 case XML_ENTITY_NODE:
1803 case XML_DOCUMENT_TYPE_NODE:
1804 case XML_DOCUMENT_FRAG_NODE:
1805 case XML_NOTATION_NODE:
1807 case XML_ELEMENT_DECL:
1808 case XML_ATTRIBUTE_DECL:
1809 case XML_ENTITY_DECL:
1810 case XML_NAMESPACE_DECL:
1811 case XML_XINCLUDE_START:
1812 case XML_XINCLUDE_END:
1821 * find the list of appliable expressions based on the name
1823 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
1824 name, ctxt->mode, ctxt->modeURI);
1827 while (list != NULL) {
1828 if (xsltTestCompMatch(ctxt, list, node,
1829 ctxt->mode, ctxt->modeURI)) {
1830 ret = list->template;
1831 priority = list->priority;
1839 * find alternate generic matches
1841 switch (node->type) {
1842 case XML_ELEMENT_NODE:
1843 list = curstyle->elemMatch;
1845 case XML_ATTRIBUTE_NODE:
1846 list = curstyle->attrMatch;
1849 list = curstyle->piMatch;
1851 case XML_DOCUMENT_NODE:
1852 case XML_HTML_DOCUMENT_NODE:
1853 list = curstyle->rootMatch;
1856 case XML_CDATA_SECTION_NODE:
1857 list = curstyle->textMatch;
1859 case XML_COMMENT_NODE:
1860 list = curstyle->commentMatch;
1862 case XML_ENTITY_REF_NODE:
1863 case XML_ENTITY_NODE:
1864 case XML_DOCUMENT_TYPE_NODE:
1865 case XML_DOCUMENT_FRAG_NODE:
1866 case XML_NOTATION_NODE:
1868 case XML_ELEMENT_DECL:
1869 case XML_ATTRIBUTE_DECL:
1870 case XML_ENTITY_DECL:
1871 case XML_NAMESPACE_DECL:
1872 case XML_XINCLUDE_START:
1873 case XML_XINCLUDE_END:
1879 while ((list != NULL) &&
1880 ((ret == NULL) || (list->priority > priority))) {
1881 if (xsltTestCompMatch(ctxt, list, node,
1882 ctxt->mode, ctxt->modeURI)) {
1883 ret = list->template;
1888 if (node->_private != NULL) {
1889 list = curstyle->keyMatch;
1890 while ((list != NULL) &&
1891 ((ret == NULL) || (list->priority > priority))) {
1892 if (xsltTestCompMatch(ctxt, list, node,
1893 ctxt->mode, ctxt->modeURI)) {
1894 ret = list->template;
1904 * Cycle on next curstylesheet import.
1906 curstyle = xsltNextImport(curstyle);
1912 * xsltCleanupTemplates:
1913 * @style: an XSLT stylesheet
1915 * Cleanup the state of the templates used by the stylesheet and
1916 * the ones it imports.
1919 xsltCleanupTemplates(xsltStylesheetPtr style) {
1920 while (style != NULL) {
1921 xmlHashScan((xmlHashTablePtr) style->templatesHash,
1922 (xmlHashScanner) xsltCleanupCompMatch, NULL);
1924 style = xsltNextImport(style);
1929 * xsltFreeTemplateHashes:
1930 * @style: an XSLT stylesheet
1932 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
1935 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
1936 if (style->templatesHash != NULL)
1937 xmlHashFree((xmlHashTablePtr) style->templatesHash,
1938 (xmlHashDeallocator) xsltFreeCompMatchList);
1939 if (style->rootMatch != NULL)
1940 xsltFreeCompMatchList(style->rootMatch);
1941 if (style->keyMatch != NULL)
1942 xsltFreeCompMatchList(style->keyMatch);
1943 if (style->elemMatch != NULL)
1944 xsltFreeCompMatchList(style->elemMatch);
1945 if (style->attrMatch != NULL)
1946 xsltFreeCompMatchList(style->attrMatch);
1947 if (style->parentMatch != NULL)
1948 xsltFreeCompMatchList(style->parentMatch);
1949 if (style->textMatch != NULL)
1950 xsltFreeCompMatchList(style->textMatch);
1951 if (style->piMatch != NULL)
1952 xsltFreeCompMatchList(style->piMatch);
1953 if (style->commentMatch != NULL)
1954 xsltFreeCompMatchList(style->commentMatch);
1960 * @node: a node in the source tree
1961 * @pattern: an XSLT pattern
1963 * Determine if a node matches a pattern.
1966 xsltMatchPattern(xsltTransformContextPtr context,
1968 const xmlChar *pattern)
1971 xsltCompMatchPtr first, comp;
1973 if ((context != NULL) && (pattern != NULL)) {
1974 first = xsltCompilePattern(pattern);
1975 for (comp = first; comp != NULL; comp = comp->next) {
1976 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
1981 xsltFreeCompMatchList(first);