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 14:45:06 +0200
Hi Martynus,

See my comments below



Martynas Jusevicius wrote:
Hi Abel,

well, on my machine I'm using PHP 5.2.2 which is giving me problems,
but my hosting uses 5.2.5 which seems to behave yet again differently.

That's PHP. I am interested in XSLT: what processor version do you use? Please follow the list guidelines and use the info you get in any subsequent posts so we can more easily help you: http://www.mulberrytech.com/xsl/xsl-list/processor-version.xsl (from: http://www.mulberrytech.com/xsl/xsl-list/#posting)


My template contained JavaScript based on Google Maps API's "Hello world":
http://www.google.com/maps/api_signup?url=http%3A%2F%2Flocalhost

That link doesn't work for me, it points to the google signup page.


So it looked like this:

<xsl:template name="map-js">

//<![CDATA[

var children = []]><xsl:call-template name="js-children-array"/><![CDATA[];
map.setCenter(locationLatLng, ]]><xsl:value-of select="$zoom-level"/><![CDATA[);

</xsl:template>
(sample truncated)

Ah, that's an interesting piece of XSLT! Thanks, that makes it easier to find a remedy. But first of all: I ran your code (after removing the pieces that gave a compile time error) and guess what: no sign of <![CDATA[ in the output *at all*! So my guess is still that your serializer has some funny tidbits, or for the oddest reason, your ISP's HTTP daemon adds them...

But rest assured, you can easily resolve it: just remove all the CDATA opening and closing tags. They have no business there. The only reason you should use them is for easier writing of &lt; and &amp;. Any other symbol can be left as is, including > and &apos; and &quot; (in all but a few situations).

To make this work, simply change the single "<" with &lt; or reverse the statement to read "children.length >= i". This will output these as &lt; or &gt;. And IIRC, modern browsers have no trouble with xml entities inside javascript when you set the doctype to XHTML.

(I hope I didn't introduce errors by cutting out not so important stuff).
And what I get as output is:

<script type="text/javascript"><![CDATA[
   //
   var children = [new Location("Denmark", 55.329144, 23.950195)];
   //
]]></script>


For now I switched to "html" output method as David suggested and all is working fine.
(sample output truncated)

Of course, if this works fine and you are content with the HTML (and you don't intend to parse it back as XML) then there's no worry about resolving this issue. Still I am wondering why you manage to get such a strange output, I hope you can update with information on your processor *and* please run your code from the commandline to be sure PHP or your ISP isn't messing things up, the output from the commandline will proof *very* interesting. I've send you the details of a download site for xsltproc in my previous mail.


Cheers, -- Abel Braaksma

Current Thread