(From a Saxon bug report by David Rudel)
<xsl:param name="p" select="0"/>
<xsl:variable name="v" select="$p + number(.)"/>
We say explicitly that xsl:on-completion can make reference to the local parameters of the xsl:iterate instruction, but we say nothing about references to local variables.
Under the scoping rules for local variables, $v is in scope, but what is its value? The obvious answer would be, the value to which it is bound during the final iteration, that is the value computed while processing the last member of the input sequence. But what if the input sequence is empty? It seems that the variable would then be in scope, but having no value, which is a situation without precedent.
The only workable answer to this I can see is to say that $v is out of scope within xsl:on-completion. This is consistent with the rule that there is no context item within xsl:on-completion.
I have added test case iterate-032 to test this situation.
Question: would this be the only violation of the general rule that a variable is in scope within any element that is a following-sibling to the variable's declaration or a descendant of such a following-sibling?
It seems that, from a consistency viewpoint, it would be desirable to restructure the xsl:iterate instruction itself so that its syntactic contents are consistent with both the scoping rules and the algorithmic notion that <xsl:on-completion> pertains to the <xsl:iterate> but is not evaluated as part of the loop. This could be done by introducing a <xsl:iterate-loop> element, so that the structure of the xsl:iterate instruction is:
Any xsl:next-iteration or xsl:break elements would occur inside the <xsl:iterate-loop> element as tail-position elements. The variables inside <xsl:iterate-loop> would, in this construction, be naturally out-of-scope once <xsl:on-completion> is hit, but the <xsl:param> elements would still be in scope.
It would not be the first exception to this rule; exceptions already exist for xsl:fallback and xsl:catch.
The working group decided (subject to review if there is pushback) to reposition the xsl:on-completion element to come after the xsl:param elements and before the sequence constructor, thus making the variables declared in the sequence constructor out of scope for the on-completion element.
The WG discussed this during the ftf meeting in Prague and reconfirmed the tentative decision taken during the call of 30 January 2014.