I'm trying to use SimpleXML to output a well-formed XHTML document. I'm doing it like this:
$pbDoc = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>'.$myTitle.'</title>
<!-- Other headers -->
</head>
</html>');
After I have created the document, I want to output pretty readable code, so I'm using DOM module like this:
$dom = new DOMDocument();
$dom->loadXML($pbDoc->asXML());
$dom->formatOutput = true;
echo $dom->saveXML();
Now, there are two strange things that bother me, and I wonder whether this behaviour is normal and how to disable it, if possible.
presence of DOCTYPE causes $pbDoc->asXML() to add an unneeded meta tag right after the opening <head> tag:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Why?
Could anyone enlighten me about where I may be wrong and how to get rid of these annoyances?
1. According to the best that I can search and guess, SimpleXML automatically spawns that tag into the HTML because the XML starts with <html>
2. You may want to try this (http://pt2.php.net/manual/en/domdocument.loadxml.php#73933):
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->loadXML($pbDoc->asXML());
$dom->formatOutput = true;
echo $dom->saveXML();
3. For the last thing (how to get rid of it), I guess you can do a simple str_replace() on the outputted XML. So your code would become:
<?php
// Define the $pbDoc here
$dom = new DOMDocument();
$dom->loadXML($pbDoc->asXML());
$dom->formatOutput = true;
echo str_replace('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />','',$dom->saveXML());
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With