1

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

5
  • Could you give a bit more details about "doesn't work" Do you get an error (if so what is the exact error message)? Or do you get a result that's not what you want (if so, what is the wanted result, and what is the actual)? Commented Oct 15, 2015 at 13:08
  • Sorry, forgot to copy the error message. I've added it to the question Commented Oct 15, 2015 at 13:10
  • Aha, I might be wrong, but I think you need to pass the value for your new element through htmlspecialchars(); for some reason, various of the XML functions in PHP expect you to pass valid XML fragments rather than text content. Commented Oct 15, 2015 at 13:24
  • I'm no PHP expert, but I am sharp with XPath. It looks like your $regions variable corresponds to the <property> tag, when it should in fact correspond to the <value> tag within the <property> tag Commented Oct 15, 2015 at 13:25
  • It doesn't have to do anything with htmlspecialchars(), 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 three value elements in each property with the name city. In that case i need to select the third element. Commented Oct 15, 2015 at 13:46

2 Answers 2

1

The error message is from a bug in PHP. Do not use the second argument for DOMDocument::createElement(). Create and append a text node to make sure that special characters are escaped into entities.

https://stackoverflow.com/a/27225157/2265374

Anything in a DOM is a node. Not only the element, but attribute and texts, too. You can work on the text nodes inside the value elements directly:

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);

foreach ($xpath->evaluate('//property[@name = "region"]/value/text()') as $text) {
  $text->data = str_replace("Cote d'azur","Côte d'Azur", $text->data);
}

echo $document->saveXml();
Sign up to request clarification or add additional context in comments.

Comments

0

Simple:

$regions = $xpath->query('//property[@name = "region"]/value');
foreach($regions as $region){
    $region->nodeValue = str_replace("Cote d'azur","Côte d'Azur",$region->nodeValue);
}
echo $document->saveXml();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.