diff artifacts/doc/conf/meta-data.xml @ 6264:e8f152163a1e double-precision

merge changes from default into double-precision branch
author Tom Gottfried <tom.gottfried@intevation.de>
date Tue, 11 Jun 2013 09:40:56 +0200
parents f58a4aafcdf6
children 1366057e017e
line wrap: on
line diff
--- a/artifacts/doc/conf/meta-data.xml	Wed Jun 05 11:56:38 2013 +0200
+++ b/artifacts/doc/conf/meta-data.xml	Tue Jun 11 09:40:56 2013 +0200
@@ -5,8 +5,12 @@
     <dc:comment>
       User specific part
       ------------------
+      This is a hack because we currently have no way to supress empty
+      folders (either in the client or in the datacage).
     </dc:comment>
-    <dc:if test="dc:contains($parameters, 'user-id') and not(dc:contains($artifact-outs, 'floodmap-hws'))">
+    <dc:if test="dc:contains($parameters, 'user-id') and
+      not(dc:contains($artifact-outs, 'floodmap-hws')) and
+      not($current-state-id = 'state.winfo.uesk.dgm')">
       <dc:call-macro name="load-user"/>
     </dc:if>
 
@@ -32,7 +36,7 @@
     </dc:macro>
 
     <dc:macro name="generate-system-content">
-      <dc:call-macro name="user-range">
+      <dc:call-macro name="artifact-range">
         <dc:call-macro name="km-filtered-wsts">
           <dc:choose>
             <dc:when test="dc:contains($parameters, 'recommended')">
@@ -186,6 +190,7 @@
                     <dc:call-macro name="basedata_1_additionals"/>
                     <dc:call-macro name="basedata_2_fixations"/>
                     <dc:call-macro name="basedata_3_officials"/>
+                    <dc:call-macro name="basedata_6_delta_w"/>
                     <dc:call-macro name="annotations_per_type"/>
                     <dc:call-macro name="morph_width"/>
                   </dc:when>
@@ -242,12 +247,6 @@
                   </dc:when>
                   <dc:when test="$out = 'minfo-heights'">
                     <dc:call-macro name="minfo-heights"/>
-                    <dc:macro name="minfo-heights">
-                      <bedheights>
-                        <dc:call-macro name="bed-heights-single"/>
-                        <dc:call-macro name="bed-heights-epoch"/>
-                      </bedheights>
-                    </dc:macro>
                   </dc:when>
                   <dc:when test="$out = 'minfo-heights-epoch'">
                     <bedheights>
@@ -255,7 +254,7 @@
                     </bedheights>
                   </dc:when>
                   <dc:when test="$out = 'waterlevels'">
-                    <dc:if test="current-state-id != 'state.winfo.uesk.wsp'">
+                    <dc:if test="$current-state-id != 'state.winfo.uesk.wsp'">
                       <dc:call-macro name="basedata_0"/>
                       <dc:call-macro name="basedata_1_additionals"/>
                       <dc:call-macro name="basedata_2_fixations"/>
@@ -272,949 +271,642 @@
     </dc:macro>
 
     <dc:macro name="generate-user-content">
-      <dc:call-macro name="user-range">
-        <dc:call-macro name="km-filtered-userdata">
-          <dc:choose>
-            <dc:when test="dc:contains($parameters, 'recommended')">
-              <dc:comment>
-                Recommendations (client shall load immediately).
-              </dc:comment>
-              <dc:iterate var="out" container="artifact-outs">
-                <dc:choose>
-                  <dc:when test="$out = 'longitudinal_section'">
-                    <dc:call-macro name="officiallines_user"/>
-                  </dc:when>
-                </dc:choose>
-              </dc:iterate>
-            </dc:when>
-            <dc:otherwise>
-              <dc:comment>
-                Non Recommendations
-              </dc:comment>
-              <dc:iterate var="out" container="artifact-outs">
-                <dc:choose>
-                  <dc:when test="$out = 'longitudinal_section'">
-                    <dc:call-macro name="longitudinal"/>
-                    <dc:call-macro name="differences"/>
-                  </dc:when>
-                  <dc:when test="$out = 'discharge_longitudinal_section'">
-                    <dc:call-macro name="longitudinal"/>
-                    <dc:call-macro name="differences"/>
-                  </dc:when>
-                  <dc:when test="$out = 'w_differences'">
-                    <dc:call-macro name="longitudinal"/>
-                    <dc:call-macro name="differences"/>
-                  </dc:when>
-                  <dc:when test="$out = 'fix_deltawt_curve'">
-                    <dc:call-macro name="delta-wt"/>
-                  </dc:when>
-                  <dc:when test="$out = 'reference_curve'">
-                    <dc:call-macro name="reference-curves"/>
-                  </dc:when>
-                  <dc:when test="$out = 'computed_discharge_curve'">
-                    <dc:call-macro name="computed-discharge-curve"/>
-                  </dc:when>
-                  <dc:when test="$out = 'cross_section'">
-                    <dc:call-macro name="waterlevels"/>
-                  </dc:when>
-                  <dc:when test="$out = 'fix_longitudinal_section_curve'">
-                    <dc:call-macro name="longitudinal-section"/>
-                  </dc:when>
-                  <dc:when test="$out = 'fix_derivate_curve'">
-                    <dc:call-macro name="fix-derivate-curve"/>
-                  </dc:when>
-                  <dc:when test="$out = 'fix_wq_curve'">
-                    <dc:call-macro name="fix-wq-curve"/>
-                  </dc:when>
-                  <dc:when test="$out = 'duration_curve'">
-                    <dc:call-macro name="duration-curve"/>
-                  </dc:when>
-                  <dc:when test="$out = 'waterlevels'">
-                    <dc:call-macro name="waterlevels-fix"/>
-                  </dc:when>
-                  <dc:when test="$out = 'fix_wq_curve'">
-                    <dc:call-macro name="waterlevels-fix"/>
-                  </dc:when>
-                  <dc:when test="$out = 'floodmap'">
-                    <dc:call-macro name="flood-map"/>
-                  </dc:when>
-                  <dc:when test="$out = 'map'">
-                    <dc:call-macro name="flood-map"/>
-                  </dc:when>
-                  <dc:when test="$out = 'bedheight_middle'">
-                    <dc:call-macro name="waterlevels-discharge"/>
-                    <dc:call-macro name="waterlevels-fix"/>
-                  </dc:when>
-                  <dc:when test="$out = 'floodmap-hws'">
-                    <dc:call-macro name="floodmap-hws-user"/>
-                  </dc:when>
-                  <dc:when test="$out = 'flow_velocity'">
-                    <dc:call-macro name="bedquality-bed"/>
-                    <dc:call-macro name="bedquality-load"/>
-                  </dc:when>
-                  <dc:when test="$out = 'bed_longitudinal_section'">
-                    <dc:call-macro name="bedquality-bed"/>
-                    <dc:call-macro name="bedquality-load"/>
-                    <dc:call-macro name="bedquality-density"/>
-                    <dc:call-macro name="bedquality-porosity"/>
-                  </dc:when>
-                  <dc:when test="$out = 'sedimentload_ls'">
-                    <dc:call-macro name="differences"/>
-                    <dc:call-macro name="bedheight-differences"/>
-                    <dc:call-macro name="flow-velocity"/>
-                    <dc:call-macro name="sediment-load"/>
-                  </dc:when>
-                  <dc:when test="$out = 'bed_difference_year'">
-                    <dc:call-macro name="waterlevels-discharge"/>
-                    <dc:call-macro name="bedheight-differences"/>
-                    <dc:call-macro name="differences"/>
-                    <dc:call-macro name="waterlevels-fix"/>
-                    <dc:call-macro name="delta-wt-ls"/>
-                  </dc:when>
-                  <dc:when test="$out = 'bed_difference_epoch'">
-                    <dc:call-macro name="waterlevels-discharge"/>
-                    <dc:call-macro name="bedheight-differences"/>
-                    <dc:call-macro name="differences"/>
-                    <dc:call-macro name="waterlevels-fix"/>
-                    <dc:call-macro name="delta-wt-ls"/>
-                  </dc:when>
-                </dc:choose>
-              </dc:iterate>
-            </dc:otherwise>
-          </dc:choose>
+      <dc:call-macro name="artifact-range">
+        <dc:call-macro name="all-user-artifacts">
+          <dc:call-macro name="km-filtered-user-artifacts">
+            <dc:choose>
+              <dc:when test="dc:contains($parameters, 'recommended')">
+                <dc:comment>
+                  Recommendations (user)
+                </dc:comment>
+                <dc:iterate var="out" container="artifact-outs">
+                  <dc:choose>
+                    <dc:when test="$out = 'longitudinal_section'">
+                      <dc:call-macro name="officiallines_user"/>
+                    </dc:when>
+                  </dc:choose>
+                </dc:iterate>
+              </dc:when>
+              <dc:otherwise>
+                <dc:comment>
+                  Non Recommendations (user)
+                </dc:comment>
+                <dc:iterate var="out" container="artifact-outs">
+                  <dc:choose>
+                    <dc:when test="$out = 'longitudinal_section'">
+                      <dc:call-macro name="longitudinal"/>
+                      <dc:call-macro name="differences"/>
+                    </dc:when>
+                    <dc:when test="$out = 'discharge_longitudinal_section'">
+                      <dc:call-macro name="longitudinal"/>
+                      <dc:call-macro name="differences"/>
+                    </dc:when>
+                    <dc:when test="$out = 'w_differences'">
+                      <dc:call-macro name="longitudinal"/>
+                      <dc:call-macro name="differences"/>
+                    </dc:when>
+                    <dc:when test="$out = 'fix_deltawt_curve'">
+                      <dc:call-macro name="delta-wt"/>
+                    </dc:when>
+                    <dc:when test="$out = 'reference_curve'">
+                      <dc:call-macro name="reference-curves"/>
+                    </dc:when>
+                    <dc:when test="$out = 'computed_discharge_curve'">
+                      <dc:call-macro name="computed-discharge-curve"/>
+                    </dc:when>
+                    <dc:when test="$out = 'cross_section'">
+                      <dc:call-macro name="waterlevels"/>
+                    </dc:when>
+                    <dc:when test="$out = 'fix_longitudinal_section_curve'">
+                      <dc:call-macro name="longitudinal-section"/>
+                    </dc:when>
+                    <dc:when test="$out = 'fix_derivate_curve'">
+                      <dc:call-macro name="fix-derivate-curve"/>
+                    </dc:when>
+                    <dc:when test="$out = 'fix_wq_curve'">
+                      <dc:call-macro name="fix-wq-curve"/>
+                    </dc:when>
+                    <dc:when test="$out = 'duration_curve'">
+                      <dc:call-macro name="duration-curve"/>
+                    </dc:when>
+                    <dc:when test="$out = 'waterlevels'">
+                      <dc:call-macro name="waterlevels-fix"/>
+                    </dc:when>
+                    <dc:when test="$out = 'fix_wq_curve'">
+                      <dc:call-macro name="waterlevels-fix"/>
+                    </dc:when>
+                    <dc:when test="$out = 'floodmap'">
+                      <dc:call-macro name="flood-map"/>
+                    </dc:when>
+                    <dc:when test="$out = 'map'">
+                      <dc:call-macro name="flood-map"/>
+                    </dc:when>
+                    <dc:when test="$out = 'bedheight_middle'">
+                      <dc:call-macro name="waterlevels-discharge"/>
+                      <dc:call-macro name="waterlevels-fix"/>
+                    </dc:when>
+                    <dc:when test="$out = 'floodmap-hws'">
+                      <dc:call-macro name="floodmap-hws-user"/>
+                    </dc:when>
+                    <dc:when test="$out = 'flow_velocity'">
+                      <dc:call-macro name="bedquality-bed"/>
+                      <dc:call-macro name="bedquality-load"/>
+                    </dc:when>
+                    <dc:when test="$out = 'bed_longitudinal_section'">
+                      <dc:call-macro name="bedquality-bed"/>
+                      <dc:call-macro name="bedquality-load"/>
+                      <dc:call-macro name="bedquality-density"/>
+                      <dc:call-macro name="bedquality-porosity"/>
+                    </dc:when>
+                    <dc:when test="$out = 'sedimentload_ls'">
+                      <dc:call-macro name="differences"/>
+                      <dc:call-macro name="bedheight-differences"/>
+                      <dc:call-macro name="flow-velocity"/>
+                      <dc:call-macro name="sediment-load"/>
+                    </dc:when>
+                    <dc:when test="$out = 'bed_difference_year'">
+                      <dc:call-macro name="waterlevels-discharge"/>
+                      <dc:call-macro name="bedheight-differences"/>
+                      <dc:call-macro name="differences"/>
+                      <dc:call-macro name="waterlevels-fix"/>
+                      <dc:call-macro name="delta-wt-ls"/>
+                    </dc:when>
+                    <dc:when test="$out = 'bed_difference_epoch'">
+                      <dc:call-macro name="waterlevels-discharge"/>
+                      <dc:call-macro name="bedheight-differences"/>
+                      <dc:call-macro name="differences"/>
+                      <dc:call-macro name="waterlevels-fix"/>
+                      <dc:call-macro name="delta-wt-ls"/>
+                    </dc:when>
+                  </dc:choose>
+                </dc:iterate>
+              </dc:otherwise>
+            </dc:choose>
+          </dc:call-macro>
         </dc:call-macro>
       </dc:call-macro>
     </dc:macro>
 
     <!-- Macros to load user data -->
 
