Index: SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServer.java
===================================================================
--- SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServer.java	(revision 6897)
+++ SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServer.java	(revision 6900)
@@ -228,5 +228,6 @@
 
             // diagnostics
-            writeDiagnosticList(out, ns, request.getDiagnostics());
+            writeDiagnosticList(out, ns, ns.getResponseNS(),
+                    request.getDiagnostics());
 
             // extraResponseData
@@ -359,5 +360,5 @@
 
             // diagnostics
-            writeDiagnosticList(out, ns, request.getDiagnostics());
+            writeDiagnosticList(out, ns, ns.getScanNS(), request.getDiagnostics());
 
             // extraResponseData
@@ -564,5 +565,6 @@
 
             // diagnostics
-            writeDiagnosticList(out, ns, request.getDiagnostics());
+            writeDiagnosticList(out, ns, ns.getResponseNS(),
+                    request.getDiagnostics());
 
             // extraResponseData
@@ -639,20 +641,28 @@
         }
 
-        out.setPrefix(ns.getResponsePrefix(), ns.getResponseNS());
         switch (operation) {
         case EXPLAIN:
+            out.setPrefix(ns.getResponsePrefix(), ns.getResponseNS());
             out.writeStartElement(ns.getResponseNS(), "explainResponse");
+            out.writeNamespace(ns.getResponsePrefix(), ns.getResponseNS());
+            // version
+            writeVersion(out, ns.getResponseNS(), version);
             break;
         case SCAN:
-            out.writeStartElement(ns.getResponseNS(), "scanResponse");
+            out.setPrefix(ns.getScanPrefix(), ns.getScanNS());
+            out.writeStartElement(ns.getScanNS(), "scanResponse");
+            out.writeNamespace(ns.getScanPrefix(), ns.getScanNS());
+            // version
+            writeVersion(out, ns.getScanNS(), version);
             break;
         case SEARCH_RETRIEVE:
+            out.setPrefix(ns.getResponsePrefix(), ns.getResponseNS());
             out.writeStartElement(ns.getResponseNS(), "searchRetrieveResponse");
+            out.writeNamespace(ns.getResponsePrefix(), ns.getResponseNS());
+            // version
+            writeVersion(out, ns.getResponseNS(), version);
             break;
         }
-        out.writeNamespace(ns.getResponsePrefix(), ns.getResponseNS());
-
-        // version
-        writeVersion(out, ns, version);
+
     }
 
@@ -701,7 +711,9 @@
             // 'explain' requires a complete explain record ...
             writeExplainRecord(out, ns, request);
+            writeDiagnosticList(out, ns, ns.getResponseNS(), diagnotics);
             break;
         case SCAN:
             // 'scan' fortunately does not need any elements ...
+            writeDiagnosticList(out, ns, ns.getScanNS(), diagnotics);
             break;
         case SEARCH_RETRIEVE:
@@ -710,7 +722,7 @@
             out.writeCharacters("0");
             out.writeEndElement(); // "numberOfRecords" element
+            writeDiagnosticList(out, ns, ns.getResponseNS(), diagnotics);
             break;
         }
-        writeDiagnosticList(out, ns, diagnotics);
         endResponse(out);
     }
@@ -718,8 +730,9 @@
 
     private void writeDiagnosticList(SRUXMLStreamWriter out, SRUNamespaces ns,
-            List<SRUDiagnostic> diagnostics) throws XMLStreamException {
+            String envelopeNs, List<SRUDiagnostic> diagnostics)
+                    throws XMLStreamException {
         if ((diagnostics != null) && !diagnostics.isEmpty()) {
             out.setPrefix(ns.getDiagnosticPrefix(), ns.getDiagnosticNS());
-            out.writeStartElement(ns.getDiagnosticNS(), "diagnostics");
+            out.writeStartElement(envelopeNs, "diagnostics");
             out.writeNamespace(ns.getDiagnosticPrefix(), ns.getDiagnosticNS());
             for (SRUDiagnostic diagnostic : diagnostics) {
@@ -952,5 +965,5 @@
         // echoedExplainRequest/version
         if (request.getRawVersion() != null) {
-            writeVersion(out, ns, request.getRawVersion());
+            writeVersion(out, ns.getResponseNS(), request.getRawVersion());
         }
 
@@ -979,5 +992,5 @@
         // echoedScanRequest/version
         if (request.getRawVersion() != null) {
-            writeVersion(out, ns, request.getRawVersion());
+            writeVersion(out, ns.getResponseNS(), request.getRawVersion());
         }
 
@@ -1029,5 +1042,5 @@
         // echoedSearchRetrieveRequest/version
         if (request.getRawVersion() != null) {
-            writeVersion(out, ns, request.getRawVersion());
+            writeVersion(out, ns.getResponseNS(), request.getRawVersion());
         }
 
@@ -1159,7 +1172,7 @@
 
 
-    private void writeVersion(SRUXMLStreamWriter out, SRUNamespaces ns,
+    private void writeVersion(SRUXMLStreamWriter out, String envelopeNs,
             SRUVersion version) throws XMLStreamException {
-        out.writeStartElement(ns.getResponseNS(), "version");
+        out.writeStartElement(envelopeNs, "version");
         switch (version) {
         case VERSION_1_1:
