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) == '-') ||
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 xmlChar *prefix = token;
1216 * This is a namespace match
1218 token = xsltScanName(ctxt);
1219 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1221 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1222 xsltGenericError(xsltGenericErrorContext,
1223 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1228 URL = xmlStrdup(ns->href);
1231 if (token == NULL) {
1234 PUSH(XSLT_OP_NS, URL, NULL);
1236 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1237 xsltGenericError(xsltGenericErrorContext,
1238 "xsltCompileStepPattern : Name expected\n");
1243 PUSH(XSLT_OP_ELEM, token, URL);
1247 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1248 name = xsltScanName(ctxt);
1250 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1251 xsltGenericError(xsltGenericErrorContext,
1252 "xsltCompileStepPattern : QName expected\n");
1256 URI = xsltGetQNameURI(ctxt->elem, &token);
1257 if (token == NULL) {
1261 name = xmlStrdup(token);
1263 URL = xmlStrdup(URI);
1265 PUSH(XSLT_OP_CHILD, name, URL);
1266 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1267 name = xsltScanName(ctxt);
1269 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1270 xsltGenericError(xsltGenericErrorContext,
1271 "xsltCompileStepPattern : QName expected\n");
1275 URI = xsltGetQNameURI(ctxt->elem, &token);
1276 if (token == NULL) {
1280 name = xmlStrdup(token);
1282 URL = xmlStrdup(URI);
1284 PUSH(XSLT_OP_ATTR, name, URL);
1286 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1287 xsltGenericError(xsltGenericErrorContext,
1288 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1294 } else if (CUR == '*') {
1296 PUSH(XSLT_OP_ALL, token, NULL);
1298 URI = xsltGetQNameURI(ctxt->elem, &token);
1299 if (token == NULL) {
1304 URL = xmlStrdup(URI);
1305 PUSH(XSLT_OP_ELEM, token, URL);
1310 while (CUR == '[') {
1312 xmlChar *ret = NULL;
1317 /* TODO: avoid breaking in strings ... */
1318 while (IS_CHAR(CUR)) {
1319 /* Skip over nested predicates */
1329 if (!IS_CHAR(CUR)) {
1330 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1331 xsltGenericError(xsltGenericErrorContext,
1332 "xsltCompileStepPattern : ']' expected\n");
1336 ret = xmlStrndup(q, CUR_PTR - q);
1337 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1338 /* push the predicate lower than local test */
1351 * xsltCompileRelativePathPattern:
1352 * @comp: the compilation context
1353 * @token: a posible precompiled name
1355 * Compile the XSLT RelativePathPattern and generates a precompiled
1356 * form suitable for fast matching.
1358 * [4] RelativePathPattern ::= StepPattern
1359 * | RelativePathPattern '/' StepPattern
1360 * | RelativePathPattern '//' StepPattern
1363 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1364 xsltCompileStepPattern(ctxt, token);
1368 while ((CUR != 0) && (CUR != '|')) {
1369 if ((CUR == '/') && (NXT(1) == '/')) {
1370 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1374 xsltCompileStepPattern(ctxt, NULL);
1375 } else if (CUR == '/') {
1376 PUSH(XSLT_OP_PARENT, NULL, NULL);
1379 if ((CUR != 0) || (CUR == '|')) {
1380 xsltCompileRelativePathPattern(ctxt, NULL);
1394 * xsltCompileLocationPathPattern:
1395 * @ctxt: the compilation context
1397 * Compile the XSLT LocationPathPattern and generates a precompiled
1398 * form suitable for fast matching.
1400 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1401 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1402 * | '//'? RelativePathPattern
1405 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1407 if ((CUR == '/') && (NXT(1) == '/')) {
1409 * since we reverse the query
1410 * a leading // can be safely ignored
1414 xsltCompileRelativePathPattern(ctxt, NULL);
1415 } else if (CUR == '/') {
1417 * We need to find root as the parent
1421 PUSH(XSLT_OP_ROOT, NULL, NULL);
1422 if ((CUR != 0) || (CUR == '|')) {
1423 PUSH(XSLT_OP_PARENT, NULL, NULL);
1424 xsltCompileRelativePathPattern(ctxt, NULL);
1426 } else if (CUR == '*') {
1427 xsltCompileRelativePathPattern(ctxt, NULL);
1428 } else if (CUR == '@') {
1429 xsltCompileRelativePathPattern(ctxt, NULL);
1432 name = xsltScanName(ctxt);
1434 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1435 xsltGenericError(xsltGenericErrorContext,
1436 "xsltCompileLocationPathPattern : Name expected\n");
1441 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1442 xsltCompileIdKeyPattern(ctxt, name, 1);
1443 if ((CUR == '/') && (NXT(1) == '/')) {
1444 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1448 xsltCompileRelativePathPattern(ctxt, NULL);
1449 } else if (CUR == '/') {
1450 PUSH(XSLT_OP_PARENT, NULL, NULL);
1453 xsltCompileRelativePathPattern(ctxt, NULL);
1457 xsltCompileRelativePathPattern(ctxt, name);
1464 * xsltCompilePattern:
1465 * @pattern: an XSLT pattern
1466 * @doc: the containing document
1467 * @node: the containing element
1469 * Compile the XSLT pattern and generates a list of precompiled form suitable
1470 * for fast matching.
1472 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1474 * Returns the generated pattern list or NULL in case of failure
1478 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc, xmlNodePtr node) {
1479 xsltParserContextPtr ctxt = NULL;
1480 xsltCompMatchPtr element, first = NULL, previous = NULL;
1481 int current, start, end;
1483 if (pattern == NULL) {
1484 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1485 xsltGenericError(xsltGenericErrorContext,
1486 "xsltCompilePattern : NULL pattern\n");
1490 ctxt = xsltNewParserContext();
1496 while (pattern[current] != 0) {
1498 while (IS_BLANK(pattern[current]))
1501 while ((pattern[end] != 0) && (pattern[end] != '|'))
1503 if (current == end) {
1504 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1505 xsltGenericError(xsltGenericErrorContext,
1506 "xsltCompilePattern : NULL pattern\n");
1509 element = xsltNewCompMatch();
1510 if (element == NULL) {
1515 else if (previous != NULL)
1516 previous->next = element;
1519 ctxt->comp = element;
1520 ctxt->base = xmlStrndup(&pattern[start], end - start);
1521 if (ctxt->base == NULL)
1523 ctxt->cur = &(ctxt->base)[current - start];
1524 element->pattern = ctxt->base;
1526 #ifdef WITH_XSLT_DEBUG_PATTERN
1527 xsltGenericDebug(xsltGenericDebugContext,
1528 "xsltCompilePattern : parsing '%s'\n",
1531 xsltCompileLocationPathPattern(ctxt);
1536 * Reverse for faster interpretation.
1538 xsltReverseCompMatch(element);
1541 * Set-up the priority
1543 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1544 (element->steps[0].op == XSLT_OP_ATTR)) &&
1545 (element->steps[0].value != NULL) &&
1546 (element->steps[1].op == XSLT_OP_END)) {
1547 element->priority = 0;
1549 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1550 (element->steps[1].op == XSLT_OP_END)) {
1551 element->priority = 0;
1553 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1554 (element->steps[0].value != NULL) &&
1555 (element->steps[1].op == XSLT_OP_END)) {
1556 element->priority = 0;
1557 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1558 (element->steps[0].value2 != NULL) &&
1559 (element->steps[1].op == XSLT_OP_END)) {
1560 element->priority = -0.25;
1561 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1562 (element->steps[0].value != NULL) &&
1563 (element->steps[1].op == XSLT_OP_END)) {
1564 element->priority = -0.25;
1565 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1566 (element->steps[0].value == NULL) &&
1567 (element->steps[0].value2 == NULL) &&
1568 (element->steps[1].op == XSLT_OP_END)) {
1569 element->priority = -0.5;
1570 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1571 (element->steps[0].op == XSLT_OP_TEXT) ||
1572 (element->steps[0].op == XSLT_OP_ALL) ||
1573 (element->steps[0].op == XSLT_OP_NODE) ||
1574 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1575 (element->steps[1].op == XSLT_OP_END)) {
1576 element->priority = -0.5;
1578 element->priority = 0.5;
1580 #ifdef WITH_XSLT_DEBUG_PATTERN
1581 xsltGenericDebug(xsltGenericDebugContext,
1582 "xsltCompilePattern : parsed %s, default priority %f\n",
1583 element->pattern, element->priority);
1585 if (pattern[end] == '|')
1590 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1591 xsltGenericError(xsltGenericErrorContext,
1592 "xsltCompilePattern : NULL pattern\n");
1596 xsltFreeParserContext(ctxt);
1601 xsltFreeParserContext(ctxt);
1603 xsltFreeCompMatchList(first);
1607 /************************************************************************
1609 * Module interfaces *
1611 ************************************************************************/
1615 * @style: an XSLT stylesheet
1616 * @cur: an XSLT template
1617 * @mode: the mode name or NULL
1618 * @modeURI: the mode URI or NULL
1620 * Register the XSLT pattern associated to @cur
1622 * Returns -1 in case of error, 0 otherwise
1625 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1626 const xmlChar *mode, const xmlChar *modeURI) {
1627 xsltCompMatchPtr pat, list, *top = NULL, next;
1628 const xmlChar *name = NULL;
1629 float priority; /* the priority */
1631 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1634 priority = cur->priority;
1635 pat = xsltCompilePattern(cur->match, style->doc, cur->elem);
1641 pat->template = cur;
1643 pat->mode = xmlStrdup(mode);
1644 if (modeURI != NULL)
1645 pat->modeURI = xmlStrdup(modeURI);
1646 if (priority != XSLT_PAT_NO_PRIORITY)
1647 pat->priority = priority;
1650 * insert it in the hash table list corresponding to its lookup name
1652 switch (pat->steps[0].op) {
1654 if (pat->steps[0].value != NULL)
1655 name = pat->steps[0].value;
1657 top = (xsltCompMatchPtr *) &(style->attrMatch);
1661 case XSLT_OP_PARENT:
1662 case XSLT_OP_ANCESTOR:
1663 name = pat->steps[0].value;
1666 top = (xsltCompMatchPtr *) &(style->rootMatch);
1669 top = (xsltCompMatchPtr *) &(style->keyMatch);
1672 /* TODO optimize ID !!! */
1675 top = (xsltCompMatchPtr *) &(style->elemMatch);
1678 case XSLT_OP_PREDICATE:
1679 xsltPrintErrorContext(NULL, style, NULL);
1680 xsltGenericError(xsltGenericErrorContext,
1681 "xsltAddTemplate: invalid compiled pattern\n");
1682 xsltFreeCompMatch(pat);
1685 * TODO: some flags at the top level about type based patterns
1686 * would be faster than inclusion in the hash table.
1689 if (pat->steps[0].value != NULL)
1690 name = pat->steps[0].value;
1692 top = (xsltCompMatchPtr *) &(style->piMatch);
1694 case XSLT_OP_COMMENT:
1695 top = (xsltCompMatchPtr *) &(style->commentMatch);
1698 top = (xsltCompMatchPtr *) &(style->textMatch);
1701 if (pat->steps[0].value != NULL)
1702 name = pat->steps[0].value;
1704 top = (xsltCompMatchPtr *) &(style->elemMatch);
1709 if (style->templatesHash == NULL) {
1710 style->templatesHash = xmlHashCreate(1024);
1711 if (style->templatesHash == NULL) {
1712 xsltFreeCompMatch(pat);
1715 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1717 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1718 name, mode, modeURI);
1720 xmlHashAddEntry3(style->templatesHash, name,
1721 mode, modeURI, pat);
1724 * Note '<=' since one must choose among the matching
1725 * template rules that are left, the one that occurs
1726 * last in the stylesheet
1728 if (list->priority <= pat->priority) {
1730 xmlHashUpdateEntry3(style->templatesHash, name,
1731 mode, modeURI, pat, NULL);
1733 while (list->next != NULL) {
1734 if (list->next->priority <= pat->priority)
1738 pat->next = list->next;
1743 } else if (top != NULL) {
1748 } else if (list->priority <= pat->priority) {
1752 while (list->next != NULL) {
1753 if (list->next->priority <= pat->priority)
1757 pat->next = list->next;
1761 xsltPrintErrorContext(NULL, style, NULL);
1762 xsltGenericError(xsltGenericErrorContext,
1763 "xsltAddTemplate: invalid compiled pattern\n");
1764 xsltFreeCompMatch(pat);
1767 #ifdef WITH_XSLT_DEBUG_PATTERN
1769 xsltGenericDebug(xsltGenericDebugContext,
1770 "added pattern : '%s' mode '%s' priority %f\n",
1771 pat->pattern, pat->mode, pat->priority);
1773 xsltGenericDebug(xsltGenericDebugContext,
1774 "added pattern : '%s' priority %f\n",
1775 pat->pattern, pat->priority);
1785 * @ctxt: a XSLT process context
1786 * @node: the node being processed
1787 * @style: the current style
1789 * Finds the template applying to this node, if @style is non-NULL
1790 * it means one needs to look for the next imported template in scope.
1792 * Returns the xsltTemplatePtr or NULL if not found
1795 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
1796 xsltStylesheetPtr style) {
1797 xsltStylesheetPtr curstyle;
1798 xsltTemplatePtr ret = NULL;
1799 const xmlChar *name = NULL;
1800 xsltCompMatchPtr list = NULL;
1803 if ((ctxt == NULL) || (node == NULL))
1806 if (style == NULL) {
1807 curstyle = ctxt->style;
1809 curstyle = xsltNextImport(style);
1812 while ((curstyle != NULL) && (curstyle != style)) {
1813 priority = XSLT_PAT_NO_PRIORITY;
1814 /* TODO : handle IDs/keys here ! */
1815 if (curstyle->templatesHash != NULL) {
1817 * Use the top name as selector
1819 switch (node->type) {
1820 case XML_ELEMENT_NODE:
1821 case XML_ATTRIBUTE_NODE:
1825 case XML_DOCUMENT_NODE:
1826 case XML_HTML_DOCUMENT_NODE:
1828 case XML_CDATA_SECTION_NODE:
1829 case XML_COMMENT_NODE:
1830 case XML_ENTITY_REF_NODE:
1831 case XML_ENTITY_NODE:
1832 case XML_DOCUMENT_TYPE_NODE:
1833 case XML_DOCUMENT_FRAG_NODE:
1834 case XML_NOTATION_NODE:
1836 case XML_ELEMENT_DECL:
1837 case XML_ATTRIBUTE_DECL:
1838 case XML_ENTITY_DECL:
1839 case XML_NAMESPACE_DECL:
1840 case XML_XINCLUDE_START:
1841 case XML_XINCLUDE_END:
1850 * find the list of appliable expressions based on the name
1852 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
1853 name, ctxt->mode, ctxt->modeURI);
1856 while (list != NULL) {
1857 if (xsltTestCompMatch(ctxt, list, node,
1858 ctxt->mode, ctxt->modeURI)) {
1859 ret = list->template;
1860 priority = list->priority;
1868 * find alternate generic matches
1870 switch (node->type) {
1871 case XML_ELEMENT_NODE:
1872 list = curstyle->elemMatch;
1874 case XML_ATTRIBUTE_NODE:
1875 list = curstyle->attrMatch;
1878 list = curstyle->piMatch;
1880 case XML_DOCUMENT_NODE:
1881 case XML_HTML_DOCUMENT_NODE:
1882 list = curstyle->rootMatch;
1885 case XML_CDATA_SECTION_NODE:
1886 list = curstyle->textMatch;
1888 case XML_COMMENT_NODE:
1889 list = curstyle->commentMatch;
1891 case XML_ENTITY_REF_NODE:
1892 case XML_ENTITY_NODE:
1893 case XML_DOCUMENT_TYPE_NODE:
1894 case XML_DOCUMENT_FRAG_NODE:
1895 case XML_NOTATION_NODE:
1897 case XML_ELEMENT_DECL:
1898 case XML_ATTRIBUTE_DECL:
1899 case XML_ENTITY_DECL:
1900 case XML_NAMESPACE_DECL:
1901 case XML_XINCLUDE_START:
1902 case XML_XINCLUDE_END:
1908 while ((list != NULL) &&
1909 ((ret == NULL) || (list->priority > priority))) {
1910 if (xsltTestCompMatch(ctxt, list, node,
1911 ctxt->mode, ctxt->modeURI)) {
1912 ret = list->template;
1917 if (node->_private != NULL) {
1918 list = curstyle->keyMatch;
1919 while ((list != NULL) &&
1920 ((ret == NULL) || (list->priority > priority))) {
1921 if (xsltTestCompMatch(ctxt, list, node,
1922 ctxt->mode, ctxt->modeURI)) {
1923 ret = list->template;
1933 * Cycle on next curstylesheet import.
1935 curstyle = xsltNextImport(curstyle);
1941 * xsltCleanupTemplates:
1942 * @style: an XSLT stylesheet
1944 * Cleanup the state of the templates used by the stylesheet and
1945 * the ones it imports.
1948 xsltCleanupTemplates(xsltStylesheetPtr style) {
1949 while (style != NULL) {
1950 xmlHashScan((xmlHashTablePtr) style->templatesHash,
1951 (xmlHashScanner) xsltCleanupCompMatch, NULL);
1953 style = xsltNextImport(style);
1958 * xsltFreeTemplateHashes:
1959 * @style: an XSLT stylesheet
1961 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
1964 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
1965 if (style->templatesHash != NULL)
1966 xmlHashFree((xmlHashTablePtr) style->templatesHash,
1967 (xmlHashDeallocator) xsltFreeCompMatchList);
1968 if (style->rootMatch != NULL)
1969 xsltFreeCompMatchList(style->rootMatch);
1970 if (style->keyMatch != NULL)
1971 xsltFreeCompMatchList(style->keyMatch);
1972 if (style->elemMatch != NULL)
1973 xsltFreeCompMatchList(style->elemMatch);
1974 if (style->attrMatch != NULL)
1975 xsltFreeCompMatchList(style->attrMatch);
1976 if (style->parentMatch != NULL)
1977 xsltFreeCompMatchList(style->parentMatch);
1978 if (style->textMatch != NULL)
1979 xsltFreeCompMatchList(style->textMatch);
1980 if (style->piMatch != NULL)
1981 xsltFreeCompMatchList(style->piMatch);
1982 if (style->commentMatch != NULL)
1983 xsltFreeCompMatchList(style->commentMatch);
1989 * @node: a node in the source tree
1990 * @pattern: an XSLT pattern
1992 * Determine if a node matches a pattern.
1995 xsltMatchPattern(xsltTransformContextPtr context,
1997 const xmlChar *pattern)
2000 xsltCompMatchPtr first, comp;
2002 if ((context != NULL) && (pattern != NULL)) {
2003 first = xsltCompilePattern(pattern);
2004 for (comp = first; comp != NULL; comp = comp->next) {
2005 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2010 xsltFreeCompMatchList(first);