Re: How about a saveXHTML for the DOM?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Thanks for the feedback Michael. I will look into your suggesstions.

Is there an option on the DOMDocument that we can set to not have saveXML() append 
 to the end of the tags?

This is normally the case if the html content was loaded using loadHTMLFile()

__
Raymond Irving

--- On Fri, 4/10/09, Michael A. Peters <mpeters@xxxxxxx> wrote:

From: Michael A. Peters <mpeters@xxxxxxx>
Subject: Re:  How about a saveXHTML for the DOM?
To: "Raymond Irving" <xwisdom@xxxxxxxxx>
Cc: "php-general@xxxxxxxxxxxxx" <php-general@xxxxxxxxxxxxx>
Date: Friday, April 10, 2009, 3:57 PM

Raymond Irving wrote:
> 
> 
> 
> --- On Wed, 4/8/09, Michael A. Peters <mpeters@xxxxxxx> wrote:
> 
>> From: Michael A. Peters <mpeters@xxxxxxx>
>> Subject: Re:  How about a saveXHTML for the DOM?
>> 
>> saveXML() already does what is needed to provide valid
>> xhtml output.
> 
> From my test it sometimes generate this like 
 at the end of the page elements. It also generate things like <script src="file.js" /> which does not work in most browsers.
> 

<script src="foo.js />

will work on any browser that advertises it accepts the xhtml+xml mime type.

Internet exploder does not and therefore internet exploder should be sent xhtml document. Send html documents to internet exploder.

<?php
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;
      }
   }
?>

That will result in $usexml being set to 1 for browsers (like Firefox and Opera) that accept xhtml+xml while setting it to 0 for browsers that do not accept xhtml+xml.

Then when you create your DOM -

<?php
$xhtmldtd="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\";>";
$htmldtd="<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\";>";

$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);
?>

You then add your children (IE head and body) to $xmlHtml.

When it comes time to serve the page -

<?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");
      }
   }
?>

makes sure the right header describing the content is set.
Send the page like this:

<?php
if ($usexml == 0) {
   print $myxhtml->saveHTML();
   } else {
   print $myxhtml->saveXML();
   }
?>

That way you only send xhtml to browsers that advertised to your server that they know what to do with the application/xhtml+xml mime type, those that did not advertise they know what to do get the text/html mime type and an html page.

It works extremely well in my test environment (my web app is not live yet) for sending valid xhtml 1.1 to browsers that properly handle xhtml and sending html 4.1 strict to browsers that do not properly handle xhtml+xml (IE internet exploder).

As far as I can tell - as long as you aren't using an extension to xhtml (like MathML) - the only problem where you can't produce both valid xhtml 1.1 and html 4.01 strict from the same php code would be if you were using the ruby (not programming language, the tags) elements that are allowed in xhtml 1.1 but not defined in HTML 4.01

No browser I have tested with that broadcasts it accepts xhtml+xml has ever had a problem with self closing script (or input or meta or ...) tags.

If you send xhtml with the text/plain mime type then the failure is on your end for violating the specification and self closing script tags are not legal.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux