Strange things when outputting XHTML using SimpleXML

I am trying to use SimpleXML to output a well-formed XHTML document. I do 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 creating the document, I want to output some pretty readable code, so I use the DOM module as follows:

$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 if this behavior is normal and how to disable it, if possible.

  • the presence of DOCTYPE causes $pbDoc->asXML()to add an unnecessary meta tag immediately after opening the tag <head>:

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    

    Why?

  • for some reason, the DOM module for me does not have backing code at all (although it works very well with another document, which is XML, not XHTML).

Can someone enlighten me about where I can be wrong, and how to get rid of these troubles?

+3
1

1. , , SimpleXML HTML, XML <html>

2. (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. ( ), , str_replace() XML. , :

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

?>
+1

Source: https://habr.com/ru/post/1712014/


All Articles