diff --git a/examples/xsltICUSort.c b/examples/xsltICUSort.c
index 33bc59e03d4cf89c648983d2f04874431268c710_ZXhhbXBsZXMveHNsdElDVVNvcnQuYw==..ce1e932f70ab898c225459549b18560bceefa831_ZXhhbXBsZXMveHNsdElDVVNvcnQuYw== 100644
--- a/examples/xsltICUSort.c
+++ b/examples/xsltICUSort.c
@@ -42,10 +42,9 @@
     xmlXPathObjectPtr *resultsTab[XSLT_MAX_SORT];
     xmlXPathObjectPtr *results = NULL, *res;
     xmlNodeSetPtr list = NULL;
-    int descending, number, desc, numb;
     int len = 0;
     int i, j, incr;
     int tst;
     int depth;
     xmlNodePtr node;
     xmlXPathObjectPtr tmp;
@@ -46,11 +45,11 @@
     int len = 0;
     int i, j, incr;
     int tst;
     int depth;
     xmlNodePtr node;
     xmlXPathObjectPtr tmp;
-    xsltStylePreCompPtr comp;
-    int tempstype[XSLT_MAX_SORT], temporder[XSLT_MAX_SORT];
+    const xsltStylePreComp *comp;
+    int number[XSLT_MAX_SORT], desc[XSLT_MAX_SORT];
 
     /* Start ICU change */
     UCollator *coll = 0;
