Accumulator tests
authorabraaksm
Thu, 05 Mar 2015 17:53:52 +0100
changeset 831 e58a0925a050
parent 830 63089e8e0e76
child 832 8b10d826dfc9
Accumulator tests
tests/decl/accumulator/_accumulator-test-set.xml
tests/decl/accumulator/accumulator-044.xsl
--- a/tests/decl/accumulator/_accumulator-test-set.xml	Thu Mar 05 17:52:37 2015 +0100
+++ b/tests/decl/accumulator/_accumulator-test-set.xml	Thu Mar 05 17:53:52 2015 +0100
@@ -717,13 +717,17 @@
    <test-case name="accumulator-041">
       <description>Keep a Word Count</description>
       <keywords>xsl:accumulator</keywords>
-      <environment ref="accumulator-040s"/>
+      <environment>
+        <source role=".">
+           <content><![CDATA[<html lang="en"><head><title>Test 1</title></head><body><h1>Test 1</h1><p>This is test 1.</p></body></html>]]></content>
+        </source>
+      </environment>
       <created by="" on="2015-01-23"/>
       <test>
          <stylesheet file="accumulator-041.xsl"/>
       </test>
       <result>
-     	 <assert>/p/text() = 'Word count: 27'</assert><!-- count(//text()/tokenize(string(.), '\W+')) -->
+     	 <assert>/p/text() = 'Word count: 9'</assert><!-- count(//text()/tokenize(string(.), '\W+')) -->
       </result>
    </test-case>
 
@@ -753,4 +757,21 @@
       </result>
    </test-case>
 
+   <test-case name="accumulator-044s">
+      <description>Remember the Title of a Document</description>
+      <keywords>xsl:accumulator</keywords>
+      <environment ref="accumulator-044s"/>
+      <created by="" on="2015-01-23"/>
+      <dependencies>
+         <feature value="streaming"/>
+      </dependencies>
+      <test>
+         <stylesheet file="accumulator-044.xsl"/>
+      </test>
+      <result>
+     	 <assert>//*:div[@class='heading-1']/text() = 'Moved to'</assert>
+      </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-044.xsl	Thu Mar 05 17:53:52 2015 +0100
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet version="3.0" 
+    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+    xmlns:xs="http://www.w3.org/2001/XMLSchema"
+    xmlns:x="http://www.w3.org/1999/xhtml"
+    exclude-result-prefixes="xs x"
+>
+    <xsl:mode on-no-match="shallow-copy"/>
+	
+    <!-- Remember the Title of a Document
+	    Consider an XHTML document in which the title of the document is represented by the content of the first title element appearing as a child of the head element,
+	    which in turn appears as a child of the html element. Suppose that we want to process the document in streaming mode,
+	    and that we want to avoid outputting the content of the h1 element if it is the same as the document title.
+
+		This can be achieved by remembering the value of the title in an accumulator variable. -->
+	<xsl:accumulator name="firstTitle" as="xs:string?" initial-value="()" streamable="yes" >
+    	<xsl:accumulator-rule match="/x:html/x:head/x:title[1]/text()" select="string(.)"/>
+	</xsl:accumulator>
+
+	<!-- Subsequently, while processing an h1 element appearing later in the document, the value can be referenced: -->
+	<xsl:template match="x:h1">
+  		<xsl:variable name="firstTitle" select="accumulator-before('firstTitle')"/>
+  		<xsl:variable name="thisTitle" select="string(.)"/>
+    	<xsl:if test="$thisTitle ne $firstTitle">
+	    	<x:div class="heading-1"><xsl:value-of select="$thisTitle"/></x:div>
+	    </xsl:if>
+    </xsl:template>
+	
+</xsl:stylesheet>