-    <dc:macro name="select-facets">
-      <dc:context>
-        <dc:statement>
-          SELECT a.gid         AS aid,
-                 f.id          AS fid,
-                 f.name        AS facet_name,
-                 f.num         AS facet_num,
-                 f.description AS facet_description
-          FROM artifacts a
-            JOIN outs o   ON o.artifact_id = a.id
-            JOIN facets f ON f.out_id = o.id
-          WHERE a.id = ${a_id}
-            AND f.name = ${facet_type}
-        </dc:statement>
+    <dc:macro name="differences">
+      <dc:filter expr="$facet_name = 'w_differences'">
         <dc:if test="dc:has-result()">
-          <dc:macro-body/>
+          <differences>
+            <dc:for-each>
+              <w_differences
+                description="{$facet_description}"
+                factory="winfo" target_out="{$out}"
+                artifact-id="{$aid}"
+                ids="{$aid}"
+                out="w_differences"/>
+            </dc:for-each>
+          </differences>
         </dc:if>
-      </dc:context>
-    </dc:macro>
-
-    <dc:macro name="differences">
-      <dc:variable name="facet_type" value="w_differences"/>
-      <dc:call-macro name="select-facets">
-        <differences>
-          <dc:for-each>
-            <w_differences
-              description="{$facet_description}"
-              factory="winfo"
-              artifact-id="{$aid}"
-              ids="{$aid}"
-              out="w_differences"/>
-          </dc:for-each>
-        </differences>
-      </dc:call-macro>
+      </dc:filter>
     </dc:macro>
 
     <dc:macro name="reference-curves">
-      <dc:variable name="facet_type" value="reference_curve"/>
-      <dc:call-macro name="select-facets">
-        <reference_curves>
-          <dc:for-each>
-            <dc:element name="${facet_name}">
-              <dc:attribute name="description" value="${facet_description}"/>
-              <dc:attribute name="factory" value="winfo"/>
-              <dc:attribute name="artifact-id" value="${aid}"/>
-              <dc:attribute name="ids" value="${aid}"/>
-              <dc:attribute name="out" value="reference_curve"/>
-            </dc:element>
-          </dc:for-each>
-        </reference_curves>
-      </dc:call-macro>
+      <dc:filter expr="$facet_name = 'reference_curve'">
+        <dc:if test="dc:has-result()">
+          <reference_curves>
+            <dc:for-each>
+              <dc:element name="${facet_name}">
+                <dc:attribute name="description" value="${facet_description}"/>
+                <dc:attribute name="factory" value="winfo"/>
+                <dc:attribute name="target_out" value="${out}"/>
+                <dc:attribute name="artifact-id" value="${a_id}"/>
+                <dc:attribute name="ids" value="${a_id}"/>
+                <dc:attribute name="out" value="reference_curve"/>
+              </dc:element>
+            </dc:for-each>
+          </reference_curves>
+        </dc:if>
+      </dc:filter>
     </dc:macro>
 
     <dc:macro name="computed-discharge-curve">