@@ -75,5 +74,4 @@
 
     for (j = 0; j < nbsorts; j++) {
 	comp = sorts[j]->_private;
-	tempstype[j] = 0;
 	if ((comp->stype == NULL) && (comp->has_stype != 0)) {
@@ -79,5 +77,5 @@
 	if ((comp->stype == NULL) && (comp->has_stype != 0)) {
-	    comp->stype =
+	    xmlChar *stype =
 		xsltEvalAttrValueTemplate(ctxt, sorts[j],
 					  (const xmlChar *) "data-type",
 					  XSLT_NAMESPACE);
@@ -81,12 +79,12 @@
 		xsltEvalAttrValueTemplate(ctxt, sorts[j],
 					  (const xmlChar *) "data-type",
 					  XSLT_NAMESPACE);
-	    if (comp->stype != NULL) {
-		tempstype[j] = 1;
-		if (xmlStrEqual(comp->stype, (const xmlChar *) "text"))
-		    comp->number = 0;
-		else if (xmlStrEqual(comp->stype, (const xmlChar *) "number"))
-		    comp->number = 1;
+	    number[j] = 0;
+	    if (stype != NULL) {
+		if (xmlStrEqual(stype, (const xmlChar *) "text"))
+		    ;
+		else if (xmlStrEqual(stype, (const xmlChar *) "number"))
+		    number[j] = 1;
 		else {
 		    xsltTransformError(ctxt, NULL, sorts[j],
 			  "xsltDoSortFunction: no support for data-type = %s\n",
@@ -90,6 +88,5 @@
 		else {
 		    xsltTransformError(ctxt, NULL, sorts[j],
 			  "xsltDoSortFunction: no support for data-type = %s\n",
-				     comp->stype);
-		    comp->number = 0; /* use default */
+			  stype);
 		}
@@ -95,2 +92,3 @@
 		}
+                xmlFree(stype);
 	    }
@@ -96,2 +94,4 @@
 	    }
+        } else {
+            number[j] = comp->number;
 	}
@@ -97,3 +97,2 @@
 	}
-	temporder[j] = 0;
 	if ((comp->order == NULL) && (comp->has_order != 0)) {
@@ -99,14 +98,13 @@
 	if ((comp->order == NULL) && (comp->has_order != 0)) {
-	    comp->order = xsltEvalAttrValueTemplate(ctxt, sorts[j],
-						    (const xmlChar *) "order",
-						    XSLT_NAMESPACE);
-	    if (comp->order != NULL) {
-		temporder[j] = 1;
-		if (xmlStrEqual(comp->order, (const xmlChar *) "ascending"))
-		    comp->descending = 0;
-		else if (xmlStrEqual(comp->order,
-				     (const xmlChar *) "descending"))
-		    comp->descending = 1;
+	    xmlChar *order = xsltEvalAttrValueTemplate(ctxt, sorts[j],
+						       BAD_CAST "order",
+						       XSLT_NAMESPACE);
+	    desc[j] = 0;
+	    if (order != NULL) {
+		if (xmlStrEqual(order, (const xmlChar *) "ascending"))
+		    ;
+		else if (xmlStrEqual(order, (const xmlChar *) "descending"))
+		    desc[j] = 1;
 		else {
 		    xsltTransformError(ctxt, NULL, sorts[j],
 			     "xsltDoSortFunction: invalid value %s for order\n",
@@ -110,6 +108,5 @@
 		else {
 		    xsltTransformError(ctxt, NULL, sorts[j],
 			     "xsltDoSortFunction: invalid value %s for order\n",
-				     comp->order);
-		    comp->descending = 0; /* use default */
+			     order);
 		}
@@ -115,2 +112,3 @@
 		}
+                xmlFree(order);
 	    }
@@ -116,4 +114,6 @@
 	    }
+        } else {
+            desc[j] = comp->descending;
 	}
     }
 
@@ -126,8 +126,6 @@
     results = resultsTab[0];
 
     comp = sorts[0]->_private;
-    descending = comp->descending;
-    number = comp->number;
     if (results == NULL)
 	return;
 
@@ -166,7 +164,7 @@
 		if (results[j] == NULL)
 		    tst = 1;
 		else {
-		    if (number) {
+		    if (number[0]) {
 			if (results[j]->floatval == results[j + incr]->floatval)
 			    tst = 0;
 			else if (results[j]->floatval >
@@ -186,7 +184,7 @@
 			tst = ucol_strcoll(coll, target, u_strlen(target), target2, u_strlen(target2));
 			/* End ICU change */
 		    }
-		    if (descending)
+		    if (desc[0])
 			tst = -tst;
 		}
 		if (tst == 0) {
@@ -200,8 +198,6 @@
 			comp = sorts[depth]->_private;
 			if (comp == NULL)
 			    break;
-			desc = comp->descending;
-			numb = comp->number;
 
 			/*
 			 * Compute the result of the next level for the
@@ -216,7 +212,7 @@
 			if (res[j] == NULL)
 			    tst = 1;
 			else {
-			    if (numb) {
+			    if (number[depth]) {
 				if (res[j]->floatval == res[j + incr]->floatval)
 				    tst = 0;
 				else if (res[j]->floatval >
@@ -236,7 +232,7 @@
 				tst = ucol_strcoll(coll, target, u_strlen(target), target2, u_strlen(target2));
 				/* End ICU change */
 			    }
-			    if (desc)
+			    if (desc[depth])
 			      tst = -tst;
 			}
 			/*
@@ -284,16 +280,6 @@
 
     for (j = 0; j < nbsorts; j++) {
 	comp = sorts[j]->_private;
-	if (tempstype[j] == 1) {
-	    /* The data-type needs to be recomputed each time */
-	    xmlFree(comp->stype);
-	    comp->stype = NULL;
-	}
-	if (temporder[j] == 1) {
-	    /* The order needs to be recomputed each time */
-	    xmlFree(comp->order);
-	    comp->order = NULL;
-	}
 	if (resultsTab[j] != NULL) {
 	    for (i = 0;i < len;i++)
 		xmlXPathFreeObject(resultsTab[j][i]);
diff --git a/libxslt/xsltutils.c b/libxslt/xsltutils.c
index 33bc59e03d4cf89c648983d2f04874431268c710_bGlieHNsdC94c2x0dXRpbHMuYw==..ce1e932f70ab898c225459549b18560bceefa831_bGlieHNsdC94c2x0dXRpbHMuYw== 100644
--- a/libxslt/xsltutils.c
+++ b/libxslt/xsltutils.c
@@ -947,5 +947,5 @@
 }
 
 /**
- * xsltComputeSortResultiInternal:
+ * xsltComputeSortResultInternal:
  * @ctxt:  a XSLT process context
@@ -951,6 +951,7 @@
  * @ctxt:  a XSLT process context
- * @sort:  node list
- * @xfrm:  Transform strings according to locale
+ * @sort:  xsl:sort node
+ * @number:  data-type is number
+ * @locale:  transform strings according to locale
  *
  * reorder the current node list accordingly to the set of sorting
  * requirement provided by the array of nodes.
@@ -959,7 +960,7 @@
  */
 static xmlXPathObjectPtr *
 xsltComputeSortResultInternal(xsltTransformContextPtr ctxt, xmlNodePtr sort,
-                              int xfrm) {
+                              int number, xsltLocale locale) {
 #ifdef XSLT_REFACTORED
     xsltStyleItemSortPtr comp;
 #else
@@ -963,7 +964,7 @@
 #ifdef XSLT_REFACTORED
     xsltStyleItemSortPtr comp;
 #else
-    xsltStylePreCompPtr comp;
+    const xsltStylePreComp *comp;
 #endif
     xmlXPathObjectPtr *results = NULL;
     xmlNodeSetPtr list = NULL;
@@ -1031,6 +1032,6 @@
 	if (res != NULL) {
 	    if (res->type != XPATH_STRING)
 		res = xmlXPathConvertString(res);
-	    if (comp->number)
+	    if (number)
 		res = xmlXPathConvertNumber(res);
 	    res->index = i;	/* Save original pos for dupl resolv */
@@ -1035,6 +1036,6 @@
 		res = xmlXPathConvertNumber(res);
 	    res->index = i;	/* Save original pos for dupl resolv */
-	    if (comp->number) {
+	    if (number) {
 		if (res->type == XPATH_NUMBER) {
 		    results[i] = res;
 		} else {
@@ -1046,5 +1047,5 @@
 		}
 	    } else {
 		if (res->type == XPATH_STRING) {
-		    if ((xfrm) && (comp->locale != (xsltLocale)0)) {
+		    if (locale != (xsltLocale)0) {
 			xmlChar *str = res->stringval;
@@ -1050,5 +1051,5 @@
 			xmlChar *str = res->stringval;
-			res->stringval = (xmlChar *) xsltStrxfrm(comp->locale, str);
+			res->stringval = (xmlChar *) xsltStrxfrm(locale, str);
 			xmlFree(str);
 		    }
 
@@ -1088,7 +1089,8 @@
  */
 xmlXPathObjectPtr *
 xsltComputeSortResult(xsltTransformContextPtr ctxt, xmlNodePtr sort) {
-    return xsltComputeSortResultInternal(ctxt, sort, /* xfrm */ 0);
+    return xsltComputeSortResultInternal(ctxt, sort, /* number */ 0,
+                                         /* locale */ 0);
 }
 
 /**
@@ -1106,8 +1108,8 @@
 #ifdef XSLT_REFACTORED
     xsltStyleItemSortPtr comp;
 #else
-    xsltStylePreCompPtr comp;
+    const xsltStylePreComp *comp;
 #endif
     xmlXPathObjectPtr *resultsTab[XSLT_MAX_SORT];
     xmlXPathObjectPtr *results = NULL, *res;
     xmlNodeSetPtr list = NULL;
@@ -1110,11 +1112,10 @@
 #endif
     xmlXPathObjectPtr *resultsTab[XSLT_MAX_SORT];
     xmlXPathObjectPtr *results = NULL, *res;
     xmlNodeSetPtr list = NULL;
-    int descending, number, desc, numb;
     int len = 0;
     int i, j, incr;
     int tst;
     int depth;
     xmlNodePtr node;
     xmlXPathObjectPtr tmp;
@@ -1115,11 +1116,11 @@
     int len = 0;
     int i, j, incr;
     int tst;
     int depth;
     xmlNodePtr node;
     xmlXPathObjectPtr tmp;
-    int tempstype[XSLT_MAX_SORT], temporder[XSLT_MAX_SORT],
-        templang[XSLT_MAX_SORT];
+    int number[XSLT_MAX_SORT], desc[XSLT_MAX_SORT];
+    xsltLocale locale[XSLT_MAX_SORT];
 
     if ((ctxt == NULL) || (sorts == NULL) || (nbsorts <= 0) ||
 	(nbsorts >= XSLT_MAX_SORT))
@@ -1136,5 +1137,4 @@
 
     for (j = 0; j < nbsorts; j++) {
 	comp = sorts[j]->psvi;
-	tempstype[j] = 0;
 	if ((comp->stype == NULL) && (comp->has_stype != 0)) {
@@ -1140,3 +1140,3 @@
 	if ((comp->stype == NULL) && (comp->has_stype != 0)) {
-	    comp->stype =
+	    xmlChar *stype =
 		xsltEvalAttrValueTemplate(ctxt, sorts[j],
@@ -1142,12 +1142,11 @@
 		xsltEvalAttrValueTemplate(ctxt, sorts[j],
-					  (const xmlChar *) "data-type",
-					  NULL);
-	    if (comp->stype != NULL) {
-		tempstype[j] = 1;
-		if (xmlStrEqual(comp->stype, (const xmlChar *) "text"))
-		    comp->number = 0;
-		else if (xmlStrEqual(comp->stype, (const xmlChar *) "number"))
-		    comp->number = 1;
+					  BAD_CAST "data-type", NULL);
+	    number[j] = 0;
+	    if (stype != NULL) {
+		if (xmlStrEqual(stype, (const xmlChar *) "text"))
+		    ;
+		else if (xmlStrEqual(stype, (const xmlChar *) "number"))
+		    number[j] = 1;
 		else {
 		    xsltTransformError(ctxt, NULL, sorts[j],
 			  "xsltDoSortFunction: no support for data-type = %s\n",
@@ -1151,6 +1150,5 @@
 		else {
 		    xsltTransformError(ctxt, NULL, sorts[j],
 			  "xsltDoSortFunction: no support for data-type = %s\n",
-				     comp->stype);
-		    comp->number = 0; /* use default */
+			  stype);
 		}
@@ -1156,2 +1154,3 @@
 		}
+                xmlFree(stype);
 	    }
@@ -1157,4 +1156,5 @@
 	    }
-	}
-	temporder[j] = 0;
+	} else {
+	    number[j] = comp->number;
+        }
 	if ((comp->order == NULL) && (comp->has_order != 0)) {
@@ -1160,14 +1160,12 @@
 	if ((comp->order == NULL) && (comp->has_order != 0)) {
-	    comp->order = xsltEvalAttrValueTemplate(ctxt, sorts[j],
-						    (const xmlChar *) "order",
-						    NULL);
-	    if (comp->order != NULL) {
-		temporder[j] = 1;
-		if (xmlStrEqual(comp->order, (const xmlChar *) "ascending"))
-		    comp->descending = 0;
-		else if (xmlStrEqual(comp->order,
-				     (const xmlChar *) "descending"))
-		    comp->descending = 1;
+	    xmlChar *order = xsltEvalAttrValueTemplate(ctxt, sorts[j],
+                                                       BAD_CAST "order", NULL);
+	    desc[j] = 0;
+	    if (order != NULL) {
+		if (xmlStrEqual(order, (const xmlChar *) "ascending"))
+		    ;
+		else if (xmlStrEqual(order, (const xmlChar *) "descending"))
+		    desc[j] = 1;
 		else {
 		    xsltTransformError(ctxt, NULL, sorts[j],
 			     "xsltDoSortFunction: invalid value %s for order\n",
@@ -1171,6 +1169,5 @@
 		else {
 		    xsltTransformError(ctxt, NULL, sorts[j],
 			     "xsltDoSortFunction: invalid value %s for order\n",
-				     comp->order);
-		    comp->descending = 0; /* use default */
+			     order);
 		}
@@ -1176,2 +1173,3 @@
 		}
+                xmlFree(order);
 	    }
@@ -1177,2 +1175,4 @@
 	    }
+	} else {
+	    desc[j] = comp->descending;
 	}
@@ -1178,7 +1178,6 @@
 	}
