title: XSLT 1.0 Grouping with multiple elements with same name
body: <p>I have an XML that looks like - </p>
  
  <pre><code>&lt;resultset&gt;
      &lt;hit&gt;
          &lt;content&gt;
              &lt;ITEM&gt;
                  &lt;TITLE&gt;Office Cleaning&lt;/TITLE&gt;
                  &lt;DESCRIPTION&gt;blah blah blah&lt;/DESCRIPTION&gt;
                  &lt;Hierarchy&gt;level1A:level2A:level3A&lt;/Hierarchy&gt;
                  &lt;Hierarchy&gt;level1B:level2B:level3B&lt;/Hierarchy&gt;
              &lt;/ITEM&gt;
          &lt;/content&gt;
      &lt;/hit&gt;
      &lt;hit&gt;
          &lt;content&gt;
              &lt;ITEM&gt;
                  &lt;TITLE&gt;Office Cleaning1&lt;/TITLE&gt;
                  &lt;DESCRIPTION&gt;blah blah blah&lt;/DESCRIPTION&gt;
                  &lt;Hierarchy&gt;level1A:level2A:level3A&lt;/Hierarchy&gt;
              &lt;/ITEM&gt;
          &lt;/content&gt;
      &lt;/hit&gt;
      &lt;hit&gt;
          &lt;content&gt;
              &lt;ITEM&gt;
                  &lt;TITLE&gt;Office Cleaning2&lt;/TITLE&gt;
                  &lt;DESCRIPTION&gt;blah blah blah&lt;/DESCRIPTION&gt;
                  &lt;Hierarchy&gt;level1A:level2B:level3C&lt;/Hierarchy&gt;
              &lt;/ITEM&gt;
          &lt;/content&gt;
      &lt;/hit&gt;
  &lt;/resultset&gt;
  </code></pre>
  
  <p>Note that there are multiple hierarchy elements which is a concatenated string of level1:level2:level3
  I am looking to transform this into something like this - </p>
  
  <pre><code>&lt;TREE&gt;
  &lt;LEVELS&gt;
  &lt;LEVEL1 name="level1A"&gt;
   &lt;LEVEL2 name="level2A"&gt;
     &lt;LEVEL3 name="level3A"&gt;
        &lt;ITEM Name="Office Cleaning"/&gt;
        &lt;ITEM Name="Office Cleaning1"/&gt;
     &lt;/LEVEL3&gt;
   &lt;/LEVEL2&gt;
  &lt;/LEVEL1&gt;
  &lt;LEVEL1 name="level1B"&gt;
   &lt;LEVEL2 name="level2B"&gt;
     &lt;LEVEL3 name="level3B"&gt;
          &lt;ITEM Name="Office Cleaning"/&gt;
     &lt;/LEVEL3&gt;
   &lt;/LEVEL2&gt;
  &lt;/LEVEL1&gt;
  &lt;LEVEL1 name="level1A"&gt;
   &lt;LEVEL2 name="level2B"&gt;
     &lt;LEVEL3 name="level3C"&gt;
        &lt;ITEM Name="Office Cleaning2"/&gt;
      &lt;/LEVEL3&gt;
   &lt;/LEVEL2&gt;
  &lt;/LEVEL1&gt;
  &lt;/LEVELS&gt;
  &lt;/TREE&gt;
  </code></pre>
  
  <p>Basically each item has multiple hierachy associated with it. I need to group them together. </p>
  
  <p>I got only as far as - </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:autn="http://schemas.autonomy.com/aci/"&gt;
  
  &lt;xsl:output method="xml" omit-xml-declaration="yes"/&gt;
  
  &lt;xsl:key name="HIERARCHYLEVELS" match="resultset/hit/content/ITEM" use="HIERARCHY" /&gt;
  &lt;xsl:template match="/"&gt;
  &lt;TREE&gt;
  
      &lt;xsl:for-each select="resultset/hit/content/ITEM[generate-id()=generate-id(key('HIERARCHYLEVELS', HIERARCHY)[1])]"&gt;
          &lt;xsl:for-each select="HIERARCHY"&gt;
          &lt;xsl:variable name="level" select="HIERARCHY"/&gt;
          &lt;HIERARCHY name="{$level}" &gt;
  
              &lt;xsl:variable name="name" select="TITLE"/&gt;
  
              &lt;ITEM name="{$name}"/&gt;
  
          &lt;/HIERARCHY&gt;
          &lt;/xsl:for-each&gt;
      &lt;/xsl:for-each&gt;
  
  
  &lt;/TREE&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>But the problem is I only get the first matching hierarchy tag. For e.g. I dont get to see "Office cleaning1". 
  What can I do to make sure all hierarchy elements are considered? I still need to split it into various levels. </p>