-      <dc:variable name="facet_type" value="computed_discharge_curve.q"/>
-      <dc:call-macro name="select-facets">
-        <computed_discharge_curves>
-          <dc:for-each>
-            <dc:element name="${facet_name}">
-              <dc:attribute name="description" value="${facet_description}"/>
-              <dc:attribute name="factory" value="winfo"/>
-              <dc:attribute name="artifact-id" value="${aid}"/>
-              <dc:attribute name="ids" value="${aid}"/>
-              <dc:attribute name="out" value="computed_discharge_curve"/>
-            </dc:element>
-          </dc:for-each>
-        </computed_discharge_curves>
-      </dc:call-macro>
-    </dc:macro>
-
-    <dc:macro name="flood-map">
-      <dc:variable name="facet_type" value="floodmap.wsplgen"/>
-      <dc:call-macro name="select-facets">
-        <floodmap>
-          <dc:for-each>
-            <dc:element name="${facet_name}">
-              <dc:attribute name="description" value="${facet_description}"/>
-              <dc:attribute name="factory" value="winfo"/>
-              <dc:attribute name="artifact-id" value="${aid}"/>
-              <dc:attribute name="ids" value="${aid}"/>
-              <dc:attribute name="out" value="floodmap"/>
-            </dc:element>
-          </dc:for-each>
-        </floodmap>
-      </dc:call-macro>
-    </dc:macro>
-
-    <dc:macro name="waterlevels">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS out_id
-          FROM outs
-          WHERE artifact_id = ${a_id} AND name = 'cross_section'
-        </dc:statement>
-        <dc:if test="dc:has-result()">
-          <waterlevels>
-            <dc:for-each>
-              <dc:context>
-                <dc:statement>
-                  SELECT name AS facet_name, num AS facet_num, description AS facet_description
-                    FROM facets
-                  WHERE out_id = ${out_id}
-                    ORDER BY num ASC, name DESC
-                </dc:statement>
-                <longitudinal_section_columns description="{$river} {$a_creation}">
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}"/>
-                      <dc:attribute name="factory" value="winfo"/>
-                      <dc:attribute name="artifact-id" value="${a_gid}"/>
-                      <dc:attribute name="out" value="cross_section"/>
-                    </dc:element>
-                  </dc:for-each>
-                </longitudinal_section_columns>
-              </dc:context>
-            </dc:for-each>
-          </waterlevels>
-        </dc:if>
-      </dc:context>
-    </dc:macro>
-
-    <dc:macro name="longitudinal">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS out_id
-          FROM outs
-          WHERE artifact_id = ${a_id}
-            AND name = 'longitudinal_section'
-        </dc:statement>
-        <dc:if test="dc:has-result()">
-          <waterlevels>
-            <dc:for-each>
-              <dc:context>
-                <dc:statement>
-                  SELECT name        AS facet_name,
-                         num         AS facet_num,
-                         description AS facet_description
-                  FROM facets
-                  WHERE out_id = ${out_id}
-                    ORDER BY num ASC, name DESC
-                </dc:statement>
-                <longitudinal_section_columns description="{$river} {$a_creation}">
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}"/>
-                      <dc:attribute name="factory" value="winfo"/>
-                      <dc:attribute name="artifact-id" value="${a_gid}"/>
-                      <dc:attribute name="out" value="longitudinal_section"/>
-                    </dc:element>
-                  </dc:for-each>
-                </longitudinal_section_columns>
-              </dc:context>
-            </dc:for-each>
-          </waterlevels>
-        </dc:if>
-      </dc:context>
-    </dc:macro>
-
-    <dc:macro name="longitudinal-section">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS out_id
-          FROM outs
-          WHERE artifact_id = ${a_id}
-            AND name = 'fix_longitudinal_section_curve'
-        </dc:statement>
-        <dc:if test="dc:has-result()">
-          <waterlevels>
-            <dc:for-each>
-              <dc:context>
-                <dc:statement>
-                  SELECT name        AS facet_name,
-                         num         AS facet_num,
-                         description AS facet_description
-                  FROM facets
-                  WHERE out_id = ${out_id} AND (
-                      name LIKE 'fix_deviation_ls%'       OR
-                      name LIKE 'fix_sector_average_ls%'  OR
-                      name LIKE 'fix_analysis_events_ls%' OR
-                      name LIKE 'fix_reference_events_ls%')
-                  ORDER BY num ASC, name DESC
-                </dc:statement>
-                <waterlevels description="{$river} {$a_creation} {$collection_name}">
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}"/>
-                      <dc:attribute name="factory" value="fixanalysis"/>
-                      <dc:attribute name="artifact-id" value="${a_gid}"/>
-                      <dc:attribute name="out" value="fix_longitudinal_section_curve"/>
-                    </dc:element>
-                  </dc:for-each>
-                </waterlevels>
-              </dc:context>
-            </dc:for-each>
-          </waterlevels>
-        </dc:if>
-      </dc:context>
-    </dc:macro>
-
-    <dc:macro name="delta-wt">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS out_id
-          FROM outs
-          WHERE artifact_id = ${a_id}
-            AND name = 'fix_deltawt_curve'
-        </dc:statement>
-        <dc:if test="dc:has-result()">
-          <waterlevels>
-            <dc:for-each>
-              <dc:context>
-                <dc:statement>
-                  SELECT name        AS facet_name,
-                         num         AS facet_num,
-                         description AS facet_description
-                  FROM facets
-                  WHERE out_id = ${out_id} and (
-                    name LIKE 'fix_sector_average_dwt%' OR
-                    name LIKE 'fix_deviation_dwt%'      OR
-                    name = 'fix_analysis_events_dwt'    OR
-                    name = 'fix_reference_events_dwt'   OR
-                    name = 'fix_analysis_periods_dwt')
-                  ORDER BY num ASC, name DESC
-                </dc:statement>
-                <waterlevels description="{$river} {$a_creation} {$collection_name}">
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}"/>
-                      <dc:attribute name="factory" value="fixanalysis"/>
-                      <dc:attribute name="artifact-id" value="${a_gid}"/>
-                      <dc:attribute name="out" value="fix_deltawt_curve"/>
-                    </dc:element>
-                  </dc:for-each>
-                </waterlevels>
-              </dc:context>
-            </dc:for-each>
-          </waterlevels>
-        </dc:if>
-      </dc:context>
-    </dc:macro>
-
-    <dc:macro name="delta-wt-ls">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS out_id
-          FROM outs
-          WHERE artifact_id = ${a_id} AND name = 'fix_deltawt_curve'
-        </dc:statement>
-        <dc:if test="dc:has-result()">
-          <waterlevels>
-            <dc:for-each>
-              <dc:context>
-                <dc:statement>
-                  SELECT name AS facet_name, num AS facet_num, description AS facet_description
-                  FROM facets
-                  WHERE out_id = ${out_id} and (
-                    name LIKE 'fix_sector_average_dwt%' OR
-                    name LIKE 'fix_deviation_dwt%')
-                  ORDER BY num ASC, name DESC
-                </dc:statement>
-                <waterlevels description="{$river} {$a_creation} {$collection_name}">
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}"/>
-                      <dc:attribute name="factory" value="fixanalysis"/>
-                      <dc:attribute name="artifact-id" value="${a_gid}"/>
-                      <dc:attribute name="out" value="fix_deltawt_curve"/>
-                    </dc:element>
-                  </dc:for-each>
-                </waterlevels>
-              </dc:context>
-            </dc:for-each>
-          </waterlevels>
-        </dc:if>
-      </dc:context>
-    </dc:macro>
-
-    <dc:macro name="fix-derivate-curve">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS out_id
-          FROM outs
-          WHERE artifact_id = ${a_id} AND name = 'fix_derivate_curve'
-        </dc:statement>
-        <dc:if test="dc:has-result()">
-          <waterlevels>
-            <dc:for-each>
-              <dc:context>
-                <dc:statement>
-                  SELECT name AS facet_name, num AS facet_num, description AS facet_description
-                  FROM facets
-                  WHERE out_id = ${out_id} and name = 'fix_derivate_curve'
-                  ORDER BY num ASC, name DESC
-                </dc:statement>
-                <waterlevels description="{$river} {$a_creation} {$collection_name}">
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}"/>
-                      <dc:attribute name="factory" value="fixanalysis"/>
-                      <dc:attribute name="artifact-id" value="${a_gid}"/>
-                      <dc:attribute name="out" value="fix_derivate_curve"/>
-                    </dc:element>
-                  </dc:for-each>
-                </waterlevels>
-              </dc:context>
-            </dc:for-each>
-          </waterlevels>
-        </dc:if>
-      </dc:context>
-    </dc:macro>
-
-    <dc:macro name="fix-wq-curve">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS out_id
-          FROM outs
-          WHERE artifact_id = ${a_id} AND name = 'fix_wq_curve'
-        </dc:statement>
-        <dc:if test="dc:has-result()">
-          <waterlevels>
-            <dc:for-each>
-              <dc:context>
-                <dc:statement>
-                  SELECT name AS facet_name, num AS facet_num, description AS facet_description
-                  FROM facets
-                  WHERE out_id = ${out_id} and (
-                    name LIKE 'fix_sector_average_wq%'  OR
-                    name = 'fix_wq_curve'               OR
-                    name LIKE 'fix_analysis_events_wq%' OR
-                    name LIKE 'fix_reference_events_wq%' )
-                  ORDER BY num ASC, name DESC
-                </dc:statement>
-                <waterlevels description="{$river} {$a_creation} {$collection_name}">
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}"/>
-                      <dc:attribute name="factory" value="fixanalysis"/>
-                      <dc:attribute name="artifact-id" value="${a_gid}"/>
-                      <dc:attribute name="out" value="fix_wq_curve"/>
-                    </dc:element>
-                  </dc:for-each>
-                </waterlevels>
-              </dc:context>
-            </dc:for-each>
-          </waterlevels>
-        </dc:if>
-      </dc:context>
-    </dc:macro>
-
-    <dc:macro name="duration-curve">
-      <dc:context>
-        <dc:statement>
-          SELECT a.gid         AS aid,
-                 f.id          AS fid,
-                 f.name        AS facet_name,
-                 f.num         AS facet_num,
-                 f.description AS facet_description
-          FROM outs AS o, facets AS f, artifacts AS a
-          WHERE
-              (f.name = 'duration_curve.q' OR f.name = 'duration_curve.w') AND
-              f.out_id = o.id         AND
-              o.artifact_id = ${a_id} AND
-              a.id = ${a_id}
-        </dc:statement>
+      <dc:filter expr="$facet_name = 'computed_discharge_curve.q'">
         <dc:if test="dc:has-result()">
           <computed_discharge_curves>
             <dc:for-each>
               <dc:element name="${facet_name}">
                 <dc:attribute name="description" value="${facet_description}"/>
                 <dc:attribute name="factory" value="winfo"/>
-                <dc:attribute name="artifact-id" value="${aid}"/>
-                <dc:attribute name="ids" value="${aid}"/>
+                <dc:attribute name="target_out" value="${out}"/>
+                <dc:attribute name="artifact-id" value="${a_id}"/>
+                <dc:attribute name="ids" value="${a_id}"/>
+                <dc:attribute name="out" value="computed_discharge_curve"/>
+              </dc:element>
+            </dc:for-each>
+          </computed_discharge_curves>
+        </dc:if>
+      </dc:filter>
+    </dc:macro>
+
+    <dc:macro name="flood-map">
+      <dc:filter expr="$facet_name = 'floodmap.wsplgen'">
+        <dc:if test="dc:has-result()">
+          <floodmap>
+            <dc:for-each>
+              <dc:variable name="combined_desc" expr="concat($facet_description, ' ', $a_creation, ' ', $collection_name)"/>
+              <dc:element name="${facet_name}">
+                <dc:attribute name="description" value="${combined_desc}"/>
+                <dc:attribute name="factory" value="winfo"/>
+                <dc:attribute name="target_out" value="${out}"/>
+                <dc:attribute name="artifact-id" value="${a_id}"/>
+                <dc:attribute name="ids" value="${a_id}"/>
+                <dc:attribute name="out" value="floodmap"/>
+              </dc:element>
+            </dc:for-each>
+          </floodmap>
+        </dc:if>
+      </dc:filter>
+    </dc:macro>
+
+    <dc:macro name="waterlevels">
+      <dc:filter expr="$out_name = 'cross_section'">
+        <dc:if test="dc:has-result()">
+          <waterlevels>
+            <dc:group expr="concat($river, ' ', $a_creation)">
+              <dc:comment>Aheinecke: Why is this grouping different from the rest?</dc:comment>
+              <longitudinal_section_columns description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}"/>
+                    <dc:attribute name="factory" value="winfo"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="artifact-id" value="${a_gid}"/>
+                    <dc:attribute name="out" value="cross_section"/>
+                  </dc:element>
+                </dc:for-each>
+              </longitudinal_section_columns>
+            </dc:group>
+          </waterlevels>
+        </dc:if>
+      </dc:filter>
+    </dc:macro>
+
+    <dc:macro name="longitudinal">
+      <dc:filter expr="$out_name = 'longitudinal_section'">
+        <dc:if test="dc:has-result()">
+          <waterlevels>
+            <dc:group expr="concat($river, ' ', $a_creation)">
+              <dc:comment>Aheinecke: Why is this grouping different from the rest?</dc:comment>
+              <longitudinal_section_columns description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}"/>
+                    <dc:attribute name="factory" value="winfo"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="artifact-id" value="${a_gid}"/>
+                    <dc:attribute name="out" value="longitudinal_section"/>
+                  </dc:element>
+                </dc:for-each>
+              </longitudinal_section_columns>
+            </dc:group>
+          </waterlevels>
+        </dc:if>
+      </dc:filter>
+    </dc:macro>
+
+    <dc:macro name="longitudinal-section">
+      <dc:filter expr="$out_name = 'fix_longitudinal_section_curve' and
+        (starts-with($facet_name, 'fix_deviation_ls') or
+        starts-with($facet_name, 'fix_sector_average_ls') or
+        starts-with($facet_name, 'fix_analysis_events_ls') or
+        starts-with($facet_name, 'fix_reference_events_ls'))">
+        <dc:if test="dc:has-result()">
+          <waterlevels>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <waterlevels description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}"/>
+                    <dc:attribute name="factory" value="fixanalysis"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="artifact-id" value="${a_gid}"/>
+                    <dc:attribute name="out" value="fix_longitudinal_section_curve"/>
+                  </dc:element>
+                </dc:for-each>
+              </waterlevels>
+            </dc:group>
+          </waterlevels>
+        </dc:if>
+      </dc:filter>
+    </dc:macro>
+
+    <dc:macro name="delta-wt">
+      <dc:filter expr="$out_name = 'fix_deltawt_curve' and
+        ($facet_name = 'fix_analysis_events_dwt' or
+        $facet_name = 'fix_analysis_periods_dwt' or
+        $facet_name = 'fix_reference_events_dwt' or
+        starts-with($facet_name, 'fix_deviation_dwt') or
+        starts-with($facet_name, 'fix_sector_average_dwt'))">
+        <dc:if test="dc:has-result()">
+          <waterlevels>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <waterlevels description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}"/>
+                    <dc:attribute name="factory" value="fixanalysis"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="artifact-id" value="${a_gid}"/>
+                    <dc:attribute name="out" value="fix_deltawt_curve"/>
+                  </dc:element>
+                </dc:for-each>
+              </waterlevels>
+            </dc:group>
+          </waterlevels>
+        </dc:if>
+      </dc:filter>
+    </dc:macro>
+
+    <dc:macro name="delta-wt-ls">
+      <dc:filter expr="$out_name = 'fix_deltawt_curve' and
+        (starts-with($facet_name, 'fix_sector_average_dwt') or
+        starts-with($facet_name, 'fix_deviation_dwt'))">
+        <dc:if test="dc:has-result()">
+          <waterlevels>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <waterlevels description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}"/>
+                    <dc:attribute name="factory" value="fixanalysis"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="artifact-id" value="${a_gid}"/>
+                    <dc:attribute name="out" value="fix_deltawt_curve"/>
+                  </dc:element>
+                </dc:for-each>
+              </waterlevels>
+            </dc:group>
+          </waterlevels>
+        </dc:if>
+      </dc:filter>
+    </dc:macro>
+
+    <dc:macro name="fix-derivate-curve">
+      <dc:filter expr="$out_name = 'fix_derivate_curve' and $facet_name = 'fix_derivate_curve'">
+        <dc:if test="dc:has-result()">
+          <waterlevels>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <waterlevels description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}"/>
+                    <dc:attribute name="factory" value="fixanalysis"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="artifact-id" value="${a_gid}"/>
+                    <dc:attribute name="out" value="fix_derivate_curve"/>
+                  </dc:element>
+                </dc:for-each>
+              </waterlevels>
+            </dc:group>
+          </waterlevels>
+        </dc:if>
+      </dc:filter>
+    </dc:macro>
+
+    <dc:macro name="fix-wq-curve">
+      <dc:filter expr="$out_name = 'fix_wq_curve' and
+        (starts-with($facet_name, 'fix_analysis_events_wq') or
+        starts-with($facet_name, 'fix_reference_events_wq') or
+        starts-with($facet_name, 'fix_sector_average_wq') or
+        $facet_name = 'fix_wq_curve')">
+        <dc:if test="dc:has-result()">
+          <waterlevels>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <waterlevels description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}"/>
+                    <dc:attribute name="factory" value="fixanalysis"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="artifact-id" value="${a_gid}"/>
+                    <dc:attribute name="out" value="fix_wq_curve"/>
+                  </dc:element>
+                </dc:for-each>
+              </waterlevels>
+            </dc:group>
+          </waterlevels>
+        </dc:if>
+      </dc:filter>
+    </dc:macro>
+
+    <dc:macro name="duration-curve">
+      <dc:filter expr="$facet_name = 'duration_curve.q' or $facet_name = 'duration_curve.w'">
+        <dc:if test="dc:has-result()">
+          <computed_discharge_curves>
+            <dc:for-each>
+              <dc:element name="${facet_name}">
+                <dc:attribute name="description" value="${facet_description}"/>
+                <dc:attribute name="factory" value="winfo"/>
+                <dc:attribute name="target_out" value="${out}"/>
+                <dc:attribute name="artifact-id" value="${a_id}"/>
+                <dc:attribute name="ids" value="${a_id}"/>
                 <dc:attribute name="out" value="duration_curve"/>
               </dc:element>
             </dc:for-each>
           </computed_discharge_curves>
         </dc:if>