-	templang[j] = 0;
 	if ((comp->lang == NULL) && (comp->has_lang != 0)) {
             xmlChar *lang = xsltEvalAttrValueTemplate(ctxt, sorts[j],
 						      (xmlChar *) "lang",
 						      NULL);
 	    if (lang != NULL) {
@@ -1180,8 +1179,7 @@
 	if ((comp->lang == NULL) && (comp->has_lang != 0)) {
             xmlChar *lang = xsltEvalAttrValueTemplate(ctxt, sorts[j],
 						      (xmlChar *) "lang",
 						      NULL);
 	    if (lang != NULL) {
-		templang[j] = 1;
-                comp->locale = xsltNewLocale(lang);
+                locale[j] = xsltNewLocale(lang);
                 xmlFree(lang);
@@ -1187,2 +1185,4 @@
                 xmlFree(lang);
+            } else {
+                locale[j] = 0;
             }
@@ -1188,6 +1188,8 @@
             }
-	}
+	} else {
+            locale[j] = comp->locale;
+        }
     }
 
     len = list->nodeNr;
 
@@ -1190,12 +1192,12 @@
     }
 
     len = list->nodeNr;
 
-    resultsTab[0] = xsltComputeSortResultInternal(ctxt, sorts[0],
-                                                  /* xfrm */ 1);
+    resultsTab[0] = xsltComputeSortResultInternal(ctxt, sorts[0], number[0],
+                                                  locale[0]);
     for (i = 1;i < XSLT_MAX_SORT;i++)
 	resultsTab[i] = NULL;
 
     results = resultsTab[0];
 
     comp = sorts[0]->psvi;
