title: XSLT 1.0 build hierarchy from parent-child relationship
body: <p>I have the following flat xml structure.</p>
  
  <pre><code>&lt;customUI &gt;
    &lt;elements&gt;
      &lt;tab id="1" parent="0"/&gt;
      &lt;tab id="-1" parent="0"/&gt;
      &lt;group id="-2"  parent="-1"/&gt;
      &lt;group id="2"  parent="1"/&gt;
      &lt;group id="11"  parent="1"/&gt;
      &lt;menu id="-27"  parent="-26"/&gt;
      &lt;menu id="-24" parent="-4"/&gt;
      &lt;menu id="-18" parent="-4"/&gt;
      &lt;menu id="-11"  parent="-9"/&gt;
      &lt;menu id="-10" parent="-9"/&gt;
      &lt;menu id="-3"  parent="-2"/&gt;
      &lt;menu id="3"  parent="2"/&gt;
      &lt;menu id="10" parent="65"/&gt;
      &lt;menu id="12"  parent="11"/&gt;
      &lt;menu id="16"  parent="11"/&gt;
      &lt;menu id="18" parent="11"/&gt;
      &lt;menu id="25" parent="11" /&gt;
      &lt;menu id="29" parent="11"/&gt;
      &lt;menu id="46" parent="11"/&gt;
      &lt;menu id="74" parent="-3"/&gt;
      &lt;menu id="85" parent="11"/&gt;
      &lt;menu id="89" parent="2"/&gt;
      &lt;menu id="95" parent="2"/&gt;
      &lt;menu id="104"  parent="2"/&gt;
      &lt;button id="-28"  parent="-2"/&gt;
      &lt;button id="-25" parent="-24"/&gt;
      &lt;button id="-12" parent="-11"/&gt;
      &lt;button id="32" parent="29"/&gt;
      &lt;button id="26" parent="25"/&gt;
      &lt;button id="41" parent="18"/&gt;
      &lt;button id="66" parent="46"/&gt;
      &lt;button id="82"  parent="74"/&gt;
      &lt;button id="86" parent="46"/&gt;
      &lt;button id="87"  parent="89"/&gt;
      &lt;button id="90"  parent="89"/&gt;
      &lt;button id="99"  parent="89"/&gt;
      &lt;button id="58"  parent="16"/&gt;
      &lt;button id="42" parent="18"/&gt;
      &lt;button id="47"  parent="46"/&gt;
      &lt;button id="48"  parent="46"/&gt;
      &lt;button id="33" parent="29"/&gt;
      &lt;!-- The list continues --&gt;
    &lt;/elements&gt;
  &lt;/customUI&gt;
  </code></pre>
  
  <p>What I would like to do is to build a hierarchy using the relationship between the 'id' value and 'parent' value.</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"&gt;
  
  &lt;xsl:template match="@*|node()" name="identity"&gt;
      &lt;xsl:copy&gt;
         &lt;xsl:apply-templates select="@*|node()"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="tab"&gt;
      &lt;xsl:variable name="controllerId" select="@id" as="xs:string"/&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="@*|node()"/&gt;
          &lt;xsl:copy-of select="//*[@parent = $controllerId]"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  
  &lt;xsl:template match="group" name="group"&gt;
      &lt;xsl:variable name="controllerId" select="@id" as="xs:string"/&gt;
      &lt;xsl:copy&gt;
         &lt;xsl:apply-templates select="@*|node()"/&gt;
         &lt;xsl:copy-of select="//*[@parent = $controllerId]"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  
  &lt;xsl:template match="menu" name="menu"&gt;
      &lt;xsl:variable name="controllerId" select="@id" as="xs:string"/&gt;
      &lt;xsl:copy&gt;
         &lt;xsl:apply-templates select="@*|node()"/&gt;
         &lt;xsl:copy-of select="//*[@parent = $controllerId]"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>This is what I have tried until now but it does not work. </p>
  
  <p>Any help will be greatly appreciated.   </p>