I have an xml structure :
<Articles>
<Article ID="111">
<author>Peter Paul</author>
<pubDate>01/01/2015</pubDate>
<Translations>
<lang1>English</lang1>
<lang2>French</lang2>
<lang3>Arab</lang3>
<lang3>Chinese</lang3>
</Translations>
</Article>
<Article ID="222">
<author>Monkey Rice</author>
<pubDate>01/01/2016</pubDate>
<Translations>
<lang1>English</lang1>
</Translations>
</Article>
<Article ID="333">
<author>John Silas</author>
<pubDate>01/01/2017</pubDate>
<Translations>
<lang1>English</lang1>
<lang2>French</lang2>
<lang3>Arab</lang3>
<lang3>Chinese</lang3>
</Translations>
</Article>
</Articles>
i created a method AddRecordByInfoMatch() that attempts to
add new node to any <Article> of a given ID anywhere as long
as a match exists:
function AddRecordByInfoMatch($ParentID, $Info_1, $Info_2, $Info_3, array $Record){
$xml = new SimpleXMLElement(blabla.xml);
$result = $xml->xpath("//*[@ID='$ParentID']"); //get the article ID
if(!empty($result)){
foreach($result[0] as $key => $value){
$noofChild = count($value);
//three info match may lakely be within 3 sub-nodes
if($noofChild >= 3){
$query = "//*[node()[contains(text(), \"$Info_1\")] and node()[contains(text(), \"$Info_2\")] and node()[contains(text(), \"$Info_3\")]]";
$data = $xml->xpath($query);
if(!empty($data[0]){
foreach ($Record as $nodname => $val){
$data[0]->addChild($nodname, $val);
}
}
}
}
}
}
with ID = 333 in mind, i test-ran it like this :
XMLAddRecordByInfoMatch(333, "English", "French", "Chinese", array(
"syntax" => irrelevant,
"adjectives" => None,
"verbs" => 2,
"prepositions" => 5
));
unfortunately, the output; upon display, added the new record to Article
with ID 111 to give :
...
<Article ID="111">
<author>Peter Paul</author>
<pubDate>01/01/2015</pubDate>
<Translations>
<lang1>English</lang1>
<lang2>French</lang2>
<lang3>Arab</lang3>
<lang3>Chinese</lang3>
<syntax>irrelevant</syntax>
<adjectives>None</adjectives>
<verbs>2</verbs>
<prepositions>5</prepositions>
</Translations>
</Article>
...
and i expected this to be within the Article node of ID 333, which
i specified. what am i doing wrong in my xpath xpression ?? or how
can i achieve this ? any help will be highly regarded. Happy new year all.