@@ -1196,11 +1198,9 @@
     for (i = 1;i < XSLT_MAX_SORT;i++)
 	resultsTab[i] = NULL;
 
     results = resultsTab[0];
 
     comp = sorts[0]->psvi;
-    descending = comp->descending;
-    number = comp->number;
     if (results == NULL)
 	goto cleanup;
 
@@ -1215,7 +1215,7 @@
 		if (results[j] == NULL)
 		    tst = 1;
 		else {
-		    if (number) {
+		    if (number[0]) {
 			/* We make NaN smaller than number in accordance
 			   with XSLT spec */
 			if (xmlXPathIsNaN(results[j]->floatval)) {
@@ -1232,5 +1232,5 @@
 				results[j + incr]->floatval)
 			    tst = 1;
 			else tst = -1;
-		    } else if(comp->locale != (xsltLocale)0) {
+		    } else if(locale[0] != (xsltLocale)0) {
 			tst = xsltLocaleStrcmp(
@@ -1236,8 +1236,8 @@
 			tst = xsltLocaleStrcmp(
-			    comp->locale,
+			    locale[0],
 			    (xsltLocaleChar *) results[j]->stringval,
 			    (xsltLocaleChar *) results[j + incr]->stringval);
 		    } else {
 			tst = xmlStrcmp(results[j]->stringval,
 				     results[j + incr]->stringval);
 		    }
@@ -1238,10 +1238,10 @@
 			    (xsltLocaleChar *) results[j]->stringval,
 			    (xsltLocaleChar *) results[j + incr]->stringval);
 		    } else {
 			tst = xmlStrcmp(results[j]->stringval,
 				     results[j + incr]->stringval);
 		    }
-		    if (descending)
+		    if (desc[0])
 			tst = -tst;
 		}
 		if (tst == 0) {
@@ -1255,8 +1255,6 @@
 			comp = sorts[depth]->psvi;
 			if (comp == NULL)
 			    break;
-			desc = comp->descending;
-			numb = comp->number;
 
 			/*
 			 * Compute the result of the next level for the
@@ -1266,7 +1264,8 @@
 			    resultsTab[depth] =
                                 xsltComputeSortResultInternal(ctxt,
                                                               sorts[depth],
-                                                              /* xfrm */ 1);
+                                                              number[depth],
+                                                              locale[depth]);
 			res = resultsTab[depth];
 			if (res == NULL)
 			    break;