-      </dc:context>
+      </dc:filter>
     </dc:macro>
 
-    <dc:comment>TODO doesnt work nicely for fix/wq-diags.</dc:comment>
+    <dc:comment>TODO doesnt work nicely for fix/wq-diags. Aheinecke (27.5.2013): Why?</dc:comment>
     <dc:macro name="waterlevels-fix">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS out_id
-          FROM outs
-          WHERE artifact_id = ${a_id} AND name = 'longitudinal_section'
-        </dc:statement>
+      <dc:filter expr="$out_name = 'longitudinal_section' and $facet_name = 'longitudinal_section.w'">
         <dc:if test="dc:has-result()">
           <waterlevels>
-            <dc:for-each>
-              <dc:context>
-                <dc:statement>
-                  SELECT name AS facet_name, num AS facet_num, description AS facet_description
-                  FROM facets
-                  WHERE out_id = ${out_id} and name = 'longitudinal_section.w'
-                  ORDER BY num ASC, name DESC
-                </dc:statement>
-                <waterlevels description="{$river} {$a_creation} {$collection_name}">
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}"/>
-                      <dc:attribute name="factory" value="winfo"/>
-                      <dc:attribute name="artifact-id" value="${a_gid}"/>
-                      <dc:attribute name="out" value="longitudinal_section"/>
-                    </dc:element>
-                  </dc:for-each>
-                </waterlevels>
-              </dc:context>
-            </dc:for-each>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <waterlevels description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}"/>
+                    <dc:attribute name="factory" value="winfo"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="artifact-id" value="${a_gid}"/>
+                    <dc:attribute name="out" value="longitudinal_section"/>
+                  </dc:element>
+                </dc:for-each>
+              </waterlevels>
+            </dc:group>
           </waterlevels>
         </dc:if>
-      </dc:context>
+      </dc:filter>
     </dc:macro>
 
     <dc:macro name="floodmap-hws-user">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS out_id
-          FROM outs
-          WHERE artifact_id = ${a_id} AND name = 'floodmap'
-        </dc:statement>
-        <dc:for-each>
-          <dc:context>
-            <dc:statement>
-              SELECT name        AS facet_name,
-                     num         AS facet_num,
-                     description AS facet_description
-              FROM facets
-              WHERE out_id = ${out_id} AND name = 'floodmap.usershape'
-              ORDER BY num ASC, name DESC
-            </dc:statement>
-            <own-hws>
-              <dc:for-each>
-                <dc:element name="${facet_name}">
-                  <dc:attribute name="description" value="${facet_description}"/>
-                  <dc:attribute name="ids" value="${facet_num}"/>
-                  <dc:attribute name="factory" value="winfo"/>
-                  <dc:attribute name="artifact-id" value="${a_gid}"/>
-                  <dc:attribute name="out" value="floodmap"/>
-                </dc:element>
-              </dc:for-each>
-            </own-hws>
-          </dc:context>
-        </dc:for-each>
-      </dc:context>
+      <dc:comment>No grouping in this?</dc:comment>
+      <dc:filter expr="$out_name = 'floodmap' and $facet_name = 'floodmap.usershape'">
+        <own-hws>
+          <dc:for-each>
+            <dc:element name="${facet_name}">
+              <dc:attribute name="description" value="${facet_description}"/>
+              <dc:attribute name="ids" value="${facet_num}"/>
+              <dc:attribute name="factory" value="winfo"/>
+              <dc:attribute name="target_out" value="${out}"/>
+              <dc:attribute name="artifact-id" value="${a_gid}"/>
+              <dc:attribute name="out" value="floodmap"/>
+            </dc:element>
+          </dc:for-each>
+        </own-hws>
+      </dc:filter>
     </dc:macro>
 
     <dc:macro name="bedquality-bed">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS oid
-          FROM outs AS o
-          WHERE
-            o.artifact_id = ${a_id} AND
-            o.name='bed_longitudinal_section'
-        </dc:statement>
+      <dc:filter expr="$out_name = 'bed_longitudinal_section' and
+        ($facet_name = 'bed_longitudinal_section.bed_diameter_toplayer' or
+        $facet_name = 'bed_longitudinal_section.bed_diameter_sublayer')">
         <dc:if test="dc:has-result()">
           <bed_quality_bed>
-            <dc:for-each>
-              <quality-bed description="{$river} {$a_creation} {$collection_name}">
-                <dc:context>
-                  <dc:statement>
-                    SELECT a.gid         AS aid,
-                           f.id          AS fid,
-                           f.name        AS facet_name,
-                           f.num         AS facet_num,
-                           f.description AS facet_description
-                    FROM outs AS o, facets AS f, artifacts AS a
-                    WHERE
-                      (f.name = 'bed_longitudinal_section.bed_diameter_toplayer'
-                       OR f.name = 'bed_longitudinal_section.bed_diameter_sublayer') AND
-                       f.out_id = o.id         AND
-                       o.artifact_id = ${a_id} AND
-                       a.id = ${a_id}
-                  </dc:statement>
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="factory" value="minfo"/>
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
-                      <dc:attribute name="artifact-id" value="${aid}"/>
-                      <dc:attribute name="out" value="bed_longitudinal_section"/>
-                    </dc:element>
-                  </dc:for-each>
-                </dc:context>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <quality-bed description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="factory" value="minfo"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
+                    <dc:attribute name="artifact-id" value="${a_id}"/>
+                    <dc:attribute name="out" value="bed_longitudinal_section"/>
+                  </dc:element>
+                </dc:for-each>
               </quality-bed>
-            </dc:for-each>
+            </dc:group>
           </bed_quality_bed>
         </dc:if>
-      </dc:context>
+      </dc:filter>
     </dc:macro>
 
     <dc:macro name="bedquality-load">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS oid
-          FROM outs AS o
-          WHERE
-            o.artifact_id = ${a_id} AND
-            o.name='bed_longitudinal_section'
-        </dc:statement>
+      <dc:filter expr="$out_name = 'bed_longitudinal_section' and
+        $facet_name = 'bed_longitudinal_section.bedload_diameter'">
         <dc:if test="dc:has-result()">
           <bed_quality_load>
-            <dc:for-each>
-              <quality-load description="{$river} {$a_creation} {$collection_name}">
-                <dc:context>
-                  <dc:statement>
-                    SELECT a.gid         AS aid,
-                           f.id          AS fid,
-                           f.name        AS facet_name,
-                           f.num         AS facet_num,
-                           f.description AS facet_description
-                    FROM outs AS o, facets AS f, artifacts AS a
-                    WHERE
-                       f.name = 'bed_longitudinal_section.bedload_diameter' AND
-                       f.out_id = o.id         AND
-                       o.artifact_id = ${a_id} AND
-                       a.id = ${a_id}
-                  </dc:statement>
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="factory" value="minfo"/>
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
-                      <dc:attribute name="artifact-id" value="${aid}"/>
-                      <dc:attribute name="out" value="bed_longitudinal_section"/>
-                    </dc:element>
-                  </dc:for-each>
-                </dc:context>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <quality-load description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="factory" value="minfo"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
+                    <dc:attribute name="artifact-id" value="${a_id}"/>
+                    <dc:attribute name="out" value="bed_longitudinal_section"/>
+                  </dc:element>
+                </dc:for-each>
               </quality-load>
-            </dc:for-each>
+            </dc:group>
           </bed_quality_load>
         </dc:if>
-      </dc:context>
+      </dc:filter>
     </dc:macro>
 
     <dc:macro name="bedquality-density">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS oid
-          FROM outs AS o
-          WHERE
-            o.artifact_id = ${a_id} AND
-            o.name='bed_longitudinal_section'
-        </dc:statement>
+      <dc:filter expr="$out_name = 'bed_longitudinal_section' and
+        ($facet_name = 'bed_longitudinal_section.sediment_density_toplayer' or
+         $facet_name = 'bed_longitudinal_section.sediment_density_sublayer')">
         <dc:if test="dc:has-result()">
           <bed_quality_density>
