2013-05-13 92 views
2

我必須在xsl樣式表文件中包含內聯腳本,但問題在於xsl會嘗試轉換腳本並導致錯誤。在xsl中嵌入javascript

<script id="template-upload" type="text/x-tmpl"> 
      {% for (var i=0, file; file=o.files[i]; i++) { %} 
       <tr class="template-upload fade"> 
        <td class="preview"><span class="fade"></span></td> 
        <td class="name"><span>{%=file.name%}</span></td> 
        <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td> 
        {% if (file.error) { %} 
         <td class="error" colspan="2"><span class="label label-important">Error</span> {%=file.error%}</td> 
        {% } else if (o.files.valid && !i) { %} 
         <td> 
          <div class="progress progress-success progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="bar" style="width:0%;"></div></div> 
         </td> 
         <td class="start">{% if (!o.options.autoUpload) { %} 
          <button class="btn"> 
           <i class="icon-upload icon-white"></i> 
           <span>Start</span> 
          </button> 
         {% } %}</td> 
        {% } else { %} 
         <td colspan="2"></td> 
        {% } %} 
        <td class="cancel">{% if (!i) { %} 
         <button class="btn btn-danger"> 
          <i class="icon-ban-circle icon-white"></i> 
          <span>Cancel</span> 
         </button> 
        {% } %}</td> 
       </tr> 
      {% } %} 
      </script> 

有沒有辦法在xsl中使用內聯腳本?

回答

6

由於&符號(&),您的javascript部分無效xml。爲避免解釋 ,您可以使用CDATA和disable-output-escaping來避免轉義。

<xsl:template match="/"> 
     <script id="template-upload" type="text/x-tmpl"> 
      <xsl:text disable-output-escaping="yes" > 
      <![CDATA[ 
    ... 

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

非常感謝 – 2013-05-13 16:09:08