0

I'm using Visual Studio 2008. I have an xsl document that contains text which I want to leave unprocessed durring the xslt transformation. For this purpose I use CDATA tag. But the text is somehow processed within CDATA. all < and > are converted to &lt; and &gt; Here is an example what I mean:

<![CDATA[
<p>What is going on?</p>
]]>

goes

&lt;p&gt;What is going on?&lt;/p&gt;

Actually my goal is to let the javascript like this:

<script type="text/javascript">
//<![CDATA[
    for(i = 0; i < 3; i++)
    document.write("XSLT will you marry me?");
//]]>
</script>

to go unprocessed in a XSL transformation. Can anyone help?

2 Answers 2

1
&lt;p&gt;What is going on?&lt;/p&gt; 

is equivalent to

<![CDATA[
<p>What is going on?</p>
]]>

A CDATA section is not a way to make portions of XML be left "unprocessed". It is just a convenience (alternative syntax) that can be useful for the editor of an XML document.


To leave JavaScript code wrapped in CDATA alone you have to add this line

<xsl:output method="html"/>

in your stylesheet. The HTML output method should not perform escaping for the content of the script and style elements as specified on http://w3.org/TR/xslt#section-HTML-Output-Method.

Sign up to request clarification or add additional context in comments.

3 Comments

:) Realy? So what have I to use in case I want to leave javascript unprocessed during a XSL transformation?
By "unprocessed", do you mean that < characters are left as they are (and not changed to &lt;)? If the stylesheet's output method is "html", then that is what happens. The html output method should not perform escaping for the content of the script and style elements. See w3.org/TR/xslt#section-HTML-Output-Method.
Yes that's exactly what I mean by 'uprocessed'. I did already read this w3.org sheet. That's why I post this question. Despite of it's written there that "The html output method should not perform escaping for the content of the script and style elements" my Visual Studio still escapes Html special symbols in <script> tag. Maybe I have to provide some particular settings but I don't see any options in my VS where I can do that. I updated my question.
1

output method didn't work for me (IE opening XML, that references XSLT), this did though, in my xslt file, within <head>, for instance :

<script type="text/javascript">
      <xsl:text disable-output-escaping ="yes">
        <![CDATA[
        var showLogs = function() {
          var logs = document.getElementsByClassName('log');
          for (var i=0; i < logs.length; i++) {
            logs[i].style.display='block' ;      
          }
        };
        ]]>
      </xsl:text>
</script>

Now, for instance, the < appears in xslt and the eventual html and showLogs() can be called.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.