-            <dc:for-each>
-              <density description="{$river} {$a_creation} {$collection_name}">
-                <dc:context>
-                  <dc:statement>
-                    SELECT a.gid         AS aid,
-                           f.id          AS fid,
-                           f.name        AS facet_name,
-                           f.num         AS facet_num,
-                           f.description AS facet_description
-                    FROM outs AS o, facets AS f, artifacts AS a
-                    WHERE
-                       (f.name = 'bed_longitudinal_section.sediment_density_toplayer' OR
-                        f.name = 'bed_longitudinal_section.sediment_density_sublayer') AND
-                       f.out_id = o.id         AND
-                       o.artifact_id = ${a_id} AND
-                       a.id = ${a_id}
-                  </dc:statement>
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="factory" value="minfo"/>
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
-                      <dc:attribute name="artifact-id" value="${aid}"/>
-                      <dc:attribute name="out" value="bed_longitudinal_section"/>
-                    </dc:element>
-                  </dc:for-each>
-                </dc:context>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <density description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="factory" value="minfo"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
+                    <dc:attribute name="artifact-id" value="${a_id}"/>
+                    <dc:attribute name="out" value="bed_longitudinal_section"/>
+                  </dc:element>
+                </dc:for-each>
               </density>
-            </dc:for-each>
+            </dc:group>
           </bed_quality_density>
         </dc:if>
-      </dc:context>
+      </dc:filter>
     </dc:macro>
 
     <dc:macro name="bedquality-porosity">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS oid
-          FROM outs AS o
-          WHERE
-            o.artifact_id = ${a_id} AND
-            o.name='bed_longitudinal_section'
-        </dc:statement>
+      <dc:filter expr="$out_name = 'bed_longitudinal_section' and
+        ($facet_name = 'bed_longitudinal_section.porosity_toplayer' or
+        $facet_name = 'bed_longitudinal_section.porosity_sublayer')">
         <dc:if test="dc:has-result()">
           <bed_quality_porosity>
-            <dc:for-each>
-              <porosity description="{$river} {$a_creation} {$collection_name}">
-                <dc:context>
-                  <dc:statement>
-                    SELECT a.gid         AS aid,
-                           f.id          AS fid,
-                           f.name        AS facet_name,
-                           f.num         AS facet_num,
-                           f.description AS facet_description
-                    FROM outs AS o, facets AS f, artifacts AS a
-                    WHERE
-                       (f.name = 'bed_longitudinal_section.porosity_toplayer' OR
-                        f.name = 'bed_longitudinal_section.porosity_sublayer') AND
-                       f.out_id = o.id         AND
-                       o.artifact_id = ${a_id} AND
-                       a.id = ${a_id}
-                  </dc:statement>
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="factory" value="minfo"/>
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
-                      <dc:attribute name="artifact-id" value="${aid}"/>
-                      <dc:attribute name="out" value="bed_longitudinal_section"/>
-                    </dc:element>
-                  </dc:for-each>
-                </dc:context>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <porosity description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="factory" value="minfo"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
+                    <dc:attribute name="artifact-id" value="${a_id}"/>
+                    <dc:attribute name="out" value="bed_longitudinal_section"/>
+                  </dc:element>
+                </dc:for-each>
               </porosity>
-            </dc:for-each>
+            </dc:group>
           </bed_quality_porosity>
         </dc:if>
-      </dc:context>
+      </dc:filter>
     </dc:macro>
 
     <dc:macro name="flow-velocity">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS oid
-          FROM outs AS o
-          WHERE
-            o.artifact_id = ${a_id} and o.name='flow_velocity'
-        </dc:statement>
+      <dc:filter expr="$out_name = 'flow_velocity' and
+        ($facet_name = 'flow_velocity.totalchannel' or
+         $facet_name = 'flow_velocity.mainchannel' or
+         $facet_name = 'flow_velocity.totalchannel.filtered' or
+         $facet_name = 'flow_velocity.mainchannel.filtered')">
         <dc:if test="dc:has-result()">
           <flow-velocity>
-            <dc:for-each>
-              <flow description="{$river} {$a_creation} {$collection_name}">
-                <dc:context>
-                  <dc:statement>
-                    SELECT a.gid         AS aid,
-                           f.id          AS fid,
-                           f.name        AS facet_name,
-                           f.num         AS facet_num,
-                           f.description AS facet_description
-                    FROM outs AS o, facets AS f, artifacts AS a
-                    WHERE
-                      (f.name = 'flow_velocity.totalchannel' OR
-                       f.name = 'flow_velocity.mainchannel' OR
-                       f.name = 'flow_velocity.totalchannel.filtered' OR
-                       f.name = 'flow_velocity.mainchannel.filtered') AND
-                       f.out_id = o.id         AND
-                       o.artifact_id = ${a_id} AND
-                       a.id = ${a_id}
-                  </dc:statement>
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="factory" value="minfo"/>
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
-                      <dc:attribute name="artifact-id" value="${aid}"/>
-                      <dc:attribute name="out" value="flow_velocity"/>
-                    </dc:element>
-                  </dc:for-each>
-                </dc:context>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <flow description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="factory" value="minfo"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
+                    <dc:attribute name="artifact-id" value="${a_id}"/>
+                    <dc:attribute name="out" value="flow_velocity"/>
+                  </dc:element>
+                </dc:for-each>
               </flow>
-            </dc:for-each>
+            </dc:group>
           </flow-velocity>
         </dc:if>
-      </dc:context>
+      </dc:filter>
     </dc:macro>
 
     <dc:macro name="sediment-load">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS oid
-          FROM outs AS o
-          WHERE
-            o.artifact_id = ${a_id} and o.name='sedimentload_ls'
-        </dc:statement>
+      <dc:filter expr="$out_name = 'sedimentload_ls' and starts-with($facet_name, 'sedimentload')">
         <dc:if test="dc:has-result()">
           <sediment-load>
-            <dc:for-each>
-              <load description="{$river} {$a_creation} {$collection_name}">
-                <dc:context>
-                  <dc:statement>
-                    SELECT a.gid         AS aid,
-                           f.id          AS fid,
-                           f.name        AS facet_name,
-                           f.num         AS facet_num,
-                           f.description AS facet_description
-                    FROM outs AS o, facets AS f, artifacts AS a
-                    WHERE f.name LIKE 'sedimentload%' AND
-                          f.out_id = o.id         AND
-                          o.artifact_id = ${a_id} AND
-                          a.id = ${a_id}
-                  </dc:statement>
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="factory" value="minfo"/>
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
-                      <dc:attribute name="artifact-id" value="${aid}"/>
-                      <dc:attribute name="out" value="sedimentload_ls"/>
-                    </dc:element>
-                  </dc:for-each>
-                </dc:context>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <load description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="factory" value="minfo"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
+                    <dc:attribute name="artifact-id" value="${a_id}"/>
+                    <dc:attribute name="out" value="sedimentload_ls"/>
+                  </dc:element>
+                </dc:for-each>
               </load>
-            </dc:for-each>
+            </dc:group>
           </sediment-load>
         </dc:if>
-      </dc:context>
+      </dc:filter>
     </dc:macro>
 
     <dc:macro name="bedheight-differences">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS oid,
-                 name AS o_name
-          FROM outs AS o
-          WHERE
-            o.artifact_id = ${a_id} AND
-            (o.name = 'bed_difference_year' OR
-             o.name = 'bed_differnece_epoch')
-        </dc:statement>
+      <dc:filter expr="($out_name = 'bed_difference_year' or $out_name = 'bed_difference_epoch') and
+        (starts-with($facet_name, 'bedheight_difference.year') or starts-with($facet_name, 'bedheight_difference.epoch'))">
         <dc:if test="dc:has-result()">
           <bedheight-differences>
-            <dc:for-each>
-              <difference description="{$river} {$a_creation} {$collection_name}">
-                <dc:context>
-                  <dc:statement>
-                    SELECT a.gid         AS aid,
-                           f.id          AS fid,
-                           f.name        AS facet_name,
-                           f.num         AS facet_num,
-                           f.description AS facet_description
-                    FROM outs AS o, facets AS f, artifacts AS a
-                    WHERE (f.name LIKE 'bedheight_difference.year%' OR
-                           f.name LIKE 'bedheight_difference.epoch%') AND
-                          f.out_id = o.id         AND
-                          o.artifact_id = ${a_id} AND
-                          a.id = ${a_id}
-                  </dc:statement>
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="factory" value="minfo"/>
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
-                      <dc:attribute name="artifact-id" value="${aid}"/>
-                      <dc:choose>
-                        <dc:when test="dc:contains($o_name, 'bed_difference_year')">
-                          <dc:attribute name="out" value="bed_difference_year"/>
-                        </dc:when>
-                        <dc:otherwise>
-                          <dc:attribute name="out" value="bed_difference_epoch"/>
-                        </dc:otherwise>
-                      </dc:choose>
-                    </dc:element>
-                  </dc:for-each>
-                </dc:context>
+            <dc:group expr="concat($river, ' ', $a_creation, ' ', $collection_name)">
+              <difference description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="factory" value="minfo"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
+                    <dc:attribute name="artifact-id" value="${a_id}"/>
+                    <dc:attribute name="out" value="${out}"/>
+                  </dc:element>
+                </dc:for-each>
               </difference>
-            </dc:for-each>
+            </dc:group>
           </bedheight-differences>
         </dc:if>
-      </dc:context>
+      </dc:filter>
     </dc:macro>
 
     <dc:macro name="waterlevels-discharge">
-      <dc:context>
-        <dc:statement>
-          SELECT id AS oid
-          FROM outs AS o
-          WHERE
-            o.artifact_id = ${a_id} AND
-            o.name = 'discharge_longitudinal_section'
-        </dc:statement>
+      <dc:filter expr="$out_name = 'discharge_longitudinal_section' and $facet_name = 'discharge_longitudinal_section.w">
         <dc:if test="dc:has-result()">
           <waterlevels-discharge>
-            <dc:for-each>
-              <discharge description="{$oid} {$river} {$a_id} {$a_creation} {$collection_name}">
-                <dc:context>
-                  <dc:statement>
-                    SELECT a.gid         AS aid,
-                           f.id          AS fid,
-                           f.name        AS facet_name,
-                           f.num         AS facet_num,
-                           f.description AS facet_description
-                    FROM outs AS o, facets AS f, artifacts AS a
-                    WHERE f.name = 'discharge_longitudinal_section.w' AND
-                          f.out_id = o.id         AND
-                          o.artifact_id = ${a_id} AND
-                          a.id = ${a_id}
-                  </dc:statement>
-                  <dc:for-each>
-                    <dc:element name="${facet_name}">
-                      <dc:attribute name="factory" value="winfo"/>
-                      <dc:attribute name="description" value="${facet_description}"/>
-                      <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
-                      <dc:attribute name="artifact-id" value="${aid}"/>
-                      <dc:attribute name="out" value="longitudinal_section"/>
-                    </dc:element>
-                  </dc:for-each>
-                </dc:context>
+            <dc:group expr="concat($oid, ' ', $river, ' ', $a_id, ' ', $a_creation, ' ', $collection_name)">
+              <discharge description="{dc:group-key()}">
+                <dc:for-each>
+                  <dc:element name="${facet_name}">
+                    <dc:attribute name="factory" value="winfo"/>
+                    <dc:attribute name="target_out" value="${out}"/>
+                    <dc:attribute name="description" value="${facet_description}"/>
+                    <dc:attribute name="ids" value="${facet_num}-${facet_name}"/>
+                    <dc:attribute name="artifact-id" value="${a_id}"/>
+                    <dc:attribute name="out" value="longitudinal_section"/>
+                  </dc:element>
+                </dc:for-each>
               </discharge>
