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[40]; /* ops for computation */
95 typedef struct _xsltParserContext xsltParserContext;
96 typedef xsltParserContext *xsltParserContextPtr;
97 struct _xsltParserContext {
98 xsltStylesheetPtr style; /* the stylesheet */
99 xsltTransformContextPtr ctxt; /* the transformation or NULL */
100 const xmlChar *cur; /* the current char being parsed */
101 const xmlChar *base; /* the full expression */
102 xmlDocPtr doc; /* the source document */
103 xmlNodePtr elem; /* the source element */
104 int error; /* error code */
105 xsltCompMatchPtr comp; /* the result */
108 /************************************************************************
112 ************************************************************************/
117 * Create a new XSLT CompMatch
119 * Returns the newly allocated xsltCompMatchPtr or NULL in case of error
121 static xsltCompMatchPtr
122 xsltNewCompMatch(void) {
123 xsltCompMatchPtr cur;
125 cur = (xsltCompMatchPtr) xmlMalloc(sizeof(xsltCompMatch));
127 xsltPrintErrorContext(NULL, NULL, NULL);
128 xsltGenericError(xsltGenericErrorContext,
129 "xsltNewCompMatch : malloc failed\n");
132 memset(cur, 0, sizeof(xsltCompMatch));
141 * @comp: an XSLT comp
143 * Free up the memory allocated by @comp
146 xsltFreeCompMatch(xsltCompMatchPtr comp) {
152 if (comp->pattern != NULL)
153 xmlFree((xmlChar *)comp->pattern);
154 if (comp->mode != NULL)
155 xmlFree((xmlChar *)comp->mode);
156 if (comp->modeURI != NULL)
157 xmlFree((xmlChar *)comp->modeURI);
158 if (comp->nsList != NULL)
159 xmlFree(comp->nsList);
160 for (i = 0;i < comp->nbStep;i++) {
161 op = &comp->steps[i];
162 if (op->value != NULL)
164 if (op->value2 != NULL)
166 if (op->value3 != NULL)
168 if (op->comp != NULL)
169 xmlXPathFreeCompExpr(op->comp);
171 memset(comp, -1, sizeof(xsltCompMatch));
176 * xsltFreeCompMatchList:
177 * @comp: an XSLT comp list
179 * Free up the memory allocated by all the elements of @comp
182 xsltFreeCompMatchList(xsltCompMatchPtr comp) {
183 xsltCompMatchPtr cur;
185 while (comp != NULL) {
188 xsltFreeCompMatch(cur);
193 * xsltNewParserContext:
194 * @style: the stylesheet
195 * @ctxt: the transformation context, if done at run-time
197 * Create a new XSLT ParserContext
199 * Returns the newly allocated xsltParserContextPtr or NULL in case of error
201 static xsltParserContextPtr
202 xsltNewParserContext(xsltStylesheetPtr style, xsltTransformContextPtr ctxt) {
203 xsltParserContextPtr cur;
205 cur = (xsltParserContextPtr) xmlMalloc(sizeof(xsltParserContext));
207 xsltPrintErrorContext(NULL, NULL, NULL);
208 xsltGenericError(xsltGenericErrorContext,
209 "xsltNewParserContext : malloc failed\n");
212 memset(cur, 0, sizeof(xsltParserContext));
219 * xsltFreeParserContext:
220 * @ctxt: an XSLT parser context
222 * Free up the memory allocated by @ctxt
225 xsltFreeParserContext(xsltParserContextPtr ctxt) {
228 memset(ctxt, -1, sizeof(xsltParserContext));
234 * @comp: the compiled match expression
236 * @value: the first value
237 * @value2: the second value
239 * Add an step to an XSLT Compiled Match
241 * Returns -1 in case of failure, 0 otherwise.
244 xsltCompMatchAdd(xsltParserContextPtr ctxt, xsltCompMatchPtr comp,
245 xsltOp op, xmlChar * value, xmlChar * value2)
247 if (comp->nbStep >= 40) {
248 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
249 xsltGenericError(xsltGenericErrorContext,
250 "xsltCompMatchAdd: overflow\n");
253 comp->steps[comp->nbStep].op = op;
254 comp->steps[comp->nbStep].value = value;
255 comp->steps[comp->nbStep].value2 = value2;
256 if (ctxt->ctxt != NULL) {
257 comp->steps[comp->nbStep].previousExtra =
258 xsltAllocateExtraCtxt(ctxt->ctxt);
259 comp->steps[comp->nbStep].indexExtra =
260 xsltAllocateExtraCtxt(ctxt->ctxt);
261 comp->steps[comp->nbStep].lenExtra =
262 xsltAllocateExtraCtxt(ctxt->ctxt);
264 comp->steps[comp->nbStep].previousExtra =
265 xsltAllocateExtra(ctxt->style);
266 comp->steps[comp->nbStep].indexExtra =
267 xsltAllocateExtra(ctxt->style);
268 comp->steps[comp->nbStep].lenExtra =
269 xsltAllocateExtra(ctxt->style);
271 if (op == XSLT_OP_PREDICATE) {
272 comp->steps[comp->nbStep].comp = xmlXPathCompile(value);
279 * xsltSwapTopCompMatch:
280 * @comp: the compiled match expression
282 * reverse the two top steps.
285 xsltSwapTopCompMatch(xsltCompMatchPtr comp) {
287 int j = comp->nbStep - 1;
290 register xmlChar *tmp;
292 register xmlXPathCompExprPtr expr;
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;
303 expr = comp->steps[i].comp;
304 comp->steps[i].comp = comp->steps[j].comp;
305 comp->steps[j].comp = expr;
310 * xsltReverseCompMatch:
311 * @comp: the compiled match expression
313 * reverse all the stack of expressions
316 xsltReverseCompMatch(xsltCompMatchPtr comp) {
318 int j = comp->nbStep - 1;
321 register xmlChar *tmp;
323 register xmlXPathCompExprPtr expr;
324 tmp = comp->steps[i].value;
325 comp->steps[i].value = comp->steps[j].value;
326 comp->steps[j].value = tmp;
327 tmp = comp->steps[i].value2;
328 comp->steps[i].value2 = comp->steps[j].value2;
329 comp->steps[j].value2 = tmp;
330 op = comp->steps[i].op;
331 comp->steps[i].op = comp->steps[j].op;
332 comp->steps[j].op = op;
333 expr = comp->steps[i].comp;
334 comp->steps[i].comp = comp->steps[j].comp;
335 comp->steps[j].comp = expr;
339 comp->steps[comp->nbStep++].op = XSLT_OP_END;
342 /************************************************************************
344 * The interpreter for the precompiled patterns *
346 ************************************************************************/
350 * @ctxt: a XSLT process context
351 * @comp: the precompiled pattern
353 * @mode: the mode name or NULL
354 * @modeURI: the mode URI or NULL
356 * Test wether the node matches the pattern
358 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
361 xsltTestCompMatch(xsltTransformContextPtr ctxt, xsltCompMatchPtr comp,
362 xmlNodePtr node, const xmlChar *mode,
363 const xmlChar *modeURI) {
365 xsltStepOpPtr step, select = NULL;
367 if ((comp == NULL) || (node == NULL) || (ctxt == NULL)) {
368 xsltPrintErrorContext(ctxt, NULL, node);
369 xsltGenericError(xsltGenericErrorContext,
370 "xsltTestCompMatch: null arg\n");
374 if (comp->mode == NULL)
376 if ((comp->mode != mode) && (!xmlStrEqual(comp->mode, mode)))
379 if (comp->mode != NULL)
382 if (modeURI != NULL) {
383 if (comp->modeURI == NULL)
385 if ((comp->modeURI != modeURI) &&
386 (!xmlStrEqual(comp->modeURI, modeURI)))
389 if (comp->modeURI != NULL)
392 for (i = 0;i < comp->nbStep;i++) {
393 step = &comp->steps[i];
394 if (step->op != XSLT_OP_PREDICATE)
400 if ((node->type == XML_DOCUMENT_NODE) ||
401 #ifdef LIBXML_DOCB_ENABLED
402 (node->type == XML_DOCB_DOCUMENT_NODE) ||
404 (node->type == XML_HTML_DOCUMENT_NODE))
408 if (node->type != XML_ELEMENT_NODE)
410 if (step->value == NULL)
412 if (!xmlStrEqual(step->value, node->name))
416 if (node->ns == NULL) {
417 if (step->value2 != NULL)
419 } else if (node->ns->href != NULL) {
420 if (step->value2 == NULL)
422 if (!xmlStrEqual(step->value2, node->ns->href))
426 case XSLT_OP_CHILD: {
429 if ((node->type != XML_ELEMENT_NODE) &&
430 (node->type != XML_DOCUMENT_NODE) &&
431 #ifdef LIBXML_DOCB_ENABLED
432 (node->type != XML_DOCB_DOCUMENT_NODE) &&
434 (node->type != XML_HTML_DOCUMENT_NODE))
437 lst = node->children;
439 if (step->value != NULL) {
440 while (lst != NULL) {
441 if ((lst->type == XML_ELEMENT_NODE) &&
442 (xmlStrEqual(step->value, lst->name)))
452 if (node->type != XML_ATTRIBUTE_NODE)
454 if (step->value == NULL)
456 if (!xmlStrEqual(step->value, node->name))
460 if (node->ns == NULL) {
461 if (step->value2 != NULL)
463 } else if (node->ns->href != NULL) {
464 if (step->value2 == NULL)
466 if (!xmlStrEqual(step->value2, node->ns->href))
471 if ((node->type == XML_DOCUMENT_NODE) ||
472 (node->type == XML_HTML_DOCUMENT_NODE) ||
473 #ifdef LIBXML_DOCB_ENABLED
474 (node->type == XML_DOCB_DOCUMENT_NODE) ||
476 (node->type == XML_NAMESPACE_DECL))
481 if (step->value == NULL)
483 if (!xmlStrEqual(step->value, node->name))
486 if (node->ns == NULL) {
487 if (step->value2 != NULL)
489 } else if (node->ns->href != NULL) {
490 if (step->value2 == NULL)
492 if (!xmlStrEqual(step->value2, node->ns->href))
496 case XSLT_OP_ANCESTOR:
497 /* TODO: implement coalescing of ANCESTOR/NODE ops */
498 if (step->value == NULL) {
500 step = &comp->steps[i];
501 if (step->op == XSLT_OP_ROOT)
503 if (step->op != XSLT_OP_ELEM)
505 if (step->value == NULL)
510 if ((node->type == XML_DOCUMENT_NODE) ||
511 (node->type == XML_HTML_DOCUMENT_NODE) ||
512 #ifdef LIBXML_DOCB_ENABLED
513 (node->type == XML_DOCB_DOCUMENT_NODE) ||
515 (node->type == XML_NAMESPACE_DECL))
518 while (node != NULL) {
521 if (xmlStrEqual(step->value, node->name)) {
523 if (node->ns == NULL) {
524 if (step->value2 == NULL)
526 } else if (node->ns->href != NULL) {
527 if ((step->value2 != NULL) &&
528 (xmlStrEqual(step->value2, node->ns->href)))
538 /* TODO Handle IDs decently, must be done differently */
541 if (node->type != XML_ELEMENT_NODE)
544 id = xmlGetID(node->doc, step->value);
545 if ((id == NULL) || (id->parent != node))
553 list = xsltGetKey(ctxt, step->value,
554 step->value3, step->value2);
557 for (indx = 0;indx < list->nodeNr;indx++)
558 if (list->nodeTab[indx] == node)
560 if (indx >= list->nodeNr)
565 if (node->type != XML_ELEMENT_NODE)
567 if (node->ns == NULL) {
568 if (step->value != NULL)
570 } else if (node->ns->href != NULL) {
571 if (step->value == NULL)
573 if (!xmlStrEqual(step->value, node->ns->href))
578 if (node->type != XML_ELEMENT_NODE)
581 case XSLT_OP_PREDICATE: {
584 int pos = 0, len = 0;
586 * The simple existing predicate code cannot handle
587 * properly cascaded predicates. If in this situation
588 * compute directly the full node list once and check
589 * if the node is in the result list.
591 if (comp->steps[i + 1].op == XSLT_OP_PREDICATE) {
593 xmlXPathObjectPtr list;
596 previous = (xmlNodePtr)
597 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
599 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
600 list = (xmlXPathObjectPtr)
601 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
605 if (comp->pattern[0] == '/')
606 query = xmlStrdup(comp->pattern);
608 query = xmlStrdup((const xmlChar *)"//");
609 query = xmlStrcat(query, comp->pattern);
611 list = xmlXPathEval(query, ctxt->xpathCtxt);
615 if (list->type != XPATH_NODESET) {
616 xmlXPathFreeObject(list);
619 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
621 XSLT_RUNTIME_EXTRA_FREE(ctxt, select->lenExtra) =
622 (xmlFreeFunc) xmlXPathFreeObject;
624 if ((list->nodesetval == NULL) ||
625 (list->nodesetval->nodeNr <= 0))
628 for (j = 0;j < list->nodesetval->nodeNr;j++) {
629 if (list->nodesetval->nodeTab[j] == node) {
638 * Depending on the last selection, one may need to
639 * recompute contextSize and proximityPosition.
641 * TODO: make this thread safe !
643 oldCS = ctxt->xpathCtxt->contextSize;
644 oldCP = ctxt->xpathCtxt->proximityPosition;
645 if ((select != NULL) &&
646 (select->op == XSLT_OP_ELEM) &&
647 (select->value != NULL) &&
648 (node->type == XML_ELEMENT_NODE) &&
649 (node->parent != NULL)) {
651 int index, nocache = 0;
653 previous = (xmlNodePtr)
654 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
656 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
657 if ((previous != NULL) &&
658 (previous->parent == node->parent)) {
660 * just walk back to adjust the index
663 xmlNodePtr sibling = node;
665 while (sibling != NULL) {
666 if (sibling == previous)
668 if (xmlStrEqual(node->name, sibling->name)) {
669 if ((select->value2 == NULL) ||
670 ((sibling->ns != NULL) &&
671 (xmlStrEqual(select->value2,
672 sibling->ns->href))))
675 sibling = sibling->prev;
677 if (sibling == NULL) {
678 /* hum going backward in document order ... */
681 while (sibling != NULL) {
682 if (sibling == previous)
684 if ((select->value2 == NULL) ||
685 ((sibling->ns != NULL) &&
686 (xmlStrEqual(select->value2,
687 sibling->ns->href))))
689 sibling = sibling->next;
692 if (sibling != NULL) {
695 * If the node is in a Value Tree we cannot
698 if (node->doc != NULL) {
700 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
701 XSLT_RUNTIME_EXTRA(ctxt,
702 select->previousExtra) = node;
703 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
711 * recompute the index
713 xmlNodePtr siblings = node->parent->children;
714 xmlNodePtr parent = node->parent;
716 while (siblings != NULL) {
717 if (siblings->type == XML_ELEMENT_NODE) {
718 if (siblings == node) {
721 } else if (xmlStrEqual(node->name,
723 if ((select->value2 == NULL) ||
724 ((siblings->ns != NULL) &&
725 (xmlStrEqual(select->value2,
726 siblings->ns->href))))
730 siblings = siblings->next;
732 if ((parent == NULL) || (node->doc == NULL))
735 while (parent->parent != NULL)
736 parent = parent->parent;
737 if (((parent->type != XML_DOCUMENT_NODE) &&
738 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
739 (parent != (xmlNodePtr) node->doc))
744 ctxt->xpathCtxt->contextSize = len;
745 ctxt->xpathCtxt->proximityPosition = pos;
747 * If the node is in a Value Tree we cannot
750 if ((node->doc != NULL) && (nocache == 0)) {
751 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
753 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
755 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
759 } else if ((select != NULL) && (select->op == XSLT_OP_ALL) &&
760 (node->type == XML_ELEMENT_NODE)) {
764 previous = (xmlNodePtr)
765 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra);
767 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra);
768 if ((previous != NULL) &&
769 (previous->parent == node->parent)) {
771 * just walk back to adjust the index
774 xmlNodePtr sibling = node;
776 while (sibling != NULL) {
777 if (sibling == previous)
779 if (sibling->type == XML_ELEMENT_NODE)
781 sibling = sibling->prev;
783 if (sibling == NULL) {
784 /* hum going backward in document order ... */
787 while (sibling != NULL) {
788 if (sibling == previous)
790 if (sibling->type == XML_ELEMENT_NODE)
792 sibling = sibling->next;
795 if (sibling != NULL) {
798 * If the node is in a Value Tree we cannot
801 if (node->doc != NULL) {
803 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra);
804 XSLT_RUNTIME_EXTRA(ctxt,
805 select->previousExtra) = node;
806 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
813 * recompute the index
815 xmlNodePtr siblings = node->parent->children;
816 xmlNodePtr parent = node->parent;
818 while (siblings != NULL) {
819 if (siblings->type == XML_ELEMENT_NODE) {
821 if (siblings == node) {
825 siblings = siblings->next;
827 if ((parent == NULL) || (node->doc == NULL))
830 while (parent->parent != NULL)
831 parent = parent->parent;
832 if (((parent->type != XML_DOCUMENT_NODE) &&
833 (parent->type != XML_HTML_DOCUMENT_NODE)) ||
834 (parent != (xmlNodePtr) node->doc))
839 ctxt->xpathCtxt->contextSize = len;
840 ctxt->xpathCtxt->proximityPosition = pos;
842 * If the node is in a Value Tree we cannot
845 if ((node->doc != NULL) && (nocache == 0)) {
846 XSLT_RUNTIME_EXTRA(ctxt, select->previousExtra) =
848 XSLT_RUNTIME_EXTRA(ctxt, select->indexExtra) =
850 XSLT_RUNTIME_EXTRA(ctxt, select->lenExtra) =
855 oldNode = ctxt->node;
858 if (step->value == NULL)
860 if (step->comp == NULL)
863 if (!xsltEvalXPathPredicate(ctxt, step->comp, comp->nsList,
868 ctxt->xpathCtxt->contextSize = oldCS;
869 ctxt->xpathCtxt->proximityPosition = oldCP;
871 ctxt->node = oldNode;
875 ctxt->xpathCtxt->contextSize = oldCS;
876 ctxt->xpathCtxt->proximityPosition = oldCP;
878 ctxt->node = oldNode;
882 if (node->type != XML_PI_NODE)
884 if (step->value != NULL) {
885 if (!xmlStrEqual(step->value, node->name))
889 case XSLT_OP_COMMENT:
890 if (node->type != XML_COMMENT_NODE)
894 if ((node->type != XML_TEXT_NODE) &&
895 (node->type != XML_CDATA_SECTION_NODE))
899 switch (node->type) {
900 case XML_DOCUMENT_NODE:
901 case XML_HTML_DOCUMENT_NODE:
902 #ifdef LIBXML_DOCB_ENABLED
903 case XML_DOCB_DOCUMENT_NODE:
905 case XML_ELEMENT_NODE:
906 case XML_CDATA_SECTION_NODE:
908 case XML_COMMENT_NODE:
910 case XML_ATTRIBUTE_NODE:
922 * xsltTestCompMatchList:
923 * @ctxt: a XSLT process context
925 * @comp: the precompiled pattern list
927 * Test wether the node matches one of the patterns in the list
929 * Returns 1 if it matches, 0 if it doesn't and -1 in case of failure
932 xsltTestCompMatchList(xsltTransformContextPtr ctxt, xmlNodePtr node,
933 xsltCompMatchPtr comp) {
936 if ((ctxt == NULL) || (node == NULL))
938 while (comp != NULL) {
939 ret = xsltTestCompMatch(ctxt, comp, node, NULL, NULL);
947 /************************************************************************
949 * Dedicated parser for templates *
951 ************************************************************************/
953 #define CUR (*ctxt->cur)
954 #define SKIP(val) ctxt->cur += (val)
955 #define NXT(val) ctxt->cur[(val)]
956 #define CUR_PTR ctxt->cur
958 #define SKIP_BLANKS \
959 while (IS_BLANK(CUR)) NEXT
961 #define CURRENT (*ctxt->cur)
962 #define NEXT ((*ctxt->cur) ? ctxt->cur++: ctxt->cur)
965 #define PUSH(op, val, val2) \
966 if (xsltCompMatchAdd(ctxt, ctxt->comp, (op), (val), (val2))) goto error;
969 xsltSwapTopCompMatch(ctxt->comp);
971 #define XSLT_ERROR(X) \
972 { xsltError(ctxt, __FILE__, __LINE__, X); \
973 ctxt->error = (X); return; }
975 #define XSLT_ERROR0(X) \
976 { xsltError(ctxt, __FILE__, __LINE__, X); \
977 ctxt->error = (X); return(0); }
981 * @ctxt: the XPath Parser context
983 * Parse an XPath Litteral:
985 * [29] Literal ::= '"' [^"]* '"'
988 * Returns the Literal parsed or NULL
992 xsltScanLiteral(xsltParserContextPtr ctxt) {
993 const xmlChar *q, *cur;
1001 val = xmlStringCurrentChar(NULL, cur, &len);
1002 while ((IS_CHAR(val)) && (val != '"')) {
1004 val = xmlStringCurrentChar(NULL, cur, &len);
1006 if (!IS_CHAR(val)) {
1010 ret = xmlStrndup(q, cur - q);
1014 } else if (CUR == '\'') {
1017 val = xmlStringCurrentChar(NULL, cur, &len);
1018 while ((IS_CHAR(val)) && (val != '\'')) {
1020 val = xmlStringCurrentChar(NULL, cur, &len);
1022 if (!IS_CHAR(val)) {
1026 ret = xmlStrndup(q, cur - q);
1031 /* XP_ERROR(XPATH_START_LITERAL_ERROR); */
1040 * @ctxt: the XPath Parser context
1042 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' |
1043 * CombiningChar | Extender
1045 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
1047 * [6] Names ::= Name (S Name)*
1049 * Returns the Name parsed or NULL
1053 xsltScanName(xsltParserContextPtr ctxt) {
1054 const xmlChar *q, *cur;
1055 xmlChar *ret = NULL;
1061 val = xmlStringCurrentChar(NULL, cur, &len);
1062 if (!IS_LETTER(val) && (val != '_') && (val != ':'))
1065 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1066 (val == '.') || (val == '-') ||
1068 (IS_COMBINING(val)) ||
1069 (IS_EXTENDER(val))) {
1071 val = xmlStringCurrentChar(NULL, cur, &len);
1073 ret = xmlStrndup(q, cur - q);
1080 * @ctxt: the XPath Parser context
1082 * Parses a non qualified name
1084 * Returns the Name parsed or NULL
1088 xsltScanNCName(xsltParserContextPtr ctxt) {
1089 const xmlChar *q, *cur;
1090 xmlChar *ret = NULL;
1096 val = xmlStringCurrentChar(NULL, cur, &len);
1097 if (!IS_LETTER(val) && (val != '_'))
1100 while ((IS_LETTER(val)) || (IS_DIGIT(val)) ||
1101 (val == '.') || (val == '-') ||
1103 (IS_COMBINING(val)) ||
1104 (IS_EXTENDER(val))) {
1106 val = xmlStringCurrentChar(NULL, cur, &len);
1108 ret = xmlStrndup(q, cur - q);
1115 * @ctxt: the XPath Parser context
1116 * @prefix: the place to store the prefix
1118 * Parse a qualified name
1120 * Returns the Name parsed or NULL
1124 xsltScanQName(xsltParserContextPtr ctxt, xmlChar **prefix) {
1125 xmlChar *ret = NULL;
1128 ret = xsltScanNCName(ctxt);
1132 ret = xsltScanNCName(ctxt);
1138 * xsltCompileIdKeyPattern:
1139 * @ctxt: the compilation context
1140 * @name: a preparsed name
1141 * @aid: whether id/key are allowed there
1143 * Compile the XSLT LocationIdKeyPattern
1144 * [3] IdKeyPattern ::= 'id' '(' Literal ')'
1145 * | 'key' '(' Literal ',' Literal ')'
1147 * also handle NodeType and PI from:
1149 * [7] NodeTest ::= NameTest
1150 * | NodeType '(' ')'
1151 * | 'processing-instruction' '(' Literal ')'
1154 xsltCompileIdKeyPattern(xsltParserContextPtr ctxt, xmlChar *name, int aid) {
1155 xmlChar *lit = NULL;
1156 xmlChar *lit2 = NULL;
1159 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1160 xsltGenericError(xsltGenericErrorContext,
1161 "xsltCompileIdKeyPattern : ( expected\n");
1165 if ((aid) && (xmlStrEqual(name, (const xmlChar *)"id"))) {
1168 lit = xsltScanLiteral(ctxt);
1173 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1174 xsltGenericError(xsltGenericErrorContext,
1175 "xsltCompileIdKeyPattern : ) expected\n");
1180 PUSH(XSLT_OP_ID, lit, NULL);
1181 } else if ((aid) && (xmlStrEqual(name, (const xmlChar *)"key"))) {
1184 lit = xsltScanLiteral(ctxt);
1189 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1190 xsltGenericError(xsltGenericErrorContext,
1191 "xsltCompileIdKeyPattern : , expected\n");
1197 lit2 = xsltScanLiteral(ctxt);
1202 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1203 xsltGenericError(xsltGenericErrorContext,
1204 "xsltCompileIdKeyPattern : ) expected\n");
1209 /* TODO: support namespace in keys */
1210 PUSH(XSLT_OP_KEY, lit, lit2);
1211 } else if (xmlStrEqual(name, (const xmlChar *)"processing-instruction")) {
1215 lit = xsltScanLiteral(ctxt);
1220 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1221 xsltGenericError(xsltGenericErrorContext,
1222 "xsltCompileIdKeyPattern : ) expected\n");
1228 PUSH(XSLT_OP_PI, lit, NULL);
1229 } else if (xmlStrEqual(name, (const xmlChar *)"text")) {
1233 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1234 xsltGenericError(xsltGenericErrorContext,
1235 "xsltCompileIdKeyPattern : ) expected\n");
1240 PUSH(XSLT_OP_TEXT, NULL, NULL);
1241 } else if (xmlStrEqual(name, (const xmlChar *)"comment")) {
1245 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1246 xsltGenericError(xsltGenericErrorContext,
1247 "xsltCompileIdKeyPattern : ) expected\n");
1252 PUSH(XSLT_OP_COMMENT, NULL, NULL);
1253 } else if (xmlStrEqual(name, (const xmlChar *)"node")) {
1257 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1258 xsltGenericError(xsltGenericErrorContext,
1259 "xsltCompileIdKeyPattern : ) expected\n");
1264 PUSH(XSLT_OP_NODE, NULL, NULL);
1266 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1267 xsltGenericError(xsltGenericErrorContext,
1268 "xsltCompileIdKeyPattern : expecting 'key' or 'id' or node type\n");
1272 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1273 xsltGenericError(xsltGenericErrorContext,
1274 "xsltCompileIdKeyPattern : node type\n");
1284 * xsltCompileStepPattern:
1285 * @ctxt: the compilation context
1286 * @token: a posible precompiled name
1288 * Compile the XSLT StepPattern and generates a precompiled
1289 * form suitable for fast matching.
1291 * [5] StepPattern ::= ChildOrAttributeAxisSpecifier NodeTest Predicate*
1292 * [6] ChildOrAttributeAxisSpecifier ::= AbbreviatedAxisSpecifier
1293 * | ('child' | 'attribute') '::'
1295 * [7] NodeTest ::= NameTest
1296 * | NodeType '(' ')'
1297 * | 'processing-instruction' '(' Literal ')'
1298 * [8] Predicate ::= '[' PredicateExpr ']'
1299 * [9] PredicateExpr ::= Expr
1300 * [13] AbbreviatedAxisSpecifier ::= '@'?
1301 * [37] NameTest ::= '*' | NCName ':' '*' | QName
1305 xsltCompileStepPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1306 xmlChar *name = NULL;
1307 const xmlChar *URI = NULL;
1308 xmlChar *URL = NULL;
1312 if ((token == NULL) && (CUR == '@')) {
1313 xmlChar *prefix = NULL;
1318 PUSH(XSLT_OP_ATTR, NULL, NULL);
1321 token = xsltScanQName(ctxt, &prefix);
1322 if (prefix != NULL) {
1325 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1327 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1328 xsltGenericError(xsltGenericErrorContext,
1329 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1332 URL = xmlStrdup(ns->href);
1336 if (token == NULL) {
1339 PUSH(XSLT_OP_ATTR, NULL, URL);
1342 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1343 xsltGenericError(xsltGenericErrorContext,
1344 "xsltCompileStepPattern : Name expected\n");
1348 PUSH(XSLT_OP_ATTR, token, URL);
1352 token = xsltScanName(ctxt);
1353 if (token == NULL) {
1356 PUSH(XSLT_OP_ALL, token, NULL);
1357 goto parse_predicate;
1359 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1360 xsltGenericError(xsltGenericErrorContext,
1361 "xsltCompileStepPattern : Name expected\n");
1370 xsltCompileIdKeyPattern(ctxt, token, 0);
1373 } else if (CUR == ':') {
1376 xmlChar *prefix = token;
1380 * This is a namespace match
1382 token = xsltScanName(ctxt);
1383 ns = xmlSearchNs(ctxt->doc, ctxt->elem, prefix);
1385 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1386 xsltGenericError(xsltGenericErrorContext,
1387 "xsltCompileStepPattern : no namespace bound to prefix %s\n",
1392 URL = xmlStrdup(ns->href);
1395 if (token == NULL) {
1398 PUSH(XSLT_OP_NS, URL, NULL);
1400 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1401 xsltGenericError(xsltGenericErrorContext,
1402 "xsltCompileStepPattern : Name expected\n");
1407 PUSH(XSLT_OP_ELEM, token, URL);
1411 if (xmlStrEqual(token, (const xmlChar *) "child")) {
1413 token = xsltScanName(ctxt);
1414 if (token == NULL) {
1417 PUSH(XSLT_OP_ALL, token, NULL);
1418 goto parse_predicate;
1420 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1421 xsltGenericError(xsltGenericErrorContext,
1422 "xsltCompileStepPattern : QName expected\n");
1427 URI = xsltGetQNameURI(ctxt->elem, &token);
1428 if (token == NULL) {
1432 name = xmlStrdup(token);
1434 URL = xmlStrdup(URI);
1436 PUSH(XSLT_OP_CHILD, name, URL);
1437 } else if (xmlStrEqual(token, (const xmlChar *) "attribute")) {
1439 token = xsltScanName(ctxt);
1440 if (token == NULL) {
1441 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1442 xsltGenericError(xsltGenericErrorContext,
1443 "xsltCompileStepPattern : QName expected\n");
1447 URI = xsltGetQNameURI(ctxt->elem, &token);
1448 if (token == NULL) {
1452 name = xmlStrdup(token);
1454 URL = xmlStrdup(URI);
1456 PUSH(XSLT_OP_ATTR, name, URL);
1458 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1459 xsltGenericError(xsltGenericErrorContext,
1460 "xsltCompileStepPattern : 'child' or 'attribute' expected\n");
1466 } else if (CUR == '*') {
1468 PUSH(XSLT_OP_ALL, token, NULL);
1470 URI = xsltGetQNameURI(ctxt->elem, &token);
1471 if (token == NULL) {
1476 URL = xmlStrdup(URI);
1477 PUSH(XSLT_OP_ELEM, token, URL);
1482 while (CUR == '[') {
1484 xmlChar *ret = NULL;
1490 /* Skip over nested predicates */
1493 else if (CUR == ']') {
1497 } else if (CUR == '"') {
1499 while ((CUR != 0) && (CUR != '"'))
1501 } else if (CUR == '\'') {
1503 while ((CUR != 0) && (CUR != '\''))
1509 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1510 xsltGenericError(xsltGenericErrorContext,
1511 "xsltCompileStepPattern : ']' expected\n");
1515 ret = xmlStrndup(q, CUR_PTR - q);
1516 PUSH(XSLT_OP_PREDICATE, ret, NULL);
1517 /* push the predicate lower than local test */
1531 * xsltCompileRelativePathPattern:
1532 * @comp: the compilation context
1533 * @token: a posible precompiled name
1535 * Compile the XSLT RelativePathPattern and generates a precompiled
1536 * form suitable for fast matching.
1538 * [4] RelativePathPattern ::= StepPattern
1539 * | RelativePathPattern '/' StepPattern
1540 * | RelativePathPattern '//' StepPattern
1543 xsltCompileRelativePathPattern(xsltParserContextPtr ctxt, xmlChar *token) {
1544 xsltCompileStepPattern(ctxt, token);
1548 while ((CUR != 0) && (CUR != '|')) {
1549 if ((CUR == '/') && (NXT(1) == '/')) {
1550 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1554 xsltCompileStepPattern(ctxt, NULL);
1555 } else if (CUR == '/') {
1556 PUSH(XSLT_OP_PARENT, NULL, NULL);
1559 if ((CUR != 0) || (CUR == '|')) {
1560 xsltCompileRelativePathPattern(ctxt, NULL);
1574 * xsltCompileLocationPathPattern:
1575 * @ctxt: the compilation context
1577 * Compile the XSLT LocationPathPattern and generates a precompiled
1578 * form suitable for fast matching.
1580 * [2] LocationPathPattern ::= '/' RelativePathPattern?
1581 * | IdKeyPattern (('/' | '//') RelativePathPattern)?
1582 * | '//'? RelativePathPattern
1585 xsltCompileLocationPathPattern(xsltParserContextPtr ctxt) {
1587 if ((CUR == '/') && (NXT(1) == '/')) {
1589 * since we reverse the query
1590 * a leading // can be safely ignored
1594 xsltCompileRelativePathPattern(ctxt, NULL);
1595 } else if (CUR == '/') {
1597 * We need to find root as the parent
1601 PUSH(XSLT_OP_ROOT, NULL, NULL);
1602 if ((CUR != 0) || (CUR == '|')) {
1603 PUSH(XSLT_OP_PARENT, NULL, NULL);
1604 xsltCompileRelativePathPattern(ctxt, NULL);
1606 } else if (CUR == '*') {
1607 xsltCompileRelativePathPattern(ctxt, NULL);
1608 } else if (CUR == '@') {
1609 xsltCompileRelativePathPattern(ctxt, NULL);
1612 name = xsltScanName(ctxt);
1614 xsltPrintErrorContext(NULL, NULL, NULL); /* TODO */
1615 xsltGenericError(xsltGenericErrorContext,
1616 "xsltCompileLocationPathPattern : Name expected\n");
1621 if ((CUR == '(') && !xmlXPathIsNodeType(name)) {
1622 xsltCompileIdKeyPattern(ctxt, name, 1);
1623 if ((CUR == '/') && (NXT(1) == '/')) {
1624 PUSH(XSLT_OP_ANCESTOR, NULL, NULL);
1628 xsltCompileRelativePathPattern(ctxt, NULL);
1629 } else if (CUR == '/') {
1630 PUSH(XSLT_OP_PARENT, NULL, NULL);
1633 xsltCompileRelativePathPattern(ctxt, NULL);
1637 xsltCompileRelativePathPattern(ctxt, name);
1644 * xsltCompilePattern:
1645 * @pattern: an XSLT pattern
1646 * @doc: the containing document
1647 * @node: the containing element
1648 * @style: the stylesheet
1649 * @runtime: the transformation context, if done at run-time
1651 * Compile the XSLT pattern and generates a list of precompiled form suitable
1652 * for fast matching.
1654 * [1] Pattern ::= LocationPathPattern | Pattern '|' LocationPathPattern
1656 * Returns the generated pattern list or NULL in case of failure
1660 xsltCompilePattern(const xmlChar *pattern, xmlDocPtr doc,
1661 xmlNodePtr node, xsltStylesheetPtr style,
1662 xsltTransformContextPtr runtime) {
1663 xsltParserContextPtr ctxt = NULL;
1664 xsltCompMatchPtr element, first = NULL, previous = NULL;
1665 int current, start, end, level, j;
1667 if (pattern == NULL) {
1668 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1669 xsltGenericError(xsltGenericErrorContext,
1670 "xsltCompilePattern : NULL pattern\n");
1674 ctxt = xsltNewParserContext(style, runtime);
1680 while (pattern[current] != 0) {
1682 while (IS_BLANK(pattern[current]))
1686 while ((pattern[end] != 0) && ((pattern[end] != '|') || (level != 0))) {
1687 if (pattern[end] == '[')
1689 else if (pattern[end] == ']')
1691 else if (pattern[end] == '\'') {
1693 while ((pattern[end] != 0) && (pattern[end] != '\''))
1695 } else if (pattern[end] == '"') {
1697 while ((pattern[end] != 0) && (pattern[end] != '"'))
1702 if (current == end) {
1703 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1704 xsltGenericError(xsltGenericErrorContext,
1705 "xsltCompilePattern : NULL pattern\n");
1708 element = xsltNewCompMatch();
1709 if (element == NULL) {
1714 else if (previous != NULL)
1715 previous->next = element;
1718 ctxt->comp = element;
1719 ctxt->base = xmlStrndup(&pattern[start], end - start);
1720 if (ctxt->base == NULL)
1722 ctxt->cur = &(ctxt->base)[current - start];
1723 element->pattern = ctxt->base;
1724 element->nsList = xmlGetNsList(doc, node);
1726 if (element->nsList != NULL) {
1727 while (element->nsList[j] != NULL)
1733 #ifdef WITH_XSLT_DEBUG_PATTERN
1734 xsltGenericDebug(xsltGenericDebugContext,
1735 "xsltCompilePattern : parsing '%s'\n",
1738 xsltCompileLocationPathPattern(ctxt);
1743 * Reverse for faster interpretation.
1745 xsltReverseCompMatch(element);
1748 * Set-up the priority
1750 if (((element->steps[0].op == XSLT_OP_ELEM) ||
1751 (element->steps[0].op == XSLT_OP_ATTR)) &&
1752 (element->steps[0].value != NULL) &&
1753 (element->steps[1].op == XSLT_OP_END)) {
1754 element->priority = 0;
1756 } else if ((element->steps[0].op == XSLT_OP_ROOT) &&
1757 (element->steps[1].op == XSLT_OP_END)) {
1758 element->priority = 0;
1760 } else if ((element->steps[0].op == XSLT_OP_PI) &&
1761 (element->steps[0].value != NULL) &&
1762 (element->steps[1].op == XSLT_OP_END)) {
1763 element->priority = 0;
1764 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1765 (element->steps[0].value2 != NULL) &&
1766 (element->steps[1].op == XSLT_OP_END)) {
1767 element->priority = -0.25;
1768 } else if ((element->steps[0].op == XSLT_OP_NS) &&
1769 (element->steps[0].value != NULL) &&
1770 (element->steps[1].op == XSLT_OP_END)) {
1771 element->priority = -0.25;
1772 } else if ((element->steps[0].op == XSLT_OP_ATTR) &&
1773 (element->steps[0].value == NULL) &&
1774 (element->steps[0].value2 == NULL) &&
1775 (element->steps[1].op == XSLT_OP_END)) {
1776 element->priority = -0.5;
1777 } else if (((element->steps[0].op == XSLT_OP_PI) ||
1778 (element->steps[0].op == XSLT_OP_TEXT) ||
1779 (element->steps[0].op == XSLT_OP_ALL) ||
1780 (element->steps[0].op == XSLT_OP_NODE) ||
1781 (element->steps[0].op == XSLT_OP_COMMENT)) &&
1782 (element->steps[1].op == XSLT_OP_END)) {
1783 element->priority = -0.5;
1785 element->priority = 0.5;
1787 #ifdef WITH_XSLT_DEBUG_PATTERN
1788 xsltGenericDebug(xsltGenericDebugContext,
1789 "xsltCompilePattern : parsed %s, default priority %f\n",
1790 element->pattern, element->priority);
1792 if (pattern[end] == '|')
1797 xsltPrintErrorContext(NULL, NULL, node); /* TODO */
1798 xsltGenericError(xsltGenericErrorContext,
1799 "xsltCompilePattern : NULL pattern\n");
1803 xsltFreeParserContext(ctxt);
1808 xsltFreeParserContext(ctxt);
1810 xsltFreeCompMatchList(first);
1814 /************************************************************************
1816 * Module interfaces *
1818 ************************************************************************/
1822 * @style: an XSLT stylesheet
1823 * @cur: an XSLT template
1824 * @mode: the mode name or NULL
1825 * @modeURI: the mode URI or NULL
1827 * Register the XSLT pattern associated to @cur
1829 * Returns -1 in case of error, 0 otherwise
1832 xsltAddTemplate(xsltStylesheetPtr style, xsltTemplatePtr cur,
1833 const xmlChar *mode, const xmlChar *modeURI) {
1834 xsltCompMatchPtr pat, list, *top = NULL, next;
1835 const xmlChar *name = NULL;
1836 float priority; /* the priority */
1838 if ((style == NULL) || (cur == NULL) || (cur->match == NULL))
1841 priority = cur->priority;
1842 pat = xsltCompilePattern(cur->match, style->doc, cur->elem, style, NULL);
1848 pat->template = cur;
1850 pat->mode = xmlStrdup(mode);
1851 if (modeURI != NULL)
1852 pat->modeURI = xmlStrdup(modeURI);
1853 if (priority != XSLT_PAT_NO_PRIORITY)
1854 pat->priority = priority;
1857 * insert it in the hash table list corresponding to its lookup name
1859 switch (pat->steps[0].op) {
1861 if (pat->steps[0].value != NULL)
1862 name = pat->steps[0].value;
1864 top = (xsltCompMatchPtr *) &(style->attrMatch);
1868 case XSLT_OP_PARENT:
1869 case XSLT_OP_ANCESTOR:
1870 top = (xsltCompMatchPtr *) &(style->elemMatch);
1873 top = (xsltCompMatchPtr *) &(style->rootMatch);
1876 top = (xsltCompMatchPtr *) &(style->keyMatch);
1879 /* TODO optimize ID !!! */
1882 top = (xsltCompMatchPtr *) &(style->elemMatch);
1885 case XSLT_OP_PREDICATE:
1886 xsltPrintErrorContext(NULL, style, NULL);
1887 xsltGenericError(xsltGenericErrorContext,
1888 "xsltAddTemplate: invalid compiled pattern\n");
1889 xsltFreeCompMatch(pat);
1892 * TODO: some flags at the top level about type based patterns
1893 * would be faster than inclusion in the hash table.
1896 if (pat->steps[0].value != NULL)
1897 name = pat->steps[0].value;
1899 top = (xsltCompMatchPtr *) &(style->piMatch);
1901 case XSLT_OP_COMMENT:
1902 top = (xsltCompMatchPtr *) &(style->commentMatch);
1905 top = (xsltCompMatchPtr *) &(style->textMatch);
1908 if (pat->steps[0].value != NULL)
1909 name = pat->steps[0].value;
1911 top = (xsltCompMatchPtr *) &(style->elemMatch);
1916 if (style->templatesHash == NULL) {
1917 style->templatesHash = xmlHashCreate(1024);
1918 if (style->templatesHash == NULL) {
1919 xsltFreeCompMatch(pat);
1922 xmlHashAddEntry3(style->templatesHash, name, mode, modeURI, pat);
1924 list = (xsltCompMatchPtr) xmlHashLookup3(style->templatesHash,
1925 name, mode, modeURI);
1927 xmlHashAddEntry3(style->templatesHash, name,
1928 mode, modeURI, pat);
1931 * Note '<=' since one must choose among the matching
1932 * template rules that are left, the one that occurs
1933 * last in the stylesheet
1935 if (list->priority <= pat->priority) {
1937 xmlHashUpdateEntry3(style->templatesHash, name,
1938 mode, modeURI, pat, NULL);
1940 while (list->next != NULL) {
1941 if (list->next->priority <= pat->priority)
1945 pat->next = list->next;
1950 } else if (top != NULL) {
1955 } else if (list->priority <= pat->priority) {
1959 while (list->next != NULL) {
1960 if (list->next->priority <= pat->priority)
1964 pat->next = list->next;
1968 xsltPrintErrorContext(NULL, style, NULL);
1969 xsltGenericError(xsltGenericErrorContext,
1970 "xsltAddTemplate: invalid compiled pattern\n");
1971 xsltFreeCompMatch(pat);
1974 #ifdef WITH_XSLT_DEBUG_PATTERN
1976 xsltGenericDebug(xsltGenericDebugContext,
1977 "added pattern : '%s' mode '%s' priority %f\n",
1978 pat->pattern, pat->mode, pat->priority);
1980 xsltGenericDebug(xsltGenericDebugContext,
1981 "added pattern : '%s' priority %f\n",
1982 pat->pattern, pat->priority);
1992 * @ctxt: a XSLT process context
1993 * @node: the node being processed
1994 * @style: the current style
1996 * Finds the template applying to this node, if @style is non-NULL
1997 * it means one needs to look for the next imported template in scope.
1999 * Returns the xsltTemplatePtr or NULL if not found
2002 xsltGetTemplate(xsltTransformContextPtr ctxt, xmlNodePtr node,
2003 xsltStylesheetPtr style) {
2004 xsltStylesheetPtr curstyle;
2005 xsltTemplatePtr ret = NULL;
2006 const xmlChar *name = NULL;
2007 xsltCompMatchPtr list = NULL;
2010 if ((ctxt == NULL) || (node == NULL))
2013 if (style == NULL) {
2014 curstyle = ctxt->style;
2016 curstyle = xsltNextImport(style);
2019 while ((curstyle != NULL) && (curstyle != style)) {
2020 priority = XSLT_PAT_NO_PRIORITY;
2021 /* TODO : handle IDs/keys here ! */
2022 if (curstyle->templatesHash != NULL) {
2024 * Use the top name as selector
2026 switch (node->type) {
2027 case XML_ELEMENT_NODE:
2028 case XML_ATTRIBUTE_NODE:
2032 case XML_DOCUMENT_NODE:
2033 case XML_HTML_DOCUMENT_NODE:
2035 case XML_CDATA_SECTION_NODE:
2036 case XML_COMMENT_NODE:
2037 case XML_ENTITY_REF_NODE:
2038 case XML_ENTITY_NODE:
2039 case XML_DOCUMENT_TYPE_NODE:
2040 case XML_DOCUMENT_FRAG_NODE:
2041 case XML_NOTATION_NODE:
2043 case XML_ELEMENT_DECL:
2044 case XML_ATTRIBUTE_DECL:
2045 case XML_ENTITY_DECL:
2046 case XML_NAMESPACE_DECL:
2047 case XML_XINCLUDE_START:
2048 case XML_XINCLUDE_END:
2057 * find the list of appliable expressions based on the name
2059 list = (xsltCompMatchPtr) xmlHashLookup3(curstyle->templatesHash,
2060 name, ctxt->mode, ctxt->modeURI);
2063 while (list != NULL) {
2064 if (xsltTestCompMatch(ctxt, list, node,
2065 ctxt->mode, ctxt->modeURI)) {
2066 ret = list->template;
2067 priority = list->priority;
2075 * find alternate generic matches
2077 switch (node->type) {
2078 case XML_ELEMENT_NODE:
2079 list = curstyle->elemMatch;
2081 case XML_ATTRIBUTE_NODE:
2082 list = curstyle->attrMatch;
2085 list = curstyle->piMatch;
2087 case XML_DOCUMENT_NODE:
2088 case XML_HTML_DOCUMENT_NODE:
2089 list = curstyle->rootMatch;
2092 case XML_CDATA_SECTION_NODE:
2093 list = curstyle->textMatch;
2095 case XML_COMMENT_NODE:
2096 list = curstyle->commentMatch;
2098 case XML_ENTITY_REF_NODE:
2099 case XML_ENTITY_NODE:
2100 case XML_DOCUMENT_TYPE_NODE:
2101 case XML_DOCUMENT_FRAG_NODE:
2102 case XML_NOTATION_NODE:
2104 case XML_ELEMENT_DECL:
2105 case XML_ATTRIBUTE_DECL:
2106 case XML_ENTITY_DECL:
2107 case XML_NAMESPACE_DECL:
2108 case XML_XINCLUDE_START:
2109 case XML_XINCLUDE_END:
2115 while ((list != NULL) &&
2116 ((ret == NULL) || (list->priority > priority))) {
2117 if (xsltTestCompMatch(ctxt, list, node,
2118 ctxt->mode, ctxt->modeURI)) {
2119 ret = list->template;
2120 priority = list->priority;
2126 * Some of the tests for elements can also apply to documents
2128 if ((node->type == XML_DOCUMENT_NODE) ||
2129 (node->type == XML_HTML_DOCUMENT_NODE)) {
2130 list = curstyle->elemMatch;
2131 while ((list != NULL) &&
2132 ((ret == NULL) || (list->priority > priority))) {
2133 if (xsltTestCompMatch(ctxt, list, node,
2134 ctxt->mode, ctxt->modeURI)) {
2135 ret = list->template;
2136 priority = list->priority;
2143 if (node->_private != NULL) {
2144 list = curstyle->keyMatch;
2145 while ((list != NULL) &&
2146 ((ret == NULL) || (list->priority > priority))) {
2147 if (xsltTestCompMatch(ctxt, list, node,
2148 ctxt->mode, ctxt->modeURI)) {
2149 ret = list->template;
2150 priority = list->priority;
2160 * Cycle on next curstylesheet import.
2162 curstyle = xsltNextImport(curstyle);
2168 * xsltCleanupTemplates:
2169 * @style: an XSLT stylesheet
2171 * Cleanup the state of the templates used by the stylesheet and
2172 * the ones it imports.
2175 xsltCleanupTemplates(xsltStylesheetPtr style ATTRIBUTE_UNUSED) {
2179 * xsltFreeTemplateHashes:
2180 * @style: an XSLT stylesheet
2182 * Free up the memory used by xsltAddTemplate/xsltGetTemplate mechanism
2185 xsltFreeTemplateHashes(xsltStylesheetPtr style) {
2186 if (style->templatesHash != NULL)
2187 xmlHashFree((xmlHashTablePtr) style->templatesHash,
2188 (xmlHashDeallocator) xsltFreeCompMatchList);
2189 if (style->rootMatch != NULL)
2190 xsltFreeCompMatchList(style->rootMatch);
2191 if (style->keyMatch != NULL)
2192 xsltFreeCompMatchList(style->keyMatch);
2193 if (style->elemMatch != NULL)
2194 xsltFreeCompMatchList(style->elemMatch);
2195 if (style->attrMatch != NULL)
2196 xsltFreeCompMatchList(style->attrMatch);
2197 if (style->parentMatch != NULL)
2198 xsltFreeCompMatchList(style->parentMatch);
2199 if (style->textMatch != NULL)
2200 xsltFreeCompMatchList(style->textMatch);
2201 if (style->piMatch != NULL)
2202 xsltFreeCompMatchList(style->piMatch);
2203 if (style->commentMatch != NULL)
2204 xsltFreeCompMatchList(style->commentMatch);
2210 * @node: a node in the source tree
2211 * @pattern: an XSLT pattern
2212 * @ctxtdoc: context document (for namespaces)
2213 * @ctxtnode: context node (for namespaces)
2215 * Determine if a node matches a pattern.
2218 xsltMatchPattern(xsltTransformContextPtr context,
2220 const xmlChar *pattern,
2222 xmlNodePtr ctxtnode)
2225 xsltCompMatchPtr first, comp;
2227 if ((context != NULL) && (pattern != NULL)) {
2228 first = xsltCompilePattern(pattern, ctxtdoc, ctxtnode);
2229 for (comp = first; comp != NULL; comp = comp->next) {
2230 match = xsltTestCompMatch(context, comp, node, NULL, NULL);
2235 xsltFreeCompMatchList(first);