CDATA in an xsl transformation

  • Thread starter Tjerk Wolterink
  • Start date
T

Tjerk Wolterink

Hello all,

I've a problem:

i've an xsl file that has a template that contains
the following:

<script type="text/javascript">
<![CDATA[

a long javascript with < > characters, like:
for(var i=0;i<xmlDataFunctions.length;i++) {
}

]]>>
</script>


Ok when this is transformed i get the following:

<script type="text/javascript">

a long javascript with &lt; &gt; characters, like:
for(var i=0;i&lt;xmlDataFunctions.length;i++) {
}

</script>


But i dont want this, because javascript does not now &lt; and &gt;
So i used:

<script type="text/javascript"><xsl:text disable-output-escaping="yes"><![CDATA[

a long javascript with &lt; &gt; characters, like:
for(var i=0;i&lt;xmlDataFunctions.length;i++) {
}

]]></xsl:text></script>

Ok now i get:

<script type="text/javascript">

a long javascript with < > characters, like:
for(var i=0;i<xmlDataFunctions.length;i++) {
}

</script>

But because it is all part of an web-application i want to
get valid xhtml. This is not valid.So what i want is this as output:


<script type="text/javascript">
<![CDATA[

a long javascript with < > characters, like:
for(var i=0;i<xmlDataFunctions.length;i++) {
}

]]>
</script>



How do i do that with xslt??
 
J

Joris Gillis

Tempore 18:12:59 said:
But because it is all part of an web-application i want to
get valid xhtml. This is not valid.So what i want is this as output:
<script type="text/javascript">
<![CDATA[
a long javascript with < > characters, like:
for(var i=0;i<xmlDataFunctions.length;i++) {
}
]]>
</script>

Just specify 'cdata-section-elements' attribute

<xsl:eek:utput cdata-section-elements="script" method="xml"/>


regards,
 
T

Tjerk Wolterink

Joris said:
Tempore 18:12:59, die Friday 07 January 2005 AD, hinc in foro
{comp.text.xml} scripsit Tjerk Wolterink said:
But because it is all part of an web-application i want to
get valid xhtml. This is not valid.So what i want is this as output:
<script type="text/javascript">
<![CDATA[
a long javascript with < > characters, like:
for(var i=0;i<xmlDataFunctions.length;i++) {
}
]]>
</script>

Just specify 'cdata-section-elements' attribute

<xsl:eek:utput cdata-section-elements="script" method="xml"/>


regards,

ok i understand, but now i want the output to be:


<script type="text/javascript">
//<![CDATA[
a long javascript with < > characters, like:
for(var i=0;i<xmlDataFunctions.length;i++) {
}
//]]>
</script>


Those // are javascript comments, these are neccesary because some old browsers
do support javascript but do not support xml, so i have to comment them out with //
..

How would you do that?
 
J

Joris Gillis

Tempore 11:05:57 said:
<script type="text/javascript">
//<![CDATA[
a long javascript with < > characters, like:
for(var i=0;i<xmlDataFunctions.length;i++) {
}
//]]>
</script>

Those // are javascript comments, these are neccesary because some old browsers
do support javascript but do not support xml, so i have to comment them out with //

How would you do that?

To the best of my knowledge, XSLT1.0 does not provide a way to create a CDATA section from only a part of a text node.

I'm not really convinced that there would ever be need of such output, but if you really need it and don't mind dirty hacks, you could use something like this:

<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="html"/>

<xsl:template match="script">

<script type="text/javascript">
<![CDATA[
//<![CDATA[
a long javascript with < > characters, like:
for(var i=0;i<xmlDataFunctions.length;i++) {
}
//]]>
]]>
</script>

</xsl:template>

</xsl:stylesheet>


regards,
 
J

Joris Gillis

//<![CDATA[
a long javascript with < > characters, like:
for(var i=0;i<xmlDataFunctions.length;i++) {
}
//]]>
</script>

How would you do that?

If the output type of your XSL is set to 'xml' (to produce xhtml) , I think this problem is literally unsolvable.
 
T

Tjerk Wolterink

Joris said:
<script type="text/javascript">
//<![CDATA[
a long javascript with < > characters, like:
for(var i=0;i<xmlDataFunctions.length;i++) {
}
//]]>
</script>

How would you do that?


If the output type of your XSL is set to 'xml' (to produce xhtml) , I
think this problem is literally unsolvable.

Thanks for your time, i already thought it was difficult to solve, but
i want both be xhtml-valid and cross-browser, those things collide.

On solution is to put the javascript in a separe js file and include it using <script src=""/>
But its a really page specific javascript so therefore i want to include it inline.

But anyways, thanks.
 
J

Joris Gillis

Tempore 11:49:41 said:
<script type="text/javascript">
//<![CDATA[
a long javascript with < > characters, like:
for(var i=0;i<xmlDataFunctions.length;i++) {
}
//]]>
</script>

How would you do that?

If the output type of your XSL is set to 'xml' (to produce xhtml) , I think this problem is literally unsolvable.
Forget what I said, I was plain wrong...

This is a working - but rather ugly - solution:

<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="xml"/>

<xsl:template match="/">
<script type="text/javascript">
<xsl:text disable-output-escaping="yes">
<![CDATA[
//<![CDATA[
a long javascript with < > characters, like:
for(var i=0;i<xmlDataFunctions.length;i++) {
}
//]]>
]]>
</xsl:text>
</script>
</xsl:template>

</xsl:stylesheet>
 
H

Henri Sivonen

Joris Gillis said:
To the best of my knowledge, XSLT1.0 does not provide a way to create a CDATA
section from only a part of a text node.

I'm not really convinced that there would ever be need of such output,

There is no need when the output is subjected to proper XML processing.

Producing XHTML with XSLT and serving it as text/html without a
specialized Appendix C converter is a bad idea. Using inline style
sheets or scripts in such a situation is an even worse idea.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top