My XML something like this:
<?xml version="1.0"?>
<document>
<consonant>
<L>
<l1>l1</l1>
</L>
<M>
<m1>m1</m1>
</M>
<N>
<n1>n1</n1>
</N>
</consonant>
<consonant>
<L>
<l1>l1</l1>
</L>
<M>
<m1>m1</m1>
</M>
<N>
<n1>n1</n1>
</N>
</consonant>
</document>
check if <p1> tag is not exist in add this <p1><a>1223</a></p1> into it.
<consonant>
<L>
<l1>l1</l1>
</L>
<M>
<m1>m1</m1>
<p1><a>1223</a></p1>
</M>
<N>
<n1>n1</n1>
</N>
</consonant>
I am trying :
$xml = simplexml_load_string($myxml); // return object
$nss = $xml->getDocNamespaces(TRUE);
$xml->registerXPathNamespace('__empty_ns', $nss[""]);
$result = $xml->xpath("/__empty_ns:Document/__empty_ns:consonant");//consonant node
foreach($result as $key=>$value){ // loop through all <consonant>
if(!array_key_exist('p1')){ // if not exist
$value['p1'] = // add node here <p1><a>1223</a></p1>
}
}
and used the object operation on it (condifion ,loop,append,.)
anybody could tell me how Can I do this ?
You could try something like this:
Output: