funkyres Posted February 8, 2009 Share Posted February 8, 2009 Hi - I'm playing with the xml php module in php 5.2.5 It's working swell and producing gorgeous output, I just can't figure out how to set the document type. This is how I'm creating the document - <?php $myxhtml = new DOMDocument("1.0","UTF-8"); $myxhtml->formatOutput = true; $xmlHtml = $myxhtml->createElement("html"); $xmlHtml->setAttribute("xmlns","http://www.w3.org/1999/xhtml"); $xmlHtml->setAttribute("xml:lang","en"); // add head and body child elements and append stuff header("Content-Type: application/xhtml+xml; charset=utf-8"); print $myxhtml->saveXML(); ?> It works just swell - except for the lack of a DOCTYPE declaration, and I can't seem to figure out how to set it. Any hints? Quote Link to comment Share on other sites More sharing options...
printf Posted February 8, 2009 Share Posted February 8, 2009 PHP has no way of doing that through the DOMDocumentType object as of yet, but looking at the road map, it is on the TO DO list. Using PHP 4 and the old DOM XML extension you could use domxml_open_mem() and just input the raw doctype, but you cannot do that with the PHP 5 DOM XML extension because DOMDocumentType object is only partly implemented, (read only)... Quote Link to comment Share on other sites More sharing options...
funkyres Posted February 8, 2009 Author Share Posted February 8, 2009 So basically for now I have to send the output of $myxhtml->saveXML() to a variable, add it manually, and print the variable. Quote Link to comment Share on other sites More sharing options...
funkyres Posted February 8, 2009 Author Share Posted February 8, 2009 Just as a note - using DOMDocument is wicked - requires a wee bit more code, but it looks cleaner, and since you don't send output until the document is built, you don't have to send the header until the document is built, so flaws in your code are easier to see in the browser (rather than the xml parsing error that firefox throws if xml mime type is sent before the error is thrown). So it is worth hacking the doctype in manually until they have it implemented, at least to me. Quote Link to comment Share on other sites More sharing options...
funkyres Posted February 8, 2009 Author Share Posted February 8, 2009 Here's my solution - I call it xhtml.inc and require it at the beginning of my code <?php function sendxhtmlheader($usexml) { if ($usexml == 1) { header("Content-Type: application/xhtml+xml; charset=utf-8"); } else { header("Content-type: text/html; charset=utf-8"); } } function sendpage($page,$usexml) { $xhtmldtd="\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"; $bar=preg_replace('/\n/',$xhtmldtd,$page,1); sendxhtmlheader($usexml); print($bar); } if (! isset($usexml)) { $usexml=1; } if ($usexml == 1) { if (isset( $_SERVER['HTTP_ACCEPT'] )) { if(! strpos( $_SERVER['HTTP_ACCEPT'], "application/xhtml+xml" ) ) { $usexml=0; } } else { $usexml=0; } } $myxhtml = new DOMDocument("1.0","UTF-8"); $myxhtml->preserveWhiteSpace = false; $myxhtml->formatOutput = true; $xmlHtml = $myxhtml->createElement("html"); $xmlHtml->setAttribute("xmlns","http://www.w3.org/1999/xhtml"); $xmlHtml->setAttribute("xml:lang","en"); ?> When I've built the document - <?php $foo=$myxhtml->saveXML(); sendpage($foo,$usexml) ?> The results W3C validate, so it works well enough for now. Hopefully in the future that hack won't be necessary. //The check to see if the browser accepts xml+xhtml is for IE - which currently doesn't. The check to see if HTTP_ACCEPT is set is because some search spiders don't send it, thus throwing an error if I assume it does. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.