Test with multiple accumulators matching text nodes
authorMichael Kay <mike@saxonica.com>
Sun, 16 Jul 2017 00:08:21 +0100
changeset 1723 9f7bb7d3030b
parent 1722 9068cdd364c3
child 1724 1e1a7646a90e
Test with multiple accumulators matching text nodes
tests/decl/accumulator/_accumulator-test-set.xml
tests/decl/accumulator/accumulator-069.xml
tests/decl/accumulator/accumulator-069.xsl
--- a/tests/decl/accumulator/_accumulator-test-set.xml	Sun Jul 16 00:07:14 2017 +0100
+++ b/tests/decl/accumulator/_accumulator-test-set.xml	Sun Jul 16 00:08:21 2017 +0100
@@ -1431,6 +1431,48 @@
          </any-of>           
       </result>
    </test-case>
+   
+   <test-case name="accumulator-069">
+      <description>Grouping using accumulators and maps (non-streamable version)</description>
+      <created by="Michael Kay after Martin Honnen" on="2017-07-14"/>
+      <environment>
+         <source file="accumulator-069.xml" role="."/>
+      </environment>
+      <dependencies>
+         <feature value="streaming"/>
+      </dependencies>
+      <test>
+         <stylesheet file="accumulator-069.xsl"/>
+         <param name="STREAMABLE" static="yes" select="false()"/>
+      </test>
+      <result>
+         <assert-xml><![CDATA[<TRANSACTION-LIST><transaction key="a"><count>2</count><amount>20</amount></transaction
+            ><transaction key="b"><count>2</count><amount>20</amount></transaction
+            ><transaction key="c"><count>2</count><amount>20</amount></transaction
+            ></TRANSACTION-LIST>]]></assert-xml>         
+      </result>
+   </test-case>
+   
+   <test-case name="accumulator-069s">
+      <description>Grouping using accumulators and maps (streamable version)</description>
+      <created by="Michael Kay after Martin Honnen" on="2017-07-14"/>
+      <environment>
+         <source file="accumulator-069.xml" role="." streaming="true"/>
+      </environment>
+      <dependencies>
+         <feature value="streaming"/>
+      </dependencies>
+      <test>
+         <stylesheet file="accumulator-069.xsl"/>
+         <param name="STREAMABLE" static="yes" select="true()"/>
+      </test>
+      <result>
+         <assert-xml><![CDATA[<TRANSACTION-LIST><transaction key="a"><count>2</count><amount>20</amount></transaction
+            ><transaction key="b"><count>2</count><amount>20</amount></transaction
+            ><transaction key="c"><count>2</count><amount>20</amount></transaction
+            ></TRANSACTION-LIST>]]></assert-xml>        
+      </result>
+   </test-case>
  
  
 </test-set>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/decl/accumulator/accumulator-069.xml	Sun Jul 16 00:08:21 2017 +0100
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<TRANSACTION-LIST>
+    <TRANSACTION>
+        <ITEM1>1</ITEM1>
+        <ITEM2>
+            <SUBITEM2.1>10</SUBITEM2.1>
+            <SUBITEM2.2>
+                <GROUPING-KEY>a</GROUPING-KEY>
+            </SUBITEM2.2>
+        </ITEM2>
+    </TRANSACTION>
+    <TRANSACTION>
+        <ITEM1>1</ITEM1>
+        <ITEM2>
+            <SUBITEM2.1>10</SUBITEM2.1>
+            <SUBITEM2.2>
+                <GROUPING-KEY>b</GROUPING-KEY>
+            </SUBITEM2.2>
+        </ITEM2>
+    </TRANSACTION>
+    <TRANSACTION>
+        <ITEM1>1</ITEM1>
+        <ITEM2>
+            <SUBITEM2.1>10</SUBITEM2.1>
+            <SUBITEM2.2>
+                <GROUPING-KEY>c</GROUPING-KEY>
+            </SUBITEM2.2>
+        </ITEM2>
+    </TRANSACTION>
+    <TRANSACTION>
+        <ITEM1>1</ITEM1>
+        <ITEM2>
+            <SUBITEM2.1>10</SUBITEM2.1>
+            <SUBITEM2.2>
+                <GROUPING-KEY>a</GROUPING-KEY>
+            </SUBITEM2.2>
+        </ITEM2>
+    </TRANSACTION>
+    <TRANSACTION>
+        <ITEM1>1</ITEM1>
+        <ITEM2>
+            <SUBITEM2.1>10</SUBITEM2.1>
+            <SUBITEM2.2>
+                <GROUPING-KEY>b</GROUPING-KEY>
+            </SUBITEM2.2>
+        </ITEM2>
+    </TRANSACTION>
+    <TRANSACTION>
+        <ITEM1>1</ITEM1>
+        <ITEM2>
+            <SUBITEM2.1>10</SUBITEM2.1>
+            <SUBITEM2.2>
+                <GROUPING-KEY>c</GROUPING-KEY>
+            </SUBITEM2.2>
+        </ITEM2>
+    </TRANSACTION>
+</TRANSACTION-LIST>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/decl/accumulator/accumulator-069.xsl	Sun Jul 16 00:08:21 2017 +0100
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
+    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
+    xmlns:math="http://www.w3.org/2005/xpath-functions/math" 
+    xmlns:map="http://www.w3.org/2005/xpath-functions/map" 
+    exclude-result-prefixes="xs math map" 
+    expand-text="true" 
+    version="3.0">
+    
+    <xsl:param name="STREAMABLE" as="xs:boolean" static="yes" select="true()"/>
+    
+    <xsl:mode _streamable="{$STREAMABLE}" on-no-match="shallow-skip" use-accumulators="item1-count subitem groups"/>
+    
+    <xsl:output indent="yes"/>
+    
+    <xsl:accumulator name="item1-count" as="xs:integer" initial-value="0" _streamable="{$STREAMABLE}">
+        <xsl:accumulator-rule match="TRANSACTION" select="0"/>
+        <xsl:accumulator-rule match="TRANSACTION/ITEM1" select="$value + 1"/>
+    </xsl:accumulator>
+    
+    <xsl:accumulator name="subitem" as="xs:integer" initial-value="0" _streamable="{$STREAMABLE}">
+        <xsl:accumulator-rule match="TRANSACTION/ITEM2/SUBITEM2.1/text()" select="xs:integer(.)"/>
+    </xsl:accumulator>
+    
+    <xsl:accumulator name="groups" as="map(xs:string, xs:integer*)" initial-value="map{}" _streamable="{$STREAMABLE}">
+        <xsl:accumulator-rule match="TRANSACTION/ITEM2/SUBITEM2.2/GROUPING-KEY/text()" 
+            select="let $key := string(),
+            $count := accumulator-before('item1-count'),
+            $sum := accumulator-before('subitem')
+            return if (not(map:contains($value, $key)))
+            then map:put($value, $key, ($count, $sum))
+            else let $value-seq := $value($key)
+            return map:put($value, $key, ($count + $value-seq[1], $sum + $value-seq[2]))"/>
+    </xsl:accumulator>
+    
+    <xsl:template match="TRANSACTION-LIST">
+        <xsl:copy>
+            <xsl:apply-templates/>
+            <xsl:variable name="groups" select="accumulator-after('groups')"/>
+            <xsl:for-each select="map:keys($groups)">
+                <transaction key="{.}">
+                    <count>{$groups(.)[1]}</count>
+                    <amount>{$groups(.)[2]}</amount>
+                </transaction>
+            </xsl:for-each>
+        </xsl:copy>
+    </xsl:template>
+    
+</xsl:stylesheet>
\ No newline at end of file