Why libxml2 quotes start of double slash in CDATA with javascript

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 the 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 notation DOCTYPE

from the XML document, CDATA works correctly and the leading / trailing double slash does not convert to CDATA.

What is the problem? Error in libxml2? PHP version is 5.2.13 for Linux. Thanks.

+2


a source to share


2 answers


I am running libxml 2.7.3 with PHP 5.2.11 on OS X.

Not comparing apples to apples, but maybe it will help you.

When I run your code (and add a PHP closing tag, here is my output.)

<?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[
    var a = 123; // JS code
    //]]>
    </script></html>

      



It seems like it looks right the way you want it to. Perhaps the version numbers will help you figure it out ... I'm running an older version of PHP5 (Mump, by the way, that's why I didn't compile it myself.)

Hope this helps you find the answer.

Be careful!

+1


a source


You must be using the HTML version of the API.
Use loadHTML

and instead saveHTML

.



0


a source







All Articles