title: How to group in XSLT using Muenchian Method?
body: <p>I'm quite new to XSLT and I'm using XSLT 1.0. Now I need to group some stuff in a quite big XML file. There are lots of examples out there but none worked for me for some reason. I'm able to group the info I want, but I also get some extra text in my output xml. Here's what I'm doing right now;</p>
  
  <p>Input XML (this is a temp XML, actual input is quite bigger but I will be able to apply the same to my real xml too)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;objects&gt;
    &lt;groupNumber&gt;15&lt;/groupNumber&gt;
    &lt;object&gt;
      &lt;items&gt;
        &lt;item&gt;
          &lt;itemOptions&gt;
            &lt;itemNumber&gt;1&lt;/itemNumber&gt;
          &lt;/itemOptions&gt;
        &lt;/item&gt;
        &lt;item&gt;
          &lt;itemOptions&gt;
            &lt;itemNumber&gt;1&lt;/itemNumber&gt;
          &lt;/itemOptions&gt;
        &lt;/item&gt;
        &lt;item&gt;
          &lt;itemOptions&gt;
            &lt;itemNumber&gt;2&lt;/itemNumber&gt;
          &lt;/itemOptions&gt;
        &lt;/item&gt;
        &lt;item&gt;
          &lt;itemOptions&gt;
            &lt;itemNumber&gt;3&lt;/itemNumber&gt;
          &lt;/itemOptions&gt;
        &lt;/item&gt;
        &lt;item&gt;
          &lt;itemOptions&gt;
            &lt;itemNumber&gt;3&lt;/itemNumber&gt;
          &lt;/itemOptions&gt;
        &lt;/item&gt;
      &lt;/items&gt;
    &lt;/object&gt;
  &lt;/objects&gt;
  </code></pre>
  
  <p>Now I want to group the items by their itemNumbers. Here's what I do;</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="2.0"
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  exclude-result-prefixes="xs xsi xsl"&gt;
  
    &lt;xsl:output method="xml" encoding="UTF-8" indent="yes"/&gt;
  
    &lt;xsl:key name="itemler" match="item" use="itemOptions/itemNumber" /&gt;
  
    &lt;xsl:template match="items" name="temp"&gt;
      &lt;numbers&gt;
      &lt;xsl:for-each select="item[count(. | key('itemler', itemOptions/itemNumber)[1])=1]"&gt;
        &lt;number&gt;
          &lt;xsl:value-of select="itemOptions/itemNumber" /&gt;
        &lt;/number&gt;
      &lt;/xsl:for-each&gt;
      &lt;/numbers&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>With this code, I'm getting this output;</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  15
  
  &lt;numbers&gt;
    &lt;number&gt;1&lt;/number&gt;
    &lt;number&gt;2&lt;/number&gt;
    &lt;number&gt;3&lt;/number&gt;
  &lt;/numbers&gt;
  </code></pre>
  
  <p>Which is almost exactly what I want, except the "15". I get the grouped numbers AND a value from  tag which is under root. </p>
  
  <p>I'm getting this exactly same error when I try to apply this XSLT to my main XML, I get what I want with lots of unwanted info from tags under root and some other tags. What is the problem here?</p>
  
  <p>I'm guessing it's something related to templates or matches but I really have no idea how to solve.</p>
  
  <p>Thank you very much.</p>