how to Delete parent Node of XML by checking the Value of it in PHP DOM
I have an XML Like below
<Row><Cell ss:StyleID="s245"><Data ss:Type="String">ABSOLUTE</Data></Cell>
<Cell ><Data ss:Type="String">Yellow</Data></Cell>
<Cell ><Data ss:Type="String">Exist</Data></Cell>
<Cell ><Data ss:Type="Number">30</Data></Cell>
<Cell ss:StyleID="s258"/>
</Row>
<Row><Cell ss:StyleID="s229"><Data ss:Type="String">PART3</Data></Cell>
<Cell ><Data ss:Type="String">Part3 Description</Data></Cell>
<Cell ><Data ss:Type="String">Buy_Part</Data></Cell>
<Cell ><Data ss:Type="Number">0</Data></Cell>
<Cell ss:StyleID="s258"/>
</Row>
<Row ss:AutoFitHeight="0" ss:Height="13.5"><Cell ss:StyleID="s245"><Data ss:Type="String">PART3</Data></Cell>
<Cell ><Data ss:Type="String">Part3 Description</Data></Cell>
<Cell ><Data ss:Type="String">Buy_Part</Data></Cell>
<Cell ><Data ss:Type="Number">0</Data></Cell>
<Cell ss:StyleID="s258"/>
</Row>
I want to delete the Nodes of the XML searching for the Value inside the Node.
Example I want to search for the Value "PART3" in the above example and want to delete COmplete Node containing that Value. So the Output should be only as below
<Row><Cell ss:StyleID="s245"><Data ss:Type="String">ABSOLUTE</Data></Cell>
<Cell ><Data ss:Type="String">Yellow</Data></Cell>
<Cell ><Data ss:Type=开发者_运维知识库"String">Exist</Data></Cell>
<Cell ><Data ss:Type="Number">30</Data></Cell>
<Cell ss:StyleID="s258"/>
</Row>
I have written below script
$doc = new DOMDOcument;
$doc->loadxml($xmldata);
$item_id = "PART3";
$xpath = new DOMXpath($doc);
foreach($xpath->query('//Row[Cell/Data="' . $item_id . '"]') as $node) {
$node->parentNode->removeChild($node);
}
echo $doc->savexml();
But it displays the complete XML without deleting.
Please Help Me how to delete The node by searching for the Value PART3 since the XML will be Very BIG and it contains lots of <ROW><CELL><DATA></Data></Cell><ROW>
Your XPATH looks correct...just curious, but you mention that this is a sample snippet of XML. Is maybe the casing on the XML tags in the actual XML slightly different, causing a mis-match in your XPATH?
精彩评论