Delete text and other elements before and after the element
<p>I need to delete everything (text and other elements) before and after the element <code>&lt;a&gt;</code> with value class <code>chatlink</code>.
All this, within each element <code>&lt;div&gt;</code> with value class <code>main</code>.</p>

<p><strong>But only if <code>&lt;div&gt;</code> element with the value <code>main</code> contains a link with the value of <code>chatlink</code>.</strong></p>

<p>For example:</p>

<pre><code>&lt;div class="main"&gt;
    Bla bla bla :)
    &lt;a href="#" class="chatlink"&gt;&lt;img src="#" /&gt;&lt;/a&gt;
    bla bla bla ...
    &lt;a href="#" class="chatlink"&gt;&lt;img src="#" /&gt;&lt;/a&gt;
    tra la la la laaa
    &lt;a href="#" class="postlink"&gt;some text&lt;/a&gt;
    tralalaaa
&lt;/div&gt;
</code></pre>

<p>Final code:</p>

<pre><code>&lt;div class="main"&gt;
    &lt;a href="#" class="chatlink"&gt;&lt;img src="#" /&gt;&lt;/a&gt;
    &lt;a href="#" class="chatlink"&gt;&lt;img src="#" /&gt;&lt;/a&gt;
&lt;/div&gt;
</code></pre>

<p>Is it possible?</p>
