[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Content of Script element getting wrapped by CDATA


Subject: Re: [xsl] Content of Script element getting wrapped by CDATA
From: "Darcy Parker" <darcyparker@xxxxxxxxx>
Date: Wed, 22 Oct 2008 17:55:44 -0400

I guess that makes sense... The XML parser reads the CDATA and encodes
everything enclosed before passing it to the XSLT processor.  So it
makes sense that the // moves inside.

I did a little research on the topic and believe your parser defaults
with the xml:output attribute @cdata-section-elements set to "script".
 As a result, all text() nodes inside it are output with CDATA.  In
order to get the // out front, you need to disable the escaping.
Here's what I came up with using saxon.  It seems to solve the
problem. I suspect it will work with the PHP XSLT processor.

Note: Many people frown upon using disable-output-escaping, but it is
the only way I can think of to get the // outside the CDATA section.

Also Note: I still recommend defining the javascript in your XML using
javascript commented CDATA start end end tags.  This way you're
confident the XML parser will be able to read your XML input.  But as
a result of this, the new XSLT template for script will output an
empty line with // at the beginning and end of the source.  You could
remove this with extra string processing... But because it doesn't
hurt, I decided to save the time in working out the necessary code to
remove these comments.

<!--XML Input Start-->
<?xml version="1.0"?>
<head>
     <title>XSLT</title>
     <script type="text/javascript">
          //<![CDATA[
          alert('No Error');
          //]]>
     </script>
</head>
<!--XML Input End-->

<!--XSLT Source Start-->
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    >

     <xsl:output method="xml"
                  cdata-section-elements="script style"
                  omit-xml-declaration="yes"
                  indent="no"
                  encoding="utf-8"
                  doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"

doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

     <xsl:template match="/">
          <html xml:lang="en" lang="en">
               <!-- Include Header-->
               <xsl:apply-templates select="/head" />
               <body>
               </body>
          </html>
     </xsl:template>

     <xsl:template match="script | style">
          <xsl:copy>
               <xsl:apply-templates select="@*"/>
               <!--Note: The &#xA; characters at the end of the CDATA
start tag and before the CDATA
                                             end tag are important
because the script text may begin and end with new lines.-->
               <xsl:value-of disable-output-escaping="yes"

select="concat('//&lt;![CDATA[&#xA;',text(),'&#xA;//]]&gt;')"/>
          </xsl:copy>
     </xsl:template>

     <!--Identity Transform-->
     <xsl:template match="element()">
          <xsl:copy>
               <xsl:apply-templates select="@*,node()"/>
          </xsl:copy>
     </xsl:template>
     <xsl:template
match="attribute()|text()|comment()|processing-instruction()">
          <xsl:copy/>
     </xsl:template>

</xsl:stylesheet>
<!--XSLT Source End-->

<!--XHTML Output Start-->
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" lang="en"><head>
     <title>XSLT</title>
     <script type="text/javascript">//<![CDATA[

          //
          alert('No Error');
          //

//]]></script>
</head><body/></html>
<!--XHTML Output End-->

Darcy
On Wed, Oct 22, 2008 at 2:10 PM, Joyce Babu <joyce@xxxxxxxxxxxxx> wrote:
> Thanks for the reply Darcy,
>
> But if I provide the input xml like that, I am getting
> <script type="text/javascript"><![CDATA[//
>
> alert('JOYCE');
> //]]></script>
>
> ie, the CDATA tag that I inserted within the xml disappears. And a new
> tag is added by the xslt processor, wrapping the content.
>
> On Wed, Oct 22, 2008 at 11:30 PM, Darcy Parker <darcyparker@xxxxxxxxx> wrote:
>>
>> Hi Joyce,
>>
>> Standard practice is to format the output like this:
>>
>> <script content="text/javascript">
>> // <![CDATA[
>>
>> javascript code here
>>
>> // ]]>
>> </script>
>>
>> Note: The // are interpreted as comments to javascript, so it won't
>> read the line with the CDATA opening and closing tags.  And it is
>> critical to have the CDATA in the XML because some characters in
>> javascript and not valid for text() nodes in XML.
>>
>> You could try writing your input XML like this.
>>
>> Darcy
>> On Wed, Oct 22, 2008 at 1:24 PM, Joyce Babu <joyce@xxxxxxxxxxxxx> wrote:
>> > I have PHP 5.2.6 and was trying to create my first XSL powered
>> > website. I am trying to build a template system. In this, the XML
>> > contains a head section, which is directly copied by the XSLT to the
>> > output document.
>> >
>> > In my XML data, I tried to include a script element, and the content
>> > of the tag is automatically wrapped in <![CDATA[ ]]> by the libxslt
>> > processor (default php processor). Hence I am getting a syntax error
>> > in my browser.
>> >
>> > Can anyone plz tell me how to fix it?
>> >
>> > Here is my xml code
>> >
>> > ----------------------------------------------------------
>> > <?xml version="1.0"?>
>> > <head>
>> >  <title>XSLT</title>
>> >  <link href="/manage/style.css" type="text/css" rel="stylesheet"/>
>> >  <script src="/js/jquery.js" type="text/javascript"/>
>> >  <script src="/js/tablesort.js" type="text/javascript"/>
>> >  <script type="text/javascript">
>> > alert('No Error');</script>
>> > </head>
>> > ----------------------------------------------------------
>> > XSL Code
>> > ----------------------------------------------------------
>> >
>> > <xsl:stylesheet version="1.0"
>> >  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>> >  xmlns:php="http://php.net/xsl"
>> >
>> >  xsl:extension-element-prefixes="php"
>> >  xmlns=""
>> >>
>> > <xsl:output method="xml" omit-xml-declaration="yes" standalone="no"
>> > indent="no" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0
>> > Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
>> >
>> > <xsl:template match="/">
>> >  <html xml:lang="en" lang="en">
>> >  <!-- Include Header-->
>> >  <xsl:copy-of select="/root/head" />
>> >  <body>
>> >  </body>
>> >  </html>
>> > </xsl:template>
>> > <!-- Identity Template -->
>> > <xsl:template match="@*|node()">
>> >  <xsl:copy-of select="." />
>> > </xsl:template>
>> >
>> > </xsl:stylesheet>
>> > ----------------------------------------------------------
>> >
>> > Output:
>> > ----------------------------------------------------------
>> > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
>> > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
>> > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
>> >  <head>
>> >    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
>> >    <title>XSLT</title>
>> >    <link href="/manage/style.css" type="text/css" rel="stylesheet" />
>> >
>> >    <script src="/js/jquery.js" type="text/javascript"></script>
>> >    <script src="/js/tablesort.js" type="text/javascript"></script>
>> >    <script type="text/javascript"><![CDATA[
>> > alert('No Error');]]></script>
>> >  </head>
>> >  <body>
>> >  </body>
>> >  </html>


Current Thread
Keywords