title: Recursively remove folders generated by Heat.exe with xslt
body: <p>I know there are questions and answers out there for removing single directories.  I have an XML generated by Heat.exe that contains 1000s of files and about 20 top level directories.  I want to use the xsl transform to remove one of those top level directories.  That directory has 6 directories in it and those directories have some etc.</p>
  
  <p>I have code that will either remove just the top level directory (called tests) and any subfolders that do not have subfolders themselves, leaving me with unresolved reference errors:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?xml version="1.0" ?&gt;
      &lt;xsl:stylesheet version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:wix="http://schemas.microsoft.com/wix/2006/wi"&gt;
  
          &lt;!-- Copy all attributes and elements to the output. --&gt;
          &lt;xsl:template match="@*|*"&gt;
              &lt;xsl:copy&gt;
                  &lt;xsl:apply-templates select="@*" /&gt;
                  &lt;xsl:apply-templates select="*" /&gt;
              &lt;/xsl:copy&gt;
          &lt;/xsl:template&gt;
  
          &lt;xsl:output method="xml" indent="yes" /&gt;
  
          &lt;!-- Create searches for the directories to remove. --&gt;
       &lt;xsl:key name="tests-search" match="wix:Directory[@Name='tests']//wix:Component" use="@Id" /&gt;
  &lt;!-- Remove directories. --&gt;
      &lt;xsl:template match="wix:Directory[@Name='tests']" /&gt;
  &lt;!-- Remove Components referencing those directories. --&gt;
   &lt;xsl:template match="wix:Component[key('tests-search', @Directory)]" /&gt;
    &lt;!-- Remove DirectoryRefs (and their parent Fragments) referencing those directories. --&gt;
  &lt;xsl:template match="wix:Fragment[wix:DirectoryRef[key('tests-search', @Id)]]" /&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>If I change the key line to be </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;xsl:key name="tests-search1" match="wix:Directory[@Name = 'tests']" use="descendant::wix:Component/@Id" /&gt; </code></pre>
  </div>
  </div>
  </p>
  
  <p>It will remove all subfolder their subfolders, but leave the all the Components (files) that are in the top level directory.</p>
  
  <p>If I use both keys, it still ends with all components in the top level directory (tests) existing, giving me Unresolved reference errors.</p>
  
  <p>Is there a way or a single search key to use that will remove all files and subfolders from a top level folder recursively?</p>
  
  <p>Thanks!</p>