title: Remove referencing element iteratively
body: <p>I'm a newbie to XSLT and don't know how to solve a case where I'd like to remove some nodes referencing other ones depending on the referenced elements position in the code.</p>
  
  <p>Here is a short and basic example (syntax might not be exact). The source is:</p>
  
  <pre><code>&lt;nodeTypeA id=0&gt;
    &lt;nodeTypeB id="1"&gt;lorem ipsum&lt;/nodeTypeB&gt;
    &lt;nodeTypeC id="2"&gt;
       &lt;nodeTypeD id="3"&gt;D1&lt;/nodeTypeD&gt;
       &lt;nodeTypeE id="4"&gt;D2&lt;/nodeTypeE&gt;
       &lt;nodeTypeF id="5"&gt;D2&lt;/nodeTypeF&gt;
    &lt;/nodeTypeC&gt;
    &lt;nodeTypeC id="6"&gt;
       &lt;nodeTypeD id="7"&gt;D1&lt;/nodeTypeD&gt;
       &lt;nodeTypeE id="8"&gt;D2&lt;/nodeTypeE&gt;
       &lt;nodeTypeF id="9"&gt;D2&lt;/nodeTypeF&gt;
    &lt;/nodeTypeC&gt; 
  &lt;/nodeTypeA&gt;
  &lt;nodeType1 id="a"&gt;
    &lt;nodeType2 id="b" ref="5"&gt;lorem ipsum&lt;/nodeType2&gt;
    &lt;nodeType3 id="c" ref="2"&gt;lorem ipsum&lt;/nodeType3&gt; 
    &lt;nodeType4 id="d" ref="1"&gt;lorem ipsum&lt;/nodeType4&gt;
    &lt;nodeType5 id="e" ref="3"&gt;lorem ipsum&lt;/nodeType5&gt;
    &lt;nodeType6 id="f" ref="4"&gt;lorem ipsum&lt;/nodeType6&gt; 
    &lt;nodeType2 id="g" ref="6"&gt;lorem ipsum&lt;/nodeType2&gt;
    &lt;nodeType3 id="h" ref="7"&gt;lorem ipsum&lt;/nodeType3&gt; 
    &lt;nodeType5 id="i" ref="8"&gt;lorem ipsum&lt;/nodeType5&gt;
    &lt;nodeType6 id="j" ref="9"&gt;lorem ipsum&lt;/nodeType6&gt;          
  &lt;/nodeType1&gt;
  </code></pre>
  
  <p>My goal is to remove all children of nodeTypeC nodes (i.e. those with ids 2 &amp; 6) as well as the nodes under nodeType1 which refer to those removed children (i.e. ids 3, 4, 5, 7, 8, 9). So I'd like to remove nodes with ids e, f, b, h, i, j. The resulting partial XML would be the following:</p>
  
  <pre><code>&lt;nodeTypeA id=0&gt;
    &lt;nodeTypeB id="1"&gt;lorem ipsum&lt;/nodeTypeB&gt;
    &lt;nodeTypeC id="2"&gt;&lt;/nodeTypeC&gt;
    &lt;nodeTypeC id="6"&gt;&lt;/nodeTypeC&gt; 
  &lt;/nodeTypeA&gt;
  &lt;nodeType1 id="a"&gt;
    &lt;nodeType3 id="c" ref="2"&gt;lorem ipsum&lt;/nodeType3&gt; 
    &lt;nodeType4 id="d" ref="1"&gt;lorem ipsum&lt;/nodeType4&gt;
    &lt;nodeType2 id="g" ref="6"&gt;lorem ipsum&lt;/nodeType2&gt;        
  &lt;/nodeType1&gt;
  </code></pre>
  
  <p>I'm completely new to XSLT, so the answer might seem trivial to you...
  Thank you for your help!</p>