Artificial scenario where we don't know statically whether or not a context item exists
authorMichael Kay <mike@saxonica.com>
Fri, 14 Dec 2018 23:32:19 +0000
changeset 1925 71129363292b
parent 1924 5bb7357846d0
child 1926 59acff18e7c5
Artificial scenario where we don't know statically whether or not a context item exists
tests/insn/call-template/_call-template-test-set.xml
tests/insn/call-template/call-template-0110.xsl
--- a/tests/insn/call-template/_call-template-test-set.xml	Fri Dec 14 23:31:04 2018 +0000
+++ b/tests/insn/call-template/_call-template-test-set.xml	Fri Dec 14 23:32:19 2018 +0000
@@ -338,6 +338,25 @@
       </result>
    </test-case>
    
+   <test-case name="call-template-0110">
+      <description>Use constructs needing context item in a named template where there may or may not be a context item.</description>
+      <created by="Michael Kay" on="2018-11-10"/>
+      <dependencies>
+         <spec value="XSLT30+"/>
+      </dependencies>
+      <test>
+         <stylesheet file="call-template-0110.xsl"/>
+      </test>
+      <result>
+         <all-of>
+            <assert>/out/getAtt[1]/@status = "caught"</assert>
+            <assert>/out/getAtt[2]/@status = "ok"</assert>
+            <assert>/out/getChild[1]/@status = "caught"</assert>
+            <assert>/out/getChild[2]/@status = "ok"</assert>
+         </all-of>
+      </result>
+   </test-case>
+   
    <test-case name="call-template-0201">
       <description>Launch transformation with initial named template and also with initial node specified, and the template references the node (as .)</description>
       <created by="Michael Kay" on="2012-10-30"/>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/insn/call-template/call-template-0110.xsl	Fri Dec 14 23:32:19 2018 +0000
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
+	<!-- Purpose: Within a named template, we don't know statically whether there will be a context item.
+	     This test tests various situations where a dynamic test for existence of a context item is needed. -->
+
+	<xsl:template name="xsl:initial-template">
+		<out>
+			<!-- run with no context item -->
+			<xsl:call-template name="all">
+				<xsl:with-param name="context-exists" select="false()" tunnel="yes"/>
+			</xsl:call-template>
+			<xsl:variable name="doc" as="element()">
+				<input status="ok"><z>ok</z></input>
+			</xsl:variable>
+			<xsl:for-each select="$doc">
+				<!-- run with context item -->
+				<xsl:call-template name="all">
+					<xsl:with-param name="context-exists" select="true()" tunnel="yes"/>
+				</xsl:call-template>
+			</xsl:for-each>
+		</out>
+	</xsl:template>
+	
+	<xsl:template name="all">
+		<xsl:call-template name="getAttribute"/>
+		<xsl:call-template name="getChild"/>
+	</xsl:template>
+
+	<xsl:template name="getAttribute">
+		<xsl:variable name="caught">
+			<xsl:try select="data(@status)">
+				<xsl:catch errors="*:XPDY0002" select="'caught'"/>
+			</xsl:try>
+		</xsl:variable>
+		<getAtt status="{$caught}"/>
+	</xsl:template>
+	
+	<xsl:template name="getChild">
+		<xsl:variable name="caught">
+			<xsl:try select="string(child::z)">
+				<xsl:catch errors="*:XPDY0002" select="'caught'"/>
+			</xsl:try>
+		</xsl:variable>
+		<getChild status="{$caught}"/>
+	</xsl:template>
+</xsl:transform>