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;
292 /************************************************************************
294 * The interpreter for the precompiled patterns *
296 ************************************************************************/
300 * @ctxt: a XSLT process context
301 * @comp: the precompiled pattern
303 * @mode: the mode name or NULL
304 * @modeURI: the mode URI or NULL
306 * Test wether the node matches the pattern
308 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
311 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
312 xmlNodePtr node, const xmlChar *mode,
313 const xmlChar *modeURI) {
315 xsltStepOpPtr step, select = NULL;
317 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
318 xsltGenericError(xsltGenericErrorContext,
319 "xsltTestCompMatch: null arg\n");
323 if (comp->mode == NULL)
325 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
328 if (comp->mode != NULL)
331 if (modeURI != NULL) {
332 if (comp->modeURI == NULL)
334 if ((comp->modeURI != modeURI) &&
335 (!xmlStrEqual(comp->modeURI, modeURI)))
338 if (comp->modeURI != NULL)
341 for (i = 0;i < comp->nbStep;i++) {
342 step = &comp->steps[i];
343 if (step->op != XSLT_OP_PREDICATE)
349 if ((node->type == XML_DOCUMENT_NODE) ||
350 (node->type == XML_HTML_DOCUMENT_NODE))
354 if (node->type != XML_ELEMENT_NODE)
356 if (step->value == NULL)
358 if (!xmlStrEqual(step->value, node->name))
362 if (node->ns == NULL) {
363 if (step->value2 != NULL)
365 } else if (node->ns->href != NULL) {
366 if (step->value2 == NULL)
368 if (!xmlStrEqual(step->value2, node->ns->href))
373 TODO /* Handle OP_CHILD */
376 if (node->type != XML_ATTRIBUTE_NODE)
378 if (step->value == NULL)
380 if (!xmlStrEqual(step->value, node->name))
384 if (node->ns == NULL) {
385 if (step->value2 != NULL)
387 } else if (node->ns->href != NULL) {
388 if (step->value2 == NULL)
390 if (!xmlStrEqual(step->value2, node->ns->href))
398 if (step->value == NULL)
400 if (!xmlStrEqual(step->value, node->name))
403 if (node->ns == NULL) {
404 if (step->value2 != NULL)
406 } else if (node->ns->href != NULL) {
407 if (step->value2 == NULL)
409 if (!xmlStrEqual(step->value2, node->ns->href))
413 case XSLT_OP_ANCESTOR:
414 /* TODO: implement coalescing of ANCESTOR/NODE ops */
415 if (step->value == NULL) {
417 step = &comp->steps[i];
418 if (step->op == XSLT_OP_ROOT)
420 if (step->op != XSLT_OP_ELEM)
422 if (step->value == NULL)
428 while (node != NULL) {
431 if (xmlStrEqual(step->value, node->name)) {
433 if (node->ns == NULL) {
434 if (step->value2 == NULL)
436 } else if (node->ns->href != NULL) {
437 if ((step->value2 != NULL) &&
438 (xmlStrEqual(step->value2, node->ns->href)))
448 /* TODO Handle IDs decently, must be done differently */
451 id = xmlGetID(node->doc, step->value);
452 if ((id == NULL) || (id->parent != node))
460 list = xsltGetKey(ctxt, step->value,
461 step->value3, step->value2);
464 for (i = 0;i < list->nodeNr;i++)
465 if (list->nodeTab[i] == node)
467 if (i >= list->nodeNr)
473 if (node->ns == NULL) {
474 if (step->value != NULL)
476 } else if (node->ns->href != NULL) {
477 if (step->value == NULL)
479 if (!xmlStrEqual(step->value, node->ns->href))
484 switch (node->type) {
485 case XML_DOCUMENT_NODE:
486 case XML_HTML_DOCUMENT_NODE:
487 case XML_ELEMENT_NODE:
493 case XSLT_OP_PREDICATE: {
496 int pos = 0, len = 0;
498 * Depending on the last selection, one may need to
499 * recompute contextSize and proximityPosition.
501 oldCS = ctxt->xpathCtxt->contextSize;
502 oldCP = ctxt->xpathCtxt->proximityPosition;
503 if ((select != NULL) &&
504 (select->op == XSLT_OP_ELEM) &&
505 (select->value != NULL) &&
506 (node->type == XML_ELEMENT_NODE) &&
507 (node->parent != NULL)) {
509 if ((select->previous != NULL) &&
510 (select->previous->parent == node->parent)) {
512 * just walk back to adjust the index
515 xmlNodePtr sibling = node;
517 while (sibling != NULL) {
518 if (sibling == select->previous)
520 if (xmlStrEqual(node->name, sibling->name)) {
521 if ((select->value2 == NULL) ||
522 ((sibling->ns != NULL) &&
523 (xmlStrEqual(select->value2,
524 sibling->ns->href))))
527 sibling = sibling->prev;
529 if (sibling == NULL) {
530 /* hum going backward in document order ... */
533 while (sibling != NULL) {
534 if (sibling == select->previous)
536 if ((select->value2 == NULL) ||
537 ((sibling->ns != NULL) &&
538 (xmlStrEqual(select->value2,
539 sibling->ns->href))))
541 sibling = sibling->next;
544 if (sibling != NULL) {
545 pos = select->index + i;
547 select->previous = node;
553 * recompute the index
555 xmlNodePtr siblings = node->parent->children;
557 while (siblings != NULL) {
558 if (siblings->type == XML_ELEMENT_NODE) {
559 if (siblings == node) {
562 } else if (xmlStrEqual(node->name,
564 if ((select->value2 == NULL) ||
565 ((siblings->ns != NULL) &&
566 (xmlStrEqual(select->value2,
567 siblings->ns->href))))
571 siblings = siblings->next;
575 ctxt->xpathCtxt->contextSize = len;
576 ctxt->xpathCtxt->proximityPosition = pos;
577 select->previous = node;
581 } else if ((select != NULL) && (select->op == XSLT_OP_ALL)) {
582 if ((select->previous != NULL) &&
583 (select->previous->parent == node->parent)) {
585 * just walk back to adjust the index
588 xmlNodePtr sibling = node;
590 while (sibling != NULL) {
591 if (sibling == select->previous)
593 if (sibling->type == XML_ELEMENT_NODE)
595 sibling = sibling->prev;
597 if (sibling == NULL) {
598 /* hum going backward in document order ... */
601 while (sibling != NULL) {
602 if (sibling == select->previous)
604 if (sibling->type == XML_ELEMENT_NODE)
606 sibling = sibling->next;
609 if (sibling != NULL) {
610 pos = select->index + i;
612 select->previous = node;
618 * recompute the index
620 xmlNodePtr siblings = node->parent->children;
622 while (siblings != NULL) {
623 if (siblings->type == XML_ELEMENT_NODE) {
625 if (siblings == node) {
629 siblings = siblings->next;
633 ctxt->xpathCtxt->contextSize = len;
634 ctxt->xpathCtxt->proximityPosition = pos;
635 select->previous = node;
640 oldNode = ctxt->node;
643 if ((step->value == NULL) ||
644 (!xsltEvalXPathPredicate(ctxt, step->value))) {
646 ctxt->xpathCtxt->contextSize = oldCS;
647 ctxt->xpathCtxt->proximityPosition = oldCP;
649 ctxt->node = oldNode;
653 ctxt->xpathCtxt->contextSize = oldCS;
654 ctxt->xpathCtxt->proximityPosition = oldCP;
656 ctxt->node = oldNode;
660 if (node->type != XML_PI_NODE)
662 if (step->value != NULL) {
663 if (!xmlStrEqual(step->value, node->name))
667 case XSLT_OP_COMMENT:
668 if (node->type != XML_COMMENT_NODE)
672 if ((node->type != XML_TEXT_NODE) &&
673 (node->type != XML_CDATA_SECTION_NODE))
677 switch (node->type) {
678 case XML_DOCUMENT_NODE:
679 case XML_HTML_DOCUMENT_NODE:
680 case XML_ELEMENT_NODE:
681 case XML_CDATA_SECTION_NODE:
683 case XML_COMMENT_NODE:
685 case XML_ATTRIBUTE_NODE:
697 * xsltTestCompMatchList:
698 * @ctxt: a XSLT process context
700 * @comp: the precompiled pattern list
702 * Test wether the node matches one of the patterns in the list
704 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
707 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
708 xsltCompMatchPtr comp) {
711 if ((ctxt == NULL) || (node == NULL))
713 while (comp != NULL) {
714 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
722 /************************************************************************
724 * Dedicated parser for templates *
726 ************************************************************************/
728 #define CUR (*ctxt->cur)
729 #define SKIP(val) ctxt->cur += (val)
730 #define NXT(val) ctxt->cur[(val)]
731 #define CUR_PTR ctxt->cur
733 #define SKIP_BLANKS \
734 while (IS_BLANK(CUR)) NEXT
736 #define CURRENT (*ctxt->cur)
737 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
740 #define PUSH(op, val, val2) \
741 if (xsltCompMatchAdd(ctxt->comp, (op), (val), (val2))) goto error;
744 xsltSwapTopCompMatch(ctxt->comp);
746 #define XSLT_ERROR(X) \
747 { xsltError(ctxt, __FILE__, __LINE__, X); \
748 ctxt->error = (X); return; }
750 #define XSLT_ERROR0(X) \
751 { xsltError(ctxt, __FILE__, __LINE__, X); \
752 ctxt->error = (X); return(0); }
756 * @ctxt: the XPath Parser context
758 * Parse an XPath Litteral:
760 * [29] Literal ::= '"' [^"]* '"'
763 * Returns the Literal parsed or NULL
767 xsltScanLiteral(xsltParserContextPtr ctxt) {
775 while ((IS_CHAR(CUR)) && (CUR != '"'))
778 /* XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); */
782 ret = xmlStrndup(q, CUR_PTR - q);
785 } else if (CUR == '\'') {
788 while ((IS_CHAR(CUR)) && (CUR != '\''))
791 /* XP_ERROR(XPATH_UNFINISHED_LITERAL_ERROR); */
795 ret = xmlStrndup(q, CUR_PTR - q);
799 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
808 * @ctxt: the XPath Parser context
810 * Trickery: parse an XML name but without consuming the input flow
811 * Needed to avoid insanity in the parser state.
813 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
814 * CombiningChar | Extender
816 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
818 * [6] Names ::= Name (S Name)*
820 * Returns the Name parsed or NULL
824 xsltScanName(xsltParserContextPtr ctxt) {
825 xmlChar buf[XML_MAX_NAMELEN];
829 if (!IS_LETTER(CUR) && (CUR != '_') &&
834 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
835 (NXT(len) == '.') || (NXT(len) == '-') ||
836 (NXT(len) == '_') || (NXT(len) == ':') ||
837 (IS_COMBINING(NXT(len))) ||
838 (IS_EXTENDER(NXT(len)))) {
841 if (len >= XML_MAX_NAMELEN) {
842 xmlGenericError(xmlGenericErrorContext,
843 "xmlScanName: reached XML_MAX_NAMELEN limit\n");
844 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) ||
845 (NXT(len) == '.') || (NXT(len) == '-') ||
846 (NXT(len) == '_') || (NXT(len) == ':') ||
847 (IS_COMBINING(NXT(len))) ||
848 (IS_EXTENDER(NXT(len))))
854 return(xmlStrndup(buf, len));
857 * xsltCompileIdKeyPattern:
858 * @comp: the compilation context
859 * @name: a preparsed name
860 * @aid: whether id/key are allowed there
862 * Compile the XSLT LocationIdKeyPattern
863 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
864 * | 'key' '(' Literal ',' Literal ')'
866 * also handle NodeType and PI from:
868 * [7] NodeTest ::= NameTest
870 * | 'processing-instruction' '(' Literal ')'
873 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
875 xmlChar *lit2 = NULL;
878 xsltGenericError(xsltGenericErrorContext,
879 "xsltCompileIdKeyPattern : ( expected\n");
883 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
886 lit = xsltScanLiteral(ctxt);
891 xsltGenericError(xsltGenericErrorContext,
892 "xsltCompileIdKeyPattern : ) expected\n");
897 PUSH(XSLT_OP_ID, lit, NULL);
898 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
901 lit = xsltScanLiteral(ctxt);
906 xsltGenericError(xsltGenericErrorContext,
907 "xsltCompileIdKeyPattern : , expected\n");
913 lit2 = xsltScanLiteral(ctxt);
918 xsltGenericError(xsltGenericErrorContext,
919 "xsltCompileIdKeyPattern : ) expected\n");
924 /* TODO: support namespace in keys */
925 PUSH(XSLT_OP_KEY, lit, lit2);
926 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
930 lit = xsltScanLiteral(ctxt);
935 xsltGenericError(xsltGenericErrorContext,
936 "xsltCompileIdKeyPattern : ) expected\n");
942 PUSH(XSLT_OP_PI, lit, NULL);
943 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
947 xsltGenericError(xsltGenericErrorContext,
948 "xsltCompileIdKeyPattern : ) expected\n");
953 PUSH(XSLT_OP_TEXT, NULL, NULL);
954 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
958 xsltGenericError(xsltGenericErrorContext,
959 "xsltCompileIdKeyPattern : ) expected\n");
964 PUSH(XSLT_OP_COMMENT, NULL, NULL);
965 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
969 xsltGenericError(xsltGenericErrorContext,
970 "xsltCompileIdKeyPattern : ) expected\n");
975 PUSH(XSLT_OP_NODE, NULL, NULL);
977 xsltGenericError(xsltGenericErrorContext,
978 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
982 xsltGenericError(xsltGenericErrorContext,
983 "xsltCompileIdKeyPattern : node type\n");
993 * xsltCompileStepPattern:
994 * @comp: the compilation context
995 * @token: a posible precompiled name
997 * Compile the XSLT StepPattern and generates a precompiled
998 * form suitable for fast matching.
1000 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1001 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1002 * | ('child' | 'attribute') '::'
1004 * [7] NodeTest ::= NameTest
1005 * | NodeType '(' ')'
1006 * | 'processing-instruction' '(' Literal ')'
1007 * [8] Predicate ::= '[' PredicateExpr ']'
1008 * [9] PredicateExpr ::= Expr
1009 * [13] AbbreviatedAxisSpecifier ::= '@'?
1010 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1014 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1015 xmlChar *name = NULL;
1016 xmlChar *prefix = NULL;
1017 xmlChar *ncname = NULL;
1018 xmlChar *URL = NULL;
1022 if ((token == NULL) && (CUR == '@')) {
1026 PUSH(XSLT_OP_ATTR, NULL, NULL);
1029 token = xsltScanName(ctxt);
1030 if (token == NULL) {
1031 xsltGenericError(xsltGenericErrorContext,
1032 "xsltCompileStepPattern : Name expected\n");
1036 PUSH(XSLT_OP_ATTR, token, NULL);
1040 token = xsltScanName(ctxt);
1041 if (token == NULL) {
1044 PUSH(XSLT_OP_ALL, token, NULL);
1045 goto parse_predicate;
1047 xsltGenericError(xsltGenericErrorContext,
1048 "xsltCompileStepPattern : Name expected\n");
1057 xsltCompileIdKeyPattern(ctxt, token, 0);
1060 } else if (CUR == ':') {
1062 if (NXT(1) != ':') {
1063 xsltGenericError(xsltGenericErrorContext,
1064 "xsltCompileStepPattern : sequence '::' expected\n");
1069 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1070 name = xsltScanName(ctxt);
1072 xsltGenericError(xsltGenericErrorContext,
1073 "xsltCompileStepPattern : QName expected\n");
1077 ncname = xmlSplitQName2(name, &prefix);
1078 if (ncname != NULL) {
1079 if (prefix != NULL) {
1082 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1084 xsltGenericError(xsltGenericErrorContext,
1085 "xsl: pattern, no namespace bound to prefix %s\n",
1088 URL = xmlStrdup(ns->href);
1095 PUSH(XSLT_OP_CHILD, name, URL);
1096 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1097 name = xsltScanName(ctxt);
1099 xsltGenericError(xsltGenericErrorContext,
1100 "xsltCompileStepPattern : QName expected\n");
1104 ncname = xmlSplitQName2(name, &prefix);
1105 if (ncname != NULL) {
1106 if (prefix != NULL) {
1109 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1111 xsltGenericError(xsltGenericErrorContext,
1112 "xsl: pattern, no namespace bound to prefix %s\n",
1115 URL = xmlStrdup(ns->href);
1122 PUSH(XSLT_OP_ATTR, name, URL);
1124 xsltGenericError(xsltGenericErrorContext,
1125 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1130 } else if (CUR == '*') {
1132 PUSH(XSLT_OP_ALL, token, NULL);
1134 ncname = xmlSplitQName2(token, &prefix);
1135 if (ncname != NULL) {
1136 if (prefix != NULL) {
1139 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1141 xsltGenericError(xsltGenericErrorContext,
1142 "xsl: pattern, no namespace bound to prefix %s\n",
1145 URL = xmlStrdup(ns->href);
1152 PUSH(XSLT_OP_ELEM, token, URL);
1157 while (CUR == '[') {
1159 xmlChar *ret = NULL;
1164 /* TODO: avoid breaking in strings ... */
1165 while (IS_CHAR(CUR)) {
1166 /* Skip over nested predicates */
1176 if (!IS_CHAR(CUR)) {
1177 xsltGenericError(xsltGenericErrorContext,
1178 "xsltCompileStepPattern : ']' expected\n");
1182 ret = xmlStrndup(q, CUR_PTR - q);
1183 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1184 /* push the predicate lower than local test */
1197 * xsltCompileRelativePathPattern:
1198 * @comp: the compilation context
1199 * @token: a posible precompiled name
1201 * Compile the XSLT RelativePathPattern and generates a precompiled
1202 * form suitable for fast matching.
1204 * [4] RelativePathPattern ::= StepPattern
1205 * | RelativePathPattern '/' StepPattern
1206 * | RelativePathPattern '//' StepPattern
1209 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1210 xsltCompileStepPattern(ctxt, token);
1214 while ((CUR != 0) && (CUR != '|')) {
1215 if ((CUR == '/') && (NXT(1) == '/')) {
1216 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1220 xsltCompileStepPattern(ctxt, NULL);
1221 } else if (CUR == '/') {
1222 PUSH(XSLT_OP_PARENT, NULL, NULL);
1225 if ((CUR != 0) || (CUR == '|')) {
1226 xsltCompileRelativePathPattern(ctxt, NULL);
1240 * xsltCompileLocationPathPattern:
1241 * @comp: the compilation context
1243 * Compile the XSLT LocationPathPattern and generates a precompiled
1244 * form suitable for fast matching.
1246 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1247 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1248 * | '//'? RelativePathPattern
1251 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1253 if ((CUR == '/') && (NXT(1) == '/')) {
1255 * since we reverse the query
1256 * a leading // can be safely ignored
1260 xsltCompileRelativePathPattern(ctxt, NULL);
1261 } else if (CUR == '/') {
1263 * We need to find root as the parent
1267 PUSH(XSLT_OP_ROOT, NULL, NULL);
1268 if ((CUR != 0) || (CUR == '|')) {
1269 PUSH(XSLT_OP_PARENT, NULL, NULL);
1270 xsltCompileRelativePathPattern(ctxt, NULL);
1272 } else if (CUR == '*') {
1273 xsltCompileRelativePathPattern(ctxt, NULL);
1274 } else if (CUR == '@') {
1275 xsltCompileRelativePathPattern(ctxt, NULL);
1278 name = xsltScanName(ctxt);
1280 xsltGenericError(xsltGenericErrorContext,
1281 "xsltCompileLocationPathPattern : Name expected\n");
1286 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1287 xsltCompileIdKeyPattern(ctxt, name, 1);
1288 if ((CUR == '/') && (NXT(1) == '/')) {
1289 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1293 xsltCompileRelativePathPattern(ctxt, NULL);
1294 } else if (CUR == '/') {
1295 PUSH(XSLT_OP_PARENT, NULL, NULL);
1298 xsltCompileRelativePathPattern(ctxt, NULL);
1302 xsltCompileRelativePathPattern(ctxt, name);
1309 * xsltCompilePattern:
1310 * @pattern an XSLT pattern
1311 * @doc: the containing document
1312 * @node: the containing element
1314 * Compile the XSLT pattern and generates a list of precompiled form suitable
1315 * for fast matching.
1317 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1319 * Returns the generated pattern list or NULL in case of failure
1323 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc, xmlNodePtr node) {
1324 xsltParserContextPtr ctxt = NULL;
1325 xsltCompMatchPtr element, first = NULL, previous = NULL;
1326 int current, start, end;
1328 if (pattern == NULL) {
1329 xsltGenericError(xsltGenericErrorContext,
1330 "xsltCompilePattern : NULL pattern\n");
1334 #ifdef DEBUG_PATTERN
1335 xsltGenericDebug(xsltGenericDebugContext,
1336 "xsltCompilePattern : parsing '%s'\n", pattern);
1339 ctxt = xsltNewParserContext();
1345 while (pattern[current] != 0) {
1347 while (IS_BLANK(pattern[current]))
1350 while ((pattern[end] != 0) && (pattern[end] != '|'))
1352 if (current == end) {
1353 xsltGenericError(xsltGenericErrorContext,
1354 "xsltCompilePattern : NULL pattern\n");
1357 element = xsltNewCompMatch();
1358 if (element == NULL) {
1363 else if (previous != NULL)
1364 previous->next = element;
1367 ctxt->comp = element;
1368 ctxt->base = xmlStrndup(&pattern[start], end - start);
1369 ctxt->cur = &(ctxt->base)[current - start];
1370 xsltCompileLocationPathPattern(ctxt);
1372 xmlFree((xmlChar *)ctxt->base);
1377 * Reverse for faster interpretation.
1379 xsltReverseCompMatch(element);
1382 * Set-up the priority
1384 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1385 (element->steps[0].op == XSLT_OP_ATTR)) &&
1386 (element->steps[0].value != NULL) &&
1387 (element->steps[1].op == XSLT_OP_END)) {
1388 element->priority = 0;
1389 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1390 (element->steps[1].op == XSLT_OP_END)) {
1391 element->priority = 0;
1392 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1393 (element->steps[0].value != NULL) &&
1394 (element->steps[1].op == XSLT_OP_END)) {
1395 element->priority = 0;
1396 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1397 (element->steps[0].value != NULL) &&
1398 (element->steps[1].op == XSLT_OP_END)) {
1399 element->priority = -0.25;
1400 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1401 (element->steps[0].op == XSLT_OP_TEXT) ||
1402 (element->steps[0].op == XSLT_OP_ALL) ||
1403 (element->steps[0].op == XSLT_OP_NODE) ||
1404 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1405 (element->steps[1].op == XSLT_OP_END)) {
1406 element->priority = -0.5;
1408 element->priority = 0.5;
1410 if (pattern[end] == '|')
1415 xsltGenericError(xsltGenericErrorContext,
1416 "xsltCompilePattern : NULL pattern\n");
1420 xsltFreeParserContext(ctxt);
1425 xsltFreeParserContext(ctxt);
1427 xsltFreeCompMatchList(first);
1431 /************************************************************************
1433 * Module interfaces *
1435 ************************************************************************/
1439 * @style: an XSLT stylesheet
1440 * @cur: an XSLT template
1441 * @mode: the mode name or NULL
1442 * @modeURI: the mode URI or NULL
1444 * Register the XSLT pattern associated to @cur
1446 * Returns -1 in case of error, 0 otherwise
1449 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1450 const xmlChar *mode, const xmlChar *modeURI) {
1451 xsltCompMatchPtr pat, list, *top = NULL, next;
1452 const xmlChar *name = NULL;
1454 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1457 pat = xsltCompilePattern(cur->match, style->doc, cur->elem);
1462 pat->template = cur;
1464 pat->mode = xmlStrdup(mode);
1465 if (modeURI != NULL)
1466 pat->modeURI = xmlStrdup(modeURI);
1467 if (cur->priority == XSLT_PAT_NO_PRIORITY)
1468 cur->priority = pat->priority;
1470 pat->priority = cur->priority;
1473 * insert it in the hash table list corresponding to its lookup name
1475 switch (pat->steps[0].op) {
1477 if (pat->steps[0].value != NULL)
1478 name = pat->steps[0].value;
1480 top = (xsltCompMatchPtr *) &(style->attrMatch);
1484 case XSLT_OP_PARENT:
1485 case XSLT_OP_ANCESTOR:
1487 name = pat->steps[0].value;
1490 top = (xsltCompMatchPtr *) &(style->rootMatch);
1493 top = (xsltCompMatchPtr *) &(style->keyMatch);
1496 /* TODO optimize ID !!! */
1498 top = (xsltCompMatchPtr *) &(style->elemMatch);
1501 case XSLT_OP_PREDICATE:
1502 xsltGenericError(xsltGenericErrorContext,
1503 "xsltAddTemplate: invalid compiled pattern\n");
1504 xsltFreeCompMatch(pat);
1507 * TODO: some flags at the top level about type based patterns
1508 * would be faster than inclusion in the hash table.
1511 if (pat->steps[0].value != NULL)
1512 name = pat->steps[0].value;
1514 top = (xsltCompMatchPtr *) &(style->piMatch);
1516 case XSLT_OP_COMMENT:
1517 top = (xsltCompMatchPtr *) &(style->commentMatch);
1520 top = (xsltCompMatchPtr *) &(style->textMatch);
1523 if (pat->steps[0].value != NULL)
1524 name = pat->steps[0].value;
1526 top = (xsltCompMatchPtr *) &(style->elemMatch);
1531 if (style->templatesHash == NULL) {
1532 style->templatesHash = xmlHashCreate(1024);
1533 if (style->templatesHash == NULL) {
1534 xsltFreeCompMatch(pat);
1537 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1539 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1540 name, mode, modeURI);
1542 xmlHashAddEntry3(style->templatesHash, name,
1543 mode, modeURI, pat);
1546 * Note '<=' since one must choose among the matching
1547 * template rules that are left, the one that occurs
1548 * last in the stylesheet
1550 if (list->priority <= pat->priority) {
1552 xmlHashUpdateEntry3(style->templatesHash, name,
1553 mode, modeURI, pat, NULL);
1555 while (list->next != NULL) {
1556 if (list->next->priority <= pat->priority)
1560 pat->next = list->next;
1565 } else if (top != NULL) {
1570 } else if (list->priority <= pat->priority) {
1574 while (list->next != NULL) {
1575 if (list->next->priority <= pat->priority)
1579 pat->next = list->next;
1583 xsltGenericError(xsltGenericErrorContext,
1584 "xsltAddTemplate: invalid compiled pattern\n");
1585 xsltFreeCompMatch(pat);
1588 #ifdef DEBUG_PATTERN
1590 xsltGenericDebug(xsltGenericDebugContext,
1591 "added pattern : '%s' mode '%s' priority %f\n",
1592 pat->template->match, pat->mode, pat->priority);
1594 xsltGenericDebug(xsltGenericDebugContext,
1595 "added pattern : '%s' priority %f\n",
1596 pat->template->match, pat->priority);
1606 * @ctxt: a XSLT process context
1608 * @style: the current style
1610 * Finds the template applying to this node, if @style is non-NULL
1611 * it means one need to look for the next imported template in scope.
1613 * Returns the xsltTemplatePtr or NULL if not found
1616 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
1617 xsltStylesheetPtr style) {
1618 xsltStylesheetPtr curstyle;
1619 xsltTemplatePtr ret = NULL;
1620 const xmlChar *name = NULL;
1621 xsltCompMatchPtr list = NULL;
1623 if ((ctxt == NULL) || (node == NULL))
1626 if (style == NULL) {
1627 curstyle = ctxt->style;
1629 curstyle = xsltNextImport(style);
1632 while ((curstyle != NULL) && (curstyle != style)) {
1633 /* TODO : handle IDs/keys here ! */
1634 if (curstyle->templatesHash != NULL) {
1636 * Use the top name as selector
1638 switch (node->type) {
1639 case XML_ELEMENT_NODE:
1640 case XML_ATTRIBUTE_NODE:
1644 case XML_DOCUMENT_NODE:
1645 case XML_HTML_DOCUMENT_NODE:
1647 case XML_CDATA_SECTION_NODE:
1648 case XML_COMMENT_NODE:
1649 case XML_ENTITY_REF_NODE:
1650 case XML_ENTITY_NODE:
1651 case XML_DOCUMENT_TYPE_NODE:
1652 case XML_DOCUMENT_FRAG_NODE:
1653 case XML_NOTATION_NODE:
1655 case XML_ELEMENT_DECL:
1656 case XML_ATTRIBUTE_DECL:
1657 case XML_ENTITY_DECL:
1658 case XML_NAMESPACE_DECL:
1659 case XML_XINCLUDE_START:
1660 case XML_XINCLUDE_END:
1669 * find the list of appliable expressions based on the name
1671 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
1672 name, ctxt->mode, ctxt->modeURI);
1675 while (list != NULL) {
1676 if (xsltTestCompMatch(ctxt, list, node,
1677 ctxt->mode, ctxt->modeURI)) {
1678 ret = list->template;
1686 * find alternate generic matches
1688 switch (node->type) {
1689 case XML_ELEMENT_NODE:
1690 list = curstyle->elemMatch;
1692 case XML_ATTRIBUTE_NODE:
1693 list = curstyle->attrMatch;
1696 list = curstyle->piMatch;
1698 case XML_DOCUMENT_NODE:
1699 case XML_HTML_DOCUMENT_NODE:
1700 list = curstyle->rootMatch;
1703 case XML_CDATA_SECTION_NODE:
1704 list = curstyle->textMatch;
1706 case XML_COMMENT_NODE:
1707 list = curstyle->commentMatch;
1709 case XML_ENTITY_REF_NODE:
1710 case XML_ENTITY_NODE:
1711 case XML_DOCUMENT_TYPE_NODE:
1712 case XML_DOCUMENT_FRAG_NODE:
1713 case XML_NOTATION_NODE:
1715 case XML_ELEMENT_DECL:
1716 case XML_ATTRIBUTE_DECL:
1717 case XML_ENTITY_DECL:
1718 case XML_NAMESPACE_DECL:
1719 case XML_XINCLUDE_START:
1720 case XML_XINCLUDE_END:
1726 while ((list != NULL) &&
1727 ((ret == NULL) || (list->priority > ret->priority))) {
1728 if (xsltTestCompMatch(ctxt, list, node,
1729 ctxt->mode, ctxt->modeURI)) {
1730 ret = list->template;
1735 if (node->_private != NULL) {
1736 list = curstyle->keyMatch;
1737 while ((list != NULL) &&
1738 ((ret == NULL) || (list->priority > ret->priority))) {
1739 if (xsltTestCompMatch(ctxt, list, node,
1740 ctxt->mode, ctxt->modeURI)) {
1741 ret = list->template;
1751 * Cycle on next curstylesheet import.
1753 curstyle = xsltNextImport(curstyle);
1760 * xsltFreeTemplateHashes:
1761 * @style: an XSLT stylesheet
1763 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
1766 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
1767 if (style->templatesHash != NULL)
1768 xmlHashFree((xmlHashTablePtr) style->templatesHash,
1769 (xmlHashDeallocator) xsltFreeCompMatchList);
1770 if (style->rootMatch != NULL)
1771 xsltFreeCompMatchList(style->rootMatch);
1772 if (style->keyMatch != NULL)
1773 xsltFreeCompMatchList(style->keyMatch);
1774 if (style->elemMatch != NULL)
1775 xsltFreeCompMatchList(style->elemMatch);
1776 if (style->attrMatch != NULL)
1777 xsltFreeCompMatchList(style->attrMatch);
1778 if (style->parentMatch != NULL)
1779 xsltFreeCompMatchList(style->parentMatch);
1780 if (style->textMatch != NULL)
1781 xsltFreeCompMatchList(style->textMatch);
1782 if (style->piMatch != NULL)
1783 xsltFreeCompMatchList(style->piMatch);
1784 if (style->commentMatch != NULL)
1785 xsltFreeCompMatchList(style->commentMatch);
1791 * @node: a node in the source tree
1792 * @pattern: an XSLT pattern
1794 * Determine if a node matches a pattern.
1797 xsltMatchPattern(xsltTransformContextPtr context,
1799 const xmlChar *pattern)
1802 xsltCompMatchPtr first, comp;
1804 if ((context != NULL) && (pattern != NULL)) {
1805 first = xsltCompilePattern(pattern);
1806 for (comp = first; comp != NULL; comp = comp->next) {
1807 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
1812 xsltFreeCompMatchList(first);