hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

SimpleXMLElement::asXML differs from Zend for empty elements

Open HRMsimon opened this issue 12 years ago • 14 comments

<?php

$a = new SimpleXMLElement("<a/>");
$a->addChild('b');
var_dump($a->asXML());

Expected (Zend) output:

string(34) "<?xml version="1.0"?>
<a><b/></a>
"

Actual output:

string(37) "<?xml version="1.0"?>
<a><b></b></a>
"

The <b> is self-closing under Zend but not HHVM. This only seems to happen with nodes added dynamically. If passed as part of the string used during construction, the outputs both have the self-closing tag.

// @danslo

HRMsimon avatar Jun 19 '14 00:06 HRMsimon

On it.

danslo avatar Jun 19 '14 03:06 danslo

$ cat test.php
<?php

$a = new SimpleXMLElement("<a/>");
$a->addChild('b');
var_dump($a->asXML());
$ ./hphp/hhvm/hhvm test.php
string(34) "<?xml version="1.0"?>
<a><b/></a>
"

Try a more recent build? :-)

danslo avatar Jun 24 '14 11:06 danslo

It's a LibXML difference. I get the problem with HHVM and libxml2.7.8 though not with PHP and 2.7.8.

HRMsimon avatar Jun 24 '14 23:06 HRMsimon

On the same topic, empty($a) yields different results in PHP 5.3 and HHVM 3.3 (libxml2.7.8).

@danslo FYI, this affects Magento when configured for separate read-only and read-write databases. In such a configuration, <use/> is often in <connection> in local.xml. Line 1407 in app/code/core/Mage/Core/Model/Config.php (Magento EE 1.12) causes a problem because HHVM thinks $conn->use is not empty.

aingram avatar Oct 12 '14 18:10 aingram

@aingram Same topic (SimpleXML), but not same issue... So please create a separate issue that has a small reproduction.

danslo avatar Oct 12 '14 23:10 danslo

Done: #3963

aingram avatar Oct 13 '14 03:10 aingram

@danslo can this be closed?

paulbiss avatar Nov 03 '14 21:11 paulbiss

@paulbiss I don't know, I could never reproduce this (seemed to depend on libxml version).

danslo avatar Nov 03 '14 21:11 danslo

Thanks!

paulbiss avatar Nov 03 '14 21:11 paulbiss

@paulbiss this is still an problem (at least with 3.3.1):

root@release-app:~# cat test.php 
<?php

$a = new SimpleXMLElement("<a/>");
$a->addChild('b');
var_dump($a->asXML());
root@release-app:~# hhvm test.php 
string(37) "<?xml version="1.0"?>
<a><b></b></a>
"
root@release-app:~# hhvm --version
HipHop VM 3.3.1 (rel)
Compiler: tags/HHVM-3.3.1-0-ge0c98e21167b425dddf1fc9efe78c9f7a36db268
Repo schema: f53e1743f31a2dee70c46aa66fb1b321c3e97c10
Extension API: 20140829
root@release-app:~# php test.php 
string(34) "<?xml version="1.0"?>
<a><b/></a>
"
root@release-app:~# php --version
PHP 5.5.12-2+deb.sury.org~precise+1 (cli) (built: May 12 2014 13:46:35) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
root@release-app:~# dpkg -l | grep libxml2
ii  libxml2                              2.7.8.dfsg-5.1ubuntu4.11             GNOME XML library
ii  libxml2-dev                          2.7.8.dfsg-5.1ubuntu4.11             Development files for the GNOME XML library

HHVM matches PHP with a later libxml version (whatever Ubuntu 14.04 has), but is different with this one (Ubuntu 12.04)

HRMsimon avatar Nov 03 '14 22:11 HRMsimon

I can re-open this as a "build" issue but if the issue is with an external library it's unlikely there will be a fix on our end.

paulbiss avatar Nov 03 '14 22:11 paulbiss

From what I can tell, it's more an interface issue. PHP and HHVM are using the exact same libxml version, but HHVM has different behaviour

HRMsimon avatar Nov 03 '14 22:11 HRMsimon

Did a bit of digging and it appears that it's down to xmlIsXhtml (part of libxml), which checks for a XHTML DTD and then outputs self closing tags. Don't know why PHP has it return 1 when HHVM doesn't though.

simonwelsh avatar Nov 04 '14 01:11 simonwelsh

This works on the current version of HHVM. https://3v4l.org/BAnGT

Orvid avatar Apr 29 '16 19:04 Orvid