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"
61 typedef struct _xsltStepOp xsltStepOp;
62 typedef xsltStepOp *xsltStepOpPtr;
69 * Optimisations for count
76 struct _xsltCompMatch {
77 struct _xsltCompMatch *next; /* siblings in the name hash */
78 float priority; /* the priority */
79 const xmlChar *mode; /* the mode */
80 const xmlChar *modeURI; /* the mode URI */
81 xsltTemplatePtr template; /* the associated template */
83 /* TODO fix the statically allocated size steps[] */
86 xsltStepOp steps[20]; /* ops for computation */
89 typedef struct _xsltParserContext xsltParserContext;
90 typedef xsltParserContext *xsltParserContextPtr;
91 struct _xsltParserContext {
92 const xmlChar *cur; /* the current char being parsed */
93 const xmlChar *base; /* the full expression */
94 xmlDocPtr doc; /* the source document */
95 xmlNodePtr elem; /* the source element */
96 int error; /* error code */
97 xsltCompMatchPtr comp; /* the result */
100 /************************************************************************
104 ************************************************************************/
109 * Create a new XSLT CompMatch
111 * Returns the newly allocated xsltCompMatchPtr or NULL in case of error
114 xsltNewCompMatch(void) {
115 xsltCompMatchPtr cur;
117 cur = (xsltCompMatchPtr) xmlMalloc(sizeof(xsltCompMatch));
119 xsltGenericError(xsltGenericErrorContext,
120 "xsltNewCompMatch : malloc failed\n");
123 memset(cur, 0, sizeof(xsltCompMatch));
130 * @comp: an XSLT comp
132 * Free up the memory allocated by @comp
135 xsltFreeCompMatch(xsltCompMatchPtr comp) {
141 if (comp->mode != NULL)
142 xmlFree((xmlChar *)comp->mode);
143 if (comp->modeURI != NULL)
144 xmlFree((xmlChar *)comp->modeURI);
145 for (i = 0;i < comp->nbStep;i++) {
146 op = &comp->steps[i];
147 if (op->value != NULL)
149 if (op->value2 != NULL)
151 if (op->value3 != NULL)
154 memset(comp, -1, sizeof(xsltCompMatch));
159 * xsltFreeCompMatchList:
160 * @comp: an XSLT comp list
162 * Free up the memory allocated by all the elements of @comp
165 xsltFreeCompMatchList(xsltCompMatchPtr comp) {
166 xsltCompMatchPtr cur;
168 while (comp != NULL) {
171 xsltFreeCompMatch(cur);
176 * xsltNewParserContext:
178 * Create a new XSLT ParserContext
180 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
183 xsltNewParserContext(void) {
184 xsltParserContextPtr cur;
186 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
188 xsltGenericError(xsltGenericErrorContext,
189 "xsltNewParserContext : malloc failed\n");
192 memset(cur, 0, sizeof(xsltParserContext));
197 * xsltFreeParserContext:
198 * @ctxt: an XSLT parser context
200 * Free up the memory allocated by @ctxt
203 xsltFreeParserContext(xsltParserContextPtr ctxt) {
206 memset(ctxt, -1, sizeof(xsltParserContext));
212 * @comp: the compiled match expression
214 * @value: the first value
215 * @value2: the second value
217 * Add an step to an XSLT Compiled Match
219 * Returns -1 in case of failure, 0 otherwise.
222 xsltCompMatchAdd(xsltCompMatchPtr comp, xsltOp op, xmlChar *value,
224 if (comp->nbStep >= 20) {
225 xsltGenericError(xsltGenericErrorContext,
226 "xsltCompMatchAddOp: overflow\n");
229 comp->steps[comp->nbStep].op = op;
230 comp->steps[comp->nbStep].value = value;
231 comp->steps[comp->nbStep].value2 = value2;
237 * xsltSwapTopCompMatch:
238 * @comp: the compiled match expression
240 * reverse the two top steps.
243 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
245 int j = comp->nbStep - 1;
248 register xmlChar *tmp;
251 tmp = comp->steps[i].value;
252 comp->steps[i].value = comp->steps[j].value;
253 comp->steps[j].value = tmp;
254 tmp = comp->steps[i].value2;
255 comp->steps[i].value2 = comp->steps[j].value2;
256 comp->steps[j].value2 = tmp;
257 op = comp->steps[i].op;
258 comp->steps[i].op = comp->steps[j].op;
259 comp->steps[j].op = op;
264 * xsltReverseCompMatch:
265 * @comp: the compiled match expression
267 * reverse all the stack of expressions
270 xsltReverseCompMatch(xsltCompMatchPtr comp) {
272 int j = comp->nbStep - 1;
275 register xmlChar *tmp;
277 tmp = comp->steps[i].value;
278 comp->steps[i].value = comp->steps[j].value;
279 comp->steps[j].value = tmp;
280 tmp = comp->steps[i].value2;
281 comp->steps[i].value2 = comp->steps[j].value2;
282 comp->steps[j].value2 = tmp;
283 op = comp->steps[i].op;
284 comp->steps[i].op = comp->steps[j].op;
285 comp->steps[j].op = op;
289 comp->steps[comp->nbStep++].op = XSLT_OP_END;
293 * xsltCleanupCompMatch:
294 * @comp: the compiled match expression
296 * remove all computation state from the pattern
299 xsltCleanupCompMatch(xsltCompMatchPtr comp) {
302 for (i = 0;i < comp->nbStep;i++) {
303 comp->steps[i].previous = NULL;
307 /************************************************************************
309 * The interpreter for the precompiled patterns *
311 ************************************************************************/
315 * @ctxt: a XSLT process context
316 * @comp: the precompiled pattern
318 * @mode: the mode name or NULL
319 * @modeURI: the mode URI or NULL
321 * Test wether the node matches the pattern
323 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
326 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
327 xmlNodePtr node, const xmlChar *mode,
328 const xmlChar *modeURI) {
330 xsltStepOpPtr step, select = NULL;
332 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
333 xsltGenericError(xsltGenericErrorContext,
334 "xsltTestCompMatch: null arg\n");
338 if (comp->mode == NULL)
340 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
343 if (comp->mode != NULL)
346 if (modeURI != NULL) {
347 if (comp->modeURI == NULL)
349 if ((comp->modeURI != modeURI) &&
350 (!xmlStrEqual(comp->modeURI, modeURI)))
353 if (comp->modeURI != NULL)
356 for (i = 0;i < comp->nbStep;i++) {
357 step = &comp->steps[i];
358 if (step->op != XSLT_OP_PREDICATE)
364 if ((node->type == XML_DOCUMENT_NODE) ||
365 (node->type == XML_HTML_DOCUMENT_NODE))
369 if (node->type != XML_ELEMENT_NODE)
371 if (step->value == NULL)
373 if (!xmlStrEqual(step->value, node->name))
377 if (node->ns == NULL) {
378 if (step->value2 != NULL)
380 } else if (node->ns->href != NULL) {
381 if (step->value2 == NULL)
383 if (!xmlStrEqual(step->value2, node->ns->href))
388 TODO /* Handle OP_CHILD */
391 if (node->type != XML_ATTRIBUTE_NODE)
393 if (step->value == NULL)
395 if (!xmlStrEqual(step->value, node->name))
399 if (node->ns == NULL) {
400 if (step->value2 != NULL)
402 } else if (node->ns->href != NULL) {
403 if (step->value2 == NULL)
405 if (!xmlStrEqual(step->value2, node->ns->href))
413 if (step->value == NULL)
415 if (!xmlStrEqual(step->value, node->name))
418 if (node->ns == NULL) {
419 if (step->value2 != NULL)
421 } else if (node->ns->href != NULL) {
422 if (step->value2 == NULL)
424 if (!xmlStrEqual(step->value2, node->ns->href))
428 case XSLT_OP_ANCESTOR:
429 /* TODO: implement coalescing of ANCESTOR/NODE ops */
430 if (step->value == NULL) {
432 step = &comp->steps[i];
433 if (step->op == XSLT_OP_ROOT)
435 if (step->op != XSLT_OP_ELEM)
437 if (step->value == NULL)
443 while (node != NULL) {
446 if (xmlStrEqual(step->value, node->name)) {
448 if (node->ns == NULL) {
449 if (step->value2 == NULL)
451 } else if (node->ns->href != NULL) {
452 if ((step->value2 != NULL) &&
453 (xmlStrEqual(step->value2, node->ns->href)))
463 /* TODO Handle IDs decently, must be done differently */
466 id = xmlGetID(node->doc, step->value);
467 if ((id == NULL) || (id->parent != node))
475 list = xsltGetKey(ctxt, step->value,
476 step->value3, step->value2);
479 for (i = 0;i < list->nodeNr;i++)
480 if (list->nodeTab[i] == node)
482 if (i >= list->nodeNr)
488 if (node->ns == NULL) {
489 if (step->value != NULL)
491 } else if (node->ns->href != NULL) {
492 if (step->value == NULL)
494 if (!xmlStrEqual(step->value, node->ns->href))
499 switch (node->type) {
500 case XML_DOCUMENT_NODE:
501 case XML_HTML_DOCUMENT_NODE:
502 case XML_ELEMENT_NODE:
508 case XSLT_OP_PREDICATE: {
511 int pos = 0, len = 0;
513 * Depending on the last selection, one may need to
514 * recompute contextSize and proximityPosition.
516 oldCS = ctxt->xpathCtxt->contextSize;
517 oldCP = ctxt->xpathCtxt->proximityPosition;
518 if ((select != NULL) &&
519 (select->op == XSLT_OP_ELEM) &&
520 (select->value != NULL) &&
521 (node->type == XML_ELEMENT_NODE) &&
522 (node->parent != NULL)) {
524 if ((select->previous != NULL) &&
525 (select->previous->parent == node->parent)) {
527 * just walk back to adjust the index
530 xmlNodePtr sibling = node;
532 while (sibling != NULL) {
533 if (sibling == select->previous)
535 if (xmlStrEqual(node->name, sibling->name)) {
536 if ((select->value2 == NULL) ||
537 ((sibling->ns != NULL) &&
538 (xmlStrEqual(select->value2,
539 sibling->ns->href))))
542 sibling = sibling->prev;
544 if (sibling == NULL) {
545 /* hum going backward in document order ... */
548 while (sibling != NULL) {
549 if (sibling == select->previous)
551 if ((select->value2 == NULL) ||
552 ((sibling->ns != NULL) &&
553 (xmlStrEqual(select->value2,
554 sibling->ns->href))))
556 sibling = sibling->next;
559 if (sibling != NULL) {
560 pos = select->index + i;
562 select->previous = node;
568 * recompute the index
570 xmlNodePtr siblings = node->parent->children;
572 while (siblings != NULL) {
573 if (siblings->type == XML_ELEMENT_NODE) {
574 if (siblings == node) {
577 } else if (xmlStrEqual(node->name,
579 if ((select->value2 == NULL) ||
580 ((siblings->ns != NULL) &&
581 (xmlStrEqual(select->value2,
582 siblings->ns->href))))
586 siblings = siblings->next;
590 ctxt->xpathCtxt->contextSize = len;
591 ctxt->xpathCtxt->proximityPosition = pos;
592 select->previous = node;
596 } else if ((select != NULL) && (select->op == XSLT_OP_ALL)) {
597 if ((select->previous != NULL) &&
598 (select->previous->parent == node->parent)) {
600 * just walk back to adjust the index
603 xmlNodePtr sibling = node;
605 while (sibling != NULL) {
606 if (sibling == select->previous)
608 if (sibling->type == XML_ELEMENT_NODE)
610 sibling = sibling->prev;
612 if (sibling == NULL) {
613 /* hum going backward in document order ... */
616 while (sibling != NULL) {
617 if (sibling == select->previous)
619 if (sibling->type == XML_ELEMENT_NODE)
621 sibling = sibling->next;
624 if (sibling != NULL) {
625 pos = select->index + i;
627 select->previous = node;
633 * recompute the index
635 xmlNodePtr siblings = node->parent->children;
637 while (siblings != NULL) {
638 if (siblings->type == XML_ELEMENT_NODE) {
640 if (siblings == node) {
644 siblings = siblings->next;
648 ctxt->xpathCtxt->contextSize = len;
649 ctxt->xpathCtxt->proximityPosition = pos;
650 select->previous = node;
655 oldNode = ctxt->node;
658 if ((step->value == NULL) ||
659 (!xsltEvalXPathPredicate(ctxt, step->value))) {
661 ctxt->xpathCtxt->contextSize = oldCS;
662 ctxt->xpathCtxt->proximityPosition = oldCP;
664 ctxt->node = oldNode;
668 ctxt->xpathCtxt->contextSize = oldCS;
669 ctxt->xpathCtxt->proximityPosition = oldCP;
671 ctxt->node = oldNode;
675 if (node->type != XML_PI_NODE)
677 if (step->value != NULL) {
678 if (!xmlStrEqual(step->value, node->name))
682 case XSLT_OP_COMMENT:
683 if (node->type != XML_COMMENT_NODE)
687 if ((node->type != XML_TEXT_NODE) &&
688 (node->type != XML_CDATA_SECTION_NODE))
692 switch (node->type) {
693 case XML_DOCUMENT_NODE:
694 case XML_HTML_DOCUMENT_NODE:
695 case XML_ELEMENT_NODE:
696 case XML_CDATA_SECTION_NODE:
698 case XML_COMMENT_NODE:
700 case XML_ATTRIBUTE_NODE:
712 * xsltTestCompMatchList:
713 * @ctxt: a XSLT process context
715 * @comp: the precompiled pattern list
717 * Test wether the node matches one of the patterns in the list
719 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
722 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
723 xsltCompMatchPtr comp) {
726 if ((ctxt == NULL) || (node == NULL))
728 while (comp != NULL) {
729 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
737 /************************************************************************
739 * Dedicated parser for templates *
741 ************************************************************************/
743 #define CUR (*ctxt->cur)
744 #define SKIP(val) ctxt->cur += (val)
745 #define NXT(val) ctxt->cur[(val)]
746 #define CUR_PTR ctxt->cur
748 #define SKIP_BLANKS \
749 while (IS_BLANK(CUR)) NEXT
751 #define CURRENT (*ctxt->cur)
752 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
755 #define PUSH(op, val, val2) \
756 if (xsltCompMatchAdd(ctxt->comp, (op), (val), (val2))) goto error;
759 xsltSwapTopCompMatch(ctxt->comp);
761 #define XSLT_ERROR(X) \
762 { xsltError(ctxt, __FILE__, __LINE__, X); \
763 ctxt->error = (X); return; }
765 #define XSLT_ERROR0(X) \
766 { xsltError(ctxt, __FILE__, __LINE__, X); \
767 ctxt->error = (X); return(0); }
771 * @ctxt: the XPath Parser context
773 * Parse an XPath Litteral:
775 * [29] Literal ::= '"' [^"]* '"'
778 * Returns the Literal parsed or NULL
782 xsltScanLiteral(xsltParserContextPtr ctxt) {
790 while ((IS_CHAR(CUR)) && (CUR != '"'))
793 /* XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); */
797 ret = xmlStrndup(q, CUR_PTR - q);
800 } else if (CUR == '\'') {
803 while ((IS_CHAR(CUR)) && (CUR != '\''))
806 /* XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); */
810 ret = xmlStrndup(q, CUR_PTR - q);
814 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
823 * @ctxt: the XPath Parser context
825 * Trickery: parse an XML name but without consuming the input flow
826 * Needed to avoid insanity in the parser state.
828 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
829 * CombiningChar | Extender
831 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
833 * [6] Names ::= Name (S Name)*
835 * Returns the Name parsed or NULL
839 xsltScanName(xsltParserContextPtr ctxt) {
840 xmlChar buf[XML_MAX_NAMELEN];
844 if (!IS_LETTER(CUR) && (CUR != '_') &&
849 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
850 (NXT(len) == '.') || (NXT(len) == '-') ||
851 (NXT(len) == '_') || (NXT(len) == ':') ||
852 (IS_COMBINING(NXT(len))) ||
853 (IS_EXTENDER(NXT(len)))) {
856 if (len >= XML_MAX_NAMELEN) {
857 xmlGenericError(xmlGenericErrorContext,
858 "xmlScanName: reached XML_MAX_NAMELEN limit\n");
859 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
860 (NXT(len) == '.') || (NXT(len) == '-') ||
861 (NXT(len) == '_') || (NXT(len) == ':') ||
862 (IS_COMBINING(NXT(len))) ||
863 (IS_EXTENDER(NXT(len))))
869 return(xmlStrndup(buf, len));
872 * xsltCompileIdKeyPattern:
873 * @comp: the compilation context
874 * @name: a preparsed name
875 * @aid: whether id/key are allowed there
877 * Compile the XSLT LocationIdKeyPattern
878 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
879 * | 'key' '(' Literal ',' Literal ')'
881 * also handle NodeType and PI from:
883 * [7] NodeTest ::= NameTest
885 * | 'processing-instruction' '(' Literal ')'
888 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
890 xmlChar *lit2 = NULL;
893 xsltGenericError(xsltGenericErrorContext,
894 "xsltCompileIdKeyPattern : ( expected\n");
898 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
901 lit = xsltScanLiteral(ctxt);
906 xsltGenericError(xsltGenericErrorContext,
907 "xsltCompileIdKeyPattern : ) expected\n");
912 PUSH(XSLT_OP_ID, lit, NULL);
913 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
916 lit = xsltScanLiteral(ctxt);
921 xsltGenericError(xsltGenericErrorContext,
922 "xsltCompileIdKeyPattern : , expected\n");
928 lit2 = xsltScanLiteral(ctxt);
933 xsltGenericError(xsltGenericErrorContext,
934 "xsltCompileIdKeyPattern : ) expected\n");
939 /* TODO: support namespace in keys */
940 PUSH(XSLT_OP_KEY, lit, lit2);
941 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
945 lit = xsltScanLiteral(ctxt);
950 xsltGenericError(xsltGenericErrorContext,
951 "xsltCompileIdKeyPattern : ) expected\n");
957 PUSH(XSLT_OP_PI, lit, NULL);
958 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
962 xsltGenericError(xsltGenericErrorContext,
963 "xsltCompileIdKeyPattern : ) expected\n");
968 PUSH(XSLT_OP_TEXT, NULL, NULL);
969 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
973 xsltGenericError(xsltGenericErrorContext,
974 "xsltCompileIdKeyPattern : ) expected\n");
979 PUSH(XSLT_OP_COMMENT, NULL, NULL);
980 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
984 xsltGenericError(xsltGenericErrorContext,
985 "xsltCompileIdKeyPattern : ) expected\n");
990 PUSH(XSLT_OP_NODE, NULL, NULL);
992 xsltGenericError(xsltGenericErrorContext,
993 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
997 xsltGenericError(xsltGenericErrorContext,
998 "xsltCompileIdKeyPattern : node type\n");
1008 * xsltCompileStepPattern:
1009 * @comp: the compilation context
1010 * @token: a posible precompiled name
1012 * Compile the XSLT StepPattern and generates a precompiled
1013 * form suitable for fast matching.
1015 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1016 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1017 * | ('child' | 'attribute') '::'
1019 * [7] NodeTest ::= NameTest
1020 * | NodeType '(' ')'
1021 * | 'processing-instruction' '(' Literal ')'
1022 * [8] Predicate ::= '[' PredicateExpr ']'
1023 * [9] PredicateExpr ::= Expr
1024 * [13] AbbreviatedAxisSpecifier ::= '@'?
1025 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1029 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1030 xmlChar *name = NULL;
1031 xmlChar *prefix = NULL;
1032 xmlChar *ncname = NULL;
1033 xmlChar *URL = NULL;
1037 if ((token == NULL) && (CUR == '@')) {
1041 PUSH(XSLT_OP_ATTR, NULL, NULL);
1044 token = xsltScanName(ctxt);
1045 if (token == NULL) {
1046 xsltGenericError(xsltGenericErrorContext,
1047 "xsltCompileStepPattern : Name expected\n");
1051 PUSH(XSLT_OP_ATTR, token, NULL);
1055 token = xsltScanName(ctxt);
1056 if (token == NULL) {
1059 PUSH(XSLT_OP_ALL, token, NULL);
1060 goto parse_predicate;
1062 xsltGenericError(xsltGenericErrorContext,
1063 "xsltCompileStepPattern : Name expected\n");
1072 xsltCompileIdKeyPattern(ctxt, token, 0);
1075 } else if (CUR == ':') {
1077 if (NXT(1) != ':') {
1078 xsltGenericError(xsltGenericErrorContext,
1079 "xsltCompileStepPattern : sequence '::' expected\n");
1084 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1085 name = xsltScanName(ctxt);
1087 xsltGenericError(xsltGenericErrorContext,
1088 "xsltCompileStepPattern : QName expected\n");
1092 ncname = xmlSplitQName2(name, &prefix);
1093 if (ncname != NULL) {
1094 if (prefix != NULL) {
1097 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1099 xsltGenericError(xsltGenericErrorContext,
1100 "xsl: pattern, no namespace bound to prefix %s\n",
1103 URL = xmlStrdup(ns->href);
1110 PUSH(XSLT_OP_CHILD, name, URL);
1111 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1112 name = xsltScanName(ctxt);
1114 xsltGenericError(xsltGenericErrorContext,
1115 "xsltCompileStepPattern : QName expected\n");
1119 ncname = xmlSplitQName2(name, &prefix);
1120 if (ncname != NULL) {
1121 if (prefix != NULL) {
1124 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1126 xsltGenericError(xsltGenericErrorContext,
1127 "xsl: pattern, no namespace bound to prefix %s\n",
1130 URL = xmlStrdup(ns->href);
1137 PUSH(XSLT_OP_ATTR, name, URL);
1139 xsltGenericError(xsltGenericErrorContext,
1140 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1145 } else if (CUR == '*') {
1147 PUSH(XSLT_OP_ALL, token, NULL);
1149 ncname = xmlSplitQName2(token, &prefix);
1150 if (ncname != NULL) {
1151 if (prefix != NULL) {
1154 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1156 xsltGenericError(xsltGenericErrorContext,
1157 "xsl: pattern, no namespace bound to prefix %s\n",
1160 URL = xmlStrdup(ns->href);
1167 PUSH(XSLT_OP_ELEM, token, URL);
1172 while (CUR == '[') {
1174 xmlChar *ret = NULL;
1179 /* TODO: avoid breaking in strings ... */
1180 while (IS_CHAR(CUR)) {
1181 /* Skip over nested predicates */
1191 if (!IS_CHAR(CUR)) {
1192 xsltGenericError(xsltGenericErrorContext,
1193 "xsltCompileStepPattern : ']' expected\n");
1197 ret = xmlStrndup(q, CUR_PTR - q);
1198 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1199 /* push the predicate lower than local test */
1212 * xsltCompileRelativePathPattern:
1213 * @comp: the compilation context
1214 * @token: a posible precompiled name
1216 * Compile the XSLT RelativePathPattern and generates a precompiled
1217 * form suitable for fast matching.
1219 * [4] RelativePathPattern ::= StepPattern
1220 * | RelativePathPattern '/' StepPattern
1221 * | RelativePathPattern '//' StepPattern
1224 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1225 xsltCompileStepPattern(ctxt, token);
1229 while ((CUR != 0) && (CUR != '|')) {
1230 if ((CUR == '/') && (NXT(1) == '/')) {
1231 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1235 xsltCompileStepPattern(ctxt, NULL);
1236 } else if (CUR == '/') {
1237 PUSH(XSLT_OP_PARENT, NULL, NULL);
1240 if ((CUR != 0) || (CUR == '|')) {
1241 xsltCompileRelativePathPattern(ctxt, NULL);
1255 * xsltCompileLocationPathPattern:
1256 * @comp: the compilation context
1258 * Compile the XSLT LocationPathPattern and generates a precompiled
1259 * form suitable for fast matching.
1261 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1262 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1263 * | '//'? RelativePathPattern
1266 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1268 if ((CUR == '/') && (NXT(1) == '/')) {
1270 * since we reverse the query
1271 * a leading // can be safely ignored
1275 xsltCompileRelativePathPattern(ctxt, NULL);
1276 } else if (CUR == '/') {
1278 * We need to find root as the parent
1282 PUSH(XSLT_OP_ROOT, NULL, NULL);
1283 if ((CUR != 0) || (CUR == '|')) {
1284 PUSH(XSLT_OP_PARENT, NULL, NULL);
1285 xsltCompileRelativePathPattern(ctxt, NULL);
1287 } else if (CUR == '*') {
1288 xsltCompileRelativePathPattern(ctxt, NULL);
1289 } else if (CUR == '@') {
1290 xsltCompileRelativePathPattern(ctxt, NULL);
1293 name = xsltScanName(ctxt);
1295 xsltGenericError(xsltGenericErrorContext,
1296 "xsltCompileLocationPathPattern : Name expected\n");
1301 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1302 xsltCompileIdKeyPattern(ctxt, name, 1);
1303 if ((CUR == '/') && (NXT(1) == '/')) {
1304 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1308 xsltCompileRelativePathPattern(ctxt, NULL);
1309 } else if (CUR == '/') {
1310 PUSH(XSLT_OP_PARENT, NULL, NULL);
1313 xsltCompileRelativePathPattern(ctxt, NULL);
1317 xsltCompileRelativePathPattern(ctxt, name);
1324 * xsltCompilePattern:
1325 * @pattern an XSLT pattern
1326 * @doc: the containing document
1327 * @node: the containing element
1329 * Compile the XSLT pattern and generates a list of precompiled form suitable
1330 * for fast matching.
1332 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1334 * Returns the generated pattern list or NULL in case of failure
1338 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc, xmlNodePtr node) {
1339 xsltParserContextPtr ctxt = NULL;
1340 xsltCompMatchPtr element, first = NULL, previous = NULL;
1341 int current, start, end;
1343 if (pattern == NULL) {
1344 xsltGenericError(xsltGenericErrorContext,
1345 "xsltCompilePattern : NULL pattern\n");
1349 #ifdef DEBUG_PATTERN
1350 xsltGenericDebug(xsltGenericDebugContext,
1351 "xsltCompilePattern : parsing '%s'\n", pattern);
1354 ctxt = xsltNewParserContext();
1360 while (pattern[current] != 0) {
1362 while (IS_BLANK(pattern[current]))
1365 while ((pattern[end] != 0) && (pattern[end] != '|'))
1367 if (current == end) {
1368 xsltGenericError(xsltGenericErrorContext,
1369 "xsltCompilePattern : NULL pattern\n");
1372 element = xsltNewCompMatch();
1373 if (element == NULL) {
1378 else if (previous != NULL)
1379 previous->next = element;
1382 ctxt->comp = element;
1383 ctxt->base = xmlStrndup(&pattern[start], end - start);
1384 ctxt->cur = &(ctxt->base)[current - start];
1385 xsltCompileLocationPathPattern(ctxt);
1387 xmlFree((xmlChar *)ctxt->base);
1392 * Reverse for faster interpretation.
1394 xsltReverseCompMatch(element);
1397 * Set-up the priority
1399 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1400 (element->steps[0].op == XSLT_OP_ATTR)) &&
1401 (element->steps[0].value != NULL) &&
1402 (element->steps[1].op == XSLT_OP_END)) {
1403 element->priority = 0;
1404 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1405 (element->steps[1].op == XSLT_OP_END)) {
1406 element->priority = 0;
1407 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1408 (element->steps[0].value != NULL) &&
1409 (element->steps[1].op == XSLT_OP_END)) {
1410 element->priority = 0;
1411 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1412 (element->steps[0].value != NULL) &&
1413 (element->steps[1].op == XSLT_OP_END)) {
1414 element->priority = -0.25;
1415 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1416 (element->steps[0].op == XSLT_OP_TEXT) ||
1417 (element->steps[0].op == XSLT_OP_ALL) ||
1418 (element->steps[0].op == XSLT_OP_NODE) ||
1419 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1420 (element->steps[1].op == XSLT_OP_END)) {
1421 element->priority = -0.5;
1423 element->priority = 0.5;
1425 if (pattern[end] == '|')
1430 xsltGenericError(xsltGenericErrorContext,
1431 "xsltCompilePattern : NULL pattern\n");
1435 xsltFreeParserContext(ctxt);
1440 xsltFreeParserContext(ctxt);
1442 xsltFreeCompMatchList(first);
1446 /************************************************************************
1448 * Module interfaces *
1450 ************************************************************************/
1454 * @style: an XSLT stylesheet
1455 * @cur: an XSLT template
1456 * @mode: the mode name or NULL
1457 * @modeURI: the mode URI or NULL
1459 * Register the XSLT pattern associated to @cur
1461 * Returns -1 in case of error, 0 otherwise
1464 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1465 const xmlChar *mode, const xmlChar *modeURI) {
1466 xsltCompMatchPtr pat, list, *top = NULL, next;
1467 const xmlChar *name = NULL;
1469 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1472 pat = xsltCompilePattern(cur->match, style->doc, cur->elem);
1477 pat->template = cur;
1479 pat->mode = xmlStrdup(mode);
1480 if (modeURI != NULL)
1481 pat->modeURI = xmlStrdup(modeURI);
1482 if (cur->priority == XSLT_PAT_NO_PRIORITY)
1483 cur->priority = pat->priority;
1485 pat->priority = cur->priority;
1488 * insert it in the hash table list corresponding to its lookup name
1490 switch (pat->steps[0].op) {
1492 if (pat->steps[0].value != NULL)
1493 name = pat->steps[0].value;
1495 top = (xsltCompMatchPtr *) &(style->attrMatch);
1499 case XSLT_OP_PARENT:
1500 case XSLT_OP_ANCESTOR:
1502 name = pat->steps[0].value;
1505 top = (xsltCompMatchPtr *) &(style->rootMatch);
1508 top = (xsltCompMatchPtr *) &(style->keyMatch);
1511 /* TODO optimize ID !!! */
1513 top = (xsltCompMatchPtr *) &(style->elemMatch);
1516 case XSLT_OP_PREDICATE:
1517 xsltGenericError(xsltGenericErrorContext,
1518 "xsltAddTemplate: invalid compiled pattern\n");
1519 xsltFreeCompMatch(pat);
1522 * TODO: some flags at the top level about type based patterns
1523 * would be faster than inclusion in the hash table.
1526 if (pat->steps[0].value != NULL)
1527 name = pat->steps[0].value;
1529 top = (xsltCompMatchPtr *) &(style->piMatch);
1531 case XSLT_OP_COMMENT:
1532 top = (xsltCompMatchPtr *) &(style->commentMatch);
1535 top = (xsltCompMatchPtr *) &(style->textMatch);
1538 if (pat->steps[0].value != NULL)
1539 name = pat->steps[0].value;
1541 top = (xsltCompMatchPtr *) &(style->elemMatch);
1546 if (style->templatesHash == NULL) {
1547 style->templatesHash = xmlHashCreate(1024);
1548 if (style->templatesHash == NULL) {
1549 xsltFreeCompMatch(pat);
1552 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1554 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1555 name, mode, modeURI);
1557 xmlHashAddEntry3(style->templatesHash, name,
1558 mode, modeURI, pat);
1561 * Note '<=' since one must choose among the matching
1562 * template rules that are left, the one that occurs
1563 * last in the stylesheet
1565 if (list->priority <= pat->priority) {
1567 xmlHashUpdateEntry3(style->templatesHash, name,
1568 mode, modeURI, pat, NULL);
1570 while (list->next != NULL) {
1571 if (list->next->priority <= pat->priority)
1575 pat->next = list->next;
1580 } else if (top != NULL) {
1585 } else if (list->priority <= pat->priority) {
1589 while (list->next != NULL) {
1590 if (list->next->priority <= pat->priority)
1594 pat->next = list->next;
1598 xsltGenericError(xsltGenericErrorContext,
1599 "xsltAddTemplate: invalid compiled pattern\n");
1600 xsltFreeCompMatch(pat);
1603 #ifdef DEBUG_PATTERN
1605 xsltGenericDebug(xsltGenericDebugContext,
1606 "added pattern : '%s' mode '%s' priority %f\n",
1607 pat->template->match, pat->mode, pat->priority);
1609 xsltGenericDebug(xsltGenericDebugContext,
1610 "added pattern : '%s' priority %f\n",
1611 pat->template->match, pat->priority);
1621 * @ctxt: a XSLT process context
1623 * @style: the current style
1625 * Finds the template applying to this node, if @style is non-NULL
1626 * it means one need to look for the next imported template in scope.
1628 * Returns the xsltTemplatePtr or NULL if not found
1631 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
1632 xsltStylesheetPtr style) {
1633 xsltStylesheetPtr curstyle;
1634 xsltTemplatePtr ret = NULL;
1635 const xmlChar *name = NULL;
1636 xsltCompMatchPtr list = NULL;
1638 if ((ctxt == NULL) || (node == NULL))
1641 if (style == NULL) {
1642 curstyle = ctxt->style;
1644 curstyle = xsltNextImport(style);
1647 while ((curstyle != NULL) && (curstyle != style)) {
1648 /* TODO : handle IDs/keys here ! */
1649 if (curstyle->templatesHash != NULL) {
1651 * Use the top name as selector
1653 switch (node->type) {
1654 case XML_ELEMENT_NODE:
1655 case XML_ATTRIBUTE_NODE:
1659 case XML_DOCUMENT_NODE:
1660 case XML_HTML_DOCUMENT_NODE:
1662 case XML_CDATA_SECTION_NODE:
1663 case XML_COMMENT_NODE:
1664 case XML_ENTITY_REF_NODE:
1665 case XML_ENTITY_NODE:
1666 case XML_DOCUMENT_TYPE_NODE:
1667 case XML_DOCUMENT_FRAG_NODE:
1668 case XML_NOTATION_NODE:
1670 case XML_ELEMENT_DECL:
1671 case XML_ATTRIBUTE_DECL:
1672 case XML_ENTITY_DECL:
1673 case XML_NAMESPACE_DECL:
1674 case XML_XINCLUDE_START:
1675 case XML_XINCLUDE_END:
1684 * find the list of appliable expressions based on the name
1686 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
1687 name, ctxt->mode, ctxt->modeURI);
1690 while (list != NULL) {
1691 if (xsltTestCompMatch(ctxt, list, node,
1692 ctxt->mode, ctxt->modeURI)) {
1693 ret = list->template;
1701 * find alternate generic matches
1703 switch (node->type) {
1704 case XML_ELEMENT_NODE:
1705 list = curstyle->elemMatch;
1707 case XML_ATTRIBUTE_NODE:
1708 list = curstyle->attrMatch;
1711 list = curstyle->piMatch;
1713 case XML_DOCUMENT_NODE:
1714 case XML_HTML_DOCUMENT_NODE:
1715 list = curstyle->rootMatch;
1718 case XML_CDATA_SECTION_NODE:
1719 list = curstyle->textMatch;
1721 case XML_COMMENT_NODE:
1722 list = curstyle->commentMatch;
1724 case XML_ENTITY_REF_NODE:
1725 case XML_ENTITY_NODE:
1726 case XML_DOCUMENT_TYPE_NODE:
1727 case XML_DOCUMENT_FRAG_NODE:
1728 case XML_NOTATION_NODE:
1730 case XML_ELEMENT_DECL:
1731 case XML_ATTRIBUTE_DECL:
1732 case XML_ENTITY_DECL:
1733 case XML_NAMESPACE_DECL:
1734 case XML_XINCLUDE_START:
1735 case XML_XINCLUDE_END:
1741 while ((list != NULL) &&
1742 ((ret == NULL) || (list->priority > ret->priority))) {
1743 if (xsltTestCompMatch(ctxt, list, node,
1744 ctxt->mode, ctxt->modeURI)) {
1745 ret = list->template;
1750 if (node->_private != NULL) {
1751 list = curstyle->keyMatch;
1752 while ((list != NULL) &&
1753 ((ret == NULL) || (list->priority > ret->priority))) {
1754 if (xsltTestCompMatch(ctxt, list, node,
1755 ctxt->mode, ctxt->modeURI)) {
1756 ret = list->template;
1766 * Cycle on next curstylesheet import.
1768 curstyle = xsltNextImport(curstyle);
1774 * xsltCleanupTemplates:
1775 * @style: an XSLT stylesheet
1777 * Cleanup the state of the templates used by the stylesheet and
1778 * the ones it imports.
1781 xsltCleanupTemplates(xsltStylesheetPtr style) {
1782 while (style != NULL) {
1783 xmlHashScan((xmlHashTablePtr) style->templatesHash,
1784 (xmlHashScanner) xsltCleanupCompMatch, NULL);
1786 style = xsltNextImport(style);
1791 * xsltFreeTemplateHashes:
1792 * @style: an XSLT stylesheet
1794 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
1797 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
1798 if (style->templatesHash != NULL)
1799 xmlHashFree((xmlHashTablePtr) style->templatesHash,
1800 (xmlHashDeallocator) xsltFreeCompMatchList);
1801 if (style->rootMatch != NULL)
1802 xsltFreeCompMatchList(style->rootMatch);
1803 if (style->keyMatch != NULL)
1804 xsltFreeCompMatchList(style->keyMatch);
1805 if (style->elemMatch != NULL)
1806 xsltFreeCompMatchList(style->elemMatch);
1807 if (style->attrMatch != NULL)
1808 xsltFreeCompMatchList(style->attrMatch);
1809 if (style->parentMatch != NULL)
1810 xsltFreeCompMatchList(style->parentMatch);
1811 if (style->textMatch != NULL)
1812 xsltFreeCompMatchList(style->textMatch);
1813 if (style->piMatch != NULL)
1814 xsltFreeCompMatchList(style->piMatch);
1815 if (style->commentMatch != NULL)
1816 xsltFreeCompMatchList(style->commentMatch);
1822 * @node: a node in the source tree
1823 * @pattern: an XSLT pattern
1825 * Determine if a node matches a pattern.
1828 xsltMatchPattern(xsltTransformContextPtr context,
1830 const xmlChar *pattern)
1833 xsltCompMatchPtr first, comp;
1835 if ((context != NULL) && (pattern != NULL)) {
1836 first = xsltCompilePattern(pattern);
1837 for (comp = first; comp != NULL; comp = comp->next) {
1838 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
1843 xsltFreeCompMatchList(first);