Howto: Remove XML, doctype and top-level namespace declarations from DOMDocument

[email protected] (Victor Engmark) Sat, 29 Mar 2008 21:10:58 +0100
Newsgroups php.xml.dev
Message-ID <[email protected]>
Hi all,

This silly little problem took quite a while to figure out without using 
any regular expression hacks, so here goes (example using XHTML namespace):

// Initialize
$doc = new DOMDocument;
$doc->load('somefile.xhtml');

// Remove XHTML namespace declaration
$doc->documentElement->removeAttributeNS('http://www.w3.org/1999/xhtml',
                                          '');

// Get the XML contents without XML and doctype declarations
$text => $doc->saveXML($doc->documentElement))

// Now you can use this to e.g. process several XML documents in XSLT
$proc->setParameter(NULL,
                     array_merge($other_params, array('text' => $text));

PS: I tried to submit this as a PHP documentation note, but my IP is 
blocked for some reason.

PPS: Coming soon to a website near you ;)

-- 
Victor Engmark