1
<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

4

1 回答 1

0
$xml->asXML('old.xml');

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

$xml->asXML('new.xml');
于 2013-09-23T17:49:46.113 回答