[xml] set <script /> tag value when parsing XHTML (CDATA problem)



Hi,

I am using libxml2 to parse valid XHTML files and somehow edit them.
It works excellent, but I found one problem now. I cannot set value of
script tag.
I am parsing file with <script  type="text/javascript"
language="Javascript" /> and I'm trying to set it to my script. But
when I print out the final DomDocument, there is <script
type="text/javascript"
language="Javascript"><![CDATA[alert('ddd');]]></script>

No browser could parse it. I need something like this:

<script type="text/javascript" language="Javascript">
//<![CDATA[
alert('ddd');
//]]>
</script>

but it seems the libxml library is automatically adding CDATA section,
when it sees script tag (he knows it's CDATA from DTD).

Anyone has idea how to solve this problem?

Petr

--------------------------------
sample code (it's PHP using DOM [libxml2])

$html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\";>\n" .
"    \n" .
"<html xmlns=\"http://www.w3.org/1999/xhtml\"; xml:lang=\"en\" lang=\"en\">\n" .
 "<head>\n" .
"    <script  type=\"text/javascript\" language=\"Javascript\" />\n" .
        "</head>\n" .
        "<body>hello\n" .
        "</body>\n" .
        "</html>";
$dom = new DomDocument();
$dom->preserveWhiteSpace = true;
$dom->loadXML($html);
$params = $dom->getElementsByTagName('script');
foreach ($params as $param) {
    $dat  = $dom->createTextSection("\n\nalert('ddd');\n\n");
    $param->appendChild($dat);
}
echo $dom->saveXML();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]