-            </dc:for-each>
+            </dc:group>
           </waterlevels-discharge>
         </dc:if>
-      </dc:context>
+      </dc:filter>
     </dc:macro>
     <!-- Macros to load system data -->
 
     <dc:macro name="annotations">
-      <annotation factory="annotations" ids="{$river_id}"/>
+      <annotation factory="annotations" ids="{$river_id}" target_out="{$out}" />
     </dc:macro>
 
     <dc:macro name="mainvalues">
-      <wmainvalue factory="mainvalue" ids="{$river_id}:w"/>
-      <qmainvalue factory="mainvalue" ids="{$river_id}:q"/>
+      <wmainvalue factory="mainvalue" ids="{$river_id}:w" target_out="{$out}" />
+      <qmainvalue factory="mainvalue" ids="{$river_id}:q" target_out="{$out}" />
     </dc:macro>
 
     <dc:macro name="cross_sections">
@@ -1232,7 +924,7 @@
           <dc:for-each>
             <cross_section name="{$prot_description}"
                            ids="{$prot_id}"
-                           factory="crosssections"/>
+                           factory="crosssections" target_out="{$out}" />
           </dc:for-each>
         </dc:context>
       </cross_sections>
@@ -1251,9 +943,9 @@
               AND he.km BETWEEN ${fromkm} AND ${tokm}
           </dc:statement>
           <dc:for-each>
-            <hyk name="{$hyk_description}" 
+            <hyk name="{$hyk_description}"
                  ids="{$hyk_id}"
-                 factory="hyk"/>
+                 factory="hyk" target_out="{$out}" />
           </dc:for-each>
         </dc:context>
       </hyks>
@@ -1279,7 +971,7 @@
       <dc:call-macro name="fixings-macro">
         <column name="{$wst_column_name}"
                 ids="fixations-wstv-{$wst_column_position}-{$wst_id}"
-                factory="wqinterpol"
+                factory="wqinterpol" target_out="{$out}"
                 info="{$info} [km {$deffrom} - {$defto}]"/>
       </dc:call-macro>
     </dc:macro>
@@ -1288,7 +980,7 @@
       <dc:call-macro name="fixings-macro">
         <column name="{$wst_column_name}"
                 ids="fixations-wstv-{$wst_column_position}-{$wst_id}"
-                factory="staticwqkms"
+                factory="staticwqkms" target_out="{$out}"
                 info="{$info} [km {$deffrom} - {$defto}]"/>
       </dc:call-macro>
     </dc:macro>
@@ -1302,7 +994,7 @@
                 <dc:for-each>
                   <column name="{$wst_column_name}"
                           ids="additionals-wstv-{$wst_column_position}-{$wst_id}"
-                          factory="staticwqkms"
+                          factory="staticwqkms" target_out="{$out}"
                           info="{$info} [km {$deffrom} - {$defto}]"/>
                 </dc:for-each>
               </official>
@@ -1321,7 +1013,7 @@
                 <dc:for-each>
                   <column name="{$wst_column_name}"
                           ids="heightmarks_points-wstv-{$wst_column_position}-{$wst_id}"
-                          factory="staticwkms"
+                          factory="staticwkms" target_out="{$out}"
                           info="{$info} [km {$deffrom} - {$defto}]"/>
                 </dc:for-each>
               </heightmark>
@@ -1340,7 +1032,7 @@
                 <dc:for-each>
                   <column name="{$wst_column_name}"
                           ids="heightmarks_points-wstv-{$wst_column_position}-{$wst_id}"
-                          factory="staticwkms"
+                          factory="staticwkms" target_out="{$out}"
                           info="{$info} [km {$deffrom} - {$defto}]"/>
                 </dc:for-each>
               </relativepoint>
@@ -1369,7 +1061,7 @@
                 <dc:for-each>
                   <column name="{$wst_column_name}"
                           ids="flood_protection-wstv-{$wst_column_position}-{$wst_id}"
-                          factory="staticwkms"
+                          factory="staticwkms" target_out="{$out}"
                           info="{$info} [km {$deffrom} - {$defto}]"/>
                 </dc:for-each>
               </flood_protection>
@@ -1382,7 +1074,7 @@
     <dc:macro name="annotations_per_type">
       <annotations>
         <dc:context>
-          <all_annotations factory="annotations" ids="{$river_id}"/>
+          <all_annotations factory="annotations" ids="{$river_id}" target_out="{$out}" />
           <dc:statement>
             SELECT id   AS anno_id,
                    name AS anno_description
@@ -1390,7 +1082,7 @@
           </dc:statement>
           <dc:for-each>
             <annotation name="{$anno_description}"
-                        factory="annotations"
+                        factory="annotations" target_out="{$out}"
                         ids="{$river_id}:{$anno_description}"/>
           </dc:for-each>
         </dc:context>
@@ -1406,7 +1098,7 @@
                 <dc:for-each>
                   <column name="{$wst_column_name}"
                           ids="fixations-wstv-{$wst_column_position}-{$wst_id}"
-                          factory="staticwkms"
+                          factory="staticwkms" target_out="{$out}"
                           info="{$info} [km {$deffrom} - {$defto}]"/>
                 </dc:for-each>
               </relativepoint>
@@ -1425,7 +1117,7 @@
                 <dc:for-each>
                   <column name="{$wst_column_name}"
                           ids="flood_protection-wstv-{$wst_column_position}-{$wst_id}"
-                          factory="staticwkms"
+                          factory="staticwkms" target_out="{$out}"
                           info="{$info} [km {$deffrom} - {$defto}]"/>
                 </dc:for-each>
               </relativepoint>
@@ -1444,7 +1136,7 @@
                 <dc:for-each>
                   <column name="{$wst_column_name}"
                           ids="additionals-wstv-{$wst_column_position}-{$wst_id}"
-                          factory="staticwkms"
+                          factory="staticwkms" target_out="{$out}"
                           info="{$info} [km {$deffrom} - {$defto}]"/>
                 </dc:for-each>
               </relativepoint>
@@ -1454,6 +1146,25 @@
       </dc:filter>
     </dc:macro>
 
+    <dc:macro name="basedata_6_delta_w">
+      <dc:filter expr="$kind=6">
+        <dc:if test="dc:has-result()">
+          <delta_w>
+            <dc:group expr="$wst_description">
+              <relativepoint name="{dc:group-key()}">
+                <dc:for-each>
+                  <column name="{$wst_column_name}"
+                          ids="delta_w-wstv-{$wst_column_position}-{$wst_id}"
+                          factory="staticwkms" target_out="{$out}"
+                          info="{$info} [km {$deffrom} - {$defto}]"/>
+                </dc:for-each>
+              </relativepoint>
+            </dc:group>
+          </delta_w>
+        </dc:if>
+      </dc:filter>
+    </dc:macro>
+
     <dc:macro name="discharge_table_gauge">
       <dc:context>
         <dc:statement>
@@ -1466,7 +1177,7 @@
             <discharge_table_gauge>
               <dc:for-each>
                 <gauge name="{$gauge_name}"
-                  factory="gaugedischarge"
+                  factory="gaugedischarge" target_out="{$out}"
                   from="{$g_start}"
                   to="{$g_stop}"
                   ids="{$gauge_name}"/>
@@ -1488,15 +1199,15 @@
     </dc:macro>
 
     <dc:macro name="qsectors">
-      <qsector factory="qsectors" ids="{$river_id}"/>
+      <qsector factory="qsectors" ids="{$river_id}" target_out="{$out}" />
     </dc:macro>
 
     <dc:macro name="flood-map-recommended">
       <kilometrage>
-        <riveraxis factory="riveraxis" ids="{$river_id}"/>
+        <riveraxis factory="riveraxis" ids="{$river_id}" target_out="{$out}" />
       </kilometrage>
       <rastermap>
-        <background factory="wmsbackground" ids="{$river_id}"/>
+        <background factory="wmsbackground" ids="{$river_id}" target_out="{$out}" />
       </rastermap>
     </dc:macro>
 
@@ -1514,10 +1225,12 @@
             SELECT id          AS bedh_id,
                    year        AS bedh_year,
                    description AS bedh_descr
-            FROM bed_height_single WHERE river_id = ${river_id}
+            FROM bed_height_single
+            WHERE river_id = ${river_id}
+              AND lower(description) NOT LIKE '%epoch%'
           </dc:statement>
           <dc:for-each>
-            <height factory="bedheight"
+            <height factory="bedheight" target_out="{$out}"
                     ids="bedheight-single-{$bedh_id}-{$bedh_year}"
                     description="{$bedh_descr}"/>
           </dc:for-each>
@@ -1529,14 +1242,16 @@
       <epoch>
         <dc:context>
           <dc:statement>
-            SELECT id               AS bedh_id,
-                   time_interval_id AS bedh_interval_id,
-                   description      AS bedh_descr
-            FROM bed_height_epoch WHERE river_id = ${river_id}
+            SELECT id          AS bedh_id,
+                   year        AS bedh_year,
+                   description AS bedh_descr
+            FROM bed_height_single
+            WHERE river_id = ${river_id}
+              AND lower(description) LIKE '%epoch%'
           </dc:statement>
           <dc:for-each>
-            <height factory="bedheight"
-                    ids="bedheight-epoch-{$bedh_id}-{$bedh_interval_id}"
+            <height factory="bedheight" target_out="{$out}"
+                    ids="bedheight-single-{$bedh_id}-{$bedh_year}"
                     description="{$bedh_descr}"/>
           </dc:for-each>
         </dc:context>
@@ -1556,7 +1271,7 @@
               <dc:comment>TODO: Why has this an id and factory?</dc:comment>
               <flow_velocity_measurement name="{$fvmd}"
                                          ids="{$fvmid}"
-                                         factory="flowvelocity">
+                                         factory="flowvelocity" target_out="{$out}" >
                 <dc:context>
                   <dc:statement>
                     SELECT id, description, station, datetime, v, w, q
@@ -1567,7 +1282,7 @@
                   <dc:for-each>
                     <measurement_value name="{$description} - {$station} - {$datetime}"
                                        ids="{$id}"
-                                       factory="flowvelocity"/>
+                                       factory="flowvelocity" target_out="{$out}" />
                   </dc:for-each>
                 </dc:context>
               </flow_velocity_measurement>
