This is my code:
<?php
$data = <<<EOL
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<script type="text/javascript">
//<![CDATA[
var a = 123; // JS code
//]]>
</script>
</html>
EOL;
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = false;
$dom->loadXml($data);
echo '<pre>' . htmlspecialchars($dom->saveXML()) . '</pre>';
This is result:
<?xml version="1.0"?>
<!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">
<script type="text/javascript"><![CDATA[
//]]><![CDATA[
var a = 123; // JS code
//]]><![CDATA[
]]></script></html>
If and when I remove the DOCTYPE
notation from XML document, CDATA works properly and leading/trailing double slash is not turned into CDATA.
What is the problem here? Bug in libxml2? PHP version is 5.2.13 on Linux. Thanks.