I (conceptually) understand which steps i have to take, but i can't translate it to a working code.
I have a XML feed with a structure like this:
<item id="1">
<properties>
<property name="region">
<value>Cote d'azur</value>
</property>
</properties>
</item>
<item id="2">
<properties>
<property name="region">
<value>Côte d'Azur</value>
</property>
</properties>
</item>
What i need is the feed to use consequent names, so i have to loop through each property with the name attribute and replace the value, but how?
So far i'm here, but this doesn't work
$xml_src = 'feed.xml';
$document = new DOMDocument();
$document->load($xml_src);
$xpath = new DOMXpath($document);
$regions = $xpath->evaluate('//property[@name = "region"]');
foreach($regions as $region){
$newregion = $document->createElement('value', str_replace("Cote d'azur","Côte d'Azur",$region->nodeValue));
$region->parentNode->replaceChild($newregion, $region);
}
echo $document->saveXml();
I get this error:
Warning: DOMDocument::createElement(): unterminated entity reference Ylläs in .. on line 17
Line 17:
$newregion = $document->createElement('value', str_replace("Cote d'azur","Côte d'Azur",$region->nodeValue));
To make it even more complicated, i sometimes have three value elements in each property with the name city. In that case i need to select the third element.
I hope anybody can help me out
htmlspecialchars(); for some reason, various of the XML functions in PHP expect you to pass valid XML fragments rather than text content.$regionsvariable corresponds to the<property>tag, when it should in fact correspond to the<value>tag within the<property>taghtmlspecialchars(), tried that but still errors. I understand, @lambo477, what you mean, but i can't find out how to select the value element, To make it even more complicated, i sometimes have threevalueelements in each property with the name city. In that case i need to select the third element.