title: Select distinct value in XSLT for-each-group
body: <p>I'm having an issue selecting distinct values for each group. The result just doesn't display on screen. The desired result is shown below: </p>
  
  <pre><code>&lt;xsl:for-each-group select="Items/Item" group-by="ProviderName"&gt;
      &lt;xsl:sort select="current-grouping-key()"/&gt;
      &lt;tr&gt;
          &lt;th&gt;
              &lt;xsl:value-of select="current-grouping-key()"/&gt;
          &lt;/th&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;xsl:text&gt;Item Number&lt;/xsl:text&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;xsl:text&gt;Quantity&lt;/xsl:text&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;xsl:text&gt;Unit Price&lt;/xsl:text&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;xsl:text&gt;Total&lt;/xsl:text&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;xsl:apply-templates select="current-group()"/&gt;
  &lt;/xsl:for-each-group&gt;
  </code></pre>
  
  <p>What I intend to do is to select <em>distinct</em> values for <code>current-group()</code> in the <code>apply-templates</code> tag which is like this</p>
  
  <pre><code>    &lt;xsl:template match="Item"&gt;
          &lt;xsl:value-of select="distinct-values(./ItemName)"/&gt;
      &lt;/xsl:template&gt;
  </code></pre>
  
  <p>I've read several samples on the net and the most optimum way to do so is to employ the <code>generate-id()</code> function with the <code>for-each-group</code> loop. But I have tried and get no positive result. This is the source XML:</p>
  
  <pre><code>&lt;Items&gt;
      &lt;Item ItemNumber="1148087"&gt;
          &lt;ProductName&gt;Dolby Metal-Black-Matte&lt;/ProductName&gt;
          &lt;ProviderName&gt;Vestal Watches&lt;/ProviderName&gt;
          &lt;Quantity&gt;4&lt;/Quantity&gt;
          &lt;UnitPrice&gt;67.99&lt;/UnitPrice&gt;
      &lt;/Item&gt;
      &lt;Item ItemNumber="1150197"&gt;
          &lt;ProductName&gt;Vercilli Blk-Tan&lt;/ProductName&gt;
          &lt;ProviderName&gt;Boston Babes&lt;/ProviderName&gt;
          &lt;Quantity&gt;1&lt;/Quantity&gt;
          &lt;UnitPrice&gt;23.99&lt;/UnitPrice&gt;
      &lt;/Item&gt;
      &lt;Item ItemNumber="1151464"&gt;
          &lt;ProductName&gt;Spritz Grape Seat and Extra Seat&lt;/ProductName&gt;
          &lt;ProviderName&gt;Bambeano&lt;/ProviderName&gt;
          &lt;Quantity&gt;1&lt;/Quantity&gt;
          &lt;UnitPrice&gt;56.99&lt;/UnitPrice&gt;
      &lt;/Item&gt;
      &lt;Item ItemNumber="1148087"&gt;
          &lt;ProductName&gt;Dolby Metal-Black-Matte&lt;/ProductName&gt;
          &lt;ProviderName&gt;Vestal Watches&lt;/ProviderName&gt;
          &lt;Quantity&gt;2&lt;/Quantity&gt;
          &lt;UnitPrice&gt;67.99&lt;/UnitPrice&gt;
      &lt;/Item&gt;
      &lt;Item ItemNumber="1150197"&gt;
          &lt;ProductName&gt;Vercilli Blk-Tan&lt;/ProductName&gt;
          &lt;ProviderName&gt;Boston Babes&lt;/ProviderName&gt;
          &lt;Quantity&gt;2&lt;/Quantity&gt;
          &lt;UnitPrice&gt;23.99&lt;/UnitPrice&gt;
      &lt;/Item&gt;
      &lt;Item ItemNumber="1150173"&gt;
          &lt;ProductName&gt;Lucille Tan&lt;/ProductName&gt;
          &lt;ProviderName&gt;Boston Babes&lt;/ProviderName&gt;
          &lt;Quantity&gt;1&lt;/Quantity&gt;
          &lt;UnitPrice&gt;24.99&lt;/UnitPrice&gt;
      &lt;/Item&gt;
      &lt;Item ItemNumber="1151464"&gt;
          &lt;ProductName&gt;Spritz Grape Seat and Extra Seat&lt;/ProductName&gt;
          &lt;ProviderName&gt;Bambeano&lt;/ProviderName&gt;
          &lt;Quantity&gt;1&lt;/Quantity&gt;
          &lt;UnitPrice&gt;56.99&lt;/UnitPrice&gt;
      &lt;/Item&gt;
      &lt;Item ItemNumber="1148089"&gt;
          &lt;ProductName&gt;Plexi Leather-Silver-Black&lt;/ProductName&gt;
          &lt;ProviderName&gt;Vestal Watches&lt;/ProviderName&gt;
          &lt;Quantity&gt;1&lt;/Quantity&gt;
          &lt;UnitPrice&gt;189.99&lt;/UnitPrice&gt;
      &lt;/Item&gt;
  &lt;/Items&gt;
  </code></pre>
  
  <p>Notice that the source XML contains several same <code>ProductName</code> elements with different <code>ItemNumber</code> attributes. What I desire is to group all the <code>Item</code> elements with similar <code>ItemNumber</code> and do a summation of the quantity. Thanks for the help guys. Really appreciate it.</p>
  
  <p>The example output would be:
   <img src="https://i.stack.imgur.com/6ZJxr.png" alt="enter image description here"></p>