diff --git a/libxslt/attributes.c b/libxslt/attributes.c
index 0cf9d09cc3db7f3c5b1be318486e03bc11abbe7c_bGlieHNsdC9hdHRyaWJ1dGVzLmM=..06b7f9bd83afd1e7346a48e2465074553eb9b95b_bGlieHNsdC9hdHRyaWJ1dGVzLmM= 100644
--- a/libxslt/attributes.c
+++ b/libxslt/attributes.c
@@ -502,6 +502,7 @@
 	               const xmlChar *name, const xmlChar *ns,
 		       ATTRIBUTE_UNUSED const xmlChar *ignored) {
     int ret;
+    xsltAttrElemPtr topSet;
 
     ret = xmlHashAddEntry2(style->attributeSets, name, ns, values);
     if (ret < 0) {
@@ -510,6 +511,6 @@
 	 */
 #ifdef WITH_XSLT_DEBUG_ATTRIBUTES
 	xsltGenericDebug(xsltGenericDebugContext,
-		"attribute sets %s present already in top stylesheet\n",
-		         name);
+		"attribute set %s present already in top stylesheet"
+		" - merging\n", name);
 #endif
@@ -515,5 +516,14 @@
 #endif
-	xsltFreeAttrElem(values);
+	topSet = xmlHashLookup2(style->attributeSets, name, ns);
+	if (topSet==NULL) {
+	    xsltGenericError(xsltGenericErrorContext,
+	        "xsl:attribute-set : logic error merging from imports for"
+		" attribute-set %s\n", name);
+	} else {
+	    topSet = xsltMergeAttrElemList(topSet, values);
+	    xmlHashUpdateEntry2(style->attributeSets, name, ns, topSet, NULL);
+	}
+	xsltFreeAttrElemList(values);
 #ifdef WITH_XSLT_DEBUG_ATTRIBUTES
     } else {
 	xsltGenericDebug(xsltGenericDebugContext,
@@ -517,7 +527,7 @@
 #ifdef WITH_XSLT_DEBUG_ATTRIBUTES
     } else {
 	xsltGenericDebug(xsltGenericDebugContext,
-		"attribute sets %s moved to top stylesheet\n",
+		"attribute set %s moved to top stylesheet\n",
 		         name);
 #endif
     }