- From: Michael A. Peters <mpeters@mac.com>
- Date: Mon, 08 Jun 2009 08:29:45 -0700
- To: www-validator@w3.org
Andreas Prilop wrote: > On Sun, 7 Jun 2009, Michael A. Peters wrote: > >> I look at $_SERVER['HTTP_ACCEPT'] >> If it is not set or does not contain application/xhtml+xml >> then I send an html 4.01 page. > > What do you do with > > Accept: application/xhtml+xml; q=0 > > ? > I send the document as xhtml. here's the gist of it - 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; } } // W3C_Validator if (isset($_SERVER['HTTP_USER_AGENT'])) { $ua = $_SERVER['HTTP_USER_AGENT']; } else { $ua = 'unknown'; } if ($usexml == 0) { $test_ua = strtolower($ua); $pos = strpos($test_ua,'validator'); if($pos === false) { $usexml = 0; } else { $usexml = 1; } } $myxhtml = new DOMDocument("1.0","UTF-8"); $myxhtml->preserveWhiteSpace = false; $myxhtml->formatOutput = true; if ($usexml == 0) { $xmlstring = $htmldtd . "<html></html>"; } else { $xmlstring = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . $xhtmldtd . "<html></html>"; } $myxhtml->loadXML($xmlstring); $elements = $myxhtml->getElementsByTagName("html"); $xmlHtml = $elements->item(0); if ($usexml == 1) { $xmlHtml->setAttribute("xmlns","http://www.w3.org/1999/xhtml"); $xmlHtml->setAttributeNS('http://www.w3.org/XML/1998/namespace','xml:lang','en'); } -- $myxhtml is now a DOMDocument object with a doctype appropriate for the client. $xmlHtml is the root html node. children are added to it - and finally if ($usexml == 0) { print $myxhtml->saveHTML(); } else { print $myxhtml->saveXML(); } serves it.
Received on Monday, 8 June 2009 15:30:23 UTC