SimpleXML在具有相同PHP版本的两个不同服务器上生成的两个不同输出

问题描述 投票:0回答:1

我有此代码:

$feed = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:yandex="http://news.yandex.ru" xmlns:media="http://search.yahoo.com/mrss/" xmlns:turbo="http://turbo.yandex.ru" />', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
$channel = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><channel></channel>', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
$counter = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><yandex:analytics>test</yandex:analytics>', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);

$toDom = dom_import_simplexml($channel);
$fromDom = dom_import_simplexml($counter);
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));

$toDom = dom_import_simplexml($feed);
$fromDom = dom_import_simplexml($channel);
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->appendChild($dom->importNode(dom_import_simplexml($feed), true));
$dom->formatOutput = true;

var_dump($feed->saveXML());

现在,在一台服务器上的输出是:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:yandex="http://news.yandex.ru" xmlns:media="http://search.yahoo.com/mrss/" xmlns:turbo="http://turbo.yandex.ru" version="2.0"><channel><yandex:analytics>test</yandex:analytics></channel></rss>

第二个输出是:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:yandex="http://news.yandex.ru" xmlns:media="http://search.yahoo.com/mrss/" xmlns:turbo="http://turbo.yandex.ru" version="2.0"><channel><analytics>test</analytics></channel></rss>

[两台服务器都具有相同的PHP版本:7.1.30我在两个phpinfo()中注意到的唯一与xml相关的区别是,第一个具有libxml2 2.9.9,另一个具有2.7.6

较旧的是旧的CentOS 6系统,很遗憾,目前无法升级,而且我什至不确定这是否是libxml2的问题。

现在的问题是,我有一个依赖于新行为的库。

确定与libxml2相关吗?

是否可以解决该问题,而不会根据第一个行为而丢弃该库?

php libxml2
1个回答
0
投票

$counter XML无效,因为您目前拥有它...

$counter = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?>
         <yandex:analytics>test</yandex:analytics>', 
     LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);

<yandex:analytics>使用的是XML本身未定义的名称空间。

您应该更改此名称以定义名称空间...

<yandex:analytics xmlns:yandex="http://news.yandex.ru">test</yandex:analytics>

或者您可以通过在添加元素之前手动创建元素来添加它...

$fromDom = $toDom->ownerDocument->createElementNS("yandex", "yandex:analytics", "test");
© www.soinside.com 2019 - 2024. All rights reserved.