470,831 Members | 1,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,831 developers. It's quick & easy.

Unexpected behavior when creating xhtml documents

Hey everybody,

The following has been posted before (2 years ago) but no response was
added. Therefor again, the following code for creating xhtml file:

<?php

error_reporting(6143);

$xmlns = "http://www.w3.org/1999/xhtml";
$lang = "en";

$xhtml_1_strict = new DomImplementation();

$dtd_xhtml_1_strict = $xhtml_1_strict->createDocumentType("html", "-//
W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/
xhtml1-strict.dtd");

$xhtml_1_strict_document = $xhtml_1_strict->createDocument("", "",
$dtd_xhtml_1_strict);
$xhtml_1_strict_document->encoding = "UTF-8";
$xhtml_1_strict_document->standalone = "no";

$html = $xhtml_1_strict_document->createElementNS($xmlns, "html");
$html->setAttribute("xml:lang", $lang);

$head = $xhtml_1_strict_document->createElement("head");
$html->appendChild($head);

$title = $xhtml_1_strict_document->createElement("title", "Testing the
DOM in PHP5");
$head->appendChild($title);

$base = $xhtml_1_strict_document->createElement("base");
$base->setAttribute("href", "http://test.beatter.com");
$head->appendChild($base);

$meta = $xhtml_1_strict_document->createElement("meta");
$meta->setAttribute("http-equiv", "Content-Type");
$meta->setAttribute("content", "application/xhtml+xml;UTF-8");
$head->appendChild($meta);

$body = $xhtml_1_strict_document->createElement("body");
$html->appendChild($body);

$xhtml_1_strict_document->appendChild($html);

header("Content-type: application/xhtml+xml;UTF-8");
echo($xhtml_1_strict_document->saveXML());

?>

the expected output would be:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Testing the DOM in PHP5</title><base href="http://
test.beatter.com" />
<meta http-equiv="Content-Type" content="application/xhtml
+xml;UTF-8" />
</head>
<body>
</body>
</html>

but the output is:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8" />
<title>Testing the DOM in PHP5</title><base href="http://
test.beatter.com" />
</head>
<body>
</body>
</html>

Why does php add this node by itself? Why can't I override that
behavior? Why does php change the order of outputing the elements (I'm
appending, not pre-pending to the head element)?

Thanks,

Marijn

Jun 6 '07 #1
4 1627
On Jun 6, 7:23 pm, Marijn <marijn.huizendv...@gmail.comwrote:
Hey everybody,

The following has been posted before (2 years ago) but no response was
added. Therefor again, the following code for creating xhtml file:

<?php

error_reporting(6143);

$xmlns = "http://www.w3.org/1999/xhtml";
$lang = "en";

$xhtml_1_strict = new DomImplementation();

$dtd_xhtml_1_strict = $xhtml_1_strict->createDocumentType("html", "-//
W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/
xhtml1-strict.dtd");

$xhtml_1_strict_document = $xhtml_1_strict->createDocument("", "",
$dtd_xhtml_1_strict);
$xhtml_1_strict_document->encoding = "UTF-8";
$xhtml_1_strict_document->standalone = "no";

$html = $xhtml_1_strict_document->createElementNS($xmlns, "html");
$html->setAttribute("xml:lang", $lang);

$head = $xhtml_1_strict_document->createElement("head");
$html->appendChild($head);

$title = $xhtml_1_strict_document->createElement("title", "Testing the
DOM in PHP5");
$head->appendChild($title);

$base = $xhtml_1_strict_document->createElement("base");
$base->setAttribute("href", "http://test.beatter.com");
$head->appendChild($base);

$meta = $xhtml_1_strict_document->createElement("meta");
$meta->setAttribute("http-equiv", "Content-Type");
$meta->setAttribute("content", "application/xhtml+xml;UTF-8");
$head->appendChild($meta);

$body = $xhtml_1_strict_document->createElement("body");
$html->appendChild($body);

$xhtml_1_strict_document->appendChild($html);

header("Content-type: application/xhtml+xml;UTF-8");
echo($xhtml_1_strict_document->saveXML());

?>

the expected output would be:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Testing the DOM in PHP5</title><base href="http://
test.beatter.com" />
<meta http-equiv="Content-Type" content="application/xhtml
+xml;UTF-8" />
</head>
<body>
</body>
</html>

but the output is:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8" />
<title>Testing the DOM in PHP5</title><base href="http://
test.beatter.com" />
</head>
<body>
</body>
</html>

Why does php add this node by itself? Why can't I override that
behavior? Why does php change the order of outputing the elements (I'm
appending, not pre-pending to the head element)?

Thanks,

Marijn
I've run into this problem, too. Unfortunately I've been unable to
find /any/ documentation on how/where/why this happens. It also seems
to depend on which version of the underlying XML libraries PHP is
using. I, too, would love to hear from anyone that has some insight
into this "feature."

Jun 7 '07 #2
On Jun 7, 3:27 pm, ZeldorBlat <zeldorb...@gmail.comwrote:
On Jun 6, 7:23 pm, Marijn <marijn.huizendv...@gmail.comwrote:
Hey everybody,
The following has been posted before (2 years ago) but no response was
added. Therefor again, the following code for creating xhtml file:
<?php
error_reporting(6143);
$xmlns = "http://www.w3.org/1999/xhtml";
$lang = "en";
$xhtml_1_strict = new DomImplementation();
$dtd_xhtml_1_strict = $xhtml_1_strict->createDocumentType("html", "-//
W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/
xhtml1-strict.dtd");
$xhtml_1_strict_document = $xhtml_1_strict->createDocument("", "",
$dtd_xhtml_1_strict);
$xhtml_1_strict_document->encoding = "UTF-8";
$xhtml_1_strict_document->standalone = "no";
$html = $xhtml_1_strict_document->createElementNS($xmlns, "html");
$html->setAttribute("xml:lang", $lang);
$head = $xhtml_1_strict_document->createElement("head");
$html->appendChild($head);
$title = $xhtml_1_strict_document->createElement("title", "Testing the
DOM in PHP5");
$head->appendChild($title);
$base = $xhtml_1_strict_document->createElement("base");
$base->setAttribute("href", "http://test.beatter.com");
$head->appendChild($base);
$meta = $xhtml_1_strict_document->createElement("meta");
$meta->setAttribute("http-equiv", "Content-Type");
$meta->setAttribute("content", "application/xhtml+xml;UTF-8");
$head->appendChild($meta);
$body = $xhtml_1_strict_document->createElement("body");
$html->appendChild($body);
$xhtml_1_strict_document->appendChild($html);
header("Content-type: application/xhtml+xml;UTF-8");
echo($xhtml_1_strict_document->saveXML());
?>
the expected output would be:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Testing the DOM in PHP5</title><base href="http://
test.beatter.com" />
<meta http-equiv="Content-Type" content="application/xhtml
+xml;UTF-8" />
</head>
<body>
</body>
</html>
but the output is:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8" />
<title>Testing the DOM in PHP5</title><base href="http://
test.beatter.com" />
</head>
<body>
</body>
</html>
Why does php add this node by itself? Why can't I override that
behavior? Why does php change the order of outputing the elements (I'm
appending, not pre-pending to the head element)?
Thanks,
Marijn

I've run into this problem, too. Unfortunately I've been unable to
find /any/ documentation on how/where/why this happens. It also seems
to depend on which version of the underlying XML libraries PHP is
using. I, too, would love to hear from anyone that has some insight
into this "feature."
unfortunately DOM/XML documentation is very poor by itself. Especially
concerning this topic. Is the problem solvable by installing another
DOM/XML Library?

Jun 8 '07 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Marijn wrote:
unfortunately DOM/XML documentation is very poor by itself. Especially
concerning this topic. Is the problem solvable by installing another
DOM/XML Library?
Hi, you are welcome to submit specific concerns and clarifications to
the PHP documentation team at news://news.php.net/php.doc

The DOM extension is built off of libxml, so quirks like these are
likely bugs in libxml and not DOM. Running your snippet on PHP 5.0.3,
with libxml 2.6.11, I get (slightly formatted for readability):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//
W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/
xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Testing the DOM in PHP5</title>
<base href="http://test.beatter.com"/>
<meta http-equiv="Content-Type" content="application/xhtml+xml;UTF-8"/>
</head>
<body/>
</html>

Which is the expected behavior. As you may already know, 5.2.3 and
2.6.28 are the latest versions for PHP and libxml respectively, so you
may be running on an ancient version of libxml. If this is the case,
please upgrade or recompile the extension with a newer version of libxml.

- --
Edward Z. Yang GnuPG: 0x869C48DA
HTML Purifier <htmlpurifier.org Anti-XSS HTML Filter
[[ 3FA8 E9A9 7385 B691 A6FC B3CB A933 BE7D 869C 48DA ]]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGaK6XqTO+fYacSNoRAj8nAJ94DFXP2vL53rYKZE8oZp qr8JlpbACfWQgX
5kpzr7VMP7744olgYN8T9/M=
=ESgZ
-----END PGP SIGNATURE-----
Jun 8 '07 #4
Hi, you are welcome to submit specific concerns and clarifications to
the PHP documentation team at news://news.php.net/php.doc
Might just do that. Will need to gain a little bit more self
confidence about my code thouht ;)
Which is the expected behavior. As you may already know, 5.2.3 and
2.6.28 are the latest versions for PHP and libxml respectively, so you
may be running on an ancient version of libxml. If this is the case,
please upgrade or recompile the extension with a newer version of libxml.
Looks like it is time I talked to my hosting provider <http://
mediatemple.net>. Seem to be running 2.6.16 for libxml and php 5.2.2

Thanks in advance

Jun 8 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Mikko Ohtamaa | last post: by
16 posts views Thread by Mcginkel | last post: by
6 posts views Thread by Samuel M. Smith | last post: by
17 posts views Thread by Christoph Schneegans | last post: by
11 posts views Thread by Kidogg | last post: by
14 posts views Thread by Stanimir Stamenkov | last post: by
13 posts views Thread by =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.