<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>29697</bug_id>
          
          <creation_ts>2016-06-18 09:25:14 +0000</creation_ts>
          <short_desc>[XSLT30]With nested xsl:merge, should the current-merge-group() of outer merge be available for a merge group selection of inner merge?</short_desc>
          <delta_ts>2016-10-06 18:42:18 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>XPath / XQuery / XSLT</product>
          <component>XSLT 3.0</component>
          <version>Candidate Recommendation</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Windows NT</op_sys>
          <bug_status>CLOSED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Martin Honnen">martin.honnen</reporter>
          <assigned_to name="Michael Kay">mike</assigned_to>
          
          
          <qa_contact name="Mailing list for public feedback on specs from XSL and XML Query WGs">public-qt-comments</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>126792</commentid>
    <comment_count>0</comment_count>
      <attachid>1645</attachid>
    <who name="Martin Honnen">martin.honnen</who>
    <bug_when>2016-06-18 09:25:14 +0000</bug_when>
    <thetext>Created attachment 1645
sample input data for test case

The following code has two nested xsl:merge, where an xsl:merge-source/@select of the inner xsl:merge tries to select the current-merge-group() of the outer xsl:merge:

	&lt;xsl:template match=&quot;root&quot;&gt;
		&lt;xsl:copy&gt;
			&lt;xsl:merge&gt;
				&lt;xsl:merge-source select=&quot;item&quot;&gt;
					&lt;xsl:merge-key select=&quot;foo&quot;/&gt;
				&lt;/xsl:merge-source&gt;
				&lt;xsl:merge-action&gt;
					&lt;group key=&quot;{current-merge-key()}&quot;&gt;
						&lt;xsl:merge&gt;
							&lt;xsl:merge-source select=&quot;current-merge-group()&quot;&gt;
								&lt;xsl:merge-key select=&quot;bar&quot;&gt;&lt;/xsl:merge-key&gt;
							&lt;/xsl:merge-source&gt;
							&lt;xsl:merge-action&gt;
								&lt;nested-group key=&quot;{current-merge-key()}&quot;&gt;
									&lt;xsl:copy-of select=&quot;current-merge-group()/value&quot;/&gt;
								&lt;/nested-group&gt;
							&lt;/xsl:merge-action&gt;
						&lt;/xsl:merge&gt;
					&lt;/group&gt;
				&lt;/xsl:merge-action&gt;
			&lt;/xsl:merge&gt;
		&lt;/xsl:copy&gt;
	&lt;/xsl:template&gt;

This works fine with Exselt, however Saxon (tested with 9.7.0.5 EE) complains

Error in xsl:merge-source/@select on line 18 column 57 of test2016061801.xsl:
  XTDE3480: There is no current merge group

So with Saxon I have to use a variable, as in 

			&lt;xsl:merge&gt;
				&lt;xsl:merge-source select=&quot;item&quot;&gt;
					&lt;xsl:merge-key select=&quot;foo&quot;/&gt;
				&lt;/xsl:merge-source&gt;
				&lt;xsl:merge-action&gt;
					&lt;group key=&quot;{current-merge-key()}&quot;&gt;
						&lt;xsl:variable name=&quot;group&quot; select=&quot;current-merge-group()&quot;/&gt;
						&lt;xsl:merge&gt;
							&lt;xsl:merge-source select=&quot;$group&quot;&gt;
								&lt;xsl:merge-key select=&quot;bar&quot;&gt;&lt;/xsl:merge-key&gt;
							&lt;/xsl:merge-source&gt;
							&lt;xsl:merge-action&gt;
								&lt;nested-group key=&quot;{current-merge-key()}&quot;&gt;
									&lt;xsl:copy-of select=&quot;current-merge-group()/value&quot;/&gt;
								&lt;/nested-group&gt;
							&lt;/xsl:merge-action&gt;
						&lt;/xsl:merge&gt;
					&lt;/group&gt;
				&lt;/xsl:merge-action&gt;
			&lt;/xsl:merge&gt;


So I would like a clarification whether with nested xsl:merge the current-merge-group() of an outer xsl:merge should be accessible with an xsl:merge-source of a inner xsl:merge.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>126793</commentid>
    <comment_count>1</comment_count>
      <attachid>1646</attachid>
    <who name="Martin Honnen">martin.honnen</who>
    <bug_when>2016-06-18 09:26:22 +0000</bug_when>
    <thetext>Created attachment 1646
