如何使用 SimpleXML 的 XPath 支持设置文本内容


How to set text content with SimpleXML's XPath support?

<xml>
    <ns:foo attribute="bar">
        OLD TEXT DATA
    </ns:foo>
</xml>

我使用 XPath 来处理 XML 命名空间。这不起作用:

$xml->asXML('old.xml');
foreach ($xml->xpath('ns:foo') as $foo) {
    $foo['attribute'] = 'new bar';
    $foo = 'NEW TEXT DATA'; //This won't be saved by asXML().
}
$xml->asXML('new.xml');

如何使用SimpleXMLElement::xpath更改文本内容?

$xml->asXML('old.xml');
foreach ($xml->xpath('ns:foo') as $foo) {
    $foo['attribute'] = 'new bar';
    $foo[0] = 'NEW TEXT DATA';
}
$xml->asXML('new.xml');