Reputation: 1575
I want to remove the child tag from this xml . for example i want to remove this one
"<ExtraCategory diffgr:id="ExtraCategory1" msdata:rowOrder="0">
<Barcode>5051694676090</Barcode>
<CategoryID>1</CategoryID>
<Value>High Quality</Value>
</ExtraCategory>"
Child. I am parsing the xml in this way.
$doc = new DOMDocument();
$doc->loadXML($Xmlresult);
$xpath = new DOMXpath($doc);
unset($doc->xpath("ExtraCategory[@diffgr:id=$id]")[0]);
But failed to remove. I have done all the anwsers on stackoverflow but failed to get solution. Any help will be appreciated. my xml is following:
<DocumentElement
xmlns="">
<ExtraCategory diffgr:id="ExtraCategory1" msdata:rowOrder="0">
<Barcode>5051694676090</Barcode>
<CategoryID>1</CategoryID>
<Value>High Quality</Value>
</ExtraCategory>
<ExtraCategory diffgr:id="ExtraCategory2" msdata:rowOrder="1">
<Barcode>5051694676090</Barcode>
<CategoryID>2</CategoryID>
<Value>Stylish Appearance</Value>
</ExtraCategory>
<ExtraCategory diffgr:id="ExtraCategory3" msdata:rowOrder="2">
<Barcode>5051694676090</Barcode>
<CategoryID>3</CategoryID>
<Value>In Fashion</Value>
</ExtraCategory>
Upvotes: 0
Views: 47
Reputation: 57121
You can't just unset()
an XML node, you need to remove the node from the DOM. Something like...
$Xmlresult = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<DocumentElement
xmlns:diffgr="http://someURL" xmlns:msdata="http://someURL">
<ExtraCategory diffgr:id="ExtraCategory1" msdata:rowOrder="0">
<Barcode>5051694676090</Barcode>
<CategoryID>1</CategoryID>
<Value>High Quality</Value>
</ExtraCategory>
<ExtraCategory diffgr:id="ExtraCategory2" msdata:rowOrder="1">
<Barcode>5051694676090</Barcode>
<CategoryID>2</CategoryID>
<Value>Stylish Appearance</Value>
</ExtraCategory>
<ExtraCategory diffgr:id="ExtraCategory3" msdata:rowOrder="2">
<Barcode>5051694676090</Barcode>
<CategoryID>3</CategoryID>
<Value>In Fashion</Value>
</ExtraCategory>
</DocumentElement>
XML;
$doc = new DOMDocument();
$doc->loadXML($Xmlresult);
$xpath = new DOMXpath($doc);
$id="ExtraCategory1";
$xpath->registerNamespace("diffgr", "http://someURL");
$removeNode = $xpath->query("//ExtraCategory[@diffgr:id=\"$id\"]")[0];
$removeNode->parentNode->removeChild($removeNode);
echo $doc->saveXML();
The registerNamespace
is needed to allow you to reference the namespace in the XPath expression, you will need to correct the namespace as this is just a dummy one I used to make it work.
The main bit is using removeChild
, this is a bit of a convoluted statement, but it takes the node you want to remove, goes up to it's parent node and then removes the child node from it.
will give...
<?xml version="1.0" encoding="UTF-8"?>
<DocumentElement xmlns:diffgr="http://someURL" xmlns:msdata="http://someURL">
<ExtraCategory diffgr:id="ExtraCategory2" msdata:rowOrder="1">
<Barcode>5051694676090</Barcode>
<CategoryID>2</CategoryID>
<Value>Stylish Appearance</Value>
</ExtraCategory>
<ExtraCategory diffgr:id="ExtraCategory3" msdata:rowOrder="2">
<Barcode>5051694676090</Barcode>
<CategoryID>3</CategoryID>
<Value>In Fashion</Value>
</ExtraCategory>
</DocumentElement>
(Note I've added the declaration of the namespaces, you will probably have your own definitions, but I needed to add something.)
Upvotes: 1