| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
  [2013-07-03 19:41 UTC] baileyp at comcast dot net
 Description:
------------
When trying to save the contents of a HTML document's body without the surrounding 
<body></body> tags I had the idea of appending all of the childNodes to a 
DOMDocumentFragment and then passing it as the first argument ($node) to 
DOMDocument::saveHTML(). However, I discovered a bug in doing so. Saving returns 
invalid markup with empty tags (<></>) surrounding the content.
I have reproduced this is both 5.4.11 (with libxml 2.7.8) and 5.5.0 (with libxml 
2.7.6).
Test script:
---------------
<?php
$dom = new DOMDocument();
$frag1 = $dom->createDocumentFragment();
var_dump($dom->saveHTML($frag1));
$frag2 = $dom->createDocumentFragment();
$frag2->appendChild($dom->createElement('div'));
var_dump($dom->saveHTML($frag2));
Expected result:
----------------
string(0) ""
string(11) "<div></div>"
Actual result:
--------------
string(5) "<></>"
string(16) "<><div></div></>"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             | 
    |||||||||||||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 05:00:01 2025 UTC | 
Reproduced this with latest 5.5 branch. $doc = new DOMDocument(); $frag = $doc -> createDocumentFragment(); $frag -> appendXML('yyy'); echo $doc -> saveHTML($frag); // outputs <>yyy workaround is to loop through childnodes of the domdocumentfragment and savehtml to each.