diff --git a/libxslt/pattern.c b/libxslt/pattern.c
index 105f3e2c05d1ee6bd66f1a32a57ccd7adcf7e4de_bGlieHNsdC9wYXR0ZXJuLmM=..71c854a7a5b680d3e133079dc450d60c5394669c_bGlieHNsdC9wYXR0ZXJuLmM= 100644
--- a/libxslt/pattern.c
+++ b/libxslt/pattern.c
@@ -2134,6 +2134,9 @@
     if ((style == NULL) || (cur == NULL))
 	return(-1);
 
+    if (cur->next != NULL)
+        cur->position = cur->next->position + 1;
+
     /* Register named template */
     if (cur->name != NULL) {
         if (style->namedTemplates == NULL) {
@@ -2495,7 +2498,10 @@
 		break;
 	}
 	while ((list != NULL) &&
-	       ((ret == NULL)  || (list->priority > priority))) {
+	       ((ret == NULL) ||
+                (list->priority > priority) ||
+                ((list->priority == priority) &&
+                 (list->template->position > ret->position)))) {
 	    if (xsltTestCompMatch(ctxt, list, node,
 			          ctxt->mode, ctxt->modeURI) == 1) {
 		ret = list->template;
@@ -2512,7 +2518,10 @@
 	    (node->type == XML_TEXT_NODE)) {
 	    list = curstyle->elemMatch;
 	    while ((list != NULL) &&
-		   ((ret == NULL)  || (list->priority > priority))) {
+                   ((ret == NULL) ||
+                    (list->priority > priority) ||
+                    ((list->priority == priority) &&
+                     (list->template->position > ret->position)))) {
 		if (xsltTestCompMatch(ctxt, list, node,
 				      ctxt->mode, ctxt->modeURI) == 1) {
 		    ret = list->template;
@@ -2525,7 +2534,10 @@
 		   (node->type == XML_COMMENT_NODE)) {
 	    list = curstyle->elemMatch;
 	    while ((list != NULL) &&
-		   ((ret == NULL)  || (list->priority > priority))) {
+                   ((ret == NULL) ||
+                    (list->priority > priority) ||
+                    ((list->priority == priority) &&
+                     (list->template->position > ret->position)))) {
 		if (xsltTestCompMatch(ctxt, list, node,
 				      ctxt->mode, ctxt->modeURI) == 1) {
 		    ret = list->template;
@@ -2540,7 +2552,10 @@
 	if (keyed) {
 	    list = curstyle->keyMatch;
 	    while ((list != NULL) &&
-		   ((ret == NULL)  || (list->priority > priority))) {
+                   ((ret == NULL) ||
+                    (list->priority > priority) ||
+                    ((list->priority == priority) &&
+                     (list->template->position > ret->position)))) {
 		if (xsltTestCompMatch(ctxt, list, node,
 				      ctxt->mode, ctxt->modeURI) == 1) {
 		    ret = list->template;
diff --git a/libxslt/xsltInternals.h b/libxslt/xsltInternals.h
index 105f3e2c05d1ee6bd66f1a32a57ccd7adcf7e4de_bGlieHNsdC94c2x0SW50ZXJuYWxzLmg=..71c854a7a5b680d3e133079dc450d60c5394669c_bGlieHNsdC94c2x0SW50ZXJuYWxzLmg= 100644
--- a/libxslt/xsltInternals.h
+++ b/libxslt/xsltInternals.h
@@ -291,6 +291,9 @@
     int              templMax;		/* Size of the templtes stack */
     xsltTemplatePtr *templCalledTab;	/* templates called */
     int             *templCountTab;  /* .. and how often */
+
+    /* Conflict resolution */
+    int position;
 };
 
 /**
diff --git a/tests/REC/test-5.5-1.out b/tests/REC/test-5.5-1.out
new file mode 100644
index 0000000000000000000000000000000000000000..71c854a7a5b680d3e133079dc450d60c5394669c_dGVzdHMvUkVDL3Rlc3QtNS41LTEub3V0
--- /dev/null
+++ b/tests/REC/test-5.5-1.out
@@ -0,0 +1,7 @@
+<?xml version="1.0"?>
+<winelist>
+  <wine>
+    <winery>Benziger</winery>
+    <product>Carneros</product>
+  </wine>
+</winelist>
diff --git a/tests/REC/test-5.5-1.xml b/tests/REC/test-5.5-1.xml
new file mode 100644
index 0000000000000000000000000000000000000000..71c854a7a5b680d3e133079dc450d60c5394669c_dGVzdHMvUkVDL3Rlc3QtNS41LTEueG1s
--- /dev/null
+++ b/tests/REC/test-5.5-1.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<winelist>
+  <wine grape="Chardonnay">
+    <winery>Benziger</winery>
+    <product>Carneros</product>
+  </wine>
+</winelist>
diff --git a/tests/REC/test-5.5-1.xsl b/tests/REC/test-5.5-1.xsl
new file mode 100644
index 0000000000000000000000000000000000000000..71c854a7a5b680d3e133079dc450d60c5394669c_dGVzdHMvUkVDL3Rlc3QtNS41LTEueHNs
--- /dev/null
+++ b/tests/REC/test-5.5-1.xsl
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- transformation of the calloutlist element -->
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+  <xsl:template match="text()">
+  </xsl:template>
+
+  <xsl:template match="@*|node()">
+    <xsl:copy>
+      <xsl:apply-templates/>
+    </xsl:copy>
+  </xsl:template>
+
+</xsl:stylesheet>