Re: [xsl] Troubles with libxslt in PHP again: CDATA in <script>

Subject: Re: [xsl] Troubles with libxslt in PHP again: CDATA in <script>
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Sat, 13 Oct 2007 18:20:31 +0200
Hi Martynas,

I think I have a couple of possible solutions for you. Googling a bit gave me some info on differences in output when using libxslt with PHP. Apparently, PHP adds some things to the serialized output tree (teriible!) and it doesn't do a very good job with it. I got most of the information, summarized below, from the annotated PHP manual here: http://php.oregonstate.edu/manual/en/function.xsl-xsltprocessor-transform-to-xml.php

1. Getting the output with PHP intervening, but without any other meddling, try this:

$proc->transformToURI($xml, 'php://output');

2. Getting (almost?) correct XHTML, use this:

   $domTranObj = $xslProcessor->transformToDoc($domXmlObj);
   $domHtmlText = $domTranObj->saveXML();

3. It is unclear whether that will or will not add the obnoxious CDATA into the script tags. If it still does so, you can try this classic (but ugly) workaround:

<xsl:text disable-output-escaping="yes">
&lt;script type="text/javascript"&gt; ... here comes the normal text of the scripts ....
&lt;/script&gt;
</xsl:text>


or the following, if you want to be more strict XHTML:

   <xsl:text disable-output-escaping="yes">
       &lt;script type="text/javascript">
       //&lt;![CDATA[
           ... here comes the normal text of the scripts ....
        // ]]>
        &lt;/script>
   </xsl:text>


Bottom line is: the culprit that is causing all these problems is in PHP 5, not with libxslt! The people writing the PHP serializer deliberately added the CDATA sections to the output stream and did so in a non-conformant non-compatible manner. According to this post, there has been a bug report, which has been closed, but I couldn't find it: http://www.abasketfulofpapayas.de/2007/08/problem-with-libxslt.html


HTH,
Cheers,
-- Abel Braaksma

PS: if the mailer, or the htmlizer of this list escapes the &lt;script..> as <script> note that I meant it to be &_lt;script..> in the example above, without the underscore.




Martynas Jusevicius wrote:
Oh, somehow I didn't think about it :) Here's what it says for 5.2.2 Win:
XSL 	enabled
libxslt Version 	1.1.17
libxslt compiled against libxml Version 	2.6.26
EXSLT 	enabled
libexslt Version 	0.8.13

and for 5.2.5 Linux:
XSL 	enabled
libxslt Version 	1.1.11
libxslt compiled against libxml Version 	2.6.16
EXSLT 	enabled
libexslt Version 	1.1.11

I wonder if they're using different versions for Win/Linux or did they
switch back to an older version...

<snip />

But didn't we agree back then that there was a bug (non-conformant
behaviour) in libxslt? :)
I think we might just as well banging our heads on a similar libxslt
"feature" here. The "Preventing CDATA output in XHTML" thread also
makes me think so. Please take a look at it if you haven't yet :)

I was part of that discussion (though briefly) and yes, I read it, but didn't find this information.


Current Thread