@@ -1276,7 +1275,7 @@
 			} else if (res[j+incr] == NULL) {
 			    tst = -1;
 			} else {
-			    if (numb) {
+			    if (number[depth]) {
 				/* We make NaN smaller than number in
 				   accordance with XSLT spec */
 				if (xmlXPathIsNaN(res[j]->floatval)) {
@@ -1295,5 +1294,5 @@
 					res[j + incr]->floatval)
 				    tst = 1;
 				else tst = -1;
-			    } else if(comp->locale != (xsltLocale)0) {
+			    } else if(locale[depth] != (xsltLocale)0) {
 				tst = xsltLocaleStrcmp(
@@ -1299,8 +1298,8 @@
 				tst = xsltLocaleStrcmp(
-				    comp->locale,
+				    locale[depth],
 				    (xsltLocaleChar *) res[j]->stringval,
 				    (xsltLocaleChar *) res[j + incr]->stringval);
 			    } else {
 				tst = xmlStrcmp(res[j]->stringval,
 					     res[j + incr]->stringval);
 			    }
@@ -1301,10 +1300,10 @@
 				    (xsltLocaleChar *) res[j]->stringval,
 				    (xsltLocaleChar *) res[j + incr]->stringval);
 			    } else {
 				tst = xmlStrcmp(res[j]->stringval,
 					     res[j + incr]->stringval);
 			    }
-			    if (desc)
+			    if (desc[depth])
 				tst = -tst;
 			}
 
@@ -1349,20 +1348,11 @@
 cleanup:
     for (j = 0; j < nbsorts; j++) {
 	comp = sorts[j]->psvi;
-	if (tempstype[j] == 1) {
-	    /* The data-type needs to be recomputed each time */
-	    xmlFree((void *)(comp->stype));
-	    comp->stype = NULL;
-	}
-	if (temporder[j] == 1) {
-	    /* The order needs to be recomputed each time */
-	    xmlFree((void *)(comp->order));
-	    comp->order = NULL;
-	}
-	if (templang[j] == 1) {
-	    xsltFreeLocale(comp->locale);
-	    comp->locale = (xsltLocale)0;
-	}
+	if ((comp->lang == NULL) && (comp->has_lang != 0)) {
+            if (locale[j] != (xsltLocale)0) {
+                xsltFreeLocale(locale[j]);
+            }
+        }
 	if (resultsTab[j] != NULL) {
 	    for (i = 0;i < len;i++)
 		xmlXPathFreeObject(resultsTab[j][i]);