diff --git a/src/libutil/xml-writer.cc b/src/libutil/xml-writer.cc
index cd37dff5608f0d6d7ce671ec9b92900ccb6ad4c5..27235933e7fb51cafa19f10574629df96e3625c6 100644
--- a/src/libutil/xml-writer.cc
+++ b/src/libutil/xml-writer.cc
@@ -89,6 +89,9 @@ void XMLWriter::writeAttrs(const XMLAttrs & attrs)
             if (c == '"') output << "&quot;";
             else if (c == '<') output << "&lt;";
             else if (c == '&') output << "&amp;";
+            /* Escape newlines to prevent attribute normalisation (see
+               XML spec, section 3.3.3. */
+            else if (c == '\n') output << "&#xA;";
             else output << c;
         }
         output << "\"";