@@ -1597,7 +1312,7 @@
                 <dc:for-each>
                   <morphologic-width name="{$from_station} - {$to_station}"
                                      ids="{$width_id}"
-                                     factory="morph-width"/>
+                                     factory="morph-width" target_out="{$out}" />
                 </dc:for-each>
               </dc:context>
             </dc:for-each>
@@ -1617,7 +1332,7 @@
         <dc:if test="dc:has-result()">
           <soundings_width>
             <dc:for-each>
-              <height factory="bedheight"
+              <height factory="bedheight" target_out="{$out}"
                       ids="bedheight-singlevalues-{$bedh_id}-{$bedh_year}"
                       description="{$bedh_descr}"/>
             </dc:for-each>
@@ -1646,7 +1361,7 @@
       <dc:call-macro name="basedata_0_macro">
         <column name="{$wst_column_name}"
                 ids="base_data-wstv-{$wst_column_position}-{$wst_id}"
-                factory="staticwqkms"
+                factory="staticwqkms" target_out="{$out}"
                 info="{$info} [km {$deffrom} - {$defto}]"/>
       </dc:call-macro>
     </dc:macro>
@@ -1655,7 +1370,7 @@
       <dc:call-macro name="basedata_0_macro">
         <column name="{$wst_column_name}"
                 ids="base_data-wstv-{$wst_column_position}-{$wst_id}"
-                factory="wqinterpol"
+                factory="wqinterpol" target_out="{$out}"
                 info="{$info} [km {$deffrom} - {$defto}]"/>
       </dc:call-macro>
     </dc:macro>
@@ -1680,7 +1395,7 @@
       <dc:call-macro name="basedata_1_additionals_macro">
         <column name="{$wst_column_name}"
                 ids="additionals-wstv-{$wst_column_position}-{$wst_id}"
-                factory="staticwqkms"
+                factory="staticwqkms" target_out="{$out}"
                 info="{$info} [km {$deffrom} - {$defto}]"/>
       </dc:call-macro>
     </dc:macro>
@@ -1689,7 +1404,7 @@
       <dc:call-macro name="basedata_1_additionals_macro">
         <column name="{$wst_column_name}"
                 ids="additionalsmarks-wstv-{$wst_column_position}-{$wst_id}"
-                factory="staticwkms"
+                factory="staticwkms" target_out="{$out}"
                 info="{$info} [km {$deffrom} - {$defto}]"/>
       </dc:call-macro>
     </dc:macro>
@@ -1733,10 +1448,10 @@
          FIXME: Following two macros look identical to me.
       </dc:comment>
       <kilometrage>
-        <riveraxis factory="riveraxis" ids="{$river_id}"/>
+        <riveraxis factory="riveraxis" ids="{$river_id}" target_out="{$out}" />
       </kilometrage>
       <rastermap>
-        <background factory="wmsbackground" ids="{$river_id}"/>
+        <background factory="wmsbackground" ids="{$river_id}" target_out="{$out}" />
       </rastermap>
     </dc:macro>
 
@@ -1750,21 +1465,22 @@
                    d.name  AS name,
                    t.start_time AS start_time,
                    t.stop_time AS stop_time,
-                   'Projektion: ' || d.projection || '$' ||
-                   'Rasterweite: '   || d.resolution || 'm$' ||
-                   'Format: '  || d.format || '$' ||
+                   'Projektion: ' || d.projection || '&lt;BR&gt;' ||
+                   'Rasterweite: '   || d.resolution || 'm&lt;BR&gt;' ||
+                   'Format: '  || d.format || '&lt;BR&gt;' ||
                    'Zeitraum: '
                    AS info
             FROM dem d
               JOIN ranges r ON d.range_id = r.id
               LEFT JOIN time_intervals t ON d.time_interval_id = t.id
-            WHERE d.river_id = ${river_id}
-          </dc:statement>
-          <dc:for-each>
-            <dem factory="demfactory" 
+              WHERE d.river_id = ${river_id}
+              AND NOT((${tokm} &lt; r.a) or (${fromkm} &gt; r.b))
+            </dc:statement>
+            <dc:for-each>
+            <dem factory="demfactory" target_out="{$out}"
               ids="{$dem_id}"
               name="{$name}"
-              info="{dc:replace($info, '$', '&lt;BR&gt;')}{dc:date-format('yyyy', $start_time)} - {dc:date-format('yyyy', $stop_time)}"/>
+              info="{$info}{dc:date-format('yyyy', $start_time)} - {dc:date-format('yyyy', $stop_time)}"/>
           </dc:for-each>
         </dc:context>
       </dems>
@@ -1774,7 +1490,7 @@
       <dc:macro name="durchlass_damm_graben">
         <dc:macro name="ddg_factory">
           <dc:for-each>
-            <hws factory="hwsfactory" name="{$hws_name}"/>
+            <hws factory="hwsfactory" name="{$hws_name}" target_out="{$out}" />
           </dc:for-each>
         </dc:macro>
 
@@ -1861,7 +1577,7 @@
               AND kind = 2
           </dc:statement>
           <dc:for-each>
-            <line factory="wmshydrboundariesfactory" 
+            <line factory="wmshydrboundariesfactory" target_out="{$out}"
                   ids="{$river_id};{$name};2"
                   name="{$name}"/>
           </dc:for-each>
@@ -1875,7 +1591,7 @@
               AND kind = 2
           </dc:statement>
           <dc:for-each>
-            <line factory="wmshydrboundariespolyfactory"
+            <line factory="wmshydrboundariespolyfactory" target_out="{$out}"
                   ids="{$river_id};{$name};2"
                   name="{$name}"/>
           </dc:for-each>
@@ -1896,7 +1612,7 @@
         <dc:if test="dc:has-result()">
           <lines>
             <dc:for-each>
-              <line factory="wmshydrboundariesfactory"
+              <line factory="wmshydrboundariesfactory" target_out="{$out}"
                     ids="{$river_id};{$name};1"
                     name="{$name}"/>
             </dc:for-each>
@@ -1921,7 +1637,7 @@
             <sobek_areas>
               <dc:for-each>
                 <boundary name="{$sectie}"
-                          factory="wmshydrboundariespolyfactory"
+                          factory="wmshydrboundariespolyfactory" target_out="{$out}"
                           ids="{$river_id};{$sectie};1;{$sectie_id};-1"/>
               </dc:for-each>
             </sobek_areas>
@@ -1941,7 +1657,7 @@
             <sobek_flooded>
               <dc:for-each>
                 <boundary name="{$sobek}"
-                          factory="wmshydrboundariespolyfactory"
+                          factory="wmshydrboundariespolyfactory" target_out="{$out}"
                           ids="{$river_id};{$sobek};1;-1;{$sobek_id}"/>
               </dc:for-each>
             </sobek_flooded>
@@ -1959,7 +1675,7 @@
           </dc:statement>
           <dc:for-each>
             <boundary name="{$name}"
-                      factory="wmshydrboundariespolyfactory"
+                      factory="wmshydrboundariespolyfactory" target_out="{$out}"
                       ids="{$river_id};{$name}"/>
           </dc:for-each>
         </dc:context>
@@ -1990,7 +1706,7 @@
                     <dc:if test="dc:has-result()">
                       <bfg>
                         <dc:for-each>
-                          <floodmaps factory="wmsfloodmapsfactory"
+                          <floodmaps factory="wmsfloodmapsfactory" target_out="{$out}"
                             ids="{$river_id};{$name}"
                             name="{$name}"/>
                         </dc:for-each>
@@ -2006,7 +1722,7 @@
                     <dc:if test="dc:has-result()">
                       <federal>
                         <dc:for-each>
-                          <floodmaps factory="wmsfloodmapsfactory"
+                          <floodmaps factory="wmsfloodmapsfactory" target_out="{$out}"
                             ids="{$river_id};{$name}"
                             name="{$name}"/>
                         </dc:for-each>
@@ -2032,7 +1748,7 @@
                     <dc:if test="dc:has-result()">
                       <bfg>
                         <dc:for-each>
-                          <floodmaps factory="wmsfloodmapsfactory"
+                          <floodmaps factory="wmsfloodmapsfactory" target_out="{$out}"
                             ids="{$river_id};{$name}"
                             name="{$name}"/>
                         </dc:for-each>
@@ -2049,7 +1765,7 @@
                     <dc:if test="dc:has-result()">
                       <federal>
                         <dc:for-each>
-                          <floodmaps factory="wmsfloodmapsfactory"
+                          <floodmaps factory="wmsfloodmapsfactory" target_out="{$out}"
                             ids="{$river_id};{$name}"
                             name="{$name}"/>
                         </dc:for-each>
@@ -2080,7 +1796,7 @@
                         ${source}
                       </dc:statement>
                       <dc:for-each>
-                        <floodmaps factory="wmsfloodmapsfactory"
+                        <floodmaps factory="wmsfloodmapsfactory" target_out="{$out}"
                           ids="{$river_id};{$name}"
                           name="{$name}"/>
                       </dc:for-each>
@@ -2110,19 +1826,21 @@
           <floodplain>
             <dc:filter expr="$kind_id=1">
               <dc:for-each>
-                <floody factory="wmsfloodplainfactory"
+                <floody factory="wmsfloodplainfactory" target_out="{$out}"
                         description="{$kind_name}"
                         ids="{$river_id};{$kind_name};{$kind_id}"/>
               </dc:for-each>
             </dc:filter>
-            <dc:filter expr="kind_id!=1">
-              <other>
-                <dc:for-each>
-                  <floody factory="wmsfloodplainfactory"
-                          description="{$layer_name}"
-                          ids="{$river_id};{$layer_name};{$kind_id}"/>
-                </dc:for-each>
-              </other>
+            <dc:filter expr="$kind_id != 1">
+              <dc:if test="dc:has-result()">
+                <other>
+                  <dc:for-each>
+                    <floody factory="wmsfloodplainfactory" target_out="{$out}"
+                            description="{$layer_name}"
+                            ids="{$river_id};{$layer_name};{$kind_id}"/>
+                  </dc:for-each>
+                </other>
+              </dc:if>
             </dc:filter>
           </floodplain>
         </dc:if>
@@ -2137,7 +1855,7 @@
 
       <dc:macro name="hwspoints_by_kind_factory">
         <dc:for-each>
-          <hws factory="wmshwspointsfactory"
+          <hws factory="wmshwspointsfactory" target_out="{$out}"
                ids="{$river_id};{$hws_name}"
                name="{$hws_name}"/>
         </dc:for-each>
@@ -2268,7 +1986,7 @@
 
       <dc:macro name="hwslines_by_kind_factory">
         <dc:for-each>
-          <hws factory="wmshwslinesfactory"
+          <hws factory="wmshwslinesfactory" target_out="{$out}"
                ids="{$river_id};{$hws_name}"
                name="{$hws_name}"/>
         </dc:for-each>
