why libxml2 quotes starting double slash in CDATA with javascript

Posted by Vincenzo on Stack Overflow See other posts from Stack Overflow or by Vincenzo
Published on 2010-04-29T04:56:58Z Indexed on 2010/05/16 17:00 UTC
Read the original article Hit count: 267

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about libxml2

Related posts about php