title: Muenchian grouping stylesheet returning duplicate ungrouped nodes
body: <p>I am trying to use Apache Xalan to group a list of product variants by their productId's. Here is an input sample:</p>
  <code>***input_1.xml***</code>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;root&gt;
      &lt;variant&gt;
          &lt;productId&gt;1&lt;/productId&gt;
          &lt;price&gt;100&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;10&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;red&quot; /&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;variant&gt;
          &lt;productId&gt;1&lt;/productId&gt;
          &lt;price&gt;100&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;8&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;blue&quot; /&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;variant&gt;
          &lt;productId&gt;1&lt;/productId&gt;
          &lt;price&gt;150&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;12&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;green&quot; /&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;variant&gt;
          &lt;productId&gt;2&lt;/productId&gt;
          &lt;price&gt;200&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;7&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;purple&quot; /&gt;
              &lt;attribute name=&quot;material&quot; value=&quot;faux-leather&quot; /&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;variant&gt;
          &lt;productId&gt;3&lt;/productId&gt;
          &lt;price&gt;190&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;22&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot; /&gt;
              &lt;attribute name=&quot;size&quot; value=&quot;XL&quot; /&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;variant&gt;
          &lt;productId&gt;3&lt;/productId&gt;
          &lt;price&gt;180&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;13&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot; /&gt;
              &lt;attribute name=&quot;size&quot; value=&quot;L&quot; /&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;variant&gt;
          &lt;productId&gt;3&lt;/productId&gt;
          &lt;price&gt;170&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;5&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot; /&gt;
              &lt;attribute name=&quot;size&quot; value=&quot;M&quot; /&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;variant&gt;
          &lt;productId&gt;3&lt;/productId&gt;
          &lt;price&gt;170&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;7&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot; /&gt;
              &lt;attribute name=&quot;size&quot; value=&quot;S&quot; /&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;variant&gt;
          &lt;productId&gt;3&lt;/productId&gt;
          &lt;price&gt;180&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;12&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot; /&gt;
              &lt;attribute name=&quot;size&quot; value=&quot;XS&quot; /&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
  &lt;/root&gt;
  </code></pre>
  <p>I then use the following command from the shell:</p>
  <pre class="lang-sh prettyprint-override"><code>xalan -in input_1.xml -xsl muenchian_1.xsl -out output_1.xml -indent 4
  </code></pre>
  <p>To transform the input with the following stylesheet:</p>
  <code>***muenchian_1.xml***</code>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;xsl:stylesheet version=&quot;1.0&quot;
      xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;   &gt;
      &lt;xsl:strip-space elements=&quot;*&quot; /&gt;
      &lt;xsl:output method=&quot;xml&quot; indent=&quot;yes&quot;/&gt;
      &lt;xsl:key name=&quot;variants-by-productId&quot; match=&quot;/root/variant&quot; use=&quot;productId&quot;/&gt;
      &lt;xsl:template match=&quot;@*|node()&quot; name=&quot;identity&quot;&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select=&quot;@*|node()&quot;/&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
      &lt;xsl:template match=&quot;/root/variant[productId][generate-id() =
           generate-id(key('variants-by-productId', productId)[1])]&quot; priority=&quot;1&quot;&gt;
          &lt;product-listing-group&gt;
              &lt;productId&gt;
                  &lt;xsl:value-of select=&quot;productId&quot;/&gt;
              &lt;/productId&gt;
              &lt;xsl:for-each select=&quot;key('variants-by-productId', productId)&quot;&gt;
                  &lt;xsl:call-template name=&quot;identity&quot; /&gt;
              &lt;/xsl:for-each&gt;
          &lt;/product-listing-group&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  <p>Expecting to get the following output:</p>
  <code>***expected_1.xml***</code>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;root&gt;
      &lt;product&gt;
          &lt;productId&gt;1&lt;/productId&gt;
          &lt;variant&gt;
              &lt;price&gt;100&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;10&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;red&quot; /&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
          &lt;variant&gt;
              &lt;price&gt;100&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;8&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;blue&quot; /&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
          &lt;variant&gt;
              &lt;price&gt;150&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;12&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;green&quot; /&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
      &lt;/product&gt;
      &lt;product&gt;
          &lt;productId&gt;2&lt;/productId&gt;
          &lt;variant&gt;
              &lt;price&gt;200&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;7&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;purple&quot; /&gt;
                  &lt;attribute name=&quot;material&quot; value=&quot;faux-leather&quot; /&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
      &lt;/product&gt;
      &lt;product&gt;
          &lt;productId&gt;3&lt;/productId&gt;
          &lt;variant&gt;
              &lt;price&gt;190&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;22&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot; /&gt;
                  &lt;attribute name=&quot;size&quot; value=&quot;XL&quot; /&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
          &lt;variant&gt;
              &lt;price&gt;180&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;13&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;L&quot; /&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
          &lt;variant&gt;
              &lt;price&gt;170&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;5&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;M&quot; /&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
          &lt;variant&gt;
              &lt;price&gt;170&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;7&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;S&quot; /&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
          &lt;variant&gt;
              &lt;price&gt;180&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;12&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;XS&quot; /&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
      &lt;/product&gt;
  &lt;/root&gt;
  </code></pre>
  <p>but instead I get:</p>
  <code>***output_1.xml***</code>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;root&gt;
      &lt;product-listing-group&gt;
          &lt;productId&gt;1&lt;/productId&gt;
          &lt;variant&gt;
              &lt;productId&gt;1&lt;/productId&gt;
              &lt;price&gt;100&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;10&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;red&quot;/&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
          &lt;variant&gt;
              &lt;productId&gt;1&lt;/productId&gt;
              &lt;price&gt;100&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;8&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;blue&quot;/&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
          &lt;variant&gt;
              &lt;productId&gt;1&lt;/productId&gt;
              &lt;price&gt;150&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;12&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;green&quot;/&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
      &lt;/product-listing-group&gt;
      &lt;variant&gt;
          &lt;productId&gt;1&lt;/productId&gt;
          &lt;price&gt;100&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;8&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;blue&quot;/&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;variant&gt;
          &lt;productId&gt;1&lt;/productId&gt;
          &lt;price&gt;150&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;12&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;green&quot;/&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;product-listing-group&gt;
          &lt;productId&gt;2&lt;/productId&gt;
          &lt;variant&gt;
              &lt;productId&gt;2&lt;/productId&gt;
              &lt;price&gt;200&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;7&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;purple&quot;/&gt;
                  &lt;attribute name=&quot;material&quot; value=&quot;faux-leather&quot;/&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
      &lt;/product-listing-group&gt;
      &lt;product-listing-group&gt;
          &lt;productId&gt;3&lt;/productId&gt;
          &lt;variant&gt;
              &lt;productId&gt;3&lt;/productId&gt;
              &lt;price&gt;190&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;22&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot;/&gt;
                  &lt;attribute name=&quot;size&quot; value=&quot;XL&quot;/&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
          &lt;variant&gt;
              &lt;productId&gt;3&lt;/productId&gt;
              &lt;price&gt;180&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;13&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot;/&gt;
                  &lt;attribute name=&quot;size&quot; value=&quot;L&quot;/&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
          &lt;variant&gt;
              &lt;productId&gt;3&lt;/productId&gt;
              &lt;price&gt;170&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;5&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot;/&gt;
                  &lt;attribute name=&quot;size&quot; value=&quot;M&quot;/&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
          &lt;variant&gt;
              &lt;productId&gt;3&lt;/productId&gt;
              &lt;price&gt;170&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;7&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot;/&gt;
                  &lt;attribute name=&quot;size&quot; value=&quot;S&quot;/&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
          &lt;variant&gt;
              &lt;productId&gt;3&lt;/productId&gt;
              &lt;price&gt;180&lt;/price&gt;
              &lt;stock unit=&quot;item&quot;&gt;12&lt;/stock&gt;
              &lt;attributes&gt;
                  &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot;/&gt;
                  &lt;attribute name=&quot;size&quot; value=&quot;XS&quot;/&gt;
              &lt;/attributes&gt;
          &lt;/variant&gt;
      &lt;/product-listing-group&gt;
      &lt;variant&gt;
          &lt;productId&gt;3&lt;/productId&gt;
          &lt;price&gt;180&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;13&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot;/&gt;
              &lt;attribute name=&quot;size&quot; value=&quot;L&quot;/&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;variant&gt;
          &lt;productId&gt;3&lt;/productId&gt;
          &lt;price&gt;170&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;5&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot;/&gt;
              &lt;attribute name=&quot;size&quot; value=&quot;M&quot;/&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;variant&gt;
          &lt;productId&gt;3&lt;/productId&gt;
          &lt;price&gt;170&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;7&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot;/&gt;
              &lt;attribute name=&quot;size&quot; value=&quot;S&quot;/&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
      &lt;variant&gt;
          &lt;productId&gt;3&lt;/productId&gt;
          &lt;price&gt;180&lt;/price&gt;
          &lt;stock unit=&quot;item&quot;&gt;12&lt;/stock&gt;
          &lt;attributes&gt;
              &lt;attribute name=&quot;color&quot; value=&quot;yellow&quot;/&gt;
              &lt;attribute name=&quot;size&quot; value=&quot;XS&quot;/&gt;
          &lt;/attributes&gt;
      &lt;/variant&gt;
  &lt;/root&gt;
  </code></pre>
  <p>As you can see while the variants are grouped correctly, all the variants except for the first in their respective groups are repeated twice, once withing the grouping and once just outside of it.</p>
  <p>Why is this? And how can I fix it?</p>