Jump to content

[SOLVED] php-xml - how to set doctype ??


funkyres

Recommended Posts

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?

 

Link to comment
Share on other sites

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)...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.