First, I'm using Laravel that's why there's return
at the end of the code but it won't affect anything actually
$strxml = '<?xml version="1.0" encoding="utf-8" ?>
<xliff>
<body>
<trans-unit id="NFDBB2FA9-tu4" xml:space="preserve">
<source xml:lang="en">He</source>
<target xml:lang="id">He</target>
</trans-unit>
<trans-unit id="NFDBB2FA9-tu5" xml:space="preserve">
<source xml:lang="en">She</source>
<target xml:lang="id">She</target>
</trans-unit>
</body>
<body>
<trans-unit id="NFDBB2FA9-tu6" xml:space="preserve">
<source xml:lang="en">They</source>
<target xml:lang="id">They</target>
</trans-unit>
<trans-unit id="NFDBB2FA9-tu7" xml:space="preserve">
<source xml:lang="en">We</source>
<target xml:lang="id">We</target>
</trans-unit>
</body>
</xliff>';
$dom = new \DOMDocument;
$dom->loadXML($strxml);
$xp = new \DOMXPath($dom);
$xp->registerNamespace('xml', 'http://www.example.com');
$col = $xp->query('//xliff/body/trans-unit');
if ($col && $col->length) {
foreach ($col as $node) {
$target = $xp->query('target', $node)->item(0);
$target->nodeValue = '<mrk id="1">Banana';
}
}
return $dom->saveXML();
it outputs:
<?xml version="1.0" encoding="utf-8" ?>
<xliff>
<body>
<trans-unit id="NFDBB2FA9-tu4" xml:space="preserve">
<source xml:lang="en">He</source>
<target xml:lang="id"><mrk id="1">Banana</target>
</trans-unit>
<trans-unit id="NFDBB2FA9-tu5" xml:space="preserve">
<source xml:lang="en">She</source>
<target xml:lang="id"><mrk id="1">Banana</target>
</trans-unit>
</body>
<body>
<trans-unit id="NFDBB2FA9-tu6" xml:space="preserve">
<source xml:lang="en">They</source>
<target xml:lang="id"><mrk id="1">Banana</target>
</trans-unit>
<trans-unit id="NFDBB2FA9-tu7" xml:space="preserve">
<source xml:lang="en">We</source>
<target xml:lang="id"><mrk id="1">Banana</target>
</trans-unit>
</body>
</xliff>
notice there are special characters on the <target>
text
have done this $target->nodeValue = html_entity_decode('<mrk id="1">Banana');
but didn't work
How do I encode it?