首先,我使用的是 Laravel,这就是为什么return在代码末尾有但它实际上不会影响任何东西
$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();
它输出:
<?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>
<target>注意文字上有特殊字符
已经这样做$target->nodeValue = html_entity_decode('<mrk id="1">Banana');但没有工作
我该如何编码?