title: Filtering XML elements using XSLT
body: <p>Supposed I have the following XML input file file:</p>
  
  <pre><code>&lt;root&gt; 
    &lt;NodeX&gt; 
        ... 
    &lt;/NodeX&gt;  
    &lt;NodeY&gt; 
        ... 
    &lt;/NodeY&gt;  
    &lt;Description&gt;
        &lt;section01&gt;
           &lt;subsection key="KeyA"&gt;Some text&lt;/subsection&gt;
           &lt;subsection key="KeyB"&gt;Some text&lt;/subsection&gt;
        &lt;/section1&gt;
  
        &lt;section02&gt;
           &lt;subsection key="KeyC"&gt;Some text&lt;/subsection&gt;
        &lt;/section2&gt;
  
        &lt;section03&gt;
           &lt;subsection key="KeyD"&gt;Some Text&lt;/subsection&gt;
        &lt;/section3&gt;
  
     &lt;/Description&gt;  
     ...
  &lt;/root&gt;
  </code></pre>
  
  <p>And another XML file with "rules" where the keys of the subsections are listed together with an audience attribute.</p>
  
  <p>Example excerpt:</p>
  
  <pre><code>&lt;rules&gt;
      &lt;subsection id="01"&gt;
          &lt;key audience="internalOnly"&gt;KeyA&lt;/key&gt;
          &lt;key audience="internalOnly"&gt;KeyB&lt;/key&gt;
      &lt;/subsection&gt;
  
      &lt;subsection id="02"&gt;
          &lt;key&gt;KeyC&lt;/key&gt;
      &lt;/subsection&gt;
  &lt;rules&gt;
  </code></pre>
  
  <p>I try to write an XSL transformation that removes subsections from the input XML file based on the value of the audience attribute in the rules XML fille. If the value is "internalOnly" the subsection has to be removed.</p>
  
  <p>In the example the following output XML should result:</p>
  
  <pre><code>&lt;root&gt; 
    &lt;NodeX&gt; 
        ... 
    &lt;/NodeX&gt;  
    &lt;NodeY&gt; 
        ... 
    &lt;/NodeY&gt;  
    &lt;Description&gt;
        &lt;section02&gt;
           &lt;subsection key="KeyC"&gt;Some text&lt;/subsection&gt;
        &lt;/section2&gt;
  
        &lt;section03&gt;
           &lt;subsection key="KeyD"&gt;Some Text&lt;/subsection&gt;
        &lt;/section3&gt;
  
     &lt;/Description&gt;  
     ...
  &lt;/root&gt;
  </code></pre>
  
  <p>(The whole section01 is removed because both subsections are "internalOnly").</p>
  
  <p>The problems are 1) the "lookup" in the rules XML file and 2) the removal of section elements if all the corresponding subsections are removed:</p>
  
  <p>If the audience attribute would be included in the input XML file I would create the following XSLT for the requirement:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/&gt;
      &lt;xsl:template match="@* | node()"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@* | node()"/&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="subsection[@audience='internalOnly']"&gt;&lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>