Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > CDATA in an xsl transformation

Reply
Thread Tools

CDATA in an xsl transformation

 
 
Tjerk Wolterink
Guest
Posts: n/a
 
      01-07-2005
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??
 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      01-07-2005
Tempore 18:12:59, die Friday 07 January 2005 AD, hinc in foro {comp.text.xml} scripsit Tjerk Wolterink <>:

> 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

<xslutput cdata-section-elements="script" method="xml"/>


regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
"Quot capita, tot sententiae" - Terentius , Phormio 454
 
Reply With Quote
 
 
 
 
Tjerk Wolterink
Guest
Posts: n/a
 
      01-08-2005
Joris Gillis wrote:
> Tempore 18:12:59, die Friday 07 January 2005 AD, hinc in foro
> {comp.text.xml} scripsit Tjerk Wolterink <>:
>
>> 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
>
> <xslutput 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?
 
Reply With Quote
 
Joris Gillis
Guest
Posts: n/a
 
      01-08-2005
Tempore 11:05:57, die Saturday 08 January 2005 AD, hinc in foro {comp.text.xml} scripsit Tjerk Wolterink <>:

> <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" xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xslutput 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,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Vincit omnia simplicitas
Keep it simple
 
Reply With Quote
 
Joris Gillis
Guest
Posts: n/a
 
      01-08-2005
> <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.

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Vincit omnia simplicitas
Keep it simple
 
Reply With Quote
 
Tjerk Wolterink
Guest
Posts: n/a
 
      01-08-2005
Joris Gillis wrote:

>> <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.
 
Reply With Quote
 
Joris Gillis
Guest
Posts: n/a
 
      01-08-2005
Tempore 11:49:41, die Saturday 08 January 2005 AD, hinc in foro {comp.text.xml} scripsit Joris Gillis <>:

>> <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" xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xslutput 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>

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Vincit omnia simplicitas
Keep it simple
 
Reply With Quote
 
Henri Sivonen
Guest
Posts: n/a
 
      01-08-2005
In article <>,
"Joris Gillis" <> wrote:

> 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.

--
Henri Sivonen

http://iki.fi/hsivonen/
Mozilla Web Author FAQ: http://mozilla.org/docs/web-developer/faq.html
 
Reply With Quote
 
Peter Flynn
Guest
Posts: n/a
 
      01-08-2005
Tjerk Wolterink wrote:

> 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>


FAQ.
http://www.ucc.ie/xml/#usecdata

///Peter
--
"The cat in the box is both a wave and a particle"
-- Terry Pratchett, introducing quantum physics in _The Authentic Cat_
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl cdata issue Lals XML 5 08-13-2006 09:56 PM
Can I un-CDATA my CDATA section and elaborate a transformation for the contained data? troppfigo@excite.it XML 3 03-06-2006 03:01 AM
Use output of XSL transformation as new XSL stylesheet barney.b@iname.com XML 0 01-16-2006 02:29 PM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
Extracting CDATA Text without CDATA Tags??? John Davison Java 1 07-06-2004 11:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57