在XSL中包含带有Curly Brackets {}的内联JavaScript

O.O*_*.O. 15 javascript xml xslt

我试图通过XSL文件将XML转换为HTML.不幸的是,它不允许我使用JavaScript卷曲括号{}.以下是一个简单的例子,但我的实际代码要大得多.

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
    <xsl:output media-type="text/html; charset=UTF-8" encoding="UTF-8"/>
    <xsl:template match='/'>
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <title> Page Title</title>
            </head>
            <body onload="javascript: if(confirm('Are you sure?')) { return true;} else { return false;}">
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

Visual Studio给出了以下错误:

Expected token '}', found 'true'.  ...firm('Are you sure?')) { return  -->true<-- ;} else { return false;}  
Run Code Online (Sandbox Code Playgroud)

有没有办法在XSL中包含内联JavaScript?我知道你可以<![CDATA[ ]]>用来逃避Javascript块.但是如何逃避内联JavaScript呢?我的实际代码太大,无法将所有内联JavaScript重新编写为脚本块.

Mic*_*Kay 19

文字结果元素属性中的大括号用于"属性值模板",包含XPath表达式.因此,如果要生成包含curlies的属性,则需要通过加倍来转义它们:

<body onload="javascript: if(confirm('Are you sure?')) {{ return true;}} else {{ return false;}}">
Run Code Online (Sandbox Code Playgroud)

但是,在onXXX属性中包含大块Javascript可能最好避免:相反,只需要在其中定义的代码中包含一个函数调用<script>.


Eko*_*tin 5

试试这个,它不需要转义:

<body>
  <xsl:attribute name="onload">
    javascript: if(confirm('Are you sure?')) { return true;} else { return false;}
  </xsl:attribute>
</body>
Run Code Online (Sandbox Code Playgroud)