Forum Moderators: coopster

Message Too Old, No Replies

domdocument

how to gen doctype in server-side

         

nyteshade

12:44 pm on Dec 18, 2011 (gmt 0)

10+ Year Member



Yesterday, by accident, I discovered the DOMDocument class and it was like discovering a new dimension, 'cause it was! How did I miss this over the past two years of learning to do the whole 'roundtrip' programming thing!

The introductory PHP books I've read never mentioned this world. I felt cheated. At the same time I felt grateful for reading Keith's DOM Scripting because as I browsed over the PHP DOMDocument manual... I understood what I was seeing, wow.

Initially I was simply searching for a way to refactor some SSI files to dynamically swap my 3WC validation links. My thoughts were to check the DOCTYPE of the current document and then select the respective link.

My question is, and I didn't see a DOMDocument method in the manual *wink*, is there a standard way to built the

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


from within an XHTML/PHP document? Humbly submitted, thanks.

nyteshade

11:33 am on Dec 20, 2011 (gmt 0)

10+ Year Member



Sorry, thought I had a solution, was wrong.

nyteshade

5:53 pm on Dec 20, 2011 (gmt 0)

10+ Year Member



I got it! albeit, with some serious head-scratching.


// from the php manual
$impl = new DOMImplementation();
$doctype = $impl->createDocumentType("html",
"-//W3C//DTD HTML 4.01//EN",
"http://www.w3.org/TR/html4/strict.dtd");
$document = $impl->createDocument(null, null, $doctype);
echo $document->saveXML();

This creates the doctype at the top of the document.

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">


And when I want to select the appropriate 3WC validation button, I can use:


//wooo hooo
$pubId = $doctype->publicId;
if($pubId == '-//W3C//DTD HTML 4.01//EN' )
include_once("navphphtml401strict.inc");


And it validates!

g1smd

6:57 pm on Dec 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm going to need to go read that again, but it sure looks useful.

Thanks for sharing!

nyteshade

9:47 pm on Dec 20, 2011 (gmt 0)

10+ Year Member



No problem g1smd. That snippet will not work with:

<?php namespace testProject;


preceeding it. Code must change, if you discover how, share please. Thx.

nyteshade

2:55 pm on Dec 24, 2011 (gmt 0)

10+ Year Member



I am using the following to call the respective SSI for the
<!DOCTYPE...
of the current document. I'm not the DOMImplementation expert; this is in hopes that one will expand on it or maybe some other novice may use it as I do. At the top of my.php files I put:


<?php
// Create document
$impl = new DOMImplementation();
$doctype = $impl->createDocumentType("html",
"-//W3C//DTD XHTML 1.0 Strict//EN",
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
$document = $impl->createDocument(null, null, $doctype);
//this line creates a closing tag for html element which is why i have hard-coded
//the namespace attribute in the opening tag below
//$document = $impl->createDocument('http://www.w3.org/1999/xhtml', 'html', $doctype);
echo $document->saveXML();
//
echo "<html xmlns='http://www.w3.org/1999/xhtml'>";
[...]


In the same current document I have another
includes
that contains:


$pubId = $doctype->publicId;
switch($pubId) {
case '-//W3C//DTD HTML 4.01//EN' :
include_once('valhtml401strict.inc');
break;
case '-//W3C//DTD XHTML 1.0 Strict//EN' :
include_once('valxhtml10strict.inc');
break;
case null ://html5
include_once('valxhtml10strict.inc');
break;
default:
include_once('valxhtml10strict.inc');
break;
}


I'll revisit DOMImplementtion in the near future with hopes of being more useful here. Peace.