Remove all list elements except the first in PHP?
<p>How do I remove all the li elements except the first in PHP?</p>

<pre><code>&lt;div class="category"&gt;
   &lt;ul class="products"&gt;
     &lt;li&gt;{nested child elements}&lt;/li&gt;
     &lt;li&gt;{nested child elements}&lt;/li&gt;
     &lt;li&gt;{nested child elements}&lt;/li&gt;
   &lt;/ul&gt;
&lt;/div&gt;
</code></pre>

<p>The code above is generated by another script via a function.</p>

<p>The result should be like this:</p>

<pre><code>&lt;div class="category"&gt;
    &lt;ul class="products"&gt;
        &lt;li&gt;{nested child elements}&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
</code></pre>

<p><strong>UPDATE: Sorry guys the "category" is a class not an ID.</strong></p>

<p>In repay to Yoshi, ul.products has siblings but I didn't include them in my post. Would that affect the query? </p>

<p>This is how my code looks like with Yoshi's code added:</p>

<pre><code>class Myclass {
    function prin_html() {
        $content = get_code();
        $dom = new DOMDocument;
        $dom-&gt;loadXml($content);

        $xpath = new DOMXPath($dom);
        foreach ($xpath-&gt;query('//li[position()&gt;1]') as $liNode) {
            $liNode-&gt;parentNode-&gt;removeChild($liNode); 
        }

        echo $dom-&gt;saveXml($dom-&gt;documentElement);    
    }
}
</code></pre>

<p>It sill prints the non-filtered html code...</p>