reduced test case</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>126796</commentid>
    <comment_count>2</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2016-06-19 07:57:33 +0000</bug_when>
    <thetext>Thanks for raising this.

I think that

(a) the spec doesn&apos;t currently provide a clear answer to the question

(b) it&apos;s desirable that this test case should work, i.e that the parent merge key/merge group should be available except within the merge-action of the child

(c) in looking for analogies in xsl:for-each-group, I&apos;m not sure the text is very clear there either.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>126821</commentid>
    <comment_count>3</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2016-06-23 17:24:15 +0000</bug_when>
    <thetext>Abel referred us to this text which provides an indication of the intent:

If neither of for-each-item and for-each-stream is present, the xsl:merge-source element defines a single merge input sequence. This sequence is the result of evaluating the expression in the select attribute. *This is evaluated using the dynamic context of the containing xsl:merge instruction.* This sequence will be merged with the sequences defined by other xsl:merge-source elements, if present.

The editor was actioned to come up with a proposal for clarification of the rules.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>126838</commentid>
    <comment_count>4</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2016-06-26 19:46:52 +0000</bug_when>
    <thetext>There seem to be a number of cases we have to consider.

(A) xsl:merge-source/@select when there is no @for-each-item or @for-each-stream. In this case we state &quot;[The select expression] is evaluated using the dynamic context of the containing xsl:merge instruction. This is the case that applies to this example, and it makes it pretty clear that the example should work.

(B) xsl:merge-source/@for-each-item. Here we say:

(B1) The for-each-item expression is evaluated with the dynamic context of the containing xsl:merge instruction

(B2) the select attribute is evaluated with the focus for the evaluation as follows...

We don&apos;t explicitly say anything about other aspects of the dynamic context for the evaluation of the select attribute.

(C) xsl:merge-source/@for-each-stream. 

(C1) We don&apos;t say anything about the dynamic context for the evaluation of for-each-stream.

(C2) We say &quot;These anchor items are then used in the same way as a sequence of anchor items selected directly using the for-each-item attribute.&quot; which hints that the dynamic context for the select expression follows the same rules as in the for-each-item case.

(D) xsl:merge-key/@select (or the contained sequence constructor): we say &quot;This is evaluated with a singleton focus based on J, or, if streamable=yes is specified on the xsl:merge-source, a singleton focus based on a snapshot of J (see 15.4 Streamable Merging).&quot; We say nothing about other aspects of the dynamic context.

(E) Attribute value templates on xsl:merge-key: we say &quot;their effective values are evaluated using the focus of the containing xsl:merge instruction.&quot; We say nothing about other aspects of the dynamic context.

In 15.6 we have a non-normative note: &quot;The current merge group and key are set during the evaluation of the sequence constructor contained in the xsl:merge-action element. They are initially absent, and they are cleared by all invocation constructs, which means in effect that the functions current-merge-group and current-merge-key can only usefully be invoked from instructions appearing lexically within the xsl:merge-action element.&quot; But this doesn&apos;t help us much because all these cases are lexically within the xsl:merge-action of the outer xsl:merge.

The statement that the current merge group and key are cleared by all invocation constructs appears normatively in 15.6.1 and 15.6.2.

Section 5.3, the general description of the static and dynamic context, sheds no additional light: for these components it delegates entirely to 15.6.

I think we should add the general rules (in 15.6): (part of this is transferred from 15.6.1 and 15.6.2).

&lt;quote&gt;
The current merge group and current merge key are available within the sequence constructor contained by an xsl:merge-action element. The values are initially absent during the evaluation of global variables and stylesheet parameters, during the evaluation of the use attribute or contained sequence constructor of xsl:key, and during the evaluation of the initial-value attribute of xsl:accumulator and the select attribute of contained sequence constructor of xsl:accumulator-rule. All invocation constructs set the current merge group and current merge key to absent. 

Note: Taken together, these rules mean that any invocation of current-merge-group or current-merge-key that is not lexically scoped by an xsl:merge-action element will raise a dynamic error.