@@ -2403,7 +2121,7 @@
           <floodmarks>
             <dc:for-each>
               <floodmark name="{$year}"
-                factory="wmsfloodmarkfactory"
+                factory="wmsfloodmarkfactory" target_out="{$out}"
                 ids="{$river_id};{$year};{$year}"/>
             </dc:for-each>
           </floodmarks>
@@ -2413,7 +2131,7 @@
 
     <dc:macro name="catchments">
       <catchments>
-        <catchment_wms factory="externalwmsfactory"
+        <catchment_wms factory="externalwmsfactory" target_out="{$out}"
                        ids="http://geoportal.bafg.de/wmsproxy/INSPIRE/DrainageBasin;HY.PHYSICALWATERS.CATCHMENTS;Einzugsgebiete (WMS)" />
         <dc:call-macro name="flood-map-gaugelocations"/>
       </catchments>
@@ -2421,10 +2139,10 @@
 
     <dc:macro name="flood-map-gaugelocations">
       <gaugelocations>
-        <gauge_points factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelpunkte;Pegelpunkte (WSV)"/>
-        <gauge_names factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelnamen;Pegelnamen (WSV)"/>
-        <gauge_level factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelwasserstand;Aktueller Wasserstand (WSV)"/>
-        <gauge_tendency factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;TendenzWasserstand;Tendenz des Wasserstands (WSV)"/>
+        <gauge_points factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelpunkte;Pegelpunkte (WSV)" target_out="{$out}" />
+        <gauge_names factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelnamen;Pegelnamen (WSV)" target_out="{$out}" />
+        <gauge_level factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;Pegelwasserstand;Aktueller Wasserstand (WSV)" target_out="{$out}" />
+        <gauge_tendency factory="externalwmsfactory" ids="http://www.pegelonline.wsv.de/webservices/gis/wms;TendenzWasserstand;Tendenz des Wasserstands (WSV)" target_out="{$out}" />
       </gaugelocations>
     </dc:macro>
 
@@ -2445,7 +2163,7 @@
               <dc:for-each>
                 <actual description="{$kind_name}"
                         ids="{$river_id};{$kind_name};{$kind_id}"
-                        factory="riveraxis"/>
+                        factory="riveraxis" target_out="{$out}" />
               </dc:for-each>
             </dc:filter>
             <dc:filter expr="$kind_id!=1">
@@ -2453,7 +2171,7 @@
                 <dc:for-each>
                   <misc-axis description="{$layer_name}"
                              ids="{$river_id};{$layer_name};{$kind_id}"
-                             factory="riveraxis"/>
+                             factory="riveraxis" target_out="{$out}" />
                 </dc:for-each>
               </other>
             </dc:filter>
@@ -2468,7 +2186,7 @@
           SELECT DISTINCT 1 FROM river_axes_km WHERE river_id = ${river_id}
         </dc:statement>
          <dc:for-each>
-            <kilometrage factory="wmskmfactory" ids="{$river_id}"/>
+            <kilometrage factory="wmskmfactory" ids="{$river_id}" target_out="{$out}" />
         </dc:for-each>
       </dc:context>
     </dc:macro>
@@ -2482,7 +2200,7 @@
             GROUP BY name
         </dc:statement>
         <dc:for-each>
-          <fixpoints factory="wmsfixpointsfactory"
+          <fixpoints factory="wmsfixpointsfactory" target_out="{$out}"
             ids="{$river_id};{$name}"/>
         </dc:for-each>
       </dc:context>
@@ -2504,18 +2222,20 @@
             <dc:filter expr="$kind_id=1">
               <dc:for-each>
                 <actual description="{$kind_name}"
-                        factory="wmsqpsfactory"
+                        factory="wmsqpsfactory" target_out="{$out}"
                         ids="{$river_id};{$kind_name};{$kind_id}"/>
               </dc:for-each>
             </dc:filter>
-            <dc:filter expr="kind_id=0">
-              <other>
-                <dc:for-each>
-                  <misc-qps description="{$layer_name}"
-                            factory="wmsqpsfactory"
-                            ids="{$river_id};{$layer_name};{$kind_id}"/>
-                </dc:for-each>
-              </other>
+            <dc:filter expr="$kind_id = 0">
+              <dc:if test="dc:has-result()">
+                <other>
+                  <dc:for-each>
+                    <misc-qps description="{$layer_name}"
+                              factory="wmsqpsfactory" target_out="{$out}"
+                              ids="{$river_id};{$layer_name};{$kind_id}"/>
+                  </dc:for-each>
+                </other>
+              </dc:if>
             </dc:filter>
           </qps>
         </dc:if>
@@ -2538,7 +2258,7 @@
             <dc:filter expr="$building_kind_id!=0">
               <dc:for-each>
                 <buildings description="{$building_kind}"
-                           factory="wmsbuildingsfactory"
+                           factory="wmsbuildingsfactory" target_out="{$out}"
                            ids="{$river_id};{$building_kind};{$building_kind_id}"/>
               </dc:for-each>
             </dc:filter>
@@ -2546,7 +2266,7 @@
               <other>
                 <dc:for-each>
                   <buildings description="{$building_name}"
-                             factory="wmsbuildingsfactory"
+                             factory="wmsbuildingsfactory" target_out="{$out}"
                              ids="{$river_id};{$building_name}"/>
                 </dc:for-each>
               </other>
@@ -2566,7 +2286,7 @@
             <jetties>
               <dc:for-each>
                 <jetty description="{$jetty_kind}"
-                       factory="wmsjettiesfactory"
+                       factory="wmsjettiesfactory" target_out="{$out}"
                        ids="{$river_id};{$jetty_kind};{$jetty_kind_id}"/>
               </dc:for-each>
             </jetties>
@@ -2619,6 +2339,7 @@
                     <dc:attribute name="name" value="${olname}"/>
                     <dc:attribute name="ids" value="additionals-wstv-${wstcolpos}-${wstid}"/>
                     <dc:attribute name="factory" value="staticwkms"/>
+                    <dc:attribute name="target_out" value="${out}"/>
                     <dc:attribute name="out" value="${out}"/>
                   </dc:element>
                 </dc:for-each>
@@ -2640,7 +2361,7 @@
                  wc.position    AS wst_column_position,
                  wc.description AS info,
                  w.description  AS wst_description,
-                 wc.name        AS wst_column_name,
+                 COALESCE(wc.name, '')        AS wst_column_name,
                  wr.a           AS deffrom,
                  wr.b           AS defto
           FROM wst_columns wc
@@ -2653,7 +2374,7 @@
       </dc:context>
     </dc:macro>
 
-    <dc:macro name="km-filtered-userdata">
+    <dc:macro name="all-user-artifacts">
         <dc:context connection="user">
         <dc:comment>Select collections and masterartifacts.</dc:comment>
         <dc:statement>
@@ -2665,10 +2386,18 @@
                  COALESCE(ma.ld_mode, '')      AS ld_m,
                  COALESCE(ma.ld_locations, '') AS ld_l,
                  COALESCE(ma.ld_from, '')      AS ld_f,
-                 COALESCE(ma.ld_to, '')        AS ld_t
+                 COALESCE(ma.ld_to, '')        AS ld_t,
+                 o.name                        AS out_name,
+                 o.id                          AS out_id,
+                 f.name                        AS facet_name,
+                 f.num                         AS facet_num,
+                 f.description                 AS facet_description,
+                 f.id                          AS fid
           FROM users u
             JOIN collections c             ON c.user_id = u.id
             JOIN master_artifacts_range ma ON ma.collection_id = c.id
+            JOIN outs o                    ON o.artifact_id = ma.id
+            JOIN facets f                  ON f.out_id = o.id
           WHERE  u.gid = CAST(${user-id} AS UUID)
             AND ma.gid &lt;&gt; CAST(${artifact-id} AS uuid)
             AND EXISTS (
@@ -2678,41 +2407,24 @@
                 AND k = 'river'
                 AND v = ${river})
         </dc:statement>
-          <dc:for-each>
-            <dc:variable name="from" type="number" expr="dc:fromValue($ld_m, $ld_l, $ld_f)"/>
-            <dc:variable name="to" type="number" expr="dc:toValue($ld_m, $ld_l, $ld_t)"/>
-            <dc:if test="not (($tokm &lt; $from) or $fromkm &gt; $tokm)">
-              <dc:macro-body/>
-            </dc:if>
-          </dc:for-each>
+        <dc:virtual-column name="deffrom" type="number" expr="dc:fromValue($ld_m, $ld_l, $ld_f)">
+          <dc:virtual-column name="defto" type="number" expr="dc:toValue($ld_m, $ld_l, $ld_t)">
+            <dc:macro-body/>
+          </dc:virtual-column>
+        </dc:virtual-column>
       </dc:context>
     </dc:macro>
 
-    <dc:macro name="user-range">
-      <dc:choose>
-        <dc:when test="dc:contains($parameters, 'user-id')">
-          <dc:context connection="user">
-            <dc:statement>
-              SELECT COALESCE(ld_mode, '')      AS ldm,
-                     COALESCE(ld_locations, '') AS ldl,
-                     COALESCE(ld_from, '')      AS ldf,
-                     COALESCE(ld_to, '')        AS ldt
-              FROM master_artifacts_range
-              WHERE gid = CAST(${artifact-id} AS uuid)
-            </dc:statement>
-            <dc:for-each>
-              <dc:variable name="fromkm" type="number" expr="dc:fromValue($ldm, $ldl, $ldf)"/>
-              <dc:variable name="tokm" type="number" expr="dc:toValue($ldm, $ldl, $ldt)"/>
-              <dc:macro-body/>
-            </dc:for-each>
-          </dc:context>
-        </dc:when>
-        <dc:otherwise>
-          <dc:variable name="fromkm" type="number" expr="dc:fromValue('', '', '')"/>
-          <dc:variable name="tokm" type="number" expr="dc:toValue('', '', '')"/>
-          <dc:macro-body/>
-        </dc:otherwise>
-      </dc:choose>
+    <dc:macro name="km-filtered-user-artifacts">
+      <dc:filter expr="not($deffrom &gt; $tokm or $defto &lt; $fromkm)">
+        <dc:macro-body/>
+      </dc:filter>
+    </dc:macro>
+
+    <dc:macro name="artifact-range">
+      <dc:variable name="fromkm" type="number" expr="dc:fromValue(dc:get('ld_mode'), dc:get('ld_locations'), dc:get('ld_from'))"/>
+      <dc:variable name="tokm" type="number" expr="dc:toValue(dc:get('ld_mode'), dc:get('ld_locations'), dc:get('ld_to'))"/>
+      <dc:macro-body/>
     </dc:macro>
 
   </datacage>

http://dive4elements.wald.intevation.org