开发者

XLST collpases my <script> contents to one line as a result commenting out javascript!

UPDATE: Apologies all it was my http server stripping white space from from xslt before it was sent and was not aware of javascript comments (I should really del the question but cannot)开发者_开发问答.

My XSLT looks like:

<?xml version="1.0"?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml">

<xsl:output
  method="xml"
  indent="yes"/>

<xsl:template match="/root">
<html>
  <head>
    <title>Title</title>
    <script type="text/javascript"><![CDATA[
      // ©2011

      function function(){
        // do stuff...
      }

    ]]></script>
  </head>
  <body>
    <p> blah blah... </p>
  </body>
</html>
</xsl:template>

But the resulting xml is always collapsed to one line resulting in my javascript being commented out from the inital comment! This happens is all major browsers! Despite indent="yes"..


I couldn't repro this.

With all of the following nine XSLT processors (MSXML3 included -- so in IE you should get a good result):

  • MSXML (3, 4, 6)
  • .NET (XslCompiledTransform and XslTransform)
  • Altova (XML-SPY)
  • Saxon 6.5.4
  • Saxon 9.1.07 (XSLT 2.0 processor)
  • XQSharp (XSLT 2.0 processor)

when I perform the provided XSLT transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns="http://www.w3.org/1999/xhtml">
 <xsl:output   method="xml"   indent="yes"/>

  <xsl:template match="/root">
   <html>
    <head>
     <title>Title</title>
      <script type="text/javascript">
       <![CDATA[
              // ©2011
              function function()
              {
              // do stuff...
              }
         ]]>
      </script>
    </head>
    <body>
     <p> blah blah... </p>
    </body>
   </html>
  </xsl:template>
</xsl:stylesheet>

on this XML document (as no source XML document is provided in the question):

<root/>

the result is the same:

<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Title</title>
    <script type="text/javascript">

              // ©2011
              function function()
              {
              // do stuff...
              }

      </script>
  </head>
  <body>
    <p> blah blah... </p>
  </body>
</html>

Therefore, this is behavior of a buggy XSLT processor, not on the above list -- or there is some missing data in the question.


Try to wrap your javascript in <xsl:text> - Element instead of the CDATA Section. This will at least keep up your linebreaks you made inside. I'm not sure if CDATA stuff cares about linebreaks.

<script type="text/javascript"><xsl:text>
  // ©2011

  function function(){
    // do stuff...
  }

</xsl:text></script>

You also should try to to use method=html instead of xml because your generating html content.

In addition: i think the indent=yes stuff only applies to the indention of the XML Elements. I don't thin that mechanism cares about Text or CDATA Sections so you have to do the linebreaks yourself (as you already did in your javascript).


Three things to try:

  1. You're generating HTML, so why have output method XML?

  2. The CDATA will be used by the XML Parser on input for the XSLT engine, and not carried through (CDATAdoesn't appear in the XML info model).

  3. Would using xml:space='preserve' on the script element help?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