When an inner xsl:merge instruction is lexically nested within the xsl:merge-action element of an outer xsl:merge instruction, any use of current-merge-group or current-merge-key that appears within the xsl:merge-action of the inner xsl:merge instruction is a reference to the current merge group or key of the inner xsl:merge instruction, while any such reference that appears within the outer xsl:merge-action element, but not within the inner xsl:merge-action, is a reference to the current merge group or key of the outer xsl:merge instruction. This means, for example, that a reference to the current merge group of the outer xsl:merge can appear in the select attribute of an xsl:merge-source child of the inner xsl:merge.
&lt;/quote&gt;

Additional changes: 

In 15.3, after &quot;When the for-each-stream attribute is present, its value must be an expression that returns a sequence of URIs.&quot; add &quot;The expression is evaluated with the dynamic context of the containing xsl:merge instruction.&quot; After &quot;These anchor items are then used in the same way as a sequence of anchor items selected directly using the for-each-item attribute.&quot; add: &quot;in particular, the focus for the select expression is established in the same way.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>126840</commentid>
    <comment_count>5</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2016-06-26 21:31:37 +0000</bug_when>
    <thetext>Test case added - merge-089

I have updated the spec as proposed in comment #4.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>126923</commentid>
    <comment_count>6</comment_count>
    <who name="Michael Kay">mike</who>
    <bug_when>2016-07-07 17:20:08 +0000</bug_when>
    <thetext>The change was accepted.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>1645</attachid>
            <date>2016-06-18 09:25:14 +0000</date>
            <delta_ts>2016-06-18 09:25:14 +0000</delta_ts>
            <desc>sample input data for test case</desc>
            <filename>test2016061801.xml</filename>
            <type>text/xml</type>
            <size>453</size>
            <attacher name="Martin Honnen">martin.honnen</attacher>
            
              <data encoding="base64">PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHJvb3Q+Cgk8aXRlbT4KCQk8
Zm9vPmE8L2Zvbz4KCQk8YmFyPjE8L2Jhcj4KCQk8dmFsdWU+MTwvdmFsdWU+Cgk8L2l0ZW0+Cgk8
aXRlbT4KCQk8Zm9vPmE8L2Zvbz4KCQk8YmFyPjE8L2Jhcj4KCQk8dmFsdWU+MjwvdmFsdWU+CQkK
CTwvaXRlbT4KCTxpdGVtPgoJCTxmb28+YTwvZm9vPgoJCTxiYXI+MjwvYmFyPgoJCTx2YWx1ZT4z
PC92YWx1ZT4KCTwvaXRlbT4KCTxpdGVtPgoJCTxmb28+YjwvZm9vPgoJCTxiYXI+MTwvYmFyPgoJ
CTx2YWx1ZT40PC92YWx1ZT4KCTwvaXRlbT4KCTxpdGVtPgoJCTxmb28+YjwvZm9vPgoJCTxiYXI+
MTwvYmFyPgoJCTx2YWx1ZT41PC92YWx1ZT4KCTwvaXRlbT4KCTxpdGVtPgoJCTxmb28+YjwvZm9v
PgoJCTxiYXI+MjwvYmFyPgoJCTx2YWx1ZT42PC92YWx1ZT4KCTwvaXRlbT4KPC9yb290PgoK
</data>

          </attachment>
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>1646</attachid>
            <date>2016-06-18 09:26:22 +0000</date>
            <delta_ts>2016-06-18 09:26:22 +0000</delta_ts>
            <desc>reduced test case</desc>
            <filename>test2016061801.xsl</filename>
            <type>application/xml</type>
            <size>971</size>
            <attacher name="Martin Honnen">martin.honnen</attacher>
            
              <data encoding="base64">PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHhzbDpzdHlsZXNoZWV0IHht
bG5zOnhzbD0iaHR0cDovL3d3dy53My5vcmcvMTk5OS9YU0wvVHJhbnNmb3JtIgoJeG1sbnM6eHM9
Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2NoZW1hIgoJeG1sbnM6bWF0aD0iaHR0cDovL3d3
dy53My5vcmcvMjAwNS94cGF0aC1mdW5jdGlvbnMvbWF0aCIgZXhjbHVkZS1yZXN1bHQtcHJlZml4
ZXM9InhzIG1hdGgiCgl2ZXJzaW9uPSIzLjAiPgoKCTx4c2w6b3V0cHV0IGluZGVudD0ieWVzIi8+
CgoJPHhzbDp0ZW1wbG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