A test involving grouping and accumulators
authorMichael Kay <mike@saxonica.com>
Sat, 02 Feb 2019 20:28:59 +0000
changeset 1959 28985bc9d1b6
parent 1958 01b2d296e64f
child 1960 b0b1af250597
A test involving grouping and accumulators
tests/insn/for-each-group/_for-each-group-test-set.xml
tests/insn/for-each-group/for-each-group-089.xsl
--- a/tests/insn/for-each-group/_for-each-group-test-set.xml	Sat Feb 02 20:28:17 2019 +0000
+++ b/tests/insn/for-each-group/_for-each-group-test-set.xml	Sat Feb 02 20:28:59 2019 +0000
@@ -1514,4 +1514,29 @@
          </all-of>
       </result>
    </test-case>
+   
+   <test-case name="for-each-group-089">
+      <description>group elements by local-name, while retaining accumulator values and dropping namespace declarations</description>
+      <created by="Michael Kay" on="2019-01-25"/>
+      <dependencies>
+         <spec value="XSLT30+"/>
+      </dependencies>
+      <test>
+         <stylesheet file="for-each-group-089.xsl"/>
+         <initial-template name="main"/>
+      </test>
+      <result>
+         <assert-xml><![CDATA[<out><line att="1" accumulator="1"/><line att="2" accumulator="3"
+            /><line att="4" accumulator="10"/><line att="7" accumulator="28"
+            /><line att="8" accumulator="36"/><line att="11" accumulator="66"
+            /><line2 att="3" accumulator="6"/><line3 att="5" accumulator="15"
+            /><line3 att="6" accumulator="21"/><line3 att="9" accumulator="45"
+            /><line3 att="10" accumulator="55"
+            /><a:line xmlns:a="http://example.com" att="12" accumulator="78"
+            /><a:line xmlns:a="http://example.com" att="13" accumulator="91"
+            /><b:line xmlns:b="http://example.com" att="16" accumulator="136"
+            /><a:line3 xmlns:a="http://example.com" att="14" accumulator="105"
+            /><b:line3 xmlns:b="http://example.com" att="15" accumulator="120"/></out>]]></assert-xml>
+      </result>
+   </test-case>
 </test-set>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/insn/for-each-group/for-each-group-089.xsl	Sat Feb 02 20:28:59 2019 +0000
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet exclude-result-prefixes="xs" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+  
+  <!-- group elements by local-name; while applying an accumulator -->
+  
+  <xsl:output encoding="UTF-8" method="xml"/>
+  
+  <xsl:accumulator name="total" as="xs:integer" initial-value="0">
+    <xsl:accumulator-rule match="root/*" select="$value + xs:integer(@att)"/>
+  </xsl:accumulator>
+  
+  <xsl:variable name="in">
+    <root xmlns:a="http://example.com" xmlns:b="http://example.com">
+      <line att="1"/>
+      <line att="2"/>
+      <line2 att="3"/>
+      <line att="4"/>
+      <line3 att="5"/>
+      <line3 att="6"/>
+      <line att="7"/>
+      <line att="8"/>
+      <line3 att="9"/>
+      <line3 att="10"/>
+      <line att="11"/>
+      <a:line att="12"/>
+      <a:line att="13"/>
+      <a:line3 att="14"/>
+      <b:line3 att="15"/>
+      <b:line att="16"/>
+    </root>
+  </xsl:variable>
+  
+  <xsl:template name="main">
+    <xsl:apply-templates select="$in"/>
+  </xsl:template>
+  
+  <xsl:template match="root">
+    <xsl:variable name="result" as="element()+">
+      <xsl:for-each-group select="*" group-by="node-name(.)">
+         <xsl:copy-of select="current-group()" copy-namespaces="no" copy-accumulators="yes"/>       
+      </xsl:for-each-group>
+    </xsl:variable>
+    <out>
+      <xsl:for-each select="$result">
+            <xsl:copy>
+              <xsl:copy-of select="@att"/>
+              <xsl:attribute name="accumulator" select="accumulator-after('total')"/>
+            </xsl:copy>
+      </xsl:for-each>
+    </out>
+  </xsl:template>
+</xsl:stylesheet>
\ No newline at end of file