<p>I have several identical elements with different attributes that I'm accessing with SimpleXML:</p>

<pre><code>&lt;data&gt;
    &lt;seg id="A1"/&gt;
    &lt;seg id="A5"/&gt;
    &lt;seg id="A12"/&gt;
    &lt;seg id="A29"/&gt;
    &lt;seg id="A30"/&gt;
&lt;/data&gt;
</code></pre>

<p>I need to remove a specific <strong>seg</strong> element, with an id of "A12", how can I do this?  I've tried looping through the <strong>seg</strong> elements and <em>unset</em>ting the specific one, but this doesn't work, the elements remain.</p>

<pre><code>foreach($doc-&gt;seg as $seg)
{
    if($seg['id'] == 'A12')
    {
        unset($seg);
    }
}
</code></pre>

While
SimpleXML
provides
a way to remove
XML nodes, its modification capabilities are somewhat limited. One other solution is to resort to using the
DOM
extension.
dom_import_simplexml()
will help you with converting your
```
SimpleXMLElement
```
into a
```
DOMElement
```
.
Just some example code (tested with PHP 5.2.5):
```
```
$data='<data>
    <seg id="A1"/>
    <seg id="A5"/>
    <seg id="A12"/>
    <seg id="A29"/>
    <seg id="A30"/>
</data>';
$doc=new SimpleXMLElement($data);
foreach($doc->seg as $seg)
{
    if($seg['id'] == 'A12') {
        $dom=dom_import_simplexml($seg);
        $dom->parentNode->removeChild($dom);
    }
}
echo $doc->asXml();
```
```
outputs
```
```
<?xml version="1.0"?>
<data><seg id="A1"/><seg id="A5"/><seg id="A29"/><seg id="A30"/></data>
```
```
By the way: selecting specific nodes is much more simple when you use XPath (
SimpleXMLElement->xpath
):
```
```
$segs=$doc->xpath('//seq[@id="A12"]');
if (count($segs)>=1) {
    $seg=$segs[0];
}
// same deletion procedure as above
```
```